Cleanup: use enum for undo step direction

The step direction was confusingly 1 for undo, -1 for redo.
This convention is from 2004 (562d6958cb).

Use enum for ed_undo.c for better readability.
This commit is contained in:
Campbell Barton
2021-01-14 09:49:22 +11:00
parent 73169628a1
commit c13383da41
2 changed files with 30 additions and 19 deletions

View File

@@ -67,7 +67,7 @@ int ED_undo_gpencil_step(bContext *C, int step, const char *name)
gpd_ptr = ED_gpencil_data_get_pointers(C, NULL);
if (step == 1) { /* undo */
if (step == -1) { /* undo */
// printf("\t\tGP - undo step\n");
if (cur_node->prev) {
if (!name || STREQ(cur_node->name, name)) {
@@ -76,7 +76,7 @@ int ED_undo_gpencil_step(bContext *C, int step, const char *name)
}
}
}
else if (step == -1) {
else if (step == 1) {
// printf("\t\tGP - redo step\n");
if (cur_node->next) {
if (!name || STREQ(cur_node->name, name)) {

View File

@@ -70,6 +70,17 @@
/** We only need this locally. */
static CLG_LogRef LOG = {"ed.undo"};
/**
* \warning Values are used in #ED_undo_gpencil_step,
* which should eventually be replaced with the undo-system.
*/
enum eUndoStepDir {
STEP_REDO = 1,
STEP_UNDO = -1,
/** Only used when the undo name is passed to #ed_undo_step_impl. */
STEP_NONE = 0,
};
/* -------------------------------------------------------------------- */
/** \name Generic Undo System Access
*
@@ -172,11 +183,11 @@ void ED_undo_push(bContext *C, const char *str)
* \note Also check #undo_history_exec in bottom if you change notifiers.
*/
static int ed_undo_step_impl(
bContext *C, int step, const char *undoname, int undo_index, ReportList *reports)
bContext *C, enum eUndoStepDir step, const char *undoname, int undo_index, ReportList *reports)
{
/* Mutually exclusives, ensure correct input. */
BLI_assert(((undoname || undo_index != -1) && !step) ||
(!(undoname || undo_index != -1) && step));
BLI_assert(((undoname || undo_index != -1) && (step == STEP_NONE)) ||
(!(undoname || undo_index != -1) && (step != STEP_NONE)));
CLOG_INFO(&LOG, 1, "name='%s', step=%d", undoname, step);
wmWindowManager *wm = CTX_wm_manager(C);
Scene *scene = CTX_data_scene(C);
@@ -197,7 +208,7 @@ static int ed_undo_step_impl(
/* TODO(campbell): undo_system: use undo system */
/* grease pencil can be can be used in plenty of spaces, so check it first */
if (ED_gpencil_session_active()) {
return ED_undo_gpencil_step(C, step, undoname);
return ED_undo_gpencil_step(C, (int)step, undoname);
}
if (area && (area->spacetype == SPACE_VIEW3D)) {
Object *obact = CTX_data_active_object(C);
@@ -207,7 +218,7 @@ static int ed_undo_step_impl(
}
UndoStep *step_data_from_name = NULL;
int step_for_callback = step;
enum eUndoStepDir step_for_callback = step;
if (undoname != NULL) {
step_data_from_name = BKE_undosys_step_find_by_name(wm->undo_stack, undoname);
if (step_data_from_name == NULL) {
@@ -218,14 +229,14 @@ static int ed_undo_step_impl(
/* Pointers match on redo. */
step_for_callback = (BLI_findindex(&wm->undo_stack->steps, step_data_from_name) <
BLI_findindex(&wm->undo_stack->steps, wm->undo_stack->step_active)) ?
1 :
-1;
STEP_UNDO :
STEP_REDO;
}
else if (undo_index != -1) {
step_for_callback = (undo_index <
BLI_findindex(&wm->undo_stack->steps, wm->undo_stack->step_active)) ?
1 :
-1;
STEP_UNDO :
STEP_REDO;
}
/* App-Handlers (pre). */
@@ -247,7 +258,7 @@ static int ed_undo_step_impl(
BKE_undosys_step_undo_from_index(wm->undo_stack, C, undo_index);
}
else {
if (step == 1) {
if (step == STEP_UNDO) {
BKE_undosys_step_undo(wm->undo_stack, C);
}
else {
@@ -310,19 +321,19 @@ static int ed_undo_step_impl(
return OPERATOR_FINISHED;
}
static int ed_undo_step_direction(bContext *C, int step, ReportList *reports)
static int ed_undo_step_direction(bContext *C, enum eUndoStepDir step, ReportList *reports)
{
return ed_undo_step_impl(C, step, NULL, -1, reports);
}
static int ed_undo_step_by_name(bContext *C, const char *undo_name, ReportList *reports)
{
return ed_undo_step_impl(C, 0, undo_name, -1, reports);
return ed_undo_step_impl(C, STEP_NONE, undo_name, -1, reports);
}
static int ed_undo_step_by_index(bContext *C, int index, ReportList *reports)
{
return ed_undo_step_impl(C, 0, NULL, index, reports);
return ed_undo_step_impl(C, STEP_NONE, NULL, index, reports);
}
void ED_undo_grouped_push(bContext *C, const char *str)
@@ -340,11 +351,11 @@ void ED_undo_grouped_push(bContext *C, const char *str)
void ED_undo_pop(bContext *C)
{
ed_undo_step_direction(C, 1, NULL);
ed_undo_step_direction(C, STEP_UNDO, NULL);
}
void ED_undo_redo(bContext *C)
{
ed_undo_step_direction(C, -1, NULL);
ed_undo_step_direction(C, STEP_REDO, NULL);
}
void ED_undo_push_op(bContext *C, wmOperator *op)
@@ -448,7 +459,7 @@ static int ed_undo_exec(bContext *C, wmOperator *op)
{
/* "last operator" should disappear, later we can tie this with undo stack nicer */
WM_operator_stack_clear(CTX_wm_manager(C));
int ret = ed_undo_step_direction(C, 1, op->reports);
int ret = ed_undo_step_direction(C, STEP_UNDO, op->reports);
if (ret & OPERATOR_FINISHED) {
/* Keep button under the cursor active. */
WM_event_add_mousemove(CTX_wm_window(C));
@@ -477,7 +488,7 @@ static int ed_undo_push_exec(bContext *C, wmOperator *op)
static int ed_redo_exec(bContext *C, wmOperator *op)
{
int ret = ed_undo_step_direction(C, -1, op->reports);
int ret = ed_undo_step_direction(C, STEP_REDO, op->reports);
if (ret & OPERATOR_FINISHED) {
/* Keep button under the cursor active. */
WM_event_add_mousemove(CTX_wm_window(C));