Commit: 7ae1949517a41fbc46ca452f3fa40e7079273503
Author: Campbell Barton
Date:   Mon Jan 13 20:36:38 2014 +1100
https://developer.blender.org/rB7ae1949517a41fbc46ca452f3fa40e7079273503

Select Random: add option to de-select

also made metaball operator behave like the others.

Path originally from Walid Shouman, with own edits.

===================================================================

M       source/blender/editors/curve/editcurve.c
M       source/blender/editors/mesh/editmesh_select.c
M       source/blender/editors/metaball/mball_edit.c
M       source/blender/editors/object/object_lattice.c
M       source/blender/editors/object/object_select.c
M       source/blender/windowmanager/WM_api.h
M       source/blender/windowmanager/intern/wm_operators.c

===================================================================

diff --git a/source/blender/editors/curve/editcurve.c 
b/source/blender/editors/curve/editcurve.c
index 17d26f4..c132a1a 100644
--- a/source/blender/editors/curve/editcurve.c
+++ b/source/blender/editors/curve/editcurve.c
@@ -5654,7 +5654,7 @@ void CURVE_OT_select_less(wmOperatorType *ot)
 
 /********************** select random *********************/
 
-static void selectrandom_curve(ListBase *editnurb, float randfac)
+static void curve_select_random(ListBase *editnurb, float randfac, bool select)
 {
        Nurb *nu;
        BezTriple *bezt;
@@ -5666,8 +5666,11 @@ static void selectrandom_curve(ListBase *editnurb, float 
randfac)
                        bezt = nu->bezt;
                        a = nu->pntsu;
                        while (a--) {
-                               if (BLI_frand() < randfac)
-                                       select_beztriple(bezt, SELECT, SELECT, 
VISIBLE);
+                               if (!bezt->hide) {
+                                       if (BLI_frand() < randfac) {
+                                               select_beztriple(bezt, select, 
SELECT, VISIBLE);
+                                       }
+                               }
                                bezt++;
                        }
                }
@@ -5676,24 +5679,26 @@ static void selectrandom_curve(ListBase *editnurb, 
float randfac)
                        a = nu->pntsu * nu->pntsv;
                        
                        while (a--) {
-                               if (BLI_frand() < randfac)
-                                       select_bpoint(bp, SELECT, SELECT, 
VISIBLE);
+                               if (!bp->hide) {
+                                       if (BLI_frand() < randfac) {
+                                               select_bpoint(bp, select, 
SELECT, VISIBLE);
+                                       }
+                               }
                                bp++;
                        }
                }
        }
 }
 
-static int select_random_exec(bContext *C, wmOperator *op)
+static int curve_select_random_exec(bContext *C, wmOperator *op)
 {
        Object *obedit = CTX_data_edit_object(C);
        ListBase *editnurb = object_editcurve_get(obedit);
+       const bool select = (RNA_enum_get(op->ptr, "action") == SEL_SELECT);
+       const float randfac = RNA_float_get(op->ptr, "percent") / 100.0f;
+
+       curve_select_random(editnurb, randfac, select);
 
-       if (!RNA_boolean_get(op->ptr, "extend"))
-               CU_deselect_all(obedit);
-       
-       selectrandom_curve(editnurb, RNA_float_get(op->ptr, "percent") / 
100.0f);
-       
        WM_event_add_notifier(C, NC_GEOM | ND_SELECT, obedit->data);
        
        return OPERATOR_FINISHED;
@@ -5707,7 +5712,7 @@ void CURVE_OT_select_random(wmOperatorType *ot)
        ot->description = "Randomly select some control points";
        
        /* api callbacks */
-       ot->exec = select_random_exec;
+       ot->exec = curve_select_random_exec;
        ot->poll = ED_operator_editsurfcurve;
 
        /* flags */
@@ -5715,7 +5720,7 @@ void CURVE_OT_select_random(wmOperatorType *ot)
 
        /* properties */
        RNA_def_float_percentage(ot->srna, "percent", 50.f, 0.0f, 100.0f, 
"Percent", "Percentage of elements to select randomly", 0.f, 100.0f);
-       RNA_def_boolean(ot->srna, "extend", false, "Extend", "Extend the 
selection");
+       WM_operator_properties_select_action_simple(ot, SEL_SELECT);
 }
 
 /********************* every nth number of point *******************/
diff --git a/source/blender/editors/mesh/editmesh_select.c 
b/source/blender/editors/mesh/editmesh_select.c
index f13ca36..1944ce8 100644
--- a/source/blender/editors/mesh/editmesh_select.c
+++ b/source/blender/editors/mesh/editmesh_select.c
@@ -2702,38 +2702,41 @@ static int edbm_select_random_exec(bContext *C, 
wmOperator *op)
 {
        Object *obedit = CTX_data_edit_object(C);
        BMEditMesh *em = BKE_editmesh_from_object(obedit);
-       BMVert *eve;
-       BMEdge *eed;
-       BMFace *efa;
-       BMIter iter;
+       const bool select = (RNA_enum_get(op->ptr, "action") == SEL_SELECT);
        const float randfac =  RNA_float_get(op->ptr, "percent") / 100.0f;
 
-       if (!RNA_boolean_get(op->ptr, "extend"))
-               EDBM_flag_disable_all(em, BM_ELEM_SELECT);
+       BMIter iter;
 
        if (em->selectmode & SCE_SELECT_VERTEX) {
+               BMVert *eve;
                BM_ITER_MESH (eve, &iter, em->bm, BM_VERTS_OF_MESH) {
                        if (!BM_elem_flag_test(eve, BM_ELEM_HIDDEN) && 
BLI_frand() < randfac) {
-                               BM_vert_select_set(em->bm, eve, true);
+                               BM_vert_select_set(em->bm, eve, select);
                        }
                }
-               EDBM_selectmode_flush(em);
        }
        else if (em->selectmode & SCE_SELECT_EDGE) {
+               BMEdge *eed;
                BM_ITER_MESH (eed, &iter, em->bm, BM_EDGES_OF_MESH) {
                        if (!BM_elem_flag_test(eed, BM_ELEM_HIDDEN) && 
BLI_frand() < randfac) {
-                               BM_edge_select_set(em->bm, eed, true);
+                               BM_edge_select_set(em->bm, eed, select);
                        }
                }
-               EDBM_selectmode_flush(em);
        }
        else {
+               BMFace *efa;
                BM_ITER_MESH (efa, &iter, em->bm, BM_FACES_OF_MESH) {
                        if (!BM_elem_flag_test(efa, BM_ELEM_HIDDEN) && 
BLI_frand() < randfac) {
-                               BM_face_select_set(em->bm, efa, true);
+                               BM_face_select_set(em->bm, efa, select);
                        }
                }
-               EDBM_selectmode_flush(em);
+       }
+
+       if (select) {
+               EDBM_select_flush(em);
+       }
+       else {
+               EDBM_deselect_flush(em);
        }
        
        WM_event_add_notifier(C, NC_GEOM | ND_SELECT, obedit->data);
@@ -2758,7 +2761,7 @@ void MESH_OT_select_random(wmOperatorType *ot)
        /* props */
        RNA_def_float_percentage(ot->srna, "percent", 50.f, 0.0f, 100.0f,
                                 "Percent", "Percentage of elements to select 
randomly", 0.f, 100.0f);
-       RNA_def_boolean(ot->srna, "extend", false, "Extend", "Extend the 
selection");
+       WM_operator_properties_select_action_simple(ot, SEL_SELECT);
 }
 
 static int edbm_select_ungrouped_poll(bContext *C)
diff --git a/source/blender/editors/metaball/mball_edit.c 
b/source/blender/editors/metaball/mball_edit.c
index 2600869..04cd7c3 100644
--- a/source/blender/editors/metaball/mball_edit.c
+++ b/source/blender/editors/metaball/mball_edit.c
@@ -373,20 +373,16 @@ static int select_random_metaelems_exec(bContext *C, 
wmOperator *op)
        Object *obedit = CTX_data_edit_object(C);
        MetaBall *mb = (MetaBall *)obedit->data;
        MetaElem *ml;
-       float percent = RNA_float_get(op->ptr, "percent");
+       const bool select = (RNA_enum_get(op->ptr, "action") == SEL_SELECT);
+       float percent = RNA_float_get(op->ptr, "percent") / 100.0f;
        
-       if (percent == 0.0f)
-               return OPERATOR_CANCELLED;
-       
-       ml = mb->editelems->first;
-       
-       /* Stupid version of random selection. Should be improved. */
-       while (ml) {
-               if (BLI_frand() < percent)
-                       ml->flag |= SELECT;
-               else
-                       ml->flag &= ~SELECT;
-               ml = ml->next;
+       for (ml = mb->editelems->first; ml; ml = ml->next) {
+               if (BLI_frand() < percent) {
+                       if (select)
+                               ml->flag |= SELECT;
+                       else
+                               ml->flag &= ~SELECT;
+               }
        }
        
        WM_event_add_notifier(C, NC_GEOM | ND_SELECT, mb);
@@ -398,21 +394,20 @@ static int select_random_metaelems_exec(bContext *C, 
wmOperator *op)
 void MBALL_OT_select_random_metaelems(struct wmOperatorType *ot)
 {
        /* identifiers */
-       ot->name = "Random...";
+       ot->name = "Select Random";
        ot->description = "Randomly select metaelements";
        ot->idname = "MBALL_OT_select_random_metaelems";
        
        /* callback functions */
        ot->exec = select_random_metaelems_exec;
-       ot->invoke = WM_operator_props_popup;
        ot->poll = ED_operator_editmball;
        
        /* flags */
        ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
        
        /* properties */
-       RNA_def_float_percentage(ot->srna, "percent", 0.5f, 0.0f, 1.0f, 
"Percent",
-                                "Percentage of metaelements to select 
randomly", 0.0001f, 1.0f);
+       RNA_def_float_percentage(ot->srna, "percent", 50.f, 0.0f, 100.0f, 
"Percent", "Percentage of elements to select randomly", 0.f, 100.0f);
+       WM_operator_properties_select_action_simple(ot, SEL_SELECT);
 }
 
 /***************************** Duplicate operator 
*****************************/
diff --git a/source/blender/editors/object/object_lattice.c 
b/source/blender/editors/object/object_lattice.c
index c5e2aff..2469737 100644
--- a/source/blender/editors/object/object_lattice.c
+++ b/source/blender/editors/object/object_lattice.c
@@ -189,6 +189,18 @@ void ED_lattice_transform(Lattice *lt, float mat[4][4])
        DAG_id_tag_update(&lt->id, 0);
 }
 
+static void bpoint_select_set(BPoint *bp, bool select)
+{
+       if (select) {
+               if (!bp->hide) {
+                       bp->f1 |= SELECT;
+               }
+       }
+       else {
+               bp->f1 &= ~SELECT;
+       }
+}
+
 /************************** Select Random Operator **********************/
 
 static int lattice_select_random_exec(bContext *C, wmOperator *op)
@@ -196,27 +208,26 @@ static int lattice_select_random_exec(bContext *C, 
wmOperator *op)
        Object *obedit = CTX_data_edit_object(C);
        Lattice *lt = ((Lattice *)obedit->data)->editlatt->latt;
        const float randfac = RNA_float_get(op->ptr, "percent") / 100.0f;
+       const bool select = (RNA_enum_get(op->ptr, "action") == SEL_SELECT);
+
        int tot;
        BPoint *bp;
 
-       if (!RNA_boolean_get(op->ptr, "extend")) {
-               ED_setflagsLatt(obedit, !SELECT);
-       }
-       else {
-               lt->actbp = LT_ACTBP_NONE;
-       }
-
        tot = lt->pntsu * lt->pntsv * lt->pntsw;
        bp = lt->def;
        while (tot--) {
                if (!bp->hide) {
                        if (BLI_frand() < randfac) {
-                               bp->f1 |= SELECT;
+                               bpoint_select_set(bp, select);
                        }
                }
                bp++;
        }
 
+       if (select == false) {
+               lt->actbp = LT_ACTBP_NONE;
+       }
+
        WM_event_add_notifier(C, NC_GEOM | ND_SELECT, obedit->data);
 
        return OPERATOR_FINISHED;
@@ -239,7 +250,7 @@ void LATTICE_OT_select_random(wmOperatorType *ot)
        /* props */
        RNA_def_float_percentage(ot->srna, "percent", 50.f, 0.0f, 100.0f,
                                 "Percent", "Percentage of elements to select 
randomly", 0.f, 100.0f);
-       RNA_def_boolean(ot->srna, "extend", false, "Extend", "Extend the 
selection");
+       WM_operator_properties_select_action_simple(ot, SEL_SELECT);
 }
 
 
diff --git a/source/blender/editors/object/object_select.c 
b/source/blender/editors/object/object_select.c
index fc83919..8e7e97a 100644
--- a/source/blender/editors/object/object_select.c
+++ b/source/blender/editors/object/object_select.c
@@ -1128,23 +1128,14 @@ void OBJECT_OT_select_mirror(wmOperatorType *ot)
 static int object_select_random_exec(bContext *C, wmOperator *op)
 {      
        float percent;
-       bool extend;
-       
-       extend = RNA_boolean_get(op->ptr, "extend");
-       
-       if (extend == false) {
-               CTX_DATA_BEGIN (C, Base *, base, visible_bases)
-               {
-                       ED_base_object_select(base, BA_DESELECT);
-               }
-               CTX_DATA_END;
-       }
+       const bool select = (RNA_enum_get(op->ptr, "action") == SEL_SELECT);
+
        percent = RNA_float_get(op->ptr, "percent") / 100.0f;
                
-       CTX_DATA_BEGIN (C, Base *, base, visible_bases)
+       CTX_DATA_BEGIN (C, Base *, base, selectable_bases)
        {
                if (BLI_frand() < percent) {
-                       ED_base_object_select(base, BA_SELECT);
+                       ED_base_object_select(base, select);
                }
        }
        CTX_DATA_END;
@@ -1171,5 +1162,5 @@ void OBJECT_OT_select_random(wmOperatorType *ot)
        
        /* properties */
        RNA_def_float_percentage(ot->srna, "percent", 50.f, 0.0f, 100.0f, "P

@@ Diff output truncated at 10240 characters. @@

_______________________________________________
Bf-blender-cvs mailing list
[email protected]
http://lists.blender.org/mailman/listinfo/bf-blender-cvs

Reply via email to