WM: add checker_interval utility functions

This commit is contained in:
2015-12-27 17:46:55 +11:00
parent b254905c38
commit f820c45534
5 changed files with 65 additions and 32 deletions

View File

@@ -1075,7 +1075,7 @@ void CURVE_OT_select_random(wmOperatorType *ot)
/********************* every nth number of point *******************/
static void select_nth_bezt(Nurb *nu, BezTriple *bezt, int nth, int skip, int offset)
static void select_nth_bezt(Nurb *nu, BezTriple *bezt, const struct CheckerIntervalParams *params)
{
int a, start;
@@ -1085,7 +1085,7 @@ static void select_nth_bezt(Nurb *nu, BezTriple *bezt, int nth, int skip, int of
while (a--) {
const int depth = abs(start - a);
if ((offset + depth) % (skip + nth) >= skip) {
if (WM_operator_properties_checker_interval_test(params, depth)) {
select_beztriple(bezt, DESELECT, SELECT, HIDDEN);
}
@@ -1093,7 +1093,7 @@ static void select_nth_bezt(Nurb *nu, BezTriple *bezt, int nth, int skip, int of
}
}
static void select_nth_bp(Nurb *nu, BPoint *bp, int nth, int skip, int offset)
static void select_nth_bp(Nurb *nu, BPoint *bp, const struct CheckerIntervalParams *params)
{
int a, startrow, startpnt;
int row, pnt;
@@ -1108,7 +1108,7 @@ static void select_nth_bp(Nurb *nu, BPoint *bp, int nth, int skip, int offset)
while (a--) {
const int depth = abs(pnt - startpnt) + abs(row - startrow);
if ((offset + depth) % (skip + nth) >= skip) {
if (WM_operator_properties_checker_interval_test(params, depth)) {
select_bpoint(bp, DESELECT, SELECT, HIDDEN);
}
@@ -1122,7 +1122,7 @@ static void select_nth_bp(Nurb *nu, BPoint *bp, int nth, int skip, int offset)
}
}
bool ED_curve_select_nth(Curve *cu, int nth, int skip, int offset)
static bool ed_curve_select_nth(Curve *cu, const struct CheckerIntervalParams *params)
{
Nurb *nu = NULL;
void *vert = NULL;
@@ -1131,10 +1131,10 @@ bool ED_curve_select_nth(Curve *cu, int nth, int skip, int offset)
return false;
if (nu->bezt) {
select_nth_bezt(nu, vert, nth, skip, offset);
select_nth_bezt(nu, vert, params);
}
else {
select_nth_bp(nu, vert, nth, skip, offset);
select_nth_bp(nu, vert, params);
}
return true;
@@ -1143,14 +1143,11 @@ bool ED_curve_select_nth(Curve *cu, int nth, int skip, int offset)
static int select_nth_exec(bContext *C, wmOperator *op)
{
Object *obedit = CTX_data_edit_object(C);
const int nth = RNA_int_get(op->ptr, "nth") - 1;
const int skip = RNA_int_get(op->ptr, "skip");
int offset = RNA_int_get(op->ptr, "offset");
struct CheckerIntervalParams op_params;
/* so input of offset zero ends up being (nth - 1) */
offset = mod_i(offset, nth + skip);
WM_operator_properties_checker_interval_from_op(op, &op_params);
if (!ED_curve_select_nth(obedit->data, nth, skip, offset)) {
if (!ed_curve_select_nth(obedit->data, &op_params)) {
if (obedit->type == OB_SURF) {
BKE_report(op->reports, RPT_ERROR, "Surface has not got active point");
}
@@ -1180,9 +1177,7 @@ void CURVE_OT_select_nth(wmOperatorType *ot)
/* flags */
ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
RNA_def_int(ot->srna, "nth", 2, 2, INT_MAX, "Nth Selection", "", 2, 100);
RNA_def_int(ot->srna, "skip", 1, 1, INT_MAX, "Skip", "", 1, 100);
RNA_def_int(ot->srna, "offset", 0, INT_MIN, INT_MAX, "Offset", "", -100, 100);
WM_operator_properties_checker_interval(ot, false);
}

View File

@@ -71,7 +71,6 @@ bool ED_curve_select_check(struct Curve *cu, struct EditNurb *editnurb);
void ED_curve_deselect_all(struct EditNurb *editnurb);
void ED_curve_select_all(struct EditNurb *editnurb);
void ED_curve_select_swap(struct EditNurb *editnurb, bool hide_handles);
bool ED_curve_select_nth(struct Curve *cu, int nth, int skip, int offset);
/* editfont.h */
void undo_push_font(struct bContext *C, const char *name);

View File

@@ -3162,7 +3162,9 @@ static bool bm_edge_is_select_isolated(BMEdge *e)
/* Walk all reachable elements of the same type as h_act in breadth-first
* order, starting from h_act. Deselects elements if the depth when they
* are reached is not a multiple of "nth". */
static void walker_deselect_nth(BMEditMesh *em, int nth, int skip, int offset, BMHeader *h_act)
static void walker_deselect_nth(
BMEditMesh *em, const struct CheckerIntervalParams *op_params,
BMHeader *h_act)
{
BMElem *ele;
BMesh *bm = em->bm;
@@ -3229,7 +3231,7 @@ static void walker_deselect_nth(BMEditMesh *em, int nth, int skip, int offset, B
if (!BM_elem_flag_test(ele, BM_ELEM_TAG)) {
/* Deselect elements that aren't at "nth" depth from active */
const int depth = BMW_current_depth(&walker) - 1;
if ((offset + depth) % (skip + nth) >= skip) {
if (WM_operator_properties_checker_interval_test(op_params, depth)) {
BM_elem_select_set(bm, ele, false);
}
BM_elem_flag_enable(ele, BM_ELEM_TAG);
@@ -3296,7 +3298,7 @@ static void deselect_nth_active(BMEditMesh *em, BMVert **r_eve, BMEdge **r_eed,
}
}
static bool edbm_deselect_nth(BMEditMesh *em, int nth, int skip, int offset)
static bool edbm_deselect_nth(BMEditMesh *em, const struct CheckerIntervalParams *op_params)
{
BMVert *v;
BMEdge *e;
@@ -3305,15 +3307,15 @@ static bool edbm_deselect_nth(BMEditMesh *em, int nth, int skip, int offset)
deselect_nth_active(em, &v, &e, &f);
if (v) {
walker_deselect_nth(em, nth, skip, offset, &v->head);
walker_deselect_nth(em, op_params, &v->head);
return true;
}
else if (e) {
walker_deselect_nth(em, nth, skip, offset, &e->head);
walker_deselect_nth(em, op_params, &e->head);
return true;
}
else if (f) {
walker_deselect_nth(em, nth, skip, offset, &f->head);
walker_deselect_nth(em, op_params, &f->head);
return true;
}
@@ -3324,14 +3326,11 @@ static int edbm_select_nth_exec(bContext *C, wmOperator *op)
{
Object *obedit = CTX_data_edit_object(C);
BMEditMesh *em = BKE_editmesh_from_object(obedit);
const int nth = RNA_int_get(op->ptr, "nth") - 1;
const int skip = RNA_int_get(op->ptr, "skip");
int offset = RNA_int_get(op->ptr, "offset");
struct CheckerIntervalParams op_params;
/* so input of offset zero ends up being (nth - 1) */
offset = mod_i(offset, nth + skip);
WM_operator_properties_checker_interval_from_op(op, &op_params);
if (edbm_deselect_nth(em, nth, skip, offset) == false) {
if (edbm_deselect_nth(em, &op_params) == false) {
BKE_report(op->reports, RPT_ERROR, "Mesh has no active vert/edge/face");
return OPERATOR_CANCELLED;
}
@@ -3356,9 +3355,7 @@ void MESH_OT_select_nth(wmOperatorType *ot)
/* flags */
ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
RNA_def_int(ot->srna, "nth", 2, 2, INT_MAX, "Nth Selection", "", 2, 100);
RNA_def_int(ot->srna, "skip", 1, 1, INT_MAX, "Skip", "", 1, 100);
RNA_def_int(ot->srna, "offset", 0, INT_MIN, INT_MAX, "Offset", "", -100, 100);
WM_operator_properties_checker_interval(ot, false);
}
void em_setup_viewcontext(bContext *C, ViewContext *vc)

View File

@@ -302,6 +302,16 @@ 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);
void WM_operator_properties_select_random(struct wmOperatorType *ot);
struct CheckerIntervalParams {
int nth; /* bypass when set to zero */
int skip;
int offset;
};
void WM_operator_properties_checker_interval(struct wmOperatorType *ot, bool nth_can_disable);
void WM_operator_properties_checker_interval_from_op(
struct wmOperator *op, struct CheckerIntervalParams *op_params);
bool WM_operator_properties_checker_interval_test(
const struct CheckerIntervalParams *op_params, int depth);
/* MOVE THIS SOMEWHERE ELSE */

View File

@@ -31,6 +31,7 @@
#include "DNA_space_types.h"
#include "BLI_rect.h"
#include "BLI_math_base.h"
#include "UI_resources.h"
@@ -248,3 +249,34 @@ void WM_operator_properties_gesture_straightline(wmOperatorType *ot, int cursor)
RNA_def_property_flag(prop, PROP_HIDDEN);
}
}
/**
* \param nth_can_disable: Enable if we want to be able to select no interval at all.
*/
void WM_operator_properties_checker_interval(wmOperatorType *ot, bool nth_can_disable)
{
const int nth_default = nth_can_disable ? 1 : 2;
const int nth_min = min_ii(nth_default, 2);
RNA_def_int(ot->srna, "nth", nth_default, nth_min, INT_MAX, "Nth Selection", "", nth_min, 100);
RNA_def_int(ot->srna, "skip", 1, 1, INT_MAX, "Skip", "", 1, 100);
RNA_def_int(ot->srna, "offset", 0, INT_MIN, INT_MAX, "Offset", "", -100, 100);
}
void WM_operator_properties_checker_interval_from_op(
struct wmOperator *op, struct CheckerIntervalParams *op_params)
{
const int nth = RNA_int_get(op->ptr, "nth") - 1;
const int skip = RNA_int_get(op->ptr, "skip");
int offset = RNA_int_get(op->ptr, "offset");
op_params->nth = nth;
op_params->skip = skip;
op_params->offset = mod_i(offset, nth + skip); /* so input of offset zero ends up being (nth - 1) */
}
bool WM_operator_properties_checker_interval_test(
const struct CheckerIntervalParams *op_params, int depth)
{
return ((op_params->nth == 0) ||
((op_params->offset + depth) % (op_params->skip + op_params->nth) >= op_params->skip));
}