diff --git a/modules/client_entergame/characterlist.lua b/modules/client_entergame/characterlist.lua index b580d837..85c400bd 100644 --- a/modules/client_entergame/characterlist.lua +++ b/modules/client_entergame/characterlist.lua @@ -6,6 +6,8 @@ local loadBox local characterList local errorBox local waitingWindow +local updateWaitEvent +local resendWaitEvent -- private functions local function tryLogin(charInfo, tries) @@ -41,11 +43,12 @@ local function tryLogin(charInfo, tries) Settings.set('lastUsedCharacter', charInfo.characterName) end -local function updateWait() +local function updateWait(timeStart, timeEnd) if waitingWindow then - if waitingWindow.elapsedTime <= waitingWindow.totalTime then - local percent = (waitingWindow.elapsedTime / waitingWindow.totalTime) * 100 - local timeStr = string.format("%.0f", (waitingWindow.totalTime - waitingWindow.elapsedTime)) + local time = g_clock.seconds() + if time <= timeEnd then + local percent = ((time - timeStart) / (timeEnd - timeStart)) * 100 + local timeStr = string.format("%.0f", timeEnd - time) local progressBar = waitingWindow:getChildById('progressBar') progressBar:setPercent(percent) @@ -53,10 +56,15 @@ local function updateWait() local label = waitingWindow:getChildById('timeLabel') label:setText('Trying to reconnect in ' .. timeStr .. ' seconds.') - waitingWindow.elapsedTime = waitingWindow.elapsedTime + (waitingWindow.totalTime / 100) - scheduleEvent(updateWait, (waitingWindow.totalTime / 100) * 1000) + updateWaitEvent = scheduleEvent(function() updateWait(timeStart, timeEnd) end, 1000 * progressBar:getPercentPixels() / 100 * (timeEnd - timeStart)) + return true end end + + if updateWaitEvent then + updateWaitEvent:cancel() + updateWaitEvent = nil + end end local function resendWait() @@ -64,6 +72,11 @@ local function resendWait() waitingWindow:destroy() waitingWindow = nil + if updateWaitEvent then + updateWaitEvent:cancel() + updateWaitEvent = nil + end + if charactersWindow then local selected = charactersWindow:getChildById('characterList'):getFocusedChild() if selected then @@ -77,16 +90,15 @@ local function resendWait() end local function onLoginWait(message, time) + CharacterList.destroyLoadBox() + waitingWindow = displayUI('waitinglist.otui') local label = waitingWindow:getChildById('infoLabel') label:setText(message) - waitingWindow.elapsedTime = 0 - waitingWindow.totalTime = time - - scheduleEvent(updateWait, 0) - scheduleEvent(resendWait, time * 1000) + updateWaitEvent = scheduleEvent(function() updateWait(g_clock.seconds(), g_clock.seconds() + time) end, 0) + resendWaitEvent = scheduleEvent(resendWait, time * 1000) end local function onCharactersWindowKeyPress(self, keyCode, keyboardModifiers) @@ -151,6 +163,22 @@ function CharacterList.terminate() loadBox:destroy() loadBox = nil end + + if waitingWindow then + waitingWindow:destroy() + waitingWindow = nil + end + + if updateWaitEvent then + updateWaitEvent:cancel() + updateWaitEvent = nil + end + + if resendWaitEvent then + resendWaitEvent:cancel() + resendWaitEvent = nil + end + CharacterList = nil end @@ -245,7 +273,18 @@ function CharacterList.cancelWait() if waitingWindow then waitingWindow:destroy() waitingWindow = nil - CharacterList.destroyLoadBox() - CharacterList.showAgain() end + + if updateWaitEvent then + updateWaitEvent:cancel() + updateWaitEvent = nil + end + + if resendWaitEvent then + resendWaitEvent:cancel() + resendWaitEvent = nil + end + + CharacterList.destroyLoadBox() + CharacterList.showAgain() end diff --git a/modules/core_lib/math/math.lua b/modules/core_lib/math/math.lua new file mode 100644 index 00000000..2492fab1 --- /dev/null +++ b/modules/core_lib/math/math.lua @@ -0,0 +1,8 @@ +function math.round(num, idp) + local mult = 10^(idp or 0) + if num >= 0 then + return math.floor(num * mult + 0.5) / mult + else + return math.ceil(num * mult - 0.5) / mult + end +end \ No newline at end of file diff --git a/modules/core_lib/widgets/uiprogressbar.lua b/modules/core_lib/widgets/uiprogressbar.lua index dceb0beb..efe45eb4 100644 --- a/modules/core_lib/widgets/uiprogressbar.lua +++ b/modules/core_lib/widgets/uiprogressbar.lua @@ -19,8 +19,12 @@ function UIProgressBar:getPercent() return self.percent end +function UIProgressBar:getPercentPixels() + return 100 / self:getWidth() +end + function UIProgressBar:updateBackground() - local width = math.max((self.percent * self:getWidth())/100, 1) + local width = math.round(math.max((self.percent * self:getWidth())/100, 1)) local height = self:getHeight() self:setBackgroundSize({width=width, height=height}) end diff --git a/src/framework/luafunctions.cpp b/src/framework/luafunctions.cpp index 1293e6cb..2bb1e4cc 100644 --- a/src/framework/luafunctions.cpp +++ b/src/framework/luafunctions.cpp @@ -52,6 +52,11 @@ void Application::registerLuaFunctions() g_lua.bindClassStaticFunction("g_crypt", "encrypt", Crypt::encrypt); g_lua.bindClassStaticFunction("g_crypt", "decrypt", Crypt::decrypt); + g_lua.registerStaticClass("g_clock"); + g_lua.bindClassStaticFunction("g_clock", "micros", std::bind(&Clock::micros, &g_clock)); + g_lua.bindClassStaticFunction("g_clock", "millis", std::bind(&Clock::millis, &g_clock)); + g_lua.bindClassStaticFunction("g_clock", "seconds", std::bind(&Clock::seconds, &g_clock)); + // Event g_lua.registerClass(); g_lua.bindClassMemberFunction("cancel", &Event::cancel);