Button.cxx 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. #include "Button.hxx"
  2. #include "../../SDLRenderer.hxx"
  3. #include <cassert>
  4. namespace {
  5. SDL_Rect const TEXTURE_RECTS_UP[] = {
  6. {.x = 0, .y = 0, .w = 6, .h = 5}, // UPPER_LEFT
  7. {.x = 6, .y = 0, .w = 37, .h = 5}, // UPPER_EDGE
  8. {.x = 43, .y = 0, .w = 6, .h = 5}, // UPPER_RIGHT
  9. {.x = 0, .y = 5, .w = 6, .h = 35}, // LEFT_EDGE
  10. {.x = 6, .y = 5, .w = 37, .h = 35}, // CENTER
  11. {.x = 43, .y = 5, .w = 6, .h = 35}, // RIGHT_EDGE
  12. {.x = 0, .y = 40, .w = 6, .h = 9}, // LOWER_LEFT
  13. {.x = 6, .y = 40, .w = 37, .h = 9}, // LOWER_EDGE
  14. {.x = 43, .y = 40, .w = 6, .h = 9}, // LOWER_RIGHT
  15. };
  16. SDL_Rect const TEXTURE_RECTS_DOWN[] = {
  17. {.x = 0, .y = 0, .w = 6, .h = 5}, // UPPER_LEFT
  18. {.x = 6, .y = 0, .w = 37, .h = 5}, // UPPER_EDGE
  19. {.x = 43, .y = 0, .w = 6, .h = 5}, // UPPER_RIGHT
  20. {.x = 0, .y = 5, .w = 6, .h = 35}, // LEFT_EDGE
  21. {.x = 6, .y = 5, .w = 37, .h = 35}, // CENTER
  22. {.x = 43, .y = 5, .w = 6, .h = 35}, // RIGHT_EDGE
  23. {.x = 0, .y = 40, .w = 6, .h = 5}, // LOWER_LEFT
  24. {.x = 6, .y = 40, .w = 37, .h = 5}, // LOWER_EDGE
  25. {.x = 43, .y = 40, .w = 6, .h = 5}, // LOWER_RIGHT
  26. };
  27. SDL_Rect calculate_text_rect(SDL_Rect const& area, SDL_Texture* texture)
  28. {
  29. int text_w, text_h;
  30. SDL_QueryTexture(texture, nullptr, nullptr, &text_w, &text_h);
  31. float const text_aspect = static_cast<float>(text_w)/static_cast<float>(text_h);
  32. float const area_aspect = static_cast<float>(area.w)/static_cast<float>(area.h);
  33. int put_w, put_h;
  34. if (text_aspect>area_aspect) {
  35. put_w = area.w;
  36. put_h = static_cast<int>(static_cast<float>(put_w)/text_aspect);
  37. }
  38. else {
  39. put_h = area.h;
  40. put_w = static_cast<int>(static_cast<float>(put_h)*text_aspect);
  41. }
  42. return {
  43. .x = area.x+(area.w-put_w)/2,
  44. .y = area.y+(area.h-put_h)/2,
  45. .w = put_w,
  46. .h = put_h,
  47. };
  48. }
  49. }
  50. Button::Button(std::string title, int const x, int const y, int const w, int const h, UiColor const color)
  51. :title_{std::move(title)}, x_{x}, y_{y}, w_{w}, h_{h}, pressed_{false},
  52. visible_{true},
  53. up_{ui_image("button_up", color)},
  54. down_{ui_image("button_down", color)},
  55. font_{"kenney_pixel.ttf"}
  56. {
  57. assert(w_>=MIN_WIDTH);
  58. assert(h_>=MIN_HEIGHT);
  59. on_click_ = [] { };
  60. }
  61. void Button::set_pressed(bool const pressed)
  62. {
  63. pressed_ = pressed;
  64. }
  65. bool Button::is_pressed() const
  66. {
  67. return pressed_;
  68. }
  69. void Button::render(SDLRenderer& renderer)
  70. {
  71. if (!visible_)
  72. return;
  73. auto const text = TTF_RenderUTF8_Solid(font_, title_.c_str(), {0, 0, 0, SDL_ALPHA_OPAQUE});
  74. auto const text_ure = SDL_CreateTextureFromSurface(renderer, text);
  75. SDL_FreeSurface(text);
  76. SDL_Texture* texture;
  77. SDL_Rect const* texture_rects;
  78. SDL_Rect target_rects[9];
  79. if (pressed_) {
  80. texture = down_;
  81. texture_rects = ::TEXTURE_RECTS_DOWN;
  82. target_rects[0] = {.x = x_, .y = y_+4, .w = 6, .h = 5};
  83. target_rects[1] = {.x = x_+6, .y = y_+4, .w = w_-12, .h = 5};
  84. target_rects[2] = {.x = x_+w_-6, .y = y_+4, .w = 6, .h = 5};
  85. target_rects[3] = {.x = x_, .y = y_+9, .w = 6, .h = h_-14};
  86. target_rects[4] = {.x = x_+6, .y = y_+9, .w = w_-12, .h = h_-14};
  87. target_rects[5] = {.x = x_+w_-6, .y = y_+9, .w = 6, .h = h_-14};
  88. target_rects[6] = {.x = x_, .y = y_+h_-5, .w = 6, .h = 5};
  89. target_rects[7] = {.x = x_+6, .y = y_+h_-5, .w = w_-12, .h = 5};
  90. target_rects[8] = {.x = x_+w_-6, .y = y_+h_-5, .w = 6, .h = 5};
  91. }
  92. else {
  93. texture = up_;
  94. texture_rects = ::TEXTURE_RECTS_UP;
  95. target_rects[0] = {.x = x_, .y = y_, .w = 6, .h = 5};
  96. target_rects[1] = {.x = x_+6, .y = y_, .w = w_-12, .h = 5};
  97. target_rects[2] = {.x = x_+w_-6, .y = y_, .w = 6, .h = 5};
  98. target_rects[3] = {.x = x_, .y = y_+5, .w = 6, .h = h_-14};
  99. target_rects[4] = {.x = x_+6, .y = y_+5, .w = w_-12, .h = h_-14};
  100. target_rects[5] = {.x = x_+w_-6, .y = y_+5, .w = 6, .h = h_-14};
  101. target_rects[6] = {.x = x_, .y = y_+h_-9, .w = 6, .h = 9};
  102. target_rects[7] = {.x = x_+6, .y = y_+h_-9, .w = w_-12, .h = 9};
  103. target_rects[8] = {.x = x_+w_-6, .y = y_+h_-9, .w = 6, .h = 9};
  104. }
  105. for (int n = 0; n<9; ++n)
  106. SDL_RenderCopy(renderer, texture, texture_rects+n, target_rects+n);
  107. SDL_Rect const text_rect = ::calculate_text_rect(target_rects[4], text_ure);
  108. SDL_RenderCopy(renderer, text_ure, nullptr, &text_rect);
  109. SDL_DestroyTexture(text_ure);
  110. }
  111. void Button::update()
  112. {
  113. if (!visible_)
  114. return;
  115. int mouse_x, mouse_y;
  116. auto const mouse_button = SDL_GetMouseState(&mouse_x, &mouse_y);
  117. if ((mouse_x>=x_ && mouse_x<=(x_+w_) && mouse_y>=y_ && mouse_y<=(y_+h_))) {
  118. if (mouse_button & SDL_BUTTON(SDL_BUTTON_LEFT)) {
  119. pressed_ = true;
  120. }
  121. else {
  122. if (pressed_) {
  123. trigger();
  124. }
  125. pressed_ = false;
  126. }
  127. }
  128. else {
  129. pressed_ = false;
  130. }
  131. }
  132. void Button::trigger()
  133. {
  134. on_click_();
  135. }
  136. void Button::set_on_click(std::function<void()> handler)
  137. {
  138. on_click_ = std::move(handler);
  139. }
  140. void Button::move(int const x, int const y)
  141. {
  142. x_ = x;
  143. y_ = y;
  144. }
  145. void Button::resize(int const w, int const h)
  146. {
  147. assert(w>=MIN_WIDTH);
  148. assert(h>=MIN_HEIGHT);
  149. w_ = w;
  150. h_ = h;
  151. }
  152. SDL_Rect Button::get_bounding_box() const
  153. {
  154. return {x_, y_, w_, h_};
  155. }
  156. void Button::set_visible(bool const visible)
  157. {
  158. visible_ = visible;
  159. }
  160. bool Button::is_visible() const
  161. {
  162. return visible_;
  163. }