Skip to content

Commit 3831534

Browse files
EkdohibsPilzAdam
authored andcommittedAug 4, 2013
Add texture pack selection to main menu
1 parent fe1fe1b commit 3831534

File tree

10 files changed

+125
-17
lines changed

10 files changed

+125
-17
lines changed
 

‎builtin/mainmenu.lua

+98-2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,17 @@ menu = {}
2020
local tabbuilder = {}
2121
local worldlist = nil
2222

23+
--------------------------------------------------------------------------------
24+
local function filterTP(TPlist)
25+
TPlist2 = {"None"}
26+
for _,i in ipairs(TPlist) do
27+
if i~="base" then
28+
table.insert(TPlist2, i)
29+
end
30+
end
31+
return TPlist2
32+
end
33+
2334
--------------------------------------------------------------------------------
2435
function menu.render_favorite(spec,render_details)
2536
local text = ""
@@ -162,6 +173,23 @@ function menu.render_world_list()
162173
return retval
163174
end
164175

176+
--------------------------------------------------------------------------------
177+
function menu.render_TP_list(TPlist)
178+
local retval = ""
179+
180+
--local current_TP = filterlist.get_list(TPlist)
181+
182+
for i,v in ipairs(TPlist) do
183+
if retval ~= "" then
184+
retval = retval ..","
185+
end
186+
187+
retval = retval .. v
188+
end
189+
190+
return retval
191+
end
192+
165193
--------------------------------------------------------------------------------
166194
function menu.init()
167195
--init menu data
@@ -179,8 +207,7 @@ function menu.init()
179207
menu.favorites = engine.get_favorites("local")
180208
end
181209

182-
menu.defaulttexturedir = engine.get_gamepath() .. DIR_DELIM .. ".." ..
183-
DIR_DELIM .. "textures" .. DIR_DELIM .. "base" ..
210+
menu.defaulttexturedir = engine.get_texturepath() .. DIR_DELIM .. "base" ..
184211
DIR_DELIM .. "pack" .. DIR_DELIM
185212
end
186213

@@ -307,6 +334,10 @@ function tabbuilder.gettab()
307334
retval = retval .. tabbuilder.tab_settings()
308335
end
309336

337+
if tabbuilder.current_tab == "texture_packs" then
338+
retval = retval .. tabbuilder.tab_TP()
339+
end
340+
310341
if tabbuilder.current_tab == "credits" then
311342
retval = retval .. tabbuilder.tab_credits()
312343
end
@@ -734,6 +765,23 @@ function tabbuilder.handle_singleplayer_buttons(fields)
734765
end
735766
end
736767

768+
--------------------------------------------------------------------------------
769+
function tabbuilder.handle_TP_buttons(fields)
770+
if fields["TPs"] ~= nil then
771+
local event = explode_textlist_event(fields["TPs"])
772+
if event.typ == "CHG" or event.typ=="DCL" then
773+
local index = engine.get_textlist_index("TPs")
774+
engine.setting_set("mainmenu_last_selected_TP",
775+
index)
776+
local TPlist = filterTP(engine.get_dirlist(engine.get_texturepath(), true))
777+
local TPname = TPlist[engine.get_textlist_index("TPs")]
778+
local TPpath = engine.get_texturepath()..DIR_DELIM..TPname
779+
if TPname == "None" then TPpath = "" end
780+
engine.setting_set("texture_path", TPpath)
781+
end
782+
end
783+
end
784+
737785
--------------------------------------------------------------------------------
738786
function tabbuilder.tab_header()
739787

@@ -798,6 +846,7 @@ function tabbuilder.init()
798846
table.insert(tabbuilder.current_buttons,{name="multiplayer", caption="Client"})
799847
table.insert(tabbuilder.current_buttons,{name="server", caption="Server"})
800848
table.insert(tabbuilder.current_buttons,{name="settings", caption="Settings"})
849+
table.insert(tabbuilder.current_buttons,{name="texture_packs", caption="Texture Packs"})
801850

802851
if engine.setting_getbool("main_menu_game_mgr") then
803852
table.insert(tabbuilder.current_buttons,{name="game_mgr", caption="Games"})
@@ -946,6 +995,49 @@ function tabbuilder.tab_singleplayer()
946995
menubar.formspec
947996
end
948997

998+
--------------------------------------------------------------------------------
999+
function tabbuilder.tab_TP()
1000+
local TPpath = engine.setting_get("texture_path")
1001+
local TPlist = filterTP(engine.get_dirlist(engine.get_texturepath(), true))
1002+
local index = tonumber(engine.setting_get("mainmenu_last_selected_TP"))
1003+
if index == nil then index = 1 end
1004+
if TPpath == "" then
1005+
return "label[4,-0.25;Select texture pack:]"..
1006+
"vertlabel[0,-0.25;TEXTURE PACKS]" ..
1007+
"textlist[4,0.25;7.5,5.0;TPs;" ..
1008+
menu.render_TP_list(TPlist) ..
1009+
";" .. index .. "]" ..
1010+
menubar.formspec
1011+
end
1012+
local TPinfofile = TPpath..DIR_DELIM.."info.txt"
1013+
local f = io.open(TPinfofile, "r")
1014+
if f==nil then
1015+
menu.TPinfo = "No information available"
1016+
else
1017+
menu.TPinfo = f:read("*all")
1018+
f:close()
1019+
end
1020+
local TPscreenfile = TPpath..DIR_DELIM.."screenshot.png"
1021+
local f = io.open(TPscreenfile, "r")
1022+
if f==nil then
1023+
menu.TPscreen = nil
1024+
else
1025+
menu.TPscreen = TPscreenfile
1026+
f:close()
1027+
end
1028+
1029+
local no_screenshot = engine.get_texturepath()..DIR_DELIM.."base"..DIR_DELIM.."pack"..DIR_DELIM.."no_screenshot.png"
1030+
1031+
return "label[4,-0.25;Select texture pack:]"..
1032+
"vertlabel[0,-0.25;TEXTURE PACKS]" ..
1033+
"textlist[4,0.25;7.5,5.0;TPs;" ..
1034+
menu.render_TP_list(TPlist) ..
1035+
";" .. index .. "]" ..
1036+
"image[0.65,0.25;4.0,3.7;"..(menu.TPscreen or no_screenshot).."]"..
1037+
"textarea[1.0,3.25;3.7,1.5;;"..(menu.TPinfo or "")..";]"..
1038+
menubar.formspec
1039+
end
1040+
9491041
--------------------------------------------------------------------------------
9501042
function tabbuilder.tab_credits()
9511043
return "vertlabel[0,-0.5;CREDITS]" ..
@@ -1039,6 +1131,10 @@ engine.button_handler = function(fields)
10391131
tabbuilder.handle_singleplayer_buttons(fields)
10401132
end
10411133

1134+
if tabbuilder.current_tab == "texture_packs" then
1135+
tabbuilder.handle_TP_buttons(fields)
1136+
end
1137+
10421138
if tabbuilder.current_tab == "multiplayer" then
10431139
tabbuilder.handle_multiplayer_buttons(fields)
10441140
end

‎src/game.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -3482,6 +3482,7 @@ void the_game(
34823482
infostream << "\t\t" << i << ":" << texture->getName().getPath().c_str()
34833483
<< std::endl;
34843484
}
3485+
clearTextureNameCache();
34853486
infostream << "\tRemaining materials: "
34863487
<< driver-> getMaterialRendererCount ()
34873488
<< " (note: irrlicht doesn't support removing renderers)"<< std::endl;

‎src/guiLuaApi.cpp

+10
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ void guiLuaApi::initialize(lua_State* L,GUIEngine* engine)
8282
retval &= API_FCT(set_topleft_text);
8383
retval &= API_FCT(get_modpath);
8484
retval &= API_FCT(get_gamepath);
85+
retval &= API_FCT(get_texturepath);
8586
retval &= API_FCT(get_dirlist);
8687
retval &= API_FCT(create_dir);
8788
retval &= API_FCT(delete_dir);
@@ -829,6 +830,15 @@ int guiLuaApi::l_get_gamepath(lua_State *L)
829830
return 1;
830831
}
831832

833+
/******************************************************************************/
834+
int guiLuaApi::l_get_texturepath(lua_State *L)
835+
{
836+
std::string gamepath
837+
= fs::RemoveRelativePathComponents(porting::path_user + DIR_DELIM + "textures");
838+
lua_pushstring(L, gamepath.c_str());
839+
return 1;
840+
}
841+
832842
/******************************************************************************/
833843
int guiLuaApi::l_get_dirlist(lua_State *L) {
834844
const char *path = luaL_checkstring(L, 1);

‎src/guiLuaApi.h

+2
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,8 @@ class guiLuaApi {
164164
static int l_get_modpath(lua_State *L);
165165

166166
static int l_get_gamepath(lua_State *L);
167+
168+
static int l_get_texturepath(lua_State *L);
167169

168170
static int l_get_dirlist(lua_State *L);
169171

‎src/server.cpp

+1-2
Original file line numberDiff line numberDiff line change
@@ -4273,8 +4273,7 @@ void Server::fillMediaCache()
42734273
paths.push_back(mod.path + DIR_DELIM + "media");
42744274
paths.push_back(mod.path + DIR_DELIM + "models");
42754275
}
4276-
std::string path_all = "textures";
4277-
paths.push_back(path_all + DIR_DELIM + "all");
4276+
paths.push_back(porting::path_user + DIR_DELIM + "textures" + DIR_DELIM + "server");
42784277

42794278
// Collect media file information from paths into cache
42804279
for(std::list<std::string>::iterator i = paths.begin();

‎src/tile.cpp

+5-12
Original file line numberDiff line numberDiff line change
@@ -131,18 +131,6 @@ std::string getTexturePath(const std::string &filename)
131131
// Check all filename extensions. Returns "" if not found.
132132
fullpath = getImagePath(testpath);
133133
}
134-
135-
/*
136-
Check from $user/textures/all
137-
*/
138-
if(fullpath == "")
139-
{
140-
std::string texture_path = porting::path_user + DIR_DELIM
141-
+ "textures" + DIR_DELIM + "all";
142-
std::string testpath = texture_path + DIR_DELIM + filename;
143-
// Check all filename extensions. Returns "" if not found.
144-
fullpath = getImagePath(testpath);
145-
}
146134

147135
/*
148136
Check from default data directory
@@ -163,6 +151,11 @@ std::string getTexturePath(const std::string &filename)
163151
return fullpath;
164152
}
165153

154+
void clearTextureNameCache()
155+
{
156+
g_texturename_to_path_cache.clear();
157+
}
158+
166159
/*
167160
Stores internal information about a texture.
168161
*/

‎src/tile.h

+2
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ std::string getImagePath(std::string path);
5757
*/
5858
std::string getTexturePath(const std::string &filename);
5959

60+
void clearTextureNameCache();
61+
6062
/*
6163
ITextureSource::generateTextureFromMesh parameters
6264
*/

‎src/util/container.h

+5
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,11 @@ class MutexedMap
118118
}
119119
return result;
120120
}
121+
122+
void clear ()
123+
{
124+
m_values.clear();
125+
}
121126

122127
private:
123128
std::map<Key, Value> m_values;

‎textures/all/textures_here.txt

-1
This file was deleted.

‎textures/texture_packs_here.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Put your texture pack folders in this folder. Textures in the "server" pack will be used by the server.

0 commit comments

Comments
 (0)
Please sign in to comment.