Dialog.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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 "UnshapedRecipie.h"
  19. using namespace Framework;
  20. UIMLDialog::UIMLDialog(
  21. Framework::Text uiml, std::function<void(UIMLDialog* self)> onClose)
  22. : Fenster()
  23. {
  24. XML::Element* xml = new XML::Element(uiml);
  25. view = new UIMLView("<v/>", uiFactory);
  26. view->setStyle(UIMLView::Style::Erlaubt | UIMLView::Style::Sichtbar
  27. | UIMLView::Style::GlobalMouseEvent);
  28. view->setMausEreignis(_ret1ME);
  29. view->addKnownElement(new InventoryElement());
  30. view->addKnownElement(new EquipmentElement());
  31. view->addKnownElement(new CraftingGridElement());
  32. view->addKnownElement(new CraftingRecipiesElement());
  33. view->addKnownElement(new RecipieIngredientElement());
  34. view->addKnownElement(new RecipieOutputElement());
  35. view->addKnownElement(new ShapedRecipieElement());
  36. view->addKnownElement(new UnshapedRecipieElement());
  37. view->addKnownElement(new ListViewElement());
  38. view->addKnownElement(new QuestGraphElement());
  39. view->addKnownElement(new QuestGraphItemElement());
  40. view->addKnownElement(new ItemStackElement());
  41. view->setOnMemberMouseEvent([](Framework::XML::Element& element,
  42. Framework::Zeichnung& z,
  43. MausEreignis me) {
  44. if (me.id == ME_RLinks)
  45. {
  46. Framework::Text onClick = element.getAttributeValue("onClick");
  47. if (onClick.getLength() == 0)
  48. {
  49. return !element.hasAttribute("onClick");
  50. }
  51. Framework::Text* dialogName
  52. = onClick.getTeilText(0, onClick.positionVon(";"));
  53. Framework::Text* tail
  54. = onClick.getTeilText(onClick.positionVon(";") + 1);
  55. Framework::InMemoryBuffer buffer;
  56. buffer.schreibe("\0", 1); // element message
  57. while (tail->getLength() > 0)
  58. {
  59. Framework::Text* current = 0;
  60. if (tail->positionVon(";") >= 0)
  61. {
  62. current = tail->getTeilText(0, tail->positionVon(";"));
  63. Framework::Text* tmp
  64. = tail->getTeilText(tail->positionVon(";") + 1);
  65. tail->release();
  66. tail = tmp;
  67. }
  68. else
  69. {
  70. current = new Text(*tail);
  71. tail->setText("");
  72. }
  73. if (current->positionVon("(char)") == 0)
  74. {
  75. current->ersetzen("(char)", "");
  76. char d = (char)(int)*current;
  77. buffer.schreibe(&d, 1);
  78. }
  79. else if (current->positionVon("(short)") == 0)
  80. {
  81. current->ersetzen("(short)", "");
  82. short d = (short)(int)*current;
  83. buffer.schreibe((char*)&d, 2);
  84. }
  85. else if (current->positionVon("(int)") == 0)
  86. {
  87. current->ersetzen("(int)", "");
  88. int d = (int)*current;
  89. buffer.schreibe((char*)&d, 4);
  90. }
  91. else if (current->positionVon("(__int64)") == 0)
  92. {
  93. current->ersetzen("(__int64)", "");
  94. __int64 d = (__int64)*current;
  95. buffer.schreibe((char*)&d, 8);
  96. }
  97. else if (current->positionVon("(float)") == 0)
  98. {
  99. current->ersetzen("(float)", "");
  100. float d = (float)*current;
  101. buffer.schreibe((char*)&d, 4);
  102. }
  103. else if (current->positionVon("(double)") == 0)
  104. {
  105. current->ersetzen("(double)", "");
  106. double d = (double)*current;
  107. buffer.schreibe((char*)&d, 8);
  108. }
  109. else
  110. {
  111. unsigned char len = (unsigned char)current->getLength();
  112. buffer.schreibe((char*)&len, 1);
  113. buffer.schreibe(*current, len);
  114. }
  115. current->release();
  116. }
  117. char* message = new char[buffer.getSize()];
  118. buffer.lese(message, (int)buffer.getSize());
  119. World::INSTANCE->zClient()->uiRequest(
  120. *dialogName, message, (int)buffer.getSize());
  121. delete[] message;
  122. dialogName->release();
  123. tail->release();
  124. }
  125. return (bool)1;
  126. });
  127. view->setUIML(xml);
  128. view->setSize((int)xml->getAttributeValue("width"),
  129. (int)xml->getAttributeValue("height"));
  130. name = xml->getAttributeValue("id");
  131. view->layout();
  132. if (!xml->hasAttribute("width"))
  133. {
  134. view->setSize(view->calculateContentSize().x, view->getHeight());
  135. }
  136. if (!xml->hasAttribute("height"))
  137. {
  138. view->setSize(view->getBreite(), view->calculateContentSize().y);
  139. }
  140. bool notifyOnClose = 0;
  141. int notifyOnCloseDimension = -1;
  142. Vec3<int> notifyOnCloseBlock;
  143. if (xml->hasAttribute("notifyOnClose"))
  144. {
  145. notifyOnClose = 1;
  146. Text value = xml->getAttributeValue("notifyOnClose");
  147. if (value.hat(","))
  148. {
  149. Text* first = value.getTeilText(0, value.positionVon(",", 0) + 1);
  150. Text* second = value.getTeilText(
  151. value.positionVon(",", 0) + 1, value.positionVon(",", 1));
  152. Text* third = value.getTeilText(
  153. value.positionVon(",", 1) + 1, value.positionVon(",", 2));
  154. Text* forth = value.getTeilText(value.positionVon(",", 2) + 1);
  155. notifyOnCloseDimension = (int)*first;
  156. notifyOnCloseBlock.x = (int)*second;
  157. notifyOnCloseBlock.y = (int)*third;
  158. notifyOnCloseBlock.z = (int)*forth;
  159. first->release();
  160. second->release();
  161. third->release();
  162. forth->release();
  163. } // TODO: else entity id
  164. }
  165. addMember(view);
  166. LTDBDatei iconsDat;
  167. iconsDat.setDatei(new Text("data/images/gui_icons.ltdb"));
  168. iconsDat.leseDaten(0);
  169. setStyle(
  170. Fenster::Style::Sichtbar | Fenster::Style::Erlaubt
  171. | Fenster::Style::Rahmen | Fenster::Style::BodyHAlpha
  172. | Fenster::Style::Beweglich | Fenster::Style::Titel
  173. | Fenster::Style::TitelHAlpha | Fenster::Style::Closable
  174. | Fenster::Style::ClosingHAlpha | Fenster::Style::ClosingKlickBuffer
  175. | Fenster::Style::TitelHintergrund | Fenster::Style::BodyHintergrund
  176. | Fenster::Style::ClosingHintergrund | Fenster::Style::MEIgnoreInside
  177. | Style::ClosingBuffer | Style::ClosingHBild);
  178. setKBgFarbe(0xA0000000);
  179. setTBgFarbe(0xA0000000);
  180. setSBgFarbe(0xA0000000);
  181. setSize(view->getBreite() + 4, view->getHeight() + 24);
  182. setPosition(window->zBildschirm()->getBackBufferSize() / 2 - getSize() / 2);
  183. setRBreite(2);
  184. this->onClose = [onClose,
  185. notifyOnClose,
  186. notifyOnCloseDimension,
  187. notifyOnCloseBlock,
  188. this](UIMLDialog* self) {
  189. if (notifyOnClose)
  190. {
  191. if (notifyOnCloseDimension >= 0)
  192. {
  193. char* msg = new char[name.getLength() + 3];
  194. msg[0] = 1; // dialog closed
  195. *(short*)(msg + 1) = (short)name.getLength();
  196. memcpy(msg + 3, name.getText(), name.getLength());
  197. World::INSTANCE->zClient()->blockAPIRequest(
  198. notifyOnCloseDimension,
  199. notifyOnCloseBlock,
  200. msg,
  201. name.getLength() + 3);
  202. delete[] msg;
  203. } // TODO: else entity notification
  204. }
  205. onClose(this);
  206. };
  207. setClosingMe(
  208. [notifyOnClose, notifyOnCloseDimension, notifyOnCloseBlock, this](
  209. void* p, void* o, MausEreignis me) {
  210. if (me.id == ME_RLinks)
  211. {
  212. this->close();
  213. }
  214. return 1;
  215. });
  216. setRFarbe(0xFF52525E);
  217. setTitel(xml->getAttributeValue("title"));
  218. setTSchriftZ(
  219. dynamic_cast<Schrift*>(uiFactory.initParam.schrift->getThis()));
  220. zTTextFeld()->setSize(0, 20);
  221. zTTextFeld()->addStyle(TextFeld::Style::Center);
  222. setTastaturEreignis(_ret1TE);
  223. setSAfStrength(10);
  224. setSAfFarbe(0x5F9C0A0A);
  225. setSBgBildZ(iconsDat.laden(0, new Text("close.png")));
  226. setSKAfFarbe(0xFF9C0A0A);
  227. setSKAfStrength(10);
  228. }
  229. UIMLDialog::~UIMLDialog() {}
  230. void UIMLDialog::api(char* message)
  231. {
  232. short idLen = *(short*)message;
  233. char* id = new char[idLen + 1];
  234. memcpy(id, message + 2, idLen);
  235. id[idLen] = 0;
  236. NetworkAPIProcessor* processor
  237. = dynamic_cast<NetworkAPIProcessor*>(view->zZeichnungById(id));
  238. if (processor) processor->api(message + 2 + idLen);
  239. delete[] id;
  240. }
  241. const Framework::Text& UIMLDialog::getName() const
  242. {
  243. return name;
  244. }
  245. void UIMLDialog::updateUIML(const char* uiml)
  246. {
  247. XML::Element* xml = new XML::Element(uiml);
  248. uiFactory.initParam.bildschirm->postAction([this, xml]() {
  249. view->setSize((int)xml->getAttributeValue("width"),
  250. (int)xml->getAttributeValue("height"));
  251. view->setUIML(xml);
  252. view->layout();
  253. if (!xml->hasAttribute("width"))
  254. {
  255. view->setSize(view->calculateContentSize().x, view->getHeight());
  256. }
  257. if (!xml->hasAttribute("height"))
  258. {
  259. view->setSize(view->getBreite(), view->calculateContentSize().y);
  260. }
  261. Punkt oldSize = getSize();
  262. setSize(view->getBreite() + 4, view->getHeight() + 24);
  263. setPosition(getPosition() + (oldSize - getSize()) / 2);
  264. });
  265. }
  266. void UIMLDialog::close()
  267. {
  268. World::INSTANCE->zClient()->uiRequest(name, "\1", 1);
  269. onClose(this);
  270. }