Debian Codegraph @

CorsixTH/Lua/dialogs/information.lua

raw
1 --[[ Copyright (c) 2009 Edvin "Lego3" Linge
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 --! Dialog that informs the player of for example what the goals for the level are.
22 class "UIInformation" (Window)
23
24 ---@type UIInformation
25 local UIInformation = _G["UIInformation"]
26
27 --! Constructor for the Information Dialog.
28 --!param text The text to show, held in a table. All elements of the table will be written
29 -- beneath each other. If instead a table within the table is supplied the texts
30 -- will be shown in consecutive dialogs.
31 --!param use_built_in_font Whether the built-in font should be used to make sure that
32 -- the given message can be read without distortions.
33 function UIInformation:UIInformation(ui, text, use_built_in_font)
34 self:Window()
35
36 local app = ui.app
37 self.modal_class = "information"
38 self.esc_closes = true
39 self.on_top = true
40 self.ui = ui
41 self.panel_sprites = app.gfx:loadSpriteTable("Data", "PulldV", true)
42 self.active_hover = false
43 if not use_built_in_font then
44 self.black_font = app.gfx:loadFontAndSpriteTable("QData", "Font00V", nil, nil, { apply_ui_scale = true })
45 else
46 self.black_font = app.gfx:loadBuiltinFont()
47 self.black_background = true
48 end
49
50 if type(text[1]) == "table" then
51 self.text = text[1]
52 table.remove(text, 1)
53 self.additional_text = text
54 else
55 self.text = text
56 end
57
58 -- Window size parameters
59 self.text_width = 300
60 self.spacing = {
61 l = 15,
62 r = 15,
63 t = 15,
64 b = 18 + 15, -- Size of close button + padding
65 }
66
67 self:onChangeLanguage()
68
69 -- Enter closes the window
70 self:registerKeyHandlers()
71 end
72
73 function UIInformation:registerKeyHandlers()
74 self:addKeyHandler("global_confirm", self.close)
75 self:addKeyHandler("global_confirm_alt", self.close)
76 end
77
78 function UIInformation:onChangeLanguage()
79 local total_req_height = 0
80 local s = TheApp.config.ui_scale
81 for _, text in ipairs(self.text) do
82 local _, req_height = self.black_font:sizeOf(text, self.text_width * s)
83 total_req_height = total_req_height + math.ceil(req_height / s)
84 end
85
86 self.width = self.spacing.l + self.text_width + self.spacing.r
87 self.height = self.spacing.t + total_req_height + self.spacing.b
88 self:setDefaultPosition(0.5, 0.5)
89
90 self:removeAllPanels()
91
92 for x = 4, self.width - 4, 4 do
93 self:addPanel(12, x, 0, 0, 0, 1) -- Dialog top and bottom borders
94 self:addPanel(16, x, self.height - 4, 0, 0, 1)
95 end
96 for y = 4, self.height - 4, 4 do
97 self:addPanel(18, 0, y, 0, 0, 1) -- Dialog left and right borders
98 self:addPanel(14, self.width - 4, y, 0, 0, 1)
99 end
100 self:addPanel(11, 0, 0, 0, 0, 1) -- Border top left corner
101 self:addPanel(17, 0, self.height - 4, 0, 0, 1) -- Border bottom left corner
102 self:addPanel(13, self.width - 4, 0, 0, 0, 1) -- Border top right corner
103 self:addPanel(15, self.width - 4, self.height - 4, 0, 0, 1) -- Border bottom right corner
104
105 -- Close button
106 self:addPanel(19, self.width - 28, self.height - 28, 18, 18, 1):makeButton(0, 0, 18, 18, 20, self.close):setTooltip(_S.tooltip.information.close)
107 .panel_for_sprite.custom_draw = --[[persistable:information_close_button]] function(panel, canvas, x, y)
108 local ds = TheApp.config.ui_scale
109 x = x + panel.x * ds
110 y = y + panel.y * ds
111 panel.window.panel_sprites:draw(canvas, panel.sprite_index, x, y, { scaleFactor = ds })
112 if self.active_hover then
113 self.panel_sprites:draw(canvas, 20, x, y, { scaleFactor = ds })
114 end
115 end
116 end
117
118 function UIInformation:draw(canvas, x, y)
119 local s = TheApp.config.ui_scale
120 local dx, dy = x + self.x * s, y + self.y * s
121 local background = self.black_background and canvas:mapRGB(0, 0, 0) or canvas:mapRGB(255, 255, 255)
122 canvas:drawRect(background, dx + 4 * s, dy + 4 * s, self.width * s - 8 * s, self.height * s - 8 * s)
123 local last_y = dy + self.spacing.t * s
124 for _, text in ipairs(self.text) do
125 last_y = self.black_font:drawWrapped(canvas, text, dx + self.spacing.l, last_y, self.text_width * s)
126 end
127
128 Window.draw(self, canvas, x, y)
129 end
130
131 function UIInformation:onMouseMove(x, y, dx, dy)
132 local s = TheApp.config.ui_scale
133 self.active_hover = self:hoverTest(
134 self.active_hover,
135 x, y,
136 self.width * s - 29 * s, self.width * s - 10 * s,
137 self.height * s - 29 * s, self.height * s - 10 * s)
138 return Window:onMouseMove(x, y, dx, dy)
139 end
140
141 function UIInformation:hitTest(x, y)
142 local s = TheApp.config.ui_scale
143 if x >= 0 and y >= 0 and x < self.width * s and y < self.height * s then
144 return true
145 else
146 return Window.hitTest(self, x, y)
147 end
148 end
149
150 function UIInformation:close()
151 self.ui:tutorialStep(3, 16, "next")
152 Window.close(self)
153 if self.additional_text and #self.additional_text > 0 then
154 self.ui:addWindow(UIInformation(self.ui, self.additional_text))
155 end
156 end
157
158 function UIInformation:afterLoad(old, new)
159 -- box resized to be more like the original
160 self.spacing = {
161 l = 15,
162 r = 15,
163 t = 15,
164 b = 18 + 15,
165 }
166 self:onChangeLanguage()
167 Window.afterLoad(self, old, new)
168 self:registerKeyHandlers()
169 end