ItemBar.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. #include <XML.h>
  2. #include <Bild.h>
  3. #include "ItemBar.h"
  4. #include "Globals.h"
  5. using namespace Framework;
  6. ItemBarElement::ItemBarElement()
  7. : UIMLElement()
  8. {}
  9. //! prüft, ob dieses UIML Element für ein bestimmtes xml Element zuständig ist
  10. bool ItemBarElement::isApplicableFor(Framework::XML::Element& element)
  11. {
  12. return element.getName().istGleich("itemBar");
  13. }
  14. //! erstellt eine neue Zeichnung zu einem gegebenen xml Element
  15. Framework::Zeichnung* ItemBarElement::parseElement(Framework::XML::Element& element, Framework::UIMLContainer& generalFactory)
  16. {
  17. return new ItemBarView(element.getAttributeValue("id"), (int)element.getAttributeValue("rowSize"), (int)element.getAttributeValue("target"), element.getAttributeValue("slotNameFilter"));
  18. }
  19. //! wendet die layout parameter zu einer Zeichnung an
  20. void ItemBarElement::layout(Framework::XML::Element& element, Framework::Zeichnung& z, int pWidth, int pHeight, Framework::UIMLContainer& generalLayouter)
  21. {
  22. UIMLElement::layout(element, z, pWidth, pHeight, generalLayouter);
  23. }
  24. ItemBarView::ItemBarView(Framework::Text id, int rowSize, int targetEntity, Framework::Text slotNameFilter)
  25. : ZeichnungHintergrund(),
  26. id(id),
  27. rowSize(rowSize),
  28. targetEntity(targetEntity),
  29. slotNameFilter(slotNameFilter),
  30. slots(0),
  31. leftHandPos(0)
  32. {
  33. char* msg = new char[id.getLength() + slotNameFilter.getLength() + 3];
  34. msg[0] = 100;
  35. msg[1] = (char)id.getLength();
  36. memcpy(msg + 2, id.getText(), id.getLength());
  37. msg[2 + id.getLength()] = (char)slotNameFilter.getLength();
  38. memcpy(msg + 3 + id.getLength(), slotNameFilter.getText(), slotNameFilter.getLength());
  39. network->zFactoryClient()->entityAPIRequest(targetEntity, msg, id.getLength() + slotNameFilter.getLength() + 3);
  40. delete[] msg;
  41. }
  42. ItemBarView::~ItemBarView()
  43. {
  44. if (slots)
  45. slots->release();
  46. }
  47. void ItemBarView::api(char* message)
  48. {
  49. switch (message[0])
  50. {
  51. case 0:
  52. // send inventory content
  53. {
  54. Array<SlotInfo>* slots = new Array<SlotInfo>();
  55. int count = *(int*)(++message);
  56. for (int i = 0; i < count; i++)
  57. {
  58. SlotInfo info;
  59. info.id = *(int*)(message += 4);
  60. info.itemCount = *(int*)(message += 4);
  61. if (info.itemCount > 0)
  62. {
  63. info.damage = *(float*)(message += 4);
  64. info.maxDamage = *(float*)(message += 4);
  65. info.durability = *(float*)(message += 4);
  66. info.maxDurability = *(float*)(message += 4);
  67. info.zItem = itemIcons->z(*(int*)(message += 4));
  68. }
  69. slots->add(info);
  70. }
  71. postAction([this, slots]()
  72. {
  73. if (this->slots)
  74. this->slots->release();
  75. this->slots = slots;
  76. });
  77. break;
  78. }
  79. case 1: // set count of items
  80. {
  81. int id = *(int*)(message + 1);
  82. int count = *(int*)(message + 5);
  83. for (int i = 0; i < slots->getEintragAnzahl(); i++)
  84. {
  85. if (slots->get(i).id == id)
  86. {
  87. SlotInfo info = slots->get(i);
  88. info.itemCount = count;
  89. slots->set(info, i);
  90. break;
  91. }
  92. }
  93. break;
  94. }
  95. case 2: // add new stack
  96. {
  97. int id = *(int*)(message + 1);
  98. for (int i = 0; i < slots->getEintragAnzahl(); i++)
  99. {
  100. if (slots->get(i).id == id)
  101. {
  102. SlotInfo info = slots->get(i);
  103. info.itemCount = *(int*)(message + 5);
  104. info.damage = *(float*)(message + 9);
  105. info.maxDamage = *(float*)(message + 13);
  106. info.durability = *(float*)(message + 17);
  107. info.maxDurability = *(float*)(message + 21);
  108. info.zItem = itemIcons->z(*(int*)(message + 25));
  109. slots->set(info, i);
  110. break;
  111. }
  112. }
  113. break;
  114. }
  115. case 3: // set selected slot
  116. {
  117. leftHandPos = *(int*)(message + 1);
  118. break;
  119. }
  120. }
  121. }
  122. void ItemBarView::render(Bild& rObj)
  123. {
  124. ZeichnungHintergrund::render(rObj);
  125. if (!rObj.setDrawOptions(pos.x, pos.y, gr.x, gr.y))
  126. return;
  127. if (slots)
  128. {
  129. int x = 0;
  130. int y = 0;
  131. int rowCount = 0;
  132. int index = 0;
  133. for (SlotInfo info : *slots)
  134. {
  135. info.render(x, y, rObj, index == leftHandPos);
  136. x += 60;
  137. if (++rowCount >= rowSize)
  138. {
  139. y += 60;
  140. x = 0;
  141. rowCount = 0;
  142. }
  143. index++;
  144. }
  145. }
  146. rObj.releaseDrawOptions();
  147. }