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);
%}
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);