summaryrefslogtreecommitdiff
path: root/src/gui/console
diff options
context:
space:
mode:
authorSimon 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)
commit9b5b85f9b01cbda7ef9a7ec2a15b2a35630a5b9d (patch)
treeac7d040253b459ce102e476cb19ab59e3cfa90d7 /src/gui/console
parent6bf98ccdca39936a3c51367862eed7c49f8786ec (diff)
parentbdc69f31c0be94191015838886bdcc2bc67f1acb (diff)
downloadpowder-9b5b85f9b01cbda7ef9a7ec2a15b2a35630a5b9d.zip
powder-9b5b85f9b01cbda7ef9a7ec2a15b2a35630a5b9d.tar.gz
Merge branch 'reorganisation' of github.com:FacialTurd/The-Powder-Toy
Diffstat (limited to 'src/gui/console')
-rw-r--r--src/gui/console/ConsoleCommand.h23
-rw-r--r--src/gui/console/ConsoleController.cpp74
-rw-r--r--src/gui/console/ConsoleController.h31
-rw-r--r--src/gui/console/ConsoleModel.cpp75
-rw-r--r--src/gui/console/ConsoleModel.h28
-rw-r--r--src/gui/console/ConsoleView.cpp102
-rw-r--r--src/gui/console/ConsoleView.h30
7 files changed, 363 insertions, 0 deletions
diff --git a/src/gui/console/ConsoleCommand.h b/src/gui/console/ConsoleCommand.h
new file mode 100644
index 0000000..31e41b0
--- /dev/null
+++ b/src/gui/console/ConsoleCommand.h
@@ -0,0 +1,23 @@
+#ifndef CONSOLECOMMAND_H_
+#define CONSOLECOMMAND_H_
+
+class ConsoleCommand
+{
+public:
+ ConsoleCommand(std::string command, int returnStatus, std::string returnValue):
+ Command(command), ReturnStatus(returnStatus), ReturnValue(returnValue)
+ {
+
+ }
+ std::string Command;
+ int ReturnStatus;
+ std::string ReturnValue;
+
+ operator std::string() const
+ {
+ return Command;
+ }
+};
+
+
+#endif /* CONSOLECOMMAND_H_ */
diff --git a/src/gui/console/ConsoleController.cpp b/src/gui/console/ConsoleController.cpp
new file mode 100644
index 0000000..602636f
--- /dev/null
+++ b/src/gui/console/ConsoleController.cpp
@@ -0,0 +1,74 @@
+#include <stack>
+#include "ConsoleController.h"
+
+ConsoleController::ConsoleController(ControllerCallback * callback, CommandInterface * commandInterface):
+ HasDone(false)
+{
+ consoleModel = new ConsoleModel();
+ consoleView = new ConsoleView();
+ consoleView->AttachController(this);
+ consoleModel->AddObserver(consoleView);
+
+ this->callback = callback;
+ this->commandInterface = commandInterface;
+}
+
+void ConsoleController::EvaluateCommand(std::string command)
+{
+ if (command.substr(0, 6) == "!load ")
+ CloseConsole();
+ int returnCode = commandInterface->Command(command);
+ if(command.length())
+ consoleModel->AddLastCommand(ConsoleCommand(command, returnCode, commandInterface->GetLastError()));
+ else
+ CloseConsole();
+}
+
+void ConsoleController::CloseConsole()
+{
+ if(ui::Engine::Ref().GetWindow() == consoleView)
+ ui::Engine::Ref().CloseWindow();
+}
+
+std::string ConsoleController::FormatCommand(std::string command)
+{
+ return commandInterface->FormatCommand(command);
+}
+
+void ConsoleController::NextCommand()
+{
+ int cIndex = consoleModel->GetCurrentCommandIndex();
+ if(cIndex < consoleModel->GetPreviousCommands().size())
+ consoleModel->SetCurrentCommandIndex(cIndex+1);
+}
+
+void ConsoleController::PreviousCommand()
+{
+ int cIndex = consoleModel->GetCurrentCommandIndex();
+ if(cIndex > 0)
+ consoleModel->SetCurrentCommandIndex(cIndex-1);
+}
+
+void ConsoleController::Exit()
+{
+ if(ui::Engine::Ref().GetWindow() == consoleView)
+ ui::Engine::Ref().CloseWindow();
+ if(callback)
+ callback->ControllerExit();
+ HasDone = true;
+}
+
+ConsoleView * ConsoleController::GetView()
+{
+ return consoleView;
+}
+
+ConsoleController::~ConsoleController() {
+ if(ui::Engine::Ref().GetWindow() == consoleView)
+ ui::Engine::Ref().CloseWindow();
+ if(callback)
+ delete callback;
+ delete consoleModel;
+ delete consoleView;
+}
+
diff --git a/src/gui/console/ConsoleController.h b/src/gui/console/ConsoleController.h
new file mode 100644
index 0000000..cf8b36e
--- /dev/null
+++ b/src/gui/console/ConsoleController.h
@@ -0,0 +1,31 @@
+#ifndef CONSOLECONTROLLER_H_
+#define CONSOLECONTROLLER_H_
+
+#include <string>
+#include "Controller.h"
+#include "ConsoleView.h"
+#include "ConsoleModel.h"
+#include "ConsoleCommand.h"
+#include "cat/CommandInterface.h"
+
+class ConsoleModel;
+class ConsoleView;
+class ConsoleController {
+ ControllerCallback * callback;
+ ConsoleView * consoleView;
+ ConsoleModel * consoleModel;
+ CommandInterface * commandInterface;
+public:
+ bool HasDone;
+ ConsoleController(ControllerCallback * callback, CommandInterface * commandInterface);
+ std::string FormatCommand(std::string command);
+ void EvaluateCommand(std::string command);
+ void NextCommand();
+ void PreviousCommand();
+ void Exit();
+ void CloseConsole();
+ ConsoleView * GetView();
+ virtual ~ConsoleController();
+};
+
+#endif /* CONSOLECONTROLLER_H_ */
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()));
+}
+
diff --git a/src/gui/console/ConsoleModel.h b/src/gui/console/ConsoleModel.h
new file mode 100644
index 0000000..312739a
--- /dev/null
+++ b/src/gui/console/ConsoleModel.h
@@ -0,0 +1,28 @@
+#ifndef CONSOLEMODEL_H_
+#define CONSOLEMODEL_H_
+
+#include <vector>
+#include <deque>
+#include "ConsoleView.h"
+#include "ConsoleCommand.h"
+
+class ConsoleView;
+class ConsoleModel {
+ int currentCommandIndex;
+ std::vector<ConsoleView*> observers;
+ std::deque<ConsoleCommand> previousCommands;
+ void notifyPreviousCommandsChanged();
+ void notifyCurrentCommandChanged();
+public:
+ int GetCurrentCommandIndex();
+ void SetCurrentCommandIndex(int index);
+ ConsoleCommand GetCurrentCommand();
+
+ std::deque<ConsoleCommand> GetPreviousCommands();
+ ConsoleModel();
+ void AddObserver(ConsoleView * observer);
+ void AddLastCommand(ConsoleCommand command);
+ virtual ~ConsoleModel();
+};
+
+#endif /* CONSOLEMODEL_H_ */
diff --git a/src/gui/console/ConsoleView.cpp b/src/gui/console/ConsoleView.cpp
new file mode 100644
index 0000000..cf398ae
--- /dev/null
+++ b/src/gui/console/ConsoleView.cpp
@@ -0,0 +1,102 @@
+#include "ConsoleView.h"
+#include "gui/interface/Keys.h"
+
+ConsoleView::ConsoleView():
+ ui::Window(ui::Point(0, 0), ui::Point(XRES+BARSIZE, 150)),
+ commandField(NULL)
+{
+ class CommandHighlighter: public ui::TextboxAction
+ {
+ ConsoleView * v;
+ public:
+ CommandHighlighter(ConsoleView * v_) { v = v_; }
+ virtual void TextChangedCallback(ui::Textbox * sender)
+ {
+ sender->SetDisplayText(v->c->FormatCommand(sender->GetText()));
+ }
+ };
+ commandField = new ui::Textbox(ui::Point(0, Size.Y-16), ui::Point(Size.X, 16), "");
+ commandField->Appearance.HorizontalAlign = ui::Appearance::AlignLeft;
+ commandField->Appearance.VerticalAlign = ui::Appearance::AlignMiddle;
+ commandField->SetActionCallback(new CommandHighlighter(this));
+ AddComponent(commandField);
+ FocusComponent(commandField);
+ commandField->SetBorder(false);
+}
+
+void ConsoleView::DoKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool alt)
+{
+ switch(key)
+ {
+ case KEY_ESCAPE:
+ case '`':
+ if (character != '~')
+ c->CloseConsole();
+ else
+ Window::DoKeyPress(key, character, shift, ctrl, alt);
+ break;
+ case KEY_RETURN:
+ case KEY_ENTER:
+ c->EvaluateCommand(commandField->GetText());
+ commandField->SetText("");
+ commandField->SetDisplayText("");
+ break;
+ case KEY_DOWN:
+ c->NextCommand();
+ break;
+ case KEY_UP:
+ c->PreviousCommand();
+ break;
+ default:
+ Window::DoKeyPress(key, character, shift, ctrl, alt);
+ break;
+ }
+}
+
+void ConsoleView::NotifyPreviousCommandsChanged(ConsoleModel * sender)
+{
+ for(int i = 0; i < commandList.size(); i++)
+ {
+ RemoveComponent(commandList[i]);
+ delete commandList[i];
+ }
+ commandList.clear();
+ std::deque<ConsoleCommand> commands = sender->GetPreviousCommands();
+ int currentY = Size.Y - 32;
+ if(commands.size())
+ for(int i = commands.size()-1; i >= 0; i--)
+ {
+ if(currentY <= 0)
+ break;
+ ui::Label * tempLabel = new ui::Label(ui::Point(Size.X/2, currentY), ui::Point(Size.X/2, 16), commands[i].ReturnValue);
+ tempLabel->Appearance.VerticalAlign = ui::Appearance::AlignMiddle;
+ tempLabel->Appearance.HorizontalAlign = ui::Appearance::AlignLeft;
+ commandList.push_back(tempLabel);
+ AddComponent(tempLabel);
+ tempLabel = new ui::Label(ui::Point(0, currentY), ui::Point(Size.X/2, 16), commands[i].Command);
+ tempLabel->Appearance.VerticalAlign = ui::Appearance::AlignMiddle;
+ tempLabel->Appearance.HorizontalAlign = ui::Appearance::AlignLeft;
+ commandList.push_back(tempLabel);
+ AddComponent(tempLabel);
+ currentY-=16;
+ }
+}
+
+void ConsoleView::NotifyCurrentCommandChanged(ConsoleModel * sender)
+{
+ commandField->SetText(sender->GetCurrentCommand().Command);
+ commandField->SetDisplayText(c->FormatCommand(commandField->GetText()));
+}
+
+
+void ConsoleView::OnDraw()
+{
+ Graphics * g = ui::Engine::Ref().g;
+ g->fillrect(Position.X, Position.Y, Size.X, Size.Y, 0, 0, 0, 110);
+ g->draw_line(Position.X, Position.Y+Size.Y-16, Position.X+Size.X, Position.Y+Size.Y-16, 255, 255, 255, 160);
+ g->draw_line(Position.X, Position.Y+Size.Y, Position.X+Size.X, Position.Y+Size.Y, 255, 255, 255, 200);
+}
+
+ConsoleView::~ConsoleView() {
+}
+
diff --git a/src/gui/console/ConsoleView.h b/src/gui/console/ConsoleView.h
new file mode 100644
index 0000000..22f172c
--- /dev/null
+++ b/src/gui/console/ConsoleView.h
@@ -0,0 +1,30 @@
+#ifndef CONSOLEVIEW_H_
+#define CONSOLEVIEW_H_
+
+#include <vector>
+#include <queue>
+#include "gui/interface/Label.h"
+#include "gui/interface/Window.h"
+#include "ConsoleController.h"
+#include "ConsoleModel.h"
+#include "gui/interface/Textbox.h"
+#include "ConsoleCommand.h"
+
+
+class ConsoleController;
+class ConsoleModel;
+class ConsoleView: public ui::Window {
+ ConsoleController * c;
+ ui::Textbox * commandField;
+ std::vector<ui::Label*> commandList;
+public:
+ ConsoleView();
+ virtual void OnDraw();
+ virtual void DoKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool alt);
+ void AttachController(ConsoleController * c_) { c = c_; }
+ void NotifyPreviousCommandsChanged(ConsoleModel * sender);
+ void NotifyCurrentCommandChanged(ConsoleModel * sender);
+ virtual ~ConsoleView();
+};
+
+#endif /* CONSOLEVIEW_H_ */