mar173
Member
- Joined
- Aug 14, 2016
- Messages
- 34
- Reaction score
- 18
Lua:
local UIWidget
local UIToolTip
local UIGameLabel
local UIProgressBar
local Class = function(...)
local cls = {};
cls.__index = cls
cls.__call = function(_, ...)
return cls:New(...)
end
function cls:New(...)
local instance = setmetatable({}, cls)
cls.__init(instance, ...)
return instance
end
return setmetatable(cls, {
__call = cls.__call
})
end
local Inherit = function(base)
local cls = Class()
setmetatable(cls, {
__index = base
})
return cls
end
UIWidget = Class()
function UIWidget:__init(type)
self.Type = type
end
function UIWidget:SetWidget(id, parent)
local widget
if parent == nil then
parent = g_ui.getRootWidget()
widget = parent:recursiveGetChildById(id)
else
widget = parent:getChildById(id)
end
if widget == nil then
widget = g_ui.createWidget(self.Type, parent)
widget:setId(id)
end
self.Widget = widget
self.Parent = parent
end
function UIWidget:Show()
self.Widget:show()
end
function UIWidget:Raise()
self.Widget:raise()
end
function UIWidget:Enable()
self.Widget:enable()
end
function UIWidget:SetAnchors(anchors)
for anchorFrom, anchorTo in pairs(anchors) do
self.Widget:addAnchor(anchorFrom, 'parent', anchorTo)
end
end
function UIWidget:Resize(width, height)
self.Widget:resize(width, height)
end
function UIWidget:SetWidth(width)
self.Widget:setWidth(width)
end
function UIWidget:SetHeight(height)
self.Widget:setHeight(height)
end
function UIWidget:GetWidth()
return self.Widget:getWidth()
end
function UIWidget:GetHeight()
return self.Widget:getHeight()
end
function UIWidget:SetMarginTop(margin)
self.Widget:setMarginTop(margin)
end
function UIWidget:SetMarginRight(margin)
self.Widget:setMarginRight(margin)
end
function UIWidget:SetMarginBottom(margin)
self.Widget:setMarginBottom(margin)
end
function UIWidget:SetMarginLeft(margin)
self.Widget:setMarginLeft(margin)
end
UIToolTip = Inherit(UIWidget:New('UILabel'))
function UIToolTip:__init(id, parent)
self:SetWidget(id, parent)
end
function UIToolTip:SetBackgroundColor(color)
self.Widget:setBackgroundColor(color)
end
function UIToolTip:SetTextAlign(align)
self.Widget:setTextAlign(align)
end
function UIToolTip:SetText(text)
self.Widget:setText(text)
end
function UIToolTip:ResizeToText()
self.Widget:resizeToText()
end
UIGameLabel = Inherit(UIWidget:New('UILabel'))
function UIGameLabel:__init(id, parent)
self:SetWidget(id, parent)
end
function UIGameLabel:SetFont(font)
self.Widget:setFont(font)
end
function UIGameLabel:SetText(text)
self.Widget:setText(text)
end
UIProgressBar = Inherit(UIWidget:New('ProgressBar'))
function UIProgressBar:__init(id, parent)
self:SetWidget(id, parent)
end
function UIProgressBar:SetBackgroundColor(color)
self.Widget:setBackgroundColor(color)
end
function UIProgressBar:SetPhantom(phantom)
self.Widget:setPhantom(phantom)
end
function UIProgressBar:SetPercent(percent)
self.Widget:setPercent(percent)
end
-----------------------
local MARGIN_TOP = 30
local MARGIN_RIGHT = 50
local LevelToolTip
local LevelProgressBar
local LevelLabel
local LevelLabelTitle
local LevelLabelValue
LevelToolTip = UIToolTip:New('LevelToolTip', modules.game_interface.getBottomPanel())
LevelToolTip:SetBackgroundColor('#111111cc')
LevelToolTip:SetTextAlign(modules.corelib.AlignCenter)
LevelToolTip:SetAnchors({
-- [modules.corelib.AnchorLeft]=modules.corelib.AnchorLeft,
[modules.corelib.AnchorRight] = modules.corelib.AnchorRight,
[modules.corelib.AnchorTop] = modules.corelib.AnchorTop
-- [modules.corelib.AnchorBottom]=modules.corelib.AnchorBottom,
})
LevelToolTip:SetMarginTop(MARGIN_TOP + 30)
LevelToolTip:SetMarginRight(MARGIN_RIGHT)
LevelProgressBar = UIProgressBar:New('LevelProgressBar', modules.game_interface.getBottomPanel())
LevelProgressBar:SetHeight(5)
LevelProgressBar:SetPhantom(false)
LevelProgressBar:SetBackgroundColor('#c21111')
LevelProgressBar:SetAnchors({
-- [modules.corelib.AnchorLeft]=modules.corelib.AnchorLeft,
[modules.corelib.AnchorRight] = modules.corelib.AnchorRight,
[modules.corelib.AnchorTop] = modules.corelib.AnchorTop
-- [modules.corelib.AnchorBottom]=modules.corelib.AnchorBottom,
})
LevelProgressBar:SetMarginTop(MARGIN_TOP + 17)
LevelProgressBar:SetMarginRight(MARGIN_RIGHT + 15)
LevelLabel = UIGameLabel:New('LevelLabel', modules.game_interface.getBottomPanel())
LevelLabel:SetAnchors({
-- [modules.corelib.AnchorLeft]=modules.corelib.AnchorLeft,
[modules.corelib.AnchorRight] = modules.corelib.AnchorRight,
[modules.corelib.AnchorTop] = modules.corelib.AnchorTop
-- [modules.corelib.AnchorBottom]=modules.corelib.AnchorBottom,
})
LevelLabel:SetMarginTop(MARGIN_TOP)
LevelLabel:SetMarginRight(MARGIN_RIGHT + 15)
LevelLabelTitle = UIGameLabel:New('LevelLabelTitle', LevelLabel.Widget)
LevelLabelTitle:SetText('Level')
LevelLabelTitle:SetFont('verdana-11px-monochrome')
LevelLabelTitle:SetAnchors({
[modules.corelib.AnchorLeft] = modules.corelib.AnchorLeft,
-- [modules.corelib.AnchorRight]=modules.corelib.AnchorRight,
[modules.corelib.AnchorTop] = modules.corelib.AnchorTop,
[modules.corelib.AnchorBottom] = modules.corelib.AnchorBottom
})
LevelLabelValue = UIGameLabel:New('LevelLabelValue', LevelLabel.Widget)
LevelLabelValue:SetText('')
LevelLabelValue:SetFont('verdana-11px-monochrome')
LevelLabelValue:SetAnchors({
-- [modules.corelib.AnchorLeft]=modules.corelib.AnchorLeft,
[modules.corelib.AnchorRight] = modules.corelib.AnchorRight,
[modules.corelib.AnchorTop] = modules.corelib.AnchorTop,
[modules.corelib.AnchorBottom] = modules.corelib.AnchorBottom
})
local function round2(num, numDecimalPlaces)
return string.format("%." .. (numDecimalPlaces or 0) .. "f", num)
end
local function expForLevel(level)
return math.floor((50 * level * level * level) / 3 - 100 * level * level + (850 * level) / 3 - 200)
end
local function getLevelPercent(lvl, exp, calculatedExp)
calculatedExp = calculatedExp or expForLevel(lvl + 1)
local expToNextLevelTotal = calculatedExp - expForLevel(lvl)
local expToNextLevel = calculatedExp - exp
return 100 - (expToNextLevel / expToNextLevelTotal * 100)
end
local function onLevelChange()
local expPerHour = 0
local hoursLeft = 0
local minutesLeft = 0
local currentExp = player:getExperience()
local currentTime = now * 0.001
local currentLevel = player:getLevel()
local totalExpToNextLvl = expForLevel(currentLevel + 1)
if player.lastExps ~= nil then
local expSpeed = (currentExp - player.lastExps[1][1]) / (currentTime - player.lastExps[1][2])
expPerHour = math.floor(expSpeed * 3600)
if expPerHour > 0 then
hoursLeft = (totalExpToNextLvl - currentExp) / expPerHour
minutesLeft = math.floor((hoursLeft - math.floor(hoursLeft)) * 60)
hoursLeft = math.floor(hoursLeft)
end
end
local text = tr('You have %s percent', round2(getLevelPercent(currentLevel, currentExp, totalExpToNextLvl), 2)) -- player:getLevelPercent())
text = text .. '\n' .. tr('%skk of experience per hour', round2(expPerHour * 0.000001, 2))
text = text .. '\n' .. tr('Next level in %d hours and %d minutes', hoursLeft, minutesLeft)
LevelProgressBar:SetPercent(math.floor(player:getLevelPercent()))
LevelToolTip:SetText(text)
LevelToolTip:ResizeToText()
local width = LevelToolTip:GetWidth()
local height = LevelToolTip:GetHeight()
LevelToolTip:Resize(width + 4, height + 4)
LevelProgressBar:SetWidth(width - 30)
LevelLabel:SetWidth(width - 30)
LevelLabelValue:SetText(currentLevel)
LevelToolTip:Show()
LevelToolTip:Raise()
LevelToolTip:Enable()
end
macro(1000, function()
onLevelChange()
end)
or if u have errors:
Lua:
local MARGIN_TOP = 30
local MARGIN_RIGHT = 50
local levelToolTip
local levelProgressBar
local levelTitleLabel
local levelTitleLabelName
local levelTitleLabelValue
local function getTooltip(id, anchors, color, align, parent)
local widget
if parent == nil then
parent = g_ui.getRootWidget()
widget = parent:recursiveGetChildById(id)
else
widget = parent:getChildById(id)
end
if widget == nil then
widget = g_ui.createWidget('UILabel', parent)
widget:setId(id)
if color then
widget:setBackgroundColor(color)
end
if align then
widget:setTextAlign(align)
end
for anchorFrom, anchorTo in pairs(anchors) do
widget:addAnchor(anchorFrom, 'parent', anchorTo)
end
end
return widget
end
local function getGameLabel(id, font, anchors, parent)
local widget
if parent == nil then
parent = g_ui.getRootWidget()
widget = parent:recursiveGetChildById(id)
else
widget = parent:getChildById(id)
end
if widget == nil then
widget = g_ui.createWidget('UILabel', parent)
widget:setId(id)
for anchorFrom, anchorTo in pairs(anchors) do
widget:addAnchor(anchorFrom, 'parent', anchorTo)
end
if font then
widget:setFont(font)
end
end
return widget
end
local function getProgressBar(id, color, height, anchors, phantom, parent)
local widget
if parent == nil then
parent = g_ui.getRootWidget()
widget = parent:recursiveGetChildById(id)
else
widget = parent:getChildById(id)
end
if widget == nil then
widget = g_ui.createWidget('ProgressBar', parent)
widget:setId(id)
widget:setBackgroundColor(color)
widget:setHeight(height)
for anchorFrom, anchorTo in pairs(anchors) do
widget:addAnchor(anchorFrom, 'parent', anchorTo)
end
widget:setPhantom(phantom)
end
return widget
end
levelToolTip = getTooltip('levelToolTip', {
--[modules.corelib.AnchorLeft]=modules.corelib.AnchorLeft,
[modules.corelib.AnchorRight]=modules.corelib.AnchorRight,
[modules.corelib.AnchorTop]=modules.corelib.AnchorTop,
--[modules.corelib.AnchorBottom]=modules.corelib.AnchorBottom,
}, '#111111cc', modules.corelib.AlignCenter, modules.game_interface.getBottomPanel())
levelToolTip:setMarginTop(MARGIN_TOP + 30)
levelToolTip:setMarginRight(MARGIN_RIGHT)
levelProgressBar = getProgressBar('levelProgressBar', '#c21111', 5, {
--[modules.corelib.AnchorLeft]=modules.corelib.AnchorLeft,
[modules.corelib.AnchorRight]=modules.corelib.AnchorRight,
[modules.corelib.AnchorTop]=modules.corelib.AnchorTop,
--[modules.corelib.AnchorBottom]=modules.corelib.AnchorBottom,
}, false, modules.game_interface.getBottomPanel())
levelProgressBar:setMarginTop(MARGIN_TOP + 17)
levelProgressBar:setMarginRight(MARGIN_RIGHT + 15)
levelTitleLabel = getGameLabel('levelTitleLabel', nil, {
--[modules.corelib.AnchorLeft]=modules.corelib.AnchorLeft,
[modules.corelib.AnchorRight]=modules.corelib.AnchorRight,
[modules.corelib.AnchorTop]=modules.corelib.AnchorTop,
--[modules.corelib.AnchorBottom]=modules.corelib.AnchorBottom,
}, modules.game_interface.getBottomPanel())
levelTitleLabel:setMarginTop(MARGIN_TOP)
levelTitleLabel:setMarginRight(MARGIN_RIGHT + 15)
levelTitleLabelName = getGameLabel('levelTitleLabelName', 'verdana-11px-monochrome', {
[modules.corelib.AnchorLeft]=modules.corelib.AnchorLeft,
--[modules.corelib.AnchorRight]=modules.corelib.AnchorRight,
[modules.corelib.AnchorTop]=modules.corelib.AnchorTop,
[modules.corelib.AnchorBottom]=modules.corelib.AnchorBottom,
}, levelTitleLabel)
levelTitleLabelName:setText('Level')
levelTitleLabelValue = getGameLabel('levelTitleLabelValue', 'verdana-11px-monochrome', {
--[modules.corelib.AnchorLeft]=modules.corelib.AnchorLeft,
[modules.corelib.AnchorRight]=modules.corelib.AnchorRight,
[modules.corelib.AnchorTop]=modules.corelib.AnchorTop,
[modules.corelib.AnchorBottom]=modules.corelib.AnchorBottom,
}, levelTitleLabel)
local function round2(num, numDecimalPlaces)
return string.format("%." .. (numDecimalPlaces or 0) .. "f", num)
end
local function expForLevel(level)
return math.floor((50*level*level*level)/3 - 100*level*level + (850*level)/3 - 200)
end
local function getLevelPercent(lvl, exp, calculatedExp)
calculatedExp = calculatedExp or expForLevel(lvl+1)
local expToNextLevelTotal = calculatedExp - expForLevel(lvl)
local expToNextLevel = calculatedExp - exp
return 100 - (expToNextLevel / expToNextLevelTotal * 100)
end
local function onLevelChange()
local expPerHour = 0
local hoursLeft = 0
local minutesLeft = 0
local currentExp = player:getExperience()
local currentTime = now * 0.001
local currentLevel = player:getLevel()
local totalExpToNextLvl = expForLevel(currentLevel + 1)
if player.lastExps ~= nil then
local expSpeed = (currentExp - player.lastExps[1][1]) / (currentTime - player.lastExps[1][2])
expPerHour = math.floor(expSpeed * 3600)
if expPerHour > 0 then
hoursLeft = (totalExpToNextLvl - currentExp) / expPerHour
minutesLeft = math.floor((hoursLeft - math.floor(hoursLeft)) * 60)
hoursLeft = math.floor(hoursLeft)
end
end
local text = tr('You have %s percent', round2(getLevelPercent(currentLevel,currentExp,totalExpToNextLvl), 2))--player:getLevelPercent())
text = text .. '\n' .. tr('%skk of experience per hour', round2(expPerHour * 0.000001, 2))
text = text .. '\n' .. tr('Next level in %d hours and %d minutes', hoursLeft, minutesLeft)
levelProgressBar:setPercent(math.floor(player:getLevelPercent()))
levelToolTip:setText(text)
levelToolTip:resizeToText()
local width = levelToolTip:getWidth()
local height = levelToolTip:getHeight()
levelToolTip:resize(width + 4, height + 4)
levelProgressBar:setWidth(width - 30)
levelTitleLabel:setWidth(width - 30)
levelTitleLabelValue:setText(currentLevel)
levelToolTip:show()
levelToolTip:raise()
levelToolTip:enable()
end
macro(1000, function()
onLevelChange()
end)
Last edited: