]> mj.ucw.cz Git - leo.git/commitdiff
Lua bindings for object attributes and basic style properties
authorMartin Mares <mj@ucw.cz>
Tue, 9 Jun 2015 11:00:47 +0000 (13:00 +0200)
committerMartin Mares <mj@ucw.cz>
Tue, 9 Jun 2015 11:00:47 +0000 (13:00 +0200)
expr.i
poskole.css
style.c
style.h

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 = &prop;
+}
+
 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);
 %}
 
index 49cc444f1df55f9f82a19f358982e6d83dc7aad1..1fe8b285377c18d6555d99b4f651709d6c3ada2d 100644 (file)
@@ -709,5 +709,10 @@ way[hack=raisetext] {
 /*** Experiments ***/
 
 node {
-       @{print(t.name)} ;
+       @{
+               if s["z-index"] ~= nil then
+                       s["z-index"] = s["z-index"] + 0.9
+                       print(s["z-index"])
+               end
+       } ;
 }
diff --git a/style.c b/style.c
index 7c203ad902ce960315b97f809072e7f1d53eda26..96f2c2220c990f4c1794b1b00f5dc3caefa5ccfb 100644 (file)
--- a/style.c
+++ b/style.c
@@ -143,6 +143,12 @@ void style_set_by_layer(struct style_results *r, layer_t layer, struct style_pro
   style_assign(style_prop_lookup(si->hash, p->key), p);
 }
 
+struct style_prop *style_get_by_layer(struct style_results *r, layer_t layer, prop_t key)
+{
+  struct style_info *si = style_get_info(r, layer);
+  return style_get(si, key);
+}
+
 void style_set(struct style_info *si, struct style_prop *p)
 {
   style_assign(style_prop_lookup(si->hash, p->key), p);
diff --git a/style.h b/style.h
index eded29a9261dbccb47a84668877f8096e61e0f84..3f4d11bd0ad24bc3ff3cb3f6810696fdf39643be 100644 (file)
--- a/style.h
+++ b/style.h
@@ -71,6 +71,7 @@ void style_end(struct style_results *r);
 void style_enable_default_layer(struct style_results *r);
 
 void style_set_by_layer(struct style_results *r, layer_t layer, struct style_prop *p);
+struct style_prop *style_get_by_layer(struct style_results *r, layer_t layer, prop_t key);
 
 void style_set(struct style_info *si, struct style_prop *p);
 struct style_prop *style_get(struct style_info *si, prop_t key);