diff --git a/druid/base/button.lua b/druid/base/button.lua index 5eba022..5398ac6 100644 --- a/druid/base/button.lua +++ b/druid/base/button.lua @@ -6,8 +6,6 @@ -- Repeated tap local const = require("druid.const") -local settings = require("druid.settings") -local b_settings = settings.button local helper = require("druid.helper") local M = {} @@ -26,16 +24,16 @@ M.interest = { -- @tparam[opt] string event Button react event, const.ACTION_TOUCH by default function M.init(self, node, callback, params, anim_node, event) assert(callback, "Button should have callback. To block input on zone use blocker component") - self.style = self.druid_style.BUTTON or {} - + self.style = helper.get_style(self, "BUTTON") self.node = helper.node(node) + self.event = const.ACTION_TOUCH self.anim_node = anim_node and helper.node(anim_node) or self.node self.scale_from = gui.get_scale(self.anim_node) self.pos = gui.get_position(self.anim_node) self.callback = callback self.params = params - self.hover_anim = b_settings.IS_HOVER + self.hover_anim = self.style.IS_HOVER self.click_zone = nil end diff --git a/druid/base/progress.lua b/druid/base/progress.lua index 450f2a4..5a5778b 100644 --- a/druid/base/progress.lua +++ b/druid/base/progress.lua @@ -3,8 +3,6 @@ local const = require("druid.const") local helper = require("druid.helper") -local settings = require("druid.settings") -local p_settings = settings.progress local M = {} @@ -20,14 +18,12 @@ M.interest = { -- @tparam string key Progress bar direction (x or y) -- @tparam number init_value Initial value of progress bar function M.init(self, node, key, init_value) - if key ~= const.SIDE.X and key ~= const.SIDE.Y then - settings.log("progress component: key must be 'x' or 'y'. Passed:", key) - key = const.SIDE.X - end + assert(key == const.SIDE.X or const.SIDE.Y, "Progress bar key should be 'x' or 'y'") self.prop = hash("scale."..key) self.key = key + self.style = helper.get_style(self, "PROGRESS") self.node = helper.node(node) self.scale = gui.get_scale(self.node) self.size = gui.get_size(self.node) @@ -152,8 +148,8 @@ end function M.update(self, dt) if self.target then local prev_value = self.last_value - local step = math.abs(self.last_value - self.target) * (p_settings.SPEED*dt) - step = math.max(step, p_settings.MIN_DELTA) + local step = math.abs(self.last_value - self.target) * (self.style.SPEED*dt) + step = math.max(step, self.style.MIN_DELTA) self:set_to(helper.step(self.last_value, self.target, step)) if self.last_value == self.target then diff --git a/druid/base/scroll.lua b/druid/base/scroll.lua index e3e79ee..798d326 100644 --- a/druid/base/scroll.lua +++ b/druid/base/scroll.lua @@ -3,7 +3,6 @@ local helper = require("druid.helper") local const = require("druid.const") -local settings = require("druid.settings").scroll local M = {} @@ -18,10 +17,12 @@ M.current_scroll = nil function M.init(self, scroll_parent, input_zone, border) + self.style = helper.get_style(self, "SCROLL") self.node = helper.node(scroll_parent) self.input_zone = helper.node(input_zone) + self.zone_size = gui.get_size(self.input_zone) - self.soft_size = settings.SOFT_ZONE_SIZE + self.soft_size = self.style.SOFT_ZONE_SIZE -- Distance from node to node's center local offset = helper.get_pivot_offset(gui.get_pivot(self.input_zone)) @@ -61,16 +62,16 @@ local function check_soft_target(self) local b = self.border if t.y < b.y then - t.y = helper.step(t.y, b.y, math.abs(t.y - b.y) * settings.BACK_SPEED) + t.y = helper.step(t.y, b.y, math.abs(t.y - b.y) * self.style.BACK_SPEED) end if t.x < b.x then - t.x = helper.step(t.x, b.x, math.abs(t.x - b.x) * settings.BACK_SPEED) + t.x = helper.step(t.x, b.x, math.abs(t.x - b.x) * self.style.BACK_SPEED) end if t.y > b.w then - t.y = helper.step(t.y, b.w, math.abs(t.y - b.w) * settings.BACK_SPEED) + t.y = helper.step(t.y, b.w, math.abs(t.y - b.w) * self.style.BACK_SPEED) end if t.x > b.z then - t.x = helper.step(t.x, b.z, math.abs(t.x - b.z) * settings.BACK_SPEED) + t.x = helper.step(t.x, b.z, math.abs(t.x - b.z) * self.style.BACK_SPEED) end end @@ -94,8 +95,8 @@ local function update_hand_scroll(self, dt) inert.x = math.abs(inert.x) * helper.sign(delta_x) inert.y = math.abs(inert.y) * helper.sign(delta_y) - inert.x = inert.x * settings.FRICT_HOLD - inert.y = inert.y * settings.FRICT_HOLD + inert.x = inert.x * self.style.FRICT_HOLD + inert.y = inert.y * self.style.FRICT_HOLD set_pos(self, self.target) end @@ -116,11 +117,11 @@ local function check_points(self) local inert = self.inert if not self.is_inert then - if math.abs(inert.x) > settings.DEADZONE then + if math.abs(inert.x) > self.style.DEADZONE then self:scroll_to_index(self.selected - helper.sign(inert.x)) return end - if math.abs(inert.y) > settings.DEADZONE then + if math.abs(inert.y) > self.style.DEADZONE then self:scroll_to_index(self.selected + helper.sign(inert.y)) return end @@ -154,13 +155,14 @@ local function check_points(self) temp_dist_on_inert = dist end end + self:scroll_to_index(index_on_inert or index) end local function check_threshold(self) local inert = self.inert - if not self.is_inert or vmath.length(inert) < settings.INERT_THRESHOLD then + if not self.is_inert or vmath.length(inert) < self.style.INERT_THRESHOLD then check_points(self) inert.x = 0 inert.y = 0 @@ -171,11 +173,11 @@ end local function update_free_inert(self, dt) local inert = self.inert if inert.x ~= 0 or inert.y ~= 0 then - self.target.x = self.pos.x + (inert.x * dt * settings.INERT_SPEED) - self.target.y = self.pos.y + (inert.y * dt * settings.INERT_SPEED) + self.target.x = self.pos.x + (inert.x * dt * self.style.INERT_SPEED) + self.target.y = self.pos.y + (inert.y * dt * self.style.INERT_SPEED) - inert.x = inert.x * settings.FRICT - inert.y = inert.y * settings.FRICT + inert.x = inert.x * self.style.FRICT + inert.y = inert.y * self.style.FRICT -- Stop, when low inert speed and go to points check_threshold(self) @@ -275,7 +277,7 @@ function M.on_input(self, action_id, action) self.target.y = self.pos.y else local dist = helper.distance(action.x, action.y, inp.start_x, inp.start_y) - if not M.current_scroll and dist >= settings.DEADZONE then + if not M.current_scroll and dist >= self.style.DEADZONE then local dx = math.abs(inp.start_x - action.x) local dy = math.abs(inp.start_y - action.y) inp.side = (dx > dy) and const.SIDE.X or const.SIDE.Y @@ -331,7 +333,7 @@ function M.scroll_to(self, point, is_instant) self.target = target set_pos(self, target) else - gui.animate(self.node, gui.PROP_POSITION, target, gui.EASING_OUTSINE, settings.ANIM_SPEED, 0, function() + gui.animate(self.node, gui.PROP_POSITION, target, gui.EASING_OUTSINE, self.style.ANIM_SPEED, 0, function() self.animate = false self.target = target set_pos(self, target) diff --git a/druid/druid.lua b/druid/druid.lua index 92988db..3f9e40f 100644 --- a/druid/druid.lua +++ b/druid/druid.lua @@ -4,17 +4,20 @@ -- to create your own rich components. -- @module druid + local const = require("druid.const") local druid_input = require("druid.helper.druid_input") local settings = require("druid.settings") local M = {} + local log = settings.log local _fct_metatable = {} -- Temporary, what the place for it? local default_style = {} + --- Basic components M.comps = { button = require("druid.base.button"), diff --git a/druid/helper.lua b/druid/helper.lua index 1a3a893..5c66d14 100644 --- a/druid/helper.lua +++ b/druid/helper.lua @@ -1,10 +1,12 @@ --- Druid helper module -- @module helper + local const = require("druid.const") local M = {} + --- Text node or icon node can be nil local function get_text_width(text_node) if text_node then @@ -154,4 +156,13 @@ function M.get_druid(self) end +function M.get_style(self, section) + if not self.druid_style then + return {} + end + + return self.druid_style[section] or {} +end + + return M diff --git a/druid/rich/progress_rich.lua b/druid/rich/progress_rich.lua index 793c9d5..277e435 100644 --- a/druid/rich/progress_rich.lua +++ b/druid/rich/progress_rich.lua @@ -2,14 +2,13 @@ -- @module rich.progress_rich local helper = require("druid.helper") -local settings = require("druid.settings") -local pr_settings = settings.progress_rich local M = {} function M.init(self, name, red, green, key) self.druid = helper.get_druid(self) + self.style = helper.get_style(self, "PROGRESS_RICH") self.red = self.druid:new_progress(red, key) self.green = self.druid:new_progress(green, key) self.fill = self.druid:new_progress(name, key) @@ -51,7 +50,7 @@ function M.to(self, to, callback) if self.fill.last_value < to then self.red:to(self.fill.last_value) self.green:to(to, function() - self.timer = timer.delay(pr_settings.DELAY, false, function() + self.timer = timer.delay(self.style.DELAY, false, function() self.red:to(to) self.fill:to(to, callback) end) @@ -61,7 +60,7 @@ function M.to(self, to, callback) if self.fill.last_value > to then self.green:to(self.red.last_value) self.fill:to(to, function() - self.timer = timer.delay(pr_settings.DELAY, false, function() + self.timer = timer.delay(self.style.DELAY, false, function() self.green:to(to) self.red:to(to, callback) end) diff --git a/druid/settings.lua b/druid/settings.lua index 5c5e68b..3529af5 100644 --- a/druid/settings.lua +++ b/druid/settings.lua @@ -3,32 +3,9 @@ local M = {} --- TODO: to JSON? + M.is_debug = false -M.button = { - IS_HOVER = true, - IS_HOLD = true -} -M.progress = { - SPEED = 5, -- progress bar fill rate, more faster - MIN_DELTA = 0.005 -} - -M.progress_rich = { - DELAY = 1, -- delay in seconds before main fill -} - -M.scroll = { - FRICT_HOLD = 0.8, -- mult. for inert, while touching - FRICT = 0.93, -- mult for free inert - INERT_THRESHOLD = 2, -- speed to stop inertion - INERT_SPEED = 25, -- koef. of inert speed - DEADZONE = 6, -- in px - SOFT_ZONE_SIZE = 160, -- size of outside zone (back move) - BACK_SPEED = 0.2, -- lerp speed - ANIM_SPEED = 0.3, -- gui.animation speed to point -} function M.get_text(name) -- override to get text for localized text diff --git a/druid/styles/bounce/const.lua b/druid/styles/bounce/const.lua deleted file mode 100644 index f497323..0000000 --- a/druid/styles/bounce/const.lua +++ /dev/null @@ -1,14 +0,0 @@ -local M = {} - - -M.BUTTON = { - HOVER_SCALE = vmath.vector3(-0.025, -0.025, 1), - HOVER_TIME = 0.05, - SCALE_CHANGE = vmath.vector3(-0.05, - 0.05, 1), - BTN_SOUND = "click", - BTN_SOUND_DISABLED = "click", - DISABLED_COLOR = vmath.vector4(0, 0, 0, 1), - ENABLED_COLOR = vmath.vector4(1), -} - -return M diff --git a/druid/styles/bounce/style.lua b/druid/styles/bounce/style.lua index f05d840..add520f 100644 --- a/druid/styles/bounce/style.lua +++ b/druid/styles/bounce/style.lua @@ -1,37 +1,66 @@ local settings = require("druid.settings") local anims = require("druid.styles.bounce.anims") -local const = require("druid.styles.bounce.const") local M = {} M.BUTTON = { + HOVER_SCALE = vmath.vector3(-0.025, -0.025, 1), + HOVER_TIME = 0.05, + SCALE_CHANGE = vmath.vector3(-0.05, - 0.05, 1), + BTN_SOUND = "click", + BTN_SOUND_DISABLED = "click", + DISABLED_COLOR = vmath.vector4(0, 0, 0, 1), + ENABLED_COLOR = vmath.vector4(1), + IS_HOVER = true, + on_hover = function(self, node, state) if self.hover_anim then - local scale_to = self.scale_from + const.BUTTON.HOVER_SCALE + local scale_to = self.scale_from + M.BUTTON.HOVER_SCALE local target_scale = state and scale_to or self.scale_from - anims.hover_scale(self, target_scale, const.BUTTON.HOVER_TIME) + anims.hover_scale(self, target_scale, M.BUTTON.HOVER_TIME) end end, on_click = function(self, node) - local scale_to = self.scale_from + const.BUTTON.SCALE_CHANGE + local scale_to = self.scale_from + M.BUTTON.SCALE_CHANGE anims.tap_scale_animation(self, node, scale_to) - settings.play_sound(const.BUTTON.BTN_SOUND) + settings.play_sound(M.BUTTON.BTN_SOUND) end, on_click_disabled = function(self, node) - settings.play_sound(const.BUTTON.BTN_SOUND_DISABLED) + settings.play_sound(M.BUTTON.BTN_SOUND_DISABLED) end, on_set_enabled = function(self, node, state) if state then - gui.set_color(node, const.BUTTON.ENABLED_COLOR) + gui.set_color(node, M.BUTTON.ENABLED_COLOR) else - gui.set_color(node, const.BUTTON.DISABLED_COLOR) + gui.set_color(node, M.BUTTON.DISABLED_COLOR) end end } + +M.SCROLL = { + FRICT_HOLD = 0.8, -- mult. for inert, while touching + FRICT = 0.93, -- mult for free inert + INERT_THRESHOLD = 2, -- speed to stop inertion + INERT_SPEED = 25, -- koef. of inert speed + DEADZONE = 6, -- in px + SOFT_ZONE_SIZE = 160, -- size of outside zone (back move) + BACK_SPEED = 0.2, -- lerp speed + ANIM_SPEED = 0.3, -- gui.animation speed to point +} + +M.PROGRESS = { + SPEED = 5, -- progress bar fill rate, more faster + MIN_DELTA = 0.005 +} + +M.PROGRESS_RICH = { + DELAY = 1, -- delay in seconds before main fill +} + return M