Debian Codegraph @

CorsixTH/Lua/dialogs/resizables/calls_dispatcher.lua

raw
1 --[[ Copyright (c) 2010 Sam Wong
2
3 Permission is hereby granted, free of charge, to any person obtaining a copy of
4 this software and associated documentation files (the "Software"), to deal in
5 the Software without restriction, including without limitation the rights to
6 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
7 of the Software, and to permit persons to whom the Software is furnished to do
8 so, subject to the following conditions:
9
10 The above copyright notice and this permission notice shall be included in all
11 copies or substantial portions of the Software.
12
13 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19 SOFTWARE. --]]
20
21 --! Calls Dispatcher Window
22 class "UICallsDispatcher" (UIResizable)
23
24 ---@type UICallsDispatcher
25 local UICallsDispatcher = _G["UICallsDispatcher"]
26
27 local col_bg = {
28 red = 154,
29 green = 146,
30 blue = 198,
31 }
32
33 local col_button = {
34 red = 84,
35 green = 200,
36 blue = 84,
37 }
38
39 local col_shadow = {
40 red = 134,
41 green = 126,
42 blue = 178,
43 }
44
45 function UICallsDispatcher:UICallsDispatcher(ui)
46 local app = ui.app
47 self:UIResizable(ui, 320, 350, col_bg)
48 self.ui = ui
49 self.dispatcher = app.world.dispatcher
50 self.on_top = false
51 self.esc_closes = true
52 self.resizable = false
53
54 self.call_list = {}
55 self.list_table = {}
56 self.rows_shown = 0
57 self:createControls()
58 self:setDefaultPosition(0.05, 0.05)
59
60 self.default_button_sound = "selectx.wav"
61
62 self.dispatcher:addChangeCallback(self.update, self)
63 self:update()
64 end
65
66 local row_height = 20
67 local window_margin = 15
68
69 function UICallsDispatcher:createControls()
70 local rows = math.floor((self.height - window_margin * 3 - 60) / row_height)
71
72 if rows ~= self.rows_shown then
73 local function assigned_factory(num)
74 return --[[persistable:calls_dispatcher_assigned_button]] function(window)
75 window:itemButtonClicked(num)
76 end
77 end
78 local function task_factory(num)
79 return --[[persistable:calls_dispatcher_task_button]] function(window)
80 window:itemButtonClicked(num)
81 end
82 end
83 local callback = --[[persistable:calls_dispatcher_scrollbar]] function()
84 self:scrollbarChange()
85 end
86
87 self.rows_shown = rows
88 self.list_table = {}
89 local y = window_margin
90 for i = 1, rows, 1 do
91 local assigned_panel = self:addBevelPanel(window_margin, y, 20, row_height, col_button)
92 local assigned_button = assigned_panel:makeButton(0, 0, 20, row_height, nil, assigned_factory(i))
93 :setTooltip(_S.tooltip.calls_dispatcher.assigned)
94 local task_panel = self:addBevelPanel(50, y, self.width - 50 - 40 - window_margin, row_height, col_bg)
95 local task_button = task_panel:makeButton(0, 0, self.width - 50 - 40 - window_margin, row_height, nil, task_factory(i))
96 :setTooltip(_S.tooltip.calls_dispatcher.task)
97 table.insert(self.list_table, {
98 assigned_panel = assigned_panel,
99 assigned_button = assigned_button,
100 task_panel = task_panel,
101 task_button = task_button,
102 })
103 y = y + row_height
104 end
105 self.summary_panel = self:addColourPanel(50, y + 10, self.width - 50 - 40 - window_margin, 20,
106 col_bg.red, col_bg.green, col_bg.blue)
107 self.scrollbar = self:addColourPanel(self.width - window_margin - 20, window_margin, 20, row_height * rows,
108 col_shadow.red, col_shadow.green, col_shadow.blue)
109 :makeScrollbar(col_bg, callback, 1, 1, 10, 1)
110 self.close_button = self:addBevelPanel(window_margin, y + 20 + window_margin, self.width - 2 * window_margin, 40, col_bg):setLabel(_S.calls_dispatcher.close)
111 :makeButton(0, 0, self.width - 2 * window_margin, 40, nil, self.close):setTooltip(_S.tooltip.calls_dispatcher.close)
112 end
113 end
114
115 function UICallsDispatcher:update()
116 self.call_list = {}
117 local assigned = 0
118 for _, queue in pairs(self.dispatcher.call_queue) do
119 for _, call in pairs(queue) do
120 table.insert(self.call_list, call)
121 if call.assigned then
122 assigned = assigned + 1
123 end
124 end
125 end
126 table.sort(self.call_list,
127 function(a,b)
128 if a.created == nil or b.created == nil then return false end
129 return a.created < b.created
130 end
131 )
132
133 self.summary_panel:setLabel(_S.calls_dispatcher.summary:format(#self.call_list, assigned), nil, "left")
134 self.scrollbar:setRange(1, math.max(1, #self.call_list), self.rows_shown, self.scrollbar.value)
135 self:scrollbarChange()
136 end
137
138 function UICallsDispatcher:scrollToEntity(entity)
139 local x, y = self.ui.app.map:WorldToScreen(entity.tile_x, entity.tile_y)
140 local px, py = entity.th:getSecondaryMarker()
141 self.ui:scrollMapTo(x + px, y + py)
142 end
143
144 function UICallsDispatcher:scrollToRoom(room)
145 local x, y = self.ui.app.map:WorldToScreen(room.x + room.width / 2, room.y + room.height / 2)
146 self.ui:scrollMapTo(x, y)
147 end
148
149 function UICallsDispatcher:itemButtonClicked(index)
150 local call = self.call_list[index + self.scrollbar.value - 1]
151 if call and call.assigned then
152 self.ui:addWindow(UIStaff(self.ui, call.assigned))
153 end
154 if call and call.object then
155 if class.is(call.object, Room) then
156 self:scrollToRoom(call.object)
157 elseif class.is(call.object, Entity) then
158 self:scrollToEntity(call.object)
159 end
160 end
161 end
162
163 function UICallsDispatcher:scrollbarChange()
164 local scroll_pos = self.scrollbar.value
165 for i = 1, self.rows_shown, 1 do
166 local call = self.call_list[i + scroll_pos - 1]
167 local row = self.list_table[i]
168 if call then
169 row.assigned_panel:setLabel(call.assigned and "@" or '')
170 row.assigned_button:enable(call.assigned and true or false)
171 row.task_panel:setLabel((call.description and call.description or call.key), nil, "left")
172 row.task_button:enable(true)
173 else
174 row.assigned_panel:setLabel("")
175 row.task_panel:setLabel("")
176 row.assigned_button:enable(false)
177 row.task_button:enable(false)
178 end
179 end
180 end
181
182 function UICallsDispatcher:close()
183 self.dispatcher:removeChangeCallback(self.update)
184 Window.close(self)
185 end