Dialog.cpp 10 KB

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