implement icon property for UIButton
This commit is contained in:
parent
71d9074fff
commit
c584426f24
3
TODO
3
TODO
|
@ -13,4 +13,5 @@ scrollbar
|
|||
make otui syntax more like css
|
||||
a real working border and background property in otui
|
||||
setIcon() for buttons
|
||||
load state styles in order
|
||||
load state styles in order
|
||||
grid layout
|
|
@ -12,13 +12,9 @@ TopPanel
|
|||
margin.top: 4
|
||||
margin.left: 6
|
||||
tooltip: Options
|
||||
icon: /core_styles/icons/settings.png
|
||||
@onClick: Options.create()
|
||||
|
||||
UIWidget
|
||||
size: 16 16
|
||||
image: /core_styles/icons/settings.png
|
||||
anchors.centerIn: parent
|
||||
phantom: true
|
||||
|
||||
TopButton
|
||||
id: enterGameButton
|
||||
|
@ -26,6 +22,7 @@ TopPanel
|
|||
anchors.left: prev.right
|
||||
margin.left: 6
|
||||
tooltip: Enter game with a character
|
||||
icon: /core_styles/icons/login.png
|
||||
@onClick: |
|
||||
if Game.isOnline() then
|
||||
CharacterList.show()
|
||||
|
@ -33,32 +30,22 @@ TopPanel
|
|||
EnterGame.show()
|
||||
end
|
||||
|
||||
UIWidget
|
||||
size: 16 16
|
||||
image: /core_styles/icons/login.png
|
||||
anchors.centerIn: parent
|
||||
phantom: true
|
||||
|
||||
TopButton
|
||||
id: motdButton
|
||||
anchors.top: prev.top
|
||||
anchors.left: prev.right
|
||||
margin.left: 6
|
||||
tooltip: Message of the day
|
||||
icon: /core_styles/icons/motd.png
|
||||
@onClick:
|
||||
|
||||
UIWidget
|
||||
size: 16 16
|
||||
image: /core_styles/icons/motd.png
|
||||
anchors.centerIn: parent
|
||||
phantom: true
|
||||
|
||||
TopButton
|
||||
anchors.top: parent.top
|
||||
anchors.right: parent.right
|
||||
margin.top: 4
|
||||
margin.right: 6
|
||||
tooltip: Logout
|
||||
icon: /core_styles/icons/logout.png
|
||||
@onClick: |
|
||||
if Game.isOnline() then
|
||||
Game.logout(false)
|
||||
|
@ -66,26 +53,15 @@ TopPanel
|
|||
exit()
|
||||
end
|
||||
|
||||
UIWidget
|
||||
size: 16 16
|
||||
image: /core_styles/icons/logout.png
|
||||
anchors.centerIn: parent
|
||||
phantom: true
|
||||
|
||||
TopButton
|
||||
anchors.top: parent.top
|
||||
anchors.right: prev.left
|
||||
margin.top: 4
|
||||
margin.right: 6
|
||||
tooltip: About OTClient
|
||||
icon: /core_styles/icons/about.png
|
||||
@onClick: About.create()
|
||||
|
||||
UIWidget
|
||||
size: 16 16
|
||||
image: /core_styles/icons/about.png
|
||||
anchors.centerIn: parent
|
||||
phantom: true
|
||||
|
||||
FrameCounter
|
||||
anchors.top: parent.top
|
||||
anchors.right: prev.left
|
||||
|
|
|
@ -26,19 +26,26 @@
|
|||
#include <framework/otml/otmlnode.h>
|
||||
#include <framework/luascript/luainterface.h>
|
||||
#include <framework/graphics/graphics.h>
|
||||
#include <framework/graphics/texture.h>
|
||||
#include <framework/graphics/texturemanager.h>
|
||||
|
||||
void UIButton::setup()
|
||||
{
|
||||
UIWidget::setup();
|
||||
setFocusable(false);
|
||||
|
||||
// by default, all callbacks call lua fields
|
||||
m_onClick = [this]() { this->callLuaField("onClick"); };
|
||||
}
|
||||
|
||||
void UIButton::render()
|
||||
{
|
||||
UIWidget::render();
|
||||
|
||||
if(m_icon) {
|
||||
Rect iconRect;
|
||||
iconRect.setSize(m_icon->getSize());
|
||||
iconRect.moveCenter(m_rect.center());
|
||||
g_graphics.drawTexturedRect(iconRect, m_icon);
|
||||
}
|
||||
|
||||
Rect textRect = m_rect;
|
||||
textRect.translate(m_textOffset);
|
||||
m_font->renderText(m_text, textRect, Fw::AlignCenter, m_foregroundColor);
|
||||
|
@ -49,18 +56,18 @@ void UIButton::onStyleApply(const OTMLNodePtr& styleNode)
|
|||
UIWidget::onStyleApply(styleNode);
|
||||
|
||||
for(OTMLNodePtr node : styleNode->children()) {
|
||||
if(node->tag() == "text-offset") {
|
||||
if(node->tag() == "text-offset")
|
||||
m_textOffset = node->value<Point>();
|
||||
} else if(node->tag() == "text") {
|
||||
else if(node->tag() == "text")
|
||||
m_text = node->value();
|
||||
}
|
||||
else if(node->tag() == "icon")
|
||||
m_icon = g_textures.getTexture(node->value());
|
||||
}
|
||||
}
|
||||
|
||||
void UIButton::onMouseRelease(const Point& mousePos, Fw::MouseButton button)
|
||||
{
|
||||
if(isPressed()) {
|
||||
if(m_onClick && getRect().contains(mousePos))
|
||||
m_onClick();
|
||||
if(isPressed() && getRect().contains(mousePos)) {
|
||||
callLuaField("onClick");
|
||||
}
|
||||
}
|
||||
|
|
|
@ -31,10 +31,7 @@ public:
|
|||
virtual void setup();
|
||||
virtual void render();
|
||||
|
||||
void setOnClick(const SimpleCallback& onClick) { m_onClick = onClick; }
|
||||
void setText(const std::string& text) { m_text = text; }
|
||||
|
||||
SimpleCallback getOnClick() const { return m_onClick; }
|
||||
std::string getText() const { return m_text; }
|
||||
|
||||
UIButtonPtr asUIButton() { return std::static_pointer_cast<UIButton>(shared_from_this()); }
|
||||
|
@ -43,8 +40,8 @@ protected:
|
|||
virtual void onStyleApply(const OTMLNodePtr& styleNode);
|
||||
virtual void onMouseRelease(const Point& mousePos, Fw::MouseButton button);
|
||||
|
||||
SimpleCallback m_onClick;
|
||||
Point m_textOffset;
|
||||
TexturePtr m_icon;
|
||||
std::string m_text;
|
||||
};
|
||||
|
||||
|
|
Loading…
Reference in New Issue