ScreenCopper.cpp

Go to the documentation of this file.
00001 #include "global.h"
00002 #include "MercuryTheme.h"
00003 #include "ScreenCopper.h"
00004 #include "MercuryInput.h"
00005 #include "MercuryDisplay.h"
00006 #include "MercuryUtil.h"
00007 #include "MercuryLog.h"
00008 
00009 #include "MercuryObjectFactory.h"
00010 REGISTER_SCREEN_CLASS( ScreenCopper )
00011 KeyMappingWithCode( button_showmenu, "0-27" );
00012 
00013 #define     MC_INPUT            2
00014 #define     MC_LOADOVERLAY      3
00015 #define     MC_UNLOADOVERLAY    4
00016 
00017 void ScreenCopper::Init()
00018 {
00019     MercuryScreen::Init();
00020 //  SetProjectionType(ORTHOGRAPHIC);
00021 
00022     m_sprCursor.SetName( m_name + "::Base::Cursor" );
00023     m_sprCursor.Init();
00024     m_sprCursor.LoadImage( THEME.GetMetricS( m_name, "Cursor" ) );
00025     m_sprCursor.SetDrawOrder(100);
00026     AddOrthoObject( &m_sprCursor );
00027 
00028     MString sMainWindowName = THEME.GetMetricS( m_name, "MainWindowName", "" );
00029     m_wndMain = GetCopperByClass( "CopperWindowLoadable", sMainWindowName );
00030     m_wndMain->Init( NULL, sMainWindowName );
00031     m_wndMain->SetHeight( DISPLAY->GetDimensions().height );
00032     m_wndMain->SetWidth( DISPLAY->GetDimensions().width );
00033     AddOrthoObject( m_wndMain );
00034 
00035     //Get ALL Input regardless of mapped/unmapped nature
00036     RegisterMessage( MC_INPUT, "unmappedinput" );
00037     RegisterMessage( MC_INPUT, "mappedinput" );
00038     RegisterMessage( MC_LOADOVERLAY, "CopperLoadOverlay" );
00039     RegisterMessage( MC_UNLOADOVERLAY, "CopperUnloadOverlay" );
00040 
00041     m_wndOverlay = NULL;
00042 
00043     this->Tweening.AddCommand(THEME.GetMetricS(m_name, "OnCommand" ));
00044 }
00045 
00046 ScreenCopper::~ScreenCopper()
00047 {
00048     SAFE_DELETE( m_wndMain );
00049     SAFE_DELETE( m_wndOverlay );
00050 }
00051 
00052 void ScreenCopper::Update( const float dTime )
00053 {
00054 //  if ( GetHide() )
00055     //  return;
00056 
00057     int x,y,dX,dY;
00058     INPUTMAN->GetCursorPosition( x, y );
00059     dX = x - m_iOldCoreX;
00060     dY = y - m_iOldCoreY;
00061     m_iOldCoreX = x;
00062     m_iOldCoreY = y;
00063 
00064     if ( ( dY != 0 ) || ( dX != 0 ) )
00065     {
00066         m_iOldX += dX;
00067         m_iOldY += dY;
00068         m_iOldX = Clamp( m_iOldX, 0, int(DISPLAY->GetBasedDimensions().width)-1 );
00069         m_iOldY = Clamp( m_iOldY, 0, int(DISPLAY->GetBasedDimensions().height)-1 );
00070 
00071         m_sprCursor.SetX( float(m_iOldX) );
00072         m_sprCursor.SetY( float(m_iOldY) );
00073         
00074         CopperMouseEvent c;
00075         c.pType = CopperMouseEvent::MOUSE_MOVE;
00076         c.x = m_iOldX;
00077         c.y = m_iOldY;
00078         if ( m_wndOverlay )
00079             m_wndOverlay->MouseEvent( c );
00080         m_wndMain->MouseEvent( c ); 
00081     }
00082     MercuryScreen::Update( dTime );
00083 }
00084 
00085 void ScreenCopper::Message( int Message, PStack & data, const MString & name )
00086 {
00087     if ( Message == MC_INPUT )
00088     {
00089         InputMessageStruct c(data);
00090 
00091         //First check to see if we should be hiding/showing ourselves.
00092         if ( c.code == button_showmenu && c.type == IET_RELEASE )
00093             if ( GetHide() )
00094             {
00095                 this->Tweening.StopTweening();
00096                 this->Tweening.AddCommand(THEME.GetMetricS(m_name, "HideOff" ));
00097             }
00098             else
00099             {
00100                 this->Tweening.StopTweening();
00101                 this->Tweening.AddCommand(THEME.GetMetricS(m_name, "HideOn" ));
00102             }
00103 
00104         if ( GetHide() )
00105             return;
00106         
00107         //It's a mouse click
00108         if ( c.button > 0 && c.button < 3 )
00109         {
00110             CopperMouseEvent g;
00111             if ( c.type == IET_DOWN )
00112                 g.pType = CopperMouseEvent::MOUSE_DOWN;
00113             else if ( c.type == IET_RELEASE )
00114                 g.pType = CopperMouseEvent::MOUSE_UP;
00115             else
00116                 return; //Bail, this isn't a normal button press type.
00117                 
00118             g.x = m_iOldX;
00119             g.y = m_iOldY;
00120             bool bOverlayGotMouse = false;
00121             if ( m_wndOverlay )
00122                 bOverlayGotMouse = m_wndOverlay->MouseEvent( g );
00123             if ( !bOverlayGotMouse || g.pType == CopperMouseEvent::MOUSE_UP )
00124                 m_wndMain->MouseEvent( g ); 
00125             return;
00126         }
00127 
00128         //It's a key press
00129         CopperKeypress cuKP;
00130         cuKP.cLetter = c.button;
00131         if ( c.type == IET_DOWN )
00132             cuKP.pType = CopperKeypress::KEY_DOWN;
00133         if ( c.type == IET_RELEASE )
00134             cuKP.pType = CopperKeypress::KEY_UP;
00135         m_wndMain->KeyPress( cuKP );
00136         if ( m_wndOverlay )
00137             m_wndOverlay->KeyPress( cuKP );
00138     } else if ( Message == MC_LOADOVERLAY ) {
00139         if ( m_wndOverlay )
00140         {
00141             RemoveObject( m_wndOverlay );
00142             SAFE_DELETE( m_wndOverlay );
00143         }
00144 
00145         // The following code decodes the comma-delimited string for the data, so it will read [ElementName],[x],[y].
00146         MString sOverlayData = data.PeekItem(0).GetValueS();
00147         MString sOverlayWindowName;
00148         float sX = 0, sY = 0;
00149 
00150         int iBU = BytesUntil( sOverlayData.c_str(), ",", 0, sOverlayData.length(), 1 );
00151 
00152         if ( iBU >= 0 && iBU < (int)sOverlayData.length() )
00153         {
00154             sOverlayWindowName = sOverlayData.substr( 0, iBU );
00155             sOverlayData = sOverlayData.substr( iBU+1 );
00156             iBU = BytesUntil( sOverlayData.c_str(), ",", 0, sOverlayData.length(), 1 );
00157             if ( iBU >= 0 )
00158             {
00159                 sX = (float)atoi( sOverlayData.substr( 0, iBU ).c_str() );
00160                 sOverlayData = sOverlayData.substr( iBU+1 );
00161                 if ( sOverlayData.length() > 0 )
00162                     sY = (float)atoi( sOverlayData.c_str() );
00163             }
00164         } else
00165             sOverlayWindowName = sOverlayData;
00166 
00167         m_wndOverlay = GetCopperByClass( "CopperWindowLoadable", sOverlayWindowName );
00168         m_wndOverlay->SetPosition( MercuryPoint( sX, sY, 0 ) );
00169         m_wndOverlay->Init( NULL, sOverlayWindowName );
00170         AddOrthoObject( m_wndOverlay );
00171     } else if ( Message == MC_UNLOADOVERLAY ) {
00172         if ( m_wndOverlay )
00173         {
00174             RemoveObject( m_wndOverlay );
00175             SAFE_DELETE( m_wndOverlay );
00176         }
00177     }
00178 }
00179 
00180 void ScreenCopper::Draw()
00181 {
00182     MercuryScreen::Draw();
00183 }
00184 
00185 /* 
00186  * Copyright (c) 2006, Charles Lohr
00187  * All rights reserved.
00188  *
00189  * Redistribution and use in source and binary forms, with or
00190  * without modification, are permitted provided that the following
00191  * conditions are met:
00192  *  -   Redistributions of source code must retain the above
00193  *      copyright notice, this list of conditions and the following disclaimer.
00194  *  -   Redistributions in binary form must reproduce the above copyright
00195  *      notice, this list of conditions and the following disclaimer in
00196  *      the documentation and/or other materials provided with the distribution.
00197  *  -   Neither the name of the Mercury Engine nor the names of its
00198  *      contributors may be used to endorse or promote products derived from
00199  *      this software without specific prior written permission.
00200  *
00201  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00202  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00203  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00204  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
00205  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00206  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00207  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00208  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00209  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
00210  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00211  */

Hosted by SourceForge.net Logo