jseely

Buttons

Mar 16th, 2013
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.26 KB | None | 0 0
  1. -- BEGIN Point class
  2.  
  3. Point = { }
  4. Point.__index = Point
  5.  
  6. function Point.create( x, y )
  7.   assert( type( x ) == "number", "Point.create() expects argument 'x' to be number or nil." )
  8.   assert( type( y ) == "number", "Point.create() expects argument 'y' to be number or nil." )
  9.  
  10.   local pt = { }
  11.   setmetatable( pt, Point )
  12.  
  13.   pt.x = x
  14.   pt.y = y
  15.  
  16.   return pt
  17. end
  18.  
  19. function Point:setX( x )
  20.   assert( type( x ) == "number", "Point.setX() expects a number or nil." )
  21.   self.x = x
  22. end
  23.  
  24. function Point:setY( y )
  25.   assert( type( y ) == "number", "Point.setY() expects a number or nil." )
  26.   self.y = y
  27. end
  28.  
  29. function Point:getX()
  30.   return self.x
  31. end
  32.  
  33. function Point:getY()
  34.   return self.y
  35. end
  36.  
  37. -- END Point class
  38.  
  39. -- BEGIN Button class
  40.  
  41. Button = { }
  42. Button.__index = Button
  43.  
  44. function Button.create( location, text, event_msg, text_color, back_color )
  45.   assert( type( location  ) == "Point",  "Button constructor expects a Point as first argument."      )
  46.   assert( type( text      ) == "string", "Button constructor expects a string as second argument."    )
  47.   assert( type( event_msg  ) == "string", "Button constructor expects a string as third argument."      )
  48.   assert( type( text_color ) == "string", "Button constructor expects a string as the fourth argument." )
  49.   assert( type( back_color ) == "string", "Button constructor expects a string as the fifth argument."  )
  50.  
  51.   local btn = { }
  52.   setmetatable( btn, Button )
  53.  
  54.   btn.loc:setX( location:getX() )
  55.   brn.loc:setY( location:getY() )
  56.  
  57.   btn.text      = text
  58.   btn.evt_msg    = event_msg -- Message to be passed along whenever Button is clicked
  59.   btn.text_color = text_color
  60.   btn.back_color = back_color
  61.  
  62.   return btn
  63. end
  64.  
  65. function Button:setLoc( location )
  66.   assert( type( location ) == "Point", "Button:setLoc() expects a Point." )
  67.  
  68.   self.loc:setX( location:getX() )
  69.   self.loc:setY( location:getY() )
  70. end
  71.  
  72. function Button:getLoc()
  73.   return self.loc
  74. end
  75.  
  76. function Button:setText( txt )
  77.   assert( type( txt ) == "string", "Button:setText() expects a string." )
  78.   self.text = txt
  79. end
  80.  
  81. function Button:getText()
  82.   return self.text
  83. end
  84.  
  85. function Button:setEventMessage( msg )
  86.   assert( type( msg ) == "string", "Button:setEventMessage() expects a string." )
  87.   self.evt_msg = msg
  88. end
  89.  
  90. function Button:getEventMessage()
  91.   return self.evt_msg
  92. end
  93.  
  94. button_table = { }
  95. table_size  = 0
  96.  
  97. function SearchButtonTableByText( text )
  98.   assert( type( text ) == "string", "SearchButtonTableByText() expects a string."  )
  99.  
  100.   if ( table_size < 1 ) then
  101.     return false
  102.   else if ( table_size == 1 ) then
  103.     if ( button_table[1]:getText() == text ) then
  104.       return true
  105.     else
  106.       return false
  107.     end
  108.   else
  109.     for i = 1, table_size do
  110.       if ( button_table[i]:getText() == text ) then
  111.         return true
  112.       end
  113.     end
  114.     return false
  115.   end
  116. end
  117.  
  118. function SearchButtonTableByLoc( location )
  119.   assert( type( location ) == "Point", "SearchButtonTableByLoc() expects a Point." )
  120.  
  121.   if ( table_size < 1 ) then
  122.     return false
  123.   else if ( table_size == 1 ) then
  124.     if ( button_table[1]:getLoc() == location ) then
  125.       return true
  126.     else
  127.       return false
  128.     end
  129.   else
  130.     for i = 1, table_size do
  131.       if ( button_table[i]:getLoc() == location ) then
  132.         return true
  133.       end
  134.     end
  135.     return false
  136.   end
  137. end
  138.  
  139. function FindButtonByText( text )
  140.   assert( type( text ) == "string", "FindButtonByText() expects a string." )
  141.  
  142.   if ( table_size < 1 ) then
  143.     return nil
  144.   else if ( table_size == 1 ) then
  145.     if ( button_table[1]:getText() == text ) then
  146.       return 1
  147.     else
  148.       return nil
  149.     end
  150.   else
  151.     for i = 1, table_size do
  152.       if ( button_table[i]:getText() == text ) then
  153.         return i
  154.       end
  155.     end
  156.     return nil
  157.   end
  158. end
  159.  
  160. function FindButtonByLoc( location )
  161.   assert( type( location ) == "Point", "FindButtonByLoc() expects a Point." )
  162.  
  163.   if ( table_size < 1 ) then
  164.     return nil
  165.   else if ( table_size == 1 ) then
  166.     if ( button_table[1]:getLoc() == location ) then
  167.       return 1
  168.     else
  169.       return nil
  170.     end
  171.   else
  172.     for i = 1, table_size do
  173.       if ( button_table[i]:getLoc() == location ) then
  174.         return i
  175.       end
  176.     end
  177.     return nil
  178.   end
  179. end
  180.  
  181. -- AddButton( Button button )
  182. -- Adds a button to the button list, if the text of
  183. -- the passed button doesn't match any other, and if
  184. -- it does not have the same location.
  185. function AddButton( button )
  186.   assert( type( button ) == "Button", "AddButton() expects a Button." )
  187.  
  188.   if ( SearchButtonTableByText( button:getText() ) == false ) then
  189.     if ( SearchButtonTableByLoc( button:getLoc() ) == false ) then
  190.       table.insert( button_list, button )
  191.       table_size = table_size + 1
  192.  
  193.       return true
  194.     else
  195.       return false
  196.     end
  197.   else
  198.     return false
  199.   end
  200. end
  201.  
  202. function RemoveButton( button )
  203.   assert( type( button ) == "Button", "RemoveButton() expects a Button." )
  204.  
  205.   if ( FindByText( button:getText() ) != nil ) then
  206.     if ( FindByLoc( button:getLoc() ) != nil ) then
  207.       table.remove( button_list, FindByText( button:getText() ) )
  208.       table_size = table_size - 1
  209.  
  210.       return true
  211.     else
  212.       return false
  213.     end
  214.   else
  215.     return false
  216.   end
  217. end
Add Comment
Please, Sign In to add comment