defos/example/example.gui_script
2018-02-18 10:43:09 +02:00

206 lines
5.6 KiB
Plaintext

local dirtylarry = require "dirtylarry/dirtylarry"
local cursor = require("example.cursor.cursor")
local cursor_url = require("example.html5_cursor")
cursor_clipped = false
local system_name = sys.get_sys_info().system_name
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
if system_name == "Windows" then
defos.restore_cursor_clip()
end
elseif event == window.WINDOW_EVENT_FOCUS_GAINED then
if system_name == "Windows" then
if cursor_clipped then
defos.clip_cursor()
end
end
elseif event == window.WINDOW_EVENT_RESIZED then
if system_name == "Windows" then
if cursor_clipped then
defos.clip_cursor()
end
end
end
end
function init(self)
local appname = sys.get_config("project.title")
-- 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 == "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
local cursor_resource = resource.load("/resources/"..v)
local raw_bytes = buffer.get_bytes(cursor_resource, hash("data"))
local cursor_path = sys.get_save_file(appname, v)
local f = io.open(cursor_path, "wb")
f:write(raw_bytes)
f:flush()
f:close()
print(cursor_path)
table.insert(self.cursors, cursor_path)
end
end
if system_name == "Darwin" then
table.insert(self.cursors, {
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, cursor_url)
end
defos.on_mouse_enter(function ()
print("Mouse entered view")
if self.is_cursor == false then
defos.disable_mouse_cursor()
end
end)
defos.on_mouse_leave(function ()
print("Mouse left view")
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 system_name == "Windows" then
if defos.is_console_visible() then
defos.hide_console()
else
defos.show_console()
end
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("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)
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()))
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_view_size(nil, nil, 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)
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)
end