Commit 177647ed by beoran

ZORI Gui wwidgets may now have captions to simplify text handling.

parent 6c3e2755
#ifndef zori_button_H_INCLUDED
#define zori_button_H_INCLUDED
#include <zori.h>
#include "zori.h"
#include "zori_caption.h"
#define ZORI_WIDGET_TYPE_BUTTON ZORI_WIDGET_TYPE('z','b','u','t')
struct zori_button {
struct zori_widget widget;
zori_string * text;
int align;
struct zori_widget widget;
struct zori_caption caption;
int align;
};
......@@ -17,10 +20,9 @@ int zori_button_on_mouse_axes(union zori_event *event);
int zori_button_on_mouse_click(union zori_event *event);
void zori_draw_button(struct zori_button *button);
int zori_button_on_draw(union zori_event *event);
struct zori_button *zori_button_text_(struct zori_button *button, zori_string *text);
struct zori_button *zori_button_init(struct zori_button *button, zori_string *text);
struct zori_button *zori_button_new(zori_id id, zori_id parent, zori_box *box, zori_string *text);
zori_id zori_new_button(zori_id id, zori_id parent, zori_box *box, zori_string *text);
struct zori_button *zori_button_init(struct zori_button *button, const char *text);
struct zori_button *zori_button_new(zori_id id, zori_id parent, zori_box *box, const char *text);
zori_id zori_new_button(zori_id id, zori_id parent, zori_box *box, const char * text);
#endif
......
......@@ -2,9 +2,24 @@
#define zori_caption_H_INCLUDED
/* Caption helper struct for use by wigdets that have captions. */
struct zori_caption {
zori_string * text;
struct zori_style * style;
zori_box box;
};
#endif
struct zori_caption *
zori_caption_set(struct zori_caption * caption, const zori_string * text);
struct zori_caption *
zori_caption_set_cstr(struct zori_caption * caption, const char * cstr);
struct zori_caption *zori_caption_init(struct zori_caption *caption, const char *cstr);
void zori_caption_draw(const struct zori_caption *caption, const zori_rebox *box, const struct zori_style *style);
void zori_caption_done(struct zori_caption *caption);
#endif
......@@ -23,6 +23,8 @@
#include "zori_screen.h"
#include "zori_console.h"
#include "zori_button.h"
#include "zori_page.h"
#include "zori_menu.h"
#include "ui.h"
......@@ -525,17 +527,13 @@ State * state_init_gui(State * self, BOOL fullscreen) {
{
USTR_INFO si;
USTR * bs = ustr_refcstr(&si, "Button 1");
Rebox box = rebox_make(300, 100, 100, 60);
zori_id button = zori_new_button(-1, main_menu, &box, bs);
zori_id button = zori_new_button(-1, main_menu, &box, "Button 1");
LOG_NOTE("Button: %d\n", button);
}
{
USTR_INFO si;
USTR * bs = ustr_refcstr(&si, "Button 2");
Rebox box = rebox_make(300, 200, 100, 60);
zori_id button = zori_new_button(-1, main_menu, &box, bs);
zori_id button = zori_new_button(-1, main_menu, &box, "Button 2");
LOG_NOTE("Button: %d\n", button);
}
......
......@@ -47,12 +47,7 @@ void zori_draw_button(struct zori_button * button) {
h = rebox_h(&button->widget.inner);
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);
}
zori_caption_draw(&button->caption, &button->widget.inner, &button->widget.style);
};
int zori_button_on_draw(union zori_event * event) {
......@@ -72,24 +67,22 @@ struct zori_handler zori_button_handlers[] = {
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);
}
if (button) {
zori_caption_set(&button->caption, 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);
zori_button_init(struct zori_button * button,const char * text) {
if (button) {
zori_caption_init(&button->caption, text);
}
return button;
}
struct zori_button * zori_button_new(zori_id id, zori_id parent_id,
zori_box * box, zori_string * text) {
zori_box * box, const char * text) {
struct zori_button * button = NULL;
button = calloc(1, sizeof(*button));
if (!button) return NULL;
......@@ -105,7 +98,7 @@ struct zori_button * zori_button_new(zori_id id, zori_id parent_id,
}
zori_id zori_new_button(zori_id id, zori_id parent, zori_box * box, zori_string * text) {
zori_id zori_new_button(zori_id id, zori_id parent, zori_box * box, const char * text) {
struct zori_button * button = zori_button_new(id, parent, box, text);
if (!button) return ZORI_ID_ENOMEM;
return button->widget.id;
......
#include "zori.h"
#include "zori_caption.h"
#include "monolog.h"
struct zori_caption *
zori_caption_set(struct zori_caption * caption, const zori_string * text) {
if (caption) {
if (caption->text) {
ustr_free(caption->text);
caption->text = NULL;
}
if (text) {
caption->text = ustr_dup(text);
if (!caption->text) {
LOG_ERROR("Out of memory in caption setup.");
}
}
}
return caption;
}
struct zori_caption *
zori_caption_set_cstr(struct zori_caption * caption, const char * cstr) {
const USTR * ustr;
USTR_INFO info;
ustr = ustr_refcstr(&info, cstr);
return zori_caption_set(caption, ustr);
}
struct zori_caption *
zori_caption_init(struct zori_caption * caption, const char * cstr) {
caption->text = NULL;
return zori_caption_set_cstr(caption, cstr);
}
void
zori_caption_draw(
const struct zori_caption * caption,
const zori_rebox * box,
const struct zori_style * style) {
if (caption->text) {
zori_font * font = style->text.font;
zori_color color = style->text.color;
float x = box->at.x;
float y = box->at.y;
float w = box->size.x;
al_draw_multiline_ustr(font, color, x, y, w, -1, ALLEGRO_ALIGN_LEFT, caption->text);
}
}
void zori_caption_done(struct zori_caption * caption) {
zori_caption_set(caption, NULL);
}
......
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