Subversion Repository Public Repository

Divide-Framework

This repository has no backups
This repository's network speed is throttled to 100KB/sec

Diff Revisions 167 vs 168 for /trunk/Source Code/GUI/GUIConsole.cpp

Diff revisions: vs.
  @@ -8,16 +8,32 @@
8 8 #include "Core/Headers/ParamHandler.h"
9 9 #include <boost/lockfree/queue.hpp>
10 10
11 - #ifndef CEGUI_DEFAULT_CONTEXT
12 - #define CEGUI_DEFAULT_CONTEXT CEGUI::System::getSingleton().getDefaultGUIContext()
13 - #endif
14 -
15 - struct MessageStruct{
16 - std::string _msg;
17 - bool _error;
11 + namespace {
12 + # ifndef CEGUI_DEFAULT_CONTEXT
13 + # define CEGUI_DEFAULT_CONTEXT CEGUI::System::getSingleton().getDefaultGUIContext()
14 + # endif
15 +
16 + class MessageStruct {
17 + public:
18 + MessageStruct(const char* msg, bool error) : _error(error)
19 + {
20 + _msg = _strdup(msg);
21 + }
22 + ~MessageStruct()
23 + {
24 + SAFE_DELETE(_msg);
25 + }
26 +
27 + const char* msg() const {return _msg;}
28 + bool error() const {return _error;}
29 + private:
30 + char* _msg;
31 + bool _error;
32 + };
33 +
34 + /// Used to queue output text to be displayed when '_init' becomes true
35 + static boost::lockfree::queue<MessageStruct*, boost::lockfree::capacity<256> > _outputBuffer;
18 36 };
19 - /// Used to queue output text to be displayed when '_init' becomes true
20 - static boost::lockfree::queue<MessageStruct*, boost::lockfree::capacity<256> > _outputBuffer;
21 37
22 38
23 39 GUIConsole::GUIConsole() : _consoleWindow(NULL),
  @@ -34,14 +50,16 @@
34 50 {
35 51 _init = false;
36 52 setVisible(false);
37 - if (_consoleWindow){
53 + if (_consoleWindow)
38 54 CEGUI_DEFAULT_CONTEXT.getRootWindow()->removeChild(_consoleWindow);
39 - }
55 +
40 56 SAFE_DELETE(_cmdParser);
41 57 }
42 58
43 59 void GUIConsole::CreateCEGUIWindow(){
44 - if(_init) ERROR_FN(Locale::get("ERROR_CONSOLE_DOUBLE_INIT"));
60 + if(_init)
61 + ERROR_FN(Locale::get("ERROR_CONSOLE_DOUBLE_INIT"));
62 +
45 63 // Get a local pointer to the CEGUI Window Manager, Purely for convenience to reduce typing
46 64 CEGUI::WindowManager *pWindowManager = CEGUI::WindowManager::getSingletonPtr();
47 65
  @@ -69,9 +87,11 @@
69 87 void GUIConsole::RegisterHandlers(){
70 88 assert(_editBox != NULL);
71 89 //We need to monitor text/command submission from the editBox
72 - _editBox->subscribeEvent(CEGUI::Editbox::EventTextAccepted,CEGUI::Event::Subscriber(&GUIConsole::Handle_TextSubmitted,this));
90 + _editBox->subscribeEvent(CEGUI::Editbox::EventTextAccepted,
91 + CEGUI::Event::Subscriber(&GUIConsole::Handle_TextSubmitted,this));
73 92 //we also monitor any key presses to make sure we do not accidentally hide the console in the middle of a sentence
74 - _editBox->subscribeEvent(CEGUI::Editbox::EventKeyUp,CEGUI::Event::Subscriber(&GUIConsole::Handle_TextInput,this));
93 + _editBox->subscribeEvent(CEGUI::Editbox::EventKeyUp,
94 + CEGUI::Event::Subscriber(&GUIConsole::Handle_TextInput,this));
75 95 }
76 96
77 97 bool GUIConsole::Handle_TextInput(const CEGUI::EventArgs &e){
  @@ -106,9 +126,9 @@
106 126 _editBox->setText("");
107 127 _inputHistory.push_back(_inputBuffer);
108 128 //Keep command history low
109 - if(_inputHistory.size() > _CEGUI_MAX_CONSOLE_ENTRIES){
129 + if(_inputHistory.size() > _CEGUI_MAX_CONSOLE_ENTRIES)
110 130 _inputHistory.pop_front();
111 - }
131 +
112 132 _inputHistoryIndex = _inputHistory.size()-1;
113 133 //reset the inputbuffer so we can handle console closing properly
114 134 _inputBuffer.clear();
  @@ -117,7 +137,9 @@
117 137
118 138 void GUIConsole::setVisible(bool visible){
119 139 //if it's not the first key (e.g., if the toggle key is "~", then "lorem~ipsum" should not close the Window)
120 - if(!_inputBuffer.empty()) return;
140 + if(!_inputBuffer.empty())
141 + return;
142 +
121 143 assert(_editBox != NULL);
122 144 _consoleWindow->setVisible(visible);
123 145
  @@ -135,29 +157,27 @@
135 157 return _consoleWindow->isVisible();
136 158 }
137 159
138 - void GUIConsole::printText(const std::string& output,bool error){
160 + void GUIConsole::printText(const char* output, bool error){
139 161 if(!_init || !Application::getInstance().isMainThread()){
140 162 //If the console Window isn't loaded, create an output buffer
141 - MessageStruct* msg = New MessageStruct;
142 - msg->_msg = output;
143 - msg->_error = error;
163 + MessageStruct* msg = New MessageStruct(output, error);
144 164 while(!_outputBuffer.push(msg));
145 165 return;
146 166 }
147 -
148 -
167 +
149 168 //print it all at once
150 169 MessageStruct* outMsg = NULL;
151 170 while(_outputBuffer.pop(outMsg)){
152 - OutputText(outMsg->_msg, outMsg->_error ? CEGUI::Colour(1.0f,0.0f,0.0f) : CEGUI::Colour(0.4f,0.4f,0.3f));
171 + OutputText(outMsg->msg(), outMsg->error());
153 172 SAFE_DELETE(outMsg);
154 173 }
155 174
156 175
157 - OutputText(output,error ? CEGUI::Colour(1.0f,0.0f,0.0f) : CEGUI::Colour(0.4f,0.4f,0.3f));
176 + OutputText(output, error);
158 177 }
159 178
160 - void GUIConsole::OutputText(const std::string& inMsg, CEGUI::Colour color){
179 + void GUIConsole::OutputText(const char* inMsg, const bool error){
180 + CEGUI::Colour color = error ? CEGUI::Colour(1.0f,0.0f,0.0f) : CEGUI::Colour(0.4f,0.4f,0.3f);
161 181 // Create a new List Box item that uses wordwrap. This will hold the output from the command
162 182 // Append the response with left wordwrap alignement
163 183 CEGUI::FormattedListboxTextItem *newItem = New CEGUI::FormattedListboxTextItem(inMsg,CEGUI::HTF_WORDWRAP_LEFT_ALIGNED);
  @@ -170,7 +190,6 @@
170 190 // Always make sure the last item is visible (auto-scroll)
171 191 _outputWindow->ensureItemIsVisible(newItem);
172 192 // Try to not overfill the listbox but make room for the new item
173 - if(_outputWindow->getItemCount() > _CEGUI_MAX_CONSOLE_ENTRIES) {
193 + if(_outputWindow->getItemCount() > _CEGUI_MAX_CONSOLE_ENTRIES)
174 194 _outputWindow->removeItem(_outputWindow->getListboxItemFromIndex(0));
175 - }
176 195 }