aboutsummaryrefslogtreecommitdiff
path: root/src/syslua/lx/tk.lua
blob: 3771748a2c4d53073490cc86124613e58df78204 (plain) (blame)
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
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

		local step = 20
		local halfstep = 10
		for x = x0 - (x0 % step), x0 + w, step do
			for y = y0 - (y0 % step), y0 + h, step do
				buf:fillrect(x - x0, y - y0, halfstep, halfstep, buf:rgb(150, 150, 150))
				buf:fillrect(x - x0 + halfstep, y - y0 + halfstep, halfstep, halfstep, buf:rgb(150, 150, 150))
				buf:fillrect(x - x0 + halfstep, y - y0, halfstep, halfstep, buf:rgb(170, 170, 170))
				buf:fillrect(x - x0, y - y0 + halfstep, halfstep, halfstep, buf:rgb(170, 170, 170))
			end
		end
		buf:blit(0, 0, self.img:sub(x0, y0, self.img:width(), self.img:height()))
	end

	return image
end


return tk