diff options
Diffstat (limited to 'apps/plugins/lua/include_lua/draw.lua')
-rw-r--r-- | apps/plugins/lua/include_lua/draw.lua | 229 |
1 files changed, 96 insertions, 133 deletions
diff --git a/apps/plugins/lua/include_lua/draw.lua b/apps/plugins/lua/include_lua/draw.lua index 4409ab7838..7b239339ff 100644 --- a/apps/plugins/lua/include_lua/draw.lua +++ b/apps/plugins/lua/include_lua/draw.lua | |||
@@ -52,35 +52,39 @@ if not rb.lcd_framebuffer then rb.splash(rb.HZ, "No Support!") return nil end | |||
52 | 52 | ||
53 | local _draw = {} do | 53 | local _draw = {} do |
54 | 54 | ||
55 | local rocklib_image = getmetatable(rb.lcd_framebuffer()) | ||
56 | setmetatable(_draw, rocklib_image) | ||
57 | |||
55 | -- Internal Constants | 58 | -- Internal Constants |
56 | local _LCD = rb.lcd_framebuffer() | 59 | local _LCD = rb.lcd_framebuffer() |
57 | local LCD_W, LCD_H = rb.LCD_WIDTH, rb.LCD_HEIGHT | 60 | local LCD_W, LCD_H = rb.LCD_WIDTH, rb.LCD_HEIGHT |
58 | local BSAND = 8 -- blits color to dst if src <> 0 | 61 | local BSAND = 8 -- blits color to dst if src <> 0 |
59 | local _NIL = nil -- nil placeholder | 62 | local _NIL = nil -- nil placeholder |
60 | 63 | ||
61 | local function set_viewport(vp) | 64 | local _abs = math.abs |
62 | if not vp then rb.set_viewport() return end | 65 | local _clear = rocklib_image.clear |
63 | if rb.LCD_DEPTH == 2 then -- invert 2-bit screens | 66 | local _copy = rocklib_image.copy |
64 | --vp.drawmode = bit.bxor(vp.drawmode, 4) | 67 | local _ellipse = rocklib_image.ellipse |
65 | vp.fg_pattern = 3 - vp.fg_pattern | 68 | local _get = rocklib_image.get |
66 | vp.bg_pattern = 3 - vp.bg_pattern | 69 | local _line = rocklib_image.line |
67 | end | 70 | local _marshal = rocklib_image.marshal |
68 | rb.set_viewport(vp) | 71 | local _min = math.min |
69 | end | 72 | local _newimg = rb.new_image |
73 | local _points = rocklib_image.points | ||
70 | 74 | ||
71 | -- line | 75 | -- line |
72 | local function line(img, x1, y1, x2, y2, color, bClip) | 76 | _draw.line = function(img, x1, y1, x2, y2, color, bClip) |
73 | img:line(x1, y1, x2, y2, color, bClip) | 77 | _line(img, x1, y1, x2, y2, color, bClip) |
74 | end | 78 | end |
75 | 79 | ||
76 | -- horizontal line; x, y define start point; length in horizontal direction | 80 | -- horizontal line; x, y define start point; length in horizontal direction |
77 | local function hline(img, x, y , length, color, bClip) | 81 | local function hline(img, x, y , length, color, bClip) |
78 | img:line(x, y, x + length, _NIL, color, bClip) | 82 | _line(img, x, y, x + length, _NIL, color, bClip) |
79 | end | 83 | end |
80 | 84 | ||
81 | -- vertical line; x, y define start point; length in vertical direction | 85 | -- vertical line; x, y define start point; length in vertical direction |
82 | local function vline(img, x, y , length, color, bClip) | 86 | local function vline(img, x, y , length, color, bClip) |
83 | img:line(x, y, _NIL, y + length, color, bClip) | 87 | _line(img, x, y, _NIL, y + length, color, bClip) |
84 | end | 88 | end |
85 | 89 | ||
86 | -- draws a non-filled figure based on points in t-points | 90 | -- draws a non-filled figure based on points in t-points |
@@ -100,7 +104,7 @@ local _draw = {} do | |||
100 | 104 | ||
101 | local pt2 = t_points[i + 1] or pt_first_last-- first and last point | 105 | local pt2 = t_points[i + 1] or pt_first_last-- first and last point |
102 | 106 | ||
103 | img:line(pt1[1] + x, pt1[2] + y, pt2[1]+x, pt2[2]+y, color, bClip) | 107 | _line(img, pt1[1] + x, pt1[2] + y, pt2[1] + x, pt2[2] + y, color, bClip) |
104 | end | 108 | end |
105 | 109 | ||
106 | end | 110 | end |
@@ -109,90 +113,80 @@ local _draw = {} do | |||
109 | local function rect(img, x, y, width, height, color, bClip) | 113 | local function rect(img, x, y, width, height, color, bClip) |
110 | if width == 0 or height == 0 then return end | 114 | if width == 0 or height == 0 then return end |
111 | 115 | ||
112 | local ppt = {{0, 0}, {width, 0}, {width, height}, {0, height}} | 116 | polyline(img, x, y, {{0, 0}, {width, 0}, {width, height}, {0, height}}, color, true, bClip) |
113 | polyline(img, x, y, ppt, color, true, bClip) | 117 | |
114 | --[[ | ||
115 | vline(img, x, y, height, color, bClip); | ||
116 | vline(img, x + width, y, height, color, bClip); | ||
117 | hline(img, x, y, width, color, bClip); | ||
118 | hline(img, x, y + height, width, color, bClip);]] | ||
119 | end | 118 | end |
120 | 119 | ||
121 | -- filled rect, fillcolor is color if left empty | 120 | -- filled rect, fillcolor is color if left empty |
122 | local function rect_filled(img, x, y, width, height, color, fillcolor, bClip) | 121 | _draw.rect_filled = function(img, x, y, width, height, color, fillcolor, bClip) |
123 | if width == 0 or height == 0 then return end | 122 | if width == 0 or height == 0 then return end |
124 | 123 | ||
125 | if not fillcolor then | 124 | if not fillcolor then |
126 | img:clear(color, x, y, x + width, y + height, bClip) | 125 | _clear(img, color, x, y, x + width, y + height, bClip) |
127 | else | 126 | else |
128 | img:clear(fillcolor, x, y, x + width, y + height, bClip) | 127 | _clear(img, fillcolor, x, y, x + width, y + height, bClip) |
129 | rect(img, x, y, width, height, color, bClip) | 128 | rect(img, x, y, width, height, color, bClip) |
130 | end | 129 | end |
131 | end | 130 | end |
132 | 131 | ||
133 | -- circle cx,cy define center point | 132 | -- circle cx,cy define center point |
134 | local function circle(img, cx, cy, radius, color, bClip) | 133 | _draw.circle = function(img, cx, cy, radius, color, bClip) |
135 | local r = radius | 134 | local r = radius |
136 | img:ellipse(cx - r, cy - r, cx + r, cy + r, color, _NIL, bClip) | 135 | _ellipse(img, cx - r, cy - r, cx + r, cy + r, color, _NIL, bClip) |
137 | end | 136 | end |
138 | 137 | ||
139 | -- filled circle cx,cy define center, fillcolor is color if left empty | 138 | -- filled circle cx,cy define center, fillcolor is color if left empty |
140 | local function circle_filled(img, cx, cy, radius, color, fillcolor, bClip) | 139 | _draw.circle_filled = function(img, cx, cy, radius, color, fillcolor, bClip) |
141 | fillcolor = fillcolor or color | 140 | fillcolor = fillcolor or color |
142 | local r = radius | 141 | local r = radius |
143 | img:ellipse(cx - r, cy - r, cx + r, cy + r, color, fillcolor, bClip) | 142 | _ellipse(img, cx - r, cy - r, cx + r, cy + r, color, fillcolor, bClip) |
144 | end | 143 | end |
145 | 144 | ||
146 | -- ellipse that fits into defined rect | 145 | -- ellipse that fits into defined rect |
147 | local function ellipse_rect(img, x1, y1, x2, y2, color, bClip) | 146 | _draw.ellipse_rect = function(img, x1, y1, x2, y2, color, bClip) |
148 | img:ellipse(x1, y1, x2, y2, color, _NIL, bClip) | 147 | _ellipse(img, x1, y1, x2, y2, color, _NIL, bClip) |
149 | end | 148 | end |
150 | 149 | ||
151 | --ellipse that fits into defined rect, fillcolor is color if left empty | 150 | --ellipse that fits into defined rect, fillcolor is color if left empty |
152 | local function ellipse_rect_filled(img, x1, y1, x2, y2, color, fillcolor, bClip) | 151 | _draw.ellipse_rect_filled = function(img, x1, y1, x2, y2, color, fillcolor, bClip) |
153 | if not fillcolor then fillcolor = color end | 152 | if not fillcolor then fillcolor = color end |
154 | 153 | ||
155 | img:ellipse(x1, y1, x2, y2, color, fillcolor, bClip) | 154 | _ellipse(img, x1, y1, x2, y2, color, fillcolor, bClip) |
156 | end | 155 | end |
157 | 156 | ||
158 | -- ellipse cx, cy define center point; a, b the major/minor axis | 157 | -- ellipse cx, cy define center point; a, b the major/minor axis |
159 | local function ellipse(img, cx, cy, a, b, color, bClip) | 158 | _draw.ellipse = function(img, cx, cy, a, b, color, bClip) |
160 | img:ellipse(cx - a, cy - b, cx + a, cy + b, color, _NIL, bClip) | 159 | _ellipse(img, cx - a, cy - b, cx + a, cy + b, color, _NIL, bClip) |
161 | end | 160 | end |
162 | 161 | ||
163 | -- filled ellipse cx, cy define center point; a, b the major/minor axis | 162 | -- filled ellipse cx, cy define center point; a, b the major/minor axis |
164 | -- fillcolor is color if left empty | 163 | -- fillcolor is color if left empty |
165 | local function ellipse_filled(img, cx, cy, a, b, color, fillcolor, bClip) | 164 | _draw.ellipse_filled = function(img, cx, cy, a, b, color, fillcolor, bClip) |
166 | if not fillcolor then fillcolor = color end | 165 | if not fillcolor then fillcolor = color end |
167 | 166 | ||
168 | img:ellipse(cx - a, cy - b, cx + a, cy + b, color, fillcolor, bClip) | 167 | _ellipse(img, cx - a, cy - b, cx + a, cy + b, color, fillcolor, bClip) |
169 | end | 168 | end |
170 | 169 | ||
171 | -- rounded rectangle | 170 | -- rounded rectangle |
172 | local function rounded_rect(img, x, y, w, h, radius, color, bClip) | 171 | local function rounded_rect(img, x, y, w, h, radius, color, bClip) |
173 | local c_img | 172 | local c_img |
174 | |||
175 | local function blit(dx, dy, sx, sy, ox, oy) | ||
176 | img:copy(c_img, dx, dy, sx, sy, ox, oy, bClip, BSAND, color) | ||
177 | end | ||
178 | |||
179 | if w == 0 or h == 0 then return end | 173 | if w == 0 or h == 0 then return end |
180 | 174 | ||
181 | -- limit the radius of the circle otherwise it will overtake the rect | 175 | -- limit the radius of the circle otherwise it will overtake the rect |
182 | radius = math.min(w / 2, radius) | 176 | radius = _min(w / 2, radius) |
183 | radius = math.min(h / 2, radius) | 177 | radius = _min(h / 2, radius) |
184 | 178 | ||
185 | local r = radius | 179 | local r = radius |
186 | 180 | ||
187 | c_img = rb.new_image(r * 2 + 1, r * 2 + 1) | 181 | c_img = _newimg(r * 2 + 1, r * 2 + 1) |
188 | c_img:clear(0) | 182 | _clear(c_img, 0) |
189 | circle(c_img, r + 1, r + 1, r, 0xFFFFFF) | 183 | _ellipse(c_img, 1, 1, 1 + r + r, 1 + r + r, 0x1, _NIL, bClip) |
190 | 184 | ||
191 | -- copy 4 pieces of circle to their respective corners | 185 | -- copy 4 pieces of circle to their respective corners |
192 | blit(x, y, _NIL, _NIL, r + 1, r + 1) --TL | 186 | _copy(img, c_img, x, y, _NIL, _NIL, r + 1, r + 1, bClip, BSAND, color) --TL |
193 | blit(x + w - r - 2, y, r, _NIL, r + 1, r + 1) --TR | 187 | _copy(img, c_img, x + w - r - 2, y, r, _NIL, r + 1, r + 1, bClip, BSAND, color) --TR |
194 | blit(x , y + h - r - 2, _NIL, r, r + 1, _NIL) --BL | 188 | _copy(img, c_img, x , y + h - r - 2, _NIL, r, r + 1, _NIL, bClip, BSAND, color) --BL |
195 | blit(x + w - r - 2, y + h - r - 2, r, r, r + 1, r + 1)--BR | 189 | _copy(img, c_img, x + w - r - 2, y + h - r - 2, r, r, r + 1, r + 1, bClip, BSAND, color)--BR |
196 | c_img = _NIL | 190 | c_img = _NIL |
197 | 191 | ||
198 | vline(img, x, y + r, h - r * 2, color, bClip); | 192 | vline(img, x, y + r, h - r * 2, color, bClip); |
@@ -202,38 +196,34 @@ local _draw = {} do | |||
202 | end | 196 | end |
203 | 197 | ||
204 | -- rounded rectangle fillcolor is color if left empty | 198 | -- rounded rectangle fillcolor is color if left empty |
205 | local function rounded_rect_filled(img, x, y, w, h, radius, color, fillcolor, bClip) | 199 | _draw.rounded_rect_filled = function(img, x, y, w, h, radius, color, fillcolor, bClip) |
206 | local c_img | 200 | local c_img |
207 | 201 | ||
208 | local function blit(dx, dy, sx, sy, ox, oy) | ||
209 | img:copy(c_img, dx, dy, sx, sy, ox, oy, bClip, BSAND, fillcolor) | ||
210 | end | ||
211 | |||
212 | if w == 0 or h == 0 then return end | 202 | if w == 0 or h == 0 then return end |
213 | 203 | ||
214 | if not fillcolor then fillcolor = color end | 204 | if not fillcolor then fillcolor = color end |
215 | 205 | ||
216 | -- limit the radius of the circle otherwise it will overtake the rect | 206 | -- limit the radius of the circle otherwise it will overtake the rect |
217 | radius = math.min(w / 2, radius) | 207 | radius = _min(w / 2, radius) |
218 | radius = math.min(h / 2, radius) | 208 | radius = _min(h / 2, radius) |
219 | 209 | ||
220 | local r = radius | 210 | local r = radius |
221 | 211 | ||
222 | c_img = rb.new_image(r * 2 + 1, r * 2 + 1) | 212 | c_img = _newimg(r * 2 + 1, r * 2 + 1) |
223 | c_img:clear(0) | 213 | _clear(c_img, 0) |
224 | circle_filled(c_img, r + 1, r + 1, r, fillcolor) | 214 | _ellipse(c_img, 1, 1, 1 + r + r, 1 + r + r, 0x1, 0x1, bClip) |
225 | 215 | ||
226 | -- copy 4 pieces of circle to their respective corners | 216 | -- copy 4 pieces of circle to their respective corners |
227 | blit(x, y, _NIL, _NIL, r + 1, r + 1) --TL | 217 | _copy(img, c_img, x, y, _NIL, _NIL, r + 1, r + 1, bClip, BSAND, fillcolor) --TL |
228 | blit(x + w - r - 2, y, r, _NIL, r + 1, r + 1) --TR | 218 | _copy(img, c_img, x + w - r - 2, y, r, _NIL, r + 1, r + 1, bClip, BSAND, fillcolor) --TR |
229 | blit(x, y + h - r - 2, _NIL, r, r + 1, _NIL) --BL | 219 | _copy(img, c_img, x, y + h - r - 2, _NIL, r, r + 1, _NIL, bClip, BSAND, fillcolor) --BL |
230 | blit(x + w - r - 2, y + h - r - 2, r, r, r + 1, r + 1) --BR | 220 | _copy(img, c_img, x + w - r - 2, y + h - r - 2, r, r, r + 1, r + 1, bClip, BSAND, fillcolor) --BR |
231 | c_img = _NIL | 221 | c_img = _NIL |
232 | 222 | ||
233 | -- finish filling areas circles didn't cover | 223 | -- finish filling areas circles didn't cover |
234 | img:clear(fillcolor, x + r, y, x + w - r, y + h - 1, bClip) | 224 | _clear(img, fillcolor, x + r, y, x + w - r, y + h - 1, bClip) |
235 | img:clear(fillcolor, x, y + r, x + r, y + h - r, bClip) | 225 | _clear(img, fillcolor, x, y + r, x + r, y + h - r, bClip) |
236 | img:clear(fillcolor, x + w - r, y + r, x + w - 1, y + h - r - 1, bClip) | 226 | _clear(img, fillcolor, x + w - r, y + r, x + w - 1, y + h - r - 1, bClip) |
237 | 227 | ||
238 | if fillcolor ~= color then | 228 | if fillcolor ~= color then |
239 | rounded_rect(img, x, y, w, h, r, color, bClip) | 229 | rounded_rect(img, x, y, w, h, r, color, bClip) |
@@ -241,23 +231,23 @@ local _draw = {} do | |||
241 | end | 231 | end |
242 | 232 | ||
243 | -- draws an image at xy coord in dest image | 233 | -- draws an image at xy coord in dest image |
244 | local function image(dst, src, x, y, bClip) | 234 | _draw.image = function(dst, src, x, y, bClip) |
245 | if not src then --make sure an image was passed, otherwise bail | 235 | if not src then --make sure an image was passed, otherwise bail |
246 | rb.splash(rb.HZ, "No Image!") | 236 | rb.splash(rb.HZ, "No Image!") |
247 | return _NIL | 237 | return _NIL |
248 | end | 238 | end |
249 | 239 | ||
250 | dst:copy(src, x, y, 1, 1, _NIL, _NIL, bClip) | 240 | _copy(dst, src, x, y, 1, 1, _NIL, _NIL, bClip) |
251 | end | 241 | end |
252 | 242 | ||
253 | -- floods an area of targetclr with fillclr x, y specifies the start seed | 243 | -- floods an area of targetclr with fillclr x, y specifies the start seed |
254 | function flood_fill(img, x, y, targetclr, fillclr) | 244 | _draw.flood_fill = function(img, x, y, targetclr, fillclr) |
255 | -- scanline 4-way flood algorithm | 245 | -- scanline 4-way flood algorithm |
256 | -- ^ | 246 | -- ^ |
257 | -- <--------x---> | 247 | -- <--------x---> |
258 | -- v | 248 | -- v |
259 | -- check that target color doesn't = fill and the first point is target color | 249 | -- check that target color doesn't = fill and the first point is target color |
260 | if targetclr == fillclr or targetclr ~= img:get(x,y, true) then return end | 250 | if targetclr == fillclr or targetclr ~= _get(img, x, y, true) then return end |
261 | local max_w = img:width() | 251 | local max_w = img:width() |
262 | local max_h = img:height() | 252 | local max_h = img:height() |
263 | 253 | ||
@@ -271,21 +261,20 @@ local _draw = {} do | |||
271 | -- y coordinates are in even indices | 261 | -- y coordinates are in even indices |
272 | 262 | ||
273 | local qtail = 0 | 263 | local qtail = 0 |
274 | local iter_n; -- North iteration | ||
275 | local iter_s; -- South iteration | ||
276 | 264 | ||
277 | local function check_ns(val, x, y) | 265 | local function check_ns(val, x, y) |
278 | if targetclr == val then | 266 | if targetclr == val then |
279 | if targetclr == iter_n() then | 267 | y = y - 1 |
268 | if targetclr == _get(img, x, y, true) then -- north | ||
280 | qtail = qtail + 2 | 269 | qtail = qtail + 2 |
281 | qpt[qtail - 1] = x | 270 | qpt[qtail - 1] = x |
282 | qpt[qtail] = (y - 1) | 271 | qpt[qtail] = y |
283 | end | 272 | end |
284 | 273 | y = y + 2 | |
285 | if targetclr == iter_s() then | 274 | if targetclr == _get(img, x, y, true) then -- south |
286 | qtail = qtail + 2 | 275 | qtail = qtail + 2 |
287 | qpt[qtail - 1] = x | 276 | qpt[qtail - 1] = x |
288 | qpt[qtail] = (y + 1) | 277 | qpt[qtail] = y |
289 | end | 278 | end |
290 | return fillclr | 279 | return fillclr |
291 | end | 280 | end |
@@ -293,17 +282,12 @@ local _draw = {} do | |||
293 | end | 282 | end |
294 | 283 | ||
295 | local function seed_pt(x, y) | 284 | local function seed_pt(x, y) |
296 | -- will never hit max_w * max_h^2 but make sure not to end early | 285 | -- should never hit max but make sure not to end early |
297 | for qhead = 2, max_w * max_h * max_w * max_h, 2 do | 286 | for qhead = 2, 0x40000000, 2 do |
298 | |||
299 | if targetclr == img:get(x, y, true) then | ||
300 | iter_n = img:points(x, y - 1, 1, y - 1) | ||
301 | iter_s = img:points(x, y + 1, 1, y + 1) | ||
302 | img:marshal(x, y, 1, y, _NIL, _NIL, true, check_ns) | ||
303 | 287 | ||
304 | iter_n = img:points(x + 1, y - 1, max_w, y - 1) | 288 | if targetclr == _get(img, x, y, true) then |
305 | iter_s = img:points(x + 1, y + 1, max_w, y + 1) | 289 | _marshal(img, x, y, 1, y, _NIL, _NIL, true, check_ns) -- west |
306 | img:marshal(x + 1, y, max_w, y, _NIL, _NIL, true, check_ns) | 290 | _marshal(img, x + 1, y, max_w, y, _NIL, _NIL, true, check_ns) -- east |
307 | end | 291 | end |
308 | 292 | ||
309 | x = qpt[qhead - 1] | 293 | x = qpt[qhead - 1] |
@@ -320,7 +304,7 @@ local _draw = {} do | |||
320 | end -- flood_fill | 304 | end -- flood_fill |
321 | 305 | ||
322 | -- draws a closed figure based on points in t_points | 306 | -- draws a closed figure based on points in t_points |
323 | local function polygon(img, x, y, t_points, color, fillcolor, bClip) | 307 | _draw.polygon = function(img, x, y, t_points, color, fillcolor, bClip) |
324 | if #t_points < 2 then error("not enough points", 3) end | 308 | if #t_points < 2 then error("not enough points", 3) end |
325 | 309 | ||
326 | if fillcolor then | 310 | if fillcolor then |
@@ -335,35 +319,41 @@ local _draw = {} do | |||
335 | if pt[2] < y_min then y_min = pt[2] end | 319 | if pt[2] < y_min then y_min = pt[2] end |
336 | if pt[2] > y_max then y_max = pt[2] end | 320 | if pt[2] > y_max then y_max = pt[2] end |
337 | end | 321 | end |
338 | w = math.abs(x_max) + math.abs(x_min) | 322 | w = _abs(x_max) + _abs(x_min) |
339 | h = math.abs(y_max) + math.abs(y_min) | 323 | h = _abs(y_max) + _abs(y_min) |
340 | x_min = x_min - 2 -- leave a border to use flood_fill | 324 | x_min = x_min - 2 -- leave a border to use flood_fill |
341 | y_min = y_min - 2 | 325 | y_min = y_min - 2 |
342 | 326 | ||
343 | local fill_img = rb.new_image(w + 3, h + 3) | 327 | local fill_img = _newimg(w + 3, h + 3) |
344 | fill_img:clear(0xFFFFFF) | 328 | _clear(fill_img, 0x1) |
345 | 329 | ||
346 | for i = 1, #t_points, 1 do | 330 | for i = 1, #t_points, 1 do |
347 | local pt1 = t_points[i] | 331 | local pt1 = t_points[i] |
348 | local pt2 = t_points[i + 1] or t_points[1]-- first and last point | 332 | local pt2 = t_points[i + 1] or t_points[1]-- first and last point |
349 | fill_img:line(pt1[1] - x_min, pt1[2] - y_min, | 333 | _line(fill_img, pt1[1] - x_min, pt1[2] - y_min, |
350 | pt2[1]- x_min, pt2[2] - y_min, 0) | 334 | pt2[1]- x_min, pt2[2] - y_min, 0) |
351 | 335 | ||
352 | end | 336 | end |
353 | flood_fill(fill_img, fill_img:width(), fill_img:height() , 1, 0) | 337 | _draw.flood_fill(fill_img, fill_img:width(), fill_img:height() , 0x1, 0x0) |
354 | img:copy(fill_img, x - 1, y - 1, _NIL, _NIL, _NIL, _NIL, bClip, BSAND, fillcolor) | 338 | _copy(img, fill_img, x - 1, y - 1, _NIL, _NIL, _NIL, _NIL, bClip, BSAND, fillcolor) |
355 | end | 339 | end |
356 | 340 | ||
357 | polyline(img, x, y, t_points, color, true, bClip) | 341 | polyline(img, x, y, t_points, color, true, bClip) |
358 | end | 342 | end |
359 | 343 | ||
360 | -- draw text onto image if width/height are supplied text is centered | 344 | -- draw text onto image if width/height are supplied text is centered |
361 | local function text(img, x, y, width, height, font, color, text) | 345 | _draw.text = function(img, x, y, width, height, font, color, text) |
362 | font = font or rb.FONT_UI | 346 | font = font or rb.FONT_UI |
363 | 347 | ||
364 | local opts = {x = 0, y = 0, width = LCD_W - 1, height = LCD_H - 1, | 348 | local opts = {x = 0, y = 0, width = LCD_W - 1, height = LCD_H - 1, |
365 | font = font, drawmode = 3, fg_pattern = 0xFFFFFF, bg_pattern = 0} | 349 | font = font, drawmode = 3, fg_pattern = 0x1, bg_pattern = 0} |
366 | set_viewport(opts) | 350 | |
351 | if rb.LCD_DEPTH == 2 then -- invert 2-bit screens | ||
352 | --vp.drawmode = bit.bxor(vp.drawmode, 4) | ||
353 | vp.fg_pattern = 3 - vp.fg_pattern | ||
354 | vp.bg_pattern = 3 - vp.bg_pattern | ||
355 | end | ||
356 | rb.set_viewport(opts) | ||
367 | 357 | ||
368 | local res, w, h = rb.font_getstringsize(text, font) | 358 | local res, w, h = rb.font_getstringsize(text, font) |
369 | 359 | ||
@@ -380,8 +370,8 @@ local _draw = {} do | |||
380 | end | 370 | end |
381 | 371 | ||
382 | -- make a copy of the current screen for later | 372 | -- make a copy of the current screen for later |
383 | local screen_img = rb.new_image(LCD_W, LCD_H) | 373 | local screen_img = _newimg(LCD_W, LCD_H) |
384 | screen_img:copy(_LCD) | 374 | _copy(screen_img, _LCD) |
385 | 375 | ||
386 | -- check if the screen buffer is supplied image if so set img to the copy | 376 | -- check if the screen buffer is supplied image if so set img to the copy |
387 | if img == _LCD then | 377 | if img == _LCD then |
@@ -391,10 +381,6 @@ local _draw = {} do | |||
391 | -- we will be printing the text to the screen then blitting into img | 381 | -- we will be printing the text to the screen then blitting into img |
392 | rb.lcd_clear_display() | 382 | rb.lcd_clear_display() |
393 | 383 | ||
394 | local function blit(dx, dy) | ||
395 | img:copy(_LCD, dx, dy, _NIL, _NIL, _NIL, _NIL, false, BSAND, color) | ||
396 | end | ||
397 | |||
398 | if w > LCD_W then -- text is too long for the screen do it in chunks | 384 | if w > LCD_W then -- text is too long for the screen do it in chunks |
399 | local l = 1 | 385 | local l = 1 |
400 | local resp, wp, hp | 386 | local resp, wp, hp |
@@ -417,7 +403,7 @@ local _draw = {} do | |||
417 | end | 403 | end |
418 | 404 | ||
419 | -- using the mask we made blit color into img | 405 | -- using the mask we made blit color into img |
420 | blit(x + width, y + height) | 406 | _copy(img, _LCD, x + width, y + height, _NIL, _NIL, _NIL, _NIL, false, BSAND, color) |
421 | x = x + wp | 407 | x = x + wp |
422 | rb.lcd_clear_display() | 408 | rb.lcd_clear_display() |
423 | lenr = text:len() | 409 | lenr = text:len() |
@@ -426,43 +412,20 @@ local _draw = {} do | |||
426 | rb.lcd_putsxy(0, 0, text) | 412 | rb.lcd_putsxy(0, 0, text) |
427 | 413 | ||
428 | -- using the mask we made blit color into img | 414 | -- using the mask we made blit color into img |
429 | blit(x + width, y + height) | 415 | _copy(img, _LCD, x + width, y + height, _NIL, _NIL, _NIL, _NIL, false, BSAND, color) |
430 | end | 416 | end |
431 | 417 | ||
432 | _LCD:copy(screen_img) -- restore screen | 418 | _copy(_LCD, screen_img) -- restore screen |
433 | set_viewport() -- set viewport default | 419 | rb.set_viewport() -- set viewport default |
434 | return res, w, h | 420 | return res, w, h |
435 | end | 421 | end |
436 | 422 | ||
437 | -- expose functions to the outside through _draw table | 423 | -- expose internal functions to the outside through _draw table |
438 | _draw.image = image | ||
439 | _draw.text = text | ||
440 | _draw.line = line | ||
441 | _draw.hline = hline | 424 | _draw.hline = hline |
442 | _draw.vline = vline | 425 | _draw.vline = vline |
443 | _draw.polygon = polygon | ||
444 | _draw.polyline = polyline | 426 | _draw.polyline = polyline |
445 | _draw.rect = rect | 427 | _draw.rect = rect |
446 | _draw.circle = circle | ||
447 | _draw.ellipse = ellipse | ||
448 | _draw.flood_fill = flood_fill | ||
449 | _draw.ellipse_rect = ellipse_rect | ||
450 | _draw.rounded_rect = rounded_rect | 428 | _draw.rounded_rect = rounded_rect |
451 | -- filled functions use color as fillcolor if fillcolor is left empty... | ||
452 | _draw.rect_filled = rect_filled | ||
453 | _draw.circle_filled = circle_filled | ||
454 | _draw.ellipse_filled = ellipse_filled | ||
455 | _draw.ellipse_rect_filled = ellipse_rect_filled | ||
456 | _draw.rounded_rect_filled = rounded_rect_filled | ||
457 | |||
458 | -- adds the above _draw functions into the metatable for RLI_IMAGE | ||
459 | local ex = getmetatable(rb.lcd_framebuffer()) | ||
460 | for k, v in pairs(_draw) do | ||
461 | if ex[k] == _NIL then | ||
462 | ex[k] = v | ||
463 | end | ||
464 | end | ||
465 | |||
466 | end -- _draw functions | 429 | end -- _draw functions |
467 | 430 | ||
468 | return _draw | 431 | return _draw |