zori_screen.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. 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. /* the keyjoy cursor is hidden off-screen to begin with. */
  63. screen->cursors.keyjoy.p.x = - 64;
  64. screen->cursors.keyjoy.p.y = - 64;
  65. screen->display = display;
  66. screen->active_page = NULL;
  67. return screen;
  68. }
  69. struct zori_screen * zori_screen_new(zori_id id, zori_display * display) {
  70. struct zori_screen * screen = NULL; zori_rebox box;
  71. box.at.x = 0;
  72. box.at.y = 0;
  73. if (!display) return NULL;
  74. screen = calloc(1, sizeof(*screen));
  75. if (!screen) return NULL;
  76. box.size.x = al_get_display_width(display);
  77. box.size.y = al_get_display_height(display);
  78. zori_widget_initall(&screen->widget, id, &zori_get_root()->widget,
  79. &box, NULL, ZORI_ARRAY_AND_AMOUNT(zori_screen_handlers));
  80. if (!zori_screen_init(screen, display)) {
  81. free(screen);
  82. screen = NULL;
  83. }
  84. return screen;
  85. }
  86. zori_id zori_new_screen(zori_id id, zori_display * display) {
  87. struct zori_screen * screen = zori_screen_new(id, display);
  88. if (!screen) return ZORI_ID_ENOMEM;
  89. return screen->widget.id;
  90. }
  91. zori_id zori_active_page(zori_id screen_id) {
  92. struct zori_screen * screen = zori_widget_to_screen(zori_get_widget(screen_id));
  93. if (!screen) {
  94. return ZORI_ID_EINVAL;
  95. }
  96. if (!screen->active_page) {
  97. return ZORI_ID_EINVAL;
  98. }
  99. return screen->active_page->widget.id;
  100. }
  101. zori_id zori_screen_go(zori_id screen_id, zori_id page_id, void * data) {
  102. struct zori_screen * screen = zori_widget_to_screen(zori_get_widget(screen_id));
  103. struct zori_page * page = zori_widget_to_page(zori_get_widget(page_id));
  104. if ((screen) && (page)) {
  105. screen->active_page = page;
  106. zori_widget_raise_action_event(&page->widget);
  107. return page->widget.id;
  108. } else {
  109. return ZORI_ID_EINVAL;
  110. }
  111. }