home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2009 February / maximum-cd-2009-02.iso / DiscContents / SMC_1.6_win32.exe / src / objects / text_box.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2008-09-23  |  6.9 KB  |  243 lines

  1. /***************************************************************************
  2.  * text_box.cpp  -  box speaking to you
  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 "../objects/text_box.h"
  17. #include "../core/obj_manager.h"
  18. #include "../core/framerate.h"
  19. #include "../core/game_core.h"
  20. #include "../core/camera.h"
  21. #include "../user/preferences.h"
  22. #include "../input/joystick.h"
  23. #include "../core/main.h"
  24. #include "../input/keyboard.h"
  25. #include "../core/i18n.h"
  26.  
  27. /* *** *** *** *** *** *** *** *** cText_Box *** *** *** *** *** *** *** *** *** */
  28.  
  29. cText_Box :: cText_Box( float x, float y )
  30. : cBaseBox( x, y )
  31. {
  32.     cText_Box::Init();
  33. }
  34.  
  35. cText_Box :: cText_Box( CEGUI::XMLAttributes &attributes )
  36. : cBaseBox()
  37. {
  38.     cText_Box::Init();
  39.     cText_Box::Create_from_Stream( attributes );
  40. }
  41.  
  42. cText_Box :: ~cText_Box( void )
  43. {
  44.  
  45. }
  46.  
  47. void cText_Box :: Init( void )
  48. {
  49.     type = TYPE_TEXT_BOX;
  50.     box_type = type;
  51.  
  52.     // default is infinite times activate-able
  53.     Set_Useable_Count( -1, 1 );
  54.     // Spinbox Animation
  55.     Set_Animation( "Default" );
  56.  
  57.     // todo : editor image needed
  58.     //item_image = NULL;
  59.  
  60.     Create_Name();
  61. }
  62.  
  63. cText_Box *cText_Box :: Copy( void )
  64. {
  65.     cText_Box *text_box = new cText_Box( startposx, startposy );
  66.     text_box->Set_Text( text );
  67.     text_box->Set_Invisible( box_invisible );
  68.  
  69.     return text_box;
  70. }
  71.  
  72. void cText_Box :: Create_from_Stream( CEGUI::XMLAttributes &attributes )
  73. {
  74.     cBaseBox::Create_from_Stream( attributes );
  75.  
  76.     // text
  77.     Set_Text( xml_string_to_string( attributes.getValueAsString( "text" ).c_str() ) );
  78. }
  79.  
  80. void cText_Box :: Save_to_Stream( ofstream &file )
  81. {
  82.     // begin box
  83.     file << "\t<box>" << std::endl;
  84.  
  85.     cBaseBox::Save_to_Stream( file );
  86.  
  87.     // text
  88.     file << "\t\t<Property name=\"text\" value=\"" << string_to_xml_string( text ) << "\" />" << std::endl;
  89.  
  90.     // end box
  91.     file << "\t</box>" << std::endl;
  92. }
  93.  
  94. void cText_Box :: Activate( void )
  95. {
  96.     CEGUI::WindowManager &wmgr = CEGUI::WindowManager::getSingleton();
  97.     CEGUI::MultiLineEditbox *editbox = static_cast<CEGUI::MultiLineEditbox *>(wmgr.createWindow( "TaharezLook/MultiLineEditbox", "text_box_text" ));
  98.  
  99.     // add to main window
  100.     pGuiSystem->getGUISheet()->addChildWindow( editbox );
  101.  
  102.  
  103.     // set on top
  104.     editbox->setAlwaysOnTop( 1 );
  105.     // set position
  106.     float text_pos_x = posx - 150 + ( rect.w * 0.5f );
  107.     float text_pos_y = posy - 5 - 200;
  108.     editbox->setXPosition( CEGUI::UDim( 0, ( text_pos_x - pActive_Camera->x ) * global_upscalex ) );
  109.     editbox->setYPosition( CEGUI::UDim( 0, ( text_pos_y - pActive_Camera->y ) * global_upscaley ) );
  110.     // set size
  111.     editbox->setWidth( CEGUI::UDim( 0, 300 * global_upscalex ) );
  112.     editbox->setHeight( CEGUI::UDim( 0, 200 * global_upscaley ) );
  113.  
  114.     // set text
  115.     editbox->setText( reinterpret_cast<const CEGUI::utf8*>(text.c_str()) );
  116.     // always hide horizontal scrollbar
  117.     editbox->getHorzScrollbar()->hide();
  118.  
  119.     bool display = 1;
  120.  
  121.     while( display )
  122.     {
  123.         while( SDL_PollEvent( &input_event ) )
  124.         {
  125.             if( input_event.type == SDL_KEYDOWN )
  126.             {
  127.                 pKeyboard->keys[input_event.key.keysym.sym] = 1;
  128.  
  129.                 // exit keys
  130.                 if( input_event.key.keysym.sym == pPreferences->key_action || input_event.key.keysym.sym == SDLK_ESCAPE || input_event.key.keysym.sym == SDLK_RETURN || input_event.key.keysym.sym == SDLK_SPACE )
  131.                 {
  132.                     display = 0;
  133.                     break;
  134.                 }
  135.             }
  136.             else if( input_event.type == SDL_JOYBUTTONDOWN )
  137.             {
  138.                 pJoystick->Set_Button( input_event.jbutton.button, 1 );
  139.  
  140.                 if( input_event.jbutton.button == pPreferences->joy_button_action || input_event.jbutton.button == pPreferences->joy_button_exit )
  141.                 {
  142.                     display = 0;
  143.                     break;
  144.                 }
  145.             }
  146.             else if( input_event.type == SDL_KEYUP )
  147.             {
  148.                 pKeyboard->keys[input_event.key.keysym.sym] = 0;
  149.             }
  150.             else if( input_event.type == SDL_JOYBUTTONUP )
  151.             {
  152.                 pJoystick->Set_Button( input_event.jbutton.button, 0 );
  153.             }
  154.         }
  155.  
  156.         Uint8 *keys = SDL_GetKeyState( NULL );
  157.         Sint16 joy_ver_axis = 0;
  158.  
  159.         // if joystick enabled
  160.         if( pPreferences->joy_enabled )
  161.         {
  162.             joy_ver_axis = SDL_JoystickGetAxis( pJoystick->joystick, pPreferences->joy_axis_ver );
  163.         }
  164.  
  165.         // down
  166.         if( keys[pPreferences->key_down] || joy_ver_axis > pPreferences->joy_axis_threshold )
  167.         {
  168.             editbox->getVertScrollbar()->setScrollPosition( editbox->getVertScrollbar()->getScrollPosition() + ( editbox->getVertScrollbar()->getStepSize() * 0.25f * pFramerate->speedfactor ) );
  169.         }
  170.         // up
  171.         if( keys[pPreferences->key_up] || joy_ver_axis < -pPreferences->joy_axis_threshold )
  172.         {
  173.             editbox->getVertScrollbar()->setScrollPosition( editbox->getVertScrollbar()->getScrollPosition() - ( editbox->getVertScrollbar()->getStepSize() * 0.25f * pFramerate->speedfactor ) );
  174.         }
  175.  
  176.         // move camera because text could not be completely visible
  177.         if( pActive_Camera->y_offset > 0 )
  178.         {
  179.             pActive_Camera->y_offset -= 2;
  180.             // set position
  181.             pActive_Camera->Center();
  182.  
  183.             // set position
  184.             editbox->setXPosition( CEGUI::UDim( 0, ( text_pos_x - pActive_Camera->x ) * global_upscalex ) );
  185.             editbox->setYPosition( CEGUI::UDim( 0, ( text_pos_y - pActive_Camera->y ) * global_upscaley ) );
  186.         }
  187.  
  188.         // update animation
  189.         Update();
  190.         
  191.         // draw
  192.         Draw_Game();
  193.         // render
  194.         pVideo->Render();
  195.         pFramerate->Update();
  196.     }
  197.  
  198.     wmgr.destroyWindow( editbox );
  199. }
  200.  
  201. void cText_Box :: Update( void )
  202. {
  203.     if( !valid_update || !is_Player_range() )
  204.     {
  205.         return;
  206.     }
  207.  
  208.     cBaseBox::Update();
  209. }
  210.  
  211. void cText_Box :: Set_Text( string str_text )
  212. {
  213.     text = str_text;
  214. }
  215.  
  216. void cText_Box :: Editor_Activate( void )
  217. {
  218.     // BaseBox Settings first
  219.     cBaseBox::Editor_Activate();
  220.  
  221.     CEGUI::WindowManager &wmgr = CEGUI::WindowManager::getSingleton();
  222.  
  223.     // text
  224.     CEGUI::MultiLineEditbox *editbox = static_cast<CEGUI::MultiLineEditbox *>(wmgr.createWindow( "TaharezLook/MultiLineEditbox", "text_box_text" ));
  225.     Editor_Add( UTF8_("Text"), UTF8_("Text to display when activated"), editbox, 300, 200 );
  226.  
  227.     editbox->setText( reinterpret_cast<const CEGUI::utf8*>(text.c_str()) );
  228.     editbox->subscribeEvent( CEGUI::MultiLineEditbox::EventKeyUp, CEGUI::Event::Subscriber( &cText_Box::Editor_Text_Key, this ) );
  229.  
  230.     // init
  231.     Editor_Init();
  232. }
  233.  
  234. bool cText_Box :: Editor_Text_Key( const CEGUI::EventArgs &event )
  235. {
  236.     const CEGUI::WindowEventArgs &windowEventArgs = static_cast<const CEGUI::WindowEventArgs&>( event );
  237.     string str_text = static_cast<CEGUI::MultiLineEditbox *>( windowEventArgs.window )->getText().c_str();
  238.  
  239.     Set_Text( str_text );
  240.  
  241.     return 1;
  242. }
  243.