00001 #include <game.h>
00002
00003 #include <iostream>
00004
00006
00007
00008
00009 console::console() : base("console")
00010 {
00011 text = new stringw(L"");
00012
00013 history = new std::vector<stringw>;
00014 histPos=0;
00015 }
00016
00017 console::~console()
00018 {
00019
00020 }
00021
00022 int console::init(coeur *c,IGUIEnvironment *e)
00023 {
00024 base::init(0,c);
00025 env = e;
00026
00027 cons = env->addImage(rect<s32>(0,0,600,420));
00028 tx = env->addEditBox(L"", rect<s32>(0, 20, 600, 400),true,cons);
00029 box = env->addEditBox(L"", rect<s32>(20, 400, 600, 420),true,cons);
00030 env->addStaticText(L">>>", rect<s32>(0, 400, 20, 420),false,true,cons);
00031 log("Console env init");
00032
00033 tx->setEnabled(false);
00034 tx->setDrawBorder(false);
00035 tx->setWordWrap(true);
00036 tx->setMultiLine(true);
00037 tx->setAutoScroll(true);
00038 tx->setTextAlignment(EGUIA_UPPERLEFT, EGUIA_LOWERRIGHT);
00039 log("Tx configured");
00040
00041 box->setDrawBorder(false);
00042 box->setMultiLine(false);
00043 log("Box configured");
00044
00045 FPS = env->addStaticText(L"", rect<s32>(0,0,100,20),false,true,cons);
00046
00047 cons->setVisible(false);
00048
00049 mycore->py->addPyEnv("co",object(ptr(this)));
00050 mycore->py->addPyEnv("inter",object(ptr(c->in)));
00051 mycore->py->addPyEnv("mycredit",object(ptr(c->mycredit)));
00052 mycore->py->addPyEnv("currentChap",object(ptr(c->currentChap)));
00053
00054 mycore->py->addPyEnv("currentLocal",object(ptr(c->currentLocal)));
00055
00056 mycore->py->exec("import sys");
00057 mycore->py->exec("out = sys.stdout");
00058 mycore->py->exec("err = sys.stderr");
00059 mycore->py->exec("sys.stdout = co");
00060 mycore->py->exec("sys.stderr = co");
00061 mycore->py->exec("def position():\n\tco.position()\n");
00062 mycore->py->exec("def rotation():\n\tco.rotation()\n");
00063 mycore->py->exec("def target():\n\tco.target()\n");
00064 mycore->py->exec("def exit():\n\tco.exit()\n");
00065 mycore->py->exec("def quit():\n\tco.quit()\n");
00066 mycore->py->exec("def help():\n\tco.help()\n");
00067 mycore->py->exec("def fps():\n\tco.fps()\n");
00068
00069 return 1;
00070 }
00071
00072 int console::update()
00073 {
00074 if (cons->isVisible())
00075 {
00076 env->setFocus(box);
00077 FPS->setText((stringw(L"FPS : ") + stringw(env->getVideoDriver()->getFPS())).c_str());
00078 }
00079 else
00080 env->setFocus(0);
00081 return 1;
00082 }
00083
00084 int console::close()
00085 {
00086 mycore->py->exec("sys.stdout = out");
00087 mycore->py->exec("sys.stderr = err");
00088 base::close();
00089 return 1;
00090 }
00091
00092 bool console::OnEvent(const SEvent& event)
00093 {
00094 if ((event.EventType==EET_KEY_INPUT_EVENT)&&(event.KeyInput.PressedDown)&&(event.KeyInput.Key==KEY_TAB))
00095 exit();
00096 if ((event.EventType==EET_KEY_INPUT_EVENT)&&(event.KeyInput.PressedDown)&&(event.KeyInput.Key==KEY_UP)&&(histPos>0))
00097 {
00098 histPos--;
00099 box->setText(history->at(histPos).c_str());
00100 }
00101 if ((event.EventType==EET_KEY_INPUT_EVENT)&&(event.KeyInput.PressedDown)&&(event.KeyInput.Key==KEY_DOWN)&&(histPos<history->size()))
00102 {
00103 histPos++;
00104 if (histPos==history->size())
00105 box->setText(L"");
00106 else
00107 box->setText(history->at(histPos).c_str());
00108 }
00109 if (! cons->isVisible())
00110 return false;
00111 if ((event.EventType==EET_GUI_EVENT) && (event.GUIEvent.EventType==EGET_EDITBOX_ENTER))
00112 {
00113 write((stringc(">>>") + box->getText() + "\n").c_str());
00114 stringc msg = mycore->py->eval(box->getText());
00115 write(msg.c_str());
00116 history->push_back(box->getText());
00117 histPos=history->size();
00118 log(stringc("Run python command : ") + box->getText());
00119 box->setText(L"");
00120 return true;
00121 }
00122 if (cons->isVisible() && (event.EventType==EET_KEY_INPUT_EVENT))
00123 return true;
00124 return false;
00125 }
00126
00130 void console::write(std::string const& message)
00131 {
00132 *text = *text + (message.c_str());
00133 tx->setText(text->c_str());
00134 return;
00135 }
00136
00137 void console::position()
00138 {
00139 write((stringc("Camera position :\n\t") + stringc(mycore->cam->getPosition().X) + "\n\t" + stringc(mycore->cam->getPosition().Y) + "\n\t" + stringc(mycore->cam->getPosition().Z) + "\n").c_str());
00140 log("Display camera position");
00141 }
00142
00143 void console::rotation()
00144 {
00145 write((stringc("Camera rotation :\n\t") + stringc(mycore->cam->getRotation().X) + "\n\t" + stringc(mycore->cam->getRotation().Y) + "\n\t" + stringc(mycore->cam->getRotation().Z) + "\n").c_str());
00146 log("Display rotation");
00147 }
00148
00149 void console::target()
00150 {
00151 write((stringc("Camera target :\n\t") + stringc(mycore->cam->getTarget().X) + "\n\t" + stringc(mycore->cam->getTarget().Y) + "\n\t" + stringc(mycore->cam->getTarget().Z) + "\n").c_str());
00152 log("Display target position");
00153 }
00154
00155 void console::exit()
00156 {
00157 if (cons->isVisible())
00158 {
00159 cons->setVisible(false);
00160 log("Closing console");
00161 }
00162 else
00163 {
00164 cons->setVisible(true);
00165 log("Opening console");
00166 }
00167 }
00168
00169 void console::quit()
00170 {
00171 write("Quitting\n");
00172 mycore->q=true;
00173 log("Quitting by console");
00174 }
00175
00176 void console::help()
00177 {
00178 write("Welcome to the console of Merry Turnip\n");
00179 write((stringc("The version is ") + mycore->getVersion() + "\n").c_str());
00180 write("For the source code : 2009 (c) DAVY Guillaume\n");
00181 write("------------------------------------------------\n");
00182 write("Here is the list of avaidable command :\n");
00183 write("\t\thelp() : Show this message\n");
00184 write("\t\tposition() : give the position of the camera\n");
00185 write("\t\trotation() : give the rotation of the camera\n");
00186 write("\t\ttarget() : give the position of the target of the camera\n");
00187 write("\t\tfps() : give the fps\n");
00188 write("\t\texit() : exit the console\n");
00189 write("\t\tquit() : quit the game\n");
00190 write("------------------------------------------------\n");
00191 log("Show help");
00192 }
00193
00194 void console::fps()
00195 {
00196 write((stringc("FPS : ") + stringw(env->getVideoDriver()->getFPS()) + "\n").c_str());
00197 log("Show FPS");
00198 }
00199