diff options
| author | Simon Robertshaw <simon@hardwired.org.uk> | 2013-03-24 12:24:17 (GMT) |
|---|---|---|
| committer | Simon Robertshaw <simon@hardwired.org.uk> | 2013-03-24 12:24:17 (GMT) |
| commit | 9b5b85f9b01cbda7ef9a7ec2a15b2a35630a5b9d (patch) | |
| tree | ac7d040253b459ce102e476cb19ab59e3cfa90d7 /src/gui/console/ConsoleModel.cpp | |
| parent | 6bf98ccdca39936a3c51367862eed7c49f8786ec (diff) | |
| parent | bdc69f31c0be94191015838886bdcc2bc67f1acb (diff) | |
| download | powder-9b5b85f9b01cbda7ef9a7ec2a15b2a35630a5b9d.zip powder-9b5b85f9b01cbda7ef9a7ec2a15b2a35630a5b9d.tar.gz | |
Merge branch 'reorganisation' of github.com:FacialTurd/The-Powder-Toy
Diffstat (limited to 'src/gui/console/ConsoleModel.cpp')
| -rw-r--r-- | src/gui/console/ConsoleModel.cpp | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/src/gui/console/ConsoleModel.cpp b/src/gui/console/ConsoleModel.cpp new file mode 100644 index 0000000..ceb6b32 --- /dev/null +++ b/src/gui/console/ConsoleModel.cpp @@ -0,0 +1,75 @@ +#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())); +} + |
