uisplitter.lua

main 86 lines · 2.5 KB Raw
Alex Alex Commit Client Upload Part 2 19/10/2026 00:06
1-- @docclass
2UISplitter = extends(UIWidget, "UISplitter")
3
4function UISplitter.create()
5 local splitter = UISplitter.internalCreate()
6 splitter:setFocusable(false)
7 splitter.relativeMargin = 'bottom'
8 return splitter
9end
10
11function UISplitter:onHoverChange(hovered)
12 -- Check if margin can be changed
13 local margin = (self.vertical and self:getMarginBottom() or self:getMarginRight())
14 if hovered and (self:canUpdateMargin(margin + 1) ~= margin or self:canUpdateMargin(margin - 1) ~= margin) then
15 if g_mouse.isCursorChanged() or g_mouse.isPressed() then return end
16 if self:getWidth() > self:getHeight() then
17 self.vertical = true
18 self.cursortype = 'vertical'
19 else
20 self.vertical = false
21 self.cursortype = 'horizontal'
22 end
23 self.hovering = true
24 g_mouse.pushCursor(self.cursortype)
25 if not self:isPressed() then
26 g_effects.fadeIn(self)
27 end
28 else
29 if not self:isPressed() and self.hovering then
30 g_mouse.popCursor(self.cursortype)
31 g_effects.fadeOut(self)
32 self.hovering = false
33 end
34 end
35end
36
37function UISplitter:onMouseMove(mousePos, mouseMoved)
38 if self:isPressed() then
39 --local currentmargin, newmargin, delta
40 if self.vertical then
41 local delta = mousePos.y - self:getY() - self:getHeight()/2
42 local newMargin = self:canUpdateMargin(self:getMarginBottom() - delta)
43 local currentMargin = self:getMarginBottom()
44 if newMargin ~= currentMargin then
45 self.newMargin = newMargin
46 if not self.event or self.event:isExecuted() then
47 self.event = addEvent(function()
48 self:setMarginBottom(self.newMargin)
49 end)
50 end
51 end
52 else
53 local delta = mousePos.x - self:getX() - self:getWidth()/2
54 local newMargin = self:canUpdateMargin(self:getMarginRight() - delta)
55 local currentMargin = self:getMarginRight()
56 if newMargin ~= currentMargin then
57 self.newMargin = newMargin
58 if not self.event or self.event:isExecuted() then
59 self.event = addEvent(function()
60 self:setMarginRight(self.newMargin)
61 end)
62 end
63 end
64 end
65 return true
66 end
67end
68
69function UISplitter:onMouseRelease(mousePos, mouseButton)
70 if not self:isHovered() then
71 g_mouse.popCursor(self.cursortype)
72 g_effects.fadeOut(self)
73 self.hovering = false
74 end
75end
76
77function UISplitter:onStyleApply(styleName, styleNode)
78 if styleNode['relative-margin'] then
79 self.relativeMargin = styleNode['relative-margin']
80 end
81end
82
83function UISplitter:canUpdateMargin(newMargin)
84 return newMargin
85end
86
Top