First pass at RPL-based menus

Added an RPL menu object and connected it to the input class

Signed-off-by: Christophe de Dinechin <christophe@dinechin.org>
This commit is contained in:
Christophe de Dinechin 2022-10-31 08:45:36 +01:00
parent 97399d8fbd
commit f789158ea5
12 changed files with 319 additions and 7 deletions

View file

@ -105,6 +105,7 @@ CXX_SOURCES += \
src/arithmetic.cc \
src/functions.cc \
src/variables.cc \
src/menu.cc \
src/list.cc \
src/font.cc \
fonts/HelpFont.cc \

View file

@ -6,7 +6,7 @@ extern const unsigned char EditorFont_sparse_font_data[];
const unsigned char EditorFont_sparse_font_data[66876] =
{
0x44, 0xB8, 0x8A, 0x04, 0x42, 0x00, 0x01, 0x00, 0x33, 0x01, 0x01, 0x00, 0x00, 0x0D, 0x01, 0x00,
0x45, 0xB8, 0x8A, 0x04, 0x42, 0x00, 0x01, 0x00, 0x33, 0x01, 0x01, 0x00, 0x00, 0x0D, 0x01, 0x00,
0x33, 0x01, 0x01, 0x08, 0x00, 0x20, 0x5F, 0x00, 0x33, 0x01, 0x01, 0x08, 0x00, 0x02, 0x13, 0x05,
0x20, 0x09, 0xEF, 0xBD, 0xF7, 0xDE, 0x7B, 0xEF, 0xBD, 0xF7, 0xDE, 0x7B, 0xEF, 0xBD, 0xF7, 0x1E,
0x00, 0xC0, 0xFD, 0xFF, 0xFF, 0x77, 0x03, 0x13, 0x0B, 0x0E, 0x11, 0x8F, 0x7F, 0xFC, 0xE3, 0x1F,

View file

@ -6,7 +6,7 @@ extern const unsigned char HelpFont_sparse_font_data[];
const unsigned char HelpFont_sparse_font_data[14189] =
{
0x44, 0xEA, 0x6E, 0x1A, 0x00, 0x01, 0x00, 0x13, 0x01, 0x01, 0x00, 0x00, 0x0D, 0x01, 0x00, 0x13,
0x45, 0xEA, 0x6E, 0x1A, 0x00, 0x01, 0x00, 0x13, 0x01, 0x01, 0x00, 0x00, 0x0D, 0x01, 0x00, 0x13,
0x01, 0x01, 0x03, 0x00, 0x20, 0x5F, 0x00, 0x13, 0x01, 0x01, 0x03, 0x00, 0x01, 0x06, 0x02, 0x0D,
0x04, 0xFF, 0xFF, 0xF3, 0x03, 0x01, 0x06, 0x04, 0x05, 0x06, 0xFF, 0xFF, 0x0F, 0x01, 0x06, 0x08,
0x0D, 0x0A, 0x12, 0x12, 0x14, 0x7F, 0x7F, 0x24, 0x24, 0x24, 0xFE, 0xFE, 0x28, 0x68, 0x48, 0x00,

View file

@ -6,7 +6,7 @@ extern const unsigned char StackFont_sparse_font_data[];
const unsigned char StackFont_sparse_font_data[32783] =
{
0x44, 0x8B, 0x80, 0x02, 0x2D, 0x00, 0x01, 0x00, 0x22, 0x01, 0x01, 0x00, 0x00, 0x0D, 0x01, 0x00,
0x45, 0x8B, 0x80, 0x02, 0x2D, 0x00, 0x01, 0x00, 0x22, 0x01, 0x01, 0x00, 0x00, 0x0D, 0x01, 0x00,
0x22, 0x01, 0x01, 0x05, 0x00, 0x20, 0x5F, 0x00, 0x22, 0x01, 0x01, 0x05, 0x00, 0x01, 0x0C, 0x03,
0x16, 0x05, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x1F, 0xF8, 0xFF, 0x03, 0x02, 0x0C, 0x07, 0x09, 0x0B,
0xF7, 0xFB, 0xFD, 0x7E, 0xBF, 0xDF, 0xEF, 0x77, 0x01, 0x0C, 0x0E, 0x16, 0x10, 0xC6, 0x81, 0x63,

View file

@ -53,6 +53,7 @@ SOURCES += \
../src/arithmetic.cc \
../src/functions.cc \
../src/variables.cc \
../src/menu.cc \
../src/list.cc \
../fonts/EditorFont.cc \
../fonts/HelpFont.cc \

View file

@ -435,6 +435,20 @@ struct graphics
// Draw a text with a foreground and background
// --------------------------------------------------------------------
template<clipping Clip = CLIP_DST>
coord text(coord x, coord y, utf8 text, size_t len, const font *f,
pattern colors = pattern::black,
blitop op = blitop_mono_fg<Mode>);
// --------------------------------------------------------------------
// Draw a text with the given operation and colors
// --------------------------------------------------------------------
template<clipping Clip = CLIP_DST>
coord text(coord x, coord y, utf8 text, size_t len, const font *f,
pattern fg, pattern bg);
// --------------------------------------------------------------------
// Draw a text with a foreground and background
// --------------------------------------------------------------------
protected:
offset pixel_offset(coord x, coord y) const
// ---------------------------------------------------------------------
@ -1345,4 +1359,53 @@ graphics::coord graphics::surface<Mode>::text(coord x,
return x;
}
template <graphics::mode Mode>
template <graphics::clipping Clip>
graphics::coord graphics::surface<Mode>::text(coord x,
coord y,
utf8 text,
size_t len,
const font *f,
pattern colors,
blitop op)
// ----------------------------------------------------------------------------
// Render a glyph on the given surface
// ----------------------------------------------------------------------------
{
while (len)
{
unicode cp = utf8_codepoint(text);
utf8 next = utf8_next(text);
len -= (next - text);
x = glyph<Clip>(x, y, cp, f, colors, op);
text = next;
}
return x;
}
template <graphics::mode Mode>
template <graphics::clipping Clip>
graphics::coord graphics::surface<Mode>::text(coord x,
coord y,
utf8 text,
size_t len,
const font *f,
pattern fg,
pattern bg)
// ----------------------------------------------------------------------------
// Render a text with a foreground and background
// ----------------------------------------------------------------------------
{
while (len)
{
unicode cp = utf8_codepoint(text);
utf8 next = utf8_next(text);
len -= (next - text);
x = glyph<Clip>(x, y, cp, f, fg, bg);
text = next;
}
return x;
}
#endif // GRAPHICS_H

View file

@ -61,6 +61,7 @@ ID(list)
ID(program)
ID(equation)
ID(array)
ID(menu)
#define FIRST_INTEGER_TYPE ID_hex_integer
ID(hex_integer)

View file

@ -315,7 +315,7 @@ void input::menus(uint count, cstring labels[], object_p function[])
if (m < count)
menu(m, labels[m], function[m]);
else
menu(m, nullptr, nullptr);
menu(m, cstring(nullptr), nullptr);
}
}
@ -337,6 +337,15 @@ void input::menu(uint menu_id, cstring label, object_p fn)
}
void input::menu(uint id, symbol_p label, object_p fn)
// ----------------------------------------------------------------------------
// The drawing of menus recognizes symbols
// ----------------------------------------------------------------------------
{
menu(id, (cstring) label, fn);
}
int input::draw_menus(uint time, uint &period)
// ----------------------------------------------------------------------------
// Draw the softkey menus
@ -395,7 +404,19 @@ int input::draw_menus(uint time, uint &period)
if (label)
{
Screen.clip(mrect);
size tw = font->width(label);
size_t len = 0;
if (*label == object::ID_symbol)
{
// If we are given a symbol, use its length
label++;
len = leb128<size_t>(label);
}
else
{
// Regular C string
len = strlen(cstring(label));
}
size tw = font->width(label, len);
if (tw > mw)
{
dirtyMenu = true;
@ -405,7 +426,7 @@ int input::draw_menus(uint time, uint &period)
{
x = x - tw / 2;
}
Screen.text(x, mrect.y1, label, font, pattern::white);
Screen.text(x, mrect.y1, label, len, font, pattern::white);
Screen.clip(clip);
}
}

View file

@ -77,7 +77,8 @@ struct input
void assign(int key, uint plane, object_p code);
object_p assigned(int key, uint plane);
void menus(uint count, cstring labels[], object_p function[]);
void menu(uint index, cstring labels, object_p function);
void menu(uint index, cstring label, object_p function);
void menu(uint index, symbol_p label, object_p function);
void draw_annunciators();
void draw_editor();

110
src/menu.cc Normal file
View file

@ -0,0 +1,110 @@
// ****************************************************************************
// menu.cc DB48X project
// ****************************************************************************
//
// File Description:
//
// An RPL object describing a soft menu
//
//
//
//
//
//
//
//
// ****************************************************************************
// (C) 2022 Christophe de Dinechin <christophe@dinechin.org>
// This software is licensed under the terms outlined in LICENSE.txt
// ****************************************************************************
// This file is part of DB48X.
//
// DB48X is free software: you can redistribute it and/or modify
// it under the terms outlined in the LICENSE.txt file
//
// DB48X is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// ****************************************************************************
//
// Payload layout:
// Each menu entry is a pair with a symbol and the associated object
// The symbol represents the name for the menu entry
#include "menu.h"
#include "input.h"
#include "parser.h"
#include "renderer.h"
OBJECT_HANDLER_BODY(menu)
// ----------------------------------------------------------------------------
// Handle commands for menus
// ----------------------------------------------------------------------------
{
switch(op)
{
case EVAL:
return obj->evaluate(rt);
case SIZE:
return size(obj, payload);
case PARSE:
return object_parser(OBJECT_PARSER_ARG(), rt);
case RENDER:
return obj->object_renderer(OBJECT_RENDERER_ARG(), rt);
case HELP:
return (intptr_t) "menu";
default:
// Check if anyone else knows how to deal with it
return DELEGATE(list);
}
}
OBJECT_PARSER_BODY(menu)
// ----------------------------------------------------------------------------
// Try to parse this as a menu
// ----------------------------------------------------------------------------
{
return SKIP;
}
OBJECT_RENDERER_BODY(menu)
// ----------------------------------------------------------------------------
// Render the menu into the given menu buffer
// ----------------------------------------------------------------------------
{
return snprintf(r.target, r.length, "Menu (internal)");
}
object::result menu::evaluate(runtime & UNUSED rt) const
// ----------------------------------------------------------------------------
// Evaluate by showing menu entries in the soft menu keys
// ----------------------------------------------------------------------------
{
byte_p p = payload();
size_t size = leb128<size_t>(p);
uint index = 0;
while (size)
{
symbol_p symbol = (symbol_p) p;
size_t ssize = symbol->object::size();
p += ssize;
object_p value = (object_p) p;
size_t osize = value->size();
p += osize;
size -= ssize + osize;
Input.menu(index++, symbol, value);
}
while (index < input::NUM_MENUS)
Input.menu(index++, cstring(nullptr), nullptr);
return OK;
}

113
src/menu.h Normal file
View file

@ -0,0 +1,113 @@
#ifndef MENU_H
#define MENU_H
// ****************************************************************************
// menu.h DB48X project
// ****************************************************************************
//
// File Description:
//
// An RPL menu object defines the content of the soft menu keys
//
// It is a catalog which, when evaluated, updates the soft menu keys
//
//
//
//
//
//
// ****************************************************************************
// (C) 2022 Christophe de Dinechin <christophe@dinechin.org>
// This software is licensed under the terms outlined in LICENSE.txt
// ****************************************************************************
// This file is part of DB48X.
//
// DB48X is free software: you can redistribute it and/or modify
// it under the terms outlined in the LICENSE.txt file
//
// DB48X is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// ****************************************************************************
#include "variables.h"
#include "symbol.h"
struct menu : catalog
// ----------------------------------------------------------------------------
// An RPL menu defines a bunch of commands with associated labels
// ----------------------------------------------------------------------------
{
// Construction of menu objects
menu(id type = ID_menu) : catalog(type) {}
template<typename ...Args>
menu(Args... args, id type = ID_menu) : catalog(type)
{
byte *p = payload();
size_t sz = items_size(args...);
p = leb128(p, sz);
p = items(p, args...);
}
static size_t required_memory(id i) { return catalog::required_memory(i); }
template<typename ...Args>
static size_t required_memory(id i, Args... args)
{
size_t size = items_size(args...);
return leb128size(i) + leb128size(size) + size;
}
public:
result evaluate(runtime &rt = RT) const;
protected:
static size_t items_size() { return 0; }
static size_t items_size(cstring lbl, object_p obj)
{
size_t len = strlen(lbl);
return leb128size(ID_symbol) + leb128size(len) + len + obj->size();
}
static size_t items_size(symbol_p lbl, object_p obj)
{
return lbl->object::size() + obj->size();
}
template<typename Label, typename ...Args>
static size_t items_size(Label lbl, object_p obj, Args... args)
{
return items_size(lbl, obj) + items_size(args...);
}
static byte *items(byte *p, cstring label, object_p obj)
{
size_t len = strlen(label);
p = leb128(p, ID_symbol);
p = leb128(p, len);
memmove(p, label, len);
p += len;
size_t objsize = obj->size();
memmove(p, obj, objsize);
p += objsize;
return p;
}
static byte *items(byte *p, symbol_p label, object_p obj)
{
size_t lblsize = label->object::size();
memmove(p, label, lblsize);
p += lblsize;
size_t objsize = obj->size();
memmove(p, obj, objsize);
p += objsize;
return p;
}
public:
OBJECT_HANDLER(menu);
OBJECT_PARSER(menu);
OBJECT_RENDERER(menu);
};
#endif // MENU_H

View file

@ -38,6 +38,7 @@
#include "functions.h"
#include "integer.h"
#include "list.h"
#include "menu.h"
#include "parser.h"
#include "renderer.h"
#include "runtime.h"