暂无描述
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

button.cpp 935B

123456789101112131415161718192021222324252627282930313233343536
  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. void 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. }
  25. }
  26. void Button::Draw()
  27. {
  28. auto sw = 8 * text.size();
  29. auto sh = 8;
  30. game->DrawRect(pos.x, pos.y, size.x, size.y, hover ? olc::WHITE : olc::GREY);
  31. game->DrawString(pos.x + (size.x - sw) / 2, pos.y + (size.y - sh) / 2, text);
  32. }
  33. }