~ubuntu-branches/ubuntu/natty/plee-the-bear/natty

« back to all changes in this revision

Viewing changes to bear-factory/model-editor/src/bf/code/model_editor.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Julien Jorge, Julien Jorge
  • Date: 2010-11-17 20:13:34 UTC
  • mfrom: (6.1.1 sid)
  • Revision ID: james.westby@ubuntu.com-20101117201334-o4dp7uq437to7oxb
Tags: 0.5.1-1
[ Julien Jorge ]
* New upstream release (Closes: #565062, #546514).
* Add armhf architecture (Closes: #604689).
* Remove patches integrated upstream: rpath-editors.diff, rpath-game.diff,
  editors-menu-section.diff.
* Bump the Standard-Version, no changes.
* Update my email address.
* Set build dependency of libclaw to 1.6.0.
* Add the missing ${misc:Depends} in debian/control.
* List gettext translations in bear-factory.install and plee-the-bear.install.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
2
    Bear Engine - model editor
3
3
 
4
 
    Copyright (C) 2005-2009 Julien Jorge, Sebastien Angibaud
 
4
    Copyright (C) 2005-2010 Julien Jorge, Sebastien Angibaud
5
5
 
6
6
    This program is free software; you can redistribute it and/or modify it
7
7
    under the terms of the GNU General Public License as published by the
37
37
 
38
38
#include <wx/tooltip.h>
39
39
#include <claw/logger.hpp>
 
40
#include <claw/exception.hpp>
40
41
 
41
42
/*----------------------------------------------------------------------------*/
42
43
/**
43
44
 * \brief Constructor.
44
45
 */
45
46
bf::model_editor::model_editor()
46
 
  : m_locale( wxLocale::GetSystemLanguage() ), m_main_frame(NULL)
 
47
  : m_main_frame(NULL)
47
48
{
48
 
  claw::logger.set( new claw::console_logger() );
49
 
  claw::logger.set_level( claw::log_verbose );
50
49
 
51
 
  m_locale.AddCatalog( wxT("bear-factory") );
52
50
} // model_editor::model_editor()
53
51
 
54
52
/*----------------------------------------------------------------------------*/
55
53
/**
56
 
 * \brief Destructor.
57
 
 */
58
 
bf::model_editor::~model_editor()
59
 
{
60
 
  // frames are deleted by wxWidgets
61
 
 
62
 
  claw::logger.clear();
63
 
} // model_editor::~model_editor()
64
 
 
65
 
/*----------------------------------------------------------------------------*/
66
 
/**
67
 
 * \brief Method called when the application is initializing.
68
 
 */
69
 
bool bf::model_editor::OnInit()
70
 
{
71
 
  bool result = false;
72
 
 
73
 
  if ( !show_help() )
74
 
    if ( !show_version() )
75
 
      {
76
 
        if ( find_and_erase_option( wxT("--compile"), wxT("-c") ) )
77
 
          compile_arguments();
78
 
        else
79
 
          {
80
 
            result = true;
81
 
            init_app();
82
 
          }
83
 
      }
84
 
 
85
 
  return result;
86
 
} // model_editor::OnInit()
87
 
 
88
 
/*----------------------------------------------------------------------------*/
89
 
/**
90
54
 * \brief Initialize the application.
91
55
 */
92
 
void bf::model_editor::init_app()
 
56
bool bf::model_editor::do_init_app()
93
57
{
94
 
  wxToolTip::Enable(true);
95
 
 
96
58
  m_main_frame = new main_frame;
97
59
  m_main_frame->Show();
98
60
 
99
61
  for (int i=1; i<argc; ++i)
100
62
    if ( wxString(argv[i]) != wxT("--") )
101
 
      m_main_frame->load_model( argv[i] );
 
63
      try
 
64
        {
 
65
          m_main_frame->load_model( argv[i] );
 
66
        }
 
67
      catch( std::ios_base::failure& e )
 
68
        {
 
69
          claw::logger << claw::log_error << e.what()
 
70
                       << "\nCreating a new model." << std::endl;
 
71
          m_main_frame->new_model( argv[i] );
 
72
        }
 
73
      catch( std::exception& e )
 
74
        {
 
75
          claw::logger << claw::log_error << e.what() << std::endl;
 
76
        }
 
77
 
 
78
  return true;
102
79
} // model_editor::init_app()
103
80
 
104
81
/*----------------------------------------------------------------------------*/
105
82
/**
106
 
 * \brief Compile the models and exit.
107
 
 */
108
 
bool bf::model_editor::compile_arguments()
109
 
{
110
 
  bool result(true);
111
 
 
112
 
  for (int i=1; i<argc; ++i)
113
 
    if ( wxString(argv[i]) != wxT("--") )
114
 
      {
115
 
        bool ok(false);
116
 
        model* mdl(NULL);
117
 
 
118
 
        try
119
 
          {
120
 
            xml::model_file reader;
121
 
            mdl = reader.load(argv[i]);
122
 
 
123
 
            if ( check_model(*mdl) )
124
 
              ok = compile_model(*mdl, argv[i]);
125
 
          }
126
 
        catch(...)
127
 
          { }
128
 
 
129
 
        delete mdl;
130
 
 
131
 
        if ( !ok )
132
 
          {
133
 
            std::cerr << "Error when processing '" << argv[i] << '\''
134
 
                      << std::endl;
135
 
            result = false;
136
 
          }
137
 
      }
138
 
 
139
 
  return result;
140
 
} // model_editor::compile_arguments()
 
83
 * \brief Compile a model.
 
84
 * \param path The path to the model file.
 
85
 */
 
86
void bf::model_editor::compile( const wxString& path ) const
 
87
{
 
88
  model* mdl(NULL);
 
89
 
 
90
  try
 
91
    {
 
92
      xml::model_file reader;
 
93
      mdl = reader.load(path);
 
94
 
 
95
      if ( check_model(*mdl) )
 
96
        {
 
97
          compile_model(*mdl, path);
 
98
          delete mdl;
 
99
        }
 
100
      else
 
101
        {
 
102
          delete mdl;
 
103
          CLAW_EXCEPTION("Invalid model.");
 
104
        }
 
105
    }
 
106
  catch(...)
 
107
    {
 
108
      delete mdl;
 
109
      throw;
 
110
    }
 
111
} // model_editor::compile()
 
112
 
 
113
/*----------------------------------------------------------------------------*/
 
114
/**
 
115
 * \brief Update a model.
 
116
 * \param path The path to the model file.
 
117
 */
 
118
void bf::model_editor::update( const wxString& path ) const
 
119
{
 
120
  gui_model* mdl(NULL);
 
121
 
 
122
  try
 
123
    {
 
124
      xml::model_file mf;
 
125
      mdl = mf.load(path);
 
126
 
 
127
      std::ofstream f( wx_to_std_string(path).c_str() );
 
128
      mf.save(*mdl, f);
 
129
 
 
130
      delete mdl;
 
131
    }
 
132
  catch(...)
 
133
    {
 
134
      delete mdl;
 
135
      throw;
 
136
    }
 
137
} // model_editor::update()
141
138
 
142
139
/*----------------------------------------------------------------------------*/
143
140
/**
144
141
 * \brief Check if a model is valid.
145
142
 * \param mdl The model to check.
146
143
 */
147
 
bool bf::model_editor::check_model( const model& mdl )
 
144
bool bf::model_editor::check_model( const model& mdl ) const
148
145
{
149
146
  bool result(true);
150
147
  model::const_action_iterator it;
151
148
 
152
149
  for ( it=mdl.action_begin(); it!=mdl.action_end(); ++it )
153
 
    if ( !it->get_auto_next().empty() ) 
 
150
    if ( !it->get_auto_next().empty() )
154
151
      if ( !mdl.has_action( it->get_auto_next() )  )
155
152
        {
156
153
          result = false;
157
 
          std::cerr << "Unknown action '" << it->get_auto_next() << '\''
158
 
                    << std::endl;
 
154
          claw::logger << claw::log_error << "Unknown action '"
 
155
                       << it->get_auto_next() << '\'' << std::endl;
159
156
        }
160
157
 
161
158
  return result;
169
166
 * \return true if the compilation went ok.
170
167
 */
171
168
bool
172
 
bf::model_editor::compile_model( const model& mdl, const wxString& path )
 
169
bf::model_editor::compile_model( const model& mdl, const wxString& path ) const
173
170
{
174
171
  bool result(true);
175
172
 
197
194
 
198
195
  return result;
199
196
} // model_editor::compile_model()
200
 
 
201
 
/*----------------------------------------------------------------------------*/
202
 
/**
203
 
 * \brief Show the command line usage.
204
 
 * \return true if the usage has been shown.
205
 
 */
206
 
bool bf::model_editor::show_help()
207
 
{
208
 
  if ( find_and_erase_option(wxT("--help"), wxT("-h")) )
209
 
    {
210
 
      std::cout << "usage:\n" << wx_to_std_string(argv[0])
211
 
                << " [option] [file...]\n"
212
 
        "Where the options are:\n\n"
213
 
        "\t--compile, -c\tCompile the models and exit,\n"
214
 
        "\t--help, -h\tDisplay this help and exit,\n"
215
 
        "\t--version, -v\tDisplay the version of the program and exit."
216
 
                << std::endl;
217
 
      return true;
218
 
    }
219
 
  else
220
 
    return false;
221
 
} // model_editor::show_help()
222
 
 
223
 
/*----------------------------------------------------------------------------*/
224
 
/**
225
 
 * \brief Show the version of the program
226
 
 * \return true if the version has been shown.
227
 
 */
228
 
bool bf::model_editor::show_version()
229
 
{
230
 
  if ( find_and_erase_option(wxT("--version"), wxT("-v")) )
231
 
    {
232
 
      std::cout << BF_VERSION_STRING << std::endl;
233
 
      return true;
234
 
    }
235
 
  else
236
 
    return false;
237
 
} // model_editor::show_version()
238
 
 
239
 
/*----------------------------------------------------------------------------*/
240
 
/**
241
 
 * \brief Check if an option is present on the command line and remove it.
242
 
 * \param long_name The long name of the option.
243
 
 * \param show_name The short name of the option.
244
 
 */
245
 
bool bf::model_editor::find_and_erase_option
246
 
( const wxString& long_name, const wxString& short_name )
247
 
{
248
 
  int index(0);
249
 
  bool stop(false);
250
 
 
251
 
  for (int i=1; !stop && (index==0) && (i<argc); ++i)
252
 
    if ( (argv[i] == long_name) || (argv[i] == short_name))
253
 
      index = i;
254
 
    else
255
 
      stop = wxString(argv[i]) == wxT("--");
256
 
 
257
 
  if ( index != 0 )
258
 
    {
259
 
      for ( int i=index; (i+1 != argc); ++i )
260
 
        argv[i] = argv[i+1];
261
 
 
262
 
      --argc;
263
 
      argv[argc] = NULL;
264
 
    }
265
 
 
266
 
  return index != 0;
267
 
} // model_editor::find_and_erase_option()