From 317544ceaceaacee4e1db699d9116af284e51c0c Mon Sep 17 00:00:00 2001
From: dg <dg@51575b47-30f0-44d4-a5cc-537603b46e54>
Date: Fri, 12 Aug 2011 16:12:41 +0000
Subject: [PATCH] more progressbar goodness

git-svn-id: http://svn.net-core.org/repos/t-engine4@4122 51575b47-30f0-44d4-a5cc-537603b46e54
---
 game/engines/default/engine/Savefile.lua  | 20 +++++++++++++----
 game/engines/default/engine/ui/Dialog.lua |  4 ++--
 game/engines/default/engine/ui/Waiter.lua | 26 +++++++++++++++++++----
 src/wait.c                                |  3 +++
 4 files changed, 43 insertions(+), 10 deletions(-)

diff --git a/game/engines/default/engine/Savefile.lua b/game/engines/default/engine/Savefile.lua
index c3b69caac2..66abf2b549 100644
--- a/game/engines/default/engine/Savefile.lua
+++ b/game/engines/default/engine/Savefile.lua
@@ -269,7 +269,8 @@ function _M:saveZone(zone, no_dialog)
 	core.display.forceRedraw()
 
 	local zip = fs.zipOpen(self.save_dir..self:nameSaveZone(zone)..".tmp")
-	self:saveObject(zone, zip)
+	local nb = self:saveObject(zone, zip)
+	zip:add("nb", tostring(nb))
 	zip:close()
 	fs.delete(self.save_dir..self:nameSaveZone(zone))
 	fs.rename(self.save_dir..self:nameSaveZone(zone)..".tmp", self.save_dir..self:nameSaveZone(zone))
@@ -297,7 +298,8 @@ function _M:saveLevel(level, no_dialog)
 	core.display.forceRedraw()
 
 	local zip = fs.zipOpen(self.save_dir..self:nameSaveLevel(level)..".tmp")
-	self:saveObject(level, zip)
+	local nb = self:saveObject(level, zip)
+	zip:add("nb", tostring(nb))
 	zip:close()
 	fs.delete(self.save_dir..self:nameSaveLevel(level))
 	fs.rename(self.save_dir..self:nameSaveLevel(level)..".tmp", self.save_dir..self:nameSaveLevel(level))
@@ -325,7 +327,8 @@ function _M:saveEntity(e, no_dialog)
 	core.display.forceRedraw()
 
 	local zip = fs.zipOpen(self.save_dir..self:nameSaveEntity(e)..".tmp")
-	self:saveObject(e, zip)
+	local nb = self:saveObject(e, zip)
+	zip:add("nb", tostring(nb))
 	zip:close()
 	fs.delete(self.save_dir..self:nameSaveEntity(e))
 	fs.rename(self.save_dir..self:nameSaveEntity(e)..".tmp", self.save_dir..self:nameSaveEntity(e))
@@ -464,7 +467,15 @@ function _M:loadZone(zone)
 
 	fs.mount(path, self.load_dir)
 
-	local popup = Dialog:simpleWaiter("Loading zone", "Please wait while loading the zone...")
+	local f = fs.open(self.load_dir.."nb", "r")
+	local nb = 0
+	if f then
+		nb = tonumber(f:read()) or 100
+		f:close()
+	end
+
+	local popup = Dialog:simpleWaiter("Loading zone", "Please wait while loading the zone...", nil, nil, nb > 0 and nb)
+	core.wait.enableManualTick(true)
 	core.display.forceRedraw()
 
 	local loadedZone = self:loadReal("main")
@@ -475,6 +486,7 @@ function _M:loadZone(zone)
 		o:loaded()
 	end
 
+	core.wait.enableManualTick(false)
 	popup:done()
 
 	fs.umount(path)
diff --git a/game/engines/default/engine/ui/Dialog.lua b/game/engines/default/engine/ui/Dialog.lua
index def1739d64..b8653ff1a7 100644
--- a/game/engines/default/engine/ui/Dialog.lua
+++ b/game/engines/default/engine/ui/Dialog.lua
@@ -25,11 +25,11 @@ local Base = require "engine.ui.Base"
 module(..., package.seeall, class.inherit(Base))
 
 --- Requests a simple waiter dialog
-function _M:simpleWaiter(title, text, width, count)
+function _M:simpleWaiter(title, text, width, count, max)
 	width = width or 400
 	local w, h = self.font:size(text)
 	local d = new(title, 1, 1)
-	local wait = require("engine.ui.Waiter").new{size=width}
+	local wait = require("engine.ui.Waiter").new{size=width, known_max=max}
 	d:loadUI{
 		{left = 3, top = 3, ui=require("engine.ui.Textzone").new{width=w+10, height=h+5, text=text}},
 		{left = 3, bottom = 3, ui=wait},
diff --git a/game/engines/default/engine/ui/Waiter.lua b/game/engines/default/engine/ui/Waiter.lua
index 376cb871dd..f9486dd00d 100644
--- a/game/engines/default/engine/ui/Waiter.lua
+++ b/game/engines/default/engine/ui/Waiter.lua
@@ -25,6 +25,7 @@ module(..., package.seeall, class.inherit(Base))
 
 function _M:init(t)
 	self.size = assert(t.size, "no waiter size")
+	self.known_max = t.known_max
 
 	Base.init(self, t)
 end
@@ -50,7 +51,10 @@ function _M:getWaitDisplay(d)
 
 	return function()
 		local dx, dy, dw, dh = self.dx + d.ui_by_ui[self].x + d.display_x, d.ui_by_ui[self].y + d.display_y, self.size, self.h
-		local i, max, dir = 0, 20, 1
+		local has_max = self.known_max
+		if has_max then core.wait.addMaxTicks(has_max) end
+		local i, max, dir = 0, has_max or 20, 1
+
 		local left = {core.display.loadImage("/data/gfx/waiter/left_basic.png"):glTexture()}
 		local right = {core.display.loadImage("/data/gfx/waiter/right_basic.png"):glTexture()}
 		local middle = {core.display.loadImage("/data/gfx/waiter/middle.png"):glTexture()}
@@ -62,21 +66,35 @@ function _M:getWaitDisplay(d)
 
 			-- Progressbar
 			local x
-			i = i + dir
-			if dir > 0 and i >= max then dir = -1
-			elseif dir < 0 and i <= -max then dir = 1
+			if has_max then
+				i, max = core.wait.getTicks()
+				i = util.bound(i, 0, max)
+			else
+				i = i + dir
+				if dir > 0 and i >= max then dir = -1
+				elseif dir < 0 and i <= -max then dir = 1
+				end
 			end
 
 			local x = dw * (i / max)
 			local x2 = x + dw
 			x = util.bound(x, 0, dw)
 			x2 = util.bound(x2, 0, dw)
+			if has_max then x, x2 = 0, x end
 			local w, h = x2 - x, dh
 
 			middle[1]:toScreenFull(dx, dy, dw, middle[7], middle[2], middle[3])
 			bar[1]:toScreenFull(dx + x, dy, w, bar[7], bar[2], bar[3])
 			left[1]:toScreenFull(dx - left[6] + 5, dy + (middle[7] - left[7]) / 2, left[6], left[7], left[2], left[3])
 			right[1]:toScreenFull(dx + dw - 5, dy + (middle[7] - right[7]) / 2, right[6], right[7], right[2], right[3])
+
+			if has_max then
+				self.font:setStyle("bold")
+				local txt = {core.display.drawStringBlendedNewSurface(self.font, math.min(100, math.floor(core.wait.getTicks() * 100 / max)).."%", 255, 255, 255):glTexture()}
+				self.font:setStyle("normal")
+				txt[1]:toScreenFull(dx + (dw - txt[6]) / 2 + 2, dy + (bar[7] - txt[7]) / 2 + 2, txt[6], txt[7], txt[2], txt[3], 0, 0, 0, 0.6)
+				txt[1]:toScreenFull(dx + (dw - txt[6]) / 2, dy + (bar[7] - txt[7]) / 2, txt[6], txt[7], txt[2], txt[3])
+			end
 		end
 	end
 end
diff --git a/src/wait.c b/src/wait.c
index 5547bcb6b4..a9c7200b25 100644
--- a/src/wait.c
+++ b/src/wait.c
@@ -187,6 +187,7 @@ static int disable(lua_State *L)
 
 static int enable_manual_tick(lua_State *L)
 {
+	if (!waiting) return 0;
 	manual_ticks_enabled = lua_toboolean(L, 1);
 	if (!manual_ticks_enabled) lua_sethook(L, hook_wait_display, LUA_MASKCOUNT, wait_hooked);
 	else lua_sethook(L, NULL, 0, 0);
@@ -195,6 +196,7 @@ static int enable_manual_tick(lua_State *L)
 
 static int manual_tick(lua_State *L)
 {
+	if (!waiting) return 0;
 	if (manual_ticks_enabled)
 	{
 		waited_count += lua_tonumber(L, 1);
@@ -205,6 +207,7 @@ static int manual_tick(lua_State *L)
 
 static int add_max_ticks(lua_State *L)
 {
+	if (!waiting) return 0;
 	waited_count_max += lua_tonumber(L, 1);
 	return 0;
 }
-- 
GitLab