zori_screen.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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_PASS;
  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_PASS;
  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. /* Handlers must beset up for many events on screen to funnel them all into
  52. * the active page. */
  53. struct zori_handler zori_screen_handlers[] = {
  54. { ZORI_SYSTEM_EVENT_KEY_DOWN , zori_screen_on_sysevent , NULL },
  55. { ZORI_SYSTEM_EVENT_KEY_UP , zori_screen_on_sysevent , NULL },
  56. { ZORI_SYSTEM_EVENT_KEY_CHAR , zori_screen_on_sysevent , NULL },
  57. { ZORI_SYSTEM_EVENT_MOUSE_BUTTON_DOWN , zori_screen_on_sysevent , NULL },
  58. { ZORI_SYSTEM_EVENT_MOUSE_BUTTON_UP , zori_screen_on_sysevent , NULL },
  59. { ZORI_SYSTEM_EVENT_MOUSE_AXES , zori_screen_on_mouse_axes , NULL },
  60. { ZORI_EVENT_OVERDRAW , zori_screen_on_overdraw , NULL },
  61. { -1, NULL, NULL }
  62. };
  63. struct zori_screen *
  64. zori_screen_init(struct zori_screen * screen, zori_display * display) {
  65. memset(&screen->cursors, 0, sizeof(screen->cursors));
  66. /* the keyjoy cursor is hidden off-screen to begin with. */
  67. screen->cursors.keyjoy.p.x = - 64;
  68. screen->cursors.keyjoy.p.y = - 64;
  69. screen->display = display;
  70. screen->active_page = NULL;
  71. return screen;
  72. }
  73. struct zori_screen * zori_screen_new(zori_id id, zori_display * display) {
  74. struct zori_screen * screen = NULL; zori_rebox box;
  75. box.at.x = 0;
  76. box.at.y = 0;
  77. if (!display) return NULL;
  78. screen = calloc(1, sizeof(*screen));
  79. if (!screen) return NULL;
  80. box.size.x = al_get_display_width(display);
  81. box.size.y = al_get_display_height(display);
  82. zori_widget_initall(&screen->widget, ZORI_WIDGET_TYPE_SCREEN, id, &zori_get_root()->widget,
  83. &box, NULL, ZORI_ARRAY_AND_AMOUNT(zori_screen_handlers));
  84. if (!zori_screen_init(screen, display)) {
  85. free(screen);
  86. screen = NULL;
  87. }
  88. return screen;
  89. }
  90. zori_id zori_new_screen(zori_id id, zori_display * display) {
  91. struct zori_screen * screen = zori_screen_new(id, display);
  92. if (!screen) return ZORI_ID_ENOMEM;
  93. return screen->widget.id;
  94. }
  95. zori_id zori_active_page(zori_id screen_id) {
  96. struct zori_screen * screen = zori_widget_to_screen(zori_get_widget(screen_id));
  97. if (!screen) {
  98. return ZORI_ID_EINVAL;
  99. }
  100. if (!screen->active_page) {
  101. return ZORI_ID_EINVAL;
  102. }
  103. return screen->active_page->widget.id;
  104. }
  105. zori_id zori_screen_go(zori_id screen_id, zori_id page_id, void * data) {
  106. struct zori_screen * screen = zori_widget_to_screen(zori_get_widget(screen_id));
  107. struct zori_page * page = zori_widget_to_page(zori_get_widget(page_id));
  108. if ((screen) && (page)) {
  109. screen->active_page = page;
  110. zori_widget_raise_action_event(&page->widget);
  111. return page->widget.id;
  112. } else {
  113. return ZORI_ID_EINVAL;
  114. }
  115. }