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_MapReader 21 #define TrenchBroom_MapReader 22 23 #include "TrenchBroom.h" 24 #include "VecMath.h" 25 #include "IO/StandardMapParser.h" 26 #include "Model/ModelTypes.h" 27 28 namespace TrenchBroom { 29 namespace Model { 30 class ModelFactory; 31 } 32 33 namespace IO { 34 class MapReader : public StandardMapParser { 35 protected: 36 class ParentInfo { 37 public: 38 typedef enum { 39 Type_Layer, 40 Type_Group, 41 Type_None 42 } Type; 43 44 Type m_type; 45 Model::IdType m_id; 46 public: 47 static ParentInfo layer(Model::IdType layerId); 48 static ParentInfo group(Model::IdType groupId); 49 private: 50 ParentInfo(Type type, Model::IdType id); 51 public: 52 bool layer() const; 53 bool group() const; 54 Model::IdType id() const; 55 }; 56 private: 57 typedef enum { 58 EntityType_Layer, 59 EntityType_Group, 60 EntityType_Worldspawn, 61 EntityType_Default 62 } EntityType; 63 64 typedef std::map<Model::IdType, Model::Layer*> LayerMap; 65 typedef std::map<Model::IdType, Model::Group*> GroupMap; 66 67 typedef std::pair<Model::Node*, ParentInfo> NodeParentPair; 68 typedef std::vector<NodeParentPair> NodeParentList; 69 70 BBox3 m_worldBounds; 71 Model::ModelFactory* m_factory; 72 73 Model::Node* m_brushParent; 74 Model::Node* m_currentNode; 75 Model::BrushFaceList m_faces; 76 77 LayerMap m_layers; 78 GroupMap m_groups; 79 NodeParentList m_unresolvedNodes; 80 protected: 81 MapReader(const char* begin, const char* end, Logger* logger = NULL); 82 MapReader(const String& str, Logger* logger = NULL); 83 84 void readEntities(Model::MapFormat::Type format, const BBox3& worldBounds); 85 void readBrushes(Model::MapFormat::Type format, const BBox3& worldBounds); 86 void readBrushFaces(Model::MapFormat::Type format, const BBox3& worldBounds); 87 public: 88 virtual ~MapReader(); 89 private: // implement MapParser interface 90 void onFormatSet(Model::MapFormat::Type format); 91 void onBeginEntity(size_t line, const Model::EntityAttribute::List& attributes, const ExtraAttributes& extraAttributes); 92 void onEndEntity(size_t startLine, size_t lineCount); 93 void onBeginBrush(size_t line); 94 void onEndBrush(size_t startLine, size_t lineCount, const ExtraAttributes& extraAttributes); 95 void onBrushFace(size_t line, const Vec3& point1, const Vec3& point2, const Vec3& point3, const Model::BrushFaceAttributes& attribs, const Vec3& texAxisX, const Vec3& texAxisY); 96 private: // helper methods 97 void createLayer(size_t line, const Model::EntityAttribute::List& attributes, const ExtraAttributes& extraAttributes); 98 void createGroup(size_t line, const Model::EntityAttribute::List& attributes, const ExtraAttributes& extraAttributes); 99 void createEntity(size_t line, const Model::EntityAttribute::List& attributes, const ExtraAttributes& extraAttributes); 100 void createBrush(size_t startLine, size_t lineCount, const ExtraAttributes& extraAttributes); 101 102 ParentInfo::Type storeNode(Model::Node* node, const Model::EntityAttribute::List& attributes); 103 void stripParentAttributes(Model::AttributableNode* attributable, ParentInfo::Type parentType); 104 105 void resolveNodes(); 106 Model::Node* resolveParent(const ParentInfo& parentInfo) const; 107 108 EntityType entityType(const Model::EntityAttribute::List& attributes) const; 109 110 void setFilePosition(Model::Node* node, size_t startLine, size_t lineCount); 111 protected: 112 void setExtraAttributes(Model::Node* node, const ExtraAttributes& extraAttributes); 113 private: // subclassing interface 114 virtual Model::ModelFactory* initialize(Model::MapFormat::Type format, const BBox3& worldBounds) = 0; 115 virtual Model::Node* onWorldspawn(const Model::EntityAttribute::List& attributes, const ExtraAttributes& extraAttributes) = 0; 116 virtual void onWorldspawnFilePosition(size_t startLine, size_t lineCount) = 0; 117 virtual void onLayer(Model::Layer* layer) = 0; 118 virtual void onNode(Model::Node* parent, Model::Node* node) = 0; 119 virtual void onUnresolvedNode(const ParentInfo& parentInfo, Model::Node* node) = 0; 120 virtual void onBrush(Model::Node* parent, Model::Brush* brush) = 0; 121 virtual void onBrushFace(Model::BrushFace* face); 122 }; 123 } 124 } 125 126 #endif /* defined(TrenchBroom_MapReader) */ 127