1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-01-26 03:52:01 +02:00

* basic in-game console

This commit is contained in:
mateuszb 2009-04-03 15:55:26 +00:00
parent 6a37aebec0
commit f794644c2a
8 changed files with 515 additions and 300 deletions

View File

@ -1387,6 +1387,8 @@ void CAdvMapInt::activate()
terrain.activate();
KeyInterested::activate();
show();
LOCPLINT->cingconsole->activate();
}
void CAdvMapInt::deactivate()
{
@ -1397,6 +1399,8 @@ void CAdvMapInt::deactivate()
}
KeyInterested::deactivate();
hide();
LOCPLINT->cingconsole->deactivate();
}
void CAdvMapInt::show(SDL_Surface *to)
{
@ -1424,6 +1428,7 @@ void CAdvMapInt::show(SDL_Surface *to)
statusbar.show();
infoBar.draw();
LOCPLINT->cingconsole->show();
}
void CAdvMapInt::hide()
{
@ -1492,6 +1497,7 @@ void CAdvMapInt::update()
for(int i=0;i<4;i++)
blitAt(gems[i]->ourImages[LOCPLINT->playerID].bitmap,ADVOPT.gemX[i],ADVOPT.gemY[i]);
updateScreen=false;
LOCPLINT->cingconsole->show();
}
if (updateMinimap)
{

View File

@ -350,6 +350,8 @@ void CBattleInterface::activate()
attackingHero->activate();
if(defendingHero)
defendingHero->activate();
LOCPLINT->cingconsole->activate();
}
void CBattleInterface::deactivate()
@ -373,6 +375,8 @@ void CBattleInterface::deactivate()
attackingHero->deactivate();
if(defendingHero)
defendingHero->deactivate();
LOCPLINT->cingconsole->deactivate();
}
void CBattleInterface::show(SDL_Surface * to)
@ -655,6 +659,8 @@ void CBattleInterface::show(SDL_Surface * to)
resWindow->show(to);
}
//showing in-gmae console
LOCPLINT->cingconsole->show();
//printing border around interface
if(screen->w != 800 || screen->h !=600)
@ -2454,7 +2460,11 @@ CBattleConsole::~CBattleConsole()
void CBattleConsole::show(SDL_Surface * to)
{
if(alterTxt.size())
if(ingcAlter.size())
{
CSDL_Ext::printAtMiddleWB(ingcAlter, pos.x + pos.w/2, pos.y + 10, GEOR13, 80, zwykly, to);
}
else if(alterTxt.size())
{
CSDL_Ext::printAtMiddleWB(alterTxt, pos.x + pos.w/2, pos.y + 10, GEOR13, 80, zwykly, to);
}

View File

@ -72,6 +72,7 @@ private:
int lastShown; //last shown line of text
public:
std::string alterTxt; //if it's not empty, this text is displayed
std::string ingcAlter; //alternative text set by in-game console - very important!
int whoSetAlter; //who set alter text; 0 - battle interface or none, 1 - button
CBattleConsole(); //c-tor
~CBattleConsole(); //d-tor
@ -282,6 +283,7 @@ public:
friend class CBattleReslutWindow;
friend class CPlayerInterface;
friend class AdventureMapButton;
friend class CInGameConsole;
};
#endif // __CBATTLEINTERFACE_H__

View File

@ -79,6 +79,7 @@ int main(int argc, char** argv)
CGameInfo * cgi = CGI = new CGameInfo; //contains all global informations about game (texts, lodHandlers, map handler itp.)
if(SDL_Init(SDL_INIT_VIDEO|SDL_INIT_TIMER|SDL_INIT_AUDIO)==0)
{
SDL_EnableUNICODE(1);
screen = SDL_SetVideoMode(800,600,conf.cc.bpp,SDL_SWSURFACE|SDL_DOUBLEBUF|(conf.cc.fullscreen?SDL_FULLSCREEN:0)); //initializing important global surface
tlog0 <<"\tInitializing screen: "<<pomtime.getDif();
tlog0 << std::endl;

View File

@ -1099,6 +1099,7 @@ CPlayerInterface::CPlayerInterface(int Player, int serial)
SDL_initFramerate(mainFPSmng);
SDL_setFramerate(mainFPSmng, 48);
//framerate keeper initialized
cingconsole = new CInGameConsole;
}
CPlayerInterface::~CPlayerInterface()
{
@ -1106,6 +1107,7 @@ CPlayerInterface::~CPlayerInterface()
delete showingDialog;
delete mainFPSmng;
delete adventureInt;
delete cingconsole;
for(std::map<int,SDL_Surface*>::iterator i=graphics->heroWins.begin(); i!= graphics->heroWins.end(); i++)
SDL_FreeSurface(i->second);
@ -1911,9 +1913,19 @@ void CPlayerInterface::handleEvent(SDL_Event *sEvent)
key.keysym.sym = (SDLKey)SDLK_RETURN;
}
bool keysCaptured = false;
for(std::list<KeyInterested*>::iterator i=keyinterested.begin(); i != keyinterested.end();i++)
{
if((*i)->captureAllKeys)
{
keysCaptured = true;
break;
}
}
std::list<KeyInterested*> miCopy = keyinterested;
for(std::list<KeyInterested*>::iterator i=miCopy.begin(); i != miCopy.end();i++)
if(vstd::contains(keyinterested,*i))
if(vstd::contains(keyinterested,*i) && (!keysCaptured || (*i)->captureAllKeys))
(**i).keyPressed(key);
}
else if(sEvent->type==SDL_MOUSEMOTION)
@ -2604,17 +2616,20 @@ CStatusBar::~CStatusBar()
SDL_FreeSurface(bg);
}
void CStatusBar::clear()
{
if(LOCPLINT->cingconsole->enteredText == "") //for appropriate support for in-game console
{
current="";
SDL_Rect pom = genRect(pos.h,pos.w,pos.x,pos.y);
SDL_BlitSurface(bg,&genRect(pos.h,pos.w,0,0),screen,&pom);
show();
}
}
void CStatusBar::print(const std::string & text)
{
if(LOCPLINT->cingconsole->enteredText == "" || text == LOCPLINT->cingconsole->enteredText) //for appropriate support for in-game console
{
current=text;
SDL_Rect pom = genRect(pos.h,pos.w,pos.x,pos.y);
SDL_BlitSurface(bg,&genRect(pos.h,pos.w,0,0),screen,&pom);
printAtMiddle(current,middlex,middley,GEOR13,zwykly);
show();
}
}
void CStatusBar::show()
{
@ -4571,3 +4586,131 @@ void CTavernWindow::HeroPortrait::hover( bool on )
else
LOCPLINT->statusbar->clear();
}
void CInGameConsole::activate()
{
KeyInterested::activate();
}
void CInGameConsole::deactivate()
{
KeyInterested::deactivate();
}
void CInGameConsole::show(SDL_Surface * to)
{
int number = 0;
std::vector<std::list< std::pair< std::string, int > >::iterator> toDel;
for(std::list< std::pair< std::string, int > >::iterator it = texts.begin(); it != texts.end(); ++it, ++number)
{
SDL_Color green = {0,0xff,0,0};
CSDL_Ext::printAtWR(it->first, 50, conf.cc.resy - texts.size() * 30 - 80 + number*30, GEOR16, green);
if(SDL_GetTicks() - it->second > defaultTimeout)
{
toDel.push_back(it);
}
}
for(int it=0; it<toDel.size(); ++it)
{
texts.erase(toDel[it]);
}
}
void CInGameConsole::keyPressed (const SDL_KeyboardEvent & key)
{
if(key.type == SDL_KEYDOWN)
{
switch(key.keysym.sym)
{
case SDLK_TAB:
{
if(captureAllKeys)
{
captureAllKeys = false;
endEnteringText(false);
}
else
{
captureAllKeys = true;
startEnteringText();
}
break;
}
case SDLK_RETURN: //enter key
{
if(enteredText.size() > 0)
{
if(captureAllKeys)
{
captureAllKeys = false;
endEnteringText(true);
}
}
break;
}
default:
{
if(enteredText.size() > 0)
{
if( key.keysym.unicode < 0x80 && key.keysym.unicode > 0 )
{
enteredText[enteredText.size()-1] = (char)key.keysym.unicode;
enteredText += "_";
if(LOCPLINT->curint == LOCPLINT->adventureInt)
{
LOCPLINT->statusbar->print(enteredText);
}
else if(LOCPLINT->curint == LOCPLINT->battleInt)
{
LOCPLINT->battleInt->console->ingcAlter = enteredText;
}
}
}
break;
}
}
}
}
void CInGameConsole::startEnteringText()
{
enteredText = "_";
if(LOCPLINT->curint == LOCPLINT->adventureInt)
{
LOCPLINT->statusbar->print(enteredText);
}
else if(LOCPLINT->curint == LOCPLINT->battleInt)
{
LOCPLINT->battleInt->console->ingcAlter = enteredText;
}
}
void CInGameConsole::endEnteringText(bool printEnteredText)
{
if(printEnteredText)
{
texts.push_back(std::make_pair(enteredText.substr(0, enteredText.size()-1), SDL_GetTicks()));
if(texts.size() > maxDisplayedTexts)
{
texts.pop_front();
}
}
enteredText = "";
if(LOCPLINT->curint == LOCPLINT->adventureInt)
{
LOCPLINT->statusbar->clear();
}
else if(LOCPLINT->curint == LOCPLINT->battleInt)
{
LOCPLINT->battleInt->console->ingcAlter = "";
}
}
CInGameConsole::CInGameConsole() : defaultTimeout(10000), maxDisplayedTexts(10)
{
}

View File

@ -40,6 +40,7 @@ class CGObjectInstance;
class CSlider;
struct UpgradeInfo;
template <typename T> struct CondSh;
class CInGameConsole;
namespace boost
{
@ -290,7 +291,7 @@ public:
class Hoverable : public virtual CIntObject
{
public:
Hoverable(){hovered=false;} //c-tor
Hoverable() : hovered(false){} //c-tor
virtual ~Hoverable();//{}; //d-tor
bool hovered; //for determining if object is hovered
virtual void hover (bool on)=0;
@ -300,6 +301,8 @@ public:
class KeyInterested : public virtual CIntObject
{
public:
bool captureAllKeys; //if true, only this object should get info about pressed keys
KeyInterested(): captureAllKeys(false){}
virtual ~KeyInterested();//{};
virtual void keyPressed(const SDL_KeyboardEvent & key)=0;
virtual void activate()=0;
@ -335,6 +338,7 @@ public:
virtual void activate();
virtual void deactivate();
};
class CInfoWindow : public CSimpleWindow //text + comp. + ok button
{ //window able to delete its components when closed
public:
@ -506,6 +510,7 @@ public:
CAdvMapInt * adventureInt;
CCastleInterface * castleInt;
CBattleInterface * battleInt;
CInGameConsole * cingconsole;
FPSmanager * mainFPSmng;
IStatusBar *statusbar; //advmap statusbar; should it be used by other windows with statusbar?
//to commucate with engine
@ -897,6 +902,25 @@ public:
void show(SDL_Surface * to = NULL);
};
class CInGameConsole : public IShowActivable, public KeyInterested
{
private:
std::list< std::pair< std::string, int > > texts; //<text to show, time of add>
int defaultTimeout; //timeout for new texts (in ms)
int maxDisplayedTexts; //hiw many texts can be displayed simultaneously
public:
std::string enteredText;
void activate();
void deactivate();
void show(SDL_Surface * to = NULL);
void keyPressed (const SDL_KeyboardEvent & key); //call-in
void startEnteringText();
void endEnteringText(bool printEnteredText);
CInGameConsole(); //c-tor
};
extern CPlayerInterface * LOCPLINT;

View File

@ -139,6 +139,34 @@ void CSDL_Ext::printAtMiddle(const std::string & text, int x, int y, TTF_Font *
SDL_UpdateRect(dst,x-(temp->w/2),y-(temp->h/2),temp->w,temp->h);
SDL_FreeSurface(temp);
}
void CSDL_Ext::printAtWR(const std::string & text, int x, int y, TTF_Font * font, SDL_Color kolor, SDL_Surface * dst, unsigned char quality)
{
if (text.length()==0)
return;
SDL_Surface * temp;
switch (quality)
{
case 0:
temp = TTF_RenderText_Solid(font,text.c_str(),kolor);
break;
case 1:
SDL_Color tem;
tem.b = 0xff-kolor.b;
tem.g = 0xff-kolor.g;
tem.r = 0xff-kolor.r;
tem.unused = 0xff-kolor.unused;
temp = TTF_RenderText_Shaded(font,text.c_str(),kolor,tem);
break;
case 2:
temp = TTF_RenderText_Blended(font,text.c_str(),kolor);
break;
default:
temp = TTF_RenderText_Blended(font,text.c_str(),kolor);
break;
}
SDL_BlitSurface(temp,NULL,dst,&genRect(temp->h,temp->w,x,y));
SDL_FreeSurface(temp);
}
void CSDL_Ext::printAt(const std::string & text, int x, int y, TTF_Font * font, SDL_Color kolor, SDL_Surface * dst, unsigned char quality)
{
if (text.length()==0)

View File

@ -67,6 +67,7 @@ namespace CSDL_Ext
void printAtMiddleWB(const std::string & text, int x, int y, TTF_Font * font, int charpr, SDL_Color kolor=tytulowy, SDL_Surface * dst=screen);
void printAtWB(const std::string & text, int x, int y, TTF_Font * font, int charpr, SDL_Color kolor=tytulowy, SDL_Surface * dst=screen);
void printAt(const std::string & text, int x, int y, TTF_Font * font, SDL_Color kolor=tytulowy, SDL_Surface * dst=screen, unsigned char quality = 2); // quality: 0 - lowest, 1 - medium, 2 - highest
void printAtWR(const std::string & text, int x, int y, TTF_Font * font, SDL_Color kolor=tytulowy, SDL_Surface * dst=screen, unsigned char quality = 2); // quality: 0 - lowest, 1 - medium, 2 - highest
void update(SDL_Surface * what = screen); //updates whole surface (default - main screen)
void drawBorder(SDL_Surface * sur, int x, int y, int w, int h, int3 color);
void setPlayerColor(SDL_Surface * sur, unsigned char player); //sets correct color of flags; -1 for neutral