#include "cWindow.hpp" #include "../FXEngine/cGFX.hpp" #include "../UtilityEngine/cUtility.hpp" #include "cGUI.hpp" #include "cLabel.hpp" #include "cLayout.hpp" using GUIEngine::cWindow; /*static*/ const UtilityEngine::cString cWindow::sNAME = "windows"; /*static*/ GUIHelpers::Position cWindow::sPOSITION = { 0, 0 }; /*static*/ GUIHelpers::Size cWindow::sSIZE = { 0, 0 }; /*static*/ GUIHelpers::Padding cWindow::sPADDING = { 5, 5, 5, 5 }; /*static*/ GUIHelpers::RGBA cWindow::sCOLOUR = { 100, 100, 100, 255 }; /*static*/ GUIHelpers::eAlign cWindow::sALIGN = GUIHelpers::eAlign::CENTER; /*static*/ GUIHelpers::eLayout cWindow::sLAYOUT = GUIHelpers::eLayout::FILL_PARENT; /*static*/ GUIHelpers::eOrientation cWindow::sORIENTATION = GUIHelpers::eOrientation::NONE; /*static*/ unsigned long int cWindow::sDebug = 0; cWindow::cWindow(cWindow* parent /*= nullptr*/, const signed int id /*= -1*/, const GUIHelpers::eOrientation& orientation /*= sORIENTATION*/, const GUIHelpers::eAlign& align /*= sALIGN*/, const GUIHelpers::eLayout& layout /*= sLAYOUT*/, const GUIHelpers::Position& pos /*= sPOSITION*/, const GUIHelpers::Size& size /*= sSIZE*/, const GUIHelpers::Padding& padding /*= sPADDING*/, const cString& name /*= sNAME*/) : cObject(id) { Create(parent, id, orientation, align, layout, pos, size, padding, name); } /*virtual*/ cWindow::~cWindow() { m_children.clear(); } /*virtual*/ void cWindow::Create(cWindow* parent /*= nullptr*/, const signed int id /*= -1*/, const GUIHelpers::eOrientation& orientation /*= sORIENTATION*/, const GUIHelpers::eAlign& align /*= sALIGN*/, const GUIHelpers::eLayout& layout /*= sLAYOUT*/, const GUIHelpers::Position& pos /*= sPOSITION*/, const GUIHelpers::Size& size /*= sSIZE*/, const GUIHelpers::Padding& padding /*= sPADDING*/, const cString& name /*= sNAME*/) { mp_parent = parent; cObject::setID(id); setOrientation(orientation); setAlign(align); setLayout(layout); m_pos = pos; m_size = size; setPadding(padding); m_name = name; } /*virtual*/ void cWindow::Display() { std::vector::iterator it; for (it = m_children.begin(); it < m_children.end(); it++) { (*it)->Display(); } DebugDisplay(); } ///Functions const bool cWindow::Inside(const MathEngine::iVector2& location) const { bool rtn = false; if ((location.x >= m_pos.x) && (location.y >= m_pos.y)) { if ((location.x <= (m_pos.x + m_size.x)) && (location.y <= (m_pos.y + m_size.y))) rtn = true; } return rtn; } void cWindow::AddChild( cWindow* obj ) { m_children.push_back(obj); } void cWindow::RemoveChild( cWindow* obj ) { std::vector::iterator it; for (it = m_children.begin(); it < m_children.end(); it++) { if (obj == (*it)) { m_children.erase(it); //break out of the for loop break; } } } ///Sets void cWindow::setOrientation( const GUIHelpers::eOrientation& orientation /*= sORIENTATION*/ ) { if (orientation == GUIHelpers::eOrientation::DEFAULT_ORIENTATION) m_orientation = cWindow::sORIENTATION; else m_orientation = orientation; } void cWindow::setAlign( const GUIHelpers::eAlign& align /*= sALIGN*/ ) { if (align == GUIHelpers::eAlign::DEFAULT_ALIGN) m_align = cWindow::sALIGN; else m_align = align; } void cWindow::setLayout( const GUIHelpers::eLayout& layout /*= sLAYOUT*/ ) { if (layout == GUIHelpers::eLayout::DEFAULT_LAYOUT) m_layout = cWindow::sLAYOUT; else m_layout = layout; } void cWindow::setPosition( const GUIHelpers::Position& pos /*= POSITION*/ ) { m_pos = pos; } void cWindow::setSize( const GUIHelpers::Size& size /*= SIZE*/ ) { m_size = size; } void cWindow::setPadding( const GUIHelpers::Padding& padding /*= PADDING*/ ) { if (padding.x < 0) m_padding = cWindow::sPADDING; else m_padding = padding; } void cWindow::setContentSize( const GUIHelpers::Size& content ) { m_content = content; } void cWindow::setName( const cString& name /*= NAME*/ ) { m_name = name; } void cWindow::setParent( cWindow* parent ) { mp_parent = parent; } ///Gets const GUIHelpers::eOrientation& cWindow::getOrientation() const { return m_orientation; } const GUIHelpers::eAlign& cWindow::getAlign() const { return m_align; } const GUIHelpers::eLayout& cWindow::getLayout() const { if (m_layout == GUIHelpers::eLayout::MATCH_PARENT) { if (mp_parent != nullptr) return mp_parent->getLayout(); } return m_layout; } const GUIHelpers::Position& cWindow::getPosition() const { return m_pos; } const GUIHelpers::Size& cWindow::getSize() const { return m_size; } const GUIHelpers::Padding& cWindow::getPadding() const { return m_padding; } const GUIHelpers::Size& cWindow::getContentSize() const { return m_content; } const cString& cWindow::getName() const { return m_name; } cWindow* cWindow::getParent() { return mp_parent; } /*virtual*/ const GUIHelpers::eType cWindow::getType() const { return GUIHelpers::eType::CWINDOW; } ///Protected /*virtual*/ void cWindow::Resize() { RebuildLayout(this->getLayout()); RePos(); } /*virtual*/ void cWindow::RebuildLayout(const GUIHelpers::eLayout& layout) { GUIHelpers::Size mySize = { 0, 0 }; std::vector::iterator it; switch (layout) { case GUIHelpers::eLayout::MATCH_PARENT: case GUIHelpers::eLayout::FILL_PARENT: if (mp_parent != nullptr) { mySize = mp_parent->getSize(); mySize.x -= m_padding.x + m_padding.w; mySize.y -= m_padding.y + m_padding.z; } else { mySize = GUIEngine::cGUI::Inst().DisplayArea(); } m_size = mySize; for (it = m_children.begin(); it < m_children.end(); it++) { //if ((*it)->getLayout() != GUIHelpers::eLayout::WRAP_CONTENT) (*it)->Resize(); } break; case GUIHelpers::eLayout::WRAP_CONTENT: GUIHelpers::Size size = { 0, 0 }; GUIHelpers::Padding pad = { 0, 0, 0, 0 }; for (it = m_children.begin(); it < m_children.end(); it++) { (*it)->Resize(); size = (*it)->getSize(); pad = (*it)->getPadding(); size.x += pad.x + pad.w; size.y += pad.y + pad.z; //mySize += size; AddSize(size.x, size.y, mySize); } m_size = mySize; break; } } /*virtual*/ void cWindow::RePos() { // if (mp_parent != nullptr) // RebuildPos(); std::vector::iterator it; for (it = m_children.begin(); it < m_children.end(); it++) { (*it)->RePos(); } if (this->getType() == GUIHelpers::eType::CLAYOUT) { GUIHelpers::Position ourSet = { 0, 0 }; GUIHelpers::Size totalSize = { 0, 0 }; GUIHelpers::Position totalCenter = { 0, 0 }; GUIHelpers::Position prtCenter = { (m_size.x / 2), (m_size.y / 2) }; for (it = m_children.begin(); it < m_children.end(); it++) { GUIHelpers::Size size = (*it)->getSize(); GUIHelpers::Padding pad = (*it)->getPadding(); totalSize += size; totalSize.x += pad.x + pad.w; totalSize.y += pad.y + pad.z; } totalCenter.x = totalSize.x / 2; totalCenter.y = totalSize.y / 2; ourSet.x = prtCenter.x - totalCenter.x; ourSet.y = prtCenter.y - totalCenter.y; GUIHelpers::Position posSet = ourSet; GUIHelpers::Position posNext = posSet; for (it = m_children.begin(); it < m_children.end(); it++) { posSet = posNext; GUIHelpers::Padding pad = (*it)->getPadding(); GUIHelpers::Size size = (*it)->getSize(); if (m_orientation == GUIHelpers::eOrientation::HORIZONTAL) { posSet.x += pad.x; posSet.y = (pad.y - pad.z - (size.y / 2)) + prtCenter.y; posNext = posSet; posNext.x += size.x + pad.w; //posNext.y = ourSet.y; } else { posSet.x = (pad.x - pad.w - (size.x / 2)) + prtCenter.x; posSet.y += pad.y; posNext = posSet; //posNext.x = ourSet.x; posNext.y += size.y + pad.z; } (*it)->setPosition(posSet); } } } /*virtual*/ void cWindow::RebuildPos() { GUIHelpers::Position prtPos = mp_parent->getPosition(); GUIHelpers::Size prtSize = mp_parent->getSize(); GUIHelpers::Position prtCenter = { (prtSize.x / 2), (prtSize.y / 2) }; GUIHelpers::Size ourSize = this->getSize(); GUIHelpers::Position ourCenter = { (ourSize.x / 2), (ourSize.y / 2) }; GUIHelpers::Position ourSet = { 0, 0 }; ourSet.x = prtCenter.x - ourCenter.x; ourSet.y = prtCenter.y - ourCenter.y; // // switch (this->getAlign()) { // case GUIHelpers::eAlign::CENTER: // ourSet.x = prtCenter.x - ourCenter.x; // ourSet.y = prtCenter.y - ourCenter.y; // break; // case GUIHelpers::eAlign::TOP: // ourSet.x = prtCenter.x - ourCenter.x; // ourSet.y = prtPos.y + m_padding.y; // break; // case GUIHelpers::eAlign::RIGHT: // ourSet.x = prtSize.x - ourSize.x - m_padding.w; // ourSet.y = prtCenter.y - ourCenter.y; // break; // case GUIHelpers::eAlign::BOTTOM: // ourSet.x = prtCenter.x - ourCenter.x; // ourSet.y = prtSize.y - ourSize.y - m_padding.z; // break; // case GUIHelpers::eAlign::LEFT: // ourSet.x = prtPos.x + m_padding.x; // ourSet.y = prtCenter.y - ourCenter.y; // break; // } this->setPosition(ourSet); } /*virtual*/ const GUIHelpers::RGBA cWindow::getDebugColour() const { return GUIHelpers::WHITE; } std::vector& cWindow::getChildren() { return m_children; } //private void cWindow::AddSize(int x, int y, GUIHelpers::Size& mySize) const { int& addFrom = x; int& largestFrom = y; int& addTo = mySize.x; int& largestTo = mySize.y; if (m_orientation == GUIHelpers::eOrientation::HORIZONTAL) { addFrom = y; largestFrom = x; addTo = mySize.y; largestTo = mySize.x; } /*We do the real work here.*/ if (largestTo > largestFrom) largestTo = largestFrom; addTo += addFrom; } void cWindow::DebugDisplay() const { if (sDebug > 0) { SDL_Rect rect; rect.x = m_pos.x; rect.y = m_pos.y; rect.w = m_pos.x + m_size.x; rect.h = m_pos.y + m_size.y; SDL_Rect strPos = rect; strPos.x += (m_size.x / 2); strPos.y += (m_size.y / 2); GUIHelpers::RGBA colour = getDebugColour(); FXEngine::cGFX::Inst().Pixel(strPos.x, strPos.y, colour); cString str = ""; str.format("X: %d Y: %d", strPos.x, strPos.y); FXEngine::cGFX::Inst().StringDefault(str, { strPos.x + 2, strPos.y + 2, 0, 0 }, colour); if (sDebug > 1) { SDL_Rect typePos = { 2, 2, 0, 0 }; cString type; switch (getType()) { case GUIHelpers::eType::CLAYOUT: type = "Layout"; break; case GUIHelpers::eType::CLABEL: typePos = { -10, -10, 0, 0 }; type = "Label"; break; } typePos.x += m_pos.x; typePos.y += m_pos.y; FXEngine::cGFX::Inst().StringDefault(type, typePos, colour); } FXEngine::cGFX::Inst().Rectangle(rect, colour); } }