PolycodeConsole.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. /*
  2. Copyright (C) 2012 by Ivan Safrin
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in
  10. all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  17. THE SOFTWARE.
  18. */
  19. #include "PolycodeConsole.h"
  20. #include "PolycodeRemoteDebugger.h"
  21. #include "PolycodeTextEditor.h"
  22. #include "PolycodeFrame.h"
  23. PolycodeConsole* PolycodeConsole::instance = NULL;
  24. extern SyntaxHighlightTheme *globalSyntaxTheme;
  25. extern PolycodeFrame *globalFrame;
  26. BackTraceEntry::BackTraceEntry(String fileName, int lineNumber, PolycodeProject *project) : UIElement() {
  27. this->project = project;
  28. this->fileName = fileName;
  29. this->lineNumber = lineNumber;
  30. Config *conf = CoreServices::getInstance()->getConfig();
  31. String fontName = conf->getStringValue("Polycode", "uiDefaultFontName");
  32. int fontSize = conf->getNumericValue("Polycode", "uiDefaultFontSize");
  33. labelBg = new UIRect(20,20);
  34. labelBg->setAnchorPoint(-1.0, -1.0, 0.0);
  35. labelBg->setColor(0.0, 0.0, 0.0, 0.15);
  36. labelBg->processInputEvents = true;
  37. addChild(labelBg);
  38. labelBg->addEventListener(this, InputEvent::EVENT_MOUSEDOWN);
  39. label = new UILabel(fileName+" on line "+String::IntToString(lineNumber), fontSize, fontName);
  40. addChild(label);
  41. label->setPosition(5,2);
  42. }
  43. void BackTraceEntry::handleEvent(Event *event) {
  44. if(event->getDispatcher() == labelBg) {
  45. if(event->getEventCode() == InputEvent::EVENT_MOUSEDOWN) {
  46. Select();
  47. }
  48. }
  49. }
  50. void BackTraceEntry::Select() {
  51. BackTraceEvent *event = new BackTraceEvent();
  52. event->fileName = fileName;
  53. event->lineNumber = lineNumber;
  54. event->project = project;
  55. dispatchEvent(event, BackTraceEvent::EVENT_BACKTRACE_SELECTED);
  56. labelBg->setColor(0.0, 0.0, 1.0, 0.35);
  57. }
  58. void BackTraceEntry::Deselect() {
  59. labelBg->setColor(0.0, 0.0, 0.0, 0.15);
  60. }
  61. BackTraceEntry::~BackTraceEntry() {
  62. delete label;
  63. delete labelBg;
  64. }
  65. void BackTraceEntry::Resize(Number width, Number height) {
  66. labelBg->Resize(width, 20);
  67. }
  68. BackTraceWindow::BackTraceWindow() : UIElement() {
  69. Config *conf = CoreServices::getInstance()->getConfig();
  70. String fontName = conf->getStringValue("Polycode", "uiDefaultFontName");
  71. labelBg = new UIRect(20,30);
  72. labelBg->setAnchorPoint(-1.0, -1.0, 0.0);
  73. labelBg->color.setColorHexFromString(CoreServices::getInstance()->getConfig()->getStringValue("Polycode", "uiHeaderBgColor"));
  74. addChild(labelBg);
  75. UILabel *label = new UILabel("CRASH STACK", 18, "section");
  76. label->color.setColorHexFromString(CoreServices::getInstance()->getConfig()->getStringValue("Polycode", "uiHeaderFontColor"));
  77. addChild(label);
  78. label->setPosition(5,3);
  79. }
  80. void BackTraceWindow::Resize(Number width, Number height) {
  81. labelBg->Resize(width, 30);
  82. setWidth(width);
  83. setHeight(height);
  84. adjustEntries();
  85. }
  86. void BackTraceWindow::adjustEntries() {
  87. for(int i=0; i < entries.size(); i++) {
  88. entries[i]->Resize(getWidth(), 20);
  89. entries[i]->setPosition(0, 30 + (i * 21));
  90. }
  91. }
  92. void BackTraceWindow::handleEvent(Event *event) {
  93. for(int i=0; i < entries.size(); i++) {
  94. if(event->getDispatcher() == entries[i]) {
  95. if(event->getEventCode() == BackTraceEvent::EVENT_BACKTRACE_SELECTED && event->getEventType() == "BackTraceEvent") {
  96. for(int j=0; j < entries.size(); j++) {
  97. entries[j]->Deselect();
  98. }
  99. BackTraceEvent *btEvent = (BackTraceEvent*) event;
  100. BackTraceEvent *_event = new BackTraceEvent();
  101. _event->fileName = btEvent->fileName;
  102. _event->lineNumber = btEvent->lineNumber;
  103. _event->project = btEvent->project;
  104. dispatchEvent(_event, BackTraceEvent::EVENT_BACKTRACE_SELECTED);
  105. }
  106. }
  107. }
  108. }
  109. void BackTraceWindow::addBackTrace(String fileName, int lineNumber, PolycodeProject *project) {
  110. BackTraceEntry *entry = new BackTraceEntry(fileName, lineNumber, project);
  111. entry->addEventListener(this, BackTraceEvent::EVENT_BACKTRACE_SELECTED);
  112. entries.push_back(entry);
  113. addChild(entry);
  114. adjustEntries();
  115. if(entries.size() == 1) {
  116. entry->Select();
  117. }
  118. }
  119. void BackTraceWindow::clearBackTraces() {
  120. for(int i=0; i < entries.size(); i++) {
  121. removeChild(entries[i]);
  122. entries[i]->removeAllHandlers();
  123. delete entries[i];
  124. }
  125. entries.clear();
  126. adjustEntries();
  127. }
  128. BackTraceWindow::~BackTraceWindow() {
  129. }
  130. ConsoleWindow::ConsoleWindow() : UIElement() {
  131. labelBg = new UIRect(20,30);
  132. labelBg->setAnchorPoint(-1.0, -1.0, 0.0);
  133. labelBg->color.setColorHexFromString(CoreServices::getInstance()->getConfig()->getStringValue("Polycode", "uiHeaderBgColor"));
  134. addChild(labelBg);
  135. UILabel *label = new UILabel("CONSOLE", 18, "section");
  136. label->color.setColorHexFromString(CoreServices::getInstance()->getConfig()->getStringValue("Polycode", "uiHeaderFontColor"));
  137. addChild(label);
  138. label->setPosition(35,3);
  139. debugTextInput = new UITextInput(true, 100, 100);
  140. consoleTextInput = new UITextInput(false, 100, 100);
  141. addChild(consoleTextInput);
  142. addChild(debugTextInput);
  143. clearButton = new UIImageButton("main/clear_buffer_icon.png", 1.0, 16, 16);
  144. addChild(clearButton);
  145. hideConsoleButton = new UIImageButton("main/console_hide_button.png", 1.0, 20, 20);
  146. addChild(hideConsoleButton);
  147. hideConsoleButton->setPosition(7,5);
  148. }
  149. void ConsoleWindow::Resize(Number width, Number height) {
  150. labelBg->Resize(width, 30);
  151. debugTextInput->Resize(width, height-25-30);
  152. debugTextInput->setPosition(0, 30);
  153. clearButton->setPosition(width - 22, 6);
  154. consoleTextInput->Resize(width, 25);
  155. consoleTextInput->setPosition(0, height-25);
  156. }
  157. PolycodeConsole::PolycodeConsole() : UIElement() {
  158. backtraceSizer = new UIHSizer(100,100,300,false);
  159. addChild(backtraceSizer);
  160. debugger = NULL;
  161. consoleWindow = new ConsoleWindow();
  162. backtraceSizer->addLeftChild(consoleWindow);
  163. backtraceWindow = new BackTraceWindow();
  164. backtraceSizer->addRightChild(backtraceWindow);
  165. debugTextInput = consoleWindow->debugTextInput;
  166. consoleTextInput = consoleWindow->consoleTextInput;
  167. consoleTextInput->addEventListener(this, Event::COMPLETE_EVENT);
  168. CoreServices::getInstance()->getCore()->getInput()->addEventListener(this, InputEvent::EVENT_KEYDOWN);
  169. consoleTextInput->setColor(0.95, 1.0, 0.647, 1.0);
  170. consoleHistoryPosition = 0;
  171. consoleHistoryMaxSize = 15;
  172. consoleWindow->clearButton->addEventListener(this, UIEvent::CLICK_EVENT);
  173. consoleWindow->hideConsoleButton->addEventListener(this, UIEvent::CLICK_EVENT);
  174. CoreServices::getInstance()->getLogger()->addEventListener(this, Event::NOTIFY_EVENT);
  175. PolycodeConsole::setInstance(this);
  176. }
  177. PolycodeConsole::~PolycodeConsole() {
  178. }
  179. void PolycodeConsole::applyTheme() {
  180. debugTextInput->setBackgroundColor(globalSyntaxTheme->bgColor);
  181. debugTextInput->setCursorColor(globalSyntaxTheme->cursorColor);
  182. debugTextInput->setSelectionColor(globalSyntaxTheme->selectionColor);
  183. debugTextInput->useStrongHinting = globalSyntaxTheme->useStrongHinting;
  184. debugTextInput->setTextColor(globalSyntaxTheme->colors[0]);
  185. }
  186. void PolycodeConsole::setDebugger(PolycodeRemoteDebugger *debugger) {
  187. this->debugger = debugger;
  188. }
  189. void PolycodeConsole::handleEvent(Event *event) {
  190. if(event->getDispatcher() == CoreServices::getInstance()->getLogger()) {
  191. if(event->getEventCode() == Event::NOTIFY_EVENT) {
  192. LoggerEvent *loggerEvent = (LoggerEvent*)event;
  193. _print(loggerEvent->message);
  194. }
  195. } else if(event->getDispatcher() == consoleWindow->clearButton) {
  196. if(event->getEventType() == "UIEvent" && event->getEventCode() == UIEvent::CLICK_EVENT) {
  197. debugTextInput->setText("");
  198. }
  199. } else if(event->getDispatcher() == consoleWindow->hideConsoleButton) {
  200. globalFrame->hideConsole();
  201. }
  202. if(event->getDispatcher() == consoleTextInput) {
  203. if(event->getEventCode() == Event::COMPLETE_EVENT && event->getEventType() == "" && consoleTextInput->getText() != "") {
  204. _print(">"+consoleTextInput->getText()+"\n");
  205. if(debugger) {
  206. if(!debugger->isConnected()) {
  207. _print("Unable to inject code. No debugger clients connected.\n");
  208. } else {
  209. debugger->injectCode(consoleTextInput->getText());
  210. }
  211. }
  212. consoleHistory.push_back(consoleTextInput->getText());
  213. if (consoleHistory.size() > consoleHistoryMaxSize) { consoleHistory.erase(consoleHistory.begin()); }
  214. consoleHistoryPosition = consoleHistory.size();
  215. consoleTextInput->setText("");
  216. }
  217. }
  218. if (event->getDispatcher() == CoreServices::getInstance()->getCore()->getInput()) {
  219. if (consoleTextInput->hasFocus && event->getEventCode() == InputEvent::EVENT_KEYDOWN) {
  220. InputEvent *inputEvent = (InputEvent*)event;
  221. if (inputEvent->keyCode() == KEY_UP) {
  222. consoleHistoryPosition--;
  223. if (consoleHistoryPosition >= 0) {
  224. consoleTextInput->setText(consoleHistory.at(consoleHistoryPosition));
  225. } else {
  226. consoleHistoryPosition = -1;
  227. consoleTextInput->setText("");
  228. }
  229. }
  230. if (inputEvent->keyCode() == KEY_DOWN) {
  231. consoleHistoryPosition++;
  232. if (consoleHistoryPosition < consoleHistory.size()) {
  233. consoleTextInput->setText(consoleHistory.at(consoleHistoryPosition));
  234. } else {
  235. consoleHistoryPosition = consoleHistory.size();
  236. consoleTextInput->setText("");
  237. }
  238. }
  239. }
  240. }
  241. }
  242. void PolycodeConsole::setInstance(PolycodeConsole *newInstance) {
  243. instance = newInstance;
  244. }
  245. void PolycodeConsole::print(String msg) {
  246. instance->_print(msg);
  247. }
  248. void PolycodeConsole::addBacktrace(String fileName, int lineNumber, PolycodeProject *project) {
  249. instance->_addBacktrace(fileName, lineNumber, project);
  250. }
  251. void PolycodeConsole::_addBacktrace(String fileName, int lineNumber, PolycodeProject *project) {
  252. backtraceWindow->addBackTrace(fileName, lineNumber, project);
  253. }
  254. void PolycodeConsole::clearBacktraces() {
  255. instance->_clearBacktraces();
  256. }
  257. void PolycodeConsole::_clearBacktraces() {
  258. backtraceWindow->clearBackTraces();
  259. }
  260. void PolycodeConsole::_print(String msg) {
  261. debugTextInput->setText(debugTextInput->getText()+msg);
  262. debugTextInput->getScrollContainer()->setScrollValue(0, 1.0);
  263. printf("%s", msg.c_str());
  264. }
  265. void PolycodeConsole::Resize(Number width, Number height) {
  266. setWidth(width);
  267. setHeight(height);
  268. backtraceSizer->Resize(width, height);
  269. backtraceSizer->setPosition(0, 0);
  270. }