Files
SDLPongCPP/.svn/pristine/fa/faacf7bc09ac0b3c0ecd857c60c98bfc0d7320ef.svn-base
T
2018-06-25 21:48:45 -04:00

322 lines
7.5 KiB
Plaintext

#include "cWindow.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::RBGA 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;
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;
m_padding = padding;
m_name = name;
}
/*virtual*/ void cWindow::Display() {
std::vector<cWindow*>::iterator it;
for (it = m_children.begin(); it < m_children.end(); it++) {
(*it)->Display();
}
}
///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<cWindow*>::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*/ )
{
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<cWindow*>::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<cWindow*>::iterator it;
for (it = m_children.begin(); it < m_children.end(); it++) {
(*it)->RePos();
}
}
/*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 };
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);
}
std::vector<cWindow*>& 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;
}