root/cafu/trunk/CaWE/EditorMaterialManager.cpp

Revision 455, 6.0 KB (checked in by Carsten, 4 months ago)

Updated copyright banners (in C++ files).

Line 
1/*
2=================================================================================
3This file is part of Cafu, the open-source game engine and graphics engine
4for multiplayer, cross-platform, real-time 3D action.
5Copyright (C) 2002-2012 Carsten Fuchs Software.
6
7Cafu is free software: you can redistribute it and/or modify it under the terms
8of the GNU General Public License as published by the Free Software Foundation,
9either version 3 of the License, or (at your option) any later version.
10
11Cafu is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
12without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
13PURPOSE. See the GNU General Public License for more details.
14
15You should have received a copy of the GNU General Public License
16along with Cafu. If not, see <http://www.gnu.org/licenses/>.
17
18For support and more information about Cafu, visit us at <http://www.cafu.de>.
19=================================================================================
20*/
21
22#include "EditorMaterialManager.hpp"
23#include "EditorMaterialDummy.hpp"
24#include "EditorMaterialEngine.hpp"
25#include "GameConfig.hpp"
26#include "MaterialSystem/MaterialManager.hpp"
27
28#include "wx/confbase.h"
29
30
31// Compare function for sorting materials.
32static bool CompareMaterials(EditorMaterialI* const& elem1, EditorMaterialI* const& elem2)
33{
34    return wxStricmp(elem1->GetName(), elem2->GetName())<0;
35}
36
37
38EditorMatManT::EditorMatManT(const GameConfigT& GameConfig)
39    : m_MaterialMan(),
40      m_Materials(),
41      m_DefaultMaterial(NULL),
42      m_LazyMatUpdateCount(0)
43{
44    // Register all material scripts of this game and obtain all found materials.
45    ArrayT<MaterialT*> Materials=m_MaterialMan.RegisterMaterialScriptsInDir(std::string(GameConfig.ModDir)+"/Materials", std::string(GameConfig.ModDir)+"/");
46
47    // Create an editor material for each loaded engine material.
48    for (unsigned long MaterialNr=0; MaterialNr<Materials.Size(); MaterialNr++)
49        m_Materials.PushBack(new EngineMaterialT(Materials[MaterialNr]));
50
51    if (m_Materials.Size()==0)
52    {
53        wxMessageBox("WARNING: No materials found in directory\n"+GameConfig.ModDir+"/Materials\n"
54                     "We can continue, but the materials will NOT work.\n"
55                     "Something is wrong that really should be fixed!", "Missing Materials", wxOK | wxICON_ERROR);
56
57        m_Materials.PushBack(new DummyMaterialT("Error: Materials not loaded!"));
58    }
59
60    m_Materials.QuickSort(CompareMaterials);
61}
62
63
64EditorMatManT::~EditorMatManT()
65{
66    // Delete all the materials from the list.
67    for (unsigned long MatNr=0; MatNr<m_Materials.Size(); MatNr++) delete m_Materials[MatNr];
68}
69
70
71EditorMaterialI* EditorMatManT::GetDefaultMaterial()
72{
73    if (m_DefaultMaterial) return m_DefaultMaterial;
74
75    // m_DefaultMaterial is NULL (so this is the very first call to this function).
76    const wxString DefaultName=wxConfigBase::Get()->Read("General/InitialDefaultMaterial", "TechDemo/walls/wall-13b").Lower();
77
78    for (unsigned long MatNr=0; MatNr<m_Materials.Size(); MatNr++)
79    {
80        // Perform a case-insensitive find.
81        if (m_Materials[MatNr]->GetName().Lower().Find(DefaultName)!=wxNOT_FOUND)
82        {
83            m_DefaultMaterial=m_Materials[MatNr];
84            return m_DefaultMaterial;
85        }
86    }
87
88    for (unsigned long MatNr=0; MatNr<m_Materials.Size(); MatNr++)
89    {
90        if (m_Materials[MatNr]->GetName().Lower().Find("wall")!=wxNOT_FOUND && m_Materials[MatNr]->ShowInMaterialBrowser())
91        {
92            m_DefaultMaterial=m_Materials[MatNr];
93            return m_DefaultMaterial;
94        }
95    }
96
97    // Try really hard to avoid dummy materials - take first material that is visible in the editor.
98    for (unsigned long MatNr=0; MatNr<m_Materials.Size(); MatNr++)
99    {
100        if (m_Materials[MatNr]->ShowInMaterialBrowser())
101        {
102            m_DefaultMaterial=m_Materials[MatNr];
103            return m_DefaultMaterial;
104        }
105    }
106
107    m_DefaultMaterial=FindMaterial(DefaultName, true);
108    return m_DefaultMaterial;
109}
110
111
112void EditorMatManT::SetDefaultMaterial(EditorMaterialI* DefaultMat)
113{
114    m_DefaultMaterial=DefaultMat;
115}
116
117
118EditorMaterialI* EditorMatManT::FindMaterial(const wxString& MatName, bool CreateDummyIfNotFound)
119{
120    static EditorMaterialI* LastMat=NULL;
121
122    // wxASSERT(MatName!="" || !CreateDummyIfNotFound);
123
124    // Before doing a full search, check if MatName is the same as in the last call.
125    if (LastMat && !wxStricmp(MatName, LastMat->GetName()))
126        return LastMat;
127
128    for (unsigned long MatNr=0; MatNr<m_Materials.Size(); MatNr++)
129        if (!wxStricmp(MatName, m_Materials[MatNr]->GetName()))
130        {
131            LastMat=m_Materials[MatNr];
132            return LastMat;
133        }
134
135
136    // Not found. If we shall not create a dummy in this case, just return NULL.
137    if (!CreateDummyIfNotFound) return NULL;
138
139
140    // Not found - create a dummy as a placeholder for the missing material.
141    EditorMaterialI* Dummy=new DummyMaterialT(MatName.c_str());
142
143    m_Materials.PushBack(Dummy);
144    LastMat=Dummy;
145    return Dummy;
146}
147
148
149void EditorMatManT::LazilyUpdateProxies()
150{
151    if (m_LazyMatUpdateCount==0) { wxLogDebug("Beginning to cache in materials."); }
152    if ((m_LazyMatUpdateCount % 100)==0 && m_LazyMatUpdateCount<m_Materials.Size()) { wxLogDebug("Caching in material number %lu of %lu materials.", m_LazyMatUpdateCount, m_Materials.Size()); }
153    if (m_LazyMatUpdateCount+1==m_Materials.Size()) { wxLogDebug("Done caching in %lu materials.", m_Materials.Size()); }
154
155    // Do nothing once we're done caching in.
156    if (m_LazyMatUpdateCount>=m_Materials.Size()) return;
157
158    // Also do nothing while one of the mouse buttons is down, the user is probably interacting with the editor then.
159    const wxMouseState MS=wxGetMouseState();
160    if (MS.LeftIsDown() || MS.MiddleIsDown() || MS.RightIsDown()) return;
161
162    m_Materials[m_LazyMatUpdateCount]->GetImage();
163    m_LazyMatUpdateCount++;
164}
Note: See TracBrowser for help on using the browser.