From 39c62942cf59a0d62e3711661c91f7cbc931cf2e Mon Sep 17 00:00:00 2001 From: Eduardo Bart Date: Thu, 3 Nov 2011 21:34:32 -0200 Subject: [PATCH] add chat buffer --- TODO | 3 ++- modules/chat/chat.lua | 19 ++++++++++++++++++- modules/chat/chat.otui | 13 +++++++++---- modules/game/game.otui | 2 +- modules/skills/skills.lua | 2 +- modules/skills/skills.otmod | 2 -- src/framework/ui/uilayout.h | 2 ++ src/framework/ui/uiverticallayout.cpp | 22 +++++++++++++++++++--- src/framework/ui/uiverticallayout.h | 4 ++++ src/framework/ui/uiwidget.cpp | 25 +++++++++++++++++++------ src/otclient/net/protocolgameparse.cpp | 7 +++++-- src/otclient/otclient.cpp | 2 +- 12 files changed, 81 insertions(+), 22 deletions(-) diff --git a/TODO b/TODO index d86bdc14..b4e89713 100644 --- a/TODO +++ b/TODO @@ -10,4 +10,5 @@ console history console text selection console scrolling padding -remember password/account \ No newline at end of file +remember password/account +move fps to a module \ No newline at end of file diff --git a/modules/chat/chat.lua b/modules/chat/chat.lua index cafa9778..55da45af 100644 --- a/modules/chat/chat.lua +++ b/modules/chat/chat.lua @@ -2,10 +2,26 @@ Chat = {} -- private variables local chatPanel +local chatBuffer + +-- private functions +local function onCreatureSpeak(name, level, msgtype, message) + style = 'ChatLabel' + if name and level > 0 then + message = name .. ' [' .. level .. ']: ' .. message + style = 'YellowChatLabel' + end + + local label = UILabel.create() + label:setStyle(style) + label:setText(message) + chatBuffer:addChild(label) +end -- public functions function Chat.create() chatPanel = loadUI("/chat/chat.otui", Game.gameBottomPanel) + chatBuffer = chatPanel:getChildById('chatBuffer') end function Chat.destroy() @@ -16,4 +32,5 @@ end -- hooked events connect(Game, { onLogin = Chat.create, - onLogout = Chat.destroy }) \ No newline at end of file + onLogout = Chat.destroy, + onCreatureSpeak = onCreatureSpeak}) \ No newline at end of file diff --git a/modules/chat/chat.otui b/modules/chat/chat.otui index 7753dca3..aed58da9 100644 --- a/modules/chat/chat.otui +++ b/modules/chat/chat.otui @@ -1,6 +1,9 @@ ChatLabel < UILabel - font: verdana-11px-monochrome - height: 16 + font: verdana-11px-antialised + height: 14 + +YellowChatLabel < ChatLabel + color: yellow Panel id: chatPanel @@ -11,12 +14,14 @@ Panel anchors.top: parent.top anchors.left: parent.left anchors.right: parent.right - anchors.bottom: next.top + anchors.bottom: chatLineEdit.top margin.right: 6 margin.left: 6 margin.bottom: 2 margin.top: 6 - layout: verticalBox + layout: + type: verticalBox + align bottom: true focusable: false LineEdit diff --git a/modules/game/game.otui b/modules/game/game.otui index af813472..a8eb0229 100644 --- a/modules/game/game.otui +++ b/modules/game/game.otui @@ -13,7 +13,7 @@ UIWidget InterfacePanel2 id: bottomPanel - height: 140 + height: 144 anchors.left: parent.left anchors.right: rightPanel.left anchors.bottom: parent.bottom diff --git a/modules/skills/skills.lua b/modules/skills/skills.lua index f41c9865..e956fd3a 100644 --- a/modules/skills/skills.lua +++ b/modules/skills/skills.lua @@ -40,7 +40,7 @@ function Skills.destroy() end -- hooked events -function Game.setSkill(id, level, percent) +function Game.onSkillUpdate(id, level, percent) local skillPanel = skillWindow:getChildById('skillPanel') local levelLabel = skillPanel:getChildById('skillLevelId' .. (id + 1)) diff --git a/modules/skills/skills.otmod b/modules/skills/skills.otmod index 2bbf3694..12340ea5 100644 --- a/modules/skills/skills.otmod +++ b/modules/skills/skills.otmod @@ -10,5 +10,3 @@ Module onLoad: | require 'skills' return true - - diff --git a/src/framework/ui/uilayout.h b/src/framework/ui/uilayout.h index 7d60c157..384279e5 100644 --- a/src/framework/ui/uilayout.h +++ b/src/framework/ui/uilayout.h @@ -25,12 +25,14 @@ #include "declarations.h" #include +#include class UILayout : public LuaObject { public: UILayout(UIWidgetPtr parentWidget) : m_parentWidget(parentWidget) { } + virtual void applyStyle(const OTMLNodePtr& styleNode) { } virtual void update() = 0; virtual void addWidget(const UIWidgetPtr& widget) = 0; virtual void removeWidget(const UIWidgetPtr& widget) = 0; diff --git a/src/framework/ui/uiverticallayout.cpp b/src/framework/ui/uiverticallayout.cpp index 69ab5944..b72e64e0 100644 --- a/src/framework/ui/uiverticallayout.cpp +++ b/src/framework/ui/uiverticallayout.cpp @@ -22,10 +22,22 @@ #include "uiverticallayout.h" #include "uiwidget.h" +#include UIVerticalLayout::UIVerticalLayout(UIWidgetPtr parentWidget) : UILayout(parentWidget) { + m_alignBottom = false; +} + +void UIVerticalLayout::applyStyle(const OTMLNodePtr& styleNode) +{ + UILayout::applyStyle(styleNode); + + for(const OTMLNodePtr& node : styleNode->children()) { + if(node->tag() == "align bottom") + m_alignBottom = node->value(); + } } void UIVerticalLayout::update() @@ -39,10 +51,14 @@ void UIVerticalLayout::update() return first->getY() < second->getY(); }); - Point pos = parentWidget->getPosition(); + if(m_alignBottom) + std::reverse(widgets.begin(), widgets.end()); + + Point pos = (m_alignBottom) ? parentWidget->getRect().bottomLeft() : parentWidget->getPosition(); + for(const UIWidgetPtr& widget : widgets) { Size size = widget->getSize(); - pos.y += widget->getMarginTop(); + pos.y += (m_alignBottom) ? -(widget->getMarginBottom()+widget->getHeight()) : widget->getMarginTop(); if(widget->isSizeFixed()) { pos.x = parentWidget->getX() + (parentWidget->getWidth() - (widget->getMarginLeft() + widget->getWidth() + widget->getMarginRight()))/2; pos.x = std::max(pos.x, parentWidget->getX()); @@ -51,7 +67,7 @@ void UIVerticalLayout::update() pos.x = std::max(pos.x, parentWidget->getX() + (parentWidget->getWidth() - size.width())/2); } widget->setRect(Rect(pos, size)); - pos.y += widget->getHeight() + widget->getMarginBottom(); + pos.y += (m_alignBottom) ? -widget->getMarginTop() : (widget->getHeight() + widget->getMarginBottom()); } } diff --git a/src/framework/ui/uiverticallayout.h b/src/framework/ui/uiverticallayout.h index bc922fd2..56a8fcef 100644 --- a/src/framework/ui/uiverticallayout.h +++ b/src/framework/ui/uiverticallayout.h @@ -30,9 +30,13 @@ class UIVerticalLayout : public UILayout public: UIVerticalLayout(UIWidgetPtr parentWidget); + virtual void applyStyle(const OTMLNodePtr& styleNode); virtual void update(); virtual void addWidget(const UIWidgetPtr& widget); virtual void removeWidget(const UIWidgetPtr& widget); + +private: + bool m_alignBottom; }; #endif diff --git a/src/framework/ui/uiwidget.cpp b/src/framework/ui/uiwidget.cpp index 8f1c0756..e099dcb8 100644 --- a/src/framework/ui/uiwidget.cpp +++ b/src/framework/ui/uiwidget.cpp @@ -101,7 +101,7 @@ void UIWidget::renderChildren() // draw children for(const UIWidgetPtr& child : m_children) { // render only visible children with a valid rect - if(child->isExplicitlyVisible() && child->getRect().isValid()) { + if(child->isExplicitlyVisible() && child->getRect().isValid() && child->getRect().intersects(m_rect)) { // store current graphics opacity int oldOpacity = g_graphics.getOpacity(); @@ -802,11 +802,24 @@ void UIWidget::onStyleApply(const OTMLNodePtr& styleNode) else if(node->tag() == "layout") { // layout is set only once assert(!m_layout); - if(node->value() == "verticalBox") { - setLayout(UILayoutPtr(new UIVerticalLayout(asUIWidget()))); - } else if(node->value() == "anchor") { - setLayout(UILayoutPtr(new UIAnchorLayout(asUIWidget()))); - } + + std::string layoutType; + if(node->hasValue()) + layoutType = node->value(); + else + layoutType = node->valueAt("type"); + + UILayoutPtr layout; + if(layoutType == "verticalBox") + layout = UILayoutPtr(new UIVerticalLayout(asUIWidget())); + else if(layoutType == "anchor") + layout = UILayoutPtr(new UIAnchorLayout(asUIWidget())); + else + throw OTMLException(node, "cannot determine layout type"); + + if(node->hasChildren()) + layout->applyStyle(node); + setLayout(layout); } // anchors else if(boost::starts_with(node->tag(), "anchors.")) { diff --git a/src/otclient/net/protocolgameparse.cpp b/src/otclient/net/protocolgameparse.cpp index a7ac9f53..2db4d03a 100644 --- a/src/otclient/net/protocolgameparse.cpp +++ b/src/otclient/net/protocolgameparse.cpp @@ -663,7 +663,7 @@ void ProtocolGame::parsePlayerSkills(InputMessage& msg) } g_dispatcher.addEvent([=] { - g_lua.callGlobalField("Game", "setSkill", skill, values[Otc::SkillLevel], values[Otc::SkillPercent]); + g_lua.callGlobalField("Game", "onSkillUpdate", skill, values[Otc::SkillLevel], values[Otc::SkillPercent]); }); } } @@ -711,7 +711,10 @@ void ProtocolGame::parseCreatureSpeak(InputMessage& msg) } std::string message = msg.getString(); // message - logDebug(name, "[", level, "]: ", message); + + g_dispatcher.addEvent([=] { + g_lua.callGlobalField("Game", "onCreatureSpeak", name, level, type, message); + }); } void ProtocolGame::parseChannelList(InputMessage& msg) diff --git a/src/otclient/otclient.cpp b/src/otclient/otclient.cpp index 2c6146ad..e3110673 100644 --- a/src/otclient/otclient.cpp +++ b/src/otclient/otclient.cpp @@ -148,7 +148,7 @@ void OTClient::run() render(); // render fps - defaultFont->renderText(fpsText, Point(g_graphics.getScreenSize().width() - fpsTextSize.width() - 10, 38)); + defaultFont->renderText(fpsText, Point(g_graphics.getScreenSize().width() - fpsTextSize.width() - 70, 10)); // render end g_graphics.endRender();