A modern Music Player Daemon based on Rockbox open source high quality audio player
libadwaita
audio
rust
zig
deno
mpris
rockbox
mpd
1--[[ Lua Poly 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 _poly.polygon
26 _poly.polyline
27]]
28--[[
29 every 2 elements in t_pts is an x, y coord pair
30 p[?] = {x,y,x,y,x,y}
31 lines get drawn between the coords
32]]
33if not rb.lcd_framebuffer then rb.splash(rb.HZ, "No Support!") return nil end
34
35local _poly = {} do
36 -- Internal Constants
37 local rocklib_image = getmetatable(rb.lcd_framebuffer())
38 local BSAND = 8 -- blits color to dst if src <> 0
39 local _NIL = nil -- nil placeholder
40
41 local _abs = math.abs
42 local _clear = rocklib_image.clear
43 local _copy = rocklib_image.copy
44 local _line = rocklib_image.line
45 local _newimg = rb.new_image
46 local flood_fill
47
48 -- draws a non-filled figure based on points in t-points
49 local function polyline(img, x, y, t_pts, color, bClosed, bClip, scale_x, scale_y, b_size_only)
50 local draw_fn = _line
51 if b_size_only == true then draw_fn = function() end end
52 scale_x = scale_x or 1
53 scale_y = scale_y or 1
54
55 local pt_first_last, pt1, pt2
56 local max_x, max_y = 0, 0
57 local len = #t_pts
58 if len < 4 then error("not enough points", 3) end
59
60 if bClosed then
61 pt_first_last = {t_pts[1] * scale_x, t_pts[2] * scale_y}
62 else
63 pt_first_last = {t_pts[len - 1] * scale_x, t_pts[len] * scale_y}
64 end
65
66 pt2 = {t_pts[1] * scale_x, t_pts[2] * scale_y}
67 for i = 3, len + 2, 2 do
68 pt1 = pt2
69 if t_pts[i + 1] == nil then
70 pt2 = pt_first_last
71 else
72 pt2 = {t_pts[i] * scale_x, t_pts[i + 1] * scale_y}
73 end-- first and last point
74 draw_fn(img, pt1[1] + x, pt1[2] + y, pt2[1] + x, pt2[2] + y, color, bClip)
75 if pt1[1] > max_x then max_x = pt1[1] end
76 if pt1[2] > max_y then max_y = pt1[2] end
77 end
78 if pt2[1] > max_x then max_x = pt2[1] end
79 if pt2[2] > max_y then max_y = pt2[2] end
80 return max_x + x, max_y + y
81 end
82
83 -- draws a closed figure based on points in t_pts
84 _poly.polygon = function(img, x, y, t_pts, color, fillcolor, bClip, scale_x, scale_y, b_size_only)
85 scale_x = scale_x or 1
86 scale_y = scale_y or 1
87 if #t_pts < 2 then error("not enough points", 3) end
88
89 if fillcolor and b_size_only ~= true then
90 flood_fill = flood_fill or require("draw_floodfill")
91 local x_min, x_max = 0, 0
92 local y_min, y_max = 0, 0
93 local w, h = 0, 0
94 local pt1, pt2
95 -- find boundries of polygon
96 for i = 1, #t_pts, 2 do
97 if t_pts[i] < x_min then x_min = t_pts[i] end
98 if t_pts[i] > x_max then x_max = t_pts[i] end
99
100 if t_pts[i+1] < y_min then y_min = t_pts[i+1] end
101 if t_pts[i+1] > y_max then y_max = t_pts[i+1] end
102 end
103 x_max = x_max * scale_x
104 x_min = x_min * scale_x
105 y_max = y_max * scale_y
106 y_min = y_min * scale_y
107 w = _abs(x_max) + _abs(x_min)
108 h = _abs(y_max) + _abs(y_min)
109 x_min = -(x_min - 2) -- leave a border to use flood_fill
110 y_min = -(y_min - 2)
111
112 local fill_img = _newimg(w + 3, h + 3)
113 _clear(fill_img, 0x1)
114
115 polyline(fill_img, x_min, y_min, t_pts,
116 0x0, true, bClip, scale_x, scale_y)
117 -- flood the outside of the figure with 0 the inside will be fillcolor
118 flood_fill(fill_img, fill_img:width(), fill_img:height() , 0x1, 0x0)
119 _copy(img, fill_img, x - 1, y - 1,
120 _NIL, _NIL, _NIL, _NIL, bClip, BSAND, fillcolor)
121 end
122
123 return polyline(img, x, y, t_pts, color, true, bClip, scale_x, scale_y, b_size_only)
124 end
125
126 -- expose internal functions to the outside through _poly table
127 _poly.polyline = polyline
128end
129return _poly