1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
|
////////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2018 Leo Tenenbaum
// This file is part of GraphColoring.
//
// GraphColoring is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// GraphColoring is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with GraphColoring. If not, see <https://www.gnu.org/licenses/>.
////////////////////////////////////////////////////////////////////////////////
#include "graphcoloring.hpp"
#include "utils/windows.hpp"
#include "utils/filesystem.hpp"
#include <sstream>
namespace graphcoloring {
namespace filesystem = utils::filesystem;
const std::string GraphColoring::TITLE = "Graph Coloring";
GraphColoring::GraphColoring()
: state(State::MAIN_MENU)
{
window = new gui::Window("Graph Coloring", 800, 600);
CheckSaves();
window->OnActivate([this] (){ SwitchToMainMenu(); });
}
GraphColoring::~GraphColoring()
{
DeleteAll();
delete window;
}
void GraphColoring::CheckSaves()
{
if (filesystem::directory_exists("saves"))
{
// Check for version mismatch
pugi::xml_document saves_document;
saves_document.load_file(LevelSelect::LEVEL_LISTING_PATH);
pugi::xml_document assets_document;
assets_document.load_file("assets/levels/level-list.xml");
std::string saves_version(
saves_document.child("category-listing")
.attribute("version").value());
std::string assets_version(
assets_document.child("category-listing")
.attribute("version").value());
if (saves_version != assets_version)
{
// Copy new level list.
filesystem::remove_file(LevelSelect::LEVEL_LISTING_PATH);
filesystem::copy_file(
"assets/levels/level-list.xml",
LevelSelect::LEVEL_LISTING_PATH);
}
return;
}
// Create saves directory
filesystem::create_directory("saves");
filesystem::copy_file(
"assets/levels/level-list.xml", LevelSelect::LEVEL_LISTING_PATH);
pugi::xml_document document;
document.load_file(LevelSelect::LEVEL_LISTING_PATH);
for (pugi::xml_node category
: document.child("category-listing").children("category"))
{
std::stringstream filename;
filename << "saves/" << category.attribute("id").value();
filesystem::create_directory(filename.str());
}
}
void GraphColoring::DeleteAll()
{
mainMenu = nullptr;
levelSelect = nullptr;
level = nullptr;
window->RemoveAllCallbacks();
}
void GraphColoring::SwitchToMainMenu()
{
DeleteAll();
mainMenu = std::make_unique<MainMenu>(window,
[this] (){ SwitchToLevelSelect(); });
window->SetKeyupCallback([this] (gui::Window*){ // Press Escape to quit
window->Quit();
}, GDK_KEY_Escape);
}
void GraphColoring::SwitchToLevelSelect()
{
DeleteAll();
auto callback = [this](std::string category_id,std::string level_id) {
SwitchToLevel(category_id, level_id);
};
levelSelect = std::make_unique<LevelSelect>(window, callback);
window->SetKeyupCallback([this] (gui::Window*){ // Press Escape to go back to menu
SwitchToMainMenu();
}, GDK_KEY_Escape);
}
void GraphColoring::SwitchToLevel(std::string category_id, std::string level_id)
{
DeleteAll();
level = std::make_unique<Level>(window, category_id, level_id);
window->SetKeyupCallback([this] (gui::Window*) { // Press Escape to go back to level selection
level->Save();
SwitchToLevelSelect();
}, GDK_KEY_Escape);
window->SetKeyupCallback([this] (gui::Window*) { // Press Control-N to go to the next level
if (!window->IsControlDown()) return;
level->Save();
std::pair<std::string,std::string> next_level = level->NextLevel();
if (next_level.first.empty()) // No next level
SwitchToLevelSelect();
else
SwitchToLevel(next_level.first, next_level.second);
}, GDK_KEY_n);
}
void GraphColoring::Start()
{
window->Mainloop();
}
} // namespace graphcoloring
|