Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Squirrel: Default argument support for exposed C++ functions #3025

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions src/object/camera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -803,25 +803,25 @@ Camera::set_scale(float scale)
void
Camera::set_scale_anchor(float scale, int anchor)
{
ease_scale_anchor(scale, 0, anchor, "");
ease_scale(scale, 0.f, LinearInterpolation, static_cast<AnchorPoint>(anchor));
}

void
Camera::scale(float scale, float time)
Camera::scale(float scale, float time, int anchor, const std::string& ease)
{
ease_scale(scale, time, "");
ease_scale(scale, time, getEasingByName(EasingMode_from_string(ease)), static_cast<AnchorPoint>(anchor));
}

void
Camera::scale_anchor(float scale, float time, int anchor)
{
ease_scale_anchor(scale, time, anchor, "");
ease_scale(scale, time, LinearInterpolation, static_cast<AnchorPoint>(anchor));
}

void
Camera::ease_scale(float scale, float time, const std::string& ease)
{
ease_scale_anchor(scale, time, AnchorPoint::ANCHOR_MIDDLE, ease);
ease_scale(scale, time, getEasingByName(EasingMode_from_string(ease)), AnchorPoint::ANCHOR_MIDDLE);
}

void
Expand Down Expand Up @@ -904,12 +904,12 @@ Camera::register_class(ssq::VM& vm)
cls.addFunc<void, Camera, float, float, float>("scroll_to", &Camera::scroll_to);
cls.addFunc("get_current_scale", &Camera::get_current_scale);
cls.addFunc("get_target_scale", &Camera::get_target_scale);
cls.addFunc("set_scale", &Camera::set_scale);
cls.addFunc("set_scale_anchor", &Camera::set_scale_anchor);
cls.addFunc("scale", &Camera::scale);
cls.addFunc("scale_anchor", &Camera::scale_anchor);
cls.addFunc<void, Camera, float, float, const std::string&>("ease_scale", &Camera::ease_scale);
cls.addFunc("ease_scale_anchor", &Camera::ease_scale_anchor);
cls.addFunc("set_scale", &Camera::set_scale); // Deprecated
cls.addFunc("set_scale_anchor", &Camera::set_scale_anchor); // Deprecated
cls.addFunc("scale", &Camera::scale, ssq::DefaultArguments<float, int, std::string>(0.f, AnchorPoint::ANCHOR_MIDDLE, ""));
cls.addFunc("scale_anchor", &Camera::scale_anchor); // Deprecated
cls.addFunc<void, Camera, float, float, const std::string&>("ease_scale", &Camera::ease_scale); // Deprecated
cls.addFunc("ease_scale_anchor", &Camera::ease_scale_anchor); // Deprecated
cls.addFunc("get_screen_width", &Camera::get_screen_width);
cls.addFunc("get_screen_height", &Camera::get_screen_height);
cls.addFunc("get_x", &Camera::get_x);
Expand Down
10 changes: 9 additions & 1 deletion src/object/camera.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,12 +159,14 @@ class Camera final : public GameObject,
void scroll_to(float x, float y, float scrolltime);
/**
* @scripting
* @deprecated Use ""scale()"" instead!
* @description Sets the scale factor.
* @param float $scale
*/
void set_scale(float scale);
/**
* @scripting
* @deprecated Use ""scale()"" instead!
* @description Sets the scale factor and the target position anchor.
NOTE: Target position anchor is only applied, if the camera is in "manual" mode.
* @param float $scale
Expand All @@ -176,10 +178,14 @@ class Camera final : public GameObject,
* @description Fades to a specified scale factor in ""time"" seconds.
* @param float $scale
* @param float $time
* @param int $anchor Anchor point as represented by the ""ANCHOR_*"" constants.
Optional, default is ""ANCHOR_MIDDLE"" (see ${SRG_REF_AnchorPoints}).
* @param string $ease Optional, empty by default.
*/
void scale(float scale, float time);
void scale(float scale, float time = 0.f, int anchor = ANCHOR_MIDDLE, const std::string& ease = "");
/**
* @scripting
* @deprecated Use ""scale()"" instead!
* @description Fades to a specified scale factor and target position anchor in ""time"" seconds.
NOTE: Target position anchor is only applied, if the camera is in "manual" mode.
* @param float $scale
Expand All @@ -189,6 +195,7 @@ class Camera final : public GameObject,
void scale_anchor(float scale, float time, int anchor);
/**
* @scripting
* @deprecated Use ""scale()"" instead!
* @description Fades to a specified scale factor in ""time"" seconds with easing (smooth movement).
* @param float $scale
* @param float $time
Expand All @@ -197,6 +204,7 @@ class Camera final : public GameObject,
void ease_scale(float scale, float time, const std::string& ease);
/**
* @scripting
* @deprecated Use ""scale()"" instead!
* @description Fades to a specified scale factor and target position anchor in ""time"" seconds with easing (smooth movement).
NOTE: Target position anchor is only applied, if the camera is in "manual" mode.
* @param float $scale
Expand Down
1 change: 1 addition & 0 deletions src/object/floating_image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ FloatingImage::register_class(ssq::VM& vm)

return &Sector::get().add<FloatingImage>(spritefile);
},
{},
false /* Do not free pointer from Squirrel */,
vm.findClass("GameObject"));

Expand Down
1 change: 1 addition & 0 deletions src/object/text_array_object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,7 @@ TextArrayObject::register_class(ssq::VM& vm)

return &Sector::get().add<TextArrayObject>();
},
{},
false /* Do not free pointer from Squirrel */,
vm.findClass("GameObject"));

Expand Down
1 change: 1 addition & 0 deletions src/object/text_object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,7 @@ TextObject::register_class(ssq::VM& vm)

return &Sector::get().add<TextObject>();
},
{},
false /* Do not free pointer from Squirrel */,
vm.findClass("GameObject"));

Expand Down
42 changes: 25 additions & 17 deletions src/squirrel/supertux_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -665,15 +665,34 @@ static bool has_active_sequence()
If that spawnpoint doesn't exist either, Tux will simply end up at the origin (top-left 0, 0).
* @param string $sector
* @param string $spawnpoint
* @param string $transition Valid transitions are ""circle"" and ""fade"". If any other value is specified, no transition effect is drawn.
Optional, empty by default.
*/
static void spawn(const std::string& sector, const std::string& spawnpoint)
static void spawn(const std::string& sector, const std::string& spawnpoint, const std::string& transition = "")
{
if (!GameSession::current()) return;
GameSession::current()->respawn(sector, spawnpoint);
}

if (transition.empty())
{
GameSession::current()->respawn(sector, spawnpoint);
return;
}

ScreenFade::FadeType fade_type = ScreenFade::FadeType::NONE;

if (transition == "fade")
fade_type = ScreenFade::FadeType::FADE;
else if (transition == "circle")
fade_type = ScreenFade::FadeType::CIRCLE;
else
log_warning << "Invalid transition type '" << transition << "'." << std::endl;

GameSession::current()->respawn_with_fade(sector, spawnpoint, fade_type, {0.0f, 0.0f}, true);
}
#ifdef DOXYGEN_SCRIPTING
/**
* @scripting
* @deprecated Use ""spawn()"" instead!
* @description Respawns Tux in sector named ""sector"" at spawnpoint named ""spawnpoint"" with the given transition ""transition"".${SRG_TABLENEWPARAGRAPH}
Exceptions: If ""sector"" or ""spawnpoint"" are empty, or the specified sector does not exist, the function will bail out the first chance it gets.
If the specified spawnpoint doesn't exist, Tux will be spawned at the spawnpoint named “main”.
Expand All @@ -684,19 +703,8 @@ static void spawn(const std::string& sector, const std::string& spawnpoint)
*/
static void spawn_transition(const std::string& sector, const std::string& spawnpoint, const std::string& transition)
{
if (!GameSession::current()) return;

ScreenFade::FadeType fade_type = ScreenFade::FadeType::NONE;

if (transition == "fade")
fade_type = ScreenFade::FadeType::FADE;
else if (transition == "circle")
fade_type = ScreenFade::FadeType::CIRCLE;
else
log_warning << "Invalid transition type '" << transition << "'." << std::endl;

GameSession::current()->respawn_with_fade(sector, spawnpoint, fade_type, {0.0f, 0.0f}, true);
}
#endif

/**
* @scripting
Expand Down Expand Up @@ -862,8 +870,8 @@ void register_supertux_scripting_api(ssq::VM& vm)
ssq::Table level = vm.addTable("Level");
level.addFunc("finish", &scripting::Level::finish);
level.addFunc("has_active_sequence", &scripting::Level::has_active_sequence);
level.addFunc("spawn", &scripting::Level::spawn);
level.addFunc("spawn_transition", &scripting::Level::spawn_transition);
level.addFunc("spawn", &scripting::Level::spawn, ssq::DefaultArguments<std::string>(""));
level.addFunc("spawn_transition", &scripting::Level::spawn); // Deprecated
level.addFunc("set_start_point", &scripting::Level::set_start_point);
level.addFunc("set_start_pos", &scripting::Level::set_start_pos);
level.addFunc("set_respawn_point", &scripting::Level::set_respawn_point);
Expand Down
1 change: 1 addition & 0 deletions tools/scripting_docs_gen/class.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ struct Parameter
std::string type {};
std::string name {};
std::string description {};
std::string default_value {};
};

struct Function
Expand Down
3 changes: 0 additions & 3 deletions tools/scripting_docs_gen/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,6 @@ int main(int argc, char** argv)
// Formatting
replace(target_data, "${SRG_NEWPARAGRAPH} ", "\r\n\r\n");
replace(target_data, "${SRG_TABLENEWPARAGRAPH}", "<br /><br />");
replace(target_data, "\"\"", "`");
replace(target_data, "NOTE:", "<br /><br />**NOTE:**");
replace(target_data, "Note:", "<br /><br />**NOTE:**");

// Write to target file
write_file(output_dir_path / std::filesystem::path("Scripting" + cl.name + ".md"), target_data);
Expand Down
21 changes: 21 additions & 0 deletions tools/scripting_docs_gen/parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,27 @@ void parse_parameterlist(tinyxml2::XMLElement* p_memberdef, Function& func)

p_parameteritem = p_parameteritem->NextSiblingElement("parameteritem");
}

// Get default parameter values
tinyxml2::XMLElement* p_param = p_memberdef->FirstChildElement("param");
while (p_param)
{
tinyxml2::XMLElement* p_defval = p_param->FirstChildElement("defval");
if (p_defval)
{
tinyxml2::XMLElement* p_declname = p_param->FirstChildElement("declname");
for (Parameter& param : func.parameters)
{
if (!strcmp(p_declname->GetText(), param.name.c_str()))
{
param.default_value = p_defval->GetText();
break;
}
}
}

p_param = p_param->NextSiblingElement("param");
}
}


Expand Down
4 changes: 2 additions & 2 deletions tools/scripting_docs_gen/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ bool attr_equal(tinyxml2::XMLElement* el, const char* attr, const std::string& r
if (!attr_obj) return false;

const char* val = attr_obj->Value();
return val == NULL ? rhs.empty() : std::string(val) == rhs;
return val == NULL ? rhs.empty() : !strcmp(val, rhs.c_str());
}

bool el_equal(tinyxml2::XMLElement* el, const char* child_el, const std::string& rhs)
Expand All @@ -85,7 +85,7 @@ bool el_equal(tinyxml2::XMLElement* el, const char* child_el, const std::string&
if (!child_el_obj) return false;

const char* text = child_el_obj->GetText();
return text == NULL ? rhs.empty() : std::string(text) == rhs;
return text == NULL ? rhs.empty() : !strcmp(text, rhs.c_str());
}


Expand Down
20 changes: 15 additions & 5 deletions tools/scripting_docs_gen/writer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@

namespace Writer {

static std::string format_description(std::string desc)
{
replace(desc, "\"\"", "`");
replace(desc, "NOTE:", "<br /><br />**NOTE:**");
replace(desc, "Note:", "<br /><br />**NOTE:**");
return desc;
}

std::string write_file_notice(const std::string& template_file)
{
std::stringstream notice;
Expand Down Expand Up @@ -84,7 +92,7 @@ std::string write_constants_table(const std::vector<Constant>& constants)
{
// Print out type, name, initializer (if available) and description
table << "`" << con.type << " " << con.name << (con.initializer.empty() ? "" : " " + con.initializer)
<< "` | " << con.description << std::endl;
<< "` | " << format_description(con.description) << std::endl;
}

return table.str();
Expand All @@ -103,7 +111,7 @@ std::string write_variables_table(const std::vector<Variable>& variables)
for (const Variable& var : variables)
{
// Print out type, name and description
table << "`" << var.type << " " << var.name << "` | " << var.description << std::endl;
table << "`" << var.type << " " << var.name << "` | " << format_description(var.description) << std::endl;
}

return table.str();
Expand All @@ -127,6 +135,8 @@ std::string write_function_table(const std::vector<Function>& functions)
{
if (i != 0) table << ", ";
table << func.parameters[i].type << " " << func.parameters[i].name;
if (!func.parameters[i].default_value.empty())
table << " = " << func.parameters[i].default_value;
}
table << ")`";

Expand All @@ -136,13 +146,13 @@ std::string write_function_table(const std::vector<Function>& functions)
{
table << "**Deprecated!**";
if (!func.deprecation_msg.empty())
table << " " << func.deprecation_msg;
table << " " << format_description(func.deprecation_msg);

// Add line breaks only if a description is available
if (!func.description.empty())
table << "<br /><br />";
}
table << func.description;
table << format_description(func.description);

// Print out descriptions of parameters
if (std::any_of(func.parameters.begin(), func.parameters.end(), [](const Parameter& param) { return !param.description.empty(); }))
Expand All @@ -155,7 +165,7 @@ std::string write_function_table(const std::vector<Function>& functions)
{
if (param.description.empty()) continue;

table << (has_printed_param_desc ? "<br />" : "") << " `" << param.name << "` - " << param.description;
table << (has_printed_param_desc ? "<br />" : "") << " `" << param.name << "` - " << format_description(param.description);
has_printed_param_desc = true;
}
}
Expand Down