From f3b3ecada1b0a138aadcd49c0a9afc4a1806d33d Mon Sep 17 00:00:00 2001 From: Henrique Date: Mon, 14 Nov 2011 00:40:18 -0200 Subject: [PATCH] progress bar and skills improve --- CMakeLists.txt | 1 + modules/skills/skills.lua | 12 +++--- modules/skills/skills.lua~ | 16 +++---- modules/skills/skills.otui | 19 ++++----- modules/tooltip/tooltip.lua | 5 +++ src/framework/luascript/luafunctions.cpp | 6 +++ src/framework/ui/ui.h | 1 + src/framework/ui/uiprogressbar.cpp | 53 ++++++++++++++++++++++++ src/framework/ui/uiprogressbar.h | 41 ++++++++++++++++++ 9 files changed, 132 insertions(+), 22 deletions(-) create mode 100644 src/framework/ui/uiprogressbar.cpp create mode 100644 src/framework/ui/uiprogressbar.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 7f5c3ed0..f37d01bd 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -140,6 +140,7 @@ SET(SOURCES src/framework/ui/uiframecounter.cpp src/framework/ui/uianchorlayout.cpp src/framework/ui/uiverticallayout.cpp src/framework/ui/uilayout.cpp + src/framework/ui/uiprogressbar.cpp ) IF(HANDLE_EXCEPTIONS) diff --git a/modules/skills/skills.lua b/modules/skills/skills.lua index e956fd3a..a693de0b 100644 --- a/modules/skills/skills.lua +++ b/modules/skills/skills.lua @@ -28,9 +28,10 @@ function Skills.create() levelLabel:setId('skillLevelId' .. i) levelLabel:setText('0') - local percentPanel = UIWidget.create() - skillPanel:addChild(percentPanel) - percentPanel:setStyle('SkillPercentPanel') + local percentBar = UIProgressBar.create() + skillPanel:addChild(percentBar) + percentBar:setStyle('SkillPercentPanel') + percentBar:setId('skillPercentId' .. i) end end @@ -46,8 +47,9 @@ function Game.onSkillUpdate(id, level, percent) local levelLabel = skillPanel:getChildById('skillLevelId' .. (id + 1)) levelLabel:setText(level) - --local percentLabel = skillPanel:getChildById('skillLevelId' .. id) - --levelLabel:setText(percent) + local percentBar = skillPanel:getChildById('skillPercentId' .. (id + 1)) + percentBar:setPercent(percent) + percentBar:setTooltip(percent .. "% to go") end connect(Game, { onLogin = Skills.create, diff --git a/modules/skills/skills.lua~ b/modules/skills/skills.lua~ index 71fb79b5..fcded665 100644 --- a/modules/skills/skills.lua~ +++ b/modules/skills/skills.lua~ @@ -28,9 +28,10 @@ function Skills.create() levelLabel:setId('skillLevelId' .. i) levelLabel:setText('0') - local percentPanel = UIWidget.create() - skillPanel:addChild(percentPanel) - percentPanel:setStyle('SkillPercentPanel') + local percentBar = UIProgressBar.create() + skillPanel:addChild(percentBar) + percentBar:setStyle('SkillPercentPanel') + percentBar:setId('skillPercentId' .. i) end end @@ -40,14 +41,15 @@ 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) + local levelLabel = skillPanel:getChildById('skillLevelId' .. (id + 1)) levelLabel:setText(level) - --local percentLabel = skillPanel:getChildById('skillLevelId' .. id) - --levelLabel:setText(percent) + local percentBar = skillPanel:getChildById('skillPercentId' .. (id + 1)) + percentBar:setPercent(percent) + percentBar:setTooltip("lalalalal2") end connect(Game, { onLogin = Skills.create, diff --git a/modules/skills/skills.otui b/modules/skills/skills.otui index 914e8de0..d5713e33 100644 --- a/modules/skills/skills.otui +++ b/modules/skills/skills.otui @@ -1,11 +1,10 @@ SkillFirstWidget < UIWidget - margin.top: 2 anchors.top: parent.top SkillNameLabel < Label font: verdana-11px-monochrome - margin.top: 2 + margin.top: 4 margin.left: 10 margin.right: 10 anchors.top: prev.bottom @@ -15,18 +14,18 @@ SkillNameLabel < Label SkillLevelLabel < Label font: verdana-11px-monochrome align: right - margin.top: 2 margin.left: 10 margin.right: 10 anchors.top: prev.top anchors.left: parent.left anchors.right: parent.right -SkillPercentPanel < UIWidget - color: blue - background-color: red - height: 4 - margin.top: 2 +SkillPercentPanel < UIProgressBar + color: black + background-color: green + tooltip: test + height: 5 + margin.top: 3 margin.left: 10 margin.right: 10 anchors.top: prev.bottom @@ -36,7 +35,7 @@ SkillPercentPanel < UIWidget MiniWindow id: skillWindow title: Skills - size: 200 185 + size: 200 220 Panel id: skillPanel @@ -44,4 +43,4 @@ MiniWindow margin.top: 26 margin.bottom: 3 margin.left: 3 - margin.right: 3 \ No newline at end of file + margin.right: 3 diff --git a/modules/tooltip/tooltip.lua b/modules/tooltip/tooltip.lua index b2d54caa..edf108cc 100644 --- a/modules/tooltip/tooltip.lua +++ b/modules/tooltip/tooltip.lua @@ -48,5 +48,10 @@ function ToolTip.hide() end end +function UIWidget:setTooltip(text) + self:applyStyle({ tooltip = text }) +end + -- hooks connect(UIButton, { onHoverChange = onButtonHoverChange}) +connect(UIProgressBar, { onHoverChange = onButtonHoverChange}) diff --git a/src/framework/luascript/luafunctions.cpp b/src/framework/luascript/luafunctions.cpp index 827c6e7d..baded23a 100644 --- a/src/framework/luascript/luafunctions.cpp +++ b/src/framework/luascript/luafunctions.cpp @@ -108,6 +108,12 @@ void LuaInterface::registerFunctions() g_lua.bindClassMemberFunction("setText", &UILabel::setText); g_lua.bindClassMemberFunction("resizeToText", &UILabel::resizeToText); + // UILabel + g_lua.registerClass(); + g_lua.bindClassStaticFunction("create", &UIWidget::create); + g_lua.bindClassMemberFunction("getPercent", &UIProgressBar::getPercent); + g_lua.bindClassMemberFunction("setPercent", &UIProgressBar::setPercent); + // UIButton g_lua.registerClass(); g_lua.bindClassStaticFunction("create", &UIWidget::create); diff --git a/src/framework/ui/ui.h b/src/framework/ui/ui.h index b6d81460..f78df8bf 100644 --- a/src/framework/ui/ui.h +++ b/src/framework/ui/ui.h @@ -30,5 +30,6 @@ #include "uilineedit.h" #include "uiwindow.h" #include "uiframecounter.h" +#include "uiprogressbar.h" #endif diff --git a/src/framework/ui/uiprogressbar.cpp b/src/framework/ui/uiprogressbar.cpp new file mode 100644 index 00000000..c10c1abd --- /dev/null +++ b/src/framework/ui/uiprogressbar.cpp @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2010-2011 OTClient + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include "uiprogressbar.h" +#include +#include + +void UIProgressBar::setup() +{ + UIWidget::setup(); + setPhantom(true); + setFocusable(false); + m_percent = 0; +} + +void UIProgressBar::render() +{ + UIWidget::render(); + + g_graphics.bindColor(m_foregroundColor); + g_graphics.drawBoundingRect(m_rect, 1); + + Rect fillRect = m_rect.expanded(-1); + fillRect.setWidth(fillRect.width() * m_percent / 100.0); + + g_graphics.bindColor(m_backgroundColor); + g_graphics.drawFilledRect(fillRect); +} + +void UIProgressBar::setPercent(double percent) +{ + assert(percent >= 0 && percent <= 100); + m_percent = percent; +} diff --git a/src/framework/ui/uiprogressbar.h b/src/framework/ui/uiprogressbar.h new file mode 100644 index 00000000..62f2e982 --- /dev/null +++ b/src/framework/ui/uiprogressbar.h @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2010-2011 OTClient + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#ifndef UIPROGRESSBAR_H +#define UIPROGRESSBAR_H + +#include "uiwidget.h" + +class UIProgressBar : public UIWidget +{ +public: + virtual void setup(); + virtual void render(); + + void setPercent(double percent); + double getPercent() { return m_percent; } + +private: + double m_percent; +}; + +#endif