Permalink
Browse files

Fix uglyness with how Favorites menu was done

  • Loading branch information...
jacob1 committed Jul 25, 2016
1 parent fe4653c commit ae247a2d2b4b02c87aeffc5c8e6bc621d580b91e
Showing with 61 additions and 25 deletions.
  1. +28 −10 src/gui/game/Favorite.cpp
  2. +12 −3 src/gui/game/Favorite.h
  3. +18 −6 src/gui/game/GameModel.cpp
  4. +3 −6 src/gui/game/GameView.cpp
@@ -1,26 +1,44 @@
#include "Favorite.h"
#include "json/json.h"
#include "client/Client.h"
#include <algorithm>
std::vector<std::string> *favoritesList;
Favorite::Favorite():
favoritesList(std::vector<std::string>())
{}
Favorite::Favorite()
std::vector<std::string> Favorite::GetFavoritesList()
{
favoritesList = new std::vector<std::string>();
return favoritesList;
}
bool Favorite::IsFavorite(std::string identifier)
{
return std::find(favoritesList.begin(), favoritesList.end(), identifier) != favoritesList.end();
}
std::vector<std::string> *Favorite::GetFavoritesList()
bool Favorite::AnyFavorites()
{
return favoritesList;
return favoritesList.size() == 0;
}
void Favorite::SetFavoritesList(std::vector<std::string> newFavoritesList)
void Favorite::AddFavorite(std::string identifier)
{
*favoritesList = newFavoritesList;
favoritesList.push_back(identifier);
}
bool Favorite::IsFavorite(std::string identifier)
void Favorite::RemoveFavorite(std::string identifier)
{
favoritesList.erase(std::remove(favoritesList.begin(), favoritesList.end(), identifier), favoritesList.end());
}
void Favorite::SaveFavoritesToPrefs()
{
Client::Ref().SetPref("Favorites", std::vector<Json::Value>(favoritesList.begin(), favoritesList.end()));
}
void Favorite::LoadFavoritesFromPrefs()
{
std::vector<std::string> tempFavoritsList = *favoritesList;
return std::find(tempFavoritsList.begin(), tempFavoritsList.end(), identifier) != tempFavoritsList.end();
favoritesList = Client::Ref().GetPrefStringArray("Favorites");
}
@@ -6,11 +6,20 @@
#include "common/Singleton.h"
class Favorite : public Singleton<Favorite> {
class Favorite : public Singleton<Favorite>
{
std::vector<std::string> favoritesList;
public:
Favorite();
std::vector<std::string> * GetFavoritesList();
void SetFavoritesList(std::vector<std::string> favoritesList);
std::vector<std::string> GetFavoritesList();
bool IsFavorite(std::string identifier);
bool AnyFavorites();
void AddFavorite(std::string identifier);
void RemoveFavorite(std::string identifier);
void SaveFavoritesToPrefs();
void LoadFavoritesFromPrefs();
};
#endif //FAVORITE_H
@@ -82,10 +82,7 @@ GameModel::GameModel():
sim->aheat_enable = Client::Ref().GetPrefInteger("Simulation.AmbientHeat", 0);
sim->pretty_powder = Client::Ref().GetPrefInteger("Simulation.PrettyPowder", 0);
//Load favorites
std::vector<std::string> favoritesList = Client::Ref().GetPrefStringArray("Favorites");
Favorite::Ref().SetFavoritesList(favoritesList);
Favorite::Ref().LoadFavoritesFromPrefs();
//Load last user
if(Client::Ref().GetAuthUser().ID)
@@ -162,7 +159,7 @@ GameModel::~GameModel()
Client::Ref().SetPref("Decoration.Blue", (int)colour.Blue);
Client::Ref().SetPref("Decoration.Alpha", (int)colour.Alpha);
Client::Ref().SetPref("Favorites", std::vector<Json::Value>(Favorite::Ref().GetFavoritesList()->begin(), Favorite::Ref().GetFavoritesList()->end()));
Favorite::Ref().SaveFavoritesToPrefs();
for (size_t i = 0; i < menuList.size(); i++)
{
@@ -374,7 +371,7 @@ void GameModel::BuildFavoritesMenu()
{
menuList[SC_FAVORITES]->ClearTools();
for (size_t i = 0; i < menuList.size(); i++)
/*for (size_t i = 0; i < menuList.size(); i++)
{
if (i == SC_FAVORITES)
continue;
@@ -387,6 +384,21 @@ void GameModel::BuildFavoritesMenu()
}
}
}
for (size_t i = 0; i < extraElementTools.size(); i++)
{
if (Favorite::Ref().IsFavorite(extraElementTools[i]->GetIdentifier()))
{
menuList[SC_FAVORITES]->AddTool(extraElementTools[i]);
}
}*/
std::vector<std::string> favList = Favorite::Ref().GetFavoritesList();
for (size_t i = 0; i < favList.size(); i++)
{
Tool *tool = GetToolFromIdentifier(favList[i]);
if (tool)
menuList[SC_FAVORITES]->AddTool(tool);
}
if (activeMenu == SC_FAVORITES)
toolList = menuList[SC_FAVORITES]->GetToolList();
@@ -542,12 +542,9 @@ class GameView::ToolAction: public ui::ButtonAction
if (v->ShiftBehaviour() && v->CtrlBehaviour() && !v->AltBehaviour())
{
if (Favorite::Ref().IsFavorite(tool->GetIdentifier()) && sender->GetSelectionState() == 1)
{
Favorite::Ref().GetFavoritesList()->erase(std::remove(Favorite::Ref().GetFavoritesList()->begin(), Favorite::Ref().GetFavoritesList()->end(),
tool->GetIdentifier()), Favorite::Ref().GetFavoritesList()->end());
}
Favorite::Ref().RemoveFavorite(tool->GetIdentifier());
else if (sender->GetSelectionState() == 0)
Favorite::Ref().GetFavoritesList()->push_back(tool->GetIdentifier());
Favorite::Ref().AddFavorite(tool->GetIdentifier());
else if (sender->GetSelectionState() == 2)
v->c->SetActiveMenu(SC_FAVORITES);
@@ -613,7 +610,7 @@ void GameView::NotifyMenuListChanged(GameModel * sender)
std::string tempString = "";
tempString += menuList[i]->GetIcon();
std::string description = menuList[i]->GetDescription();
if (i == SC_FAVORITES && Favorite::Ref().GetFavoritesList()->size() == 0)
if (i == SC_FAVORITES && Favorite::Ref().AnyFavorites())
description += " (Use ctrl+shift+click to favorite an element)";
ui::Button * tempButton = new ui::Button(ui::Point(WINDOWW-16, currentY), ui::Point(15, 15), tempString, description);
tempButton->Appearance.Margin = ui::Border(0, 2, 3, 2);

0 comments on commit ae247a2

Please sign in to comment.