From 77e56e874769d130ce9ffa129ff6683a63d6a80a Mon Sep 17 00:00:00 2001 From: Insality Date: Sun, 17 Jul 2022 19:07:35 +0300 Subject: [PATCH] Add hotkey example --- druid/const.lua | 6 +- druid/extended/hotkey.lua | 6 +- druid/styles/default/style.lua | 2 +- druid/system/druid_instance.lua | 13 +- example/example.collection | 63 ++++++ example/example.gui_script | 1 + .../examples/general/hotkey/hotkey.collection | 37 ++++ example/examples/general/hotkey/hotkey.gui | 207 ++++++++++++++++++ .../examples/general/hotkey/hotkey.gui_script | 38 ++++ input/game.input_binding | 12 + 10 files changed, 379 insertions(+), 6 deletions(-) create mode 100644 example/examples/general/hotkey/hotkey.collection create mode 100644 example/examples/general/hotkey/hotkey.gui create mode 100644 example/examples/general/hotkey/hotkey.gui_script diff --git a/druid/const.lua b/druid/const.lua index d05c537..12839ba 100755 --- a/druid/const.lua +++ b/druid/const.lua @@ -86,11 +86,11 @@ M.REVERSE_PIVOTS = { M.LAYOUT_MODE = { STRETCH_X = "stretch_x", - STRETCH_Y = "stretch_y", + STRETCH_Y = "stretch_y", ZOOM_MIN = "zoom_min", ZOOM_MAX = "zoom_max", - FIT = gui.ADJUST_FIT, - STRETCH = gui.ADJUST_STRETCH, + FIT = gui.ADJUST_FIT, + STRETCH = gui.ADJUST_STRETCH, } diff --git a/druid/extended/hotkey.lua b/druid/extended/hotkey.lua index e95bdf3..8e8be29 100644 --- a/druid/extended/hotkey.lua +++ b/druid/extended/hotkey.lua @@ -101,7 +101,7 @@ end function Hotkey.on_input(self, action_id, action) if not action_id then - return + return false end if self._modificators[action_id] ~= nil then @@ -136,12 +136,16 @@ function Hotkey.on_input(self, action_id, action) hotkey.is_processing = false if hotkey.callback_argument then self._callback(self:get_context(), hotkey.callback_argument) + return true else self._callback(self:get_context()) + return true end end end end + + return false end diff --git a/druid/styles/default/style.lua b/druid/styles/default/style.lua index a4d4dbc..897b599 100644 --- a/druid/styles/default/style.lua +++ b/druid/styles/default/style.lua @@ -148,7 +148,7 @@ M["text"] = { M["hotkey"] = { - MODIFICATORS = {}, -- Add key ids to mark it as modificator keys + MODIFICATORS = { "key_lshift", "key_rshift", "key_lctrl", "key_rctrl", "key_lalt", "key_ralt", "key_lsuper", "key_rsuper" }, -- Add key ids to mark it as modificator keys } diff --git a/druid/system/druid_instance.lua b/druid/system/druid_instance.lua index e286e78..ee0d77b 100755 --- a/druid/system/druid_instance.lua +++ b/druid/system/druid_instance.lua @@ -31,6 +31,7 @@ -- @see DataList -- @see Hover -- @see Layout +-- @see Hotkey local helper = require("druid.helper") local class = require("druid.system.middleclass") @@ -58,7 +59,6 @@ local radio_group = require("druid.extended.radio_group") local slider = require("druid.extended.slider") local timer = require("druid.extended.timer") local data_list = require("druid.extended.data_list") -local layout = require("druid.extended.layout") local DruidInstance = class("druid.druid_instance") @@ -750,4 +750,15 @@ function DruidInstance.new_layout(self, node, mode) end +--- Create hotkey component +-- @tparam DruidInstance self +-- @tparam string|string[] string Keys for trigger action. Should contains one action key and any amount of modificator keys +-- @tparam function callback Button callback +-- @tparam[opt] value params Button callback params +-- @treturn Layout layout component +function DruidInstance.new_hotkey(self, keys_array, callback, params) + return helper.extended_component("hotkey") +end + + return DruidInstance diff --git a/example/example.collection b/example/example.collection index fd4aca8..f82f0b2 100644 --- a/example/example.collection +++ b/example/example.collection @@ -1751,3 +1751,66 @@ embedded_instances { z: 1.0 } } +embedded_instances { + id: "general_hotkey" + data: "components {\n" + " id: \"screen_factory\"\n" + " component: \"/monarch/screen_factory.script\"\n" + " position {\n" + " x: 0.0\n" + " y: 0.0\n" + " z: 0.0\n" + " }\n" + " rotation {\n" + " x: 0.0\n" + " y: 0.0\n" + " z: 0.0\n" + " w: 1.0\n" + " }\n" + " properties {\n" + " id: \"screen_id\"\n" + " value: \"general_hotkey\"\n" + " type: PROPERTY_TYPE_HASH\n" + " }\n" + " properties {\n" + " id: \"popup\"\n" + " value: \"true\"\n" + " type: PROPERTY_TYPE_BOOLEAN\n" + " }\n" + "}\n" + "embedded_components {\n" + " id: \"collectionfactory\"\n" + " type: \"collectionfactory\"\n" + " data: \"prototype: \\\"/example/examples/general/hotkey/hotkey.collection\\\"\\n" + "load_dynamically: false\\n" + "\"\n" + " position {\n" + " x: 0.0\n" + " y: 0.0\n" + " z: 0.0\n" + " }\n" + " rotation {\n" + " x: 0.0\n" + " y: 0.0\n" + " z: 0.0\n" + " w: 1.0\n" + " }\n" + "}\n" + "" + position { + x: 0.0 + y: 0.0 + z: 0.0 + } + rotation { + x: 0.0 + y: 0.0 + z: 0.0 + w: 1.0 + } + scale3 { + x: 1.0 + y: 1.0 + z: 1.0 + } +} diff --git a/example/example.gui_script b/example/example.gui_script index 138172f..e60a116 100644 --- a/example/example.gui_script +++ b/example/example.gui_script @@ -133,6 +133,7 @@ local function init_lobby(self) self.lobby_grid:add(get_button(self, "Layout", "general_layout", "/general/layout/layout.gui_script")) self.lobby_grid:add(get_button(self, "Swipe", "general_swipe", "/general/swipe/swipe.gui_script")) self.lobby_grid:add(get_button(self, "Drag", "general_drag", "/general/drag/drag.gui_script")) + self.lobby_grid:add(get_button(self, "Hotkey", "general_hotkey", "/general/hotkey/hotkey.gui_script")) self.lobby_grid:add(get_title(self, "Texts")) self.lobby_grid:add(get_button(self, "Texts", "texts_general", "/texts/texts_general/texts_general.gui_script")) diff --git a/example/examples/general/hotkey/hotkey.collection b/example/examples/general/hotkey/hotkey.collection new file mode 100644 index 0000000..0b41685 --- /dev/null +++ b/example/examples/general/hotkey/hotkey.collection @@ -0,0 +1,37 @@ +name: "hotkey" +scale_along_z: 0 +embedded_instances { + id: "go" + data: "components {\n" + " id: \"template\"\n" + " component: \"/example/examples/general/hotkey/hotkey.gui\"\n" + " position {\n" + " x: 0.0\n" + " y: 0.0\n" + " z: 0.0\n" + " }\n" + " rotation {\n" + " x: 0.0\n" + " y: 0.0\n" + " z: 0.0\n" + " w: 1.0\n" + " }\n" + "}\n" + "" + position { + x: 0.0 + y: 0.0 + z: 0.0 + } + rotation { + x: 0.0 + y: 0.0 + z: 0.0 + w: 1.0 + } + scale3 { + x: 1.0 + y: 1.0 + z: 1.0 + } +} diff --git a/example/examples/general/hotkey/hotkey.gui b/example/examples/general/hotkey/hotkey.gui new file mode 100644 index 0000000..34d0b7c --- /dev/null +++ b/example/examples/general/hotkey/hotkey.gui @@ -0,0 +1,207 @@ +script: "/example/examples/general/hotkey/hotkey.gui_script" +fonts { + name: "game" + font: "/example/assets/fonts/game.font" +} +textures { + name: "kenney" + texture: "/example/assets/images/kenney.atlas" +} +background_color { + x: 0.0 + y: 0.0 + z: 0.0 + w: 0.0 +} +nodes { + position { + x: 300.0 + y: 415.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: 600.0 + y: 830.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: "kenney/empty" + id: "root" + 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_MANUAL + custom_type: 0 +} +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: 1.0 + y: 1.0 + z: 1.0 + w: 1.0 + } + size { + x: 400.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_TEXT + blend_mode: BLEND_MODE_ALPHA + text: "Use Ctrl + G or Cmd + G to tirgger the hotkey" + font: "game" + id: "text_hint" + xanchor: XANCHOR_NONE + yanchor: YANCHOR_NONE + pivot: PIVOT_CENTER + outline { + x: 0.0 + y: 0.0 + z: 0.0 + w: 1.0 + } + shadow { + x: 1.0 + y: 1.0 + z: 1.0 + w: 1.0 + } + adjust_mode: ADJUST_MODE_FIT + line_break: true + parent: "root" + layer: "" + inherit_alpha: true + alpha: 1.0 + outline_alpha: 1.0 + shadow_alpha: 0.0 + template_node_child: false + text_leading: 1.0 + text_tracking: 0.0 + custom_type: 0 +} +nodes { + position { + x: 0.0 + y: -140.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: 400.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_TEXT + blend_mode: BLEND_MODE_ALPHA + text: "Counter: 0" + font: "game" + id: "text_counter" + xanchor: XANCHOR_NONE + yanchor: YANCHOR_NONE + pivot: PIVOT_CENTER + outline { + x: 0.0 + y: 0.0 + z: 0.0 + w: 1.0 + } + shadow { + x: 1.0 + y: 1.0 + z: 1.0 + w: 1.0 + } + adjust_mode: ADJUST_MODE_FIT + line_break: true + parent: "root" + layer: "" + inherit_alpha: true + alpha: 1.0 + outline_alpha: 1.0 + shadow_alpha: 0.0 + template_node_child: false + text_leading: 1.0 + text_tracking: 0.0 + custom_type: 0 +} +layers { + name: "image" +} +layers { + name: "text" +} +material: "/builtins/materials/gui.material" +adjust_reference: ADJUST_REFERENCE_PARENT +max_nodes: 512 diff --git a/example/examples/general/hotkey/hotkey.gui_script b/example/examples/general/hotkey/hotkey.gui_script new file mode 100644 index 0000000..2b920d6 --- /dev/null +++ b/example/examples/general/hotkey/hotkey.gui_script @@ -0,0 +1,38 @@ +local hotkey = require("druid.extended.hotkey") +local druid = require("druid.druid") + + +local function on_hotkey(self) + self.counter = self.counter + 1 + gui.set_text(gui.get_node("text_counter"), "Counter: " .. self.counter) +end + + +function init(self) + druid.register("hotkey", hotkey) + self.druid = druid.new(self) + self.counter = 0 + + self.hotkey = self.druid:new_hotkey({ "key_lsuper", "key_g" }, on_hotkey) + self.hotkey:add_hotkey({ "key_lctrl", "key_g"}) +end + + +function final(self) + self.druid:final() +end + + +function update(self, dt) + self.druid:update(dt) +end + + +function on_message(self, message_id, message, sender) + self.druid:on_message(message_id, message, sender) +end + + +function on_input(self, action_id, action) + return self.druid:on_input(action_id, action) +end diff --git a/input/game.input_binding b/input/game.input_binding index 0b6d8ca..a6d0b3a 100644 --- a/input/game.input_binding +++ b/input/game.input_binding @@ -18,6 +18,18 @@ key_trigger { input: KEY_ESC action: "key_esc" } +key_trigger { + input: KEY_LSUPER + action: "key_lsuper" +} +key_trigger { + input: KEY_LCTRL + action: "key_lctrl" +} +key_trigger { + input: KEY_G + action: "key_g" +} mouse_trigger { input: MOUSE_WHEEL_UP action: "scroll_up"