-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdemo.lua
executable file
·166 lines (153 loc) · 6.94 KB
/
demo.lua
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
#!/bin/lua9
local pt = g.pt
local rect = g.rect
local ZP = g.ZP
function intro()
local p = g.addpt(screen.r.min, pt(10, 10))
draw.draw(screen, screen.r, display.white, nil, ZP)
draw.string(screen, p, display.black, nil, font, "lua9 libdraw demo")
p = g.addpt(p, pt(0, font.height + 2))
draw.string(screen, p, display.black, nil, font, "Select a demo from the right-click menu")
end
function lines()
local p = g.addpt(screen.r.min, pt(10, 10))
draw.draw(screen, screen.r, display.white, nil, ZP)
draw.string(screen, p, display.black, nil, font, "draw.line(screen, p0, p1, draw.END_SQUARE, draw.END_ARROW, 2, display.black, ZP)")
p = g.addpt(p, pt(0, 50))
draw.line(screen, p, g.addpt(p, pt(200, 0)), draw.END_SQUARE, draw.END_ARROW, 2, display.black, ZP)
end
function polys()
local p = g.addpt(screen.r.min, pt(10, 10))
draw.draw(screen, screen.r, display.white, nil, ZP)
draw.string(screen, p, display.black, nil, font, "draw.poly(screen, { p0, p1, p2, p0 }, draw.END_SQUARE, draw.END_SQUARE, 1, display.black, ZP)")
p = g.addpt(p, pt(0, 50))
p1 = g.addpt(p, pt(200, 200))
p2 = g.addpt(p, pt(0, 200))
draw.poly(screen, { p, p1, p2, p }, draw.END_SQUARE, draw.END_SQUARE, 0, display.black, ZP)
p = g.addpt(p, pt(0, 250))
draw.string(screen, p, display.black, nil, font, "draw.fillpoly(screen, { p0, p1, p2, p3, p4, p0 }, 3, display.black, ZP)")
p = g.addpt(p, pt(50, 50))
p1 = g.addpt(p, pt(200, 100))
p2 = g.addpt(p, pt(100, 150))
p3 = g.addpt(p, pt(50, 200))
p4 = g.addpt(p, pt(-50, 50))
draw.fillpoly(screen, { p, p1, p2, p3, p4, p }, 3, display.black, ZP)
end
function beziers()
local p = g.addpt(screen.r.min, pt(10, 10))
draw.draw(screen, screen.r, display.white, nil, ZP)
draw.string(screen, p, display.black, nil, font, "draw.bezier(screen, p0, p1, p2, p3, draw.END_DISC, draw.END_DISC, 1, display.black, ZP)")
p = g.addpt(p, pt(0, 50))
p1 = g.addpt(p, pt(200, 100))
p2 = g.addpt(p, pt(50, 150))
p3 = g.addpt(p, pt(20, 75))
draw.bezier(screen, p, p1, p2, p3, draw.END_DISC, draw.END_DISC, 1, display.black, ZP)
p = g.addpt(p, pt(0, 150))
draw.string(screen, p, display.black, nil, font, "draw.fillbezier(screen, p0, p1, p2, p3, 5, display.black, ZP)")
p = g.addpt(p, pt(0, 50))
p1 = g.addpt(p, pt(100, 50))
p2 = g.addpt(p, pt(50, 150))
p3 = g.addpt(p, pt(200, 75))
draw.fillbezier(screen, p, p1, p2, p3, 5, display.black, ZP)
end
function bezsplines()
local p = g.addpt(screen.r.min, pt(10, 10))
draw.draw(screen, screen.r, display.white, nil, ZP)
draw.string(screen, p, display.black, nil, font, "draw.bezspline(screen, { p0, p1, p2, p0 }, draw.END_SQUARE, draw.END_SQUARE, 3, display.black, ZP)")
p = g.addpt(p, pt(0, 0))
p1 = g.addpt(p, pt(200, 200))
p2 = g.addpt(p, pt(0, 200))
draw.bezspline(screen, { p, p1, p2, p }, draw.END_SQUARE, draw.END_SQUARE, 3, display.black, ZP)
p = g.addpt(p, pt(0, 250))
draw.string(screen, p, display.black, nil, font, "draw.fillbezspline(screen, { p0, p1, p2, p3, p4, p0 }, 3, display.black, ZP)")
p = g.addpt(p, pt(50, 50))
p1 = g.addpt(p, pt(200, 100))
p2 = g.addpt(p, pt(100, 150))
p3 = g.addpt(p, pt(50, 200))
p4 = g.addpt(p, pt(-50, 50))
draw.fillbezspline(screen, { p, p1, p2, p3, p4, p }, 3, display.black, ZP)
end
function ellipses()
local p = g.addpt(screen.r.min, pt(10, 10))
draw.draw(screen, screen.r, display.white, nil, ZP)
draw.string(screen, p, display.black, nil, font, "draw.ellipse(screen, p, 200, 50, 2, display.black, ZP)")
p1 = g.addpt(p, pt(200, 100))
draw.ellipse(screen, p1, 200, 50, 2, display.black, ZP)
p = g.addpt(p, pt(0, 200))
draw.string(screen, p, display.black, nil, font, "draw.fillellipse(screen, p, 50, 50, display.black, ZP)")
p1 = g.addpt(p, pt(50, 100))
draw.fillellipse(screen, p1, 50, 50, display.black, ZP)
end
function arcs()
local p = g.addpt(screen.r.min, pt(10, 10))
draw.draw(screen, screen.r, display.white, nil, ZP)
draw.string(screen, p, display.black, nil, font, "draw.arc(screen, p, 200, 50, 2, display.black, ZP, 30, 260)")
p1 = g.addpt(p, pt(200, 100))
draw.arc(screen, p1, 200, 50, 2, display.black, ZP, 30, 260)
p = g.addpt(p, pt(0, 200))
draw.string(screen, p, display.black, nil, font, "draw.fillarc(screen, p, 50, 50, display.black, ZP, 10, 145)")
p1 = g.addpt(p, pt(50, 100))
draw.fillarc(screen, p1, 50, 50, display.black, ZP, 10, 145)
end
function strings()
local p = g.addpt(screen.r.min, pt(10, 10))
draw.draw(screen, screen.r, display.white, nil, ZP)
draw.string(screen, p, display.black, nil, font, "draw.string(screen, p, display.black, ZP, font, 'Hello LUA')")
p = g.addpt(p, pt(0, font.height + 2))
draw.string(screen, p, display.black, ZP, font, 'Hello LUA')
p = g.addpt(p, pt(0, 2 * font.height + 2))
draw.string(screen, p, display.black, nil, font, "draw.stringn(screen, p, display.black, ZP, font, 'Hello LUA', 5)")
p = g.addpt(p, pt(0, font.height + 2))
draw.stringn(screen, p, display.black, ZP, font, 'Hello LUA', 5)
p = g.addpt(p, pt(0, 2 * font.height + 2))
draw.string(screen, p, display.black, nil, font, "draw.stringbg(screen, p, display.white, ZP, font, 'Hello LUA', display.black, ZP)")
p = g.addpt(p, pt(0, font.height + 2))
draw.stringbg(screen, p, display.white, ZP, font, 'Hello LUA', display.black, ZP)
p = g.addpt(p, pt(0, 2 * font.height + 2))
draw.string(screen, p, display.black, nil, font, "draw.stringnbg(screen, p, display.white, ZP, font, 'Hello LUA', 5, display.black, ZP)")
p = g.addpt(p, pt(0, font.height + 2))
draw.stringnbg(screen, p, display.white, ZP, font, 'Hello LUA', 5, display.black, ZP)
end
function colors()
local p = g.addpt(screen.r.min, pt(10, 10))
draw.draw(screen, screen.r, display.white, nil, ZP)
draw.string(screen, p, display.black, nil, font, "red = draw.allocimage(display, rect(0,0,1,1), screen.chan, 1, color.RED)")
p = g.addpt(p, pt(0, font.height + 2))
draw.string(screen, p, display.black, nil, font, "draw.draw(screen, r, red, nil, ZP)")
local red = draw.allocimage(display, rect(0,0,1,1), screen.chan, 1, color.RED)
p = g.addpt(p, pt(0, font.height + 2))
draw.draw(screen, g.rectaddpt(rect(0,0,50,50), p), red, nil, ZP)
p = g.addpt(p, pt(0, font.height + 2 + 50))
draw.string(screen, p, display.black, nil, font, "draw.allocimagemix(display, color.BLUE, color.YELLOW)")
local c = draw.allocimagemix(display, color.BLUE, color.YELLOW)
p = g.addpt(p, pt(0, font.height + 2))
draw.draw(screen, g.rectaddpt(rect(0,0,50,50), p), c, nil, ZP)
end
local index = nil
local menu = { "line", "poly", "bezier", "bezspline", "ellipse", "arc", "string", "colors", "exit" }
local demos = { lines, polys, beziers, bezsplines, ellipses, arcs, strings, colors, os.exit }
function eresized()
if index then
demos[index]()
else
intro()
end
end
draw.init("lua9 demo")
event.init(event.MOUSE|event.KEYBOARD)
eresized()
while true do
local e, ev = event.event()
if e == event.MOUSE then
if ev.mouse.buttons == 4 then
index = event.menuhit(3, ev.mouse, menu)
if index then
demos[index]()
end
end
elseif e == event.KEYBOARD then
if key.eq(ev.kbdc, 'q') or ev.kbdc == key.DEL then
os.exit()
end
end
end