-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor server parameters, add boolean parameter (Checkbox)
- Loading branch information
Showing
13 changed files
with
230 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#include "BooleanParameter.h" | ||
|
||
#include "Core/Utils/StringUtils.h" | ||
|
||
BooleanParameter::BooleanParameter(std::string name) | ||
: AbstractParameter(std::move(name)) | ||
{ | ||
} | ||
|
||
std::string BooleanParameter::getType() const { | ||
return TYPE; | ||
} | ||
|
||
std::string BooleanParameter::getValueAsString() const { | ||
return value_ ? "1" : "0"; | ||
} | ||
|
||
void BooleanParameter::setValue(const std::string& val) { | ||
std::string lower = IuStringUtils::toLower(val); | ||
if (val == "1" || lower == "true") { | ||
value_ = true; | ||
} else { | ||
value_ = false; | ||
} | ||
} | ||
|
||
void BooleanParameter::setValue(bool val) { | ||
value_ = val; | ||
} | ||
|
||
bool BooleanParameter::getValue() const { | ||
return value_; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
#pragma once | ||
|
||
#include "AbstractParameter.h" | ||
|
||
class BooleanParameter: public AbstractParameter { | ||
public: | ||
explicit BooleanParameter(std::string name); | ||
|
||
std::string getType() const override; | ||
std::string getValueAsString() const override; | ||
void setValue(const std::string& val) override; | ||
|
||
inline static const std::string TYPE = "boolean"; | ||
|
||
void setValue(bool val); | ||
bool getValue() const; | ||
private: | ||
bool value_ = false; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
#include "ParameterFactory.h" | ||
|
||
|
||
#include <unordered_map> | ||
#include <functional> | ||
|
||
#include "Core/Scripting/API/ScriptAPI.h" | ||
#include "TextParameter.h" | ||
#include "ChoiceParameter.h" | ||
#include "BooleanParameter.h" | ||
|
||
class ParameterFactory { | ||
public: | ||
virtual std::unique_ptr<AbstractParameter> create(const std::string& name, Sqrat::Table& table) const = 0; | ||
}; | ||
|
||
class ChoiceParameterFactory : public ParameterFactory { | ||
public: | ||
std::unique_ptr<AbstractParameter> create(const std::string& name, Sqrat::Table& table) const override { | ||
auto choiceParameter = std::make_unique<ChoiceParameter>(name); | ||
auto choices = table.GetValue<Sqrat::Array>("items"); | ||
if (!!choices) { | ||
Sqrat::Array::iterator it; | ||
while (choices->Next(it)) { | ||
Sqrat::Table tbl(it.getValue(), choices->GetVM()); | ||
std::string itemLabel = ScriptAPI::GetValue(tbl.GetValue<std::string>("label")); | ||
std::string itemId = ScriptAPI::GetValue(tbl.GetValue<std::string>("id")); | ||
choiceParameter->addItem(itemId, itemLabel); | ||
} | ||
} | ||
return choiceParameter; | ||
} | ||
}; | ||
|
||
class BooleanParameterFactory : public ParameterFactory { | ||
public: | ||
std::unique_ptr<AbstractParameter> create(const std::string& name, Sqrat::Table& table) const override { | ||
return std::make_unique<BooleanParameter>(name); | ||
} | ||
}; | ||
|
||
class TextParameterFactory : public ParameterFactory { | ||
public: | ||
std::unique_ptr<AbstractParameter> create(const std::string& name, Sqrat::Table& table) const override { | ||
return std::make_unique<TextParameter>(name); | ||
} | ||
}; | ||
|
||
class ParameterFactoryRegistry { | ||
public: | ||
ParameterFactoryRegistry() { | ||
// Registering factories by parameter type | ||
registry_[ChoiceParameter::TYPE] = std::make_unique<ChoiceParameterFactory>(); | ||
registry_[BooleanParameter::TYPE] = std::make_unique<BooleanParameterFactory>(); | ||
registry_[TextParameter::TYPE] = std::make_unique<TextParameterFactory>(); // TextParameter by default | ||
} | ||
|
||
std::unique_ptr<AbstractParameter> createParameter(const std::string& type, const std::string& name, Sqrat::Table& table) const { | ||
auto it = registry_.find(type); | ||
if (it != registry_.end()) { | ||
return it->second->create(name, table); | ||
} | ||
// Fallback to default factory | ||
it = registry_.find(TextParameter::TYPE); | ||
if (it != registry_.end()) { | ||
return it->second->create(name, table); | ||
} | ||
return {}; | ||
} | ||
|
||
private: | ||
std::unordered_map<std::string, std::unique_ptr<ParameterFactory>> registry_; | ||
}; | ||
|
||
std::unique_ptr<AbstractParameter> SqTableToParameter(const std::string& name, const std::string& type, Sqrat::Table& table) { | ||
static ParameterFactoryRegistry factoryRegistry; | ||
return factoryRegistry.createParameter(type, name, table); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
|
||
#pragma once | ||
|
||
#include <memory> | ||
#include <string> | ||
|
||
#include "AbstractParameter.h" | ||
#include "Core/Scripting/Squirrelnc.h" | ||
|
||
std::unique_ptr<AbstractParameter> SqTableToParameter(const std::string& name, const std::string& type, Sqrat::Table& table); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.