make styles closer to CSS syntax

This commit is contained in:
Eduardo Bart
2011-11-17 19:41:02 -02:00
parent 55136fe866
commit 5c654f685c
35 changed files with 290 additions and 269 deletions

View File

@@ -29,10 +29,10 @@
void Font::load(const OTMLNodePtr& fontNode)
{
std::string textureName = fontNode->valueAt("texture");
Size glyphSize = fontNode->valueAt<Size>("glyph size");
Size glyphSize = fontNode->valueAt<Size>("glyph-size");
m_glyphHeight = fontNode->valueAt<int>("height");
m_topMargin = fontNode->valueAt("top margin", 0);
m_firstGlyph = fontNode->valueAt("first glyph", 32);
m_yOffset = fontNode->valueAt("y-offset", 0);
m_firstGlyph = fontNode->valueAt("first-glyph", 32);
m_glyphSpacing = fontNode->valueAt("spacing", Size(0,0));
// load font texture
@@ -40,14 +40,14 @@ void Font::load(const OTMLNodePtr& fontNode)
if(!m_texture)
throw std::runtime_error("failed to load texture for font");
if(OTMLNodePtr node = fontNode->get("fixed glyph width")) {
if(OTMLNodePtr node = fontNode->get("fixed-glyph-width")) {
for(int glyph = m_firstGlyph; glyph < 256; ++glyph)
m_glyphsSize[glyph] = Size(node->value<int>(), m_glyphHeight);
} else
calculateGlyphsWidthsAutomatically(glyphSize);
// read custom widths
if(OTMLNodePtr node = fontNode->get("glyph widths")) {
if(OTMLNodePtr node = fontNode->get("glyph-widths")) {
for(const OTMLNodePtr& child : node->children())
m_glyphsSize[Fw::safeCast<int>(child->tag())].setWidth(child->value<int>());
}
@@ -200,7 +200,7 @@ const std::vector<Point>& Font::calculateGlyphsPositions(const std::string& text
}
}
Point virtualPos(0, m_topMargin);
Point virtualPos(0, m_yOffset);
lines = 0;
for(i = 0; i < textLength; ++i) {
glyph = (uchar)text[i];

View File

@@ -59,7 +59,7 @@ public:
const Rect* getGlyphsTextureCoords() const { return m_glyphsTextureCoords; }
const Size* getGlyphsSize() const { return m_glyphsSize; }
const TexturePtr& getTexture() const { return m_texture; }
int getTopMargin() const { return m_topMargin; }
int getYOffset() const { return m_yOffset; }
Size getGlyphSpacing() const { return m_glyphSpacing; }
private:
@@ -69,7 +69,7 @@ private:
std::string m_name;
int m_glyphHeight;
int m_firstGlyph;
int m_topMargin;
int m_yOffset;
Size m_glyphSpacing;
TexturePtr m_texture;
Rect m_glyphsTextureCoords[256];

View File

@@ -29,15 +29,15 @@ UILabel::UILabel()
{
m_focusable = false;
m_phantom = true;
m_align = Fw::AlignLeft;
m_textAlign = Fw::AlignLeft;
}
void UILabel::render()
{
UIWidget::render();
Rect textRect = m_rect;
textRect.setTopLeft(textRect.topLeft() + m_offset);
m_font->renderText(m_text, textRect, m_align, m_foregroundColor);
textRect.setTopLeft(textRect.topLeft() + m_textOffset);
m_font->renderText(m_text, textRect, m_textAlign, m_foregroundColor);
}
void UILabel::setText(const std::string& text)
@@ -66,10 +66,10 @@ void UILabel::onStyleApply(const OTMLNodePtr& styleNode)
for(const OTMLNodePtr& node : styleNode->children()) {
if(node->tag() == "text")
setText(node->value());
else if(node->tag() == "align")
setAlign(Fw::translateAlignment(node->value()));
else if(node->tag() == "offset") {
setOffset(node->value<Point>());
else if(node->tag() == "text-align")
setTextAlign(Fw::translateAlignment(node->value()));
else if(node->tag() == "text-offset") {
setTextOffset(node->value<Point>());
}
}
}

View File

@@ -34,20 +34,20 @@ public:
void resizeToText();
void setText(const std::string& text);
void setAlign(Fw::AlignmentFlag align) { m_align = align; }
void setOffset(const Point& offset) { m_offset = offset; }
void setTextAlign(Fw::AlignmentFlag align) { m_textAlign = align; }
void setTextOffset(const Point& offset) { m_textOffset = offset; }
std::string getText() const { return m_text; }
Fw::AlignmentFlag getAlign() const { return m_align; }
Point getOffset() const { return m_offset; }
Fw::AlignmentFlag getTextAlign() const { return m_textAlign; }
Point getTextOffset() const { return m_textOffset; }
protected:
virtual void onStyleApply(const OTMLNodePtr& styleNode);
private:
std::string m_text;
Point m_offset;
Fw::AlignmentFlag m_align;
Point m_textOffset;
Fw::AlignmentFlag m_textAlign;
};
#endif

View File

@@ -33,7 +33,7 @@ UILineEdit::UILineEdit()
m_startRenderPos = 0;
m_textHorizontalMargin = 0;
m_textHidden = false;
m_alwaysFocused = false;
m_alwaysActive = false;
blinkCursor();
}
@@ -51,7 +51,7 @@ void UILineEdit::render()
g_graphics.drawTexturedRect(m_glyphsCoords[i], texture, m_glyphsTexCoords[i]);
// render cursor
if(isExplicitlyEnabled() && (isActive() || m_alwaysFocused) && m_cursorPos >= 0) {
if(isExplicitlyEnabled() && (isActive() || m_alwaysActive) && m_cursorPos >= 0) {
assert(m_cursorPos <= textLength);
// draw every 333ms
const int delay = 333;
@@ -98,7 +98,7 @@ void UILineEdit::update()
if(m_cursorPos < m_startRenderPos) // cursor is before the previuos first rendered glyph, so we need to update
{
m_startInternalPos.x = glyphsPositions[m_cursorPos].x;
m_startInternalPos.y = glyphsPositions[m_cursorPos].y - m_font->getTopMargin();
m_startInternalPos.y = glyphsPositions[m_cursorPos].y - m_font->getYOffset();
m_startRenderPos = m_cursorPos;
} else if(m_cursorPos > m_startRenderPos || // cursor is after the previuos first rendered glyph
(m_cursorPos == m_startRenderPos && textLength == m_cursorPos)) // cursor is at the previuos rendered element, and is the last text element
@@ -119,13 +119,13 @@ void UILineEdit::update()
for(pos = 0; pos < textLength; ++pos) {
glyph = (uchar)text[pos];
glyphRect = Rect(glyphsPositions[pos], glyphsSize[glyph]);
glyphRect.setTop(std::max(glyphRect.top() - m_font->getTopMargin() - m_font->getGlyphSpacing().height(), 0));
glyphRect.setTop(std::max(glyphRect.top() - m_font->getYOffset() - m_font->getGlyphSpacing().height(), 0));
glyphRect.setLeft(std::max(glyphRect.left() - m_font->getGlyphSpacing().width(), 0));
// first glyph entirely visible found
if(glyphRect.topLeft() >= startGlyphPos) {
m_startInternalPos.x = glyphsPositions[pos].x;
m_startInternalPos.y = glyphsPositions[pos].y - m_font->getTopMargin();
m_startInternalPos.y = glyphsPositions[pos].y - m_font->getYOffset();
m_startRenderPos = pos;
break;
}
@@ -351,7 +351,7 @@ int UILineEdit::getTextPos(Point pos)
int candidatePos = -1;
for(int i=0;i<textLength;++i) {
Rect clickGlyphRect = m_glyphsCoords[i];
clickGlyphRect.addTop(m_font->getTopMargin() + m_font->getGlyphSpacing().height());
clickGlyphRect.addTop(m_font->getYOffset() + m_font->getGlyphSpacing().height());
clickGlyphRect.addLeft(m_font->getGlyphSpacing().width()+1);
if(clickGlyphRect.contains(pos))
return i;
@@ -381,12 +381,12 @@ void UILineEdit::onStyleApply(const OTMLNodePtr& styleNode)
if(node->tag() == "text") {
setText(node->value());
setCursorPos(m_text.length());
} else if(node->tag() == "text hidden") {
} else if(node->tag() == "text-hidden") {
setTextHidden(node->value<bool>());
} else if(node->tag() == "text margin") {
} else if(node->tag() == "text-margin") {
m_textHorizontalMargin = node->value<int>();
} else if(node->tag() == "always focused") {
m_alwaysFocused = true;
} else if(node->tag() == "always-active") {
m_alwaysActive = true;
}
}
}
@@ -398,7 +398,7 @@ void UILineEdit::onGeometryUpdate(const Rect& oldRect, const Rect& newRect)
void UILineEdit::onFocusChange(bool focused, Fw::FocusReason reason)
{
if(focused && !m_alwaysFocused) {
if(focused && !m_alwaysActive) {
if(reason == Fw::TabFocusReason)
setCursorPos(m_text.length());
else
@@ -423,7 +423,7 @@ bool UILineEdit::onKeyPress(uchar keyCode, char keyChar, int keyboardModifiers)
else if(keyCode == Fw::KeyV && keyboardModifiers == Fw::KeyboardCtrlModifier)
appendText(g_platform.getClipboardText());
else if(keyCode == Fw::KeyTab) {
if(!m_alwaysFocused) {
if(!m_alwaysActive) {
if(UIWidgetPtr parent = getParent())
parent->focusNextChild(Fw::TabFocusReason);
}

View File

@@ -71,7 +71,7 @@ private:
int m_cursorTicks;
int m_textHorizontalMargin;
bool m_textHidden;
bool m_alwaysFocused;
bool m_alwaysActive;
std::vector<Rect> m_glyphsCoords;
std::vector<Rect> m_glyphsTexCoords;

View File

@@ -36,7 +36,7 @@ void UIVerticalLayout::applyStyle(const OTMLNodePtr& styleNode)
UILayout::applyStyle(styleNode);
for(const OTMLNodePtr& node : styleNode->children()) {
if(node->tag() == "align bottom")
if(node->tag() == "align-bottom")
m_alignBottom = node->value<bool>();
else if(node->tag() == "padding")
m_padding = node->value<int>();

View File

@@ -40,7 +40,7 @@ UIWidget::UIWidget()
m_states = Fw::DefaultState;
m_font = g_fonts.getDefaultFont();
m_opacity = 255;
m_marginTop = m_marginBottom = m_marginLeft = m_marginRight = 0;
m_marginTop = m_marginRight = m_marginBottom = m_marginLeft = 0;
// generate an unique id, this is need because anchored layouts find widgets by id
static unsigned long id = 1;
@@ -817,7 +817,7 @@ void UIWidget::onStyleApply(const OTMLNodePtr& styleNode)
setWidth(node->value<int>());
else if(node->tag() == "height")
setHeight(node->value<int>());
else if(node->tag() == "size fixed")
else if(node->tag() == "fixed-size")
setSizeFixed(node->value<bool>());
else if(node->tag() == "position")
moveTo(node->value<Point>());
@@ -825,14 +825,46 @@ void UIWidget::onStyleApply(const OTMLNodePtr& styleNode)
setX(node->value<int>());
else if(node->tag() == "y")
setY(node->value<int>());
else if(node->tag() == "margin.left")
setMarginLeft(node->value<int>());
else if(node->tag() == "margin.right")
setMarginRight(node->value<int>());
else if(node->tag() == "margin.top")
else if(node->tag() == "margin-top")
setMarginTop(node->value<int>());
else if(node->tag() == "margin.bottom")
else if(node->tag() == "margin-right")
setMarginRight(node->value<int>());
else if(node->tag() == "margin-bottom")
setMarginBottom(node->value<int>());
else if(node->tag() == "margin-left")
setMarginLeft(node->value<int>());
else if(node->tag() == "margin") {
std::string marginDesc = node->value();
std::vector<std::string> split;
boost::split(split, marginDesc, boost::is_any_of(std::string(" ")));
if(split.size() == 4) {
setMarginTop(Fw::safeCast<int>(split[0]));
setMarginRight(Fw::safeCast<int>(split[1]));
setMarginBottom(Fw::safeCast<int>(split[2]));
setMarginLeft(Fw::safeCast<int>(split[3]));
} else if(split.size() == 3) {
int marginTop = Fw::safeCast<int>(split[0]);
int marginHorizontal = Fw::safeCast<int>(split[1]);
int marginBottom = Fw::safeCast<int>(split[2]);
setMarginTop(marginTop);
setMarginRight(marginHorizontal);
setMarginBottom(marginBottom);
setMarginLeft(marginHorizontal);
} else if(split.size() == 2) {
int marginVertical = Fw::safeCast<int>(split[0]);
int marginHorizontal = Fw::safeCast<int>(split[1]);
setMarginTop(marginVertical);
setMarginRight(marginHorizontal);
setMarginBottom(marginVertical);
setMarginLeft(marginHorizontal);
} else if(split.size() == 1) {
int margin = Fw::safeCast<int>(split[0]);
setMarginTop(margin);
setMarginRight(margin);
setMarginBottom(margin);
setMarginLeft(margin);
}
}
// layouts
else if(node->tag() == "layout") {
// layout is set only once

View File

@@ -62,10 +62,10 @@ public:
void setOpacity(int opacity) { m_opacity = opacity; }
void setBackgroundColor(const Color& color) { m_backgroundColor = color; }
void setForegroundColor(const Color& color) { m_foregroundColor = color; }
void setMarginLeft(int margin) { m_marginLeft = margin; updateParentLayout(); }
void setMarginRight(int margin) { m_marginRight = margin; updateParentLayout(); }
void setMarginTop(int margin) { m_marginTop = margin; updateParentLayout(); }
void setMarginRight(int margin) { m_marginRight = margin; updateParentLayout(); }
void setMarginBottom(int margin) { m_marginBottom = margin; updateParentLayout(); }
void setMarginLeft(int margin) { m_marginLeft = margin; updateParentLayout(); }
void setSizeFixed(bool fixed) { m_fixedSize = fixed; updateParentLayout(); }
void setLastFocusReason(Fw::FocusReason reason) { m_lastFocusReason = reason; }
@@ -112,10 +112,10 @@ public:
Color getForegroundColor() { return m_foregroundColor; }
Color getBackgroundColor() { return m_backgroundColor; }
int getOpacity() { return m_opacity; }
int getMarginLeft() { return m_marginLeft; }
int getMarginRight() { return m_marginRight; }
int getMarginTop() { return m_marginTop; }
int getMarginRight() { return m_marginRight; }
int getMarginBottom() { return m_marginBottom; }
int getMarginLeft() { return m_marginLeft; }
Fw::FocusReason getLastFocusReason() { return m_lastFocusReason; }
OTMLNodePtr getStyle() { return m_style; }
@@ -205,10 +205,10 @@ protected:
Color m_foregroundColor;
int m_states;
int m_opacity;
int m_marginLeft;
int m_marginRight;
int m_marginTop;
int m_marginRight;
int m_marginBottom;
int m_marginLeft;
};
#endif

View File

@@ -41,19 +41,17 @@ void UIWindow::render()
// render children
UIWidget::render();
// draw window head
// draw window head text
Rect headTextRect = m_rect;
headTextRect.addTop(-m_headOffset.y);
headTextRect.addTop(-m_headTextOffset.y);
headTextRect.setHeight(m_headHeight);
if(m_titleAlign & Fw::AlignLeft)
headTextRect.addLeft(-m_headOffset.x);
headTextRect.addLeft(-m_headTextOffset.x);
else if(m_titleAlign & Fw::AlignRight)
headTextRect.addRight(-m_headOffset.x);
headTextRect.addRight(-m_headTextOffset.x);
else {
headTextRect.addLeft(-m_headOffset.x);
headTextRect.addRight(-m_headOffset.x);
headTextRect.addLeft(-m_headTextOffset.x);
headTextRect.addRight(-m_headTextOffset.x);
}
m_font->renderText(m_title, headTextRect, m_titleAlign, m_foregroundColor);
}
@@ -63,15 +61,15 @@ void UIWindow::onStyleApply(const OTMLNodePtr& styleNode)
UIWidget::onStyleApply(styleNode);
for(OTMLNodePtr node : styleNode->children()) {
if(node->tag() == "head height")
if(node->tag() == "head-height")
m_headHeight = node->value<int>();
else if(node->tag() == "head offset")
m_headOffset = node->value<Point>();
else if(node->tag() == "head-text-offset")
m_headTextOffset = node->value<Point>();
else if(node->tag() == "title")
setTitle(node->value());
else if(node->tag() == "head text align")
else if(node->tag() == "head-text-align")
m_titleAlign = Fw::translateAlignment(node->value());
else if(node->tag() == "move policy") {
else if(node->tag() == "move-policy") {
if(node->value() == "free")
m_movePolicy = FREE_MOVE;
else if(node->value() == "free updated")
@@ -79,14 +77,6 @@ void UIWindow::onStyleApply(const OTMLNodePtr& styleNode)
else
m_movePolicy = DONT_MOVE;
}
else if(node->tag() == "onEnter") {
g_lua.loadFunction(node->value(), "@" + node->source() + "[" + node->tag() + "]");
luaSetField(node->tag());
}
else if(node->tag() == "onEscape") {
g_lua.loadFunction(node->value(), "@" + node->source() + "[" + node->tag() + "]");
luaSetField(node->tag());
}
}
}

View File

@@ -53,7 +53,7 @@ private:
bool m_moving;
MovePolicy m_movePolicy;
Fw::AlignmentFlag m_titleAlign;
Point m_headOffset;
Point m_headTextOffset;
Point m_movingReference;
Point m_oldPos;
int m_oldIndex;