From 67df3a0723832dccb8d926503839b99c17590207 Mon Sep 17 00:00:00 2001 From: Henrique Santiago Date: Thu, 22 Dec 2011 02:06:00 -0200 Subject: [PATCH] skill bar improvements --- modules/game_healthbar/healthbar.lua | 8 +- modules/game_skills/skills.lua | 135 +++++++++++++++-------- modules/game_skills/skills.otui | 146 +++++++++++++++++++++++-- src/otclient/net/protocolgameparse.cpp | 19 +++- 4 files changed, 244 insertions(+), 64 deletions(-) diff --git a/modules/game_healthbar/healthbar.lua b/modules/game_healthbar/healthbar.lua index 237296b4..ebc5202c 100644 --- a/modules/game_healthbar/healthbar.lua +++ b/modules/game_healthbar/healthbar.lua @@ -38,7 +38,7 @@ function HealthBar.destroy() end -- hooked events -function Game.onHealthChange(health, maxHealth) +function HealthBar.onHealthChange(health, maxHealth) local label = healthManaPanel:getChildById('healthLabel') label:setText(health .. ' / ' .. maxHealth) @@ -46,7 +46,7 @@ function Game.onHealthChange(health, maxHealth) healthBar:setPercent(health / maxHealth * 100) end -function Game.onManaChange(mana, maxMana) +function HealthBar.onManaChange(mana, maxMana) local label = healthManaPanel:getChildById('manaLabel') label:setText(mana .. ' / ' .. maxMana) @@ -62,4 +62,6 @@ function Game.onManaChange(mana, maxMana) end connect(Game, { onLogin = HealthBar.create, - onLogout = HealthBar.destroy }) + onLogout = HealthBar.destroy, + onHealthChange = HealthBar.onHealthChange, + onManaChange = HealthBar.onManaChange }) diff --git a/modules/game_skills/skills.lua b/modules/game_skills/skills.lua index bf018cc1..b3d70519 100644 --- a/modules/game_skills/skills.lua +++ b/modules/game_skills/skills.lua @@ -2,66 +2,109 @@ Skills = {} -- private variables local skillWindow = nil -local skills = {"Fist Fighting", "Club Fighting", "Sword Fighting", "Axe Fighting", "Distance Fighting", "Shielding", "Fishing"} --- public functions -function Skills.create() - skillWindow = UI.display('skills.otui', { parent = Game.gameRightPanel }) +-- private functions +local function getNumberString(number) + local out = '' + number = tostring(number):reverse() + for i=1,#number do + out = out .. number:sub(i, i) + if i % 3 == 0 and i ~= #number then + out = out .. ',' + end + end + out = out:reverse() + return out +end - local skillPanel = skillWindow:getChildById('skillPanel') - - -- create first widget cause of layout - local widget = UIWidget.create() - skillPanel:addChild(widget) - widget:setStyle('SkillFirstWidget') - - -- create skills - for i=1,#skills,1 do - local skillButton = UIButton.create() - skillPanel:addChild(skillButton) - skillButton:setStyle('SkillButton') - - local nameLabel = UILabel.create() - skillButton :addChild(nameLabel) - nameLabel:setStyle('SkillNameLabel') - nameLabel:setText(skills[i]) - nameLabel:resizeToText() - - local levelLabel = UILabel.create() - skillButton:addChild(levelLabel) - levelLabel:setStyle('SkillLevelLabel') - levelLabel:setId('skillLevelId' .. i) - levelLabel:setText('0') - - local percentBar = UIProgressBar.create() - skillPanel:addChild(percentBar) - percentBar:setStyle('SkillPercentPanel') - percentBar:setId('skillPercentId' .. i) - - skillButton.onClick = function(self) - percentBar:setVisible(not percentBar:isVisible()) - self:updateParentLayout() - return true +local function setSkillValue(id, value) + local skill = skillWindow:recursiveGetChildById(id) + + if skill then + local widget = skill:getChildById('value') + widget:setText(value) + end +end + +local function setSkillPercent(id, percent, tooltip) + local skill = skillWindow:recursiveGetChildById(id) + + if skill then + local widget = skill:getChildById('percent') + widget:setPercent(percent) + + if tooltip then + widget:setTooltip(tooltip) end end end +-- public functions +function Skills.create() + skillWindow = UI.display('skills.otui', { parent = Game.gameRightPanel }) +end + function Skills.destroy() skillWindow:destroy() skillWindow = nil end -- hooked events -function Game.onSkillUpdate(id, level, percent) - local skillPanel = skillWindow:recursiveGetChildById('skillPanel') +function Skills.onExperienceChange(value) + setSkillValue('experience', getNumberString(value)) +end - local levelLabel = skillPanel:recursiveGetChildById('skillLevelId' .. (id + 1)) - levelLabel:setText(level) +function Skills.onLevelChange(value, percent) + setSkillValue('level', getNumberString(value)) + setSkillPercent('level', percent, 'You have ' .. (100 - percent) .. ' percent to go') +end + +function Skills.onHealthChange(health, maxHealth) + setSkillValue('health', getNumberString(health)) +end + +function Skills.onManaChange(mana, maxMana) + setSkillValue('mana', getNumberString(mana)) +end + +function Skills.onSoulChange(soul) + setSkillValue('soul', soul) +end + +function Skills.onFreeCapacityChange(freeCapacity) + setSkillValue('capacity', freeCapacity) +end + +function Skills.onStaminaChange(stamina) + local hours = math.floor(stamina / 60) + local minutes = stamina % 60 + if minutes < 10 then + minutes = '0' .. minutes + end + local percent = 100 * stamina / (42 * 60) -- max is 42 hours + + setSkillValue('stamina', hours .. ":" .. minutes) + setSkillPercent('stamina', percent, 'You have ' .. percent .. ' percent') +end + +function Skills.onMagicLevelChange(value, percent) + setSkillValue('magiclevel', value) + setSkillPercent('magiclevel', percent, 'You have ' .. (100 - percent) .. ' percent to go') +end - local percentBar = skillPanel:getChildById('skillPercentId' .. (id + 1)) - percentBar:setPercent(percent) - percentBar:setTooltip(percent .. "% to go") +function Skills.onSkillChange(id, level, percent) + setSkillValue('skillId' .. id, level) + setSkillPercent('skillId' .. id, percent, 'You have ' .. (100 - percent) .. ' percent to go') end connect(Game, { onLogin = Skills.create, - onLogout = Skills.destroy }) + onLogout = Skills.destroy, + onExperienceChange = Skills.onExperienceChange, + onLevelChange = Skills.onLevelChange, + onHealthChange = Skills.onHealthChange, + onManaChange = Skills.onManaChange, + onSoulChange = Skills.onSoulChange, + onFreeCapacityChange = Skills.onFreeCapacityChange, + onStaminaChange = Skills.onStaminaChange, + onMagicLevelChange = Skills.onMagicLevelChange, + onSkillChange = Skills.onSkillChange }) diff --git a/modules/game_skills/skills.otui b/modules/game_skills/skills.otui index 5e6ab1bb..a88f2a34 100644 --- a/modules/game_skills/skills.otui +++ b/modules/game_skills/skills.otui @@ -1,10 +1,24 @@ SkillFirstWidget < UIWidget SkillButton < UIButton - height: 14 - margin-top: 4 + height: 21 + margin-top: 3 margin-left: 10 margin-right: 10 + + @onClick: | + function(self) + local percentBar = self:getChildById('percent') + if percentBar then + percentBar:setVisible(not percentBar:isVisible()) + if percentBar:isVisible() then + self:setHeight(21) + else + self:setHeight(21 - 6) + end + self:updateParentLayout() + end + end SkillNameLabel < Label font: verdana-11px-monochrome @@ -12,28 +26,31 @@ SkillNameLabel < Label anchors.top: parent.top anchors.bottom: parent.bottom -SkillLevelLabel < Label +SkillValueLabel < Label + id: value font: verdana-11px-monochrome - text-align: right - width: 32 + text-align: topright + width: 64 anchors.right: parent.right anchors.top: parent.top anchors.bottom: parent.bottom SkillPercentPanel < UIProgressBar + id: percent color: black background-color: green - tooltip: test height: 5 - margin-top: 3 - margin-left: 10 - margin-right: 10 + margin-top: 15 + anchors.left: parent.left + anchors.right: parent.right + anchors.top: parent.top + tooltip: 0 MiniWindow id: skillWindow title: Skills size: 200 220 - + Panel id: skillPanel anchors.fill: parent @@ -42,3 +59,112 @@ MiniWindow margin-left: 3 margin-right: 3 layout: verticalBox + + SkillButton + id: experience + height: 15 + SkillNameLabel + text: Experience + SkillValueLabel + + SkillButton + id: level + SkillNameLabel + text: Level + SkillValueLabel + SkillPercentPanel + background-color: red + + SkillButton + id: health + height: 15 + SkillNameLabel + text: Hit Points + SkillValueLabel + + SkillButton + id: mana + height: 15 + SkillNameLabel + text: Mana + SkillValueLabel + + SkillButton + id: soul + height: 15 + SkillNameLabel + text: Soul Points + SkillValueLabel + + SkillButton + id: capacity + height: 15 + SkillNameLabel + text: Capacity + SkillValueLabel + + SkillButton + id: stamina + SkillNameLabel + text: Stamina + SkillValueLabel + SkillPercentPanel + + SkillButton + id: magiclevel + SkillNameLabel + text: Magic Level + SkillValueLabel + SkillPercentPanel + background-color: red + + SkillButton + id: skillId0 + SkillNameLabel + text: Fist Fighting + SkillValueLabel + SkillPercentPanel + + SkillButton + id: skillId1 + SkillNameLabel + text: Club Fighting + SkillValueLabel + SkillPercentPanel + + SkillButton + id: skillId2 + + SkillNameLabel + text: Sword Fighting + SkillValueLabel + SkillPercentPanel + + SkillButton + id: skillId3 + SkillNameLabel + text: Axe Fighting + SkillValueLabel + SkillPercentPanel + + SkillButton + id: skillId4 + SkillNameLabel + text: Distance Fighting + SkillValueLabel + SkillPercentPanel + + SkillButton + id: skillId5 + SkillNameLabel + text: Shielding + SkillValueLabel + SkillPercentPanel + + SkillButton + id: skillId6 + SkillNameLabel + text: Fishing + SkillValueLabel + SkillPercentPanel + diff --git a/src/otclient/net/protocolgameparse.cpp b/src/otclient/net/protocolgameparse.cpp index 813612fc..b68c5d56 100644 --- a/src/otclient/net/protocolgameparse.cpp +++ b/src/otclient/net/protocolgameparse.cpp @@ -660,38 +660,47 @@ void ProtocolGame::parsePlayerStats(InputMessage& msg) { m_localPlayer->setStatistic(Otc::Health, msg.getU16()); m_localPlayer->setStatistic(Otc::MaxHealth, msg.getU16()); - g_dispatcher.addEvent([=] { g_lua.callGlobalField("Game", "onHealthChange", m_localPlayer->getStatistic(Otc::Health), m_localPlayer->getStatistic(Otc::MaxHealth)); }); m_localPlayer->setStatistic(Otc::FreeCapacity, msg.getU32() / 100.0); - g_dispatcher.addEvent([=] { g_lua.callGlobalField("Game", "onFreeCapacityChange", m_localPlayer->getStatistic(Otc::FreeCapacity)); }); m_localPlayer->setStatistic(Otc::Experience, msg.getU32()); + g_dispatcher.addEvent([=] { + g_lua.callGlobalField("Game", "onExperienceChange", m_localPlayer->getStatistic(Otc::Experience)); + }); + m_localPlayer->setStatistic(Otc::Level, msg.getU16()); m_localPlayer->setStatistic(Otc::LevelPercent, msg.getU8()); + g_dispatcher.addEvent([=] { + g_lua.callGlobalField("Game", "onLevelChange", m_localPlayer->getStatistic(Otc::Level), m_localPlayer->getStatistic(Otc::LevelPercent)); + }); m_localPlayer->setStatistic(Otc::Mana, msg.getU16()); m_localPlayer->setStatistic(Otc::MaxMana, msg.getU16()); - g_dispatcher.addEvent([=] { g_lua.callGlobalField("Game", "onManaChange", m_localPlayer->getStatistic(Otc::Mana), m_localPlayer->getStatistic(Otc::MaxMana)); }); m_localPlayer->setStatistic(Otc::MagicLevel, msg.getU8()); m_localPlayer->setStatistic(Otc::MagicLevelPercent, msg.getU8()); + g_dispatcher.addEvent([=] { + g_lua.callGlobalField("Game", "onMagicLevelChange", m_localPlayer->getStatistic(Otc::MagicLevel), m_localPlayer->getStatistic(Otc::MagicLevelPercent)); + }); m_localPlayer->setStatistic(Otc::Soul, msg.getU8()); - g_dispatcher.addEvent([=] { g_lua.callGlobalField("Game", "onSoulChange", m_localPlayer->getStatistic(Otc::Soul)); }); m_localPlayer->setStatistic(Otc::Stamina, msg.getU16()); + g_dispatcher.addEvent([=] { + g_lua.callGlobalField("Game", "onStaminaChange", m_localPlayer->getStatistic(Otc::Stamina)); + }); } void ProtocolGame::parsePlayerSkills(InputMessage& msg) @@ -704,7 +713,7 @@ void ProtocolGame::parsePlayerSkills(InputMessage& msg) } g_dispatcher.addEvent([=] { - g_lua.callGlobalField("Game", "onSkillUpdate", skill, values[Otc::SkillLevel], values[Otc::SkillPercent]); + g_lua.callGlobalField("Game", "onSkillChange", skill, values[Otc::SkillLevel], values[Otc::SkillPercent]); }); } }