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
156 lines
4.9 KiB
#include "coreWidget.h"
|
|
|
|
sdlCoreWidget::sdlCoreWidget(int x, int y, int width, int height, bool decor, sdlCoreWidget * pParent):sdlGraphicElement(x, y, width, height){
|
|
m_pParent = pParent;
|
|
m_sColorStyle = DEFAULT_COLOR_STYLE;
|
|
m_pTexItem = NULL;
|
|
m_pContentTexture = NULL;
|
|
m_bNoDecoration = !decor;
|
|
m_bInitComplete = false;
|
|
m_pEnabled = true;
|
|
m_pDecoration = sdlDecoration::getInstance();
|
|
m_pSubNodeProtector = new basicMutex();
|
|
memset(&m_sBgColor, 0, sizeof(SDL_Color));
|
|
if( m_pParent != NULL )m_pParent->addWidget(this);
|
|
};
|
|
|
|
sdlCoreWidget::~sdlCoreWidget(){
|
|
if (m_pParent != NULL) m_pParent->removeWidget(this);
|
|
if (m_pTexItem != NULL) DestroyTextureHeapItem(m_pTexItem);
|
|
if (m_pContentTexture != NULL) DestroyTextureHeapItem(m_pContentTexture);
|
|
if (m_pSubNodeProtector != NULL) delete(m_pSubNodeProtector);
|
|
};
|
|
|
|
bool sdlCoreWidget::create(void) { // Runs in SDL_MAIN context
|
|
if (!m_bInitComplete) {
|
|
if (m_pGUI== NULL) return false;
|
|
m_pTexItem = m_pGUI->createTexture(m_sGeometry.w, m_sGeometry.h);
|
|
if (!m_bNoDecoration) {
|
|
SDL_Rect tR = m_pDecoration->getClipRect(m_sGeometry);
|
|
m_pContentTexture = m_pGUI->createTexture(tR.w, tR.h);
|
|
};
|
|
m_bInitComplete = true;
|
|
};
|
|
return m_bInitComplete;
|
|
};
|
|
|
|
void sdlCoreWidget::render(SDL_Renderer *pRenderer){
|
|
if (!m_bInitComplete) return;
|
|
if (pRenderer == NULL) return;
|
|
if (m_bHidden) return;
|
|
if (m_pTexItem == NULL) {
|
|
m_bInitComplete = false;
|
|
return;
|
|
};
|
|
if (m_pTexItem->texture == NULL) return;
|
|
SDL_Texture* backup;
|
|
backup = SDL_GetRenderTarget(pRenderer);
|
|
if (m_bUpdateReq){
|
|
SDL_SetRenderDrawBlendMode(pRenderer, SDL_BLENDMODE_BLEND);
|
|
SDL_SetTextureBlendMode(m_pTexItem->texture, SDL_BLENDMODE_BLEND);
|
|
SDL_SetRenderTarget(pRenderer, m_pTexItem->texture);
|
|
SDL_Rect VPRect;
|
|
if (m_bNoDecoration) {
|
|
SDL_SetRenderDrawColor(pRenderer, m_sBgColor.r, m_sBgColor.g, m_sBgColor.b, m_sBgColor.a);
|
|
SDL_RenderClear(pRenderer);
|
|
} else {
|
|
SDL_SetRenderDrawColor(pRenderer, 0, 0, 0, 0);
|
|
SDL_RenderClear(pRenderer);
|
|
m_pDecoration->make_back(this, pRenderer);
|
|
VPRect = m_pDecoration->getClipRect(m_sGeometry);
|
|
if (m_pContentTexture != NULL ) {
|
|
SDL_SetTextureBlendMode(m_pContentTexture->texture, SDL_BLENDMODE_BLEND);
|
|
SDL_SetRenderTarget(pRenderer, m_pContentTexture->texture);
|
|
};
|
|
};
|
|
drawBefore(pRenderer);
|
|
m_pSubNodeProtector->lock();
|
|
if (m_vWidgets.size()) {
|
|
for (pWidgetVector::iterator index = m_vWidgets.begin(); index < m_vWidgets.end(); index++) {
|
|
if ((*index)->m_bInitComplete) {
|
|
(*index)->render(pRenderer);
|
|
}else {
|
|
if((*index)->create())(*index)->render(pRenderer);
|
|
};
|
|
};
|
|
};
|
|
m_pSubNodeProtector->unlock();
|
|
drawAfter(pRenderer);
|
|
if (!m_bNoDecoration) {
|
|
if (m_pContentTexture != NULL) {
|
|
SDL_SetRenderTarget(pRenderer, m_pTexItem->texture);
|
|
SDL_RenderCopy(pRenderer, m_pContentTexture->texture, NULL, &VPRect);
|
|
};
|
|
m_pDecoration->decorate( this, pRenderer );
|
|
};
|
|
};
|
|
SDL_SetRenderTarget(pRenderer, backup);
|
|
SDL_RenderCopy(pRenderer, m_pTexItem->texture, NULL, &m_sGeometry);
|
|
};
|
|
|
|
void sdlCoreWidget::setColorStyle(sdlColorStyle style){
|
|
m_sColorStyle = style;
|
|
};
|
|
|
|
void sdlCoreWidget::addWidget(sdlCoreWidget* tPtr) {
|
|
if (tPtr == NULL) return;
|
|
m_pSubNodeProtector->lock();
|
|
m_vWidgets.push_back(tPtr);
|
|
m_pSubNodeProtector->unlock();
|
|
m_bUpdateReq = true;
|
|
};
|
|
|
|
void sdlCoreWidget::removeWidget(sdlCoreWidget* tPtr) {
|
|
if (tPtr == NULL) return;
|
|
m_pSubNodeProtector->lock();
|
|
if (m_vWidgets.size()) {
|
|
for (pWidgetVector::iterator index = m_vWidgets.begin(); index < m_vWidgets.end(); index++) {
|
|
if ((*index) == tPtr) {
|
|
m_vWidgets.erase(index);
|
|
break;
|
|
};
|
|
};
|
|
};
|
|
m_pSubNodeProtector->unlock();
|
|
};
|
|
|
|
void sdlCoreWidget::requerstUpdate(void) {
|
|
m_bUpdateReq = true;
|
|
};
|
|
|
|
void sdlCoreWidget::setBgColor(SDL_Color Param) {
|
|
memcpy(&m_sBgColor, &Param, sizeof(SDL_Color));
|
|
m_bUpdateReq = true;
|
|
};
|
|
|
|
void sdlCoreWidget::setBgColor(uint8_t r, uint8_t g, uint8_t b, uint8_t a ) {
|
|
m_sBgColor.a = a;
|
|
m_sBgColor.r = r;
|
|
m_sBgColor.g = g;
|
|
m_sBgColor.b = b;
|
|
m_bUpdateReq = true;
|
|
};
|
|
|
|
void sdlCoreWidget::setEnabled(void) {
|
|
m_pSubNodeProtector->lock();
|
|
if (m_vWidgets.size()) {
|
|
for (pWidgetVector::iterator index = m_vWidgets.begin(); index < m_vWidgets.end(); index++)(*index)->setEnabled();
|
|
};
|
|
m_pSubNodeProtector->unlock();
|
|
m_pEnabled = true;
|
|
};
|
|
|
|
void sdlCoreWidget::setDisabled(void) {
|
|
m_pSubNodeProtector->lock();
|
|
if (m_vWidgets.size()) {
|
|
for (pWidgetVector::iterator index = m_vWidgets.begin(); index < m_vWidgets.end(); index++)(*index)->setDisabled();
|
|
};
|
|
m_pSubNodeProtector->unlock();
|
|
m_pEnabled = false;
|
|
};
|
|
|
|
void sdlCoreWidget::dispatch(SDL_Event* pEvt) {
|
|
if (m_vWidgets.size()) {
|
|
for (pWidgetVector::iterator index = m_vWidgets.begin(); index < m_vWidgets.end(); index++) (*index)->dispatch(pEvt);
|
|
};
|
|
};
|