Browse Source

Improvied gui somewhat

Beoran 7 years ago
parent
commit
c0d1a2752f
6 changed files with 164 additions and 10 deletions
  1. 26 2
      include/zori/zori.h
  2. 6 0
      include/zori/zori_widget.h
  3. 71 0
      src/store.c
  4. 12 2
      src/zori/zori.c
  5. 6 3
      src/zori/zori_menu.c
  6. 43 3
      src/zori/zori_widget.c

+ 26 - 2
include/zori/zori.h

@@ -159,12 +159,25 @@ enum zori_custom_event_type {
   ZORI_EVENT_DONE,
   /** Destruction event. */
   ZORI_EVENT_FREE,
-  /** Activation event, such as a button being clicked or a menu being opened. */
+  /** User definable activation event, such as a button being clicked 
+   * or a menu being opened. */
   ZORI_EVENT_ACTION, 
-  /** Close event, such as a menu being closed. Not for cleanup.*/
+  /** User definable close event, such as a menu being closed. 
+   * Note that the element may be re-opened later. */
   ZORI_EVENT_CLOSE,
   /** Child added event to ease setup. */
   ZORI_EVENT_NEW_CHILD,
+  
+  /* Below these line are internal events that should not be set or 
+   * activated by the user of the Zori library.  */
+  /** Activation event, such as a button being clicked or a menu being opened. 
+   * This event is ment to be used internally by the ZORI library before 
+   * the ZORI_EVENT_ACTION is raised. */
+  ZORI_INTERNAL_EVENT_ACTION,
+  
+  /** Raised just before closing a widget. Sent to the sub-widgets to indicate 
+   * that they should close as well. */
+  ZORI_INTERNAL_EVENT_CLOSE,
 };
 
 
@@ -409,10 +422,20 @@ struct zori_cursors {
   struct zori_cursor keyjoy;
 };
 
+
+/* The type of the value of the result of a widget. */
+enum zori_result_type {
+  ZORI_RESULT_TYPE_NONE   = 0,
+  ZORI_RESULT_TYPE_INTEGER= 1,
+  ZORI_RESULT_TYPE_STRING = 2,
+  ZORI_RESULT_TYPE_CLOSED = 3,
+};
+
 /* The value of a result of a  widget, see below. */
 union zori_result_value {
   int           integer;
   zori_string * string;
+  int           closed;
 };
 
 /* The "result" of a widget. If the flag ready is set, the 
@@ -421,6 +444,7 @@ union zori_result_value {
 struct zori_result {
   int                     ready;
   union zori_result_value value;
+  enum zori_result_type   type;
   void                  * extra;
 };
 

+ 6 - 0
include/zori/zori_widget.h

@@ -17,6 +17,8 @@ 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_internal_action_event(struct zori_widget * widget);
+int zori_widget_raise_internal_close_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_handler *zori_widget_add_handler(struct zori_widget *widget, zori_event_type type, zori_handler_func *handler, void *data);
@@ -27,6 +29,8 @@ struct zori_widget *zori_widget_add_child(struct zori_widget *parent, struct zor
 zori_id zori_widget_margins_(struct zori_widget *widget, int left, int top, int right, int bottom);
 zori_id zori_widget_margin_(struct zori_widget *widget, int size);
 void zori_widget_draw_background(struct zori_widget *widget);
+enum zori_handle_result 
+zori_widget_must_draw_children(struct zori_widget * widget);
 int zori_widget_visible(struct zori_widget *widget);
 int zori_widget_active(struct zori_widget *widget);
 int zori_widget_active_(struct zori_widget *widget, int set);
@@ -60,6 +64,8 @@ struct zori_widget *zori_widget_get_parent_of_type(struct zori_widget *widget, z
 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);
+zori_id zori_widget_set_string_result(struct zori_widget * widget, zori_string * value);
+zori_id zori_widget_set_closed_result(struct zori_widget * widget, int value);
 
 
 #endif

+ 71 - 0
src/store.c

@@ -261,5 +261,76 @@ int store_get_unused_id(int minimum) {
   return -3;
 }
 
+#define STORE_WITH_ID(INDEX, MIN, FUN, ARGS)  \
+  { int INDEX = store_get_unused_id(MIN);       \
+  if (FUN ARGS) {                             \
+    return index;                             \
+  } else {                                    \
+    return -1;                                \
+  }}
+
+/* Loads a font and puts it in the store. .  */
+int store_load_ttf_font_stretch_id(int min, const char * vpath, int w, int h, int f) {
+  STORE_WITH_ID(index, min, store_load_ttf_font_stretch, (index, vpath, w, h, f))
+}
+
+/* Loads a font and puts it in the store.  */
+int 
+store_load_ttf_font_id(int min, const char * vpath, int h, int f) {  
+  STORE_WITH_ID(index, min, store_load_ttf_font, (index, vpath, h, f))
+}
+
+/* Loads a font and puts it in the store.  */
+int
+store_load_bitmap_font_flags_id(int min, const char * vpath, int f) {  
+  STORE_WITH_ID(index, min, store_load_bitmap_font_flags, (index, vpath, f))
+}
+
+/* Loads a font and puts it in the store.  */
+int
+store_load_bitmap_font_id(int min, const char * vpath) {  
+  STORE_WITH_ID(index, min, store_load_bitmap_font, (index, vpath))
+}
+
+/* Grabs a font from a bitmap resouce at bmp_index and puts it in the store */
+
+int store_grab_font_id(int min, int from, int count, int ranges[]) {
+  STORE_WITH_ID(index, min, store_grab_font, (index, from, count, ranges));
+}
+
+
+/* Loads an audio stream and puts it in the storage. */
+int  store_load_audio_stream_id
+(int min, const char * vpath, size_t buffer_count, int samples) {
+  STORE_WITH_ID(index, min, store_load_audio_stream, (index, vpath, buffer_count, samples));
+}
+
+/* Loads a sample and puts it in the storage. */
+int store_load_sample_id(int min, const char * vpath) {
+  STORE_WITH_ID(index, min, store_load_sample, (index, vpath));
+}
+
+/* Loads a bitmap and puts it in the  storage. */
+int store_load_bitmap_flags_id
+(int min, const char * vpath, int flags) {
+  STORE_WITH_ID(index, min, store_load_bitmap_flags, (index, vpath, flags));
+}
+
+/* Loads a bitmap and puts it in the storage. */
+int store_load_bitmap_id(int min, const char * vpath) {
+  STORE_WITH_ID(index, min, store_load_bitmap, (index, vpath));
+}
+
+/* Loads "other" data and puts it in the storage. */
+int
+store_load_other_id(int min, const char* vpath, ResorKind kind, ResorLoader* loader, 
+                 ResorDestructor* destroy, void* extra) {
+  STORE_WITH_ID(index, min,  store_load_other, (index, vpath, kind, loader, destroy, extra));
+}
+
+/* Loads a tile map and puts it in the storage. */
+int store_load_tilemap_id(int min, const char * vpath) {
+  STORE_WITH_ID(index, min, store_load_tilemap, (index, vpath));
+}
 
 

+ 12 - 2
src/zori/zori.c

@@ -266,13 +266,23 @@ 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. */
+ * because the widget was not "ready" or not "clicked" yet.
+ * Result will be negative if the widget was closed. 
+ */
 int zori_result(zori_id id) {
   int result = 0;
   struct zori_widget * widget = zori_get_widget(id);
   if (widget) {
     if (widget->result.ready) {
-      result = widget->result.value.integer;
+      switch (widget->result.type) { 
+          case ZORI_RESULT_TYPE_INTEGER:
+            result = widget->result.value.integer;
+          break;
+          
+          default:
+            result = - widget->result.type;
+          break;
+      }    
       widget->result.ready = 0;
     }
   }

+ 6 - 3
src/zori/zori_menu.c

@@ -56,7 +56,7 @@ int zori_menu_activate_selected(struct zori_menu * menu) {
   if (selected) { 
     LOG_NOTE("Item selected: %p!\n", selected);
 
-    return zori_widget_raise_action_event(selected);
+    return zori_widget_raise_internal_action_event(selected);
   }
   LOG_WARNING("No item selected!\n");
   return ZORI_HANDLE_IGNORE;
@@ -69,6 +69,7 @@ int zori_menu_close(struct zori_menu * menu)  {
   zori_widget_raise_close_event(parent, &menu->widget);
   zori_widget_active_(&menu->widget, false);
   zori_widget_visible_(&menu->widget, false);
+  zori_widget_set_closed_result(&menu->widget, 1);
   return ZORI_HANDLE_DONE;
 }
 
@@ -119,8 +120,10 @@ int zori_menu_on_key_down(union zori_event * event) {
 
 int zori_menu_on_draw(union zori_event * event) {
   struct zori_widget * widget = event->any.widget;
-  zori_widget_draw_background(widget);
-  return ZORI_HANDLE_PASS;
+  if (zori_widget_visible(widget)) { 
+    zori_widget_draw_background(widget);
+  }
+  return zori_widget_must_draw_children(widget);
 }
 
 struct zori_handler zori_menu_handlers[] = {

+ 43 - 3
src/zori/zori_widget.c

@@ -29,8 +29,8 @@ union zori_event * event) {
     event->any.widget = widget;
     return zori_handlers_handle(&widget->handlers, event);
   }
-  /* If the event is not accepted, it is passed on to the child widgets. */
-  return ZORI_HANDLE_PASS;
+  /* If the event is not accepted, it is NOT passed on to the child widgets. */
+  return ZORI_HANDLE_DONE;
 }
 
 /* Raises an event on the widget, and if necessary, propagates it on to it's 
@@ -113,6 +113,13 @@ int zori_widget_raise_action_event
   return zori_widget_raise_event(widget, &event);
 }
 
+int zori_widget_raise_internal_action_event
+(struct zori_widget * widget) {
+  union zori_event event;
+  event.action.any.type   = ZORI_INTERNAL_EVENT_ACTION;
+  event.action.any.widget = widget;
+  return zori_widget_raise_event(widget, &event);
+}
 
 int zori_widget_raise_close_event
 (struct zori_widget * widget, struct zori_widget * from) {
@@ -240,7 +247,20 @@ void zori_widget_draw_background(struct zori_widget * widget) {
     }
 }
 
-
+enum zori_handle_result 
+zori_widget_must_draw_children(struct zori_widget * widget) {
+  
+  
+  float dx = rebox_x(&widget->box);
+  float dy = rebox_y(&widget->box);
+  
+  if (zori_widget_visible(widget)) {
+    draw_roundbox(dx,  dy, 10, 10, 3, 3, al_map_rgb(64,16,255), 3);
+    return ZORI_HANDLE_PASS;
+  }
+  draw_roundbox(dx, dy, 10, 10, 3, 3, al_map_rgb(255,16,64), 3);
+  return ZORI_HANDLE_DONE;
+}
 
 
 int zori_widget_visible(struct zori_widget * widget)  {
@@ -507,11 +527,31 @@ int zori_mark_widget(struct zori_widget * widget) {
 zori_id zori_widget_set_int_result(struct zori_widget * widget, int value) {
   if (widget) { 
     widget->result.value.integer = value;
+    widget->result.type          = ZORI_RESULT_TYPE_INTEGER;
     widget->result.ready         = widget->id;
     return widget->id;
   }
   return ZORI_ID_EINVAL;
 }
 
+zori_id zori_widget_set_string_result(struct zori_widget * widget, 
+  zori_string * value) {
+  if (widget) { 
+    widget->result.value.string  = value;
+    widget->result.type          = ZORI_RESULT_TYPE_STRING;
+    widget->result.ready         = widget->id;
+    return widget->id;
+  }
+  return ZORI_ID_EINVAL;
+}
 
+zori_id zori_widget_set_closed_result(struct zori_widget * widget, int value) {
+  if (widget) { 
+    widget->result.value.closed  = value;
+    widget->result.type          = ZORI_RESULT_TYPE_CLOSED;
+    widget->result.ready         = widget->id;
+    return widget->id;
+  }
+  return ZORI_ID_EINVAL;
+}