Browse Source

Check in forgoten files from Zori gui reorganization.

Beoran 7 years ago
parent
commit
202c1bf036
5 changed files with 193 additions and 16 deletions
  1. 26 16
      data/script/main.rb
  2. 16 0
      include/zori/zori_registry.h
  3. 11 0
      include/zori/zori_style.h
  4. 104 0
      src/zori/zori_registry.c
  5. 36 0
      src/zori/zori_style.c

+ 26 - 16
data/script/main.rb

@@ -464,6 +464,12 @@ module Main
     end
     return nil
   end
+  
+  # Handle events from the C GUI side
+  def on_gui(widget_id, kind)
+    # 
+    puts "Received GUI event: #{widget_id}, #{kind}"
+  end 
 
   # Make all methods and constants are available as Main.XXX
   extend self
@@ -530,24 +536,28 @@ def eruta_on_update(dt)
   Timer.update
 end
 
-# Called when an input event occurs.
+# Called when an input event occurs. 
+# Note that the C side GUI will capture input events while it is active.
 def eruta_on_poll(*args)
-  # Send to Zori ui first. If it returns non-nil the event is handled,
-  # otherwise, pass on to key handler.
-  res = Zori.on_event(*args)
-  if res
-    return nil
+  type = args.shift
+  meth = "on_#{type}".to_sym
+  if Main.respond_to?(meth)
+    Main.send(meth, *args)
   else
-    type = args.shift
-    meth = "on_#{type}".to_sym
-    if Main.respond_to?(meth)
-      Main.send(meth, *args)
-    else
-      # ignore
-      log_to "input", "#{__FILE__.split('/').last}:#{__LINE__}: input: #{type} #{meth} #{args}"
-      # puts "input #{type} #{meth} #{args}"
-    end
-  end
+    # ignore
+    log_to "input", "#{__FILE__.split('/').last}:#{__LINE__}: input: #{type} #{meth} #{args}"
+     # puts "input #{type} #{meth} #{args}"
+  end  
 end
 
+# Called when a GUI event occurs
+def eruta_on_gui(*args)
+  type = args.shift
+  meth = "on_gui".to_sym
+  if Main.respond_to?(meth)
+    Main.send(meth, *args)
+  else
+    raise "Main must support on_gui!"
+  end  
+end
 

+ 16 - 0
include/zori/zori_registry.h

@@ -0,0 +1,16 @@
+#ifndef zori_registry_H_INCLUDED
+#define zori_registry_H_INCLUDED
+
+
+int zori_registry_entry_compare(const void *v1, const void *v2);
+zori_id zori_registry_init(struct zori_registry *registry);
+void zori_registry_destroy(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);
+zori_id zori_initialize_registry(void);
+zori_id zori_destroy_registry(void);
+struct zori_registry *zori_get_registry(void);
+
+#endif

+ 11 - 0
include/zori/zori_style.h

@@ -0,0 +1,11 @@
+#ifndef zori_style_H_INCLUDED
+#define zori_style_H_INCLUDED
+
+struct zori_style *zori_get_default_style(void);
+void zori_destroy_default_style(void);
+zori_id zori_initialize_default_style(void);
+struct zori_style *zori_get_default_style(void);
+void zori_destroy_default_style(void);
+
+#endif
+

+ 104 - 0
src/zori/zori_registry.c

@@ -0,0 +1,104 @@
+
+#include "zori.h"
+#include "zori_registry.h"
+
+/* registry functionality */
+
+/** Compare registry entries. */
+int zori_registry_entry_compare(const void * v1, const void * v2) {
+  const struct zori_registry_entry * entry1 = v1;
+  const struct zori_registry_entry * entry2 = v2;
+  return (entry2->id - entry1->id);
+}
+
+/** Initialize the registry. */
+zori_id zori_registry_init(struct zori_registry * registry) {
+  miao_init(registry);
+  return ZORI_ID_OK;
+}
+
+/** Destroy the registry */
+void zori_registry_destroy(struct zori_registry * registry) {
+  if(registry) { 
+    miao_done(registry);
+  }
+}
+
+
+/** Add an entry to the registry. */
+zori_id 
+zori_registry_add(struct zori_registry * registry, zori_id id, 
+struct zori_widget * widget) {
+  struct zori_registry_entry entry = { id, widget };
+  if (miao_push(registry, entry)) { 
+    miao_qsort(registry, zori_registry_entry_compare);
+    return ZORI_ID_OK;
+  }
+  return ZORI_ID_ENOMEM;
+}
+
+/** Look up an entry in the registry. */
+struct zori_registry_entry *  
+zori_registry_lookup_entry(struct zori_registry * registry, zori_id id) {
+  struct zori_registry_entry key = { id, NULL };
+  return miao_bsearch(registry, zori_registry_entry_compare, &key);
+}
+
+
+/** Look up a widget in the registry. */
+struct zori_widget *  
+zori_registry_lookup(struct zori_registry * registry, zori_id id) {
+  struct zori_widget * result = NULL;
+  struct zori_registry_entry * entry = NULL;
+  
+  entry = zori_registry_lookup_entry(registry, id);
+  if (entry) { 
+    result = entry->widget;
+  }
+  return result;
+}
+
+
+
+/** Remove an entry from the registry. */
+zori_id 
+zori_registry_remove(struct zori_registry * registry, zori_id id) {
+  struct zori_registry_entry * entry = NULL;
+  entry = zori_registry_lookup_entry(registry, id);
+  if (entry) {
+    miao_delete_entry(registry, entry);
+  }
+  return ZORI_ID_OK;
+}
+
+/** The global registry for Zori. */
+static struct zori_registry * the_zori_registry = NULL;
+
+/** Intializes the global registry for zori. */
+zori_id zori_initialize_registry() {
+  the_zori_registry = calloc(1, sizeof(*the_zori_registry));
+  if (!the_zori_registry) return ZORI_ID_ENOMEM;
+  return zori_registry_init(the_zori_registry);
+}
+
+/** Destroys the global registry for zori. */
+zori_id zori_destroy_registry() {
+  zori_registry_destroy(the_zori_registry); 
+  free(the_zori_registry);
+  the_zori_registry = NULL;
+  return ZORI_ID_OK;
+}
+
+
+/** Returns the global registry for Zori. */
+struct zori_registry * zori_get_registry() {
+  return the_zori_registry;
+}
+
+
+
+
+
+
+
+

+ 36 - 0
src/zori/zori_style.c

@@ -0,0 +1,36 @@
+#include <allegro5/allegro_color.h>
+
+
+#include "zori.h"
+#include "zori_style.h"
+
+static struct zori_style * the_default_style = NULL;
+
+/** Initialize the global default style. */
+zori_id zori_initialize_default_style(void) {
+  the_default_style = calloc(1, sizeof(*the_default_style));
+  if (!the_default_style) return ZORI_ID_ENOMEM;
+ 
+  the_default_style->text.font      = al_create_builtin_font();  
+  the_default_style->text.color     = al_color_name("white");
+  the_default_style->text.font_flags= ALLEGRO_ALIGN_LEFT;
+  the_default_style->border.color   = al_color_name("white");  
+  the_default_style->back.color     = al_color_name("green");
+  the_default_style->fore.color     = al_color_name("white");  
+  the_default_style->mark.color     = al_color_name("lightgreen");
+  the_default_style->hover.color    = al_color_name("yellowgreen");  
+
+  return ZORI_ID_OK;
+}
+
+
+/** Returns the golbal default style */
+struct zori_style * zori_get_default_style() {
+  return the_default_style;
+}
+
+/** Destroys the global default style. */
+void zori_destroy_default_style(void) {
+  free(the_default_style);
+  the_default_style = NULL;
+}