Advertisement
AI_UBI

Part of game on Lua with Love2D

Jan 3rd, 2016
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.34 KB | None | 0 0
  1. local BUTTON_META = {}
  2. BUTTON_META = table.MergeAdvanced(BUTTON_META, table.CopySimple(game.vgui.types["base_panel"]))
  3. BUTTON_META = table.MergeAdvanced(
  4.     BUTTON_META,
  5.     {
  6.         colors =
  7.         {
  8.             color(100, 100, 100, 255),
  9.             color(255, 255, 255, 255),
  10.             color(0, 155, 255, 255),
  11.             color(0,0,0,255)
  12.         },
  13.         prev_color  = color(255, 255, 255),
  14.         _status     = 1,
  15.         tggl_status = false,
  16.         is_toggled  = false
  17.     })
  18.  
  19.  
  20.  
  21. function BUTTON_META:OnInit()
  22.    
  23.     self:OnSkin()
  24.     self:SetBorder(true)
  25.  
  26. end
  27.  
  28. function BUTTON_META:SetColor(_col1, _col2, _col3, _col4)
  29.  
  30.     self.colors[1] = _col1 or color(100, 100, 100, 255)
  31.     self.color     = self.colors[1]
  32.     self.colors[2] = _col2 or color(255, 255, 255, 255)
  33.     self.colors[3] = _col3 or color(177.5, 177.5, 177.5, 255)
  34.     self.colors[4] = _col4 or color(0, 0, 0, 255)
  35.  
  36. end
  37.  
  38. function BUTTON_META:SetToggle(_bool)
  39.  
  40.     self.is_toggled = type(_bool) == "boolean" and _bool or false
  41.  
  42. end
  43.  
  44. function BUTTON_META:GetColorArray()
  45.  
  46.     return self.colors
  47.  
  48. end
  49.  
  50. function BUTTON_META:DoFunction()
  51.  
  52.     --self:SetPos(self.orig_pos.x+25,0)
  53.  
  54. end
  55.  
  56. function BUTTON_META:OnMouseEntered()
  57.  
  58. end
  59.  
  60. function BUTTON_META:OnMouseExited()
  61.  
  62. end
  63.  
  64. function BUTTON_META:OnMousePressed()
  65.  
  66. end
  67.  
  68. function BUTTON_META:OnMouseReleased()
  69.  
  70. end
  71.  
  72. function BUTTON_META:OnColor()
  73.  
  74.     local _color_to = color()
  75.  
  76.     if not self.is_toggled then
  77.  
  78.         if self._status == 1 or self._status == 4 then --Exited
  79.            
  80.             if _color_to ~= self.colors[1] then
  81.  
  82.                 _color_to       = self.colors[1]
  83.                 self.prev_color = self.color
  84.  
  85.             end
  86.        
  87.         elseif self._status == 2 then --Entered
  88.  
  89.             if _color_to ~= self.colors[2] then
  90.  
  91.                 _color_to       = self.colors[2]
  92.                 self.prev_color = self.color
  93.  
  94.             end
  95.  
  96.         elseif self._status == 3 then --Pressed
  97.  
  98.             if _color_to ~= self.colors[3] then
  99.  
  100.                 _color_to       = self.colors[3]
  101.                 self.prev_color = self.color
  102.  
  103.             end    
  104.  
  105.         end
  106.  
  107.     else
  108.  
  109.         if self.tggl_status and _color_to ~= self.colors[3] then
  110.  
  111.             _color_to       = self.colors[3]
  112.             self.prev_color = self.color           
  113.  
  114.         elseif _color_to ~= self.colors[2] then
  115.  
  116.             _color_to       = self.colors[2]
  117.             self.prev_color = self.color       
  118.  
  119.         end
  120.  
  121.     end
  122.  
  123.     if self.color ~= _color_to then
  124.  
  125.         self.color      = self.prev_color+(_color_to-self.prev_color)/100
  126.         self.prev_color = self.color
  127.  
  128.     end
  129.  
  130. end
  131.  
  132. function BUTTON_META:OnMouse()
  133.  
  134.     if (self.global_owner and self.global_owner.is_enabled == true) and self.is_enabled then
  135.  
  136.         local _in_region = self:MouseInRegion()
  137.         local _l_mouse   = love.mouse.isDown("l")
  138.        
  139.         if not _in_region then --Normal
  140.  
  141.             if self._status ~= 1 then
  142.  
  143.                 self._status = 1
  144.                 self:OnMouseExited()
  145.  
  146.             end
  147.  
  148.         else
  149.  
  150.             if not _l_mouse then --Entered
  151.  
  152.                 if self._status ~= 4 and self._status == 3 then --Released
  153.  
  154.                     if self.is_toggled then self.tggl_status = not self.tggl_status end
  155.  
  156.                     self._status = 4
  157.                     self:OnMouseReleased()
  158.                     self:DoFunction()
  159.  
  160.                 elseif self._status ~= 2 then
  161.  
  162.                     self._status = 2
  163.                     self:OnMouseEntered()
  164.  
  165.                 end
  166.  
  167.             elseif _l_mouse and self._status ~= 3 and self._status == 2 then --Pressed
  168.  
  169.                 self._status = 3
  170.                 self:OnMousePressed()
  171.  
  172.             end
  173.  
  174.         end
  175.  
  176.         return  
  177.  
  178.     end
  179.  
  180.     self._status = 1
  181.  
  182. end
  183.  
  184. function BUTTON_META:RequiredTick()
  185.  
  186.     self:OnMouse()
  187.     self:OnColor()
  188.  
  189. end
  190.  
  191. game.vgui.Register("base_button",BUTTON_META)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement