From 7ae1949517a41fbc46ca452f3fa40e7079273503 Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Mon, 13 Jan 2014 20:36:38 +1100 Subject: Select Random: add option to de-select also made metaball operator behave like the others. Path originally from Walid Shouman, with own edits. --- source/blender/editors/curve/editcurve.c | 31 +++++++++++++--------- source/blender/editors/mesh/editmesh_select.c | 29 +++++++++++--------- source/blender/editors/metaball/mball_edit.c | 29 +++++++++----------- source/blender/editors/object/object_lattice.c | 29 +++++++++++++------- source/blender/editors/object/object_select.c | 19 ++++--------- source/blender/windowmanager/WM_api.h | 1 + source/blender/windowmanager/intern/wm_operators.c | 22 ++++++++++++++- 7 files changed, 93 insertions(+), 67 deletions(-) (limited to 'source/blender') diff --git a/source/blender/editors/curve/editcurve.c b/source/blender/editors/curve/editcurve.c index 17d26f4d96c..c132a1a17b6 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 f13ca368b27..1944ce84be7 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 26008694b3c..04cd7c34010 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 c5e2aff0bc2..2469737c76c 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(<->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 fc83919144f..8e7e97aad86 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, "Percent", "Percentage of objects to select randomly", 0.f, 100.0f); - RNA_def_boolean(ot->srna, "extend", FALSE, "Extend Selection", "Extend selection instead of deselecting everything first"); + WM_operator_properties_select_action_simple(ot, SEL_SELECT); } diff --git a/source/blender/windowmanager/WM_api.h b/source/blender/windowmanager/WM_api.h index 8c4c41be84b..046152d07a1 100644 --- a/source/blender/windowmanager/WM_api.h +++ b/source/blender/windowmanager/WM_api.h @@ -249,6 +249,7 @@ void WM_operator_properties_mouse_select(struct wmOperatorType *ot); void WM_operator_properties_gesture_straightline(struct wmOperatorType *ot, int cursor); void WM_operator_properties_select_all(struct wmOperatorType *ot); void WM_operator_properties_select_action(struct wmOperatorType *ot, int default_action); +void WM_operator_properties_select_action_simple(struct wmOperatorType *ot, int default_action); bool WM_operator_check_ui_enabled(const struct bContext *C, const char *idname); wmOperator *WM_operator_last_redo(const struct bContext *C); diff --git a/source/blender/windowmanager/intern/wm_operators.c b/source/blender/windowmanager/intern/wm_operators.c index 72ccf4d1015..99437479df1 100644 --- a/source/blender/windowmanager/intern/wm_operators.c +++ b/source/blender/windowmanager/intern/wm_operators.c @@ -1216,6 +1216,12 @@ void WM_operator_properties_filesel(wmOperatorType *ot, int filter, short type, RNA_def_property_flag(prop, PROP_HIDDEN | PROP_SKIP_SAVE); } +static void wm_operator_properties_select_action_ex(wmOperatorType *ot, int default_action, + const EnumPropertyItem *select_actions) +{ + RNA_def_enum(ot->srna, "action", select_actions, default_action, "Action", "Selection action to execute"); +} + void WM_operator_properties_select_action(wmOperatorType *ot, int default_action) { static EnumPropertyItem select_actions[] = { @@ -1226,7 +1232,21 @@ void WM_operator_properties_select_action(wmOperatorType *ot, int default_action {0, NULL, 0, NULL, NULL} }; - RNA_def_enum(ot->srna, "action", select_actions, default_action, "Action", "Selection action to execute"); + wm_operator_properties_select_action_ex(ot, default_action, select_actions); +} + +/** + * only SELECT/DESELECT + */ +void WM_operator_properties_select_action_simple(wmOperatorType *ot, int default_action) +{ + static EnumPropertyItem select_actions[] = { + {SEL_SELECT, "SELECT", 0, "Select", "Select all elements"}, + {SEL_DESELECT, "DESELECT", 0, "Deselect", "Deselect all elements"}, + {0, NULL, 0, NULL, NULL} + }; + + wm_operator_properties_select_action_ex(ot, default_action, select_actions); } void WM_operator_properties_select_all(wmOperatorType *ot) -- cgit v1.2.3