CraftingRecipies.cpp 7.9 KB

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