ListView.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. #include "ListView.h"
  2. #include <Font.h>
  3. #include <Image.h>
  4. #include <List.h>
  5. #include <TextField.h>
  6. #include <Window.h>
  7. #include <XML.h>
  8. #include "FactoryClient.h"
  9. #include "World.h"
  10. ListViewElement::ListViewElement()
  11. : Framework::UIMLElement()
  12. {}
  13. bool ListViewElement::isApplicableFor(Framework::XML::Element& element)
  14. {
  15. return element.getName().isEqual("listView");
  16. }
  17. Framework::Drawable* ListViewElement::parseElement(
  18. Framework::XML::Element& element, Framework::UIMLContainer& generalFactory)
  19. {
  20. ListView* result = new ListView();
  21. result->list->setFontZ(dynamic_cast<Framework::Font*>(
  22. generalFactory.getFactory().initParam.font->getThis()));
  23. updateElement(element, *result, generalFactory);
  24. return result;
  25. }
  26. bool ListViewElement::updateElement(Framework::XML::Element& element,
  27. Framework::Drawable& z,
  28. Framework::UIMLContainer& generalFactory)
  29. {
  30. ListView* view = dynamic_cast<ListView*>(&z);
  31. if (!view) return false;
  32. for (int index = 0; index < view->list->getEntryCount(); index++)
  33. {
  34. if (!element.selectChildsByName("listItem")
  35. .whereAttributeEquals("id", *view->ids.z(index))
  36. .exists())
  37. {
  38. view->list->removeEntry(index);
  39. view->ids.remove(index);
  40. index--;
  41. }
  42. }
  43. int childIndex = 0;
  44. for (Framework::XML::Element* zChild :
  45. element.selectChildsByName("listItem"))
  46. {
  47. int index = view->ids.findIndex([zChild](const Framework::Text* zId) {
  48. return zId->isEqual(zChild->getAttributeValue("id"));
  49. });
  50. if (index < 0)
  51. {
  52. Framework::Text id = zChild->getAttributeValue("id");
  53. view->ids.add(new Framework::Text(id));
  54. view->list->addEntry(zChild->getText());
  55. }
  56. else
  57. {
  58. view->list->setEntry(index, zChild->getText());
  59. view->list->setEntryPos(index, childIndex);
  60. }
  61. childIndex++;
  62. };
  63. for (int i = 0; i < view->list->getEntryCount(); i++)
  64. {
  65. view->list->zEntry(i)->setFontSize(20);
  66. view->list->zEntry(i)->setFontColor(0XFFFFFFFF);
  67. view->list->zEntry(i)->setBorderColor(0xFF52525E);
  68. }
  69. int auswahl
  70. = element.selectChildsByAttribute("selected")
  71. .getFirstElement()
  72. .map<int>(
  73. [](Framework::RCPointer<Framework::XML::Element> child) {
  74. return child->zParent()->getChildIndex(child);
  75. })
  76. .orElse(-1);
  77. view->list->setSelection(auswahl);
  78. view->lastSelection = auswahl;
  79. view->onSelected = element.getAttributeValue("onSelectMessage");
  80. return true;
  81. }
  82. void ListViewElement::layout(Framework::XML::Element& element,
  83. Framework::Drawable& z,
  84. int pWidth,
  85. int pHeight,
  86. Framework::UIMLContainer& generalLayouter)
  87. {
  88. UIMLElement::layout(element, z, pWidth, pHeight, generalLayouter);
  89. ListView* view = dynamic_cast<ListView*>(&z);
  90. if (view)
  91. {
  92. Framework::Text height = element.getAttributeValue("member-height");
  93. if (height.isEqual(""))
  94. {
  95. height = 30;
  96. }
  97. for (int i = 0; i < view->list->getEntryCount(); i++)
  98. {
  99. view->list->zEntry(i)->setHeight((int)height);
  100. }
  101. view->list->setSize(view->getInnerWidth(), view->getInnerHeight());
  102. if ((int)height * view->list->getEntryCount() > view->getHeight())
  103. {
  104. view->list->addStyle(Framework::SelectionList::Style::VScroll);
  105. view->list->setVerticalClickScroll((int)height);
  106. view->list->updateVScroll();
  107. }
  108. }
  109. }
  110. ListView::ListView()
  111. : Framework::Drawable(),
  112. list(new Framework::SelectionList())
  113. {
  114. addStyle(Framework::Drawable::Style::Visible
  115. | Framework::Drawable::Style::Allowed);
  116. setMouseEvent(Framework::_ret1ME);
  117. list->setStyle(Framework::SelectionList::Style::Normal);
  118. list->setMouseEvent(Framework::_ret1ME);
  119. list->setBorderColor(0xFF52525E);
  120. list->setBorderWidth(1);
  121. list->setSelectionBorderWidth(2);
  122. list->setSelectionBorderColor(0xFF0018FF);
  123. list->setSelectionAFColor(0xA0337AB7);
  124. list->setSelectionAFStrength(5);
  125. lastSelection = list->getSelection();
  126. }
  127. bool ListView::tick(double tickVal)
  128. {
  129. rend |= list->tick(tickVal);
  130. int auswahl = list->getSelection();
  131. if (auswahl != lastSelection)
  132. {
  133. Framework::Text* dialogName
  134. = onSelected.getTeilText(0, onSelected.positionOf(";"));
  135. Framework::Text* messageId
  136. = onSelected.getTeilText(onSelected.positionOf(";") + 1);
  137. char msg[6];
  138. msg[0] = 0; // element message
  139. msg[1] = (char)(int)*messageId;
  140. *(int*)(msg + 2) = auswahl;
  141. World::INSTANCE->zClient()->uiRequest(*dialogName, msg, 6);
  142. dialogName->release();
  143. messageId->release();
  144. lastSelection = auswahl;
  145. }
  146. return Drawable::tick(tickVal);
  147. }
  148. void ListView::render(Framework::Image& rObj)
  149. {
  150. Drawable::render(rObj);
  151. if (rObj.setDrawOptions(pos, gr))
  152. {
  153. list->render(rObj);
  154. rObj.releaseDrawOptions();
  155. }
  156. }
  157. void ListView::doMouseEvent(Framework::MouseEvent& me, bool userRet)
  158. {
  159. bool vera = me.processed;
  160. if (!userRet)
  161. {
  162. me.processed = 1;
  163. }
  164. list->doPublicMouseEvent(me);
  165. if (!userRet)
  166. {
  167. me.processed = vera;
  168. }
  169. }