RecipieIngredient.cpp 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008
  1. #include "RecipieIngredient.h"
  2. #include <DateiSystem.h>
  3. #include <ToolTip.h>
  4. #include <XML.h>
  5. #include "Globals.h"
  6. RecipieIngredientElement::RecipieIngredientElement()
  7. : Framework::UIMLElement()
  8. {}
  9. LogicTree* RecipieIngredientElement::parse(Framework::XML::Element* zElement)
  10. {
  11. if (zElement->getName().istGleich("anyItem"))
  12. {
  13. return new LogicTree(LogicalOperator::OR, new RequirementSet());
  14. }
  15. else if (zElement->getName().istGleich("attribute"))
  16. {
  17. AttributeOperator op = AttributeOperator::Equals;
  18. if (zElement->getAttributeValue("operator").istGleich("!="))
  19. {
  20. op = AttributeOperator::NotEquals;
  21. }
  22. else if (zElement->getAttributeValue("operator").istGleich(">"))
  23. {
  24. op = AttributeOperator::GreaterThan;
  25. }
  26. else if (zElement->getAttributeValue("operator").istGleich(">="))
  27. {
  28. op = AttributeOperator::GreaterThanOrEquals;
  29. }
  30. else if (zElement->getAttributeValue("operator").istGleich("<"))
  31. {
  32. op = AttributeOperator::LessThan;
  33. }
  34. else if (zElement->getAttributeValue("operator").istGleich("<="))
  35. {
  36. op = AttributeOperator::LessThanOrEquals;
  37. }
  38. Requirement* req = new Requirement(zElement->getAttributeValue("name"),
  39. zElement->getAttributeValue("value"),
  40. op);
  41. RequirementSet* set = new RequirementSet();
  42. set->addRequirement(req);
  43. req->release();
  44. return new LogicTree(LogicalOperator::OR, set);
  45. }
  46. else if (zElement->getName().istGleich("operator"))
  47. {
  48. bool result0_0 = (bool)(int)zElement->getAttributeValue("result_0_0");
  49. bool result0_1 = (bool)(int)zElement->getAttributeValue("result_0_1");
  50. bool result1_0 = (bool)(int)zElement->getAttributeValue("result_1_0");
  51. bool result1_1 = (bool)(int)zElement->getAttributeValue("result_1_1");
  52. if (!result0_0 && !result0_1 && !result1_0 && !result1_1)
  53. { // none match
  54. RequirementSet* set = new RequirementSet();
  55. Requirement* req
  56. = new Requirement("_x", "0", AttributeOperator::Equals);
  57. set->addRequirement(req);
  58. req->release();
  59. req = new Requirement("_x", "1", AttributeOperator::Equals);
  60. set->addRequirement(req);
  61. req->release();
  62. return new LogicTree(LogicalOperator::OR, set);
  63. }
  64. if (result0_0 && result0_1 && result1_0 && result1_1)
  65. { // any match
  66. return new LogicTree(LogicalOperator::OR, new RequirementSet());
  67. }
  68. auto iterator = zElement->getChilds();
  69. LogicTree* left = parse(iterator.val());
  70. if (!result0_0 && !result0_1 && result1_0 && result1_1)
  71. {
  72. return left;
  73. }
  74. if (result0_0 && result0_1 && !result1_0 && !result1_1)
  75. {
  76. left->negate();
  77. return left;
  78. }
  79. LogicTree* right = 0;
  80. iterator++;
  81. right = parse(iterator.val());
  82. if (!result0_0 && result0_1 && !result1_0 && result1_1)
  83. {
  84. left->release();
  85. return right;
  86. }
  87. if (result0_0 && !result0_1 && result1_0 && !result1_1)
  88. {
  89. left->release();
  90. right->negate();
  91. return right;
  92. }
  93. if (!result0_0 && !result0_1 && !result1_0 && result1_1)
  94. {
  95. LogicTree* result = new LogicTree(LogicalOperator::AND, 0);
  96. result->addChildren(left);
  97. result->addChildren(right);
  98. return result;
  99. }
  100. if (!result0_0 && result0_1 && result1_0 && result1_1)
  101. {
  102. LogicTree* result = new LogicTree(LogicalOperator::OR, 0);
  103. result->addChildren(left);
  104. result->addChildren(right);
  105. return result;
  106. }
  107. if (result0_0 && result0_1 && result1_0 && !result1_1)
  108. {
  109. left->negate();
  110. right->negate();
  111. LogicTree* result = new LogicTree(LogicalOperator::OR, 0);
  112. result->addChildren(left);
  113. result->addChildren(right);
  114. return result;
  115. }
  116. if (result0_0 && !result0_1 && !result1_0 && !result1_1)
  117. {
  118. left->negate();
  119. right->negate();
  120. LogicTree* result = new LogicTree(LogicalOperator::AND, 0);
  121. result->addChildren(left);
  122. result->addChildren(right);
  123. return result;
  124. }
  125. if (!result0_0 && !result0_1 && result1_0 && !result1_1)
  126. {
  127. right->negate();
  128. LogicTree* result = new LogicTree(LogicalOperator::AND, 0);
  129. result->addChildren(left);
  130. result->addChildren(right);
  131. return result;
  132. }
  133. if (!result0_0 && result0_1 && !result1_0 && !result1_1)
  134. {
  135. left->negate();
  136. LogicTree* result = new LogicTree(LogicalOperator::AND, 0);
  137. result->addChildren(left);
  138. result->addChildren(right);
  139. return result;
  140. }
  141. if (!result0_0 && result0_1 && result1_0 && !result1_1)
  142. {
  143. LogicTree* or = new LogicTree(LogicalOperator::OR, 0);
  144. or->addChildren(left);
  145. or->addChildren(right);
  146. LogicTree* notLeft = left->clone();
  147. notLeft->negate();
  148. LogicTree* notRight = right->clone();
  149. notRight->negate();
  150. LogicTree* notOr = new LogicTree(LogicalOperator::OR, 0);
  151. notOr->addChildren(notLeft);
  152. notOr->addChildren(notRight);
  153. LogicTree* result = new LogicTree(LogicalOperator::AND, 0);
  154. result->addChildren(or);
  155. result->addChildren(notOr);
  156. return result;
  157. }
  158. if (result0_0 && !result0_1 && !result1_0 && result1_1)
  159. {
  160. LogicTree*and = new LogicTree(LogicalOperator::AND, 0);
  161. and->addChildren(left);
  162. and->addChildren(right);
  163. LogicTree* notLeft = left->clone();
  164. notLeft->negate();
  165. LogicTree* notRight = right->clone();
  166. notRight->negate();
  167. LogicTree* notAnd = new LogicTree(LogicalOperator::AND, 0);
  168. notAnd->addChildren(notLeft);
  169. notAnd->addChildren(notRight);
  170. LogicTree* result = new LogicTree(LogicalOperator::OR, 0);
  171. result->addChildren(and);
  172. result->addChildren(notAnd);
  173. return result;
  174. }
  175. if (result0_0 && !result0_1 && result1_0 && result1_1)
  176. {
  177. right->negate();
  178. LogicTree* result = new LogicTree(LogicalOperator::OR, 0);
  179. result->addChildren(left);
  180. result->addChildren(right);
  181. return result;
  182. }
  183. if (result0_0 && result0_1 && !result1_0 && result1_1)
  184. {
  185. left->negate();
  186. LogicTree* result = new LogicTree(LogicalOperator::OR, 0);
  187. result->addChildren(left);
  188. result->addChildren(right);
  189. return result;
  190. }
  191. }
  192. return 0;
  193. }
  194. //! prüft, ob dieses UIML Element für ein bestimmtes xml Element zuständig
  195. //! ist
  196. bool RecipieIngredientElement::isApplicableFor(Framework::XML::Element& element)
  197. {
  198. return element.getName().istGleich("ingredient");
  199. }
  200. //! erstellt eine neue Zeichnung zu einem gegebenen xml Element
  201. Framework::Zeichnung* RecipieIngredientElement::parseElement(
  202. Framework::XML::Element& element, Framework::UIMLContainer& generalFactory)
  203. {
  204. int amount = (int)element.getAttributeValue("amount");
  205. RecipieIngredient* result = new RecipieIngredient(amount);
  206. Framework::XML::Editor logicSelector = element.selectChildsByName("logic");
  207. if (logicSelector.exists())
  208. {
  209. LogicTree* logic = new LogicTree(LogicalOperator::OR, 0);
  210. logicSelector.selectChildren().forEach(
  211. [this, logic](Framework::XML::Element* zElement) {
  212. logic->addChildren(parse(zElement));
  213. });
  214. Framework::RCArray<RequirementSet>* requirements = logic->resolve();
  215. logic->release();
  216. for (RequirementSet* set : *requirements)
  217. {
  218. result->addPossibleItem(
  219. set->getIcon(), new Text(set->renderToTooltip()), set->getItemType());
  220. }
  221. requirements->release();
  222. }
  223. return result;
  224. }
  225. //! wendet die layout parameter zu einer Zeichnung an
  226. void RecipieIngredientElement::layout(Framework::XML::Element& element,
  227. Framework::Zeichnung& z,
  228. int pWidth,
  229. int pHeight,
  230. Framework::UIMLContainer& generalLayouter)
  231. {
  232. UIMLElement::layout(element, z, pWidth, pHeight, generalLayouter);
  233. z.setWidth(50);
  234. z.setHeight(50);
  235. }
  236. RecipieIngredient::RecipieIngredient(int amount)
  237. : Framework::ZeichnungHintergrund(),
  238. currentIconIndex(0),
  239. timtUntilNextIcon(2.0),
  240. amount(amount)
  241. {
  242. setStyle(Framework::Zeichnung::Style::Erlaubt
  243. | Framework::Zeichnung::Style::Sichtbar);
  244. Framework::ToolTip* tip = new Framework::ToolTip(window->zBildschirm());
  245. tip->addStyle(Framework::ZeichnungHintergrund::Style::Hintergrund
  246. | Framework::ZeichnungHintergrund::Style::HAlpha
  247. | Framework::ZeichnungHintergrund::Style::Rahmen
  248. | Framework::ZeichnungHintergrund::Style::Sichtbar);
  249. tip->setHintergrundFarbe(0xA0000000);
  250. tip->setRahmenFarbe(0xFFFFFFFF);
  251. tip->setRahmenBreite(1);
  252. toolTip = uiFactory.createTextFeld(uiFactory.initParam);
  253. toolTip->setText("");
  254. toolTip->setSize(0, 0);
  255. toolTip->addStyle(Framework::TextFeld::Style::Mehrzeilig);
  256. tip->addMember(toolTip);
  257. tip->setWarten(0.5);
  258. setToolTipZ(tip);
  259. }
  260. void RecipieIngredient::addPossibleItem(
  261. Framework::Bild* icon, Framework::Text* toolTip, int typeId)
  262. {
  263. icons.add(icon);
  264. toolTips.add(toolTip);
  265. itemTypes.add(typeId);
  266. if (toolTips.getEintragAnzahl() == 1)
  267. {
  268. this->toolTip->setText(toolTips.z(0)->getText());
  269. this->toolTip->setSize(
  270. this->toolTip->getNeededWidth(), this->toolTip->getNeededHeight());
  271. }
  272. }
  273. bool RecipieIngredient::tick(double tickVal)
  274. {
  275. if (!zToolTip()->isVisible())
  276. {
  277. timtUntilNextIcon -= tickVal;
  278. if (timtUntilNextIcon <= 0)
  279. {
  280. timtUntilNextIcon = 2.0;
  281. currentIconIndex++;
  282. if (currentIconIndex >= icons.getEintragAnzahl())
  283. {
  284. currentIconIndex = 0;
  285. }
  286. if (toolTips.getEintragAnzahl() > 0)
  287. {
  288. toolTip->setText(toolTips.z(currentIconIndex)->getText());
  289. toolTip->setSize(
  290. toolTip->getNeededWidth(), toolTip->getNeededHeight());
  291. rend = 1;
  292. }
  293. }
  294. }
  295. return ZeichnungHintergrund::tick(tickVal);
  296. }
  297. void RecipieIngredient::render(Framework::Bild& rObj)
  298. {
  299. ZeichnungHintergrund::render(rObj);
  300. if (!rObj.setDrawOptions(pos.x, pos.y, gr.x, gr.y)) return;
  301. TextRenderer tr;
  302. tr.setSchriftZ(
  303. dynamic_cast<Schrift*>(uiFactory.initParam.schrift->getThis()));
  304. tr.setSchriftSize(12);
  305. rObj.fillRegion(0, 0, 50, 50, 0xFF222222);
  306. if (icons.getEintragAnzahl() > 0)
  307. rObj.alphaBild(0, 0, 50, 50, *icons.z(currentIconIndex));
  308. const char* units[] = {"", "K", "M", "G", "T", "P"};
  309. int i = 0;
  310. int tmpCount = amount;
  311. for (; i < 6 && tmpCount > 1024; i++)
  312. tmpCount = tmpCount / 1024;
  313. Text count = tmpCount;
  314. count += units[i];
  315. tr.renderText(45 - tr.getTextBreite(count),
  316. 45 - tr.getTextHeight(count),
  317. count,
  318. rObj,
  319. 0xFFFFFFFF);
  320. rObj.releaseDrawOptions();
  321. }
  322. void RecipieIngredient::doMausEreignis(Framework::MausEreignis& me, bool userRet)
  323. {
  324. if (me.id == ME_RLinks)
  325. {
  326. World::INSTANCE->zClient()->craftingUIMLRequest(
  327. itemTypes.get(currentIconIndex));
  328. }
  329. ZeichnungHintergrund::doMausEreignis(me, userRet);
  330. }
  331. Requirement::Requirement(
  332. Framework::Text attribute, Framework::Text value, AttributeOperator op)
  333. : Framework::ReferenceCounter(),
  334. attribute(attribute),
  335. value(value),
  336. op(op)
  337. {}
  338. void Requirement::negate()
  339. {
  340. switch (op)
  341. {
  342. case AttributeOperator::Equals:
  343. op = AttributeOperator::NotEquals;
  344. break;
  345. case AttributeOperator::NotEquals:
  346. op = AttributeOperator::Equals;
  347. break;
  348. case AttributeOperator::GreaterThan:
  349. op = AttributeOperator::LessThanOrEquals;
  350. break;
  351. case AttributeOperator::GreaterThanOrEquals:
  352. op = AttributeOperator::LessThan;
  353. break;
  354. case AttributeOperator::LessThan:
  355. op = AttributeOperator::GreaterThanOrEquals;
  356. break;
  357. case AttributeOperator::LessThanOrEquals:
  358. op = AttributeOperator::GreaterThan;
  359. break;
  360. }
  361. }
  362. Requirement* Requirement::clone()
  363. {
  364. return new Requirement(attribute, value, op);
  365. }
  366. bool Requirement::contradicts(Requirement* zOther)
  367. {
  368. if (zOther->attribute.istGleich(attribute.getText()))
  369. {
  370. if (zOther->value.istGleich(value))
  371. {
  372. if (op == AttributeOperator::Equals
  373. && zOther->op == AttributeOperator::NotEquals)
  374. {
  375. return 1;
  376. }
  377. if (op == AttributeOperator::NotEquals
  378. && zOther->op == AttributeOperator::Equals)
  379. {
  380. return 1;
  381. }
  382. if (op == AttributeOperator::GreaterThan
  383. && zOther->op == AttributeOperator::LessThan)
  384. {
  385. return 1;
  386. }
  387. if (op == AttributeOperator::LessThan
  388. && zOther->op == AttributeOperator::GreaterThan)
  389. {
  390. return 1;
  391. }
  392. }
  393. if (op == AttributeOperator::LessThan
  394. || op == AttributeOperator::LessThanOrEquals
  395. || op == AttributeOperator::GreaterThan
  396. || op == AttributeOperator::GreaterThanOrEquals)
  397. {
  398. double v1 = (double)value;
  399. double v2 = (double)zOther->value;
  400. if (op == AttributeOperator::LessThan
  401. || op == AttributeOperator::LessThanOrEquals)
  402. {
  403. if (v1 < v2)
  404. {
  405. if (zOther->op == AttributeOperator::GreaterThan
  406. || zOther->op == AttributeOperator::GreaterThanOrEquals
  407. || zOther->op == AttributeOperator::Equals)
  408. {
  409. return 1;
  410. }
  411. }
  412. }
  413. else if (op == AttributeOperator::GreaterThan
  414. || op == AttributeOperator::GreaterThanOrEquals)
  415. {
  416. if (v1 > v2)
  417. {
  418. if (zOther->op == AttributeOperator::LessThan
  419. || zOther->op == AttributeOperator::LessThanOrEquals
  420. || zOther->op == AttributeOperator::Equals)
  421. {
  422. return 1;
  423. }
  424. }
  425. }
  426. }
  427. if (zOther->op == AttributeOperator::LessThan
  428. || zOther->op == AttributeOperator::LessThanOrEquals
  429. || zOther->op == AttributeOperator::GreaterThan
  430. || zOther->op == AttributeOperator::GreaterThanOrEquals)
  431. {
  432. double v1 = (double)zOther->value;
  433. double v2 = (double)value;
  434. if (zOther->op == AttributeOperator::LessThan
  435. || zOther->op == AttributeOperator::LessThanOrEquals)
  436. {
  437. if (v1 < v2)
  438. {
  439. if (op == AttributeOperator::GreaterThan
  440. || op == AttributeOperator::GreaterThanOrEquals
  441. || op == AttributeOperator::Equals)
  442. {
  443. return 1;
  444. }
  445. }
  446. }
  447. else if (zOther->op == AttributeOperator::GreaterThan
  448. || zOther->op == AttributeOperator::GreaterThanOrEquals)
  449. {
  450. if (v1 > v2)
  451. {
  452. if (op == AttributeOperator::LessThan
  453. || op == AttributeOperator::LessThanOrEquals
  454. || op == AttributeOperator::Equals)
  455. {
  456. return 1;
  457. }
  458. }
  459. }
  460. }
  461. }
  462. return 0;
  463. }
  464. bool Requirement::merge(Requirement* zOther)
  465. {
  466. if (zOther->attribute.istGleich(attribute.getText()))
  467. {
  468. if (zOther->value.istGleich(value))
  469. {
  470. if (op == zOther->op)
  471. {
  472. return 1;
  473. }
  474. }
  475. else
  476. {
  477. if (op == AttributeOperator::Equals
  478. && zOther->op == AttributeOperator::NotEquals)
  479. {
  480. return 1;
  481. }
  482. if (op == AttributeOperator::NotEquals
  483. && zOther->op == AttributeOperator::Equals)
  484. {
  485. op = AttributeOperator::Equals;
  486. value = zOther->value;
  487. return 1;
  488. }
  489. }
  490. if (op == AttributeOperator::LessThan
  491. || op == AttributeOperator::LessThanOrEquals
  492. || op == AttributeOperator::GreaterThan
  493. || op == AttributeOperator::GreaterThanOrEquals)
  494. {
  495. double v1 = (double)value;
  496. double v2 = (double)zOther->value;
  497. if (op == AttributeOperator::LessThan
  498. || op == AttributeOperator::LessThanOrEquals)
  499. {
  500. if (zOther->op == AttributeOperator::LessThan
  501. || zOther->op == AttributeOperator::LessThanOrEquals)
  502. {
  503. if (v1 > v2
  504. || (v1 == v2
  505. && op == AttributeOperator::LessThanOrEquals))
  506. {
  507. op = zOther->op;
  508. value = zOther->value;
  509. }
  510. return 1;
  511. }
  512. if (zOther->op == AttributeOperator::Equals)
  513. {
  514. if (v2 < v1
  515. || (v2 == v1
  516. && op == AttributeOperator::LessThanOrEquals))
  517. {
  518. op = zOther->op;
  519. value = zOther->value;
  520. return 1;
  521. }
  522. }
  523. if (zOther->op == AttributeOperator::NotEquals)
  524. {
  525. if (v2 > v1
  526. || (v2 == v1 && op == AttributeOperator::LessThan))
  527. {
  528. return 1;
  529. }
  530. }
  531. }
  532. else if (op == AttributeOperator::GreaterThan
  533. || op == AttributeOperator::GreaterThanOrEquals)
  534. {
  535. if (zOther->op == AttributeOperator::GreaterThan
  536. || zOther->op == AttributeOperator::GreaterThanOrEquals)
  537. {
  538. if (v1 < v2
  539. || (v1 == v2
  540. && op == AttributeOperator::GreaterThanOrEquals))
  541. {
  542. op = zOther->op;
  543. value = zOther->value;
  544. }
  545. return 1;
  546. }
  547. if (zOther->op == AttributeOperator::Equals)
  548. {
  549. if (v2 > v1
  550. || (v2 == v1
  551. && op == AttributeOperator::GreaterThanOrEquals))
  552. {
  553. op = zOther->op;
  554. value = zOther->value;
  555. return 1;
  556. }
  557. }
  558. if (zOther->op == AttributeOperator::NotEquals)
  559. {
  560. if (v2 < v1
  561. || (v2 == v1 && op == AttributeOperator::GreaterThan))
  562. {
  563. return 1;
  564. }
  565. }
  566. }
  567. }
  568. if (zOther->op == AttributeOperator::LessThan
  569. || zOther->op == AttributeOperator::LessThanOrEquals
  570. || zOther->op == AttributeOperator::GreaterThan
  571. || zOther->op == AttributeOperator::GreaterThanOrEquals)
  572. {
  573. double v1 = (double)zOther->value;
  574. double v2 = (double)value;
  575. if (zOther->op == AttributeOperator::LessThan
  576. || zOther->op == AttributeOperator::LessThanOrEquals)
  577. {
  578. if (op == AttributeOperator::Equals)
  579. {
  580. if (v2 < v1
  581. || (v2 == v1
  582. && zOther->op
  583. == AttributeOperator::LessThanOrEquals))
  584. {
  585. return 1;
  586. }
  587. }
  588. if (op == AttributeOperator::NotEquals)
  589. {
  590. if (v2 > v1
  591. || (v2 == v1
  592. && zOther->op == AttributeOperator::LessThan))
  593. {
  594. op = zOther->op;
  595. value = zOther->value;
  596. return 1;
  597. }
  598. }
  599. }
  600. else if (zOther->op == AttributeOperator::GreaterThan
  601. || zOther->op == AttributeOperator::GreaterThanOrEquals)
  602. {
  603. if (op == AttributeOperator::Equals)
  604. {
  605. if (v2 > v1
  606. || (v2 == v1
  607. && zOther->op
  608. == AttributeOperator::GreaterThanOrEquals))
  609. {
  610. return 1;
  611. }
  612. }
  613. if (op == AttributeOperator::NotEquals)
  614. {
  615. if (v2 < v1
  616. || (v2 == v1
  617. && zOther->op == AttributeOperator::GreaterThan))
  618. {
  619. op = zOther->op;
  620. value = zOther->value;
  621. return 1;
  622. }
  623. }
  624. }
  625. }
  626. }
  627. return 0;
  628. }
  629. int Requirement::getItemType()
  630. {
  631. if (attribute.istGleich("Type") && op == AttributeOperator::Equals)
  632. {
  633. return (int)value;
  634. }
  635. return -1;
  636. }
  637. Framework::Text Requirement::getDescription()
  638. {
  639. Framework::Text result = attribute;
  640. result += " ";
  641. switch (op)
  642. {
  643. case AttributeOperator::Equals:
  644. result += "is";
  645. break;
  646. case AttributeOperator::NotEquals:
  647. result += "is not";
  648. break;
  649. case AttributeOperator::LessThan:
  650. result += "is less than";
  651. break;
  652. case AttributeOperator::LessThanOrEquals:
  653. result += "is less than or equal to";
  654. break;
  655. case AttributeOperator::GreaterThan:
  656. result += "is greater than";
  657. break;
  658. case AttributeOperator::GreaterThanOrEquals:
  659. result += "is greater than or equal to";
  660. break;
  661. }
  662. result += " ";
  663. result += value;
  664. return result;
  665. }
  666. RequirementSet::RequirementSet()
  667. : Framework::ReferenceCounter()
  668. {}
  669. void RequirementSet::addRequirement(Requirement* zReq)
  670. {
  671. for (Requirement* req : requirements)
  672. {
  673. if (req->merge(zReq)) return;
  674. }
  675. requirements.add(zReq->clone());
  676. }
  677. RequirementSet* RequirementSet::clone()
  678. {
  679. RequirementSet* result = new RequirementSet();
  680. for (Requirement* req : requirements)
  681. {
  682. result->requirements.add(req->clone());
  683. }
  684. return result;
  685. }
  686. void RequirementSet::addRequirements(RequirementSet* zOther)
  687. {
  688. for (Requirement* req : zOther->requirements)
  689. {
  690. addRequirement(req);
  691. }
  692. }
  693. void RequirementSet::negate(LogicTree* zNode)
  694. {
  695. for (Requirement* req : requirements)
  696. {
  697. Requirement* negate = req->clone();
  698. negate->negate();
  699. RequirementSet* set = new RequirementSet();
  700. set->addRequirement(negate);
  701. negate->release();
  702. zNode->addChildren(new LogicTree(LogicalOperator::OR, set));
  703. }
  704. }
  705. bool RequirementSet::isNoneMatch() const
  706. {
  707. for (Requirement* req : requirements)
  708. {
  709. for (Requirement* req2 : requirements)
  710. {
  711. if (req != req2)
  712. {
  713. if (req->contradicts(req2))
  714. {
  715. return 1;
  716. }
  717. }
  718. }
  719. }
  720. return 0;
  721. }
  722. bool RequirementSet::isAnyMatch() const
  723. {
  724. return requirements.getEintragAnzahl() == 0;
  725. }
  726. Framework::Text RequirementSet::renderToTooltip() const
  727. {
  728. int itemType = -1;
  729. for (Requirement* req : requirements)
  730. {
  731. int rT = req->getItemType();
  732. if (rT != -1)
  733. {
  734. if (itemType == -1)
  735. {
  736. itemType = rT;
  737. }
  738. else if (itemType != rT)
  739. {
  740. itemType = -2;
  741. }
  742. }
  743. }
  744. Framework::Text result = "";
  745. if (itemType == -2)
  746. {
  747. return "No Item matches this filter";
  748. }
  749. else if (itemType == -1)
  750. {
  751. result += "Any Item";
  752. }
  753. else
  754. {
  755. result += zItemType(itemType)->getName();
  756. }
  757. if (requirements.getEintragAnzahl() > 0)
  758. {
  759. bool first = 1;
  760. for (Requirement* req : requirements)
  761. {
  762. if (req->getItemType() == -1)
  763. {
  764. if (first)
  765. {
  766. result += ":\n";
  767. first = 0;
  768. }
  769. result += " ";
  770. result += req->getDescription();
  771. result += "\n";
  772. }
  773. }
  774. }
  775. return result;
  776. }
  777. Framework::Bild* RequirementSet::getIcon() const
  778. {
  779. int itemType = -1;
  780. for (Requirement* req : requirements)
  781. {
  782. int rT = req->getItemType();
  783. if (rT != -1)
  784. {
  785. if (itemType == -1)
  786. {
  787. itemType = rT;
  788. }
  789. else if (itemType != rT)
  790. {
  791. itemType = -2;
  792. }
  793. }
  794. }
  795. if (itemType == -2)
  796. {
  797. LTDBDatei dat;
  798. dat.setDatei(new Text("data/bilder/gui_icons.ltdb"));
  799. dat.leseDaten(0);
  800. return dat.laden(0, new Text("noitem.png"));
  801. }
  802. else if (itemType == -1)
  803. {
  804. LTDBDatei dat;
  805. dat.setDatei(new Text("data/images/gui_icons.ltdb"));
  806. dat.leseDaten(0);
  807. return dat.laden(0, new Text("anyitem.png"));
  808. }
  809. else
  810. {
  811. return dynamic_cast<Framework::Bild*>(
  812. zItemType(itemType)->zIcon()->getThis());
  813. }
  814. }
  815. int RequirementSet::getItemType() const
  816. {
  817. int itemType = -1;
  818. for (Requirement* req : requirements)
  819. {
  820. int rT = req->getItemType();
  821. if (rT != -1)
  822. {
  823. if (itemType == -1)
  824. {
  825. itemType = rT;
  826. }
  827. else if (itemType != rT)
  828. {
  829. itemType = -2;
  830. }
  831. }
  832. }
  833. return itemType;
  834. }
  835. LogicTree::LogicTree(LogicalOperator op, RequirementSet* set)
  836. : Framework::ReferenceCounter(),
  837. op(op),
  838. requirementSet(set)
  839. {}
  840. LogicTree::~LogicTree()
  841. {
  842. if (requirementSet) requirementSet->release();
  843. }
  844. Framework::RCArray<RequirementSet>* LogicTree::multiply(
  845. Framework::RCArray<RequirementSet>* a,
  846. Framework::RCArray<RequirementSet>* b)
  847. {
  848. Framework::RCArray<RequirementSet>* result
  849. = new Framework::RCArray<RequirementSet>();
  850. for (RequirementSet* aSet : *a)
  851. {
  852. for (RequirementSet* bSet : *b)
  853. {
  854. RequirementSet* set = aSet->clone();
  855. set->addRequirements(bSet);
  856. if (!set->isNoneMatch())
  857. {
  858. if (set->isAnyMatch())
  859. {
  860. result->leeren();
  861. result->add(set);
  862. a->release();
  863. b->release();
  864. return result;
  865. }
  866. else
  867. {
  868. result->add(set);
  869. }
  870. }
  871. else
  872. {
  873. set->release();
  874. }
  875. }
  876. }
  877. return result;
  878. }
  879. void LogicTree::addChildren(LogicTree* tree)
  880. {
  881. children.add(tree);
  882. }
  883. LogicTree* LogicTree::clone()
  884. {
  885. LogicTree* result = new LogicTree(op, requirementSet->clone());
  886. for (LogicTree* child : children)
  887. {
  888. result->children.add(child->clone());
  889. }
  890. return result;
  891. }
  892. void LogicTree::negate()
  893. {
  894. if (requirementSet != 0)
  895. {
  896. requirementSet->negate(this);
  897. requirementSet->release();
  898. requirementSet = 0;
  899. }
  900. else
  901. {
  902. if (op == LogicalOperator::AND)
  903. {
  904. op = LogicalOperator::OR;
  905. }
  906. else if (op == LogicalOperator::OR)
  907. {
  908. op = LogicalOperator::AND;
  909. }
  910. for (LogicTree* child : children)
  911. {
  912. child->negate();
  913. }
  914. }
  915. }
  916. Framework::RCArray<RequirementSet>* LogicTree::resolve()
  917. {
  918. if (requirementSet != 0)
  919. {
  920. Framework::RCArray<RequirementSet>* result
  921. = new Framework::RCArray<RequirementSet>();
  922. result->add(dynamic_cast<RequirementSet*>(requirementSet->getThis()));
  923. return result;
  924. }
  925. else
  926. {
  927. Framework::RCArray<RequirementSet>* result = 0;
  928. if (op == LogicalOperator::OR)
  929. {
  930. result = new Framework::RCArray<RequirementSet>();
  931. for (LogicTree* child : children)
  932. {
  933. Framework::RCArray<RequirementSet>* childSet = child->resolve();
  934. for (RequirementSet* set : *childSet)
  935. {
  936. result->add(dynamic_cast<RequirementSet*>(set->getThis()));
  937. }
  938. childSet->release();
  939. }
  940. }
  941. else if (op == LogicalOperator::AND)
  942. {
  943. result = children.z(0)->resolve();
  944. for (int i = 1; i < children.getEintragAnzahl(); i++)
  945. {
  946. Framework::RCArray<RequirementSet>* childSet
  947. = children.z(i)->resolve();
  948. result = multiply(result, childSet);
  949. }
  950. }
  951. if (result != 0)
  952. {
  953. for (int i = 0; i < result->getEintragAnzahl(); i++)
  954. {
  955. if (result->z(i)->isNoneMatch())
  956. {
  957. result->remove(i);
  958. i--;
  959. }
  960. else if (result->z(i)->isAnyMatch())
  961. {
  962. Framework::RCArray<RequirementSet>* anyMatch
  963. = new Framework::RCArray<RequirementSet>();
  964. anyMatch->add(result->get(i));
  965. result->release();
  966. return anyMatch;
  967. }
  968. }
  969. }
  970. return result;
  971. }
  972. }