ItemList.cpp 5.7 KB

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