diff --git a/druid/materials/gui_tiling_node/gui_tiling_node.fp b/druid/custom/tiling_node/gui_tiling_node.fp similarity index 100% rename from druid/materials/gui_tiling_node/gui_tiling_node.fp rename to druid/custom/tiling_node/gui_tiling_node.fp diff --git a/druid/materials/gui_tiling_node/gui_tiling_node.material b/druid/custom/tiling_node/gui_tiling_node.material similarity index 77% rename from druid/materials/gui_tiling_node/gui_tiling_node.material rename to druid/custom/tiling_node/gui_tiling_node.material index a24d1e6..4c0a29e 100644 --- a/druid/materials/gui_tiling_node/gui_tiling_node.material +++ b/druid/custom/tiling_node/gui_tiling_node.material @@ -1,7 +1,7 @@ name: "repeat" tags: "gui" -vertex_program: "/druid/materials/gui_tiling_node/gui_tiling_node.vp" -fragment_program: "/druid/materials/gui_tiling_node/gui_tiling_node.fp" +vertex_program: "/druid/custom/tiling_node/gui_tiling_node.vp" +fragment_program: "/druid/custom/tiling_node/gui_tiling_node.fp" vertex_constants { name: "view_proj" type: CONSTANT_TYPE_VIEWPROJ diff --git a/druid/materials/gui_tiling_node/gui_tiling_node.vp b/druid/custom/tiling_node/gui_tiling_node.vp similarity index 100% rename from druid/materials/gui_tiling_node/gui_tiling_node.vp rename to druid/custom/tiling_node/gui_tiling_node.vp diff --git a/druid/widget/tiling_node/tiling_node.lua b/druid/custom/tiling_node/tiling_node.lua similarity index 84% rename from druid/widget/tiling_node/tiling_node.lua rename to druid/custom/tiling_node/tiling_node.lua index ba55472..2fef09b 100644 --- a/druid/widget/tiling_node/tiling_node.lua +++ b/druid/custom/tiling_node/tiling_node.lua @@ -24,6 +24,10 @@ function M:init(node) self.params = gui.get(self.node, "params") --[[@as vector4]] + self.timer_no_init = timer.delay(0.1, false, function() + print("The druid.script is not found, please add it nearby to the GUI collection", msg.url()) + end) + defer.push("druid.get_atlas_path", { texture_name = gui.get_texture(self.node), sender = msg.url(), @@ -31,26 +35,30 @@ function M:init(node) end +---@param atlas_path string function M:on_get_atlas_path(atlas_path) + timer.cancel(self.timer_no_init) self.is_inited = self:init_tiling_animation(atlas_path) - local repeat_x, repeat_y = self:get_repeat() - self:refresh(repeat_x, repeat_y) + local repeat_x, repeat_y = self:get_repeat_count_from_node() + self:start_animation(repeat_x, repeat_y) end +---@param node node +---@param property string function M:on_node_property_changed(node, property) if not self.is_inited or node ~= self.node then return end if property == "size" or property == "scale" then - local repeat_x, repeat_y = self:get_repeat() + local repeat_x, repeat_y = self:get_repeat_count_from_node() self:set_repeat(repeat_x, repeat_y) end end -function M:get_repeat() +function M:get_repeat_count_from_node() if not self.is_inited then return 1, 1 end @@ -66,6 +74,7 @@ function M:get_repeat() end +---@param atlas_path string ---@return boolean function M:init_tiling_animation(atlas_path) if not atlas_path then @@ -82,21 +91,21 @@ end -- Start our repeat shader work ---@param repeat_x number X factor ---@param repeat_y number Y factor -function M:refresh(repeat_x, repeat_y) +function M:start_animation(repeat_x, repeat_y) if not self.is_inited then return end + self:set_repeat(repeat_x, repeat_y) + local node = self.node local animation = self.animation - local frame = animation.frames[1] gui.set(node, "uv_coord", frame.uv_coord) - self:set_repeat(repeat_x, repeat_y) if #animation.frames > 1 and animation.fps > 0 then animation.handle = - timer.delay(1/animation.fps, true, function(self, handle, time_elapsed) + timer.delay(1/animation.fps, true, function(_, handle, time_elapsed) local next_rame = animation.frames[animation.current_frame] gui.set(node, "uv_coord", next_rame.uv_coord) @@ -134,6 +143,8 @@ function M:set_repeat(repeat_x, repeat_y) end +---@param offset_perc_x number? X offset +---@param offset_perc_y number? Y offset function M:set_offset(offset_perc_x, offset_perc_y) self.params.z = offset_perc_x or self.params.z self.params.w = offset_perc_y or self.params.w @@ -142,6 +153,8 @@ function M:set_offset(offset_perc_x, offset_perc_y) end +---@param margin_x number? X margin +---@param margin_y number? Y margin function M:set_margin(margin_x, margin_y) self.params.x = margin_x or self.params.x self.params.y = margin_y or self.params.y diff --git a/druid/helper.lua b/druid/helper.lua index d3f1842..c4f49ee 100644 --- a/druid/helper.lua +++ b/druid/helper.lua @@ -367,6 +367,14 @@ function M.get_pivot_offset(pivot_or_node) end +---Check if device is desktop +---@return boolean +function M.is_desktop() + return const.CURRENT_SYSTEM_NAME == const.OS.WINDOWS or const.CURRENT_SYSTEM_NAME == const.OS.MAC or const.CURRENT_SYSTEM_NAME == const.OS.LINUX +end + + + ---Check if device is native mobile (Android or iOS) ---@return boolean Is mobile function M.is_mobile() diff --git a/example/druid.gui_script b/example/druid.gui_script index cc01253..9fabd98 100644 --- a/example/druid.gui_script +++ b/example/druid.gui_script @@ -2,6 +2,7 @@ local lang = require("lang.lang") local saver = require("saver.saver") local storage = require("saver.storage") local druid = require("druid.druid") +local helper = require("druid.helper") local druid_logo = require("example.components.druid_logo.druid_logo") local panel_information = require("example.components.panel_information.panel_information") @@ -35,12 +36,16 @@ local druid_examples = require("example.examples.druid_examples") ---@field output_list output_list +---@param self druid.example +local function setup_saver(self) + saver.init() + saver.bind_save_state("lang", lang.state) +end + + --- Generic setup that should be done once per application ---@param self druid.example local function setup_druid(self) - saver.init() - saver.bind_save_state("lang", lang.state) - lang.init() lang.set_lang("en") -- Force default start language to EN, switch through the UI to check druid.set_text_function(lang.txp) @@ -172,8 +177,38 @@ local function setup_examples(self) end +---@param self druid.example +local function setup_defos(self) + local is_desktop = helper.is_desktop() + local is_debug = sys.get_engine_info().is_debug + + if is_desktop and is_debug then + timer.delay(1, true, function() + local x, y, w, h = defos.get_window_size() + saver.set_value("window.last_state", { x, y, w, h }) + saver.save_game_state() + end) + + -- Restore window size and position + local prev_settings = saver.get_value("window.last_state") + if prev_settings then + ---@cast prev_settings number[] + local x, y, w, h = unpack(prev_settings) + -- Limit size to 300x200 + x = vmath.clamp(x, 0, 4000) + y = vmath.clamp(y, 0, 4000) + w = vmath.clamp(w, 300, 4000) + h = vmath.clamp(h, 200, 4000) + defos.set_window_size(x, y, w, h) + end + end +end + + ---@param self druid.example function init(self) + setup_saver(self) + setup_defos(self) setup_druid(self) self.druid = druid.new(self) diff --git a/example/examples/widgets/tiling_node/example_tiling_node.gui b/example/examples/widgets/tiling_node/example_tiling_node.gui index aae8d7a..f98269f 100644 --- a/example/examples/widgets/tiling_node/example_tiling_node.gui +++ b/example/examples/widgets/tiling_node/example_tiling_node.gui @@ -30,5 +30,5 @@ material: "/builtins/materials/gui.material" adjust_reference: ADJUST_REFERENCE_PARENT materials { name: "gui_tiling_node" - material: "/druid/materials/gui_tiling_node/gui_tiling_node.material" + material: "/druid/custom/tiling_node/gui_tiling_node.material" } diff --git a/example/examples/widgets/tiling_node/example_tiling_node.lua b/example/examples/widgets/tiling_node/example_tiling_node.lua index 84548ee..53a8522 100644 --- a/example/examples/widgets/tiling_node/example_tiling_node.lua +++ b/example/examples/widgets/tiling_node/example_tiling_node.lua @@ -1,4 +1,4 @@ -local tiling_node = require("druid.widget.tiling_node.tiling_node") +local tiling_node = require("druid.custom.tiling_node.tiling_node") ---@class examples.example_tiling_node: druid.widget local M = {} diff --git a/game.project b/game.project index 03b90d8..66da382 100644 --- a/game.project +++ b/game.project @@ -19,11 +19,11 @@ publisher = Insality developer = Maksim Tuprikov custom_resources = /example/locales dependencies#0 = https://github.com/britzl/deftest/archive/refs/tags/2.8.0.zip -dependencies#1 = https://github.com/Insality/defold-saver/archive/refs/tags/1.zip +dependencies#1 = https://github.com/Insality/defold-saver/archive/refs/heads/develop.zip dependencies#2 = https://github.com/Insality/defold-tweener/archive/refs/tags/3.zip dependencies#3 = https://github.com/Insality/panthera/archive/refs/heads/develop.zip dependencies#4 = https://github.com/Insality/defold-lang/archive/refs/tags/3.zip -dependencies#5 = https://github.com/Insality/defold-event/archive/refs/tags/10.zip +dependencies#5 = https://github.com/Insality/defold-event/archive/refs/heads/develop.zip dependencies#6 = https://github.com/subsoap/defos/archive/refs/tags/v2.8.0.zip [library] diff --git a/test/tests/test_blocker.lua b/test/tests/test_blocker.lua index 3c32481..3191e58 100644 --- a/test/tests/test_blocker.lua +++ b/test/tests/test_blocker.lua @@ -6,7 +6,6 @@ return function() local druid_system = nil local druid = nil - local context = nil describe("Blocker component", function() before(function() @@ -20,15 +19,13 @@ return function() mock_time.mock() mock_time.set(60) - context = test_helper.get_context() - druid = druid_system.new(context) + druid = druid_system.new({}) end) after(function() mock_gui.unmock() mock_time.unmock() - druid:final(context) - druid = nil + druid:final() end) it("Should consume input", function()