diff --git a/grid_common/grid_protocol.h b/grid_common/grid_protocol.h index b0c7e8c4..249f0e6f 100644 --- a/grid_common/grid_protocol.h +++ b/grid_common/grid_protocol.h @@ -677,6 +677,9 @@ #define GRID_LUA_FNC_EP_BUTTON_MODE_short "bmo" #define GRID_LUA_FNC_EP_BUTTON_MODE_human "button_mode" +#define GRID_LUA_FNC_EP_BUTTON_STEP_short "bstp" +#define GRID_LUA_FNC_EP_BUTTON_STEP_human "button_step" + #define GRID_LUA_FNC_EP_BUTTON_ELAPSED_index 6 #define GRID_LUA_FNC_EP_BUTTON_ELAPSED_short "bel" #define GRID_LUA_FNC_EP_BUTTON_ELAPSED_human "button_elapsed_time" diff --git a/grid_common/grid_ui_button.c b/grid_common/grid_ui_button.c index 6825f07a..82b2a5f0 100644 --- a/grid_common/grid_ui_button.c +++ b/grid_common/grid_ui_button.c @@ -348,14 +348,18 @@ void grid_ui_button_store_input(struct grid_ui_button_state* state, uint16_t val last = clampi32(last, min, max); - int32_t new_value = last + (tmax - tmin) / steps; - - if (new_value > max) { - new_value = min; + int32_t step_size = (tmax - tmin) / steps; + int32_t new_value; + + if (step_size != 0) { + int32_t k = (last - tmin) / step_size; + new_value = tmin + (k + 1) * step_size; + } else { + new_value = last; } - if (new_value < min) { - new_value = max; + if (new_value > max || new_value < min) { + new_value = tmin; } int32_t value_range = tmax - tmin; diff --git a/grid_common/grid_ui_encoder.h b/grid_common/grid_ui_encoder.h index 194c5570..b6a4884d 100644 --- a/grid_common/grid_ui_encoder.h +++ b/grid_common/grid_ui_encoder.h @@ -75,8 +75,8 @@ extern const luaL_Reg GRID_LUA_E_INDEX_META[]; GRID_LUA_FNC_ASSIGN_META_PAR1_RET("gen", GRID_LUA_FNC_G_ELEMENTNAME_short) "," \ \ GRID_LUA_FNC_E_BUTTON_STEP_short " =function (self) " \ - "local steps, min, max, value = self:" GRID_LUA_FNC_B_BUTTON_MODE_short "(), self:" GRID_LUA_FNC_B_BUTTON_MIN_short "(), self:" GRID_LUA_FNC_B_BUTTON_MAX_short \ - "(), self:" GRID_LUA_FNC_B_BUTTON_VALUE_short "() " \ + "local steps, min, max, value = self:" GRID_LUA_FNC_E_BUTTON_MODE_short "(), self:" GRID_LUA_FNC_E_BUTTON_MIN_short "(), self:" GRID_LUA_FNC_E_BUTTON_MAX_short \ + "(), self:" GRID_LUA_FNC_E_BUTTON_VALUE_short "() " \ "if steps == 0 then return false end " \ "return value // ((max - min) // steps) " \ "end," \ diff --git a/grid_common/grid_ui_endless.h b/grid_common/grid_ui_endless.h index f2cdd111..ded0e23f 100644 --- a/grid_common/grid_ui_endless.h +++ b/grid_common/grid_ui_endless.h @@ -58,6 +58,13 @@ extern const luaL_Reg GRID_LUA_EP_INDEX_META[]; \ GRID_LUA_FNC_ASSIGN_META_PAR1_RET("gen", GRID_LUA_FNC_G_ELEMENTNAME_short) "," \ \ + GRID_LUA_FNC_EP_BUTTON_STEP_short " =function (self) " \ + "local steps, min, max, value = self:" GRID_LUA_FNC_EP_BUTTON_MODE_short "(), self:" GRID_LUA_FNC_EP_BUTTON_MIN_short "(), self:" GRID_LUA_FNC_EP_BUTTON_MAX_short \ + "(), self:" GRID_LUA_FNC_EP_BUTTON_VALUE_short "() " \ + "if steps == 0 then return false end " \ + "return value // ((max - min) // steps) " \ + "end," \ + \ "}}" #define GRID_ACTIONSTRING_ENDLESS_INIT ""