-
Notifications
You must be signed in to change notification settings - Fork 1
/
loadbar.lua
83 lines (73 loc) · 2.47 KB
/
loadbar.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
require 'asyncloader'
LoadBar =
{
x = 0;
y = 0;
width = 0;
height = 0;
progress = 0;
hasRenderedFirstFrame = false;
frontTexture = nil;
backTexture = nil;
}
function LoadBar:new(width, height, frontTexture, backTexture)
local obj = setmetatable({}, self)
self.__index = self
obj.width = width
obj.height = height
obj.frontTexture = frontTexture or nil
obj.backTexture = backTexture or nil
return obj
end
function LoadBar:setPosition(x, y)
self.x=x
self.y=y
end
--If content name has a $, it will be substituted for the current progress
function LoadBar:addContentToLoad(loadFunction, onLoad, contentName)
asyncLoader.loadFunc(loadFunction, onLoad, contentName)
end
function LoadBar:startLoading(onResourcesLoaded)
local _self = self
asyncLoader.startLoading(function()
_self.hasRenderedFirstFrame = false
onResourcesLoaded()
end)
end
function LoadBar:update(dt)
if(self.hasRenderedFirstFrame) then
asyncLoader.update(dt)
end
end
function LoadBar:render()
if(not asyncLoader.hasStarted)then
return
else
self.hasRenderedFirstFrame = true
end
self.progress = asyncLoader.progress
love.graphics.clear()
if self.backTexture then
love.graphics.draw(self.backTexture, self.x, self.y, 0, self.width / self.backTexture:getWidth(), self.backTexture:getHeight())
else
love.graphics.setColor(1,1,1,1)
love.graphics.rectangle('fill', self.x, self.y, self.width, self.height)
end
if self.frontTexture then
love.graphics.draw(self.frontTexture, self.x, self.y, 0, self.width * self.progress, self.backTexture:getHeight())
else
love.graphics.setColor(0.2,0.8,0.2,1)
love.graphics.rectangle('fill', self.x, self.y, self.width * self.progress, self.height)
end
if(asyncLoader.currentContent ~= "") then
love.graphics.setColor(1,1,1,1)
if(asyncLoader.currentContent:match("%$")) then
local indexS = string.find(asyncLoader.currentContent, "%$")
local toPrint = string.sub(asyncLoader.currentContent, 1, indexS - 1)
toPrint = toPrint..tostring(self.progress*100).."%"..asyncLoader.currentContent:sub(indexS + 1)
love.graphics.printf("Loading: "..toPrint, self.x, self.y-40, self.width,"center")
else
love.graphics.printf("Loading: "..asyncLoader.currentContent, self.x, self.y-40, self.width,"center")
end
end
end