QuestGraph.cpp 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323
  1. #include "QuestGraph.h"
  2. #include <Bild.h>
  3. #include <HashMap.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().istGleich("questGraph");
  13. }
  14. Framework::Zeichnung* 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::Zeichnung* 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->getBreite(),
  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.hat("||"))
  57. {
  58. part = requirements.getTeilText(
  59. 0, requirements.positionVon("||"));
  60. requirements.remove(0, requirements.positionVon("||") + 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->hat("&&"))
  71. {
  72. requirement = part->getTeilText(0, part->positionVon("&&"));
  73. part->remove(0, part->positionVon("&&") + 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::Zeichnung& z,
  121. Framework::UIMLContainer& generalFactory)
  122. {
  123. return false;
  124. }
  125. void QuestGraphElement::layout(Framework::XML::Element& element,
  126. Framework::Zeichnung& 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().istGleich("questGraphItem");
  139. }
  140. Framework::Zeichnung* 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.schrift);
  149. item->setHintergrundBildZ(loadImage(element.getAttributeValue("image")));
  150. item->finished = (int)element.getAttributeValue("finished") != 0;
  151. if (item->finished)
  152. {
  153. item->setRahmenFarbe(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->setRahmenBreite(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::Zeichnung& z,
  168. Framework::UIMLContainer& generalFactory)
  169. {
  170. return false;
  171. }
  172. void QuestGraphItemElement::layout(Framework::XML::Element& element,
  173. Framework::Zeichnung& 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::Bild& rObj)
  188. {
  189. if (targets.getEintragAnzahl())
  190. {
  191. rObj.drawLinieV(
  192. vx, minY, maxY - minY, isActive() ? 0xFFFFFFFF : 0xFF52525E);
  193. }
  194. }
  195. void Connection::renderHorizontal(Framework::Bild& rObj)
  196. {
  197. if (targets.getEintragAnzahl())
  198. {
  199. Framework::Punkt start = zSource->getPosition();
  200. start.x += zSource->getBreite();
  201. start.y += zSource->getHeight() / 2;
  202. rObj.drawLinieH(start.x, start.y - 2, vx - start.x, 0xA0000000);
  203. rObj.drawLinieH(start.x, start.y - 1, vx - start.x, 0xA0000000);
  204. rObj.drawLinieH(start.x,
  205. start.y,
  206. vx - start.x,
  207. isActive() ? 0xFFFFFFFF : 0xFF52525E);
  208. rObj.drawLinieH(start.x, start.y + 1, vx - start.x, 0xA0000000);
  209. rObj.drawLinieH(start.x, start.y + 2, vx - start.x, 0xA0000000);
  210. for (const ConnectionTarget& target : targets)
  211. {
  212. Framework::Punkt end
  213. = target.zTarget->getTargetPosition(target.targetIndex);
  214. rObj.drawLinieH(vx + 1, end.y - 2, end.x - vx - 1, 0xA0000000);
  215. rObj.drawLinieH(vx + 1, end.y - 1, end.x - vx - 1, 0xA0000000);
  216. rObj.drawLinieH(vx + 1,
  217. end.y,
  218. end.x - vx - 1,
  219. isActive() ? 0xFFFFFFFF : 0xFF52525E);
  220. rObj.drawLinieH(vx + 1, end.y + 1, end.x - vx - 1, 0xA0000000);
  221. rObj.drawLinieH(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::Punkt 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::Punkt origin = zSource->getPosition();
  264. origin.x += zSource->getBreite();
  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.getEintragAnzahl();
  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. : ZeichnungHintergrund()
  303. {
  304. setStyle(ZeichnungHintergrund::Style::Sichtbar
  305. | ZeichnungHintergrund::Style::Rahmen);
  306. setRahmenBreite(1);
  307. setRahmenFarbe(0xFF52525E);
  308. tr.setSchriftSize(12);
  309. tr.setSchriftZ(
  310. dynamic_cast<Schrift*>(uiFactory.initParam.schrift->getThis()));
  311. }
  312. void AndNode::addConnection(Connection* zConnection)
  313. {
  314. int pos = 0;
  315. for (Connection* other : connections)
  316. {
  317. if (other->getOrderNum() > zConnection->getOrderNum()) break;
  318. pos++;
  319. }
  320. zConnection->addConnectionTarget({pos, this});
  321. connections.add(zConnection, pos);
  322. pos = 0;
  323. for (Connection* con : connections)
  324. {
  325. con->setTargetIndex(this, pos);
  326. pos++;
  327. }
  328. }
  329. void AndNode::render(Framework::Bild& rObj)
  330. {
  331. setRahmenFarbe(isActive() ? 0xFFFFFFFF : 0xFF52525E);
  332. if (connections.getEintragAnzahl() > 1)
  333. {
  334. ZeichnungHintergrund::render(rObj);
  335. if (rObj.setDrawOptions(pos.x + getRahmenBreite(),
  336. pos.y + getRahmenBreite(),
  337. getInnenBreite(),
  338. getInnenHeight()))
  339. {
  340. tr.renderText(getInnenBreite() / 2 - tr.getTextBreite("&") / 2,
  341. getInnenHeight() / 2 - tr.getTextHeight("&") / 2,
  342. "&",
  343. rObj,
  344. isActive() ? 0xFFFFFFFF : 0xFF52525E);
  345. rObj.releaseDrawOptions();
  346. }
  347. }
  348. else if (connections.getEintragAnzahl() > 0)
  349. {
  350. if (rObj.setDrawOptions(pos, gr))
  351. {
  352. rObj.drawLinieH(0,
  353. 0,
  354. gr.x,
  355. connections.get(0)->isActive() ? 0xFFFFFFFF : 0xFF52525E);
  356. rObj.releaseDrawOptions();
  357. }
  358. }
  359. }
  360. Framework::Punkt AndNode::getTargetPosition(int index)
  361. {
  362. if (connections.getEintragAnzahl() == 1)
  363. {
  364. return pos;
  365. }
  366. return pos + Framework::Punkt(0, 10 + index * 11);
  367. }
  368. void AndNode::layout()
  369. {
  370. if (connections.getEintragAnzahl() == 1)
  371. {
  372. setSize(20, 1);
  373. }
  374. else
  375. {
  376. setSize(20, 10 + connections.getEintragAnzahl() * 11);
  377. }
  378. }
  379. bool AndNode::isActive() const
  380. {
  381. for (Connection* connection : connections)
  382. {
  383. if (!connection->isActive()) return false;
  384. }
  385. return connections.getEintragAnzahl() > 0;
  386. }
  387. int AndNode::getConnectionCount() const
  388. {
  389. return connections.getEintragAnzahl();
  390. }
  391. OrConnection::OrConnection(QuestGraphItem* zTarget,
  392. const Framework::Array<ConnectionInfo>& connections)
  393. : ReferenceCounter(),
  394. zTarget(zTarget),
  395. minY(0),
  396. maxY(0)
  397. {
  398. int currId = -1;
  399. AndNode* currNode = 0;
  400. for (const ConnectionInfo& info : connections)
  401. {
  402. if (info.id != currId)
  403. {
  404. currNode = new AndNode();
  405. andNodes.add(currNode);
  406. currId = info.id;
  407. }
  408. currNode->addConnection(info.target->zOutgoingConnection());
  409. }
  410. }
  411. void OrConnection::render(Framework::Bild& rObj)
  412. {
  413. if (andNodes.getEintragAnzahl() == 0) return;
  414. bool active = isActive();
  415. rObj.drawLinieV(zTarget->getX() - 11,
  416. minY,
  417. maxY - minY,
  418. active ? 0xFFFFFFFF : 0xFF52525E);
  419. rObj.drawLinieH(zTarget->getX() - 10,
  420. zTarget->getY() + zTarget->getHeight() / 2,
  421. 10,
  422. active ? 0xFFFFFFFF : 0xFF52525E);
  423. for (AndNode* node : andNodes)
  424. {
  425. rObj.drawLinieH(node->getX() + node->getBreite(),
  426. node->getY() + node->getHeight() / 2,
  427. 10,
  428. active ? 0xFFFFFFFF : 0xFF52525E);
  429. node->render(rObj);
  430. }
  431. }
  432. void OrConnection::layout()
  433. {
  434. int nodeHeight = -10;
  435. for (AndNode* node : andNodes)
  436. {
  437. node->layout();
  438. nodeHeight += node->getHeight() + 10;
  439. }
  440. int y = zTarget->getY() + zTarget->getHeight() / 2 - nodeHeight / 2;
  441. bool start = 1;
  442. AndNode* last = 0;
  443. for (AndNode* node : andNodes)
  444. {
  445. node->setPosition(zTarget->getX() - 21 - node->getBreite(), y);
  446. if (start)
  447. {
  448. minY = y + node->getHeight() / 2;
  449. }
  450. y += node->getHeight() + 10;
  451. start = 0;
  452. last = node;
  453. }
  454. if (last)
  455. {
  456. y -= 10 + last->getHeight();
  457. maxY = y + last->getHeight() / 2 + 1;
  458. }
  459. else
  460. {
  461. maxY = minY + 1;
  462. }
  463. }
  464. int OrConnection::getNeededHeight() const
  465. {
  466. int minY = 0;
  467. int maxY = 0;
  468. bool first = 1;
  469. for (AndNode* node : andNodes)
  470. {
  471. if (first || node->getY() < minY)
  472. {
  473. minY = node->getY();
  474. }
  475. if (first || node->getY() + node->getHeight() > maxY)
  476. {
  477. maxY = node->getY() + node->getHeight();
  478. }
  479. first = 0;
  480. }
  481. return maxY - minY;
  482. }
  483. bool OrConnection::isActive() const
  484. {
  485. for (AndNode* node : andNodes)
  486. {
  487. if (node->isActive()) return 1;
  488. }
  489. return 0;
  490. }
  491. bool OrConnection::isHorizontalLineConflict(
  492. int y, Connection* zIgnoredIncommingConnection) const
  493. {
  494. for (AndNode* node : andNodes)
  495. {
  496. for (int i = 0; i < node->getConnectionCount(); i++)
  497. {
  498. if (!zIgnoredIncommingConnection || !zIgnoredIncommingConnection
  499. ->hasTarget(node, i))
  500. {
  501. Punkt connection = node->getTargetPosition(i);
  502. if (abs(y - connection.y) < 5) return 1;
  503. }
  504. }
  505. }
  506. return 0;
  507. }
  508. QuestGraphItem::QuestGraphItem()
  509. : ZeichnungHintergrund(),
  510. layerIndex(-1),
  511. nodeIndex(-1),
  512. outgoingConnection(0),
  513. incommingConnection(0),
  514. finished(0),
  515. rewarded(0),
  516. mainQuest(0),
  517. virtualNode(0),
  518. animationProgress(0)
  519. {
  520. addStyle(ZeichnungHintergrund::Style::Sichtbar
  521. | ZeichnungHintergrund::Style::Rahmen
  522. | ZeichnungHintergrund::Style::Erlaubt
  523. | ZeichnungHintergrund::Style::HBild
  524. | ZeichnungHintergrund::Style::HBildScale
  525. | ZeichnungHintergrund::Style::HAlpha
  526. | ZeichnungHintergrund::Style::Hintergrund);
  527. setRahmenFarbe(0xFF52525E);
  528. setRahmenBreite(1);
  529. setMausEreignis(Framework::_ret1ME);
  530. }
  531. QuestGraphItem::~QuestGraphItem()
  532. {
  533. if (outgoingConnection) outgoingConnection->release();
  534. if (incommingConnection) incommingConnection->release();
  535. }
  536. bool QuestGraphItem::tick(double tickVal)
  537. {
  538. animationProgress += tickVal * 20;
  539. if (animationProgress > getBreite() * 2 + getHeight() * 2 - 4)
  540. {
  541. animationProgress -= getBreite() * 2 + getHeight() * 2 - 4;
  542. }
  543. rend = 1;
  544. return ZeichnungHintergrund::tick(tickVal);
  545. }
  546. int getBorderX(int p, int width, int height)
  547. {
  548. if (p < width)
  549. {
  550. return p;
  551. }
  552. else if (p < width + height - 1)
  553. {
  554. return width - 1;
  555. }
  556. else if (p < width * 2 + height - 2)
  557. {
  558. return width - (p - width - height + 2);
  559. }
  560. else
  561. {
  562. return 0;
  563. }
  564. }
  565. int getBorderY(int p, int width, int height)
  566. {
  567. if (p < width)
  568. {
  569. return 0;
  570. }
  571. else if (p < width + height - 1)
  572. {
  573. return p - width + 1;
  574. }
  575. else if (p < width * 2 + height - 2)
  576. {
  577. return height - 1;
  578. }
  579. else
  580. {
  581. return height - (p - width * 2 - height + 3);
  582. }
  583. }
  584. void QuestGraphItem::render(Framework::Bild& rObj)
  585. {
  586. if (incommingConnection) incommingConnection->render(rObj);
  587. if (isVirtual())
  588. {
  589. rObj.drawLinieH(
  590. pos.x, pos.y, gr.x, isFinished() ? 0xFFFFFFFF : 0xFF52525E);
  591. return;
  592. }
  593. ZeichnungHintergrund::render(rObj);
  594. if (finished && !rewarded)
  595. {
  596. if (rObj.setDrawOptions(pos.x, pos.y, gr.x, gr.y))
  597. {
  598. for (int i = 0; i < 7; i++)
  599. {
  600. int p
  601. = ((int)animationProgress + i) % (gr.x * 2 + gr.y * 2 - 4);
  602. rObj.setPixelDP(getBorderX(p, gr.x, gr.y),
  603. getBorderY(p, gr.x, gr.y),
  604. 0xFFFFFF00);
  605. rObj.setPixelDP(getBorderX(p, gr.x, gr.y) - 1,
  606. getBorderY(p, gr.x, gr.y),
  607. 0xFFFFFF00);
  608. rObj.setPixelDP(getBorderX(p, gr.x, gr.y),
  609. getBorderY(p, gr.x, gr.y) - 1,
  610. 0xFFFFFF00);
  611. rObj.setPixelDP(getBorderX(p, gr.x, gr.y) + 1,
  612. getBorderY(p, gr.x, gr.y),
  613. 0xFFFFFF00);
  614. rObj.setPixelDP(getBorderX(p, gr.x, gr.y),
  615. getBorderY(p, gr.x, gr.y) + 1,
  616. 0xFFFFFF00);
  617. p = ((int)animationProgress + i + gr.x - 1)
  618. % (gr.x * 2 + gr.y * 2 - 4);
  619. rObj.setPixelDP(getBorderX(p, gr.x, gr.y),
  620. getBorderY(p, gr.x, gr.y),
  621. 0xFFFFFF00);
  622. rObj.setPixelDP(getBorderX(p, gr.x, gr.y) - 1,
  623. getBorderY(p, gr.x, gr.y),
  624. 0xFFFFFF00);
  625. rObj.setPixelDP(getBorderX(p, gr.x, gr.y),
  626. getBorderY(p, gr.x, gr.y) - 1,
  627. 0xFFFFFF00);
  628. rObj.setPixelDP(getBorderX(p, gr.x, gr.y) + 1,
  629. getBorderY(p, gr.x, gr.y),
  630. 0xFFFFFF00);
  631. rObj.setPixelDP(getBorderX(p, gr.x, gr.y),
  632. getBorderY(p, gr.x, gr.y) + 1,
  633. 0xFFFFFF00);
  634. p = ((int)animationProgress + i + gr.x + gr.y - 2)
  635. % (gr.x * 2 + gr.y * 2 - 4);
  636. rObj.setPixelDP(getBorderX(p, gr.x, gr.y),
  637. getBorderY(p, gr.x, gr.y),
  638. 0xFFFFFF00);
  639. rObj.setPixelDP(getBorderX(p, gr.x, gr.y) - 1,
  640. getBorderY(p, gr.x, gr.y),
  641. 0xFFFFFF00);
  642. rObj.setPixelDP(getBorderX(p, gr.x, gr.y),
  643. getBorderY(p, gr.x, gr.y) - 1,
  644. 0xFFFFFF00);
  645. rObj.setPixelDP(getBorderX(p, gr.x, gr.y) + 1,
  646. getBorderY(p, gr.x, gr.y),
  647. 0xFFFFFF00);
  648. rObj.setPixelDP(getBorderX(p, gr.x, gr.y),
  649. getBorderY(p, gr.x, gr.y) + 1,
  650. 0xFFFFFF00);
  651. p = ((int)animationProgress + i + gr.x * 2 + gr.y - 3)
  652. % (gr.x * 2 + gr.y * 2 - 4);
  653. rObj.setPixelDP(getBorderX(p, gr.x, gr.y),
  654. getBorderY(p, gr.x, gr.y),
  655. 0xFFFFFF00);
  656. rObj.setPixelDP(getBorderX(p, gr.x, gr.y) - 1,
  657. getBorderY(p, gr.x, gr.y),
  658. 0xFFFFFF00);
  659. rObj.setPixelDP(getBorderX(p, gr.x, gr.y),
  660. getBorderY(p, gr.x, gr.y) - 1,
  661. 0xFFFFFF00);
  662. rObj.setPixelDP(getBorderX(p, gr.x, gr.y) + 1,
  663. getBorderY(p, gr.x, gr.y),
  664. 0xFFFFFF00);
  665. rObj.setPixelDP(getBorderX(p, gr.x, gr.y),
  666. getBorderY(p, gr.x, gr.y) + 1,
  667. 0xFFFFFF00);
  668. }
  669. rObj.releaseDrawOptions();
  670. }
  671. }
  672. }
  673. void QuestGraphItem::renderVerticalConnections(Framework::Bild& rObj)
  674. {
  675. if (outgoingConnection) outgoingConnection->renderVertical(rObj);
  676. }
  677. void QuestGraphItem::renderHorizontalConnections(Framework::Bild& rObj)
  678. {
  679. if (outgoingConnection) outgoingConnection->renderHorizontal(rObj);
  680. }
  681. void QuestGraphItem::replacePreviousConnections(
  682. QuestGraphItem* zBefore, QuestGraphItem* zAfter)
  683. {
  684. for (auto con = previousLayersConnections.begin(); con; con++)
  685. {
  686. if (con.val().target == zBefore) con.set({zAfter, con.val().id});
  687. }
  688. }
  689. const Framework::Text& QuestGraphItem::getId() const
  690. {
  691. return id;
  692. }
  693. const Framework::Text& QuestGraphItem::getRequirements() const
  694. {
  695. return requirements;
  696. }
  697. bool QuestGraphItem::calculateLaxerIndex()
  698. {
  699. int oldLayerIndex = layerIndex;
  700. int maxLayerIndex = -1;
  701. for (const ConnectionInfo& item : previousLayersConnections)
  702. {
  703. if (item.target->getLayerIndex() < 0) return 0;
  704. if (item.target->getLayerIndex() > maxLayerIndex)
  705. {
  706. maxLayerIndex = item.target->getLayerIndex();
  707. }
  708. }
  709. layerIndex = maxLayerIndex + 1;
  710. return oldLayerIndex != layerIndex;
  711. }
  712. int QuestGraphItem::getLayerIndex() const
  713. {
  714. return layerIndex;
  715. }
  716. void QuestGraphItem::setVirtual(bool virtualNode)
  717. {
  718. this->virtualNode = virtualNode;
  719. gr.y = virtualNode ? 1 : gr.y;
  720. gr.x = 1;
  721. setStyle(ZeichnungHintergrund::Style::Sichtbar, virtualNode);
  722. }
  723. void QuestGraphItem::setNodeIndex(int index)
  724. {
  725. this->nodeIndex = index;
  726. }
  727. void QuestGraphItem::addNextLayerRef(ConnectionInfo zItem)
  728. {
  729. nextLayersConnections.add(zItem);
  730. }
  731. void QuestGraphItem::addPreviousLayerRef(ConnectionInfo zItem)
  732. {
  733. previousLayersConnections.add(zItem);
  734. }
  735. void QuestGraphItem::initializeConnections()
  736. {
  737. outgoingConnection = new Connection(this);
  738. incommingConnection = new OrConnection(this, previousLayersConnections);
  739. }
  740. const Framework::Array<ConnectionInfo>&
  741. QuestGraphItem::getNextLayersConnections() const
  742. {
  743. return nextLayersConnections;
  744. }
  745. int QuestGraphItem::getNodeIndex() const
  746. {
  747. return nodeIndex;
  748. }
  749. bool QuestGraphItem::isMainQuest() const
  750. {
  751. return mainQuest;
  752. }
  753. bool QuestGraphItem::isVirtual() const
  754. {
  755. return virtualNode;
  756. }
  757. bool QuestGraphItem::isFinished() const
  758. {
  759. return virtualNode ? incommingConnection->isActive() : finished;
  760. }
  761. Connection* QuestGraphItem::zOutgoingConnection() const
  762. {
  763. return outgoingConnection;
  764. }
  765. OrConnection* QuestGraphItem::zIncommingConnection() const
  766. {
  767. return incommingConnection;
  768. }
  769. QuestGraphItemLayer::QuestGraphItemLayer()
  770. : ReferenceCounter()
  771. {}
  772. bool QuestGraphItemLayer::tick(double tickVal)
  773. {
  774. bool result = 0;
  775. for (QuestGraphItem* item : items)
  776. {
  777. result |= item->tick(tickVal);
  778. }
  779. return result;
  780. }
  781. void QuestGraphItemLayer::render(Framework::Bild& rObj)
  782. {
  783. for (QuestGraphItem* item : items)
  784. {
  785. item->render(rObj);
  786. item->renderVerticalConnections(rObj);
  787. }
  788. for (QuestGraphItem* item : items)
  789. {
  790. item->renderHorizontalConnections(rObj);
  791. }
  792. }
  793. void QuestGraphItemLayer::doMausEreignis(Framework::MausEreignis& me)
  794. {
  795. for (QuestGraphItem* item : items)
  796. {
  797. item->doPublicMausEreignis(me);
  798. }
  799. }
  800. void QuestGraphItemLayer::addItem(QuestGraphItem* item)
  801. {
  802. items.add(item);
  803. item->setNodeIndex(items.getEintragAnzahl() - 1);
  804. }
  805. bool QuestGraphItemLayer::sortItems()
  806. {
  807. bool changed = 0;
  808. for (int index = 0; index < items.getEintragAnzahl() - 1; index++)
  809. {
  810. QuestGraphItem* current = items.z(index);
  811. int conflicts = 0;
  812. int afterSwapConflicts = 0;
  813. QuestGraphItem* other = items.z(index + 1);
  814. for (const ConnectionInfo& connection :
  815. current->getNextLayersConnections())
  816. {
  817. for (const ConnectionInfo& otherConnection :
  818. other->getNextLayersConnections())
  819. {
  820. if ((otherConnection.target->getNodeIndex()
  821. < connection.target->getNodeIndex()))
  822. {
  823. conflicts++;
  824. }
  825. if ((otherConnection.target->getNodeIndex()
  826. > connection.target->getNodeIndex()))
  827. {
  828. afterSwapConflicts++;
  829. }
  830. }
  831. }
  832. if (conflicts > afterSwapConflicts)
  833. {
  834. // move node down
  835. QuestGraphItem* after = items.z(index + 1);
  836. after->setNodeIndex(index);
  837. current->setNodeIndex(index + 1);
  838. items.tausch(index, index + 1);
  839. changed = 1;
  840. }
  841. }
  842. return changed;
  843. }
  844. int QuestGraphItemLayer::fixItemPositions(int x)
  845. {
  846. // calculate size needed for & nodes and | nodes
  847. int maxWidth = 0;
  848. for (QuestGraphItem* item : items)
  849. {
  850. item->initializeConnections();
  851. item->zIncommingConnection()->layout();
  852. if (item->getBreite() > maxWidth)
  853. {
  854. maxWidth = item->getBreite();
  855. }
  856. }
  857. x += 20 + 21; // ofset for incomming connections
  858. int y = 0;
  859. // calculate y positions of nodes
  860. for (QuestGraphItem* item : items)
  861. {
  862. int height = 0;
  863. int lastId = -1;
  864. int nodeHeight = item->zIncommingConnection()->getNeededHeight();
  865. if (nodeHeight < item->getHeight()) nodeHeight = item->getHeight();
  866. item->setPosition(x + maxWidth / 2 - item->getBreite() / 2,
  867. y + nodeHeight / 2 - item->getHeight() / 2);
  868. y += nodeHeight + 20;
  869. }
  870. x += maxWidth; // this layers node size
  871. for (QuestGraphItem* item : items)
  872. {
  873. item->zIncommingConnection()->layout();
  874. item->zOutgoingConnection()->setVx(x);
  875. item->zOutgoingConnection()->layout();
  876. }
  877. x += items.getEintragAnzahl() * 11 + 10; // offset for outgoing connections
  878. return x + 20; // min space between layers
  879. }
  880. void QuestGraphItemLayer::sortConnections()
  881. {
  882. for (QuestGraphItem* item : items)
  883. {
  884. item->zIncommingConnection()->layout();
  885. item->zOutgoingConnection()->layout();
  886. }
  887. int connectionCount = 0;
  888. bool* sorted = new bool[items.getEintragAnzahl()];
  889. memset(sorted, 0, sizeof(bool) * items.getEintragAnzahl());
  890. int sortedCount = 0;
  891. while (true)
  892. {
  893. int minHeight = -1;
  894. QuestGraphItem* next = 0;
  895. for (QuestGraphItem* item : items)
  896. {
  897. if (sorted[item->getNodeIndex()]) continue;
  898. if (item->zOutgoingConnection()->getTargetCount()
  899. == connectionCount)
  900. {
  901. if (minHeight < 0
  902. || item->zOutgoingConnection()->getVerticalLength()
  903. < minHeight)
  904. {
  905. minHeight
  906. = item->zOutgoingConnection()->getVerticalLength();
  907. next = item;
  908. }
  909. }
  910. }
  911. if (!next)
  912. {
  913. if (sortedCount < items.getEintragAnzahl())
  914. {
  915. connectionCount++;
  916. }
  917. else
  918. {
  919. break;
  920. }
  921. }
  922. else
  923. {
  924. next->zOutgoingConnection()->setVx(
  925. next->zOutgoingConnection()->getVx() + 10 + sortedCount * 11);
  926. sorted[next->getNodeIndex()] = 1;
  927. sortedCount++;
  928. }
  929. }
  930. delete[] sorted;
  931. }
  932. const Framework::RCArray<QuestGraphItem>& QuestGraphItemLayer::getItems() const
  933. {
  934. return items;
  935. }
  936. int QuestGraphItemLayer::getLeyerHeight(int& minY) const
  937. {
  938. minY = 0;
  939. int end = 0;
  940. bool first = 1;
  941. for (QuestGraphItem* item : items)
  942. {
  943. if (first || item->getY() < minY) minY = item->getY();
  944. if (first || item->getY() + item->getHeight() > end)
  945. end = item->getY() + item->getHeight();
  946. first = 0;
  947. }
  948. return end - minY;
  949. }
  950. int QuestGraphItemLayer::getLeyerHeight() const
  951. {
  952. int y;
  953. return getLeyerHeight(y);
  954. }
  955. void QuestGraphItemLayer::centerVertically(int pos)
  956. {
  957. int minY = 0;
  958. int height = getLeyerHeight(minY);
  959. int yOffset = pos - height / 2 - minY;
  960. for (QuestGraphItem* item : items)
  961. {
  962. item->setPosition(item->getX(), item->getY() + yOffset);
  963. item->zIncommingConnection()->layout();
  964. item->zOutgoingConnection()->layout();
  965. }
  966. }
  967. void QuestGraphItemLayer::resolveHorizontalConflicts(
  968. const QuestGraphItemLayer* zNextLayer)
  969. {
  970. for (QuestGraphItem* item : items)
  971. {
  972. int outgoingY = item->getY() + item->getHeight() / 2;
  973. if (zNextLayer->isHorizontalLineConflict(outgoingY, item->zOutgoingConnection()))
  974. {
  975. int offset = 0;
  976. for (int i = 1; i <= 10; i++)
  977. {
  978. if (!zNextLayer->isHorizontalLineConflict(
  979. outgoingY - i, item->zOutgoingConnection()))
  980. {
  981. offset = -i;
  982. break;
  983. }
  984. if (!zNextLayer->isHorizontalLineConflict(
  985. outgoingY + i, item->zOutgoingConnection()))
  986. {
  987. offset = i;
  988. break;
  989. }
  990. }
  991. item->setPosition(item->getX(), item->getY() + offset);
  992. item->zIncommingConnection()->layout();
  993. item->zOutgoingConnection()->layout();
  994. }
  995. }
  996. }
  997. bool QuestGraphItemLayer::isHorizontalLineConflict(
  998. int y, Connection* zIgnoredIncommingConnection) const
  999. {
  1000. for (QuestGraphItem* item : items)
  1001. {
  1002. if (item->zIncommingConnection()->isHorizontalLineConflict(
  1003. y, zIgnoredIncommingConnection))
  1004. return 1;
  1005. }
  1006. return 0;
  1007. }
  1008. QuestGraph::QuestGraph()
  1009. : ZeichnungHintergrund()
  1010. {
  1011. addStyle(ZeichnungHintergrund::Style::Sichtbar
  1012. | ZeichnungHintergrund::Style::Erlaubt
  1013. | ZeichnungHintergrund::Style::Rahmen);
  1014. setRahmenBreite(1);
  1015. setRahmenFarbe(0xFF52525E);
  1016. setMausEreignis(Framework::_ret1ME);
  1017. }
  1018. bool QuestGraph::tick(double tickVal)
  1019. {
  1020. for (QuestGraphItemLayer* layer : layers)
  1021. {
  1022. rend |= layer->tick(tickVal);
  1023. }
  1024. return ZeichnungHintergrund::tick(tickVal);
  1025. }
  1026. void QuestGraph::render(Framework::Bild& rObj)
  1027. {
  1028. if (hatStyle(ZeichnungHintergrund::Style::Sichtbar)
  1029. && layers.getEintragAnzahl() > 0)
  1030. {
  1031. ZeichnungHintergrund::render(rObj);
  1032. if (rObj.setDrawOptions(pos, gr))
  1033. {
  1034. if (rObj.setDrawOptions(getRahmenBreite(),
  1035. getRahmenBreite(),
  1036. getInnenBreite(),
  1037. getInnenHeight()))
  1038. {
  1039. int hScrollOffset = 0;
  1040. if (hatStyle(ZeichnungHintergrund::Style::HScroll)
  1041. && horizontalScrollBar)
  1042. {
  1043. hScrollOffset = horizontalScrollBar->getScroll();
  1044. }
  1045. int vScrollOffset = 0;
  1046. if (hatStyle(ZeichnungHintergrund::Style::VScroll)
  1047. && vertikalScrollBar)
  1048. {
  1049. vScrollOffset = vertikalScrollBar->getScroll();
  1050. }
  1051. if (hScrollOffset || vScrollOffset)
  1052. {
  1053. rObj.addScrollOffset(hScrollOffset, vScrollOffset);
  1054. }
  1055. for (QuestGraphItemLayer* layer : layers)
  1056. {
  1057. layer->render(rObj);
  1058. }
  1059. rObj.releaseDrawOptions();
  1060. }
  1061. rObj.releaseDrawOptions();
  1062. }
  1063. }
  1064. }
  1065. void QuestGraph::doMausEreignis(Framework::MausEreignis& me, bool userRet)
  1066. {
  1067. userRet &= hatStyle(ZeichnungHintergrund::Style::Sichtbar);
  1068. bool vera = me.verarbeitet;
  1069. if (!userRet)
  1070. {
  1071. me.verarbeitet = 1;
  1072. }
  1073. for (QuestGraphItemLayer* layer : layers)
  1074. {
  1075. layer->doMausEreignis(me);
  1076. }
  1077. if (!userRet)
  1078. {
  1079. me.verarbeitet = vera;
  1080. }
  1081. }
  1082. void QuestGraph::addVirtualConnectionNodes()
  1083. {
  1084. int layerIndex = 0;
  1085. // add virtual items for connections that do not go directly to the next
  1086. // layer after this operation each node only have connections to the
  1087. // immediate following layer
  1088. for (QuestGraphItemLayer* layer : layers)
  1089. {
  1090. for (int i = 0; i < layer->getItems().getEintragAnzahl(); i++)
  1091. {
  1092. QuestGraphItem* item = layer->getItems().z(i);
  1093. auto iterator = item->getNextLayersConnections().begin();
  1094. QuestGraphItem* virtualItem = 0;
  1095. while (iterator)
  1096. {
  1097. if (iterator.val().target->getLayerIndex() > layerIndex + 1)
  1098. {
  1099. if (!virtualItem)
  1100. {
  1101. virtualItem = new QuestGraphItem();
  1102. virtualItem->addPreviousLayerRef(
  1103. {item, iterator.val().id});
  1104. virtualItem->addNextLayerRef(
  1105. {iterator.val().target, iterator.val().id});
  1106. iterator.val().target->replacePreviousConnections(
  1107. item, virtualItem);
  1108. virtualItem->setVirtual(true);
  1109. virtualItem->calculateLaxerIndex();
  1110. iterator.set({virtualItem, iterator.val().id});
  1111. iterator++;
  1112. }
  1113. else
  1114. {
  1115. virtualItem->addNextLayerRef(
  1116. {iterator.val().target, iterator.val().id});
  1117. iterator.val().target->replacePreviousConnections(
  1118. item, virtualItem);
  1119. iterator.remove();
  1120. }
  1121. }
  1122. else if (iterator.val().target->getLayerIndex() < 0)
  1123. { // remove connections to invalid nodes
  1124. iterator.remove();
  1125. }
  1126. else
  1127. {
  1128. iterator++;
  1129. }
  1130. }
  1131. if (virtualItem)
  1132. {
  1133. addItem(virtualItem);
  1134. }
  1135. }
  1136. layerIndex++;
  1137. }
  1138. }
  1139. void QuestGraph::sortItems()
  1140. {
  1141. bool changed = 1;
  1142. while (changed)
  1143. {
  1144. changed = 0;
  1145. for (QuestGraphItemLayer* layer : layers)
  1146. {
  1147. changed |= layer->sortItems();
  1148. }
  1149. }
  1150. }
  1151. void QuestGraph::fixItemPositions()
  1152. {
  1153. int height = 0;
  1154. int x = 0;
  1155. for (QuestGraphItemLayer* layer : layers)
  1156. {
  1157. x = layer->fixItemPositions(x);
  1158. if (layer->getLeyerHeight() > height)
  1159. {
  1160. height = layer->getLeyerHeight();
  1161. }
  1162. }
  1163. for (QuestGraphItemLayer* layer : layers)
  1164. {
  1165. layer->sortConnections();
  1166. }
  1167. if (height > getInnenHeight())
  1168. {
  1169. addStyle(ZeichnungHintergrund::Style::VScroll);
  1170. setVertikalKlickScroll(10);
  1171. vertikalScrollBar->update(height, getInnenHeight());
  1172. }
  1173. if (layers.getEintragAnzahl() * 150 - 80 > getInnenBreite())
  1174. {
  1175. addStyle(ZeichnungHintergrund::Style::HScroll);
  1176. setHorizontalKlickScroll(10);
  1177. horizontalScrollBar->update(
  1178. layers.getEintragAnzahl() * 150 - 80, getInnenBreite());
  1179. }
  1180. QuestGraphItemLayer* last = 0;
  1181. for (int i = layers.getEintragAnzahl() - 1; i >= 0; i--)
  1182. {
  1183. QuestGraphItemLayer* layer = layers.z(i);
  1184. layer->centerVertically(getHeight() / 2);
  1185. if (last) layer->resolveHorizontalConflicts(last);
  1186. last = layer;
  1187. }
  1188. }
  1189. void QuestGraph::centerVertically()
  1190. {
  1191. int height = 0;
  1192. for (QuestGraphItemLayer* layer : layers)
  1193. {
  1194. if (layer->getLeyerHeight() > height)
  1195. {
  1196. height = layer->getLeyerHeight();
  1197. }
  1198. }
  1199. if (height > getInnenHeight())
  1200. {
  1201. addStyle(ZeichnungHintergrund::Style::VScroll);
  1202. setVertikalKlickScroll(10);
  1203. vertikalScrollBar->update(height, getInnenHeight());
  1204. }
  1205. if (layers.getEintragAnzahl() * 150 - 80 > getInnenBreite())
  1206. {
  1207. addStyle(ZeichnungHintergrund::Style::HScroll);
  1208. setHorizontalKlickScroll(10);
  1209. horizontalScrollBar->update(
  1210. layers.getEintragAnzahl() * 150 - 80, getInnenBreite());
  1211. }
  1212. QuestGraphItemLayer* last = 0;
  1213. for (int i = layers.getEintragAnzahl() - 1; i >= 0; i--)
  1214. {
  1215. QuestGraphItemLayer* layer = layers.z(i);
  1216. layer->centerVertically(getHeight() / 2);
  1217. if (last) layer->resolveHorizontalConflicts(last);
  1218. last = layer;
  1219. }
  1220. }
  1221. void QuestGraph::addItem(QuestGraphItem* item)
  1222. {
  1223. int layerInex = item->getLayerIndex();
  1224. if (layerInex >= 0)
  1225. {
  1226. while (layerInex >= layers.getEintragAnzahl())
  1227. {
  1228. layers.add(new QuestGraphItemLayer());
  1229. }
  1230. layers.z(layerInex)->addItem(item);
  1231. }
  1232. else
  1233. {
  1234. invalidNodes.addItem(item);
  1235. }
  1236. }