CraftingRecipies.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. #include "CraftingRecipies.h"
  2. #include <Image.h>
  3. #include <XML.h>
  4. #include "Globals.h"
  5. #include "ItemType.h"
  6. #include "RecipieGroup.h"
  7. CraftingRecipiesElement::CraftingRecipiesElement()
  8. : Framework::UIMLElement()
  9. {}
  10. //! prüft, ob dieses UIML Element für ein bestimmtes xml Element zuständig
  11. //! ist
  12. bool CraftingRecipiesElement::isApplicableFor(Framework::XML::Element& element)
  13. {
  14. return element.getName().isEqual("craftingRecipies");
  15. }
  16. //! erstellt eine neue Drawable zu einem gegebenen xml Element
  17. Framework::Drawable* CraftingRecipiesElement::parseElement(
  18. Framework::XML::Element& element, Framework::UIMLContainer& generalFactory)
  19. {
  20. CraftingRecipies* recipies = new CraftingRecipies();
  21. element.selectChildsByName("craftingRecipieGroup")
  22. .forEach([recipies, &generalFactory](
  23. Framework::XML::Element* zElement) {
  24. RecipieGroup* group = new RecipieGroup();
  25. group->setToolTip(zElement->getAttributeValue("name"));
  26. if (zElement->hasAttribute("itemIcon"))
  27. {
  28. int type = (int)zElement->getAttributeValue("itemIcon");
  29. group->setIcon(dynamic_cast<Framework::Image*>(
  30. zItemType(type)->zIcon()->getThis()));
  31. }
  32. zElement->selectChildren().forEach(
  33. [group, &generalFactory](Framework::XML::Element* zRecipie) {
  34. group->addRecipie(
  35. generalFactory.parseElement(*zRecipie, generalFactory));
  36. });
  37. recipies->addRecipieGroup(group);
  38. });
  39. return recipies;
  40. }
  41. bool CraftingRecipiesElement::updateElement(Framework::XML::Element& element,
  42. Framework::Drawable& z,
  43. Framework::UIMLContainer& generalFactory)
  44. {
  45. return false;
  46. }
  47. //! wendet die layout parameter zu einer Drawable an
  48. void CraftingRecipiesElement::layout(Framework::XML::Element& element,
  49. Framework::Drawable& z,
  50. int pWidth,
  51. int pHeight,
  52. Framework::UIMLContainer& generalLayouter)
  53. {
  54. z.setSize(500, 500); // TODO: calculate size
  55. UIMLElement::layout(element, z, pWidth, pHeight, generalLayouter);
  56. element.selectChildsByName("craftingRecipieGroup")
  57. .selectChildren()
  58. .forEach([&z, &generalLayouter](Framework::XML::Element* zElement) {
  59. Framework::Drawable* child = 0;
  60. if (zElement->hasAttribute("id"))
  61. {
  62. child = generalLayouter.zDrawableById(
  63. zElement->getAttributeValue("id"));
  64. }
  65. if (child)
  66. {
  67. generalLayouter.layout(*zElement,
  68. *child,
  69. z.getWidth(),
  70. z.getHeight(),
  71. generalLayouter);
  72. }
  73. });
  74. }
  75. CraftingRecipies::CraftingRecipies()
  76. : Framework::DrawableBackground()
  77. {
  78. setStyle(Framework::Drawable::Style::Allowed
  79. | Framework::Drawable::Style::Visible);
  80. previousPage = uiFactory.createButton(uiFactory.initParam);
  81. previousPage->setText("<");
  82. previousPage->setToolTipText("Previous Page",
  83. uiFactory.initParam.bildschirm,
  84. uiFactory.initParam.font);
  85. previousPage->setSize(20, 20);
  86. previousPage->setPosition(0, 35);
  87. previousPage->setStyle(
  88. Framework::Button::Style::Normal & ~Framework::Button::Style::Visible);
  89. previousPage->setMouseEvent([this](void* p, void* z, MouseEvent me) {
  90. if (me.id == ME_RLeft)
  91. {
  92. RecipieGroup* previous = 0;
  93. for (RecipieGroup* group : recipieGroups)
  94. {
  95. if (group->hasStyle(Drawable::Style::Visible) && previous)
  96. {
  97. group->removeStyle(Drawable::Style::Visible);
  98. previous->addStyle(Drawable::Style::Visible);
  99. }
  100. previous = group;
  101. }
  102. }
  103. return 1;
  104. });
  105. nextPage = uiFactory.createButton(uiFactory.initParam);
  106. nextPage->setText(">");
  107. nextPage->setToolTipText(
  108. "Next Page", uiFactory.initParam.bildschirm, uiFactory.initParam.font);
  109. nextPage->setSize(20, 20);
  110. nextPage->setPosition(0, 35);
  111. nextPage->setStyle(
  112. Framework::Button::Style::Normal & ~Framework::Button::Style::Visible);
  113. nextPage->setMouseEvent([this](void* p, void* z, MouseEvent me) {
  114. if (me.id == ME_RLeft)
  115. {
  116. bool next = 0;
  117. int index = 0;
  118. for (RecipieGroup* group : recipieGroups)
  119. {
  120. if (next)
  121. {
  122. group->addStyle(Drawable::Style::Visible);
  123. break;
  124. }
  125. if (group->hasStyle(Drawable::Style::Visible)
  126. && recipieGroups.getEntryCount() > index + 1)
  127. {
  128. group->removeStyle(Drawable::Style::Visible);
  129. next = 1;
  130. }
  131. index++;
  132. }
  133. }
  134. return 1;
  135. });
  136. }
  137. void CraftingRecipies::addRecipieGroup(RecipieGroup* recipieGroup)
  138. {
  139. recipieGroups.add(recipieGroup);
  140. }
  141. bool CraftingRecipies::tick(double tickVal)
  142. {
  143. int visibleGroupIndex = -1;
  144. int index = 0;
  145. for (RecipieGroup* group : recipieGroups)
  146. {
  147. if (group->hasStyle(Drawable::Style::Visible))
  148. {
  149. visibleGroupIndex = index;
  150. }
  151. rend |= group->tick(tickVal);
  152. index++;
  153. }
  154. nextPage->setX(getWidth() - 20);
  155. previousPage->setStyle(Button::Style::Visible, visibleGroupIndex > 0);
  156. nextPage->setStyle(Button::Style::Visible,
  157. visibleGroupIndex < recipieGroups.getEntryCount() - 1);
  158. rend |= previousPage->tick(tickVal);
  159. rend |= nextPage->tick(tickVal);
  160. return DrawableBackground::tick(tickVal);
  161. }
  162. void CraftingRecipies::render(Framework::Image& rObj)
  163. {
  164. if (!hasStyle(Drawable::Style::Visible)) return;
  165. DrawableBackground::render(rObj);
  166. if (!rObj.setDrawOptions(pos.x, pos.y, gr.x, gr.y)) return;
  167. int visibleGroupIndex = -1;
  168. int index = 0;
  169. for (RecipieGroup* group : recipieGroups)
  170. {
  171. if (group->hasStyle(Drawable::Style::Visible))
  172. {
  173. visibleGroupIndex = index;
  174. }
  175. group->setPosition(0, 60);
  176. group->setSize(getWidth(), getHeight() - 60);
  177. group->render(rObj);
  178. index++;
  179. }
  180. int totalWidth = recipieGroups.getEntryCount() * 31 + 1;
  181. int possibleDisplayedIcons = getWidth() / 31;
  182. int x = 0;
  183. if (possibleDisplayedIcons < recipieGroups.getEntryCount()
  184. && visibleGroupIndex > possibleDisplayedIcons / 2)
  185. {
  186. x = (visibleGroupIndex - possibleDisplayedIcons / 2) * -31;
  187. if (recipieGroups.getEntryCount() * 31 + 1 + x < getWidth())
  188. {
  189. x += getWidth() - (recipieGroups.getEntryCount() * 31 + 1 + x);
  190. }
  191. }
  192. index = 0;
  193. for (RecipieGroup* group : recipieGroups)
  194. {
  195. rObj.fillRegion(x, 0, 30, 30, 0xFF000000);
  196. if (group->zIcon())
  197. {
  198. rObj.alphaImageScaled(x, 0, 30, 30, *group->zIcon());
  199. }
  200. else
  201. {
  202. // TODO: any item icon
  203. }
  204. rObj.drawLineH(x - 1, 0, 32, 0xFFFFFFFF);
  205. if (index != visibleGroupIndex)
  206. {
  207. rObj.drawLineH(x - 1, 30, 32, 0xFFFFFFFF);
  208. }
  209. rObj.drawLineV(x + 30, 0, 30, 0xFFFFFFFF);
  210. }
  211. Framework::TextRenderer tr(
  212. dynamic_cast<Framework::Font*>(uiFactory.initParam.font->getThis()));
  213. tr.setFontSize(12);
  214. Text t = Text("Machine page ") + Text(visibleGroupIndex + 1) + Text(" of ")
  215. + Text(recipieGroups.getEntryCount());
  216. int tbr = tr.getTextWidth(t);
  217. tr.renderText(getWidth() / 2 - tbr / 2, 34, t, rObj, 0xFFFFFFFF);
  218. previousPage->render(rObj);
  219. nextPage->render(rObj);
  220. rObj.releaseDrawOptions();
  221. }
  222. void CraftingRecipies::doMouseEvent(Framework::MouseEvent& me, bool userRet)
  223. {
  224. previousPage->doPublicMouseEvent(me);
  225. nextPage->doPublicMouseEvent(me);
  226. for (RecipieGroup* group : recipieGroups)
  227. {
  228. group->doPublicMouseEvent(me);
  229. }
  230. }