Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

173
LINES

< > BotCompany Repo | #172 // Editor 2

Rendition of first frame

Lua code - GUI

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}

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

Comments [hide]

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

add comment

Image recognition results

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: 1164 / 383
Referenced in: [show references]