defos/example/example.gui_script

144 lines
3.9 KiB
Plaintext

local dirtylarry = require "dirtylarry/dirtylarry"
local cursor = require("example.cursor.cursor")
cursor_clipped = false
function window_callback(self, event, data)
if event == window.WINDOW_EVENT_FOCUS_LOST then
-- though after lost focus cursor clipping will restore, we should restore it
defos.restore_cursor_clip()
elseif event == window.WINDOW_EVENT_FOCUS_GAINED then
if cursor_clipped then
defos.clip_cursor()
end
elseif event == window.WINDOW_EVENT_RESIZED then
if cursor_clipped then
defos.clip_cursor()
end
end
end
function init(self)
defos.on_mouse_enter(function ()
print("Mouse entered window")
if self.is_cursor == false then
defos.disable_mouse_cursor()
end
end)
defos.on_mouse_leave(function ()
print("Mouse left window")
if self.is_cursor == false then
defos.enable_mouse_cursor()
end
end)
self.is_cursor = true
msg.post(".", "acquire_input_focus")
-- Console related commands only work on Windows and in bundled builds not in editor builds
if defos.is_console_visible() then
defos.hide_console()
else
defos.show_console()
end
window.set_listener(window_callback)
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("position"),"position\n"..x.." "..y.." "..w.." "..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_inside_window"),"is_mouse_inside_window "..tostring(defos.is_mouse_inside_window()))
end
function update_clipping_label()
if 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_window_size(100,100,1024,768)
end)
dirtylarry:button("toggle_fullscreen", action_id, action, function ()
defos.toggle_fullscreen()
end)
dirtylarry:button("toggle_maximize", action_id, action, function ()
defos.toggle_maximize()
end)
dirtylarry:button("disable_mouse_cursor", action_id, action, function ()
if self.is_cursor then
defos.disable_mouse_cursor()
self.is_cursor = false
change_mouse_label("Enable mouse cursor")
cursor.set_software_cursor_enabled(true)
else
defos.enable_mouse_cursor()
self.is_cursor = true
change_mouse_label("Disable mouse cursor")
cursor.set_software_cursor_enabled(false)
end
end)
dirtylarry:button("random_cursor_pos", action_id, action, function()
local x = math.random(1,2000)
local y = math.random(1,2000)
defos.move_cursor_to(x, y)
end)
dirtylarry:button("clip_cursor", action_id, action, function()
if not cursor_clipped then
defos.clip_cursor()
cursor_clipped = true
else
-- we have to restore to old rect as cursor is a shared resource
-- or moving the window will restore it
defos.restore_cursor_clip()
cursor_clipped = false
end
update_clipping_label()
end)
end