Button.cxx 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. #include "Button.hxx"
  2. #include "../AssetManager.hxx"
  3. #include "../../SDLRenderer.hxx"
  4. #include <cassert>
  5. namespace {
  6. SDL_Rect const UPPER_LEFT{
  7. .x = 0,
  8. .y = 0,
  9. .w = 6,
  10. .h = 5,
  11. };
  12. SDL_Rect const UPPER_EDGE{
  13. .x = 6,
  14. .y = 0,
  15. .w = 37,
  16. .h = 5,
  17. };
  18. SDL_Rect const UPPER_RIGHT{
  19. .x = 43,
  20. .y = 0,
  21. .w = 6,
  22. .h = 5,
  23. };
  24. SDL_Rect const LEFT_EDGE{
  25. .x = 0,
  26. .y = 5,
  27. .w = 6,
  28. .h = 35,
  29. };
  30. SDL_Rect const CENTER{
  31. .x = 6,
  32. .y = 5,
  33. .w = 37,
  34. .h = 35,
  35. };
  36. SDL_Rect const RIGHT_EDGE{
  37. .x = 43,
  38. .y = 5,
  39. .w = 6,
  40. .h = 35,
  41. };
  42. SDL_Rect const LOWER_LEFT_UP{
  43. .x = 0,
  44. .y = 40,
  45. .w = 6,
  46. .h = 9,
  47. };
  48. SDL_Rect const LOWER_EDGE_UP{
  49. .x = 6,
  50. .y = 40,
  51. .w = 37,
  52. .h = 9,
  53. };
  54. SDL_Rect const LOWER_RIGHT_UP{
  55. .x = 43,
  56. .y = 40,
  57. .w = 6,
  58. .h = 9,
  59. };
  60. SDL_Rect const LOWER_LEFT_DOWN{
  61. .x = 0,
  62. .y = 40,
  63. .w = 6,
  64. .h = 5,
  65. };
  66. SDL_Rect const LOWER_EDGE_DOWN{
  67. .x = 6,
  68. .y = 40,
  69. .w = 37,
  70. .h = 5,
  71. };
  72. SDL_Rect const LOWER_RIGHT_DOWN{
  73. .x = 43,
  74. .y = 40,
  75. .w = 6,
  76. .h = 5,
  77. };
  78. SDL_Rect calculate_text_rect(SDL_Rect const& area, SDL_Texture* texture)
  79. {
  80. int text_w, text_h;
  81. SDL_QueryTexture(texture, nullptr, nullptr, &text_w, &text_h);
  82. float const text_aspect = static_cast<float>(text_w)/static_cast<float>(text_h);
  83. float const area_aspect = static_cast<float>(area.w)/static_cast<float>(area.h);
  84. int put_w, put_h;
  85. if (text_aspect>area_aspect) {
  86. put_w = area.w;
  87. put_h = static_cast<int>(static_cast<float>(put_w)/text_aspect);
  88. }
  89. else {
  90. put_h = area.h;
  91. put_w = static_cast<int>(static_cast<float>(put_h)*text_aspect);
  92. }
  93. return {
  94. .x = area.x+(area.w-put_w)/2,
  95. .y = area.y+(area.h-put_h)/2,
  96. .w = put_w,
  97. .h = put_h,
  98. };
  99. }
  100. }
  101. Button::Button(std::string title, int x, int y, int w, int h)
  102. :title_{std::move(title)}, x_{x}, y_{y}, w_{w}, h_{h}, pressed_{false}
  103. {
  104. assert(w_>=12);
  105. assert(h_>=14);
  106. up_ = std::async(std::launch::deferred, [] {
  107. return AssetManager::instance().get_texture_asset("blue_button_up.png");
  108. });
  109. down_ = std::async(std::launch::deferred, [] {
  110. return AssetManager::instance().get_texture_asset("blue_button_down.png");
  111. });
  112. font_ = std::async(std::launch::deferred, [] {
  113. return AssetManager::instance().get_font_asset("kenvector_future.ttf");
  114. });
  115. }
  116. void Button::set_pressed(bool const pressed)
  117. {
  118. pressed_ = pressed;
  119. }
  120. bool Button::is_pressed() const
  121. {
  122. return pressed_;
  123. }
  124. void Button::render(SDLRenderer& renderer)
  125. {
  126. auto const text = TTF_RenderUTF8_Solid(font_.get(), title_.c_str(), {0, 0, 0, SDL_ALPHA_OPAQUE});
  127. auto const text_ure = SDL_CreateTextureFromSurface(renderer, text);
  128. SDL_FreeSurface(text);
  129. if (pressed_) {
  130. auto const down = down_.get();
  131. SDL_Rect const upper_left{
  132. .x = x_,
  133. .y = y_+4,
  134. .w = 6,
  135. .h = 5,
  136. };
  137. SDL_RenderCopy(renderer, down, &::UPPER_LEFT, &upper_left);
  138. SDL_Rect const upper_edge{
  139. .x = x_+6,
  140. .y = y_+4,
  141. .w = w_-12,
  142. .h = 5,
  143. };
  144. SDL_RenderCopy(renderer, down, &::UPPER_EDGE, &upper_edge);
  145. SDL_Rect const upper_right{
  146. .x = x_+w_-6,
  147. .y = y_+4,
  148. .w = 6,
  149. .h = 5,
  150. };
  151. SDL_RenderCopy(renderer, down, &::UPPER_RIGHT, &upper_right);
  152. SDL_Rect const left_edge{
  153. .x = x_,
  154. .y = y_+9,
  155. .w = 6,
  156. .h = h_-14,
  157. };
  158. SDL_RenderCopy(renderer, down, &::LEFT_EDGE, &left_edge);
  159. SDL_Rect const center{
  160. .x = x_+6,
  161. .y = y_+9,
  162. .w = w_-12,
  163. .h = h_-14,
  164. };
  165. SDL_RenderCopy(renderer, down, &::CENTER, &center);
  166. SDL_Rect const text_rect = ::calculate_text_rect(center, text_ure);
  167. SDL_RenderCopy(renderer, text_ure, nullptr, &text_rect);
  168. SDL_Rect const right_edge{
  169. .x = x_+w_-6,
  170. .y = y_+9,
  171. .w = 6,
  172. .h = h_-14,
  173. };
  174. SDL_RenderCopy(renderer, down, &::RIGHT_EDGE, &right_edge);
  175. SDL_Rect const lower_left{
  176. .x = x_,
  177. .y = y_+h_-5,
  178. .w = 6,
  179. .h = 5,
  180. };
  181. SDL_RenderCopy(renderer, down, &::LOWER_LEFT_DOWN, &lower_left);
  182. SDL_Rect const lower_edge{
  183. .x = x_+6,
  184. .y = y_+h_-5,
  185. .w = w_-12,
  186. .h = 5,
  187. };
  188. SDL_RenderCopy(renderer, down, &::LOWER_EDGE_DOWN, &lower_edge);
  189. SDL_Rect const lower_right{
  190. .x = x_+w_-6,
  191. .y = y_+h_-5,
  192. .w = 6,
  193. .h = 5,
  194. };
  195. SDL_RenderCopy(renderer, down, &::LOWER_RIGHT_DOWN, &lower_right);
  196. }
  197. else {
  198. auto const up = up_.get();
  199. SDL_Rect const upper_left{
  200. .x = x_,
  201. .y = y_,
  202. .w = 6,
  203. .h = 5,
  204. };
  205. SDL_RenderCopy(renderer, up, &::UPPER_LEFT, &upper_left);
  206. SDL_Rect const upper_edge{
  207. .x = x_+6,
  208. .y = y_,
  209. .w = w_-12,
  210. .h = 5,
  211. };
  212. SDL_RenderCopy(renderer, up, &::UPPER_EDGE, &upper_edge);
  213. SDL_Rect const upper_right{
  214. .x = x_+w_-6,
  215. .y = y_,
  216. .w = 6,
  217. .h = 5,
  218. };
  219. SDL_RenderCopy(renderer, up, &::UPPER_RIGHT, &upper_right);
  220. SDL_Rect const left_edge{
  221. .x = x_,
  222. .y = y_+5,
  223. .w = 6,
  224. .h = h_-14,
  225. };
  226. SDL_RenderCopy(renderer, up, &::LEFT_EDGE, &left_edge);
  227. SDL_Rect const center{
  228. .x = x_+6,
  229. .y = y_+5,
  230. .w = w_-12,
  231. .h = h_-14,
  232. };
  233. SDL_RenderCopy(renderer, up, &::CENTER, &center);
  234. SDL_Rect const text_rect = ::calculate_text_rect(center, text_ure);
  235. SDL_RenderCopy(renderer, text_ure, nullptr, &text_rect);
  236. SDL_Rect const right_edge{
  237. .x = x_+w_-6,
  238. .y = y_+5,
  239. .w = 6,
  240. .h = h_-14,
  241. };
  242. SDL_RenderCopy(renderer, up, &::RIGHT_EDGE, &right_edge);
  243. SDL_Rect const lower_left{
  244. .x = x_,
  245. .y = y_+h_-9,
  246. .w = 6,
  247. .h = 9,
  248. };
  249. SDL_RenderCopy(renderer, up, &::LOWER_LEFT_UP, &lower_left);
  250. SDL_Rect const lower_edge{
  251. .x = x_+6,
  252. .y = y_+h_-9,
  253. .w = w_-12,
  254. .h = 9,
  255. };
  256. SDL_RenderCopy(renderer, up, &::LOWER_EDGE_UP, &lower_edge);
  257. SDL_Rect const lower_right{
  258. .x = x_+w_-6,
  259. .y = y_+h_-9,
  260. .w = 6,
  261. .h = 9,
  262. };
  263. SDL_RenderCopy(renderer, up, &::LOWER_RIGHT_UP, &lower_right);
  264. }
  265. SDL_DestroyTexture(text_ure);
  266. }