123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972 |
- #include "zori.h"
- #include "miao.h"
- #include <allegro5/allegro_color.h>
- #include "str.h"
- #include "draw.h"
- /*
- * 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, union zori_event * event) {
- struct zori_handler * handler = zori_handlers_search(me, event->type);
- if (!handler) return 0;
- event->any.data = handler->data;
- return handler->handler(event);
- }
- int zori_widget_raise_system_event
- (struct zori_widget * widget, zori_system_event * sysev) {
- union zori_event event;
- event.sys.any.type = sysev->type;
- event.sys.any.widget = widget;
- event.sys.ev = sysev;
- return zori_handlers_handle(&widget->handlers, &event);
- }
- int zori_widget_raise_draw_event(struct zori_widget * widget) {
- union zori_event event;
- event.draw.any.type = ZORI_EVENT_DRAW;
- event.draw.any.widget = widget;
- return zori_handlers_handle(&widget->handlers, &event);
- }
- int zori_widget_raise_done_event(struct zori_widget * widget) {
- union zori_event event;
- event.done.any.type = ZORI_EVENT_DONE;
- event.done.any.widget = widget;
- return zori_handlers_handle(&widget->handlers, &event);
- }
- int zori_widget_raise_free_event(struct zori_widget * widget) {
- union zori_event event;
- event.free.any.type = ZORI_EVENT_FREE;
- event.free.any.widget = widget;
- return zori_handlers_handle(&widget->handlers, &event);
- }
- int zori_widget_raise_update_event
- (struct zori_widget * widget, double dt) {
- union zori_event event;
- event.update.any.type = ZORI_EVENT_DONE;
- event.update.any.widget = widget;
- event.update.dt = dt;
- return zori_handlers_handle(&widget->handlers, &event);
- }
- int zori_widget_raise_action_event
- (struct zori_widget * widget) {
- union zori_event event;
- event.action.any.type = ZORI_EVENT_ACTION;
- event.action.any.widget = widget;
- return zori_handlers_handle(&widget->handlers, &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;
- struct zori_root * zori_get_root(void) {
- return the_zori_root;
- }
- 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) {
- if (!the_zori_registry) return NULL;
- return zori_registry_lookup(the_zori_registry, id);
- }
- 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;
- }
- struct zori_handler * zori_widget_add_handler
- (struct zori_widget * widget, zori_event_type type, zori_handler_func * handler, void * data) {
- if ((!widget) || (!handler)) return NULL;
-
- return zori_handlers_add(&widget->handlers, type, handler, data);
- }
- struct zori_handler * zori_widget_add_handlers
- (struct zori_widget * widget, struct zori_handler * handlers, size_t amount) {
- size_t i;
- for (i = 0; i < amount; i++) {
- struct zori_handler * handler = handlers + i;
- if (!zori_widget_add_handler(widget, handler->type, handler->handler, handler->data)) return NULL;
- }
- return handlers + amount - 1;
- }
- 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;
- }
-
- the_zori_root->widget.id = 0;
- zori_registry_add(the_zori_registry, the_zori_root->widget.id, &the_zori_root->widget);
- return the_zori_root->widget.id;
- }
- void zori_widget_free(struct zori_widget * widget);
- struct zori_widget * zori_widget_done(struct zori_widget * widget) {
- size_t index;
- struct zori_widget * aid, * next;
- if (!widget) return widget;
-
- for (index = 0; index < miao_size(&widget->children); index ++) {
- struct zori_widget * child = miao_unsafe_get(&widget->children, index);
- zori_widget_free(child);
- }
-
- miao_done(&widget->handlers);
- miao_done(&widget->children);
-
- return widget;
- }
- void zori_widget_free(struct zori_widget * widget) {
- if (widget) {
- zori_registry_remove(the_zori_registry, widget->id);
- }
- zori_widget_done(widget);
- free(widget);
- }
- struct zori_widget *
- zori_widget_add_child(struct zori_widget * parent, struct zori_widget * child) {
- if (parent) {
- miao_push((&parent->children), child);
- child->parent = child;
- }
- return child;
- }
- struct zori_widget * zori_widget_init
- ( struct zori_widget * widget,
- zori_id id,
- struct zori_widget * parent,
- zori_rebox * box, struct zori_style * style) {
- if (!widget) return NULL;
-
- miao_init(&widget->children);
- miao_init(&widget->handlers);
- widget->z = 0;
- widget->flags = 0;
- widget->id = ( (id < 0) ? widget->id : id);
-
- if (style) {
- widget->style = *style;
- if (!widget->style.text.font) {
- widget->style.text.font = the_default_style->text.font;
- }
- } else {
- widget->style = *the_default_style;
- }
- if (box) {
- widget->box = *box;
- } else if (parent) {
- widget->box = parent->box;
- } else {
- widget->box = rebox_make(0, 0, ZORI_WIDGET_DEFAULT_W, ZORI_WIDGET_DEFAULT_H);
- }
- zori_widget_add_child(parent, widget);
- zori_registry_add(the_zori_registry, widget->id, widget);
- return widget;
- }
- /* Shut down Zori and destroys all widgets. Return 0 on succes or
- * negative on error.
- */
- zori_id zori_shutdown() {
- assert((void *)(&the_zori_root->widget) == (void *)the_zori_root);
- zori_widget_free(&the_zori_root->widget);
- the_zori_root = NULL;
- free(the_default_style);
- the_default_style = NULL;
- /* clean up registry last so zori_widget fre can unregister it's widgets.*/
- miao_done(the_zori_registry);
- free(the_zori_registry);
- the_zori_registry = NULL;
-
-
- return ZORI_ID_OK;
- }
- struct zori_widget *
- zori_widget_initall(struct zori_widget * widget, int id,
- struct zori_widget * parent, zori_rebox * box, struct zori_style * style,
- size_t amount, struct zori_handler * handlers) {
- if (zori_widget_init(widget, id, parent, box, style)) {
- zori_widget_add_handlers(widget, handlers, amount);
- }
- return widget;
- }
- /* 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 widget and it's children recursively. */
- void zori_draw_widget(struct zori_widget * widget) {
- size_t index;
- if (widget && zori_widget_visible(widget)) {
- zori_widget_raise_draw_event(widget);
- for (index = 0; index < miao_size(&widget->children); index++) {
- struct zori_widget * child = miao_unsafe_get(&widget->children, index);
- zori_draw_widget(child);
- }
- }
- }
- /* Draws the whole UI and all visible parts. */
- void zori_draw_all(void) {
- zori_draw_widget(&the_zori_root->widget);
- }
- int zori_widget_visible(struct zori_widget * widget) {
- return widget && ((widget->flags & ZORI_FLAG_HIDDEN) != ZORI_FLAG_HIDDEN);
- }
- int zori_widget_active(struct zori_widget * widget) {
- return widget && ((widget->flags & ZORI_FLAG_DISABLED) != ZORI_FLAG_DISABLED);
- }
- int zori_widget_active_(struct zori_widget * widget, int set) {
- if (set) {
- widget->flags = widget->flags & (~ZORI_FLAG_DISABLED);
- } else {
- widget->flags = widget->flags | ZORI_FLAG_DISABLED;
- }
- return zori_widget_active(widget);
- }
- int zori_widget_visible_(struct zori_widget * widget, int set) {
- if (set) {
- widget->flags = widget->flags & (~ZORI_FLAG_HIDDEN);
- } else {
- widget->flags = widget->flags | ZORI_FLAG_HIDDEN;
- }
- return zori_widget_active(widget);
- }
- /* Updates the state of the UI. Pass in the time passed since last update. */
- void zori_update(double dt)
- {
- zori_widget_raise_update_event(&the_zori_root->widget, dt);
- }
- /* Registers an event handler for a widget. */
- zori_id zori_register(zori_id id, zori_event_type type, zori_handler_func handler, void * extra);
- zori_font * zori_widget_font(struct zori_widget * widget) {
- return widget->style.text.font;
- }
- zori_color zori_widget_forecolor(struct zori_widget * widget) {
- return widget->style.text.color;
- }
- zori_color zori_widget_backcolor(struct zori_widget * widget) {
- return widget->style.back.color;
- }
- int zori_widget_h(struct zori_widget * widget) {
- return widget->box.size.y;
- }
- int zori_widget_w(struct zori_widget * widget) {
- return widget->box.size.x;
- }
- int zori_widget_x(struct zori_widget * widget) {
- return widget->box.at.x;
- }
- int zori_widget_y(struct zori_widget * widget) {
- return widget->box.at.y;
- }
- /* Helper struct that keeps track of the BYTE positions within
- a c string or USTR where a line or word starts or ends with a given maxwidth. */
- struct zori_textinfo {
- int from_char;
- int start_char;
- int stop_char;
- int maxwidth;
- };
- /* Creates a temporary ustr as per al_ref_ustr but with
- start and stop as code positions, not byte positions. */
- static const USTR *
- ustrinfo_newref(
- USTR_INFO * uinfo, const USTR * ustr, int start, int stop) {
- return ustr_refustr(uinfo, ustr,
- ustr_offset(ustr, start),
- ustr_offset(ustr, stop)
- );
- }
- /* Creates a temporary ustr that refers ustr but respecds the bounds of the
- textinfo (start_char and enc_char) */
- const USTR * zori_textinfo_refustr(struct zori_textinfo * self,
- USTR_INFO * uinfo,
- const USTR * ustr) {
- return ustrinfo_newref(uinfo, ustr, self->start_char, self->stop_char);
- }
- struct zori_textinfo *
- zori_textinfo_wordfromtext(struct zori_textinfo * self, USTR * ustr, zori_font * font) {
- int found;
- int start_pos;
- int end_pos;
- int now_char;
- int end_char;
- int len;
- int ch;
- if(!self) return NULL;
- now_char = self->from_char;
- self->start_char = now_char;
- ch = ustr_getnext(ustr, &now_char);
- while (ch > 0) {
- switch(ch) {
- case ' ': /* Found a space, here the word ends, include the space. */
- self->stop_char = now_char;
- return self;
- case '\n': /* A newline ends a word, include the newline. */
- self->stop_char = now_char;
- return self;
- default: /* Other characters mean the word is not finished yet. */
- break;
- }
- /* XXX: Should handle the case for languages that use no spaces,
- * by checking with al_get_ustr_width but it's not a pressing matter yet.
- */
- ch = ustr_getnext(ustr, &now_char);
- }
- // no word found, just set end here and be done.
- self->stop_char = now_char;
- /* return nULL to signify end */
- return NULL;
- }
- /** Prints a ustring, since puts or printf print too much some
- times for a refstring.*/
- static int ustr_print(USTR * word) {
- size_t index;
- for(index = 0; index < ustr_length(word) ; index++) {
- putchar(ustr_get(word, index));
- }
- return index;
- }
- /** Gets the positions of the next line of text fort he given Unicode string
- and store them in the given info. If the info's from is set to 0 or less,
- the first line is assumed, otherwise, the line will be started from "from".
- Uses the given font to determine the width of the text as it is built.
- */
- struct zori_textinfo *
- zori_textinfo_linefromtext(struct zori_textinfo * self, USTR * ustr, zori_font * font) {
- struct zori_textinfo wordinfo;
- USTR_INFO lineuinfo;
- const USTR * line;
- USTR_INFO worduinfo = { 0, 0, 0};
- const USTR * word;
- int ch;
- int index;
- int width;
- int last_stop;
- self->start_char = self->from_char;
- wordinfo.from_char = self->from_char;
-
- while(zori_textinfo_wordfromtext(&wordinfo, ustr, font)) {
- word = zori_textinfo_refustr(&wordinfo, &worduinfo, ustr);
- line = ustrinfo_newref(&lineuinfo, ustr, self->start_char, wordinfo.stop_char);
- width = al_get_ustr_width(font, line);
- if (width > self->maxwidth) {
- /* XXX: handle case of text overflow by bluntly retuning the word as is.
- Should split single word based on length too.
- There is overflow if this is still the first word as see from wordinfo_start_char.
- */
- if (wordinfo.start_char == self->start_char) {
- self->stop_char = wordinfo.stop_char;
- } else {
- self->stop_char = wordinfo.start_char;
- }
- return self;
- }
- // If we get here, the word way still end on a newline character
- // check this case. XXX: It works like this because
- // stop_char is a bit wonky... it points at the first character of the
- // next word in this case...
- ch = ustr_get(ustr, wordinfo.stop_char - 1);
- if (ch == '\n') {
- self->start_char = self->from_char;
- self->stop_char = wordinfo.stop_char - 1;
- return self;
- }
- wordinfo.from_char = wordinfo.stop_char;
- }
- /* if we get here, the whole string fits. */
- self->start_char = self->from_char;
- self->stop_char = wordinfo.stop_char;
- /* Return NULL to tell caller text has been completely split up. */
- return NULL;
- }
- #define ZORI_WIDGET_BORDER 3
- /** Draws a rounded frame as background for a widget. */
- void zori_widget_drawroundframe(struct zori_widget * self) {
- if(!self) return;
- draw_roundframe(zori_widget_x(self), zori_widget_y(self),
- zori_widget_w(self), zori_widget_h(self),
- ZORI_WIDGET_BORDER,
- zori_widget_forecolor(self), zori_widget_backcolor(self));
- }
- /** Skips the text info to the next word or line of text. Must be called
- when looping over zori_textinfo_linefromtext. */
- struct zori_textinfo * zori_textinfo_next(struct zori_textinfo * self) {
- if(!self) return NULL;
- self->from_char = self->stop_char + 1;
- self->start_char = self->from_char;
- return self;
- }
- /* Converts a widget to a console. Only works if the pointer is wrapped correctly,
- by a console. */
- struct zori_console * zori_widget_console(struct zori_widget * widget) {
- if (!widget) return NULL;
- return ZORI_CONTAINER_OF(widget, struct zori_console, widget);
- }
- /** Sets the console's command function and data. */
- void zori_console_command_(struct zori_console * self, zori_console_command * command, void * data) {
- self->command = command;
- self->command_data = data;
- }
- /** Let the console perform a command if possible. returns nonzero on error,
- zero if OK. */
- int zori_console_docommand(struct zori_console * self, const char * text) {
- if(!self) return -1;
- if(!self->command) return -2;
- return self->command(&self->widget, text, self->command_data);
- }
- /** Adds a line of text to the console. */
- int zori_console_addstr(struct zori_console * self, const char * str) {
- if(!self) return -1;
- if(!ustrlist_shiftcstr(&self->text, str)) {
- return -3;
- }
- while(ustrlist_size(&self->text) >= self->max) { // remove last node(s)
- ustrlist_droplast(&self->text);
- }
- return ustrlist_size(&self->text);
- }
- /** Adds a line of text to the console. */
- int zori_console_addustr(struct zori_console * self, const USTR * ustr) {
- if(!self) return -1;
- if(!ustrlist_shiftustr(&self->text, ustr)) {
- return -3;
- }
- while(ustrlist_size(&self->text) >= self->max) { // remove last node(s)
- ustrlist_droplast(&self->text);
- }
- return ustrlist_size(&self->text);
- }
- /** Puts a string on the console .*/
- int zori_console_puts(struct zori_console * self, const char * str) {
- int index;
- int size = strlen(str);
- int leftsize = size;
- int lines = 0;
- USTR_INFO uinfo;
- struct zori_textinfo info = { 0, 0, 0, 0};
- info.maxwidth = zori_widget_w(&self->widget) - 10;
- USTR * ustr;
- const USTR * uline;
- ustr = ustr_new(str);
- while(zori_textinfo_linefromtext(&info, ustr, self->widget.style.text.font)) {
- uline = zori_textinfo_refustr(&info, &uinfo, ustr);
- zori_console_addustr(self, uline);
- // don't forget to skip to next line!!!
- zori_textinfo_next(&info);
- }
- uline = zori_textinfo_refustr(&info, &uinfo, ustr);
- zori_console_addustr(self, uline);
- ustr_free(ustr);
- return lines;
- }
- #define BBCONSOLE_VPRINTF_MAX 1024
- /** Prints a formatted string on the console, truncaded to 1024 characters. */
- int zori_console_vprintf(struct zori_console * self, const char * format, va_list args) {
- char buffer[BBCONSOLE_VPRINTF_MAX] = { '\0' };
- vsnprintf(buffer, BBCONSOLE_VPRINTF_MAX, format, args);
- return zori_console_puts(self, buffer);
- }
- /** Prints a formatted string on the console, truncaded to 1024 characters. */
- int zori_console_printf(struct zori_console * self, const char * format, ...) {
- int result;
- va_list args;
- va_start(args, format);
- result = zori_console_vprintf(self, format, args);
- va_end(args);
- return result;
- }
- /** Draws a console. */
- int zori_console_draw(union zori_event * zevent) {
- struct zori_console * self ;
- zori_font * font ;
- zori_color color ;
- USTRListNode * now;
- int high, linehigh, index, x, y, skip;
- int linew;
- struct zori_widget * widget = zevent->any.widget;
-
- if (!zori_widget_visible(widget)) return ZORI_HANDLE_IGNORE;
-
- self = zori_widget_console(widget);
- font = zori_widget_font(widget);
- color = zori_widget_forecolor(widget);
-
- zori_widget_drawroundframe(widget);
- high = zori_widget_h(widget) - 10;
- x = zori_widget_x(widget) + 5;
- y = zori_widget_y(widget) - 5;
- linehigh = zori_font_lineheight(font);
-
- now = ustrlist_head(&self->text);
- // skip start lines (to allow scrolling backwards)
- now = ustrlist_skipnode(&self->text, self->start);
-
- for (index = high-(linehigh*2); index > 0; index -= linehigh) {
- USTR * textstr;
- if(!now) break;
- textstr = ustrlistnode_ustr(now);
- if(textstr) {
- zori_font_drawstr(font, color, x, y + index, 0, textstr);
- }
- now = ustrlistnode_next(now);
- }
- // draw input string
- zori_font_drawstr(font, color, x, y + high - linehigh, 0, self->input);
- // Draw cursor
- linew = al_get_ustr_width(font, self->input);
- al_draw_line(x + linew, y + high - linehigh, x + linew, y + high, color, 1);
- // draw start for debugging
- al_draw_textf(font, color, x, y, 0, "start: %d, size: %d", self->start,
- ustrlist_size(&self->text));
- return ZORI_HANDLE_OK;
- }
- /** Activates or deactivates the console. */
- void zori_console_active_(struct zori_console * self, int active) {
- if(!self) return;
- zori_widget_active_(&self->widget, active);
- zori_widget_visible_(&self->widget, active);
- }
- /** Returns nonzero if console is active zero if not. */
- int zori_console_active(struct zori_console * self) {
- if(!self) return 0;
- return zori_widget_active(&self->widget);
- }
- /** scrolls the console 1 step in the given direction. */
- int zori_console_scroll(struct zori_console * self, int direction) {
- if((!self) || (!direction)) return FALSE;
- if(direction < 0) self->start--;
- if(direction > 0) self->start++;
- /* Clamp start between 0 and size of list. */
- self->start = bad_clampi(self->start, 0, ustrlist_size(&self->text));
- return ZORI_HANDLE_OK;
- }
- /* Key input event handler for console. */
- int zori_console_handle_keychar(union zori_event * zevent) {
- struct zori_console * self = zori_widget_console(zori_event_widget(zevent));
- zori_system_event * event = zori_event_system_event(zevent);
- int ch = event->keyboard.unichar;
- int kc = event->keyboard.keycode;
- switch(kc) {
- // ignore the start-console key
- case ALLEGRO_KEY_F1:
- case ALLEGRO_KEY_F3:
- return ZORI_HANDLE_OK;
- case ALLEGRO_KEY_PGUP: return zori_console_scroll(self, 1);
- case ALLEGRO_KEY_PGDN: return zori_console_scroll(self, -1);
- case ALLEGRO_KEY_BACKSPACE:
- // remove last character typed.
- ustr_remove_chr(self->input, ustr_offset(self->input, -1));
- return ZORI_HANDLE_OK;
- break;
- case ALLEGRO_KEY_ENTER: {
- const char * command = ustr_c(self->input);
- // execute command
- if(zori_console_docommand(self, command)) {
- zori_console_puts(self, "Error in running comand");
- zori_console_puts(self, command);
- }
- ustr_truncate(self->input, 0);
- // empty string by truncating it
- return ZORI_HANDLE_OK;
- }
- default:
- break;
- }
-
- ustr_appendch(self->input, ch);
- return ZORI_HANDLE_OK;
- }
- /* Key down event handler for console. */
- int zori_console_handle_keydown(union zori_event * zevent) {
- struct zori_console * self = zori_widget_console(zori_event_widget(zevent));
- zori_system_event * event = zori_event_system_event(zevent);
- int ch = event->keyboard.unichar;
- int kc = event->keyboard.keycode;
- switch(kc) {
- case ALLEGRO_KEY_F1:
- case ALLEGRO_KEY_F3:
- zori_console_active_(self, false);
- /* disable console if F1 is pressed.
- * Note: this shouldnever happen if react is set up well.
- */
- return ZORI_HANDLE_OK;
- default:
- break;
- }
- return ZORI_HANDLE_IGNORE;
- }
- /* Mouse axe event handler for console */
- int zori_console_handle_mouseaxes(union zori_event * zevent) {
- struct zori_console * self = zori_widget_console(zori_event_widget(zevent));
- zori_system_event * event = zori_event_system_event(zevent);
- int z = event->mouse.dz;
- // only capture mouse scroll wheel...
- if(z == 0) return ZORI_HANDLE_IGNORE;
- if(z < 0) return zori_console_scroll(self, -1);
- if(z > 0) return zori_console_scroll(self, +1);
- return ZORI_HANDLE_OK;
- }
- int zori_console_handle_ignore(union zori_event * zevent) {
- return ZORI_HANDLE_IGNORE;
- }
- static struct zori_handler zori_console_actions[] = {
- { ZORI_SYSTEM_EVENT_KEY_DOWN , zori_console_handle_keydown , NULL },
- { ZORI_SYSTEM_EVENT_KEY_UP , zori_console_handle_ignore , NULL },
- { ZORI_SYSTEM_EVENT_KEY_CHAR , zori_console_handle_keychar , NULL },
- { ZORI_SYSTEM_EVENT_MOUSE_AXES, zori_console_handle_mouseaxes , NULL },
- { ZORI_EVENT_DRAW , zori_console_draw , NULL },
- { ZORI_EVENT_DONE , zori_console_handle_ignore , NULL },
- { ZORI_EVENT_FREE , zori_console_handle_ignore , NULL },
- { -1, NULL, NULL }
- };
- /** Let the console handle allegro events. Returns 0 if event was consumed,
- positive if not, and negative on error. */
- int zori_console_handle(struct zori_widget * widget, zori_system_event * sevent) {
- union zori_event zevent;
- if (!widget) return ZORI_HANDLE_ERROR;
- if (!zori_widget_active(widget)) return ZORI_HANDLE_IGNORE;
- return zori_widget_raise_system_event(widget, sevent);
- }
- /** Cleans up a console. */
- int zori_console_done(struct zori_widget * widget) {
- struct zori_console * self = zori_widget_console(widget);
- if(!self) return ZORI_HANDLE_IGNORE;
- free(self->buf);
- self->buf = NULL;
- ustr_free(self->input);
- self->input = NULL;
- ustrlist_done(&self->text);
- return ZORI_HANDLE_OK;
- }
- /** Deallocates a console. */
- int zori_console_free(struct zori_widget * widget) {
- struct zori_console * self = zori_widget_console(widget);
- zori_console_done(&self->widget);
- free(self);
- return ZORI_HANDLE_OK;
- }
- /** Allocates a console. */
- struct zori_console * zori_console_alloc() {
- return calloc(1 , sizeof(struct zori_console));
- }
- /* Amount of lines of display the console hat o keep track of. */
- #define ZORI_CONSOLE_MAX 200
- /** Initializes a console. */
- struct zori_console * zori_console_initall(struct zori_console * self, int id, zori_rebox * bounds, struct zori_style * style) {
- if(!self) return NULL;
- if(!zori_widget_initall(&self->widget, id, &the_zori_root->widget, bounds, style, 7, zori_console_actions)) {
- return NULL;
- }
- ustrlist_init(&self->text);
- // ustrlist_shiftcstr(&self->text, "empty line");
- zori_widget_active_(&self->widget, FALSE);
- self->count = 0;
- // max MUST be at least 2, 3 to see anything...
- self->max = ZORI_CONSOLE_MAX;
- self->start = 0;
- self->charw = 80;
- self->buf = calloc(self->charw + 1, 1);
- // one extra for NULL at end .
- if (!self->buf) { zori_console_done(&self->widget); return NULL; }
- self->input = ustr_new("");
- self->cursor= 0;
- if (!self->input) { zori_console_done(&self->widget); return NULL; }
- self->command = NULL;
- self->command_data = NULL;
- return self;
- }
- /** Initializes a new console. */
- struct zori_console * zori_console_new(int id, zori_rebox * bounds, struct zori_style * style) {
- struct zori_console * self = zori_console_alloc();
- if(!zori_console_initall(self, id, bounds, style)) {
- zori_console_free(&self->widget);
- return NULL;
- }
- return self;
- }
|