1 /*
2  Copyright (C) 2010-2014 Kristian Duske
3 
4  This file is part of TrenchBroom.
5 
6  TrenchBroom is free software: you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  TrenchBroom is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with TrenchBroom. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #ifndef TrenchBroom_TextureManager
21 #define TrenchBroom_TextureManager
22 
23 #include "Assets/AssetTypes.h"
24 #include "IO/Path.h"
25 #include "Model/ModelTypes.h"
26 
27 #include <map>
28 #include <vector>
29 
30 namespace TrenchBroom {
31     class Logger;
32 
33     namespace IO {
34         class TextureLoader;
35     }
36 
37     namespace Assets {
38         class TextureCollectionSpec;
39 
40         class TextureManager {
41         public:
42             typedef enum {
43                 SortOrder_Name,
44                 SortOrder_Usage
45             } SortOrder;
46 
47             typedef std::pair<TextureCollection*, TextureList> Group;
48             typedef std::vector<Group> GroupList;
49         private:
50             typedef std::map<String, TextureCollection*> TextureCollectionMap;
51             typedef std::pair<String, TextureCollection*> TextureCollectionMapEntry;
52             typedef std::map<String, Texture*> TextureMap;
53 
54             Logger* m_logger;
55             const IO::TextureLoader* m_loader;
56 
57             TextureCollectionList m_builtinCollections;
58             TextureCollectionMap m_builtinCollectionsByName;
59 
60             TextureCollectionList m_externalCollections;
61             TextureCollectionMap m_externalCollectionsByName;
62 
63             TextureCollectionMap m_toPrepare;
64             TextureCollectionMap m_toRemove;
65 
66             TextureCollectionList m_allCollections;
67             TextureList m_sortedTextures[2];
68             GroupList m_sortedGroups[2];
69 
70             TextureMap m_texturesByName;
71 
72             int m_minFilter;
73             int m_magFilter;
74             bool m_resetTextureMode;
75         public:
76             TextureManager(Logger* logger, int minFilter, int magFilter);
77             ~TextureManager();
78 
79             void setBuiltinTextureCollections(const IO::Path::List& paths);
80 
81             void addExternalTextureCollection(const TextureCollectionSpec& spec);
82             void removeExternalTextureCollection(const String& name);
83             void moveExternalTextureCollectionUp(const String& name);
84             void moveExternalTextureCollectionDown(const String& name);
85             void clear();
86 
87             void setTextureMode(int minFilter, int magFilter);
88             void setLoader(const IO::TextureLoader* loader);
89             void commitChanges();
90 
91             Texture* texture(const String& name) const;
92             const TextureList& textures(const SortOrder sortOrder) const;
93             const GroupList& groups(const SortOrder sortOrder) const;
94             const TextureCollectionList& collections() const;
95             const StringList externalCollectionNames() const;
96         private:
97             void addTextureCollection(const TextureCollectionSpec& spec, TextureCollectionList& collections, TextureCollectionMap& collectionsByName);
98             void removeTextureCollection(const String& name, TextureCollectionList& collections, TextureCollectionMap& collectionsByName);
99 
100             TextureCollection* loadTextureCollection(const TextureCollectionSpec& spec) const;
101 
102             void resetTextureMode();
103             void prepare();
104 
105             void clearBuiltinTextureCollections();
106             void clearExternalTextureCollections();
107             void updateTextures();
108             TextureList textureList() const;
109         };
110     }
111 }
112 
113 #endif /* defined(TrenchBroom_TextureManager) */
114