Suby

input.cpp

May 25th, 2012
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.96 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.     TurningCellsOnOrOff = TURNING_CELLS_ON;
  10.     ShouldWeUpdateTurningCellsOnOrOff = 0;
  11. }
  12.  
  13. void Input::readInput(bool &quit, Timer &Time)
  14. {
  15.     //While there's events to handle
  16.     while (SDL_PollEvent (&event))
  17.     {  
  18.         if (event.type == SDL_MOUSEBUTTONDOWN)
  19.         {
  20.             if (event.button.button == SDL_BUTTON_LEFT)
  21.             {
  22.                 IsLeftClickHeldDown = true;
  23.             }
  24.             else if (event.button.button == SDL_BUTTON_RIGHT)
  25.             {
  26.  
  27.                 IsRightClickHeldDown = true;
  28.             }
  29.  
  30.         }
  31.  
  32.         if (event.type == SDL_MOUSEBUTTONUP)
  33.         {
  34.             if (event.button.button == SDL_BUTTON_LEFT)
  35.             {
  36.                 IsLeftClickHeldDown = false;
  37.                 ShouldWeUpdateTurningCellsOnOrOff = true;
  38.             }
  39.            
  40.             if (event.button.button == SDL_BUTTON_RIGHT)
  41.             {
  42.                 IsRightClickHeldDown = false;
  43.                 ShouldWeUpdateTurningCellsOnOrOff = true;
  44.             }
  45.         }
  46.         if (event.type == SDL_MOUSEMOTION)
  47.         {
  48.             //Get new xy position for mouse
  49.             CurrentMousex = event.motion.x;
  50.             CurrentMousey = event.motion.y;
  51.         }
  52.        
  53.         if( event.type == SDL_KEYDOWN )    
  54.         {
  55.             switch( event.key.keysym.sym )
  56.             {
  57.                 case SDLK_SPACE:
  58.                     if (Time.is_paused() == true)
  59.                         Time.unpause();
  60.                     else
  61.                         Time.pause();
  62.                     break;
  63.             }
  64.         }
  65.    
  66.         //If the user has Xed out the window
  67.         if (event.type == SDL_QUIT)
  68.             quit = true;
  69.     }          
  70. }
  71.  
  72. void Input::handleMouseInput(Board &BoardObject, Timer Time)
  73. {
  74.     if (IsLeftClickHeldDown == true || IsRightClickHeldDown == true)
  75.     {
  76.         if (Time.is_paused() == true)
  77.         {
  78.             SDL_Rect BoundingRegion;
  79.             int counter = 0;
  80.             while (counter < NUMBER_OF_CELLS_ON_GRID)
  81.             {
  82.                 BoundingRegion = BoardObject.getCell(counter); 
  83.                 if (((CurrentMousex > BoundingRegion.x) && (CurrentMousex < BoundingRegion.x + BoundingRegion.w) && (CurrentMousey > BoundingRegion.y) && (CurrentMousey < BoundingRegion.y + BoundingRegion.h)))
  84.                 {
  85.                     //to make it so cells will only go on or off per each mouse hold
  86.                     if (BoardObject.getIsCellOccupied(counter) == true && ShouldWeUpdateTurningCellsOnOrOff == true)
  87.                     {
  88.                         TurningCellsOnOrOff = TURNING_CELLS_OFF;
  89.                          ShouldWeUpdateTurningCellsOnOrOff = false;
  90.                     }
  91.                     else if (BoardObject.getIsCellOccupied(counter) == false && ShouldWeUpdateTurningCellsOnOrOff == true)
  92.                     {
  93.                         TurningCellsOnOrOff = TURNING_CELLS_ON;
  94.                         ShouldWeUpdateTurningCellsOnOrOff = false;
  95.                     }
  96.                    
  97.                    
  98.                     if (TurningCellsOnOrOff == TURNING_CELLS_OFF)
  99.                     {
  100.                         BoardObject.setIsCellOccupied(counter, false);
  101.                     }
  102.                     else if (TurningCellsOnOrOff == TURNING_CELLS_ON)
  103.                     {
  104.                         BoardObject.setIsCellOccupied(counter, true);
  105.                        
  106.                         if (IsLeftClickHeldDown == true)
  107.                             BoardObject.setColor(counter, GREEN);
  108.                         if (IsRightClickHeldDown == true)
  109.                             BoardObject.setColor(counter, RED);
  110.                     }
  111.  
  112.                     counter = NUMBER_OF_CELLS_ON_GRID;
  113.                 }
  114.                 counter++;
  115.             }
  116.         }
  117.     }
  118. }
Advertisement
Add Comment
Please, Sign In to add comment