forked from stefanhendriks/Dune-II---The-Maker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConsole.cpp
103 lines (81 loc) · 2.78 KB
/
Console.cpp
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
#include "Console.hpp"
#include "Game.hpp"
#include <boost/algorithm/string.hpp>
Console::Console(MessageSystem& messages, const std::vector<Player> &players):
chatLayout(sfg::Box::Create(sfg::Box::VERTICAL, 5.f)),
chatAreaLayout(sfg::Box::Create(sfg::Box::VERTICAL)),
chatEntry(sfg::Entry::Create()),
chatWindow(sfg::ScrolledWindow::Create()),
window(sfg::Window::Create(sfg::Window::SHADOW)),
messages(messages), players(players)
{
chatWindow->SetRequisition(sf::Vector2f(750.f,100.f));
chatWindow->SetScrollbarPolicy( sfg::ScrolledWindow::HORIZONTAL_AUTOMATIC | sfg::ScrolledWindow::VERTICAL_AUTOMATIC );
chatWindow->AddWithViewport(chatAreaLayout);
chatLayout->Pack(chatWindow);
chatLayout->Pack(chatEntry);
chatEntry->GetSignal(sfg::Entry::OnKeyPress).Connect(&Console::dataReady, this);
window->Add(chatLayout);
window->Show(false);
desktop.LoadThemeFromFile("gui.theme");
desktop.Add(window);
chatEntry->GrabFocus();
unitMap["trike"] = Unit::Type::Trike;
unitMap["quad"] = Unit::Type::Quad;
unitMap["devastator"] = Unit::Type::Devastator;
unitMap["carryall"] = Unit::Type::Carryall;
unitMap["frigate"] = Unit::Type::Frigate;
unitMap["soldier"] = Unit::Type::Soldier;
}
void Console::update(sf::Time dt)
{
desktop.Update(dt.asSeconds());
}
void Console::handleEvent(sf::Event event)
{
desktop.HandleEvent(event);
}
void Console::display(sf::RenderWindow &screen)
{
sfgui.Display(screen);
}
void Console::toggle()
{
window->Show(!window->IsLocallyVisible());
}
void Console::dataReady()
{
if (!sf::Keyboard::isKeyPressed(sf::Keyboard::Return)) return; //this is cheap
std::string toProcess(chatEntry->GetText());
if (toProcess.empty()) return;
chatEntry->SetText("");
std::vector<std::string> tokens;
boost::split(tokens, toProcess, boost::is_any_of(" "));
if (tokens[0]=="add"){
if (tokens.size() != 5){
toConsole("Incorrect number of arguments.");
return;
}
messages.triggerEvent(CreateUnitMessage(unitMap[tokens[1]], players[std::stoi(tokens[2])],
{std::stof(tokens[3]), std::stof(tokens[4])}));
toConsole("Unit added.");
}else{
toConsole("Bad command.");
return;
}
}
void Console::toConsole(std::string toWrite)
{
sfg::Container::WidgetsList lines(chatAreaLayout->GetChildren());
if (lines.size()>120) //if more, pop front
chatAreaLayout->Remove(lines.front());
sfg::Label::Ptr toAdd(sfg::Label::Create(toWrite));
toAdd->SetAlignment(sf::Vector2f(0.f,0.f));
chatAreaLayout->PackEnd(toAdd);
autoscroll();
}
void Console::autoscroll()
{
sfg::Adjustment::Ptr toAdjust(chatWindow->GetVerticalAdjustment());
toAdjust->SetValue(toAdjust->GetUpper());
}