defos/example/example.gui_script
2018-12-31 13:07:22 +02:00

242 lines
7.5 KiB
Plaintext

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
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
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()))
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_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("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