Commit fbfd2425 by beoran

Fiddling with buttons and menu results.

parent c6aeb37e
......@@ -22,6 +22,7 @@ int callrb_on_reload();
int callrb_on_update(State * self);
int callrb_on_gui(zori_id widget_id, int kind, void * data);
#endif
......
......@@ -121,6 +121,7 @@ typedef ALLEGRO_USTR zori_text;
/* Zori ID's. */
#define ZORI_ID_OK_P(ID) ((ID) > -1)
#define ZORI_ID_OK ((zori_id)(0))
#define ZOTI_ID_GENERATE ((zori_id)(-1))
#define ZORI_ID_ERROR ((zori_id)(-1))
#define ZORI_ID_ENOMEM ((zori_id)(-2))
#define ZORI_ID_EINVAL ((zori_id)(-3))
......@@ -284,22 +285,36 @@ static inline void * zori_event_set_data(union zori_event * event, void * data)
return event->any.data = data;
}
/* A style part is a color, image, font, and font flags applied to a part of the GUI. */
struct zori_stylepart {
/* Style flags */
enum zori_style_flag {
ZORI_STYLE_FLAG_BORDER = 1,
ZORI_STYLE_FLAG_FILL = 2,
ZORI_STYLE_FLAG_DEFAULT= (ZORI_STYLE_FLAG_BORDER | ZORI_STYLE_FLAG_FILL),
};
/* A graphic is a color, image, and style flags applied to a part of the GUI. */
struct zori_graphic_style {
zori_color color;
zori_bitmap * bitmap;
int style_flags;
};
/* A text style has all elements needed to style a piece of text. I consists of the text color, font and font flags flags applied to a part of the GUI.
*/
struct zori_text_style {
zori_color color;
zori_font * font;
int font_flags;
int font_flags;
};
/* A style is a set of style parts for different parts of the GUI. */
struct zori_style {
struct zori_stylepart fore;
struct zori_stylepart back;
struct zori_stylepart text;
struct zori_stylepart border;
struct zori_stylepart hover;
struct zori_stylepart mark;
struct zori_graphic_style fore;
struct zori_graphic_style back;
struct zori_graphic_style border;
struct zori_graphic_style hover;
struct zori_graphic_style mark;
struct zori_text_style text;
};
struct zori_widget;
......@@ -361,6 +376,9 @@ enum zori_flag {
ZORI_FLAG_HOVERED = 1 << 2,
/* The object is "marked" for activation by the keyjoy cursor */
ZORI_FLAG_MARKED = 1 << 3,
/* The object is ready to report a "result".
* What that is depends on the object. */
ZORI_FLAG_READY = 1 << 4,
};
/* Typedef for the type of a widget.
......@@ -381,6 +399,7 @@ struct zori_cursor {
struct zori_widget * focus;
zori_bitmap * bitmap;
enum zori_flag flags;
struct zori_style style;
};
......@@ -390,6 +409,21 @@ struct zori_cursors {
struct zori_cursor keyjoy;
};
/* The value of a result of a widget, see below. */
union zori_result_value {
int integer;
zori_string * string;
};
/* The "result" of a widget. If the flag ready is set, the
* widget has a result to report . This normally happens when it was
* clicked, or when a menu item was selected, */
struct zori_result {
int ready;
union zori_result_value value;
void * extra;
};
/*
on_enter
......@@ -445,8 +479,8 @@ struct zori_widget {
/* Type of the widget. */
zori_widget_type type;
/* Generic "result", of last operation on widget, normally zero. */
int result;
/* Generic "result", of last operation on widget. */
struct zori_result result;
/* Cannot easily use the handers for destroying the widget, so in stead
* provide a destructor. */
......@@ -539,84 +573,6 @@ void zori_update(double dt);
zori_id zori_register(zori_id id, zori_event_type type, zori_handler_func handler, void * extra);
int zori_registry_entry_compare(const void *v1, const void *v2);
zori_id zori_registry_init(struct zori_registry *registry);
zori_id zori_registry_add(struct zori_registry *registry, zori_id id, struct zori_widget *widget);
struct zori_registry_entry *zori_registry_lookup_entry(struct zori_registry *registry, zori_id id);
struct zori_widget *zori_registry_lookup(struct zori_registry *registry, zori_id id);
zori_id zori_registry_remove(struct zori_registry *registry, zori_id id);
int zori_handler_compare(const void *v1, const void *v2);
struct zori_handler *zori_handlers_add(struct zori_handlers *me, zori_event_type type, zori_handler_func *handler, void *data);
void zori_handlers_done(struct zori_handlers *me);
void zori_handlers_init(struct zori_handlers *me);
struct zori_handler *zori_handlers_search(struct zori_handlers *me, zori_event_type type);
int zori_handlers_handle(struct zori_handlers *me, union zori_event *event);
int zori_widget_raise_system_event(struct zori_widget *widget, zori_system_event *sysev);
int zori_widget_raise_draw_event(struct zori_widget *widget);
int zori_widget_raise_done_event(struct zori_widget *widget);
int zori_widget_raise_free_event(struct zori_widget *widget);
int zori_widget_raise_update_event(struct zori_widget *widget, double dt);
int zori_widget_raise_action_event(struct zori_widget *widget);
int zori_widget_raise_close_event(struct zori_widget *widget, struct zori_widget * from);
int zori_widget_compare(const void *v1, const void *v2);
struct zori_widget *zori_get_widget(zori_id id);
zori_id zori_get_unused_id(void);
struct zori_handler *zori_widget_add_handler(struct zori_widget *widget, zori_event_type type, zori_handler_func *handler, void *data);
struct zori_handler *zori_widget_add_handlers(struct zori_widget *widget, struct zori_handler *handlers, size_t amount);
zori_id zori_start(struct zori_style *default_style);
struct zori_widget *zori_widget_done(struct zori_widget *widget);
void zori_widget_free(struct zori_widget *widget);
struct zori_widget *zori_widget_add_child(struct zori_widget *parent, struct zori_widget *child);
struct zori_widget *zori_widget_init(struct zori_widget *widget, zori_widget_type type, zori_id id, struct zori_widget *parent, zori_rebox *box, struct zori_style *style);
zori_id zori_shutdown(void);
struct zori_widget *zori_widget_initall(struct zori_widget *widget, zori_widget_type type, zori_id id, struct zori_widget *parent, zori_rebox *box, struct zori_style *style, struct zori_handler *handlers, size_t amount);
void zori_draw_all(void);
int zori_widget_visible(struct zori_widget *widget);
int zori_widget_visible_(struct zori_widget *widget, int set);
int zori_widget_active(struct zori_widget *widget);
int zori_widget_active_(struct zori_widget *widget, int set);
int zori_widget_hover(struct zori_widget *widget);
int zori_widget_hover_(struct zori_widget *widget, int set);
int zori_widget_marked(struct zori_widget *widget);
int zori_widget_marked_(struct zori_widget *widget, int set);
int zori_widget_active_(struct zori_widget *widget, int set);
void zori_update(double dt);
zori_font *zori_widget_font(struct zori_widget *widget);
zori_color zori_widget_forecolor(struct zori_widget *widget);
zori_color zori_widget_backcolor(struct zori_widget *widget);
int zori_widget_h(struct zori_widget *widget);
int zori_widget_w(struct zori_widget *widget);
int zori_widget_x(struct zori_widget *widget);
int zori_widget_y(struct zori_widget *widget);
int zori_xy_inside_widget_p(struct zori_widget * widget, double x, double y);
zori_font * zori_widget_text_font(struct zori_widget * widget);
zori_font * zori_text_font(zori_id id);
void zori_widget_drawroundframe(struct zori_widget *self);
int zori_handle_system_event(zori_system_event * sysev);
int zori_result(int zori_id);
int zori_mark_widget(struct zori_widget * widget);
int zori_registry_entry_compare(const void *v1, const void *v2);
zori_id zori_registry_init(struct zori_registry *registry);
zori_id zori_registry_add(struct zori_registry *registry, zori_id id, struct zori_widget *widget);
struct zori_registry_entry *zori_registry_lookup_entry(struct zori_registry *registry, zori_id id);
struct zori_widget *zori_registry_lookup(struct zori_registry *registry, zori_id id);
zori_id zori_registry_remove(struct zori_registry *registry, zori_id id);
int zori_handler_compare(const void *v1, const void *v2);
struct zori_handler *zori_handlers_add(struct zori_handlers *me, zori_event_type type, zori_handler_func *handler, void *data);
void zori_handlers_done(struct zori_handlers *me);
......@@ -624,14 +580,17 @@ void zori_handlers_init(struct zori_handlers *me);
struct zori_handler *zori_handlers_search(struct zori_handlers *me, zori_event_type type);
int zori_handlers_handle(struct zori_handlers *me, union zori_event *event);
struct zori_root *zori_get_root(void);
struct zori_widget *zori_get_root_widget(void);
struct zori_widget *zori_get_widget(zori_id id);
zori_id zori_get_unused_id(void);
zori_id zori_initialize_root(void);
zori_id zori_start(struct zori_style *default_style);
zori_id zori_set_margins(zori_id id, int left, int top, int right, int bottom);
zori_id zori_set_margin(zori_id id, int size);
zori_id zori_set_paddings(zori_id id, int left, int top, int right, int bottom);
zori_id zori_set_padding(zori_id id, int size);
zori_font *zori_text_font(zori_id id);
void zori_destroy_root(void);
zori_id zori_shutdown(void);
void zori_draw_all(void);
int zori_handle_system_event(zori_system_event *sysev);
......@@ -639,6 +598,7 @@ void zori_update(double dt);
int zori_result(zori_id id);
#endif
......
......@@ -35,6 +35,8 @@ int zori_widget_hover(struct zori_widget *widget);
int zori_widget_hover_(struct zori_widget *widget, int set);
int zori_widget_marked(struct zori_widget *widget);
int zori_widget_marked_(struct zori_widget *widget, int set);
int zori_widget_ready(struct zori_widget *widget);
int zori_widget_ready_(struct zori_widget *widget, int set);
int zori_xy_inside_widget_p(struct zori_widget *widget, double x, double y);
zori_font *zori_widget_font(struct zori_widget *widget);
zori_color zori_widget_forecolor(struct zori_widget *widget);
......@@ -57,6 +59,7 @@ _Bool zori_widget_is_type_predicate(struct zori_widget *widget, void *extra);
struct zori_widget *zori_widget_get_parent_of_type(struct zori_widget *widget, zori_widget_type type);
struct zori_screen *zori_widget_get_screen(struct zori_widget *widget);
int zori_mark_widget(struct zori_widget *widget);
zori_id zori_widget_set_int_result(struct zori_widget * widget, int value);
#endif
......
......@@ -97,3 +97,15 @@ int callrb_on_update(State * self) {
return rh_tobool(res);
}
/* GUI vent handler. Calls the mruby callback eruta_on_gui. */
int callrb_on_gui(zori_id widget_id, int kind, void * data) {
mrb_value res;
State * state = state_get();
Ruby * ruby = state_ruby(state);
res = rh_run_toplevel(ruby, "eruta_on_gui", "ii",
widget_id, kind);
(void) data;
return rh_tobool(res);
}
......@@ -792,7 +792,11 @@ void state_update(State * self) {
}
if (self->area) {
area_update(self->area, state_frametime(self));
area_update(self->area, state_frametime(self));
}
/* if (self->ui) */ {
ui_state_update(&self->ui, self->display, self->font);
}
camera_update(self->camera);
......
......@@ -65,7 +65,7 @@ zori_font * font
ui->screen = zori_new_screen(-1, display);
if(!ZORI_ID_OK_P(ui->screen)) {
if (!ZORI_ID_OK_P(ui->screen)) {
LOG_ERROR( "Cannot set up main screen.");
return;
}
......@@ -73,7 +73,7 @@ zori_font * font
ui->main.page = zori_new_page(-1, ui->screen);
LOG_NOTE("Main page: %d\n", ui->main.page);
box = rebox_make(280, 80, 140, 240);
ui->main.menu = zori_new_menu(-1, ui->main.page, &box);
ui->main.menu = zori_new_menu(-1, ui->main.page, &box);
LOG_NOTE("Main menu: %d\n", ui->main.menu);
......@@ -90,8 +90,27 @@ zori_font * font
}
}
void ui_handle_main_menu(struct ui_state * ui, int selected) {
LOG_NOTE("Main menu selected: %d\n", selected);
}
void ui_state_update(struct ui_state * ui,
zori_display * display, zori_font * font) {
int value = 0;
if ((value = zori_result(ui->main.menu))) {
ui_handle_main_menu(ui, value);
}
}
#ifdef COMMENT_
/* Direct mode operation? */
INTRO_TEXT2 =
......
......@@ -79,6 +79,14 @@ struct zori_root * zori_get_root(void) {
return the_zori_root;
}
struct zori_widget * zori_get_root_widget(void) {
if (!the_zori_root) {
return NULL;
}
return &the_zori_root->widget;
}
struct zori_widget * zori_get_widget(zori_id id) {
struct zori_registry * registry = zori_get_registry();
if (!registry) return NULL;
......@@ -257,14 +265,41 @@ void zori_update(double dt)
}
/** Returns the "result" of a widget. 0 if no result was available
* because the widget was not "ready" or not "clicked" yet. */
int zori_result(zori_id id) {
int result = 0;
struct zori_widget * widget = zori_get_widget(id);
if (widget) {
result = widget->result;
widget->result = 0;
if (widget->result.ready) {
result = widget->result.value.integer;
widget->result.ready = 0;
}
}
return result;
}
/** Returns the "result" of a widget. 0 if no result was available
* because the widget was not "ready" or not "clicked" yet. */
int zori_get_result(zori_id id, struct zori_result * result) {
struct zori_widget * widget = zori_get_widget(id);
if (widget && result) {
if (widget->result.ready) {
(*result) = widget->result;
widget->result.ready = 0;
return result->ready;
}
}
return 0;
}
int zori_result_int(zori_id id, int * result);
int zori_result_string(zori_id id, char ** result, size_t * size);
int zori_result_buffer(zori_id id, char ** result, size_t * size);
......@@ -3,6 +3,7 @@
#include "zori_widget.h"
#include "zori_caption.h"
#include "zori_button.h"
#include "zori_menu.h"
struct zori_button * zori_widget_to_button(struct zori_widget * widget) {
if (!zori_widget_is_type(widget, ZORI_WIDGET_TYPE_BUTTON)) return NULL;
......@@ -25,15 +26,31 @@ int zori_button_on_mouse_axes(union zori_event * event) {
return ZORI_HANDLE_PASS;
}
/** Handles a mouse click or activation and set the
* button and it's parent's result. */
int zori_button_activate(struct zori_widget * widget) {
}
/** Handles a mouse click event and pass it on. */
int zori_button_on_mouse_click(union zori_event * event) {
struct zori_widget * widget = event->any.widget;
struct zori_button * button = zori_widget_to_button(widget);
struct zori_widget * parent = widget->parent;
float x = event->sys.ev->mouse.x;
float y = event->sys.ev->mouse.y;
if (zori_xy_inside_widget_p(widget, x, y)) {
widget->result = widget->id;
LOG_NOTE("Button %d clicked: %d, result %d.\n", widget->id, widget->result);
zori_widget_set_int_result(widget, widget->id);
LOG_NOTE("Button clicked: %d, result %d.\n", widget->id, widget->result.ready);
/* If the parent is a menu, also set it's result. */
if (zori_widget_is_type(parent, ZORI_WIDGET_TYPE_MENU)) {
zori_widget_set_int_result(parent, widget->id);
}
return zori_widget_raise_action_event(widget);
}
return ZORI_HANDLE_PASS;
......
......@@ -461,8 +461,8 @@ struct zori_console * zori_console_alloc() {
/** Destructor for console. */
void zori_console_destroy(struct zori_widget * widget) {
struct zori_console * console = zori_widget_to_console(widget);
zori_console_done(console);
/* struct zori_console * console = zori_widget_to_console(widget); */
zori_console_done(widget);
}
......
......@@ -446,7 +446,8 @@ int zori_longtext_on_mouse_click(union zori_event * event) {
float x = event->sys.ev->mouse.x;
float y = event->sys.ev->mouse.y;
if (zori_xy_inside_widget_p(widget, x, y)) {
widget->result = widget->id;
widget->result.ready = widget->id;
widget->result.value.string = longtext->text;
LOG_NOTE("Longtext %d clicked: %d, result %d.\n", widget->id, widget->result);
return zori_widget_raise_action_event(widget);
}
......
#include "monolog.h"
#include "zori.h"
#include "zori_widget.h"
#include "zori_caption.h"
......@@ -53,8 +54,11 @@ int zori_menu_select_first(struct zori_menu * menu) {
int zori_menu_activate_selected(struct zori_menu * menu) {
struct zori_widget * selected = zori_menu_get_selected(menu);
if (selected) {
LOG_NOTE("Item selected: %p!\n", selected);
return zori_widget_raise_action_event(selected);
}
LOG_WARNING("No item selected!\n");
return ZORI_HANDLE_IGNORE;
}
......@@ -64,6 +68,7 @@ int zori_menu_close(struct zori_menu * menu) {
parent = menu->widget.parent;
zori_widget_raise_close_event(parent, &menu->widget);
zori_widget_active_(&menu->widget, false);
zori_widget_visible_(&menu->widget, false);
return ZORI_HANDLE_DONE;
}
......
#include <allegro5/allegro_color.h>
#include "zori.h"
#include "zori_widget.h"
#include "zori_caption.h"
......@@ -37,7 +40,8 @@ int zori_screen_on_sysevent(union zori_event * event) {
}
void zori_draw_cursor(const struct zori_cursor * cursor, const struct zori_style * style) {
void zori_draw_cursor(const struct zori_cursor * cursor) {
const struct zori_style * style = &cursor->style;
if (cursor->bitmap) {
al_draw_bitmap(cursor->bitmap, cursor->p.x, cursor->p.y, 0);
} else {
......@@ -54,14 +58,14 @@ void zori_draw_cursor(const struct zori_cursor * cursor, const struct zori_style
};
void zori_draw_cursors(const struct zori_cursors * cursors, const struct zori_style * style) {
zori_draw_cursor(&cursors->keyjoy, style);
zori_draw_cursor(&cursors->mouse, style);
void zori_draw_cursors(const struct zori_cursors * cursors) {
zori_draw_cursor(&cursors->keyjoy);
zori_draw_cursor(&cursors->mouse);
};
int zori_screen_on_overdraw(union zori_event * event) {
struct zori_screen * screen = zori_widget_to_screen(event->any.widget);
zori_draw_cursors(&screen->cursors, &screen->widget.style);
zori_draw_cursors(&screen->cursors);
return ZORI_HANDLE_PASS;
}
......@@ -85,6 +89,13 @@ zori_screen_init(struct zori_screen * screen, zori_display * display) {
/* the keyjoy cursor is hidden off-screen to begin with. */
screen->cursors.keyjoy.p.x = - 64;
screen->cursors.keyjoy.p.y = - 64;
/* Copy the styles, but set them a bit different */
screen->cursors.keyjoy.style = screen->widget.style;
screen->cursors.mouse.style = screen->widget.style;
screen->cursors.keyjoy.style.back.color = al_map_rgb(0,0,255);
screen->cursors.keyjoy.style.fore.color = al_map_rgb(255,255,0);
screen->display = display;
screen->active_page = NULL;
......@@ -100,8 +111,9 @@ struct zori_screen * zori_screen_new(zori_id id, zori_display * display) {
if (!screen) return NULL;
box.size.x = al_get_display_width(display);
box.size.y = al_get_display_height(display);
zori_widget_initall(&screen->widget, ZORI_WIDGET_TYPE_SCREEN, id, &zori_get_root()->widget,
&box, NULL, ZORI_ARRAY_AND_AMOUNT(zori_screen_handlers));
zori_widget_initall(&screen->widget, ZORI_WIDGET_TYPE_SCREEN, id,
zori_get_root_widget(), &box, NULL,
ZORI_ARRAY_AND_AMOUNT(zori_screen_handlers));
if (!zori_screen_init(screen, display)) {
free(screen);
screen = NULL;
......
......@@ -213,25 +213,25 @@ void zori_widget_draw_background(struct zori_widget * widget) {
struct zori_style * style = &widget->style;
zori_bitmap * background = style->back.bitmap;
zori_color color = style->back.color;
struct zori_stylepart * part = & style->back;
struct zori_graphic_style * graphic_style = & style->back;
dx = rebox_x(&widget->box);
dy = rebox_y(&widget->box);
dw = rebox_w(&widget->box);
dh = rebox_h(&widget->box);
if (zori_widget_hover(widget)) {
part = &style->hover;
graphic_style = &style->hover;
}
if (zori_widget_marked(widget)) {
part = &style->mark;
graphic_style = &style->mark;
}
if (part->bitmap) {
background = part->bitmap;
if (graphic_style->bitmap) {
background = graphic_style->bitmap;
}
color = part->color;
color = graphic_style->color;
if (background) {
image_blitscale9(background, dx, dy, dw, dh, -1, -1);
......@@ -252,7 +252,7 @@ int zori_widget_active(struct zori_widget * widget) {
}
int zori_widget_active_(struct zori_widget * widget, int set) {
if (set) {
if (set) {
widget->flags = widget->flags & (~ZORI_FLAG_DISABLED);
} else {
widget->flags = widget->flags | ZORI_FLAG_DISABLED;
......@@ -261,7 +261,7 @@ int zori_widget_active_(struct zori_widget * widget, int set) {
}
int zori_widget_visible_(struct zori_widget * widget, int set) {
if (set) {
if (set) {
widget->flags = widget->flags & (~ZORI_FLAG_HIDDEN);
} else {
widget->flags = widget->flags | ZORI_FLAG_HIDDEN;
......@@ -275,7 +275,7 @@ int zori_widget_hover(struct zori_widget * widget) {
}
int zori_widget_hover_(struct zori_widget * widget, int set) {
if (set) {
if (set) {
widget->flags = widget->flags | ZORI_FLAG_HOVERED;
} else {
widget->flags = widget->flags & (~ZORI_FLAG_HOVERED);
......@@ -288,12 +288,25 @@ int zori_widget_marked(struct zori_widget * widget) {
}
int zori_widget_marked_(struct zori_widget * widget, int set) {
if (set) {
if (set) {
widget->flags = widget->flags | ZORI_FLAG_MARKED;
} else {
widget->flags = widget->flags & (~ZORI_FLAG_MARKED);
}
return zori_widget_hover(widget);
return zori_widget_marked(widget);
}
int zori_widget_ready(struct zori_widget * widget) {
return widget && ((widget->flags & ZORI_FLAG_READY) == ZORI_FLAG_READY);
}
int zori_widget_ready_(struct zori_widget * widget, int set) {
if (set) {
widget->flags = widget->flags | ZORI_FLAG_READY;
} else {
widget->flags = widget->flags & (~ZORI_FLAG_READY);
}
return zori_widget_ready(widget);
}
......@@ -491,5 +504,14 @@ int zori_mark_widget(struct zori_widget * widget) {
return ZORI_HANDLE_DONE;
}
zori_id zori_widget_set_int_result(struct zori_widget * widget, int value) {
if (widget) {
widget->result.value.integer = value;
widget->result.ready = widget->id;
return widget->id;
}
return ZORI_ID_EINVAL;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment