Debian Codegraph @

CorsixTH/Lua/dialogs/resizable.lua

raw
1 --[[ Copyright (c) 2010 Manuel "Roujin" Wolf
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 --! Class for resizable windows.
22 --! If resizable is set to true, the user can resize by clicking and dragging
23 --any of the corners.
24 class "UIResizable" (Window)
25
26 ---@type UIResizable
27 local UIResizable = _G["UIResizable"]
28
29 local border_offset_x = 9
30 local border_offset_y = 9
31 local border_size_x = 40
32 local border_size_y = 40
33
34 function UIResizable:UIResizable(ui, width, height, colour, no_borders, background_bevel)
35 self:Window()
36
37 local app = ui.app
38 self.ui = ui
39 self.resizable = false -- by default, not user-resizable
40 if not no_borders then
41 self.border_sprites = app.gfx:loadSpriteTable("Bitmap", "aux_ui", true)
42 end
43
44 if background_bevel then
45 self.background_panel = self:addBevelPanel(0, 0, 0, 0, colour)
46 self.background_panel.lowered = true
47 else
48 self.background_panel = self:addColourPanel(0, 0, 0, 0, 0, 0, 0)
49 end
50
51 -- Minimum size. Can be changed per window, but should never be smaller than this
52 -- because it would result in visual glitches
53 self.min_width = 50
54 self.min_height = 50
55
56 self.border_pos = {}
57 self.border_pos.left = -border_offset_x
58 self.border_pos.upper = -border_offset_y
59
60 -- NB: intentionally calling like this to allow subclasses to extend setSize without being called from here
61 UIResizable.setSize(self, width, height)
62 self:setColour(colour)
63 end
64
65 --! Apply the initial width and height of the window
66 --!param width (int) The initial width
67 --!param height (int) The initial height
68 function UIResizable:setSize(width, height)
69 width = math.max(self.min_width, width)
70 height = math.max(self.min_height, height)
71
72 self.width = width
73 self.height = height
74 self.background_panel.w = width
75 self.background_panel.h = height
76
77 self.border_pos.right = self.width
78 self.border_pos.corner_right = self.width - border_size_x
79
80 self.border_pos.lower = self.height
81 self.border_pos.corner_lower = self.height - border_size_y
82 end
83
84 --! Must be called before UIResizable:setSize
85 --!param min_width (int) The new minimum width of the window
86 --!param min_height (int) The new minimum height of the window
87 function UIResizable:overrideMinSize(min_width, min_height)
88 self.min_width = min_width
89 self.min_height = min_height
90 end
91
92 function UIResizable:setColour(colour)
93 self.colour = colour
94 self.background_panel:setColour(colour)
95 end
96
97 function UIResizable:draw(canvas, x, y)
98 local sprites = self.border_sprites
99 if sprites then
100 local s = TheApp.config.ui_scale
101 local xabs = self.x * s + x
102 local yabs = self.y * s + y
103
104 for xpos = xabs + border_size_x * s, xabs + self.border_pos.corner_right * s - 1, border_size_x * s do
105 sprites:draw(canvas, 11, xpos, yabs + self.border_pos.upper * s, { scaleFactor = s }) -- upper edge
106 sprites:draw(canvas, 16, xpos, yabs + self.border_pos.lower * s, { scaleFactor = s }) -- lower edge
107 end
108 for ypos = yabs + border_size_y * s, yabs + self.border_pos.corner_lower * s - 1, border_size_y * s do
109 sprites:draw(canvas, 13, xabs + self.border_pos.left * s, ypos, { scaleFactor = s }) -- left edge
110 sprites:draw(canvas, 14, xabs + self.border_pos.right * s, ypos, { scaleFactor = s }) -- right edge
111 end
112
113 sprites:draw(canvas, 10, xabs + self.border_pos.left * s, yabs + self.border_pos.upper * s, { scaleFactor = s }) -- upper left corner
114 sprites:draw(canvas, 12, xabs + self.border_pos.corner_right * s, yabs + self.border_pos.upper * s, { scaleFactor = s }) -- upper right corner
115 sprites:draw(canvas, 15, xabs + self.border_pos.left * s, yabs + self.border_pos.corner_lower * s, { scaleFactor = s }) -- lower left corner
116 sprites:draw(canvas, 17, xabs + self.border_pos.corner_right * s, yabs + self.border_pos.corner_lower * s, { scaleFactor = s }) -- lower right corner
117 end
118 -- Draw window components
119 Window.draw(self, canvas, x, y)
120 end
121
122 function UIResizable:onMouseDown(button, x, y)
123 local res = self.resizable and self:hitTestCorners(x, y)
124 if res then
125 self:beginResize(x, y, res)
126 return true
127 end
128 return Window.onMouseDown(self, button, x, y)
129 end
130
131 function UIResizable:hitTest(x, y)
132 local s = TheApp.config.ui_scale
133 if x >= 0 and y >= 0 and x < self.width * s and y < self.height * s then -- inside window
134 return Window.hitTest(self, x, y)
135 end
136 local sprites = self.border_sprites
137 if not sprites then
138 return false
139 end
140 if x < -9 * s or y < -9 * s or x >= self.width * s + 9 * s or y >= self.height * s + 9 * s then -- outside border bounds
141 return false
142 end
143 if (0 <= x and x < self.width * s) or (0 <= y and y < self.height * s) then -- edges (upper/lower/left/right)
144 return true
145 end
146 return self:hitTestCorners(x, y) and true
147 end
148
149 --! Tests if any of the four corners of the window border is hit
150 --!param x the x coordinate to test
151 --!param y the y coordinate to test
152 --!return (boolean or string) false if not hit, else a string to denote which corner was hit (can be "ul", "ur", "ll" or "lr")
153 function UIResizable:hitTestCorners(x, y)
154 if self.border_sprites then
155 local s = TheApp.config.ui_scale
156 local yzone = (-9 * s <= y and y < 0) and "u" or (self.height * s <= y and y < self.height * s + 9 * s) and "l"
157 local xzone = (-9 * s <= x and x < 0) and "l" or (self.width * s <= x and x < self.width * s + 9 * s) and "r"
158
159 local sprite_ids = {ul = 10, ur = 12, ll = 15, lr = 17}
160 if yzone and xzone then
161 local zone = yzone .. xzone
162 local dy = (yzone == "u" and self.border_pos.upper or self.border_pos.corner_lower)
163 local dx = (xzone == "l" and self.border_pos.left or self.border_pos.corner_right)
164 return self.border_sprites:hitTest(sprite_ids[zone], x - dx, y - dy) and zone
165 end
166 end
167 return false
168 end
169
170 --[[ Initiate resizing of the resizable window.
171 !param x The X position of the cursor in window coordinates.
172 !param y The Y position of the cursor in window coordinates.
173 !param mode Either one of "ul", "ur", "ll" or "lr" to denote in which direction to resize. (upper/lower + left/right)
174 ]]
175 function UIResizable:beginResize(x, y, mode)
176 local orig_x = self.x
177 local orig_y = self.y
178 local ref_x = self.x + x
179 local ref_y = self.y + y
180 local orig_width = self.width
181 local orig_height = self.height
182
183 self.dragging = true
184 self.ui.drag_mouse_move = --[[persistable:window_resize_mouse_move]] function (sx, sy)
185 -- sx and sy are cursor screen co-ords. Convert to relative change.
186 sx = sx - ref_x
187 sy = sy - ref_y
188
189 local invert_x = mode == "ul" or mode == "ll"
190 local invert_y = mode == "ul" or mode == "ur"
191
192 sx = invert_x and -sx or sx
193 sy = invert_y and -sy or sy
194
195 self:setSize(orig_width + sx, orig_height + sy)
196 local new_x, new_y
197
198 if invert_x then
199 new_x = orig_x + orig_width - self.width
200 end
201 if invert_y then
202 new_y = orig_y + orig_height - self.height
203 end
204
205 if new_x or new_y then
206 self:setPosition(new_x or orig_x, new_y or orig_y)
207 end
208 end
209 end
210
211 function UIResizable:afterLoad(old, new)
212 Window.afterLoad(self, old, new)
213 if old < 65 then
214 -- added min_width and min_height
215 self.min_width = 50
216 self.min_height = 50
217 end
218 end