From 9bb1c5371affb2ff0b83256470dec7461b404264 Mon Sep 17 00:00:00 2001 From: Alex Auvolat Date: Thu, 4 May 2017 11:48:08 +0200 Subject: Beginning of tk.lua --- src/syslua/lx/tk.lua | 97 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 src/syslua/lx/tk.lua (limited to 'src/syslua/lx/tk.lua') 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 -- cgit v1.2.3