lua use lcd_drawline to draw lines inside rliimages

rewrite draw_text to use new viewport buffer

set_viewport now accepts rliimage to allowe interfacing with rb. functions

fix long standing 2-bit bug with text drawing in lua
fix 2-bit img
saving bug (i'm guessing just a one off, just enabled clipping)

fix font_getstringsize bug

fix shape of numbers draw_num.lua also add auto centering

add page scrolling to printtable

add a new demo script 'stars'

Change-Id: I866905cee82ee89ebc0eb020a56a7ecdb101bf5e
This commit is contained in:
William Wilgus 2024-04-05 00:38:35 -04:00 committed by William Wilgus
parent 7f1b49693c
commit a6570b7d37
7 changed files with 432 additions and 107 deletions

View file

@ -20,17 +20,13 @@
*
****************************************************************************/
]]
--[[ Exposed Functions
_draw_nums.print; binary (base = 2) , octal (base = 8), hexadecimal (base = 16)
_draw_nums.nums; table of number characters
]]
if not rb.lcd_framebuffer then rb.splash(rb.HZ, "No Support!") return nil end
local _draw_nums = {} do
local _poly = require "draw_poly"
-- every 2 elements is an x, y coord pair
-- n[?] = {x,y,x,y,x,y}
local nums = {
@ -43,11 +39,11 @@ local _draw_nums = {} do
[2] = {1,1,3,1,4,2,4,3,3,4,1,5,1,7,4,7},
[3] = {1,1,3,1,4,2,4,3,3,4,2,4,3,4,4,5,4,6,3,7,1,7},
[4] = {1,1,1,3,2,4,4,4,4,1,4,7},
[5] = {1,1,4,1,1,1,1,4,3,4,4,5,4,7,1,7},
[6] = {1,2,1,4,1,6,2,7,3,7,4,6,4,4,1,4,1,2,2,1,4,1},
[5] = {1,1,4,1,1,1,1,4,3,4,4,5,4,6,3,7,1,7},
[6] = {1,2,1,4,1,6,2,7,3,7,4,6,4,5,3,4,1,4,1,2,2,1,3,1,4,2},
[7] = {1,1,4,1,4,2,1,7},
[8] = {1,2,1,6,2,7,3,7,4,6,4,4,1,4,4,4,4,2,3,1,2,1,1,2},
[9] = {4,6,4,4,4,2,3,1,2,1,1,2,1,4,4,4,4,6,3,7,1,7},
[8] = {1,2,4,5,4,6,3,7,2,7,1,6,1,5,4,2,3,1,2,1,1,2},
[9] = {4,6,4,4,4,2,3,1,2,1,1,2,1,3,2,4,4,4,4,6,3,7,2,7,1,6},
[10] = {1,7,1,4,4,4,4,7,4,2,3,1,2,1,1,2,1,4},
[11] = {1,1,1,7,3,7,4,6,4,5,3,4,1,4,3,4,4,3,4,2,3,1,1,1},
[12] = {4,2,3,1,2,1,1,2,1,6,2,7,3,7,4,6},
@ -56,8 +52,6 @@ local _draw_nums = {} do
[15] = {4,1,1,1,1,4,3,4,1,4,1,7},
}
_draw_nums.nums = nums
_draw_nums.print = function(img, num, x, y, chrw, color, base, prefix, bClip, scale_x, scale_y, t_nums)
scale_x = scale_x or 1
scale_y = scale_y or 1
@ -65,7 +59,6 @@ local _draw_nums = {} do
prefix = (prefix == nil or prefix == true) and true or false
t_nums = t_nums or nums
local max_x, max_y, digits = 0, 0, {}
if num <= 0 then
if num < 0 then
digits[-3] = -1
@ -74,7 +67,6 @@ local _draw_nums = {} do
digits[0] = 0
end
end
if not prefix and (base == 2 or base == 8 or base == 16) then
-- no prefix
elseif base == 2 then
@ -92,23 +84,20 @@ local _draw_nums = {} do
error("unknown number base: " .. base)
return nil
end
while num > 0 do -- get each digit (LeastSignificant)
digits[#digits + 1] = num % base;
num=num/base;
end
digits[#digits + 1] = digits[0] -- zero
digits[#digits + 1] = digits[-1] -- base prefix
digits[#digits + 1] = digits[-2] -- base prefix (hex)
digits[#digits + 1] = digits[-3] -- neg sign
for i = #digits, 1, -1 do
max_x, max_y = _poly.polyline(img, x, y, t_nums[digits[i]],
color, false, bClip, scale_x, scale_y)
if chrw == 0 then chrw = max_x end
x = x + chrw
end
return x, y + max_y, chrw
end
end

View file

@ -23,7 +23,6 @@
-- draw text onto image if width/height are supplied text is centered
if not rb.lcd_framebuffer then rb.splash(rb.HZ, "No Support!") return nil end
do
-- Internal Constants
local rocklib_image = getmetatable(rb.lcd_framebuffer())
@ -40,81 +39,35 @@ do
return function(img, x, y, width, height, font, color, text)
font = font or rb.FONT_UI
local opts = {x = 0, y = 0, width = LCD_W - 1, height = LCD_H - 1,
font = font, drawmode = 3, fg_pattern = 0x1, bg_pattern = 0}
if rb.LCD_DEPTH == 2 then -- invert 2-bit screens
--vp.drawmode = bit.bxor(vp.drawmode, 4)
opts.fg_pattern = 3 - opts.fg_pattern
opts.bg_pattern = 3 - opts.bg_pattern
end
rb.set_viewport(opts)
local res, w, h = rb.font_getstringsize(text, font)
if not width then
width = 0
if rb.lcd_rgbpack ~= _NIL then -- Color target
rb.set_viewport(img, {fg_pattern = color, font = font, drawmode = 2})--DRMODE_FG
else
width = (width - w) / 2
if color ~= 0 then color = 3 end--DRMODE_SOLID
rb.set_viewport(img, {font = font, drawmode = color})
end
if not height then
height = 0
else
height = (height - h) / 2
end
if width or height then
local res, w, h = rb.font_getstringsize(text, font)
-- make a copy of the current screen for later
--local screen_img = _newimg(LCD_W, LCD_H)
local screen_img = _newimg(LCD_W, h * 2)
_copy(screen_img, _LCD)
-- check if the screen buffer is supplied image if so set img to the copy
if img == _LCD then
img = screen_img
end
-- we will be printing the text to the screen then blitting into img
--rb.lcd_clear_display()
_clear(_LCD, opts.bg_pattern or 0, 1, 1, LCD_W, h * 2)
if w > LCD_W then -- text is too long for the screen do it in chunks
local l = 1
local resp, wp, hp
local lenr = text:len()
while lenr > 1 do
l = lenr
resp, wp, hp = rb.font_getstringsize(text:sub(1, l), font)
while wp >= LCD_W and l > 1 do
l = l - 1
resp, wp, hp = rb.font_getstringsize(text:sub( 1, l), font)
end
rb.lcd_putsxy(0, 0, text:sub(1, l))
text = text:sub(l)
if x + width > img:width() or y + height > img:height() then
break
end
-- using the mask we made blit color into img
_copy(img, _LCD, x + width, y + height, _NIL, _NIL, _NIL, _NIL, false, BSAND, color)
x = x + wp
--rb.lcd_clear_display()
_clear(_LCD, opts.bg_pattern or 0, 1, 1, LCD_W, h * 2)
lenr = text:len()
if not width then
width = 0
else
width = (width - w) / 2
end
else --w <= LCD_W
rb.lcd_putsxy(0, 0, text)
-- using the mask we made blit color into img
_copy(img, _LCD, x + width, y + height, _NIL, _NIL, _NIL, _NIL, false, BSAND, color)
if not height then
height = 0
else
height = (height - h) / 2
end
x = width + x
y = height + y
end
_copy(_LCD, screen_img) -- restore screen
rb.lcd_putsxy(x, y, text)
rb.set_viewport() -- set viewport default
return res, w, h
end

View file

@ -198,7 +198,7 @@ do
end
-- Bitmap lines start at bottom unless biHeight is negative
for point in _points(img, 1, h, w + bytesleft, 1) do
for point in _points(img, 1, h, w + bytesleft, 1, 1, 1, true) do
imgdata[#imgdata + 1] = fs_bytes_E(bpp, point or 0)
if #fbuffer >= 31 then -- buffered write, increase # for performance

View file

@ -145,13 +145,14 @@ function print_table(t, t_count, settings)
local wrap, justify, start, curpos, co_routine, hasheader, m_sel
local header_fgc, header_bgc, item_fgc, item_bgc, item_selc
local table_linedesc, drawsep, overflow, dpad_fn
local table_linedesc, drawsep, overflow, dpad_fn, pagescroll
do
local s = settings or _print.get_settings()
wrap, justify = s.wrap, s.justify
start, curpos = s.start, s.curpos
co_routine = s.co_routine
hasheader = s.hasheader
pagescroll = s.pagescroll
drawsep = s.drawsep
sb_width = s.sb_width or sb_width
m_sel = false
@ -234,8 +235,13 @@ function print_table(t, t_count, settings)
dpad_fn(t_p.col, -1, -t_p.col_scrl, t_p.row, -1, -t_p.row_scrl,
nil, overflow, (t_p.row + t_p.vcursor - 1))
if pagescroll == true then
t_p.row = t_p.row + y_chg * maxline - 1
end
t_p.vcursor = t_p.vcursor + y_chg
if t_p.vcursor > maxline or t_p.vcursor < t_p.vcursor_min then
t_p.row = yi
end
@ -268,6 +274,7 @@ function print_table(t, t_count, settings)
elseif y_chg ~= 0 then
--t_p.col = 0 -- reset column to the beginning
_print.clear()
_print.opt.sel_line(t_p.vcursor)
t_p.row_scrl = set_accel(timeb, t_p.row_scrl, t_p)