2
Bear Engine - Level editor
4
Copyright (C) 20052011 Julien Jorge, Sebastien Angibaud
6
This program is free software; you can redistribute it and/or modify it
7
under the terms of the GNU General Public License as published by the
8
Free Software Foundation; either version 2 of the License, or (at your
9
option) any later version.
11
This program is distributed in the hope that it will be useful, but WITHOUT
12
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16
You should have received a copy of the GNU General Public License along
17
with this program; if not, write to the Free Software Foundation, Inc.,
18
51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20
contact: plee-the-bear@gamned.org
22
Please add the tag [Bear] in the subject of your mails.
25
* \file bf/configuration.cpp
26
* \brief Implementation of the bf::configuration class.
27
* \author Julien Jorge
29
#include "bf/configuration.hpp"
31
#include "bf/path_configuration.hpp"
33
#include <boost/filesystem/convenience.hpp>
37
/*----------------------------------------------------------------------------*/
38
const std::string bf::configuration::s_config_file_name = "level-editor.config";
40
const char bf::configuration::s_section_left = '[';
41
const char bf::configuration::s_section_right = ']';
42
const char bf::configuration::s_comment = '#';
43
const char bf::configuration::s_field_assign = '=';
45
const std::string bf::configuration::s_main_frame_section = "main_frame";
47
bf::configuration::s_properties_frame_section = "properties_frame";
49
bf::configuration::s_layer_list_frame_section = "layer_list_frame";
51
bf::configuration::s_item_class_pool_frame_section = "item_class_pool_frame";
53
bf::configuration::s_level_frame_section = "level_frame";
55
const std::string bf::configuration::s_x_field = "x";
56
const std::string bf::configuration::s_y_field = "y";
57
const std::string bf::configuration::s_width_field = "width";
58
const std::string bf::configuration::s_height_field = "height";
59
const std::string bf::configuration::s_visible_field = "visible";
61
/*----------------------------------------------------------------------------*/
65
bf::configuration::configuration()
68
} // configuration::configuration()
70
/*----------------------------------------------------------------------------*/
72
* \brief Load the configuration.
74
void bf::configuration::load()
76
if ( create_config_file() )
79
( path_configuration::get_instance().get_config_directory()
80
+ s_config_file_name );
82
std::ifstream f( path.c_str() );
86
claw::configuration_file config(f);
88
main_rect = read_rect( config, s_main_frame_section );
89
properties_visible = read_bool( config, s_properties_frame_section,
90
s_visible_field, true );
91
layer_list_visible = read_bool( config, s_layer_list_frame_section,
92
s_visible_field, true );
93
item_class_pool_visible =
94
read_bool( config, s_item_class_pool_frame_section,
95
s_visible_field, true );
97
default_level_window_size =
98
read_size( config, s_level_frame_section );
101
} // configuration::load()
103
/*----------------------------------------------------------------------------*/
105
* \brief Save the configuration.
107
void bf::configuration::save() const
109
if ( create_config_file() )
112
( path_configuration::get_instance().get_config_directory()
113
+ s_config_file_name );
115
std::ofstream f( path.c_str() );
119
f << '\n' << s_section_left << s_main_frame_section << s_section_right
120
<< '\n' << s_comment << " Position and size of the main window\n";
121
write_rect( f, main_rect );
124
f << s_comment << " Visibility of the item properties window\n"
125
<< s_visible_field << ' ' << s_field_assign << ' '
126
<< properties_visible << "\n\n";
128
f << s_comment << " Visibility of the layer list window\n"
129
<< s_visible_field << ' ' << s_field_assign << ' '
130
<< layer_list_visible << "\n\n";
132
f << s_comment << " Visibility of the item class pool window\n"
133
<< s_visible_field << ' ' << s_field_assign << ' '
134
<< item_class_pool_visible << "\n\n";
136
f << s_section_left << s_level_frame_section
137
<< s_section_right << '\n'
138
<< s_comment << " Size of the level frame\n";
139
write_size( f, default_level_window_size );
143
} // configuration::load()
145
/*----------------------------------------------------------------------------*/
147
* \brief Create the configuration file, if it does not exists.
148
* \return true if the file already exists or if it has been created.
150
bool bf::configuration::create_config_file() const
154
boost::filesystem::path path
155
( path_configuration::get_instance().get_config_directory()
156
+ s_config_file_name,
157
boost::filesystem::native );
159
if ( !boost::filesystem::exists( path ) )
161
std::ofstream f( path.string().c_str() );
162
f << s_comment << " Configuration file for Bear Factory - Level editor\n";
165
if ( boost::filesystem::exists( path ) )
166
result = !boost::filesystem::is_directory( path );
169
} // configuration::create_config_file()
171
/*----------------------------------------------------------------------------*/
173
* \brief Read a wxRect from a configuration file.
174
* \param config The configuration file.
175
* \param section The name of the section in which we get the rectangle.
177
wxRect bf::configuration::read_rect
178
( const claw::configuration_file& config, const std::string& section ) const
180
wxRect result( wxDefaultPosition, wxDefaultSize );
182
std::istringstream iss
183
( config(section, s_x_field) + ' ' + config(section, s_y_field) + ' ' +
184
config(section, s_width_field) + ' ' + config(section, s_height_field) );
186
iss >> result.x >> result.y >> result.width >> result.height;
189
} // configuration::read_rect()
191
/*----------------------------------------------------------------------------*/
193
* \brief Read a wxSize from a configuration file.
194
* \param config The configuration file.
195
* \param section The name of the section in which we get the size.
197
wxSize bf::configuration::read_size
198
( const claw::configuration_file& config, const std::string& section ) const
200
wxSize result( wxDefaultSize );
202
std::istringstream iss
203
( config(section, s_width_field) + ' ' + config(section, s_height_field) );
205
iss >> result.x >> result.y;
208
} // configuration::read_size()
210
/*----------------------------------------------------------------------------*/
212
* \brief Read a boolean value from a configuration file.
213
* \param config The configuration file.
214
* \param section The name of the section in which we get the bool.
215
* \param field_name The name of the field to read.
216
* \param default_value The default value in case of not defined.
218
bool bf::configuration::read_bool
219
( const claw::configuration_file& config, const std::string& section,
220
const std::string& field_name, bool default_value ) const
222
bool result = default_value;
223
std::istringstream iss( config(section, field_name) );
228
} // configuration::read_bool()
230
/*----------------------------------------------------------------------------*/
232
* \brief Write a wxRect in a file.
233
* \param f The file to write in.
234
* \param r The rectangle to write.
236
void bf::configuration::write_rect( std::ostream& f, const wxRect& r ) const
238
f << s_x_field << ' ' << s_field_assign << ' ' << r.x << '\n'
239
<< s_y_field << ' ' << s_field_assign << ' ' << r.y << '\n'
240
<< s_width_field << ' ' << s_field_assign << ' ' << r.width << '\n'
241
<< s_height_field << ' ' << s_field_assign << ' ' << r.height << '\n';
242
} // configuration::write_rect()
244
/*----------------------------------------------------------------------------*/
246
* \brief Write a wxSize in a file.
247
* \param f The file to write in.
248
* \param s The size to write.
250
void bf::configuration::write_size( std::ostream& f, const wxSize& s ) const
252
f << s_width_field << ' ' << s_field_assign << ' ' << s.x << '\n'
253
<< s_height_field << ' ' << s_field_assign << ' ' << s.y << '\n';
254
} // configuration::write_size()