Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/archive/tutorial2/src/orxonox/LevelInfo.cc @ 12373

Last change on this file since 12373 was 9016, checked in by jo, 13 years ago

Merging presentation2011 branch to trunk. Please check for possible bugs.

  • Property svn:eol-style set to native
File size: 5.9 KB
Line 
1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
3 *                    > www.orxonox.net <
4 *
5 *
6 *   License notice:
7 *
8 *   This program is free software; you can redistribute it and/or
9 *   modify it under the terms of the GNU General Public License
10 *   as published by the Free Software Foundation; either version 2
11 *   of the License, or (at your option) any later version.
12 *
13 *   This program is distributed in the hope that it will be useful,
14 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 *   GNU General Public License for more details.
17 *
18 *   You should have received a copy of the GNU General Public License
19 *   along with this program; if not, write to the Free Software
20 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21 *
22 *   Author:
23 *      Damian 'Mozork' Frick
24 *   Co-authors:
25 *      ...
26 *
27 */
28
29#include "LevelInfo.h"
30
31#include <sstream>
32#include <vector>
33
34#include "util/SubString.h"
35#include "core/CoreIncludes.h"
36#include "core/XMLPort.h"
37
38#include "LevelManager.h"
39
40namespace orxonox
41{
42
43    // LevelInfoItem
44   
45    //! The list of allowed tags.
46    /*static*/ std::set<std::string> LevelInfoItem::possibleTags_s = std::set<std::string>();
47
48    /**
49    @brief
50        Default constructor.
51    */
52    LevelInfoItem::LevelInfoItem()
53    {
54
55    }
56
57    /**
58    @brief
59        Constructor. Initializes the object.
60    @param name
61        The name of the Level.
62    @param filename
63        The XML-filename of the Level.
64    */
65    LevelInfoItem::LevelInfoItem(const std::string& name, const std::string filename)
66    {
67        this->setName(name);
68        this->setXMLFilename(filename);
69    }
70
71    /**
72    @brief
73        Destructor.
74    */
75    LevelInfoItem::~LevelInfoItem()
76    {
77
78    }
79
80    /**
81    @brief
82        Initialize the set of allowed tags.
83    */
84    /*static*/ void LevelInfoItem::initializeTags(void)
85    {
86        if(!LevelInfoItem::initialized_s)
87        {
88            LevelInfoItem::possibleTags_s.insert("test");
89            LevelInfoItem::possibleTags_s.insert("showcase");
90            LevelInfoItem::possibleTags_s.insert("tutorial");
91            LevelInfoItem::possibleTags_s.insert("presentation");
92            LevelInfoItem::possibleTags_s.insert("mission");
93            LevelInfoItem::possibleTags_s.insert("gametype");
94            LevelInfoItem::possibleTags_s.insert("minigame");
95        }
96    }
97
98    /**
99    @brief
100        Set the tags the Level is tagged with.
101    @param tags
102        A comma-seperated string of all the tags to be set.
103    */
104    void LevelInfoItem::setTags(const std::string& tags)
105    {
106        SubString substr = SubString(tags, ",", " "); // Split the string into tags.
107        const std::vector<std::string>& strings = substr.getAllStrings();
108        for(std::vector<std::string>::const_iterator it = strings.begin(); it != strings.end(); it++)
109            this->addTag(*it, false);
110
111        this->tagsUpdated();
112    }
113
114    /**
115    @brief
116        Add a tag to the set of tags the Level is tagged with.
117    @param tag
118        The tag to be added.
119    @param update
120        Whether the comma-seperated string of all tags should be updated. Default is true.
121    @return
122        Returns true if the tag was successfully added, if the tag was already present it returns false.
123    */
124    bool LevelInfoItem::addTag(const std::string& tag, bool update)
125    {
126        if(!this->validateTag(tag))
127        {
128            orxout(internal_warning) << "Bad LevelInfo tag '" << tag << "' in " << this->getXMLFilename() << ". Ignoring..." << endl;
129            return false;
130        }
131        bool success = this->tags_.insert(*LevelInfoItem::possibleTags_s.find(tag)).second;
132        if(update && success)
133            this->tagsUpdated();
134        return success;
135    }
136
137    /**
138    @brief
139        Updates the comma-seperated string of all tags, if the set of tags has changed.
140    */
141    void LevelInfoItem::tagsUpdated(void)
142    {
143        std::stringstream stream;
144        std::set<std::string>::iterator temp;
145        for(std::set<std::string>::iterator it = this->tags_.begin(); it != this->tags_.end(); )
146        {
147            temp = it;
148            if(++it == this->tags_.end()) // If this is the last tag we don't add a comma.
149                stream << *temp;
150            else
151                stream << *temp << ", ";
152        }
153
154        this->tagsString_ = std::string(stream.str());
155    }
156
157    // LevelInfo
158
159    CreateFactory(LevelInfo);
160
161    /**
162    @brief
163
164    @param creator
165        The creator of this object.
166    */
167    LevelInfo::LevelInfo(BaseObject* creator) : BaseObject(creator)
168    {
169        RegisterObject(LevelInfo);
170
171        this->xmlfilename_ = this->getFilename();
172    }
173
174    /**
175    @brief
176        Destructor.
177    */
178    LevelInfo::~LevelInfo()
179    {
180
181    }
182
183    /**
184    @brief
185        Creates a LevelInfo object through XML.
186    */
187    void LevelInfo::XMLPort(Element& xmlelement, XMLPort::Mode mode)
188    {
189        SUPER(LevelInfo, XMLPort, xmlelement, mode);
190
191        XMLPortParam(LevelInfo, "description", setDescription, getDescription, xmlelement, mode);
192        XMLPortParam(LevelInfo, "screenshot", setScreenshot, getScreenshot, xmlelement, mode);
193        XMLPortParam(LevelInfo, "tags", setTags, getTags, xmlelement, mode);
194    }
195
196    /**
197    @brief
198        Copies the contents of this LevelInfo object to a new LevelInfoItem object.
199        This is needed, because a LeveInfo object is only created within the scope of the XML-file it is loaded with and is destroyed once that is unloaded.
200    @return
201        Returns a new LevelInfoItem with the same contents as the LevelInfo object.
202    */
203    LevelInfoItem* LevelInfo::copy(void)
204    {
205        LevelInfoItem* info = new LevelInfoItem(this->BaseObject::getName(), this->getXMLFilename());
206        info->setDescription(this->getDescription());
207        info->setScreenshot(this->getScreenshot());
208        info->setTags(this->getTags());
209        return info;
210    }
211
212}
213
Note: See TracBrowser for help on using the repository browser.