home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2009 February / maximum-cd-2009-02.iso / DiscContents / SMC_1.6_win32.exe / src / overworld / world_editor.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2008-06-27  |  5.3 KB  |  267 lines

  1. /***************************************************************************
  2.  * world_editor.cpp  -  class for the World Editor
  3.  *
  4.  * Copyright (C) 2007 - 2008 Florian Richter
  5.  ***************************************************************************/
  6. /*
  7.    This program is free software; you can redistribute it and/or modify
  8.    it under the terms of the GNU General Public License as published by
  9.    the Free Software Foundation; either version 3 of the License, or
  10.    (at your option) any later version.
  11.    
  12.    You should have received a copy of the GNU General Public License
  13.    along with this program.  If not, see <http://www.gnu.org/licenses/>.
  14. */
  15.  
  16. #include "../core/globals.h"
  17. #include "../overworld/world_editor.h"
  18. #include "../core/game_core.h"
  19. #include "../overworld/overworld.h"
  20. #include "../input/keyboard.h"
  21. #include "../core/sprite_manager.h"
  22. #include "../audio/audio.h"
  23. #include "../core/i18n.h"
  24.  
  25. /* *** *** *** *** *** *** *** cEditor_World *** *** *** *** *** *** *** *** *** *** */
  26.  
  27. cEditor_World :: cEditor_World( void )
  28. : cEditor()
  29. {
  30.     menu_filename = DATA_DIR "/" GAME_EDITOR_DIR "/world_menu.xml";
  31.     items_filename = DATA_DIR "/" GAME_EDITOR_DIR "/world_items.xml";
  32.  
  33.     editor_item_tag = "world";
  34.     camera_speed = 20;
  35. }
  36.  
  37. cEditor_World :: ~cEditor_World( void )
  38. {
  39.     //
  40. }
  41.  
  42. void cEditor_World :: Init( void )
  43. {
  44.     // already loaded
  45.     if( editor_window )
  46.     {
  47.         return;
  48.     }
  49.  
  50.     // nothing
  51.  
  52.     cEditor::Init();
  53. }
  54.  
  55. void cEditor_World :: Enable( void )
  56. {
  57.     // already enabled
  58.     if( enabled )
  59.     {
  60.         return;
  61.     }
  62.     
  63.     editor_world_enabled = 1;
  64.     pOverworld_Manager->draw_layer = 1;
  65.  
  66.     if( Game_Mode == MODE_OVERWORLD )
  67.     {
  68.         editor_enabled = 1;
  69.     }
  70.     
  71.     cEditor::Enable();
  72. }
  73.  
  74. void cEditor_World :: Disable( bool native_mode /* = 0 */ )
  75. {
  76.     // already disabled
  77.     if( !enabled )
  78.     {
  79.         return;
  80.     }
  81.  
  82.     debugdisplay->Set_Text( _("World Editor disabled") );
  83.  
  84.     editor_world_enabled = 0;
  85.     pOverworld_Manager->draw_layer = 0;
  86.     pOverworld_Manager->cameramode = 0;
  87.  
  88.     if( Game_Mode == MODE_OVERWORLD )
  89.     {
  90.         native_mode = 1;
  91.         editor_enabled = 0;
  92.     }
  93.  
  94.     cEditor::Disable( native_mode );
  95. }
  96.  
  97. bool cEditor_World :: Key_Down( SDLKey key )
  98. {
  99.     if( !enabled )
  100.     {
  101.         return 0;
  102.     }
  103.  
  104.  
  105.     // check basic editor events
  106.     if( cEditor::Key_Down( key ) )
  107.     {
  108.         return 1;
  109.     }
  110.     // save level
  111.     else if( key == SDLK_s && ( input_event.key.keysym.mod & KMOD_LCTRL || input_event.key.keysym.mod & KMOD_RCTRL ) )
  112.     {
  113.         pActive_Overworld->Save();
  114.     }
  115.     else
  116.     {
  117.         // not processed
  118.         return 0;
  119.     }
  120.  
  121.     // key got processed
  122.     return 1;
  123. }
  124.  
  125. void cEditor_World :: Activate_Menu( cEditor_Menu_Object *entry )
  126. {
  127.     // If Function
  128.     if( entry->bfunction )
  129.     {
  130.         if( entry->tags.compare( "new" ) == 0 )
  131.         {
  132.             Function_New();
  133.         }
  134.         else if( entry->tags.compare( "load" ) == 0 )
  135.         {
  136.             Function_Load();
  137.         }
  138.         else if( entry->tags.compare( "save" ) == 0 )
  139.         {
  140.             Function_Save();
  141.         }
  142.         /*else if( entry->tags.compare( "save_as" ) == 0 )
  143.         {
  144.             Function_Save_as();
  145.         }*/
  146.         else if( entry->tags.compare( "reload" ) == 0 )
  147.         {
  148.             Function_Reload();
  149.         }
  150.         else if( entry->tags.compare( "clear" ) == 0 )
  151.         {
  152.             Function_Clear();
  153.         }
  154.         /*else if( entry->tags.compare( "settings" ) == 0 )
  155.         {
  156.             Function_Settings();
  157.         }*/
  158.         // unknown level function
  159.         else
  160.         {
  161.             cEditor::Activate_Menu( entry );
  162.         }
  163.     }
  164.     // unknown level function
  165.     else
  166.     {
  167.         cEditor::Activate_Menu( entry );
  168.     }
  169. }
  170.  
  171. bool cEditor_World :: Function_New( void )
  172. {
  173.     string world_name = Box_Text_Input( _("Create a new World"), _("Name") );
  174.  
  175.     // aborted/invalid
  176.     if( world_name.empty() )
  177.     {
  178.         return 0;
  179.     }
  180.  
  181.     if( pOverworld_Manager->New( world_name ) )
  182.     {
  183.         debugdisplay->Set_Text( _("Created ") + world_name );
  184.         return 1;
  185.     }
  186.     else
  187.     {
  188.         debugdisplay->Set_Text( _("World ") + world_name + _(" already exists") );
  189.     }
  190.  
  191.     return 0;
  192. }
  193.  
  194. void cEditor_World :: Function_Load( void )
  195. {
  196.     string world_name = _("Name");
  197.  
  198.     // valid world
  199.     while( world_name.length() )
  200.     {
  201.         world_name = Box_Text_Input( world_name, _("Load an Overworld"), world_name.compare( _("Name") ) == 0 ? 1 : 0 );
  202.  
  203.         // break if empty
  204.         if( world_name.empty() )
  205.         {
  206.             break;
  207.         }
  208.  
  209.         cOverworld *new_world = pOverworld_Manager->Get( world_name );
  210.  
  211.         // success
  212.         if( new_world )
  213.         {
  214.             Game_Action = GA_ENTER_WORLD;
  215.             Game_Action_Data.add( "world", world_name.c_str() );
  216.  
  217.             debugdisplay->Set_Text( _("Loaded ") + world_name );
  218.             break;
  219.         }
  220.         // failed
  221.         else
  222.         {
  223.             pAudio->Play_Sound( "error.ogg" );
  224.         }
  225.     }
  226. }
  227.  
  228. void cEditor_World :: Function_Save( bool with_dialog /* = 0 */ )
  229. {
  230.     // if denied
  231.     if( with_dialog && !Box_Question( _("Save ") + Get_Filename( pActive_Overworld->description->name, 0, 0 ) + " ?" ) )
  232.     {
  233.         return;
  234.     }
  235.  
  236.     pActive_Overworld->Save();
  237. }
  238.  
  239. void cEditor_World :: Function_Reload( void )
  240. {
  241.     // if denied
  242.     if( !Box_Question( _("Reload World ?") ) )
  243.     {
  244.         return;
  245.     }
  246.  
  247.     pActive_Overworld->Save();
  248.     pActive_Overworld->Load();
  249. }
  250.  
  251. void cEditor_World :: Function_Clear( void )
  252. {
  253.     // if denied
  254.     if( !Box_Question( _("Clear World ?") ) )
  255.     {
  256.         return;
  257.     }
  258.  
  259.     pActive_Sprite_Manager->Delete_All();
  260.     pActive_Overworld->waypoints.clear();
  261.     pOverworld_Player->Reset();
  262. }
  263.  
  264. /* *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** */
  265.  
  266. cEditor_World *pWorld_Editor = NULL;
  267.