Skip to content

Commit 54c72ae

Browse files
committed
Added example showing dual code using "!!".
1 parent 1760a1b commit 54c72ae

File tree

2 files changed

+92
-0
lines changed

2 files changed

+92
-0
lines changed

examples/dualCode.lua2p

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
--[[============================================================
2+
--=
3+
--= LuaPreprocess example: Dual code.
4+
--=
5+
--= Here we have some constants that are used in both the
6+
--= metaprogram and in the final program.
7+
--=
8+
--============================================================]]
9+
10+
-- Assignments starting with !! will appear in both the metaprogram and the final output.
11+
!!local TEXT_HEIGHT = 30
12+
13+
!!local BUTTON_PADDING = 15
14+
!!local BUTTON_BORDER = 2
15+
16+
!!local BUTTON_WIDTH = 400
17+
!!local BUTTON_HEIGHT = TEXT_HEIGHT + 2*BUTTON_PADDING + 2*BUTTON_BORDER
18+
19+
!(
20+
local function getImageWidth(imagePath)
21+
return 100 -- Dummy value.
22+
end
23+
)
24+
25+
function drawImage(imagePath, x, y, scale) print("Drawing image with scale "..scale) end
26+
function drawBackground(x, y, width, height) print("Drawing background") end
27+
function drawBorder(x, y, width, height) print("Drawing border") end
28+
function drawLabel(x, y, label) print("Drawing label: "..label) end
29+
30+
function drawButton(label, x, y)
31+
!local scale = getImageWidth("button_background.png") / BUTTON_WIDTH
32+
33+
drawImage("button_background.png", x, y, !(scale))
34+
35+
drawBorder(x, y, BUTTON_WIDTH, BUTTON_HEIGHT)
36+
drawLabel(x, y, label)
37+
end
38+
39+
function drawContextMenu(x, y, menuItemLabels)
40+
local menuWidth = BUTTON_WIDTH
41+
local menuHeight = #menuItemLabels * BUTTON_HEIGHT
42+
43+
drawBackground(x, y, menuWidth, menuHeight)
44+
45+
for i, label in ipairs(menuItemLabels) do
46+
drawButton(label, x, y)
47+
y = y+BUTTON_HEIGHT
48+
end
49+
end
50+
51+
drawContextMenu(20, 50, {"Copy","Cut","Paste"})

examples/dualCode.output.lua

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
--[[============================================================
2+
--=
3+
--= LuaPreprocess example: Dual code.
4+
--=
5+
--= Here we have some constants that are used in both the
6+
--= metaprogram and in the final program.
7+
--=
8+
--============================================================]]
9+
10+
-- Assignments starting with !! will appear in both the metaprogram and the final output.
11+
local TEXT_HEIGHT = 30
12+
local BUTTON_PADDING = 15
13+
local BUTTON_BORDER = 2
14+
local BUTTON_WIDTH = 400
15+
local BUTTON_HEIGHT = 64
16+
17+
function drawImage(imagePath, x, y, scale) print("Drawing image with scale "..scale) end
18+
function drawBackground(x, y, width, height) print("Drawing background") end
19+
function drawBorder(x, y, width, height) print("Drawing border") end
20+
function drawLabel(x, y, label) print("Drawing label: "..label) end
21+
22+
function drawButton(label, x, y)
23+
drawImage("button_background.png", x, y, 0.25)
24+
25+
drawBorder(x, y, BUTTON_WIDTH, BUTTON_HEIGHT)
26+
drawLabel(x, y, label)
27+
end
28+
29+
function drawContextMenu(x, y, menuItemLabels)
30+
local menuWidth = BUTTON_WIDTH
31+
local menuHeight = #menuItemLabels * BUTTON_HEIGHT
32+
33+
drawBackground(x, y, menuWidth, menuHeight)
34+
35+
for i, label in ipairs(menuItemLabels) do
36+
drawButton(label, x, y)
37+
y = y+BUTTON_HEIGHT
38+
end
39+
end
40+
41+
drawContextMenu(20, 50, {"Copy","Cut","Paste"})

0 commit comments

Comments
 (0)