176 lines
6.2 KiB
Lua
176 lines
6.2 KiB
Lua
local gui = require("gui")
|
|
local color = require("gui.core.color")
|
|
local loader = require("loader")
|
|
local theme = require("gui.core.theme")
|
|
local fmt = require("fmt")
|
|
local timer = require("utils")
|
|
local multi, thread = require("multi"):init()
|
|
|
|
local timesup = love.audio.newSource("timesup.mp3", "static")
|
|
local double = love.audio.newSource("double.mp3", "static")
|
|
|
|
local boardUpdater = gui:getProcessor():newProcessor("board-updater")
|
|
boardUpdater.Start()
|
|
|
|
-- Create a table to manage GUI elements
|
|
local manage = {}
|
|
|
|
-- Function to resize fonts for all managed elements
|
|
local function resizeFonts()
|
|
-- Use multi-threading to improve performance
|
|
multi:newThread(function()
|
|
-- Introduce a small delay to avoid too many iterations at once
|
|
thread.skip(2)
|
|
-- Iterate over each element in the 'manage' table
|
|
for i = 1, #manage do
|
|
local elem = manage[i]
|
|
|
|
-- Check if the current element has a centerFont property
|
|
if elem.centerFont then
|
|
-- Adjust font size and re-center the text
|
|
elem:fitFont(nil, nil, {scale = 2/3})
|
|
elem:centerFont()
|
|
end
|
|
end
|
|
end)
|
|
end
|
|
|
|
gui.Events.OnResized(resizeFonts)
|
|
|
|
local function buildBoard(frame, path)
|
|
local data = loader:new(path)
|
|
index = data.index
|
|
local board, question = frame:newFrame(), frame:newFrame()
|
|
board:fullFrame()
|
|
question:fullFrame()
|
|
question.visible = false
|
|
question.color = color.new("#060ce9")
|
|
local tiers = index.settings.tiers or 5
|
|
local start = index.settings.start or 100
|
|
local inc = index.settings.increment or 100
|
|
for cat,v in pairs(index.categories) do
|
|
local c
|
|
if v.image then
|
|
c = board:newImageLabel(v.image, 0, 0, 0, 0,(1/#index.categories)*(cat-1),0,1/#index.categories,1/(tiers+1))
|
|
else
|
|
c = board:newTextLabel(v.displayName or v.name,0,0,0,0,(1/#index.categories)*(cat-1),0,1/#index.categories,1/(tiers+1))
|
|
c.align = gui.ALIGN_CENTER
|
|
c.textColor = color.new("#ffffff")
|
|
c.color = color.new("#060ce9")
|
|
end
|
|
|
|
img = c:newImageButton("assets/placeholder.jpg")
|
|
img.visibility = 0
|
|
|
|
img:OnReleased(function(self)
|
|
self.visible = false
|
|
end)
|
|
|
|
img:fullFrame()
|
|
table.insert(manage,c)
|
|
for tier = 1,tiers do
|
|
local t = board:newTextButton("$" .. start + inc*(tier-1),0,0,0,0,(1/#index.categories)*(cat-1),1/(tiers+1)*tier,(1/#index.categories),1/(tiers+1))
|
|
t.textColor = color.new("#9b9024")
|
|
t.align = gui.ALIGN_CENTER
|
|
t.color = color.new("#060ce9")
|
|
t.category = v.name
|
|
t.index = tier
|
|
t.price = start + inc*(tier-1)
|
|
t:OnReleased(boardUpdater:newFunction(function(self)
|
|
if self.text == "" then return end
|
|
if index.categories[cat].questions == nil then fmt.Printf("Question not defined: File: %v Category: %v - %v\n",path,index.categories[cat].name,start + inc*(tier-1)) return end
|
|
local q = index.categories[cat].questions[self.index]
|
|
if q == nil then fmt.Printf("Question contains no data: File: %v Category: %v Tier: %v\n",path,index.categories[cat].name,start + inc*(tier-1)) return end
|
|
self.textVisibility = 0
|
|
local template = LoadTemplate(q.template, path)
|
|
question.visible = true
|
|
local player = GetActivePlayer()
|
|
local tm
|
|
local stop
|
|
fmt.Printf("Question: %v \nAnswer: %v\n",q["title"],q["answer"])
|
|
if q["time-limit"] then
|
|
tm = timer.startTimer({duration = q["time-limit"]})
|
|
tm.OnStop(function()
|
|
-- Make sound? Subtract if daily double
|
|
if stop then return end
|
|
timesup:play()
|
|
end)
|
|
end
|
|
template.index(question, q, function(ans)
|
|
player = GetActivePlayer()
|
|
tm:Cleanup()
|
|
stop = true
|
|
if ans then
|
|
player:Add(self.price)
|
|
question.visible = false
|
|
elseif ans == false then
|
|
player:Add(-self.price)
|
|
else
|
|
question.visible = false
|
|
end -- nil is a valid option where you weren't right or wrong, you just skipped
|
|
self.text = ""
|
|
end)
|
|
boardUpdater:newThread("QuestionUpdater",function()
|
|
while true do
|
|
template.update(dt)
|
|
thread.yield()
|
|
if self.text == "" then return end
|
|
end
|
|
end)
|
|
end))
|
|
table.insert(manage,t)
|
|
end
|
|
end
|
|
resizeFonts()
|
|
resizeFonts()
|
|
end
|
|
|
|
local qUpdater = gui:getProcessor():newProcessor("question-updater")
|
|
qUpdater.Start()
|
|
|
|
function LoadTemplate(name, path)
|
|
|
|
if love.filesystem.getInfo("templates/" .. name .. ".lua") then
|
|
data = love.filesystem.read("templates/" .. name .. ".lua")
|
|
elseif love.filesystem.getInfo(path .."/templates/" .. name .. ".lua") then
|
|
data = love.filesystem.read(path .."/templates/" .. name .. ".lua")
|
|
end
|
|
|
|
local template, err = loadstring(data)
|
|
|
|
if err ~= nil then
|
|
error(err)
|
|
end
|
|
|
|
local env = { -- lua built-ins except io/os code execution
|
|
pairs=pairs,
|
|
print=print,
|
|
tostring=tostring,
|
|
tonumber=tonumber,
|
|
type=type,
|
|
assert=assert,
|
|
ipairs=ipairs,
|
|
table=table,
|
|
math=math,
|
|
string=string,
|
|
next=next,
|
|
pcall=pcall,
|
|
select=select,
|
|
xpcall=xpcall,
|
|
utf8=utf8,
|
|
-- Global vars
|
|
color = color,
|
|
error = multi.error,
|
|
theme = theme,
|
|
gui = gui
|
|
}
|
|
|
|
env._G = env
|
|
local isoTemplate = multi.isolateFunction(template, env)
|
|
|
|
return isoTemplate()
|
|
end
|
|
|
|
return {
|
|
buildBoard = buildBoard
|
|
} |