Umasoft
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Groups Pages
Content.h
1 //
2 // Content.h
3 //
4 // Header for the Content Management classes.
5 //
6 // Copyright (c) 2001-2006 Virtual Terrain Project.
7 // Free for all uses, see license.txt for details.
8 //
9 
10 #ifndef VTDATA_CONTENTH
11 #define VTDATA_CONTENTH
12 
13 #include "xmlhelper/easyxml.hpp"
14 #include "vtString.h"
15 #include "MathTypes.h"
16 
21 class vtModel
22 {
23 public:
24  vtModel()
25  {
26  m_distance = 0.0f;
27  m_scale = 1.0f;
28  m_attempted_load = false;
29  }
30 
31  vtString m_filename;
32  float m_distance;
33  float m_scale; // meters per unit (e.g. cm = .01)
34  bool m_attempted_load;
35 };
36 
41 class vtTag
42 {
43 public:
44  vtString name;
45  vtString value;
46 
47  bool operator==(const vtTag &v) const
48  {
49  return (name == v.name && value == v.value);
50  }
51  bool operator!=(const vtTag &v) const
52  {
53  return (name != v.name || value != v.value);
54  }
55 };
56 
65 {
66 public:
67  virtual ~vtTagArray() {}
68 
69  void AddTag(const vtTag &pTag);
70  void AddTag(const char *name, const char *value);
71 
72  vtTag *FindTag(const char *szTagName);
73  const vtTag *FindTag(const char *szTagName) const;
74 
75  vtTag *GetTag(int index);
76  const vtTag *GetTag(int index) const;
77 
78  uint NumTags() const;
79  void RemoveTag(int index);
80  void RemoveTag(const char *szTagName);
81  void Clear();
82 
83  // Set value
84  void SetValueString(const char *szTagName, const vtString &string, bool bCreating = false);
85  void SetValueBool(const char *szTagName, bool value, bool bCreating = false);
86  void SetValueInt(const char *szTagName, int value, bool bCreating = false);
87  void SetValueFloat(const char *szTagName, float value, bool bCreating = false);
88  void SetValueDouble(const char *szTagName, double value, bool bCreating = false);
89  void SetValueRGBi(const char *szTagName, const RGBi &value, bool bCreating = false);
90 
91  // Get value directly
92  const char *GetValueString(const char *szTagName, bool bSuppressWarning = false) const;
93  bool GetValueBool(const char *szTagName) const;
94  int GetValueInt(const char *szTagName) const;
95  float GetValueFloat(const char *szTagName) const;
96  double GetValueDouble(const char *szTagName) const;
97  RGBi GetValueRGBi(const char *szTagName) const;
98 
99  // Get by reference
100  bool GetValueString(const char *szTagName, vtString &string) const;
101  bool GetValueBool(const char *szTagName, bool &bValue) const;
102  bool GetValueInt(const char *szTagName, int &iValue) const;
103  bool GetValueFloat(const char *szTagName, float &fValue) const;
104  bool GetValueDouble(const char *szTagName, double &dValue) const;
105  bool GetValueRGBi(const char *szTagName, RGBi &color) const;
106 
107  // Operators
108  vtTagArray &operator=(const vtTagArray &v);
109  bool operator==(const vtTagArray &v) const;
110  bool operator!=(const vtTagArray &v) const;
111 
112  // Copy each tag from one array to another
113  void CopyTagsFrom(const vtTagArray &v);
114 
115  // File IO
116  bool WriteToXML(const char *fname, const char *title) const;
117  void WriteToXMLBody(FILE *fp, int iIndent) const;
118  bool LoadFromXML(const char *fname);
119 
120  // Allow overriding values by subclasses
121  virtual bool OverrideValue(const char *szTagName, const vtString &string)
122  {
123  return false;
124  }
125  virtual void WriteOverridesToXML(FILE *fp) const {}
126 
127  // Debugging info
128  static void SetVerbose(bool value);
129  void LogTags() const;
130 
131 protected:
132  std::vector<vtTag> m_tags;
133  static bool s_bVerbose; // display debugging info
134 };
135 
136 
138 // Visitor class for XML parsing of TagArray files.
139 
140 class TagVisitor : public XMLVisitor
141 {
142 public:
143  TagVisitor(vtTagArray *pArray) : m_level(0), m_pArray(pArray) {}
144  void startElement(const char *name, const XMLAttributes &atts);
145  void endElement(const char *name);
146  void data(const char *s, int length);
147 
148 protected:
149  int m_level;
150  string m_data;
151  vtTagArray *m_pArray;
152 };
153 
154 
160 class vtItem : public vtTagArray
161 {
162 public:
163  vtItem();
164  virtual ~vtItem();
165 
166  void Empty();
167  void AddModel(vtModel *item) { m_models.Append(item); }
168  void RemoveModel(vtModel *model);
169  uint NumModels() { return m_models.GetSize(); }
170  vtModel *GetModel(int i) { return m_models.GetAt(i); }
171 
172  vtString m_name;
173  FRECT m_extents;
174 
175 protected:
176  vtArray<vtModel*> m_models;
177 };
178 
192 {
193 public:
194  virtual ~vtContentManager();
195 
196  void ReadXML(const char *filename);
197  void WriteXML(const char *filename) const;
198 
199  void Empty();
200  void AddItem(vtItem *item) { m_items.Append(item); }
201  void RemoveItem(vtItem *item);
202  uint NumItems() { return m_items.GetSize(); }
203  virtual vtItem *NewItem() { return new vtItem; }
204  vtItem *GetItem(int i) { return m_items.GetAt(i); }
205  vtItem *FindItemByName(const char *name);
206  vtItem *FindItemByType(const char *type, const char *subtype);
207  vtString GetFilename() { return m_strFilename; }
208 
209 protected:
210  vtArray<vtItem*> m_items;
211  vtString m_strFilename;
212 };
213 
214 #endif // VTDATA_CONTENTH
215