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.

156 lines
4.9 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "coreWidget.h"
  2. sdlCoreWidget::sdlCoreWidget(int x, int y, int width, int height, bool decor, sdlCoreWidget * pParent):sdlGraphicElement(x, y, width, height){
  3. m_pParent = pParent;
  4. m_sColorStyle = DEFAULT_COLOR_STYLE;
  5. m_pTexItem = NULL;
  6. m_pContentTexture = NULL;
  7. m_bNoDecoration = !decor;
  8. m_bInitComplete = false;
  9. m_pEnabled = true;
  10. m_pDecoration = sdlDecoration::getInstance();
  11. m_pSubNodeProtector = new basicMutex();
  12. memset(&m_sBgColor, 0, sizeof(SDL_Color));
  13. if( m_pParent != NULL )m_pParent->addWidget(this);
  14. };
  15. sdlCoreWidget::~sdlCoreWidget(){
  16. if (m_pParent != NULL) m_pParent->removeWidget(this);
  17. if (m_pTexItem != NULL) DestroyTextureHeapItem(m_pTexItem);
  18. if (m_pContentTexture != NULL) DestroyTextureHeapItem(m_pContentTexture);
  19. if (m_pSubNodeProtector != NULL) delete(m_pSubNodeProtector);
  20. };
  21. bool sdlCoreWidget::create(void) { // Runs in SDL_MAIN context
  22. if (!m_bInitComplete) {
  23. if (m_pGUI== NULL) return false;
  24. m_pTexItem = m_pGUI->createTexture(m_sGeometry.w, m_sGeometry.h);
  25. if (!m_bNoDecoration) {
  26. SDL_Rect tR = m_pDecoration->getClipRect(m_sGeometry);
  27. m_pContentTexture = m_pGUI->createTexture(tR.w, tR.h);
  28. };
  29. m_bInitComplete = true;
  30. };
  31. return m_bInitComplete;
  32. };
  33. void sdlCoreWidget::render(SDL_Renderer *pRenderer){
  34. if (!m_bInitComplete) return;
  35. if (pRenderer == NULL) return;
  36. if (m_bHidden) return;
  37. if (m_pTexItem == NULL) {
  38. m_bInitComplete = false;
  39. return;
  40. };
  41. if (m_pTexItem->texture == NULL) return;
  42. SDL_Texture* backup;
  43. backup = SDL_GetRenderTarget(pRenderer);
  44. if (m_bUpdateReq){
  45. SDL_SetRenderDrawBlendMode(pRenderer, SDL_BLENDMODE_BLEND);
  46. SDL_SetTextureBlendMode(m_pTexItem->texture, SDL_BLENDMODE_BLEND);
  47. SDL_SetRenderTarget(pRenderer, m_pTexItem->texture);
  48. SDL_Rect VPRect;
  49. if (m_bNoDecoration) {
  50. SDL_SetRenderDrawColor(pRenderer, m_sBgColor.r, m_sBgColor.g, m_sBgColor.b, m_sBgColor.a);
  51. SDL_RenderClear(pRenderer);
  52. } else {
  53. SDL_SetRenderDrawColor(pRenderer, 0, 0, 0, 0);
  54. SDL_RenderClear(pRenderer);
  55. m_pDecoration->make_back(this, pRenderer);
  56. VPRect = m_pDecoration->getClipRect(m_sGeometry);
  57. if (m_pContentTexture != NULL ) {
  58. SDL_SetTextureBlendMode(m_pContentTexture->texture, SDL_BLENDMODE_BLEND);
  59. SDL_SetRenderTarget(pRenderer, m_pContentTexture->texture);
  60. };
  61. };
  62. drawBefore(pRenderer);
  63. m_pSubNodeProtector->lock();
  64. if (m_vWidgets.size()) {
  65. for (pWidgetVector::iterator index = m_vWidgets.begin(); index < m_vWidgets.end(); index++) {
  66. if ((*index)->m_bInitComplete) {
  67. (*index)->render(pRenderer);
  68. }else {
  69. if((*index)->create())(*index)->render(pRenderer);
  70. };
  71. };
  72. };
  73. m_pSubNodeProtector->unlock();
  74. drawAfter(pRenderer);
  75. if (!m_bNoDecoration) {
  76. if (m_pContentTexture != NULL) {
  77. SDL_SetRenderTarget(pRenderer, m_pTexItem->texture);
  78. SDL_RenderCopy(pRenderer, m_pContentTexture->texture, NULL, &VPRect);
  79. };
  80. m_pDecoration->decorate( this, pRenderer );
  81. };
  82. };
  83. SDL_SetRenderTarget(pRenderer, backup);
  84. SDL_RenderCopy(pRenderer, m_pTexItem->texture, NULL, &m_sGeometry);
  85. };
  86. void sdlCoreWidget::setColorStyle(sdlColorStyle style){
  87. m_sColorStyle = style;
  88. };
  89. void sdlCoreWidget::addWidget(sdlCoreWidget* tPtr) {
  90. if (tPtr == NULL) return;
  91. m_pSubNodeProtector->lock();
  92. m_vWidgets.push_back(tPtr);
  93. m_pSubNodeProtector->unlock();
  94. m_bUpdateReq = true;
  95. };
  96. void sdlCoreWidget::removeWidget(sdlCoreWidget* tPtr) {
  97. if (tPtr == NULL) return;
  98. m_pSubNodeProtector->lock();
  99. if (m_vWidgets.size()) {
  100. for (pWidgetVector::iterator index = m_vWidgets.begin(); index < m_vWidgets.end(); index++) {
  101. if ((*index) == tPtr) {
  102. m_vWidgets.erase(index);
  103. break;
  104. };
  105. };
  106. };
  107. m_pSubNodeProtector->unlock();
  108. };
  109. void sdlCoreWidget::requerstUpdate(void) {
  110. m_bUpdateReq = true;
  111. };
  112. void sdlCoreWidget::setBgColor(SDL_Color Param) {
  113. memcpy(&m_sBgColor, &Param, sizeof(SDL_Color));
  114. m_bUpdateReq = true;
  115. };
  116. void sdlCoreWidget::setBgColor(uint8_t r, uint8_t g, uint8_t b, uint8_t a ) {
  117. m_sBgColor.a = a;
  118. m_sBgColor.r = r;
  119. m_sBgColor.g = g;
  120. m_sBgColor.b = b;
  121. m_bUpdateReq = true;
  122. };
  123. void sdlCoreWidget::setEnabled(void) {
  124. m_pSubNodeProtector->lock();
  125. if (m_vWidgets.size()) {
  126. for (pWidgetVector::iterator index = m_vWidgets.begin(); index < m_vWidgets.end(); index++)(*index)->setEnabled();
  127. };
  128. m_pSubNodeProtector->unlock();
  129. m_pEnabled = true;
  130. };
  131. void sdlCoreWidget::setDisabled(void) {
  132. m_pSubNodeProtector->lock();
  133. if (m_vWidgets.size()) {
  134. for (pWidgetVector::iterator index = m_vWidgets.begin(); index < m_vWidgets.end(); index++)(*index)->setDisabled();
  135. };
  136. m_pSubNodeProtector->unlock();
  137. m_pEnabled = false;
  138. };
  139. void sdlCoreWidget::dispatch(SDL_Event* pEvt) {
  140. if (m_vWidgets.size()) {
  141. for (pWidgetVector::iterator index = m_vWidgets.begin(); index < m_vWidgets.end(); index++) (*index)->dispatch(pEvt);
  142. };
  143. };