Skip to content

Commit

Permalink
added undraw
Browse files Browse the repository at this point in the history
  • Loading branch information
qeffects committed Jul 21, 2021
1 parent 0f99994 commit 2144c5a
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 14 deletions.
26 changes: 26 additions & 0 deletions core/element.lua
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ function element:new(param, immediate, w, h, flags)
isSetup = false,
pendingUpdate = true,
needsRendering = true,
active = true,
remove = false,
--Unused for now?
calculatedDimensions = true,
Expand Down Expand Up @@ -285,8 +286,13 @@ function element:renderWrapper()

self.context:unset()
end

local lg = love.graphics
function element:externalRender()
if not self.settings.active then
return
end

local cnvs = getCanvas()
love.graphics.push('all')

Expand Down Expand Up @@ -348,6 +354,9 @@ function element:externalRender()
end

function element:externalUpdate()
if not self.settings.active then
return
end
self.context:set()
self.context:zIndex()
if ((not self.settings.failedCanvas
Expand Down Expand Up @@ -402,6 +411,10 @@ function element:draw(x, y, w, h)
if w then self.view.w = w end
if h then self.view.h = h end

if not self.settings.active then
self:redraw()
end

local cx = context.getContext()
if cx then
if cx:childRender(self) then
Expand Down Expand Up @@ -429,10 +442,23 @@ end
---Destroys this element
function element:destroy()
self.settings.remove = true
self.settings.inserted = false
self.settings.active = false
self.settings.firstDraw = true
self.settings.isSetup = false
self:onDestroy()
self.context:destroy()
end

function element:redraw()
self.settings.active = true
self.context:redraw()
end

---Stops rendering, updates and draw
function element:undraw()
self.settings.active = false
self.context:undraw()
end

return element
26 changes: 13 additions & 13 deletions core/input.lua
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ function input.eventHandlers.mousereleased(x, y, btn)
local captured = false
if input.subscriptions.clicked then
for index, sub in ipairs(input.subscriptions.clicked) do
if sub.currentEvent then
if sub.currentEvent and sub.active and sub.stack.element.settings.active then
sub.currentEvent = false
captured = true
if sub.cleanUp then
Expand All @@ -219,7 +219,7 @@ function input.eventHandlers.mousereleased(x, y, btn)

if input.subscriptions.dragged then
for index, sub in ipairs(input.subscriptions.dragged) do
if sub.currentEvent then
if sub.currentEvent and sub.active and sub.stack.element.settings.active then
sub.currentEvent = false
captured = true
if sub.cleanUp then
Expand All @@ -231,7 +231,7 @@ function input.eventHandlers.mousereleased(x, y, btn)

if input.subscriptions.mousereleased then
for index, sub in ipairs(input.subscriptions.mousereleased) do
if sub.active and sub:checkInside(x, y) then
if sub.active and sub.stack.element.settings.active and sub:checkInside(x, y) then
sub:emit(x-sub.x, y-sub.y, btn)
captured = true
end
Expand All @@ -241,7 +241,7 @@ function input.eventHandlers.mousereleased(x, y, btn)

if input.subscriptions.mousereleased_outside then
for index, sub in ipairs(input.subscriptions.mousereleased_outside) do
if sub.active and sub:checkOutside(x, y) then
if sub.active and sub.stack.element.settings.active and sub:checkOutside(x, y) then
sub:emit(x-sub.x, y-sub.y, btn)
captured = true
end
Expand All @@ -259,7 +259,7 @@ function input.eventHandlers.mousepressed(x, y, btn)
for index, sub in ipairs(input.subscriptions.clicked) do
local succ = sub:checkInside(x, y)

if succ and sub.active then
if succ and sub.active and sub.stack.element.settings.active then
sub.cleanUp = sub:emit(x-sub.x, y-sub.y, btn) or dummyfunc
sub.currentEvent = true
return true
Expand All @@ -270,7 +270,7 @@ function input.eventHandlers.mousepressed(x, y, btn)

if input.subscriptions.dragged then
for index, sub in ipairs(input.subscriptions.dragged) do
if sub.active and sub:checkInside(x, y) then
if sub.active and sub.stack.element.settings.active and sub:checkInside(x, y) then
sub.currentEvent = true
return true
end
Expand All @@ -280,7 +280,7 @@ function input.eventHandlers.mousepressed(x, y, btn)

if input.subscriptions.mousepressed then
for index, sub in ipairs(input.subscriptions.mousepressed) do
if sub.active and sub:checkInside(x, y) then
if sub.active and sub.stack.element.settings.active and sub:checkInside(x, y) then
sub:emit(x-sub.x, y-sub.y, btn)
return true
end
Expand All @@ -290,7 +290,7 @@ function input.eventHandlers.mousepressed(x, y, btn)

if input.subscriptions.mousepressed_outside then
for index, sub in ipairs(input.subscriptions.mousepressed_outside) do
if sub.active and sub:checkOutside(x, y) then
if sub.active and sub.stack.element.settings.active and sub:checkOutside(x, y) then
sub:emit(x-sub.x, y-sub.y, btn)
return true
end
Expand All @@ -304,7 +304,7 @@ function input.eventHandlers.keypressed(btn, btncode)
local captured = false
if input.subscriptions.keypressed then
for index, sub in ipairs(input.subscriptions.keypressed) do
if sub.active then
if sub.active and sub.stack.element.settings.active then
sub:emit(btn, btncode)
captured = true
end
Expand All @@ -319,7 +319,7 @@ function input.eventHandlers.keyreleased(btn, btncode)
local captured = false
if input.subscriptions.keyreleased then
for index, sub in ipairs(input.subscriptions.keyreleased) do
if sub.active then
if sub.active and sub.stack.element.settings.active then
sub:emit(btn, btncode)
captured = true
end
Expand All @@ -333,7 +333,7 @@ function input.eventHandlers.textinput(text)
local captured = false
if input.subscriptions.textinput then
for index, sub in ipairs(input.subscriptions.textinput) do
if sub.active then
if sub.active and sub.stack.element.settings.active then
sub:emit(text)
captured = true
end
Expand All @@ -350,7 +350,7 @@ function input.eventHandlers.mousemoved(x, y, dx, dy)
for index, sub in ipairs(input.subscriptions.hover) do
local succ = sub:checkInside(x, y)

if sub.active and not sub.currentEvent and succ then
if sub.active and sub.stack.element.settings.active and not sub.currentEvent and succ then
sub.cleanUp = sub:emit(x-sub.x, y-sub.y, dx, dy) or dummyfunc
sub.currentEvent = true
return true
Expand All @@ -367,7 +367,7 @@ function input.eventHandlers.mousemoved(x, y, dx, dy)

if input.subscriptions.dragged then
for index, sub in ipairs(input.subscriptions.dragged) do
if sub.active and sub.currentEvent then
if sub.active and sub.stack.element.settings.active and sub.currentEvent then
if not sub.cleanUp then
sub.cleanUp = sub:emit(x-sub.x, y-sub.y, dx, dy) or dummyfunc
else
Expand Down
12 changes: 12 additions & 0 deletions core/stack.lua
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,18 @@ function context:destroy()
end
end

function context:undraw()
for i = 1, #self.childrenContexts do
self.childrenContexts[i].element:undraw()
end
end

function context:redraw()
for i = 1, #self.childrenContexts do
self.childrenContexts[i].element:redraw()
end
end

function context:getCanvasIndex(forCanvas)
if self.parentCtx then
if self.element.settings.hasCanvas then
Expand Down
8 changes: 7 additions & 1 deletion docs/core/Element.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,13 @@ Inside it renders immediately, so you can put it inbetween other graphics operat

`Element:destroy()`

Use destroy to remove this element from the scene
Use destroy to completely and irreversibly remove this element from the scene

---

`Element:undraw()`

Use undraw to hide this element for now with the intention of re-drawing it sometime later, to do that just :draw() it again

---

Expand Down

0 comments on commit 2144c5a

Please sign in to comment.