Text alignment and Text Button

This commit is contained in:
Jack Andersen 2015-12-13 11:00:30 -10:00
parent 72bd17890d
commit 2efa64b7b0
8 changed files with 278 additions and 94 deletions

View File

@ -9,16 +9,26 @@ namespace Specter
class Button : public Control
{
public:
enum class Style
{
Block,
Text,
};
private:
Style m_style;
Zeus::CColor m_textColor;
std::string m_textStr;
std::unique_ptr<TextView> m_text;
SolidShaderVert m_verts[28];
ViewBlock m_bBlock;
boo::IGraphicsBufferD* m_bBlockBuf;
boo::IGraphicsBufferD* m_bBlockBuf = nullptr;
boo::IGraphicsBufferD* m_bVertsBuf;
boo::IVertexFormat* m_bVtxFmt; /* OpenGL only */
boo::IShaderDataBinding* m_bShaderBinding;
boo::IGraphicsBufferD* m_bVertsBuf = nullptr;
boo::IVertexFormat* m_bVtxFmt = nullptr; /* OpenGL only */
boo::IShaderDataBinding* m_bShaderBinding = nullptr;
int m_nomWidth, m_nomHeight;
bool m_pressed = false;
@ -28,6 +38,7 @@ class Button : public Control
void setHover();
void setPressed();
void setDisabled();
public:
class Resources
{
@ -38,7 +49,11 @@ public:
};
Button(ViewResources& res, View& parentView,
IButtonBinding* controlBinding, const std::string& text);
IButtonBinding* controlBinding, const std::string& text,
Style style=Style::Block);
Button(ViewResources& res, View& parentView,
IButtonBinding* controlBinding, const std::string& text,
const Zeus::CColor& textColor, Style style=Style::Block);
void mouseDown(const boo::SWindowCoord&, boo::EMouseButton, boo::EModifierKey);
void mouseUp(const boo::SWindowCoord&, boo::EMouseButton, boo::EModifierKey);
void mouseEnter(const boo::SWindowCoord&);
@ -46,7 +61,9 @@ public:
void resized(const boo::SWindowRect& rootView, const boo::SWindowRect& sub);
void draw(boo::IGraphicsCommandQueue* gfxQ);
void setText(const std::string& text, const Zeus::CColor& textColor);
void setText(const std::string& text);
void colorGlyphs(const Zeus::CColor& newColor);
int nominalWidth() const {return m_nomWidth;}
int nominalHeight() const {return m_nomHeight;}
};

View File

@ -9,8 +9,8 @@ namespace Specter
struct IControlBinding
{
virtual const std::string& name() const=0;
virtual const std::string& help() const=0;
virtual const char* name() const=0;
virtual const char* help() const=0;
};
struct IButtonBinding : IControlBinding
@ -37,8 +37,8 @@ struct CVarControlBinding : IControlBinding
HECL::CVar* m_cvar;
CVarControlBinding(HECL::CVar* cvar)
: m_cvar(cvar) {}
const std::string& name() const {return m_cvar->name();}
const std::string& help() const {return m_cvar->rawHelp();}
const char* name() const {return m_cvar->name().c_str();}
const char* help() const {return m_cvar->rawHelp().c_str();}
};
class Control : public View

View File

@ -13,14 +13,20 @@ class MultiLineTextView : public View
ViewResources& m_viewSystem;
std::vector<std::unique_ptr<TextView>> m_lines;
const FontAtlas& m_fontAtlas;
TextView::Alignment m_align;
size_t m_lineCapacity;
float m_lineHeight;
int m_width;
std::string LineWrap(const std::string& str, int wrap);
std::wstring LineWrap(const std::wstring& str, int wrap);
public:
MultiLineTextView(ViewResources& res, View& parentView, const FontAtlas& font, size_t lineCapacity=256, float lineHeight=1.0);
MultiLineTextView(ViewResources& res, View& parentView, FontTag font, size_t lineCapacity=256, float lineHeight=1.0);
MultiLineTextView(ViewResources& res, View& parentView, const FontAtlas& font,
TextView::Alignment align=TextView::Alignment::Left,
size_t lineCapacity=256, float lineHeight=1.0);
MultiLineTextView(ViewResources& res, View& parentView, FontTag font,
TextView::Alignment align=TextView::Alignment::Left,
size_t lineCapacity=256, float lineHeight=1.0);
void typesetGlyphs(const std::string& str,
const Zeus::CColor& defaultColor=Zeus::CColor::skWhite,
@ -29,6 +35,8 @@ public:
const Zeus::CColor& defaultColor=Zeus::CColor::skWhite,
unsigned wrap=0);
void colorGlyphs(const Zeus::CColor& newColor);
void resized(const boo::SWindowRect& root, const boo::SWindowRect& sub);
void draw(boo::IGraphicsCommandQueue* gfxQ);

View File

@ -14,11 +14,21 @@ class ViewResources;
class TextView : public View
{
public:
enum class Alignment
{
Left,
Center,
Right
};
private:
size_t m_capacity;
boo::IGraphicsBufferD* m_glyphBuf;
boo::IVertexFormat* m_vtxFmt = nullptr; /* OpenGL only */
boo::IShaderDataBinding* m_shaderBinding;
const FontAtlas& m_fontAtlas;
Alignment m_align;
bool m_valid = false;
int m_width = 0;
@ -44,8 +54,8 @@ public:
#endif
};
TextView(ViewResources& res, View& parentView, const FontAtlas& font, size_t capacity=256);
TextView(ViewResources& res, View& parentView, FontTag font, size_t capacity=256);
TextView(ViewResources& res, View& parentView, const FontAtlas& font, Alignment align=Alignment::Left, size_t capacity=256);
TextView(ViewResources& res, View& parentView, FontTag font, Alignment align=Alignment::Left, size_t capacity=256);
struct RenderGlyph
{

View File

@ -12,8 +12,14 @@ void Button::Resources::init(boo::IGraphicsDataFactory* factory, const ThemeData
}
Button::Button(ViewResources& res, View& parentView,
IButtonBinding* controlBinding, const std::string& text)
: Control(res, parentView, controlBinding), m_textStr(text)
IButtonBinding* controlBinding, const std::string& text,
Style style)
: Button(res, parentView, controlBinding, text, res.themeData().uiText(), style) {}
Button::Button(ViewResources& res, View& parentView,
IButtonBinding* controlBinding, const std::string& text,
const Zeus::CColor& textColor, Style style)
: Control(res, parentView, controlBinding), m_style(style), m_textColor(textColor), m_textStr(text)
{
m_bBlockBuf = res.m_factory->newDynamicBuffer(boo::BufferUse::Uniform, sizeof(ViewBlock), 1);
m_bVertsBuf = res.m_factory->newDynamicBuffer(boo::BufferUse::Vertex, sizeof(SolidShaderVert), 28);
@ -39,11 +45,10 @@ Button::Button(ViewResources& res, View& parentView,
m_bVertsBuf, nullptr,
nullptr, 1, bufs, 0, nullptr);
}
commitResources(res);
m_text.reset(new TextView(res, *this, res.m_mainFont));
setText(m_textStr);
if (style == Style::Block)
{
m_verts[0].m_color = res.themeData().button1Inactive();
m_verts[1].m_color = res.themeData().button2Inactive();
m_verts[2].m_color = res.themeData().button1Inactive();
@ -53,15 +58,35 @@ Button::Button(ViewResources& res, View& parentView,
m_verts[i].m_color = res.themeData().button2Inactive();
m_bVertsBuf->load(m_verts, sizeof(SolidShaderVert) * 28);
}
else
{
for (int i=0 ; i<4 ; ++i)
m_verts[i].m_color = Zeus::CColor::skClear;
m_bVertsBuf->load(m_verts, sizeof(SolidShaderVert) * 4);
}
m_text.reset(new TextView(res, *this, res.m_mainFont));
setText(m_textStr);
}
void Button::setText(const std::string &text)
{
m_textStr = text;
setText(text, m_textColor);
}
m_text->typesetGlyphs(text, rootView().themeData().uiText());
void Button::setText(const std::string& text, const Zeus::CColor& textColor)
{
m_textStr = text;
m_textColor = textColor;
m_text->typesetGlyphs(text, textColor);
float pf = rootView().viewRes().pixelFactor();
float width = m_text->nominalWidth() + 10 * pf;
float height = 20 * pf;
float width, height;
if (m_style == Style::Block)
{
width = m_text->nominalWidth() + 12 * pf;
height = 20 * pf;
m_verts[0].m_pos.assign(1, height+1, 0);
m_verts[1].m_pos.assign(1, 1, 0);
m_verts[2].m_pos.assign(width+1, height+1, 0);
@ -96,11 +121,32 @@ void Button::setText(const std::string& text)
m_verts[27].m_pos.assign(width+1, 0, 0);
m_bVertsBuf->load(m_verts, sizeof(SolidShaderVert) * 28);
m_nomWidth = width + 2;
}
else
{
width = m_text->nominalWidth();
height = 10 * pf;
m_verts[0].m_pos.assign(1*pf, -1*pf, 0);
m_verts[1].m_pos.assign(1*pf, -2*pf, 0);
m_verts[2].m_pos.assign(width, -1*pf, 0);
m_verts[3].m_pos.assign(width, -2*pf, 0);
m_bVertsBuf->load(m_verts, sizeof(SolidShaderVert) * 4);
}
m_nomWidth = width;
m_nomHeight = height;
}
void Button::colorGlyphs(const Zeus::CColor& newColor)
{
m_textColor = newColor;
m_text->colorGlyphs(newColor);
}
void Button::setInactive()
{
if (m_style == Style::Block)
{
m_verts[0].m_color = rootView().themeData().button1Inactive();
m_verts[1].m_color = rootView().themeData().button2Inactive();
@ -109,8 +155,18 @@ void Button::setInactive()
m_verts[4].m_color = rootView().themeData().button2Inactive();
m_bVertsBuf->load(m_verts, sizeof(SolidShaderVert) * 28);
}
else
{
for (int i=0 ; i<4 ; ++i)
m_verts[i].m_color = Zeus::CColor::skClear;
m_bVertsBuf->load(m_verts, sizeof(SolidShaderVert) * 4);
m_text->colorGlyphs(m_textColor);
}
}
void Button::setHover()
{
if (m_style == Style::Block)
{
m_verts[0].m_color = rootView().themeData().button1Hover();
m_verts[1].m_color = rootView().themeData().button2Hover();
@ -119,8 +175,18 @@ void Button::setHover()
m_verts[4].m_color = rootView().themeData().button2Hover();
m_bVertsBuf->load(m_verts, sizeof(SolidShaderVert) * 28);
}
else
{
for (int i=0 ; i<4 ; ++i)
m_verts[i].m_color = m_textColor;
m_bVertsBuf->load(m_verts, sizeof(SolidShaderVert) * 4);
m_text->colorGlyphs(m_textColor);
}
}
void Button::setPressed()
{
if (m_style == Style::Block)
{
m_verts[0].m_color = rootView().themeData().button1Press();
m_verts[1].m_color = rootView().themeData().button2Press();
@ -129,8 +195,18 @@ void Button::setPressed()
m_verts[4].m_color = rootView().themeData().button2Press();
m_bVertsBuf->load(m_verts, sizeof(SolidShaderVert) * 28);
}
else
{
for (int i=0 ; i<4 ; ++i)
m_verts[i].m_color = m_textColor;
m_bVertsBuf->load(m_verts, sizeof(SolidShaderVert) * 4);
m_text->colorGlyphs(m_textColor);
}
}
void Button::setDisabled()
{
if (m_style == Style::Block)
{
m_verts[0].m_color = rootView().themeData().button1Disabled();
m_verts[1].m_color = rootView().themeData().button2Disabled();
@ -139,6 +215,16 @@ void Button::setDisabled()
m_verts[4].m_color = rootView().themeData().button2Disabled();
m_bVertsBuf->load(m_verts, sizeof(SolidShaderVert) * 28);
}
else
{
for (int i=0 ; i<4 ; ++i)
m_verts[i].m_color = Zeus::CColor::skClear;
m_bVertsBuf->load(m_verts, sizeof(SolidShaderVert) * 4);
Zeus::CColor dimText = m_textColor;
dimText[3] *= 0.5;
m_text->colorGlyphs(dimText);
}
}
void Button::mouseDown(const boo::SWindowCoord& coord, boo::EMouseButton button, boo::EModifierKey mod)
{
@ -184,11 +270,14 @@ void Button::resized(const boo::SWindowRect& root, const boo::SWindowRect& sub)
{
View::resized(root, sub);
boo::SWindowRect textRect = sub;
float pf = rootView().viewRes().pixelFactor();
m_bBlock.setViewRect(root, sub);
m_bBlockBuf->load(&m_bBlock, sizeof(ViewBlock));
float pf = rootView().viewRes().pixelFactor();
if (m_style == Style::Block)
{
textRect.location[0] += 5 * pf;
textRect.location[1] += 8 * pf;
}
m_text->resized(root, textRect);
}
@ -197,7 +286,10 @@ void Button::draw(boo::IGraphicsCommandQueue* gfxQ)
View::draw(gfxQ);
gfxQ->setShaderDataBinding(m_bShaderBinding);
gfxQ->setDrawPrimitive(boo::Primitive::TriStrips);
if (m_style == Style::Block)
gfxQ->draw(0, 28);
else
gfxQ->draw(0, 4);
m_text->draw(gfxQ);
}

View File

@ -129,11 +129,13 @@ std::wstring MultiLineTextView::LineWrap(const std::wstring& str, int wrap)
MultiLineTextView::MultiLineTextView(ViewResources& res,
View& parentView,
const FontAtlas& font,
TextView::Alignment align,
size_t lineCapacity,
float lineHeight)
: View(res, parentView),
m_viewSystem(res),
m_fontAtlas(font),
m_align(align),
m_lineCapacity(lineCapacity),
m_lineHeight(lineHeight)
{
@ -143,11 +145,13 @@ MultiLineTextView::MultiLineTextView(ViewResources& res,
MultiLineTextView::MultiLineTextView(ViewResources& res,
View& parentView,
FontTag font,
TextView::Alignment align,
size_t lineCapacity,
float lineHeight)
: MultiLineTextView(res,
parentView,
res.m_textRes.m_fcache->lookupAtlas(font),
align,
lineCapacity,
lineHeight) {}
@ -190,7 +194,7 @@ void MultiLineTextView::typesetGlyphs(const std::string& str,
utf8proc_ssize_t sz = utf8proc_iterate(it, -1, &ch);
if (ch == '\n' || ch == '\0')
{
m_lines.emplace_back(new TextView(m_viewSystem, *this, m_fontAtlas, m_lineCapacity));
m_lines.emplace_back(new TextView(m_viewSystem, *this, m_fontAtlas, m_align, m_lineCapacity));
m_lines.back()->typesetGlyphs(std::string((char*)beginIt, it - beginIt), defaultColor);
m_width = std::max(m_width, m_lines.back()->nominalWidth());
beginIt = it + 1;
@ -235,7 +239,7 @@ void MultiLineTextView::typesetGlyphs(const std::wstring& str,
{
if (*it == L'\n' || *it == L'\0')
{
m_lines.emplace_back(new TextView(m_viewSystem, *this, m_fontAtlas, m_lineCapacity));
m_lines.emplace_back(new TextView(m_viewSystem, *this, m_fontAtlas, m_align, m_lineCapacity));
m_lines.back()->typesetGlyphs(std::wstring(beginIt, it), defaultColor);
m_width = std::max(m_width, m_lines.back()->nominalWidth());
beginIt = it + 1;
@ -247,6 +251,12 @@ void MultiLineTextView::typesetGlyphs(const std::wstring& str,
updateSize();
}
void MultiLineTextView::colorGlyphs(const Zeus::CColor& newColor)
{
for (std::unique_ptr<TextView>& tv : m_lines)
tv->colorGlyphs(newColor);
}
void MultiLineTextView::resized(const boo::SWindowRect& root, const boo::SWindowRect& sub)
{
View::resized(root, sub);

View File

@ -259,10 +259,11 @@ void TextView::Resources::init(boo::MetalDataFactory* factory, FontCache* fcache
#endif
TextView::TextView(ViewResources& res, View& parentView, const FontAtlas& font, size_t capacity)
TextView::TextView(ViewResources& res, View& parentView, const FontAtlas& font, Alignment align, size_t capacity)
: View(res, parentView),
m_capacity(capacity),
m_fontAtlas(font)
m_fontAtlas(font),
m_align(align)
{
m_glyphBuf =
res.m_factory->newDynamicBuffer(boo::BufferUse::Vertex,
@ -312,8 +313,8 @@ TextView::TextView(ViewResources& res, View& parentView, const FontAtlas& font,
commitResources(res);
}
TextView::TextView(ViewResources& res, View& parentView, FontTag font, size_t capacity)
: TextView(res, parentView, res.m_textRes.m_fcache->lookupAtlas(font), capacity) {}
TextView::TextView(ViewResources& res, View& parentView, FontTag font, Alignment align, size_t capacity)
: TextView(res, parentView, res.m_textRes.m_fcache->lookupAtlas(font), align, capacity) {}
TextView::RenderGlyph::RenderGlyph(int& adv, const FontAtlas::Glyph& glyph, const Zeus::CColor& defaultColor)
{
@ -386,6 +387,29 @@ void TextView::typesetGlyphs(const std::string& str, const Zeus::CColor& default
break;
}
if (m_align == Alignment::Right)
{
int adj = -adv;
for (RenderGlyph& g : m_glyphs)
{
g.m_pos[0][0] += adj;
g.m_pos[1][0] += adj;
g.m_pos[2][0] += adj;
g.m_pos[3][0] += adj;
}
}
else if (m_align == Alignment::Center)
{
int adj = -adv / 2;
for (RenderGlyph& g : m_glyphs)
{
g.m_pos[0][0] += adj;
g.m_pos[1][0] += adj;
g.m_pos[2][0] += adj;
g.m_pos[3][0] += adj;
}
}
m_width = adv;
m_valid = false;
updateSize();
@ -420,6 +444,29 @@ void TextView::typesetGlyphs(const std::wstring& str, const Zeus::CColor& defaul
break;
}
if (m_align == Alignment::Right)
{
int adj = -adv;
for (RenderGlyph& g : m_glyphs)
{
g.m_pos[0][0] += adj;
g.m_pos[1][0] += adj;
g.m_pos[2][0] += adj;
g.m_pos[3][0] += adj;
}
}
else if (m_align == Alignment::Center)
{
int adj = -adv / 2;
for (RenderGlyph& g : m_glyphs)
{
g.m_pos[0][0] += adj;
g.m_pos[1][0] += adj;
g.m_pos[2][0] += adj;
g.m_pos[3][0] += adj;
}
}
m_width = adv;
m_valid = false;
updateSize();

View File

@ -44,8 +44,8 @@ Tooltip::Tooltip(ViewResources& res, View& parentView, const std::string& title,
for (int i=0 ; i<4 ; ++i)
{
m_cornersOutline[i].reset(new TextView(res, *this, res.m_curveFont, 1));
m_cornersFilled[i].reset(new TextView(res, *this, res.m_curveFont, 1));
m_cornersOutline[i].reset(new TextView(res, *this, res.m_curveFont, TextView::Alignment::Left, 1));
m_cornersFilled[i].reset(new TextView(res, *this, res.m_curveFont, TextView::Alignment::Left, 1));
}
m_cornersOutline[0]->typesetGlyphs(L"\xF4F0");
m_cornersFilled[0]->typesetGlyphs(L"\xF4F1", res.themeData().tooltipBackground());