From 85ec4cfe5d56c96ac753d9682370f8e1c115a8e1 Mon Sep 17 00:00:00 2001 From: Martin Mares Date: Tue, 9 Jun 2015 13:00:47 +0200 Subject: [PATCH] Lua bindings for object attributes and basic style properties --- expr.i | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++--- poskole.css | 7 +++++- style.c | 6 +++++ style.h | 1 + 4 files changed, 80 insertions(+), 4 deletions(-) diff --git a/expr.i b/expr.i index 14baf32..d376154 100644 --- 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); %} diff --git a/poskole.css b/poskole.css index 49cc444..1fe8b28 100644 --- a/poskole.css +++ b/poskole.css @@ -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 7c203ad..96f2c22 100644 --- 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 eded29a..3f4d11b 100644 --- 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); -- 2.39.2