From c894c16ed9d0146132bdb94a3c31ce373dcf022f Mon Sep 17 00:00:00 2001 From: Insality Date: Wed, 27 Mar 2019 00:45:56 +0300 Subject: [PATCH] remove components from main factory --- druid/base/android_back.lua | 11 ++ druid/base/button.lua | 139 +++++++++++++++++ druid/base/text.lua | 42 +++++ druid/base/timer.lua | 53 +++++++ druid/druid.lua | 296 ------------------------------------ example/example.gui | 121 +++++++++++++++ game.project | 4 +- 7 files changed, 368 insertions(+), 298 deletions(-) create mode 100644 druid/base/android_back.lua create mode 100644 druid/base/button.lua create mode 100644 druid/base/text.lua create mode 100644 druid/base/timer.lua diff --git a/druid/base/android_back.lua b/druid/base/android_back.lua new file mode 100644 index 0000000..a237675 --- /dev/null +++ b/druid/base/android_back.lua @@ -0,0 +1,11 @@ +local M = {} + +--- input handler +-- @param action_id - input action id +-- @param action - input action +function M.on_input(instance, action_id, action) + instance.callback(instance.parent.parent) + return true +end + +return M diff --git a/druid/base/button.lua b/druid/base/button.lua new file mode 100644 index 0000000..9b3b722 --- /dev/null +++ b/druid/base/button.lua @@ -0,0 +1,139 @@ +local data = require("druid.data") +local ui_animate = require "druid.help_modules.druid_animate" + +local M = {} +M.interest = { + data.ON_INPUT +} + +M.DEFAULT_SCALE_CHANGE = vmath.vector3(-0.05, - 0.1, 1) +M.DEFAULT_POS_CHANGE = vmath.vector3(0, - 10, 0) +M.DEFAULT_MOVE_SPEED = 5 +M.DEFAULT_ALPHA_DOWN = 0.8 +M.DEFAULT_TIME_ANIM = 0.1 +M.DEFAULT_DEACTIVATE_COLOR = vmath.vector4(0, 0, 0, 0) +M.DEFAULT_DEACTIVATE_SCALE = vmath.vector3(0.8, 0.9, 1) +M.DEFAULT_ACTIVATE_SCALE = vmath.vector3(1, 1, 1) +M.DEFAUL_ACTIVATION_TIME = 0.2 + + +function M.init(instance, callback, event, action, animate_node_name, sound) + instance.event = event or data.A_TOUCH + instance.action = action or data.RELEASED + instance.anim_node = animate_node_name and gui.get_node(animate_node_name) or instance.node + instance.scale_from = gui.get_scale(instance.anim_node) + instance.scale_to = instance.scale_from + M.DEFAULT_SCALE_CHANGE + instance.pos = gui.get_position(instance.anim_node) + instance.callback = callback + -- instance.params = params + instance.tap_anim = M.tap_scale_animation + instance.back_anim = M.back_scale_animation + -- instance.sound = sound or M.BTN_SOUND_FUNC + -- instance.sound_disable = sound_disable or M.BTN_SOUND_DISABLE_FUNC +end + +--- Set text to text field +-- @param action_id - input action id +-- @param action - input action +function M.on_input(instance, action_id, action) + if gui.is_enabled(instance.node) and gui.pick_node(instance.node, action.x, action.y) then + if not instance.disabled then + instance.tap_anim(instance) + return true + else + -- instance.sound_disable() + return false + end + end + return false +end + +function M.tap_scale_animation(instance) + ui_animate.scale_to(instance, instance.anim_node, instance.scale_to, + function() + if instance.back_anim then + instance.back_anim(instance) + end + -- instance.sound() + instance.callback(instance.parent.parent, instance.params, instance) + end + ) +end + +function M.back_scale_animation(instance) + ui_animate.scale_to(instance, instance.anim_node, instance.scale_from) +end + +function M.tap_tab_animation(instance, force) + ui_animate.alpha(instance, instance.anim_node, M.DEFAULT_ALPHA_DOWN, nil, M.DEFAULT_TIME_ANIM) + ui_animate.fly_to(instance, instance.anim_node, instance.pos + M.DEFAULT_POS_CHANGE, M.DEFAULT_MOVE_SPEED) + ui_animate.scale_to(instance, instance.anim_node, instance.scale_to, + function() + if instance.back_anim then + instance.back_anim(instance) + end + instance.callback(instance.parent.parent, instance.params, force) + end + ) +end + +function M.back_tab_animation(instance) + ui_animate.alpha(instance, instance.anim_node, 1, nil, M.DEFAULT_TIME_ANIM) + ui_animate.fly_to(instance, instance.anim_node, instance.pos, M.DEFAULT_MOVE_SPEED) + ui_animate.scale_to(instance, instance.anim_node, instance.scale_from) +end + +function M.deactivate(instance, is_animate, callback) + instance.disabled = true + if is_animate then + local counter = 0 + local clbk = function() + counter = counter + 1 + if counter == 3 and callback then + callback(instance.parent.parent) + end + end + ui_animate.color(instance, instance.node, M.DEFAULT_DEACTIVATE_COLOR, clbk, M.DEFAUL_ACTIVATION_TIME, 0, + gui.EASING_OUTBOUNCE) + ui_animate.scale_y_from_to(instance, instance.node, M.DEFAULT_ACTIVATE_SCALE.x, M.DEFAULT_DEACTIVATE_SCALE.x, clbk, + M.DEFAUL_ACTIVATION_TIME, gui.EASING_OUTBOUNCE) + ui_animate.scale_x_from_to(instance, instance.node, M.DEFAULT_ACTIVATE_SCALE.y, M.DEFAULT_DEACTIVATE_SCALE.y, clbk, + M.DEFAUL_ACTIVATION_TIME, gui.EASING_OUTBOUNCE) + else + gui.set_color(instance.node, M.DEFAULT_DEACTIVATE_COLOR) + gui.set_scale(instance.node, M.DEFAULT_DEACTIVATE_SCALE) + if callback then + callback(instance.parent.parent) + end + end +end + +function M.activate(instance, is_animate, callback) + if is_animate then + local counter = 0 + local clbk = function() + counter = counter + 1 + if counter == 3 then + instance.disabled = false + if callback then + callback(instance.parent.parent) + end + end + end + ui_animate.color(instance, instance.node, ui_animate.TINT_SHOW, clbk, M.DEFAUL_ACTIVATION_TIME, 0, + gui.EASING_OUTBOUNCE) + ui_animate.scale_y_from_to(instance, instance.node, M.DEFAULT_DEACTIVATE_SCALE.x, M.DEFAULT_ACTIVATE_SCALE.x, clbk, + M.DEFAUL_ACTIVATION_TIME, gui.EASING_OUTBOUNCE) + ui_animate.scale_x_from_to(instance, instance.node, M.DEFAULT_DEACTIVATE_SCALE.y, M.DEFAULT_ACTIVATE_SCALE.y, clbk, + M.DEFAUL_ACTIVATION_TIME, gui.EASING_OUTBOUNCE) + else + gui.set_color(instance.node, ui_animate.TINT_SHOW) + gui.set_scale(instance.node, M.DEFAULT_ACTIVATE_SCALE) + instance.disabled = false + if callback then + callback(instance.parent.parent) + end + end +end + +return M diff --git a/druid/base/text.lua b/druid/base/text.lua new file mode 100644 index 0000000..fbc6eb1 --- /dev/null +++ b/druid/base/text.lua @@ -0,0 +1,42 @@ +local M = {} + +local ui_animate = require "druid.help_modules.druid_animate" + +--- Bounce text field +function M.bounce(instance, callback) + gui.set_scale(instance.node, instance.scale_from) + ui_animate.bounce(nil, instance.node, instance.scale_to, callback) +end + +--- Set text to text field +-- @param set_to - set value to text field +function M.set_to(instance, set_to) + instance.last_value = set_to + gui.set_text(instance.node, set_to) +end + +--- Set color +-- @param color +function M.set_color(instance, color) + instance.last_color = color + gui.set_color(instance.node, color) +end + +--- Set scale +-- @param scale +function M.set_scale(instance, scale) + instance.last_scale = scale + gui.set_scale(instance.node, scale) +end + +--- Called when layout updated (rotate for example) +function M.on_layout_updated(instance) + if instance.last_color then + M.set_color(instance, instance.last_color) + end + if instance.last_scale then + M.set_scale(instance, instance.last_scale) + end +end + +return M diff --git a/druid/base/timer.lua b/druid/base/timer.lua new file mode 100644 index 0000000..3f7329d --- /dev/null +++ b/druid/base/timer.lua @@ -0,0 +1,53 @@ +local M = {} + +local formats = require "druid.help_modules.formats" + +--- Set text to text field +-- @param set_to - set value in seconds +function M.set_to(instance, set_to) + instance.last_value = set_to + gui.set_text(instance.node, formats.second_string_min(set_to)) +end + +--- Called when layout updated (rotate for example) +function M.on_layout_updated(instance) + M.set_to(instance, instance.last_value) +end + +--- Called when update +-- @param is_on - boolean is timer on +function M.set_work_mode(instance, is_on) + instance.is_on = is_on +end + +--- Set time interval +-- @param from - "from" time in seconds +-- @param to - "to" time in seconds +function M.set_interval(instance, from, to) + instance.second_from = from + instance.seconds_counter = from + instance.seconds_temp = 0 + instance.seconds_to = to + instance.second_step = from < to and 1 or - 1 + M.set_work_mode(instance, true) + M.set_to(instance, from) +end + +--- Called when update +-- @param dt - delta time +function M.on_updated(instance, dt) + if instance.is_on then + instance.seconds_temp = instance.seconds_temp + dt + if instance.seconds_temp > 1 then + instance.seconds_temp = instance.seconds_temp - 1 + instance.seconds_counter = instance.seconds_counter + instance.second_step + M.set_to(instance, instance.seconds_counter) + if instance.seconds_counter == instance.seconds_to then + instance.is_on = false + instance.callback(instance) + end + end + end +end + +return M diff --git a/druid/druid.lua b/druid/druid.lua index 4c34465..ab31684 100644 --- a/druid/druid.lua +++ b/druid/druid.lua @@ -3,20 +3,7 @@ local M = {} local druid_input = require "druid.help_modules.druid_input" M.input = druid_input -local pie_progress_bar = require "druid.components.pie_progress_bar" -local progress_bar = require "druid.components.progress_bar" -local flying_particles = require "druid.components.flying_particles" -local text_field = require "druid.components.text_field" -local counter = require "druid.components.counter" -local image = require "druid.components.image" -local button = require "druid.components.button" -local timer = require "druid.components.timer" -local tab_page = require "druid.components.tab_page" -local tabs_container = require "druid.components.tabs_container" -local spine_anim = require "druid.components.spine_anim" -local scrolling_box = require "druid.components.scrolling_box" -local andr_back_btn = require "druid.components.andr_back_btn" local LAYOUT_CHANGED = hash("layout_changed") local ON_MESSAGE = hash("on_message") @@ -27,24 +14,6 @@ M.TRANSLATABLE = hash("TRANSLATABLE") local STRING = "string" ---- Call this method when you need to update translations. -function M.translate(factory) - if factory[M.TRANSLATABLE] then - local key, result - for i, v in ipairs(factory[M.TRANSLATABLE]) do - key = v.lang_key or v.name - if key then - if v.lang_params then - result = lang.txp(key, v.lang_params) - else - result = lang.txt(key) - end - if result then - lang.set_node_properties(v.node, key) - end - result = result or v.last_value - v:set_to(result) - end end end end @@ -142,279 +111,14 @@ local function create(meta, factory, name, ...) factory[v] = {} end factory[v][#factory[v] + 1] = instance - end - return instance -end ---- Create new instance of a text_field --- @param factory - parent factory --- @param name - name of text node --- @param init_value - init ui object with this value --- @return instance of a text_field -function M.new_text_field(factory, name, init_value, bounce_in) - local instance = create(text_field, factory, name, M.TRANSLATABLE, LAYOUT_CHANGED) - instance.scale_from = gui.get_scale(instance.node) - instance.scale_to = bounce_in and vmath.mul_per_elem(instance.scale_from, bounce_in) or instance.scale_from - instance:set_to(init_value or 0) - return instance -end - ---- Create new instance of a counter --- @param factory - parent factory --- @param name - name of text node --- @param init_value - init ui object with this value --- @return instance of a text_field -function M.new_counter(factory, name, init_value) - local instance = create(counter, factory, name, LAYOUT_CHANGED, ON_UPDATE) - instance.scale_from = gui.get_scale(instance.node) - instance.scale_to = instance.scale_from * 1.2 - instance:set_to(init_value or 0) - return instance -end - ---- Create new instance of an image --- @param factory - parent factory --- @param name - name of image node --- @param anim_table - table with animations or frames --- @param init_frame - init with this frame --- @return instance of an image -function M.new_image(factory, name, anim_table, init_frame, bounce_in) - local instance = create(image, factory, name, LAYOUT_CHANGED) - instance.scale_from = gui.get_scale(instance.node) - instance.scale_to = bounce_in and vmath.mul_per_elem(instance.scale_from, bounce_in) or instance.scale_from - instance.anim_table = anim_table - if init_frame then - instance:set_to(init_frame) - elseif anim_table then - instance:set_to(1) - end - return instance -end - ---- Create new instance of a timer --- @param factory - parent factory --- @param name - name of image node --- @param second_from - start time --- @param seconds_to - end time --- @param callback - call when timer finished --- @return instance of a timer -function M.new_timer(factory, name, second_from, seconds_to, callback) - local instance = create(timer, factory, name, LAYOUT_CHANGED, ON_UPDATE) - instance:set_to(second_from) - instance:set_interval(second_from, seconds_to) - instance.is_on = true - instance.callback = callback - return instance -end - ---- Add new pie progress component for handling --- @param factory - parent factory --- @param name - a node name for a pie progress instance --- @param init_value - init ui object with this value --- @return instance with pie_progress -function M.new_pie_progress(factory, name, init_value) - local instance = create(pie_progress_bar, factory, name, LAYOUT_CHANGED) - instance:set_to(init_value or 1) - return instance -end - ---- Add new progress bar component for handling --- @param factory - parent factory --- @param name - name of the fill node --- @param key - x or y - key for scale --- @param init_value - init ui object with this value --- @return instance with pie_progress -function M.new_progress_bar(factory, name, key, init_value) - local instance = create(progress_bar, factory, name, LAYOUT_CHANGED) - instance.prop = hash("scale."..key) - instance.key = key - instance.node = gui.get_node(name) - instance.scale = gui.get_scale(instance.node) - instance:set_to(init_value or 1) - return instance -end - ---- Create new instance of a flying particles --- @param factory - parent factory --- @param name - name of prototype --- @param count - how many particles need to cache --- @param get_pos_func - function that returns target pos for flying --- @return instance of a flying particles -function M.new_flying_particles(factory, name, count, get_pos_func) - local instance = create(flying_particles, factory, name, LAYOUT_CHANGED) - instance.get_pos_func = get_pos_func - local node = instance.node - instance.node = node - instance.fly_particles = {} - instance.fly_particles[1] = node - for i = 2, count do - instance.fly_particles[i] = gui.clone(node) - end - instance.scale = gui.get_scale(node) - instance.last_particle = 0 - return instance -end - -M.BTN_SOUND_FUNC = function() end -M.BTN_SOUND_DISABLE_FUNC = function()end - ---- Add new button component for handling --- @param factory - parent factory --- @param name - a node name for a button instance --- @param callback - click button callback --- @param params - callback parameters, will be returned with self callback(self, params) --- @param animate_node_name - node for animation, if it's not a main node --- @return instance of button -function M.new_button(factory, name, callback, params, animate_node_name, event, action, sound, sound_disable) - input_init(factory) - local instance = create(button, factory, name, ON_INPUT) - instance.event = event or druid_input.A_CLICK - instance.action = action or druid_input.RELEASED - instance.anim_node = animate_node_name and gui.get_node(animate_node_name) or instance.node - instance.scale_from = gui.get_scale(instance.anim_node) - instance.scale_to = instance.scale_from + button.DEFAULT_SCALE_CHANGE - instance.pos = gui.get_position(instance.anim_node) - instance.callback = callback - instance.params = params - instance.tap_anim = button.tap_scale_animation - instance.back_anim = button.back_scale_animation - instance.sound = sound or M.BTN_SOUND_FUNC - instance.sound_disable = sound_disable or M.BTN_SOUND_DISABLE_FUNC - return instance -end - ---- Add reaction for back btn (on Android for example) --- @param factory - parent factory --- @param callback - tap button callback -function M.new_back_handler(factory, callback) - input_init(factory) - local instance = create(andr_back_btn, factory, nil, ON_INPUT) - instance.event = druid_input.A_ANDR_BACK - instance.action = druid_input.RELEASED - instance.callback = callback - return instance -end - ---- Create new tab page instance --- @param factory - parent factory --- @param name - name of parental node that represents tab page content --- @param easing - easing for tab page --- @param duration - duration of animation for tab page --- @param callback - call when change page --- @return instance that represents the tab page -function M.new_tab_page(factory, name, easing, duration, callback) - local instance = create(tab_page, factory, name, M.EVENTS.ON_MESSAGE) - instance.in_pos = gui.get_position(instance.node) - instance.out_pos = gui.get_position(instance.node) - instance.easing = easing or tab_page.DEFAULT_EASING - instance.duration = duration or tab_page.DEFAULT_DURATION - instance.callback = callback - return instance -end - ---- Create new tab btns container instance --- @param factory - parent factory --- @param name - name of parental node that represents tab btns container --- @return instance that represents the tab btns container -function M.new_tabs_container(factory, name, callback) - local instance = create(tabs_container, factory, name, LAYOUT_CHANGED) - instance:update_sizes() - instance.url = msg.url() - --- Create new tab btn instance - -- @param name - name of parental node that represents tab btn - -- @return instance that represents the tab btn - function instance.new_tab_btn(_instance, _name, url, index) - local params = {url = url, index = index, name = _name} - local btn = M.new_button(factory, _name, nil, params) - btn.back_anim = nil - btn.manual_back = button.back_tab_animation - btn.tap_anim = button.tap_tab_animation - btn.callback = function(_, _, force) - instance.switch_tab(instance, params, force) - if callback then - callback(factory.parent, index, force) - end end - instance[_name] = params - if not instance.btns then - instance.btns = {} - end - instance.btns[index] = btn - return btn end - return instance end ---- Add new spine animation --- @param factory - parent factory --- @param name - a node name for a spine anim --- @param idle_table - table with idle animations --- @param active_table - table with active animations --- @param init_idle - init idle animation name or index in idle table --- @return instance with spine anim -function M.new_spine_anim(factory, name, idle_table, active_table, init_idle) - local instance = create(spine_anim, factory, name, LAYOUT_CHANGED) - instance.idle_table = idle_table - instance.active_table = active_table - instance:play_idle(init_idle) - return instance -end ---- Add new scrolling box --- @param factory - parent factory --- @param name - a node name for a spine anim --- @param zone_name - node name of zone for tap --- @param speed_coef - vector3 coef. of speed for scrolling --- @param maximum - vector3 maximum position for scrolling --- @param points_of_interest - table with vector3 point of interes --- @param callback - scrolling events callback --- @return instance with scrolling box -function M.new_scrolling_box(factory, name, zone_name, speed_coef, maximum, points_of_interest, callback) - local instance = create(scrolling_box, factory, name, ON_UPDATE, ON_SWIPE) - instance.pos = gui.get_position(instance.node) - instance.start_pos = vmath.vector3(instance.pos) - instance.maximum = maximum - instance.points_of_interest = points_of_interest - instance.callback = callback - if instance.start_pos.x > instance.maximum.x then - instance.start_pos.x, instance.maximum.x = instance.maximum.x, instance.start_pos.x end - if instance.start_pos.y > instance.maximum.y then - instance.start_pos.y, instance.maximum.y = instance.maximum.y, instance.start_pos.y - end - if type(name) == STRING then - instance.scrolling_zone = gui.get_node(zone_name) - else - instance.scrolling_zone = zone_name - end - instance.swipe = { - minSwipeDistance = 40, - speed_down_coef = 1.1, - speed_up_coef = speed_coef or vmath.vector3(1.1, 1.1, 0), - speed = vmath.vector3(0, 0, 0), - maximum = vmath.vector3(0, 0, 0), - min_speed = 2, - beginX = 0, - beginY = 0, - endX = 0, - endY = 0, - xDistance = nil, - yDistance = nil, - totalSwipeDistanceLeft = nil, - totalSwipeDistanceRight = nil, - totalSwipeDistanceUp = nil, - totalSwipeDistanceDown = nil, - is_swipe = nil, - end_move_coef_x = 1, - end_move_coef_y = 1, - back_slow_coef = 0.4, - end_position_x = nil, - end_position_y = nil, - is_x = instance.start_pos.x ~= instance.maximum.x, - is_y = instance.start_pos.y ~= instance.maximum.y - } - return instance end return M diff --git a/example/example.gui b/example/example.gui index 12b1015..b01879a 100644 --- a/example/example.gui +++ b/example/example.gui @@ -1,10 +1,131 @@ script: "/example/example.gui.gui_script" +fonts { + name: "system_font" + font: "/builtins/fonts/system_font.font" +} background_color { x: 0.0 y: 0.0 z: 0.0 w: 0.0 } +nodes { + position { + x: 200.0 + y: 200.0 + z: 0.0 + w: 1.0 + } + rotation { + x: 0.0 + y: 0.0 + z: 0.0 + w: 1.0 + } + scale { + x: 1.0 + y: 1.0 + z: 1.0 + w: 1.0 + } + size { + x: 200.0 + y: 100.0 + z: 0.0 + w: 1.0 + } + color { + x: 1.0 + y: 1.0 + z: 1.0 + w: 1.0 + } + type: TYPE_BOX + blend_mode: BLEND_MODE_ALPHA + texture: "" + id: "button" + xanchor: XANCHOR_NONE + yanchor: YANCHOR_NONE + pivot: PIVOT_CENTER + adjust_mode: ADJUST_MODE_FIT + layer: "" + inherit_alpha: true + slice9 { + x: 0.0 + y: 0.0 + z: 0.0 + w: 0.0 + } + clipping_mode: CLIPPING_MODE_NONE + clipping_visible: true + clipping_inverted: false + alpha: 1.0 + template_node_child: false + size_mode: SIZE_MODE_AUTO +} +nodes { + position { + x: 0.0 + y: 0.0 + z: 0.0 + w: 1.0 + } + rotation { + x: 0.0 + y: 0.0 + z: 0.0 + w: 1.0 + } + scale { + x: 2.0 + y: 2.0 + z: 1.0 + w: 1.0 + } + size { + x: 200.0 + y: 100.0 + z: 0.0 + w: 1.0 + } + color { + x: 0.0 + y: 0.0 + z: 0.0 + w: 1.0 + } + type: TYPE_TEXT + blend_mode: BLEND_MODE_ALPHA + text: "button text" + font: "system_font" + id: "text" + xanchor: XANCHOR_NONE + yanchor: YANCHOR_NONE + pivot: PIVOT_CENTER + outline { + x: 1.0 + y: 1.0 + z: 1.0 + w: 1.0 + } + shadow { + x: 1.0 + y: 1.0 + z: 1.0 + w: 1.0 + } + adjust_mode: ADJUST_MODE_FIT + line_break: false + parent: "button" + layer: "" + inherit_alpha: true + alpha: 1.0 + outline_alpha: 1.0 + shadow_alpha: 1.0 + template_node_child: false + text_leading: 1.0 + text_tracking: 0.0 +} material: "/builtins/materials/gui.material" adjust_reference: ADJUST_REFERENCE_PARENT max_nodes: 512 diff --git a/game.project b/game.project index a165b1d..adf34bf 100644 --- a/game.project +++ b/game.project @@ -5,8 +5,8 @@ main_collection = /example/example.collectionc shared_state = 1 [display] -width = 960 -height = 640 +width = 400 +height = 400 [project] title = druid