Advertisement
Guest User

Untitled

a guest
Jun 12th, 2013
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 16.05 KB | None | 0 0
  1. -- util functions
  2.  
  3. function ags.DrawingSurface:Frame(x,y,w,h)
  4.     x = x or 0
  5.     y = y or 0
  6.     w = w or self.Width - 1
  7.     h = h or self.Height - 1
  8.    
  9.     local fillColor = 12678
  10.     local lineColor = 27501
  11.     local cornerColor = 21130
  12.    
  13.     --fill first
  14.     --self.DrawingColor = fillColor
  15.     --self:DrawRectangle(x,y,x + w, y + h)
  16.    
  17.     --lines
  18.     self.DrawingColor = lineColor
  19.     self:DrawUnfilledRect(x,y,w,h)
  20.    
  21.     --corners
  22.     self.DrawingColor = cornerColor
  23.     self:DrawRectangle(x,y,x + 2, y + 2) -- top left
  24.     self:DrawRectangle(x + w - 2, y,x + w, y + 2) -- top right
  25.     self:DrawRectangle(x + w - 2, y + h - 2,x + w, y + h) -- bottom right
  26.     self:DrawRectangle(x,y + h - 2,x, y + h) -- bottom left
  27.    
  28. end
  29.  
  30. function ags.DrawingSurface:DrawUnfilledRect(x,y,w,h)
  31.     self:DrawLine(x, y, x + w, y)
  32.     self:DrawLine(x, y + h, x + w, y + h)
  33.     self:DrawLine(x, y, x, y + h)
  34.     self:DrawLine(x + w, y, x + w, y + h)
  35. end
  36.  
  37. --color stuff
  38.  
  39. Color = {}
  40.  
  41. Color.mt = {}
  42.  
  43. Color.mt.__index = function(t,v)
  44.     if v == "r" or v == "g" or v == "b" or v == "a" then
  45.             return t.rawColor[v]
  46.         else
  47.             return Color[v]
  48.         end
  49.     end
  50.  
  51. Color.mt__newindex = function(t,k,v)
  52.     if k == "r" or k == "g" or k == "b" or k == "a" then
  53.         t.rawColor[k] = clamp(v,0,255)
  54.     else
  55.         t[k] = v
  56.     end
  57. end
  58.  
  59. function Color.FromRGBA(r,g,b,a)
  60.     local o = {}
  61.     setmetatable(o, Color.mt)
  62.     o.rawColor = {}
  63.     o.rawColor.r = r
  64.     o.rawColor.g = g
  65.     o.rawColor.b = b
  66.     o.rawColor.a = a or 255
  67.     return o
  68. end
  69.  
  70. function Color:Copy()
  71.     return Color.FromRGBA(self:unpack())
  72. end
  73.  
  74. function Color:AsAGS()
  75.     return ags.Game.GetColorFromRGB(self.r,self.g,self.b)
  76. end
  77.  
  78. function Color:unpack()
  79.     return self.r, self.g, self.b, self.a
  80. end
  81.  
  82. Color.DarkRed = Color.FromRGBA(128,0,0)
  83. Color.DarkGreen = Color.FromRGBA(0,128,0)
  84. Color.DarkBlue = Color.FromRGBA(0,0,128)
  85. Color.LightGrey = Color.FromRGBA(200,200,200)
  86. Color.DarkGrey = Color.FromRGBA(50,50,50)
  87. Color.Blue = Color.FromRGBA(0,8,200)
  88. Color.White = Color.FromRGBA(255,255,255)
  89. Color.Black = Color.FromRGBA(0,0,0)
  90.  
  91. --GUI
  92.  
  93. manager = require('guimanager')
  94.  
  95. gui =
  96. {
  97.     x = 0,
  98.     y = 0,
  99.     width = 100,
  100.     height = 100,
  101.     bgColor = Color.FromRGBA(10,10,10,255),
  102.     fgColor = Color.White,
  103.     borderColor = Color.LightGrey,
  104.     titleBarHeight = 11,
  105.     font = 1,
  106.     zOrder = 1,
  107.     draggable = true
  108. }
  109.  
  110. function gui:new(o)
  111.     o = o or {}
  112.     setmetatable(o, self)
  113.     self.__index = self
  114.     o.visible = o.visible == null and true or o.visible
  115.     o.controls = {}
  116.     o.sprite = ags.DynamicSprite.Create(o.width, o.height)
  117.     manager:add(o)
  118.     return o
  119. end
  120.  
  121. function gui:mouseLeave()
  122.  
  123. end
  124.  
  125. function gui:mouseEnter()
  126.     -- body
  127. end
  128.  
  129. function gui:mouseUp()
  130.  
  131. end
  132.  
  133. function gui:keyDown(key)
  134.     return false
  135. end
  136.  
  137. function gui:keyUp(key)
  138.  
  139. end
  140.  
  141. function gui:mouseDown(x,y,mb)
  142.     if y < self.titleBarHeight and self.draggable then
  143.         self.dragging = true
  144.         manager.dragging = self
  145.         self.offset = {x,y}
  146.     elseif x > self.width - 3 and y > self.height - 3 then
  147.         self.resizing = true
  148.         manager.resizing = self
  149.         self.offset = {x,y}
  150.         self.lastSize = {self.width, self.height}
  151.     end
  152. end
  153.  
  154. function gui:clampToScreen()
  155.     local w = ags.System.ViewportWidth
  156.     local h = ags.System.ViewportHeight
  157.     self.x = clamp(self.x,0,w - self.width)
  158.     self.y = clamp(self.y,0,h - self.height)
  159. end
  160.  
  161. function gui:getControl(x,y)
  162.     for i,v in ipairs(self.controls) do
  163.         if v.x < x and x < v.x + v.width and v.y < y and y < v.y + v.height then
  164.             if v.getControl then
  165.                 return v:getControl(x - v.x, y - v.y) or v
  166.             else
  167.                 return v
  168.             end
  169.         end
  170.     end
  171. end
  172.  
  173. function gui:update( dt )
  174.     if self.dragging then
  175.         local x,y = ags.Mouse.x, ags.Mouse.y
  176.         self.x = x - self.offset[1]
  177.         self.y = y - self.offset[2]
  178.     elseif self.resizing then
  179.         local x,y = ags.Mouse.x, ags.Mouse.y
  180.        
  181.         self.width =  math.max(self.lastSize[1] + (x - (self.lastSize[1] + self.x)), 1)
  182.         self.height = math.max(self.lastSize[2] + (y - (self.lastSize[2] + self.y)), 1)
  183.        
  184.         end
  185.         self:clampToScreen()
  186.         for i,v in ipairs(self.controls or {}) do
  187.             v:update(dt)
  188.         end
  189. end
  190.  
  191. function gui:show()
  192.     self.visible = true
  193. end
  194.  
  195. function gui:hide()
  196.     self.visible = false
  197. end
  198.  
  199. function gui:addControl(c)
  200.     table.insert(self.controls, c)
  201. end
  202.  
  203. function gui:absolutePos()
  204.     return self.x, self.y
  205. end
  206.  
  207. function gui.__tostring()
  208.     return "GUI"
  209. end
  210.  
  211. function gui:draw(surface)
  212.    
  213.     if not self.visible then return end
  214.    
  215.     if self.width ~= self.sprite.Width or self.height ~= self.sprite.height then
  216.         self.sprite:Delete()
  217.         self.sprite = ags.DynamicSprite.Create(self.width, self.height)
  218.     end
  219.    
  220.     local s = self.sprite:GetDrawingSurface()
  221.     s:Clear()
  222.     if self.background then
  223.             s:DrawImage(0,0,self.background)
  224.     else
  225.         s.DrawingColor = ags.Game.GetColorFromRGB(self.bgColor:unpack())
  226.         s:DrawRectangle(0, 0,self.width, self.height)
  227.         s.DrawingColor = ags.Game.GetColorFromRGB(self.borderColor:unpack())
  228.         s:DrawRectangle(0,0,  self.width, self.titleBarHeight)        
  229.         s:Frame()
  230.        
  231.         if self.text then
  232.             s.DrawingColor = ags.Game.GetColorFromRGB(self.fgColor:unpack())
  233.             s:DrawString(5, 2 ,self.font, self.text)
  234.         end
  235.     end
  236.     for i,v in ipairs(self.controls or {}) do
  237.             v:draw(0,0,s)
  238.     end
  239.    
  240.     surface:DrawImage(self.x,self.y,self.sprite.Graphic)
  241. end
  242.  
  243. -- gui control
  244.  
  245. gui.control =
  246. {
  247.     x = 0,
  248.     y = 0,
  249.     width = 30,
  250.     height = 10,
  251.     bgColor = Color.DarkGrey,
  252.     fgColor = Color.White,
  253.     hlColor = Color.Blue,
  254.     font = 1,
  255.     text = ""
  256. }
  257.  
  258. function gui.control:new(o)    
  259.     local nullFunc = function() end
  260.     o = o or {}
  261.     o.update = nullFunc
  262.     o.draw = nullFunc
  263.     o.mouseOver = nullFunc
  264.     o.mouseEnter = nullFunc
  265.     o.mouseLeave = nullFunc
  266.     o.mouseDown = nullFunc
  267.     o.mouseUp = nullFunc
  268.     o.keyDown = nullFunc
  269.     o.type = "control"
  270.     o.visible = true
  271.     setmetatable(o, { __index = self})
  272.     return o
  273. end
  274.  
  275. function gui.control.__tostring()
  276.     return self.type .. " - " .. (self.name or "")
  277. end
  278.  
  279. function gui.control:parentTo(parent)
  280.     parent:addControl(self)
  281.     self.parent = parent
  282. end
  283.  
  284. function gui.control:focus()
  285.     manager:setFocus(self)
  286. end
  287.  
  288. function gui.control:isFocussed()
  289.     return manager.focussedControl == self
  290. end
  291.  
  292. function gui.control:absolutePos()
  293.     local x,y = self.x, self.y
  294.     local p = self.parent
  295.     local xx,yy = p:absolutePos()
  296.     x,y = xx + x, yy +y
  297.     return x,y
  298. end
  299.  
  300. -- gui button
  301.  
  302. gui.button = gui.control:new()
  303. gui.button.type = "button"
  304. --setmetatable(gui.button, { __index = gui.control} )
  305.  
  306. function gui.button:new(o, parent)
  307.     o = o or {}
  308.     setmetatable(o, { __index = self })
  309.     o:parentTo(parent)
  310.     return o
  311. end
  312.  
  313. function gui.button:mouseEnter()
  314.     self.isMouseOver = true
  315. end
  316.  
  317. function gui.button:mouseLeave()
  318.     self.isMouseOver = false
  319.     self.isPressed = false
  320. end
  321.  
  322. function gui.button:mouseDown(x,y,mb)
  323.     if mb == 1 then
  324.             self.isPressed = true
  325.     end
  326. end
  327.  
  328. function gui.button:mouseUp(x,y,mb)
  329.     if mb == 1 and self.isPressed then
  330.         self:onClick()
  331.         self.isPressed = false
  332.     end
  333. end
  334.  
  335. function gui.button:onClick( ... )
  336.     -- body
  337. end
  338.  
  339. function gui.button:draw(x,y,surface)
  340.     if self.parent.isLayout then
  341.         x,y = self.x + x, self.y + y
  342.     else
  343.         x,y = self.x, self.y
  344.     end
  345.    
  346.     if self.clip then
  347.         self.width = ags.GetTextWidth(self.text,self.font) + 6
  348.     end
  349.    
  350.     surface.DrawingColor = ags.Game.GetColorFromRGB(self.bgColor:unpack())
  351.     surface:DrawRectangle(x,y,x + self.width, y + self.height)
  352.    
  353.     local low = {self.bgColor.r * 0.7, self.bgColor.g  * 0.7, self.bgColor.b * 0.7,255}
  354.     local high =
  355.     {
  356.         clamp(self.bgColor.r * 1.5,0,255),
  357.         clamp(self.bgColor.g * 1.5,0,255),
  358.         clamp(self.bgColor.b * 1.5,0,255),
  359.         255
  360.     }
  361.    
  362.     if self.isPressed or self.toggled then
  363.         low,high = high,low
  364.     end
  365.     surface.DrawingColor = ags.Game.GetColorFromRGB(unpack(high))
  366.     surface:DrawPath(x,y + self.height,x, y, x + self.width, y)
  367.     surface.DrawingColor = ags.Game.GetColorFromRGB(unpack(low))
  368.     surface:DrawPath(x,y + self.height,x + self.width, y + self.height, x + self.width, y)
  369.     surface.DrawingColor = ags.Game.GetColorFromRGB(self.fgColor:unpack())
  370.    
  371.   surface:DrawString(x + 3, y + 2, self.font, self.text)
  372.    
  373. end
  374.  
  375. gui.label = gui.control:new()
  376. gui.label.type = "label"
  377.  
  378. function gui.label:new( o,parent )
  379.     o = o or {}
  380.     setmetatable(o, { __index = self })
  381.     o:parentTo(parent)
  382.     o.alignment = o.alignment or "left"
  383.     o.clip = o.clip or false
  384.     return o
  385. end
  386.  
  387. function gui.label:draw(x,y, surface)
  388.     if self.parent.isLayout then
  389.         x,y = self.x + x, self.y + y
  390.     end
  391.    
  392.     surface.DrawingColor= self.fgColor:AsAGS()
  393.     surface:DrawString(x,y,self.font,self.text)
  394. --  local lines = self.text:split("\n")
  395. --  local w = 0
  396. --  for _,v in ipairs(lines) do
  397. --      local lw = self.font:getWidth(v)
  398. --      w = w > lw and w or lw
  399. --  end
  400. --  self.width = w
  401. --  local _, linecount = string.gsub(self.text, "\n", "")
  402. --  linecount = linecount + 1
  403. --  self.height = self.font:getHeight() * linecount
  404. --  love.graphics.printf(self.text, x, y, self.width + 1, self.alignment)
  405. end
  406.  
  407. -- gui textbox
  408.  
  409. gui.textbox = gui.control:new()
  410. gui.textbox.type = "textbox"
  411.  
  412. function gui.textbox:new(o, parent)
  413.     o = o or {}
  414.     o.caret = 0
  415.     o.caretOn = false
  416.     o.caretSpeed = 1
  417.     o.activate = o.activate or function() end
  418.     setmetatable(o, { __index = self })
  419.     o:parentTo(parent)
  420.     return o
  421. end
  422.  
  423. function gui.textbox:keyDown(key)
  424.    
  425.     if key == 13 then self:activate() return end -- pressed enter
  426.    
  427.     if key == 8 and self.text:len() > 0 then -- pressed backspace
  428.         self.text = self.text:sub(1, self.text:len() - 1)
  429.         return
  430.     end
  431.    
  432.     if key < 32 or key > 90 then return end --non ascii keys
  433.    
  434.     key = string.char(key) -- else
  435.     key = key:lower()
  436.     if ags.IsKeyPressed(403) or ags.System.CapsLock then
  437.         key = key:upper()
  438.     end
  439.     self.text = self.text .. key
  440. end
  441.  
  442. function gui.textbox:update(dt)
  443.     self.caret = self.caret + dt
  444.     if self.caret > self.caretSpeed then
  445.         self.caretOn = not self.caretOn
  446.         self.caret = self.caret - self.caretSpeed
  447.     end
  448. end
  449.  
  450. function gui.textbox:draw(x,y,surface)
  451.     if self.parent.isLayout then
  452.         x,y = self.x + x, self.y + y
  453.     else
  454.         x,y = self.x, self.y
  455.     end
  456.    
  457.     local w,h = self.width, self.height
  458.        
  459.     if self.label then
  460.         surface.DrawingColor = self.fgColor:AsAGS()
  461.         local txHeight = ags.GetTextHeight(self.label, self.font, 300)
  462.         surface:DrawString(x, y + h / 2 - txHeight / 2, self.font, self.label)
  463.         local ext = ags.GetTextWidth(self.label, self.font) + 2
  464.         x = x + ext
  465.         w = w - ext
  466.     end
  467.    
  468.     local tempSpr = ags.DynamicSprite.Create(w,h)
  469.     local tempSur = tempSpr:GetDrawingSurface()
  470.    
  471.     tempSur.DrawingColor = self.bgColor:AsAGS()
  472.     tempSur:Clear(tempSur.DrawingColor)
  473.  
  474.     tempSur.DrawingColor = self.fgColor:AsAGS()
  475.     tempSur:DrawString(2, 2, self.font, self.text)
  476.     if self:isFocussed() then
  477.         if self.caretOn then
  478.             local textWidth = ags.GetTextWidth(self.text, self.font)
  479.             local textHeight =  ags.GetTextHeight("aYJGilymn", self.font, 500)
  480.             tempSur:DrawLine(textWidth + 2, 2, textWidth + 2, textHeight)
  481.         end
  482.     end
  483.    
  484.     if self:isFocussed() then
  485.             tempSur.DrawingColor = self.hlColor:AsAGS()
  486.     else
  487.             tempSur.DrawingColor = self.fgColor:AsAGS()
  488.     end
  489.     tempSur:DrawUnfilledRect(0,0,w - 1,h - 1)
  490.    
  491.     tempSur:Release()
  492.     surface:DrawImage(x,y,tempSpr.Graphic)
  493.     tempSpr:Delete()
  494. end
  495.  
  496. -- gui imagebox
  497.  
  498. gui.imagebox = gui.control:new()
  499. gui.imagebox.type = "imagebox"
  500.  
  501. function gui.imagebox:new(o, parent)
  502.     o = o or {}
  503.     setmetatable(o, { __index = self })
  504.     o:parentTo(parent)
  505.     return o
  506. end
  507.  
  508. function gui.imagebox:draw()
  509.     if not self.image then return end
  510.         ags.AbortGame("Image box hasnt been converted to AGS yet")
  511.     local x,y = self:absolutePos()
  512.     if self.clipped then
  513.       lg.setScissor(x,y,self.width, self.height)
  514.     end
  515.     lg.draw(self.image,x,y)
  516.     lg.setScissor()
  517. end
  518.  
  519. -- gui layout
  520.  
  521. gui.layout = gui.control:new()
  522. gui.layout.type = "layout"
  523.  
  524. function gui.layout:new(o, parent)
  525.     o = o or {}
  526.     setmetatable(o, { __index = self })
  527.     o:parentTo(parent)
  528.     o.direction = o.direction or "vertical"
  529.     o.drawX = o.x
  530.     o.drawY = o.y
  531.     o.margin = o.margin or 4
  532.     o.spacing = o.spacing or 2
  533.     o.border = o.border == null and true or o.border
  534.     o.controls = {}
  535.     o.isLayout = true
  536.     return o
  537. end
  538.  
  539. function gui.layout.__tostring()
  540.     return "layout"
  541. end
  542.  
  543. function gui.layout:addControl(con)
  544.     table.insert(self.controls, con)
  545. end
  546.  
  547. --function gui.layout:absolutePos()
  548. --local xx,yy = self.parent:absolutePos()
  549.     --return xx + self.drawX, yy + self.drawY
  550. --end
  551.  
  552. function gui.layout:getControl(x,y)
  553.     for g,v in pairs(self.positions or {}) do
  554.         if v[1] < x and x < v[1] + v[3] and v[2] < y and y < v[2] + v[4] then
  555.             if g.getControl then
  556.                 return g:getControl(x - v[1], y - v[2]) or g
  557.             else
  558.                 return g
  559.             end
  560.         end
  561.     end
  562. end
  563.  
  564. function gui.layout:update(dt)
  565.     for i,v in ipairs(self.controls or {}) do
  566.         v:update(dt)
  567.     end
  568. end
  569.  
  570. function gui.layout:draw(x,y,surface)
  571.     if not self.visible then return end
  572.     self.drawX = x + self.x + self.margin
  573.     self.drawY = y + self.y + self.margin
  574.     if self.text and self.text ~= "" and self.direction == "vertical" then
  575.         -- not supported in AGS yet
  576.     end
  577.     self.positions = {}
  578.     self.maxHeight = 0
  579.     self.maxWidth = 0
  580.     local drawnOne = false
  581.     for i,v in ipairs(self.controls or {}) do
  582.         if drawnOne and v.visible then
  583.             if self.direction == "vertical" then
  584.                 self.drawY = self.drawY + self.spacing
  585.             else
  586.                 self.drawX = self.drawX + self.spacing
  587.             end
  588.         end
  589.         v:draw(self.drawX, self.drawY, surface)
  590.         drawnOne = true
  591.         if v.visible then
  592.             self.positions[v] = {self.drawX - x - self.x, self.drawY - y - self.y, v.width, v.height}
  593.             if self.direction == "vertical" then
  594.                 self.drawY = self.drawY + v.height
  595.                 self.maxWidth = math.max(self.maxWidth, v.width)
  596.             else
  597.                 self.drawX = self.drawX + v.width
  598.                 self.maxHeight = math.max(self.maxHeight, v.height)
  599.             end
  600.         end
  601.     end
  602.     if self.direction == "vertical" then
  603.         self.height = self.drawY + self.margin - y - self.y
  604.         self.width = self.maxWidth + self.margin * 2
  605.     else
  606.         self.width = self.drawX + self.margin - x - self.x
  607.         self.height = self.maxHeight + self.margin * 2
  608.     end
  609.     if self.border then
  610.         --love.graphics.setColor(self.fgColor:unpack())
  611.         --love.graphics.rectangle("line",self.x + x, self.y + y, self.width, self.height)
  612.     end
  613. end
  614.  
  615. gui.hline = gui.control:new()
  616. gui.hline.type = "hline"
  617.  
  618. function gui.hline:new(o, parent)
  619.     o = o or {}
  620.     o.x = o.x or 0
  621.     o.y = o.y or 0
  622.     o.height = 0
  623.     setmetatable(o, { __index = self })
  624.     o:parentTo(parent)
  625.  
  626.    
  627. end
  628.  
  629. function gui.hline:draw(x,y, surface)
  630.     love.graphics.setColor(self.fgColor:unpack())
  631.     love.graphics.line(x,y,x + self.parent.width - 10, y)
  632. end
  633.  
  634. gui.selector = {}
  635.  
  636. function gui.selector:new(o,parent)
  637.     o.choices = o.choices or {}
  638.     local lo = gui.layout:new({direction = "horizontal"}, parent)
  639.     lo.label = gui.label:new({text = o.text, y = 2, x =0}, lo)
  640.     lo.left = gui.button:new({text = "<", x=0,y=0, width = 15, height = 15}, lo)
  641.     lo.choice = gui.label:new({alignment = "center", text = o.choices[1], clip = true,width = 65, x=0,y=2,height = 15}, lo)
  642.     lo.right = gui.button:new({text = ">", x=0,y=0, width = 15, height = 15}, lo)
  643.     lo.selectedIndex = 1
  644.     lo.left.onClick = function()
  645.         lo.selectedIndex = loop(lo.selectedIndex - 1,1,#lo.choices)
  646.         lo.choice.text = lo.choices[lo.selectedIndex]
  647.         if lo.onChange and type(lo.onChange == "function") then
  648.             lo.onChange(lo)
  649.         end
  650.     end
  651.     lo.right.onClick = function()
  652.         lo.selectedIndex = loop(lo.selectedIndex + 1,1,#lo.choices)
  653.         lo.choice.text = lo.choices[lo.selectedIndex]
  654.         if lo.onChange and type(lo.onChange == "function") then
  655.             lo.onChange(lo)
  656.         end
  657.     end
  658.     lo.update = function(dt)
  659.         gui.layout.update(self,dt)
  660.         lo.choice.text = lo.choices[lo.selectedIndex]
  661.     end
  662.    
  663.     lo.choices = o.choices
  664.     return lo
  665. end
  666.  
  667. gui.toolbar = {}
  668.  
  669. function gui.toolbar:new(o,parent)
  670.     o.choices = o.choices or {}
  671.     o.direction = "horizontal"
  672.    
  673.     local lo = gui.layout:new(o, parent)
  674.     lo.buttons = {}
  675.     for i,v in ipairs(o.choices) do
  676.         v.width = o.buttonWidth
  677.         v.height = o.buttonHeight
  678.         v.clip = true
  679.         v.onClick = function(self)
  680.             for i,v in ipairs(self.parent.buttons) do
  681.                 v.toggled = false
  682.             end
  683.             self.toggled = true
  684.             self.parent.selected = self
  685.         end
  686.         local b = gui.button:new(v, lo)
  687.         table.insert(lo.buttons, b)
  688.     end
  689.     return lo
  690. end
  691.  
  692. return gui
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement