Advertisement
incinirate

UITextbox

Feb 5th, 2016
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.45 KB | None | 0 0
  1. _G.UIElement.UITextbox = {}
  2. local pointer = _G.UIElement.UITextbox
  3.  
  4. function pointer.new(parent,x,y,w,ctx,cbg,btx)
  5.     local superclass = UIElement:new(parent)
  6.     x = x or 3
  7.     w = w or superclass.parent.width - 6
  8.     ctx = ctx or {0,0,0,255}
  9.     cbg = cbg or {195,195,195,255}
  10.     btx = btx or ""
  11.     local bu = {x=x,y=y,w=w,ctx=ctx,cbg=cbg,btx=btx,t="",tpos=0,poff=0,tick=true,focus=false,fon=love.graphics.getFont(),canvas=love.graphics.newCanvas(w,18),eventHandles={},parent=parent,callback=function()end}
  12.    
  13.     local timer = 0
  14.     local upe = events.registerListener("update",function(dt)
  15.         timer = timer + dt
  16.         if timer > 0.5 then
  17.             bu.tick = not bu.tick
  18.             timer = 0
  19.         end
  20.     end,parent.UID)
  21.     table.insert(bu.eventHandles,{upe,"update"})
  22.     local fon = love.graphics.getFont()
  23.     local tie = events.registerListener("textinput",function(char)
  24.         if screen.hasFocus(parent.UID) and bu.focus then
  25.             bu.t = bu.t:sub(1,bu.tpos) .. char .. bu.t:sub(bu.tpos+1)
  26.             bu.tpos = bu.tpos+1
  27.             bu.tick = true
  28.             timer = 0
  29.             if bu.fon:getWidth(bu.t:sub(1,bu.tpos))+bu.x+bu.poff >= bu.x+bu.w-5 then
  30.                 bu.poff = bu.w-bu.fon:getWidth(bu.t:sub(1,bu.tpos))-5
  31.             end
  32.         end
  33.     end,parent.UID)
  34.     table.insert(bu.eventHandles,{tie,"textinput"})
  35.     local mousec = events.registerListener("mousepressed",function(x,y)
  36.         if screen.hasFocus(parent.UID) then
  37.             if x-parent.x >= bu.x and y- parent.y - 18 >= bu.y and y- parent.y - 18 < bu.y+18 and x-parent.x < bu.x+bu.w then
  38.                 bu.focus = true
  39.             else
  40.                 bu.focus = false
  41.             end
  42.         end
  43.     end,parent.UID)
  44.     table.insert(bu.eventHandles,{mousec,"mousepressed"})
  45.     local keye = events.registerListener("keypressed",function(key)
  46.         if key == "backspace" then
  47.             if screen.hasFocus(parent.UID) and bu.focus then
  48.                 local byteoffset = utf8.offset(bu.t, bu.tpos)
  49.          
  50.                 if byteoffset then
  51.                     bu.tick = true
  52.                     timer = 0
  53.                     bu.t = string.sub(bu.t, 1, byteoffset - 1)..string.sub(bu.t, byteoffset+1)
  54.                     bu.tpos = bu.tpos - 1
  55.                     if bu.fon:getWidth(bu.t:sub(1,bu.tpos))+bu.x+bu.poff < bu.x then
  56.                         bu.poff = 15-bu.fon:getWidth(bu.t:sub(1,bu.tpos))
  57.                         if bu.poff > 0 then
  58.                             bu.poff = 0
  59.                         end
  60.                     end
  61.                 end
  62.             end
  63.         elseif key == "return" then
  64.             bu.callback(bu.t)
  65.         elseif key == "right" then
  66.             if bu.tpos < #bu.t then
  67.                 bu.tpos = bu.tpos+1
  68.                 if bu.fon:getWidth(bu.t:sub(1,bu.tpos))+bu.x+bu.poff >= bu.x+bu.w-5 then
  69.                     bu.poff = bu.w-bu.fon:getWidth(bu.t:sub(1,bu.tpos))-5
  70.                 end
  71.                 bu.tick = true
  72.                 timer = 0
  73.             end
  74.         elseif key == "left" then
  75.             if bu.tpos > 0 then
  76.                 bu.tpos = bu.tpos-1
  77.                 if bu.fon:getWidth(bu.t:sub(1,bu.tpos))+bu.x+bu.poff < bu.x then
  78.                     bu.poff = -bu.fon:getWidth(bu.t:sub(1,bu.tpos))
  79.                 end
  80.                 bu.tick = true
  81.                 timer = 0
  82.             end
  83.         end
  84.     end,parent.UID)
  85.     table.insert(bu.eventHandles,{keye,"keypressed"})
  86.    
  87.     setmetatable(bu, {
  88.         __index=function(t,k) if rawget(pointer,k) then return rawget(pointer, k) end end,
  89.         __newindex = function(t,k,v) return false end,
  90.     })
  91.     return bu
  92. end
  93.  
  94. function pointer:exit()
  95.     for k,v in ipairs(self.eventHandles) do
  96.         events.unregisterListener(v[2], v[1], self.parent.UID)
  97.     end
  98. end
  99.  
  100. function pointer:draw()
  101.     local stencilFunc = function()
  102.         love.graphics.rectangle("fill",self.x,self.y,self.w,18)
  103.     end
  104.    
  105.     love.graphics.stencil(stencilFunc, "replace", 1)
  106.     love.graphics.setStencilTest("greater", 0)
  107.         love.graphics.setColor(unpack(self.cbg))
  108.         love.graphics.rectangle("fill",self.x,self.y,self.w,18)
  109.         love.graphics.setColor(unpack(self.ctx))
  110.         love.graphics.print(self.t,self.x+2+self.poff,self.y+2)
  111.         if self.tick and self.focus then
  112.             love.graphics.print("|",self.fon:getWidth(self.t:sub(1,self.tpos))+self.x+self.poff,self.y+1)
  113.         end
  114.     love.graphics.setStencilTest()
  115. end
  116.  
  117. function pointer:focusOn()
  118.     self.focus = true
  119. end
  120.  
  121. function pointer:unfocusOn()
  122.     self.focus = false
  123. end
  124.  
  125. function pointer:setCallback(func)
  126.     self.callback = func
  127. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement