diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/lib/include/kogata/draw.h | 4 | ||||
-rw-r--r-- | src/lib/libkogata/draw.c | 33 | ||||
-rw-r--r-- | src/sysapp/login/main.lua | 12 | ||||
-rw-r--r-- | src/sysbin/lx/lxdrawlib.c | 6 | ||||
-rw-r--r-- | src/sysbin/terminal/main.c | 6 | ||||
-rw-r--r-- | src/syslua/lx/gui.lua | 6 | ||||
-rw-r--r-- | src/syslua/lx/tk.lua | 97 |
7 files changed, 150 insertions, 14 deletions
diff --git a/src/lib/include/kogata/draw.h b/src/lib/include/kogata/draw.h index 470ab4c..d96f2ee 100644 --- a/src/lib/include/kogata/draw.h +++ b/src/lib/include/kogata/draw.h @@ -63,8 +63,8 @@ font_t *g_load_ttf_font(const char* filename); void g_incref_font(font_t *f); void g_decref_font(font_t *f); -int g_text_width(font_t *f, const char* text); -int g_text_height(font_t *f, const char* text); +int g_text_width(font_t *f, const char* text, int size); +int g_text_height(font_t *f, const char* text, int size); void g_write(fb_t *fb, int x, int y, const char* text, font_t *font, int size, color_t c); diff --git a/src/lib/libkogata/draw.c b/src/lib/libkogata/draw.c index 662b0d5..14f1dcf 100644 --- a/src/lib/libkogata/draw.c +++ b/src/lib/libkogata/draw.c @@ -433,16 +433,43 @@ void g_decref_font(font_t *f) { } } -int g_text_width(font_t *font, const char* text) { +int g_text_width(font_t *font, const char* text, int size) { if (font->type == FONT_ASCII_BITMAP) { return font->ascii_bitmap.cw * strlen(text); + } else if (font->type == FONT_STBTT) { + float scale = stbtt_ScaleForPixelHeight(&font->stbtt.info, size); + float xpos = 1; + while (*text != 0) { + int codepoint = *text; // TODO utf8 + text++; + + int advance, lsb; + stbtt_GetCodepointHMetrics(&font->stbtt.info, codepoint, &advance, &lsb); + xpos += scale * advance; + if (*(text+1)) + xpos += scale * stbtt_GetCodepointKernAdvance(&font->stbtt.info, *text, *(text+1)); + } + return ceil(xpos); + } return 0; } -int g_text_height(font_t *font, const char* text) { +int g_text_height(font_t *font, const char* text, int size) { if (font->type == FONT_ASCII_BITMAP) { return font->ascii_bitmap.ch; + } else if (font->type == FONT_STBTT) { + float scale = stbtt_ScaleForPixelHeight(&font->stbtt.info, size); + int max_h = 0; + while (*text != 0) { + int codepoint = *text; // TODO utf8 + text++; + + int x0, y0, x1, y1; + stbtt_GetCodepointBitmapBox(&font->stbtt.info, codepoint, scale, scale, &x0, &y0, &x1, &y1); + if (y1 - y0 > max_h) max_h = y1 - y0; + } + return max_h; } return 0; } @@ -501,7 +528,7 @@ void g_write(fb_t *fb, int x, int y, const char* text, font_t *font, int size, c int sx = (fb->geom.memory_model == FB_MM_RGB32 ? 4 : 3); for (int i = 0; i < h; i++) { - int yy = y + y0 + i; + int yy = y + size + y0 + i; if (yy < 0) continue; if (yy >= fb->geom.height) continue; uint8_t *line = fb->data + yy * fb->geom.pitch; diff --git a/src/sysapp/login/main.lua b/src/sysapp/login/main.lua index de258b9..6be064d 100644 --- a/src/sysapp/login/main.lua +++ b/src/sysapp/login/main.lua @@ -6,6 +6,8 @@ local draw = require 'lx.draw' local gui = require 'lx.gui' local mainloop = require 'lx.mainloop' +local tk = require 'lx.tk' + print("Hello, world!") gui.open() @@ -17,8 +19,13 @@ for x = 0, 255, 16 do end end +local f = draw.load_image('root:/logo.png') +local img = tk.image_widget(f) +tk.init(gui, img) + gui.show_cursor() +--[[ local fnt = draw.load_ttf_font('sys:/fonts/vera.ttf') if fnt == nil then print("Could not load vera.ttf!") @@ -32,7 +39,7 @@ gui.on_mouse_down = function (lb, rb, mb) gui.hide_cursor() gui.surface:write(gui.mouse_x, gui.mouse_y, string.format("Click at %d, %d", gui.mouse_x, gui.mouse_y), fnt, 16, gui.surface:rgb(0, 0, 255)) txt_x = gui.mouse_x - txt_y = gui.mouse_y + fnt:text_height(" ") + txt_y = gui.mouse_y + 16 gui.show_cursor() end end @@ -40,9 +47,10 @@ end gui.on_text_input = function(chr) gui.hide_cursor() gui.surface:write(txt_x, txt_y, chr, fnt, 16, gui.surface:rgb(0, 0, 255)) - txt_x = txt_x + fnt:text_width(chr) + txt_x = txt_x + fnt:text_width(chr, 16) gui.show_cursor() end +--]] mainloop.run() diff --git a/src/sysbin/lx/lxdrawlib.c b/src/sysbin/lx/lxdrawlib.c index 22c6a3b..1a24fcb 100644 --- a/src/sysbin/lx/lxdrawlib.c +++ b/src/sysbin/lx/lxdrawlib.c @@ -90,16 +90,18 @@ static int font_gc(lua_State *L) { static int font_text_width(lua_State *L) { drawlib_font *f = (drawlib_font*)luaL_checkudata(L, 1, FONT); const char* txt = luaL_checkstring(L, 2); + int size = luaL_checkinteger(L, 3); - lua_pushinteger(L, g_text_width(f->font, txt)); + lua_pushinteger(L, g_text_width(f->font, txt, size)); return 1; } static int font_text_height(lua_State *L) { drawlib_font *f = (drawlib_font*)luaL_checkudata(L, 1, FONT); const char* txt = luaL_checkstring(L, 2); + int size = luaL_checkinteger(L, 3); - lua_pushinteger(L, g_text_height(f->font, txt)); + lua_pushinteger(L, g_text_height(f->font, txt, size)); return 1; } diff --git a/src/sysbin/terminal/main.c b/src/sysbin/terminal/main.c index 4805f9d..77bf141 100644 --- a/src/sysbin/terminal/main.c +++ b/src/sysbin/terminal/main.c @@ -101,10 +101,10 @@ int main(int argc, char **argv) { term.kb = init_keyboard(); ASSERT(term.kb != 0); - term.font = g_load_ascii_bitmap_font("sys:/fonts/default.bf"); + term.font = g_load_ascii_bitmap_font("sys:/fonts/pcvga.bf"); ASSERT(term.font != 0); - term.cw = g_text_width(term.font, "#"); - term.ch = g_text_height(term.font, "#"); + term.cw = g_text_width(term.font, "#", 8); + term.ch = g_text_height(term.font, "#", 8); gip_handler_t *h = new_gip_handler(&term_gip_cb, &term); ASSERT(h != 0); diff --git a/src/syslua/lx/gui.lua b/src/syslua/lx/gui.lua index 23e6cb6..35e73b8 100644 --- a/src/syslua/lx/gui.lua +++ b/src/syslua/lx/gui.lua @@ -121,6 +121,8 @@ end function gui.on_mouse(dx, dy, dw, lb, rb, mb) if dx ~= 0 or dy ~= 0 then + local prev_x, prev_y = gui.mouse_x, gui.mouse_y + local csr = gui.cursor_visible if csr then gui.hide_cursor() end @@ -133,7 +135,7 @@ function gui.on_mouse(dx, dy, dw, lb, rb, mb) if csr then gui.show_cursor() end - gui.on_mouse_move(gui.mouse_x, gui.mouse_y) + gui.on_mouse_move(prev_x, prev_y, gui.mouse_x, gui.mouse_y) end if lb == 1 and not gui.mouse_lbtn then @@ -153,7 +155,7 @@ function gui.on_mouse(dx, dy, dw, lb, rb, mb) end end -function gui.on_mouse_move(x, y) +function gui.on_mouse_move(prev_x, prev_y, x, y) -- Nothing, can be replaced :) end diff --git a/src/syslua/lx/tk.lua b/src/syslua/lx/tk.lua new file mode 100644 index 0000000..0b0c69c --- /dev/null +++ b/src/syslua/lx/tk.lua @@ -0,0 +1,97 @@ +local draw = require 'lx.draw' +local sys = require 'lx.sys' + +local tk = {} + +function tk.widget(width, height) + local w = { + width = width, + height = height, + } + + function w:resize(width, height) + w.width = width + w.height = height + end + + function w:get_draw_buffer(x0, y0, w, h) + -- Replaced by parent by a function that returns the buffer for + return nil + end + + function w:redraw(x0, y0, w, h) + -- Replaced by widget code by a function that does the actual drawing + end + + function w:on_mouse_down(lb, rb, mb) + -- Handler for mouse down event + end + + function w:on_mouse_up(lb, rb, mb) + -- Handler for mouse up event + end + + function w:on_mouse_move(prev_x, prev_y, new_x, new_y) + -- Handler for mouse move event + end + + function w:on_text_input(char) + -- Handler for text input + end + + function w:on_key_down(key) + -- Handler for key press + end + + function w:on_key_up(key) + -- Handler for key release + end + + return w +end + +function tk.init(gui, root_widget) + tk.fonts = { + default = draw.load_ttf_font("sys:/fonts/vera.ttf") + } + + root_widget:resize(gui.surface:width(), gui.surface:height()) + + gui.on_key_down = function(key) root_widget:on_key_down(key) end + gui.on_key_up = function(key) root_widget:on_key_up(key) end + gui.on_text_input = function(key) root_widget:on_text_input(char) end + gui.on_mouse_move = function(ox, oy, nx, ny) root_widget:on_mouse_move(ox, oy, nx, ny) end + gui.on_mouse_down = function(lb, rb, mb) root_widget:on_mouse_down(lb, rb, mb) end + gui.on_mouse_up = function(lb, rb, mb) root_widget:on_mouse_up(lb, rb, mb) end + + function root_widget:get_draw_buffer(x0, y0, w, h) + return gui.surface:sub(x0, y0, w, h) + end + + root_widget:redraw(0, 0, root_widget.width, root_widget.height) +end + +function tk.image_widget(img) + local image = tk.widget(img:width(), img:height()) + image.img = img + + function image:redraw(x0, y0, w, h) + local buf = self:get_draw_buffer(x0, y0, w, h) + if buf == nil then return end + + for x = x0 - (x0 % 32), x0 + w, 32 do + for y = y0 - (y0 % 32), y0 + h, 32 do + buf:fillrect(x - x0, y - y0, 16, 16, buf:rgb(150, 150, 150)) + buf:fillrect(x - x0 + 16, y - y0 + 16, 16, 16, buf:rgb(150, 150, 150)) + buf:fillrect(x - x0 + 16, y - y0, 16, 16, buf:rgb(200, 200, 200)) + buf:fillrect(x - x0, y - y0 + 16, 16, 16, buf:rgb(200, 200, 200)) + end + end + buf:blit(0, 0, self.img:sub(x0, y0, self.img:width(), self.img:height())) + end + + return image +end + + +return tk |