guiMessageVectorCtrl.cc 31 KB

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