Advertisement
Guest User

Codea in Codea with built in examples

a guest
Mar 23rd, 2012
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.23 KB | None | 0 0
  1.  
  2. --# Buffer
  3. Buffer = class()
  4.  
  5. function Buffer:init()
  6.     self.buffer = {}
  7.    
  8.     self.font = "Inconsolata"
  9.     self.fontSize = 20
  10.    
  11.     -- x = line, y = pos
  12.     self.cursor = vec2(1,1)
  13.    
  14.     self.t = 0
  15.     self.cursorBlink = 0
  16. end
  17.  
  18. function Buffer:setStyle()
  19.     textMode(CORNER)
  20.     font( self.font )
  21.     fill(255)
  22.     fontSize( self.fontSize )
  23.     textWrapWidth(10000)
  24. end
  25.  
  26. function Buffer:cursorToScreen(c)
  27.     -- Get a subset of the buffer
  28.     local lines = table_slice( self.buffer, 1, c.x )
  29.     local l = self.buffer[c.x]
  30.    
  31.     local upToStr = self:bufferToString(lines)
  32.     local lstr = nil
  33.    
  34.     if l then
  35.         lstr = table.concat(l)
  36.     end
  37.    
  38.     pushStyle()
  39.    
  40.     self:setStyle()
  41.    
  42.     local lw = 0
  43.     local _,lh = textSize("A")
  44.     local emptyLine = true
  45.    
  46.     if lstr and lstr ~= "" then
  47.         lw,lh = textSize(string.sub(lstr,1,c.y - 1))
  48.         emptyLine = false
  49.     end
  50.    
  51.     local pw,ph = textSize(upToStr)
  52.    
  53.     if emptyLine then
  54.         ph = ph + lh
  55.     end
  56.    
  57.     popStyle()
  58.    
  59.     return lw,(ph - lh/2)
  60. end
  61.  
  62. function Buffer:bufferToString(b)
  63.     local bstrings = {}
  64.     for k,v in pairs(b) do
  65.         table.insert(bstrings, table.concat( b[k] ) )
  66.     end
  67.    
  68.     return table.concat( bstrings, "\n" )
  69. end
  70.  
  71. function Buffer:moveCursor(o)
  72.     self.cursor = self.cursor + o
  73.    
  74.     self.cursor.x = math.max(1, math.min(self.cursor.x, #self.buffer))
  75.    
  76.     local l = self.buffer[self.cursor.x]
  77.     local y = self.cursor.y
  78.    
  79.     y = math.max(1, math.min(y, #l + 1))
  80.     self.cursor.y = y
  81. end
  82.  
  83. function Buffer:insertCharacter(c)
  84.     local l = self.buffer[self.cursor.x]
  85.    
  86.     if l == nil then
  87.         l = {}
  88.         self.buffer[self.cursor.x] = l
  89.     end
  90.  
  91.     if c == "\n" then
  92.         local start = table_slice(l,1,self.cursor.y)
  93.         local tail = table_slice(l,self.cursor.y+1,#l)
  94.        
  95.         table[self.cursor.x] = start
  96.         table.insert(self.buffer, self.cursor.x+1, tail)
  97.        
  98.         self.cursor = vec2( self.cursor.x + 1, 1 )
  99.     elseif c == BACKSPACE then
  100.         if self.cursor.y == 1 then
  101.             -- delete line
  102.             local prevLine = self.buffer[self.cursor.x - 1]
  103.            
  104.             table.remove(self.buffer, self.cursor.x)
  105.            
  106.             if prevLine then
  107.                 self.cursor = vec2(self.cursor.x - 1, #prevLine + 1)
  108.                 table_append( prevLine, l )
  109.             end
  110.         else
  111.             -- delete character
  112.             table.remove(l, self.cursor.y - 1 )
  113.             self.cursor = vec2(self.cursor.x, self.cursor.y - 1 )
  114.         end
  115.     else
  116.         table.insert(l, self.cursor.y, c)
  117.    
  118.         self.cursor = vec2( self.cursor.x, self.cursor.y + 1 )
  119.     end
  120. end
  121.  
  122. function Buffer:draw()
  123.     self.t = self.t + 8 * DeltaTime
  124.     self.cursorBlink = (math.sin(self.t) + 1) * 128
  125.    
  126.     pushStyle()
  127.    
  128.     self:setStyle()
  129.    
  130.     local str = self:toString()
  131.     local w,h = textSize(str)
  132.    
  133.     pushMatrix()
  134.     translate( 40, -40 )
  135.    
  136.     text(str, 0, HEIGHT - h)
  137.    
  138.     -- Draw cursor
  139.     -- Cursor pos x,y
  140.     local cpx,cpy = self:cursorToScreen(self.cursor)
  141.     fill(0, 87, 255, self.cursorBlink)
  142.     rectMode(CENTER)
  143.     rect(cpx,HEIGHT - cpy,5,22)
  144.    
  145.     popMatrix()
  146.     popStyle()
  147. end
  148.  
  149. function Buffer:clear()
  150.     self.cursor = vec2(1,1)
  151.     self.buffer = {}
  152. end
  153.  
  154. function Buffer:load(code)  
  155.     self:clear()
  156.     local i
  157.     for i = 1,string.len(code) do
  158.         self:insertCharacter(string.sub(code,i,i))
  159.     end
  160. end
  161.  
  162. function Buffer:toString()
  163.     return self:bufferToString(self.buffer)
  164. end
  165.  
  166. function Buffer:toStringWithoutActiveLine()
  167.     local bstrings = {}
  168.     for k,v in pairs(self.buffer) do
  169.         if k ~= self.cursor.x then
  170.             table.insert(bstrings, table.concat( self.buffer[k] ) )
  171.         end
  172.     end
  173.    
  174.     return table.concat( bstrings, "\n" )
  175. end
  176.  
  177. --# Embutton
  178. -- Emoji Button
  179. EmButton = class()
  180.  
  181. function EmButton:init(pos,txt)
  182.     -- you can accept and set parameters here
  183.     self.pos = pos
  184.     self.text = txt
  185.     self.action = nil
  186.     self.highlight = false
  187.     self.color = color(255,255,255,255)
  188. end
  189.  
  190. function EmButton:size()
  191.     pushStyle()
  192.     self:setStyle()
  193.  
  194.     local w,h = textSize(self.text)
  195.  
  196.     popStyle()
  197.  
  198.     return w,h
  199. end
  200.  
  201. function EmButton:hitTest(lp)
  202.     local w,h = self:size()
  203.  
  204.     local left,right = self.pos.x - w/2, self.pos.x + w/2
  205.     local top,bottom = self.pos.y + h/2, self.pos.y - h/2
  206.  
  207.     if lp.x > left and lp.x < right and
  208.        lp.y > bottom and lp.y < top then
  209.         return true
  210.     end
  211.  
  212.     return false
  213. end
  214.  
  215. function EmButton:setStyle()
  216.     fill(self.color)
  217.     noStroke()
  218.     textMode(CENTER)
  219.     fontSize(50)
  220.     font("AppleColorEmoji")
  221. end
  222.  
  223. function EmButton:draw()
  224.     pushMatrix()
  225.  
  226.     translate(self.pos.x,self.pos.y)
  227.  
  228.     pushStyle()
  229.     self:setStyle()
  230.  
  231.     text(self.text,0,0)
  232.  
  233.     popStyle()
  234.  
  235.     popMatrix()
  236. end
  237.  
  238. function EmButton:touched(touch)
  239.     if touch.state == ENDED then
  240.         -- Tapped
  241.         if self:hitTest( vec2(touch.x,touch.y) ) then
  242.             if self.action then self.action() end
  243.         end
  244.     end
  245. end
  246. --# Main
  247. supportedOrientations(PORTRAIT)
  248. function keyHeight()    
  249.     if CurrentOrientation == LANDSCAPE_LEFT or
  250.        CurrentOrientation == LANDSCAPE_RIGHT then
  251.         return 350
  252.     else
  253.         return 260
  254.     end
  255. end
  256.  
  257. function layoutAndDrawKeys()
  258.     local start = WIDTH
  259.     local keySz = 30
  260.     local height = keyHeight()
  261.     for i = #keys,1,-1 do
  262.         local k = keys[i]
  263.        
  264.         k.pos = vec2( start - keySz, height + keySz )
  265.        
  266.         start = start - 2*keySz
  267.        
  268.         k:draw()
  269.     end
  270.    
  271.     trash.pos = vec2(WIDTH - keySz - 10, HEIGHT - keySz - 20)
  272.     trash:draw()
  273. end
  274.  
  275. function setup()
  276.     showKeyboard()
  277.    
  278.     displayMode(FULLSCREEN_NO_BUTTONS)
  279.  
  280.     buffer = Buffer()
  281.    
  282.     s = Samples()
  283.  
  284.     local closeKey = EmButton(vec2(0,0), "◀")
  285.     closeKey.color = color(255,100,50,255)
  286.     closeKey.action = function() close() end
  287.            
  288.     local startKey = EmButton(vec2(0,0), "⏪")
  289.     startKey.action = function() buffer:moveCursor(vec2(0,-1000)) end
  290.    
  291.     local endKey = EmButton(vec2(0,0), "⏩")
  292.     endKey.action = function() buffer:moveCursor(vec2(0,1000)) end
  293.    
  294.     local leftKey = EmButton(vec2(0,0), "⬅")
  295.     leftKey.action = function() buffer:moveCursor(vec2(0,-1)) end
  296.    
  297.     local rightKey = EmButton(vec2(0,0), "➡")
  298.     rightKey.action = function() buffer:moveCursor(vec2(0,1)) end
  299.  
  300.     local upKey = EmButton(vec2(0,0), "⬆")
  301.     upKey.action = function() buffer:moveCursor(vec2(-1,0)) end
  302.    
  303.     local downKey = EmButton(vec2(0,0), "⬇")
  304.     downKey.action = function() buffer:moveCursor(vec2(1,0)) end
  305.    
  306.     local keyKey = EmButton(vec2(0,0), "📝")
  307.     keyKey.action = function() showKeyboard() end
  308.    
  309.     local prevKey = EmButton(vec2(0,0), "◀")
  310.     prevKey.color = color(50,255,100,255)
  311.     prevKey.action = function() buffer:load(s:prev()) end
  312.    
  313.     local nextKey = EmButton(vec2(0,0), "▶")
  314.     nextKey.color = color(50,255,100,255)
  315.     nextKey.action = function() buffer:load(s:next()) end  
  316.    
  317.     keys = {closeKey,startKey,leftKey,
  318.             rightKey,endKey,upKey,
  319.             downKey,keyKey,prevKey,nextKey}
  320.    
  321.     trash = EmButton(vec2(0,0), "❌")
  322.     trash.action = function() buffer:clear() end
  323.    
  324.    
  325. end
  326.  
  327. function keyboard(key)
  328.     buffer:insertCharacter(key)
  329. end
  330.  
  331. function touched(touch)
  332.     for k,v in pairs(keys) do
  333.         v:touched(touch)
  334.     end
  335.    
  336.     trash:touched(touch)
  337. end
  338.  
  339. -- This function gets called once every frame
  340. function draw()
  341.     -- This sets a dark background color
  342.     background(40, 40, 50)
  343.    
  344.     resetStyle()
  345.    
  346.     local str = buffer:toString()
  347.    
  348.     local chunk = loadstring(str)
  349.    
  350.     if chunk then
  351.         chunk()
  352.     else
  353.         str = buffer:toStringWithoutActiveLine()
  354.         chunk = loadstring(str)
  355.         if chunk then chunk() end
  356.     end
  357.    
  358.     resetStyle()
  359.    
  360.     fill(0,0,0,50)
  361.     rectMode(CORNER)
  362.     rect(0,0,WIDTH,HEIGHT)
  363.  
  364.     -- Do your drawing here
  365.     buffer:draw()
  366.    
  367.     layoutAndDrawKeys()
  368. end
  369. --# Samples
  370. Samples = class()
  371.  
  372. function Samples:init()
  373.     self.ex = {}
  374.     self.i = 0
  375.     local nl = "\n"
  376.     self.ex[1] = "text(\"Hello World\",300,500)"
  377.     self.ex[2] = "fill(255,0,0)" .. nl
  378.     self.ex[2] = self.ex[2] .. "ellipse(300,500,100,100)"
  379.     self.max = 2
  380. end
  381.  
  382. function Samples:next()
  383.     if self.i ~= self.max then self.i = self.i + 1 end
  384.     return self.ex[self.i]
  385. end
  386.  
  387. function Samples:prev()
  388.     if self.i ~= 1 then self.i = self.i - 1 end
  389.     return self.ex[self.i]
  390. end
  391. --# Util
  392. function table_append (t1, t2)
  393.     local t1s = #t1
  394.     for k,v in pairs(t2) do t1[k + t1s] = v end
  395. end
  396.  
  397. function table_slice (values,i1,i2)
  398.     local res = {}
  399.     local n = #values
  400.  
  401.     -- default values for range
  402.     i1 = i1 or 1
  403.     i2 = i2 or n
  404.     if i2 < 0 then
  405.         i2 = n + i2 + 1
  406.     elseif i2 > n then
  407.         i2 = n
  408.     end
  409.  
  410.     if i1 < 1 or i1 > n then
  411.         return {}
  412.     end
  413.  
  414.     local k = 1
  415.  
  416.     for i = i1,i2 do
  417.         res[k] = values[i]
  418.         k = k + 1
  419.     end
  420.  
  421.     return res
  422. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement