@@ -11,9 +11,21 @@ local VisualElement = setmetatable({}, BaseElement)
1111VisualElement .__index = VisualElement
1212
1313--- @property x number 1 The horizontal position relative to parent
14- VisualElement .defineProperty (VisualElement , " x" , {default = 1 , type = " number" , canTriggerRender = true })
14+ VisualElement .defineProperty (VisualElement , " x" , {default = 1 , type = " number" , canTriggerRender = true , setter = function (self , value )
15+ if self .parent then
16+ self .parent .set (" childrenSorted" , false )
17+ self .parent .set (" childrenEventsSorted" , false )
18+ end
19+ return value
20+ end })
1521--- @property y number 1 The vertical position relative to parent
16- VisualElement .defineProperty (VisualElement , " y" , {default = 1 , type = " number" , canTriggerRender = true })
22+ VisualElement .defineProperty (VisualElement , " y" , {default = 1 , type = " number" , canTriggerRender = true , setter = function (self , value )
23+ if self .parent then
24+ self .parent .set (" childrenSorted" , false )
25+ self .parent .set (" childrenEventsSorted" , false )
26+ end
27+ return value
28+ end })
1729--- @property z number 1 The z-index for layering elements
1830VisualElement .defineProperty (VisualElement , " z" , {default = 1 , type = " number" , canTriggerRender = true , setter = function (self , value )
1931 if self .parent then
@@ -29,9 +41,21 @@ VisualElement.defineProperty(VisualElement, "constraints", {
2941})
3042
3143--- @property width number 1 The width of the element
32- VisualElement .defineProperty (VisualElement , " width" , {default = 1 , type = " number" , canTriggerRender = true })
44+ VisualElement .defineProperty (VisualElement , " width" , {default = 1 , type = " number" , canTriggerRender = true , setter = function (self , value )
45+ if self .parent then
46+ self .parent .set (" childrenSorted" , false )
47+ self .parent .set (" childrenEventsSorted" , false )
48+ end
49+ return value
50+ end })
3351--- @property height number 1 The height of the element
34- VisualElement .defineProperty (VisualElement , " height" , {default = 1 , type = " number" , canTriggerRender = true })
52+ VisualElement .defineProperty (VisualElement , " height" , {default = 1 , type = " number" , canTriggerRender = true , setter = function (self , value )
53+ if self .parent then
54+ self .parent .set (" childrenSorted" , false )
55+ self .parent .set (" childrenEventsSorted" , false )
56+ end
57+ return value
58+ end })
3559--- @property background color black The background color
3660VisualElement .defineProperty (VisualElement , " background" , {default = colors .black , type = " color" , canTriggerRender = true })
3761--- @property foreground color white The text/foreground color
@@ -75,6 +99,7 @@ VisualElement.combineProperties(VisualElement, "size", "width", "height")
7599VisualElement .combineProperties (VisualElement , " color" , " foreground" , " background" )
76100
77101--- @event onClick {button string, x number, y number} Fired on mouse click
102+ --- @event onDoubleClick {button, x, y} Fired on double click (two clicks within 400ms)
78103--- @event onClickUp {button, x, y} Fired on mouse button release
79104--- @event onRelease {button, x, y} Fired when mouse leaves while clicked
80105--- @event onDrag {button, x, y} Fired when mouse moves while clicked
@@ -91,11 +116,12 @@ VisualElement.defineEvent(VisualElement, "focus")
91116VisualElement .defineEvent (VisualElement , " blur" )
92117
93118VisualElement .registerEventCallback (VisualElement , " Click" , " mouse_click" , " mouse_up" )
119+ VisualElement .registerEventCallback (VisualElement , " DoubleClick" , " mouse_double_click" , " mouse_click" )
94120VisualElement .registerEventCallback (VisualElement , " ClickUp" , " mouse_up" , " mouse_click" )
95121VisualElement .registerEventCallback (VisualElement , " Drag" , " mouse_drag" , " mouse_click" , " mouse_up" )
96122VisualElement .registerEventCallback (VisualElement , " Scroll" , " mouse_scroll" )
97123VisualElement .registerEventCallback (VisualElement , " Enter" , " mouse_enter" , " mouse_move" )
98- VisualElement .registerEventCallback (VisualElement , " LeEave " , " mouse_leave" , " mouse_move" )
124+ VisualElement .registerEventCallback (VisualElement , " Leave " , " mouse_leave" , " mouse_move" )
99125VisualElement .registerEventCallback (VisualElement , " Focus" , " focus" , " blur" )
100126VisualElement .registerEventCallback (VisualElement , " Blur" , " blur" , " focus" )
101127VisualElement .registerEventCallback (VisualElement , " Key" , " key" , " key_up" )
@@ -780,10 +806,22 @@ end
780806--- @param y number The y position of the click
781807--- @return boolean clicked Whether the element was clicked
782808--- @protected
809+ local DOUBLE_CLICK_THRESHOLD = 0.4 -- seconds
810+
783811function VisualElement :mouse_click (button , x , y )
784812 if self :isInBounds (x , y ) then
785813 self :setState (" clicked" )
786- self :fireEvent (" mouse_click" , button , self :getRelativePosition (x , y ))
814+ local rx , ry = self :getRelativePosition (x , y )
815+ local now = os.clock ()
816+ local last = self ._lastClick
817+ if last and (now - last .time ) <= DOUBLE_CLICK_THRESHOLD
818+ and last .button == button then
819+ self ._lastClick = nil
820+ self :fireEvent (" mouse_double_click" , button , rx , ry )
821+ else
822+ self ._lastClick = { time = now , button = button }
823+ self :fireEvent (" mouse_click" , button , rx , ry )
824+ end
787825 return true
788826 end
789827 return false
@@ -824,16 +862,16 @@ end
824862--- @protected
825863function VisualElement :mouse_move (_ , x , y )
826864 if (x == nil )or (y == nil )then return false end
827- local hover = self . getResolved (" hover" )
865+ local hover = self : hasState (" hover" )
828866 if (self :isInBounds (x , y ))then
829867 if (not hover )then
830- self . set (" hover" , true )
868+ self : setState (" hover" )
831869 self :fireEvent (" mouse_enter" , self :getRelativePosition (x , y ))
832870 end
833871 return true
834872 else
835873 if (hover )then
836- self . set (" hover" , false )
874+ self : unsetState (" hover" )
837875 self :fireEvent (" mouse_leave" , self :getRelativePosition (x , y ))
838876 end
839877 end
@@ -899,13 +937,6 @@ function VisualElement:setFocused(focused, internal)
899937 return self
900938end
901939
902- --- Gets whether this element is focused
903- --- @shortDescription Checks if element is focused
904- --- @return boolean isFocused
905- function VisualElement :isFocused ()
906- return self :hasState (" focused" )
907- end
908-
909940--- @shortDescription Handles a focus event
910941--- @protected
911942function VisualElement :focus ()
916947--- @protected
917948function VisualElement :blur ()
918949 self :fireEvent (" blur" )
919- -- Attempt to clear cursor; signature may expect (x,y,blink,fg,bg)
920- pcall (function () self :setCursor (1 ,1 ,false , self .get and self .getResolved (" foreground" )) end )
950+ self :setCursor (1 , 1 , false , self .getResolved (" foreground" ))
921951end
922952
923953--- Gets whether this element is focused
0 commit comments