Commit 7d875c99 by beoran

Work on GUI subsystem, move console to it's own C file.

parent 06e3ed99
......@@ -14,13 +14,6 @@ struct Rebox_ {
};
/* This file was generated with:
'cfunctions -c -aoff -n -w rebox_proto src/rebox.c' */
#ifndef CFH_REBOX_PROTO
#define CFH_REBOX_PROTO
/* From 'src/rebox.c': */
Rebox rebox_make (int x , int y , int w , int h );
Rebox * rebox_initbounds (Rebox * self , Rebox bounds );
......@@ -67,12 +60,9 @@ double rebox_delta_x (Rebox * self , Rebox * other );
double rebox_delta_y (Rebox * self , Rebox * other );
#endif /* CFH_REBOX_PROTO */
int rebox_coordinates_inside_p(Rebox * self, double x, double y);
int rebox_point_inside_p(Rebox * self, Point * point);
#endif
......@@ -15,10 +15,13 @@
/* Macros for possible later system library portability. */
#define ZORI_EVENT_TYPE_IS_USER(T) ALLEGRO_EVENT_TYPE_IS_USER(T)
#define ZORI_SYSTEM_EVENT_KEY_DOWN ALLEGRO_EVENT_KEY_DOWN
#define ZORI_SYSTEM_EVENT_KEY_UP ALLEGRO_EVENT_KEY_UP
#define ZORI_SYSTEM_EVENT_KEY_CHAR ALLEGRO_EVENT_KEY_CHAR
#define ZORI_SYSTEM_EVENT_MOUSE_AXES ALLEGRO_EVENT_MOUSE_AXES
#define ZORI_SYSTEM_EVENT_KEY_DOWN ALLEGRO_EVENT_KEY_DOWN
#define ZORI_SYSTEM_EVENT_KEY_UP ALLEGRO_EVENT_KEY_UP
#define ZORI_SYSTEM_EVENT_KEY_CHAR ALLEGRO_EVENT_KEY_CHAR
#define ZORI_SYSTEM_EVENT_MOUSE_AXES ALLEGRO_EVENT_MOUSE_AXES
#define ZORI_SYSTEM_EVENT_MOUSE_AXES ALLEGRO_EVENT_MOUSE_AXES
#define ZORI_SYSTEM_EVENT_MOUSE_BUTTON_DOWN ALLEGRO_EVENT_MOUSE_BUTTON_DOWN
#define ZORI_SYSTEM_EVENT_MOUSE_BUTTON_UP ALLEGRO_EVENT_MOUSE_BUTTON_UP
/* Typedefs for possible later system library portability. */
......@@ -32,8 +35,11 @@ typedef ALLEGRO_DISPLAY zori_display;
typedef Point zori_point;
typedef Rebox zori_rebox;
typedef Rebox zori_box;
typedef int zori_id;
typedef ALLEGRO_USTR zori_string;
typedef ALLEGRO_USTR zori_text;
/* Macros for possible portability. */
#define zori_font_lineheight(FONT) al_get_font_line_height(FONT)
......@@ -53,11 +59,11 @@ typedef ALLEGRO_USTR zori_string;
/** Macro for the number of elements of an array .*/
#define ZORI_ARRAY_AMOUNT(A) ((sizeof(A))/(sizeof(*A)))
#define ZORI_ARRAY_AMOUNT(A) ((size_t)(sizeof(A))/(sizeof(*A)))
/** Macro that expands to the amount of elemnts in the array and the array
* itself, in that order. */
#define ZORI_AMOUNT_AND_ARRAY(A) ZORI_ARRAY_AMOUNT(A), (A)
/** Macro that expands to the the array itself, and the amount of elements
* in that array, in that order. Only works on a statically sized array. */
#define ZORI_ARRAY_AND_AMOUNT(A) (A), ZORI_ARRAY_AMOUNT(A)
/* Macro: ZORI_CONTAINER_OF(PTR, TYPE, MEMBER)
......@@ -256,6 +262,10 @@ enum zori_flag {
ZORI_FLAG_DISABLED = 1 << 1,
/* The object is both hidden and disabled. */
ZORI_FLAG_DEACTIVATED = ZORI_FLAG_HIDDEN | ZORI_FLAG_DISABLED,
/* The object is being hovered by the mouse cursor. */
ZORI_FLAG_HOVERED = 1 << 2,
/* The object is "marked" for actovation by the keyjoy cursor */
ZORI_FLAG_MARKED = 1 << 3,
};
......@@ -435,10 +445,15 @@ 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_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_id id, struct zori_widget *parent, zori_rebox *box, struct zori_style *style, size_t amount, struct zori_handler *handlers);
struct zori_widget *zori_widget_initall(struct zori_widget *widget, 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_active(struct zori_widget *widget);
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);
......@@ -448,50 +463,11 @@ 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);
void zori_widget_drawroundframe(struct zori_widget *self);
typedef int (zori_console_command)(struct zori_widget *, const char * command, void * data);
/* A console is a console for command-line interaction and error display. When it's active it captures all input (as long as it's active) */
struct zori_console {
struct zori_widget widget;
USTRList text;
int count;
int max;
int start;
int charw;
int cursor;
char * buf;
USTR * input;
zori_console_command * command; // called when a command has been entered, if set.
void * command_data; // command data.
};
int zori_xy_inside_widget_p(struct zori_widget * widget, double x, double y);
void zori_widget_drawroundframe(struct zori_widget *self);
struct zori_console *zori_widget_console(struct zori_widget *widget);
int zori_console_docommand(struct zori_console *self, const char *text);
int zori_console_addstr(struct zori_console *self, const char *str);
int zori_console_addustr(struct zori_console *self, const ALLEGRO_USTR *ustr);
int zori_console_puts(struct zori_console *self, const char *str);
int zori_console_vprintf(struct zori_console *self, const char *format, va_list args);
int zori_console_printf(struct zori_console *self, const char *format, ...);
int zori_console_draw(union zori_event *zevent);
void zori_console_active_(struct zori_console *self, int active);
int zori_console_active(struct zori_console *self);
int zori_console_scroll(struct zori_console *self, int direction);
int zori_console_handle_keychar(union zori_event *zevent);
int zori_console_handle_keydown(union zori_event *zevent);
int zori_console_handle_mouseaxes(union zori_event *zevent);
int zori_console_handle_ignore(union zori_event *zevent);
int zori_console_handle(struct zori_widget *widget, zori_system_event *sevent);
int zori_console_done(struct zori_widget *widget);
int zori_console_free(struct zori_widget *widget);
struct zori_console *zori_console_alloc(void);
struct zori_console *zori_console_initall(struct zori_console *self, int id, zori_rebox *bounds, struct zori_style *style);
struct zori_console *zori_console_new(int id, zori_rebox *bounds, struct zori_style *style);
int zori_handle_system_event(zori_system_event * sysev);
......
......@@ -5,7 +5,7 @@
struct zori_button {
struct zori_widget widget;
zori_string * caption;
zori_string * text;
int align;
};
......
#ifndef zori_console_H_INCLUDED
#define zori_console_H_INCLUDED
typedef int (zori_console_command)(struct zori_widget *, const char * command, void * data);
/* A console is a console for command-line interaction and error display. When it's active it captures all input (as long as it's active) */
struct zori_console {
struct zori_widget widget;
USTRList text;
int count;
int max;
int start;
int charw;
int cursor;
char * buf;
USTR * input;
zori_console_command * command; // called when a command has been entered, if set.
void * command_data; // command data.
};
struct zori_console *zori_widget_console(struct zori_widget *widget);
int zori_console_docommand(struct zori_console *self, const char *text);
int zori_console_addstr(struct zori_console *self, const char *str);
int zori_console_addustr(struct zori_console *self, const ALLEGRO_USTR *ustr);
int zori_console_puts(struct zori_console *self, const char *str);
int zori_console_vprintf(struct zori_console *self, const char *format, va_list args);
int zori_console_printf(struct zori_console *self, const char *format, ...);
int zori_console_draw(union zori_event *zevent);
void zori_console_active_(struct zori_console *self, int active);
int zori_console_active(struct zori_console *self);
int zori_console_scroll(struct zori_console *self, int direction);
int zori_console_handle_keychar(union zori_event *zevent);
int zori_console_handle_keydown(union zori_event *zevent);
int zori_console_handle_mouseaxes(union zori_event *zevent);
int zori_console_handle_ignore(union zori_event *zevent);
int zori_console_handle(struct zori_widget *widget, zori_system_event *sevent);
int zori_console_done(struct zori_widget *widget);
int zori_console_free(struct zori_widget *widget);
struct zori_console *zori_console_alloc(void);
struct zori_console *zori_console_initall(struct zori_console *self, int id, zori_rebox *bounds, struct zori_style *style);
struct zori_console *zori_console_new(int id, zori_rebox *bounds, struct zori_style *style);
#endif
......
#ifndef zori_widget_H_INCLUDED
#define zori_widget_H_INCLUDED
void zori_widget_draw_background(struct zori_widget * widget);
#endif
......
......@@ -127,8 +127,21 @@ Point rebox_center_(Rebox * self, Point center) {
return rebox_at_(self, at);
}
/* Returns TRUE if the coordinates x and y are inside self, FALSE if not. */
int rebox_coordinates_inside_p(Rebox * self, double x, double y) {
if (x < self->at.x) return FALSE;
if (y < self->at.y) return FALSE;
if (x > rebox_br_x(self)) return FALSE;
if (y > rebox_br_y(self)) return FALSE;
return TRUE;
}
/* Returns TRUE if the point is inside self, FALSE if not. */
int rebox_point_inside_p(Rebox * self, Point * point) {
return rebox_coordinates_inside_p(self, point->x, point->y);
}
/* Returns TRUE if box 2 if other is wholly inside self, FALSE if not. */
int rebox_inside_p(Rebox * self, Rebox * other) {
if (other->at.x < self->at.x) return FALSE;
......
......@@ -21,7 +21,7 @@
#include "store.h"
#include "zori.h"
#include "zori_screen.h"
#include "zori_console.h"
/* Sub data struct for the particular GUI state */
struct GuiState {
......
#include "zori.h"
#include "zori_widget.h"
#include "zori_button.h"
struct zori_button * zori_widget_to_button(struct zori_widget * widget) {
return ZORI_CONTAINER_OF(widget, struct zori_button, widget);
}
/** Handles a mouse axis event and pass it on. */
int zori_button_on_mouse_axes(union zori_event * event) {
struct zori_widget * widget = event->any.widget;
struct zori_button * button = zori_widget_to_button(widget);
float x = event->sys.ev->mouse.x;
float y = event->sys.ev->mouse.y;
if (zori_xy_inside_widget_p(widget, x, y)) {
zori_widget_hover_(widget, TRUE);
} else {
zori_widget_hover_(widget, FALSE);
}
return ZORI_HANDLE_PASS;
}
/** 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);
float x = event->sys.ev->mouse.x;
float y = event->sys.ev->mouse.y;
if (zori_xy_inside_widget_p(widget, x, y)) {
return zori_widget_raise_action_event(widget);
}
return ZORI_HANDLE_PASS;
}
void zori_draw_button(struct zori_button * button) {
float x, y, w, h;
struct zori_style * style = &button->widget.style;
x = rebox_x(&button->widget.box) + 5;
y = rebox_y(&button->widget.box) + 5;
w = rebox_w(&button->widget.box) - 10;
h = rebox_h(&button->widget.box) - 10;
zori_widget_draw_background(&button->widget);
if (button->text) {
zori_font * font = style->text.font;
zori_color color = style->text.color;
al_draw_multiline_ustr(font, color, x, y, w, -1, ALLEGRO_ALIGN_LEFT, button->text);
}
};
int zori_button_on_draw(union zori_event * event) {
struct zori_button * button = zori_widget_to_button(event->any.widget);
zori_draw_button(button);
return ZORI_HANDLE_PASS;
}
struct zori_handler zori_button_handlers[] = {
{ ZORI_SYSTEM_EVENT_MOUSE_BUTTON_DOWN , zori_button_on_mouse_click , NULL },
{ ZORI_SYSTEM_EVENT_MOUSE_AXES , zori_button_on_mouse_axes , NULL },
{ ZORI_EVENT_DRAW , zori_button_on_draw , NULL },
{ -1, NULL, NULL }
};
struct zori_button *
zori_button_text_(struct zori_button * button, zori_string * text) {
if (button->text) {
al_ustr_free(button->text);
button->text = NULL;
}
if (text) {
button->text = al_ustr_dup(text);
}
return button;
}
struct zori_button *
zori_button_init(struct zori_button * button, zori_string * text) {
button->text = NULL;
return zori_button_text_(button, text);
}
struct zori_button * zori_button_new(zori_id id, zori_id parent,
zori_box * box, zori_string * text) {
struct zori_button * button = NULL;
button = calloc(1, sizeof(*button));
if (!button) return NULL;
zori_widget_initall(&button->widget, id, zori_get_widget(parent),
box, NULL, ZORI_ARRAY_AND_AMOUNT(zori_button_handlers));
if (!zori_button_init(button, text)) {
free(button);
button = NULL;
}
return button;
}
zori_id zori_new_button(zori_id id, zori_id parent, zori_box * box, zori_string * text) {
struct zori_button * button = zori_button_new(id, parent, box, text);
if (!button) return ZORI_ID_ENOMEM;
return button->widget.id;
}
......@@ -12,7 +12,7 @@ struct zori_page * zori_page_new(zori_id id, struct zori_widget * parent) {
page = calloc(1, sizeof(*page));
if (!page) return NULL;
zori_widget_initall(&page->widget, id, parent,
NULL, NULL, 0, NULL);
NULL, NULL, NULL, 0);
return page;
}
......
......@@ -75,6 +75,10 @@ struct zori_handler zori_screen_handlers[] = {
struct zori_screen *
zori_screen_init(struct zori_screen * screen, zori_display * display) {
memset(&screen->cursors, 0, sizeof(screen->cursors));
/* the keyjoy cursor is hidden off-screen to begin with. */
screen->cursors.keyjoy.p.x = - 64;
screen->cursors.keyjoy.p.y = - 64;
screen->display = display;
screen->active_page = NULL;
return screen;
......@@ -90,7 +94,7 @@ struct zori_screen * zori_screen_new(zori_id id, zori_display * display) {
box.size.x = al_get_display_width(display);
box.size.y = al_get_display_height(display);
zori_widget_initall(&screen->widget, id, &zori_get_root()->widget,
&box, NULL, ZORI_AMOUNT_AND_ARRAY(zori_screen_handlers));
&box, NULL, ZORI_ARRAY_AND_AMOUNT(zori_screen_handlers));
if (!zori_screen_init(screen, display)) {
free(screen);
screen = NULL;
......
#include "zori.h"
#include "zori_widget.h"
#include "draw.h"
/* Draws a widget's frame based on it's style. If the background bitmap is set
* then that is used, otherwise, the background color is used, including a border
* if needed. */
void zori_widget_draw_background(struct zori_widget * widget) {
float dx, dy, dw, dh;
struct zori_style * style = &widget->style;
zori_bitmap * background = style->back.image;
dx = rebox_x(&widget->box);
dy = rebox_y(&widget->box);
dw = rebox_w(&widget->box);
dh = rebox_h(&widget->box);
if (background) {
image_blitscale9(background, dx, dy, dw, dh, -1, -1);
} else {
draw_frame(dx, dy, dw, dh, 3, style->border.color, style->back.color);
}
}
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