You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

251 lines
7.1 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "scrollListWidget.h"
  2. ScrollListItem::ScrollListItem(CSTR text, int fontSize) :sdlLabel(text, 0, 0, -1, -1, NULL, fontSize) {
  3. m_bSelected = false;
  4. };
  5. ScrollListItem::~ScrollListItem() {
  6. };
  7. void ScrollListItem::setSelected(bool a) {
  8. m_bSelected = a;
  9. };
  10. sdlScrollListWidget::sdlScrollListWidget(int x, int y, int w, int h, int fontsize, bool deco, sdlCoreWidget* pPtr) :sdlCoreWidget(x, y, w, h, deco, pPtr) {
  11. m_nFontSize = fontsize;
  12. m_nLineSize = m_nFontSize + 4;
  13. m_nXOffset = 0;
  14. m_nYOffset = 0;
  15. m_nContentX = 0;
  16. m_nContentY = 0;
  17. m_nSelectedID = -1;
  18. m_nPreSelectedID = -1;
  19. m_pFillParameter = NULL;
  20. m_bVScroll = true;
  21. m_bHScroll = true;
  22. m_bUnderCursor = false;
  23. sdlGraphicElement::setType(GEL_SIMPLE_PANEL);
  24. if (pPtr == NULL) {
  25. m_pGUI->registerGEL(this);
  26. } else pPtr->addWidget(this);
  27. };
  28. sdlScrollListWidget::~sdlScrollListWidget() {
  29. m_pGUI->removeGEL(this);
  30. };
  31. void sdlScrollListWidget::drawAfter(SDL_Renderer* pRenderer) {
  32. if (pRenderer == NULL) return;
  33. if (m_bVScroll) {
  34. if (m_nContentY < m_sGeometry.h) { // no need vbar
  35. SDL_Rect tR;
  36. } else {
  37. SDL_Rect tR;
  38. int but_l = (m_sGeometry.h * m_sGeometry.h) / m_nContentY;
  39. int z_max = (m_nContentY - m_sGeometry.h);
  40. int l_max = m_sGeometry.h - 9 - but_l;
  41. int c_y = ( l_max * m_nYOffset ) / l_max;
  42. tR.x = m_sGeometry.w - 3 - 8;
  43. tR.y = 2;
  44. tR.w = 3;
  45. tR.h = m_sGeometry.h - 3 - 6;
  46. SDL_SetRenderDrawColor(pRenderer, 0, 0, 0, 255);
  47. SDL_RenderDrawRect(pRenderer, &tR);
  48. tR.x = m_sGeometry.w - 3 - 8;
  49. tR.y = 2 + c_y;
  50. tR.w = 3;
  51. tR.h = but_l;
  52. SDL_SetRenderDrawColor(pRenderer, 0, 0, 0, 255);
  53. SDL_RenderFillRect(pRenderer, &tR);
  54. };
  55. };
  56. if (m_bHScroll) {
  57. };
  58. };
  59. int sdlScrollListWidget::addItem(ScrollListItem* ptr) {
  60. if (ptr != NULL) {
  61. ptr->setAlign(ALIGN_LEFT);
  62. ptr->setGeometry(0, m_vLines.size() * m_nLineSize, m_sGeometry.w, m_nLineSize );
  63. m_nContentY += m_nLineSize;
  64. if (m_vLines.size() % 2 == 0) {
  65. ptr->setBgColor(80, 80, 80, 20);
  66. } else {
  67. ptr->setBgColor(100, 100, 100, 20);
  68. };
  69. m_vLines.push_back(ptr);
  70. addWidget(ptr);
  71. return (m_vLines.size() - 1);
  72. };
  73. return -1;
  74. };
  75. int sdlScrollListWidget::addItem(CSTR text) {
  76. ScrollListItem* tVal = NULL;
  77. tVal = new ScrollListItem(text, m_nFontSize);
  78. return addItem(tVal);
  79. };
  80. void sdlScrollListWidget::selectItem(int index) {
  81. if (index < m_vLines.size()) {
  82. int cy = -index * m_nLineSize;
  83. m_nSelectedID = index;
  84. if (m_pFillParameter != NULL) {
  85. sprintf(m_pFillParameter, "%s", m_vLines.at(m_nSelectedID)->getText());
  86. };
  87. m_nYOffset = ((m_nYOffset - cy) < (m_nContentY - m_sGeometry.h + 6)) ? (m_nYOffset - cy) : (m_nContentY - m_sGeometry.h + 6);
  88. m_vLines.at(m_nSelectedID)->setBgColor(255, 186, 0);
  89. m_vLines.at(index)->setSelected(true);
  90. };
  91. };
  92. int sdlScrollListWidget::getSelectedIndex() {
  93. return -1;
  94. };
  95. ScrollListItem* sdlScrollListWidget::getSelectedItem() {
  96. return NULL;
  97. };
  98. ScrollListItem* sdlScrollListWidget::getItem(int index) {
  99. return NULL;
  100. };
  101. bool sdlScrollListWidget::create(void) {
  102. return sdlCoreWidget::create();
  103. };
  104. void sdlScrollListWidget::drawBefore(SDL_Renderer* pRenderer) {
  105. if (!m_pEnabled) return;
  106. if (m_vLines.size()) {
  107. for (int index = 0; index < m_vLines.size(); index++) {
  108. int cx = - m_nXOffset;
  109. int cy = index * m_nLineSize - m_nYOffset;
  110. m_vLines.at(index)->moveTo(cx, cy);
  111. };
  112. };
  113. };
  114. void sdlScrollListWidget::dispatch(SDL_Event* pEvt) {
  115. if (!m_pEnabled) return;
  116. if (pEvt->type == SDL_MOUSEMOTION) {
  117. int cx, cy;
  118. if (m_pParent == NULL) {
  119. cx = pEvt->motion.x - m_sGeometry.x;
  120. cy = pEvt->motion.y - m_sGeometry.y;
  121. } else {
  122. cx = pEvt->motion.x - m_sGeometry.x - m_pParent->m_sGeometry.x;
  123. cy = pEvt->motion.y - m_sGeometry.y - m_pParent->m_sGeometry.y;
  124. };
  125. if (cx >= 0 && cy >= 0 && cx <= m_sGeometry.w && cy <= m_sGeometry.h) {
  126. m_bUnderCursor = true;
  127. } else {
  128. m_bUnderCursor = false;
  129. };
  130. if (m_bUnderCursor) {
  131. if (m_vLines.size()) {
  132. int idx = (cy + m_nYOffset) / m_nLineSize;
  133. if (idx < 0) idx = 0;
  134. if (idx > (m_vLines.size()-1)) idx = m_vLines.size()-1;
  135. if (m_nPreSelectedID != idx) {
  136. if (m_nPreSelectedID != -1) {
  137. m_vLines.at(m_nPreSelectedID)->setSelected(false);
  138. if (m_nSelectedID == m_nPreSelectedID) {
  139. m_vLines.at(m_nPreSelectedID)->setBgColor(255, 186, 0);
  140. } else {
  141. if (m_nPreSelectedID % 2 == 0) {
  142. m_vLines.at(m_nPreSelectedID)->setBgColor(80, 80, 80);
  143. } else {
  144. m_vLines.at(m_nPreSelectedID)->setBgColor(100, 100, 100);
  145. };
  146. };
  147. };
  148. m_vLines.at(idx)->setSelected(true);
  149. m_vLines.at(idx)->setBgColor(255, 255, 255);
  150. m_nPreSelectedID = idx;
  151. };
  152. };
  153. } else {
  154. if (m_nPreSelectedID != -1) {
  155. m_vLines.at(m_nPreSelectedID)->setSelected(false);
  156. if (m_nSelectedID != -1) {
  157. if (m_nSelectedID == m_nPreSelectedID) {
  158. m_vLines.at(m_nPreSelectedID)->setBgColor(255, 186, 0);
  159. } else {
  160. if (m_nPreSelectedID % 2 == 0) {
  161. m_vLines.at(m_nPreSelectedID)->setBgColor(80, 80, 80);
  162. } else {
  163. m_vLines.at(m_nPreSelectedID)->setBgColor(100, 100, 100);
  164. };
  165. };
  166. } else {
  167. if (m_nPreSelectedID % 2 == 0) {
  168. m_vLines.at(m_nPreSelectedID)->setBgColor(80, 80, 80);
  169. } else {
  170. m_vLines.at(m_nPreSelectedID)->setBgColor(100, 100, 100);
  171. };
  172. };
  173. m_nPreSelectedID = -1;
  174. };
  175. };
  176. };
  177. if (pEvt->type == SDL_MOUSEWHEEL) {
  178. if (m_bUnderCursor) {
  179. if (pEvt->wheel.y > 0) {
  180. #ifdef WHEEL_MOVES_LINE
  181. int k = m_nLineSize >> 2;
  182. #ifdef WHEEL_REVERSE_CONTROLL
  183. m_nYOffset = ((m_nYOffset + m_nLineSize) < (m_nContentY - m_sGeometry.h)) ? (m_nYOffset + m_nLineSize) : (m_nContentY - m_sGeometry.h);
  184. #else
  185. m_nYOffset = (m_nYOffset > k) ? (m_nYOffset - k) : 0;
  186. #endif
  187. #else
  188. #ifdef WHEEL_REVERSE_CONTROLL
  189. m_nYOffset = ((m_nYOffset + pEvt->wheel.y) < (m_nContentY - m_sGeometry.h)) ? (m_nYOffset + pEvt->wheel.y) : (m_nContentY - m_sGeometry.h);
  190. #else
  191. m_nYOffset = (m_nYOffset > pEvt->wheel.y) ? (m_nYOffset - pEvt->wheel.y) : 0;
  192. #endif
  193. #endif
  194. };
  195. if (pEvt->wheel.y < 0) {
  196. #ifdef WHEEL_MOVES_LINE
  197. int k = m_nLineSize >> 2;
  198. #ifdef WHEEL_REVERSE_CONTROLL
  199. m_nYOffset = (m_nYOffset > m_nLineSize) ? (m_nYOffset - m_nLineSize) : 0;
  200. #else
  201. m_nYOffset = ((m_nYOffset + k) < (m_nContentY - m_sGeometry.h + 6)) ? (m_nYOffset + k) : (m_nContentY - m_sGeometry.h + 6);
  202. #endif
  203. #else
  204. #ifdef WHEEL_REVERSE_CONTROLL
  205. m_nYOffset = (m_nYOffset > -pEvt->wheel.y) ? ( m_nYOffset + pEvt->wheel.y ): 0;
  206. #else
  207. m_nYOffset = ((m_nYOffset - pEvt->wheel.y) < (m_nContentY - m_sGeometry.h+6)) ? (m_nYOffset - pEvt->wheel.y) : (m_nContentY - m_sGeometry.h+6);
  208. #endif
  209. #endif
  210. };
  211. };
  212. };
  213. if (pEvt->type == SDL_MOUSEBUTTONDOWN) {
  214. if (m_bUnderCursor) {
  215. if (m_nSelectedID != -1) {
  216. if (m_nSelectedID % 2 == 0) {
  217. m_vLines.at(m_nSelectedID)->setBgColor(80, 80, 80);
  218. } else {
  219. m_vLines.at(m_nSelectedID)->setBgColor(100, 100, 100);
  220. };
  221. m_nSelectedID = -1;
  222. };
  223. };
  224. };
  225. if (pEvt->type == SDL_MOUSEBUTTONUP) {
  226. if (m_bUnderCursor) {
  227. m_nSelectedID = m_nPreSelectedID;
  228. if (m_pFillParameter != NULL) {
  229. sprintf(m_pFillParameter, "%s", m_vLines.at(m_nSelectedID)->getText());
  230. };
  231. };
  232. };
  233. };
  234. void sdlScrollListWidget::setParamPtr(CSTR ptr) {
  235. m_pFillParameter = ptr;
  236. };