Dialog.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. #include "Dialog.h"
  2. #include <FileSystem.h>
  3. #include <InMemoryBuffer.h>
  4. #include <KeyboardEvent.h>
  5. #include <TextField.h>
  6. #include <XML.h>
  7. #include "CraftingGrid.h"
  8. #include "CraftingRecipies.h"
  9. #include "Equipment.h"
  10. #include "Globals.h"
  11. #include "InventoryView.h"
  12. #include "ItemStack.h"
  13. #include "ListView.h"
  14. #include "QuestGraph.h"
  15. #include "RecipieIngredient.h"
  16. #include "RecipieOutput.h"
  17. #include "ShapedRecipie.h"
  18. #include "UIMLCraftingProgress.h"
  19. #include "UIMLFuelState.h"
  20. #include "UnshapedRecipie.h"
  21. using namespace Framework;
  22. UIMLDialog::UIMLDialog(
  23. Framework::Text uiml, std::function<void(UIMLDialog* self)> onClose)
  24. : Window()
  25. {
  26. XML::Element* xml = new XML::Element(uiml);
  27. view = new UIMLView("<v/>", uiFactory);
  28. view->setStyle(UIMLView::Style::Allowed | UIMLView::Style::Visible
  29. | UIMLView::Style::GlobalMouseEvent);
  30. view->setMouseEvent(_ret1ME);
  31. view->addKnownElement(new InventoryElement());
  32. view->addKnownElement(new EquipmentElement());
  33. view->addKnownElement(new CraftingGridElement());
  34. view->addKnownElement(new CraftingRecipiesElement());
  35. view->addKnownElement(new RecipieIngredientElement());
  36. view->addKnownElement(new RecipieOutputElement());
  37. view->addKnownElement(new ShapedRecipieElement());
  38. view->addKnownElement(new UnshapedRecipieElement());
  39. view->addKnownElement(new ListViewElement());
  40. view->addKnownElement(new QuestGraphElement());
  41. view->addKnownElement(new QuestGraphItemElement());
  42. view->addKnownElement(new ItemStackElement());
  43. view->addKnownElement(new UIMLCraftingProgressElement());
  44. view->addKnownElement(new UIMLFuelStateElement());
  45. view->setOnMemberMouseEvent([](Framework::XML::Element& element,
  46. Framework::Drawable& z,
  47. MouseEvent me) {
  48. if (me.id == ME_RLeft)
  49. {
  50. Framework::Text onClick = element.getAttributeValue("onClick");
  51. if (onClick.getLength() == 0)
  52. {
  53. return !element.hasAttribute("onClick");
  54. }
  55. Framework::Text* dialogName
  56. = onClick.getTeilText(0, onClick.positionOf(";"));
  57. Framework::Text* tail
  58. = onClick.getTeilText(onClick.positionOf(";") + 1);
  59. Framework::InMemoryBuffer buffer;
  60. buffer.write("\0", 1); // element message
  61. while (tail->getLength() > 0)
  62. {
  63. Framework::Text* current = 0;
  64. if (tail->positionOf(";") >= 0)
  65. {
  66. current = tail->getTeilText(0, tail->positionOf(";"));
  67. Framework::Text* tmp
  68. = tail->getTeilText(tail->positionOf(";") + 1);
  69. tail->release();
  70. tail = tmp;
  71. }
  72. else
  73. {
  74. current = new Text(*tail);
  75. tail->setText("");
  76. }
  77. if (current->positionOf("(char)") == 0)
  78. {
  79. current->replace("(char)", "");
  80. char d = (char)(int)*current;
  81. buffer.write(&d, 1);
  82. }
  83. else if (current->positionOf("(short)") == 0)
  84. {
  85. current->replace("(short)", "");
  86. short d = (short)(int)*current;
  87. buffer.write((char*)&d, 2);
  88. }
  89. else if (current->positionOf("(int)") == 0)
  90. {
  91. current->replace("(int)", "");
  92. int d = (int)*current;
  93. buffer.write((char*)&d, 4);
  94. }
  95. else if (current->positionOf("(__int64)") == 0)
  96. {
  97. current->replace("(__int64)", "");
  98. __int64 d = (__int64)*current;
  99. buffer.write((char*)&d, 8);
  100. }
  101. else if (current->positionOf("(float)") == 0)
  102. {
  103. current->replace("(float)", "");
  104. float d = (float)*current;
  105. buffer.write((char*)&d, 4);
  106. }
  107. else if (current->positionOf("(double)") == 0)
  108. {
  109. current->replace("(double)", "");
  110. double d = (double)*current;
  111. buffer.write((char*)&d, 8);
  112. }
  113. else
  114. {
  115. unsigned char len = (unsigned char)current->getLength();
  116. buffer.write((char*)&len, 1);
  117. buffer.write(*current, len);
  118. }
  119. current->release();
  120. }
  121. char* message = new char[buffer.getSize()];
  122. buffer.read(message, (int)buffer.getSize());
  123. World::INSTANCE->zClient()->uiRequest(
  124. *dialogName, message, (int)buffer.getSize());
  125. delete[] message;
  126. dialogName->release();
  127. tail->release();
  128. }
  129. return (bool)1;
  130. });
  131. view->setUIML(xml);
  132. view->setSize((int)xml->getAttributeValue("width"),
  133. (int)xml->getAttributeValue("height"));
  134. name = xml->getAttributeValue("id");
  135. view->layout();
  136. if (!xml->hasAttribute("width"))
  137. {
  138. view->setSize(view->calculateContentSize().x, view->getHeight());
  139. }
  140. if (!xml->hasAttribute("height"))
  141. {
  142. view->setSize(view->getWidth(), view->calculateContentSize().y);
  143. }
  144. bool notifyOnClose = 0;
  145. int notifyOnCloseDimension = -1;
  146. Vec3<int> notifyOnCloseBlock;
  147. if (xml->hasAttribute("notifyOnClose"))
  148. {
  149. notifyOnClose = 1;
  150. Text value = xml->getAttributeValue("notifyOnClose");
  151. if (value.has(","))
  152. {
  153. Text* first = value.getTeilText(0, value.positionOf(",", 0) + 1);
  154. Text* second = value.getTeilText(
  155. value.positionOf(",", 0) + 1, value.positionOf(",", 1));
  156. Text* third = value.getTeilText(
  157. value.positionOf(",", 1) + 1, value.positionOf(",", 2));
  158. Text* forth = value.getTeilText(value.positionOf(",", 2) + 1);
  159. notifyOnCloseDimension = (int)*first;
  160. notifyOnCloseBlock.x = (int)*second;
  161. notifyOnCloseBlock.y = (int)*third;
  162. notifyOnCloseBlock.z = (int)*forth;
  163. first->release();
  164. second->release();
  165. third->release();
  166. forth->release();
  167. } // TODO: else entity id
  168. }
  169. addMember(view);
  170. LTDBFile iconsDat;
  171. iconsDat.setFile(new Text("data/images/gui_icons.ltdb"));
  172. iconsDat.readData(0);
  173. setStyle(Window::Style::Visible | Window::Style::Allowed
  174. | Window::Style::Border | Window::Style::BodyBAlpha
  175. | Window::Style::Movable | Window::Style::Title
  176. | Window::Style::TitleBAlpha | Window::Style::Closable
  177. | Window::Style::ClosingBAlpha | Window::Style::ClosingBuffer
  178. | Window::Style::TitleBackground | Window::Style::BodyBackground
  179. | Window::Style::ClosingBackground | Window::Style::MEIgnoreInside
  180. | Style::ClosingBuffer | Style::ClosingBImage);
  181. setBodyBgColor(0xA0000000);
  182. setTBgColor(0xA0000000);
  183. setCloseBgColor(0xA0000000);
  184. setSize(view->getWidth() + 4, view->getHeight() + 24);
  185. setPosition(window->zScreen()->getBackBufferSize() / 2 - getSize() / 2);
  186. setBorderWidth(2);
  187. this->onClose = [onClose,
  188. notifyOnClose,
  189. notifyOnCloseDimension,
  190. notifyOnCloseBlock,
  191. this](UIMLDialog* self) {
  192. if (notifyOnClose)
  193. {
  194. if (notifyOnCloseDimension >= 0)
  195. {
  196. char* msg = new char[name.getLength() + 3];
  197. msg[0] = 1; // dialog closed
  198. *(short*)(msg + 1) = (short)name.getLength();
  199. memcpy(msg + 3, name.getText(), name.getLength());
  200. World::INSTANCE->zClient()->blockAPIRequest(
  201. notifyOnCloseDimension,
  202. notifyOnCloseBlock,
  203. msg,
  204. name.getLength() + 3);
  205. delete[] msg;
  206. } // TODO: else entity notification
  207. }
  208. onClose(this);
  209. };
  210. setClosingMe(
  211. [notifyOnClose, notifyOnCloseDimension, notifyOnCloseBlock, this](
  212. void* p, void* o, MouseEvent me) {
  213. if (me.id == ME_RLeft)
  214. {
  215. this->close();
  216. }
  217. return 1;
  218. });
  219. setBorderColor(0xFF52525E);
  220. setTitel(xml->getAttributeValue("title"));
  221. setTFontZ(dynamic_cast<Font*>(uiFactory.initParam.font->getThis()));
  222. zTTextField()->setSize(0, 20);
  223. zTTextField()->addStyle(TextField::Style::Center);
  224. setKeyboardEvent(_ret1TE);
  225. setCloseAfStrength(10);
  226. setCloseAfColor(0x5F9C0A0A);
  227. setCloseBgImageZ(iconsDat.load(0, new Text("close.png")));
  228. setCloseClickAfColor(0xFF9C0A0A);
  229. setCloseClickAfStrength(10);
  230. }
  231. UIMLDialog::~UIMLDialog() {}
  232. void UIMLDialog::api(char* message)
  233. {
  234. short idLen = *(short*)message;
  235. char* id = new char[idLen + 1];
  236. memcpy(id, message + 2, idLen);
  237. id[idLen] = 0;
  238. NetworkAPIProcessor* processor
  239. = dynamic_cast<NetworkAPIProcessor*>(view->zDrawableById(id));
  240. if (processor
  241. && (*(int*)(message + 2 + idLen) == -1 // global message
  242. || processor->getId() == *(int*)(message + 2 + idLen)))
  243. {
  244. processor->api(message + 6 + idLen);
  245. }
  246. delete[] id;
  247. }
  248. const Framework::Text& UIMLDialog::getName() const
  249. {
  250. return name;
  251. }
  252. void UIMLDialog::updateUIML(const char* uiml)
  253. {
  254. XML::Element* xml = new XML::Element(uiml);
  255. uiFactory.initParam.bildschirm->postAction([this, xml]() {
  256. view->setSize((int)xml->getAttributeValue("width"),
  257. (int)xml->getAttributeValue("height"));
  258. view->setUIML(xml);
  259. view->layout();
  260. if (!xml->hasAttribute("width"))
  261. {
  262. view->setSize(view->calculateContentSize().x, view->getHeight());
  263. }
  264. if (!xml->hasAttribute("height"))
  265. {
  266. view->setSize(view->getWidth(), view->calculateContentSize().y);
  267. }
  268. Point oldSize = getSize();
  269. setSize(view->getWidth() + 4, view->getHeight() + 24);
  270. setPosition(getPosition() + (oldSize - getSize()) / 2);
  271. });
  272. }
  273. void UIMLDialog::close()
  274. {
  275. World::INSTANCE->zClient()->uiRequest(name, "\1", 1);
  276. onClose(this);
  277. }