summaryrefslogtreecommitdiff
path: root/src/console/ConsoleModel.cpp
diff options
context:
space:
mode:
authorSimon Robertshaw <simon@hardwired.org.uk>2012-11-17 19:44:09 (GMT)
committer Simon Robertshaw <simon@hardwired.org.uk>2012-11-17 19:44:09 (GMT)
commit058a2edd75debbd0297f92572316daa704bd379f (patch)
treead303f091f9a08b209b91eb34a9fcad996a3de69 /src/console/ConsoleModel.cpp
parente3594aba9e05c6865d396418c028049cda92c2f3 (diff)
parent7a21ae192fe19868539956f3fe28e62b2c7c4429 (diff)
downloadpowder-058a2edd75debbd0297f92572316daa704bd379f.zip
powder-058a2edd75debbd0297f92572316daa704bd379f.tar.gz
Merge branch 'master' of github.com:FacialTurd/PowderToypp
Diffstat (limited to 'src/console/ConsoleModel.cpp')
-rw-r--r--src/console/ConsoleModel.cpp82
1 files changed, 82 insertions, 0 deletions
diff --git a/src/console/ConsoleModel.cpp b/src/console/ConsoleModel.cpp
new file mode 100644
index 0000000..df900a3
--- /dev/null
+++ b/src/console/ConsoleModel.cpp
@@ -0,0 +1,82 @@
+/*
+ * ConsoleModel.cpp
+ *
+ * Created on: Feb 1, 2012
+ * Author: Simon
+ */
+
+#include "client/Client.h"
+#include "ConsoleModel.h"
+
+ConsoleModel::ConsoleModel() {
+ std::vector<std::string> previousHistory = Client::Ref().GetPrefStringArray("Console.History");
+ for(std::vector<std::string>::reverse_iterator iter = previousHistory.rbegin(), end = previousHistory.rend(); iter != end; ++iter)
+ {
+ if(previousCommands.size()<25)
+ {
+ previousCommands.push_front(ConsoleCommand(*iter, 0, ""));
+ currentCommandIndex = previousCommands.size();
+ }
+ }
+}
+
+void ConsoleModel::AddObserver(ConsoleView * observer)
+{
+ observers.push_back(observer);
+ observer->NotifyPreviousCommandsChanged(this);
+}
+
+int ConsoleModel::GetCurrentCommandIndex()
+{
+ return currentCommandIndex;
+}
+
+void ConsoleModel::SetCurrentCommandIndex(int index)
+{
+ currentCommandIndex = index;
+ notifyCurrentCommandChanged();
+}
+
+ConsoleCommand ConsoleModel::GetCurrentCommand()
+{
+ if(currentCommandIndex < 0 || currentCommandIndex >= previousCommands.size())
+ {
+ return ConsoleCommand("", 0, "");
+ }
+ return previousCommands[currentCommandIndex];
+}
+
+void ConsoleModel::AddLastCommand(ConsoleCommand command)
+{
+ previousCommands.push_back(command);
+ if(previousCommands.size()>25)
+ previousCommands.pop_front();
+ currentCommandIndex = previousCommands.size();
+ notifyPreviousCommandsChanged();
+}
+
+std::deque<ConsoleCommand> ConsoleModel::GetPreviousCommands()
+{
+ return previousCommands;
+}
+
+void ConsoleModel::notifyPreviousCommandsChanged()
+{
+ for(int i = 0; i < observers.size(); i++)
+ {
+ observers[i]->NotifyPreviousCommandsChanged(this);
+ }
+}
+
+void ConsoleModel::notifyCurrentCommandChanged()
+{
+ for(int i = 0; i < observers.size(); i++)
+ {
+ observers[i]->NotifyCurrentCommandChanged(this);
+ }
+}
+
+ConsoleModel::~ConsoleModel() {
+ Client::Ref().SetPref("Console.History", std::vector<std::string>(previousCommands.begin(), previousCommands.end()));
+}
+