Commit 3f382b4d authored by Loic Guegan's avatar Loic Guegan
Browse files

Cleaning code

parent 2c37946e
Pipeline #213286939 passed with stage
in 7 minutes and 35 seconds
......@@ -3,22 +3,18 @@
#include "View.hpp"
#include <vector>
#include <mutex>
namespace ochess {
namespace gui {
typedef enum CTRL_TYPE {
BOARD, ENGINE, UNKNOWN
} CTRL_TYPE;
class Controller {
protected:
std::vector<View*> RegisteredViews;
std::mutex model;
public:
virtual CTRL_TYPE GetType() {
return (UNKNOWN);
}
void RegisterView(View *v) {
RegisteredViews.push_back(v);
}
......
......@@ -30,7 +30,7 @@ public:
/**
* Should return a pointer to the actual window controller
*/
virtual std::shared_ptr<Controller> GetController() = 0;
virtual Controller* GetController() = 0;
virtual wxMenu* GetMenu() {
return (nullptr);
}
......
......@@ -9,7 +9,7 @@ EVT_KEY_DOWN(BoardPanel::HandleKey)
END_EVENT_TABLE()
BoardPanel::BoardPanel(wxFrame *parent) :
wxPanel(parent), Configurable("BoardView"), C(std::make_shared<BoardController>(this)) {
wxPanel(parent), Configurable("BoardView"), C(this) {
UA = std::make_shared<UserActions>();
CNF.AddModule(this);
UA->IsDraggingPiece = false;
......@@ -20,12 +20,12 @@ BoardPanel::BoardPanel(wxFrame *parent) :
}
void BoardPanel::SetPiecesSkin(std::string path) {
C->ActiveSkin->LoadPiecesSkin(path);
C.ActiveSkin->LoadPiecesSkin(path);
Refresh();
}
void BoardPanel::SetBoardSkin(std::string path) {
C->ActiveSkin->LoadSquaresSkin(path);
C.ActiveSkin->LoadSquaresSkin(path);
Refresh();
}
......@@ -39,89 +39,89 @@ void BoardPanel::Touch() {
}
void BoardPanel::render(wxDC &dc) {
C->RefreshDC(dc);
C.RefreshDC(dc);
// Draw Board
C->DrawBorder();
C.DrawBorder();
for (short j = 0; j <= 7; j++) {
for (short i = 0; i <= 7; i++) {
Coord XY(i, j);
if ((i + j) % 2 == 0)
C->DrawSquare(i, j, 'S');
C.DrawSquare(i, j, 'S');
else
C->DrawSquare(i, j, 's');
C.DrawSquare(i, j, 's');
}
}
// Draw highlighted squares
for (auto &square : C->HLS)
C->HighlightSquare(square.i, square.j);
for (auto &square : C.HLS)
C.HighlightSquare(square.i, square.j);
// Draw Pieces
for (short j = 0; j <= 7; j++) {
for (short i = 0; i <= 7; i++) {
Coord XY(i, j);
boost::optional<PPiece> p = C->G.GetPiece(XY);
boost::optional<PPiece> p = C.G.GetPiece(XY);
if (p) {
if (!(UA->IsDraggingPiece && UA->DraggingPieceSquare
&& UA->DraggingPieceSquare.value() == XY))
C->DrawPiece(i, j, p.value()->GetName());
C.DrawPiece(i, j, p.value()->GetName());
}
}
}
// Draw the dragging piece
if (UA->IsDraggingPiece && UA->DraggingPieceSquare) {
boost::optional<PPiece> p = C->G.GetPiece(
boost::optional<PPiece> p = C.G.GetPiece(
UA->DraggingPieceSquare.value());
if (p)
C->DrawPieceOnMouse(p.value()->GetName());
C.DrawPieceOnMouse(p.value()->GetName());
}
// Draw arrows
for (auto &arrow : C->Arrows)
C->DrawArrow(arrow.first.i, arrow.first.j, arrow.second.i,
for (auto &arrow : C.Arrows)
C.DrawArrow(arrow.first.i, arrow.first.j, arrow.second.i,
arrow.second.j);
// Select promoted pawn ?
if (C->G.GetNextPromotingPawn()) {
Coord pos = C->G.GetNextPromotingPawn().value();
C->DrawPromote(pos.i, pos.j, C->G.GetPiece(pos).value()->GetColor());
if (C.G.GetNextPromotingPawn()) {
Coord pos = C.G.GetNextPromotingPawn().value();
C.DrawPromote(pos.i, pos.j, C.G.GetPiece(pos).value()->GetColor());
}
}
void BoardPanel::HandleKey(wxKeyEvent &event) {
switch (event.GetKeyCode()) {
case WXK_LEFT:
C->G.Previous();
C.G.Previous();
Refresh();
break;
case WXK_RIGHT:
C->G.Next();
C.G.Next();
Refresh();
break;
}
}
void BoardPanel::HandleMouse(wxMouseEvent &event) {
C->MouseEvent = event;
C.MouseEvent = event;
if (event.LeftUp()) {
wxPoint pos = event.GetPosition();
boost::optional<Coord> square = C->GetSquareAt(pos.x, pos.y);
boost::optional<Coord> square = C.GetSquareAt(pos.x, pos.y);
if (square && UA->DraggingPieceSquare) {
if (UA->DraggingPieceSquare.value() != square.value()) {
C->Move(UA->DraggingPieceSquare.value(), square.value());
C.Move(UA->DraggingPieceSquare.value(), square.value());
/// @todo Optimize call to isCheckMate
if (C->G.IsCheckMate(ochess::model::BLACK)) {
if (C.G.IsCheckMate(ochess::model::BLACK)) {
wxMessageBox("Blacks lost", "Info",
wxOK | wxICON_INFORMATION);
}
if (C->G.IsCheckMate(ochess::model::WHITE)) {
if (C.G.IsCheckMate(ochess::model::WHITE)) {
wxMessageBox("Whites lost", "Info",
wxOK | wxICON_INFORMATION);
}
if (C->G.IsStaleMate(ochess::model::WHITE)
|| C->G.IsStaleMate(ochess::model::BLACK)) {
if (C.G.IsStaleMate(ochess::model::WHITE)
|| C.G.IsStaleMate(ochess::model::BLACK)) {
wxMessageBox("Draw by StaleMate", "Info",
wxOK | wxICON_INFORMATION);
}
......@@ -133,16 +133,16 @@ void BoardPanel::HandleMouse(wxMouseEvent &event) {
}
if (event.RightUp()) {
wxPoint pos = event.GetPosition();
boost::optional<Coord> square = C->GetSquareAt(pos.x, pos.y);
boost::optional<Coord> square = C.GetSquareAt(pos.x, pos.y);
// If we are not Drawing an arrow
if (!UA->WasDragging) {
if (square)
C->HLS.push_back(square.value());
C.HLS.push_back(square.value());
Refresh();
} else {
if (square && UA->ArrowStartingSquare) {
if (square.value() != UA->ArrowStartingSquare.value()) {
C->Arrows.push_back(
C.Arrows.push_back(
std::make_pair(UA->ArrowStartingSquare.value(),
square.value()));
Refresh();
......@@ -153,32 +153,32 @@ void BoardPanel::HandleMouse(wxMouseEvent &event) {
}
if (event.LeftDown()) {
wxPoint pos = event.GetPosition();
boost::optional<Coord> promotedOpt = C->G.GetNextPromotingPawn();
boost::optional<Coord> promotedOpt = C.G.GetNextPromotingPawn();
if (promotedOpt) {
boost::optional<Coord> squareOpt = C->GetSquareAt(pos.x, pos.y);
boost::optional<Coord> squareOpt = C.GetSquareAt(pos.x, pos.y);
Coord promoted = promotedOpt.value();
if (squareOpt) {
Coord square = squareOpt.value();
ochess::model::COLOR c =
C->G.GetPiece(promoted).value()->GetColor();
boost::optional<char> pieceName = C->GetPromotedPiece(promoted.i,
C.G.GetPiece(promoted).value()->GetColor();
boost::optional<char> pieceName = C.GetPromotedPiece(promoted.i,
promoted.j, square.i, square.j, c);
if (pieceName)
C->G.Promote((char)pieceName.value());
C.G.Promote((char)pieceName.value());
}
} else {
UA->DraggingPieceSquare = C->GetSquareAt(pos.x, pos.y);
UA->DraggingPieceSquare = C.GetSquareAt(pos.x, pos.y);
// Left click removes every arrow/square hightlight on the board
C->HLS.clear();
C->Arrows.clear();
C.HLS.clear();
C.Arrows.clear();
}
}
if (event.RightDown()) {
wxPoint pos = event.GetPosition();
UA->ArrowStartingSquare = C->GetSquareAt(pos.x, pos.y);
UA->ArrowStartingSquare = C.GetSquareAt(pos.x, pos.y);
}
if (event.Dragging() && UA->DraggingPieceSquare) {
if (!C->G.IsEmpty(UA->DraggingPieceSquare.value())) {
if (!C.G.IsEmpty(UA->DraggingPieceSquare.value())) {
UA->IsDraggingPiece = true;
Refresh();
}
......
......@@ -29,7 +29,7 @@ typedef struct UserActions {
class BoardPanel: public wxPanel, public Configurable, public View {
private:
std::shared_ptr<BoardController> C;
BoardController C;
std::shared_ptr<UserActions> UA;
public:
......@@ -50,8 +50,8 @@ public:
void Initialize() override {
}
;
std::shared_ptr<Controller> GetController() override {
return (C);
Controller* GetController() override {
return (&C);
}
DECLARE_EVENT_TABLE()
......
......@@ -22,7 +22,7 @@ BoardView::BoardView(wxFrame *parent) :
}
std::shared_ptr<Controller> BoardView::GetController() {
Controller* BoardView::GetController() {
return (nullptr);
}
......
......@@ -15,7 +15,7 @@ private:
public:
BoardPanel *BP;
BoardView(wxFrame *parent);
std::shared_ptr<Controller> GetController() override;
Controller* GetController() override;
};
}
}
......
......@@ -17,6 +17,7 @@ BoardController::BoardController(View *v) :
}
void BoardController::Move(Coord src, Coord dst) {
model.lock();
if (G.Move(src, dst) && BlackEngine != NULL) {
FenState state = G.GetState();
if (state.ActiveColor == ochess::model::BLACK) {
......@@ -32,6 +33,7 @@ void BoardController::Move(Coord src, Coord dst) {
G.Promote(m.promoteTo);
}
}
model.unlock();
}
} // namespace gui
} // namespace ochess
......@@ -32,9 +32,7 @@ public:
BoardController(View *v);
void Move(Coord src, Coord dst);
CTRL_TYPE GetType() override {
return (BOARD);
}
};
} // namespace gui
......
......@@ -5,7 +5,7 @@ namespace gui {
EngineView::EngineView() :
wxDialog(NULL, -1, "Engine Configuration", wxDefaultPosition,
wxSize(250, 230)),C(std::make_shared<EngineController>(this)) {
wxSize(250, 230)),C(this) {
// panel = new wxPanel(this, -1);
this->Bind(wxEVT_CLOSE_WINDOW, &EngineView::OnClose, this);
menu = new wxMenu;
......@@ -29,8 +29,8 @@ void EngineView::AddEngine(wxCommandEvent &event) {
this->Show();
}
std::shared_ptr<Controller> EngineView::GetController() {
return (C);
Controller* EngineView::GetController() {
return (&C);
}
} // namespace gui
} // namespace ochess
......@@ -6,20 +6,20 @@
#include "ochess.hpp"
#include "wx/sizer.h"
#include "wx/wx.h"
#include "../../log.hpp"
#include "log.hpp"
namespace ochess {
namespace gui {
class EngineView: public wxDialog, public View {
private:
std::shared_ptr<EngineController> C;
EngineController C;
//wxPanel *panel;
wxMenu *menu;
public:
EngineView();
std::shared_ptr<Controller> GetController() override;
Controller* GetController() override;
wxMenu* GetMenu() override;
void OnClose(wxCloseEvent &event);
void AddEngine(wxCommandEvent &event);
......
......@@ -33,21 +33,29 @@ wxMenu* MainFrame::getSquaresMenu() {
return themes;
}
void MainFrame::Create(CTRL name){
View *v=nullptr;
switch(name){
case BRD:
v=new BoardView((wxFrame*) this);
Controllers[name]=v->GetController();
void MainFrame::Create(CTRL name) {
switch (name) {
case BOARD: {
BoardView *b = new BoardView((wxFrame*) this);
Panels.push_back(b);
Views[name] =b;
break;
}
if(v!=nullptr)
OchessViews.push_back(v);
case ENGINE:
{
EngineView *e = new EngineView();
Panels.push_back(e);
Views[name] = e;
break;
}
break;
}
}
MainFrame::MainFrame(const wxString &title, const wxPoint &pos,
const wxSize &size) :
wxFrame(NULL, wxID_ANY, title, pos, size) {
wxFrame(NULL, wxID_ANY, title, pos, size) {
wxMenu *menuFile = new wxMenu;
menuFile->Append(ID_Hello, "&Hello...\tCtrl-H",
"Help string shown in status bar for this menu item");
......@@ -60,12 +68,10 @@ MainFrame::MainFrame(const wxString &title, const wxPoint &pos,
menuBar->Append(menuHelp, "&Help");
menuBar->Append(getPiecesMenu(), "Pieces");
p = new BoardView((wxFrame*) this);
OchessViews.push_back(p);
e = new EngineView();
OchessViews.push_back(e);
Create(BOARD);
//Create(ENGINE);
menuBar->Append(e->GetMenu(), "Engines");
//menuBar->Append(e->GetMenu(), "Engines");
this->Bind(wxEVT_CLOSE_WINDOW, &MainFrame::OnClose, this);
menuBar->Append(getSquaresMenu(), "Board");
SetMenuBar(menuBar);
......@@ -75,8 +81,8 @@ MainFrame::MainFrame(const wxString &title, const wxPoint &pos,
void MainFrame::OnClose(wxCloseEvent &event) {
event.Skip();
e->Destroy();
p->Destroy();
for(auto w:Panels)
w->Destroy();
}
void MainFrame::ChangeSquares(wxCommandEvent &event) {
......@@ -102,7 +108,7 @@ void MainFrame::OnExit(wxCommandEvent &event) {
void MainFrame::OnAbout(wxCommandEvent &event) {
(void) event;
wxMessageBox("This is a wxWidgets' Hello world sample", "About Hello World",
wxOK | wxICON_INFORMATION);
wxOK | wxICON_INFORMATION);
}
void MainFrame::OnHello(wxCommandEvent &event) {
(void) event;
......
#ifndef MAINFRAME_HPP
#define MAINFRAME_HPP
#ifndef GUI_MAINFRAME_HPP
#define GUI_MAINFRAME_HPP
#include <wx/wxprec.h>
#ifndef WX_PRECOMP
#include <wx/wx.h>
#endif
#include "board/BoardView.hpp"
#include "engine/EngineView.hpp"
#include "gui/board/BoardView.hpp"
#include "gui/engine/EngineView.hpp"
#include "ochess.hpp"
#include "ControllerManager.hpp"
#include "ViewManager.hpp"
namespace ochess {
namespace gui {
......@@ -19,7 +19,7 @@ namespace gui {
*
* This class contains every sub-graphical components (board etc..).
*/
class MainFrame: public wxFrame, ControllerManager {
class MainFrame: public wxFrame, ViewManager {
public:
/**
......@@ -35,7 +35,7 @@ private:
*/
BoardView *p;
EngineView *e;
vector<View*> OchessViews;
vector<wxWindow*> Panels;
void OnHello(wxCommandEvent &event);
void OnExit(wxCommandEvent &event);
......
......@@ -5,28 +5,18 @@
* Author: loic
*/
#include "ControllerManager.hpp"
#include "ViewManager.hpp"
namespace ochess {
namespace gui {
void ControllerManager::Acquire(CTRL name){
Mutexes[name].lock();
}
void ControllerManager::Release(CTRL name){
Mutexes[name].unlock();
}
shared_ptr<Controller> ControllerManager::operator[](CTRL name){
View* ViewManager::operator[](CTRL name){
// If the controller is not available, ask the main frame to create it
if(Controllers.find(name)==Controllers.end()){
if(Views.find(name)==Views.end()){
this->Create(name);
}
if(Mutexes[name].try_lock()){
Mutexes[name].unlock();
std::cerr << "Error in Controller Manager, trying to access to " << name << " without a lock" << std::endl;
exit(1);
}
return(Controllers[name]);
return(Views[name]);
}
} /* namespace gui */
......
......@@ -11,7 +11,7 @@
#include <unordered_map>
#include <mutex>
#include <memory>
#include "Controller.hpp"
#include "gui/Controller.hpp"
#include <iostream>
using namespace std;
......@@ -20,20 +20,16 @@ namespace ochess {
namespace gui {
typedef enum CTRL {
BRD
BOARD,ENGINE
} CTRL;
class ControllerManager {
private:
unordered_map<CTRL, mutex> Mutexes;
class ViewManager {
protected:
unordered_map<CTRL, shared_ptr<Controller>> Controllers;
unordered_map<CTRL, View*> Views;
virtual void Create(CTRL name) = 0;
public:
void Acquire(CTRL name);
void Release(CTRL name);
shared_ptr<Controller> operator[](CTRL name);
View* operator[](CTRL name);
};
......
......@@ -17,7 +17,7 @@
#include <cctype> // For tolower()
// ---------- ochess ----------
#include "log.hpp"
#include "gui/MainFrame.hpp"
#include "gui/mainframe/MainFrame.hpp"
#include "engine/Engine.hpp"
#include "config/ConfigManager.hpp"
#include "command/CommandManager.hpp"
......
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