Example #1
0
/*
Check if the mouse is interacting with a single button. Mouse input can be left
clicked or it can be mouse motion. The state of a button will change based on
mouse interaction. Button will have a hover state if mouse is over it. Button
can have a held and pressed state if mouse is clicked over it. Button can have
a relased state if mouse button is release over it.

@param button the button to check for state modification
@return void
*/
void ButtonHandler::ModifyButtons(Button& button) {
  // On mouse movement.
  if (mouseEvent.motion) {
    SDL_Rect mouseRect = {mouseEvent.x, mouseEvent.y, 0, 0};
    SDL_Rect buttonRect = {button.x, button.y, button.w, button.h};
    ScaleButton(buttonRect.w, buttonRect.h);

    if (phyiscs.Collision(mouseRect, buttonRect)) {
      if (button.GetState() != ButtonState::HELD) {
        button.SetState(ButtonState::HOVER);
      }
    }
    else {
      button.SetState(ButtonState::NONE);
    }
  }
  // On mouse pressed.
  else if (mouseEvent.leftButtonDown) {
    SDL_Rect mouseRect = {mouseEvent.x, mouseEvent.y, 0, 0};
    SDL_Rect buttonRect = {button.x, button.y, button.w, button.h};
    ScaleButton(buttonRect.w, buttonRect.h);

    if (phyiscs.Collision(mouseRect, buttonRect)) {
      if (button.GetState() == ButtonState::PRESSED ||
        button.GetState() == ButtonState::HELD) {
        button.SetState(ButtonState::HELD);
      }
      else {
        button.SetState(ButtonState::PRESSED);
      }
    }
  }
  // On mouse release.
  else if (mouseEvent.leftButtonUp) {
    SDL_Rect mouseRect = {mouseEvent.x, mouseEvent.y, 0, 0};
    SDL_Rect buttonRect = {button.x, button.y, button.w, button.h};
    ScaleButton(buttonRect.w, buttonRect.h);

    if (phyiscs.Collision(mouseRect, buttonRect)) {
      if (button.GetState() == ButtonState::RELEASED ||
        button.GetState() == ButtonState::HOVER) {
        button.SetState(ButtonState::HOVER);
      }
      else {
        button.SetState(ButtonState::RELEASED);
      }
    }
  }
}
Example #2
0
/*
Button actions are performed based on the current state of a button and the 
current action associated with it. Button action are created in button actions.
Button state to action relationship is choosen in the .tmx file under each 
button property.

@param button the button with action that is going to be performed
@return void
*/
void ButtonHandler::ButtonActions(Button& button) {
  button.actions[(int) button.GetState()](button, this->msgHandler);
}