guiMessageVectorCtrl.cpp 33 KB

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