Dialog.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. #include "Dialog.h"
  2. #include <DateiSystem.h>
  3. #include <InMemoryBuffer.h>
  4. #include <TastaturEreignis.h>
  5. #include <TextFeld.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. : Fenster()
  25. {
  26. XML::Element* xml = new XML::Element(uiml);
  27. view = new UIMLView("<v/>", uiFactory);
  28. view->setStyle(UIMLView::Style::Erlaubt | UIMLView::Style::Sichtbar
  29. | UIMLView::Style::GlobalMouseEvent);
  30. view->setMausEreignis(_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::Zeichnung& z,
  47. MausEreignis me) {
  48. if (me.id == ME_RLinks)
  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.positionVon(";"));
  57. Framework::Text* tail
  58. = onClick.getTeilText(onClick.positionVon(";") + 1);
  59. Framework::InMemoryBuffer buffer;
  60. buffer.schreibe("\0", 1); // element message
  61. while (tail->getLength() > 0)
  62. {
  63. Framework::Text* current = 0;
  64. if (tail->positionVon(";") >= 0)
  65. {
  66. current = tail->getTeilText(0, tail->positionVon(";"));
  67. Framework::Text* tmp
  68. = tail->getTeilText(tail->positionVon(";") + 1);
  69. tail->release();
  70. tail = tmp;
  71. }
  72. else
  73. {
  74. current = new Text(*tail);
  75. tail->setText("");
  76. }
  77. if (current->positionVon("(char)") == 0)
  78. {
  79. current->ersetzen("(char)", "");
  80. char d = (char)(int)*current;
  81. buffer.schreibe(&d, 1);
  82. }
  83. else if (current->positionVon("(short)") == 0)
  84. {
  85. current->ersetzen("(short)", "");
  86. short d = (short)(int)*current;
  87. buffer.schreibe((char*)&d, 2);
  88. }
  89. else if (current->positionVon("(int)") == 0)
  90. {
  91. current->ersetzen("(int)", "");
  92. int d = (int)*current;
  93. buffer.schreibe((char*)&d, 4);
  94. }
  95. else if (current->positionVon("(__int64)") == 0)
  96. {
  97. current->ersetzen("(__int64)", "");
  98. __int64 d = (__int64)*current;
  99. buffer.schreibe((char*)&d, 8);
  100. }
  101. else if (current->positionVon("(float)") == 0)
  102. {
  103. current->ersetzen("(float)", "");
  104. float d = (float)*current;
  105. buffer.schreibe((char*)&d, 4);
  106. }
  107. else if (current->positionVon("(double)") == 0)
  108. {
  109. current->ersetzen("(double)", "");
  110. double d = (double)*current;
  111. buffer.schreibe((char*)&d, 8);
  112. }
  113. else
  114. {
  115. unsigned char len = (unsigned char)current->getLength();
  116. buffer.schreibe((char*)&len, 1);
  117. buffer.schreibe(*current, len);
  118. }
  119. current->release();
  120. }
  121. char* message = new char[buffer.getSize()];
  122. buffer.lese(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->getBreite(), 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.hat(","))
  152. {
  153. Text* first = value.getTeilText(0, value.positionVon(",", 0) + 1);
  154. Text* second = value.getTeilText(
  155. value.positionVon(",", 0) + 1, value.positionVon(",", 1));
  156. Text* third = value.getTeilText(
  157. value.positionVon(",", 1) + 1, value.positionVon(",", 2));
  158. Text* forth = value.getTeilText(value.positionVon(",", 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. LTDBDatei iconsDat;
  171. iconsDat.setDatei(new Text("data/images/gui_icons.ltdb"));
  172. iconsDat.leseDaten(0);
  173. setStyle(
  174. Fenster::Style::Sichtbar | Fenster::Style::Erlaubt
  175. | Fenster::Style::Rahmen | Fenster::Style::BodyHAlpha
  176. | Fenster::Style::Beweglich | Fenster::Style::Titel
  177. | Fenster::Style::TitelHAlpha | Fenster::Style::Closable
  178. | Fenster::Style::ClosingHAlpha | Fenster::Style::ClosingKlickBuffer
  179. | Fenster::Style::TitelHintergrund | Fenster::Style::BodyHintergrund
  180. | Fenster::Style::ClosingHintergrund | Fenster::Style::MEIgnoreInside
  181. | Style::ClosingBuffer | Style::ClosingHBild);
  182. setKBgFarbe(0xA0000000);
  183. setTBgFarbe(0xA0000000);
  184. setSBgFarbe(0xA0000000);
  185. setSize(view->getBreite() + 4, view->getHeight() + 24);
  186. setPosition(window->zBildschirm()->getBackBufferSize() / 2 - getSize() / 2);
  187. setRBreite(2);
  188. this->onClose = [onClose,
  189. notifyOnClose,
  190. notifyOnCloseDimension,
  191. notifyOnCloseBlock,
  192. this](UIMLDialog* self) {
  193. if (notifyOnClose)
  194. {
  195. if (notifyOnCloseDimension >= 0)
  196. {
  197. char* msg = new char[name.getLength() + 3];
  198. msg[0] = 1; // dialog closed
  199. *(short*)(msg + 1) = (short)name.getLength();
  200. memcpy(msg + 3, name.getText(), name.getLength());
  201. World::INSTANCE->zClient()->blockAPIRequest(
  202. notifyOnCloseDimension,
  203. notifyOnCloseBlock,
  204. msg,
  205. name.getLength() + 3);
  206. delete[] msg;
  207. } // TODO: else entity notification
  208. }
  209. onClose(this);
  210. };
  211. setClosingMe(
  212. [notifyOnClose, notifyOnCloseDimension, notifyOnCloseBlock, this](
  213. void* p, void* o, MausEreignis me) {
  214. if (me.id == ME_RLinks)
  215. {
  216. this->close();
  217. }
  218. return 1;
  219. });
  220. setRFarbe(0xFF52525E);
  221. setTitel(xml->getAttributeValue("title"));
  222. setTSchriftZ(
  223. dynamic_cast<Schrift*>(uiFactory.initParam.schrift->getThis()));
  224. zTTextFeld()->setSize(0, 20);
  225. zTTextFeld()->addStyle(TextFeld::Style::Center);
  226. setTastaturEreignis(_ret1TE);
  227. setSAfStrength(10);
  228. setSAfFarbe(0x5F9C0A0A);
  229. setSBgBildZ(iconsDat.laden(0, new Text("close.png")));
  230. setSKAfFarbe(0xFF9C0A0A);
  231. setSKAfStrength(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->zZeichnungById(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->getBreite(), view->calculateContentSize().y);
  269. }
  270. Punkt oldSize = getSize();
  271. setSize(view->getBreite() + 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. }