Skip to content

Commit

Permalink
Added a favorites menu. (#298)
Browse files Browse the repository at this point in the history
Added a favorites menu.
  • Loading branch information
me4502 authored and jacob1 committed Jun 25, 2016
1 parent 9a18338 commit 083d488
Show file tree
Hide file tree
Showing 14 changed files with 137 additions and 16 deletions.
4 changes: 2 additions & 2 deletions src/gui/elementsearch/ElementSearchActivity.cpp
Expand Up @@ -129,9 +129,9 @@ void ElementSearchActivity::searchTools(std::string query)
ToolButton * tempButton;

if(tempTexture)
tempButton = new ToolButton(current+viewPosition, ui::Point(30, 18), "", tool->GetDescription());
tempButton = new ToolButton(current+viewPosition, ui::Point(30, 18), "", tool->GetIdentifier(), tool->GetDescription());
else
tempButton = new ToolButton(current+viewPosition, ui::Point(30, 18), tool->GetName(), tool->GetDescription());
tempButton = new ToolButton(current+viewPosition, ui::Point(30, 18), tool->GetName(), tool->GetIdentifier(), tool->GetDescription());

tempButton->Appearance.SetTexture(tempTexture);
tempButton->Appearance.BackgroundInactive = ui::Colour(tool->colRed, tool->colGreen, tool->colBlue);
Expand Down
26 changes: 26 additions & 0 deletions src/gui/game/Favorite.cpp
@@ -0,0 +1,26 @@
#include "Favorite.h"
#include <algorithm>

std::vector<std::string> *favoritesList;

Favorite::Favorite()
{
favoritesList = new std::vector<std::string>();
}


std::vector<std::string> *Favorite::GetFavoritesList()
{
return favoritesList;
}

void Favorite::SetFavoritesList(std::vector<std::string> newFavoritesList)
{
*favoritesList = newFavoritesList;
}

bool Favorite::IsFavorite(std::string identifier)
{
std::vector<std::string> tempFavoritsList = *favoritesList;
return std::find(tempFavoritsList.begin(), tempFavoritsList.end(), identifier) != tempFavoritsList.end();
}
16 changes: 16 additions & 0 deletions src/gui/game/Favorite.h
@@ -0,0 +1,16 @@
#ifndef FAVORITE_H
#define FAVORITE_H

#include <string>
#include <vector>

#include "common/Singleton.h"

class Favorite : public Singleton<Favorite> {
public:
Favorite();
std::vector<std::string> * GetFavoritesList();
void SetFavoritesList(std::vector<std::string> favoritesList);
bool IsFavorite(std::string identifier);
};
#endif //FAVORITE_H
5 changes: 5 additions & 0 deletions src/gui/game/GameController.cpp
Expand Up @@ -1045,6 +1045,11 @@ std::vector<Menu*> GameController::GetMenuList()
return gameModel->GetMenuList();
}

void GameController::RebuildFavoritesMenu()
{
gameModel->BuildFavoritesMenu();
}

void GameController::ActiveToolChanged(int toolSelection, Tool *tool)
{
commandInterface->OnActiveToolChanged(toolSelection, tool);
Expand Down
1 change: 1 addition & 0 deletions src/gui/game/GameController.h
Expand Up @@ -103,6 +103,7 @@ class GameController: public ClientListener
void SetDebugFlags(unsigned int flags) { debugFlags = flags; }
void SetActiveMenu(int menuID);
std::vector<Menu*> GetMenuList();
void RebuildFavoritesMenu();
Tool * GetActiveTool(int selection);
void SetActiveTool(int toolSelection, Tool * tool);
void SetLastTool(Tool * tool);
Expand Down
38 changes: 38 additions & 0 deletions src/gui/game/GameModel.cpp
Expand Up @@ -17,6 +17,7 @@
#include "QuickOptions.h"
#include "GameModelException.h"
#include "Format.h"
#include "Favorite.h"

GameModel::GameModel():
clipboard(NULL),
Expand Down Expand Up @@ -81,6 +82,11 @@ 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);

//Load last user
if(Client::Ref().GetAuthUser().ID)
{
Expand Down Expand Up @@ -156,6 +162,8 @@ 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()));

for (size_t i = 0; i < menuList.size(); i++)
{
delete menuList[i];
Expand Down Expand Up @@ -353,6 +361,36 @@ void GameModel::BuildMenus()
notifyToolListChanged();
notifyActiveToolsChanged();
notifyLastToolChanged();

//Build menu for favorites
BuildFavoritesMenu();
}

void GameModel::BuildFavoritesMenu()
{
menuList[SC_FAVORITES]->ClearTools();

for (int i = 0; i < menuList.size(); i++)
{
if (i == SC_FAVORITES)
continue;

for (int j = 0; j < menuList[i]->GetToolList().size(); j++)
{
if (Favorite::Ref().IsFavorite(menuList[i]->GetToolList()[j]->GetIdentifier()))
{
menuList[SC_FAVORITES]->AddTool(menuList[i]->GetToolList()[j]);
}
}
}

if (activeMenu == SC_FAVORITES)
toolList = menuList[SC_FAVORITES]->GetToolList();

notifyMenuListChanged();
notifyToolListChanged();
notifyActiveToolsChanged();
notifyLastToolChanged();
}

Tool * GameModel::GetToolFromIdentifier(std::string identifier)
Expand Down
1 change: 1 addition & 0 deletions src/gui/game/GameModel.h
Expand Up @@ -125,6 +125,7 @@ class GameModel
std::string GetInfoTip();

void BuildMenus();
void BuildFavoritesMenu();
void BuildQuickOptionMenu(GameController * controller);

std::deque<Snapshot*> GetHistory();
Expand Down
34 changes: 26 additions & 8 deletions src/gui/game/GameView.cpp
@@ -1,5 +1,6 @@
#include <sstream>
#include <iomanip>
#include <algorithm>

#include "Config.h"
#include "gui/Style.h"
Expand All @@ -19,6 +20,7 @@
#include "QuickOptions.h"
#include "IntroText.h"
#include "DecorationTool.h"
#include "Favorite.h"


class SplitButton;
Expand Down Expand Up @@ -536,11 +538,27 @@ class GameView::ToolAction: public ui::ButtonAction
void ActionCallback(ui::Button * sender_)
{
ToolButton *sender = (ToolButton*)sender_;
if (v->CtrlBehaviour() && v->AltBehaviour() && !v->ShiftBehaviour())
if (tool->GetIdentifier().find("DEFAULT_PT_") != tool->GetIdentifier().npos)
sender->SetSelectionState(3);
if(sender->GetSelectionState() >= 0 && sender->GetSelectionState() <= 3)
v->c->SetActiveTool(sender->GetSelectionState(), tool);
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());
else if (sender->GetSelectionState() == 0)
Favorite::Ref().GetFavoritesList()->push_back(tool->GetIdentifier());
else if (sender->GetSelectionState() == 2)
v->c->SetActiveMenu(SC_FAVORITES);

v->c->RebuildFavoritesMenu();
}
else
{
if (v->CtrlBehaviour() && v->AltBehaviour() && !v->ShiftBehaviour())
if (tool->GetIdentifier().find("DEFAULT_PT_") != tool->GetIdentifier().npos)
sender->SetSelectionState(3);

if (sender->GetSelectionState() >= 0 && sender->GetSelectionState() <= 3)
v->c->SetActiveTool(sender->GetSelectionState(), tool);
}
}
};

Expand Down Expand Up @@ -740,9 +758,9 @@ void GameView::NotifyToolListChanged(GameModel * sender)
tempTexture = ((DecorationTool*)toolList[i])->GetIcon(toolList[i]->GetToolID(), 26, 14);

if(tempTexture)
tempButton = new ToolButton(ui::Point(currentX, YRES+1), ui::Point(30, 18), "", toolList[i]->GetDescription());
tempButton = new ToolButton(ui::Point(currentX, YRES+1), ui::Point(30, 18), "", toolList[i]->GetIdentifier(), toolList[i]->GetDescription());
else
tempButton = new ToolButton(ui::Point(currentX, YRES+1), ui::Point(30, 18), toolList[i]->GetName(), toolList[i]->GetDescription());
tempButton = new ToolButton(ui::Point(currentX, YRES+1), ui::Point(30, 18), toolList[i]->GetName(), toolList[i]->GetIdentifier(), toolList[i]->GetDescription());

//currentY -= 17;
currentX -= 31;
Expand Down Expand Up @@ -832,7 +850,7 @@ void GameView::NotifyColourPresetsChanged(GameModel * sender)
int i = 0;
for(std::vector<ui::Colour>::iterator iter = colours.begin(), end = colours.end(); iter != end; ++iter)
{
ToolButton * tempButton = new ToolButton(ui::Point(currentX, YRES+1), ui::Point(30, 18), "", "Decoration Presets.");
ToolButton * tempButton = new ToolButton(ui::Point(currentX, YRES+1), ui::Point(30, 18), "", "", "Decoration Presets.");
tempButton->Appearance.BackgroundInactive = *iter;
tempButton->SetActionCallback(new ColourPresetAction(this, i));

Expand Down
5 changes: 5 additions & 0 deletions src/gui/game/Menu.h
Expand Up @@ -45,6 +45,11 @@ class Menu
{
tools.push_back(tool_);
}

void ClearTools()
{
tools.clear();
}
};


Expand Down
11 changes: 9 additions & 2 deletions src/gui/game/ToolButton.cpp
@@ -1,11 +1,14 @@
#include "ToolButton.h"
#include "gui/interface/Keys.h"
#include "Favorite.h"

ToolButton::ToolButton(ui::Point position, ui::Point size, std::string text_, std::string toolTip):
ui::Button(position, size, text_, toolTip)
ToolButton::ToolButton(ui::Point position, ui::Point size, std::string text_, std::string toolIdentifier, std::string toolTip):
ui::Button(position, size, text_, toolTip),
toolIdentifier(toolIdentifier)
{
SetSelectionState(-1);
Appearance.BorderActive = ui::Colour(255, 0, 0);
Appearance.BorderFavorite = ui::Colour(255, 255, 0);

//don't use "..." on elements that have long names
buttonDisplayText = ButtonText.substr(0, 7);
Expand Down Expand Up @@ -56,6 +59,10 @@ void ToolButton::Draw(const ui::Point& screenPos)
{
g->drawrect(screenPos.X, screenPos.Y, Size.X, Size.Y, Appearance.BorderActive.Red, Appearance.BorderActive.Green, Appearance.BorderActive.Blue, Appearance.BorderActive.Alpha);
}
else if (Favorite::Ref().IsFavorite(toolIdentifier) && currentSelection == -1)
{
g->drawrect(screenPos.X, screenPos.Y, Size.X, Size.Y, Appearance.BorderFavorite.Red, Appearance.BorderFavorite.Green, Appearance.BorderFavorite.Blue, Appearance.BorderFavorite.Alpha);
}
else
{
g->drawrect(screenPos.X, screenPos.Y, Size.X, Size.Y, Appearance.BorderInactive.Red, Appearance.BorderInactive.Green, Appearance.BorderInactive.Blue, Appearance.BorderInactive.Alpha);
Expand Down
3 changes: 2 additions & 1 deletion src/gui/game/ToolButton.h
Expand Up @@ -6,8 +6,9 @@
class ToolButton: public ui::Button
{
int currentSelection;
std::string toolIdentifier;
public:
ToolButton(ui::Point position, ui::Point size, std::string text_, std::string toolTip = "");
ToolButton(ui::Point position, ui::Point size, std::string text_, std::string toolIdentifier, std::string toolTip = "");
virtual void OnMouseUnclick(int x, int y, unsigned int button);
virtual void OnMouseUp(int x, int y, unsigned int button);
virtual void OnMouseClick(int x, int y, unsigned int button);
Expand Down
1 change: 1 addition & 0 deletions src/gui/interface/Appearance.h
Expand Up @@ -38,6 +38,7 @@ namespace ui
ui::Colour BorderHover;
ui::Colour BorderInactive;
ui::Colour BorderActive;
ui::Colour BorderFavorite;
ui::Colour BorderDisabled;

ui::Border Margin;
Expand Down
1 change: 1 addition & 0 deletions src/simulation/SimulationData.cpp
Expand Up @@ -158,6 +158,7 @@ menu_section * LoadMenus(int & menuCount)
{"\xD2", "Game Of Life", 0, 1},
{"\xD7", "Tools", 0, 1},
{"\xE4", "Decoration tools", 0, 1},
{"\xCC", "Favorites", 0, 1},
{"\xC8", "Cracker", 0, 0},
{"\xC8", "Cracker!", 0, 0},
};
Expand Down
7 changes: 4 additions & 3 deletions src/simulation/SimulationData.h
Expand Up @@ -15,9 +15,10 @@
#define SC_LIFE 12
#define SC_TOOL 13
#define SC_DECO 14
#define SC_CRACKER 15
#define SC_CRACKER2 16
#define SC_TOTAL 15
#define SC_FAVORITES 15
#define SC_CRACKER 16
#define SC_CRACKER2 17
#define SC_TOTAL 16

#define O_WL_WALLELEC 122
#define O_WL_EWALL 123
Expand Down

0 comments on commit 083d488

Please sign in to comment.