Advertisement
Suby

input.cpp

Jun 1st, 2012
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.80 KB | None | 0 0
  1. #include "input.h"
  2. #include "constants.h"
  3.  
  4. Input::Input()
  5. {
  6.     CurrentMousex = 0;
  7.     CurrentMousey = 0;
  8.     IsLeftClickHeldDown = false;
  9.     IsRightClickHeldDown = false;
  10.     DidWeJustClickLeftClick = false;
  11.     TurningCellsOnOrOff = TURNING_CELLS_ON;
  12.     ShouldWeUpdateTurningCellsOnOrOff = 0;
  13. }
  14.  
  15. void Input::readInput(bool &quit, Timer &Time)
  16. {
  17.     DidWeJustClickLeftClick = false;
  18.     //While there's events to handle
  19.     while (SDL_PollEvent (&event))
  20.     {  
  21.         if (event.type == SDL_MOUSEBUTTONDOWN)
  22.         {
  23.             if (event.button.button == SDL_BUTTON_LEFT)
  24.             {
  25.                 DidWeJustClickLeftClick = true;
  26.                 IsLeftClickHeldDown = true;
  27.             }
  28.             else if (event.button.button == SDL_BUTTON_RIGHT)
  29.             {
  30.  
  31.                 IsRightClickHeldDown = true;
  32.             }
  33.  
  34.         }
  35.  
  36.         if (event.type == SDL_MOUSEBUTTONUP)
  37.         {
  38.             if (event.button.button == SDL_BUTTON_LEFT)
  39.             {
  40.                 IsLeftClickHeldDown = false;
  41.                 ShouldWeUpdateTurningCellsOnOrOff = true;
  42.             }
  43.            
  44.             if (event.button.button == SDL_BUTTON_RIGHT)
  45.             {
  46.                 IsRightClickHeldDown = false;
  47.                 ShouldWeUpdateTurningCellsOnOrOff = true;
  48.             }
  49.         }
  50.         if (event.type == SDL_MOUSEMOTION)
  51.         {
  52.             //Get new xy position for mouse
  53.             CurrentMousex = event.motion.x;
  54.             CurrentMousey = event.motion.y;
  55.         }
  56.        
  57.         if( event.type == SDL_KEYDOWN )    
  58.         {
  59.             switch( event.key.keysym.sym )
  60.             {
  61.                 case SDLK_SPACE:
  62.                     if (Time.is_paused() == true)
  63.                         Time.unpause();
  64.                     else
  65.                         Time.pause();
  66.                     break;
  67.             }
  68.         }
  69.    
  70.         //If the user has Xed out the window
  71.         if (event.type == SDL_QUIT)
  72.             quit = true;
  73.     }          
  74. }
  75.  
  76. void Input::handleMouseInput(Board &BoardObject, Timer &Time, Menu &aMenuObject)
  77. {
  78.     handleMenuInput(aMenuObject, Time);
  79.  
  80.     if (IsLeftClickHeldDown == true || IsRightClickHeldDown == true)
  81.     {
  82.         SDL_Rect BoundingRegion;
  83.         int counter = 0;
  84.         while (counter < NUMBER_OF_CELLS_ON_GRID)
  85.         {
  86.             BoundingRegion = BoardObject.getCell(counter); 
  87.             if (((CurrentMousex > BoundingRegion.x) && (CurrentMousex < BoundingRegion.x + BoundingRegion.w) && (CurrentMousey > BoundingRegion.y) && (CurrentMousey < BoundingRegion.y + BoundingRegion.h)))
  88.             {
  89.                 //to make it so cells will only go on or off per each mouse hold
  90.                 if (BoardObject.getIsCellOccupied(counter) == true && ShouldWeUpdateTurningCellsOnOrOff == true)
  91.                 {
  92.                     TurningCellsOnOrOff = TURNING_CELLS_OFF;
  93.                         ShouldWeUpdateTurningCellsOnOrOff = false;
  94.                 }
  95.                 else if (BoardObject.getIsCellOccupied(counter) == false && ShouldWeUpdateTurningCellsOnOrOff == true)
  96.                 {
  97.                     TurningCellsOnOrOff = TURNING_CELLS_ON;
  98.                     ShouldWeUpdateTurningCellsOnOrOff = false;
  99.                 }
  100.                    
  101.                    
  102.                 if (TurningCellsOnOrOff == TURNING_CELLS_OFF)
  103.                 {
  104.                     BoardObject.setIsCellOccupied(counter, false);
  105.                     BoardObject.setColor(counter, NO_COLOR);
  106.                 }
  107.                 else if (TurningCellsOnOrOff == TURNING_CELLS_ON)
  108.                 {
  109.                     BoardObject.setIsCellOccupied(counter, true);
  110.                        
  111.                     if (IsLeftClickHeldDown == true)
  112.                         BoardObject.setColor(counter, GREEN);
  113.                     if (IsRightClickHeldDown == true)
  114.                         BoardObject.setColor(counter, RED);
  115.                 }
  116.  
  117.                 counter = NUMBER_OF_CELLS_ON_GRID;
  118.             }
  119.             counter++;
  120.         }
  121.     }
  122. }
  123.  
  124. void Input::handleMenuInput(Menu &aMenuObject, Timer &Time)
  125. {
  126.     int counter = 0;
  127.     SDL_Rect BoundingRegion;
  128.     if (DidWeJustClickLeftClick == true)
  129.     {
  130.         aMenuObject.setDisplaySpeedMenu(false);
  131.         BoundingRegion = aMenuObject.getButtonArea(counter);
  132.  
  133.  
  134.         while (counter < NUMBER_OF_BUTTONS)
  135.         {
  136.             BoundingRegion = aMenuObject.getButtonArea(counter);
  137.        
  138.             if (((CurrentMousex > BoundingRegion.x) && (CurrentMousex < BoundingRegion.x + BoundingRegion.w) && (CurrentMousey > BoundingRegion.y) && (CurrentMousey < BoundingRegion.y + BoundingRegion.h)))
  139.             {
  140.                 aMenuObject.setIsButtonPressed(counter, 1);
  141.                 aMenuObject.enactMenuChoice(Time);
  142.             }
  143.  
  144.             counter++;
  145.         }
  146.  
  147.     }
  148.  
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement