-
Notifications
You must be signed in to change notification settings - Fork 354
/
calls_dispatcher.lua
185 lines (162 loc) · 6.32 KB
/
calls_dispatcher.lua
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
--[[ Copyright (c) 2010 Sam Wong
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE. --]]
--! Calls Dispatcher Window
class "UICallsDispatcher" (UIResizable)
---@type UICallsDispatcher
local UICallsDispatcher = _G["UICallsDispatcher"]
local col_bg = {
red = 154,
green = 146,
blue = 198,
}
local col_button = {
red = 84,
green = 200,
blue = 84,
}
local col_shadow = {
red = 134,
green = 126,
blue = 178,
}
function UICallsDispatcher:UICallsDispatcher(ui)
local app = ui.app
self:UIResizable(ui, 320, 350, col_bg)
self.ui = ui
self.dispatcher = app.world.dispatcher
self.on_top = false
self.esc_closes = true
self.resizable = false
self.call_list = {}
self.list_table = {}
self.rows_shown = 0
self:createControls()
self:setDefaultPosition(0.05, 0.05)
self.default_button_sound = "selectx.wav"
self.dispatcher:addChangeCallback(self.update, self)
self:update()
end
local row_height = 20
local window_margin = 15
function UICallsDispatcher:createControls()
local rows = math.floor((self.height - window_margin * 3 - 60) / row_height)
if rows ~= self.rows_shown then
local function assigned_factory(num)
return --[[persistable:calls_dispatcher_assigned_button]] function(window)
window:itemButtonClicked(num)
end
end
local function task_factory(num)
return --[[persistable:calls_dispatcher_task_button]] function(window)
window:itemButtonClicked(num)
end
end
local callback = --[[persistable:calls_dispatcher_scrollbar]] function()
self:scrollbarChange()
end
self.rows_shown = rows
self.list_table = {}
local y = window_margin
for i = 1, rows, 1 do
local assigned_panel = self:addBevelPanel(window_margin, y, 20, row_height, col_button)
local assigned_button = assigned_panel:makeButton(0, 0, 20, row_height, nil, assigned_factory(i))
:setTooltip(_S.tooltip.calls_dispatcher.assigned)
local task_panel = self:addBevelPanel(50, y, self.width - 50 - 40 - window_margin, row_height, col_bg)
local task_button = task_panel:makeButton(0, 0, self.width - 50 - 40 - window_margin, row_height, nil, task_factory(i))
:setTooltip(_S.tooltip.calls_dispatcher.task)
table.insert(self.list_table, {
assigned_panel = assigned_panel,
assigned_button = assigned_button,
task_panel = task_panel,
task_button = task_button,
})
y = y + row_height
end
self.summary_panel = self:addColourPanel(50, y + 10, self.width - 50 - 40 - window_margin, 20,
col_bg.red, col_bg.green, col_bg.blue)
self.scrollbar = self:addColourPanel(self.width - window_margin - 20, window_margin, 20, row_height * rows,
col_shadow.red, col_shadow.green, col_shadow.blue)
:makeScrollbar(col_bg, callback, 1, 1, 10, 1)
self.close_button = self:addBevelPanel(window_margin, y + 20 + window_margin, self.width - 2 * window_margin, 40, col_bg):setLabel(_S.calls_dispatcher.close)
:makeButton(0, 0, self.width - 2 * window_margin, 40, nil, self.close):setTooltip(_S.tooltip.calls_dispatcher.close)
end
end
function UICallsDispatcher:update()
self.call_list = {}
local assigned = 0
for _, queue in pairs(self.dispatcher.call_queue) do
for _, call in pairs(queue) do
table.insert(self.call_list, call)
if call.assigned then
assigned = assigned + 1
end
end
end
table.sort(self.call_list,
function(a,b)
if a.created == nil or b.created == nil then return false end
return a.created < b.created
end
)
self.summary_panel:setLabel(_S.calls_dispatcher.summary:format(#self.call_list, assigned), nil, "left")
self.scrollbar:setRange(1, math.max(1, #self.call_list), self.rows_shown, self.scrollbar.value)
self:scrollbarChange()
end
function UICallsDispatcher:scrollToEntity(entity)
local x, y = self.ui.app.map:WorldToScreen(entity.tile_x, entity.tile_y)
local px, py = entity.th:getMarker()
self.ui:scrollMapTo(x + px, y + py)
end
function UICallsDispatcher:scrollToRoom(room)
local x, y = self.ui.app.map:WorldToScreen(room.x + room.width / 2, room.y + room.height / 2)
self.ui:scrollMapTo(x, y)
end
function UICallsDispatcher:itemButtonClicked(index)
local call = self.call_list[index + self.scrollbar.value - 1]
if call and call.assigned then
self.ui:addWindow(UIStaff(self.ui, call.assigned))
end
if call and call.object then
if class.is(call.object, Room) then
self:scrollToRoom(call.object)
elseif class.is(call.object, Entity) then
self:scrollToEntity(call.object)
end
end
end
function UICallsDispatcher:scrollbarChange()
local scroll_pos = self.scrollbar.value
for i = 1, self.rows_shown, 1 do
local call = self.call_list[i + scroll_pos - 1]
local row = self.list_table[i]
if call then
row.assigned_panel:setLabel(call.assigned and "@" or '')
row.assigned_button:enable(call.assigned and true or false)
row.task_panel:setLabel((call.description and call.description or call.key), nil, "left")
row.task_button:enable(true)
else
row.assigned_panel:setLabel("")
row.task_panel:setLabel("")
row.assigned_button:enable(false)
row.task_button:enable(false)
end
end
end
function UICallsDispatcher:close()
self.dispatcher:removeChangeCallback(self.update)
Window.close(self)
end