Dialog.cpp 11 KB

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