~ubuntu-branches/ubuntu/trusty/plee-the-bear/trusty-proposed

« back to all changes in this revision

Viewing changes to .pc/ptb-filesystem-v3.diff/bear-factory/level-editor/src/bf/code/configuration.cpp

  • Committer: Package Import Robot
  • Author(s): Vincent Cheng, Evgeni Golov, Gonéri Le Bouder, Julien Jorge, Vincent Cheng
  • Date: 2014-01-23 13:20:52 UTC
  • mfrom: (6.1.3 sid)
  • Revision ID: package-import@ubuntu.com-20140123132052-r0kg74mh6egto11t
Tags: 0.6.0-2
* Team upload.

[ Evgeni Golov ]
* Correct Vcs-* URLs to point to anonscm.debian.org

[ Gonéri Le Bouder ]
* import patches by Julien Jorge to fix a FTBFS  with the current
  Boost.FileSystem (closes: #720819)
* Add myself in Uploaders
* Indent the B-D

[ Julien Jorge ]
* Add mipsn32el mips64 mips64el in the architecures (closes: #726176)
* Add a patch to use the full path to the icon in the menu files
  (closes: #726853)

[ Vincent Cheng ]
* Refresh patches.
* Update to Standards version 3.9.5.
* Update to source format "3.0 (quilt)".

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
    Bear Engine - Level editor
 
3
 
 
4
    Copyright (C) 20052011 Julien Jorge, Sebastien Angibaud
 
5
 
 
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.
 
10
 
 
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
 
14
    more details.
 
15
 
 
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
 
19
 
 
20
    contact: plee-the-bear@gamned.org
 
21
 
 
22
    Please add the tag [Bear] in the subject of your mails.
 
23
*/
 
24
/**
 
25
 * \file bf/configuration.cpp
 
26
 * \brief Implementation of the bf::configuration class.
 
27
 * \author Julien Jorge
 
28
 */
 
29
#include "bf/configuration.hpp"
 
30
 
 
31
#include "bf/path_configuration.hpp"
 
32
 
 
33
#include <boost/filesystem/convenience.hpp>
 
34
#include <fstream>
 
35
#include <sstream>
 
36
 
 
37
/*----------------------------------------------------------------------------*/
 
38
const std::string bf::configuration::s_config_file_name = "level-editor.config";
 
39
 
 
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 = '=';
 
44
 
 
45
const std::string bf::configuration::s_main_frame_section = "main_frame";
 
46
const std::string
 
47
bf::configuration::s_properties_frame_section = "properties_frame";
 
48
const std::string
 
49
bf::configuration::s_layer_list_frame_section = "layer_list_frame";
 
50
const std::string
 
51
bf::configuration::s_item_class_pool_frame_section = "item_class_pool_frame";
 
52
const std::string
 
53
bf::configuration::s_level_frame_section = "level_frame";
 
54
 
 
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";
 
60
 
 
61
/*----------------------------------------------------------------------------*/
 
62
/**
 
63
 * \brief Constructor.
 
64
 */
 
65
bf::configuration::configuration()
 
66
{
 
67
  load();
 
68
} // configuration::configuration()
 
69
 
 
70
/*----------------------------------------------------------------------------*/
 
71
/**
 
72
 * \brief Load the configuration.
 
73
 */
 
74
void bf::configuration::load()
 
75
{
 
76
  if ( create_config_file() )
 
77
    {
 
78
      std::string path
 
79
        ( path_configuration::get_instance().get_config_directory()
 
80
          + s_config_file_name );
 
81
 
 
82
      std::ifstream f( path.c_str() );
 
83
 
 
84
      if (f)
 
85
        {
 
86
          claw::configuration_file config(f);
 
87
 
 
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 );
 
96
 
 
97
          default_level_window_size =
 
98
            read_size( config, s_level_frame_section );
 
99
        }
 
100
    }
 
101
} // configuration::load()
 
102
 
 
103
/*----------------------------------------------------------------------------*/
 
104
/**
 
105
 * \brief Save the configuration.
 
106
 */
 
107
void bf::configuration::save() const
 
108
{
 
109
  if ( create_config_file() )
 
110
    {
 
111
      std::string path
 
112
        ( path_configuration::get_instance().get_config_directory()
 
113
          + s_config_file_name );
 
114
 
 
115
      std::ofstream f( path.c_str() );
 
116
 
 
117
      if (f)
 
118
        {
 
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 );
 
122
          f << '\n';
 
123
 
 
124
          f << s_comment << " Visibility of the item properties window\n"
 
125
            << s_visible_field << ' ' << s_field_assign << ' '
 
126
            << properties_visible << "\n\n";
 
127
 
 
128
          f << s_comment << " Visibility of the layer list window\n"
 
129
            << s_visible_field << ' ' << s_field_assign << ' '
 
130
            << layer_list_visible << "\n\n";
 
131
 
 
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";
 
135
 
 
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 );
 
140
          f << '\n';
 
141
        }
 
142
    }
 
143
} // configuration::load()
 
144
 
 
145
/*----------------------------------------------------------------------------*/
 
146
/**
 
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.
 
149
 */
 
150
bool bf::configuration::create_config_file() const
 
151
{
 
152
  bool result = false;
 
153
 
 
154
  boost::filesystem::path path
 
155
    ( path_configuration::get_instance().get_config_directory()
 
156
      + s_config_file_name,
 
157
      boost::filesystem::native );
 
158
 
 
159
  if ( !boost::filesystem::exists( path ) )
 
160
    {
 
161
      std::ofstream f( path.string().c_str() );
 
162
      f << s_comment << " Configuration file for Bear Factory - Level editor\n";
 
163
    }
 
164
 
 
165
  if ( boost::filesystem::exists( path ) )
 
166
    result = !boost::filesystem::is_directory( path );
 
167
 
 
168
  return result;
 
169
} // configuration::create_config_file()
 
170
 
 
171
/*----------------------------------------------------------------------------*/
 
172
/**
 
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.
 
176
 */
 
177
wxRect bf::configuration::read_rect
 
178
( const claw::configuration_file& config, const std::string& section ) const
 
179
{
 
180
  wxRect result( wxDefaultPosition, wxDefaultSize );
 
181
 
 
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) );
 
185
 
 
186
  iss >> result.x >> result.y >> result.width >> result.height;
 
187
 
 
188
  return result;
 
189
} // configuration::read_rect()
 
190
 
 
191
/*----------------------------------------------------------------------------*/
 
192
/**
 
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.
 
196
 */
 
197
wxSize bf::configuration::read_size
 
198
( const claw::configuration_file& config, const std::string& section ) const
 
199
{
 
200
  wxSize result( wxDefaultSize );
 
201
 
 
202
  std::istringstream iss
 
203
    ( config(section, s_width_field) + ' ' + config(section, s_height_field) );
 
204
 
 
205
  iss >> result.x >> result.y;
 
206
 
 
207
  return result;
 
208
} // configuration::read_size()
 
209
 
 
210
/*----------------------------------------------------------------------------*/
 
211
/**
 
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.
 
217
 */
 
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
 
221
{
 
222
  bool result = default_value;
 
223
  std::istringstream iss( config(section, field_name) );
 
224
 
 
225
  iss >> result;
 
226
 
 
227
  return result;
 
228
} // configuration::read_bool()
 
229
 
 
230
/*----------------------------------------------------------------------------*/
 
231
/**
 
232
 * \brief Write a wxRect in a file.
 
233
 * \param f The file to write in.
 
234
 * \param r The rectangle to write.
 
235
 */
 
236
void bf::configuration::write_rect( std::ostream& f, const wxRect& r ) const
 
237
{
 
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()
 
243
 
 
244
/*----------------------------------------------------------------------------*/
 
245
/**
 
246
 * \brief Write a wxSize in a file.
 
247
 * \param f The file to write in.
 
248
 * \param s The size to write.
 
249
 */
 
250
void bf::configuration::write_size( std::ostream& f, const wxSize& s ) const
 
251
{
 
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()