summaryrefslogtreecommitdiffstats
path: root/apps/plugins/lua/include_lua/print.lua
blob: 5ea0423f01254906d08154c3d8f1a1a5f37eadae (plain)
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
--[[ Lua Print functions
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2017 William Wilgus
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/
]]

--[[ Exposed Functions

    _print.clear
    _print.f
    _print.opt
    _print.opt.area
    _print.opt.autoupdate
    _print.opt.color
    _print.opt.column
    _print.opt.defaults
    _print.opt.get
    _print.opt.justify
    _print.opt.line
    _print.opt.overflow
    _print.opt.sel_line
    _print.opt.set

]]

if not rb.lcd_framebuffer then rb.splash(rb.HZ, "No Support!") return nil end

local _print = {} do

    -- internal constants
    local _clr = require("color") -- _clr functions required

    local _NIL = nil -- _NIL placeholder
    local _LCD = rb.lcd_framebuffer()
    local WHITE = _clr.set(-1, 255, 255, 255)
    local BLACK = _clr.set(0, 0, 0, 0)
    local DRMODE_SOLID = 3
    local col_buf, s_lines = {}, {}
    local _p_opts = _NIL
    local tabstring = string.rep(" ", 2)
-- print internal helper functions
--------------------------------------------------------------------------------
   -- clamps value to >= min and <= max
    local function clamp(val, min, max)
        -- Warning doesn't check if min < max
        if val < min then
            return min
        elseif val < max then
            return val
        end
        return max
    end

    -- Gets size of text
    local function text_extent(msg, font)
        -- res, w, h
        return rb.font_getstringsize(msg, font or rb.FONT_UI)
    end

    -- Updates a single line on the screen
    local function update_line(enabled, opts, line, h)
        if enabled ~= true then return end
        local o = opts or _p_opts
    -- updates screen in specified rectangle
        rb.lcd_update_rect(o.x - 1, o.y + line * h,
             clamp(o.x + o.width, 1, rb.LCD_WIDTH) - 1,
             clamp(o.y + line * h + 1 + h, 1, rb.LCD_HEIGHT) - 1)
    end

    -- Clears a single line on the screen
    local function clear_line(opts, line, h)
        local o = opts or _p_opts
        _LCD:clear(o.bg_pattern, o.x, o.y + line * h + 1,
                                 o.x + o.width, line * h + h + o.y)
    end

    -- Sets the maximum number of lines on the screen
    local function max_lines(opts)
        local h  = opts.height
        local _, _, th = text_extent("W", opts.font)
        return h / th
    end

    --saves the items displayed for side to side scroll
    local function col_buf_insert(msg, line, _p_opts)
        --if _p_opts.line <= 1 then col_buf = {} end
        if not col_buf[line] then
            table.insert(col_buf, line, msg)
        end
    end

    --replaces / strips tab characters
    local function check_escapes(o, msg)
--[[    --for replacing a variety of escapes
        local tabsz  = 2
        local tabstr = string.rep(" ", tabsz)
        local function repl(esc)
            local ret = ""
            if esc:sub(1,1) == "\t" then ret = string.rep(tabstr, esc:len()) end
            return ret
        end
        msg = msg:gsub("(%c+)", repl)
]]
        msg = msg:gsub("\t", tabstring)
        local res, w, h = text_extent(msg, o.font)
        return w, h, msg
    end
--------------------------------------------------------------------------------

    -- set defaults for print view
    local function set_defaults()
        _p_opts = { x = 1,
                    y = 1,
                    width = rb.LCD_WIDTH - 1,
                    height = rb.LCD_HEIGHT - 1,
                    font = rb.FONT_UI,
                    drawmode = DRMODE_SOLID,
                    fg_pattern = WHITE,
                    bg_pattern = BLACK,
                    sel_pattern = WHITE,
                    line = 1,
                    max_line = _NIL,
                    col = 0,
                    ovfl = "auto",
                    justify = "left",
                    autoupdate = true,
                  }
        _p_opts.max_line = max_lines(_p_opts)

        s_lines, col_buf = {}, {}
        return _p_opts
    end

    -- returns table with settings for print
    -- if bByRef is _NIL or false then a copy is returned
    local function get_settings(bByRef)
        _p_opts = _p_opts or set_defaults()
        if not bByRef then
            -- shallow copy of table
            local copy = {}
            for k, v in pairs(_p_opts) do
                copy[k] = v
            end
            return copy
        end

        return _p_opts
    end

    -- sets the settings for print with your passed table
    local function set_settings(t_opts)
        _p_opts = t_opts or set_defaults()
        if t_opts then
            _p_opts.max_line = max_lines(_p_opts)
            col_buf = {}
        end
    end

    -- sets colors for print
    local function set_color(fgclr, bgclr, selclr)
        local o = get_settings(true)

        if fgclr ~= _NIL then
            o.fg_pattern, o.sel_pattern = fgclr, fgclr
        end
        o.sel_pattern = selclr or o.sel_pattern
        o.bg_pattern = bgclr or o.bg_pattern
    end

    -- helper function sets up colors/marker for selected items
    local function show_selected(iLine, msg)
        local o = get_settings() -- using a copy of opts so changes revert

        if s_lines[iLine] == true then
            if not rb.lcd_set_background then
                o.drawmode = bit.bxor(o.drawmode, 4)
            else
                o.fg_pattern = o.bg_pattern
                o.bg_pattern = o.sel_pattern
            end
            -- alternative selection method
            --msg = "> " .. msg
        end

        if not o then rb.set_viewport() return end

        if rb.LCD_DEPTH  == 2 then -- invert 2-bit screens
            o.fg_pattern = 3 - o.fg_pattern
            o.bg_pattern = 3 - o.bg_pattern
        end

        rb.set_viewport(o)

        o = _NIL
    end

    -- sets line explicitly or increments line if line is _NIL
    local function set_line(iLine)
        local o = get_settings(true)

        o.line = iLine or o.line + 1

        if(o.line < 1 or o.line > o.max_line) then
            o.line = 1
        end
    end

    -- clears the set print area
    local function clear()
        local o = get_settings(true)
        _LCD:clear(o.bg_pattern, o.x, o.y, o.x + o.width, o.y + o.height)
        if o.autoupdate == true then rb.lcd_update() end
        set_line(1)
        for i=1, #col_buf do col_buf[i] = _NIL end
        s_lines = {}
        collectgarbage("collect")
    end

    -- screen update after each call to print.f
    local function set_update(bAutoUpdate)
        local o = get_settings(true)
        o.autoupdate = bAutoUpdate or false
    end

    -- sets print area
    local function set_area(x, y, w, h)
        local o = get_settings(true)
        o.x, o.y = clamp(x, 1, rb.LCD_WIDTH), clamp(y, 1, rb.LCD_HEIGHT)
        o.width, o.height = clamp(w, 1, rb.LCD_WIDTH - o.x), clamp(h, 1, rb.LCD_HEIGHT - o.y)
        o.max_line = max_lines(_p_opts)

        clear()
        return o.line, o.max_line
    end

    -- when string is longer than print width scroll -- "auto", "manual", "none"
    local function set_overflow(str_mode)
        -- "auto", "manual", "none"
        local str_mode = str_mode or "auto"
        local o = get_settings(true)
        o.ovfl = str_mode:lower()
        col_buf = {}
    end

    -- aligns text to: "left", "center", "right"
    local function set_justify(str_mode)
        -- "left", "center", "right"
        local str_mode = str_mode or "left"
        local o = get_settings(true)
        o.justify = str_mode:lower()
    end

    -- selects line
    local function select_line(iLine)
        s_lines[iLine] = true
    end

    -- Internal print function
    local function print_internal(t_opts, x, w, h, msg)

        local o = t_opts
        if o.justify == "center" then
            x = x + (o.width - w) / 2
        elseif o.justify == "right" then
            x = x + (o.width - w)
        end

        local line = o.line - 1 -- rb is 0-based lua is 1-based
        if(o.ovfl == "auto" and w >= o.width) then -- -o.x
            rb.lcd_puts_scroll(0, line, msg)
        else
            rb.lcd_putsxy(x, line * h, msg)
            if o.ovfl == "manual" then --save msg for later side scroll
                col_buf_insert(msg, o.line, o)
            end
        end

        --only update the line we changed
        update_line(o.autoupdate, o, line, h)

        set_line(_NIL) -- increments line counter
    end

    -- Helper function that acts mostly like a normal printf() would
    local function printf(fmt, v1, ...)
        local o = get_settings(true)
        local w, h, msg
        local line = o.line - 1 -- rb is 0-based lua is 1-based

        if not (fmt) or (fmt) == "\n" then -- handles blank line / single '\n'
             local res, w, h = text_extent(" ", o.font)

            clear_line(o, line, h)
            update_line(o.autoupdate, o, line, h)

            if (fmt) then set_line(_NIL) end

            return o.line, o.max_line, o.width, h
        end

        msg = string.format(fmt, v1, ...)

        show_selected(o.line, msg)

        w, h, msg = check_escapes(o, msg)

        print_internal(o, o.col, w, h, msg)

        return o.line, o.max_line, w, h
    end

    -- x > 0 scrolls right x < 0 scrolls left
    local function set_column(x)
        local o = get_settings()
        if o.ovfl ~= "manual" then return end -- no buffer stored to scroll
        local res, w, h, str, line

        for key, value in pairs(col_buf) do
            line = key - 1 -- rb is 0-based lua is 1-based
            o.line = key

            if value then
                show_selected(key, value)
                res, w, h = text_extent(value, o.font)
                clear_line(o, line, h)

                print_internal(o, x + o.col, w, h, value)
                update_line(o.autoupdate, o, line, h)
            end
        end
        o = _NIL
    end

    --expose functions to the outside through _print table
    _print.opt            = {}
    _print.opt.column     = set_column
    _print.opt.color      = set_color
    _print.opt.area       = set_area
    _print.opt.set        = set_settings
    _print.opt.get        = get_settings
    _print.opt.defaults   = set_defaults
    _print.opt.overflow   = set_overflow
    _print.opt.justify    = set_justify
    _print.opt.sel_line   = select_line
    _print.opt.line       = set_line
    _print.opt.autoupdate = set_update
    _print.clear = clear
    _print.f     = printf

end --_print functions

return _print