Browse Source

Zori widget works for console, now implement a whole gui subsystem in it.

Beoran 7 years ago
parent
commit
e5b9807072
11 changed files with 105 additions and 35 deletions
  1. 1 0
      cmake/ErutaFiles.cmake
  2. 2 2
      include/rh.h
  3. 10 20
      include/zori.h
  4. 27 0
      include/zori_screen.h
  5. 2 2
      src/rh.c
  6. 0 1
      src/state.c
  7. 1 3
      src/toruby.c
  8. 1 2
      src/ui.c
  9. 13 5
      src/zori.c
  10. 28 0
      src/zori_screen.c
  11. 20 0
      test/test_zori_screen.c

+ 1 - 0
cmake/ErutaFiles.cmake

@@ -78,4 +78,5 @@ set(ERUTA_SRC_FILES
   src/utf8.c
   src/xresor.c
   src/zori.c
+  src/zori_screen.c
 )

+ 2 - 2
include/rh.h

@@ -13,7 +13,7 @@ typedef mrb_state Ruby;
 typedef struct Script_ Script;
 
 
-#include "widget.h"
+#include "zori.h"
 
 void toruby_Font_free(mrb_state * state , void * ptr );
 
@@ -71,7 +71,7 @@ int rh_load_main();
 int rh_on_start();
 int rh_on_reload(); 
 
-int rh_run_console_command(BBConsole * console, const char * command, void * extra);
+int rh_run_console_command(struct zori_console * console, const char * command, void * extra);
 
 #endif
 

+ 10 - 20
include/zori.h

@@ -39,6 +39,8 @@ typedef int zori_id;
 #define zori_font_drawstr(FONT, COLOR, X, Y, ATTR, TEXT) al_draw_ustr(FONT, COLOR, X, Y, ATTR, TEXT)
 
 
+
+/* Zori ID's. */
 #define ZORI_ID_OK_P(ID)  ((ID) > -1)
 #define ZORI_ID_OK        ((zori_id)(0))
 #define ZORI_ID_ERROR     ((zori_id)(-1))
@@ -51,10 +53,9 @@ typedef int zori_id;
  This macro returns, for the given pointer, a pointer to a containing struct
  of type TYPE, in which PTR is a member named MEMBER. 
  This enables cool ways of type genericity and extension in plain C.
- It does not run afoul of strict aliasing since it passes over a char * pointer 
+ It should not run afoul of strict aliasing since it passes over a char * pointer 
  and  a pointer of a containing struct or union.
 */
-
 #define ZORI_CONTAINER_OF(PTR, TYPE, MEMBER) \
         ((TYPE *)(((char *)(PTR)) - offsetof(TYPE, MEMBER)))
 
@@ -151,7 +152,6 @@ static inline zori_system_event * zori_event_system_event(union zori_event * eve
   return event->sys.sysev;
 }
 
-
 /* Helper functions to set widget for an event. */
 static inline struct zori_widget *  
 zori_event_set_widget(union zori_event * event, struct zori_widget * widget) {
@@ -159,20 +159,20 @@ zori_event_set_widget(union zori_event * event, struct zori_widget * widget) {
   return event->any.widget = widget;
 }
 
-
 /* Helper functions to set data for an event. */
 static inline void * zori_event_set_data(union zori_event * event, void * data) {
   if (!event) return NULL;
   return event->any.data = data;
 }
 
-
+/* A style part is a color, image and font applied to a part of the GUI. */
 struct zori_stylepart {
   zori_color       color;
   zori_bitmap    * image;
   zori_font      * font;
 };
 
+/* 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;
@@ -309,18 +309,6 @@ struct zori_root {
 /* Forward declaration of a page. */
 struct zori_page;
 
-/* The top level widget for a single display. */
-struct zori_screen { 
-  /* A screen is a widget. */ 
-  struct zori_widget widget;
-  /* It also manages the cursors. */
-  struct zori_cursors cursors;
-  /* Display this screen is on. */
-  zori_display * display; 
-  /* The GUI page that is active on this screen if any. */
-  struct zori_page * active_page; 
-};
-
 
 /* In Zori, the GUI is paginated. This means that on any 
  * screen, only a single GUI page can be active. The intent is to 
@@ -331,6 +319,9 @@ struct zori_page {
   struct zori_widget widget;
 };
 
+
+struct zori_root * zori_get_root(void);
+
 /* Initializes Zori and creates a top level widget. Returns 0 on success 
  * or negative on error. The style will be copied and set as default 
  * if it is not NULL. Otherwise a built-in style will be used. 
@@ -345,7 +336,6 @@ zori_id zori_shutdown();
 
 /* 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. */
@@ -420,9 +410,9 @@ 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, struct zori_widget *parent, zori_rebox *box, struct zori_style *style);
+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, 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, size_t amount, struct zori_handler *handlers);
 void zori_draw_all(void);
 int zori_widget_visible(struct zori_widget *widget);
 int zori_widget_active(struct zori_widget *widget);

+ 27 - 0
include/zori_screen.h

@@ -0,0 +1,27 @@
+#ifndef zori_screen_H_INCLUDED
+#define zori_screen_H_INCLUDED
+
+#include "zori.h"
+
+/* The top level widget for a single display. */
+struct zori_screen { 
+  /* A screen is a widget. */ 
+  struct zori_widget widget;
+  /* It also manages the cursors. */
+  struct zori_cursors cursors;
+  /* Display this screen is on. */
+  zori_display * display; 
+  /* The GUI page that is active on this screen if any. */
+  struct zori_page * active_page; 
+};
+
+
+zori_id zori_new_screen(zori_id id, zori_display * display);
+
+
+
+#endif
+
+
+
+

+ 2 - 2
src/rh.c

@@ -3,7 +3,7 @@
 #include "image.h"
 #include "rh.h"
 #include "fifi.h"
-#include "widget.h"
+#include "zori.h"
 #include "str.h"
 #include "mem.h"
 #include "state.h"
@@ -577,7 +577,7 @@ int rh_load_main() {
   
 
 /** For execution of ruby strings by the console */
-int rh_run_console_command(BBConsole * console, const char * command, void * extra)
+int rh_run_console_command(struct zori_console * console, const char * command, void * extra)
 {
   int res;
   LOG_ENABLE_CONSOLE();

+ 0 - 1
src/state.c

@@ -12,7 +12,6 @@
 #include "rh.h"
 #include "toruby.h"
 #include "event.h"
-#include "widget.h"
 #include "area.h"
 #include "thing.h"
 #include "sprite.h"

+ 1 - 3
src/toruby.c

@@ -161,8 +161,7 @@ static mrb_value tr_warn(mrb_state * mrb, mrb_value self) {
 
 /** Enables a certain log level */
 static mrb_value tr_log_enable(mrb_state * mrb, mrb_value self) {
-  State   * state   = NULL;
-  BBConsole * console = NULL;
+  State               * state   = NULL;
   
   (void) self; (void) mrb;
   
@@ -176,7 +175,6 @@ static mrb_value tr_log_enable(mrb_state * mrb, mrb_value self) {
 /** Disables a certain log level */
 static mrb_value tr_log_disable(mrb_state * mrb, mrb_value self) {
   State   * state   = NULL;
-  BBConsole * console = NULL;
   
   (void) self; (void) mrb;
 

+ 1 - 2
src/ui.c

@@ -1,7 +1,6 @@
 #include "eruta.h"
 #include "image.h"
 #include "ui.h"
-#include "widget.h"
 
 /*
 * UI handles the user interface of Eruta.
@@ -54,7 +53,7 @@
 /** UI manages the graphical user interface and menus. */
 struct UI_ {
   /* Ths UI is a widget itself. How meta that is! :) */
-  BBWidget widget;
+  /*BBWidget widget;*/
   /* ID generator. */
   int last_id; 
   /* The widgets in the UI, in a dynamic array. */

+ 13 - 5
src/zori.c

@@ -173,6 +173,10 @@ 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;
@@ -278,13 +282,16 @@ zori_widget_add_child(struct zori_widget * parent, struct zori_widget * child) {
 
 struct zori_widget * zori_widget_init
   ( struct zori_widget * widget, 
-    struct zori_widget * parent, zori_rebox * box, struct zori_style * style) {
+    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    = zori_get_unused_id();
+    widget->id    = ( (id < 0) ? widget->id : id);
     
     if (style) {
       widget->style = *style;
@@ -302,6 +309,7 @@ struct zori_widget * zori_widget_init
       widget->box = rebox_make(0, 0, 200, 100);
     }
     zori_widget_add_child(parent, widget);
+    zori_registry_add(the_zori_registry, widget->id, widget);
     return widget;
 }
 
@@ -326,10 +334,10 @@ zori_id zori_shutdown() {
 
 
 struct zori_widget * 
-zori_widget_initall(struct zori_widget * 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, parent, box, style)) {
+  if (zori_widget_init(widget, id, parent, box, style)) {
     zori_widget_add_handlers(widget, handlers, amount);
   }
   return widget;
@@ -916,7 +924,7 @@ struct zori_console * zori_console_alloc() {
 /** 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, &the_zori_root->widget, bounds, style, 7, zori_console_actions)) { 
+  if(!zori_widget_initall(&self->widget, id, &the_zori_root->widget, bounds, style, 7, zori_console_actions)) { 
     return NULL;
   }
   ustrlist_init(&self->text);

+ 28 - 0
src/zori_screen.c

@@ -0,0 +1,28 @@
+
+#include "zori_screen.h"
+
+struct zori_screen * zori_screen_new(zori_id id, zori_display * display) {
+  struct zori_screen * screen = NULL; zori_rebox box;
+  box.at.x = 0;
+  box.at.y = 0;
+  if (!display) return NULL;
+  screen = calloc(1, sizeof(*screen));
+  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, id, &zori_get_root()->widget, 
+                      &box, NULL, 0, NULL); 
+  return screen;
+}
+
+
+zori_id zori_new_screen(zori_id id, zori_display * display) {
+  struct zori_screen * screen = zori_screen_new(id, display);
+  if (!screen) return ZORI_ID_ENOMEM;
+  return screen->widget.id;
+}
+
+
+
+
+

+ 20 - 0
test/test_zori_screen.c

@@ -0,0 +1,20 @@
+/**
+* This is a test for zori_screen in $package$
+*/
+#include "si_test.h"
+#include "zori_screen.h"
+
+
+TEST_FUNC(zori_screen) {
+  TEST_DONE();
+}
+
+
+int main(void) {
+  TEST_INIT();
+  TEST_RUN(zori_screen);
+  TEST_REPORT();
+}
+
+
+