Advertisement
Guest User

UI_Base.lua

a guest
Dec 15th, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.57 KB | None | 0 0
  1. local renderorder = {}
  2. local obj_c = 0
  3.  
  4. --[[
  5.     Class Shortcut
  6. --]]
  7.  
  8. function UIClass(name,...) --http://lua-users.org/wiki/ObjectOrientationTutorial
  9.  
  10.     local cls, bases = {}, {...}
  11.     for i, base in ipairs(bases) do --copy over vars
  12.         for k, v in pairs(base) do
  13.             cls[k] = v
  14.         end
  15.     end
  16.  
  17.     cls.__index, cls.is_a = cls, {[cls] = true, [name] = true}
  18.     for i, base in ipairs(bases) do     --inheritence checkability
  19.         for c in pairs(base.is_a) do
  20.             cls.is_a[c] = true
  21.         end
  22.         cls.is_a[base] = true
  23.     end
  24.    
  25.     setmetatable(cls, {__call = function (c, x,y,w,h,r,rx,ry, ...)
  26.         local instance = setmetatable({}, c)
  27.        
  28.         obj_c = obj_c + 1
  29.  
  30.         instance.x = x or 0 --pos
  31.         instance.y = y or 0
  32.         instance.z = obj_c
  33.        
  34.         instance.w = w or 0 --size
  35.         instance.h = h or 0
  36.        
  37.         instance.r = r or 0     --rotation and pivot-offset
  38.         instance.rx = rx or w/2
  39.         instance.ry = ry or h/2
  40.        
  41.         instance.hover = false  --interactivenessivity...wat?
  42.         instance.press = false
  43.        
  44.         instance.mx = 0 --local mouse pos
  45.         instance.my = 0
  46.                 --[pos]   x     y     w     h [off]x    y       w   h
  47.         instance.aligninfo = {false,false,false,false,0,0,0,0}
  48.         instance.align = false
  49.        
  50.         instance.base = nil
  51.         instance.Children =  {}--setmetatable({}, {__newindex = function(t,k,v) rawset(t,k,v) print(tostring(t),tostring(k),tostring(v)) end})
  52.         renderorder[obj_c] = instance
  53.        
  54.         local init = instance.init
  55.         if init then init(instance, ...) end
  56.         return instance
  57.         end,
  58.         __tostring = function(c) return "[UIClass: "..name.."]" end,
  59.     })
  60.     return cls
  61. end
  62.  
  63. local UIObj = UIClass("Base")
  64.  
  65. UI = {}
  66. UI.__index = UI
  67. UI.Base = UIObj
  68.  
  69. setmetatable(UI,{__call = function (self,item,...)
  70.     if UI[item] then
  71.         return UI[item](...)
  72.     end
  73. end})
  74.  
  75. VarFunc(UIObj,"Visible",true)
  76. VarFunc(UIObj,"Parent")
  77.  
  78. do
  79.     local def_col = Color(255,255,255)
  80.     VarFunc(UIObj,"Color",def_col)
  81. end
  82. do
  83.     --local x,y,w,h,r,mx,my = 0,0,0,0,0,0,0
  84.     local top,ltop = nil,nil
  85.     local press,lpress,dpress = nil,nil,nil
  86.     local MousePos = love.mouse.getPosition
  87.    
  88.     local MouseDown,M1Down,CM1Down,M2Down,CM2Down = love.mouse.isDown,false,false,false,false
  89.    
  90.     local sin,cos = math.sin,math.cos
  91.     --local clickcache = {}
  92.    
  93.     --[[local function checkhover(item)
  94.         if item:HasChildren() then
  95.            
  96.         end
  97.     end]]
  98.    
  99.     local function pvis(this)
  100.         if not this.Visible then return false end
  101.         if this.Parent then
  102.             return pvis(this.Parent)
  103.         end
  104.        
  105.         return true
  106.     end
  107.    
  108.     GAME:AddHook("Tick","UpdateUI",function(dt)
  109.         ltop = top
  110.         top = nil
  111.        
  112.         CM1Down = M1Down
  113.         M1Down = MouseDown(1)
  114.         CM1Down = (CM1Down ~= M1Down)
  115.        
  116.         CM2Down = M2Down
  117.         M2Down = MouseDown(2)
  118.         CM2Down = (CM2Down ~= M2Down)
  119.        
  120.         --checktop(renderorder)
  121.        
  122.         for i,v in ipairs(renderorder) do --desired ui-item --#
  123.             --if v.Visible and (not v.base or v.base.Visible) then
  124.             if pvis(v) then
  125.                 local x,y,w,h,r,rx,ry = v:Layout()
  126.                
  127.                 v:Tick(x,y,w,h)
  128.                
  129.                 if v.mx >= 0 and v.my >= 0 and v.mx <= w and v.my <= h then
  130.                     top = v
  131.                 end
  132.             end
  133.         end
  134.         GAME:AddDebugID("top",tostring(top).." "..tostring(ltop)..tostring(CM1Down),2)
  135.         --if CM1Down then GAME:AddDebug(tostring(M1Down),1) end
  136.         if ltop ~= top then --interaction logic
  137.             if top then
  138.                 top.hover = true
  139.                 top:OnHoverStart()
  140.             end
  141.             if ltop then
  142.                 ltop.hover = false
  143.                 if ltop.press then ltop.press = false end
  144.                 --if ltop:HasFocus() then ltop:OnLoseFocus() end
  145.                 ltop:OnHoverEnd()
  146.             end
  147.         end
  148.        
  149.         if top then
  150.             if CM1Down then
  151.                 if M1Down then
  152.                     top:OnLeftClick()
  153.                     if GetUIFocus() ~= top then top:RequestFocus() end
  154.                     top.press = true
  155.                 else
  156.                     top:OnLeftReleased()
  157.                     top.press = false
  158.                 end
  159.                 GAME:AddDebug(tostring(M1Down),1)
  160.             end
  161.            
  162.             if CM2Down then
  163.                 if M2Down then
  164.                     top:OnRightClick()
  165.                 else
  166.                     top:OnRightReleased()
  167.                 end
  168.             end
  169.         end
  170.        
  171.     end)
  172.    
  173.     --[[GAME:AddHook("MousePressed","UIMP",function(x,y,b,t)
  174.         if top and b == 1 then
  175.             top:OnLeftClick(b,x,y,t)
  176.         end
  177.     end)]]--#make funktions use hooks mby
  178.    
  179.     GAME:AddHook("MouseWheeled","UIMW",function(up,rgt)
  180.         if top then
  181.             top:MouseWheeled(up,rgt)
  182.         end
  183.     end)
  184.    
  185.     local col_def = Color(255,255,255)
  186.     local function DrawUIStuff(pnl,dt,pmx,pmy)
  187.  
  188.         if pnl.Visible and (not pnl.base or pnl.base.Visible) then
  189.             local x,y,w,h,r,rx,ry = pnl:Layout()
  190.            
  191.             x,y,w,h,r,rx,ry = pnl:PostLayout(x,y,w,h,r,rx,ry)
  192.            
  193.             pnl:Think(x,y,w,h)
  194.            
  195.             if r ~= 0 then
  196.                 love.graphics.translate(rx,ry)
  197.                 love.graphics.rotate(r)
  198.                 love.graphics.translate(-rx,-ry)
  199.  
  200.                 local s = sin(-r)   --rotated local mousepos
  201.                 local c = cos(-r)
  202.                
  203.                 if pnl.Parent then
  204.                     pnl.mx,pnl.my = pmx, pmy
  205.                 else
  206.                     pnl.mx,pnl.my = MousePos()
  207.                 end
  208.                
  209.                
  210.                 pnl.mx = pnl.mx - rx
  211.                 pnl.my = pnl.my - ry
  212.                
  213.                 local vmx = pnl.mx * c - pnl.my * s
  214.                 local vmy = pnl.mx * s + pnl.my * c
  215.                
  216.                 pnl.mx = vmx + rx - x
  217.                 pnl.my = vmy + ry - y
  218.             else --local mousepos
  219.                 if pnl.Parent then
  220.                     pnl.mx,pnl.my = pmx, pmy
  221.                 else
  222.                     pnl.mx,pnl.my = MousePos()
  223.                 end
  224.                 pnl.mx = pnl.mx - x
  225.                 pnl.my = pnl.my - y
  226.             end
  227.            
  228.             pnl:DrawBackground(x,y,w,h)
  229.             pnl:Draw(x,y,w,h)
  230.             pnl:DrawOverlay(x,y,w,h)
  231.            
  232.             for _,c in ipairs(pnl.Children) do
  233.                 DrawUIStuff(c,dt,pnl.mx,pnl.my)
  234.             end
  235.            
  236.             if r ~= 0 then
  237.                 love.graphics.translate(rx,ry)
  238.                 love.graphics.rotate(-r)
  239.                 love.graphics.translate(-rx,-ry)
  240.             end
  241.         end
  242.     end
  243.    
  244.     local setcolor,getcolor = love.graphics.setColor, love.graphics.getColor
  245.     local precol = Color(255,255,255)
  246.     GAME:AddHook("Draw","DrawUI",function(dt)
  247.         love.graphics.setColor( col_def )
  248.         for i,v in ipairs(renderorder) do
  249.             if not v.base then
  250.                 setcolor(v:GetColor() or precol)
  251.                 DrawUIStuff(v,dt)
  252.             end
  253.         end
  254.     end)
  255. end
  256. function UIObj:init() end
  257.  
  258. function UIObj:Layout()
  259.     local p = self.Parent
  260.     local x,y,w,h = self.x,self.y,self.w,self.h
  261.     if p then
  262.         local px,py,pw,ph=p:Layout()
  263.        
  264.        
  265.         if self.align then  --alignment overrides
  266.             local aX, aY, aW, aH, oX, oY, oW, oH = unpack(self.aligninfo)
  267.            
  268.             if aX then x = aX*pw+oX end
  269.             if aY then y = aY*ph+oY end
  270.             if aW then w = aW*pw-x+oW+oX end
  271.             if aH then h = aH*ph-y+oH+oY end
  272.            
  273.         end
  274.        
  275.         return x+px , y+py , w, h, self.r, self.rx+x+px, self.ry+y+py
  276.        
  277.     elseif self.align then  --alignment overrides
  278.         local aX, aY, aW, aH, oX, oY, oW, oH = unpack(self.aligninfo)
  279.        
  280.         if aX then x = aX*ScrW()+oX end
  281.         if aY then y = aY*ScrH()+oY end
  282.         if aW then w = aW*ScrW()-x+oW+oX end
  283.         if aH then h = aH*ScrH()-y+oH+oY end
  284.     end
  285.    
  286.     return x,y,w,h, self.r, self.rx+self.x, self.ry+self.y
  287. end
  288.  
  289. function UIObj:Align(ON,xA,yA,wA,hA,xO,yO,wO,hO)--oh yeah fkn worth it :3
  290.     self.align = ON
  291.     self.aligninfo = {xA or false,yA or false,wA or false,hA or false,xO or 0,yO or 0,wO or 0,hO or 0}
  292. end
  293.  
  294. function UIObj:SetParent(p)
  295.     if p then
  296.         if p.base then
  297.             self.base = p.base
  298.         else
  299.             self.base = p
  300.         end
  301.         p.Children[#p.Children+1] = self
  302.     else
  303.         self.base = p
  304.         if self.Parent then
  305.             for i,v in ipairs(self.Parent.Children) do
  306.                 if v == self then table.remove(self.Parent.Children,i) break end
  307.             end
  308.         end
  309.     end
  310.     self.Parent = p
  311. end
  312.  
  313. function UIObj:Add(...)
  314.     local new = UI(...)
  315.     new:SetParent(self)
  316.    
  317.     return new
  318. end
  319.  
  320. function UIObj:HasChildren()
  321.     return self.Children[1] ~= nil
  322. end
  323.  
  324. function UIObj:Remove()
  325.    
  326.     self:SetParent()
  327.    
  328.     if self:HasChildren() then
  329.         for i,v in ipairs(self.Children) do
  330.             v:Remove()
  331.         end
  332.     end
  333.    
  334.     for i = self.z, obj_c do
  335.         renderorder[i].z = renderorder[i].z - 1
  336.     end
  337.     obj_c = obj_c - 1
  338.     table.remove(renderorder,self.z)
  339.     self = nil
  340. end
  341.  
  342. function UIObj:SetZ(z, moveChildren)
  343.     local tmp
  344.     if z < self.z then --nach vorne z->0
  345.         for i = z, self.z-1 do --alle anderen nach hinten
  346.             tmp = renderorder[i]
  347.             tmp.z = tmp.z + 1
  348.             renderorder[tmp.z] = tmp
  349.         end
  350.     elseif z > self.z then --nach hinten
  351.         for i = self.z+1, z do --vice versa
  352.             tmp = renderorder[i]
  353.             tmp.z = tmp.z - 1
  354.             renderorder[tmp.z] = tmp
  355.         end
  356.     end
  357.     renderorder[z] = self
  358.     self.z = z
  359.     if moveChildren then
  360.         for i,v2 in ipairs(self.Children) do
  361.             v2:SetZ(z,true)
  362.         end
  363.     end
  364. end
  365.  
  366. function UIObj:MoveToTop()
  367.     self:SetZ(obj_c,true)
  368. end
  369.  
  370. do
  371.     local focus = nil
  372.     function UIObj:RequestFocus()
  373.    
  374.         if focus and focus ~= self then focus:OnLoseFocus() end
  375.        
  376.         if self.base then
  377.             self.base:MoveToTop()
  378.         else
  379.             self:MoveToTop()
  380.         end
  381.         focus = self
  382.         self:OnGetFocus()
  383.     end
  384.  
  385.     function UIObj:HasFocus()
  386.         return (focus == self)
  387.     end
  388.    
  389.     function GetUIFocus()
  390.         return focus
  391.     end
  392. end
  393.  
  394. function UIObj:MoveToBottom()
  395.     self:SetZ(1,true)
  396. end
  397.  
  398. function UIObj:SetPos(x,y)
  399.     self.x = x
  400.     self.y = y
  401. end
  402.  
  403. function UIObj:GetPos()
  404.     return self.x, self.y
  405. end
  406.  
  407. function UIObj:SetSize(w,h)
  408.     self.w = w
  409.     self.h = h
  410. end
  411.  
  412. function UIObj:GetSize()
  413.     return self.w, self.h
  414. end
  415.  
  416. do
  417.     local rect = love.graphics.rectangle
  418.     function UIObj:DrawBackground(x,y,w,h)  end
  419.     function UIObj:Draw(x,y,w,h)    rect("fill", x,y,w,h )  end
  420.     function UIObj:DrawOverlay(x,y,w,h) end
  421. end
  422.  
  423.  
  424.  
  425.  
  426. function UIObj:OnGetFocus() end
  427. function UIObj:OnLoseFocus()    end
  428.  
  429. function UIObj:OnHoverStart()   end
  430. function UIObj:OnHoverEnd() end
  431.  
  432. function UIObj:OnLeftClick()    end
  433. function UIObj:OnRightClick()   end
  434. function UIObj:OnLeftReleased() end
  435. function UIObj:OnRightReleased()    end
  436.  
  437. function UIObj:PostLayout(x,y,w,h,r,rx,ry) return x,y,w,h,r,rx,ry end
  438.  
  439. function UIObj:MouseWheeled(up,rgt) end
  440.  
  441. function UIObj:Tick()   end
  442. function UIObj:Think()  end
  443. function UIObj:NewRes(w,h)  end
  444.  
  445. GAME:AddHook("UpdateResolution","UIRes",function(w,h,dW,dH,cx,cy)
  446.         for i,v in ipairs(renderorder) do
  447.             v:NewRes(w,h,dW,dH,cx,cy)
  448.         end
  449.     end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement