shithub: lua9

Download patch

ref: 46de8be0003dbc851949c9ae1388db103a6b2396
parent: 554c1ebdd87fb0f506160561d7a88bdf359ebb98
author: telephil9 <telephil9@gmail.com>
date: Mon Oct 26 04:03:20 EDT 2020

Added new demo script and removed previous one

--- /dev/null
+++ b/demo.lua
@@ -1,0 +1,62 @@
+#!/bin/ape/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)
+end
+
+local index = nil
+local menu = { "line", "poly", "exit" }
+local demos = { lines, polys, 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
--- a/sample.lua
+++ /dev/null
@@ -1,38 +1,0 @@
-#!/bin/ape/lua9
-
--- lua libdraw sample
-
-function rect(x0, y0, x1, y1)
-	return { min = { x = x0, y = y0 }, max = { x = x1, y = y1 } }
-end
-
-function pt(x, y)
-	return { x = x, y = y }
-end
-
-local ZP = pt(0, 0)
-local red = nil
-
-function eresized()
-	draw.draw(screen, screen.r, display.white, nil, ZP)
-	draw.draw(screen, rect(50, 150, 100, 200), display.black, nil, ZP)
-	draw.string(screen, pt(110, 160), display.black, nil, font, 'Hello LUA')
-	draw.line(screen, pt(50, 210), pt(150, 210), draw.END_DISC, draw.END_ARROW, 1, display.black, ZP)
-	draw.ellipse(screen, pt(300, 300), 200, 100, 2, display.black, ZP)
-	draw.fillellipse(screen, pt(300, 300), 20, 50, red, ZP)
-end
-
-draw.init('lua sample')
-event.init(event.MOUSE|event.KEYBOARD)
-red = draw.allocimage(display, rect(0,0,1,1), screen.chan, 1, color.RED)
-eresized()
-while true do
-	local e, ev = event.event()
-	if e == event.MOUSE then
-		--print 'Mouse event'
-	elseif e == event.KEYBOARD then
-		if key.eq(ev.kbdc, 'q') or ev.kbdc == key.DEL then
-			os.exit()
-		end
-	end
-end