-
Notifications
You must be signed in to change notification settings - Fork 28
/
languages.lua
130 lines (125 loc) · 3.48 KB
/
languages.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
--Languages use .lua files inside the languages folder
--English is default, any missing strings in the language file will fallback to English
function set_language(lang)
local s = love.filesystem.read("languages/english.json")
TEXT = JSON:decode(s)
s = nil
if lang ~= "english" then
if love.filesystem.getInfo("languages/" .. lang .. ".json") then
local s = love.filesystem.read("languages/" .. lang .. ".json")
local t = JSON:decode(s)
for j, w in pairs(t) do
TEXT[j] = w
end
else
CurrentLanguage = "english"
end
end
end
set_language("english")
local Languages, LanguageCount
local sel = 1
local languagemenu_select
function languagemenu_open()
--Load list of languages
if not Lanugages then
Languages = {}
LanguageCount = 0
local files = love.filesystem.getDirectoryItems("languages")
for num, filename in pairs(files) do
if string.sub(filename, -5, -1) == ".json" and string.sub(filename, 1, -6) ~= "names" then
Languages[string.sub(filename, 1, -6)] = {name=string.sub(filename, 1, -6)}
LanguageCount = LanguageCount + 1
end
end
--Load Names
local s = love.filesystem.read("languages/names.json")
local t = JSON:decode(s)
for j, w in pairs(t) do
if Languages[j] then
Languages[j].name = w
end
end
end
if not CurrentLanguage then
CurrentLanguage = "english"
end
--Find Current Selection
local i = 1
for name, t in pairs(Languages) do
if name == CurrentLanguage then
sel = i
break
end
i = i + 1
end
end
function languagemenu_draw()
--Darken Background
love.graphics.setColor(0, 0, 0, 100)
love.graphics.rectangle("fill", 0, 0, width*16*scale, 224*scale)
--Window
local w, h = 220, (LanguageCount+1)*10+10
local x, y = (width*16-w)/2, (height*16-h)/2
love.graphics.setColor(0, 0, 0)
love.graphics.rectangle("fill", x*scale, y*scale, w*scale, h*scale)
love.graphics.setColor(255, 255, 255)
drawrectangle(x+1, y+1, w-2, h-2)
--Text
properprintF(TEXT["language"], (x+(w-utf8.len(TEXT["language"])*8)/2)*scale, (y+6)*scale)
local i = 1
for name, t in pairs(Languages) do
love.graphics.setColor(100, 100, 100, 255)
if sel == i then
love.graphics.setColor(255, 255, 255)
properprintf(">", (x+6)*scale, (y+6+i*10)*scale)
end
properprintf(t.name, (x+16)*scale, (y+6+i*10)*scale)
i = i + 1
end
end
function languagemenu_keypressed(key)
if (key == "up" or key == "w") then
sel = math.max(1, sel-1)
elseif (key == "down" or key == "s") then
sel = math.min(LanguageCount, sel+1)
elseif (key == "return" or key == "enter" or key == "kpenter" or key == " ") then
return languagemenu_select()
elseif key == "escape" then
return true
end
end
function languagemenu_mousepressed(mx, my, button)
local w, h = 220, (LanguageCount+1)*10+10
local x, y = (width*16-w)/2, (height*16-h)/2
for i = 1, LanguageCount do
if mx > (x+6)*scale and my > (y+6+i*10)*scale and mx < (x+6+w)*scale and my < (y+16+i*10)*scale then
return languagemenu_select()
end
end
end
function languagemenu_mousemoved(mx, my, dx, dy)
local w, h = 220, (LanguageCount+1)*10+10
local x, y = (width*16-w)/2, (height*16-h)/2
for i = 1, LanguageCount do
if mx > (x+6)*scale and my > (y+6+i*10)*scale and mx < (x+6+w)*scale and my < (y+16+i*10)*scale then
sel = i
break
end
end
end
function languagemenu_update(dt)
end
function languagemenu_select()
local i = 1
for name, t in pairs(Languages) do
if i == sel then
CurrentLanguage = name
set_language(CurrentLanguage)
saveconfig()
return true
end
i = i + 1
end
return false
end