Advertisement
MrCoxall

HelperClass

Oct 5th, 2014
2,683
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.99 KB | None | 0 0
  1. -- HelperClass
  2.  
  3. -- Created by: Mr Coxall
  4. -- Created on: Nov 2014
  5. -- Created for: ICS2O
  6. -- This is a collection of classes and a scene management system,
  7. -- to help in the aid of coding.
  8.  
  9.  
  10. -- Button Class
  11.  
  12. -- This class simplifies the process of checking if a sprite button is pressed or not
  13. -- Then you can ask the button if it is being touched or was ended.
  14. -- Works best with vector sprite buttons, since it enlarges them when they are touched
  15. --
  16. -- Parameters to pass in:
  17. --  1. Untouched button image name
  18. --  2. A vector2 that is the location of the button
  19.  
  20. Button = class()
  21.  
  22. function Button:init(buttonImage, buttonPosition)
  23.     -- accepts the button image and location to draw it
  24.    
  25.     self.buttonImage = buttonImage
  26.     self.buttonLocation = buttonPosition
  27.    
  28.     self.buttonTouchScale = 1.15
  29.     self.buttonImageSize = vec2(spriteSize(self.buttonImage))    
  30.     self.currentButtonImage = self.buttonImage
  31.     self.buttonTouchedImage = resizeImage(self.buttonImage, (self.buttonImageSize.x*self.buttonTouchScale), (self.buttonImageSize.y*self.buttonTouchScale))  
  32.     self.selected = false
  33. end
  34.  
  35. function Button:draw()
  36.     -- Codea does not automatically call this method
  37.  
  38.     pushStyle()  
  39.     pushMatrix()
  40.     noFill()
  41.     noSmooth()
  42.     noStroke()
  43.      
  44.     sprite(self.currentButtonImage, self.buttonLocation.x, self.buttonLocation.y)
  45.    
  46.     popMatrix()
  47.     popStyle()
  48. end
  49.  
  50. function Button:touched(touch)  
  51.     -- local varaibles
  52.     local currentTouchPosition = vec2(touch.x, touch.y)
  53.    
  54.     -- reset touching variable to false
  55.     self.selected = false
  56.    
  57.     if (touch.state == BEGAN) then
  58.          if( (self.buttonLocation.x - self.buttonImageSize.x/2) < currentTouchPosition.x and
  59.             (self.buttonLocation.x + self.buttonImageSize.x/2) > currentTouchPosition.x and
  60.             (self.buttonLocation.y - self.buttonImageSize.y/2) < currentTouchPosition.y and
  61.             (self.buttonLocation.y + self.buttonImageSize.y/2) > currentTouchPosition.y ) then
  62.                
  63.             self.currentButtonImage = self.buttonTouchedImage
  64.             --print("Now touching! - began")
  65.         else          
  66.             self.currentButtonImage = self.buttonImage  
  67.             --print("Not touching - began")
  68.         end            
  69.     end
  70.    
  71.     if (touch.state == MOVING) then
  72.         if( (self.buttonLocation.x - self.buttonImageSize.x/2) < currentTouchPosition.x and
  73.             (self.buttonLocation.x + self.buttonImageSize.x/2) > currentTouchPosition.x and
  74.             (self.buttonLocation.y - self.buttonImageSize.y/2) < currentTouchPosition.y and
  75.             (self.buttonLocation.y + self.buttonImageSize.y/2) > currentTouchPosition.y ) then
  76.        
  77.             self.currentButtonImage = self.buttonTouchedImage
  78.             --print("Now touching! - moving")
  79.         else
  80.             self.currentButtonImage = self.buttonImage  
  81.             --print("Not touching - moving")
  82.         end
  83.     end
  84.    
  85.     if (touch.state == ENDED) then
  86.         if( (self.buttonLocation.x - self.buttonImageSize.x/2) < currentTouchPosition.x and
  87.             (self.buttonLocation.x + self.buttonImageSize.x/2) > currentTouchPosition.x and
  88.             (self.buttonLocation.y - self.buttonImageSize.y/2) < currentTouchPosition.y and
  89.             (self.buttonLocation.y + self.buttonImageSize.y/2) > currentTouchPosition.y ) then
  90.        
  91.             self.selected = true
  92.             --print("Activated button")
  93.         end
  94.          
  95.         self.currentButtonImage = self.buttonImage
  96.     end
  97. end
  98.  
  99. function resizeImage(img, width, height)
  100.     -- function from
  101.     -- http://codea.io/talk/discussion/3490/importing-pics-from-dropbox/p1
  102.    
  103.     local newImg = image(width,height)
  104.     setContext(newImg)
  105.     sprite( img, width/2, height/2, width, height )    
  106.     setContext()
  107.     return newImg
  108. end
  109.  
  110.  
  111. -- Dragging Object Class
  112. -- This class simplifies the process of dragging and dropping objects
  113. -- You can have several objects interacting, but it is not mulit-touch
  114. --
  115. -- Parameters to pass in:
  116. --  1. Object image name
  117. --  2. A vector2 that is the location of the button
  118. --  3. Optional object id
  119.  
  120.  
  121. SpriteObject = class()
  122.  
  123. function SpriteObject:init(objectImage, objectStartPosition, objectID)
  124.  
  125.     self.objectImage = objectImage
  126.     self.objectStartLocation = objectStartPosition
  127.     self.ID = objectID or math.random()
  128.    
  129.     self.objectCurrentLocation = self.objectStartLocation
  130.     self.objectImageSize = vec2(spriteSize(self.objectImage))
  131.     self.selected = false
  132.     self.dragOffset = vec2(0,0)
  133.     self.draggable = true
  134.     -- yes, the following does need to be global to the entire program
  135.     -- this is the only way (easy way!) to move things around and no get
  136.     -- several object "attached" to each other
  137.     -- this way just the top object in the stack moves
  138.     DRAGGING_OBJECT_MOVING = nil or DRAGGING_OBJECT_MOVING    
  139. end
  140.  
  141. function SpriteObject:draw()
  142.     -- Codea does not automatically call this method
  143.    
  144.     pushStyle()  
  145.     pushMatrix()
  146.     noFill()
  147.     noSmooth()
  148.     noStroke()
  149.      
  150.     sprite(self.objectImage, self.objectCurrentLocation.x, self.objectCurrentLocation.y)
  151.      
  152.     popMatrix()
  153.     popStyle()
  154.  
  155. end
  156.  
  157. function SpriteObject:touched(touch)
  158.     -- Codea does not automatically call this method
  159.    
  160.     -- local varaibles
  161.     local currentTouchPosition = vec2(touch.x, touch.y)
  162.    
  163.     -- reset touching variable to false
  164.     self.selected = false
  165.    
  166.     if (touch.state == BEGAN and self.draggable == true) then
  167.         if( (self.objectCurrentLocation.x - self.objectImageSize.x/2) < currentTouchPosition.x and
  168.             (self.objectCurrentLocation.x + self.objectImageSize.x/2) > currentTouchPosition.x and
  169.             (self.objectCurrentLocation.y - self.objectImageSize.y/2) < currentTouchPosition.y and
  170.             (self.objectCurrentLocation.y + self.objectImageSize.y/2) > currentTouchPosition.y ) then
  171.             -- if the touch has began, we need to find delta from touch to center of object
  172.             -- since will need it to reposition the object for draw
  173.             -- subtracting 2 vec2s here
  174.             self.dragOffset = self.objectCurrentLocation - currentTouchPosition
  175.             DRAGGING_OBJECT_MOVING = self.ID
  176.         end        
  177.     end
  178.    
  179.     if (touch.state == MOVING and self.draggable == true) then
  180.         if( (self.objectCurrentLocation.x - self.objectImageSize.x/2) < currentTouchPosition.x and
  181.             (self.objectCurrentLocation.x + self.objectImageSize.x/2) > currentTouchPosition.x and
  182.             (self.objectCurrentLocation.y - self.objectImageSize.y/2) < currentTouchPosition.y and
  183.             (self.objectCurrentLocation.y + self.objectImageSize.y/2) > currentTouchPosition.y ) then
  184.                 -- only let it move if self.draggable == true
  185.             if (self.draggable == true) then
  186.                 -- add the offset back in for its new position
  187.                 if (self.ID == DRAGGING_OBJECT_MOVING) then
  188.                     self.objectCurrentLocation = currentTouchPosition + self.dragOffset
  189.                 end
  190.             end
  191.         end      
  192.     end
  193.    
  194.     if (touch.state == ENDED and self.draggable == true) then
  195.         DRAGGING_OBJECT_MOVING = nil  
  196.     end
  197.    
  198.     -- this checks for if you have just touched the image
  199.     -- you will have to release and re-touch for this to be activated again
  200.     if (touch.state == BEGAN) then
  201.         if( (self.objectCurrentLocation.x - self.objectCurrentLocation.x/2) < currentTouchPosition.x and
  202.             (self.objectCurrentLocation.x + self.objectCurrentLocation.x/2) > currentTouchPosition.x and
  203.             (self.objectCurrentLocation.y - self.objectCurrentLocation.y/2) < currentTouchPosition.y and
  204.             (self.objectCurrentLocation.y + self.objectCurrentLocation.y/2) > currentTouchPosition.y ) then
  205.        
  206.             self.selected = true
  207.             --print("Activated button")
  208.         end
  209.     end
  210. end
  211.  
  212. function SpriteObject:isTouching(otherSpriteObject)
  213.     -- this method checks if one dragging object is touching another dragging object
  214.    
  215.     local isItTouching = false
  216.    
  217.     if( (self.objectCurrentLocation.x + self.objectImageSize.x/2) > (otherSpriteObject.objectCurrentLocation.x - otherSpriteObject.objectImageSize.x/2) and
  218.         (self.objectCurrentLocation.x - self.objectImageSize.x/2) < (otherSpriteObject.objectCurrentLocation.x + otherSpriteObject.objectImageSize.x/2) and
  219.         (self.objectCurrentLocation.y - self.objectImageSize.y/2) < (otherSpriteObject.objectCurrentLocation.y + otherSpriteObject.objectImageSize.y/2) and
  220.         (self.objectCurrentLocation.y + self.objectImageSize.y/2) > (otherSpriteObject.objectCurrentLocation.y - otherSpriteObject.objectImageSize.y/2) ) then
  221.         -- if true, then not touching
  222.         isItTouching = true
  223.     end        
  224.    
  225.     return isItTouching
  226. end
  227.  
  228.  
  229. -- SceneManager
  230. --
  231. -- This file lets you easily manage different scenes
  232. --     Original code from Brainfox, off the Codea forums
  233.  
  234. Scene = {}
  235. local scenes = {}
  236. local sceneNames = {}
  237. local currentScene = nil
  238.  
  239. setmetatable(Scene,{__call = function(_,name,cls)
  240.    if (not currentScene) then
  241.        currentScene = 1
  242.    end
  243.    table.insert(scenes,cls)
  244.    sceneNames[name] = #scenes
  245.    Scene_Select = nil
  246. end})
  247.  
  248. --Change scene
  249. Scene.Change = function(name)
  250.   currentScene = sceneNames[name]
  251.     scenes[currentScene]:init()
  252.    if (Scene_Select) then
  253.        Scene_Select = currentScene
  254.    end
  255.    
  256.    collectgarbage()
  257. end
  258.  
  259. Scene.Draw = function()
  260.    pushStyle()
  261.    pushMatrix()
  262.    scenes[currentScene]:draw()
  263.    popMatrix()
  264.    popStyle()
  265. end
  266.  
  267. Scene.Touched = function(t)
  268.    if (scenes[currentScene].touched) then
  269.        scenes[currentScene]:touched(t)
  270.    end
  271. end
  272.  
  273. Scene.Keyboard = function()
  274.    if (scenes[currentScene].keyboard) then
  275.        scenes[currentScene]:keyboard(key)
  276.    end
  277. end
  278.  
  279. Scene.OrientationChanged = function()
  280.    if (scenes[currentScene].orientationChanged) then
  281.        scenes[currentScene]:orientationChanged()
  282.    end
  283. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement