Skip to content

Commit

Permalink
Fixed: notifying the parent istead of dispatching the event. ALPHA.
Browse files Browse the repository at this point in the history
  • Loading branch information
Tourahi committed Dec 18, 2023
1 parent eda96f5 commit 77f0c70
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 25 deletions.
9 changes: 8 additions & 1 deletion src/Core/Control.moon
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ class Control
@makeTopWhenClicked = false

@focusEnabled = true

@focused = false

--- gets control id
-- @treturn string id
getId: =>
Expand Down Expand Up @@ -648,6 +649,12 @@ class Control
isFocusEnabled: =>
@focusEnabled

setFocused: (bool) =>
@focused = bool

getFocused: =>
@focused

--- list of functions to override when boundingbox is of type Box.
-- @table boxOverrides
@boxOverrides: {
Expand Down
58 changes: 34 additions & 24 deletions src/Core/Manager.moon
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,6 @@ Timer = love.timer
Mouse = love.mouse
Chrono = assert require MeowUI.cwd .. "Core.Chrono"

-- @local
dispatch = (control, name, ...) ->
control.events\dispatch control.events\getEvent(name),
...
if control\getNotifyParent!
p = control\getParent!
dispatch p, name, ...


-- @local
debug = (hitControl) ->
if hitControl and hitControl._d
Expand All @@ -33,6 +24,14 @@ debug = (hitControl) ->

class Manager extends Singleton

@dispatch: (control, name, ...) =>
control.events\dispatch control.events\getEvent(name),
...
if control\getNotifyParent!
p = control\getParent!
@notify p, name, ...


--- constructor.
new: =>
@rootControl = Root!
Expand All @@ -50,10 +49,11 @@ class Manager extends Singleton
--- updates the manager.
-- @tparam number dt
update: (dt) =>
-- if @focusControl then print @focusControl\getId!
Chrono.getInstance!\update dt
if @rootControl then @rootControl\update dt
if @focusControl and @focusControl.updateWhenFocused
dispatch @focusControl, "UI_UPDATE", dt
@@dispatch @focusControl, "UI_UPDATE", dt

--- draws the manager.
draw: =>
Expand All @@ -75,30 +75,40 @@ class Manager extends Singleton
hitControl = debug hitControl

if hitControl ~= @hoverControl
if @hoverControl then dispatch @hoverControl, "UI_MOUSE_LEAVE"
if @hoverControl then @@dispatch @hoverControl, "UI_MOUSE_LEAVE"

@hoverControl = hitControl

if hitControl then dispatch hitControl, "UI_MOUSE_ENTER"
if hitControl then @@dispatch hitControl, "UI_MOUSE_ENTER"

if @holdControl then dispatch @holdControl, "UI_MOUSE_MOVE", x, y, dx, dy
if @holdControl then @@dispatch @holdControl, "UI_MOUSE_MOVE", x, y, dx, dy
else
if @hoverControl then dispatch @hoverControl, "UI_MOUSE_MOVE", x, y, dx, dy
if @hoverControl then @@dispatch @hoverControl, "UI_MOUSE_MOVE", x, y, dx, dy

--- focuse on given control.
-- @tparam Control control
setFocus: (control) =>
if @focusControl == control then return

if @focusControl
dispatch @focusControl, "UI_UN_FOCUS"
@@dispatch @focusControl, "UI_UN_FOCUS"
@focusControl\rollBackDepth!
@focusControl\setFocused false

@focusControl = control
if control\getMakeTopWhenClicked! then control\makeTop!

if @focusControl
dispatch @focusControl, "UI_FOCUS"
@@dispatch @focusControl, "UI_FOCUS"
@focusControl\setFocused true

-- ALPHA
notify: (control, name) =>
switch name
when "UI_FOCUS"
control\setFocused true
when "UI_UN_FOCUS"
control\setFocused false

--- callback function triggered when a mouse button is pressed.
-- @tparam number x
Expand All @@ -114,7 +124,7 @@ class Manager extends Singleton
if MeowUI.debug then hitControl = debug hitControl

if hitControl
dispatch hitControl, "UI_MOUSE_DOWN", x, y, button, isTouch
@@dispatch hitControl, "UI_MOUSE_DOWN", x, y, button, isTouch
@holdControl = hitControl

if hitControl
Expand All @@ -129,7 +139,7 @@ class Manager extends Singleton
-- @tparam boolean isTouch
mousereleased: (x, y, button, isTouch) =>
if @holdControl
dispatch @holdControl, "UI_MOUSE_UP", x, y, button, isTouch
@@dispatch @holdControl, "UI_MOUSE_UP", x, y, button, isTouch
if @rootControl

hitControl = @rootControl\hitTest x, y
Expand All @@ -142,12 +152,12 @@ class Manager extends Singleton
@lastClickControl == @holdControl and
(Timer.getTime! - @lastClickTime <= 0.4)

dispatch @holdControl, "UI_DB_CLICK", @holdControl, x, y
@@dispatch @holdControl, "UI_DB_CLICK", @holdControl, x, y
@lastClickControl = nil
@lastClickTime = 0
else

dispatch @holdControl, "UI_CLICK", @holdControl, x, y
@@dispatch @holdControl, "UI_CLICK", @holdControl, x, y
@lastClickControl = @holdControl
@lastClickTime = Timer.getTime!

Expand All @@ -164,7 +174,7 @@ class Manager extends Singleton

while hitControl
@mousemoved Mouse\getX!, Mouse\getY!, 0, 0
if dispatch hitControl, "UI_WHELL_MOVE", x, y then return
if @@dispatch hitControl, "UI_WHELL_MOVE", x, y then return
hitControl = hitControl\getParent!

--- callback function triggered when a key is pressed.
Expand All @@ -173,17 +183,17 @@ class Manager extends Singleton
-- @tparam boolean isrepeat
keypressed: (key, scancode, isrepeat) =>
if key == "f1" then MeowUI.debug = not MeowUI.debug
if @focusControl then dispatch @focusControl, "UI_KEY_DOWN", key, scancode, isrepeat
if @focusControl then @@dispatch @focusControl, "UI_KEY_DOWN", key, scancode, isrepeat

--- callback function triggered when a keyboard key is released.
-- @tparam KeyConstant key
keyreleased: (key) =>
if @focusControl then dispatch @focusControl, "UI_KEY_UP", key
if @focusControl then @@dispatch @focusControl, "UI_KEY_UP", key

--- called when text has been entered by the user.
-- @tparam string text
textinput: (text) =>
if @focusControl then dispatch @focusControl, "UI_TEXT_INPUT", text
if @focusControl then @@dispatch @focusControl, "UI_TEXT_INPUT", text

--- resize the root control
-- @tparam number w
Expand Down

0 comments on commit 77f0c70

Please sign in to comment.