zori_button.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #include "zori.h"
  2. #include "zori_widget.h"
  3. #include "zori_button.h"
  4. struct zori_button * zori_widget_to_button(struct zori_widget * widget) {
  5. return ZORI_CONTAINER_OF(widget, struct zori_button, widget);
  6. }
  7. /** Handles a mouse axis event and pass it on. */
  8. int zori_button_on_mouse_axes(union zori_event * event) {
  9. struct zori_widget * widget = event->any.widget;
  10. struct zori_button * button = zori_widget_to_button(widget);
  11. float x = event->sys.ev->mouse.x;
  12. float y = event->sys.ev->mouse.y;
  13. if (zori_xy_inside_widget_p(widget, x, y)) {
  14. zori_widget_hover_(widget, TRUE);
  15. } else {
  16. zori_widget_hover_(widget, FALSE);
  17. }
  18. return ZORI_HANDLE_PASS;
  19. }
  20. /** Handles a mouse click event and pass it on. */
  21. int zori_button_on_mouse_click(union zori_event * event) {
  22. struct zori_widget * widget = event->any.widget;
  23. struct zori_button * button = zori_widget_to_button(widget);
  24. float x = event->sys.ev->mouse.x;
  25. float y = event->sys.ev->mouse.y;
  26. if (zori_xy_inside_widget_p(widget, x, y)) {
  27. return zori_widget_raise_action_event(widget);
  28. }
  29. return ZORI_HANDLE_PASS;
  30. }
  31. void zori_draw_button(struct zori_button * button) {
  32. float x, y, w, h;
  33. struct zori_style * style = &button->widget.style;
  34. x = rebox_x(&button->widget.box) + 5;
  35. y = rebox_y(&button->widget.box) + 5;
  36. w = rebox_w(&button->widget.box) - 10;
  37. h = rebox_h(&button->widget.box) - 10;
  38. zori_widget_draw_background(&button->widget);
  39. if (button->text) {
  40. zori_font * font = style->text.font;
  41. zori_color color = style->text.color;
  42. al_draw_multiline_ustr(font, color, x, y, w, -1, ALLEGRO_ALIGN_LEFT, button->text);
  43. }
  44. };
  45. int zori_button_on_draw(union zori_event * event) {
  46. struct zori_button * button = zori_widget_to_button(event->any.widget);
  47. zori_draw_button(button);
  48. return ZORI_HANDLE_PASS;
  49. }
  50. struct zori_handler zori_button_handlers[] = {
  51. { ZORI_SYSTEM_EVENT_MOUSE_BUTTON_DOWN , zori_button_on_mouse_click , NULL },
  52. { ZORI_SYSTEM_EVENT_MOUSE_AXES , zori_button_on_mouse_axes , NULL },
  53. { ZORI_EVENT_DRAW , zori_button_on_draw , NULL },
  54. { -1, NULL, NULL }
  55. };
  56. struct zori_button *
  57. zori_button_text_(struct zori_button * button, zori_string * text) {
  58. if (button->text) {
  59. al_ustr_free(button->text);
  60. button->text = NULL;
  61. }
  62. if (text) {
  63. button->text = al_ustr_dup(text);
  64. }
  65. return button;
  66. }
  67. struct zori_button *
  68. zori_button_init(struct zori_button * button, zori_string * text) {
  69. button->text = NULL;
  70. return zori_button_text_(button, text);
  71. }
  72. struct zori_button * zori_button_new(zori_id id, zori_id parent,
  73. zori_box * box, zori_string * text) {
  74. struct zori_button * button = NULL;
  75. button = calloc(1, sizeof(*button));
  76. if (!button) return NULL;
  77. zori_widget_initall(&button->widget, id, zori_get_widget(parent),
  78. box, NULL, ZORI_ARRAY_AND_AMOUNT(zori_button_handlers));
  79. if (!zori_button_init(button, text)) {
  80. free(button);
  81. button = NULL;
  82. }
  83. return button;
  84. }
  85. zori_id zori_new_button(zori_id id, zori_id parent, zori_box * box, zori_string * text) {
  86. struct zori_button * button = zori_button_new(id, parent, box, text);
  87. if (!button) return ZORI_ID_ENOMEM;
  88. return button->widget.id;
  89. }