Skip to content

Commit

Permalink
#6 Update GUI text alignment
Browse files Browse the repository at this point in the history
  • Loading branch information
ducphamhong committed Dec 13, 2020
1 parent b459bc3 commit e0797b3
Show file tree
Hide file tree
Showing 9 changed files with 135 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ namespace Skylicht

boxLayout = new GUI::CBoxLayout(transformColapsible);
boxLayout->setPadding(GUI::SPadding(5.0, 5.0, 5.0, 5.0));
addText(boxLayout, L"X", L"0.0");
addText(boxLayout, L"Position X", L"0.0");
addText(boxLayout, L"Y", L"0.0");
addText(boxLayout, L"Z", L"0.0");

Expand All @@ -86,7 +86,9 @@ namespace Skylicht
GUI::CLayout* layout = boxLayout->beginVertical();

GUI::CLabel* label = new GUI::CLabel(layout);
label->setPadding(GUI::SMargin(0.0f, 2.0, 0.0f, 0.0f));
label->setString(name);
label->setTextAlignment(GUI::TextRight);

GUI::CTextBox* text = new GUI::CTextBox(layout);
text->setString(value);
Expand Down
14 changes: 4 additions & 10 deletions Projects/Editor/Source/GUI/Controls/CBoxLayout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ namespace Skylicht
layout->setPos(0.0f, 0.0f);
layout->sendToBack();

dock(EPosition::Top);

m_layoutStack.push(layout);
}

Expand All @@ -49,19 +51,11 @@ namespace Skylicht

}

void CBoxLayout::layout()
{
float w = m_parent->width() - m_parent->getPadding().Left - m_parent->getPadding().Right;
setWidth(w);

CBase::layout();

sizeToChildren(false, true);
}

void CBoxLayout::postLayout()
{
CBase::postLayout();

sizeToChildren(false, true);
}

CLayoutHorizontal* CBoxLayout::beginHorizontal()
Expand Down
2 changes: 0 additions & 2 deletions Projects/Editor/Source/GUI/Controls/CBoxLayout.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@ namespace Skylicht

virtual ~CBoxLayout();

virtual void layout();

virtual void postLayout();

CLayoutHorizontal* beginHorizontal();
Expand Down
2 changes: 1 addition & 1 deletion Projects/Editor/Source/GUI/Controls/CLabel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ namespace Skylicht
{
CBase::onBoundsChanged(oldBounds);

sizeToContents();
m_text->sizeToContents();
invalidate();
}

Expand Down
14 changes: 12 additions & 2 deletions Projects/Editor/Source/GUI/Controls/CLabel.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ namespace Skylicht
class CLabel : public CBase
{
protected:
CTextContainer *m_text;
CTextContainer* m_text;

public:
CLabel(CBase *parent);
CLabel(CBase* parent);
virtual ~CLabel();

virtual void sizeToContents();
Expand Down Expand Up @@ -66,6 +66,16 @@ namespace Skylicht
return m_text->getFontSize();
}

inline void setTextAlignment(ETextAlign align)
{
m_text->setTextAlignment(align);
}

inline ETextAlign getTextAlignment()
{
return m_text->getTextAlignment();
}

Listener OnTextChanged;
};
}
Expand Down
20 changes: 12 additions & 8 deletions Projects/Editor/Source/GUI/Controls/CLayoutVertical.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,19 @@ namespace Skylicht

void CLayoutVertical::layout()
{
int numChild = Children.size();
float size = (width() - m_childPadding * (numChild - 1)) / (float)numChild;
float x = 0.0f;
float y = 0.0f;

for (CBase* c : Children)
int numChild = (int)Children.size();
if (numChild > 0)
{
c->setBounds(x, y, size, c->height());
x = x + size + m_childPadding;
float size = (width() - m_childPadding * (numChild - 1)) / (float)numChild;
float x = 0.0f;
float y = 0.0f;

for (CBase* c : Children)
{
float h = core::max_(c->height(), height());
c->setBounds(x, y, size, h);
x = x + size + m_childPadding;
}
}

CLayout::layout();
Expand Down
100 changes: 80 additions & 20 deletions Projects/Editor/Source/GUI/Controls/CTextContainer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ namespace Skylicht
{
namespace GUI
{
CTextContainer::CTextContainer(CBase *parent) :
CTextContainer::CTextContainer(CBase* parent) :
CBase(parent),
m_wrapMultiLine(false),
m_textChange(true),
Expand All @@ -51,7 +51,8 @@ namespace Skylicht
m_caretEndPosition(0),
m_caretBlinkSpeed(500.0f),
m_caretBlink(0.0f),
m_moveCaretToEnd(false)
m_moveCaretToEnd(false),
m_textAlign(ETextAlign::TextLeft)
{
setMouseInputEnabled(false);
enableClip(true);
Expand Down Expand Up @@ -85,7 +86,7 @@ namespace Skylicht
}

u32 lineID = 0;
for (CText *line : m_lines)
for (CText* line : m_lines)
{
// draw selection
if (fromLine != toLine || from != to)
Expand Down Expand Up @@ -136,7 +137,7 @@ namespace Skylicht
}
}

void CTextContainer::drawSelection(CText *line, u32 from, u32 to)
void CTextContainer::drawSelection(CText* line, u32 from, u32 to)
{
SDimension selectBegin = line->getCharacterPosition(from);
SDimension selectEnd = line->getCharacterPosition(to);
Expand Down Expand Up @@ -205,7 +206,7 @@ namespace Skylicht
string = L"";
u32 lineID = 0;

for (CText *line : m_lines)
for (CText* line : m_lines)
{
if (fromLine != toLine || from != to)
{
Expand Down Expand Up @@ -383,7 +384,7 @@ namespace Skylicht
bool foundBegin = false;
bool foundEnd = false;

for (CText *l : m_lines)
for (CText* l : m_lines)
{
if (pos <= m_caretBegin && m_caretBegin <= pos + l->getLengthNoNewLine())
{
Expand Down Expand Up @@ -414,7 +415,7 @@ namespace Skylicht
u32 ret = 0;
u32 lineID = 0;

for (CText *l : m_lines)
for (CText* l : m_lines)
{
if (lineID == line)
{
Expand Down Expand Up @@ -446,10 +447,10 @@ namespace Skylicht
outChar = 0;

u32 lineOffset = 0;
CText *foundLine = NULL;
CText *lastLine = NULL;
CText* foundLine = NULL;
CText* lastLine = NULL;

for (CText *line : m_lines)
for (CText* line : m_lines)
{
lineOffset += line->getLength();
outLine++;
Expand Down Expand Up @@ -513,8 +514,8 @@ namespace Skylicht
from = charPosition;
to = charPosition;

CText *foundLine = NULL;
for (CText *l : m_lines)
CText* foundLine = NULL;
for (CText* l : m_lines)
{
charID += l->getLength();

Expand Down Expand Up @@ -568,8 +569,8 @@ namespace Skylicht
u32 charID = 0;
u32 lineID = 0;

CText *foundLine = NULL;
for (CText *l : m_lines)
CText* foundLine = NULL;
for (CText* l : m_lines)
{
charID += l->getLength();

Expand Down Expand Up @@ -598,6 +599,36 @@ namespace Skylicht
sizeToContents();
m_textChange = false;
}

updateTextAlignment();
}

void CTextContainer::updateTextAlignment()
{
if (m_textAlign == ETextAlign::TextLeft)
return;

float x = 0.0f;
float w = floor(m_parent->width() - m_parent->getPadding().Left - m_parent->getPadding().Right - m_paddingRight);

ListTextControl::iterator i = m_lines.begin(), end = m_lines.end();
while (i != end)
{
CText* line = (*i);

if (m_textAlign == ETextAlign::TextRight)
{
x = w - line->width();
}
else if (m_textAlign == ETextAlign::TextCenter)
{
x = floorf(w * 0.5f - line->width() * 0.5f);
}

line->setPos(x, line->getPos().Y);

++i;
}
}

void CTextContainer::setColor(const SGUIColor& color)
Expand All @@ -617,13 +648,15 @@ namespace Skylicht
float x = 0.0f;
float y = 0.0f;

float lineWidth = 0.0f;

// this is not yet layout, so calc from parent (Label, TextBox,...)
float w = floor(m_parent->width() - m_parent->getPadding().Left - m_parent->getPadding().Right - m_paddingRight);

if (m_wrapMultiLine == true)
{
removeAllLines();

// this is not yet layout, so calc from parent (Label, TextBox,...)
float w = m_parent->width() - m_parent->getPadding().Left - m_parent->getPadding().Right - m_paddingRight;

// multi line
std::vector<std::wstring> words;
std::wstring strLine;
Expand All @@ -647,11 +680,18 @@ namespace Skylicht
strLine += *it;
SDimension p = CRenderer::getRenderer()->measureText(m_fontSize, strLine);

if (lineWidth == 0.0f)
lineWidth = p.Width;

if (p.Width > w)
{
finishLine = true;
wrapped = true;
}
else
{
lineWidth = p.Width;
}

// If this is the last word then finish the line
if (--words.end() == it)
Expand All @@ -671,16 +711,27 @@ namespace Skylicht
}
else
{
t->setString(strLine.substr(0, strLine.length()));
t->setString(strLine);
//new line is empty
strLine.clear();
}
t->setBounds(x, y, w, p.Height);

if (m_textAlign == ETextAlign::TextRight)
{
x = w - lineWidth;
}
else if (m_textAlign == ETextAlign::TextCenter)
{
x = floorf(w * 0.5f - lineWidth * 0.5f);
}

t->setBounds(x, y, lineWidth, p.Height);
m_lines.push_back(t);

// Position the newline
y += p.Height;
x = 0.0f;
lineWidth = 0.0f;
}
}
}
Expand All @@ -694,6 +745,15 @@ namespace Skylicht
// compute size of text
SDimension p = CRenderer::getRenderer()->measureText(m_fontSize, m_string);

if (m_textAlign == ETextAlign::TextRight)
{
x = w - p.Width;
}
else if (m_textAlign == ETextAlign::TextCenter)
{
x = floorf(w * 0.5f - p.Width * 0.5f);
}

// single line
CText* t = new CText(this);
t->setFontSize(m_fontSize);
Expand Down Expand Up @@ -789,7 +849,7 @@ namespace Skylicht
ListTextControl::iterator i = m_lines.begin(), end = m_lines.end();
while (i != end)
{
CText *t = (*i);
CText* t = (*i);
removeChild(t);
delete t;
++i;
Expand Down
Loading

0 comments on commit e0797b3

Please sign in to comment.