ItemList.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. #include "ItemList.h"
  2. #include <FileSystem.h>
  3. #include <Scroll.h>
  4. #include <TextField.h>
  5. #include "Game.h"
  6. #include "Globals.h"
  7. #include "UIMLToolTip.h"
  8. #include "World.h"
  9. #include <Globals.h>
  10. ItemList::ItemList()
  11. : DrawableBackground(),
  12. currentTooltipSlot(-1)
  13. {
  14. setStyle(DrawableBackground::Style::Visible
  15. | DrawableBackground::Style::Allowed);
  16. slotList = new int[itemTypeCount];
  17. memset(slotList, 0, sizeof(int) * itemTypeCount);
  18. slotCount = 0;
  19. setNeedToolTipEvent([this](Drawable* z, Point p) {
  20. int slot = getSlotByLocalPos(p);
  21. if (currentTooltipSlot != slot && currentTooltipSlot != -1)
  22. {
  23. std::cout << "closing tooltip\n";
  24. this->setToolTipZ(0);
  25. currentTooltipSlot = -1;
  26. }
  27. if (slot != -1)
  28. {
  29. std::cout << "requesting tooltip for slot " << slot << "\n";
  30. UIMLToolTip* tip = new UIMLToolTip();
  31. tip->setUIML(itemTypes[slot]->getTooltipUIML());
  32. tip->setWarten(0);
  33. tip->setPosition(mausPos.x, mausPos.y + 15);
  34. setToolTipZ(tip);
  35. currentTooltipSlot = slot;
  36. return 1;
  37. }
  38. return 0;
  39. });
  40. }
  41. ItemList::~ItemList()
  42. {
  43. delete[] slotList;
  44. }
  45. int ItemList::getSlotByLocalPos(Framework::Point pos)
  46. {
  47. int x = 0;
  48. int y = 0;
  49. for (int i = 0; i < slotCount; i++)
  50. {
  51. if (pos.x >= x && pos.x < x + 50 && pos.y >= y && pos.y < y + 50)
  52. return slotList[i];
  53. x += 60;
  54. if (x >= getWidth())
  55. {
  56. y += 60;
  57. x = 0;
  58. }
  59. }
  60. return -1;
  61. }
  62. void ItemList::doMouseEvent(Framework::MouseEvent& me, bool userRet)
  63. {
  64. mausPos.x = me.originalX;
  65. mausPos.y = me.originalY;
  66. if (me.id == ME_Move)
  67. {
  68. if (getSlotByLocalPos(Point(me.mx, me.my)) != currentTooltipSlot)
  69. {
  70. if (currentTooltipSlot != -1)
  71. {
  72. std::cout << "closing tooltip\n";
  73. setToolTipZ(0);
  74. }
  75. else
  76. toolTipRequested = 0;
  77. currentTooltipSlot = -1;
  78. }
  79. }
  80. else if (me.id == ME_RLeft)
  81. {
  82. int pos = getSlotByLocalPos(Point(me.mx, me.my));
  83. if (pos >= 0)
  84. {
  85. World::INSTANCE->zClient()->craftingUIMLRequest(
  86. itemTypes[pos]->getId());
  87. }
  88. }
  89. else if (me.id == ME_RMiddle)
  90. {
  91. int pos = getSlotByLocalPos(Point(me.mx, me.my));
  92. if (pos >= 0)
  93. {
  94. if (Framework::getKeyState(T_Shift))
  95. {
  96. World::INSTANCE->zClient()->giveItemRequest(
  97. itemTypes[pos]->getId(), 1);
  98. }
  99. else
  100. {
  101. World::INSTANCE->zClient()->giveItemRequest(
  102. itemTypes[pos]->getId(), 0);
  103. }
  104. }
  105. }
  106. DrawableBackground::doMouseEvent(me, userRet);
  107. }
  108. bool ItemList::tick(double time)
  109. {
  110. return DrawableBackground::tick(time);
  111. }
  112. void ItemList::adjustSize(int parentWidth, int parentHeight)
  113. {
  114. int ipr = (parentWidth + 10) / 60;
  115. if (ipr == 0)
  116. {
  117. ipr = 1;
  118. }
  119. setSize(ipr * 60 - 10,
  120. (itemTypeCount / ipr + (itemTypeCount % ipr == 0 ? 0 : 1)) * 60 - 10);
  121. }
  122. void ItemList::render(Framework::Image& rObj)
  123. {
  124. const Text* filter
  125. = dynamic_cast<Game*>((Menu*)menuRegister->get("game"))->zFilterText();
  126. DrawableBackground::render(rObj);
  127. if (!rObj.setDrawOptions(pos.x, pos.y, gr.x, gr.y)) return;
  128. int x = 0;
  129. int y = 0;
  130. int index = 0;
  131. for (int i = 0; i < itemTypeCount; i++)
  132. {
  133. if (filter->getLength() > 0
  134. && !itemTypes[i]->getName().has(filter->getText()))
  135. {
  136. continue;
  137. }
  138. slotList[index] = i;
  139. rObj.fillRegion(x, y, 50, 50, 0xFF222222);
  140. rObj.alphaImage(x, y, 50, 50, *itemTypes[i]->zIcon());
  141. x += 60;
  142. if (x >= getWidth())
  143. {
  144. x = 0;
  145. y += 60;
  146. }
  147. index++;
  148. }
  149. slotCount = index;
  150. rObj.releaseDrawOptions();
  151. }
  152. ItemListContainer::ItemListContainer()
  153. : Window()
  154. {
  155. LTDBFile iconsDat;
  156. iconsDat.setFile(new Text("data/images/gui_icons.ltdb"));
  157. iconsDat.readData(0);
  158. setStyle(Window::Style::Allowed | Window::Style::Border
  159. | Window::Style::BodyBAlpha | Window::Style::Movable
  160. | Window::Style::Title | Window::Style::TitleBAlpha
  161. | Window::Style::TitleBackground | Window::Style::BodyBackground
  162. | Window::Style::MEIgnoreInside | Window::Style::VScroll
  163. | Window::Style::WidthChangeable | Window::Style::BodyMinWidth
  164. | Window::Style::BodyMinHeight | Window::Style::HeightChangeable
  165. | Window::Style::Closable | Window::Style::ClosingBAlpha
  166. | Window::Style::ClosingBuffer | Window::Style::ClosingBAlpha
  167. | Window::Style::ClosingBackground | Style::ClosingBImage
  168. | Style::ClosingBuffer);
  169. setBodyBgColor(0xA0000000);
  170. setTBgColor(0xA0000000);
  171. setCloseBgColor(0xA0000000);
  172. setSize(329, window->zScreen()->getBackBufferSize().y - 20);
  173. setPosition(window->zScreen()->getBackBufferSize().x - getWidth() - 10, 10);
  174. setBorderWidth(2);
  175. setBorderColor(0xFF52525E);
  176. setTitel("Recipies");
  177. setTFontZ(dynamic_cast<Font*>(uiFactory.initParam.font->getThis()));
  178. zTTextField()->setSize(0, 20);
  179. zTTextField()->addStyle(TextField::Style::Center);
  180. setKeyboardEvent(_ret1TE);
  181. setBodyMin(70, 70);
  182. setVSBScroll(0);
  183. zVScrollBar()->setBgColor(0xA0000000, 1);
  184. setClosingMe([this](void* p, void* o, MouseEvent me) {
  185. if (me.id == ME_RLeft) removeStyle(Style::Visible);
  186. return 1;
  187. });
  188. setCloseAfStrength(10);
  189. setCloseAfColor(0x5F9C0A0A);
  190. setCloseBgImageZ(iconsDat.load(0, new Text("close.png")));
  191. setCloseClickAfColor(0xFF9C0A0A);
  192. setCloseClickAfStrength(10);
  193. list = new ItemList();
  194. list->setPosition(10, 10);
  195. addMember(list);
  196. }
  197. bool ItemListContainer::tick(double time)
  198. {
  199. list->adjustSize(getInnerWidth() - 35, getInnerHeight());
  200. if (zVScrollBar()->getScrollData()->max != list->getHeight() + 20)
  201. setVSBMax(list->getHeight() + 20);
  202. return Window::tick(time);
  203. }