/** * \brief Implementation of sol.text_surface.create(). * \param l The Lua context that is calling this function. * \return Number of values to return to Lua. */ int LuaContext::text_surface_api_create(lua_State* l) { return LuaTools::exception_boundary_handle(l, [&] { std::shared_ptr<TextSurface> text_surface = std::make_shared<TextSurface>(0, 0); if (lua_gettop(l) > 0) { LuaTools::check_type(l, 1, LUA_TTABLE); const std::string& font_id = LuaTools::opt_string_field( l, 1, "font", FontResource::get_default_font_id() ); RenderingMode rendering_mode = LuaTools::opt_enum_field<RenderingMode>( l, 1, "rendering_mode", rendering_mode_names, RenderingMode::SOLID ); HorizontalAlignment horizontal_alignment = LuaTools::opt_enum_field<HorizontalAlignment>( l, 1, "horizontal_alignment", horizontal_alignment_names, HorizontalAlignment::LEFT ); VerticalAlignment vertical_alignment = LuaTools::opt_enum_field<VerticalAlignment>( l, 1, "vertical_alignment", vertical_alignment_names, VerticalAlignment::MIDDLE ); const Color& color = LuaTools::opt_color_field(l, 1, "color", Color::white); int font_size = LuaTools::opt_int_field(l, 1, "font_size", TextSurface::default_font_size); const std::string& text = LuaTools::opt_string_field(l, 1, "text", ""); const std::string& text_key = LuaTools::opt_string_field(l, 1, "text_key", ""); if (!FontResource::exists(font_id)) { LuaTools::error(l, std::string("No such font: '") + font_id + "'"); } text_surface->set_font(font_id); text_surface->set_rendering_mode(rendering_mode); text_surface->set_horizontal_alignment(horizontal_alignment); text_surface->set_vertical_alignment(vertical_alignment); text_surface->set_text_color(color); text_surface->set_font_size(font_size); if (!text.empty()) { text_surface->set_text(text); } else if (!text_key.empty()) { if (!CurrentQuest::string_exists(text_key)) { LuaTools::error(l, std::string("No value with key '") + text_key + "' in strings.dat for language '" + CurrentQuest::get_language() + "'" ); } text_surface->set_text(CurrentQuest::get_string(text_key)); } } get_lua_context(l).add_drawable(text_surface); push_text_surface(l, *text_surface); return 1; }); }
/** * \brief Implementation of sol.text_surface.create(). * \param l The Lua context that is calling this function. * \return Number of values to return to Lua. */ int LuaContext::text_surface_api_create(lua_State* l) { TextSurface* text_surface = new TextSurface(0, 0); if (lua_gettop(l) > 0) { luaL_checktype(l, 1, LUA_TTABLE); // Traverse the table, looking for properties. lua_pushnil(l); // First key. while (lua_next(l, 1) != 0) { const std::string& key = luaL_checkstring(l, 2); if (key == "font") { const std::string& font_id = luaL_checkstring(l, 3); if (!TextSurface::has_font(font_id)) { LuaTools::error(l, std::string("No such font: '") + font_id + "'"); } text_surface->set_font(font_id); } else if (key == "rendering_mode") { TextSurface::RenderingMode mode = LuaTools::check_enum<TextSurface::RenderingMode>(l, 3, rendering_mode_names); text_surface->set_rendering_mode(mode); } else if (key == "horizontal_alignment") { TextSurface::HorizontalAlignment alignment = LuaTools::check_enum<TextSurface::HorizontalAlignment>(l, 3, horizontal_alignment_names); text_surface->set_horizontal_alignment(alignment); } else if (key == "vertical_alignment") { TextSurface::VerticalAlignment alignment = LuaTools::check_enum<TextSurface::VerticalAlignment>(l, 3, vertical_alignment_names); text_surface->set_vertical_alignment(alignment); } else if (key == "color") { Color color = LuaTools::check_color(l, 3); text_surface->set_text_color(color); } else if (key == "text") { const std::string& text = luaL_checkstring(l, 3); text_surface->set_text(text); } else if (key == "text_key") { const std::string& text_key = luaL_checkstring(l, 3); if (!StringResource::exists(text_key)) { delete text_surface; LuaTools::error(l, std::string("No value with key '") + text_key + "' in strings.dat for language '" + Language::get_language() + "'" ); } text_surface->set_text(StringResource::get_string(text_key)); } else { delete text_surface; LuaTools::error(l, std::string("Invalid key '") + key + "' for text surface properties"); } lua_pop(l, 1); // Pop the value, let the key for the iteration. } } get_lua_context(l).add_drawable(text_surface); push_text_surface(l, *text_surface); return 1; }