zori_screen.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. int zori_screen_on_sysevent(union zori_event * event) {
  8. struct zori_screen * screen = zori_widget_to_screen(event->any.widget);
  9. /* Move the mouse cursor before passing on the event to the active page, if any. */
  10. if (event->any.type == ALLEGRO_EVENT_MOUSE_AXES) {
  11. screen->cursors.mouse.p.x = event->sys.ev->mouse.x;
  12. screen->cursors.mouse.p.y = event->sys.ev->mouse.y;
  13. }
  14. if (screen->active_page) {
  15. return zori_widget_raise_sys_event(&screen->active_page->widget, &event->sys);
  16. }
  17. return ZORI_HANDLE_OK;
  18. }
  19. void zori_draw_cursor(const struct zori_cursor * cursor, const struct zori_style * style) {
  20. if (cursor->bitmap) {
  21. al_draw_bitmap(cursor->bitmap, cursor->p.x, cursor->p.y, 0);
  22. } else {
  23. float x1, x2, x3, y1, y2, y3;
  24. x1 = cursor->p.x;
  25. y1 = cursor->p.y;
  26. x2 = x1 + 5;
  27. y2 = y1 + 10;
  28. x3 = x1 + 3;
  29. y3 = y1 + 12;
  30. al_draw_filled_triangle(x1, y1, x2, y2, x3, y3, style->fore.color);
  31. }
  32. };
  33. void zori_draw_cursors(const struct zori_cursors * cursors, const struct zori_style * style) {
  34. zori_draw_cursor(&cursors->keyjoy, style);
  35. zori_draw_cursor(&cursors->mouse, style);
  36. };
  37. int zori_screen_on_draw(union zori_event * event) {
  38. struct zori_screen * screen = zori_widget_to_screen(event->any.widget);
  39. zori_draw_cursors(&screen->cursors, &screen->widget.style);
  40. return ZORI_HANDLE_OK;
  41. }
  42. /*
  43. struct zori_handler zori_screen_handlers[] = {
  44. { ZORI_SYSTEM_EVENT_KEY_DOWN , zori_console_handle_keydown , NULL },
  45. { ZORI_SYSTEM_EVENT_KEY_UP , zori_console_handle_ignore , NULL },
  46. { ZORI_SYSTEM_EVENT_KEY_CHAR , zori_console_handle_keychar , NULL },
  47. { ZORI_EVENT_ _MOUSE_AXES, zori_console_handle_mouseaxes , NULL },
  48. { ZORI_EVENT_DRAW , zori_console_draw , NULL },
  49. { ZORI_EVENT_DONE , zori_console_handle_ignore , NULL },
  50. { ZORI_EVENT_FREE , zori_console_handle_ignore , NULL },
  51. { -1, NULL, NULL }
  52. };
  53. */
  54. struct zori_screen *
  55. zori_screen_init(struct zori_screen * screen, zori_display * display) {
  56. memset(&screen->cursors, 0, sizeof(screen->cursors));
  57. screen->display = display;
  58. screen->active_page = NULL;
  59. return screen;
  60. }
  61. struct zori_screen * zori_screen_new(zori_id id, zori_display * display) {
  62. struct zori_screen * screen = NULL; zori_rebox box;
  63. box.at.x = 0;
  64. box.at.y = 0;
  65. if (!display) return NULL;
  66. screen = calloc(1, sizeof(*screen));
  67. if (!screen) return NULL;
  68. box.size.x = al_get_display_width(display);
  69. box.size.y = al_get_display_height(display);
  70. zori_widget_initall(&screen->widget, id, &zori_get_root()->widget,
  71. &box, NULL, 0, NULL);
  72. if (!zori_screen_init(screen, display)) {
  73. free(screen);
  74. screen = NULL;
  75. }
  76. return screen;
  77. }
  78. zori_id zori_new_screen(zori_id id, zori_display * display) {
  79. struct zori_screen * screen = zori_screen_new(id, display);
  80. if (!screen) return ZORI_ID_ENOMEM;
  81. return screen->widget.id;
  82. }
  83. zori_id zori_active_page(zori_id screen_id) {
  84. struct zori_screen * screen = zori_widget_to_page(zori_get_widget(screen_id));
  85. if (!screen) {
  86. return ZORI_ID_EINVAL;
  87. }
  88. if (!screen->active_page) {
  89. return ZORI_ID_EINVAL;
  90. }
  91. return screen->active_page->widget.id;
  92. }
  93. zori_id zori_screen_go(zori_id screen_id, zori_id page_id, void * data) {
  94. struct zori_screen * screen = zori_widget_to_screen(zori_get_widget(screen_id));
  95. struct zori_page * page = zori_widget_to_page(zori_get_widget(page_id));
  96. if ((screen) && (page)) {
  97. screen->active_page = page;
  98. zori_widget_raise_action_event(&page->widget);
  99. return page->widget.id;
  100. } else {
  101. return ZORI_ID_EINVAL;
  102. }
  103. }