|
| 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"}) |
0 commit comments