zori_screen.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. #include "zori.h"
  2. #include "zori_page.h"
  3. #include "zori_screen.h"
  4. struct zori_screen * zori_widget_to_screen(struct zori_widget * widget) {
  5. return ZORI_CONTAINER_OF(widget, struct zori_screen, widget);
  6. }
  7. /** Handles a mouse axis event and then pass it on to the active page. */
  8. int zori_screen_on_mouse_axes(union zori_event * event) {
  9. struct zori_screen * screen = zori_widget_to_screen(event->any.widget);
  10. screen->cursors.mouse.p.x = event->sys.ev->mouse.x;
  11. screen->cursors.mouse.p.y = event->sys.ev->mouse.y;
  12. if (screen->active_page) {
  13. return zori_widget_raise_system_event(&screen->active_page->widget,
  14. event->sys.ev);
  15. }
  16. return ZORI_HANDLE_DONE;
  17. }
  18. /** Handles a system event by passing it on to the active page. */
  19. int zori_screen_on_sysevent(union zori_event * event) {
  20. struct zori_screen * screen = zori_widget_to_screen(event->any.widget);
  21. if (screen->active_page) {
  22. return zori_widget_raise_system_event(&screen->active_page->widget,
  23. event->sys.ev);
  24. }
  25. return ZORI_HANDLE_DONE;
  26. }
  27. void zori_draw_cursor(const struct zori_cursor * cursor, const struct zori_style * style) {
  28. if (cursor->bitmap) {
  29. al_draw_bitmap(cursor->bitmap, cursor->p.x, cursor->p.y, 0);
  30. } else {
  31. float x1, x2, x3, y1, y2, y3;
  32. x1 = cursor->p.x;
  33. y1 = cursor->p.y;
  34. x2 = x1 + 24 + 8;
  35. y2 = y1 + 24 - 8;
  36. x3 = x1 + 24 - 8;
  37. y3 = y1 + 24 + 8;
  38. al_draw_filled_triangle(x1, y1, x2, y2, x3, y3, style->fore.color);
  39. al_draw_triangle(x1, y1, x2, y2, x3, y3, style->back.color, 1.0);
  40. }
  41. };
  42. void zori_draw_cursors(const struct zori_cursors * cursors, const struct zori_style * style) {
  43. zori_draw_cursor(&cursors->keyjoy, style);
  44. zori_draw_cursor(&cursors->mouse, style);
  45. };
  46. int zori_screen_on_overdraw(union zori_event * event) {
  47. struct zori_screen * screen = zori_widget_to_screen(event->any.widget);
  48. zori_draw_cursors(&screen->cursors, &screen->widget.style);
  49. return ZORI_HANDLE_PASS;
  50. }
  51. struct zori_handler zori_screen_handlers[] = {
  52. { ZORI_SYSTEM_EVENT_KEY_DOWN , zori_screen_on_sysevent , NULL },
  53. { ZORI_SYSTEM_EVENT_KEY_UP , zori_screen_on_sysevent , NULL },
  54. { ZORI_SYSTEM_EVENT_KEY_CHAR , zori_screen_on_sysevent , NULL },
  55. { ZORI_SYSTEM_EVENT_MOUSE_AXES, zori_screen_on_mouse_axes , NULL },
  56. { ZORI_EVENT_OVERDRAW , zori_screen_on_overdraw , NULL },
  57. { -1, NULL, NULL }
  58. };
  59. struct zori_screen *
  60. zori_screen_init(struct zori_screen * screen, zori_display * display) {
  61. memset(&screen->cursors, 0, sizeof(screen->cursors));
  62. screen->display = display;
  63. screen->active_page = NULL;
  64. return screen;
  65. }
  66. struct zori_screen * zori_screen_new(zori_id id, zori_display * display) {
  67. struct zori_screen * screen = NULL; zori_rebox box;
  68. box.at.x = 0;
  69. box.at.y = 0;
  70. if (!display) return NULL;
  71. screen = calloc(1, sizeof(*screen));
  72. if (!screen) return NULL;
  73. box.size.x = al_get_display_width(display);
  74. box.size.y = al_get_display_height(display);
  75. zori_widget_initall(&screen->widget, id, &zori_get_root()->widget,
  76. &box, NULL, ZORI_AMOUNT_AND_ARRAY(zori_screen_handlers));
  77. if (!zori_screen_init(screen, display)) {
  78. free(screen);
  79. screen = NULL;
  80. }
  81. return screen;
  82. }
  83. zori_id zori_new_screen(zori_id id, zori_display * display) {
  84. struct zori_screen * screen = zori_screen_new(id, display);
  85. if (!screen) return ZORI_ID_ENOMEM;
  86. return screen->widget.id;
  87. }
  88. zori_id zori_active_page(zori_id screen_id) {
  89. struct zori_screen * screen = zori_widget_to_screen(zori_get_widget(screen_id));
  90. if (!screen) {
  91. return ZORI_ID_EINVAL;
  92. }
  93. if (!screen->active_page) {
  94. return ZORI_ID_EINVAL;
  95. }
  96. return screen->active_page->widget.id;
  97. }
  98. zori_id zori_screen_go(zori_id screen_id, zori_id page_id, void * data) {
  99. struct zori_screen * screen = zori_widget_to_screen(zori_get_widget(screen_id));
  100. struct zori_page * page = zori_widget_to_page(zori_get_widget(page_id));
  101. if ((screen) && (page)) {
  102. screen->active_page = page;
  103. zori_widget_raise_action_event(&page->widget);
  104. return page->widget.id;
  105. } else {
  106. return ZORI_ID_EINVAL;
  107. }
  108. }