guiMessageVectorCtrl.cc 31 KB

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