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 #include "BrushContentTypeEvaluator.h"
21 
22 #include "Macros.h"
23 #include "Model/AttributableNode.h"
24 #include "Model/Brush.h"
25 #include "Model/BrushFace.h"
26 
27 namespace TrenchBroom {
28     namespace Model {
29         class BrushFaceEvaluator : public BrushContentTypeEvaluator {
30         public:
~BrushFaceEvaluator()31             virtual ~BrushFaceEvaluator() {}
32         private:
doEvaluate(const Brush * brush) const33             bool doEvaluate(const Brush* brush) const {
34                 const Model::BrushFaceList& faces = brush->faces();
35                 Model::BrushFaceList::const_iterator it, end;
36                 for (it = faces.begin(), end = faces.end(); it != end; ++it) {
37                     const Model::BrushFace* face = *it;
38                     if (!doEvaluate(face))
39                         return false;
40                 }
41                 return true;
42             }
43 
44             virtual bool doEvaluate(const BrushFace* brush) const = 0;
45         };
46 
47         class TextureNameEvaluator : public BrushFaceEvaluator {
48         private:
49             StringUtils::CaseSensitiveStringMatcher m_matcher;
50         public:
TextureNameEvaluator(const String & pattern)51             TextureNameEvaluator(const String& pattern) :
52             m_matcher(pattern) {}
53         private:
doEvaluate(const BrushFace * face) const54             bool doEvaluate(const BrushFace* face) const {
55                 const String& textureName = face->textureName();
56                 const size_t pos = textureName.find_last_of('/');
57                 if (pos == String::npos)
58                     return m_matcher.matches(textureName);
59                 return m_matcher.matches(textureName.substr(pos + 1));
60             }
61         };
62 
63         class ContentFlagsEvaluator : public BrushFaceEvaluator {
64         private:
65             int m_flags;
66         public:
ContentFlagsEvaluator(const int flags)67             ContentFlagsEvaluator(const int flags) :
68             m_flags(flags) {}
69         private:
doEvaluate(const BrushFace * face) const70             bool doEvaluate(const BrushFace* face) const {
71                 return (face->surfaceContents() & m_flags) != 0;
72             }
73         };
74 
75         class EntityClassnameEvaluator : public BrushContentTypeEvaluator {
76         private:
77             StringUtils::CaseSensitiveStringMatcher m_matcher;
78         public:
EntityClassnameEvaluator(const String & pattern)79             EntityClassnameEvaluator(const String& pattern) :
80             m_matcher(pattern) {}
81         private:
doEvaluate(const Brush * brush) const82             bool doEvaluate(const Brush* brush) const {
83                 const AttributableNode* entity = brush->entity();
84                 if (entity == NULL)
85                     return false;
86 
87                 return m_matcher.matches(entity->classname());
88             }
89         };
90 
~BrushContentTypeEvaluator()91         BrushContentTypeEvaluator::~BrushContentTypeEvaluator() {}
92 
textureNameEvaluator(const String & pattern)93         BrushContentTypeEvaluator* BrushContentTypeEvaluator::textureNameEvaluator(const String& pattern) {
94             return new TextureNameEvaluator(pattern);
95         }
96 
contentFlagsEvaluator(const int value)97         BrushContentTypeEvaluator* BrushContentTypeEvaluator::contentFlagsEvaluator(const int value) {
98             return new ContentFlagsEvaluator(value);
99         }
100 
entityClassnameEvaluator(const String & pattern)101         BrushContentTypeEvaluator* BrushContentTypeEvaluator::entityClassnameEvaluator(const String& pattern) {
102             return new EntityClassnameEvaluator(pattern);
103         }
104 
evaluate(const Brush * brush) const105         bool BrushContentTypeEvaluator::evaluate(const Brush* brush) const {
106             return doEvaluate(brush);
107         }
108     }
109 }
110