Example #1
0
void luaW_pushfaivariant(lua_State* L, variant val) {
	if(val.is_int()) {
		lua_pushinteger(L, val.as_int());
	} else if(val.is_decimal()) {
		lua_pushnumber(L, val.as_decimal() / 1000.0);
	} else if(val.is_string()) {
		const std::string result_string = val.as_string();
		lua_pushlstring(L, result_string.c_str(), result_string.size());
	} else if(val.is_list()) {
		lua_newtable(L);
		for(const variant& v : val.as_list()) {
			lua_pushinteger(L, lua_rawlen(L, -1) + 1);
			luaW_pushfaivariant(L, v);
			lua_settable(L, -3);
		}
	} else if(val.is_map()) {
		typedef std::map<variant,variant>::value_type kv_type;
		lua_newtable(L);
		for(const kv_type& v : val.as_map()) {
			luaW_pushfaivariant(L, v.first);
			luaW_pushfaivariant(L, v.second);
			lua_settable(L, -3);
		}
	} else if(val.is_callable()) {
		// First try a few special cases
		if(unit_callable* u_ref = val.try_convert<unit_callable>()) {
			const unit& u = u_ref->get_unit();
			unit_map::iterator un_it = resources::gameboard->units().find(u.get_location());
			if(&*un_it == &u) {
				luaW_pushunit(L, u.underlying_id());
			} else {
				luaW_pushunit(L, u.side(), u.underlying_id());
			}
		} else if(location_callable* loc_ref = val.try_convert<location_callable>()) {
			luaW_pushlocation(L, loc_ref->loc());
		} else {
			// If those fail, convert generically to a map
			const formula_callable* obj = val.as_callable();
			std::vector<formula_input> inputs;
			obj->get_inputs(&inputs);
			lua_newtable(L);
			for(const formula_input& attr : inputs) {
				if(attr.access == FORMULA_WRITE_ONLY) {
					continue;
				}
				lua_pushstring(L, attr.name.c_str());
				luaW_pushfaivariant(L, obj->query_value(attr.name));
				lua_settable(L, -3);
			}
		}
	} else if(val.is_null()) {
		lua_pushnil(L);
	}
}
Example #2
0
widget_ptr create(const variant& v, game_logic::formula_callable* e)
{
	if(v.is_callable()) {
		widget_ptr w = v.try_convert<gui::widget>();
		ASSERT_LOG(w != NULL, "Error converting widget from callable.");
		return w;
	}
	ASSERT_LOG(v.is_map(), "TYPE ERROR: widget must be specified by a map, found: " << v.to_debug_string());
	std::string wtype = v["type"].as_string();
	if(wtype == "animation_widget") {
		return widget_ptr(new gui::animation_widget(v,e));
#ifndef NO_EDITOR
	} else if(wtype == "animation_preview") {
		return widget_ptr(new gui::animation_preview_widget(v,e));
#endif
	} else if(wtype == "border_widget") {
		return widget_ptr(new gui::border_widget(v,e));
	} else if(wtype == "button") {
		return widget_ptr(new gui::button(v,e));
	} else if(wtype == "checkbox") {
		return widget_ptr(new gui::checkbox(v,e));
	} else if(wtype == "dialog") {
		return widget_ptr(new gui::dialog(v,e));
#ifndef NO_EDITOR
	} else if(wtype == "drag_widget") {
		return widget_ptr(new gui::drag_widget(v,e));
#endif
	} else if(wtype == "graphical_font_label") {
		return widget_ptr(new gui::graphical_font_label(v,e));
	} else if(wtype == "grid") {
		return widget_ptr(new gui::grid(v,e));
	} else if(wtype == "image") {
		return widget_ptr(new gui::image_widget(v,e));
	} else if(wtype == "section") {
		return widget_ptr(new gui::gui_section_widget(v,e));
	} else if(wtype == "key_button") {
		return widget_ptr(new gui::key_button(v,e));
	} else if(wtype == "label") {
		return widget_ptr(new gui::label(v,e));
	} else if(wtype == "poly_line_widget") {
		return widget_ptr(new gui::poly_line_widget(v,e));
	} else if(wtype == "rich_text_label") {
		return widget_ptr(new gui::rich_text_label(v,e));
	} else if(wtype == "tileset_preview") {
		return widget_ptr(new gui::preview_tileset_widget(v,e));
	} else if(wtype == "scrollbar") {
		return widget_ptr(new gui::scrollbar_widget(v,e));
	} else if(wtype == "slider") {
		return widget_ptr(new gui::slider(v,e));
	} else if(wtype == "text_editor") {
		return widget_ptr(new gui::text_editor_widget(v,e));
	} else if(wtype == "progress") {
		return widget_ptr(new gui::progress_bar(v, e));
	} else if(wtype == "selector") {
		return widget_ptr(new gui::selector_widget(v, e));
	} else if(wtype == "object") {
		return widget_ptr(new gui::custom_object_widget(v, e));
	} else if(wtype == "bar") {
		return widget_ptr(new gui::bar_widget(v, e));
	} else if(wtype == "color_picker") {
		return widget_ptr(new gui::color_picker(v, e));
	} else if(wtype == "layout") {
		return widget_ptr(new gui::layout_widget(v, e));
	} else if(wtype == "file_chooser") {
		return widget_ptr(new gui::file_chooser_dialog(v, e));
#if defined(USE_ISOMAP)
	} else if(wtype == "view3d") {
		return widget_ptr(new gui::view3d_widget(v, e));
#endif
	//} else if(wtype == "scrollable") {
	//} else if(wtype == "widget") {
	} else {
		ASSERT_LOG(true, "Unable to create a widget of type " << wtype);
		return widget_ptr();
	}
}