guiMessageVectorCtrl.cpp 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 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 "platform/platform.h"
  23. #include "gui/game/guiMessageVectorCtrl.h"
  24. #include "gui/utility/messageVector.h"
  25. #include "console/consoleTypes.h"
  26. #include "gui/containers/guiScrollCtrl.h"
  27. #include "gfx/gfxDevice.h"
  28. #include "gfx/gfxDrawUtil.h"
  29. #include "console/engineAPI.h"
  30. IMPLEMENT_CONOBJECT(GuiMessageVectorCtrl);
  31. ConsoleDocClass( GuiMessageVectorCtrl,
  32. "@brief A chat HUD control that displays messages from a MessageVector.\n\n"
  33. "This renders messages from a MessageVector; the important thing "
  34. "here is that the MessageVector holds all the messages we care "
  35. "about, while we can destroy and create these GUI controls as "
  36. "needed.\n\n"
  37. "@tsexample\n"
  38. "// Declare ChatHud, which is what will display the actual chat from a MessageVector\n"
  39. "new GuiMessageVectorCtrl(ChatHud) {\n"
  40. " profile = \"ChatHudMessageProfile\";\n"
  41. " horizSizing = \"width\";\n"
  42. " vertSizing = \"height\";\n"
  43. " position = \"1 1\";\n"
  44. " extent = \"252 16\";\n"
  45. " minExtent = \"8 8\";\n"
  46. " visible = \"1\";\n"
  47. " helpTag = \"0\";\n"
  48. " lineSpacing = \"0\";\n"
  49. " lineContinuedIndex = \"10\";\n"
  50. " matchColor = \"0 0 255 255\";\n"
  51. " maxColorIndex = \"5\";\n"
  52. "};\n\n"
  53. "// All messages are stored in this HudMessageVector, the actual\n"
  54. "// MainChatHud only displays the contents of this vector.\n"
  55. "new MessageVector(HudMessageVector);\n\n"
  56. "// Attach the MessageVector to the chat control\n"
  57. "chatHud.attach(HudMessageVector);\n"
  58. "@endtsexample\n\n"
  59. "@see MessageVector for more details on how this is used\n"
  60. "@ingroup GuiUtil\n");
  61. //-------------------------------------- Console functions
  62. DefineEngineMethod( GuiMessageVectorCtrl, attach, bool, ( MessageVector* item),,
  63. "@brief Push a line onto the back of the list.\n\n"
  64. "@param item The GUI element being pushed into the control\n\n"
  65. "@tsexample\n"
  66. "// All messages are stored in this HudMessageVector, the actual\n"
  67. "// MainChatHud only displays the contents of this vector.\n"
  68. "new MessageVector(HudMessageVector);\n\n"
  69. "// Attach the MessageVector to the chat control\n"
  70. "chatHud.attach(HudMessageVector);\n"
  71. "@endtsexample\n\n"
  72. "@return Value")
  73. {
  74. if (item == NULL)
  75. {
  76. Con::errorf(ConsoleLogEntry::General, "Could not find MessageVector: %s", item);
  77. return false;
  78. }
  79. return object->attach(item);
  80. }
  81. //ConsoleMethod(GuiMessageVectorCtrl, attach, bool, 3, 3, "(MessageVector item)"
  82. // "Make this gui control display messages from the specified MessageVector")
  83. //{
  84. // MessageVector* pMV = NULL;
  85. // Sim::findObject(argv[2], pMV);
  86. // if (pMV == NULL) {
  87. // Con::errorf(ConsoleLogEntry::General, "Could not find MessageVector: %s", argv[2]);
  88. // return false;
  89. // }
  90. //
  91. // return object->attach(pMV);
  92. //}
  93. DefineEngineMethod( GuiMessageVectorCtrl, detach, void, (),,
  94. "@brief Stop listing messages from the MessageVector previously attached to, if any.\n\n"
  95. "Detailed description\n\n"
  96. "@param param Description\n\n"
  97. "@tsexample\n"
  98. "// Deatch the MessageVector from HudMessageVector\n"
  99. "// HudMessageVector will no longer render the text\n"
  100. "chatHud.detach();\n"
  101. "@endtsexample\n\n")
  102. {
  103. if (object->isAttached() == false)
  104. {
  105. Con::warnf(ConsoleLogEntry::General, "GuiMessageVectorCtrl: double detach");
  106. return;
  107. }
  108. object->detach();
  109. }
  110. //ConsoleMethod(GuiMessageVectorCtrl, detach, void, 2, 2, "()"
  111. // "Stop listing messages from the MessageVector previously attached to, if any.")
  112. //{
  113. // if (object->isAttached() == false) {
  114. // Con::warnf(ConsoleLogEntry::General, "GuiMessageVectorCtrl: double detach");
  115. // return;
  116. // }
  117. //
  118. // object->detach();
  119. //}
  120. struct TempLineBreak
  121. {
  122. S32 start;
  123. S32 end;
  124. };
  125. //--------------------------------------------------------------------------
  126. // Callback for messageVector
  127. void sMVCtrlCallback(void * spectatorKey,
  128. const MessageVector::MessageCode code,
  129. const U32 argument)
  130. {
  131. GuiMessageVectorCtrl* pMVC = reinterpret_cast<GuiMessageVectorCtrl*>(spectatorKey);
  132. pMVC->callbackRouter(code, argument);
  133. }
  134. //--------------------------------------------------------------------------
  135. GuiMessageVectorCtrl::GuiMessageVectorCtrl()
  136. {
  137. VECTOR_SET_ASSOCIATION(mLineWrappings);
  138. VECTOR_SET_ASSOCIATION(mSpecialMarkers);
  139. VECTOR_SET_ASSOCIATION(mLineElements);
  140. mMessageVector = NULL;
  141. mLineSpacingPixels = 0;
  142. mLineContinuationIndent = 10;
  143. mMouseDown = false;
  144. mMouseSpecialLine = -1;
  145. mMouseSpecialRef = -1;
  146. for (U32 i = 0; i < 16; i++)
  147. mAllowedMatches[i] = "";
  148. mSpecialColor.set(0, 0, 255);
  149. mMaxColorIndex = 9;
  150. }
  151. //--------------------------------------------------------------------------
  152. GuiMessageVectorCtrl::~GuiMessageVectorCtrl()
  153. {
  154. AssertFatal(mLineWrappings.size() == 0, "Error, line wrappings not properly cleared!");
  155. AssertFatal(mSpecialMarkers.size() == 0, "Error, special markers not properly cleared!");
  156. AssertFatal(mLineElements.size() == 0, "Error, line elements not properly cleared!");
  157. }
  158. //--------------------------------------------------------------------------
  159. void GuiMessageVectorCtrl::initPersistFields()
  160. {
  161. addField("lineSpacing", TypeS32, Offset(mLineSpacingPixels, GuiMessageVectorCtrl));
  162. addField("lineContinuedIndex", TypeS32, Offset(mLineContinuationIndent, GuiMessageVectorCtrl));
  163. addField("allowedMatches", TypeString, Offset(mAllowedMatches, GuiMessageVectorCtrl), 16);
  164. addField("matchColor", TypeColorI, Offset(mSpecialColor, GuiMessageVectorCtrl));
  165. addField("maxColorIndex", TypeS32, Offset(mMaxColorIndex, GuiMessageVectorCtrl));
  166. Parent::initPersistFields();
  167. }
  168. bool GuiMessageVectorCtrl::onAdd()
  169. {
  170. return Parent::onAdd();
  171. }
  172. void GuiMessageVectorCtrl::onRemove()
  173. {
  174. Parent::onRemove();
  175. }
  176. //--------------------------------------------------------------------------
  177. bool GuiMessageVectorCtrl::isAttached() const
  178. {
  179. return (mMessageVector != NULL);
  180. }
  181. //--------------------------------------------------------------------------
  182. bool GuiMessageVectorCtrl::attach(MessageVector* newAttachment)
  183. {
  184. AssertFatal(newAttachment, "No attachment!");
  185. if (newAttachment == NULL || !isAwake())
  186. return false;
  187. if (isAttached()) {
  188. Con::warnf(ConsoleLogEntry::General, "GuiMessageVectorCtrl::attach: overriding attachment");
  189. detach();
  190. }
  191. AssertFatal(mLineWrappings.size() == 0, "Error, line wrappings not properly cleared!");
  192. mMessageVector = newAttachment;
  193. mMessageVector->registerSpectator(sMVCtrlCallback, this);
  194. return true;
  195. }
  196. //--------------------------------------------------------------------------
  197. void GuiMessageVectorCtrl::detach()
  198. {
  199. if (isAttached() == false) {
  200. Con::warnf(ConsoleLogEntry::General, "GuiMessageVectorCtrl::detach: not attached!");
  201. return;
  202. }
  203. mMessageVector->unregisterSpectator(this);
  204. mMessageVector = NULL;
  205. AssertFatal(mLineWrappings.size() == 0, "Error, line wrappings not properly cleared!");
  206. }
  207. //--------------------------------------------------------------------------
  208. void GuiMessageVectorCtrl::lineInserted(const U32 arg)
  209. {
  210. AssertFatal(mMessageVector != NULL, "Should not be here unless we're attached!");
  211. GuiScrollCtrl* pScroll = dynamic_cast<GuiScrollCtrl*>(getParent());
  212. bool fullyScrolled = pScroll->isScrolledToBottom();
  213. mSpecialMarkers.insert(arg);
  214. createSpecialMarkers(mSpecialMarkers[arg], mMessageVector->getLine(arg).message);
  215. mLineWrappings.insert(arg);
  216. createLineWrapping(mLineWrappings[arg], mMessageVector->getLine(arg).message);
  217. mLineElements.insert(arg);
  218. createLineElement(mLineElements[arg], mLineWrappings[arg], mSpecialMarkers[arg]);
  219. U32 numLines = 0;
  220. for (U32 i = 0; i < mLineWrappings.size(); i++) {
  221. // We need to rebuild the physicalLineStart markers at the same time as
  222. // we find out how many of them are left...
  223. mLineElements[i].physicalLineStart = numLines;
  224. numLines += mLineWrappings[i].numLines;
  225. }
  226. Point2I newExtent = getExtent();
  227. newExtent.y = (mProfile->mFont->getHeight() + mLineSpacingPixels) * getMax(numLines, U32(1));
  228. setExtent(newExtent);
  229. if(fullyScrolled)
  230. pScroll->scrollTo(0, 0x7FFFFFFF);
  231. }
  232. void GuiMessageVectorCtrl::lineDeleted(const U32 arg)
  233. {
  234. AssertFatal(mMessageVector != NULL, "Should not be here unless we're attached!");
  235. AssertFatal(arg < mLineWrappings.size(), "Error, out of bounds line deleted!");
  236. // It's a somewhat involved process to delete the lineelements...
  237. LineElement& rElement = mLineElements[arg];
  238. TextElement* walk = rElement.headLineElements;
  239. while (walk != NULL) {
  240. TextElement* lineWalk = walk->nextInLine;
  241. while (lineWalk != NULL) {
  242. TextElement* temp = lineWalk;
  243. lineWalk = lineWalk->nextPhysicalLine;
  244. delete temp;
  245. }
  246. TextElement* temp = walk;
  247. walk = walk->nextPhysicalLine;
  248. delete temp;
  249. }
  250. rElement.headLineElements = NULL;
  251. mLineElements.erase(arg);
  252. delete [] mLineWrappings[arg].startEndPairs;
  253. mLineWrappings.erase(arg);
  254. delete [] mSpecialMarkers[arg].specials;
  255. mSpecialMarkers.erase(arg);
  256. U32 numLines = 0;
  257. for (U32 i = 0; i < mLineWrappings.size(); i++) {
  258. // We need to rebuild the physicalLineStart markers at the same time as
  259. // we find out how many of them are left...
  260. mLineElements[i].physicalLineStart = numLines;
  261. numLines += mLineWrappings[i].numLines;
  262. }
  263. U32 newHeight = (mProfile->mFont->getHeight() + mLineSpacingPixels) * getMax(numLines, U32(1));
  264. resize(getPosition(), Point2I(getWidth(), newHeight));
  265. }
  266. void GuiMessageVectorCtrl::vectorDeleted()
  267. {
  268. AssertFatal(mMessageVector != NULL, "Should not be here unless we're attached!");
  269. AssertFatal(mLineWrappings.size() == 0, "Error, line wrappings not properly cleared out!");
  270. mMessageVector = NULL;
  271. U32 newHeight = mProfile->mFont->getHeight() + mLineSpacingPixels;
  272. resize(getPosition(), Point2I(getWidth(), newHeight));
  273. }
  274. void GuiMessageVectorCtrl::callbackRouter(const MessageVector::MessageCode code,
  275. const U32 arg)
  276. {
  277. switch (code) {
  278. case MessageVector::LineInserted:
  279. lineInserted(arg);
  280. break;
  281. case MessageVector::LineDeleted:
  282. lineDeleted(arg);
  283. break;
  284. case MessageVector::VectorDeletion:
  285. vectorDeleted();
  286. break;
  287. }
  288. }
  289. //--------------------------------------------------------------------------
  290. void GuiMessageVectorCtrl::createSpecialMarkers(SpecialMarkers& rSpecial, const char* string)
  291. {
  292. // The first thing we need to do is create a version of the string with no uppercase
  293. // chars for matching...
  294. String pLCCopyStr = String::ToLower( string );
  295. const char* pLCCopy = pLCCopyStr.c_str();
  296. Vector<TempLineBreak> tempSpecials(__FILE__, __LINE__);
  297. Vector<S32> tempTypes(__FILE__, __LINE__);
  298. const char* pCurr = pLCCopy;
  299. while (pCurr[0] != '\0') {
  300. const char* pMinMatch = &pLCCopy[dStrlen(string)];
  301. U32 minMatchType = 0xFFFFFFFF;
  302. AssertFatal(pMinMatch[0] == '\0', "Error, bad positioning of sentry...");
  303. // Find the earliest match
  304. for (U32 i = 0; i < 16; i++) {
  305. if (mAllowedMatches[i][0] == '\0')
  306. continue;
  307. const char* pMatch = dStrstr(pCurr, mAllowedMatches[i]);
  308. if (pMatch != NULL && pMatch < pMinMatch) {
  309. pMinMatch = pMatch;
  310. minMatchType = i;
  311. }
  312. }
  313. if (pMinMatch[0] != '\0') {
  314. AssertFatal(minMatchType != 0xFFFFFFFF, "Hm, that's bad");
  315. // Found a match => now find the end
  316. U32 start = pMinMatch - pLCCopy;
  317. U32 j;
  318. for (j = 1; pLCCopy[start + j] != '\0'; j++) {
  319. if (pLCCopy[start + j] == '\n' ||
  320. pLCCopy[start + j] == ' ' ||
  321. pLCCopy[start + j] == '\t')
  322. break;
  323. }
  324. AssertFatal(j > 0, "Error, j must be > 0 at this point!");
  325. U32 end = start + j - 1;
  326. tempSpecials.increment();
  327. tempSpecials.last().start = start;
  328. tempSpecials.last().end = end;
  329. tempTypes.push_back(minMatchType);
  330. pCurr = &pLCCopy[end + 1];
  331. } else {
  332. // No match. This will cause the while loop to terminate...
  333. pCurr = pMinMatch;
  334. }
  335. }
  336. if ((rSpecial.numSpecials = tempSpecials.size()) != 0) {
  337. rSpecial.specials = new SpecialMarkers::Special[tempSpecials.size()];
  338. for (U32 i = 0; i < tempSpecials.size(); i++) {
  339. rSpecial.specials[i].start = tempSpecials[i].start;
  340. rSpecial.specials[i].end = tempSpecials[i].end;
  341. rSpecial.specials[i].specialType = tempTypes[i];
  342. }
  343. } else {
  344. rSpecial.specials = NULL;
  345. }
  346. }
  347. //--------------------------------------------------------------------------
  348. void GuiMessageVectorCtrl::createLineWrapping(LineWrapping& rWrapping, const char* string)
  349. {
  350. Vector<TempLineBreak> tempBreaks(__FILE__, __LINE__);
  351. U32 i;
  352. U32 currStart = 0;
  353. U32 length = dStrlen(string);
  354. if (length != 0) {
  355. for (i = 0; i < length; i++) {
  356. if (string[i] == '\n') {
  357. tempBreaks.increment();
  358. tempBreaks.last().start = currStart;
  359. tempBreaks.last().end = i-1;
  360. currStart = i+1;
  361. } else if (i == length - 1) {
  362. tempBreaks.increment();
  363. tempBreaks.last().start = currStart;
  364. tempBreaks.last().end = i;
  365. currStart = i+1;
  366. }
  367. }
  368. } else {
  369. tempBreaks.increment();
  370. tempBreaks.last().start = 0;
  371. tempBreaks.last().end = -1;
  372. }
  373. U32 splitWidth = getWidth();
  374. U32 currLine = 0;
  375. while (currLine < tempBreaks.size()) {
  376. TempLineBreak& rLine = tempBreaks[currLine];
  377. if (rLine.start >= rLine.end) {
  378. if (currLine == 0)
  379. splitWidth -= mLineContinuationIndent;
  380. currLine++;
  381. continue;
  382. }
  383. // Ok, there's some actual text in this line. How long is it?
  384. U32 baseLength = mProfile->mFont->getStrNWidthPrecise((const UTF8 *)&string[rLine.start], rLine.end-rLine.start+1);
  385. if (baseLength > splitWidth) {
  386. // DMMNOTE: Replace with bin search eventually
  387. U32 currPos = 0;
  388. U32 breakPos = 0;
  389. for (currPos = 0; currPos < rLine.end-rLine.start+1; currPos++) {
  390. U32 currLength = mProfile->mFont->getStrNWidthPrecise((const UTF8 *)&string[rLine.start], currPos+1);
  391. if (currLength > splitWidth)
  392. {
  393. // Make sure that the currPos has advanced, then set the breakPoint.
  394. breakPos = currPos != 0 ? currPos - 1 : 0;
  395. break;
  396. }
  397. }
  398. if (currPos == rLine.end-rLine.start+1) {
  399. AssertFatal(false, "Error, if the line must be broken, the position must be before this point!");
  400. currLine++;
  401. continue;
  402. }
  403. // Ok, the character at breakPos is the last valid char we can render. We
  404. // want to scan back to the first whitespace character (which, in the bounds
  405. // of the line, is guaranteed to be a space or a tab).
  406. U32 originalBreak = breakPos;
  407. while (true) {
  408. if (string[rLine.start + breakPos] == ' ' || string[rLine.start + breakPos] == '\t') {
  409. break;
  410. } else {
  411. AssertFatal(string[rLine.start + breakPos] != '\n',
  412. "Bad characters in line range...");
  413. if (breakPos == 0) {
  414. breakPos = originalBreak;
  415. break;
  416. }
  417. breakPos--;
  418. }
  419. }
  420. // Ok, everything up to and including breakPos is in the currentLine. Insert
  421. // a new line at this point, and put everything after in that line.
  422. S32 oldStart = rLine.start;
  423. S32 oldEnd = rLine.end;
  424. rLine.end = rLine.start + breakPos;
  425. // Note that rLine is NOTNOTNOTNOT valid after this point!
  426. tempBreaks.insert(currLine+1);
  427. tempBreaks[currLine+1].start = oldStart + breakPos + 1;
  428. tempBreaks[currLine+1].end = oldEnd;
  429. }
  430. if (currLine == 0)
  431. splitWidth -= mLineContinuationIndent;
  432. currLine++;
  433. }
  434. rWrapping.numLines = tempBreaks.size();
  435. rWrapping.startEndPairs = new LineWrapping::SEPair[tempBreaks.size()];
  436. for (i = 0; i < tempBreaks.size(); i++) {
  437. rWrapping.startEndPairs[i].start = tempBreaks[i].start;
  438. rWrapping.startEndPairs[i].end = tempBreaks[i].end;
  439. }
  440. }
  441. //--------------------------------------------------------------------------
  442. void GuiMessageVectorCtrl::createLineElement(LineElement& rElement,
  443. LineWrapping& rWrapping,
  444. SpecialMarkers& rSpecial)
  445. {
  446. // First, do a straighforward translation of the wrapping...
  447. TextElement** ppWalk = &rElement.headLineElements;
  448. for (U32 i = 0; i < rWrapping.numLines; i++) {
  449. *ppWalk = new TextElement;
  450. (*ppWalk)->nextInLine = NULL;
  451. (*ppWalk)->nextPhysicalLine = NULL;
  452. (*ppWalk)->specialReference = -1;
  453. (*ppWalk)->start = rWrapping.startEndPairs[i].start;
  454. (*ppWalk)->end = rWrapping.startEndPairs[i].end;
  455. ppWalk = &((*ppWalk)->nextPhysicalLine);
  456. }
  457. if (rSpecial.numSpecials != 0) {
  458. // Ok. Now, walk down the lines, and split the elements into their constituent parts...
  459. //
  460. TextElement* walk = rElement.headLineElements;
  461. while (walk) {
  462. TextElement* walkAcross = walk;
  463. while (walkAcross) {
  464. S32 specialMatch = -1;
  465. for (U32 i = 0; i < rSpecial.numSpecials; i++) {
  466. if (walkAcross->start <= rSpecial.specials[i].end &&
  467. walkAcross->end >= rSpecial.specials[i].start) {
  468. specialMatch = i;
  469. break;
  470. }
  471. }
  472. if (specialMatch != -1) {
  473. // We have a match here. Break it into the appropriate number of segments.
  474. // this will vary depending on how the overlap is occuring...
  475. if (walkAcross->start >= rSpecial.specials[specialMatch].start) {
  476. if (walkAcross->end <= rSpecial.specials[specialMatch].end) {
  477. // The whole thing is part of the special
  478. AssertFatal(walkAcross->nextInLine == NULL, "Bad assumption!");
  479. walkAcross->specialReference = specialMatch;
  480. } else {
  481. // The first part is in the special, the tail is out
  482. AssertFatal(walkAcross->nextInLine == NULL, "Bad assumption!");
  483. walkAcross->nextInLine = new TextElement;
  484. walkAcross->nextInLine->nextInLine = NULL;
  485. walkAcross->nextInLine->nextPhysicalLine = NULL;
  486. walkAcross->nextInLine->specialReference = -1;
  487. walkAcross->specialReference = specialMatch;
  488. walkAcross->nextInLine->end = walkAcross->end;
  489. walkAcross->end = rSpecial.specials[specialMatch].end;
  490. walkAcross->nextInLine->start = rSpecial.specials[specialMatch].end + 1;
  491. AssertFatal(walkAcross->end >= walkAcross->start && walkAcross->nextInLine->end >= walkAcross->nextInLine->start, "Bad textelements generated!");
  492. }
  493. walkAcross = walkAcross->nextInLine;
  494. } else {
  495. if (walkAcross->end <= rSpecial.specials[specialMatch].end) {
  496. // The first part is out of the special, the second part in.
  497. AssertFatal(walkAcross->nextInLine == NULL, "Bad assumption!");
  498. walkAcross->nextInLine = new TextElement;
  499. walkAcross->nextInLine->nextInLine = NULL;
  500. walkAcross->nextInLine->nextPhysicalLine = NULL;
  501. walkAcross->nextInLine->specialReference = specialMatch;
  502. walkAcross->specialReference = -1;
  503. walkAcross->nextInLine->end = walkAcross->end;
  504. walkAcross->end = rSpecial.specials[specialMatch].start - 1;
  505. walkAcross->nextInLine->start = rSpecial.specials[specialMatch].start;
  506. AssertFatal(walkAcross->end >= walkAcross->start && walkAcross->nextInLine->end >= walkAcross->nextInLine->start, "Bad textelements generated!");
  507. walkAcross = walkAcross->nextInLine;
  508. } else {
  509. // First out, middle in, last out. Oy.
  510. AssertFatal(walkAcross->nextInLine == NULL, "Bad assumption!");
  511. walkAcross->nextInLine = new TextElement;
  512. walkAcross->nextInLine->nextInLine = NULL;
  513. walkAcross->nextInLine->nextPhysicalLine = NULL;
  514. walkAcross->nextInLine->specialReference = specialMatch;
  515. walkAcross->nextInLine->nextInLine = new TextElement;
  516. walkAcross->nextInLine->nextInLine->nextInLine = NULL;
  517. walkAcross->nextInLine->nextInLine->nextPhysicalLine = NULL;
  518. walkAcross->nextInLine->nextInLine->specialReference = -1;
  519. walkAcross->nextInLine->start = rSpecial.specials[specialMatch].start;
  520. walkAcross->nextInLine->end = rSpecial.specials[specialMatch].end;
  521. walkAcross->nextInLine->nextInLine->start = rSpecial.specials[specialMatch].end+1;
  522. walkAcross->nextInLine->nextInLine->end = walkAcross->end;
  523. walkAcross->end = walkAcross->nextInLine->start - 1;
  524. AssertFatal((walkAcross->end >= walkAcross->start &&
  525. walkAcross->nextInLine->end >= walkAcross->nextInLine->start &&
  526. walkAcross->nextInLine->nextInLine->end >= walkAcross->nextInLine->nextInLine->start),
  527. "Bad textelements generated!");
  528. walkAcross = walkAcross->nextInLine->nextInLine;
  529. }
  530. }
  531. } else {
  532. walkAcross = walkAcross->nextInLine;
  533. }
  534. }
  535. walk = walk->nextPhysicalLine;
  536. }
  537. }
  538. }
  539. //--------------------------------------------------------------------------
  540. bool GuiMessageVectorCtrl::onWake()
  541. {
  542. if (Parent::onWake() == false)
  543. return false;
  544. if (bool(mProfile->mFont) == false)
  545. return false;
  546. mMinSensibleWidth = 1;
  547. for (U32 i = 0; i < 256; i++) {
  548. if (mProfile->mFont->isValidChar(U8(i))) {
  549. if (mProfile->mFont->getCharWidth(U8(i)) > mMinSensibleWidth)
  550. mMinSensibleWidth = mProfile->mFont->getCharWidth(U8(i));
  551. }
  552. }
  553. AssertFatal(mLineWrappings.size() == 0, "Error, line wrappings not properly cleared!");
  554. return true;
  555. }
  556. //--------------------------------------------------------------------------
  557. void GuiMessageVectorCtrl::onSleep()
  558. {
  559. if (isAttached())
  560. detach();
  561. Parent::onSleep();
  562. }
  563. //--------------------------------------------------------------------------
  564. void GuiMessageVectorCtrl::onRender(Point2I offset,
  565. const RectI& updateRect)
  566. {
  567. GFXDrawUtil *drawer = GFX->getDrawUtil();
  568. Parent::onRender(offset, updateRect);
  569. if (isAttached()) {
  570. U32 linePixels = mProfile->mFont->getHeight() + mLineSpacingPixels;
  571. U32 currLine = 0;
  572. ColorI lastColor = mProfile->mFontColor;
  573. for (U32 i = 0; i < mMessageVector->getNumLines(); i++) {
  574. TextElement* pElement = mLineElements[i].headLineElements;
  575. while (pElement != NULL) {
  576. Point2I localStart(pElement == mLineElements[i].headLineElements ? 0 : mLineContinuationIndent, currLine * linePixels);
  577. Point2I globalCheck = localToGlobalCoord(localStart);
  578. U32 globalRangeStart = globalCheck.y;
  579. U32 globalRangeEnd = globalCheck.y + mProfile->mFont->getHeight();
  580. if (globalRangeStart > updateRect.point.y + updateRect.extent.y ||
  581. globalRangeEnd < updateRect.point.y) {
  582. currLine++;
  583. pElement = pElement->nextPhysicalLine;
  584. continue;
  585. }
  586. TextElement* walkAcross = pElement;
  587. while (walkAcross) {
  588. if (walkAcross->start > walkAcross->end)
  589. break;
  590. Point2I globalStart = localToGlobalCoord(localStart);
  591. U32 strWidth;
  592. if (walkAcross->specialReference == -1) {
  593. drawer->setBitmapModulation(lastColor);
  594. drawer->setTextAnchorColor(mProfile->mFontColor);
  595. strWidth = drawer->drawTextN(mProfile->mFont, globalStart, &mMessageVector->getLine(i).message[walkAcross->start],
  596. walkAcross->end - walkAcross->start + 1, mProfile->mFontColors, mMaxColorIndex);
  597. drawer->getBitmapModulation(&lastColor); // in case an embedded color tag changed it
  598. } else {
  599. drawer->getBitmapModulation( &lastColor );
  600. drawer->setBitmapModulation(mSpecialColor);
  601. drawer->setTextAnchorColor(mProfile->mFontColor);
  602. strWidth = drawer->drawTextN(mProfile->mFont, globalStart, &mMessageVector->getLine(i).message[walkAcross->start],
  603. walkAcross->end - walkAcross->start + 1);
  604. // in case we have 2 in a row...
  605. drawer->setBitmapModulation(lastColor);
  606. }
  607. // drawTextN returns the rightmost X coord, so subtract leftmost coord to get the width
  608. strWidth -= globalStart.x;
  609. if (walkAcross->specialReference != -1) {
  610. Point2I lineStart = localStart;
  611. Point2I lineEnd = localStart;
  612. lineStart.y += mProfile->mFont->getBaseline() + 1;
  613. lineEnd.x += strWidth;
  614. lineEnd.y += mProfile->mFont->getBaseline() + 1;
  615. drawer->drawLine(localToGlobalCoord(lineStart),
  616. localToGlobalCoord(lineEnd),
  617. mSpecialColor);
  618. }
  619. localStart.x += strWidth;
  620. walkAcross = walkAcross->nextInLine;
  621. }
  622. currLine++;
  623. pElement = pElement->nextPhysicalLine;
  624. }
  625. }
  626. drawer->clearBitmapModulation();
  627. }
  628. }
  629. //--------------------------------------------------------------------------
  630. void GuiMessageVectorCtrl::inspectPostApply()
  631. {
  632. Parent::inspectPostApply();
  633. }
  634. //--------------------------------------------------------------------------
  635. void GuiMessageVectorCtrl::parentResized(const RectI& oldParentRect, const RectI& newParentRect)
  636. {
  637. Parent::parentResized(oldParentRect, newParentRect);
  638. // If we have a MesssageVector, detach/reattach so we can reflow the text.
  639. if (mMessageVector)
  640. {
  641. MessageVector *reflowme = mMessageVector;
  642. detach();
  643. attach(reflowme);
  644. }
  645. }
  646. //--------------------------------------------------------------------------
  647. void GuiMessageVectorCtrl::findSpecialFromCoord(const Point2I& point, S32* specialLine, S32* specialRef)
  648. {
  649. if (mLineElements.size() == 0) {
  650. *specialLine = -1;
  651. *specialRef = -1;
  652. return;
  653. }
  654. U32 linePixels = mProfile->mFont->getHeight() + mLineSpacingPixels;
  655. if ((point.x < 0 || point.x >= getWidth()) ||
  656. (point.y < 0 || point.y >= getHeight())) {
  657. *specialLine = -1;
  658. *specialRef = -1;
  659. return;
  660. }
  661. // Ok, we have real work to do here. Let's determine the physical line that it's on...
  662. U32 physLine = point.y / linePixels;
  663. AssertFatal(physLine >= 0, "Bad physical line!");
  664. // And now we find the LineElement that contains that physicalLine...
  665. U32 elemIndex;
  666. for (elemIndex = 0; elemIndex < mLineElements.size(); elemIndex++) {
  667. if (mLineElements[elemIndex].physicalLineStart > physLine) {
  668. // We've passed it.
  669. AssertFatal(elemIndex != 0, "Error, bad elemIndex, check assumptions.");
  670. elemIndex--;
  671. break;
  672. }
  673. }
  674. if (elemIndex == mLineElements.size()) {
  675. // On the last line...
  676. elemIndex = mLineElements.size() - 1;
  677. }
  678. TextElement* line = mLineElements[elemIndex].headLineElements;
  679. for (U32 i = 0; i < physLine - mLineElements[elemIndex].physicalLineStart; i++)
  680. {
  681. if (line->nextPhysicalLine == NULL)
  682. {
  683. *specialLine = -1;
  684. *specialRef = -1;
  685. return;
  686. }
  687. line = line->nextPhysicalLine;
  688. AssertFatal(line != NULL, "Error, moved too far!");
  689. }
  690. // Ok, line represents the current line. We now need to find out which textelement
  691. // the points x coord falls in.
  692. U32 currX = 0;
  693. if ((physLine - mLineElements[elemIndex].physicalLineStart) != 0) {
  694. currX = mLineContinuationIndent;
  695. // First, if this isn't the first line in this wrapping, we have to make sure
  696. // that the coord isn't in the margin...
  697. if (point.x < mLineContinuationIndent) {
  698. *specialLine = -1;
  699. *specialRef = -1;
  700. return;
  701. }
  702. }
  703. if (line->start > line->end) {
  704. // Empty line special case...
  705. *specialLine = -1;
  706. *specialRef = -1;
  707. return;
  708. }
  709. while (line) {
  710. U32 newX = currX + mProfile->mFont->getStrNWidth((const UTF8 *)mMessageVector->getLine(elemIndex).message,
  711. line->end - line->start + 1);
  712. if (point.x < newX) {
  713. // That's the one!
  714. *specialLine = elemIndex;
  715. *specialRef = line->specialReference;
  716. return;
  717. }
  718. currX = newX;
  719. line = line->nextInLine;
  720. }
  721. // Off to the right. Aw...
  722. *specialLine = -1;
  723. *specialRef = -1;
  724. }
  725. void GuiMessageVectorCtrl::onMouseDown(const GuiEvent& event)
  726. {
  727. Parent::onMouseDown(event);
  728. mouseUnlock();
  729. mMouseDown = true;
  730. // Find the special we are in, if any...
  731. findSpecialFromCoord(globalToLocalCoord(event.mousePoint),
  732. &mMouseSpecialLine, &mMouseSpecialRef);
  733. }
  734. void GuiMessageVectorCtrl::onMouseUp(const GuiEvent& event)
  735. {
  736. Parent::onMouseUp(event);
  737. mouseUnlock();
  738. // Is this an up from a dragged click?
  739. if (mMouseDown == false)
  740. return;
  741. // Find the special we are in, if any...
  742. S32 currSpecialLine;
  743. S32 currSpecialRef;
  744. findSpecialFromCoord(globalToLocalCoord(event.mousePoint), &currSpecialLine, &currSpecialRef);
  745. if (currSpecialRef != -1 &&
  746. (currSpecialLine == mMouseSpecialLine &&
  747. currSpecialRef == mMouseSpecialRef)) {
  748. // Execute the callback
  749. SpecialMarkers& rSpecial = mSpecialMarkers[currSpecialLine];
  750. S32 specialStart = rSpecial.specials[currSpecialRef].start;
  751. S32 specialEnd = rSpecial.specials[currSpecialRef].end;
  752. char* copyURL = new char[specialEnd - specialStart + 2];
  753. dStrncpy(copyURL, &mMessageVector->getLine(currSpecialLine).message[specialStart], specialEnd - specialStart + 1);
  754. copyURL[specialEnd - specialStart + 1] = '\0';
  755. Con::executef(this, "urlClickCallback", copyURL);
  756. delete [] copyURL;
  757. }
  758. mMouseDown = false;
  759. mMouseSpecialLine = -1;
  760. mMouseSpecialRef = -1;
  761. }