StatusBars.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. #include "StatusBars.h"
  2. #include <Bild.h>
  3. #include <DateiSystem.h>
  4. #include <XML.h>
  5. #include "Globals.h"
  6. StatusBarsElement::StatusBarsElement()
  7. : UIMLElement()
  8. {}
  9. //! prüft, ob dieses UIML Element für ein bestimmtes xml Element zuständig
  10. //! ist
  11. bool StatusBarsElement::isApplicableFor(Framework::XML::Element& element)
  12. {
  13. return element.getName().istGleich("statusBars");
  14. }
  15. //! erstellt eine neue Zeichnung zu einem gegebenen xml Element
  16. Framework::Zeichnung* StatusBarsElement::parseElement(
  17. Framework::XML::Element& element, Framework::UIMLContainer& generalFactory)
  18. {
  19. return new StatusBarsView(element.getAttributeValue("id"),
  20. (int)element.getAttributeValue("target"));
  21. }
  22. bool StatusBarsElement::updateElement(Framework::XML::Element& element,
  23. Framework::Zeichnung& z,
  24. Framework::UIMLContainer& generalFactory)
  25. {
  26. return false;
  27. }
  28. //! wendet die layout parameter zu einer Zeichnung an
  29. void StatusBarsElement::layout(Framework::XML::Element& element,
  30. Framework::Zeichnung& z,
  31. int pWidth,
  32. int pHeight,
  33. Framework::UIMLContainer& generalLayouter)
  34. {
  35. z.setHeight(54);
  36. z.setWidth(592);
  37. UIMLElement::layout(element, z, pWidth, pHeight, generalLayouter);
  38. }
  39. StatusBarsView::StatusBarsView(Framework::Text id, int targetEntity)
  40. : ZeichnungHintergrund(),
  41. id(id),
  42. targetEntity(targetEntity)
  43. {
  44. setStyle(ZeichnungHintergrund::Style::Sichtbar
  45. | ZeichnungHintergrund::Style::Rahmen
  46. | ZeichnungHintergrund::Style::Hintergrund
  47. | ZeichnungHintergrund::Style::HAlpha);
  48. setRahmenFarbe(0xA052525E);
  49. setHintergrundFarbe(0xA0222222);
  50. setRahmenBreite(2);
  51. Framework::LTDBDatei file;
  52. file.setDatei(new Framework::Text("data/images/gui_icons.ltdb"));
  53. file.leseDaten(0);
  54. healthIcon = file.laden(0, new Framework::Text("health.png"));
  55. energyIcon = file.laden(0, new Framework::Text("energy.png"));
  56. hungerIcon = file.laden(0, new Framework::Text("food.png"));
  57. thirstIcon = file.laden(0, new Framework::Text("water.png"));
  58. health = 0.f;
  59. hunger = 0.f;
  60. thirst = 0.f;
  61. energy = 0.f;
  62. tr.setSchriftZ(
  63. dynamic_cast<Schrift*>(uiFactory.initParam.schrift->getThis()));
  64. tr.setSchriftSize(12);
  65. char* msg = new char[id.getLength() + 2];
  66. msg[0] = 0; // request status bar state
  67. msg[1] = (char)id.getLength();
  68. memcpy(msg + 2, id.getText(), id.getLength());
  69. World::INSTANCE->zClient()->entityAPIRequest(
  70. targetEntity, msg, id.getLength() + 2);
  71. delete[] msg;
  72. }
  73. StatusBarsView::~StatusBarsView()
  74. {
  75. char* msg = new char[id.getLength() + 2];
  76. msg[0] = 1; // remove status bar observer
  77. msg[1] = (char)id.getLength();
  78. memcpy(msg + 2, id.getText(), id.getLength());
  79. World::INSTANCE->zClient()->entityAPIRequest(
  80. targetEntity, msg, id.getLength() + 2);
  81. delete[] msg;
  82. healthIcon->release();
  83. energyIcon->release();
  84. hungerIcon->release();
  85. thirstIcon->release();
  86. }
  87. void StatusBarsView::api(char* message)
  88. {
  89. switch (message[0])
  90. {
  91. case 0: // initialisation
  92. {
  93. float maxHealth = *(float*)(message + 1);
  94. float health = *(float*)(message + 5);
  95. float maxStamina = *(float*)(message + 9);
  96. float stamina = *(float*)(message + 13);
  97. float maxHunger = *(float*)(message + 17);
  98. float hunger = *(float*)(message + 21);
  99. float maxThirst = *(float*)(message + 25);
  100. float thirst = *(float*)(message + 29);
  101. this->health = health / maxHealth;
  102. this->energy = stamina / maxStamina;
  103. this->hunger = hunger / maxHunger;
  104. this->thirst = thirst / maxThirst;
  105. lockZeichnung();
  106. healthText = (int)ceil(health);
  107. energyText = (int)ceil(stamina);
  108. hungerText = (int)ceil(hunger);
  109. thirstText = (int)ceil(thirst);
  110. unlockZeichnung();
  111. break;
  112. }
  113. case 1: // update health
  114. {
  115. float maxHealth = *(float*)(message + 1);
  116. float health = *(float*)(message + 5);
  117. this->health = health / maxHealth;
  118. lockZeichnung();
  119. healthText = (int)ceil(health);
  120. unlockZeichnung();
  121. break;
  122. }
  123. case 2: // update stamina
  124. {
  125. float maxStamina = *(float*)(message + 1);
  126. float stamina = *(float*)(message + 5);
  127. this->energy = stamina / maxStamina;
  128. lockZeichnung();
  129. energyText = (int)ceil(stamina);
  130. unlockZeichnung();
  131. break;
  132. }
  133. case 3: // update hunger
  134. {
  135. float maxHunger = *(float*)(message + 1);
  136. float hunger = *(float*)(message + 5);
  137. this->hunger = hunger / maxHunger;
  138. lockZeichnung();
  139. hungerText = (int)ceil(hunger);
  140. unlockZeichnung();
  141. break;
  142. }
  143. case 4: // update thirst
  144. {
  145. float maxThirst = *(float*)(message + 1);
  146. float thirst = *(float*)(message + 5);
  147. this->thirst = thirst / maxThirst;
  148. lockZeichnung();
  149. thirstText = (int)ceil(thirst);
  150. unlockZeichnung();
  151. break;
  152. }
  153. }
  154. setRender();
  155. }
  156. void StatusBarsView::render(Framework::Bild& rObj)
  157. {
  158. ZeichnungHintergrund::render(rObj);
  159. if (!rObj.setDrawOptions(pos.x + getRahmenBreite(),
  160. pos.y + getRahmenBreite(),
  161. gr.x - getRahmenBreite() * 2,
  162. gr.y - getRahmenBreite() * 2))
  163. return;
  164. rObj.alphaBild(0, 0, 20, 20, *healthIcon);
  165. rObj.alphaBild(gr.x - getRahmenBreite() * 2 - 20, 10, 20, 20, *energyIcon);
  166. rObj.alphaBild(0, 20, 20, 20, *hungerIcon);
  167. rObj.alphaBild(gr.x - getRahmenBreite() * 2 - 20, 30, 20, 20, *thirstIcon);
  168. lockZeichnung();
  169. tr.renderText(25, 4, healthText, rObj, 0xFFFFFFFF);
  170. tr.renderText(25, 24, hungerText, rObj, 0xFFFFFFFF);
  171. int leftTxtBr
  172. = MAX(tr.getTextBreite(healthText), tr.getTextBreite(hungerText));
  173. int rightTxtBr
  174. = MAX(tr.getTextBreite(energyText), tr.getTextBreite(thirstText));
  175. tr.renderText(gr.x - getRahmenBreite() * 2 - 25 - rightTxtBr,
  176. 14,
  177. energyText,
  178. rObj,
  179. 0xFFFFFFFF);
  180. tr.renderText(gr.x - getRahmenBreite() * 2 - 25 - rightTxtBr,
  181. 34,
  182. thirstText,
  183. rObj,
  184. 0xFFFFFFFF);
  185. unlockZeichnung();
  186. int leftWidth = gr.x - 62 - leftTxtBr - getRahmenBreite() * 2 - rightTxtBr;
  187. if (leftWidth > 0)
  188. {
  189. rObj.drawLinieVAlpha(30 + leftTxtBr, 0, gr.y, 0xA052525E);
  190. rObj.drawLinieVAlpha(gr.x - getRahmenBreite() * 2 - 31 - rightTxtBr,
  191. 0,
  192. gr.y,
  193. 0xA052525E);
  194. for (int i = 0; i <= 6; i++)
  195. rObj.drawLinieHAlpha(30 + leftTxtBr + 1, i, leftWidth, 0xA052525E);
  196. for (int i = 13; i <= 16; i++)
  197. rObj.drawLinieHAlpha(30 + leftTxtBr + 1, i, leftWidth, 0xA052525E);
  198. for (int i = 23; i <= 22; i++)
  199. rObj.drawLinieHAlpha(30 + leftTxtBr + 1, i, leftWidth, 0xA052525E);
  200. for (int i = 23; i <= 32; i++)
  201. rObj.drawLinieHAlpha(30 + leftTxtBr + 1, i, leftWidth, 0xA052525E);
  202. for (int i = 33; i <= 42; i++)
  203. rObj.drawLinieHAlpha(30 + leftTxtBr + 1, i, leftWidth, 0xA052525E);
  204. for (int i = 43; i <= 49; i++)
  205. rObj.drawLinieHAlpha(30 + leftTxtBr + 1, i, leftWidth, 0xA052525E);
  206. for (int i = 0; i < 6; i++)
  207. {
  208. rObj.drawLinieH(30 + leftTxtBr + 1,
  209. 7 + i,
  210. (int)((float)leftWidth * health),
  211. 0xFFB9180C);
  212. rObj.drawLinieHAlpha(
  213. 30 + leftTxtBr + 1 + (int)((float)leftWidth * health),
  214. 7 + i,
  215. leftWidth - (int)((float)leftWidth * health),
  216. 0xA052525E);
  217. rObj.drawLinieH(30 + leftTxtBr + 1,
  218. 17 + i,
  219. (int)((float)leftWidth * energy),
  220. 0xFFF0C800);
  221. rObj.drawLinieHAlpha(
  222. 30 + leftTxtBr + 1 + (int)((float)leftWidth * energy),
  223. 17 + i,
  224. leftWidth - (int)((float)leftWidth * energy),
  225. 0xA052525E);
  226. rObj.drawLinieH(30 + leftTxtBr + 1,
  227. 27 + i,
  228. (int)((float)leftWidth * hunger),
  229. 0xFF008300);
  230. rObj.drawLinieHAlpha(
  231. 30 + leftTxtBr + 1 + (int)((float)leftWidth * hunger),
  232. 27 + i,
  233. leftWidth - (int)((float)leftWidth * hunger),
  234. 0xA052525E);
  235. rObj.drawLinieH(30 + leftTxtBr + 1,
  236. 37 + i,
  237. (int)((float)leftWidth * thirst),
  238. 0xFF0018FF);
  239. rObj.drawLinieHAlpha(
  240. 30 + leftTxtBr + 1 + (int)((float)leftWidth * thirst),
  241. 37 + i,
  242. leftWidth - (int)((float)leftWidth * thirst),
  243. 0xA052525E);
  244. }
  245. }
  246. rObj.releaseDrawOptions();
  247. }