From 6cbd09a460e3b4cdc30436ef7db43c4e98ada62d Mon Sep 17 00:00:00 2001 From: Ryan Ward Date: Thu, 21 Dec 2023 02:14:42 -0500 Subject: [PATCH] Fixed canvas, working on simulating events --- core/canvas.lua | 97 ++++------------------------------- core/simulate.lua | 108 +++++++++++++++++++++++++++++++++++++++ elements/transitions.lua | 2 +- init.lua | 15 +++--- 4 files changed, 126 insertions(+), 96 deletions(-) create mode 100644 core/simulate.lua diff --git a/core/canvas.lua b/core/canvas.lua index 0592e6b..93b8b5d 100644 --- a/core/canvas.lua +++ b/core/canvas.lua @@ -1,98 +1,21 @@ local gui = require("gui") -local multi, thread = require("multi"):init() -local canvas = {} - --- Run the canvas logic within another processor -local proc = gui:newProcessor("Canvas Handler") -local updater = proc:newLoop().OnLoop - -local draw_handler = gui.draw_handler - -function canvas:newCanvas() - local c = {} - setmetatable(c, gui) - local active = false - c.type = gui.TYPE_FRAME - c.children = {} - c.dualDim = gui:newDualDim() - c.x = 0 - c.y = 0 - - local w, h = love.graphics.getDimensions() - c.dualDim.offset.size.x = w - c.dualDim.offset.size.y = h - c.w = w - c.h = h - c.parent = c - - table.insert(canvas, c) - - proc:newThread(function() - while true do - print("Holding...") - thread.hold(c.isActive) - local children = c:getAllChildren() - for i = 1, #children do - local child = children[i] - if child.effect then - child.effect(function() draw_handler(child, true) end) - else - draw_handler(child, true) - end - end - first_loop = true - end - end) - - function c:isActive(b) - if b == nil then - return active - end - active = b - end - - function c:OnUpdate(func) - if type(self) == "function" then func = self end - updater(function() func(c) end) - end - - function c:newThread(func) - return proc:newThread("ThreadHandler<" .. self.type .. ">", func, self, thread) - end +function newCanvas() + local c = gui:newVirtualFrame(0,0,0,0,0,0,1,1) function c:swap(c1, c2) - local ch1 = c1:getChildren() - local ch2 = c2:getChildren() - for i = 1, #ch1 do - ch1[i]:setParent(c2) + local temp = c1.children + c1.children = c2.children + c2.children = temp + for i,v in pairs(c1.children) do + v.parent = c1 end - for i = 1, #ch2 do - ch2[i]:setParent(c1) + for i,v in pairs(c2.children) do + v.parent = c2 end end return c end -gui.Events.OnResized(proc:newFunction(function(w, h) - for i = 1, #canvas do - local c = canvas[i] - if gui.aspect_ratio then - local nw, nh, xt, yt = gui.GetSizeAdjustedToAspectRatio(w, h) - c.x = xt - c.y = yt - c.dualDim.offset.size.x = nw - c.dualDim.offset.size.y = nh - c.w = nw - c.h = nh - else - c.dualDim.offset.size.x = w - c.dualDim.offset.size.y = h - c.w = w - c.h = h - end - end -end)) - -return canvas \ No newline at end of file +return newCanvas \ No newline at end of file diff --git a/core/simulate.lua b/core/simulate.lua new file mode 100644 index 0000000..42e147b --- /dev/null +++ b/core/simulate.lua @@ -0,0 +1,108 @@ +local gui = require("gui") +local multi, thread = require("multi"):init() +local transition = require("gui.elements.transitions") + +-- Triggers press then release +local function getPosition(obj, x, y) + if not x or y then + local cx, cy, w, h = obj:getAbsolutes() + return cx + w/2, cy + h/2 + else + return x, y + end +end + +proc = gui:getProcessor() + +local simulate = {} +local cursor = false +local smooth = false + +function simulate:moveCursor(bool) + cursor = bool +end + +function simulate:smoothMovement(bool) + smooth = bool +end + +function simulate:Press(button, x, y, istouch) + if self then + x, y = getPosition(self, x, y) + elseif x == nil or y == nil then + x, y = love.mouse.getPosition() + end + if cursor then + love.mouse.setPosition(x, y) + end + gui.Events.OnMousePressed:Fire(x, y, button or gui.MOUSE_PRIMARY, istouch or false) +end + +function simulate:Release(button, x, y, istouch) + if self then + x, y = getPosition(self, x, y) + elseif x == nil or y == nil then + x, y = love.mouse.getPosition() + end + if cursor then + love.mouse.setPosition(x, y) + end + gui.Events.OnMouseReleased:Fire(x, y, button or gui.MOUSE_PRIMARY, istouch or false) +end + +simulate.Click = proc:newFunction(function(self, button, x, y, istouch) + if self then + x, y = getPosition(self, x, y) + elseif x == nil or y == nil then + x, y = love.mouse.getPosition() + end + if cursor then + love.mouse.setPosition(x, y) + end + gui.Events.OnMousePressed:Fire(x, y, button or gui.MOUSE_PRIMARY, istouch or false) + thread.skip(1) + gui.Events.OnMouseReleased:Fire(x, y, button or gui.MOUSE_PRIMARY, istouch or false) +end, true) + +simulate.Move = proc:newFunction(function(self, dx, dy, x, y, istouch) + local dx, dy = dx or 0, dy or 0 + + if self then + x, y = getPosition(self, x, y) + elseif x == nil or y == nil then + x, y = love.mouse.getPosition() + end + gui.Events.OnMouseMoved:Fire(x, y, 0, 0, istouch or false) + thread.skip(1) + if smooth then + local gx = transition.glide(0, dx, .25) + local gy = transition.glide(0, dy, .25) + local xx = gx() + xx.OnStep(function(p) + _x, _y = love.mouse.getPosition() + if cursor then + love.mouse.setPosition(x + p, _y) + else + gui.Events.OnMouseMoved:Fire(x + p, _y, 0, 0, istouch or false) + end + end) + local yy = gy() + yy.OnStep(function(p) + _x, _y = love.mouse.getPosition() + if cursor then + love.mouse.setPosition(_x, y + p) + else + gui.Events.OnMouseMoved:Fire(_x, y + p, 0, 0, istouch or false) + end + end) + thread.hold(xx.OnStop * yy.OnStop) + gui.Events.OnMouseMoved:Fire(x + dx, y + dy, 0, 0, istouch or false) + else + if cursor then + love.mouse.setPosition(x + dx, y + dy) + end + gui.Events.OnMouseMoved:Fire(x + dx, y + dy, 0, 0, istouch or false) + end +end, true) + +return simulate \ No newline at end of file diff --git a/elements/transitions.lua b/elements/transitions.lua index a036305..003ed36 100644 --- a/elements/transitions.lua +++ b/elements/transitions.lua @@ -15,7 +15,7 @@ transition.__call = function(t, start, stop, time, ...) local args = {...} return function() if not start or not stop then return multi.error("start and stop must be supplied") end - if start == stop then return multi.error("start and stop cannot be the same!") end + if start == stop then multi.print("start and stop cannot be the same!") return {OnStep = function() end, OnStop = function() end} end local handle = t.func(t, start, stop, time or 1, unpack(args)) return { OnStep = handle.OnStatus, diff --git a/init.lua b/init.lua index 133325d..af98b2b 100644 --- a/init.lua +++ b/init.lua @@ -327,6 +327,11 @@ function gui:bottomStack() table.insert(siblings, 1, self) end +function gui:OnUpdate(func) -- Not crazy about this approach, will probably rework this + if type(self) == "function" then func = self end + mainupdater(function() func(c) end) +end + local mainupdater = updater:newLoop().OnLoop function gui:canPress(mx, my) -- Get the intersection of the clip area and the self then test with the clip, otherwise test as normal @@ -480,8 +485,8 @@ function gui:newBase(typ, x, y, w, h, sx, sy, sw, sh, virtual) end return false end - - setmetatable(c, gui) + setmetatable(c, self) + c.__index = self.__index c.__variables = {clip = {false, 0, 0, 0, 0}} c.active = true c.type = typ @@ -586,11 +591,6 @@ function gui:newBase(typ, x, y, w, h, sx, sy, sw, sh, virtual) function c:respectHierarchy(bool) hierarchy = bool end - function c:OnUpdate(func) -- Not crazy about this approach, will probably rework this - if type(self) == "function" then func = self end - mainupdater(function() func(c) end) - end - local function centerthread() local centerfunc = function() return centerX or centerY -- If the condition is true it acts like a yield @@ -1488,7 +1488,6 @@ local processors = { gui.draw = drawer.run gui.update = function() for i = 1, #processors do - print(i) processors[i]() end end