Changes to UIProgressBar, simplified healthinfo module

This commit is contained in:
Henrique Santiago 2013-02-09 19:57:37 -02:00
parent 6f37361df0
commit 11cb287a21
8 changed files with 120 additions and 102 deletions

View File

@ -4,6 +4,15 @@ ProgressBar < UIProgressBar
border: 1 black border: 1 black
image-source: /images/ui/progressbar image-source: /images/ui/progressbar
image-border: 1 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 ProgressRect < UIProgressRect
anchors.fill: parent anchors.fill: parent

View File

@ -5,35 +5,72 @@ function UIProgressBar.create()
local progressbar = UIProgressBar.internalCreate() local progressbar = UIProgressBar.internalCreate()
progressbar:setFocusable(false) progressbar:setFocusable(false)
progressbar:setPhantom(true) progressbar:setPhantom(true)
progressbar.percent = 0 progressbar.min = 0
progressbar.max = 100
progressbar.value = 0
progressbar.bgBorderLeft = 0 progressbar.bgBorderLeft = 0
progressbar.bgBorderRight = 0 progressbar.bgBorderRight = 0
progressbar.bgBorderTop = 0 progressbar.bgBorderTop = 0
progressbar.bgBorderBottom = 0 progressbar.bgBorderBottom = 0
progressbar:updateBackground()
return progressbar return progressbar
end end
function UIProgressBar:setPercent(percent) function UIProgressBar:setMinimum(minimum)
self.percent = math.max(math.min(percent, 100), 0) 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() self:updateBackground()
end end
function UIProgressBar:setPercent(percent)
self:setValue(percent, 0, 100)
end
function UIProgressBar:getPercent() function UIProgressBar:getPercent()
return self.percent return self.value
end end
function UIProgressBar:getPercentPixels() 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 end
function UIProgressBar:updateBackground() function UIProgressBar:updateBackground()
local width = math.round(math.max((self.percent * (self:getWidth() - self.bgBorderLeft - self.bgBorderRight))/100, 1)) 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 height = self:getHeight() - self.bgBorderTop - self.bgBorderBottom
local rect = { x = self.bgBorderLeft, y = self.bgBorderTop, width = width, height = height } local rect = { x = self.bgBorderLeft, y = self.bgBorderTop, width = width, height = height }
self:setBackgroundRect(rect) self:setBackgroundRect(rect)
end end
end
function UIProgressBar:onSetup()
self:updateBackground()
end
function UIProgressBar:onStyleApply(name, node) function UIProgressBar:onStyleApply(name, node)
for name,value in pairs(node) do for name,value in pairs(node) do
@ -55,5 +92,8 @@ function UIProgressBar:onStyleApply(name, node)
end end
function UIProgressBar:onGeometryChange(oldRect, newRect) function UIProgressBar:onGeometryChange(oldRect, newRect)
if not self:isOn() then
self:setHeight(0)
end
self:updateBackground() self:updateBackground()
end end

View File

@ -21,10 +21,6 @@ healthInfoWindow = nil
healthBar = nil healthBar = nil
manaBar = nil manaBar = nil
experienceBar = nil experienceBar = nil
soulBar = nil
healthLabel = nil
manaLabel = nil
experienceLabel = nil
soulLabel = nil soulLabel = nil
capLabel = nil capLabel = nil
@ -46,10 +42,6 @@ function init()
healthBar = healthInfoWindow:recursiveGetChildById('healthBar') healthBar = healthInfoWindow:recursiveGetChildById('healthBar')
manaBar = healthInfoWindow:recursiveGetChildById('manaBar') manaBar = healthInfoWindow:recursiveGetChildById('manaBar')
experienceBar = healthInfoWindow:recursiveGetChildById('experienceBar') experienceBar = healthInfoWindow:recursiveGetChildById('experienceBar')
healthLabel = healthInfoWindow:recursiveGetChildById('healthLabel')
manaLabel = healthInfoWindow:recursiveGetChildById('manaLabel')
experienceLabel = healthInfoWindow:recursiveGetChildById('experienceLabel')
soulBar = healthInfoWindow:recursiveGetChildById('soulBar')
soulLabel = healthInfoWindow:recursiveGetChildById('soulLabel') soulLabel = healthInfoWindow:recursiveGetChildById('soulLabel')
capLabel = healthInfoWindow:recursiveGetChildById('capLabel') capLabel = healthInfoWindow:recursiveGetChildById('capLabel')
@ -90,48 +82,54 @@ function toggle()
end 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
function hideLabels() function hideLabels()
capLabel:hide() local removeHeight = math.max(capLabel:getMarginRect().height, soulLabel:getMarginRect().height)
soulLabel:hide() capLabel:setOn(false)
local removeHeight = capLabel:getHeight() + capLabel:getMarginTop() + capLabel:getMarginBottom() soulLabel:setOn(false)
healthInfoWindow:setHeight(math.max(healthInfoWindow.minimizedHeight, healthInfoWindow:getHeight() - removeHeight)) healthInfoWindow:setHeight(math.max(healthInfoWindow.minimizedHeight, healthInfoWindow:getHeight() - removeHeight))
end end
function hideExperience() function hideExperience()
experienceBar:hide() local removeHeight = experienceBar:getMarginRect().height
experienceLabel:hide() experienceBar:setOn(false)
local removeHeight = experienceBar:getHeight() + experienceBar:getMarginTop() + experienceBar:getMarginBottom()
healthInfoWindow:setHeight(math.max(healthInfoWindow.minimizedHeight, healthInfoWindow:getHeight() - removeHeight)) healthInfoWindow:setHeight(math.max(healthInfoWindow.minimizedHeight, healthInfoWindow:getHeight() - removeHeight))
end end
function onMiniWindowClose()
healthInfoButton:setOn(false)
end
function offline() function offline()
healthInfoWindow:recursiveGetChildById('conditionPanel'):destroyChildren() healthInfoWindow:recursiveGetChildById('conditionPanel'):destroyChildren()
end end
-- hooked events -- hooked events
function onMiniWindowClose()
healthInfoButton:setOn(false)
end
function onHealthChange(localPlayer, health, maxHealth) function onHealthChange(localPlayer, health, maxHealth)
healthLabel:setText(health .. ' / ' .. maxHealth) healthBar:setText(health .. ' / ' .. maxHealth)
healthBar:setPercent(health / maxHealth * 100) healthBar:setValue(health, 0, maxHealth)
end end
function onManaChange(localPlayer, mana, maxMana) function onManaChange(localPlayer, mana, maxMana)
manaLabel:setText(mana .. ' / ' .. maxMana) manaBar:setText(mana .. ' / ' .. maxMana)
manaBar:setValue(mana, 0, maxMana)
local percent
if maxMana == 0 then
percent = 100
else
percent = (mana * 100)/maxMana
end
manaBar:setPercent(percent)
end end
function onLevelChange(localPlayer, value, percent) function onLevelChange(localPlayer, value, percent)
experienceLabel:setText(percent .. "%") experienceBar:setText(percent .. "%")
experienceBar:setPercent(percent) experienceBar:setPercent(percent)
end end
@ -143,7 +141,6 @@ function onFreeCapacityChange(player, freeCapacity)
capLabel:setText(tr('Cap') .. ': ' .. freeCapacity) capLabel:setText(tr('Cap') .. ': ' .. freeCapacity)
end end
function onStatesChange(localPlayer, now, old) function onStatesChange(localPlayer, now, old)
if now == old then return end if now == old then return end
@ -157,17 +154,3 @@ function onStatesChange(localPlayer, now, old)
end end
end 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

View File

@ -1,6 +1,5 @@
HealthBar < ProgressBar HealthBar < ProgressBar
id: healthBar id: healthBar
height: 15
background-color: #ff4444 background-color: #ff4444
anchors.top: parent.top anchors.top: parent.top
anchors.left: parent.left anchors.left: parent.left
@ -8,48 +7,19 @@ HealthBar < ProgressBar
ManaBar < ProgressBar ManaBar < ProgressBar
id: manaBar id: manaBar
height: 15
background-color: #4444ff background-color: #4444ff
anchors.top: prev.bottom anchors.top: prev.bottom
anchors.left: parent.left anchors.left: parent.left
anchors.right: parent.right anchors.right: parent.right
margin-top: 4 margin-top: 3
ExperienceBar < ProgressBar ExperienceBar < ProgressBar
id: experienceBar id: experienceBar
height: 15
background-color: yellow background-color: yellow
anchors.top: prev.bottom anchors.top: prev.bottom
anchors.left: parent.left anchors.left: parent.left
anchors.right: parent.right anchors.right: parent.right
margin-top: 4 margin-top: 3
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
SoulLabel < GameLabel SoulLabel < GameLabel
id: soulLabel id: soulLabel
@ -61,7 +31,12 @@ SoulLabel < GameLabel
anchors.left: parent.horizontalCenter anchors.left: parent.horizontalCenter
margin-top: 5 margin-top: 5
margin-right: 3 margin-right: 3
text: Soul: on: true
$!on:
visible: false
margin-top: 0
height: 0
CapLabel < GameLabel CapLabel < GameLabel
id: capLabel id: capLabel
@ -72,7 +47,12 @@ CapLabel < GameLabel
anchors.right: parent.horizontalCenter anchors.right: parent.horizontalCenter
margin-top: 5 margin-top: 5
margin-left: 3 margin-left: 3
text: Cap: on: true
$!on:
visible: false
margin-top: 0
height: 0
ConditionWidget < UIWidget ConditionWidget < UIWidget
size: 18 18 size: 18 18
@ -84,17 +64,14 @@ MiniWindow
icon: /images/topbuttons/healthinfo icon: /images/topbuttons/healthinfo
id: healthInfoWindow id: healthInfoWindow
!text: tr('Health Info') !text: tr('Health Info')
height: 117 height: 121
@onClose: modules.game_healthinfo.onMiniWindowClose() @onClose: modules.game_healthinfo.onMiniWindowClose()
&save: true &save: true
MiniWindowContents MiniWindowContents
HealthBar HealthBar
HealthLabel
ManaBar ManaBar
ManaLabel
ExperienceBar ExperienceBar
ExperienceLabel
Panel Panel
id: conditionPanel id: conditionPanel
layout: layout:

View File

@ -592,6 +592,8 @@ void Application::registerLuaFunctions()
g_lua.bindClassMemberFunction<UIWidget>("setTextOffset", &UIWidget::setTextOffset); g_lua.bindClassMemberFunction<UIWidget>("setTextOffset", &UIWidget::setTextOffset);
g_lua.bindClassMemberFunction<UIWidget>("setTextWrap", &UIWidget::setTextWrap); g_lua.bindClassMemberFunction<UIWidget>("setTextWrap", &UIWidget::setTextWrap);
g_lua.bindClassMemberFunction<UIWidget>("setTextAutoResize", &UIWidget::setTextAutoResize); g_lua.bindClassMemberFunction<UIWidget>("setTextAutoResize", &UIWidget::setTextAutoResize);
g_lua.bindClassMemberFunction<UIWidget>("setTextVerticalAutoResize", &UIWidget::setTextVerticalAutoResize);
g_lua.bindClassMemberFunction<UIWidget>("setTextHorizontalAutoResize", &UIWidget::setTextHorizontalAutoResize);
g_lua.bindClassMemberFunction<UIWidget>("setFont", &UIWidget::setFont); g_lua.bindClassMemberFunction<UIWidget>("setFont", &UIWidget::setFont);
g_lua.bindClassMemberFunction<UIWidget>("getText", &UIWidget::getText); g_lua.bindClassMemberFunction<UIWidget>("getText", &UIWidget::getText);
g_lua.bindClassMemberFunction<UIWidget>("getDrawText", &UIWidget::getDrawText); g_lua.bindClassMemberFunction<UIWidget>("getDrawText", &UIWidget::getDrawText);

View File

@ -139,12 +139,12 @@ void UITextEdit::update(bool focusCursor)
int glyph; int glyph;
// update rect size // 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(); textBoxSize += Size(m_padding.left + m_padding.right, m_padding.top + m_padding.bottom) + m_textOffset.toSize();
Size size = getSize(); Size size = getSize();
if(size.width() <= 0 || (m_textAutoResize && !m_textWrap)) if(size.width() <= 0 || (m_textHorizontalAutoResize && !m_textWrap))
size.setWidth(textBoxSize.width()); size.setWidth(textBoxSize.width());
if(size.height() <= 0 || m_textAutoResize) if(size.height() <= 0 || m_textVerticalAutoResize)
size.setHeight(textBoxSize.height()); size.setHeight(textBoxSize.height());
setSize(size); setSize(size);
} }

View File

@ -480,7 +480,8 @@ protected:
Fw::AlignmentFlag m_textAlign; Fw::AlignmentFlag m_textAlign;
Point m_textOffset; Point m_textOffset;
stdext::boolean<false> m_textWrap; stdext::boolean<false> m_textWrap;
stdext::boolean<false> m_textAutoResize; stdext::boolean<false> m_textVerticalAutoResize;
stdext::boolean<false> m_textHorizontalAutoResize;
stdext::boolean<false> m_textOnlyUpperCase; stdext::boolean<false> m_textOnlyUpperCase;
BitmapFontPtr m_font; BitmapFontPtr m_font;
@ -492,7 +493,9 @@ public:
void setTextAlign(Fw::AlignmentFlag align) { m_textAlign = align; updateText(); } void setTextAlign(Fw::AlignmentFlag align) { m_textAlign = align; updateText(); }
void setTextOffset(const Point& offset) { m_textOffset = offset; updateText(); } void setTextOffset(const Point& offset) { m_textOffset = offset; updateText(); }
void setTextWrap(bool textWrap) { m_textWrap = textWrap; 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 setTextOnlyUpperCase(bool textOnlyUpperCase) { m_textOnlyUpperCase = textOnlyUpperCase; setText(m_text); }
void setFont(const std::string& fontName); void setFont(const std::string& fontName);

View File

@ -42,13 +42,13 @@ void UIWidget::updateText()
m_drawText = m_text; m_drawText = m_text;
// update rect size // update rect size
if(!m_rect.isValid() || m_textAutoResize) { if(!m_rect.isValid() || m_textHorizontalAutoResize || m_textVerticalAutoResize) {
Size textBoxSize = getTextSize(); Size textBoxSize = getTextSize();
textBoxSize += Size(m_padding.left + m_padding.right, m_padding.top + m_padding.bottom) + m_textOffset.toSize(); textBoxSize += Size(m_padding.left + m_padding.right, m_padding.top + m_padding.bottom) + m_textOffset.toSize();
Size size = getSize(); Size size = getSize();
if(size.width() <= 0 || (m_textAutoResize && !m_textWrap)) if(size.width() <= 0 || (m_textHorizontalAutoResize && !m_textWrap))
size.setWidth(textBoxSize.width()); size.setWidth(textBoxSize.width());
if(size.height() <= 0 || m_textAutoResize) if(size.height() <= 0 || m_textVerticalAutoResize)
size.setHeight(textBoxSize.height()); size.setHeight(textBoxSize.height());
setSize(size); setSize(size);
} }
@ -69,6 +69,10 @@ void UIWidget::parseTextStyle(const OTMLNodePtr& styleNode)
setTextWrap(node->value<bool>()); setTextWrap(node->value<bool>());
else if(node->tag() == "text-auto-resize") else if(node->tag() == "text-auto-resize")
setTextAutoResize(node->value<bool>()); setTextAutoResize(node->value<bool>());
else if(node->tag() == "text-horizontal-auto-resize")
setTextHorizontalAutoResize(node->value<bool>());
else if(node->tag() == "text-vertical-auto-resize")
setTextVerticalAutoResize(node->value<bool>());
else if(node->tag() == "text-only-upper-case") else if(node->tag() == "text-only-upper-case")
setTextOnlyUpperCase(node->value<bool>()); setTextOnlyUpperCase(node->value<bool>());
else if(node->tag() == "font") else if(node->tag() == "font")