guiMessageVectorCtrl.cpp 32 KB

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