]> git.lizzy.rs Git - dragonfireclient.git/blob - builtin/mainmenu/tab_texturepacks.lua
Support for scalable font and gui elements
[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         retval = {"None"}
21         for _,i in ipairs(list) do
22                 if i~="base" then
23                         table.insert(retval, i)
24                 end
25         end
26         return retval
27 end
28
29 --------------------------------------------------------------------------------
30 local function render_texture_pack_list(list)
31         local retval = ""
32
33         for i, v in ipairs(list) do
34                 if retval ~= "" then
35                         retval = retval ..","
36                 end
37
38                 retval = retval .. core.formspec_escape(v)
39         end
40
41         return retval
42 end
43
44 --------------------------------------------------------------------------------
45 local function get_formspec(tabview, name, tabdata)
46         
47         local retval = "label[4,-0.25;".. fgettext("Select texture pack:") .. "]"..
48                         "vertlabel[0,-0.25;".. fgettext("TEXTURE PACKS") .. "]" ..
49                         "textlist[4,0.25;7.5,5.0;TPs;"
50
51         local current_texture_path = core.setting_get("texture_path")
52         local list = filter_texture_pack_list(core.get_dirlist(core.get_texturepath(), true))
53         local index = tonumber(core.setting_get("mainmenu_last_selected_TP"))
54
55         if index == nil then index = 1 end
56
57         if current_texture_path == "" then
58                 retval = retval ..
59                         render_texture_pack_list(list) ..
60                         ";" .. index .. "]"
61                 return retval
62         end
63
64         local infofile = current_texture_path ..DIR_DELIM.."info.txt"
65         local infotext = ""
66         local f = io.open(infofile, "r")
67         if f==nil then
68                 infotext = fgettext("No information available")
69         else
70                 infotext = f:read("*all")
71                 f:close()
72         end
73
74         local screenfile = current_texture_path..DIR_DELIM.."screenshot.png"
75         local no_screenshot = nil
76         if not file_exists(screenfile) then
77                 screenfile = nil
78                 no_screenshot = defaulttexturedir .. "no_screenshot.png"
79         end
80
81         return  retval ..
82                         render_texture_pack_list(list) ..
83                         ";" .. index .. "]" ..
84                         "image[0.65,0.25;4.0,3.7;"..core.formspec_escape(screenfile or no_screenshot).."]"..
85                         "textarea[1.0,3.25;3.7,1.5;;"..core.formspec_escape(infotext or "")..";]"
86 end
87
88 --------------------------------------------------------------------------------
89 local function main_button_handler(tabview, fields, name, tabdata)
90         if fields["TPs"] ~= nil then
91                 local event = core.explode_textlist_event(fields["TPs"])
92                 if event.type == "CHG" or event.type == "DCL" then
93                         local index = core.get_textlist_index("TPs")
94                         core.setting_set("mainmenu_last_selected_TP",
95                                 index)
96                         local list = filter_texture_pack_list(core.get_dirlist(core.get_texturepath(), true))
97                         local current_index = core.get_textlist_index("TPs")
98                         if current_index ~= nil and #list >= current_index then
99                                 local new_path = core.get_texturepath()..DIR_DELIM..list[current_index]
100                                 if list[current_index] == "None" then new_path = "" end
101
102                                 core.setting_set("texture_path", new_path)
103                         end
104                 end
105                 return true
106         end
107         return false
108 end
109
110 --------------------------------------------------------------------------------
111 tab_texturepacks = {
112         name = "texturepacks",
113         caption = fgettext("Texturepacks"),
114         cbf_formspec = get_formspec,
115         cbf_button_handler = main_button_handler,
116         on_change = nil
117         }