]> git.lizzy.rs Git - minetest.git/blob - builtin/mainmenu/tab_texturepacks.lua
Faster insertion into table
[minetest.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         for _, item in ipairs(list) do
22                 if item ~= "base" then
23                         retval[#retval + 1] = item
24                 end
25         end
26
27         table.sort(retval)
28         table.insert(retval, 1, fgettext("None"))
29
30         return retval
31 end
32
33 --------------------------------------------------------------------------------
34 local function render_texture_pack_list(list)
35         local retval = ""
36
37         for i, v in ipairs(list) do
38                 if v:sub(1,1) ~= "." then
39                         if retval ~= "" then
40                                 retval = retval ..","
41                         end
42
43                         retval = retval .. core.formspec_escape(v)
44                 end
45         end
46
47         return retval
48 end
49
50 --------------------------------------------------------------------------------
51 local function get_formspec(tabview, name, tabdata)
52
53         local retval = "label[4,-0.25;".. fgettext("Select texture pack:") .. "]"..
54                         "textlist[4,0.25;7.5,5.0;TPs;"
55
56         local current_texture_path = core.setting_get("texture_path")
57         local list = filter_texture_pack_list(core.get_dir_list(core.get_texturepath(), true))
58         local index = tonumber(core.setting_get("mainmenu_last_selected_TP"))
59
60         if index == nil then index = 1 end
61
62         if current_texture_path == "" then
63                 retval = retval ..
64                         render_texture_pack_list(list) ..
65                         ";" .. index .. "]"
66                 return retval
67         end
68
69         local infofile = current_texture_path ..DIR_DELIM.."description.txt"
70         -- This adds backwards compatibility for old texture pack description files named
71         -- "info.txt", and should be removed once all such texture packs have been updated
72         if not file_exists(infofile) then
73                 infofile = current_texture_path ..DIR_DELIM.."info.txt"
74                 if file_exists(infofile) then
75                         core.log("info.txt is depreciated. description.txt should be used instead.");
76                 end
77         end
78         local infotext = ""
79         local f = io.open(infofile, "r")
80         if not f then
81                 infotext = fgettext("No information available")
82         else
83                 infotext = f:read("*all")
84                 f:close()
85         end
86
87         local screenfile = current_texture_path..DIR_DELIM.."screenshot.png"
88         local no_screenshot = nil
89         if not file_exists(screenfile) then
90                 screenfile = nil
91                 no_screenshot = defaulttexturedir .. "no_screenshot.png"
92         end
93
94         return  retval ..
95                         render_texture_pack_list(list) ..
96                         ";" .. index .. "]" ..
97                         "image[0.25,0.25;4.0,3.7;"..core.formspec_escape(screenfile or no_screenshot).."]"..
98                         "textarea[0.6,3.25;3.7,1.5;;"..core.formspec_escape(infotext or "")..";]"
99 end
100
101 --------------------------------------------------------------------------------
102 local function main_button_handler(tabview, fields, name, tabdata)
103         if fields["TPs"] ~= nil then
104                 local event = core.explode_textlist_event(fields["TPs"])
105                 if event.type == "CHG" or event.type == "DCL" then
106                         local index = core.get_textlist_index("TPs")
107                         core.setting_set("mainmenu_last_selected_TP",
108                                 index)
109                         local list = filter_texture_pack_list(core.get_dir_list(core.get_texturepath(), true))
110                         local current_index = core.get_textlist_index("TPs")
111                         if current_index ~= nil and #list >= current_index then
112                                 local new_path = core.get_texturepath()..DIR_DELIM..list[current_index]
113                                 if list[current_index] == fgettext("None") then new_path = "" end
114
115                                 core.setting_set("texture_path", new_path)
116                         end
117                 end
118                 return true
119         end
120         return false
121 end
122
123 --------------------------------------------------------------------------------
124 tab_texturepacks = {
125         name = "texturepacks",
126         caption = fgettext("Texturepacks"),
127         cbf_formspec = get_formspec,
128         cbf_button_handler = main_button_handler,
129         on_change = nil
130         }