tibia-client/modules/client_skins/skins.lua

136 lines
3.3 KiB
Lua
Raw Normal View History

2012-06-18 09:47:35 +02:00
Skins = { }
-- private variables
local defaultSkinName = 'Default'
local installedSkins
local currentSkin
local skinComboBox
-- private functions
local function onSkinComboBoxOptionChange(self, optionText, optionData)
if Skins.setSkin(optionText) then
2012-06-26 00:13:30 +02:00
g_settings.set('skin', optionText)
2012-07-14 23:30:00 +02:00
g_textures.clearTexturesCache()
2012-06-26 00:13:30 +02:00
g_modules.reloadModules()
2012-06-18 09:47:35 +02:00
end
end
2012-06-19 10:46:49 +02:00
local function getSkinPath(name)
local current = getfsrcpath()
return g_resources.getRealDir(current) .. current .. '/skins/' .. string.lower(name)
2012-06-19 10:46:49 +02:00
end
2012-06-18 09:47:35 +02:00
-- public functions
function Skins.init()
installedSkins = {}
Skins.installSkins('skins')
2012-06-19 10:46:49 +02:00
if installedSkins[defaultSkinName] then
g_resources.addToSearchPath(getSkinPath(defaultSkinName), 0)
end
2012-06-26 00:13:30 +02:00
local userSkinName = g_settings.get('skin', 'false')
if userSkinName ~= 'false' and Skins.setSkin(userSkinName) then
pdebug('Using configured skin: ' .. userSkinName)
2012-06-18 09:47:35 +02:00
else
pdebug('Using default skin: ' .. defaultSkinName)
2012-06-18 09:47:35 +02:00
Skins.setSkin(defaultSkinName)
2012-06-26 00:13:30 +02:00
g_settings.set('skin', defaultSkinName)
2012-06-18 09:47:35 +02:00
end
addEvent( function()
2012-06-26 00:13:30 +02:00
skinComboBox = g_ui.createWidget('ComboBox', rootWidget:recursiveGetChildById('rightButtonsPanel'))
2012-06-18 09:47:35 +02:00
for key,value in pairs(installedSkins) do
skinComboBox:addOption(value.name)
end
skinComboBox:setCurrentOption(currentSkin.name)
skinComboBox.onOptionChange = onSkinComboBoxOptionChange
end, false)
end
function Skins.terminate()
2012-06-19 10:46:49 +02:00
g_resources.removeFromSearchPath(getSkinPath(defaultSkinName))
if currentSkin then
g_resources.removeFromSearchPath(getSkinPath(currentSkin.name))
end
2012-06-18 09:47:35 +02:00
installedSkins = nil
currentSkin = nil
skinComboBox = nil
end
function Skins.installSkin(skin)
if not skin or not skin.name or not skin.styles then
error('Unable to install skin.')
return false
end
2012-06-19 10:46:49 +02:00
if installedSkins[skin.name] then
pwarning(skin.name .. ' has been replaced.')
2012-06-19 10:46:49 +02:00
end
2012-06-18 09:47:35 +02:00
installedSkins[skin.name] = skin
return true
end
function Skins.installSkins(directory)
dofiles(directory)
end
function Skins.setSkin(name)
local skin = installedSkins[name]
if not skin then
pwarning("Skin " .. name .. ' does not exist.')
2012-06-18 09:47:35 +02:00
return false
end
2012-06-19 10:46:49 +02:00
g_fonts.clearFonts()
g_ui.clearStyles()
2012-06-19 10:46:49 +02:00
if name ~= defaultSkinName then
local defaultSkin = installedSkins[defaultSkinName]
if not defaultSkin then
error("Default skin is not installed.")
return false
end
Skins.loadSkin(defaultSkin)
2012-06-18 09:47:35 +02:00
end
2012-06-19 10:46:49 +02:00
if currentSkin then
g_resources.removeFromSearchPath(getSkinPath(currentSkin.name))
end
g_resources.addToSearchPath(getSkinPath(skin.name), true)
Skins.loadSkin(skin)
2012-06-18 09:47:35 +02:00
currentSkin = skin
return true
end
2012-06-19 10:46:49 +02:00
function Skins.loadSkin(skin)
local lowerName = string.lower(skin.name)
2012-07-06 07:00:49 +02:00
if skin.fonts then
for i=1,#skin.fonts do
g_fonts.importFont('skins/' .. lowerName .. '/fonts/' .. skin.fonts[i])
2012-06-19 10:46:49 +02:00
2012-07-06 07:00:49 +02:00
if i == 1 then
g_fonts.setDefaultFont(skin.fonts[i])
end
2012-06-19 10:46:49 +02:00
end
end
2012-07-06 07:00:49 +02:00
if skin.styles then
for i=1,#skin.styles do
g_ui.importStyle('skins/' .. lowerName .. '/styles/' .. skin.styles[i])
end
end
if skin.particles then
for i=1,#skin.particles do
g_particles.importParticle('skins/' .. lowerName .. '/particles/' .. skin.particles[i])
end
2012-06-19 10:46:49 +02:00
end
end