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

  1. /***************************************************************************
  2.  * level_editor.cpp  -  Level Editor class
  3.  *
  4.  * Copyright (C) 2006 - 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 "../level/level_editor.h"
  18. #include "../level/level.h"
  19. #include "../level/level_settings.h"
  20. #include "../core/game_core.h"
  21. #include "../core/sprite_manager.h"
  22. #include "../core/camera.h"
  23. #include "../user/preferences.h"
  24. #include "../input/mouse.h"
  25. #include "../input/keyboard.h"
  26. #include "../audio/audio.h"
  27. #include "../core/i18n.h"
  28.  
  29. /* *** *** *** *** *** *** *** cEditor_Level *** *** *** *** *** *** *** *** *** *** */
  30.  
  31. cEditor_Level :: cEditor_Level( void )
  32. : cEditor()
  33. {
  34.     menu_filename = DATA_DIR "/" GAME_EDITOR_DIR "/level_menu.xml";
  35.     items_filename = DATA_DIR "/" GAME_EDITOR_DIR "/level_items.xml";
  36.  
  37.     editor_item_tag = "level";
  38.  
  39.     pSettings = new cLevel_Settings();
  40. }
  41.  
  42. cEditor_Level :: ~cEditor_Level( void )
  43. {
  44.     delete pSettings;
  45. }
  46.  
  47. void cEditor_Level :: Init( void )
  48. {
  49.     // already loaded
  50.     if( editor_window )
  51.     {
  52.         return;
  53.     }
  54.  
  55.     // nothing
  56.  
  57.     cEditor::Init();
  58. }
  59.  
  60. void cEditor_Level :: Enable( void )
  61. {
  62.     // already enabled
  63.     if( enabled )
  64.     {
  65.         return;
  66.     }
  67.  
  68.     editor_level_enabled = 1;
  69.  
  70.     if( Game_Mode == MODE_LEVEL )
  71.     {
  72.         editor_enabled = 1;
  73.     }
  74.  
  75.     cEditor::Enable();
  76. }
  77.  
  78. void cEditor_Level :: Disable( bool native_mode /* = 0 */ )
  79. {
  80.     // already disabled
  81.     if( !enabled )
  82.     {
  83.         return;
  84.     }
  85.  
  86.     debugdisplay->Set_Text( _("Level Editor disabled") );
  87.  
  88.     editor_level_enabled = 0;
  89.  
  90.     if( Game_Mode == MODE_LEVEL )
  91.     {
  92.         native_mode = 1;
  93.         editor_enabled = 0;
  94.     }
  95.     
  96.     cEditor::Disable( native_mode );
  97. }
  98.  
  99. bool cEditor_Level :: Key_Down( SDLKey key )
  100. {
  101.     if( !enabled )
  102.     {
  103.         return 0;
  104.     }
  105.  
  106.  
  107.     // check basic editor events
  108.     if( cEditor::Key_Down( key ) )
  109.     {
  110.         return 1;
  111.     }
  112.     // save level
  113.     else if( key == SDLK_s && ( input_event.key.keysym.mod & KMOD_LCTRL || input_event.key.keysym.mod & KMOD_RCTRL ) )
  114.     {
  115.         pActive_Level->Save();
  116.     }
  117.     // focus last levelexit
  118.     else if( key == SDLK_END )
  119.     {
  120.         float new_cameraposx = 0;
  121.         float new_cameraposy = 0;
  122.  
  123.         for( SpriteList::iterator itr = pActive_Sprite_Manager->objects.begin(), itr_end = pActive_Sprite_Manager->objects.end(); itr != itr_end; ++itr )
  124.         {
  125.             cSprite *obj = (*itr);
  126.  
  127.             if( obj->sprite_array != ARRAY_ACTIVE )
  128.             {
  129.                 continue;
  130.             }
  131.  
  132.             if( obj->type == TYPE_LEVEL_EXIT && new_cameraposx < obj->posx )
  133.             {
  134.                 new_cameraposx = obj->posx;
  135.                 new_cameraposy = obj->posy;
  136.             }
  137.         }
  138.  
  139.         if( new_cameraposx != 0 || new_cameraposy != 0 )
  140.         {
  141.             pActive_Camera->Set_Pos( new_cameraposx - ( game_res_w / 2 ), new_cameraposy - ( game_res_h / 2 ) );
  142.         }
  143.     }
  144.     // modify selected objects state
  145.     else if( key == SDLK_m )
  146.     {
  147.         if( pMouseCursor->selected_objects.size() )
  148.         {
  149.             // change state of the base object
  150.             if( pLevel_Editor->Switch_Object_State( pMouseCursor->selected_objects[0]->obj ) )
  151.             {
  152.                 // change all object states to the base object state
  153.                 for( SelectedObjectList::iterator itr = pMouseCursor->selected_objects.begin(), itr_end = pMouseCursor->selected_objects.end(); itr != itr_end; ++itr )
  154.                 {
  155.                     cSprite *obj = (*itr)->obj;
  156.  
  157.                     // skip base object
  158.                     if( obj == pMouseCursor->selected_objects[0]->obj )
  159.                     {
  160.                         continue;
  161.                     }
  162.  
  163.                     // sprites need additional data
  164.                     if( obj->type == TYPE_PASSIVE || obj->type == TYPE_FRONT_PASSIVE || obj->type == TYPE_MASSIVE || obj->type == TYPE_CLIMBABLE || obj->type == TYPE_HALFMASSIVE )
  165.                     {
  166.                         obj->type = pMouseCursor->selected_objects[0]->obj->type;
  167.                         obj->sprite_array = pMouseCursor->selected_objects[0]->obj->sprite_array;
  168.                         obj->can_be_ground = pMouseCursor->selected_objects[0]->obj->can_be_ground;
  169.                     }
  170.                     
  171.                     // set state
  172.                     obj->Set_Massive_Type( pMouseCursor->selected_objects[0]->obj->massivetype );
  173.                 }
  174.             }
  175.         }
  176.     }
  177.     // modify mouse object state
  178.     else if( key == SDLK_m && pMouseCursor->mouse_object->obj )
  179.     {
  180.         Switch_Object_State( pMouseCursor->mouse_object->obj );
  181.         pMouseCursor->Clear_Mouse_Object();
  182.     }
  183.     else
  184.     {
  185.         // not processed
  186.         return 0;
  187.     }
  188.  
  189.     // key got processed
  190.     return 1;
  191. }
  192.  
  193. void cEditor_Level :: Activate_Menu( cEditor_Menu_Object *entry )
  194. {
  195.     // If Function
  196.     if( entry->bfunction )
  197.     {
  198.         if( entry->tags.compare( "new" ) == 0 )
  199.         {
  200.             Function_New();
  201.         }
  202.         else if( entry->tags.compare( "load" ) == 0 )
  203.         {
  204.             Function_Load();
  205.         }
  206.         else if( entry->tags.compare( "save" ) == 0 )
  207.         {
  208.             Function_Save();
  209.         }
  210.         else if( entry->tags.compare( "save_as" ) == 0 )
  211.         {
  212.             Function_Save_as();
  213.         }
  214.         else if( entry->tags.compare( "delete" ) == 0 )
  215.         {
  216.             Function_Delete();
  217.         }
  218.         else if( entry->tags.compare( "reload" ) == 0 )
  219.         {
  220.             Function_Reload();
  221.         }
  222.         else if( entry->tags.compare( "clear" ) == 0 )
  223.         {
  224.             Function_Clear();
  225.         }
  226.         else if( entry->tags.compare( "settings" ) == 0 )
  227.         {
  228.             Function_Settings();
  229.         }
  230.         // unknown level function
  231.         else
  232.         {
  233.             cEditor::Activate_Menu( entry );
  234.         }
  235.     }
  236.     // unknown level function
  237.     else
  238.     {
  239.         cEditor::Activate_Menu( entry );
  240.     }
  241. }
  242.  
  243. bool cEditor_Level :: Switch_Object_State( cSprite *obj )
  244. {
  245.     // empty object
  246.     if( !obj )
  247.     {
  248.         return 0;
  249.     }
  250.  
  251.     // from Passive to Front Passive
  252.     if( obj->type == TYPE_PASSIVE )
  253.     {
  254.         obj->Set_Sprite_Type( TYPE_FRONT_PASSIVE );
  255.     }
  256.     // from Front Passive to Massive
  257.     else if( obj->type == TYPE_FRONT_PASSIVE )
  258.     {
  259.         obj->Set_Sprite_Type( TYPE_MASSIVE );
  260.     }
  261.     // from Massive to Halfmassive
  262.     else if( obj->type == TYPE_MASSIVE )
  263.     {
  264.         obj->Set_Sprite_Type( TYPE_HALFMASSIVE );
  265.     }
  266.     // from Halfmassive to Climbable
  267.     else if( obj->type == TYPE_HALFMASSIVE )
  268.     {
  269.         obj->Set_Sprite_Type( TYPE_CLIMBABLE );
  270.     }
  271.     // from Climbable to Passive
  272.     else if( obj->type == TYPE_CLIMBABLE )
  273.     {
  274.         obj->Set_Sprite_Type( TYPE_PASSIVE );
  275.     }
  276.     // moving platform
  277.     else if( obj->type == TYPE_MOVING_PLATFORM )
  278.     {
  279.         if( obj->massivetype == MASS_PASSIVE )
  280.         {
  281.             obj->Set_Massive_Type( MASS_MASSIVE );
  282.         }
  283.         else if( obj->massivetype == MASS_MASSIVE )
  284.         {
  285.             obj->Set_Massive_Type( MASS_HALFMASSIVE );
  286.         }
  287.         else if( obj->massivetype == MASS_HALFMASSIVE )
  288.         {
  289.             obj->Set_Massive_Type( MASS_CLIMBABLE );
  290.         }
  291.         else if( obj->massivetype == MASS_CLIMBABLE )
  292.         {
  293.             obj->Set_Massive_Type( MASS_PASSIVE );
  294.         }
  295.     }
  296.     // invalid object type
  297.     else
  298.     {
  299.         return 0;
  300.     }
  301.  
  302.     return 1;
  303. }
  304.  
  305. bool cEditor_Level :: Function_New( void )
  306. {
  307.     string level_name = Box_Text_Input( _("Create a new Level"), _("Name") );
  308.  
  309.     // aborted/invalid
  310.     if( level_name.empty() )
  311.     {
  312.         return 0;
  313.     }
  314.  
  315.     if( pActive_Level->New( level_name ) )
  316.     {
  317.         debugdisplay->Set_Text( _("Created ") + level_name );
  318.         return 1;
  319.     }
  320.     else
  321.     {
  322.         debugdisplay->Set_Text( _("Level ") + level_name + _(" already exists") );
  323.     }
  324.  
  325.     return 0;
  326. }
  327.  
  328. void cEditor_Level :: Function_Load( void )
  329. {
  330.     string level_name = _("Name");
  331.  
  332.     // valid level
  333.     while( level_name.length() )
  334.     {
  335.         level_name = Box_Text_Input( level_name, _("Load a Level"), level_name.compare( _("Name") ) == 0 ? 1 : 0 );
  336.  
  337.         // break if empty
  338.         if( level_name.empty() )
  339.         {
  340.             break;
  341.         }
  342.  
  343.         // if available
  344.         if( pActive_Level->Get_Path( level_name ) )
  345.         {
  346.             Game_Action = GA_ENTER_LEVEL;
  347.             Game_Mode_Type = MODE_TYPE_LEVEL_CUSTOM;
  348.             Game_Action_Data.add( "level", level_name.c_str() );
  349.  
  350.             debugdisplay->Set_Text( _("Loaded ") + Get_Filename( level_name, 0, 0 ) );
  351.  
  352.             break;
  353.         }
  354.         // not found
  355.         else
  356.         {
  357.             pAudio->Play_Sound( "error.ogg" );
  358.         }
  359.     }
  360. }
  361.  
  362. void cEditor_Level :: Function_Save( bool with_dialog /* = 0 */ )
  363. {
  364.     // not loaded
  365.     if( !pActive_Level->Is_Loaded() )
  366.     {
  367.         return;
  368.     }
  369.  
  370.     // if denied
  371.     if( with_dialog && !Box_Question( _("Save ") + Get_Filename( pActive_Level->levelfile, 0, 0 ) + " ?" ) )
  372.     {
  373.         return;
  374.     }
  375.  
  376.     pActive_Level->Save();
  377. }
  378.  
  379. void cEditor_Level :: Function_Save_as( void )
  380. {
  381.     string levelname = Box_Text_Input( _("Save Level as"), _("New name"), 1 );
  382.  
  383.     // aborted/invalid
  384.     if( levelname.empty() )
  385.     {
  386.         return;
  387.     }
  388.  
  389.     pActive_Level->Set_Levelfile( levelname, 0 );
  390.     pActive_Level->Save();
  391. }
  392.  
  393. void cEditor_Level :: Function_Delete( void )
  394. {
  395.     string filename = pActive_Level->levelfile;
  396.     if( !pActive_Level->Get_Path( filename, 1 ) )
  397.     {
  398.         return;
  399.     }
  400.  
  401.     // if denied
  402.     if( !Box_Question( _("Delete and Unload ") + Get_Filename( filename, 0, 0 ) + " ?" ) )
  403.     {
  404.         return;
  405.     }
  406.  
  407.     pActive_Level->Delete();
  408.     pLevel_Editor->Disable();
  409.  
  410.     Game_Action = GA_ENTER_MENU;
  411. }
  412.  
  413. void cEditor_Level :: Function_Reload( void )
  414. {
  415.     // if denied
  416.     if( !Box_Question( _("Reload Level ?") ) )
  417.     {
  418.         return;
  419.     }
  420.  
  421.     pActive_Level->Save();
  422.     pActive_Level->Load( Get_Filename( pActive_Level->data_file, 0 ) );
  423. }
  424.  
  425. void cEditor_Level :: Function_Clear( void )
  426. {
  427.     // if denied
  428.     if( !Box_Question( _("Clear Level ?") ) )
  429.     {
  430.         return;
  431.     }
  432.  
  433.     pActive_Sprite_Manager->Delete_All();
  434. }
  435.  
  436. void cEditor_Level :: Function_Settings( void )
  437. {
  438.     Game_Action = GA_ENTER_LEVEL_SETTINGS;
  439. }
  440.  
  441. /* *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** */
  442.  
  443. cEditor_Level *pLevel_Editor = NULL;
  444.