ItemFilter.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. #include "ItemFilter.h"
  2. #include "Item.h"
  3. #include "ItemSlot.h"
  4. #include "ItemType.h"
  5. ItemFilter::ItemFilter()
  6. : ReferenceCounter()
  7. {}
  8. bool ItemFilter::matchItem(const Item* zItem) const
  9. {
  10. return 1;
  11. }
  12. bool ItemFilter::matchSourceSlot(ItemSlot* zSlot) const
  13. {
  14. return zSlot->zStack() ? matchItem(zSlot->zStack()->zItem()) : 0;
  15. }
  16. bool ItemFilter::matchTargetSlot(ItemSlot* zSlot) const
  17. {
  18. return 1;
  19. }
  20. CombinedItemFilter::CombinedItemFilter(ItemFilter* filterA,
  21. ItemFilter* filterB,
  22. std::function<bool(bool, bool)> op)
  23. : ItemFilter(),
  24. filterA(filterA),
  25. filterB(filterB),
  26. op(op)
  27. {}
  28. CombinedItemFilter::~CombinedItemFilter()
  29. {
  30. filterA->release();
  31. filterB->release();
  32. }
  33. bool CombinedItemFilter::matchItem(const Item* zItem) const
  34. {
  35. return op(filterA->matchItem(zItem), filterB->matchItem(zItem));
  36. }
  37. bool CombinedItemFilter::matchSourceSlot(ItemSlot* zSlot) const
  38. {
  39. return op(filterA->matchSourceSlot(zSlot), filterB->matchSourceSlot(zSlot));
  40. }
  41. bool CombinedItemFilter::matchTargetSlot(ItemSlot* zSlot) const
  42. {
  43. return op(filterA->matchTargetSlot(zSlot), filterB->matchTargetSlot(zSlot));
  44. }
  45. Framework::Text CombinedItemFilter::getImageUIML() const
  46. {
  47. return filterA->getImageUIML() + filterB->getImageUIML();
  48. }
  49. Framework::Text CombinedItemFilter::getLogicUIML() const
  50. {
  51. Framework::Text result = "<operator result_0_0=\"";
  52. result += op(0, 0);
  53. result += "\" result_0_1=\"";
  54. result += op(0, 1);
  55. result += "\" result_1_0=\"";
  56. result += op(1, 0);
  57. result += "\" result_1_1=\"";
  58. result += op(1, 1);
  59. result += ">";
  60. result += filterA->getLogicUIML();
  61. result += filterB->getLogicUIML();
  62. result += "</operator>";
  63. return result;
  64. }
  65. AnyItemFilter::AnyItemFilter()
  66. : ItemFilter()
  67. {}
  68. bool AnyItemFilter::matchItem(const Item* zItem) const
  69. {
  70. return true;
  71. }
  72. Framework::Text AnyItemFilter::getImageUIML() const
  73. {
  74. return "<img str=\"bilder/gui_icons.ltdb/anyItem.png\"/>";
  75. }
  76. Framework::Text AnyItemFilter::getLogicUIML() const
  77. {
  78. return "<anyItem/>";
  79. }
  80. TypeItemFilter::TypeItemFilter(const ItemType* zType)
  81. : ItemFilter(),
  82. zType(zType)
  83. {}
  84. bool TypeItemFilter::matchItem(const Item* zItem) const
  85. {
  86. return zItem->zItemType() == zType;
  87. }
  88. Framework::Text TypeItemFilter::getImageUIML() const
  89. {
  90. Framework::Text result = "<img itemType=\"";
  91. result += zType->getId();
  92. result += "\"/>";
  93. return result;
  94. }
  95. Framework::Text TypeItemFilter::getLogicUIML() const
  96. {
  97. Framework::Text result = "<attribute name=\"Type\" operator=\"=\" value=\"";
  98. result += zType->getName();
  99. result += "\"/>";
  100. return result;
  101. }
  102. SpecificSlotFilter::SpecificSlotFilter(int sourceSlotId, int targetSlotId)
  103. : ItemFilter(),
  104. sourceSlotId(sourceSlotId),
  105. targetSlotId(targetSlotId)
  106. {}
  107. bool SpecificSlotFilter::matchSourceSlot(ItemSlot* zSlot) const
  108. {
  109. return sourceSlotId == zSlot->getId();
  110. }
  111. bool SpecificSlotFilter::matchTargetSlot(ItemSlot* zSlot) const
  112. {
  113. return targetSlotId == zSlot->getId();
  114. }
  115. Framework::Text SpecificSlotFilter::getImageUIML() const
  116. {
  117. return "<img str=\"bilder/gui_icons.ltdb/anyItem.png\"/>";
  118. }
  119. Framework::Text SpecificSlotFilter::getLogicUIML() const
  120. {
  121. return "<anyItem/>";
  122. }
  123. SourceSlotBlacklistFilter::SourceSlotBlacklistFilter()
  124. : ItemFilter()
  125. {}
  126. void SourceSlotBlacklistFilter::addBlackListSlotId(int id)
  127. {
  128. blackList.add(id);
  129. }
  130. bool SourceSlotBlacklistFilter::matchSourceSlot(ItemSlot* zSlot) const
  131. {
  132. for (int black : blackList)
  133. {
  134. if (black == zSlot->getId()) return 0;
  135. }
  136. return 1;
  137. }
  138. bool SourceSlotBlacklistFilter::matchTargetSlot(ItemSlot* zSlot) const
  139. {
  140. return 1;
  141. }
  142. Framework::Text SourceSlotBlacklistFilter::getImageUIML() const
  143. {
  144. return "<img str=\"bilder/gui_icons.ltdb/anyItem.png\"/>";
  145. }
  146. Framework::Text SourceSlotBlacklistFilter::getLogicUIML() const
  147. {
  148. return "<anyItem/>";
  149. }
  150. TargetSlotBlacklistFilter::TargetSlotBlacklistFilter()
  151. : ItemFilter()
  152. {}
  153. void TargetSlotBlacklistFilter::addBlackListSlotId(int id)
  154. {
  155. blackList.add(id);
  156. }
  157. bool TargetSlotBlacklistFilter::matchSourceSlot(ItemSlot* zSlot) const
  158. {
  159. return 1;
  160. }
  161. bool TargetSlotBlacklistFilter::matchTargetSlot(ItemSlot* zSlot) const
  162. {
  163. for (int black : blackList)
  164. {
  165. if (black == zSlot->getId()) return 0;
  166. }
  167. return 1;
  168. }
  169. Framework::Text TargetSlotBlacklistFilter::getImageUIML() const
  170. {
  171. return "<img str=\"bilder/gui_icons.ltdb/anyItem.png\"/>";
  172. }
  173. Framework::Text TargetSlotBlacklistFilter::getLogicUIML() const
  174. {
  175. return "<anyItem/>";
  176. }