QuestGraph.cpp 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293
  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. int height = 0;
  1164. for (QuestGraphItemLayer* layer : layers)
  1165. {
  1166. if (layer->getLeyerHeight() > height)
  1167. {
  1168. height = layer->getLeyerHeight();
  1169. }
  1170. }
  1171. if (height > getInnenHeight())
  1172. {
  1173. addStyle(ZeichnungHintergrund::Style::VScroll);
  1174. setVertikalKlickScroll(10);
  1175. vertikalScrollBar->update(height, getInnenHeight());
  1176. }
  1177. if (layers.getEintragAnzahl() * 150 - 80 > getInnenBreite())
  1178. {
  1179. addStyle(ZeichnungHintergrund::Style::HScroll);
  1180. setHorizontalKlickScroll(10);
  1181. horizontalScrollBar->update(
  1182. layers.getEintragAnzahl() * 150 - 80, getInnenBreite());
  1183. }
  1184. QuestGraphItemLayer* last = 0;
  1185. for (int i = layers.getEintragAnzahl() - 1; i >= 0; i--)
  1186. {
  1187. QuestGraphItemLayer* layer = layers.z(i);
  1188. layer->centerVertically(getHeight() / 2);
  1189. if (last) layer->resolveHorizontalConflicts(last);
  1190. last = layer;
  1191. }
  1192. }
  1193. void QuestGraph::addItem(QuestGraphItem* item)
  1194. {
  1195. int layerInex = item->getLayerIndex();
  1196. if (layerInex >= 0)
  1197. {
  1198. while (layerInex >= layers.getEintragAnzahl())
  1199. {
  1200. layers.add(new QuestGraphItemLayer());
  1201. }
  1202. layers.z(layerInex)->addItem(item);
  1203. }
  1204. else
  1205. {
  1206. invalidNodes.addItem(item);
  1207. }
  1208. }