forked from PathOfBuildingCommunity/PathOfBuilding
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathControlHost.lua
More file actions
124 lines (115 loc) · 3.63 KB
/
Copy pathControlHost.lua
File metadata and controls
124 lines (115 loc) · 3.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
-- Path of Building
--
-- Class: Control Host
-- Host for UI controls
--
local t_insert = table.insert
local t_sort = table.sort
local ControlHostClass = newClass("ControlHost", function(self)
self.controls = { }
end)
function ControlHostClass:SelectControl(newSelControl)
if self.selControl == newSelControl then
return
end
if self.selControl then
if self.selControl.selDragActive and self.selControl.dragTargetList then
return
end
self.selControl:SetFocus(false)
end
self.selControl = newSelControl
if self.selControl then
self.selControl:SetFocus(true)
end
end
function ControlHostClass:GetMouseOverControl()
for _, control in pairs(self.controls) do
if control.IsMouseOver and control:IsMouseOver() then
return control
end
end
end
function ControlHostClass:ProcessControlsInput(inputEvents, viewPort)
for id, event in ipairs(inputEvents) do
if event.type == "KeyDown" then
if self.selControl then
self:SelectControl(self.selControl:OnKeyDown(event.key, event.doubleClick))
inputEvents[id] = nil
end
if not self.selControl and event.key:match("BUTTON") then
self:SelectControl()
if isMouseInRegion(viewPort) then
local mOverControl = self:GetMouseOverControl()
if mOverControl and mOverControl.OnKeyDown then
self:SelectControl(mOverControl:OnKeyDown(event.key, event.doubleClick))
inputEvents[id] = nil
end
end
end
elseif event.type == "KeyUp" then
local selControl = self.selControl
if selControl then
if selControl.OnKeyUp then
self:SelectControl(selControl:OnKeyUp(event.key))
end
inputEvents[id] = nil
end
local mOverControl = self:GetMouseOverControl(viewPort)
-- Avoid calculating isMouseInRegion as much as possible as it's expensive
if mOverControl and (not selControl or mOverControl.OnHoverKeyUp) then
if isMouseInRegion(viewPort) then
if not selControl and mOverControl.OnKeyUp and mOverControl:OnKeyUp(event.key) then
inputEvents[id] = nil
end
if mOverControl.OnHoverKeyUp then
mOverControl:OnHoverKeyUp(event.key)
end
end
end
elseif event.type == "Char" then
if self.selControl then
if self.selControl.OnChar then
self:SelectControl(self.selControl:OnChar(event.key))
end
inputEvents[id] = nil
end
end
end
end
function ControlHostClass:DrawControls(viewPort, selControl)
local negZOrder = nil
local posZOrder = nil
for _, control in pairs(self.controls) do
if control.zOrder and control:IsShown() and control.Draw then
if control.zOrder < 0 then
negZOrder = negZOrder or { }
t_insert(negZOrder, control)
elseif control.zOrder > 0 then
posZOrder = posZOrder or { }
t_insert(posZOrder, control)
end
end
end
if negZOrder then
t_sort(negZOrder, function(a, b)
return a.zOrder < b.zOrder
end)
for _, control in ipairs(negZOrder) do
control:Draw(viewPort, (self.selControl and self.selControl.hasFocus and self.selControl ~= control) or (selControl and selControl.hasFocus and selControl ~= control))
end
end
for _, control in pairs(self.controls) do
if (not control.zOrder or control.zOrder == 0) and control:IsShown() and control.Draw then
control:Draw(viewPort, (self.selControl and self.selControl.hasFocus and self.selControl ~= control) or (selControl and selControl.hasFocus and selControl ~= control))
end
end
if posZOrder then
t_sort(posZOrder, function(a, b)
return a.zOrder < b.zOrder
end)
for _, control in ipairs(posZOrder) do
control:Draw(viewPort, (self.selControl and self.selControl.hasFocus and self.selControl ~= control) or (selControl and selControl.hasFocus and selControl ~= control))
end
end
end