mirror of
https://github.com/OpenRCT2/OpenRCT2
synced 2026-01-23 06:44:38 +01:00
Implement scenario select window
This commit is contained in:
@@ -22,6 +22,7 @@
|
||||
# include <openrct2/Context.h>
|
||||
# include <openrct2/Input.h>
|
||||
# include <openrct2/common.h>
|
||||
# include <openrct2/scenario/ScenarioRepository.h>
|
||||
# include <openrct2/scripting/Duktape.hpp>
|
||||
# include <openrct2/scripting/ScriptEngine.h>
|
||||
# include <string>
|
||||
@@ -38,6 +39,37 @@ namespace OpenRCT2::Ui::Windows
|
||||
|
||||
namespace OpenRCT2::Scripting
|
||||
{
|
||||
static const DukEnumMap<SCENARIO_CATEGORY> ScenarioCategoryMap({
|
||||
{ "beginner", SCENARIO_CATEGORY_BEGINNER },
|
||||
{ "challenging", SCENARIO_CATEGORY_CHALLENGING },
|
||||
{ "expert", SCENARIO_CATEGORY_EXPERT },
|
||||
{ "real", SCENARIO_CATEGORY_REAL },
|
||||
{ "other", SCENARIO_CATEGORY_OTHER },
|
||||
{ "dlc", SCENARIO_CATEGORY_DLC },
|
||||
{ "build_your_own", SCENARIO_CATEGORY_BUILD_YOUR_OWN },
|
||||
});
|
||||
|
||||
static const DukEnumMap<ScenarioSource> ScenarioSourceMap({
|
||||
{ "rct1", ScenarioSource::RCT1 },
|
||||
{ "rct1_aa", ScenarioSource::RCT1_AA },
|
||||
{ "rct1_ll", ScenarioSource::RCT1_LL },
|
||||
{ "rct2", ScenarioSource::RCT2 },
|
||||
{ "rct2_ww", ScenarioSource::RCT2_WW },
|
||||
{ "rct2_tt", ScenarioSource::RCT2_TT },
|
||||
{ "real", ScenarioSource::Real },
|
||||
{ "other", ScenarioSource::Other },
|
||||
});
|
||||
|
||||
template<> inline DukValue ToDuk(duk_context* ctx, const SCENARIO_CATEGORY& value)
|
||||
{
|
||||
return ToDuk(ctx, ScenarioCategoryMap[value]);
|
||||
}
|
||||
|
||||
template<> inline DukValue ToDuk(duk_context* ctx, const ScenarioSource& value)
|
||||
{
|
||||
return ToDuk(ctx, ScenarioSourceMap[value]);
|
||||
}
|
||||
|
||||
class ScTool
|
||||
{
|
||||
private:
|
||||
@@ -250,6 +282,19 @@ namespace OpenRCT2::Scripting
|
||||
}
|
||||
}
|
||||
|
||||
void showScenarioSelect(const DukValue& desc)
|
||||
{
|
||||
auto plugin = _scriptEngine.GetExecInfo().GetCurrentPlugin();
|
||||
auto callback = desc["callback"];
|
||||
|
||||
window_scenarioselect_open(
|
||||
[this, plugin, callback](std::string_view path) {
|
||||
auto dukValue = GetScenarioFile(path);
|
||||
_scriptEngine.ExecutePluginCall(plugin, callback, { dukValue }, false);
|
||||
},
|
||||
false, true);
|
||||
}
|
||||
|
||||
void activateTool(const DukValue& desc)
|
||||
{
|
||||
InitialiseCustomTool(_scriptEngine, desc);
|
||||
@@ -278,6 +323,7 @@ namespace OpenRCT2::Scripting
|
||||
dukglue_register_method(ctx, &ScUi::showError, "showError");
|
||||
dukglue_register_method(ctx, &ScUi::showTextInput, "showTextInput");
|
||||
dukglue_register_method(ctx, &ScUi::showFileBrowse, "showFileBrowse");
|
||||
dukglue_register_method(ctx, &ScUi::showScenarioSelect, "showScenarioSelect");
|
||||
dukglue_register_method(ctx, &ScUi::activateTool, "activateTool");
|
||||
dukglue_register_method(ctx, &ScUi::registerMenuItem, "registerMenuItem");
|
||||
}
|
||||
@@ -287,6 +333,39 @@ namespace OpenRCT2::Scripting
|
||||
{
|
||||
return WC_NULL;
|
||||
}
|
||||
|
||||
DukValue GetScenarioFile(std::string_view path)
|
||||
{
|
||||
auto ctx = _scriptEngine.GetContext();
|
||||
DukObject obj(ctx);
|
||||
obj.Set("path", path);
|
||||
|
||||
auto* scenarioRepo = GetScenarioRepository();
|
||||
auto entry = scenarioRepo->GetByPath(std::string(path).c_str());
|
||||
if (entry != nullptr)
|
||||
{
|
||||
obj.Set("id", entry->sc_id);
|
||||
obj.Set("category", ToDuk(ctx, static_cast<SCENARIO_CATEGORY>(entry->category)));
|
||||
obj.Set("sourceGame", ToDuk(ctx, entry->source_game));
|
||||
obj.Set("internalName", entry->internal_name);
|
||||
obj.Set("name", entry->name);
|
||||
obj.Set("details", entry->details);
|
||||
|
||||
auto* highscore = entry->highscore;
|
||||
if (highscore == nullptr)
|
||||
{
|
||||
obj.Set("highscore", nullptr);
|
||||
}
|
||||
else
|
||||
{
|
||||
DukObject dukHighscore(ctx);
|
||||
dukHighscore.Set("name", highscore->name);
|
||||
dukHighscore.Set("companyValue", highscore->company_value);
|
||||
obj.Set("highscore", dukHighscore.Take());
|
||||
}
|
||||
}
|
||||
return obj.Take();
|
||||
}
|
||||
};
|
||||
} // namespace OpenRCT2::Scripting
|
||||
|
||||
|
||||
Reference in New Issue
Block a user