-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Continuation of "semi support for touchscreens + clickRegion" (#10)
* Create clickRegion.lua button without stuff being rendered on screen uwu owo (also got monitor support lol) * Added touch support for monitors to buttons yes. * Update clickRegion.lua idk something didn't work don't sue me * fix cosmetic problems with button.lua and clickRegion.lua --------- Co-authored-by: tizu69 <60812901+tizu69@users.noreply.github.com>
- Loading branch information
1 parent
315e8d6
commit 3199af8
Showing
2 changed files
with
56 additions
and
7 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
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,42 @@ | ||
local PrimeUI = require "util" -- DO NOT COPY THIS LINE | ||
local expect = require "cc.expect".expect -- DO NOT COPY THIS LINE | ||
-- Start copying below this line. -- | ||
|
||
--- Creates a clickable region on screen without any content. | ||
---@param win window The window to draw on | ||
---@param x number The X position of the button | ||
---@param y number The Y position of the button | ||
---@param width number The width of the inner box | ||
---@param height number The height of the inner box | ||
---@param action function|string A function to call when clicked, or a string to send with a `run` event | ||
---@param periphName string|nil The name of the monitor peripheral, or nil (set if you're using a monitor - events will be filtered to that monitor) | ||
function PrimeUI.clickRegion(win, x, y, width, height, action, periphName) | ||
expect(1, win, "table") | ||
expect(2, x, "number") | ||
expect(3, y, "number") | ||
expect(4, width, "number") | ||
expect(5, height, "number") | ||
expect(6, action, "function", "string") | ||
expect(7, periphName, "string", "nil") | ||
PrimeUI.addTask(function() | ||
-- Get the screen position and add a click handler. | ||
local screenX, screenY = PrimeUI.getWindowPos(win, x, y) | ||
local buttonDown = false | ||
while true do | ||
local event, button, clickX, clickY = os.pullEvent() | ||
if (event == "monitor_touch" and periphName == button) | ||
or (event == "mouse_down" and button == 1 and periphName == nil) then | ||
-- Finish a click event. | ||
if clickX >= screenX and clickX < screenX + width | ||
and clickY >= screenY and clickY < screenY + height then | ||
-- Trigger the action. | ||
if type(action) == "string" then | ||
PrimeUI.resolve("clickRegion", action) | ||
else | ||
action() | ||
end | ||
end | ||
end | ||
end | ||
end) | ||
end |