Skip to content

Commit

Permalink
Introduce shortcut contexts
Browse files Browse the repository at this point in the history
  • Loading branch information
LBPHacker committed Oct 12, 2019
1 parent 348ef06 commit 9e3158b
Show file tree
Hide file tree
Showing 17 changed files with 1,014 additions and 751 deletions.
17 changes: 0 additions & 17 deletions src/client/Client.cpp
Expand Up @@ -1946,11 +1946,6 @@ std::vector<bool> Client::GetPrefBoolArray(ByteString prop)
return std::vector<bool>();
}

Json::Value Client::GetPrefJson(ByteString prop, Json::Value defaultValue)
{
return GetPref(preferences, prop, defaultValue);
}

// Helper preference setting function.
// To actually save any changes to preferences, we need to directly do preferences[property] = thing
// any other way will set the value of a copy of preferences, not the original
Expand Down Expand Up @@ -1984,18 +1979,6 @@ void Client::SetPref(ByteString prop, Json::Value value)
}
}

void Client::ClearPref(ByteString prop)
{
try
{
preferences[prop].clear();
}
catch (std::exception & e)
{

}
}

void Client::SetPref(ByteString prop, std::vector<Json::Value> value)
{
try
Expand Down
105 changes: 103 additions & 2 deletions src/client/Client.h
Expand Up @@ -3,13 +3,86 @@

#include <vector>
#include <list>
#include <tuple>
#include <utility>

#include "common/String.h"
#include "common/Singleton.h"
#include "json/json.h"

#include "User.h"

template<class Item>
void JsonArrayItemToTupleItem(Item &i, Json::Value const &v);

template<>
inline void JsonArrayItemToTupleItem<bool>(bool &i, Json::Value const &v)
{
i = v.asBool();
}

template<>
inline void JsonArrayItemToTupleItem<int>(int &i, Json::Value const &v)
{
i = v.asInt();
}

template<>
inline void JsonArrayItemToTupleItem<ByteString>(ByteString &i, Json::Value const &v)
{
auto s = v.asString();
i = ByteString(s.begin(), s.end());
}

template<std::size_t Item = 0, class... Types>
typename std::enable_if<Item == sizeof...(Types), void>::type
JsonArrayToTuple(std::tuple<Types...> &, Json::Value const &)
{
}

template<std::size_t Item = 0, class... Types>
typename std::enable_if<Item < sizeof...(Types), void>::type
JsonArrayToTuple(std::tuple<Types...> &t, Json::Value const &va)
{
JsonArrayItemToTupleItem(std::get<Item>(t), va[(Json::ArrayIndex)Item]);
JsonArrayToTuple<Item + 1, Types...>(t, va);
}

template<class Item>
void JsonArrayItemFromTupleItem(Item const &i, Json::Value &v);

template<>
inline void JsonArrayItemFromTupleItem<bool>(bool const &i, Json::Value &v)
{
v = Json::Value((bool)i);
}

template<>
inline void JsonArrayItemFromTupleItem<int>(int const &i, Json::Value &v)
{
v = Json::Value((Json::Int)i);
}

template<>
inline void JsonArrayItemFromTupleItem<ByteString>(ByteString const &i, Json::Value &v)
{
v = Json::Value(std::string(i.begin(), i.end()));
}

template<std::size_t Item = 0, class... Types>
typename std::enable_if<Item == sizeof...(Types), void>::type
JsonArrayFromTuple(std::tuple<Types...> const &, Json::Value &)
{
}

template<std::size_t Item = 0, class... Types>
typename std::enable_if<Item < sizeof...(Types), void>::type
JsonArrayFromTuple(std::tuple<Types...> const &t, Json::Value &va)
{
JsonArrayItemFromTupleItem(std::get<Item>(t), va[(Json::ArrayIndex)Item]);
JsonArrayFromTuple<Item + 1, Types...>(t, va);
}

class SaveInfo;
class SaveFile;
class SaveComment;
Expand Down Expand Up @@ -181,12 +254,40 @@ class Client: public Singleton<Client> {
std::vector<int> GetPrefIntegerArray(ByteString prop);
std::vector<unsigned int> GetPrefUIntegerArray(ByteString prop);
std::vector<bool> GetPrefBoolArray(ByteString prop);
Json::Value GetPrefJson(ByteString prop, Json::Value defaultValue = Json::nullValue);

void SetPref(ByteString prop, Json::Value value);
void SetPref(ByteString property, std::vector<Json::Value> value);
void SetPrefUnicode(ByteString prop, String value);
void ClearPref(ByteString prop);

template<class Tuple>
std::vector<Tuple> GetPrefTupleArray(ByteString prop)
{
try
{
Json::Value arr = GetPref(preferences, prop);
std::vector<Tuple> ret(arr.size());
for (int i = 0; i < (int)arr.size(); i++)
{
JsonArrayToTuple(ret[i], arr[i]);
}
return ret;
}
catch (std::exception &)
{
}
return std::vector<Tuple>();
}

template<class Tuple>
void SetPref(ByteString prop, std::vector<Tuple> const &value)
{
std::vector<Json::Value> arr(value.size());
for (int i = 0; i < (int)arr.size(); i++)
{
JsonArrayFromTuple(value[i], arr[i]);
}
SetPref(prop, arr);
}
};

#endif // CLIENT_H
6 changes: 3 additions & 3 deletions src/gui/game/GameController.cpp
Expand Up @@ -661,7 +661,7 @@ bool GameController::MouseDown(int x, int y, unsigned button)
{
MouseDownEvent ev(x, y, button);
bool ret = commandInterface->HandleEvent(LuaEvents::mousedown, &ev);
if (ret && y<YRES && x<XRES && !gameView->GetPlacingSave() && !gameView->GetPlacingZoom())
if (ret && y<YRES && x<XRES && gameView->IsIdle())
{
ui::Point point = gameModel->AdjustZoomCoords(ui::Point(x, y));
x = point.X;
Expand All @@ -687,7 +687,7 @@ bool GameController::MouseUp(int x, int y, unsigned button, char type)
bool ret = commandInterface->HandleEvent(LuaEvents::mouseup, &ev);
if (type)
return ret;
if (ret && foundSignID != -1 && y<YRES && x<XRES && !gameView->GetPlacingSave())
if (ret && foundSignID != -1 && y<YRES && x<XRES && gameView->IsIdle())
{
ui::Point point = gameModel->AdjustZoomCoords(ui::Point(x, y));
x = point.X;
Expand Down Expand Up @@ -752,7 +752,7 @@ bool GameController::KeyPress(int key, int scan, bool repeat, bool shift, bool c
if (ret)
{
Simulation * sim = gameModel->GetSimulation();
if (!gameView->GetPlacingSave())
if (gameView->IsIdle())
{
// Go right command
if (key == SDLK_RIGHT)
Expand Down
20 changes: 18 additions & 2 deletions src/gui/game/GameModel.cpp
Expand Up @@ -156,6 +156,8 @@ GameModel::GameModel():

mouseClickRequired = Client::Ref().GetPrefBool("MouseClickRequired", false);
includePressure = Client::Ref().GetPrefBool("Simulation.IncludePressure", true);

keyconfig = Client::Ref().GetPrefTupleArray<decltype(keyconfig)::value_type>("Keyconfig");
}

GameModel::~GameModel()
Expand Down Expand Up @@ -188,6 +190,8 @@ GameModel::~GameModel()
Client::Ref().SetPref("MouseClickRequired", mouseClickRequired);
Client::Ref().SetPref("Simulation.IncludePressure", includePressure);

Client::Ref().SetPref("Keyconfig", keyconfig);

Favorite::Ref().SaveFavoritesToPrefs();

for (size_t i = 0; i < menuList.size(); i++)
Expand Down Expand Up @@ -546,6 +550,7 @@ void GameModel::AddObserver(GameView * observer){
observer->NotifyColourActivePresetChanged(this);
observer->NotifyQuickOptionsChanged(this);
observer->NotifyLastToolChanged(this);
observer->NotifyKeyconfigChanged(this);
UpdateQuickOptions();
}

Expand Down Expand Up @@ -691,11 +696,11 @@ void GameModel::SetSave(SaveInfo * newSave, bool invertIncludePressure)
UpdateQuickOptions();
}

void GameModel::NotifyKeyBindingsChanged()
void GameModel::notifyKeyconfigChanged()
{
for (auto observer : observers)
{
observer->NotifyKeyBindingsChanged(this);
observer->NotifyKeyconfigChanged(this);
}
}

Expand Down Expand Up @@ -1328,3 +1333,14 @@ void GameModel::SetIncludePressure(bool includePressure_)
{
includePressure = includePressure_;
}

Keyconfig GameModel::GetKeyconfig()
{
return keyconfig;
}

void GameModel::SetKeyconfig(Keyconfig keyconfig_)
{
keyconfig = keyconfig_;
notifyKeyconfigChanged();
}
9 changes: 8 additions & 1 deletion src/gui/game/GameModel.h
Expand Up @@ -7,6 +7,7 @@
#include "gui/interface/Colour.h"
#include "client/User.h"
#include "gui/interface/Point.h"
#include "Keyconfig.h"

class Menu;
class Tool;
Expand Down Expand Up @@ -103,6 +104,10 @@ class GameModel
void notifyToolTipChanged();
void notifyQuickOptionsChanged();
void notifyLastToolChanged();
void notifyKeyconfigChanged();

Keyconfig keyconfig;

public:
GameModel();
~GameModel();
Expand Down Expand Up @@ -159,6 +164,9 @@ class GameModel
int GetBrushID();
void SetBrushID(int i);

Keyconfig GetKeyconfig();
void SetKeyconfig(Keyconfig keyconfig);

void SetVote(int direction);
SaveInfo * GetSave();
SaveFile * GetSaveFile();
Expand Down Expand Up @@ -208,7 +216,6 @@ class GameModel
void SetMouseClickRequired(bool mouseClickRequired);
bool GetIncludePressure();
void SetIncludePressure(bool includePressure);
void NotifyKeyBindingsChanged();

std::vector<Notification*> GetNotifications();
void AddNotification(Notification * notification);
Expand Down

0 comments on commit 9e3158b

Please sign in to comment.