Nessuna descrizione
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

button.cpp 1006B

1234567891011121314151617181920212223242526272829303132333435363738
  1. #include "button.h"
  2. #include "debuggers.h"
  3. namespace pabloader {
  4. Button::Button(Debuggers* game, int32_t x, int32_t y, int32_t w, int32_t h, std::string text, GameState state)
  5. : game(game)
  6. , pos(x, y)
  7. , size(w, h)
  8. , text(text)
  9. , state(state)
  10. {
  11. }
  12. bool Button::Update()
  13. {
  14. auto x = game->GetMouseX();
  15. auto y = game->GetMouseY();
  16. auto newHover = x >= pos.x && x <= pos.x + size.x && y >= pos.y && y <= pos.y + size.y;
  17. if (!hover && newHover) {
  18. olc::SOUND::PlaySample(game->hoverSample);
  19. }
  20. hover = newHover;
  21. if (hover && game->GetMouse(0).bPressed) {
  22. olc::SOUND::PlaySample(game->clickSample);
  23. game->SwitchState(state);
  24. return true;
  25. }
  26. return false;
  27. }
  28. void Button::Draw()
  29. {
  30. auto sw = 8 * text.size();
  31. auto sh = 8;
  32. game->DrawRect(pos.x, pos.y, size.x, size.y, hover ? olc::WHITE : olc::GREY);
  33. game->DrawString(pos.x + (size.x - sw) / 2, pos.y + (size.y - sh) / 2, text, hover ? olc::WHITE : olc::GREY);
  34. }
  35. }