guiMessageVectorCtrl.cc 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "gui/guiMessageVectorCtrl.h"
  23. #include "gui/messageVector.h"
  24. #include "graphics/dgl.h"
  25. #include "console/consoleTypes.h"
  26. #include "gui/containers/guiScrollCtrl.h"
  27. IMPLEMENT_CONOBJECT(GuiMessageVectorCtrl);
  28. //-------------------------------------- Console functions
  29. ConsoleMethod(GuiMessageVectorCtrl, attach, bool, 3, 3, "( aVector ) Make this gui control display messages from the specified MessageVector.\n"
  30. "@param aVector A previously created messageVector instance.\n"
  31. "@return No return value")
  32. {
  33. MessageVector* pMV = NULL;
  34. Sim::findObject(argv[2], pMV);
  35. if (pMV == NULL) {
  36. Con::errorf(ConsoleLogEntry::General, "Could not find MessageVector: %s", argv[2]);
  37. return false;
  38. }
  39. return object->attach(pMV);
  40. }
  41. ConsoleMethod(GuiMessageVectorCtrl, detach, void, 2, 2, "() Stop listening to messages from the MessageVector this control was previously attached to.\n"
  42. "@return No return value")
  43. {
  44. if (object->isAttached() == false) {
  45. Con::warnf(ConsoleLogEntry::General, "GuiMessageVectorCtrl: double detach");
  46. return;
  47. }
  48. object->detach();
  49. }
  50. struct TempLineBreak
  51. {
  52. S32 start;
  53. S32 end;
  54. };
  55. //--------------------------------------------------------------------------
  56. // Callback for messageVector
  57. void sMVCtrlCallback(void * spectatorKey,
  58. const MessageVector::MessageCode code,
  59. const U32 argument)
  60. {
  61. GuiMessageVectorCtrl* pMVC = reinterpret_cast<GuiMessageVectorCtrl*>(spectatorKey);
  62. pMVC->callbackRouter(code, argument);
  63. }
  64. //--------------------------------------------------------------------------
  65. GuiMessageVectorCtrl::GuiMessageVectorCtrl()
  66. {
  67. VECTOR_SET_ASSOCIATION(mLineWrappings);
  68. VECTOR_SET_ASSOCIATION(mSpecialMarkers);
  69. VECTOR_SET_ASSOCIATION(mLineElements);
  70. mMessageVector = NULL;
  71. mLineSpacingPixels = 0;
  72. mLineContinuationIndent = 10;
  73. mMouseDown = false;
  74. mMouseSpecialLine = -1;
  75. mMouseSpecialRef = -1;
  76. for (U32 i = 0; i < 16; i++)
  77. mAllowedMatches[i] = "";
  78. mSpecialColor.set(0, 0, 255);
  79. mMaxColorIndex = 9;
  80. mRendersChildren = false;
  81. mIsContainer = false;
  82. }
  83. //--------------------------------------------------------------------------
  84. GuiMessageVectorCtrl::~GuiMessageVectorCtrl()
  85. {
  86. AssertFatal(mLineWrappings.size() == 0, "Error, line wrappings not properly cleared!");
  87. AssertFatal(mSpecialMarkers.size() == 0, "Error, special markers not properly cleared!");
  88. AssertFatal(mLineElements.size() == 0, "Error, line elements not properly cleared!");
  89. }
  90. //--------------------------------------------------------------------------
  91. void GuiMessageVectorCtrl::initPersistFields()
  92. {
  93. Parent::initPersistFields();
  94. addField("lineSpacing", TypeS32, Offset(mLineSpacingPixels, GuiMessageVectorCtrl));
  95. addField("lineContinuedIndex", TypeS32, Offset(mLineContinuationIndent, GuiMessageVectorCtrl));
  96. addField("allowedMatches", TypeString, Offset(mAllowedMatches, GuiMessageVectorCtrl), 16);
  97. addField("matchColor", TypeColorI, Offset(mSpecialColor, GuiMessageVectorCtrl));
  98. addField("maxColorIndex", TypeS32, Offset(mMaxColorIndex, GuiMessageVectorCtrl));
  99. }
  100. bool GuiMessageVectorCtrl::onAdd()
  101. {
  102. return Parent::onAdd();
  103. }
  104. void GuiMessageVectorCtrl::onRemove()
  105. {
  106. Parent::onRemove();
  107. }
  108. //--------------------------------------------------------------------------
  109. bool GuiMessageVectorCtrl::isAttached() const
  110. {
  111. return (mMessageVector != NULL);
  112. }
  113. //--------------------------------------------------------------------------
  114. bool GuiMessageVectorCtrl::attach(MessageVector* newAttachment)
  115. {
  116. AssertFatal(newAttachment, "No attachment!");
  117. if (newAttachment == NULL || !isAwake())
  118. return false;
  119. if (isAttached()) {
  120. Con::warnf(ConsoleLogEntry::General, "GuiMessageVectorCtrl::attach: overriding attachment");
  121. detach();
  122. }
  123. AssertFatal(mLineWrappings.size() == 0, "Error, line wrappings not properly cleared!");
  124. mMessageVector = newAttachment;
  125. mMessageVector->registerSpectator(sMVCtrlCallback, this);
  126. return true;
  127. }
  128. //--------------------------------------------------------------------------
  129. void GuiMessageVectorCtrl::detach()
  130. {
  131. if (isAttached() == false) {
  132. Con::warnf(ConsoleLogEntry::General, "GuiMessageVectorCtrl::detach: not attached!");
  133. return;
  134. }
  135. mMessageVector->unregisterSpectator(this);
  136. mMessageVector = NULL;
  137. AssertFatal(mLineWrappings.size() == 0, "Error, line wrappings not properly cleared!");
  138. }
  139. //--------------------------------------------------------------------------
  140. void GuiMessageVectorCtrl::lineInserted(const U32 arg)
  141. {
  142. AssertFatal(mMessageVector != NULL, "Should not be here unless we're attached!");
  143. GuiScrollCtrl* pScroll = dynamic_cast<GuiScrollCtrl*>(getParent());
  144. bool fullyScrolled = pScroll->isScrolledToBottom();
  145. mSpecialMarkers.insert(arg);
  146. createSpecialMarkers(mSpecialMarkers[arg], mMessageVector->getLine(arg).message);
  147. mLineWrappings.insert(arg);
  148. createLineWrapping(mLineWrappings[arg], mMessageVector->getLine(arg).message);
  149. mLineElements.insert(arg);
  150. createLineElement(mLineElements[arg], mLineWrappings[arg], mSpecialMarkers[arg]);
  151. U32 numLines = 0;
  152. for (U32 i = 0; i < (U32)mLineWrappings.size(); i++) {
  153. // We need to rebuild the physicalLineStart markers at the same time as
  154. // we find out how many of them are left...
  155. mLineElements[i].physicalLineStart = numLines;
  156. numLines += mLineWrappings[i].numLines;
  157. }
  158. U32 newHeight = (mProfile->getFont(mFontSizeAdjust)->getHeight() + mLineSpacingPixels) * getMax(numLines, U32(1));
  159. resize(mBounds.point, Point2I(mBounds.extent.x, newHeight));
  160. if(fullyScrolled)
  161. pScroll->scrollTo(0, 0x7FFFFFFF);
  162. }
  163. void GuiMessageVectorCtrl::lineDeleted(const U32 arg)
  164. {
  165. AssertFatal(mMessageVector != NULL, "Should not be here unless we're attached!");
  166. AssertFatal(arg < (U32)mLineWrappings.size(), "Error, out of bounds line deleted!");
  167. // It's a somewhat involved process to delete the lineelements...
  168. LineElement& rElement = mLineElements[arg];
  169. TextElement* walk = rElement.headLineElements;
  170. while (walk != NULL) {
  171. TextElement* lineWalk = walk->nextInLine;
  172. while (lineWalk != NULL) {
  173. TextElement* temp = lineWalk;
  174. lineWalk = lineWalk->nextPhysicalLine;
  175. delete temp;
  176. }
  177. TextElement* temp = walk;
  178. walk = walk->nextPhysicalLine;
  179. delete temp;
  180. }
  181. rElement.headLineElements = NULL;
  182. mLineElements.erase(arg);
  183. delete [] mLineWrappings[arg].startEndPairs;
  184. mLineWrappings.erase(arg);
  185. delete [] mSpecialMarkers[arg].specials;
  186. mSpecialMarkers.erase(arg);
  187. U32 numLines = 0;
  188. for (U32 i = 0; i < (U32)mLineWrappings.size(); i++) {
  189. // We need to rebuild the physicalLineStart markers at the same time as
  190. // we find out how many of them are left...
  191. mLineElements[i].physicalLineStart = numLines;
  192. numLines += mLineWrappings[i].numLines;
  193. }
  194. U32 newHeight = (mProfile->getFont(mFontSizeAdjust)->getHeight() + mLineSpacingPixels) * getMax(numLines, U32(1));
  195. resize(mBounds.point, Point2I(mBounds.extent.x, newHeight));
  196. }
  197. void GuiMessageVectorCtrl::vectorDeleted()
  198. {
  199. AssertFatal(mMessageVector != NULL, "Should not be here unless we're attached!");
  200. AssertFatal(mLineWrappings.size() == 0, "Error, line wrappings not properly cleared out!");
  201. mMessageVector = NULL;
  202. U32 newHeight = mProfile->getFont(mFontSizeAdjust)->getHeight() + mLineSpacingPixels;
  203. resize(mBounds.point, Point2I(mBounds.extent.x, newHeight));
  204. }
  205. void GuiMessageVectorCtrl::callbackRouter(const MessageVector::MessageCode code,
  206. const U32 arg)
  207. {
  208. switch (code) {
  209. case MessageVector::LineInserted:
  210. lineInserted(arg);
  211. break;
  212. case MessageVector::LineDeleted:
  213. lineDeleted(arg);
  214. break;
  215. case MessageVector::VectorDeletion:
  216. vectorDeleted();
  217. break;
  218. }
  219. }
  220. //--------------------------------------------------------------------------
  221. void GuiMessageVectorCtrl::createSpecialMarkers(SpecialMarkers& rSpecial, const char* string)
  222. {
  223. // The first thing we need to do is create a version of the string with no uppercase
  224. // chars for matching...
  225. char* pLCCopy = new char[dStrlen(string) + 1];
  226. for (U32 i = 0; string[i] != '\0'; i++)
  227. pLCCopy[i] = dTolower(string[i]);
  228. pLCCopy[dStrlen(string)] = '\0';
  229. Vector<TempLineBreak> tempSpecials(__FILE__, __LINE__);
  230. Vector<S32> tempTypes(__FILE__, __LINE__);
  231. const char* pCurr = pLCCopy;
  232. while (pCurr[0] != '\0') {
  233. const char* pMinMatch = &pLCCopy[dStrlen(string)];
  234. U32 minMatchType = 0xFFFFFFFF;
  235. AssertFatal(pMinMatch[0] == '\0', "Error, bad positioning of sentry...");
  236. for (U32 i = 0; i < 16; i++) {
  237. if (mAllowedMatches[i][0] == '\0')
  238. continue;
  239. // Not the most efficient...
  240. char matchBuffer[512];
  241. dStrncpy(matchBuffer, mAllowedMatches[i], 500);
  242. matchBuffer[499] = '\0';
  243. dStrcat(matchBuffer, "://");
  244. const char* pMatch = dStrstr(pCurr, matchBuffer);
  245. if (pMatch != NULL && pMatch < pMinMatch) {
  246. pMinMatch = pMatch;
  247. minMatchType = i;
  248. }
  249. }
  250. if (pMinMatch[0] != '\0') {
  251. AssertFatal(minMatchType != 0xFFFFFFFF, "Hm, that's bad");
  252. // Found a match...
  253. U32 start = (U32)(pMinMatch - pLCCopy);
  254. U32 j;
  255. for (j = 0; pLCCopy[start + j] != '\0'; j++) {
  256. if (pLCCopy[start + j] == '\n' ||
  257. pLCCopy[start + j] == ' ' ||
  258. pLCCopy[start + j] == '\t')
  259. break;
  260. }
  261. AssertFatal(j > 0, "Error, j must be > 0 at this point!");
  262. U32 end = start + j - 1;
  263. tempSpecials.increment();
  264. tempSpecials.last().start = start;
  265. tempSpecials.last().end = end;
  266. tempTypes.push_back(minMatchType);
  267. pCurr = &pLCCopy[end + 1];
  268. } else {
  269. // No match. This will cause the while loop to terminate...
  270. pCurr = pMinMatch;
  271. }
  272. }
  273. if ((rSpecial.numSpecials = tempSpecials.size()) != 0) {
  274. rSpecial.specials = new SpecialMarkers::Special[tempSpecials.size()];
  275. for (U32 i = 0; i < (U32)tempSpecials.size(); i++) {
  276. rSpecial.specials[i].start = tempSpecials[i].start;
  277. rSpecial.specials[i].end = tempSpecials[i].end;
  278. rSpecial.specials[i].specialType = tempTypes[i];
  279. }
  280. } else {
  281. rSpecial.specials = NULL;
  282. }
  283. }
  284. //--------------------------------------------------------------------------
  285. void GuiMessageVectorCtrl::createLineWrapping(LineWrapping& rWrapping, const char* string)
  286. {
  287. Vector<TempLineBreak> tempBreaks(__FILE__, __LINE__);
  288. U32 i;
  289. U32 currStart = 0;
  290. if (dStrlen(string) != 0) {
  291. for (i = 0; i < dStrlen(string); i++) {
  292. if (string[i] == '\n') {
  293. tempBreaks.increment();
  294. tempBreaks.last().start = currStart;
  295. tempBreaks.last().end = i-1;
  296. currStart = i+1;
  297. } else if (i == dStrlen(string) - 1) {
  298. tempBreaks.increment();
  299. tempBreaks.last().start = currStart;
  300. tempBreaks.last().end = i;
  301. currStart = i+1;
  302. }
  303. }
  304. } else {
  305. tempBreaks.increment();
  306. tempBreaks.last().start = 0;
  307. tempBreaks.last().end = -1;
  308. }
  309. U32 splitWidth = mBounds.extent.x;
  310. U32 currLine = 0;
  311. while (currLine < (U32)tempBreaks.size()) {
  312. TempLineBreak& rLine = tempBreaks[currLine];
  313. if (rLine.start >= rLine.end) {
  314. if (currLine == 0)
  315. splitWidth -= mLineContinuationIndent;
  316. currLine++;
  317. continue;
  318. }
  319. // Ok, there's some actual text in this line. How long is it?
  320. U32 baseLength = mProfile->getFont(mFontSizeAdjust)->getStrNWidthPrecise((const UTF8 *)&string[rLine.start], rLine.end-rLine.start+1);
  321. if (baseLength > splitWidth) {
  322. // DMMNOTE: Replace with bin search eventually
  323. U32 currPos;
  324. U32 breakPos;
  325. for (currPos = 0; currPos < (U32)(rLine.end-rLine.start+1); currPos++) {
  326. U32 currLength = mProfile->getFont(mFontSizeAdjust)->getStrNWidthPrecise((const UTF8 *)&string[rLine.start], currPos+1);
  327. if (currLength > splitWidth) {
  328. // Make sure that the currPos has advanced, then set the breakPoint.
  329. breakPos = currPos != 0 ? currPos - 1 : 0;
  330. break;
  331. }
  332. }
  333. if (currPos == rLine.end-rLine.start+1) {
  334. AssertFatal(false, "Error, if the line must be broken, the position must be before this point!");
  335. currLine++;
  336. continue;
  337. }
  338. // Ok, the character at breakPos is the last valid char we can render. We
  339. // want to scan back to the first whitespace character (which, in the bounds
  340. // of the line, is guaranteed to be a space or a tab).
  341. U32 originalBreak = breakPos;
  342. while (true) {
  343. if (string[rLine.start + breakPos] == ' ' || string[rLine.start + breakPos] == '\t') {
  344. break;
  345. } else {
  346. AssertFatal(string[rLine.start + breakPos] != '\n',
  347. "Bad characters in line range...");
  348. if (breakPos == 0) {
  349. breakPos = originalBreak;
  350. break;
  351. }
  352. breakPos--;
  353. }
  354. }
  355. // Ok, everything up to and including breakPos is in the currentLine. Insert
  356. // a new line at this point, and put everything after in that line.
  357. S32 oldStart = rLine.start;
  358. S32 oldEnd = rLine.end;
  359. rLine.end = rLine.start + breakPos;
  360. // Note that rLine is NOTNOTNOTNOT valid after this point!
  361. tempBreaks.insert(currLine+1);
  362. tempBreaks[currLine+1].start = oldStart + breakPos + 1;
  363. tempBreaks[currLine+1].end = oldEnd;
  364. }
  365. if (currLine == 0)
  366. splitWidth -= mLineContinuationIndent;
  367. currLine++;
  368. }
  369. rWrapping.numLines = tempBreaks.size();
  370. rWrapping.startEndPairs = new LineWrapping::SEPair[tempBreaks.size()];
  371. for (i = 0; i < (U32)tempBreaks.size(); i++) {
  372. rWrapping.startEndPairs[i].start = tempBreaks[i].start;
  373. rWrapping.startEndPairs[i].end = tempBreaks[i].end;
  374. }
  375. }
  376. //--------------------------------------------------------------------------
  377. void GuiMessageVectorCtrl::createLineElement(LineElement& rElement,
  378. LineWrapping& rWrapping,
  379. SpecialMarkers& rSpecial)
  380. {
  381. // First, do a straighforward translation of the wrapping...
  382. TextElement** ppWalk = &rElement.headLineElements;
  383. for (U32 i = 0; i < rWrapping.numLines; i++) {
  384. *ppWalk = new TextElement;
  385. (*ppWalk)->nextInLine = NULL;
  386. (*ppWalk)->nextPhysicalLine = NULL;
  387. (*ppWalk)->specialReference = -1;
  388. (*ppWalk)->start = rWrapping.startEndPairs[i].start;
  389. (*ppWalk)->end = rWrapping.startEndPairs[i].end;
  390. ppWalk = &((*ppWalk)->nextPhysicalLine);
  391. }
  392. if (rSpecial.numSpecials != 0) {
  393. // Ok. Now, walk down the lines, and split the elements into their contiuent parts...
  394. //
  395. TextElement* walk = rElement.headLineElements;
  396. while (walk) {
  397. TextElement* walkAcross = walk;
  398. while (walkAcross) {
  399. S32 specialMatch = -1;
  400. for (U32 i = 0; i < rSpecial.numSpecials; i++) {
  401. if (walkAcross->start <= rSpecial.specials[i].end &&
  402. walkAcross->end >= rSpecial.specials[i].start) {
  403. specialMatch = i;
  404. break;
  405. }
  406. }
  407. if (specialMatch != -1) {
  408. // We have a match here. Break it into the appropriate number of segments.
  409. // this will vary depending on how the overlap is occuring...
  410. if (walkAcross->start >= rSpecial.specials[specialMatch].start) {
  411. if (walkAcross->end <= rSpecial.specials[specialMatch].end) {
  412. // The whole thing is part of the special
  413. AssertFatal(walkAcross->nextInLine == NULL, "Bad assumption!");
  414. walkAcross->specialReference = specialMatch;
  415. } else {
  416. // The first part is in the special, the tail is out
  417. AssertFatal(walkAcross->nextInLine == NULL, "Bad assumption!");
  418. walkAcross->nextInLine = new TextElement;
  419. walkAcross->nextInLine->nextInLine = NULL;
  420. walkAcross->nextInLine->nextPhysicalLine = NULL;
  421. walkAcross->nextInLine->specialReference = -1;
  422. walkAcross->specialReference = specialMatch;
  423. walkAcross->nextInLine->end = walkAcross->end;
  424. walkAcross->end = rSpecial.specials[specialMatch].end;
  425. walkAcross->nextInLine->start = rSpecial.specials[specialMatch].end + 1;
  426. AssertFatal(walkAcross->end >= walkAcross->start && walkAcross->nextInLine->end >= walkAcross->nextInLine->start, "Bad textelements generated!");
  427. }
  428. walkAcross = walkAcross->nextInLine;
  429. } else {
  430. if (walkAcross->end <= rSpecial.specials[specialMatch].end) {
  431. // The first part is out of the special, the second part in.
  432. AssertFatal(walkAcross->nextInLine == NULL, "Bad assumption!");
  433. walkAcross->nextInLine = new TextElement;
  434. walkAcross->nextInLine->nextInLine = NULL;
  435. walkAcross->nextInLine->nextPhysicalLine = NULL;
  436. walkAcross->nextInLine->specialReference = specialMatch;
  437. walkAcross->specialReference = -1;
  438. walkAcross->nextInLine->end = walkAcross->end;
  439. walkAcross->end = rSpecial.specials[specialMatch].start - 1;
  440. walkAcross->nextInLine->start = rSpecial.specials[specialMatch].start;
  441. AssertFatal(walkAcross->end >= walkAcross->start && walkAcross->nextInLine->end >= walkAcross->nextInLine->start, "Bad textelements generated!");
  442. walkAcross = walkAcross->nextInLine;
  443. } else {
  444. // First out, middle in, last out. Oy.
  445. AssertFatal(walkAcross->nextInLine == NULL, "Bad assumption!");
  446. walkAcross->nextInLine = new TextElement;
  447. walkAcross->nextInLine->nextInLine = NULL;
  448. walkAcross->nextInLine->nextPhysicalLine = NULL;
  449. walkAcross->nextInLine->specialReference = specialMatch;
  450. walkAcross->nextInLine->nextInLine = new TextElement;
  451. walkAcross->nextInLine->nextInLine->nextInLine = NULL;
  452. walkAcross->nextInLine->nextInLine->nextPhysicalLine = NULL;
  453. walkAcross->nextInLine->nextInLine->specialReference = -1;
  454. walkAcross->nextInLine->start = rSpecial.specials[specialMatch].start;
  455. walkAcross->nextInLine->end = rSpecial.specials[specialMatch].end;
  456. walkAcross->nextInLine->nextInLine->start = rSpecial.specials[specialMatch].end+1;
  457. walkAcross->nextInLine->nextInLine->end = walkAcross->end;
  458. walkAcross->end = walkAcross->nextInLine->start - 1;
  459. AssertFatal((walkAcross->end >= walkAcross->start &&
  460. walkAcross->nextInLine->end >= walkAcross->nextInLine->start &&
  461. walkAcross->nextInLine->nextInLine->end >= walkAcross->nextInLine->nextInLine->start),
  462. "Bad textelements generated!");
  463. walkAcross = walkAcross->nextInLine->nextInLine;
  464. }
  465. }
  466. } else {
  467. walkAcross = walkAcross->nextInLine;
  468. }
  469. }
  470. walk = walk->nextPhysicalLine;
  471. }
  472. }
  473. }
  474. //--------------------------------------------------------------------------
  475. bool GuiMessageVectorCtrl::onWake()
  476. {
  477. if (Parent::onWake() == false)
  478. return false;
  479. GFont* font = mProfile->getFont(mFontSizeAdjust);
  480. if (bool(font) == false)
  481. return false;
  482. mMinSensibleWidth = 1;
  483. for (U32 i = 0; i < 256; i++) {
  484. if (font->isValidChar(U8(i))) {
  485. if (font->getCharWidth(U8(i)) > mMinSensibleWidth)
  486. mMinSensibleWidth = font->getCharWidth(U8(i));
  487. }
  488. }
  489. AssertFatal(mLineWrappings.size() == 0, "Error, line wrappings not properly cleared!");
  490. return true;
  491. }
  492. //--------------------------------------------------------------------------
  493. void GuiMessageVectorCtrl::onSleep()
  494. {
  495. if (isAttached())
  496. detach();
  497. Parent::onSleep();
  498. }
  499. //--------------------------------------------------------------------------
  500. void GuiMessageVectorCtrl::onRender(Point2I offset,
  501. const RectI& updateRect)
  502. {
  503. Parent::onRender(offset, updateRect);
  504. GFont* font = mProfile->getFont(mFontSizeAdjust);
  505. if (isAttached()) {
  506. U32 linePixels = font->getHeight() + mLineSpacingPixels;
  507. U32 currLine = 0;
  508. for (U32 i = 0; i < mMessageVector->getNumLines(); i++) {
  509. TextElement* pElement = mLineElements[i].headLineElements;
  510. ColorI lastColor = mProfile->mFontColor;
  511. while (pElement != NULL) {
  512. Point2I localStart(pElement == mLineElements[i].headLineElements ? 0 : mLineContinuationIndent, currLine * linePixels);
  513. Point2I globalCheck = localToGlobalCoord(localStart);
  514. U32 globalRangeStart = globalCheck.y;
  515. U32 globalRangeEnd = globalCheck.y + font->getHeight();
  516. if (globalRangeStart > (U32)(updateRect.point.y + updateRect.extent.y) ||
  517. globalRangeEnd < (U32)updateRect.point.y) {
  518. currLine++;
  519. pElement = pElement->nextPhysicalLine;
  520. continue;
  521. }
  522. TextElement* walkAcross = pElement;
  523. while (walkAcross) {
  524. if (walkAcross->start > walkAcross->end)
  525. break;
  526. Point2I globalStart = localToGlobalCoord(localStart);
  527. U32 strWidth;
  528. if (walkAcross->specialReference == -1) {
  529. dglSetBitmapModulation(lastColor);
  530. dglSetTextAnchorColor(mProfile->mFontColor);
  531. strWidth = dglDrawTextN(font, globalStart, &mMessageVector->getLine(i).message[walkAcross->start],
  532. walkAcross->end - walkAcross->start + 1, mProfile->mFontColors, mMaxColorIndex);
  533. dglGetBitmapModulation(&lastColor);
  534. } else {
  535. dglGetBitmapModulation(&lastColor);
  536. dglSetBitmapModulation(mSpecialColor);
  537. dglSetTextAnchorColor(mProfile->mFontColor);
  538. strWidth = dglDrawTextN(font, globalStart, &mMessageVector->getLine(i).message[walkAcross->start],
  539. walkAcross->end - walkAcross->start + 1);
  540. // in case we have 2 in a row...
  541. dglSetBitmapModulation(lastColor);
  542. }
  543. if (walkAcross->specialReference != -1) {
  544. Point2I lineStart = localStart;
  545. Point2I lineEnd = localStart;
  546. lineStart.y += font->getBaseline() + 1;
  547. lineEnd.x += strWidth;
  548. lineEnd.y += font->getBaseline() + 1;
  549. dglDrawLine(localToGlobalCoord(lineStart),
  550. localToGlobalCoord(lineEnd),
  551. mSpecialColor);
  552. }
  553. localStart.x += strWidth;
  554. walkAcross = walkAcross->nextInLine;
  555. }
  556. currLine++;
  557. pElement = pElement->nextPhysicalLine;
  558. }
  559. }
  560. dglClearBitmapModulation();
  561. }
  562. }
  563. //--------------------------------------------------------------------------
  564. void GuiMessageVectorCtrl::inspectPostApply()
  565. {
  566. Parent::inspectPostApply();
  567. }
  568. //--------------------------------------------------------------------------
  569. void GuiMessageVectorCtrl::parentResized(const Point2I& oldSize,
  570. const Point2I& newSize)
  571. {
  572. Parent::parentResized(oldSize, newSize);
  573. // If we have a MesssageVector, detach/reattach so we can reflow the text.
  574. if (mMessageVector)
  575. {
  576. MessageVector *reflowme = mMessageVector;
  577. detach();
  578. attach(reflowme);
  579. }
  580. }
  581. //--------------------------------------------------------------------------
  582. void GuiMessageVectorCtrl::findSpecialFromCoord(const Point2I& point, S32* specialLine, S32* specialRef)
  583. {
  584. if (mLineElements.size() == 0) {
  585. *specialLine = -1;
  586. *specialRef = -1;
  587. return;
  588. }
  589. U32 linePixels = mProfile->getFont(mFontSizeAdjust)->getHeight() + mLineSpacingPixels;
  590. if ((point.x < 0 || point.x >= mBounds.extent.x) ||
  591. (point.y < 0 || point.y >= mBounds.extent.y)) {
  592. *specialLine = -1;
  593. *specialRef = -1;
  594. return;
  595. }
  596. // Ok, we have real work to do here. Let's determine the physical line that it's on...
  597. U32 physLine = point.y / linePixels;
  598. AssertFatal(physLine >= 0, "Bad physical line!");
  599. // And now we find the LineElement that contains that physicalLine...
  600. U32 elemIndex;
  601. for (elemIndex = 0; elemIndex < (U32)mLineElements.size(); elemIndex++) {
  602. if (mLineElements[elemIndex].physicalLineStart > physLine) {
  603. // We've passed it.
  604. AssertFatal(elemIndex != 0, "Error, bad elemIndex, check assumptions.");
  605. elemIndex--;
  606. break;
  607. }
  608. }
  609. if (elemIndex == mLineElements.size()) {
  610. // On the last line...
  611. elemIndex = mLineElements.size() - 1;
  612. }
  613. TextElement* line = mLineElements[elemIndex].headLineElements;
  614. for (U32 i = 0; i < physLine - mLineElements[elemIndex].physicalLineStart; i++)
  615. {
  616. if (line->nextPhysicalLine == NULL)
  617. {
  618. *specialLine = -1;
  619. *specialRef = -1;
  620. return;
  621. }
  622. line = line->nextPhysicalLine;
  623. AssertFatal(line != NULL, "Error, moved too far!");
  624. }
  625. // Ok, line represents the current line. We now need to find out which textelement
  626. // the points x coord falls in.
  627. U32 currX = 0;
  628. if ((physLine - mLineElements[elemIndex].physicalLineStart) != 0) {
  629. currX = mLineContinuationIndent;
  630. // First, if this isn't the first line in this wrapping, we have to make sure
  631. // that the coord isn't in the margin...
  632. if (point.x < (S32)mLineContinuationIndent) {
  633. *specialLine = -1;
  634. *specialRef = -1;
  635. return;
  636. }
  637. }
  638. if (line->start > line->end) {
  639. // Empty line special case...
  640. *specialLine = -1;
  641. *specialRef = -1;
  642. return;
  643. }
  644. while (line) {
  645. U32 newX = currX + mProfile->getFont(mFontSizeAdjust)->getStrNWidth((const UTF8 *)mMessageVector->getLine(elemIndex).message,
  646. line->end - line->start + 1);
  647. if (point.x < (S32)newX) {
  648. // That's the one!
  649. *specialLine = elemIndex;
  650. *specialRef = line->specialReference;
  651. return;
  652. }
  653. currX = newX;
  654. line = line->nextInLine;
  655. }
  656. // Off to the right. Aw...
  657. *specialLine = -1;
  658. *specialRef = -1;
  659. }
  660. void GuiMessageVectorCtrl::onMouseDown(const GuiEvent& event)
  661. {
  662. Parent::onTouchDown(event);
  663. mouseUnlock();
  664. mMouseDown = true;
  665. // Find the special we are in, if any...
  666. findSpecialFromCoord(globalToLocalCoord(event.mousePoint),
  667. &mMouseSpecialLine, &mMouseSpecialRef);
  668. }
  669. void GuiMessageVectorCtrl::onMouseUp(const GuiEvent& event)
  670. {
  671. Parent::onTouchUp(event);
  672. mouseUnlock();
  673. // Is this an up from a dragged click?
  674. if (mMouseDown == false)
  675. return;
  676. // Find the special we are in, if any...
  677. S32 currSpecialLine;
  678. S32 currSpecialRef;
  679. findSpecialFromCoord(globalToLocalCoord(event.mousePoint), &currSpecialLine, &currSpecialRef);
  680. if (currSpecialRef != -1 &&
  681. (currSpecialLine == mMouseSpecialLine &&
  682. currSpecialRef == mMouseSpecialRef)) {
  683. // Execute the callback
  684. SpecialMarkers& rSpecial = mSpecialMarkers[currSpecialLine];
  685. S32 specialStart = rSpecial.specials[currSpecialRef].start;
  686. S32 specialEnd = rSpecial.specials[currSpecialRef].end;
  687. char* copyURL = new char[specialEnd - specialStart + 2];
  688. dStrncpy(copyURL, &mMessageVector->getLine(currSpecialLine).message[specialStart], specialEnd - specialStart + 1);
  689. copyURL[specialEnd - specialStart + 1] = '\0';
  690. Con::executef(this, 2, "urlClickCallback", copyURL);
  691. delete [] copyURL;
  692. }
  693. mMouseDown = false;
  694. mMouseSpecialLine = -1;
  695. mMouseSpecialRef = -1;
  696. }