Commit 1529283e authored by Loic Guegan's avatar Loic Guegan
Browse files

Improve settings

parent 04d257c1
Pipeline #215783579 failed with stage
in 7 minutes and 42 seconds
......@@ -16,6 +16,8 @@ BoardPanel::BoardPanel(wxFrame *parent) :
UA->IsDrawingArrow = false;
UA->ArrowStartingSquare = boost::none;
UA->WasDragging = false;
CNF.Subscribe(this);
Configure();
}
void BoardPanel::SetPiecesSkin(std::string path) {
......@@ -33,6 +35,16 @@ void BoardPanel::paintEvent(wxPaintEvent&WXUNUSED(evt)) {
render(dc);
}
void BoardPanel::Configure(){
C.ActiveSkin->LoadPiecesSkin(CNF.GetThemePath(CNF.Get<string>("board_view.theme.pieces"),false));
C.ActiveSkin->LoadSquaresSkin(CNF.GetThemePath(CNF.Get<string>("board_view.theme.board"),true));
}
void BoardPanel::Notify(SUBJECT sbj){
Configure();
Refresh();
}
void BoardPanel::render(wxDC &dc) {
C.RefreshDC(dc);
......
......@@ -11,6 +11,7 @@
#include <sstream>
#include <boost/thread/mutex.hpp>
#include "ochess.hpp"
#include "utils/observer/Observer.hpp"
using namespace ochess::model;
......@@ -25,7 +26,7 @@ typedef struct UserActions {
bool WasDragging;
} UserActions;
class BoardPanel: public wxPanel {
class BoardPanel: public wxPanel, public Observer {
private:
BoardController C;
std::shared_ptr<UserActions> UA;
......@@ -41,6 +42,8 @@ public:
void HandleKey(wxKeyEvent &event);
void SetPiecesSkin(std::string path);
void SetBoardSkin(std::string path);
void Notify(SUBJECT sbj) override;
void Configure();
DECLARE_EVENT_TABLE()
};
......
......@@ -33,7 +33,7 @@ wxFrame(NULL, wxID_ANY, title, pos, size) {
SetMenuBar(menuBar);
// Create required windows
Create(WIN_SETTINGS);
Create(WIN_BOARD);
// Setup status bar
CreateStatusBar();
......
/*
* Applicable.hpp
*
* Created on: 13 nov. 2020
* Author: loic
*/
#pragma once
namespace ochess {
namespace gui {
class Applicable {
public:
virtual void Apply()=0;
};
}
}
......@@ -10,9 +10,53 @@
namespace ochess {
namespace gui {
BoardSettings::BoardSettings(wxWindow *parent):wxPanel(parent,wxID_ANY,wxDefaultPosition) {
new wxButton(this,wxID_ANY,"BoardSettings");
BoardSettings::BoardSettings(wxWindow *parent,BOARD_SETTING_MODE mode):wxPanel(parent,wxID_ANY,wxDefaultPosition) {
VBox=new wxBoxSizer(wxVERTICAL);
Mode=mode;
switch(mode){
case BOARD_SETTING_GENERAL:
SetupGeneralMode();
break;
case BOARD_SETTING_APPEARANCE:
SetupAppearanceMode();
break;
}
this->SetSizer(VBox);
//this->SetBackgroundColour(*wxBLUE);
}
void BoardSettings::SetupGeneralMode(){
VBox->Add(new wxButton(this,wxID_ANY,"General"));
}
void BoardSettings::SetupAppearanceMode(){
// Pieces theme
wxStaticText* pieceLabel=new wxStaticText(this, wxID_ANY, "Pieces Theme");
wxFont font=pieceLabel->GetFont();
font.SetWeight(wxFONTWEIGHT_BOLD);
pieceLabel->SetFont(font);
VBox->Add(pieceLabel,0,wxEXPAND);
pieceTheme=new wxChoice(this,wxID_ANY);
for(string location: CNF.ListPiecesThemes()){
pieceTheme->Append(bfs::path(location).stem().string());
}
pieceTheme->SetSelection(pieceTheme->FindString(CNF.Get<string>("board_view.theme.pieces"), true));
VBox->Add(pieceTheme,0,wxTOP,10);
// Board theme
wxStaticText* boardLabel=new wxStaticText(this, wxID_ANY, "Board Theme");
boardLabel->SetFont(font);
VBox->Add(boardLabel,0,wxEXPAND|wxTOP,30);
boardTheme=new wxChoice(this,wxID_ANY);
for(string location: CNF.ListBoardThemes()){
boardTheme->Append(bfs::path(location).stem().string());
}
boardTheme->SetSelection(0);
VBox->Add(boardTheme,0,wxTOP,10);
}
void BoardSettings::Apply(){
if(Mode == BOARD_SETTING_APPEARANCE){
CNF.Set<wxString>("board_view.theme.pieces", pieceTheme->GetStringSelection());
CNF.Set<wxString>("board_view.theme.board", boardTheme->GetStringSelection());
}
}
} /* namespace gui */
} /* namespace ochess */
......@@ -11,15 +11,32 @@
#ifndef WX_PRECOMP
#include <wx/wx.h>
#endif
#include <boost/filesystem.hpp>
#include "ochess.hpp"
#include "Applicable.hpp"
namespace bfs = boost::filesystem;
namespace ochess {
namespace gui {
typedef enum BOARD_SETTING_MODE {
BOARD_SETTING_GENERAL,BOARD_SETTING_APPEARANCE
} BOARD_SETTING_MODE;
class BoardSettings: public wxPanel, public Applicable {
private:
wxBoxSizer *VBox;
void SetupGeneralMode();
void SetupAppearanceMode();
BOARD_SETTING_MODE Mode;
// Appearance
wxChoice *pieceTheme;
wxChoice *boardTheme;
class BoardSettings: public wxPanel {
public:
BoardSettings(wxWindow* parent);
BoardSettings(wxWindow* parent,BOARD_SETTING_MODE mode);
void Apply() override;
};
} /* namespace gui */
......
/*
* Engine.cpp
*
* Created on: 13 nov. 2020
* Author: loic
*/
#include "EngineSettings.hpp"
namespace ochess {
namespace gui {
EngineSettings::EngineSettings(wxWindow* parent):wxPanel(parent,wxID_ANY,wxDefaultPosition) {
wxStaticText* pieceLabel=new wxStaticText(this, wxID_ANY, "Engine");
}
void EngineSettings::Apply(){
}
} /* namespace gui */
} /* namespace ochess */
/*
* Engine.hpp
*
* Created on: 13 nov. 2020
* Author: loic
*/
#pragma once
#include <wx/wxprec.h>
#ifndef WX_PRECOMP
#include <wx/wx.h>
#endif
#include "Applicable.hpp"
namespace ochess {
namespace gui {
class EngineSettings: public wxPanel, public Applicable{
public:
EngineSettings(wxWindow* parent);
void Apply() override;
};
} /* namespace gui */
} /* namespace ochess */
......@@ -12,6 +12,11 @@
namespace ochess {
namespace gui {
enum ID_SETTINGS_BTN{
ID_SETTINGS_APPLY,
ID_SETTINGS_CANCEL,
ID_SETTINGS_OK
};
Settings::Settings(wxFrame *parent) :
wxDialog(parent, -1, "Board Settings",wxDefaultPosition,wxSize(800,700)) {
......@@ -26,15 +31,28 @@ Settings::Settings(wxFrame *parent) :
MenuTreeNode* selected=entry;
entry=model->CreateEntry("Board",1);
model->CreateSubEntry(entry,"Appearance",2);
entry=model->CreateEntry("Engine",3);
menu->AssociateModel(model);
for(auto item:model->GetItems()) // Expand everything
menu->Expand(item);
menu->Select(wxDataViewItem((void*)selected));
menu->Bind(wxEVT_DATAVIEW_SELECTION_CHANGED, &Settings::Button, this);
menu->Bind(wxEVT_DATAVIEW_SELECTION_CHANGED, &Settings::MenuHandler, this);
// Create all the settings windows an add them to the book
Book=new wxSimplebook(MainPanel,wxID_ANY,wxDefaultPosition,wxSize(600,-1));
Book->ShowNewPage(new BoardSettings(Book));
BoardSettings* test=new BoardSettings(Book,BOARD_SETTING_GENERAL);
ToApply.push_back(test);
Book->AddPage(test,"General");
BoardSettings* bsg=new BoardSettings(Book,BOARD_SETTING_GENERAL);
ToApply.push_back(bsg);
Book->AddPage(bsg,"General");
BoardSettings* bsa=new BoardSettings(Book,BOARD_SETTING_APPEARANCE);
ToApply.push_back(bsa);
Book->AddPage(bsa,"Appearance");
EngineSettings* es=new EngineSettings(Book);
ToApply.push_back(es);
Book->AddPage(es,"Engine");
Book->ChangeSelection(0);
// Setup MainPanel sizers
wxBoxSizer *HBox=new wxBoxSizer(wxHORIZONTAL);
......@@ -45,9 +63,10 @@ Settings::Settings(wxFrame *parent) :
// SubPanel that contains the buttons
wxPanel* SubPanel=new wxPanel(this,wxID_ANY,wxDefaultPosition);
wxBoxSizer* SubPanelSizer=new wxBoxSizer(wxHORIZONTAL);
wxButton* Ok=new wxButton(SubPanel, wxID_ANY, "Ok");
wxButton* Apply=new wxButton(SubPanel, wxID_ANY, "Apply");
wxButton* Cancel=new wxButton(SubPanel, wxID_ANY, "Cancel");
wxButton* Ok=new wxButton(SubPanel, ID_SETTINGS_OK, "Ok");
wxButton* Apply=new wxButton(SubPanel, ID_SETTINGS_APPLY, "Apply");
wxButton* Cancel=new wxButton(SubPanel, ID_SETTINGS_CANCEL, "Cancel");
this->Bind(wxEVT_BUTTON, &Settings::ButtonHandler, this);
SubPanelSizer->AddStretchSpacer(); // Align button on the right side
SubPanelSizer->Add(Ok,0,wxRIGHT,5);
SubPanelSizer->Add(Apply,0,wxRIGHT,5);
......@@ -55,7 +74,7 @@ Settings::Settings(wxFrame *parent) :
SubPanel->SetSizer(SubPanelSizer);
VBox->AddStretchSpacer(1); // Align the buttons at the bottom
VBox->Add(SubPanel,0,wxEXPAND);
HBox->Add(VBox,0,wxEXPAND|wxLEFT,10);
HBox->Add(VBox,0,wxEXPAND|wxLEFT,20);
MainPanel->SetSizer(HBox);
// Sizer of this dialog
......@@ -66,14 +85,38 @@ Settings::Settings(wxFrame *parent) :
this->ShowModal();
}
#include <iostream>
void Settings::Button(wxDataViewEvent& event){
#include <wx/object.h>
#include <wx/defs.h>
void Settings::ButtonHandler(wxCommandEvent &event){
switch(event.GetId()){
case ID_SETTINGS_CANCEL:
this->Close();
break;
case ID_SETTINGS_APPLY:
Apply();
break;
case ID_SETTINGS_OK:
Apply();
this->Close();
break;
}
}
void Settings::Apply(){
for(Applicable *apply:ToApply){
apply->Apply();
}
CNF.Apply();
}
void Settings::OnClose(wxCloseEvent &event){
this->Destroy();
}
void Settings::MenuHandler(wxDataViewEvent& event){
MenuTreeNode* selected=(MenuTreeNode*)event.GetItem().GetID();
if(selected){
if(selected->Id==0)
std::cout << "You selected general" << std::endl;
if(selected->Id==2){
}
Book->ChangeSelection(selected->Id);
}
event.Skip();
}
......
......@@ -18,6 +18,9 @@
#include <algorithm>
#include "BoardSettings.hpp"
#include <wx/simplebook.h>
#include "Applicable.hpp"
#include "ochess.hpp"
#include "EngineSettings.hpp"
using namespace std;
......@@ -29,9 +32,15 @@ private:
wxPanel *MainPanel;
unordered_map<int,wxWindow*> Panels;
wxSimplebook* Book;
vector<Applicable*> ToApply;
void Apply();
public:
Settings(wxFrame *parent);
void Button(wxDataViewEvent &event);
void MenuHandler(wxDataViewEvent &event);
void ButtonHandler(wxCommandEvent &event);
void OnClose(wxCloseEvent &event);
};
} /* namespace gui */
......
#include "ConfigManager.hpp"
#include <iostream>
namespace ochess {
ConfigManager::ConfigManager() {
......@@ -23,29 +23,27 @@ ConfigManager::ConfigManager() {
json_parser::read_json(ConfigFile.string(), Configuration);
} else {
for (auto &root : Roots) {
bfs::path configFile = root / ConfigFile.stem();
bfs::path configFile = root / ConfigFile.filename();
if (bfs::exists(configFile)) {
json_parser::read_json(configFile.string(), Configuration);
break;
}
}
}
// Init configurations
InitDefaultBoardView();
}
void ConfigManager::InitDefaultBoardView(){
CNF_DEFAULT("board_view.theme.pieces",string,"cburnett");
CNF_DEFAULT("board_view.theme.board",string,"chesscom_green");
}
void ConfigManager::Apply() {
json_parser::write_json(ConfigFile.string(), Configuration);
NotifyAll(SBJ_CNF);
}
ptree ConfigManager::operator[](string entry) const{
boost::optional< const ptree& > child=Configuration.get_child_optional(entry);
if(child)
return(child.value());
return(ptree());
}
ptree& ConfigManager::operator[](string entry){
return(Configuration.get_child(entry)); // Might raise a segfault if not found ?
}
vector<string> ConfigManager::ListPiecesThemes() {
vector<string> themes;
......@@ -59,6 +57,20 @@ vector<string> ConfigManager::ListPiecesThemes() {
return (themes);
}
string ConfigManager::GetThemePath(string name, bool isBoard){
name+=".png";
string type="pieces";
if(isBoard)
type="boards";
for (auto &root : Roots) {
bfs::path path=root / type / name;
if (bfs::exists(path)) {
return(path.string());
}
}
return("");
}
vector<string> ConfigManager::ListBoardThemes() {
vector<string> themes;
for (auto &root : Roots) {
......
......@@ -12,6 +12,10 @@ using namespace boost::property_tree;
using namespace std;
namespace bfs = boost::filesystem;
#define CNF_DEFAULT(PATH, TYPE, DEFAULT)\
if(!Configuration.get_optional<TYPE>(PATH))\
Configuration.put(PATH,DEFAULT)
namespace ochess {
/**
......@@ -33,6 +37,8 @@ class ConfigManager : public Subject{
/// @brief Contains the loaded application configuration
ptree Configuration;
void InitDefaultBoardView();
public:
ConfigManager();
......@@ -45,8 +51,17 @@ public:
*/
void Apply();
ptree operator[](string entry) const;
ptree& operator[](string entry);
template<typename T>
T Get(string path){
return(Configuration.get<T>(path));
}
template<typename T>
void Set(string path, T value){
Configuration.put<T>(path, value);
}
string GetThemePath(string name, bool isBoard);
/**
* @brief List pieces themes absolute paths presents in all known configure directories.
......
......@@ -6,6 +6,7 @@
*/
#include "Subject.hpp"
#include <iostream>
namespace ochess {
......
......@@ -22,16 +22,14 @@ private:
vector<Observer*> Observers;
SUBJECT Insight;
protected:
void NotifyAll();
void NotifyAll(SUBJECT insight);
void NotifyAll();
public:
Subject(SUBJECT insight);
Subject();
void Subscribe(Observer* obs);
void Unsubscribe(Observer *obs);
};
} /* namespace ochess */
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment