A modern Music Player Daemon based on Rockbox open source high quality audio player
libadwaita
audio
rust
zig
deno
mpris
rockbox
mpd
1--[[ Lua Drawing functions
2/***************************************************************************
3 * __________ __ ___.
4 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
5 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
6 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
7 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
8 * \/ \/ \/ \/ \/
9 * $Id$
10 *
11 * Copyright (C) 2017 William Wilgus
12 *
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License
15 * as published by the Free Software Foundation; either version 2
16 * of the License, or (at your option) any later version.
17 *
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
20 *
21 ****************************************************************************/
22]]
23
24--[[ Exposed Functions
25
26 _draw.circle
27 _draw.circle_filled
28 _draw.ellipse
29 _draw.ellipse_filled
30 _draw.ellipse_rect_filled
31 _draw.ellipse_rect
32 _draw.hline
33 _draw.image
34 _draw.line
35 _draw.polygon
36 _draw.polyline
37 _draw.rect
38 _draw.rect_filled
39 _draw.rounded_rect
40 _draw.rounded_rect_filled
41 _draw.vline
42
43]]
44
45--[[ bClip allows drawing out of bounds without raising an error it is slower
46 than having a correctly bounded figure, but can be helpful in some cases..
47]]
48
49if not rb.lcd_framebuffer then rb.splash(rb.HZ, "No Support!") return nil end
50
51local _draw = {} do
52
53 local rocklib_image = getmetatable(rb.lcd_framebuffer())
54 setmetatable(_draw, rocklib_image)
55
56 -- Internal Constants
57 local BSAND = 8 -- blits color to dst if src <> 0
58 local _NIL = nil -- nil placeholder
59
60
61 local _clear = rocklib_image.clear
62 local _copy = rocklib_image.copy
63 local _ellipse = rocklib_image.ellipse
64 local _get = rocklib_image.get
65 local _line = rocklib_image.line
66 local _min = math.min
67 local _newimg = rb.new_image
68
69 -- line
70 _draw.line = function(img, x1, y1, x2, y2, color, bClip)
71 _line(img, x1, y1, x2, y2, color, bClip)
72 end
73
74 -- horizontal line; x, y define start point; length in horizontal direction
75 local function hline(img, x, y , length, color, bClip)
76 _line(img, x, y, x + length, _NIL, color, bClip)
77 end
78
79 -- vertical line; x, y define start point; length in vertical direction
80 local function vline(img, x, y , length, color, bClip)
81 _line(img, x, y, _NIL, y + length, color, bClip)
82 end
83
84 -- draws a non-filled rect based on points in t-points
85 local function polyrect(img, x, y, t_points, color, bClip)
86 local pt_first_last = t_points[1]
87 local pt1, pt2
88 for i = 1, 4, 1 do
89 pt1 = t_points[i]
90 pt2 = t_points[i + 1] or pt_first_last-- first and last point
91 _line(img, pt1[1] + x, pt1[2] + y, pt2[1] + x, pt2[2] + y, color, bClip)
92 end
93 end
94
95 -- rectangle
96 local function rect(img, x, y, width, height, color, bClip)
97 if width == 0 or height == 0 then return end
98
99 polyrect(img, x, y, {{0, 0}, {width, 0}, {width, height}, {0, height}}, color, bClip)
100
101 end
102
103 -- filled rect, fillcolor is color if left empty
104 _draw.rect_filled = function(img, x, y, width, height, color, fillcolor, bClip)
105 if width == 0 or height == 0 then return end
106
107 if not fillcolor then
108 _clear(img, color, x, y, x + width, y + height, bClip)
109 else
110 _clear(img, fillcolor, x, y, x + width, y + height, bClip)
111 rect(img, x, y, width, height, color, bClip)
112 end
113 end
114
115 -- circle cx,cy define center point
116 _draw.circle = function(img, cx, cy, radius, color, bClip)
117 local r = radius
118 _ellipse(img, cx - r, cy - r, cx + r, cy + r, color, _NIL, bClip)
119 end
120
121 -- filled circle cx,cy define center, fillcolor is color if left empty
122 _draw.circle_filled = function(img, cx, cy, radius, color, fillcolor, bClip)
123 fillcolor = fillcolor or color
124 local r = radius
125 _ellipse(img, cx - r, cy - r, cx + r, cy + r, color, fillcolor, bClip)
126 end
127
128 -- ellipse that fits into defined rect
129 _draw.ellipse_rect = function(img, x1, y1, x2, y2, color, bClip)
130 _ellipse(img, x1, y1, x2, y2, color, _NIL, bClip)
131 end
132
133 --ellipse that fits into defined rect, fillcolor is color if left empty
134 _draw.ellipse_rect_filled = function(img, x1, y1, x2, y2, color, fillcolor, bClip)
135 if not fillcolor then fillcolor = color end
136
137 _ellipse(img, x1, y1, x2, y2, color, fillcolor, bClip)
138 end
139
140 -- ellipse cx, cy define center point; a, b the major/minor axis
141 _draw.ellipse = function(img, cx, cy, a, b, color, bClip)
142 _ellipse(img, cx - a, cy - b, cx + a, cy + b, color, _NIL, bClip)
143 end
144
145 -- filled ellipse cx, cy define center point; a, b the major/minor axis
146 -- fillcolor is color if left empty
147 _draw.ellipse_filled = function(img, cx, cy, a, b, color, fillcolor, bClip)
148 if not fillcolor then fillcolor = color end
149
150 _ellipse(img, cx - a, cy - b, cx + a, cy + b, color, fillcolor, bClip)
151 end
152
153 -- rounded rectangle
154 local function rounded_rect(img, x, y, w, h, radius, color, bClip)
155 local c_img
156 if w == 0 or h == 0 then return end
157
158 -- limit the radius of the circle otherwise it will overtake the rect
159 radius = _min(w / 2, radius)
160 radius = _min(h / 2, radius)
161
162 local r = radius
163
164 c_img = _newimg(r * 2 + 1, r * 2 + 1)
165 _clear(c_img, 0)
166 _ellipse(c_img, 1, 1, 1 + r + r, 1 + r + r, 0x1, _NIL, bClip)
167
168 -- copy 4 pieces of circle to their respective corners
169 _copy(img, c_img, x, y, _NIL, _NIL, r + 1, r + 1, bClip, BSAND, color) --TL
170 _copy(img, c_img, x + w - r - 2, y, r, _NIL, r + 1, r + 1, bClip, BSAND, color) --TR
171 _copy(img, c_img, x , y + h - r - 2, _NIL, r, r + 1, _NIL, bClip, BSAND, color) --BL
172 _copy(img, c_img, x + w - r - 2, y + h - r - 2, r, r, r + 1, r + 1, bClip, BSAND, color)--BR
173 c_img = _NIL
174
175 vline(img, x, y + r, h - r * 2, color, bClip);
176 vline(img, x + w - 1, y + r, h - r * 2, color, bClip);
177 hline(img, x + r, y, w - r * 2, color, bClip);
178 hline(img, x + r, y + h - 1, w - r * 2, color, bClip);
179 end
180
181 -- rounded rectangle fillcolor is color if left empty
182 _draw.rounded_rect_filled = function(img, x, y, w, h, radius, color, fillcolor, bClip)
183 local c_img
184
185 if w == 0 or h == 0 then return end
186
187 if not fillcolor then fillcolor = color end
188
189 -- limit the radius of the circle otherwise it will overtake the rect
190 radius = _min(w / 2, radius)
191 radius = _min(h / 2, radius)
192
193 local r = radius
194
195 c_img = _newimg(r * 2 + 1, r * 2 + 1)
196 _clear(c_img, 0)
197 _ellipse(c_img, 1, 1, 1 + r + r, 1 + r + r, 0x1, 0x1, bClip)
198
199 -- copy 4 pieces of circle to their respective corners
200 _copy(img, c_img, x, y, _NIL, _NIL, r + 1, r + 1, bClip, BSAND, fillcolor) --TL
201 _copy(img, c_img, x + w - r - 2, y, r, _NIL, r + 1, r + 1, bClip, BSAND, fillcolor) --TR
202 _copy(img, c_img, x, y + h - r - 2, _NIL, r, r + 1, _NIL, bClip, BSAND, fillcolor) --BL
203 _copy(img, c_img, x + w - r - 2, y + h - r - 2, r, r, r + 1, r + 1, bClip, BSAND, fillcolor) --BR
204 c_img = _NIL
205
206 -- finish filling areas circles didn't cover
207 _clear(img, fillcolor, x + r, y, x + w - r, y + h - 1, bClip)
208 _clear(img, fillcolor, x, y + r, x + r, y + h - r, bClip)
209 _clear(img, fillcolor, x + w - r, y + r, x + w - 1, y + h - r - 1, bClip)
210
211 if fillcolor ~= color then
212 rounded_rect(img, x, y, w, h, r, color, bClip)
213 end
214 end
215
216 -- draws an image at xy coord in dest image
217 _draw.image = function(dst, src, x, y, bClip)
218 if not src then --make sure an image was passed, otherwise bail
219 rb.splash(rb.HZ, "No Image!")
220 return _NIL
221 end
222
223 _copy(dst, src, x, y, 1, 1, _NIL, _NIL, bClip)
224 end
225
226 -- expose internal functions to the outside through _draw table
227 _draw.hline = hline
228 _draw.vline = vline
229 _draw.rect = rect
230 _draw.rounded_rect = rounded_rect
231end -- _draw functions
232
233return _draw
234