]> mj.ucw.cz Git - leo.git/blobdiff - expr.i
Lua bindings for object attributes and basic style properties
[leo.git] / expr.i
diff --git a/expr.i b/expr.i
index 14baf32849165b99e1a1ec02cce90e807917bd69..d37615438d52b79785d052d487dcd454207c0ffe 100644 (file)
--- a/expr.i
+++ b/expr.i
@@ -67,23 +67,87 @@ static void push_proxy_style(lua_State *L, struct proxy_style *s)
   SWIG_NewPointerObj(L, s, SWIGTYPE_p_proxy_style, 0);
 }
 
-static const char *proxy_style_index(struct proxy_style *s, const char *key)
+static struct style_prop *proxy_style_index(struct proxy_style *s, const char *key)
 {
-  return NULL;
+  prop_t k = style_prop_encode(key);
+  return style_get_by_layer(s->sr, s->layer, k);
+}
+
+static void proxy_style_newindex(struct proxy_style *s, const char *key, struct style_prop *p)
+{
+  p->key = style_prop_encode(key);
+  style_set_by_layer(s->sr, s->layer, p);
+}
+
+static void push_style_prop(lua_State *L, struct style_prop *p)
+{
+  if (!p)
+    {
+      lua_pushnil(L);
+      return;
+    }
+  switch (p->type)
+    {
+    case PROP_TYPE_STRING:
+      lua_pushstring(L, osm_val_decode(p->val.id));
+      break;
+    case PROP_TYPE_NUMBER:
+      lua_pushnumber(L, (lua_Number) p->val.number);
+      break;
+    default:
+      msg(L_WARN, "Property %s has value with no LUA representation", style_prop_decode(p->key));
+      lua_pushnil(L);
+    }
+}
+
+static const char *decode_style_prop(lua_State *L, int idx, struct style_prop *p)
+{
+  if (lua_isnil(L, idx))
+    return "Cannot set property to nil. Yet."; // FIXME
+
+  if (lua_isnumber(L, idx))                    // FIXME: numbers vs. strings
+    {
+      p->type = PROP_TYPE_NUMBER;
+      p->val.number = lua_tonumber(L, idx);
+      return NULL;
+    }
+
+  if (lua_isstring(L, idx))
+    {
+      p->type = PROP_TYPE_STRING;
+      p->val.id = osm_val_encode(lua_tostring(L, idx));
+      return NULL;
+    }
+
+  return "Invalid type of style property";
 }
 
 %}
 
+%typemap(out) struct style_prop * { push_style_prop(L, $1); SWIG_arg++; }
+
+%typemap(in) struct style_prop * (const char *err, struct style_prop prop) {
+  err = decode_style_prop(L, $input, &prop);
+  if (err)
+    {
+      lua_pushstring(L, err);
+      SWIG_fail;
+    }
+  $1 = ∝
+}
+
 struct proxy_style { };
 
 %extend proxy_style {
-  const char *index(const char *key);
+  struct style_prop *index(const char *key);
+  void newindex(const char *key, struct style_prop *p);
 };
 
 %init %{
   // This overrides default index function generated by SWIG.
   SWIG_Lua_get_class_metatable(L, "proxy_style");
   SWIG_Lua_add_function(L, "__index", _wrap_proxy_style_index);
+  SWIG_Lua_add_function(L, "__newindex", _wrap_proxy_style_newindex);
   lua_pop(L, 1);
 %}