@@ -10,6 +10,7 @@ local RenderPlugin = require("plugin.render")
1010local LayoutPlugin = require (" plugin.layout" )
1111local TextPlugin = require (" plugin.text" )
1212local UIPlugin = require (" plugin.ui" )
13+ local OverlayPlugin = require (" plugin.overlay" )
1314
1415--- @alias Message
1516--- | { type: "onWindowCreate", window: Window }
@@ -60,6 +61,7 @@ local UIPlugin = require("plugin.ui")
6061--- @field text plugin.Text
6162--- @field ui plugin.UI
6263--- @field layout plugin.Layout
64+ --- @field overlay plugin.Overlay
6365
6466--- @alias App.Tool " brush" | " eraser" | " fill" | " pencil" | " text" | " select" | " square" | " circle" | " line" | " curve"
6567
@@ -74,6 +76,7 @@ local UIPlugin = require("plugin.ui")
7476--- @field isDrawing boolean
7577--- @field currentColor { r : number , g : number , b : number , a : number }
7678--- @field currentAction App.Action
79+ --- @field startTime number
7780local App = {}
7881App .__index = App
7982
@@ -84,10 +87,12 @@ function App.new()
8487 self .plugins .text = TextPlugin .new (self .plugins .render )
8588 self .plugins .layout = LayoutPlugin .new (function (w ) return self :view (w ) end , self .plugins .text )
8689 self .plugins .ui = UIPlugin .new (self .plugins .layout , self .plugins .render )
90+ self .plugins .overlay = OverlayPlugin .new (self .plugins .render )
8791
8892 self .isDrawing = false
8993 self .currentColor = { r = 0 , g = 0 , b = 0 , a = 1 }
9094 self .currentAction = { tool = " brush" }
95+ self .startTime = os.clock ()
9196
9297 return self
9398end
@@ -650,6 +655,14 @@ function App:view(window)
650655 elementHeight = elementHeight ,
651656 }
652657 end ),
658+ Element .new (" div" )
659+ :withStyle ({
660+ bgImage = self .plugins .overlay :getTexture (window ),
661+ width = { rel = 1 },
662+ height = { rel = 1 },
663+ margin = { right = 20 , left = 20 , top = 20 , bottom = 20 },
664+ position = " relative" ,
665+ }),
653666 }),
654667
655668 Element .new (" div" )
@@ -674,6 +687,46 @@ function App:event(event, handler)
674687 return windowUpdate
675688 end
676689
690+ if event .name == " redraw" then
691+ local ctx = self .plugins .render :getContext (event .window )
692+ self .plugins .render :draw (ctx )
693+
694+ self .plugins .overlay :clear (event .window )
695+
696+ if self .currentAction .tool == " select" and self .currentAction .start then
697+ local start = self .currentAction .start
698+ local finish = self .currentAction .finish or start
699+
700+ local x1 = start .x
701+ local y1 = start .y
702+ local x2 = finish .x
703+ local y2 = finish .y
704+
705+ local boxX = math.min (x1 , x2 )
706+ local boxY = math.min (y1 , y2 )
707+ local boxW = math.abs (x2 - x1 )
708+ local boxH = math.abs (y2 - y1 )
709+
710+ self .plugins .overlay :addBox (
711+ event .window ,
712+ boxX , boxY , boxW , boxH ,
713+ { r = 0 , g = 0 , b = 0 , a = 1 },
714+ 2
715+ )
716+ end
717+
718+ local time = os.clock () - self .startTime
719+ self .plugins .overlay :draw (event .window , " marching_ants" , time )
720+
721+ ctx .renderCtx :swapBuffers ()
722+
723+ if self .currentAction .tool == " select" and (self .currentAction .start or self .isDrawing ) then
724+ handler :requestRedraw (event .window )
725+ end
726+
727+ return nil
728+ end
729+
677730 local renderUpdate = self .plugins .render :event (event , handler )
678731 if renderUpdate then
679732 return renderUpdate
@@ -691,6 +744,7 @@ function App:update(message, window)
691744 if message .type == " onWindowCreate" then
692745 -- Now we can initialize assets for a specific window
693746 self .plugins .render :register (window )
747+ self .plugins .overlay :register (window )
694748
695749 if window == self .plugins .window .mainCtx .window then
696750 self .resources = self :makeResources ()
@@ -719,6 +773,7 @@ function App:update(message, window)
719773 local x = (message .x / message .elementWidth ) * 800
720774 local y = (message .y / message .elementHeight ) * 600
721775 self .currentAction .start = { x = x , y = y }
776+ self .currentAction .finish = nil
722777 elseif self .currentAction .tool == " pencil" then
723778 self .resources .compute :stamp (
724779 (message .x / message .elementWidth ) * 800 ,
@@ -751,9 +806,11 @@ function App:update(message, window)
751806 local startPos = { x = math.min (start .x , x ), y = math.min (start .y , y ) }
752807 local finishPos = { x = math.max (start .x , x ), y = math.max (start .y , y ) }
753808 self .resources .compute :setSelection (startPos .x , startPos .y , finishPos .x , finishPos .y )
809+ self .currentAction .finish = { x = x , y = y }
754810 end
755811 end
756812 self .isDrawing = false
813+ window .shouldRedraw = true
757814 elseif message .type == " Hovered" then
758815 if self .isDrawing then
759816 if self .currentAction .tool == " eraser" then
@@ -776,6 +833,10 @@ function App:update(message, window)
776833 1 ,
777834 self .currentColor
778835 )
836+ elseif self .currentAction .tool == " select" and self .currentAction .start then
837+ local x = (message .x / message .elementWidth ) * 800
838+ local y = (message .y / message .elementHeight ) * 600
839+ self .currentAction .finish = { x = x , y = y }
779840 end
780841 self .plugins .ui :refreshView (window )
781842 end
@@ -785,8 +846,10 @@ function App:update(message, window)
785846 elseif message .type == " ToolClicked" then
786847 if message .tool == " select" then
787848 self .resources .compute :resetSelection ()
849+ self .currentAction = { tool = message .tool }
850+ else
851+ self .currentAction = { tool = message .tool }
788852 end
789- self .currentAction = { tool = message .tool }
790853 self .plugins .ui :refreshView (window )
791854 elseif message .type == " ClearClicked" then
792855 local textureManager = self .plugins .render .sharedResources .textureManager
0 commit comments