-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdrawing.lua
216 lines (190 loc) · 9.84 KB
/
drawing.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
local module = {}
-- Screen width divided by height.
local ASPECT_RATIO
--------------------------------------------------------------------------------
module.Draw_Item = {}
module.Draw_Item.__index = module.Draw_Item
--[[
The coordinate system for `pivot` and `anchor` is based on the bounding box of the label and parent shape respectively. X is horizontal, Y is vertical, the bottom-left corner is always (0, 0), and the top-right corner is always (1, 1).
pivot: The point on the label acting as its origin.
anchor: The point on the parent shape where the label's pivot will be placed.
anchor_screen_offset: Screen coordinate offset to add to the anchor, in screen width units.
]]
module.Draw_Item.LABEL_POSITION = {
CENTER = { pivot = Vec2:new(0.5, 0.5), anchor = Vec2:new(0.5, 0.5), anchor_screen_offset = Vec2:new(0.0, 0.0) },
LEFT = { pivot = Vec2:new(0.0, 0.5), anchor = Vec2:new(0.0, 0.5), anchor_screen_offset = Vec2:new(0.01, 0.0) },
RIGHT = { pivot = Vec2:new(1.0, 0.5), anchor = Vec2:new(1.0, 0.5), anchor_screen_offset = Vec2:new(-0.01, 0.0) },
BOTTOM = { pivot = Vec2:new(0.5, 0.0), anchor = Vec2:new(0.5, 0.0), anchor_screen_offset = Vec2:new(0.0, 0.01) },
TOP = { pivot = Vec2:new(0.5, 1.0), anchor = Vec2:new(0.5, 1.0), anchor_screen_offset = Vec2:new(0.0, -0.01) }
}
module.Draw_Item.label_size = 24
module.Draw_Item.label_position = module.Draw_Item.LABEL_POSITION.CENTER
function module.Draw_Item:new(o)
o = o or {}
setmetatable(o, self)
return o
end
function module.Draw_Item:draw(ctx)
if self.shape then
if self.shape.tris then
for _, tri in ipairs(self.shape.tris) do
local v1 = Vec2:new(screen_position(tri.v1.x, tri.v1.y))
local v2 = Vec2:new(screen_position(tri.v2.x, tri.v2.y))
local v3 = Vec2:new(screen_position(tri.v3.x, tri.v3.y))
ctx:draw_triangle_filled(v1, v3, v2, self.ucolors.fill)
if tri.ea_exposed then
ctx:draw_line(v1.x, v1.y, v2.x, v2.y, 1, self.ucolors.line)
end
if tri.eb_exposed then
ctx:draw_line(v2.x, v2.y, v3.x, v3.y, 1, self.ucolors.line)
end
if tri.ec_exposed then
ctx:draw_line(v3.x, v3.y, v1.x, v1.y, 1, self.ucolors.line)
end
end
end
if self.shape.convex_polygons then
for _, convex_polygon in ipairs(self.shape.convex_polygons) do
local screen_verts = {}
for i, v in ipairs(convex_polygon) do
screen_verts[i] = Vec2:new(screen_position(v.x, v.y))
end
ctx:draw_poly_filled(screen_verts, self.ucolors.fill)
-- Close the poly-line by adding the first vertex to the end.
table.insert(screen_verts, screen_verts[1])
ctx:draw_poly(screen_verts, 1, self.ucolors.line)
end
end
if self.shape.lines then
for _, line in ipairs(self.shape.lines) do
local x1, y1 = screen_position(line.v1.x, line.v1.y)
local x2, y2 = screen_position(line.v2.x, line.v2.y)
ctx:draw_line(x1, y1, x2, y2, 1, self.ucolors.line)
end
end
if self.shape.points then
for _, point in ipairs(self.shape.points) do
module.draw_point_mark(ctx, point.x, point.y, 0.75, self.ucolors.line)
end
end
if self.label and self.shape.bounds then
local label_origin_x, label_origin_y = screen_position(
((1.0 - self.label_position.anchor.x) * self.shape.bounds.left) + (self.label_position.anchor.x * self.shape.bounds.right),
((1.0 - self.label_position.anchor.y) * self.shape.bounds.bottom) + (self.label_position.anchor.y * self.shape.bounds.top))
label_origin_x = label_origin_x + self.label_position.anchor_screen_offset.x
label_origin_y = label_origin_y + (self.label_position.anchor_screen_offset.y * ASPECT_RATIO)
local label_draw_helper = module.Text_Draw_Helper:new(label_origin_x, label_origin_y,
self.label_position.pivot.x, self.label_position.pivot.y, self.label_size)
local text_x, text_y = label_draw_helper:get_text_position(self.label, self.timer and 0.5 or 0.0)
ctx:draw_text(text_x, text_y, self.label_size, self.label, self.ucolors.text)
if self.timer then
local bar_left, bar_bottom, bar_right, bar_top = label_draw_helper:get_text_bounds("0000/0000", -0.5)
ctx:draw_rect(bar_left, bar_top, bar_right, bar_bottom, 1, 0, self.ucolors.line)
local bar_fill_right = bar_left + ((bar_right - bar_left) * self.timer.value / self.timer.max_value)
ctx:draw_rect_filled(bar_left, bar_top, bar_fill_right, bar_bottom, 0, self.ucolors.fill)
local timer_text = self.timer.value.."/"..self.timer.max_value
local timer_text_x, timer_text_y = label_draw_helper:get_text_position(timer_text, -0.5)
ctx:draw_text(timer_text_x, timer_text_y, self.label_size, timer_text, self.ucolors.text)
end
end
end
end
function module.Draw_Item.flip_label_position_horizontal(label_position)
if label_position == module.Draw_Item.LABEL_POSITION.LEFT then
return module.Draw_Item.LABEL_POSITION.RIGHT
elseif label_position == module.Draw_Item.LABEL_POSITION.RIGHT then
return module.Draw_Item.LABEL_POSITION.LEFT
else
return label_position
end
end
--------------------------------------------------------------------------------
local LINE_COLOR_ALPHA = 0.5
local FILL_COLOR_ALPHA = 0.125
local TEXT_COLOR_ALPHA = 0.75
module.Draw_Color = {}
module.Draw_Color.__index = module.Draw_Color
function module.Draw_Color:new(colors)
local o = {
ucolors = {}
}
for _, color in ipairs(colors) do
table.insert(o.ucolors, {
bright = {
line = Color:new(color.r, color.g, color.b, LINE_COLOR_ALPHA):get_ucolor(),
fill = Color:new(color.r, color.g, color.b, FILL_COLOR_ALPHA):get_ucolor(),
text = Color:new(color.r, color.g, color.b, TEXT_COLOR_ALPHA):get_ucolor(),
},
dim = {
line = Color:new((0.325 * color.r) + 0.125, (0.325 * color.g) + 0.125, (0.325 * color.b) + 0.125, LINE_COLOR_ALPHA):get_ucolor(),
fill = Color:new((0.325 * color.r) + 0.125, (0.325 * color.g) + 0.125, (0.325 * color.b) + 0.125, FILL_COLOR_ALPHA):get_ucolor(),
text = Color:new((0.325 * color.r) + 0.125, (0.325 * color.g) + 0.125, (0.325 * color.b) + 0.125, TEXT_COLOR_ALPHA):get_ucolor()
}
})
end
setmetatable(o, self)
return o
end
function module.Draw_Color:get()
return self.ucolors[1]
end
function module.Draw_Color:get_variant(index)
index = ((index - 1) % #self.ucolors) + 1
return self.ucolors[index]
end
--------------------------------------------------------------------------------
module.Text_Draw_Helper = {}
module.Text_Draw_Helper.__index = module.Text_Draw_Helper
function module.Text_Draw_Helper:new(origin_x, origin_y, pivot_x, pivot_y, text_size)
local o = {
origin_x = origin_x,
origin_y = origin_y,
pivot_x = pivot_x,
pivot_y = pivot_y,
text_size = text_size
}
setmetatable(o, self)
return o
end
-- Returns the screen coordinates to provide to the text drawing function for the given text.
function module.Text_Draw_Helper:get_text_position(text, row)
local text_width, text_height = draw_text_size(self.text_size, text)
-- Math for X and Y differs because screen coordinates increase from bottom-left to top-right, but the text drawing origin is its top-left corner and the text height is negative.
return self.origin_x - (self.pivot_x * text_width), self.origin_y - ((1 + row - self.pivot_y) * text_height)
end
-- Returns the bounds of the given text in screen coordinates, in order of left, bottom, right, and top.
function module.Text_Draw_Helper:get_text_bounds(text, row)
local text_width, text_height = draw_text_size(self.text_size, text)
-- Math for X and Y differs because screen coordinates increase from bottom-left to top-right, but the text drawing origin is its top-left corner and the text height is negative.
return self.origin_x - (self.pivot_x * text_width), self.origin_y + ((self.pivot_y - row) * text_height),
self.origin_x + ((1 - self.pivot_x) * text_width), self.origin_y - ((1 + row - self.pivot_y) * text_height)
end
--------------------------------------------------------------------------------
local POINT_MARK_UCOLOR = Color:new(0, 0.5, 1, 0.75):get_ucolor()
-- Width of the square enclosing the point mark, in screen coordinates.
local POINT_MARK_W = 0.015
-- Height of the square enclosing the point mark, in screen coordinates.
local POINT_MARK_H
function module.draw_point_mark(ctx, x, y, size, ucolor)
size = size or 1
ucolor = ucolor or POINT_MARK_UCOLOR
local screen_x, screen_y = screen_position(x, y)
local left = screen_x - (size * POINT_MARK_W / 2)
local right = screen_x + (size * POINT_MARK_W / 2)
local bottom = screen_y - (size * POINT_MARK_H / 2)
local top = screen_y + (size * POINT_MARK_H / 2)
ctx:draw_line(left, screen_y, right, screen_y, 2, ucolor)
ctx:draw_line(screen_x, bottom, screen_x, top, 2, ucolor)
end
-- Compute all variables that depend on the screen size. This should be called once before every drawing frame.
function module.compute_screen_vars()
local window_w, window_h = get_window_size()
if window_h == 0 then
-- Use a sane fallback value in case window height is briefly zero.
ASPECT_RATIO = 1
else
ASPECT_RATIO = window_w / window_h
end
POINT_MARK_H = POINT_MARK_W * ASPECT_RATIO
end
return module