plain_button.cpp 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #include "plain_button.h"
  2. #include "utils.hpp"
  3. plain_button::plain_button(graphical::color color, range2D bounds) :
  4. movable_bounds(bounds),
  5. ui_element(static_cast<i_bounds<int2>&>(*this)),
  6. color(color)
  7. { }
  8. void plain_button::draw(const graphical::surface& target)
  9. {
  10. switch(current_state())
  11. {
  12. case ui_element::state::hover:
  13. lowlight(target, color, bounds, int2(2,2));
  14. [[fallthrough]]; // so ugly, my poor switch :(
  15. case ui_element::state::idle:
  16. outline(target, color, bounds);
  17. break;
  18. case ui_element::state::pressed:
  19. graphical::fill(target, color, bounds);
  20. break;
  21. case ui_element::state::disabled:
  22. lowlight(target, color, bounds, int2(3,3));
  23. break;
  24. }
  25. if(focus())
  26. {
  27. const auto padding = support::min_element(upper() - lower())/10;
  28. if(current_state() == ui_element::state::disabled)
  29. outline(target, color, {lower() + padding, upper() - padding});
  30. else
  31. outline_lowlight(target, color, {lower() + padding, upper() - padding});
  32. }
  33. }
  34. void plain_button::update(const interactive::event& event) noexcept
  35. {
  36. ui_element::update(event);
  37. if(current_state() == ui_element::state::disabled)
  38. return;
  39. if(focus())
  40. {
  41. using namespace interactive;
  42. std::visit(support::overload
  43. {
  44. [this](const key_pressed& event)
  45. {
  46. if(event.data.scancode == scancode::space && event.data.repeat == 0)
  47. {
  48. current = state::pressed;
  49. for(auto&& callback : on_press)
  50. callback(*this);
  51. }
  52. },
  53. [this](const key_released& event)
  54. {
  55. if(event.data.scancode == scancode::space)
  56. {
  57. current = state::idle;
  58. for(auto&& callback : on_release)
  59. callback(*this);
  60. for(auto&& callback : on_click)
  61. callback(*this);
  62. }
  63. },
  64. [](auto&&){}
  65. }, event);
  66. }
  67. }