Page Menu
Home
Phabricator (Chris)
Search
Configure Global Search
Log In
Files
F125846
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Flag For Later
Award Token
Authored By
Unknown
Size
7 KB
Referenced Files
None
Subscribers
None
View Options
diff --git a/util/console.cpp b/util/console.cpp
new file mode 100644
index 00000000..0a54706f
--- /dev/null
+++ b/util/console.cpp
@@ -0,0 +1,202 @@
+#include "console.h"
+#include "util/bitmap.h"
+#include "util/trans-bitmap.h"
+#include "util/font.h"
+#include "util/funcs.h"
+#include "util/file-system.h"
+#include "exceptions/exception.h"
+#include "util/input/input-manager.h"
+#include "util/input/input-map.h"
+#include "globals.h"
+#include "util/debug.h"
+#include <string>
+#include <sstream>
+#include <string.h>
+
+using namespace std;
+
+namespace Console{
+
+ConsoleEnd Console::endl;
+
+static void doProcess(void * self){
+ Console * console = (Console*) self;
+ console->activate();
+}
+
+static void doToggle(void * self){
+ Console * console = (Console*) self;
+ console->toggle();
+}
+
+Console::Console(const int maxHeight, const Filesystem::RelativePath & font):
+state(Closed),
+maxHeight(maxHeight),
+height(0),
+font(font),
+textHeight(15),
+textWidth(15),
+offset(0){
+ textInput.addBlockingHandle(Keyboard::Key_TILDE, doToggle, this);
+ textInput.addBlockingHandle(Keyboard::Key_ENTER, doProcess, this);
+}
+
+Console::~Console(){
+ textInput.disable();
+ // InputManager::releaseInput(input);
+ for (map<string, Command*>::iterator it = commands.begin(); it != commands.end(); it++){
+ delete (*it).second;
+ }
+}
+
+void Console::addCommand(const std::string & name, Command * command){
+ if (commands[name] != 0){ Global::debug(0) << "Warning: duplicate console command for '" << name << "'" << std::endl;
+ delete commands[name];
+ }
+ commands[name] = command;
+}
+
+void Console::act(){
+ double speed = 10.0;
+ switch (state){
+ case Closed : {
+ break;
+ }
+ case Open : {
+ break;
+ }
+ case Closing : {
+ int distance = height;
+ height -= (int)((double)distance / speed + 1.0);
+ if (height <= 0){
+ height = 0;
+ state = Closed;
+ }
+ break;
+ }
+ case Opening : {
+ int distance = maxHeight - height;
+ height += (int)((double)distance / speed + 1.0);
+ if (height >= maxHeight){
+ height = maxHeight;
+ state = Open;
+ }
+ break;
+ }
+ }
+ checkStream();
+}
+
+/*
+static bool isChar(char c){
+ const char * letters = "abcdefghijklmnopqrstuvwxyz ,.";
+ return strchr(letters, c) != NULL;
+}
+*/
+
+void Console::activate(){
+ if (textInput.getText() != ""){
+ process(textInput.getText());
+ }
+ textInput.clearInput();
+}
+
+/* console input */
+bool Console::doInput() {
+ if (state != Open){
+ return false;
+ }
+
+ textInput.doInput();
+
+ return true;
+}
+
+void Console::draw(const Graphics::Bitmap & work){
+ /* if we can show something */
+ if (height > 0){
+ // Graphics::Bitmap::drawingMode(Bitmap::MODE_TRANS);
+ Graphics::Bitmap::transBlender(0, 0, 0, 160);
+ work.translucent().rectangleFill(0, 0, work.getWidth(), height, Graphics::makeColor(200,0,0));
+ work.translucent().horizontalLine(0, height, work.getWidth(), Graphics::makeColor(200, 200, 200));
+ const Font & font = Font::getFont(getFont(), textWidth, textHeight);
+ //font.printf(0, height - font.getHeight(), Bitmap::makeColor(255, 255, 255), work, "Console!", 0 );
+ // Bitmap::drawingMode(Bitmap::MODE_SOLID);
+ // if (state == Open){
+ if (!lines.empty()){
+ int start = height - font.getHeight() * 2;
+ for (std::vector<std::string>::reverse_iterator i = lines.rbegin(); i != lines.rend() && start > 0; ++i){
+ std::string str = *i;
+ font.printf(0, start, Graphics::makeColor(255,255,255), work, str, 0);
+ start -= font.getHeight();
+ }
+ }
+ font.printf(0, height - font.getHeight(), Graphics::makeColor(255,255,255), work, "> " + textInput.getText() + "|", 0);
+ // }
+ }
+}
+
+void Console::toggle(){
+ switch (state){
+ case Open:
+ case Opening: {
+ state = Closing;
+ textInput.disable();
+ break;
+ }
+ case Closed:
+ case Closing: {
+ state = Opening;
+ textInput.enable();
+ break;
+ }
+ }
+}
+
+static vector<string> split(string str, char splitter){
+ vector<string> strings;
+ size_t next = str.find(splitter);
+ while (next != string::npos){
+ strings.push_back(str.substr(0, next));
+ str = str.substr(next+1);
+ next = str.find(splitter);
+ }
+ if (str != ""){
+ strings.push_back(str);
+ }
+
+ return strings;
+}
+
+/* do something with a command */
+void Console::process(const string & command){
+ if (commands[command] != 0){
+ string out = commands[command]->act();
+ vector<string> each = split(out, '\n');
+ for (vector<string>::iterator it = each.begin(); it != each.end(); it++){
+ lines.push_back(*it);
+ }
+ } else {
+ lines.push_back("Unknown command '" + command + "'");
+ }
+}
+
+Console & Console::operator<<(const ConsoleEnd & e){
+ // checkStream();
+ return *this;
+}
+
+void Console::checkStream(){
+ /*
+ if (!textInput.str().empty()){
+ lines.push_back(textInput.str());
+ textInput.str("");
+ }
+ */
+}
+
+void Console::clear(){
+ lines.clear();
+}
+
+}
diff --git a/util/console.h b/util/console.h
new file mode 100644
index 00000000..428b7b7d
--- /dev/null
+++ b/util/console.h
@@ -0,0 +1,113 @@
+#ifndef _paintown_console_h
+#define _paintown_console_h
+
+#include <string>
+#include <vector>
+#include <sstream>
+#include <map>
+#include "util/input/input-map.h"
+#include "util/input/text-input.h"
+#include "util/file-system.h"
+
+namespace Graphics{
+class Bitmap;
+}
+
+namespace Console{
+
+/* equivalent of std::endl */
+class ConsoleEnd{
+private:
+ friend class Console;
+ ConsoleEnd(){
+ }
+};
+
+class Command{
+public:
+ Command(){
+ }
+
+ virtual std::string act() = 0;
+
+ virtual ~Command(){
+ }
+};
+
+/* starts disabled */
+class Console{
+public:
+ Console(const int maxHeight, const Filesystem::RelativePath & font = Global::DEFAULT_FONT);
+ virtual ~Console();
+
+ /* handle input and whatnot */
+ virtual void act();
+ virtual void draw(const Graphics::Bitmap & work);
+ /* enable / disable */
+ virtual void toggle();
+
+ /* removes all output from the console */
+ virtual void clear();
+
+ virtual bool doInput();
+
+ inline int getTextHeight(){ return textHeight; };
+ inline int getTextWidth(){ return textWidth; };
+
+ inline void setTextHeight(int h){ textHeight = h; };
+ inline void setTextWidth(int w){ textWidth = w; };
+
+ inline const Filesystem::RelativePath & getFont() const {
+ return font;
+ }
+
+ /* for arbitrary data */
+ template<typename T> Console & operator<<(const T & x){
+ // textInput << x;
+ return *this;
+ }
+
+ void addCommand(const std::string & name, Command * command);
+
+ /* for end of line, always pass Console::endl */
+ Console & operator<<(const ConsoleEnd & e);
+
+ static ConsoleEnd endl;
+
+ void activate();
+
+protected:
+
+ void process(const std::string & command);
+
+ enum State{
+ Closed,
+ Open,
+ Opening,
+ Closing,
+ } state;
+
+ const int maxHeight;
+ int height;
+
+ Filesystem::RelativePath font;
+
+ // Text height
+ int textHeight;
+ // Text width
+ int textWidth;
+ std::vector<std::string> lines;
+ // Our text inputer
+ // std::stringstream textInput;
+ // std::stringstream currentCommand;
+ unsigned int offset;
+ // InputMap<char> input;
+ TextInput textInput;
+ std::map<std::string, Command*> commands;
+
+ void checkStream();
+};
+
+}
+
+#endif
File Metadata
Details
Attached
Mime Type
text/x-diff
Expires
Thu, Jun 11, 9:54 AM (3 w, 5 d ago)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
68222
Default Alt Text
(7 KB)
Attached To
Mode
R75 R-Tech1
Attached
Detach File
Event Timeline