From 11cb287a21878c4a7fa761faa5312b32f60dcc49 Mon Sep 17 00:00:00 2001 From: Henrique Santiago Date: Sat, 9 Feb 2013 19:57:37 -0200 Subject: [PATCH] Changes to UIProgressBar, simplified healthinfo module --- data/styles/10-progressbars.otui | 9 +++ modules/corelib/ui/uiprogressbar.lua | 60 ++++++++++++++++---- modules/game_healthinfo/healthinfo.lua | 73 ++++++++++--------------- modules/game_healthinfo/healthinfo.otui | 55 ++++++------------- src/framework/luafunctions.cpp | 2 + src/framework/ui/uitextedit.cpp | 6 +- src/framework/ui/uiwidget.h | 7 ++- src/framework/ui/uiwidgettext.cpp | 10 +++- 8 files changed, 120 insertions(+), 102 deletions(-) diff --git a/data/styles/10-progressbars.otui b/data/styles/10-progressbars.otui index d1a378d7..8718efa9 100644 --- a/data/styles/10-progressbars.otui +++ b/data/styles/10-progressbars.otui @@ -4,6 +4,15 @@ ProgressBar < UIProgressBar border: 1 black image-source: /images/ui/progressbar image-border: 1 + font: verdana-11px-rounded + text-offset: 0 2 + on: true + + $!on: + visible: false + margin-top: 0 + margin-bottom: 0 + height: 0 ProgressRect < UIProgressRect anchors.fill: parent diff --git a/modules/corelib/ui/uiprogressbar.lua b/modules/corelib/ui/uiprogressbar.lua index 5ee6ba3d..1005a7ef 100644 --- a/modules/corelib/ui/uiprogressbar.lua +++ b/modules/corelib/ui/uiprogressbar.lua @@ -5,34 +5,71 @@ function UIProgressBar.create() local progressbar = UIProgressBar.internalCreate() progressbar:setFocusable(false) progressbar:setPhantom(true) - progressbar.percent = 0 + progressbar.min = 0 + progressbar.max = 100 + progressbar.value = 0 progressbar.bgBorderLeft = 0 progressbar.bgBorderRight = 0 progressbar.bgBorderTop = 0 progressbar.bgBorderBottom = 0 - progressbar:updateBackground() return progressbar end -function UIProgressBar:setPercent(percent) - self.percent = math.max(math.min(percent, 100), 0) +function UIProgressBar:setMinimum(minimum) + self.minimum = minimum + if self.value < minimum then + self:setValue(minimum) + end +end + +function UIProgressBar:setMaximum(maximum) + self.maximum = maximum + if self.value > maximum then + self:setValue(maximum) + end +end + +function UIProgressBar:setValue(value, minimum, maximum) + if minimum then + self:setMinimum(minimum) + end + + if maximum then + self:setMaximum(maximum) + end + + self.value = math.max(math.min(value, self.maximum), self.minimum) self:updateBackground() end +function UIProgressBar:setPercent(percent) + self:setValue(percent, 0, 100) +end function UIProgressBar:getPercent() - return self.percent + return self.value end function UIProgressBar:getPercentPixels() - return 100 / self:getWidth() + return (self.maximum - self.minimum) / self:getWidth() +end + +function UIProgressBar:getProgress() + if self.minimum == self.maximum then return 1 end + return (self.value - self.minimum) / (self.maximum - self.minimum) end function UIProgressBar:updateBackground() - local width = math.round(math.max((self.percent * (self:getWidth() - self.bgBorderLeft - self.bgBorderRight))/100, 1)) - local height = self:getHeight() - self.bgBorderTop - self.bgBorderBottom - local rect = { x = self.bgBorderLeft, y = self.bgBorderTop, width = width, height = height } - self:setBackgroundRect(rect) + if self:isOn() then + local width = math.round(math.max((self:getProgress() * (self:getWidth() - self.bgBorderLeft - self.bgBorderRight)), 1)) + local height = self:getHeight() - self.bgBorderTop - self.bgBorderBottom + local rect = { x = self.bgBorderLeft, y = self.bgBorderTop, width = width, height = height } + self:setBackgroundRect(rect) + end +end + +function UIProgressBar:onSetup() + self:updateBackground() end function UIProgressBar:onStyleApply(name, node) @@ -55,5 +92,8 @@ function UIProgressBar:onStyleApply(name, node) end function UIProgressBar:onGeometryChange(oldRect, newRect) + if not self:isOn() then + self:setHeight(0) + end self:updateBackground() end diff --git a/modules/game_healthinfo/healthinfo.lua b/modules/game_healthinfo/healthinfo.lua index 77569121..33b14110 100644 --- a/modules/game_healthinfo/healthinfo.lua +++ b/modules/game_healthinfo/healthinfo.lua @@ -21,10 +21,6 @@ healthInfoWindow = nil healthBar = nil manaBar = nil experienceBar = nil -soulBar = nil -healthLabel = nil -manaLabel = nil -experienceLabel = nil soulLabel = nil capLabel = nil @@ -46,10 +42,6 @@ function init() healthBar = healthInfoWindow:recursiveGetChildById('healthBar') manaBar = healthInfoWindow:recursiveGetChildById('manaBar') experienceBar = healthInfoWindow:recursiveGetChildById('experienceBar') - healthLabel = healthInfoWindow:recursiveGetChildById('healthLabel') - manaLabel = healthInfoWindow:recursiveGetChildById('manaLabel') - experienceLabel = healthInfoWindow:recursiveGetChildById('experienceLabel') - soulBar = healthInfoWindow:recursiveGetChildById('soulBar') soulLabel = healthInfoWindow:recursiveGetChildById('soulLabel') capLabel = healthInfoWindow:recursiveGetChildById('capLabel') @@ -90,48 +82,54 @@ function toggle() end end +function toggleIcon(bitChanged) + local content = healthInfoWindow:recursiveGetChildById('conditionPanel') + + local icon = content:getChildById(Icons[bitChanged].id) + if icon then + icon:destroy() + else + icon = g_ui.createWidget('ConditionWidget', content) + icon:setId(Icons[bitChanged].id) + icon:setImageSource(Icons[bitChanged].path) + icon:setTooltip(Icons[bitChanged].tooltip) + end +end + function hideLabels() - capLabel:hide() - soulLabel:hide() - local removeHeight = capLabel:getHeight() + capLabel:getMarginTop() + capLabel:getMarginBottom() + local removeHeight = math.max(capLabel:getMarginRect().height, soulLabel:getMarginRect().height) + capLabel:setOn(false) + soulLabel:setOn(false) healthInfoWindow:setHeight(math.max(healthInfoWindow.minimizedHeight, healthInfoWindow:getHeight() - removeHeight)) end function hideExperience() - experienceBar:hide() - experienceLabel:hide() - local removeHeight = experienceBar:getHeight() + experienceBar:getMarginTop() + experienceBar:getMarginBottom() + local removeHeight = experienceBar:getMarginRect().height + experienceBar:setOn(false) healthInfoWindow:setHeight(math.max(healthInfoWindow.minimizedHeight, healthInfoWindow:getHeight() - removeHeight)) end -function onMiniWindowClose() - healthInfoButton:setOn(false) -end - function offline() healthInfoWindow:recursiveGetChildById('conditionPanel'):destroyChildren() end -- hooked events +function onMiniWindowClose() + healthInfoButton:setOn(false) +end + function onHealthChange(localPlayer, health, maxHealth) - healthLabel:setText(health .. ' / ' .. maxHealth) - healthBar:setPercent(health / maxHealth * 100) + healthBar:setText(health .. ' / ' .. maxHealth) + healthBar:setValue(health, 0, maxHealth) end function onManaChange(localPlayer, mana, maxMana) - manaLabel:setText(mana .. ' / ' .. maxMana) - - local percent - if maxMana == 0 then - percent = 100 - else - percent = (mana * 100)/maxMana - end - manaBar:setPercent(percent) + manaBar:setText(mana .. ' / ' .. maxMana) + manaBar:setValue(mana, 0, maxMana) end function onLevelChange(localPlayer, value, percent) - experienceLabel:setText(percent .. "%") + experienceBar:setText(percent .. "%") experienceBar:setPercent(percent) end @@ -143,7 +141,6 @@ function onFreeCapacityChange(player, freeCapacity) capLabel:setText(tr('Cap') .. ': ' .. freeCapacity) end - function onStatesChange(localPlayer, now, old) if now == old then return end @@ -157,17 +154,3 @@ function onStatesChange(localPlayer, now, old) end end end - -function toggleIcon(bitChanged) - local content = healthInfoWindow:recursiveGetChildById('conditionPanel') - - local icon = content:getChildById(Icons[bitChanged].id) - if icon then - icon:destroy() - else - icon = g_ui.createWidget('ConditionWidget', content) - icon:setId(Icons[bitChanged].id) - icon:setImageSource(Icons[bitChanged].path) - icon:setTooltip(Icons[bitChanged].tooltip) - end -end \ No newline at end of file diff --git a/modules/game_healthinfo/healthinfo.otui b/modules/game_healthinfo/healthinfo.otui index 098b712c..550ea3f1 100644 --- a/modules/game_healthinfo/healthinfo.otui +++ b/modules/game_healthinfo/healthinfo.otui @@ -1,6 +1,5 @@ HealthBar < ProgressBar id: healthBar - height: 15 background-color: #ff4444 anchors.top: parent.top anchors.left: parent.left @@ -8,48 +7,19 @@ HealthBar < ProgressBar ManaBar < ProgressBar id: manaBar - height: 15 background-color: #4444ff anchors.top: prev.bottom anchors.left: parent.left anchors.right: parent.right - margin-top: 4 + margin-top: 3 ExperienceBar < ProgressBar id: experienceBar - height: 15 background-color: yellow anchors.top: prev.bottom anchors.left: parent.left anchors.right: parent.right - margin-top: 4 - -HealthLabel < GameLabel - id: healthLabel - color: white - text-align: center - font: verdana-11px-rounded - anchors.fill: healthBar - margin-top: 2 - text: 0 / 0 - -ManaLabel < GameLabel - id: manaLabel - color: white - text-align: center - font: verdana-11px-rounded - anchors.fill: manaBar - margin-top: 2 - text: 0 / 0 - -ExperienceLabel < GameLabel - id: experienceLabel - color: white - text-align: center - font: verdana-11px-rounded - anchors.fill: experienceBar - margin-top: 2 - text: 0 / 0 + margin-top: 3 SoulLabel < GameLabel id: soulLabel @@ -61,7 +31,12 @@ SoulLabel < GameLabel anchors.left: parent.horizontalCenter margin-top: 5 margin-right: 3 - text: Soul: + on: true + + $!on: + visible: false + margin-top: 0 + height: 0 CapLabel < GameLabel id: capLabel @@ -72,7 +47,12 @@ CapLabel < GameLabel anchors.right: parent.horizontalCenter margin-top: 5 margin-left: 3 - text: Cap: + on: true + + $!on: + visible: false + margin-top: 0 + height: 0 ConditionWidget < UIWidget size: 18 18 @@ -84,17 +64,14 @@ MiniWindow icon: /images/topbuttons/healthinfo id: healthInfoWindow !text: tr('Health Info') - height: 117 + height: 121 @onClose: modules.game_healthinfo.onMiniWindowClose() &save: true MiniWindowContents HealthBar - HealthLabel ManaBar - ManaLabel ExperienceBar - ExperienceLabel Panel id: conditionPanel layout: @@ -109,4 +86,4 @@ MiniWindow border-color: #00000077 background-color: #ffffff11 SoulLabel - CapLabel \ No newline at end of file + CapLabel diff --git a/src/framework/luafunctions.cpp b/src/framework/luafunctions.cpp index 159711a2..bef1c519 100644 --- a/src/framework/luafunctions.cpp +++ b/src/framework/luafunctions.cpp @@ -592,6 +592,8 @@ void Application::registerLuaFunctions() g_lua.bindClassMemberFunction("setTextOffset", &UIWidget::setTextOffset); g_lua.bindClassMemberFunction("setTextWrap", &UIWidget::setTextWrap); g_lua.bindClassMemberFunction("setTextAutoResize", &UIWidget::setTextAutoResize); + g_lua.bindClassMemberFunction("setTextVerticalAutoResize", &UIWidget::setTextVerticalAutoResize); + g_lua.bindClassMemberFunction("setTextHorizontalAutoResize", &UIWidget::setTextHorizontalAutoResize); g_lua.bindClassMemberFunction("setFont", &UIWidget::setFont); g_lua.bindClassMemberFunction("getText", &UIWidget::getText); g_lua.bindClassMemberFunction("getDrawText", &UIWidget::getDrawText); diff --git a/src/framework/ui/uitextedit.cpp b/src/framework/ui/uitextedit.cpp index 00630115..232da156 100644 --- a/src/framework/ui/uitextedit.cpp +++ b/src/framework/ui/uitextedit.cpp @@ -139,12 +139,12 @@ void UITextEdit::update(bool focusCursor) int glyph; // update rect size - if(!m_rect.isValid() || m_textAutoResize) { + if(!m_rect.isValid() || m_textHorizontalAutoResize || m_textVerticalAutoResize) { textBoxSize += Size(m_padding.left + m_padding.right, m_padding.top + m_padding.bottom) + m_textOffset.toSize(); Size size = getSize(); - if(size.width() <= 0 || (m_textAutoResize && !m_textWrap)) + if(size.width() <= 0 || (m_textHorizontalAutoResize && !m_textWrap)) size.setWidth(textBoxSize.width()); - if(size.height() <= 0 || m_textAutoResize) + if(size.height() <= 0 || m_textVerticalAutoResize) size.setHeight(textBoxSize.height()); setSize(size); } diff --git a/src/framework/ui/uiwidget.h b/src/framework/ui/uiwidget.h index 1fa734b0..c91393ee 100644 --- a/src/framework/ui/uiwidget.h +++ b/src/framework/ui/uiwidget.h @@ -480,7 +480,8 @@ protected: Fw::AlignmentFlag m_textAlign; Point m_textOffset; stdext::boolean m_textWrap; - stdext::boolean m_textAutoResize; + stdext::boolean m_textVerticalAutoResize; + stdext::boolean m_textHorizontalAutoResize; stdext::boolean m_textOnlyUpperCase; BitmapFontPtr m_font; @@ -492,7 +493,9 @@ public: void setTextAlign(Fw::AlignmentFlag align) { m_textAlign = align; updateText(); } void setTextOffset(const Point& offset) { m_textOffset = offset; updateText(); } void setTextWrap(bool textWrap) { m_textWrap = textWrap; updateText(); } - void setTextAutoResize(bool textAutoResize) { m_textAutoResize = textAutoResize; updateText(); } + void setTextAutoResize(bool textAutoResize) { m_textHorizontalAutoResize = textAutoResize; m_textVerticalAutoResize = textAutoResize; updateText(); } + void setTextHorizontalAutoResize(bool textAutoResize) { m_textHorizontalAutoResize = textAutoResize; updateText(); } + void setTextVerticalAutoResize(bool textAutoResize) { m_textVerticalAutoResize = textAutoResize; updateText(); } void setTextOnlyUpperCase(bool textOnlyUpperCase) { m_textOnlyUpperCase = textOnlyUpperCase; setText(m_text); } void setFont(const std::string& fontName); diff --git a/src/framework/ui/uiwidgettext.cpp b/src/framework/ui/uiwidgettext.cpp index 6f7bda97..969e3a79 100644 --- a/src/framework/ui/uiwidgettext.cpp +++ b/src/framework/ui/uiwidgettext.cpp @@ -42,13 +42,13 @@ void UIWidget::updateText() m_drawText = m_text; // update rect size - if(!m_rect.isValid() || m_textAutoResize) { + if(!m_rect.isValid() || m_textHorizontalAutoResize || m_textVerticalAutoResize) { Size textBoxSize = getTextSize(); textBoxSize += Size(m_padding.left + m_padding.right, m_padding.top + m_padding.bottom) + m_textOffset.toSize(); Size size = getSize(); - if(size.width() <= 0 || (m_textAutoResize && !m_textWrap)) + if(size.width() <= 0 || (m_textHorizontalAutoResize && !m_textWrap)) size.setWidth(textBoxSize.width()); - if(size.height() <= 0 || m_textAutoResize) + if(size.height() <= 0 || m_textVerticalAutoResize) size.setHeight(textBoxSize.height()); setSize(size); } @@ -69,6 +69,10 @@ void UIWidget::parseTextStyle(const OTMLNodePtr& styleNode) setTextWrap(node->value()); else if(node->tag() == "text-auto-resize") setTextAutoResize(node->value()); + else if(node->tag() == "text-horizontal-auto-resize") + setTextHorizontalAutoResize(node->value()); + else if(node->tag() == "text-vertical-auto-resize") + setTextVerticalAutoResize(node->value()); else if(node->tag() == "text-only-upper-case") setTextOnlyUpperCase(node->value()); else if(node->tag() == "font")