1 | bgColor = -1 -- white |
2 | fontW, fontH = 16, 16 |
3 | displayCols, displayRows = 40, 10 |
4 | displayW, displayH = fontW*displayCols, fontH*displayRows |
5 | width, height = displayW, displayH |
6 | cursorBlinkRate = 500 |
7 | --eventRate = 50 |
8 | eventRate = 20 -- high speed! |
9 | eventsPerCursorBlink = cursorBlinkRate/eventRate |
10 | needsRepaint = false |
11 | repaintTriggeringVars = {"curX", "curY", "cursorVisible", "scrollX", "scrollY"} |
12 | fatCursor = true |
13 | |
14 | -- volatile data |
15 | if not inited then |
16 | scrollX, scrollY = 0, 0 |
17 | curX, curY = 0, 0 |
18 | lines = {"hello", "world"} |
19 | needsRepaint = true |
20 | inited = true |
21 | end |
22 | |
23 | local min, max = math.min, math.max |
24 | |
25 | get("#109") -- copyRect |
26 | |
27 | fontRendererEnv = newCleanEnv() |
28 | fontRenderer = get("#178", fontRendererEnv) |
29 | |
30 | grab("#175") -- tableToString |
31 | |
32 | function renderText(x, y, s) |
33 | --print("renderText", x, y, s) |
34 | fontRendererEnv.render(s) |
35 | --print("fontWidth="..tostring(fontRendererEnv.fontWidth)) |
36 | copyRect(fontRendererEnv, _G, 0, 0, fontRendererEnv.width, fontRendererEnv.height, x, y) |
37 | --[[for y=0, 1 do |
38 | for x=0, 15 do |
39 | print(fontRendererEnv.pixels[x+y*fontRendererEnv.width+1]) |
40 | end |
41 | end]] |
42 | end |
43 | |
44 | function setPixel(x, y, col) |
45 | if x >= 0 and y >= 0 and x < displayW and y < displayH then |
46 | pixels[x+y*width+1] = col |
47 | end |
48 | end |
49 | |
50 | function invertPixel(x, y) |
51 | if x >= 0 and y >= 0 and x < displayW and y < displayH then |
52 | local ofs = x+y*width+1 |
53 | pixels[ofs] = pixels[ofs] and -1-pixels[ofs] or -1 |
54 | end |
55 | end |
56 | |
57 | function stepCursor() |
58 | if lastCurX ~= curX or lastCurY ~= curY then |
59 | cursorCount = -1 |
60 | end |
61 | lastCurX = curX |
62 | lastCurY = curY |
63 | cursorCount = (cursorCount+1) % (eventsPerCursorBlink*2) |
64 | cursorVisible = cursorCount < eventsPerCursorBlink |
65 | end |
66 | |
67 | function renderCursor() |
68 | if cursorVisible then |
69 | for x=0, fatCursor and fontW-1 or 0 do |
70 | for y=0, fontH-1 do |
71 | invertPixel((curX-scrollX)*fontW+x, (curY-scrollY)*fontH+y) |
72 | end |
73 | end |
74 | end |
75 | end |
76 | |
77 | function cursorLeft() |
78 | if curX > 0 then |
79 | curX=curX-1 |
80 | elseif curY > 0 then |
81 | curY=curY-1 |
82 | end |
83 | end |
84 | |
85 | function cursorRight() |
86 | curX=curX+1 |
87 | end |
88 | |
89 | function cursorUp() |
90 | if curY > 0 then |
91 | curY=curY-1 |
92 | end |
93 | end |
94 | |
95 | function cursorDown() |
96 | if curY < #lines-1 then |
97 | curY=curY+1 |
98 | end |
99 | end |
100 | |
101 | function saveVars() |
102 | savedVars = {} |
103 | for _, var in ipairs(repaintTriggeringVars) do |
104 | savedVars[var] = _G[var] |
105 | end |
106 | end |
107 | |
108 | function compareVars() |
109 | for _, var in ipairs(repaintTriggeringVars) do |
110 | if savedVars[var] ~= _G[var] then |
111 | needsRepaint = true |
112 | return |
113 | end |
114 | end |
115 | end |
116 | |
117 | function moveCursorToSpot(x, y) |
118 | local col = math.floor((x+scrollX)/fontW) |
119 | local row = math.floor((y+scrollY)/fontH) |
120 | row = max(0, min(row, #lines-1)) |
121 | col = max(0, col) |
122 | curX, curY = col, row |
123 | end |
124 | |
125 | function processEvents() |
126 | saveVars() |
127 | |
128 | if inputEvents then |
129 | for _, e in ipairs(inputEvents) do |
130 | --print("event:", tableToString(e)) |
131 | if e[1] == 'keyPressed' then |
132 | if e.keyCode == 39 then |
133 | cursorRight() |
134 | elseif e.keyCode == 37 then |
135 | cursorLeft() |
136 | elseif e.keyCode == 40 then |
137 | cursorDown() |
138 | elseif e.keyCode == 38 then |
139 | cursorUp() |
140 | end |
141 | elseif e[1] == 'mousePressed' then |
142 | moveCursorToSpot(e.x, e.y) |
143 | end |
144 | end |
145 | end |
146 | |
147 | stepCursor() |
148 | |
149 | compareVars() |
150 | end |
151 | |
152 | function render() |
153 | if needsRepaint then |
154 | pixels = {} |
155 | for i = 1, width*height do pixels[i] = bgColor end |
156 | |
157 | for l = 1, min(#lines-scrollY, displayRows) do |
158 | renderText(0, (l-1)*fontH, lines[l+scrollY]:sub(scrollX+1, scrollX+1+displayCols)) |
159 | end |
160 | |
161 | renderCursor() |
162 | needsRepaint = false |
163 | end |
164 | end |
165 | |
166 | function step() |
167 | pixels = nil |
168 | processEvents() |
169 | render() |
170 | end |
171 | |
172 | step() |
173 | animateAgainIn = {eventRate, step} |
show applet test run test run with input download show line numbers
Travelled to 12 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt
ID | Author/Program | Comment | Date |
---|---|---|---|
287 | #1000604 (pitcher) | 2015-08-18 00:54:22 | |
242 | #1000610 (pitcher) | Edit suggestion: !636 !629 main { static Object androidContext; static String programID; public static void main(String[] args) throws Exception { bgColor = -1 -- white fontW, fontH = 16, 16 displayCols, displayRows = 40, 10 displayW, displayH = fontW*displayCols, fontH*displayRows width, height = displayW, displayH cursorBlinkRate = 500 --eventRate = 50 eventRate = 20 -- high speed! eventsPerCursorBlink = cursorBlinkRate/eventRate needsRepaint = false repaintTriggeringVars = {"curX", "curY", "cursorVisible", "scrollX", "scrollY"} fatCursor = true -- volatile data if not inited then scrollX, scrollY = 0, 0 curX, curY = 0, 0 lines = {"hello", "world"} needsRepaint = true inited = true end local min, max = math.min, math.max get("#109") -- copyRect fontRendererEnv = newCleanEnv() fontRenderer = get("#178", fontRendererEnv) grab("#175") -- tableToString function renderText(x, y, s) --print("renderText", x, y, s) fontRendererEnv.render(s) --print("fontWidth="..tostring(fontRendererEnv.fontWidth)) copyRect(fontRendererEnv, _G, 0, 0, fontRendererEnv.width, fontRendererEnv.height, x, y) --[[for y=0, 1 do for x=0, 15 do print(fontRendererEnv.pixels[x+y*fontRendererEnv.width+1]) end end]] end function setPixel(x, y, col) if x >= 0 and y >= 0 and x < displayW and y < displayH then pixels[x+y*width+1] = col end end function invertPixel(x, y) if x >= 0 and y >= 0 and x < displayW and y < displayH then local ofs = x+y*width+1 pixels[ofs] = pixels[ofs] and -1-pixels[ofs] or -1 end end function stepCursor() if lastCurX ~= curX or lastCurY ~= curY then cursorCount = -1 end lastCurX = curX lastCurY = curY cursorCount = (cursorCount+1) % (eventsPerCursorBlink*2) cursorVisible = cursorCount < eventsPerCursorBlink end function renderCursor() if cursorVisible then for x=0, fatCursor and fontW-1 or 0 do for y=0, fontH-1 do invertPixel((curX-scrollX)*fontW+x, (curY-scrollY)*fontH+y) end end end end function cursorLeft() if curX > 0 then curX=curX-1 elseif curY > 0 then curY=curY-1 end end function cursorRight() curX=curX+1 end function cursorUp() if curY > 0 then curY=curY-1 end end function cursorDown() if curY < #lines-1 then curY=curY+1 end end function saveVars() savedVars = {} for _, var in ipairs(repaintTriggeringVars) do savedVars[var] = _G[var] end end function compareVars() for _, var in ipairs(repaintTriggeringVars) do if savedVars[var] ~= _G[var] then needsRepaint = true return end end end function moveCursorToSpot(x, y) local col = math.floor((x+scrollX)/fontW) local row = math.floor((y+scrollY)/fontH) row = max(0, min(row, #lines-1)) col = max(0, col) curX, curY = col, row end function processEvents() saveVars() if inputEvents then for _, e in ipairs(inputEvents) do --print("event:", tableToString(e)) if e[1] == 'keyPressed' then if e.keyCode == 39 then cursorRight() elseif e.keyCode == 37 then cursorLeft() elseif e.keyCode == 40 then cursorDown() elseif e.keyCode == 38 then cursorUp() end elseif e[1] == 'mousePressed' then moveCursorToSpot(e.x, e.y) end end end stepCursor() compareVars() end function render() if needsRepaint then pixels = {} for i = 1, width*height do pixels[i] = bgColor end for l = 1, min(#lines-scrollY, displayRows) do renderText(0, (l-1)*fontH, lines[l+scrollY]:sub(scrollX+1, scrollX+1+displayCols)) end renderCursor() needsRepaint = false end end function step() pixels = nil processEvents() render() end step() animateAgainIn = {eventRate, step} }} | 2015-08-18 00:52:37 |
Recognizer | Recognition Result | Visualize | Recalc |
---|---|---|---|
#308 | 3828 | [visualize] |
Snippet ID: | #172 |
Snippet name: | Editor 2 |
Eternal ID of this version: | #172/1 |
Text MD5: | 293d830650473b54094ebbc3f8b9d4e8 |
Author: | stefan |
Category: | editors |
Type: | Lua code - GUI |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2014-01-16 06:39:05 |
Source code size: | 3828 bytes / 173 lines |
Pitched / IR pitched: | No / Yes |
Views / Downloads: | 1320 / 400 |
Referenced in: | [show references] |