

Here is the code:
Console.h
Code: Select all
#pragma once
#include <OgreEventListeners.h>
#include <OgreKeyEvent.h>
#include <list>
#include <vector>
using namespace std;
class OgreConsole:public Singleton<OgreConsole>,FrameListener,LogListener,KeyListener{
public:
OgreConsole();
~OgreConsole();
void init(Ogre::Root *root);
void setVisible(bool visible);
void print(const String &text);
virtual bool frameStarted(const Ogre::FrameEvent &evt);
virtual bool frameEnded(const Ogre::FrameEvent &evt);
void keyClicked(KeyEvent* e);
void keyPressed(KeyEvent* e) {}
void keyReleased(KeyEvent* e) {}
void addCommand(const String &command, void (*)(vector<String>&));
void removeCommand(const String &command);
//log
virtual void write( const String& name,
const String& message,
LogMessageLevel lml = LML_NORMAL,
bool maskDebug = false ){print(name+": "+message);}
private:
bool visible;
Ogre::EventProcessor *input;
Ogre::Root *root;
Ogre::SceneManager *scene;
Ogre::Rectangle2D *rect;
Ogre::OverlayElement *textoverlay;
float height;
int scroll_lines;
bool update_overlay;
int start_line;
list<String> lines;
String prompt;
map<String,void (*)(vector<String>&)> commands;
};
Code: Select all
#include "main.h"
OgreConsole *Singleton<OgreConsole>::ms_Singleton=0;
#define CONSOLE_LINE_LENGTH 85
#define CONSOLE_LINE_COUNT 16
OgreConsole::OgreConsole(){
start_line=0;
}
OgreConsole::~OgreConsole(){
if(input)
delete input;
}
void OgreConsole::init(Ogre::Root *root){
if(!root->getSceneManagerIterator().hasMoreElements())
OGRE_EXCEPT( Exception::ERR_INTERNAL_ERROR, "No scene manager found!", "init" );
this->root=root;
scene=root->getSceneManagerIterator().getNext();
root->addFrameListener(this);
input = new EventProcessor();
input->initialise(root->getAutoCreatedWindow());
input->startProcessingEvents();
input->addKeyListener(this);
height=1;
// Create background rectangle covering the whole screen
rect = new Rectangle2D(true);
rect->setCorners(-1, 1, 1, 1-height);
rect->setMaterial("console/background");
rect->setRenderQueueGroup(RENDER_QUEUE_OVERLAY);
rect->setBoundingBox(AxisAlignedBox(-100000.0*Vector3::UNIT_SCALE, 100000.0*Vector3::UNIT_SCALE));
textoverlay=OverlayManager::getSingleton().createOverlayElement("TextArea","ConsoleText");
textoverlay->setCaption("hello");
textoverlay->setMetricsMode(GMM_RELATIVE);
textoverlay->setPosition(0,0);
textoverlay->setParameter("font_name","Console");
textoverlay->setParameter("colour_top","1 1 1");
textoverlay->setParameter("colour_bottom","1 1 1");
textoverlay->setParameter("char_height","0.03");
Overlay *overlay=OverlayManager::getSingleton().create("Console");
overlay->add2D((OverlayContainer*)textoverlay);
overlay->show();
LogManager::getSingleton().addListener(this);
// Attach background to the scene
SceneNode* node = scene->getRootSceneNode()->createChildSceneNode("#Console");
node->attachObject(rect);
// Example of background scrolling
//material->getTechnique(0)->getPass(0)->getTextureUnitState(0)->setScrollAnimation(-0.25, 0.0);
}
void OgreConsole::keyClicked(Ogre::KeyEvent *e){
if(e->getKey()==KC_F1){
setVisible(!visible);
return;
}
if(!visible)
return;
if(e->getKey()==KC_RETURN&&prompt.length()){
//split the parameter list
const char *str=prompt.c_str();
vector<String> params;
String param="";
for(int c=0;c<prompt.length();c++){
if(str[c]==' '){
if(param.length())
params.push_back(param);
param="";
}
else
param+=str[c];
}
if(param.length())
params.push_back(param);
//try to execute the command
map<String,void(*)(vector<String>&)>::iterator i;
for(i=commands.begin();i!=commands.end();i++){
if((*i).first==params[0]){
if((*i).second)
(*i).second(params);
break;
}
}
print(prompt);
prompt="";
}
else if(e->getKey()==KC_BACK){
prompt=prompt.substr(0,prompt.length()-1);
}
else if(e->getKey()==KC_UP){
if(start_line>0)
start_line--;
}
else if(e->getKey()==KC_DOWN){
if(start_line<lines.size())
start_line++;
}
else if(e->getKey()==KC_SYSRQ){
static int count=0;
char tmp[20];
sprintf(tmp, "screenshot_%d.png", ++count);
root->getAutoCreatedWindow()->writeContentsToFile(tmp);
print(String("Wrote: ")+tmp);
}
else{
char legalchars[]="ABCDEFGHIJKLMNOPQRSTUVWXUZabcdefghijklmnopqrstuvwxyz1234567890+!\"#%&/()=?[]\\*-_.:,; ";
for(int c=0;c<sizeof(legalchars);c++){
if(legalchars[c]==e->getKeyChar()){
prompt+=e->getKeyChar();
break;
}
}
}
update_overlay=true;
}
bool OgreConsole::frameStarted(const Ogre::FrameEvent &evt){
if(visible&&height<1){
height+=evt.timeSinceLastFrame*2;
textoverlay->show();
if(height>=1){
height=1;
}
}
else if(!visible&&height>0){
height-=evt.timeSinceLastFrame*2;
if(height<=0){
height=0;
textoverlay->hide();
}
}
textoverlay->setPosition(0,(height-1)*0.5);
rect->setCorners(-1,1+height,1,1-height);
if(update_overlay){
String text;
list<String>::iterator i,start,end;
//make sure is in range
if(start_line>lines.size())
start_line=lines.size();
int lcount=0;
start=lines.begin();
for(int c=0;c<start_line;c++)
start++;
end=start;
for(int c=0;c<CONSOLE_LINE_COUNT;c++){
if(end==lines.end())
break;
end++;
}
for(i=start;i!=end;i++)
text+=(*i)+"\n";
//add the prompt
text+="] "+prompt;
textoverlay->setCaption(text);
update_overlay=false;
}
return true;
}
void OgreConsole::print(const String &text){
//subdivide it into lines
const char *str=text.c_str();
int start=0,count=0;
int len=text.length();
String line;
for(int c=0;c<len;c++){
if(str[c]=='\n'||line.length()>=CONSOLE_LINE_LENGTH){
lines.push_back(line);
line="";
}
if(str[c]!='\n')
line+=str[c];
}
if(line.length())
lines.push_back(line);
if(lines.size()>CONSOLE_LINE_COUNT)
start_line=lines.size()-CONSOLE_LINE_COUNT;
else
start_line=0;
update_overlay=true;
}
bool OgreConsole::frameEnded(const Ogre::FrameEvent &evt){
return true;
}
void OgreConsole::setVisible(bool visible){
this->visible=visible;
}
void OgreConsole::addCommand(const String &command, void (*func)(vector<String>&)){
commands[command]=func;
}
void OgreConsole::removeCommand(const String &command){
commands.erase(commands.find(command));
}
Code: Select all
material console/background
{
technique
{
pass
{
scene_blend alpha_blend
diffuse 0 0 1 0.5
}
}
}
Code: Select all
Console
{
type truetype
source console.ttf
size 32
resolution 55
}
Hope this was useful to someone

btw, how do I put it on wiki? I'm totaly clueless on how to use the wiki
