game: keep settings in a config file

This commit is contained in:
Crypto City 2020-01-04 20:21:23 +00:00
parent 5822dfd221
commit 1734a6219c
4 changed files with 82 additions and 4 deletions

View File

@ -25,6 +25,7 @@
#include <Urho3D/UI/UIEvents.h>
#include "ProcSky.h"
#include "string_tools.h"
#include "file_io_utils.h"
#include "cryptonote_basic/cryptonote_format_utils.h"
#include "citymesh-urho3d.h"
#include "ui-urho3d.h"
@ -48,6 +49,9 @@ using namespace Urho3D;
const float TOUCH_SENSITIVITY = 2.0f;
#define CONFIG_SHADOWS "shadows"
#define CONFIG_DYNAMIC_SKY "dynamic-sky"
enum SelectionMode
{
SM_NORMAL,
@ -188,6 +192,9 @@ private:
void EnableShadows(bool enable);
void EnableDynamicSky(bool enable);
template<typename T> T GetConfigValue(const char *key, const T &default_value) const;
template<typename T> T SetConfigValue(const char *key, const T &value);
private:
GameState gameState;
Map &map;
@ -930,8 +937,8 @@ printf("%d plots, origin %u %u\n", plots, ox, oy);
light->SetCastShadows(false);
#endif
EnableShadows(false);
EnableDynamicSky(false);
EnableShadows(GetConfigValue(CONFIG_SHADOWS, false));
EnableDynamicSky(GetConfigValue(CONFIG_DYNAMIC_SKY, false));
}
void CryptoCityUrho3D::SetupViewport()
@ -2052,14 +2059,71 @@ void CryptoCityUrho3D::HandleChat(StringHash eventType, VariantMap& eventData)
ToggleConsole();
}
template<typename T> static bool GetJSONValue(const JSONValue &field, T &value);
template<> bool GetJSONValue(const JSONValue &field, bool &value) { if (!field.IsBool()) return false; value = field.GetBool(); return true; }
template<> bool GetJSONValue(const JSONValue &field, int &value) { if (!field.IsNumber()) return false; value = field.GetInt(); return true; }
template<> bool GetJSONValue(const JSONValue &field, unsigned &value) { if (!field.IsNumber()) return false; value = field.GetUInt(); return true; }
template<typename T>
T CryptoCityUrho3D::GetConfigValue(const char *key, const T &default_value) const
{
ResourceCache* cache = GetSubsystem<ResourceCache>();
SharedPtr<JSONFile> config(cache->GetResource<JSONFile>("config.json"));
if (!config)
return default_value;
const JSONValue &root = config->GetRoot();
if (!root.IsObject())
return default_value;
if (!root.Contains(key))
return default_value;
const JSONValue &field = root.Get(key);
T v;
if (!GetJSONValue(field, v))
return default_value;
return v;
}
template<typename T>
T CryptoCityUrho3D::SetConfigValue(const char *key, const T &value)
{
ResourceCache* cache = GetSubsystem<ResourceCache>();
SharedPtr<JSONFile> config(cache->GetResource<JSONFile>("config.json"));
if (!config)
config = new JSONFile(context_);
JSONValue &root = config->GetRoot();
if (!root.IsObject())
root = JSONObject();
root[key] = value;
String path = GetSubsystem<FileSystem>()->GetProgramDir() + "GameData";
if (!epee::file_io_utils::is_file_exist(path.CString()))
{
path = GetSubsystem<FileSystem>()->GetProgramDir() + "../GameData";
if (!epee::file_io_utils::is_file_exist(path.CString()))
{
new MessageBox(context_, "Config file path not found");
return value;
}
}
File f(context_, path + "/config.json", FILE_WRITE);
if (!config->Save(f))
new MessageBox(context_, "Error saving config file");
return value;
}
void CryptoCityUrho3D::HandleEnableShadows(StringHash eventType, VariantMap& eventData)
{
EnableShadows(eventData[EnableShadows::P_ENABLE].GetBool());
if (eventData.Contains(EnableShadows::P_ENABLE))
EnableShadows(SetConfigValue(CONFIG_SHADOWS, eventData[EnableShadows::P_ENABLE].GetBool()));
else
eventData[EnableShadows::P_ENABLE] = GetConfigValue(CONFIG_SHADOWS, false);
}
void CryptoCityUrho3D::HandleEnableDynamicSky(StringHash eventType, VariantMap& eventData)
{
EnableDynamicSky(eventData[EnableDynamicSky::P_ENABLE].GetBool());
if (eventData.Contains(EnableDynamicSky::P_ENABLE))
EnableDynamicSky(SetConfigValue(CONFIG_DYNAMIC_SKY, eventData[EnableDynamicSky::P_ENABLE].GetBool()));
else
eventData[EnableDynamicSky::P_ENABLE] = GetConfigValue(CONFIG_DYNAMIC_SKY, false);
}
void CryptoCityUrho3D::HandleResearch(StringHash eventType, VariantMap& eventData)

View File

@ -169,6 +169,17 @@ void UIOptionsDialog::RegisterObject(Context* context)
context->RegisterFactory<UIOptionsDialog>();
}
void UIOptionsDialog::Configure()
{
VariantMap newEventData;
SendEvent(E_OPTIONS_ENABLE_SHADOWS, newEventData);
shadowsWidget->SetChecked(newEventData[OptionsEnableShadows::P_ENABLE].GetBool());
newEventData = {};
SendEvent(E_OPTIONS_ENABLE_DYNAMIC_SKY, newEventData);
dynamicSkyWidget->SetChecked(newEventData[OptionsEnableDynamicSky::P_ENABLE].GetBool());
}
void UIOptionsDialog::HandleShadowsChanged(StringHash eventType, VariantMap& eventData)
{
VariantMap newEventData;

View File

@ -19,6 +19,7 @@ namespace Urho3D
URHO3D_EVENT(E_OPTIONS_OKAYED, OptionsOkayed) { }
URHO3D_EVENT(E_OPTIONS_CANCELLED, OptionsCancelled) {}
URHO3D_EVENT(E_OPTIONS_ENABLE_SHADOWS, OptionsEnableShadows) { URHO3D_PARAM(P_ENABLE, Enable); }
URHO3D_EVENT(E_OPTIONS_ENABLE_DYNAMIC_SKY, OptionsEnableDynamicSky) { URHO3D_PARAM(P_ENABLE, Enable); }
@ -32,6 +33,7 @@ public:
static void RegisterObject(Urho3D::Context* context);
void Configure();
void Update();
void CancelUI();

View File

@ -1138,6 +1138,7 @@ void UIUrho3D::HandleOptions(StringHash eventType, VariantMap& eventData)
SubscribeToEvent(optionsDialog, E_OPTIONS_ENABLE_DYNAMIC_SKY, [this](StringHash eventType, VariantMap& eventData) {
SendEvent(E_CRYPTOCITY_ENABLE_DYNAMIC_SKY, eventData);
});
optionsDialog->Configure();
}
void UIUrho3D::HandleAssignItems(StringHash eventType, VariantMap& eventData)