Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local renderorder = {}
- local obj_c = 0
- --[[
- Class Shortcut
- --]]
- function UIClass(name,...) --http://lua-users.org/wiki/ObjectOrientationTutorial
- local cls, bases = {}, {...}
- for i, base in ipairs(bases) do --copy over vars
- for k, v in pairs(base) do
- cls[k] = v
- end
- end
- cls.__index, cls.is_a = cls, {[cls] = true, [name] = true}
- for i, base in ipairs(bases) do --inheritence checkability
- for c in pairs(base.is_a) do
- cls.is_a[c] = true
- end
- cls.is_a[base] = true
- end
- setmetatable(cls, {__call = function (c, x,y,w,h,r,rx,ry, ...)
- local instance = setmetatable({}, c)
- obj_c = obj_c + 1
- instance.x = x or 0 --pos
- instance.y = y or 0
- instance.z = obj_c
- instance.w = w or 0 --size
- instance.h = h or 0
- instance.r = r or 0 --rotation and pivot-offset
- instance.rx = rx or w/2
- instance.ry = ry or h/2
- instance.hover = false --interactivenessivity...wat?
- instance.press = false
- instance.mx = 0 --local mouse pos
- instance.my = 0
- --[pos] x y w h [off]x y w h
- instance.aligninfo = {false,false,false,false,0,0,0,0}
- instance.align = false
- instance.base = nil
- instance.Children = {}--setmetatable({}, {__newindex = function(t,k,v) rawset(t,k,v) print(tostring(t),tostring(k),tostring(v)) end})
- renderorder[obj_c] = instance
- local init = instance.init
- if init then init(instance, ...) end
- return instance
- end,
- __tostring = function(c) return "[UIClass: "..name.."]" end,
- })
- return cls
- end
- local UIObj = UIClass("Base")
- UI = {}
- UI.__index = UI
- UI.Base = UIObj
- setmetatable(UI,{__call = function (self,item,...)
- if UI[item] then
- return UI[item](...)
- end
- end})
- VarFunc(UIObj,"Visible",true)
- VarFunc(UIObj,"Parent")
- do
- local def_col = Color(255,255,255)
- VarFunc(UIObj,"Color",def_col)
- end
- do
- --local x,y,w,h,r,mx,my = 0,0,0,0,0,0,0
- local top,ltop = nil,nil
- local press,lpress,dpress = nil,nil,nil
- local MousePos = love.mouse.getPosition
- local MouseDown,M1Down,CM1Down,M2Down,CM2Down = love.mouse.isDown,false,false,false,false
- local sin,cos = math.sin,math.cos
- --local clickcache = {}
- --[[local function checkhover(item)
- if item:HasChildren() then
- end
- end]]
- local function pvis(this)
- if not this.Visible then return false end
- if this.Parent then
- return pvis(this.Parent)
- end
- return true
- end
- GAME:AddHook("Tick","UpdateUI",function(dt)
- ltop = top
- top = nil
- CM1Down = M1Down
- M1Down = MouseDown(1)
- CM1Down = (CM1Down ~= M1Down)
- CM2Down = M2Down
- M2Down = MouseDown(2)
- CM2Down = (CM2Down ~= M2Down)
- --checktop(renderorder)
- for i,v in ipairs(renderorder) do --desired ui-item --#
- --if v.Visible and (not v.base or v.base.Visible) then
- if pvis(v) then
- local x,y,w,h,r,rx,ry = v:Layout()
- v:Tick(x,y,w,h)
- if v.mx >= 0 and v.my >= 0 and v.mx <= w and v.my <= h then
- top = v
- end
- end
- end
- GAME:AddDebugID("top",tostring(top).." "..tostring(ltop)..tostring(CM1Down),2)
- --if CM1Down then GAME:AddDebug(tostring(M1Down),1) end
- if ltop ~= top then --interaction logic
- if top then
- top.hover = true
- top:OnHoverStart()
- end
- if ltop then
- ltop.hover = false
- if ltop.press then ltop.press = false end
- --if ltop:HasFocus() then ltop:OnLoseFocus() end
- ltop:OnHoverEnd()
- end
- end
- if top then
- if CM1Down then
- if M1Down then
- top:OnLeftClick()
- if GetUIFocus() ~= top then top:RequestFocus() end
- top.press = true
- else
- top:OnLeftReleased()
- top.press = false
- end
- GAME:AddDebug(tostring(M1Down),1)
- end
- if CM2Down then
- if M2Down then
- top:OnRightClick()
- else
- top:OnRightReleased()
- end
- end
- end
- end)
- --[[GAME:AddHook("MousePressed","UIMP",function(x,y,b,t)
- if top and b == 1 then
- top:OnLeftClick(b,x,y,t)
- end
- end)]]--#make funktions use hooks mby
- GAME:AddHook("MouseWheeled","UIMW",function(up,rgt)
- if top then
- top:MouseWheeled(up,rgt)
- end
- end)
- local col_def = Color(255,255,255)
- local function DrawUIStuff(pnl,dt,pmx,pmy)
- if pnl.Visible and (not pnl.base or pnl.base.Visible) then
- local x,y,w,h,r,rx,ry = pnl:Layout()
- x,y,w,h,r,rx,ry = pnl:PostLayout(x,y,w,h,r,rx,ry)
- pnl:Think(x,y,w,h)
- if r ~= 0 then
- love.graphics.translate(rx,ry)
- love.graphics.rotate(r)
- love.graphics.translate(-rx,-ry)
- local s = sin(-r) --rotated local mousepos
- local c = cos(-r)
- if pnl.Parent then
- pnl.mx,pnl.my = pmx, pmy
- else
- pnl.mx,pnl.my = MousePos()
- end
- pnl.mx = pnl.mx - rx
- pnl.my = pnl.my - ry
- local vmx = pnl.mx * c - pnl.my * s
- local vmy = pnl.mx * s + pnl.my * c
- pnl.mx = vmx + rx - x
- pnl.my = vmy + ry - y
- else --local mousepos
- if pnl.Parent then
- pnl.mx,pnl.my = pmx, pmy
- else
- pnl.mx,pnl.my = MousePos()
- end
- pnl.mx = pnl.mx - x
- pnl.my = pnl.my - y
- end
- pnl:DrawBackground(x,y,w,h)
- pnl:Draw(x,y,w,h)
- pnl:DrawOverlay(x,y,w,h)
- for _,c in ipairs(pnl.Children) do
- DrawUIStuff(c,dt,pnl.mx,pnl.my)
- end
- if r ~= 0 then
- love.graphics.translate(rx,ry)
- love.graphics.rotate(-r)
- love.graphics.translate(-rx,-ry)
- end
- end
- end
- local setcolor,getcolor = love.graphics.setColor, love.graphics.getColor
- local precol = Color(255,255,255)
- GAME:AddHook("Draw","DrawUI",function(dt)
- love.graphics.setColor( col_def )
- for i,v in ipairs(renderorder) do
- if not v.base then
- setcolor(v:GetColor() or precol)
- DrawUIStuff(v,dt)
- end
- end
- end)
- end
- function UIObj:init() end
- function UIObj:Layout()
- local p = self.Parent
- local x,y,w,h = self.x,self.y,self.w,self.h
- if p then
- local px,py,pw,ph=p:Layout()
- if self.align then --alignment overrides
- local aX, aY, aW, aH, oX, oY, oW, oH = unpack(self.aligninfo)
- if aX then x = aX*pw+oX end
- if aY then y = aY*ph+oY end
- if aW then w = aW*pw-x+oW+oX end
- if aH then h = aH*ph-y+oH+oY end
- end
- return x+px , y+py , w, h, self.r, self.rx+x+px, self.ry+y+py
- elseif self.align then --alignment overrides
- local aX, aY, aW, aH, oX, oY, oW, oH = unpack(self.aligninfo)
- if aX then x = aX*ScrW()+oX end
- if aY then y = aY*ScrH()+oY end
- if aW then w = aW*ScrW()-x+oW+oX end
- if aH then h = aH*ScrH()-y+oH+oY end
- end
- return x,y,w,h, self.r, self.rx+self.x, self.ry+self.y
- end
- function UIObj:Align(ON,xA,yA,wA,hA,xO,yO,wO,hO)--oh yeah fkn worth it :3
- self.align = ON
- 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}
- end
- function UIObj:SetParent(p)
- if p then
- if p.base then
- self.base = p.base
- else
- self.base = p
- end
- p.Children[#p.Children+1] = self
- else
- self.base = p
- if self.Parent then
- for i,v in ipairs(self.Parent.Children) do
- if v == self then table.remove(self.Parent.Children,i) break end
- end
- end
- end
- self.Parent = p
- end
- function UIObj:Add(...)
- local new = UI(...)
- new:SetParent(self)
- return new
- end
- function UIObj:HasChildren()
- return self.Children[1] ~= nil
- end
- function UIObj:Remove()
- self:SetParent()
- if self:HasChildren() then
- for i,v in ipairs(self.Children) do
- v:Remove()
- end
- end
- for i = self.z, obj_c do
- renderorder[i].z = renderorder[i].z - 1
- end
- obj_c = obj_c - 1
- table.remove(renderorder,self.z)
- self = nil
- end
- function UIObj:SetZ(z, moveChildren)
- local tmp
- if z < self.z then --nach vorne z->0
- for i = z, self.z-1 do --alle anderen nach hinten
- tmp = renderorder[i]
- tmp.z = tmp.z + 1
- renderorder[tmp.z] = tmp
- end
- elseif z > self.z then --nach hinten
- for i = self.z+1, z do --vice versa
- tmp = renderorder[i]
- tmp.z = tmp.z - 1
- renderorder[tmp.z] = tmp
- end
- end
- renderorder[z] = self
- self.z = z
- if moveChildren then
- for i,v2 in ipairs(self.Children) do
- v2:SetZ(z,true)
- end
- end
- end
- function UIObj:MoveToTop()
- self:SetZ(obj_c,true)
- end
- do
- local focus = nil
- function UIObj:RequestFocus()
- if focus and focus ~= self then focus:OnLoseFocus() end
- if self.base then
- self.base:MoveToTop()
- else
- self:MoveToTop()
- end
- focus = self
- self:OnGetFocus()
- end
- function UIObj:HasFocus()
- return (focus == self)
- end
- function GetUIFocus()
- return focus
- end
- end
- function UIObj:MoveToBottom()
- self:SetZ(1,true)
- end
- function UIObj:SetPos(x,y)
- self.x = x
- self.y = y
- end
- function UIObj:GetPos()
- return self.x, self.y
- end
- function UIObj:SetSize(w,h)
- self.w = w
- self.h = h
- end
- function UIObj:GetSize()
- return self.w, self.h
- end
- do
- local rect = love.graphics.rectangle
- function UIObj:DrawBackground(x,y,w,h) end
- function UIObj:Draw(x,y,w,h) rect("fill", x,y,w,h ) end
- function UIObj:DrawOverlay(x,y,w,h) end
- end
- function UIObj:OnGetFocus() end
- function UIObj:OnLoseFocus() end
- function UIObj:OnHoverStart() end
- function UIObj:OnHoverEnd() end
- function UIObj:OnLeftClick() end
- function UIObj:OnRightClick() end
- function UIObj:OnLeftReleased() end
- function UIObj:OnRightReleased() end
- function UIObj:PostLayout(x,y,w,h,r,rx,ry) return x,y,w,h,r,rx,ry end
- function UIObj:MouseWheeled(up,rgt) end
- function UIObj:Tick() end
- function UIObj:Think() end
- function UIObj:NewRes(w,h) end
- GAME:AddHook("UpdateResolution","UIRes",function(w,h,dW,dH,cx,cy)
- for i,v in ipairs(renderorder) do
- v:NewRes(w,h,dW,dH,cx,cy)
- end
- end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement