-
Notifications
You must be signed in to change notification settings - Fork 2
/
breitbandgraphics.lua
779 lines (743 loc) · 32.2 KB
/
breitbandgraphics.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
if emu.set_renderer then
-- Specify D2D renderer
emu.set_renderer(2)
end
BreitbandGraphics = {
internal = {
brushes = {},
images = {},
brush_from_color = function(color)
local key = (color.r << 24) | (color.g << 16) | (color.b << 8) | (color.a and color.a or 255)
if not BreitbandGraphics.internal.brushes[key] then
local float_color = BreitbandGraphics.color_to_float(color)
BreitbandGraphics.internal.brushes[key] = d2d.create_brush(float_color.r, float_color.g, float_color.b,
float_color.a)
end
return BreitbandGraphics.internal.brushes[key]
end,
image_from_path = function(path)
if not BreitbandGraphics.internal.images[path] then
BreitbandGraphics.internal.images[path] = d2d.load_image(path)
end
return BreitbandGraphics.internal.images[path]
end,
},
--- Converts a color value to its corresponding hexadecimal representation
--- @param color table The color value to convert
--- @return _ string The hexadecimal representation of the color
color_to_hex = function(color)
return string.format('#%06X',
(color.r * 0x10000) + (color.g * 0x100) + color.b)
end,
--- Converts a color's hexadecimal representation into a color table
--- @param hex string The hexadecimal color to convert
--- @return _ table The color
hex_to_color = function(hex)
if #hex > 7 then
return
{
r = tonumber(hex:sub(2, 3), 16),
g = tonumber(hex:sub(4, 5), 16),
b = tonumber(hex:sub(6, 7), 16),
a = tonumber(hex:sub(8, 9), 16),
}
end
return
{
r = tonumber(hex:sub(2, 3), 16),
g = tonumber(hex:sub(4, 5), 16),
b = tonumber(hex:sub(6, 7), 16),
}
end,
--- Creates a color with the red, green and blue channels assigned to the specified value
--- @param value number The value to be used for the red, green and blue channels
--- @return _ table The color with the red, green and blue channels set to the specified value
repeated_to_color = function(value)
return
{
r = value,
g = value,
b = value,
}
end,
---Inverts a color
---@param value table The color value to invert, with byte-range channels (0-255)
---@return _ table The inverted color
invert_color = function(value)
return {
r = 255 - value.r,
g = 255 - value.r,
b = 255 - value.r,
a = value.a,
}
end,
--- A collection of common colors as tables with red, green and blue channels channels ranging from `0` to `255`
colors = {
white = {
r = 255,
g = 255,
b = 255,
},
black = {
r = 0,
g = 0,
b = 0,
},
red = {
r = 255,
g = 0,
b = 0,
},
green = {
r = 0,
g = 255,
b = 0,
},
blue = {
r = 0,
g = 0,
b = 255,
},
yellow = {
r = 255,
g = 255,
b = 0,
},
orange = {
r = 255,
g = 128,
b = 0,
},
magenta = {
r = 255,
g = 0,
b = 255,
},
},
---Whether a point is inside a rectangle
---@param point table `{x, y}`
---@param rectangle table `{x, y, width, height}`
is_point_inside_rectangle = function(point, rectangle)
return point.x > rectangle.x and
point.y > rectangle.y and
point.x < rectangle.x + rectangle.width and
point.y < rectangle.y + rectangle.height
end,
---Whether a point is inside any of the rectangles
---@param point table `{x, y}`
---@param rectangles table[] `{{x, y, width, height}}`
is_point_inside_any_rectangle = function(point, rectangles)
for i = 1, #rectangles, 1 do
if BreitbandGraphics.is_point_inside_rectangle(point, rectangles[i]) then
return true
end
end
return false
end,
--- Creates a rectangle inflated around its center by the specified amount
--- @param rectangle table The rectangle to be inflated
--- @param amount number The amount to inflate the rectangle by
--- @return _ table The inflated rectangle
inflate_rectangle = function(rectangle, amount)
return {
x = rectangle.x - amount,
y = rectangle.y - amount,
width = rectangle.width + amount * 2,
height = rectangle.height + amount * 2,
}
end,
--- Maps a color's byte-range channels `0-255` to `0-1`
--- @param color table The color to be converted
--- @return _ table The color with remapped channels
color_to_float = function(color)
return {
r = (color.r and (color.r / 255.0) or 0.0),
g = (color.g and (color.g / 255.0) or 0.0),
b = (color.b and (color.b / 255.0) or 0.0),
a = (color.a and (color.a / 255.0) or 1.0),
}
end,
---Measures the size of a string
---@param text string The string to be measured
---@param font_size number The font size
---@param font_name string The font name
---@return _ table The text's bounding box as `{width, height}`
get_text_size = function(text, font_size, font_name)
return d2d.get_text_size(text, font_name, font_size, 99999999, 99999999)
end,
---Draws a rectangle's outline
---@param rectangle table The bounding rectangle as `{x, y, width, height}`
---@param color table The color as `{r, g, b, [optional] a}` with a channel range of `0-255`
---@param thickness number The outline's thickness
draw_rectangle = function(rectangle, color, thickness)
local brush = BreitbandGraphics.internal.brush_from_color(color)
d2d.draw_rectangle(
rectangle.x,
rectangle.y,
rectangle.x + rectangle.width,
rectangle.y + rectangle.height,
thickness,
brush)
end,
---Draws a rectangle
---@param rectangle table The bounding rectangle as `{x, y, width, height}`
---@param color table The color as `{r, g, b, [optional] a}` with a channel range of `0-255`
fill_rectangle = function(rectangle, color)
local brush = BreitbandGraphics.internal.brush_from_color(color)
d2d.fill_rectangle(
rectangle.x,
rectangle.y,
rectangle.x + rectangle.width,
rectangle.y + rectangle.height,
brush)
end,
---Draws a rounded rectangle's outline
---@param rectangle table The bounding rectangle as `{x, y, width, height}`
---@param color table The color as `{r, g, b, [optional] a}` with a channel range of `0-255`
---@param radii table The corner radii as `{x, y}`
---@param thickness number The outline's thickness
draw_rounded_rectangle = function(rectangle, color, radii, thickness)
local brush = BreitbandGraphics.internal.brush_from_color(color)
d2d.draw_rounded_rectangle(
rectangle.x,
rectangle.y,
rectangle.x + rectangle.width,
rectangle.y + rectangle.height,
radii.x,
radii.y,
thickness,
brush)
end,
---Fills a rounded rectangle
---@param rectangle table The bounding rectangle as `{x, y, width, height}`
---@param color table The color as `{r, g, b, [optional] a}` with a channel range of `0-255`
---@param radii table The corner radii as `{x, y}`
fill_rounded_rectangle = function(rectangle, color, radii)
local brush = BreitbandGraphics.internal.brush_from_color(color)
d2d.fill_rounded_rectangle(
rectangle.x,
rectangle.y,
rectangle.x + rectangle.width,
rectangle.y + rectangle.height,
radii.x,
radii.y,
brush)
end,
---Draws an ellipse's outline
---@param rectangle table The bounding rectangle as `{x, y, width, height}`
---@param color table The color as `{r, g, b, [optional] a}` with a channel range of `0-255`
---@param thickness number The outline's thickness
draw_ellipse = function(rectangle, color, thickness)
local brush = BreitbandGraphics.internal.brush_from_color(color)
d2d.draw_ellipse(
rectangle.x + rectangle.width / 2,
rectangle.y + rectangle.height / 2,
rectangle.width / 2,
rectangle.height / 2,
thickness,
brush)
end,
---Draws an ellipse
---@param rectangle table The bounding rectangle as `{x, y, width, height}`
---@param color table The color as `{r, g, b, [optional] a}` with a channel range of `0-255`
fill_ellipse = function(rectangle, color)
local brush = BreitbandGraphics.internal.brush_from_color(color)
d2d.fill_ellipse(
rectangle.x + rectangle.width / 2,
rectangle.y + rectangle.height / 2,
rectangle.width / 2,
rectangle.height / 2,
brush)
end,
---Draws text
---@param rectangle table The bounding rectangle as `{x, y, width, height}`
---@param horizontal_alignment string The text's horizontal alignment inside the bounding rectangle. `center` | `start` | `end` | `stretch`
---@param vertical_alignment string The text's vertical alignment inside the bounding rectangle. `center` | `start` | `end` | `stretch`
---@param style table The miscellaneous text styling as `{is_bold, is_italic, clip, grayscale, aliased, fit}`
---@param color table The color as `{r, g, b, [optional] a}` with a channel range of `0-255`
---@param font_size number The font size
---@param font_name string The font name
---@param text string The text
draw_text = function(rectangle, horizontal_alignment, vertical_alignment, style, color, font_size, font_name,
text)
if text == nil then
text = ''
end
local rect_x = rectangle.x
local rect_y = rectangle.y
local rect_w = rectangle.width
local rect_h = rectangle.height
local brush = BreitbandGraphics.internal.brush_from_color(color)
local d_horizontal_alignment = 0
local d_vertical_alignment = 0
local d_style = 0
local d_weight = 400
local d_options = 0
local d_text_antialias_mode = 1
if horizontal_alignment == 'center' then
d_horizontal_alignment = 2
elseif horizontal_alignment == 'start' then
d_horizontal_alignment = 0
elseif horizontal_alignment == 'end' then
d_horizontal_alignment = 1
elseif horizontal_alignment == 'stretch' then
d_horizontal_alignment = 3
end
if vertical_alignment == 'center' then
d_vertical_alignment = 2
elseif vertical_alignment == 'start' then
d_vertical_alignment = 0
elseif vertical_alignment == 'end' then
d_vertical_alignment = 1
end
if style.is_bold then
d_weight = 700
end
if style.is_italic then
d_style = 2
end
if style.clip then
d_options = d_options | 0x00000002
end
if style.grayscale then
d_text_antialias_mode = 2
end
if style.aliased then
d_text_antialias_mode = 3
end
if style.fit then
-- Try to fit the text into the specified rectangle by reducing the font size
local text_size = d2d.get_text_size(text, font_name, font_size, math.maxinteger, math.maxinteger)
if text_size.width > rectangle.width then
font_size = font_size / math.max(0.01, (text_size.width / rectangle.width))
end
if text_size.height > rectangle.height then
font_size = font_size / math.max(0.01, (text_size.height / rectangle.height))
end
local text_size = d2d.get_text_size(text, font_name, font_size, math.maxinteger, math.maxinteger)
-- Since the rect stays the same, the text will want to wrap.
-- We solve that by recomputing the rect and alignments
if horizontal_alignment == 'center' or horizontal_alignment == 'stretch' then
rect_x = rect_x + rect_w / 2 - text_size.width / 2
elseif horizontal_alignment == 'start' then
rect_x = rect_x
elseif horizontal_alignment == 'end' then
rect_x = rect_x + rect_w - text_size.width
end
if vertical_alignment == 'center' or vertical_alignment == 'stretch' then
rect_y = rect_y + rect_h / 2 - text_size.height / 2
elseif vertical_alignment == 'start' then
rect_y = rect_y
elseif vertical_alignment == 'end' then
rect_y = rect_y + rect_h - text_size.height
end
d_horizontal_alignment = 0
d_vertical_alignment = 0
rect_w = text_size.width + 1
rect_h = text_size.height + 1
end
if type(text) ~= 'string' then
text = tostring(text)
end
d2d.set_text_antialias_mode(d_text_antialias_mode)
d2d.draw_text(
rect_x,
rect_y,
rect_x + rect_w,
rect_y + rect_h,
text,
font_name,
font_size,
d_weight,
d_style,
d_horizontal_alignment,
d_vertical_alignment,
d_options,
brush)
end,
---Draws a line
---@param from table The start point as `{x, y}`
---@param to table The end point as `{x, y}`
---@param color table The color as `{r, g, b, [optional] a}` with a channel range of `0-255`
---@param thickness number The line's thickness
draw_line = function(from, to, color, thickness)
local brush = BreitbandGraphics.internal.brush_from_color(color)
d2d.draw_line(
from.x,
from.y,
to.x,
to.y,
thickness,
brush)
end,
---Pushes a clip layer to the clip stack
---@param rectangle table The bounding rectangle as `{x, y, width, height}`
push_clip = function(rectangle)
d2d.push_clip(rectangle.x, rectangle.y, rectangle.x + rectangle.width,
rectangle.y + rectangle.height)
end,
--- Removes the topmost clip layer from the clip stack
pop_clip = function()
d2d.pop_clip()
end,
---Draws an image
---@param destination_rectangle table The bounding rectangle as `{x, y, width, height}`
---@param source_rectangle table The rectangle from the source image as `{x, y, width, height}`
---@param path string The image's absolute path on disk
---@param color table The color as `{r, g, b, [optional] a}` with a channel range of `0-255`
---@param filter string The texture filter to be used while drawing the image. `nearest` | `linear`
draw_image = function(destination_rectangle, source_rectangle, path, color, filter)
if not filter then
filter = 'nearest'
end
local float_color = BreitbandGraphics.color_to_float(color)
local image = BreitbandGraphics.internal.image_from_path(path)
local interpolation = filter == 'nearest' and 0 or 1
d2d.draw_image(
destination_rectangle.x,
destination_rectangle.y,
destination_rectangle.x + destination_rectangle.width,
destination_rectangle.y + destination_rectangle.height,
source_rectangle.x,
source_rectangle.y,
source_rectangle.x + source_rectangle.width,
source_rectangle.y + source_rectangle.height,
float_color.a,
interpolation,
image)
end,
---Draws a nineslice-scalable image
---@param destination_rectangle table The destination rectangle as `{x, y, width, height}`
---@param source_rectangle table The source rectangle as `{x, y, width, height}`
---@param source_rectangle_center table The source rectangle's center part as `{x, y, width, height}`
---@param path string The image's absolute path on disk
---@param color table The color as `{r, g, b, [optional] a}` with a channel range of `0-255`
---@param filter string The texture filter to be used while drawing the image. `nearest` | `linear`
draw_image_nineslice = function(destination_rectangle, source_rectangle, source_rectangle_center, path,
color, filter)
destination_rectangle = {
x = math.floor(destination_rectangle.x),
y = math.floor(destination_rectangle.y),
width = math.ceil(destination_rectangle.width),
height = math.ceil(destination_rectangle.height),
}
source_rectangle = {
x = math.floor(source_rectangle.x),
y = math.floor(source_rectangle.y),
width = math.ceil(source_rectangle.width),
height = math.ceil(source_rectangle.height),
}
local corner_size = {
x = math.abs(source_rectangle_center.x - source_rectangle.x),
y = math.abs(source_rectangle_center.y - source_rectangle.y),
}
local top_left = {
x = source_rectangle.x,
y = source_rectangle.y,
width = corner_size.x,
height = corner_size.y,
}
local bottom_left = {
x = source_rectangle.x,
y = source_rectangle_center.y + source_rectangle_center.height,
width = corner_size.x,
height = corner_size.y,
}
local left = {
x = source_rectangle.x,
y = source_rectangle_center.y,
width = corner_size.x,
height = source_rectangle.height - corner_size.y * 2,
}
local top_right = {
x = source_rectangle.x + source_rectangle.width - corner_size.x,
y = source_rectangle.y,
width = corner_size.x,
height = corner_size.y,
}
local bottom_right = {
x = source_rectangle.x + source_rectangle.width - corner_size.x,
y = source_rectangle_center.y + source_rectangle_center.height,
width = corner_size.x,
height = corner_size.y,
}
local top = {
x = source_rectangle_center.x,
y = source_rectangle.y,
width = source_rectangle.width - corner_size.x * 2,
height = corner_size.y,
}
local right = {
x = source_rectangle.x + source_rectangle.width - corner_size.x,
y = source_rectangle_center.y,
width = corner_size.x,
height = source_rectangle.height - corner_size.y * 2,
}
local bottom = {
x = source_rectangle_center.x,
y = source_rectangle.y + source_rectangle.height - corner_size.y,
width = source_rectangle.width - corner_size.x * 2,
height = corner_size.y,
}
BreitbandGraphics.draw_image({
x = destination_rectangle.x,
y = destination_rectangle.y,
width = top_left.width,
height = top_left.height,
}, top_left, path, color, filter)
BreitbandGraphics.draw_image({
x = destination_rectangle.x + destination_rectangle.width - top_right.width,
y = destination_rectangle.y,
width = top_right.width,
height = top_right.height,
}, top_right, path, color, filter)
BreitbandGraphics.draw_image({
x = destination_rectangle.x,
y = destination_rectangle.y + destination_rectangle.height - bottom_left.height,
width = bottom_left.width,
height = bottom_left.height,
}, bottom_left, path, color, filter)
BreitbandGraphics.draw_image({
x = destination_rectangle.x + destination_rectangle.width - bottom_right.width,
y = destination_rectangle.y + destination_rectangle.height - bottom_right.height,
width = bottom_right.width,
height = bottom_right.height,
}, bottom_right, path, color, filter)
BreitbandGraphics.draw_image({
x = destination_rectangle.x + top_left.width,
y = destination_rectangle.y + top_left.height,
width = destination_rectangle.width - bottom_right.width * 2,
height = destination_rectangle.height - bottom_right.height * 2,
}, source_rectangle_center, path, color, filter)
BreitbandGraphics.draw_image({
x = destination_rectangle.x,
y = destination_rectangle.y + top_left.height,
width = left.width,
height = destination_rectangle.height - bottom_left.height * 2,
}, left, path, color, filter)
BreitbandGraphics.draw_image({
x = destination_rectangle.x + destination_rectangle.width - top_right.width,
y = destination_rectangle.y + top_right.height,
width = left.width,
height = destination_rectangle.height - bottom_right.height * 2,
}, right, path, color, filter)
BreitbandGraphics.draw_image({
x = destination_rectangle.x + top_left.width,
y = destination_rectangle.y,
width = destination_rectangle.width - top_right.width * 2,
height = top.height,
}, top, path, color, filter)
BreitbandGraphics.draw_image({
x = destination_rectangle.x + top_left.width,
y = destination_rectangle.y + destination_rectangle.height - bottom.height,
width = destination_rectangle.width - bottom_right.width * 2,
height = bottom.height,
}, bottom, path, color, filter)
end,
---Gets an image's metadata
---@param path string The image's absolute path on disk
get_image_info = function(path)
local image = BreitbandGraphics.internal.image_from_path(path)
return d2d.get_image_info(image)
end,
---Releases allocated resources
---Must be called before stopping script
free = function()
for key, value in pairs(BreitbandGraphics.internal.brushes) do
d2d.free_brush(value)
end
for key, value in pairs(BreitbandGraphics.internal.images) do
d2d.free_image(value)
end
end,
}
-- 1.1.5 - 1.1.6 shim
if d2d and d2d.create_render_target then
print('BreitbandGraphics: Using 1.1.5 - 1.1.6 shim. Please update to mupen64-rr-lua 1.1.7')
BreitbandGraphics.bitmap_cache = {}
BreitbandGraphics.get_text_size = function(text, font_size, font_name)
return d2d.get_text_size(text, font_name, font_size, 99999999, 99999999)
end
BreitbandGraphics.draw_rectangle = function(rectangle, color, thickness)
local float_color = BreitbandGraphics.color_to_float(color)
d2d.draw_rectangle(rectangle.x, rectangle.y, rectangle.x + rectangle.width,
rectangle.y + rectangle.height, float_color.r, float_color.g, float_color.b, 1.0, thickness)
end
BreitbandGraphics.fill_rectangle = function(rectangle, color)
local float_color = BreitbandGraphics.color_to_float(color)
d2d.fill_rectangle(rectangle.x, rectangle.y, rectangle.x + rectangle.width,
rectangle.y + rectangle.height, float_color.r, float_color.g, float_color.b, 1.0)
end
BreitbandGraphics.draw_rounded_rectangle = function(rectangle, color, radii, thickness)
local float_color = BreitbandGraphics.color_to_float(color)
d2d.draw_rounded_rectangle(rectangle.x, rectangle.y, rectangle.x + rectangle.width,
rectangle.y + rectangle.height, radii.x, radii.y, float_color.r, float_color.g, float_color.b, 1.0,
thickness)
end
BreitbandGraphics.fill_rounded_rectangle = function(rectangle, color, radii)
local float_color = BreitbandGraphics.color_to_float(color)
d2d.fill_rounded_rectangle(rectangle.x, rectangle.y, rectangle.x + rectangle.width,
rectangle.y + rectangle.height, radii.x, radii.y, float_color.r, float_color.g, float_color.b, 1.0)
end
BreitbandGraphics.draw_ellipse = function(rectangle, color, thickness)
local float_color = BreitbandGraphics.color_to_float(color)
d2d.draw_ellipse(rectangle.x + rectangle.width / 2, rectangle.y + rectangle.height / 2,
rectangle.width / 2, rectangle.height / 2, float_color.r, float_color.g, float_color.b, 1.0,
thickness)
end
---Draws an ellipse
---@param rectangle table The bounding rectangle as `{x, y, width, height}`
---@param color table The color as `{r, g, b, [optional] a}` with a channel range of `0-255`
BreitbandGraphics.fill_ellipse = function(rectangle, color)
local float_color = BreitbandGraphics.color_to_float(color)
d2d.fill_ellipse(rectangle.x + rectangle.width / 2, rectangle.y + rectangle.height / 2,
rectangle.width / 2, rectangle.height / 2, float_color.r, float_color.g, float_color.b, 1.0)
end
---Draws text
---@param rectangle table The bounding rectangle as `{x, y, width, height}`
---@param horizontal_alignment string The text's horizontal alignment inside the bounding rectangle. `center` | `start` | `end` | `stretch`
---@param vertical_alignment string The text's vertical alignment inside the bounding rectangle. `center` | `start` | `end` | `stretch`
---@param style table The miscellaneous text styling as `{is_bold, is_italic, clip, grayscale, aliased, fit}`
---@param color table The color as `{r, g, b, [optional] a}` with a channel range of `0-255`
---@param font_size number The font size
---@param font_name string The font name
---@param text string The text
BreitbandGraphics.draw_text = function(rectangle, horizontal_alignment, vertical_alignment, style, color, font_size,
font_name,
text)
if text == nil then
text = ''
end
local rect_x = rectangle.x
local rect_y = rectangle.y
local rect_w = rectangle.width
local rect_h = rectangle.height
local d_horizontal_alignment = 0
local d_vertical_alignment = 0
local d_style = 0
local d_weight = 400
local d_options = 0
local d_text_antialias_mode = 1
if horizontal_alignment == 'center' then
d_horizontal_alignment = 2
elseif horizontal_alignment == 'start' then
d_horizontal_alignment = 0
elseif horizontal_alignment == 'end' then
d_horizontal_alignment = 1
elseif horizontal_alignment == 'stretch' then
d_horizontal_alignment = 3
end
if vertical_alignment == 'center' then
d_vertical_alignment = 2
elseif vertical_alignment == 'start' then
d_vertical_alignment = 0
elseif vertical_alignment == 'end' then
d_vertical_alignment = 1
end
if style.is_bold then
d_weight = 700
end
if style.is_italic then
d_style = 2
end
if style.clip then
d_options = d_options | 0x00000002
end
if style.grayscale then
d_text_antialias_mode = 2
end
if style.aliased then
d_text_antialias_mode = 3
end
if style.fit then
-- Try to fit the text into the specified rectangle by reducing the font size
local text_size = d2d.get_text_size(text, font_name, font_size, math.maxinteger, math.maxinteger)
if text_size.width > rectangle.width then
font_size = font_size / math.max(0.01, (text_size.width / rectangle.width))
end
if text_size.height > rectangle.height then
font_size = font_size / math.max(0.01, (text_size.height / rectangle.height))
end
local text_size = d2d.get_text_size(text, font_name, font_size, math.maxinteger, math.maxinteger)
-- Since the rect stays the same, the text will want to wrap.
-- We solve that by recomputing the rect and alignments
if horizontal_alignment == 'center' or horizontal_alignment == 'stretch' then
rect_x = rect_x + rect_w / 2 - text_size.width / 2
elseif horizontal_alignment == 'start' then
rect_x = rect_x
elseif horizontal_alignment == 'end' then
rect_x = rect_x + rect_w - text_size.width
end
if vertical_alignment == 'center' or vertical_alignment == 'stretch' then
rect_y = rect_y + rect_h / 2 - text_size.height / 2
elseif vertical_alignment == 'start' then
rect_y = rect_y
elseif vertical_alignment == 'end' then
rect_y = rect_y + rect_h - text_size.height
end
d_horizontal_alignment = 0
d_vertical_alignment = 0
rect_w = text_size.width + 1
rect_h = text_size.height + 1
end
if type(text) ~= 'string' then
text = tostring(text)
end
local float_color = BreitbandGraphics.color_to_float(color)
d2d.set_text_antialias_mode(d_text_antialias_mode)
d2d.draw_text(rect_x, rect_y, rect_w, rect_h, float_color.r, float_color.g, float_color.b, 1.0, text, font_name,
font_size, d_weight, d_style, d_horizontal_alignment, d_vertical_alignment, d_options)
end
---Draws a line
---@param from table The start point as `{x, y}`
---@param to table The end point as `{x, y}`
---@param color table The color as `{r, g, b, [optional] a}` with a channel range of `0-255`
---@param thickness number The line's thickness
BreitbandGraphics.draw_line = function(from, to, color, thickness)
local float_color = BreitbandGraphics.color_to_float(color)
d2d.draw_line(from.x, from.y, to.x, to.y, float_color.r, float_color.g, float_color.b, 1.0,
thickness)
end
---Pushes a clip layer to the clip stack
---@param rectangle table The bounding rectangle as `{x, y, width, height}`
BreitbandGraphics.push_clip = function(rectangle)
d2d.push_clip(rectangle.x, rectangle.y, rectangle.x + rectangle.width,
rectangle.y + rectangle.height)
end
--- Removes the topmost clip layer from the clip stack
BreitbandGraphics.pop_clip = function()
d2d.pop_clip()
end
---Draws an image
---@param destination_rectangle table The bounding rectangle as `{x, y, width, height}`
---@param source_rectangle table The rectangle from the source image as `{x, y, width, height}`
---@param path string The image's absolute path on disk
---@param color table The color as `{r, g, b, [optional] a}` with a channel range of `0-255`
---@param filter string The texture filter to be used while drawing the image. `nearest` | `linear`
BreitbandGraphics.draw_image = function(destination_rectangle, source_rectangle, path, color, filter)
if not BreitbandGraphics.bitmap_cache[path] then
print('Loaded image from ' .. path)
d2d.load_image(path, path)
BreitbandGraphics.bitmap_cache[path] = path
end
if not filter then
filter = 'nearest'
end
local float_color = BreitbandGraphics.color_to_float(color)
d2d.draw_image(destination_rectangle.x, destination_rectangle.y,
destination_rectangle.x + destination_rectangle.width,
destination_rectangle.y + destination_rectangle.height,
source_rectangle.x, source_rectangle.y, source_rectangle.x + source_rectangle.width,
source_rectangle.y + source_rectangle.height, path, float_color.a, filter == 'nearest' and 0 or 1)
end
---Gets an image's metadata
---@param path string The image's absolute path on disk
BreitbandGraphics.get_image_info = function(path)
if not BreitbandGraphics.bitmap_cache[path] then
print('Loaded image from ' .. path)
d2d.load_image(path, path)
BreitbandGraphics.bitmap_cache[path] = path
end
return d2d.get_image_info(path)
end
end