Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "input.h"
- #include "constants.h"
- Input::Input()
- {
- CurrentMousex = 0;
- CurrentMousey = 0;
- IsLeftClickHeldDown = false;
- IsRightClickHeldDown = false;
- DidWeJustClickLeftClick = false;
- TurningCellsOnOrOff = TURNING_CELLS_ON;
- ShouldWeUpdateTurningCellsOnOrOff = 0;
- }
- void Input::readInput(bool &quit, Timer &Time)
- {
- DidWeJustClickLeftClick = false;
- //While there's events to handle
- while (SDL_PollEvent (&event))
- {
- if (event.type == SDL_MOUSEBUTTONDOWN)
- {
- if (event.button.button == SDL_BUTTON_LEFT)
- {
- DidWeJustClickLeftClick = true;
- IsLeftClickHeldDown = true;
- }
- else if (event.button.button == SDL_BUTTON_RIGHT)
- {
- IsRightClickHeldDown = true;
- }
- }
- if (event.type == SDL_MOUSEBUTTONUP)
- {
- if (event.button.button == SDL_BUTTON_LEFT)
- {
- IsLeftClickHeldDown = false;
- ShouldWeUpdateTurningCellsOnOrOff = true;
- }
- if (event.button.button == SDL_BUTTON_RIGHT)
- {
- IsRightClickHeldDown = false;
- ShouldWeUpdateTurningCellsOnOrOff = true;
- }
- }
- if (event.type == SDL_MOUSEMOTION)
- {
- //Get new xy position for mouse
- CurrentMousex = event.motion.x;
- CurrentMousey = event.motion.y;
- }
- if( event.type == SDL_KEYDOWN )
- {
- switch( event.key.keysym.sym )
- {
- case SDLK_SPACE:
- if (Time.is_paused() == true)
- Time.unpause();
- else
- Time.pause();
- break;
- }
- }
- //If the user has Xed out the window
- if (event.type == SDL_QUIT)
- quit = true;
- }
- }
- void Input::handleMouseInput(Board &BoardObject, Timer &Time, Menu &aMenuObject)
- {
- handleMenuInput(aMenuObject, Time);
- if (IsLeftClickHeldDown == true || IsRightClickHeldDown == true)
- {
- SDL_Rect BoundingRegion;
- int counter = 0;
- while (counter < NUMBER_OF_CELLS_ON_GRID)
- {
- BoundingRegion = BoardObject.getCell(counter);
- if (((CurrentMousex > BoundingRegion.x) && (CurrentMousex < BoundingRegion.x + BoundingRegion.w) && (CurrentMousey > BoundingRegion.y) && (CurrentMousey < BoundingRegion.y + BoundingRegion.h)))
- {
- //to make it so cells will only go on or off per each mouse hold
- if (BoardObject.getIsCellOccupied(counter) == true && ShouldWeUpdateTurningCellsOnOrOff == true)
- {
- TurningCellsOnOrOff = TURNING_CELLS_OFF;
- ShouldWeUpdateTurningCellsOnOrOff = false;
- }
- else if (BoardObject.getIsCellOccupied(counter) == false && ShouldWeUpdateTurningCellsOnOrOff == true)
- {
- TurningCellsOnOrOff = TURNING_CELLS_ON;
- ShouldWeUpdateTurningCellsOnOrOff = false;
- }
- if (TurningCellsOnOrOff == TURNING_CELLS_OFF)
- {
- BoardObject.setIsCellOccupied(counter, false);
- BoardObject.setColor(counter, NO_COLOR);
- }
- else if (TurningCellsOnOrOff == TURNING_CELLS_ON)
- {
- BoardObject.setIsCellOccupied(counter, true);
- if (IsLeftClickHeldDown == true)
- BoardObject.setColor(counter, GREEN);
- if (IsRightClickHeldDown == true)
- BoardObject.setColor(counter, RED);
- }
- counter = NUMBER_OF_CELLS_ON_GRID;
- }
- counter++;
- }
- }
- }
- void Input::handleMenuInput(Menu &aMenuObject, Timer &Time)
- {
- int counter = 0;
- SDL_Rect BoundingRegion;
- if (DidWeJustClickLeftClick == true)
- {
- aMenuObject.setDisplaySpeedMenu(false);
- BoundingRegion = aMenuObject.getButtonArea(counter);
- while (counter < NUMBER_OF_BUTTONS)
- {
- BoundingRegion = aMenuObject.getButtonArea(counter);
- if (((CurrentMousex > BoundingRegion.x) && (CurrentMousex < BoundingRegion.x + BoundingRegion.w) && (CurrentMousey > BoundingRegion.y) && (CurrentMousey < BoundingRegion.y + BoundingRegion.h)))
- {
- aMenuObject.setIsButtonPressed(counter, 1);
- aMenuObject.enactMenuChoice(Time);
- }
- counter++;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement