QuestGraph.cpp 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322
  1. #include "QuestGraph.h"
  2. #include <HashMap.h>
  3. #include <Image.h>
  4. #include <Scroll.h>
  5. #include <XML.h>
  6. #include "Globals.h"
  7. #include "Load.h"
  8. #include "World.h"
  9. QuestGraphElement::QuestGraphElement() {}
  10. bool QuestGraphElement::isApplicableFor(Framework::XML::Element& element)
  11. {
  12. return element.getName().isEqual("questGraph");
  13. }
  14. Framework::Drawable* QuestGraphElement::parseElement(
  15. Framework::XML::Element& element, Framework::UIMLContainer& generalFactory)
  16. {
  17. QuestGraph* graph = new QuestGraph();
  18. graph->collectionName = element.getAttributeValue("collectionName");
  19. graph->id = element.getAttributeValue("id");
  20. auto children = element.selectChildsByName("questGraphItem");
  21. Framework::HashMap<Framework::Text, QuestGraphItem*> map(
  22. 100, [](Framework::Text str) { return str.hashCode(); });
  23. // parse graph elements
  24. for (Framework::XML::Element* itemElement : children)
  25. {
  26. Framework::Drawable* parsed
  27. = generalFactory.parseElement(*itemElement, generalFactory);
  28. QuestGraphItem* item = dynamic_cast<QuestGraphItem*>(parsed);
  29. if (!item)
  30. {
  31. if (parsed)
  32. {
  33. parsed->release();
  34. }
  35. }
  36. else
  37. {
  38. map.put(item->getId(), item);
  39. }
  40. generalFactory.layout(*itemElement,
  41. *parsed,
  42. graph->getWidth(),
  43. graph->getHeight(),
  44. generalFactory);
  45. }
  46. // set connection references
  47. int connectionId = -1;
  48. for (MapEntry<Framework::Text, QuestGraphItem*> entry : map)
  49. {
  50. QuestGraphItem* item = entry.getValue();
  51. Framework::Text requirements = item->getRequirements();
  52. while (requirements.getLength() > 0)
  53. {
  54. connectionId++;
  55. Framework::Text* part = 0;
  56. if (requirements.has("||"))
  57. {
  58. part = requirements.getTeilText(
  59. 0, requirements.positionOf("||"));
  60. requirements.remove(0, requirements.positionOf("||") + 2);
  61. }
  62. else
  63. {
  64. part = new Framework::Text(requirements);
  65. requirements = "";
  66. }
  67. while (part->getLength() > 0)
  68. {
  69. Framework::Text* requirement;
  70. if (part->has("&&"))
  71. {
  72. requirement = part->getTeilText(0, part->positionOf("&&"));
  73. part->remove(0, part->positionOf("&&") + 2);
  74. }
  75. else
  76. {
  77. requirement = new Text(part->getText());
  78. part->setText("");
  79. }
  80. requirement->removeWhitespaceAfter(0);
  81. requirement->removeWhitespaceBefore(requirement->getLength());
  82. QuestGraphItem* requiredItem = map.get(*requirement);
  83. if (requiredItem)
  84. {
  85. requiredItem->addNextLayerRef({item, connectionId});
  86. item->addPreviousLayerRef({requiredItem, connectionId});
  87. }
  88. requirement->release();
  89. }
  90. part->release();
  91. }
  92. }
  93. // calculate layer index based on connections
  94. bool changed = 1;
  95. while (changed)
  96. {
  97. changed = 0;
  98. for (MapEntry<Framework::Text, QuestGraphItem*> entry : map)
  99. {
  100. changed |= entry.getValue()->calculateLaxerIndex();
  101. }
  102. }
  103. // add items to graph
  104. for (MapEntry<Framework::Text, QuestGraphItem*> entry : map)
  105. {
  106. if (entry.getValue()->getLayerIndex() < 0)
  107. {
  108. entry.getValue()->setVirtual(
  109. 1); // hide nodes witch layers could not be calculated because
  110. // of invalid circular connections
  111. }
  112. graph->addItem(entry.getValue());
  113. }
  114. graph->addVirtualConnectionNodes();
  115. graph->sortItems();
  116. graph->fixItemPositions();
  117. return graph;
  118. }
  119. bool QuestGraphElement::updateElement(Framework::XML::Element& element,
  120. Framework::Drawable& z,
  121. Framework::UIMLContainer& generalFactory)
  122. {
  123. return false;
  124. }
  125. void QuestGraphElement::layout(Framework::XML::Element& element,
  126. Framework::Drawable& z,
  127. int pWidth,
  128. int pHeight,
  129. Framework::UIMLContainer& generalLayouter)
  130. {
  131. UIMLElement::layout(element, z, pWidth, pHeight, generalLayouter);
  132. QuestGraph* graph = dynamic_cast<QuestGraph*>(&z);
  133. graph->centerVertically();
  134. }
  135. QuestGraphItemElement::QuestGraphItemElement() {}
  136. bool QuestGraphItemElement::isApplicableFor(Framework::XML::Element& element)
  137. {
  138. return element.getName().isEqual("questGraphItem");
  139. }
  140. Framework::Drawable* QuestGraphItemElement::parseElement(
  141. Framework::XML::Element& element, Framework::UIMLContainer& generalFactory)
  142. {
  143. QuestGraphItem* item = new QuestGraphItem();
  144. Framework::Text name = element.getAttributeValue("name");
  145. Framework::Text description = element.getAttributeValue("description");
  146. item->setToolTipText(name + "\n" + description,
  147. generalFactory.getFactory().initParam.bildschirm,
  148. generalFactory.getFactory().initParam.font);
  149. item->setBackgroundImageZ(loadImage(element.getAttributeValue("image")));
  150. item->finished = (int)element.getAttributeValue("finished") != 0;
  151. if (item->finished)
  152. {
  153. item->setBorderColor(0xFF55FF00);
  154. }
  155. item->rewarded = (int)element.getAttributeValue("rewarded") != 0;
  156. item->mainQuest = (int)element.getAttributeValue("mainQuest") != 0;
  157. if (item->mainQuest)
  158. {
  159. item->setBorderWidth(3);
  160. }
  161. item->id = element.getAttributeValue("id");
  162. item->requirements = element.getAttributeValue("requirements");
  163. item->virtualNode = 0;
  164. return item;
  165. }
  166. bool QuestGraphItemElement::updateElement(Framework::XML::Element& element,
  167. Framework::Drawable& z,
  168. Framework::UIMLContainer& generalFactory)
  169. {
  170. return false;
  171. }
  172. void QuestGraphItemElement::layout(Framework::XML::Element& element,
  173. Framework::Drawable& z,
  174. int pWidth,
  175. int pHeight,
  176. Framework::UIMLContainer& generalLayouter)
  177. {
  178. UIMLElement::layout(element, z, pWidth, pHeight, generalLayouter);
  179. }
  180. Connection::Connection(QuestGraphItem* zSource)
  181. : ReferenceCounter(),
  182. zSource(zSource),
  183. vx(0),
  184. minY(0),
  185. maxY(0)
  186. {}
  187. void Connection::renderVertical(Framework::Image& rObj)
  188. {
  189. if (targets.getEntryCount())
  190. {
  191. rObj.drawLineV(
  192. vx, minY, maxY - minY, isActive() ? 0xFFFFFFFF : 0xFF52525E);
  193. }
  194. }
  195. void Connection::renderHorizontal(Framework::Image& rObj)
  196. {
  197. if (targets.getEntryCount())
  198. {
  199. Framework::Point start = zSource->getPosition();
  200. start.x += zSource->getWidth();
  201. start.y += zSource->getHeight() / 2;
  202. rObj.drawLineH(start.x, start.y - 2, vx - start.x, 0xA0000000);
  203. rObj.drawLineH(start.x, start.y - 1, vx - start.x, 0xA0000000);
  204. rObj.drawLineH(start.x,
  205. start.y,
  206. vx - start.x,
  207. isActive() ? 0xFFFFFFFF : 0xFF52525E);
  208. rObj.drawLineH(start.x, start.y + 1, vx - start.x, 0xA0000000);
  209. rObj.drawLineH(start.x, start.y + 2, vx - start.x, 0xA0000000);
  210. for (const ConnectionTarget& target : targets)
  211. {
  212. Framework::Point end
  213. = target.zTarget->getTargetPosition(target.targetIndex);
  214. rObj.drawLineH(vx + 1, end.y - 2, end.x - vx - 1, 0xA0000000);
  215. rObj.drawLineH(vx + 1, end.y - 1, end.x - vx - 1, 0xA0000000);
  216. rObj.drawLineH(vx + 1,
  217. end.y,
  218. end.x - vx - 1,
  219. isActive() ? 0xFFFFFFFF : 0xFF52525E);
  220. rObj.drawLineH(vx + 1, end.y + 1, end.x - vx - 1, 0xA0000000);
  221. rObj.drawLineH(vx + 1, end.y + 2, end.x - vx - 1, 0xA0000000);
  222. }
  223. }
  224. }
  225. void Connection::setVx(int vx)
  226. {
  227. this->vx = vx;
  228. }
  229. void Connection::addConnectionTarget(ConnectionTarget target)
  230. {
  231. targets.add(target);
  232. }
  233. void Connection::setTargetIndex(AndNode* zTarget, int index)
  234. {
  235. for (auto target = targets.begin(); target; target++)
  236. {
  237. if (target.val().zTarget == zTarget)
  238. {
  239. target.set({index, target.val().zTarget});
  240. return;
  241. }
  242. }
  243. }
  244. void Connection::layout()
  245. {
  246. minY = 0;
  247. maxY = 0;
  248. bool start = 1;
  249. for (const ConnectionTarget& target : targets)
  250. {
  251. Framework::Point end
  252. = target.zTarget->getTargetPosition(target.targetIndex);
  253. if (start || end.y < minY)
  254. {
  255. minY = end.y;
  256. }
  257. if (start || end.y > maxY)
  258. {
  259. maxY = end.y;
  260. }
  261. start = 0;
  262. }
  263. Framework::Point origin = zSource->getPosition();
  264. origin.x += zSource->getWidth();
  265. origin.y += zSource->getHeight() / 2;
  266. if (minY > origin.y) minY = origin.y;
  267. if (maxY < origin.y) maxY = origin.y;
  268. maxY++;
  269. }
  270. bool Connection::isActive() const
  271. {
  272. return zSource->isFinished();
  273. }
  274. int Connection::getOrderNum() const
  275. {
  276. return zSource->getNodeIndex();
  277. }
  278. int Connection::getTargetCount() const
  279. {
  280. return targets.getEntryCount();
  281. }
  282. int Connection::getVerticalLength() const
  283. {
  284. return maxY - minY;
  285. }
  286. int Connection::getVx() const
  287. {
  288. return vx;
  289. }
  290. bool Connection::hasTarget(AndNode* zAndNode, int index) const
  291. {
  292. for (auto target : targets)
  293. {
  294. if (target.zTarget == zAndNode && target.targetIndex == index)
  295. {
  296. return true;
  297. }
  298. }
  299. return false;
  300. }
  301. AndNode::AndNode()
  302. : DrawableBackground()
  303. {
  304. setStyle(
  305. DrawableBackground::Style::Visible | DrawableBackground::Style::Border);
  306. setBorderWidth(1);
  307. setBorderColor(0xFF52525E);
  308. tr.setFontSize(12);
  309. tr.setFontZ(dynamic_cast<Font*>(uiFactory.initParam.font->getThis()));
  310. }
  311. void AndNode::addConnection(Connection* zConnection)
  312. {
  313. int pos = 0;
  314. for (Connection* other : connections)
  315. {
  316. if (other->getOrderNum() > zConnection->getOrderNum()) break;
  317. pos++;
  318. }
  319. zConnection->addConnectionTarget({pos, this});
  320. connections.add(zConnection, pos);
  321. pos = 0;
  322. for (Connection* con : connections)
  323. {
  324. con->setTargetIndex(this, pos);
  325. pos++;
  326. }
  327. }
  328. void AndNode::render(Framework::Image& rObj)
  329. {
  330. setBorderColor(isActive() ? 0xFFFFFFFF : 0xFF52525E);
  331. if (connections.getEntryCount() > 1)
  332. {
  333. DrawableBackground::render(rObj);
  334. if (rObj.setDrawOptions(pos.x + getBorderWidth(),
  335. pos.y + getBorderWidth(),
  336. getInnerWidth(),
  337. getInnerHeight()))
  338. {
  339. tr.renderText(getInnerWidth() / 2 - tr.getTextWidth("&") / 2,
  340. getInnerHeight() / 2 - tr.getTextHeight("&") / 2,
  341. "&",
  342. rObj,
  343. isActive() ? 0xFFFFFFFF : 0xFF52525E);
  344. rObj.releaseDrawOptions();
  345. }
  346. }
  347. else if (connections.getEntryCount() > 0)
  348. {
  349. if (rObj.setDrawOptions(pos, gr))
  350. {
  351. rObj.drawLineH(0,
  352. 0,
  353. gr.x,
  354. connections.get(0)->isActive() ? 0xFFFFFFFF : 0xFF52525E);
  355. rObj.releaseDrawOptions();
  356. }
  357. }
  358. }
  359. Framework::Point AndNode::getTargetPosition(int index)
  360. {
  361. if (connections.getEntryCount() == 1)
  362. {
  363. return pos;
  364. }
  365. return pos + Framework::Point(0, 10 + index * 11);
  366. }
  367. void AndNode::layout()
  368. {
  369. if (connections.getEntryCount() == 1)
  370. {
  371. setSize(20, 1);
  372. }
  373. else
  374. {
  375. setSize(20, 10 + connections.getEntryCount() * 11);
  376. }
  377. }
  378. bool AndNode::isActive() const
  379. {
  380. for (Connection* connection : connections)
  381. {
  382. if (!connection->isActive()) return false;
  383. }
  384. return connections.getEntryCount() > 0;
  385. }
  386. int AndNode::getConnectionCount() const
  387. {
  388. return connections.getEntryCount();
  389. }
  390. OrConnection::OrConnection(QuestGraphItem* zTarget,
  391. const Framework::Array<ConnectionInfo>& connections)
  392. : ReferenceCounter(),
  393. zTarget(zTarget),
  394. minY(0),
  395. maxY(0)
  396. {
  397. int currId = -1;
  398. AndNode* currNode = 0;
  399. for (const ConnectionInfo& info : connections)
  400. {
  401. if (info.id != currId)
  402. {
  403. currNode = new AndNode();
  404. andNodes.add(currNode);
  405. currId = info.id;
  406. }
  407. currNode->addConnection(info.target->zOutgoingConnection());
  408. }
  409. }
  410. void OrConnection::render(Framework::Image& rObj)
  411. {
  412. if (andNodes.getEntryCount() == 0) return;
  413. bool active = isActive();
  414. rObj.drawLineV(zTarget->getX() - 11,
  415. minY,
  416. maxY - minY,
  417. active ? 0xFFFFFFFF : 0xFF52525E);
  418. rObj.drawLineH(zTarget->getX() - 10,
  419. zTarget->getY() + zTarget->getHeight() / 2,
  420. 10,
  421. active ? 0xFFFFFFFF : 0xFF52525E);
  422. for (AndNode* node : andNodes)
  423. {
  424. rObj.drawLineH(node->getX() + node->getWidth(),
  425. node->getY() + node->getHeight() / 2,
  426. 10,
  427. active ? 0xFFFFFFFF : 0xFF52525E);
  428. node->render(rObj);
  429. }
  430. }
  431. void OrConnection::layout()
  432. {
  433. int nodeHeight = -10;
  434. for (AndNode* node : andNodes)
  435. {
  436. node->layout();
  437. nodeHeight += node->getHeight() + 10;
  438. }
  439. int y = zTarget->getY() + zTarget->getHeight() / 2 - nodeHeight / 2;
  440. bool start = 1;
  441. AndNode* last = 0;
  442. for (AndNode* node : andNodes)
  443. {
  444. node->setPosition(zTarget->getX() - 21 - node->getWidth(), y);
  445. if (start)
  446. {
  447. minY = y + node->getHeight() / 2;
  448. }
  449. y += node->getHeight() + 10;
  450. start = 0;
  451. last = node;
  452. }
  453. if (last)
  454. {
  455. y -= 10 + last->getHeight();
  456. maxY = y + last->getHeight() / 2 + 1;
  457. }
  458. else
  459. {
  460. maxY = minY + 1;
  461. }
  462. }
  463. int OrConnection::getNeededHeight() const
  464. {
  465. int minY = 0;
  466. int maxY = 0;
  467. bool first = 1;
  468. for (AndNode* node : andNodes)
  469. {
  470. if (first || node->getY() < minY)
  471. {
  472. minY = node->getY();
  473. }
  474. if (first || node->getY() + node->getHeight() > maxY)
  475. {
  476. maxY = node->getY() + node->getHeight();
  477. }
  478. first = 0;
  479. }
  480. return maxY - minY;
  481. }
  482. bool OrConnection::isActive() const
  483. {
  484. for (AndNode* node : andNodes)
  485. {
  486. if (node->isActive()) return 1;
  487. }
  488. return 0;
  489. }
  490. bool OrConnection::isHorizontalLineConflict(
  491. int y, Connection* zIgnoredIncommingConnection) const
  492. {
  493. for (AndNode* node : andNodes)
  494. {
  495. for (int i = 0; i < node->getConnectionCount(); i++)
  496. {
  497. if (!zIgnoredIncommingConnection
  498. || !zIgnoredIncommingConnection->hasTarget(node, i))
  499. {
  500. Point connection = node->getTargetPosition(i);
  501. if (abs(y - connection.y) < 5) return 1;
  502. }
  503. }
  504. }
  505. return 0;
  506. }
  507. QuestGraphItem::QuestGraphItem()
  508. : DrawableBackground(),
  509. layerIndex(-1),
  510. nodeIndex(-1),
  511. outgoingConnection(0),
  512. incommingConnection(0),
  513. finished(0),
  514. rewarded(0),
  515. mainQuest(0),
  516. virtualNode(0),
  517. animationProgress(0)
  518. {
  519. addStyle(
  520. DrawableBackground::Style::Visible | DrawableBackground::Style::Border
  521. | DrawableBackground::Style::Allowed | DrawableBackground::Style::BImage
  522. | DrawableBackground::Style::BImageScale
  523. | DrawableBackground::Style::BAlpha
  524. | DrawableBackground::Style::Background);
  525. setBorderColor(0xFF52525E);
  526. setBorderWidth(1);
  527. setMouseEvent(Framework::_ret1ME);
  528. }
  529. QuestGraphItem::~QuestGraphItem()
  530. {
  531. if (outgoingConnection) outgoingConnection->release();
  532. if (incommingConnection) incommingConnection->release();
  533. }
  534. bool QuestGraphItem::tick(double tickVal)
  535. {
  536. animationProgress += tickVal * 20;
  537. if (animationProgress > getWidth() * 2 + getHeight() * 2 - 4)
  538. {
  539. animationProgress -= getWidth() * 2 + getHeight() * 2 - 4;
  540. }
  541. rend = 1;
  542. return DrawableBackground::tick(tickVal);
  543. }
  544. int getBorderX(int p, int width, int height)
  545. {
  546. if (p < width)
  547. {
  548. return p;
  549. }
  550. else if (p < width + height - 1)
  551. {
  552. return width - 1;
  553. }
  554. else if (p < width * 2 + height - 2)
  555. {
  556. return width - (p - width - height + 2);
  557. }
  558. else
  559. {
  560. return 0;
  561. }
  562. }
  563. int getBorderY(int p, int width, int height)
  564. {
  565. if (p < width)
  566. {
  567. return 0;
  568. }
  569. else if (p < width + height - 1)
  570. {
  571. return p - width + 1;
  572. }
  573. else if (p < width * 2 + height - 2)
  574. {
  575. return height - 1;
  576. }
  577. else
  578. {
  579. return height - (p - width * 2 - height + 3);
  580. }
  581. }
  582. void QuestGraphItem::render(Framework::Image& rObj)
  583. {
  584. if (incommingConnection) incommingConnection->render(rObj);
  585. if (isVirtual())
  586. {
  587. rObj.drawLineH(
  588. pos.x, pos.y, gr.x, isFinished() ? 0xFFFFFFFF : 0xFF52525E);
  589. return;
  590. }
  591. DrawableBackground::render(rObj);
  592. if (finished && !rewarded)
  593. {
  594. if (rObj.setDrawOptions(pos.x, pos.y, gr.x, gr.y))
  595. {
  596. for (int i = 0; i < 7; i++)
  597. {
  598. int p
  599. = ((int)animationProgress + i) % (gr.x * 2 + gr.y * 2 - 4);
  600. rObj.setPixelDP(getBorderX(p, gr.x, gr.y),
  601. getBorderY(p, gr.x, gr.y),
  602. 0xFFFFFF00);
  603. rObj.setPixelDP(getBorderX(p, gr.x, gr.y) - 1,
  604. getBorderY(p, gr.x, gr.y),
  605. 0xFFFFFF00);
  606. rObj.setPixelDP(getBorderX(p, gr.x, gr.y),
  607. getBorderY(p, gr.x, gr.y) - 1,
  608. 0xFFFFFF00);
  609. rObj.setPixelDP(getBorderX(p, gr.x, gr.y) + 1,
  610. getBorderY(p, gr.x, gr.y),
  611. 0xFFFFFF00);
  612. rObj.setPixelDP(getBorderX(p, gr.x, gr.y),
  613. getBorderY(p, gr.x, gr.y) + 1,
  614. 0xFFFFFF00);
  615. p = ((int)animationProgress + i + gr.x - 1)
  616. % (gr.x * 2 + gr.y * 2 - 4);
  617. rObj.setPixelDP(getBorderX(p, gr.x, gr.y),
  618. getBorderY(p, gr.x, gr.y),
  619. 0xFFFFFF00);
  620. rObj.setPixelDP(getBorderX(p, gr.x, gr.y) - 1,
  621. getBorderY(p, gr.x, gr.y),
  622. 0xFFFFFF00);
  623. rObj.setPixelDP(getBorderX(p, gr.x, gr.y),
  624. getBorderY(p, gr.x, gr.y) - 1,
  625. 0xFFFFFF00);
  626. rObj.setPixelDP(getBorderX(p, gr.x, gr.y) + 1,
  627. getBorderY(p, gr.x, gr.y),
  628. 0xFFFFFF00);
  629. rObj.setPixelDP(getBorderX(p, gr.x, gr.y),
  630. getBorderY(p, gr.x, gr.y) + 1,
  631. 0xFFFFFF00);
  632. p = ((int)animationProgress + i + gr.x + gr.y - 2)
  633. % (gr.x * 2 + gr.y * 2 - 4);
  634. rObj.setPixelDP(getBorderX(p, gr.x, gr.y),
  635. getBorderY(p, gr.x, gr.y),
  636. 0xFFFFFF00);
  637. rObj.setPixelDP(getBorderX(p, gr.x, gr.y) - 1,
  638. getBorderY(p, gr.x, gr.y),
  639. 0xFFFFFF00);
  640. rObj.setPixelDP(getBorderX(p, gr.x, gr.y),
  641. getBorderY(p, gr.x, gr.y) - 1,
  642. 0xFFFFFF00);
  643. rObj.setPixelDP(getBorderX(p, gr.x, gr.y) + 1,
  644. getBorderY(p, gr.x, gr.y),
  645. 0xFFFFFF00);
  646. rObj.setPixelDP(getBorderX(p, gr.x, gr.y),
  647. getBorderY(p, gr.x, gr.y) + 1,
  648. 0xFFFFFF00);
  649. p = ((int)animationProgress + i + gr.x * 2 + gr.y - 3)
  650. % (gr.x * 2 + gr.y * 2 - 4);
  651. rObj.setPixelDP(getBorderX(p, gr.x, gr.y),
  652. getBorderY(p, gr.x, gr.y),
  653. 0xFFFFFF00);
  654. rObj.setPixelDP(getBorderX(p, gr.x, gr.y) - 1,
  655. getBorderY(p, gr.x, gr.y),
  656. 0xFFFFFF00);
  657. rObj.setPixelDP(getBorderX(p, gr.x, gr.y),
  658. getBorderY(p, gr.x, gr.y) - 1,
  659. 0xFFFFFF00);
  660. rObj.setPixelDP(getBorderX(p, gr.x, gr.y) + 1,
  661. getBorderY(p, gr.x, gr.y),
  662. 0xFFFFFF00);
  663. rObj.setPixelDP(getBorderX(p, gr.x, gr.y),
  664. getBorderY(p, gr.x, gr.y) + 1,
  665. 0xFFFFFF00);
  666. }
  667. rObj.releaseDrawOptions();
  668. }
  669. }
  670. }
  671. void QuestGraphItem::renderVerticalConnections(Framework::Image& rObj)
  672. {
  673. if (outgoingConnection) outgoingConnection->renderVertical(rObj);
  674. }
  675. void QuestGraphItem::renderHorizontalConnections(Framework::Image& rObj)
  676. {
  677. if (outgoingConnection) outgoingConnection->renderHorizontal(rObj);
  678. }
  679. void QuestGraphItem::replacePreviousConnections(
  680. QuestGraphItem* zBefore, QuestGraphItem* zAfter)
  681. {
  682. for (auto con = previousLayersConnections.begin(); con; con++)
  683. {
  684. if (con.val().target == zBefore) con.set({zAfter, con.val().id});
  685. }
  686. }
  687. const Framework::Text& QuestGraphItem::getId() const
  688. {
  689. return id;
  690. }
  691. const Framework::Text& QuestGraphItem::getRequirements() const
  692. {
  693. return requirements;
  694. }
  695. bool QuestGraphItem::calculateLaxerIndex()
  696. {
  697. int oldLayerIndex = layerIndex;
  698. int maxLayerIndex = -1;
  699. for (const ConnectionInfo& item : previousLayersConnections)
  700. {
  701. if (item.target->getLayerIndex() < 0) return 0;
  702. if (item.target->getLayerIndex() > maxLayerIndex)
  703. {
  704. maxLayerIndex = item.target->getLayerIndex();
  705. }
  706. }
  707. layerIndex = maxLayerIndex + 1;
  708. return oldLayerIndex != layerIndex;
  709. }
  710. int QuestGraphItem::getLayerIndex() const
  711. {
  712. return layerIndex;
  713. }
  714. void QuestGraphItem::setVirtual(bool virtualNode)
  715. {
  716. this->virtualNode = virtualNode;
  717. gr.y = virtualNode ? 1 : gr.y;
  718. gr.x = 1;
  719. setStyle(DrawableBackground::Style::Visible, virtualNode);
  720. }
  721. void QuestGraphItem::setNodeIndex(int index)
  722. {
  723. this->nodeIndex = index;
  724. }
  725. void QuestGraphItem::addNextLayerRef(ConnectionInfo zItem)
  726. {
  727. nextLayersConnections.add(zItem);
  728. }
  729. void QuestGraphItem::addPreviousLayerRef(ConnectionInfo zItem)
  730. {
  731. previousLayersConnections.add(zItem);
  732. }
  733. void QuestGraphItem::initializeConnections()
  734. {
  735. outgoingConnection = new Connection(this);
  736. incommingConnection = new OrConnection(this, previousLayersConnections);
  737. }
  738. const Framework::Array<ConnectionInfo>&
  739. QuestGraphItem::getNextLayersConnections() const
  740. {
  741. return nextLayersConnections;
  742. }
  743. int QuestGraphItem::getNodeIndex() const
  744. {
  745. return nodeIndex;
  746. }
  747. bool QuestGraphItem::isMainQuest() const
  748. {
  749. return mainQuest;
  750. }
  751. bool QuestGraphItem::isVirtual() const
  752. {
  753. return virtualNode;
  754. }
  755. bool QuestGraphItem::isFinished() const
  756. {
  757. return virtualNode ? incommingConnection->isActive() : finished;
  758. }
  759. Connection* QuestGraphItem::zOutgoingConnection() const
  760. {
  761. return outgoingConnection;
  762. }
  763. OrConnection* QuestGraphItem::zIncommingConnection() const
  764. {
  765. return incommingConnection;
  766. }
  767. QuestGraphItemLayer::QuestGraphItemLayer()
  768. : ReferenceCounter()
  769. {}
  770. bool QuestGraphItemLayer::tick(double tickVal)
  771. {
  772. bool result = 0;
  773. for (QuestGraphItem* item : items)
  774. {
  775. result |= item->tick(tickVal);
  776. }
  777. return result;
  778. }
  779. void QuestGraphItemLayer::render(Framework::Image& rObj)
  780. {
  781. for (QuestGraphItem* item : items)
  782. {
  783. item->render(rObj);
  784. item->renderVerticalConnections(rObj);
  785. }
  786. for (QuestGraphItem* item : items)
  787. {
  788. item->renderHorizontalConnections(rObj);
  789. }
  790. }
  791. void QuestGraphItemLayer::doMouseEvent(Framework::MouseEvent& me)
  792. {
  793. for (QuestGraphItem* item : items)
  794. {
  795. item->doPublicMouseEvent(me);
  796. }
  797. }
  798. void QuestGraphItemLayer::addItem(QuestGraphItem* item)
  799. {
  800. items.add(item);
  801. item->setNodeIndex(items.getEntryCount() - 1);
  802. }
  803. bool QuestGraphItemLayer::sortItems()
  804. {
  805. bool changed = 0;
  806. for (int index = 0; index < items.getEntryCount() - 1; index++)
  807. {
  808. QuestGraphItem* current = items.z(index);
  809. int conflicts = 0;
  810. int afterSwapConflicts = 0;
  811. QuestGraphItem* other = items.z(index + 1);
  812. for (const ConnectionInfo& connection :
  813. current->getNextLayersConnections())
  814. {
  815. for (const ConnectionInfo& otherConnection :
  816. other->getNextLayersConnections())
  817. {
  818. if ((otherConnection.target->getNodeIndex()
  819. < connection.target->getNodeIndex()))
  820. {
  821. conflicts++;
  822. }
  823. if ((otherConnection.target->getNodeIndex()
  824. > connection.target->getNodeIndex()))
  825. {
  826. afterSwapConflicts++;
  827. }
  828. }
  829. }
  830. if (conflicts > afterSwapConflicts)
  831. {
  832. // move node down
  833. QuestGraphItem* after = items.z(index + 1);
  834. after->setNodeIndex(index);
  835. current->setNodeIndex(index + 1);
  836. items.swap(index, index + 1);
  837. changed = 1;
  838. }
  839. }
  840. return changed;
  841. }
  842. int QuestGraphItemLayer::fixItemPositions(int x)
  843. {
  844. // calculate size needed for & nodes and | nodes
  845. int maxWidth = 0;
  846. for (QuestGraphItem* item : items)
  847. {
  848. item->initializeConnections();
  849. item->zIncommingConnection()->layout();
  850. if (item->getWidth() > maxWidth)
  851. {
  852. maxWidth = item->getWidth();
  853. }
  854. }
  855. x += 20 + 21; // ofset for incomming connections
  856. int y = 0;
  857. // calculate y positions of nodes
  858. for (QuestGraphItem* item : items)
  859. {
  860. int height = 0;
  861. int lastId = -1;
  862. int nodeHeight = item->zIncommingConnection()->getNeededHeight();
  863. if (nodeHeight < item->getHeight()) nodeHeight = item->getHeight();
  864. item->setPosition(x + maxWidth / 2 - item->getWidth() / 2,
  865. y + nodeHeight / 2 - item->getHeight() / 2);
  866. y += nodeHeight + 20;
  867. }
  868. x += maxWidth; // this layers node size
  869. for (QuestGraphItem* item : items)
  870. {
  871. item->zIncommingConnection()->layout();
  872. item->zOutgoingConnection()->setVx(x);
  873. item->zOutgoingConnection()->layout();
  874. }
  875. x += items.getEntryCount() * 11 + 10; // offset for outgoing connections
  876. return x + 20; // min space between layers
  877. }
  878. void QuestGraphItemLayer::sortConnections()
  879. {
  880. for (QuestGraphItem* item : items)
  881. {
  882. item->zIncommingConnection()->layout();
  883. item->zOutgoingConnection()->layout();
  884. }
  885. int connectionCount = 0;
  886. bool* sorted = new bool[items.getEntryCount()];
  887. memset(sorted, 0, sizeof(bool) * items.getEntryCount());
  888. int sortedCount = 0;
  889. while (true)
  890. {
  891. int minHeight = -1;
  892. QuestGraphItem* next = 0;
  893. for (QuestGraphItem* item : items)
  894. {
  895. if (sorted[item->getNodeIndex()]) continue;
  896. if (item->zOutgoingConnection()->getTargetCount()
  897. == connectionCount)
  898. {
  899. if (minHeight < 0
  900. || item->zOutgoingConnection()->getVerticalLength()
  901. < minHeight)
  902. {
  903. minHeight
  904. = item->zOutgoingConnection()->getVerticalLength();
  905. next = item;
  906. }
  907. }
  908. }
  909. if (!next)
  910. {
  911. if (sortedCount < items.getEntryCount())
  912. {
  913. connectionCount++;
  914. }
  915. else
  916. {
  917. break;
  918. }
  919. }
  920. else
  921. {
  922. next->zOutgoingConnection()->setVx(
  923. next->zOutgoingConnection()->getVx() + 10 + sortedCount * 11);
  924. sorted[next->getNodeIndex()] = 1;
  925. sortedCount++;
  926. }
  927. }
  928. delete[] sorted;
  929. }
  930. const Framework::RCArray<QuestGraphItem>& QuestGraphItemLayer::getItems() const
  931. {
  932. return items;
  933. }
  934. int QuestGraphItemLayer::getLeyerHeight(int& minY) const
  935. {
  936. minY = 0;
  937. int end = 0;
  938. bool first = 1;
  939. for (QuestGraphItem* item : items)
  940. {
  941. if (first || item->getY() < minY) minY = item->getY();
  942. if (first || item->getY() + item->getHeight() > end)
  943. end = item->getY() + item->getHeight();
  944. first = 0;
  945. }
  946. return end - minY;
  947. }
  948. int QuestGraphItemLayer::getLeyerHeight() const
  949. {
  950. int y;
  951. return getLeyerHeight(y);
  952. }
  953. void QuestGraphItemLayer::centerVertically(int pos)
  954. {
  955. int minY = 0;
  956. int height = getLeyerHeight(minY);
  957. int yOffset = pos - height / 2 - minY;
  958. for (QuestGraphItem* item : items)
  959. {
  960. item->setPosition(item->getX(), item->getY() + yOffset);
  961. item->zIncommingConnection()->layout();
  962. item->zOutgoingConnection()->layout();
  963. }
  964. }
  965. void QuestGraphItemLayer::resolveHorizontalConflicts(
  966. const QuestGraphItemLayer* zNextLayer)
  967. {
  968. for (QuestGraphItem* item : items)
  969. {
  970. int outgoingY = item->getY() + item->getHeight() / 2;
  971. if (zNextLayer->isHorizontalLineConflict(
  972. outgoingY, item->zOutgoingConnection()))
  973. {
  974. int offset = 0;
  975. for (int i = 1; i <= 10; i++)
  976. {
  977. if (!zNextLayer->isHorizontalLineConflict(
  978. outgoingY - i, item->zOutgoingConnection()))
  979. {
  980. offset = -i;
  981. break;
  982. }
  983. if (!zNextLayer->isHorizontalLineConflict(
  984. outgoingY + i, item->zOutgoingConnection()))
  985. {
  986. offset = i;
  987. break;
  988. }
  989. }
  990. item->setPosition(item->getX(), item->getY() + offset);
  991. item->zIncommingConnection()->layout();
  992. item->zOutgoingConnection()->layout();
  993. }
  994. }
  995. }
  996. bool QuestGraphItemLayer::isHorizontalLineConflict(
  997. int y, Connection* zIgnoredIncommingConnection) const
  998. {
  999. for (QuestGraphItem* item : items)
  1000. {
  1001. if (item->zIncommingConnection()->isHorizontalLineConflict(
  1002. y, zIgnoredIncommingConnection))
  1003. return 1;
  1004. }
  1005. return 0;
  1006. }
  1007. QuestGraph::QuestGraph()
  1008. : DrawableBackground()
  1009. {
  1010. addStyle(DrawableBackground::Style::Visible
  1011. | DrawableBackground::Style::Allowed
  1012. | DrawableBackground::Style::Border);
  1013. setBorderWidth(1);
  1014. setBorderColor(0xFF52525E);
  1015. setMouseEvent(Framework::_ret1ME);
  1016. }
  1017. bool QuestGraph::tick(double tickVal)
  1018. {
  1019. for (QuestGraphItemLayer* layer : layers)
  1020. {
  1021. rend |= layer->tick(tickVal);
  1022. }
  1023. return DrawableBackground::tick(tickVal);
  1024. }
  1025. void QuestGraph::render(Framework::Image& rObj)
  1026. {
  1027. if (hasStyle(DrawableBackground::Style::Visible)
  1028. && layers.getEntryCount() > 0)
  1029. {
  1030. DrawableBackground::render(rObj);
  1031. if (rObj.setDrawOptions(pos, gr))
  1032. {
  1033. if (rObj.setDrawOptions(getBorderWidth(),
  1034. getBorderWidth(),
  1035. getInnerWidth(),
  1036. getInnerHeight()))
  1037. {
  1038. int hScrollOffset = 0;
  1039. if (hasStyle(DrawableBackground::Style::HScroll)
  1040. && horizontalScrollBar)
  1041. {
  1042. hScrollOffset = horizontalScrollBar->getScroll();
  1043. }
  1044. int vScrollOffset = 0;
  1045. if (hasStyle(DrawableBackground::Style::VScroll)
  1046. && vertikalScrollBar)
  1047. {
  1048. vScrollOffset = vertikalScrollBar->getScroll();
  1049. }
  1050. if (hScrollOffset || vScrollOffset)
  1051. {
  1052. rObj.addScrollOffset(hScrollOffset, vScrollOffset);
  1053. }
  1054. for (QuestGraphItemLayer* layer : layers)
  1055. {
  1056. layer->render(rObj);
  1057. }
  1058. rObj.releaseDrawOptions();
  1059. }
  1060. rObj.releaseDrawOptions();
  1061. }
  1062. }
  1063. }
  1064. void QuestGraph::doMouseEvent(Framework::MouseEvent& me, bool userRet)
  1065. {
  1066. userRet &= hasStyle(DrawableBackground::Style::Visible);
  1067. bool vera = me.processed;
  1068. if (!userRet)
  1069. {
  1070. me.processed = 1;
  1071. }
  1072. for (QuestGraphItemLayer* layer : layers)
  1073. {
  1074. layer->doMouseEvent(me);
  1075. }
  1076. if (!userRet)
  1077. {
  1078. me.processed = vera;
  1079. }
  1080. }
  1081. void QuestGraph::addVirtualConnectionNodes()
  1082. {
  1083. int layerIndex = 0;
  1084. // add virtual items for connections that do not go directly to the next
  1085. // layer after this operation each node only have connections to the
  1086. // immediate following layer
  1087. for (QuestGraphItemLayer* layer : layers)
  1088. {
  1089. for (int i = 0; i < layer->getItems().getEntryCount(); i++)
  1090. {
  1091. QuestGraphItem* item = layer->getItems().z(i);
  1092. auto iterator = item->getNextLayersConnections().begin();
  1093. QuestGraphItem* virtualItem = 0;
  1094. while (iterator)
  1095. {
  1096. if (iterator.val().target->getLayerIndex() > layerIndex + 1)
  1097. {
  1098. if (!virtualItem)
  1099. {
  1100. virtualItem = new QuestGraphItem();
  1101. virtualItem->addPreviousLayerRef(
  1102. {item, iterator.val().id});
  1103. virtualItem->addNextLayerRef(
  1104. {iterator.val().target, iterator.val().id});
  1105. iterator.val().target->replacePreviousConnections(
  1106. item, virtualItem);
  1107. virtualItem->setVirtual(true);
  1108. virtualItem->calculateLaxerIndex();
  1109. iterator.set({virtualItem, iterator.val().id});
  1110. iterator++;
  1111. }
  1112. else
  1113. {
  1114. virtualItem->addNextLayerRef(
  1115. {iterator.val().target, iterator.val().id});
  1116. iterator.val().target->replacePreviousConnections(
  1117. item, virtualItem);
  1118. iterator.remove();
  1119. }
  1120. }
  1121. else if (iterator.val().target->getLayerIndex() < 0)
  1122. { // remove connections to invalid nodes
  1123. iterator.remove();
  1124. }
  1125. else
  1126. {
  1127. iterator++;
  1128. }
  1129. }
  1130. if (virtualItem)
  1131. {
  1132. addItem(virtualItem);
  1133. }
  1134. }
  1135. layerIndex++;
  1136. }
  1137. }
  1138. void QuestGraph::sortItems()
  1139. {
  1140. bool changed = 1;
  1141. while (changed)
  1142. {
  1143. changed = 0;
  1144. for (QuestGraphItemLayer* layer : layers)
  1145. {
  1146. changed |= layer->sortItems();
  1147. }
  1148. }
  1149. }
  1150. void QuestGraph::fixItemPositions()
  1151. {
  1152. int height = 0;
  1153. int x = 0;
  1154. for (QuestGraphItemLayer* layer : layers)
  1155. {
  1156. x = layer->fixItemPositions(x);
  1157. if (layer->getLeyerHeight() > height)
  1158. {
  1159. height = layer->getLeyerHeight();
  1160. }
  1161. }
  1162. for (QuestGraphItemLayer* layer : layers)
  1163. {
  1164. layer->sortConnections();
  1165. }
  1166. if (height > getInnerHeight())
  1167. {
  1168. addStyle(DrawableBackground::Style::VScroll);
  1169. setVerticalClickScroll(10);
  1170. vertikalScrollBar->update(height, getInnerHeight());
  1171. }
  1172. if (layers.getEntryCount() * 150 - 80 > getInnerWidth())
  1173. {
  1174. addStyle(DrawableBackground::Style::HScroll);
  1175. setHorizontalClickScroll(10);
  1176. horizontalScrollBar->update(
  1177. layers.getEntryCount() * 150 - 80, getInnerWidth());
  1178. }
  1179. QuestGraphItemLayer* last = 0;
  1180. for (int i = layers.getEntryCount() - 1; i >= 0; i--)
  1181. {
  1182. QuestGraphItemLayer* layer = layers.z(i);
  1183. layer->centerVertically(getHeight() / 2);
  1184. if (last) layer->resolveHorizontalConflicts(last);
  1185. last = layer;
  1186. }
  1187. }
  1188. void QuestGraph::centerVertically()
  1189. {
  1190. int height = 0;
  1191. for (QuestGraphItemLayer* layer : layers)
  1192. {
  1193. if (layer->getLeyerHeight() > height)
  1194. {
  1195. height = layer->getLeyerHeight();
  1196. }
  1197. }
  1198. if (height > getInnerHeight())
  1199. {
  1200. addStyle(DrawableBackground::Style::VScroll);
  1201. setVerticalClickScroll(10);
  1202. vertikalScrollBar->update(height, getInnerHeight());
  1203. }
  1204. if (layers.getEntryCount() * 150 - 80 > getInnerWidth())
  1205. {
  1206. addStyle(DrawableBackground::Style::HScroll);
  1207. setHorizontalClickScroll(10);
  1208. horizontalScrollBar->update(
  1209. layers.getEntryCount() * 150 - 80, getInnerWidth());
  1210. }
  1211. QuestGraphItemLayer* last = 0;
  1212. for (int i = layers.getEntryCount() - 1; i >= 0; i--)
  1213. {
  1214. QuestGraphItemLayer* layer = layers.z(i);
  1215. layer->centerVertically(getHeight() / 2);
  1216. if (last) layer->resolveHorizontalConflicts(last);
  1217. last = layer;
  1218. }
  1219. }
  1220. void QuestGraph::addItem(QuestGraphItem* item)
  1221. {
  1222. int layerInex = item->getLayerIndex();
  1223. if (layerInex >= 0)
  1224. {
  1225. while (layerInex >= layers.getEntryCount())
  1226. {
  1227. layers.add(new QuestGraphItemLayer());
  1228. }
  1229. layers.z(layerInex)->addItem(item);
  1230. }
  1231. else
  1232. {
  1233. invalidNodes.addItem(item);
  1234. }
  1235. }