-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add coordinate conversion helper for GUI picking, & demo script (#14)
- Credit to subsoap / Pkeod for commissioning this feature.
- Loading branch information
Showing
4 changed files
with
102 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
|
||
-- A sample script to demonstrate GUI node hovering with the mouse. | ||
|
||
local coords = require "lowrezjam.render.coords" | ||
|
||
function init(self) | ||
msg.post(".", "acquire_input_focus") | ||
self.hearts = {} | ||
for i=1,3 do | ||
self.hearts[i] = gui.get_node("life"..i) | ||
end | ||
end | ||
|
||
local function update_hover(self, x, y) | ||
local hit_i, hit_heart | ||
for i,heart in ipairs(self.hearts) do | ||
if gui.pick_node(heart, x, y) then | ||
gui.set_alpha(heart, 0.5) | ||
hit_i, hit_heart = i, heart | ||
else | ||
gui.set_alpha(heart, 1) | ||
end | ||
end | ||
return hit_i, hit_heart | ||
end | ||
|
||
function on_input(self, action_id, action) | ||
if not action_id then | ||
local px, py = coords.action_to_gui_pick(action.x, action.y) | ||
update_hover(self, px, py) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
|
||
-- A module for converting mouse coordinates to GUI coordinates for checking collision with GUI nodes. | ||
|
||
local M = {} | ||
|
||
local proj_w = sys.get_config_int("display.width", 960) | ||
local proj_h = sys.get_config_int("display.height", 640) | ||
|
||
M.window_width, M.window_height = proj_w, proj_h | ||
M.game_width = 64 | ||
M.game_height = 64 | ||
M.scale_snap = true | ||
|
||
local function get_zoom() | ||
local zoom = math.min(M.window_width / M.game_width, M.window_height / M.game_height) | ||
if M.scale_snap then | ||
zoom = math.max(1, math.floor(zoom)) | ||
end | ||
return zoom | ||
end | ||
|
||
local function get_offset(zoom) | ||
local w, h = M.game_width * zoom, M.game_height * zoom | ||
return (M.window_width - w)/2, (M.window_height - h)/2 | ||
end | ||
|
||
function M.action_to_screen(ax, ay) | ||
local sx = (ax / proj_w) * M.window_width | ||
local sy = (ay / proj_h) * M.window_height | ||
return sx, sy | ||
end | ||
local action_to_screen = M.action_to_screen | ||
|
||
function M.screen_to_action(sx, sy) | ||
local ax = sx * proj_w / M.window_width | ||
local ay = sy * proj_h / M.window_height | ||
return ax, ay | ||
end | ||
local screen_to_action = M.screen_to_action | ||
|
||
function M.action_to_gui_pick(ax, ay) | ||
local sx, sy = action_to_screen(ax, ay) -- It's way less confusing to work in real screen coords. | ||
|
||
local action_width, action_height = screen_to_action(M.game_width, M.game_height) -- Bottom left of gui is always at 0, 0. | ||
local scalex, scaley = action_width/M.game_width, action_height/M.game_height | ||
|
||
local zoom = get_zoom() | ||
local ox, oy = get_offset(zoom) | ||
|
||
local x = (sx - ox)/zoom -- Zero to `width` inside canvas area. | ||
local y = (sy - oy)/zoom -- Zero to `height` inside canvas area. | ||
x, y = x * scalex, y * scaley | ||
|
||
return x, y -- NOTE: Not rounded to even pixels. | ||
end | ||
|
||
return M |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters