guiMessageVectorCtrl.cpp 32 KB

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