ShapedRecipie.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. #include "ShapedRecipie.h"
  2. #include <Bild.h>
  3. #include <XML.h>
  4. ShapedRecipieElement::ShapedRecipieElement()
  5. : Framework::UIMLElement()
  6. {}
  7. //! prüft, ob dieses UIML Element für ein bestimmtes xml Element zuständig
  8. //! ist
  9. bool ShapedRecipieElement::isApplicableFor(Framework::XML::Element& element)
  10. {
  11. return element.getName().istGleich("recipie")
  12. && element.getAttributeValue("type").istGleich("shaped");
  13. }
  14. //! erstellt eine neue Zeichnung zu einem gegebenen xml Element
  15. Framework::Zeichnung* ShapedRecipieElement::parseElement(
  16. Framework::XML::Element& element, Framework::UIMLContainer& generalFactory)
  17. {
  18. int width = (int)element.getAttributeValue("width");
  19. int height = (int)element.getAttributeValue("height");
  20. ShapedRecipie* recipie = new ShapedRecipie(width, height);
  21. element.selectChildsByName("ingredients")
  22. .selectChildren()
  23. .forEach([recipie, &generalFactory](Framework::XML::Element* zElement) {
  24. recipie->addIngredient(
  25. generalFactory.parseElement(*zElement, generalFactory));
  26. });
  27. element.selectChildsByName("outputs").selectChildren().forEach(
  28. [recipie, &generalFactory](Framework::XML::Element* zElement) {
  29. recipie->addOutput(
  30. generalFactory.parseElement(*zElement, generalFactory));
  31. });
  32. return recipie;
  33. }
  34. //! wendet die layout parameter zu einer Zeichnung an
  35. void ShapedRecipieElement::layout(Framework::XML::Element& element,
  36. Framework::Zeichnung& z,
  37. int pWidth,
  38. int pHeight,
  39. Framework::UIMLContainer& generalLayouter)
  40. {
  41. UIMLElement::layout(element, z, pWidth, pHeight, generalLayouter);
  42. int width = (int)element.getAttributeValue("width");
  43. int height = (int)element.getAttributeValue("height");
  44. int outputs
  45. = element.selectChildsByName("outputs").selectChildren().getSize();
  46. int outputCols = outputs / height;
  47. if (outputs % height != 0)
  48. {
  49. outputCols++;
  50. }
  51. if (outputCols == 0)
  52. {
  53. outputCols = 1;
  54. }
  55. z.setWidth(width * 60 + 50 + outputCols * 60 - 10);
  56. z.setHeight(height * 60 - 10);
  57. element.selectChildren()
  58. .selectChildren()
  59. .forEach([&z, &generalLayouter](Framework::XML::Element* zElement) {
  60. Framework::Zeichnung* child = 0;
  61. if (zElement->hasAttribute("id"))
  62. {
  63. child = generalLayouter.zZeichnungById(
  64. zElement->getAttributeValue("id"));
  65. }
  66. if (child)
  67. {
  68. generalLayouter.layout(*zElement,
  69. *child,
  70. z.getBreite(),
  71. z.getHeight(),
  72. generalLayouter);
  73. }
  74. });
  75. }
  76. ShapedRecipie::ShapedRecipie(int width, int height)
  77. : Framework::ZeichnungHintergrund(),
  78. width(width),
  79. height(height)
  80. {
  81. setStyle(Framework::Zeichnung::Style::Erlaubt
  82. | Framework::Zeichnung::Style::Sichtbar);
  83. }
  84. void ShapedRecipie::addIngredient(Zeichnung* ingredient)
  85. {
  86. ingredients.add(ingredient);
  87. }
  88. void ShapedRecipie::addOutput(Zeichnung* output)
  89. {
  90. outputs.add(output);
  91. }
  92. bool ShapedRecipie::tick(double tickVal)
  93. {
  94. bool ret = false;
  95. for (Zeichnung* ingredient : ingredients)
  96. {
  97. ret |= ingredient->tick(tickVal);
  98. }
  99. for (Zeichnung* output : outputs)
  100. {
  101. ret |= output->tick(tickVal);
  102. }
  103. return ret;
  104. }
  105. void ShapedRecipie::render(Framework::Bild& rObj)
  106. {
  107. if (!hatStyle(Zeichnung::Style::Sichtbar)) return;
  108. ZeichnungHintergrund::render(rObj);
  109. if (!rObj.setDrawOptions(pos.x, pos.y, gr.x, gr.y)) return;
  110. int x = 0;
  111. int y = 0;
  112. for (Zeichnung* ingredient : ingredients)
  113. {
  114. ingredient->setX(x);
  115. ingredient->setY(y);
  116. ingredient->render(rObj);
  117. x += 60;
  118. if (x >= width * 60)
  119. {
  120. x = 0;
  121. y += 60;
  122. }
  123. }
  124. rObj.fillRegion(width * 60, gr.y / 2 - 5, 25, 10, 0xFF52525E);
  125. rObj.drawDreieck(Framework::Punkt(width * 60 + 25, gr.y / 2 - 15),
  126. Framework::Punkt(width * 60 + 40, gr.y / 2),
  127. Framework::Punkt(width * 60 + 25, gr.y / 2 + 15),
  128. 0xFF52525E);
  129. x = width * 60 + 50;
  130. y = 0;
  131. for (Zeichnung* output : outputs)
  132. {
  133. output->setX(x);
  134. output->setY(y);
  135. output->render(rObj);
  136. y += 60;
  137. }
  138. rObj.releaseDrawOptions();
  139. }
  140. void ShapedRecipie::doMausEreignis(Framework::MausEreignis& me, bool userRet)
  141. {
  142. for (Zeichnung* ingredient : ingredients)
  143. {
  144. ingredient->doPublicMausEreignis(me);
  145. }
  146. for (Zeichnung* output : outputs)
  147. {
  148. output->doPublicMausEreignis(me);
  149. }
  150. }