Button.cxx 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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. on_click_ = [] { };
  107. up_ = std::async(std::launch::deferred, [] {
  108. return AssetManager::instance().get_texture_asset("blue_button_up.png");
  109. });
  110. down_ = std::async(std::launch::deferred, [] {
  111. return AssetManager::instance().get_texture_asset("blue_button_down.png");
  112. });
  113. font_ = std::async(std::launch::deferred, [] {
  114. return AssetManager::instance().get_font_asset("kenney_pixel.ttf");
  115. });
  116. }
  117. void Button::set_pressed(bool const pressed)
  118. {
  119. pressed_ = pressed;
  120. }
  121. bool Button::is_pressed() const
  122. {
  123. return pressed_;
  124. }
  125. void Button::render(SDLRenderer& renderer)
  126. {
  127. auto const text = TTF_RenderUTF8_Solid(font_.get(), title_.c_str(), {0, 0, 0, SDL_ALPHA_OPAQUE});
  128. auto const text_ure = SDL_CreateTextureFromSurface(renderer, text);
  129. SDL_FreeSurface(text);
  130. if (pressed_) {
  131. auto const down = down_.get();
  132. SDL_Rect const upper_left{
  133. .x = x_,
  134. .y = y_+4,
  135. .w = 6,
  136. .h = 5,
  137. };
  138. SDL_RenderCopy(renderer, down, &::UPPER_LEFT, &upper_left);
  139. SDL_Rect const upper_edge{
  140. .x = x_+6,
  141. .y = y_+4,
  142. .w = w_-12,
  143. .h = 5,
  144. };
  145. SDL_RenderCopy(renderer, down, &::UPPER_EDGE, &upper_edge);
  146. SDL_Rect const upper_right{
  147. .x = x_+w_-6,
  148. .y = y_+4,
  149. .w = 6,
  150. .h = 5,
  151. };
  152. SDL_RenderCopy(renderer, down, &::UPPER_RIGHT, &upper_right);
  153. SDL_Rect const left_edge{
  154. .x = x_,
  155. .y = y_+9,
  156. .w = 6,
  157. .h = h_-14,
  158. };
  159. SDL_RenderCopy(renderer, down, &::LEFT_EDGE, &left_edge);
  160. SDL_Rect const center{
  161. .x = x_+6,
  162. .y = y_+9,
  163. .w = w_-12,
  164. .h = h_-14,
  165. };
  166. SDL_RenderCopy(renderer, down, &::CENTER, &center);
  167. SDL_Rect const text_rect = ::calculate_text_rect(center, text_ure);
  168. SDL_RenderCopy(renderer, text_ure, nullptr, &text_rect);
  169. SDL_Rect const right_edge{
  170. .x = x_+w_-6,
  171. .y = y_+9,
  172. .w = 6,
  173. .h = h_-14,
  174. };
  175. SDL_RenderCopy(renderer, down, &::RIGHT_EDGE, &right_edge);
  176. SDL_Rect const lower_left{
  177. .x = x_,
  178. .y = y_+h_-5,
  179. .w = 6,
  180. .h = 5,
  181. };
  182. SDL_RenderCopy(renderer, down, &::LOWER_LEFT_DOWN, &lower_left);
  183. SDL_Rect const lower_edge{
  184. .x = x_+6,
  185. .y = y_+h_-5,
  186. .w = w_-12,
  187. .h = 5,
  188. };
  189. SDL_RenderCopy(renderer, down, &::LOWER_EDGE_DOWN, &lower_edge);
  190. SDL_Rect const lower_right{
  191. .x = x_+w_-6,
  192. .y = y_+h_-5,
  193. .w = 6,
  194. .h = 5,
  195. };
  196. SDL_RenderCopy(renderer, down, &::LOWER_RIGHT_DOWN, &lower_right);
  197. }
  198. else {
  199. auto const up = up_.get();
  200. SDL_Rect const upper_left{
  201. .x = x_,
  202. .y = y_,
  203. .w = 6,
  204. .h = 5,
  205. };
  206. SDL_RenderCopy(renderer, up, &::UPPER_LEFT, &upper_left);
  207. SDL_Rect const upper_edge{
  208. .x = x_+6,
  209. .y = y_,
  210. .w = w_-12,
  211. .h = 5,
  212. };
  213. SDL_RenderCopy(renderer, up, &::UPPER_EDGE, &upper_edge);
  214. SDL_Rect const upper_right{
  215. .x = x_+w_-6,
  216. .y = y_,
  217. .w = 6,
  218. .h = 5,
  219. };
  220. SDL_RenderCopy(renderer, up, &::UPPER_RIGHT, &upper_right);
  221. SDL_Rect const left_edge{
  222. .x = x_,
  223. .y = y_+5,
  224. .w = 6,
  225. .h = h_-14,
  226. };
  227. SDL_RenderCopy(renderer, up, &::LEFT_EDGE, &left_edge);
  228. SDL_Rect const center{
  229. .x = x_+6,
  230. .y = y_+5,
  231. .w = w_-12,
  232. .h = h_-14,
  233. };
  234. SDL_RenderCopy(renderer, up, &::CENTER, &center);
  235. SDL_Rect const text_rect = ::calculate_text_rect(center, text_ure);
  236. SDL_RenderCopy(renderer, text_ure, nullptr, &text_rect);
  237. SDL_Rect const right_edge{
  238. .x = x_+w_-6,
  239. .y = y_+5,
  240. .w = 6,
  241. .h = h_-14,
  242. };
  243. SDL_RenderCopy(renderer, up, &::RIGHT_EDGE, &right_edge);
  244. SDL_Rect const lower_left{
  245. .x = x_,
  246. .y = y_+h_-9,
  247. .w = 6,
  248. .h = 9,
  249. };
  250. SDL_RenderCopy(renderer, up, &::LOWER_LEFT_UP, &lower_left);
  251. SDL_Rect const lower_edge{
  252. .x = x_+6,
  253. .y = y_+h_-9,
  254. .w = w_-12,
  255. .h = 9,
  256. };
  257. SDL_RenderCopy(renderer, up, &::LOWER_EDGE_UP, &lower_edge);
  258. SDL_Rect const lower_right{
  259. .x = x_+w_-6,
  260. .y = y_+h_-9,
  261. .w = 6,
  262. .h = 9,
  263. };
  264. SDL_RenderCopy(renderer, up, &::LOWER_RIGHT_UP, &lower_right);
  265. }
  266. SDL_DestroyTexture(text_ure);
  267. }
  268. void Button::update()
  269. {
  270. int mouse_x, mouse_y;
  271. auto const mouse_button = SDL_GetMouseState(&mouse_x, &mouse_y);
  272. if ((mouse_x>=x_ && mouse_x<=(x_+w_) && mouse_y>=y_ && mouse_y<=(y_+h_))) {
  273. if (mouse_button & SDL_BUTTON(SDL_BUTTON_LEFT)) {
  274. pressed_ = true;
  275. }
  276. else {
  277. if (pressed_) {
  278. trigger();
  279. }
  280. pressed_ = false;
  281. }
  282. }
  283. else {
  284. pressed_ = false;
  285. }
  286. }
  287. void Button::trigger()
  288. {
  289. on_click_();
  290. }
  291. void Button::set_on_click(std::function<void()> handler)
  292. {
  293. on_click_ = std::move(handler);
  294. }
  295. void Button::move(int const x, int const y)
  296. {
  297. x_ = x;
  298. y_ = y;
  299. }
  300. void Button::resize(int const w, int const h)
  301. {
  302. w_ = w;
  303. h_ = h;
  304. }
  305. SDL_Rect Button::get_bounding_box() const
  306. {
  307. return {x_, y_, w_, h_};
  308. }