diff options
| author | Simon Robertshaw <simon@hardwired.org.uk> | 2012-02-01 18:45:59 (GMT) |
|---|---|---|
| committer | Simon Robertshaw <simon@hardwired.org.uk> | 2012-02-01 18:45:59 (GMT) |
| commit | 038da72c61ea6a251d805e2de3662f240da52b02 (patch) | |
| tree | 05170050b16f9d663ec44ae451211e686ba196ac /src/console | |
| parent | 857b0cc1fc58f066acd59404d16ee5e566e20f00 (diff) | |
| download | powder-038da72c61ea6a251d805e2de3662f240da52b02.zip powder-038da72c61ea6a251d805e2de3662f240da52b02.tar.gz | |
Console UI, open in browser button, tab and enter shortcut for Login UI, various
Diffstat (limited to 'src/console')
| -rw-r--r-- | src/console/ConsoleCommand.h | 25 | ||||
| -rw-r--r-- | src/console/ConsoleController.cpp | 102 | ||||
| -rw-r--r-- | src/console/ConsoleController.h | 35 | ||||
| -rw-r--r-- | src/console/ConsoleModel.cpp | 72 | ||||
| -rw-r--r-- | src/console/ConsoleModel.h | 35 | ||||
| -rw-r--r-- | src/console/ConsoleView.cpp | 90 | ||||
| -rw-r--r-- | src/console/ConsoleView.h | 37 |
7 files changed, 396 insertions, 0 deletions
diff --git a/src/console/ConsoleCommand.h b/src/console/ConsoleCommand.h new file mode 100644 index 0000000..f6f91a3 --- /dev/null +++ b/src/console/ConsoleCommand.h @@ -0,0 +1,25 @@ +/* + * ConsoleCommand.h + * + * Created on: Feb 1, 2012 + * Author: Simon + */ + +#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; +}; + + +#endif /* CONSOLECOMMAND_H_ */ diff --git a/src/console/ConsoleController.cpp b/src/console/ConsoleController.cpp new file mode 100644 index 0000000..cf4cdcd --- /dev/null +++ b/src/console/ConsoleController.cpp @@ -0,0 +1,102 @@ +/* + * ConsoleController.cpp + * + * Created on: Jan 31, 2012 + * Author: Simon + */ + +#include <stack> +#include "ConsoleController.h" + +ConsoleController::ConsoleController(ControllerCallback * callback): + HasDone(false) +{ + consoleModel = new ConsoleModel(); + consoleView = new ConsoleView(); + consoleView->AttachController(this); + consoleModel->AddObserver(consoleView); + + this->callback = callback; +} + +void ConsoleController::EvaluateCommand(std::string command) +{ + if(command.length()) + consoleModel->AddLastCommand(ConsoleCommand(command, -1, "Syntax error")); + else + if(ui::Engine::Ref().GetWindow() == consoleView) + ui::Engine::Ref().CloseWindow(); +} + +std::string ConsoleController::FormatCommand(std::string command) +{ + char * rawText = (char*)command.c_str(); + char * outputData = (char *)calloc(command.length()*6, 1); + int rawTextLoc = 0; + int outputDataLoc = 0; + std::stack<char> pstack; + while(rawText[rawTextLoc]) + { + switch(rawText[rawTextLoc]) + { + case '\\': + outputData[outputDataLoc++] = rawText[rawTextLoc++]; + if(rawText[rawTextLoc]) + outputData[outputDataLoc++] = rawText[rawTextLoc++]; + break; + case '"': + if(pstack.size() && pstack.top() == '"') + { + pstack.pop(); + outputData[outputDataLoc++] = rawText[rawTextLoc++]; + outputData[outputDataLoc++] = '\b'; + outputData[outputDataLoc++] = 'w'; + } + else + { + pstack.push('"'); + outputData[outputDataLoc++] = '\b'; + outputData[outputDataLoc++] = 'o'; + outputData[outputDataLoc++] = rawText[rawTextLoc++]; + } + break; + default: + outputData[outputDataLoc++] = rawText[rawTextLoc++]; + break; + } + } + return outputData; +} + +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() { + // TODO Auto-generated destructor stub +} + diff --git a/src/console/ConsoleController.h b/src/console/ConsoleController.h new file mode 100644 index 0000000..9316f71 --- /dev/null +++ b/src/console/ConsoleController.h @@ -0,0 +1,35 @@ +/* + * ConsoleController.h + * + * Created on: Jan 31, 2012 + * Author: Simon + */ + +#ifndef CONSOLECONTROLLER_H_ +#define CONSOLECONTROLLER_H_ + +#include <string> +#include "Controller.h" +#include "ConsoleView.h" +#include "ConsoleModel.h" +#include "ConsoleCommand.h" + +class ConsoleModel; +class ConsoleView; +class ConsoleController { + ControllerCallback * callback; + ConsoleView * consoleView; + ConsoleModel * consoleModel; +public: + bool HasDone; + ConsoleController(ControllerCallback * callback); + std::string FormatCommand(std::string command); + void EvaluateCommand(std::string command); + void NextCommand(); + void PreviousCommand(); + void Exit(); + ConsoleView * GetView(); + virtual ~ConsoleController(); +}; + +#endif /* CONSOLECONTROLLER_H_ */ diff --git a/src/console/ConsoleModel.cpp b/src/console/ConsoleModel.cpp new file mode 100644 index 0000000..fcbee80 --- /dev/null +++ b/src/console/ConsoleModel.cpp @@ -0,0 +1,72 @@ +/* + * ConsoleModel.cpp + * + * Created on: Feb 1, 2012 + * Author: Simon + */ + +#include "ConsoleModel.h" + +ConsoleModel::ConsoleModel() { + +} + +void ConsoleModel::AddObserver(ConsoleView * observer) +{ + observers.push_back(observer); +} + +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() { + +} + diff --git a/src/console/ConsoleModel.h b/src/console/ConsoleModel.h new file mode 100644 index 0000000..b340ea8 --- /dev/null +++ b/src/console/ConsoleModel.h @@ -0,0 +1,35 @@ +/* + * ConsoleModel.h + * + * Created on: Feb 1, 2012 + * Author: Simon + */ + +#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/console/ConsoleView.cpp b/src/console/ConsoleView.cpp new file mode 100644 index 0000000..117d8cb --- /dev/null +++ b/src/console/ConsoleView.cpp @@ -0,0 +1,90 @@ +/* + * ConsoleView.cpp + * + * Created on: Jan 31, 2012 + * Author: Simon + */ + +#include "ConsoleView.h" +#include "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_; } + 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->SetAlignment(AlignLeft, AlignBottom); + commandField->SetActionCallback(new CommandHighlighter(this)); + AddComponent(commandField); + FocusComponent(commandField); + commandField->SetBorder(false); +} + +void ConsoleView::OnKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool alt) +{ + switch(key) + { + case KEY_RETURN: + case KEY_ENTER: + c->EvaluateCommand(commandField->GetText()); + commandField->SetText(""); + break; + case KEY_DOWN: + c->NextCommand(); + break; + case KEY_UP: + c->PreviousCommand(); + 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(0, currentY), ui::Point(Size.X, 16), commands[i].Command); + tempLabel->SetAlignment(AlignLeft, AlignMiddle); + commandList.push_back(tempLabel); + AddComponent(tempLabel); + currentY-=16; + } +} + +void ConsoleView::NotifyCurrentCommandChanged(ConsoleModel * sender) +{ + commandField->SetText(sender->GetCurrentCommand().Command); +} + + +void ConsoleView::OnDraw() +{ + Graphics * g = ui::Engine::Ref().g; + g->fillrect(Position.X, Position.Y, Size.X, Size.Y, 0, 0, 0, 110); + g->blend_line(Position.X, Position.Y+Size.Y-16, Position.X+Size.X, Position.Y+Size.Y-16, 255, 255, 255, 160); + g->blend_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/console/ConsoleView.h b/src/console/ConsoleView.h new file mode 100644 index 0000000..bb1a98e --- /dev/null +++ b/src/console/ConsoleView.h @@ -0,0 +1,37 @@ +/* + * ConsoleView.h + * + * Created on: Jan 31, 2012 + * Author: Simon + */ + +#ifndef CONSOLEVIEW_H_ +#define CONSOLEVIEW_H_ + +#include <vector> +#include <queue> +#include "interface/Label.h" +#include "interface/Window.h" +#include "ConsoleController.h" +#include "ConsoleModel.h" +#include "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 OnKeyPress(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_ */ |
