QuestGraph.cpp 36 KB

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