-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathexample.gui_script
More file actions
274 lines (230 loc) · 8.8 KB
/
example.gui_script
File metadata and controls
274 lines (230 loc) · 8.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
local dirtylarry = require "dirtylarry/dirtylarry"
local cursor = require("example.cursor.cursor")
local cursor_url = require("example.html5_cursor")
local system_name = sys.get_sys_info().system_name
--pls add the files using bundle_resources parameter in game.project https://www.defold.com/manuals/project-settings/
local ICON_NAMES = {
["Windows"] = "win.ico",
["HTML5"] = "html5.ico",
["Darwin"] = "mac.png"
}
function toggle_cursor_lock(self)
self.cursor_locked = not self.cursor_locked
defos.set_cursor_locked(self.cursor_locked)
local text = self.cursor_locked and "Unlock cursor" or "Lock cursor"
gui.set_text(gui.get_node("lock_cursor/larrylabel"), text)
end
function extract_to_savefolder(res)
local appname = sys.get_config("project.title")
local resbuff = resource.load("/resources/"..res)
local raw_bytes = buffer.get_bytes(resbuff, hash("data"))
local path = sys.get_save_file(appname, res)
local f = io.open(path, "wb")
f:write(raw_bytes)
f:flush()
f:close()
print(path)
return path
end
function init(self)
local appname = sys.get_config("project.title")
gui.set_text(gui.get_node("bundle_path"), defos.get_bundle_root()..defos.PATH_SEP)
-- custom cursors we will change between
self.current_cursor = 0 -- start with default cursor (nil)
self.cursors = {
defos.CURSOR_HAND,
defos.CURSOR_ARROW,
defos.CURSOR_CROSSHAIR,
defos.CURSOR_IBEAM,
}
if system_name == "Linux" then
-- NOTE: since Defold cannot load resource without extension, we added .xcur here.
-- NOTE: cursor should be normal x11 cursor file that type is: image/x-xcursor
table.insert(self.cursors, defos.load_cursor(extract_to_savefolder("cursor.xcur")))
end
if system_name == "Windows" then
for i, v in ipairs({"cursor_01.ani", "cursor_02.ani" }) do
-- load source file and write them to save folder, so that we can access them with fullpath, or you can use bundle_resource
table.insert(self.cursors, defos.load_cursor(extract_to_savefolder(v)))
end
end
if system_name == "Darwin" then
table.insert(self.cursors, defos.load_cursor({
image = resource.load("/resources/cursor_mac.tiff"),
hot_spot_x = 18,
hot_spot_y = 2,
}))
end
if system_name == "HTML5" then
table.insert(self.cursors, defos.load_cursor(cursor_url))
end
defos.on_mouse_enter(function ()
print("Mouse entered view")
if not self.cursor_visible then
defos.set_cursor_visible(false)
end
end)
defos.on_mouse_leave(function ()
print("Mouse left view")
if not self.cursor_visible then
defos.set_cursor_visible(true)
end
end)
if system_name == "HTML5" then
defos.on_click(function ()
-- If we're hovering over the button
if gui.get_flipbook(gui.get_node("lock_cursor/larrybutton")) == hash("button_pressed") then
toggle_cursor_lock(self)
end
if gui.get_flipbook(gui.get_node("toggle_fullscreen/larrybutton")) == hash("button_pressed") then
defos.toggle_fullscreen()
end
if gui.get_flipbook(gui.get_node("toggle_borderless/larrybutton")) == hash("button_pressed") then
defos.toggle_borderless()
end
if gui.get_flipbook(gui.get_node("toggle_keep_awake/larrybutton")) == hash("button_pressed") then
if defos.is_keep_awake_supported() then
self.keep_awake = not self.keep_awake
defos.set_keep_awake(self.keep_awake)
gui.set_text(gui.get_node("toggle_keep_awake/larrylabel"), self.keep_awake and "Allow screen sleep" or "Keep screen awake")
else
print("defos: Keep-awake / Wake Lock is not supported on this platform.")
end
end
end)
end
defos.on_cursor_lock_disabled(function ()
self.cursor_locked = false
gui.set_text(gui.get_node("lock_cursor/larrylabel"), "Lock cursor")
end)
self.cursor_visible = true
self.cursor_clipped = false
self.cursor_locked = false
self.keep_awake = false
msg.post(".", "acquire_input_focus")
-- Console related commands only work on Windows and in bundled builds not in editor builds
if system_name == "Windows" then
defos.set_console_visible(not defos.is_console_visible())
end
local displays = defos.get_displays()
print("Found " .. #displays .. " displays:")
for i, display in ipairs(displays) do
pprint(display)
end
local current_display_id = defos.get_current_display_id()
print("Current display id: ", current_display_id)
local modes = defos.get_display_modes(current_display_id)
print("Found " .. #modes .. " modes for current display:")
for i, mode in ipairs(modes) do
print(
mode.width .. "x" .. mode.height .. " " ..
mode.bits_per_pixel .. "bit @" .. mode.refresh_rate .. "Hz x" ..
mode.scaling_factor .. " " .. mode.orientation .. "deg"
)
end
print("App command line arguments:")
pprint(defos.get_arguments())
end
local function change_mouse_label(text)
local label = gui.get_node("disable_mouse_cursor/larrylabel")
gui.set_text(label,text)
end
function update(self, dt)
local x,y,w,h = defos.get_window_size()
gui.set_text(gui.get_node("window_pos"),"window position "..x.." "..y.." "..w.." "..h)
x,y,w,h = defos.get_view_size()
gui.set_text(gui.get_node("view_pos"),"view position "..x.." "..y.." "..w.." "..h)
x, y = defos.get_cursor_pos()
w, h = defos.get_cursor_pos_view()
gui.set_text(gui.get_node("cursor_pos"),"cursor: "..math.floor(x).." "..math.floor(y).." view: "..math.floor(w).." "..math.floor(h))
gui.set_text(gui.get_node("is_fullscreen"),"is_fullscreen "..tostring(defos.is_fullscreen()))
gui.set_text(gui.get_node("is_maximized"),"is_maximized "..tostring(defos.is_maximized()))
gui.set_text(gui.get_node("is_mouse_in_view"),"is_mouse_in_view "..tostring(defos.is_mouse_in_view()))
gui.set_text(gui.get_node("is_borderless"),"is_borderless "..tostring(defos.is_borderless()))
end
function update_clipping_label(self)
if self.cursor_clipped then
text = "Stop clipping"
else
text = "Clip cursor"
end
gui.set_text(gui.get_node("clip_cursor/larrylabel"), text)
end
function on_input(self, action_id, action)
self.input_title = dirtylarry:input("window_title", action_id, action, gui.KEYBOARD_TYPE_DEFAULT, "Type text")
dirtylarry:button("set_window_title", action_id, action, function ()
defos.set_window_title(tostring(self.input_title))
end)
dirtylarry:button("disable_minimize_button", action_id, action, function ()
defos.disable_minimize_button()
end)
dirtylarry:button("disable_maximize_button", action_id, action, function ()
defos.disable_maximize_button()
end)
dirtylarry:button("disable_window_resize", action_id, action, function ()
defos.disable_window_resize()
end)
dirtylarry:button("random_window_size", action_id, action, function ()
defos.set_window_size(math.random(1,500),math.random(1,500),math.random(100,1024),math.random(100,768))
end)
dirtylarry:button("set_window_size", action_id, action, function ()
defos.set_view_size(nil, nil, 1024, 768)
end)
dirtylarry:button("toggle_fullscreen", action_id, action, function ()
if system_name ~= "HTML5" then
defos.toggle_fullscreen()
end
end)
dirtylarry:button("toggle_borderless", action_id, action, function ()
if system_name == "Windows" or system_name == "Darwin" then
defos.toggle_borderless()
end
end)
dirtylarry:button("toggle_maximize", action_id, action, function ()
defos.toggle_maximized()
end)
dirtylarry:button("disable_mouse_cursor", action_id, action, function ()
local cursor_visible = not self.cursor_visible
self.cursor_visible = cursor_visible
defos.set_cursor_visible(cursor_visible)
cursor.set_software_cursor_enabled(not cursor_visible)
change_mouse_label(cursor_visible and "Hide mouse cursor" or "Show mouse cursor")
end)
dirtylarry:button("random_cursor_pos", action_id, action, function()
local x = math.random(1,2000)
local y = math.random(1,2000)
defos.set_cursor_pos_view(x, y)
end)
dirtylarry:button("clip_cursor", action_id, action, function()
self.cursor_clipped = not self.cursor_clipped
defos.set_cursor_clipped(self.cursor_clipped)
update_clipping_label(self)
end)
dirtylarry:button("lock_cursor", action_id, action, function()
if system_name ~= "HTML5" then
toggle_cursor_lock(self)
end
end)
dirtylarry:button("set_icon", action_id, action, function()
defos.set_window_icon(defos.get_bundle_root()..defos.PATH_SEP..ICON_NAMES[system_name])
end)
dirtylarry:button("activate", action_id, action, function()
timer.delay(5, false, function ()
defos.activate()
end)
end)
dirtylarry:button("change_cursor", action_id, action, function()
self.current_cursor = self.current_cursor + 1
if self.current_cursor > #self.cursors then
self.current_cursor = 0
end
defos.set_cursor(self.cursors[self.current_cursor])
end)
dirtylarry:button("toggle_keep_awake", action_id, action, function()
if system_name ~= "HTML5" then
self.keep_awake = not self.keep_awake
defos.set_keep_awake(self.keep_awake)
gui.set_text(gui.get_node("toggle_keep_awake/larrylabel"), self.keep_awake and "Allow screen sleep" or "Keep screen awake")
end
end)
end