fixed ratio image drawing support

This commit is contained in:
Eduardo Bart 2011-10-28 21:56:45 -02:00
parent 6c200bd764
commit f4f0d7e960
10 changed files with 66 additions and 68 deletions

View File

@ -36,8 +36,9 @@ BorderImage::BorderImage(TexturePtr texture,
const Rect& topRight, const Rect& topRight,
const Rect& bottomLeft, const Rect& bottomLeft,
const Rect& bottomRight, const Rect& bottomRight,
const Rect& center) : Image(texture) const Rect& center)
{ {
m_texture = texture;
m_leftBorderTexCoords = left; m_leftBorderTexCoords = left;
m_rightBorderTexCoords = right; m_rightBorderTexCoords = right;
m_topBorderTexCoords = top; m_topBorderTexCoords = top;
@ -102,7 +103,7 @@ BorderImagePtr BorderImage::loadFromOTML(const OTMLNodePtr& borderImageNode)
bottomBorder = Rect(subRect.left() + left, subRect.bottom() - bottom + 1, subRect.width() - right - left, bottom); bottomBorder = Rect(subRect.left() + left, subRect.bottom() - bottom + 1, subRect.width() - right - left, bottom);
topLeftCorner = Rect(subRect.left(), subRect.top(), left, top); topLeftCorner = Rect(subRect.left(), subRect.top(), left, top);
topRightCorner = Rect(subRect.right() - right + 1, subRect.top(), right, top); topRightCorner = Rect(subRect.right() - right + 1, subRect.top(), right, top);
bottomLeftCorner = Rect(subRect.left(), subRect.bottom() - bottom, left, bottom); bottomLeftCorner = Rect(subRect.left(), subRect.bottom() - bottom + 1, left, bottom);
bottomRightCorner = Rect(subRect.right() - right + 1, subRect.bottom() - bottom + 1, right, bottom); bottomRightCorner = Rect(subRect.right() - right + 1, subRect.bottom() - bottom + 1, right, bottom);
center = Rect(subRect.left() + left, subRect.top() + top, subRect.width() - right - left, subRect.height() - top - bottom); center = Rect(subRect.left() + left, subRect.top() + top, subRect.width() - right - left, subRect.height() - top - bottom);
@ -184,7 +185,7 @@ void BorderImage::draw(const Rect& screenCoords)
// bottom left corner // bottom left corner
rectCoords = Rect(screenCoords.left(), rectCoords = Rect(screenCoords.left(),
screenCoords.top() + m_topBorderTexCoords.height() + centerSize.height(), screenCoords.top() + m_topLeftCornerTexCoords.height() + centerSize.height(),
m_bottomLeftCornerTexCoords.size()); m_bottomLeftCornerTexCoords.size());
g_graphics.drawTexturedRect(rectCoords, m_texture, m_bottomLeftCornerTexCoords); g_graphics.drawTexturedRect(rectCoords, m_texture, m_bottomLeftCornerTexCoords);

View File

@ -27,37 +27,45 @@
#include <framework/otml/otml.h> #include <framework/otml/otml.h>
Image::Image(TexturePtr texture, Rect textureCoords) Image::Image()
{ {
m_texture = texture; m_fixedRatio = false;
if(!textureCoords.isValid())
m_textureCoords = Rect(0, 0, m_texture->getSize());
else
m_textureCoords = textureCoords;
} }
ImagePtr Image::loadFromOTML(const OTMLNodePtr& imageNode) void Image::loadFromOTML(const OTMLNodePtr& imageNode)
{ {
// load configs from otml node // load configs from otml node
std::string source = imageNode->hasValue() ? imageNode->value() : imageNode->valueAt("source"); std::string source = imageNode->hasValue() ? imageNode->value() : imageNode->valueAt("source");
bool smooth = imageNode->valueAt("smooth", false); bool smooth = imageNode->valueAt("smooth", false);
Rect textureCoords = imageNode->valueAt("coords", Rect()); m_textureCoords = imageNode->valueAt("coords", Rect());
m_fixedRatio = imageNode->valueAt("fixed ratio", false);
// load texture // load texture
TexturePtr texture = g_textures.getTexture(source); m_texture = g_textures.getTexture(source);
if(!texture) if(!m_texture)
throw OTMLException(imageNode, "could not load image texture"); throw OTMLException(imageNode, "could not load image texture");
// enable texture bilinear filter // enable texture bilinear filter
if(smooth) if(smooth)
texture->enableBilinearFilter(); m_texture->enableBilinearFilter();
// create image
return ImagePtr(new Image(texture, textureCoords));
} }
void Image::draw(const Rect& screenCoords) void Image::draw(const Rect& screenCoords)
{ {
if(m_texture) if(m_texture) {
if(m_fixedRatio) {
const Size& texSize = m_texture->getSize();
Size texCoordsSize = screenCoords.size();
texCoordsSize.scale(texSize, Fw::KeepAspectRatio);
Point texCoordsOffset;
if(texSize.height() > texCoordsSize.height())
texCoordsOffset.y = (texSize.height() - texCoordsSize.height())/2;
else if(texSize.width() > texCoordsSize.width())
texCoordsOffset.x = (texSize.width() - texCoordsSize.width())/2;
g_graphics.drawTexturedRect(screenCoords, m_texture, Rect(texCoordsOffset, texCoordsSize));
} else {
g_graphics.drawTexturedRect(screenCoords, m_texture, m_textureCoords); g_graphics.drawTexturedRect(screenCoords, m_texture, m_textureCoords);
} }
}
}

View File

@ -30,15 +30,16 @@
class Image class Image
{ {
public: public:
Image(TexturePtr texture, Rect textureCoords = Rect()); Image();
static ImagePtr loadFromOTML(const OTMLNodePtr& imageNode); void loadFromOTML(const OTMLNodePtr& imageNode);
virtual void draw(const Rect& screenCoords); virtual void draw(const Rect& screenCoords);
protected: protected:
TexturePtr m_texture; TexturePtr m_texture;
Rect m_textureCoords; Rect m_textureCoords;
bool m_fixedRatio;
}; };
#endif #endif

View File

@ -44,7 +44,7 @@ public:
int getWidth() const { return m_size.width(); } int getWidth() const { return m_size.width(); }
int getHeight() const { return m_size.height(); } int getHeight() const { return m_size.height(); }
const Size getSize() const { return m_size; } const Size& getSize() const { return m_size; }
const Size& getGlSize() const { return m_glSize; } const Size& getGlSize() const { return m_glSize; }
bool isEmpty() const { return m_textureId == 0; } bool isEmpty() const { return m_textureId == 0; }

View File

@ -155,7 +155,7 @@ bool luavalue_cast(int index, std::function<void(Args...)>& func) {
// weak references are used here, this means that the script must hold another reference // weak references are used here, this means that the script must hold another reference
// to this function, otherwise it will expire // to this function, otherwise it will expire
int funcWeakRef = g_lua.weakRef(); int funcWeakRef = g_lua.weakRef();
func = [=](Args... args...) { func = [=](Args... args) {
// note that we must catch exceptions, because this lambda can be called from anywhere // note that we must catch exceptions, because this lambda can be called from anywhere
// and most of them won't catch exceptions (e.g. dispatcher) // and most of them won't catch exceptions (e.g. dispatcher)
g_lua.getWeakRef(funcWeakRef); g_lua.getWeakRef(funcWeakRef);
@ -187,7 +187,7 @@ luavalue_cast(int index, std::function<Ret(Args...)>& func) {
// weak references are used here, this means that the script must hold another reference // weak references are used here, this means that the script must hold another reference
// to this function, otherwise it will expire // to this function, otherwise it will expire
int funcWeakRef = g_lua.weakRef(); int funcWeakRef = g_lua.weakRef();
func = [=](Args... args...) -> Ret { func = [=](Args... args) -> Ret {
// note that we must catch exceptions, because this lambda can be called from anywhere // note that we must catch exceptions, because this lambda can be called from anywhere
// and most of them won't catch exceptions (e.g. dispatcher) // and most of them won't catch exceptions (e.g. dispatcher)
try { try {

View File

@ -888,7 +888,7 @@ std::string Platform::generateBacktrace(int maxLevel)
} }
if(i > 1) if(i > 1)
ss << "\n"; ss << "\n";
ss << line; ss << i << ": " << line;
} }
free(tracebackBuffer); free(tracebackBuffer);
} }

View File

@ -31,7 +31,7 @@ UILineEdit::UILineEdit()
m_align = Fw::AlignLeftCenter; m_align = Fw::AlignLeftCenter;
m_cursorPos = 0; m_cursorPos = 0;
m_startRenderPos = 0; m_startRenderPos = 0;
m_textHorizontalMargin = 3; m_textHorizontalMargin = 0;
m_textHidden = false; m_textHidden = false;
blinkCursor(); blinkCursor();
} }
@ -381,6 +381,8 @@ void UILineEdit::onStyleApply(const OTMLNodePtr& styleNode)
setCursorPos(m_text.length()); setCursorPos(m_text.length());
} else if(node->tag() == "text hidden") { } else if(node->tag() == "text hidden") {
setTextHidden(node->value<bool>()); setTextHidden(node->value<bool>());
} else if(node->tag() == "text margin") {
m_textHorizontalMargin = node->value<int>();
} }
} }
} }

View File

@ -102,7 +102,7 @@ void UIWidget::render()
child->render(); child->render();
// debug draw box // debug draw box
//g_graphics.bindColor(Color::green); //g_graphics.bindColor(Fw::green);
//g_graphics.drawBoundingRect(child->getRect()); //g_graphics.drawBoundingRect(child->getRect());
//g_fonts.getDefaultFont()->renderText(child->getId(), child->getPosition() + Point(2, 0), Color::red); //g_fonts.getDefaultFont()->renderText(child->getId(), child->getPosition() + Point(2, 0), Color::red);
@ -709,7 +709,9 @@ void UIWidget::onStyleApply(const OTMLNodePtr& styleNode)
for(const OTMLNodePtr& node : styleNode->children()) { for(const OTMLNodePtr& node : styleNode->children()) {
// background image // background image
if(node->tag() == "image") { if(node->tag() == "image") {
setImage(Image::loadFromOTML(node)); ImagePtr image = ImagePtr(new Image);
image->loadFromOTML(node);
setImage(image);
} }
else if(node->tag() == "border-image") { else if(node->tag() == "border-image") {
setImage(BorderImage::loadFromOTML(node)); setImage(BorderImage::loadFromOTML(node));

View File

@ -31,39 +31,29 @@ void UIWindow::setup()
UIWidget::setup(); UIWidget::setup();
m_moving = false; m_moving = false;
m_headHeight = 0; m_headHeight = 0;
m_headMargin = 0;
m_titleAlign = Fw::AlignCenter; m_titleAlign = Fw::AlignCenter;
} }
void UIWindow::render() void UIWindow::render()
{ {
// draw window head
Rect headRect = getRect();
headRect.setHeight(m_headHeight);
if(m_headImage && m_headHeight > 0) {
g_graphics.bindColor(m_backgroundColor);
m_headImage->draw(headRect);
// draw window head text
Rect headTextRect = headRect;
if(m_titleAlign & Fw::AlignLeft)
headTextRect.addLeft(-m_headMargin);
else if(m_titleAlign & Fw::AlignRight)
headTextRect.addRight(-m_headMargin);
m_font->renderText(m_title, headTextRect, m_titleAlign, m_foregroundColor);
}
// draw window body
Rect bodyRect = getRect();
bodyRect.setTop(headRect.bottom() + 1);
if(m_bodyImage) {
g_graphics.bindColor(m_backgroundColor);
m_bodyImage->draw(bodyRect);
}
// render children // render children
UIWidget::render(); UIWidget::render();
// draw window head
// draw window head text
Rect headTextRect = m_rect;
headTextRect.addTop(-m_headOffset.y);
headTextRect.setHeight(m_headHeight);
if(m_titleAlign & Fw::AlignLeft)
headTextRect.addLeft(-m_headOffset.x);
else if(m_titleAlign & Fw::AlignRight)
headTextRect.addRight(-m_headOffset.x);
else {
headTextRect.addLeft(-m_headOffset.x);
headTextRect.addRight(-m_headOffset.x);
}
m_font->renderText(m_title, headTextRect, m_titleAlign, m_foregroundColor);
} }
void UIWindow::onStyleApply(const OTMLNodePtr& styleNode) void UIWindow::onStyleApply(const OTMLNodePtr& styleNode)
@ -71,20 +61,14 @@ void UIWindow::onStyleApply(const OTMLNodePtr& styleNode)
UIWidget::onStyleApply(styleNode); UIWidget::onStyleApply(styleNode);
for(OTMLNodePtr node : styleNode->children()) { for(OTMLNodePtr node : styleNode->children()) {
if(node->tag() == "head") { if(node->tag() == "head height")
if(OTMLNodePtr cnode = node->get("border-image")) m_headHeight = node->value<int>();
m_headImage = BorderImage::loadFromOTML(cnode); else if(node->tag() == "head offset")
m_headHeight = node->valueAt("height", m_headImage->getDefaultSize().height()); m_headOffset = node->value<Point>();
m_headMargin = node->valueAt("margin", 0); else if(node->tag() == "title")
m_titleAlign = Fw::translateAlignment(node->valueAt("text align", std::string("center")));
}
else if(node->tag() == "body") {
if(OTMLNodePtr cnode = node->get("border-image"))
m_bodyImage = BorderImage::loadFromOTML(cnode);
}
else if(node->tag() == "title") {
setTitle(node->value()); setTitle(node->value());
} else if(node->tag() == "head text align")
m_titleAlign = Fw::translateAlignment(node->value());
else if(node->tag() == "onEnter") { else if(node->tag() == "onEnter") {
g_lua.loadFunction(node->value(), "@" + node->source() + "[" + node->tag() + "]"); g_lua.loadFunction(node->value(), "@" + node->source() + "[" + node->tag() + "]");
luaSetField(node->tag()); luaSetField(node->tag());

View File

@ -52,7 +52,7 @@ private:
BorderImagePtr m_headImage; BorderImagePtr m_headImage;
ImagePtr m_bodyImage; ImagePtr m_bodyImage;
int m_headHeight; int m_headHeight;
int m_headMargin; Point m_headOffset;
Fw::AlignmentFlag m_titleAlign; Fw::AlignmentFlag m_titleAlign;
}; };