Add hotkey example

This commit is contained in:
Insality 2022-07-17 19:07:35 +03:00
parent 3e5a90920e
commit 77e56e8747
10 changed files with 379 additions and 6 deletions

View File

@ -86,11 +86,11 @@ M.REVERSE_PIVOTS = {
M.LAYOUT_MODE = { M.LAYOUT_MODE = {
STRETCH_X = "stretch_x", STRETCH_X = "stretch_x",
STRETCH_Y = "stretch_y", STRETCH_Y = "stretch_y",
ZOOM_MIN = "zoom_min", ZOOM_MIN = "zoom_min",
ZOOM_MAX = "zoom_max", ZOOM_MAX = "zoom_max",
FIT = gui.ADJUST_FIT, FIT = gui.ADJUST_FIT,
STRETCH = gui.ADJUST_STRETCH, STRETCH = gui.ADJUST_STRETCH,
} }

View File

@ -101,7 +101,7 @@ end
function Hotkey.on_input(self, action_id, action) function Hotkey.on_input(self, action_id, action)
if not action_id then if not action_id then
return return false
end end
if self._modificators[action_id] ~= nil then if self._modificators[action_id] ~= nil then
@ -136,12 +136,16 @@ function Hotkey.on_input(self, action_id, action)
hotkey.is_processing = false hotkey.is_processing = false
if hotkey.callback_argument then if hotkey.callback_argument then
self._callback(self:get_context(), hotkey.callback_argument) self._callback(self:get_context(), hotkey.callback_argument)
return true
else else
self._callback(self:get_context()) self._callback(self:get_context())
return true
end end
end end
end end
end end
return false
end end

View File

@ -148,7 +148,7 @@ M["text"] = {
M["hotkey"] = { 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
} }

View File

@ -31,6 +31,7 @@
-- @see DataList -- @see DataList
-- @see Hover -- @see Hover
-- @see Layout -- @see Layout
-- @see Hotkey
local helper = require("druid.helper") local helper = require("druid.helper")
local class = require("druid.system.middleclass") 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 slider = require("druid.extended.slider")
local timer = require("druid.extended.timer") local timer = require("druid.extended.timer")
local data_list = require("druid.extended.data_list") local data_list = require("druid.extended.data_list")
local layout = require("druid.extended.layout")
local DruidInstance = class("druid.druid_instance") local DruidInstance = class("druid.druid_instance")
@ -750,4 +750,15 @@ function DruidInstance.new_layout(self, node, mode)
end 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 return DruidInstance

View File

@ -1751,3 +1751,66 @@ embedded_instances {
z: 1.0 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
}
}

View File

@ -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, "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, "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, "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_title(self, "Texts"))
self.lobby_grid:add(get_button(self, "Texts", "texts_general", "/texts/texts_general/texts_general.gui_script")) self.lobby_grid:add(get_button(self, "Texts", "texts_general", "/texts/texts_general/texts_general.gui_script"))

View File

@ -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
}
}

View File

@ -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

View File

@ -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

View File

@ -18,6 +18,18 @@ key_trigger {
input: KEY_ESC input: KEY_ESC
action: "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 { mouse_trigger {
input: MOUSE_WHEEL_UP input: MOUSE_WHEEL_UP
action: "scroll_up" action: "scroll_up"