#include "zori.h" #include "miao.h" #include /* * Pardon the pun name, but Zori is the submodule that handles the user * interface and the menus. */ /* registry functionality */ /** Compare registry entries. */ int zori_registry_entry_compare(const void * v1, const void * v2) { const struct zori_registry_entry * entry1 = v1; const struct zori_registry_entry * entry2 = v2; return (entry2->id - entry1->id); } /** Initialize the registry. */ zori_id zori_registry_init(struct zori_registry * registry) { miao_init(registry); return ZORI_ID_OK; } /** Add an entry to the registry. */ zori_id zori_registry_add(struct zori_registry * registry, zori_id id, struct zori_widget * widget) { struct zori_registry_entry entry = { id, widget }; if (miao_push(registry, entry)) { miao_qsort(registry, zori_registry_entry_compare); return ZORI_ID_OK; } return ZORI_ID_ENOMEM; } /** Look up an entry in the registry. */ struct zori_registry_entry * zori_registry_lookup_entry(struct zori_registry * registry, zori_id id) { struct zori_registry_entry key = { id, NULL }; return miao_bsearch(registry, zori_registry_entry_compare, &key); } /** Look up a widget in the registry. */ struct zori_widget * zori_registry_lookup(struct zori_registry * registry, zori_id id) { struct zori_widget * result = NULL; struct zori_registry_entry * entry = NULL; entry = zori_registry_lookup_entry(registry, id); if (entry) { result = entry->widget; } return result; } /** Remove an entry from the registry. */ zori_id zori_registry_remove(struct zori_registry * registry, zori_id id) { struct zori_registry_entry * entry = NULL; entry = zori_registry_lookup_entry(registry, id); if (entry) { miao_delete_entry(registry, entry); } return ZORI_ID_OK; } /** Handler functionality */ int zori_handler_compare(const void * v1, const void * v2) { const struct zori_handler * h1 = v1; const struct zori_handler * h2 = v2; if (h1->type < h2->type) return -1; if (h1->type > h2->type) return 1; return 0; } struct zori_handler * zori_handlers_add(struct zori_handlers * me, zori_event_type type, zori_handler_func * handler, void * data) { struct zori_handler * result = NULL; result = miao_push_ptr(me); if (result) { result->type = type; result->handler = handler; result->data = data; } miao_qsort(me, zori_handler_compare); return result; } void zori_handlers_done(struct zori_handlers * me) { miao_done(me); } void zori_handlers_init(struct zori_handlers * me) { miao_init(me); } struct zori_handler * zori_handlers_search(struct zori_handlers * me, zori_event_type type) { struct zori_handler * result; struct zori_handler key; key.type = type; key.handler = NULL; key.data = NULL; if (!me) return NULL; result = miao_bsearch(me, zori_handler_compare, &key); return result; } int zori_handlers_handle(struct zori_handlers * me, struct zori_event * event, struct zori_widget * widget) { struct zori_handler * handler = zori_handlers_search(me, event->sysev.type); if (!handler) return 0; return handler->handler(event); } static struct zori_root * the_zori_root = NULL; static struct zori_style * the_default_style = NULL; static struct zori_registry * the_zori_registry = NULL; int zori_widget_compare(const void * v1, const void * v2) { const struct zori_widget * w1 = v1, * w2 = v2; return (w2->id - w1->id); } struct zori_widget * zori_get_widget(zori_id id) { struct zori_widget key = {0}; key.id = id; return miao_bsearch(the_zori_root->widgets, zori_widget_compare, &key); } zori_id zori_get_unused_id(void) { zori_id id = -1; struct zori_widget * found; do { id++; if (id == INT_MAX) { return ZORI_ID_ERROR; } found = zori_get_widget(id); } while(found); return id; } zori_id zori_start(struct zori_style * default_style) { if (the_zori_root) return ZORI_ID_OK; the_zori_root = calloc(1, sizeof(*the_zori_root)); if (!the_zori_root) return ZORI_ID_ENOMEM; the_default_style = calloc(1, sizeof(*the_default_style)); if (!the_default_style) return ZORI_ID_ENOMEM; the_zori_registry = calloc(1, sizeof(*the_zori_registry)); if (!the_zori_registry) return ZORI_ID_ENOMEM; the_default_style->text.font = al_create_builtin_font(); the_default_style->text.color = al_color_name("white"); the_default_style->border.color = al_color_name("white"); the_default_style->back.color = al_color_name("green"); if (default_style) { the_zori_root->widget.style = *default_style; } else { the_zori_root->widget.style = *the_default_style; } miao_init(the_zori_root->widgets); the_zori_root->widget.id = 0; zori_registry_add(the_zori_registry, the_zori_root->widget.id, &the_zori_root->widget); /* * miao_push(the_zori_root->widgets, &the_zori_root->widget); * miao_qsort(the_zori_root->widgets, zori_widget_compare); */ return the_zori_root->widget.id; } void zori_widget_free(struct zori_widget * widget); struct zori_widget * zori_widget_done(struct zori_widget * widget) { struct zori_widget * aid, * next; if (!widget) return widget; if (widget->child) { /* Free all children. */ aid = widget->child; while (aid) { next = aid->sibling; zori_widget_free(aid); aid = next; } } return widget; } void zori_widget_free(struct zori_widget * widget) { /* XXX remove from widget registry too... */ return free(zori_widget_done(widget)); } /* Shut down Zori and destroys all widgets. Return 0 on succes or * negative on error. */ zori_id zori_shutdown() { miao_done(the_zori_registry); free(the_zori_registry); the_zori_registry = NULL; free(the_default_style); the_default_style = NULL; assert((void *)(&the_zori_root->widget) == (void *)the_zori_root); zori_widget_free(&the_zori_root->widget); the_zori_root = NULL; return ZORI_ID_OK; } /* Creates a new screen widget. Normally this should be the first widget * you create after zori_start. */ zori_id zori_new_screen(zori_display * display); /* Creates a new page widget on the given screen. The page is not * made the active page, unless if it is the first one created. */ zori_id zori_new_page(zori_id screen); /* Activates the page on it's display. All other pages are dectivated and * hidden. */ zori_id zori_activate_page(zori_id page); /* Creates a new generic widget on the given screen with the given * dimensions. */ zori_id zori_new(zori_id screen, zori_rebox * box); /* Sets the flags of a widget. */ zori_id zori_set_flags(zori_id widget, enum zori_flag flags); /* Sets the whole style of a widget. */ zori_id zori_set_style(zori_id id, struct zori_style * style); /* Sets the background color of the widget. */ zori_id zori_set_background_color(zori_id id, zori_color color); /* Sets the foreground color of the widget. */ zori_id zori_set_foreground_color(zori_id id, zori_color color); /* Creates a new frame widget. */ zori_id zori_new_frame_widget(zori_id parent, zori_rebox box); /* Creates a new (vertical) menu widget. */ zori_id zori_new_menu_widget(zori_id parent, zori_rebox box, char * text); /* Creates a new button widget. */ zori_id zori_new_button_widget(zori_id parent, zori_rebox box, char * text); /* Creates a new conversation widget. */ zori_id zori_new_conversation_widget(zori_id parent, zori_rebox box, char * text); /* Draws the whole UI and all visible parts. */ void zori_draw_all(void) { } /* Updates the state of the UI. Pass in the time passed since last update. */ void zori_update(double dt) { struct zori_event event; event.sysev.type = ZORI_EVENT_UPDATE; event.sysev.user.data1 = (intptr_t)&dt; zori_handlers_handle(&the_zori_root->widget.handlers, &event, &the_zori_root->widget); } /* Registers an event handler for a widget. */ zori_id zori_register(zori_id id, zori_event_type type, zori_handler_func handler, void * extra);