]> git.lizzy.rs Git - dragonfireclient.git/blob - builtin/mainmenu/tab_texturepacks.lua
Fix animation frame_speed and blend loosing precision due to incorrec… (#6357)
[dragonfireclient.git] / builtin / mainmenu / tab_texturepacks.lua
1 --Minetest
2 --Copyright (C) 2014 sapier
3 --
4 --This program is free software; you can redistribute it and/or modify
5 --it under the terms of the GNU Lesser General Public License as published by
6 --the Free Software Foundation; either version 2.1 of the License, or
7 --(at your option) any later version.
8 --
9 --This program is distributed in the hope that it will be useful,
10 --but WITHOUT ANY WARRANTY; without even the implied warranty of
11 --MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 --GNU Lesser General Public License for more details.
13 --
14 --You should have received a copy of the GNU Lesser General Public License along
15 --with this program; if not, write to the Free Software Foundation, Inc.,
16 --51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17
18 --------------------------------------------------------------------------------
19 local function filter_texture_pack_list(list)
20         local retval = {}
21
22         for _, item in ipairs(list) do
23                 if item ~= "base" then
24                         retval[#retval + 1] = item
25                 end
26         end
27
28         table.sort(retval)
29         table.insert(retval, 1, fgettext("None"))
30
31         return retval
32 end
33
34 --------------------------------------------------------------------------------
35 local function render_texture_pack_list(list)
36         local retval = ""
37
38         for i, v in ipairs(list) do
39                 if v:sub(1, 1) ~= "." then
40                         if retval ~= "" then
41                                 retval = retval .. ","
42                         end
43
44                         retval = retval .. core.formspec_escape(v)
45                 end
46         end
47
48         return retval
49 end
50
51 --------------------------------------------------------------------------------
52 local function get_formspec(tabview, name, tabdata)
53
54         local retval = "label[4,-0.25;" .. fgettext("Select texture pack:") .. "]" ..
55                         "textlist[4,0.25;7.5,5.0;TPs;"
56
57         local current_texture_path = core.settings:get("texture_path")
58         local list = filter_texture_pack_list(core.get_dir_list(core.get_texturepath(), true))
59         local index = tonumber(core.settings:get("mainmenu_last_selected_TP"))
60
61         if not index then index = 1 end
62
63         if current_texture_path == "" then
64                 retval = retval ..
65                         render_texture_pack_list(list) ..
66                         ";" .. index .. "]" ..
67                         "textarea[0.6,2.85;3.7,1.5;;" ..
68                         fgettext("Default textures will be used.") ..
69                         ";]"
70                 return retval
71         end
72
73         local infofile = current_texture_path .. DIR_DELIM .. "description.txt"
74         -- This adds backwards compatibility for old texture pack description files named
75         -- "info.txt", and should be removed once all such texture packs have been updated
76         if not file_exists(infofile) then
77                 infofile = current_texture_path .. DIR_DELIM .. "info.txt"
78                 if file_exists(infofile) then
79                         core.log("deprecated", "info.txt is deprecated. description.txt should be used instead.")
80                 end
81         end
82
83         local infotext = ""
84         local f = io.open(infofile, "r")
85         if not f then
86                 infotext = fgettext("No information available")
87         else
88                 infotext = f:read("*all")
89                 f:close()
90         end
91
92         local screenfile = current_texture_path .. DIR_DELIM .. "screenshot.png"
93         local no_screenshot
94         if not file_exists(screenfile) then
95                 screenfile = nil
96                 no_screenshot = defaulttexturedir .. "no_screenshot.png"
97         end
98
99         return  retval ..
100                         render_texture_pack_list(list) ..
101                         ";" .. index .. "]" ..
102                         "image[0.25,0.25;4.05,2.7;" .. core.formspec_escape(screenfile or no_screenshot) .. "]" ..
103                         "textarea[0.6,2.85;3.7,1.5;;" .. core.formspec_escape(infotext or "") .. ";]"
104 end
105
106 --------------------------------------------------------------------------------
107 local function main_button_handler(tabview, fields, name, tabdata)
108         if fields["TPs"] then
109                 local event = core.explode_textlist_event(fields["TPs"])
110                 if event.type == "CHG" or event.type == "DCL" then
111                         local index = core.get_textlist_index("TPs")
112                         core.settings:set("mainmenu_last_selected_TP", index)
113                         local list = filter_texture_pack_list(core.get_dir_list(core.get_texturepath(), true))
114                         local current_index = core.get_textlist_index("TPs")
115                         if current_index and #list >= current_index then
116                                 local new_path = core.get_texturepath() .. DIR_DELIM .. list[current_index]
117                                 if list[current_index] == fgettext("None") then
118                                         new_path = ""
119                                 end
120                                 core.settings:set("texture_path", new_path)
121                         end
122                 end
123                 return true
124         end
125         return false
126 end
127
128 --------------------------------------------------------------------------------
129 return {
130         name = "texturepacks",
131         caption = fgettext("Texturepacks"),
132         cbf_formspec = get_formspec,
133         cbf_button_handler = main_button_handler,
134         on_change = nil
135 }