📄 oasisconsolesystem.cpp
字号:
/******************************************************************************
* This source file is part of Bad Camel Gaming
* Copyright (C) 2003 Zephie Greyvenstein
* See Readme.html for acknowledgements
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*****************************************************************************/
/******************************************************************************
* FILENAME : oasisConsoleSystem.cpp
* DESCRIPTION : The console system
* AUTHOR : Zephie Greyvenstein
*****************************************************************************/
#include "oasisConsoleSystem.h"
#include "oasisGraphicsSystem.h"
#include "oasisConfiguration.h"
#include "oasisInputSystem.h"
#include "oasisFrameListenerSystem.h"
#include "oasisScriptSystem.h"
/// Include Ogre stuff
#include "OgreOverlayManager.h"
#include "OgreOverlay.h"
#include "OgreGuiManager.h"
#include "OgreTextAreaGuiElement.h"
#include "OgrePanelGuiElement.h"
/// Include optional profiler
#if USE_PROFILER == 1
#include "OgreProfiler.h"
#endif
namespace Oasis {
template<> consoleSystem *singleton< consoleSystem >::singletonPtr = 0;
consoleSystem::consoleSystem( ) : system( "Console" ) {
uint8 count = ( ( graphicsSystem::get( ).
getWindowHeight( ) / 2 ) / CONSOLE_TEXT_HEIGHT );
if ( count > CONSOLE_MAX_LINES ) {
count = CONSOLE_MAX_LINES;
} else if ( count < 2 ) {
count = 2;
}
textLines.resize( count, NULL );
// Grab the overlay
consoleOverlay = _getOverlay( "console" );
// Setup the text lines
for ( uint8 line = 0; line < count; line++ ) {
char nameBuffer[ 10 ];
sprintf( nameBuffer, "line%d", line );
// Create a new textarea and add it to the console
textLines[ line ] = _createTextArea( nameBuffer );
// Set it up as we need it
textLines[ line ]->setVerticalAlignment( Ogre::GVA_BOTTOM );
textLines[ line ]->setFontName( "console" );
textLines[ line ]->setMetricsMode( Ogre::GMM_PIXELS );
textLines[ line ]->setLeft( 5 );
textLines[ line ]->setCharHeight( CONSOLE_TEXT_HEIGHT );
textLines[ line ]->setColour( Ogre::ColourValue( 0.7, 0.7, 0.7 ) );
textLines[ line ]->setTop( -CONSOLE_TEXT_HEIGHT -
( line * CONSOLE_TEXT_HEIGHT ) );
textLines[ line ]->setCaption( "" );
// Add this element to the console
_addGuiElement( "console/panel", textLines[ line ] );
}
// Set it closed
state = CS_CLOSING;
// Set some defaults
cursorPos = 0;
command = "";
textLines[ 0 ]->setColour( Ogre::ColourValue( 1, 1, 1 ) );
// Register ourselves with input
inputSystem::get( ).addKeyListener( this );
// Register ourselves with the frame listener
frameListenerSystem::get( ).addFrameListener( this );
}
consoleSystem::~consoleSystem( ) {
}
consoleSystem &consoleSystem::get( void ) {
return singleton< consoleSystem >::get( );
}
consoleSystem *consoleSystem::getPtr( void ) {
return singleton< consoleSystem >::getPtr( );
}
void consoleSystem::reset( void ) {
clear( );
// Some output
write( string( "\n\n" ) + PACKAGE +
" console\nVersion " +
VERSION + "\n\nPlease see the log for all " +
"initialisations\n" );
}
void consoleSystem::update( real time ) {
frameStarted( time );
}
const uint8 consoleSystem::getLineCount( void ) const {
return textLines.size( );
}
void consoleSystem::clear( void ) {
for( uint8 it = 1; it < getLineCount( ); it++ ) {
setLineText( it, "" );
}
setLineText( 0, "# " + command + "_" );
}
void consoleSystem::open( void ) {
consoleOverlay->show( );
state = CS_OPENING;
}
void consoleSystem::close( void ) {
state = CS_CLOSING;
}
void consoleSystem::setLineText( uint8 line, const string &text ) {
textLines[ line ]->setCaption( text );
}
const string &consoleSystem::getLineText( uint8 line ) const {
return textLines[ line ]->getCaption( );
}
void consoleSystem::keyReleased( KeyCode key, char keyChar ) {
if ( key == KC_GRAVE ) {
if ( state == CS_OPEN || state == CS_OPENING ) {
close( );
} else {
open( );
}
}
}
void consoleSystem::keyPressed( KeyCode key, char keyChar ) {
if ( state == CS_CLOSED || state == CS_CLOSING ) {
return;
}
// Buffer for building command line
char buffer[ 2 ];
buffer[ 1 ] = 0;
// Get key pressed and insert it into the command string
if ( ( keyChar >= 'a' && keyChar <= 'z' ) ||
( keyChar >= 'A' && keyChar <= 'Z' ) ||
keyChar == KC_LBRACKET ||
keyChar == KC_RBRACKET ) {
buffer[ 0 ] = keyChar;
command.insert( cursorPos, buffer );
cursorPos++;
} else if ( key >= KC_1 && key <= KC_0 ) {
buffer[ 0 ] = keyChar;
command.insert( cursorPos, buffer );
cursorPos++;
} else if ( key == KC_EQUALS ) {
buffer[ 0 ] = keyChar;
command.insert( cursorPos, buffer );
cursorPos++;
} else if ( key == KC_SPACE ) {
command.insert( cursorPos, " " );
cursorPos++;
} else if ( key == KC_PERIOD ) {
buffer[ 0 ] = keyChar;
command.insert( cursorPos, buffer );
cursorPos++;
} else if ( key == KC_APOSTROPHE ) {
buffer[ 0 ] = keyChar;
command.insert( cursorPos, buffer );
cursorPos++;
} else if ( key == KC_COMMA ) {
buffer[ 0 ] = keyChar;
command.insert( cursorPos, buffer );
cursorPos++;
} else if ( key == KC_SEMICOLON ) {
buffer[ 0 ] = keyChar;
command.insert( cursorPos, buffer );
cursorPos++;
} else if ( key == KC_MINUS ) {
buffer[ 0 ] = keyChar;
command.insert( cursorPos, buffer );
cursorPos++;
} else if ( key == KC_BACK ) {
if( cursorPos > 0 ) {
cursorPos--;
command.erase( cursorPos, 1 );
}
} else if ( key == KC_UP ) {
if ( !pressedUp ) {
command = lastCommand;
pressedUp = true;
cursorPos = command.size( );
}
} else if ( key == KC_DOWN ) {
command = "";
pressedUp = false;
cursorPos = command.size( );
} else if ( key == KC_RETURN ) {
lastCommand = command;
pressedUp = false;
// Move all lines up one
setLineText( 0, "# " + command );
_bumpText( );
// Pass command to the script engine
uint8 rettype;
void *retv;
retv = scriptSystem::get( ).executeString( rettype, command.c_str( ) );
// Display return value to the screen
scriptSystem::get().scriptLog( scriptSystem::get( ).toString( retv, rettype ) );
// Free the variable
scriptSystem::get( ).freeVariable( retv, rettype );
command = "";
cursorPos = 0;
}
// Refresh console output
setLineText( 0, "# " + command + "_" );
}
void consoleSystem::write( const string &text ) {
char buffer[ 2 ];
buffer[ 1 ] = 0;
setLineText( 0, "" );
// _bumpText( );
for( uint16 pos = 0; pos < text.size( ); pos++ ) {
if ( text.at( pos ) == '\n' ) {
_bumpText( );
} else if ( text.at( pos ) == '\t' ) {
setLineText( 0, getLineText( 0 ) + " " );
} else {
buffer[ 0 ] = text.at( pos );
setLineText( 0, getLineText( 0 ) + buffer );
}
}
_bumpText( );
setLineText( 0, "# " + command + "_" );
}
void consoleSystem::_bumpText( void ) {
for ( uint8 line = getLineCount( ) - 1; line > 0; line-- ) {
setLineText( line, getLineText( line - 1 ) );
}
setLineText( 0, "" );
}
bool consoleSystem::frameStarted( real time ) {
#if USE_PROFILER == 1
{
OgreProfile( "Oasis Console System" );
#endif
if( state == CS_OPENING ) {
// Scroll some more
consoleOverlay->scroll( 0, -CONSOLE_SPEED * time );
// Check if we are fully open
if( consoleOverlay->getScrollY( ) <= 0 ) {
consoleOverlay->setScroll( 0, 0 );
state = CS_OPEN;
}
} else if ( state == CS_CLOSING ) {
// Scroll some more
consoleOverlay->scroll( 0, CONSOLE_SPEED * time );
// Check if we are fully closed
if( consoleOverlay->getScrollY( ) >= 1 ) {
consoleOverlay->setScroll( 0, 1 );
state = CS_CLOSED;
consoleOverlay->hide( );
}
}
#if USE_PROFILER == 1
}
#endif
return true;
}
Ogre::Overlay *consoleSystem::_getOverlay( const string &overlay ) {
return static_cast< Ogre::Overlay* >( Ogre::OverlayManager::getSingleton( ).
getByName( overlay ) );
}
Ogre::TextAreaGuiElement *consoleSystem::_createTextArea( const string &name ) {
return static_cast< Ogre::TextAreaGuiElement* >
( Ogre::GuiManager::getSingleton( ).createGuiElement("TextArea", name ) );
}
void consoleSystem::_addGuiElement( const string &parent,
Ogre::GuiElement *child ) {
static_cast< Ogre::PanelGuiElement* >( Ogre::GuiManager::getSingleton( ).
getGuiElement( parent ) )->addChild( static_cast< Ogre::GuiElement* >( child ) );
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -