E:/Eigene Dateien/Eigene Projekte/c0re/Menu.cpp

Go to the documentation of this file.
00001 /*
00002         This file is part of c0re.
00003 
00004         c0re is a multiplayer RTS on a hexagonal map with an evolving unit concept.
00005     Copyright (C) 2007 Stephan Hofmann
00006 
00007     c0re is free software: you can redistribute it and/or modify
00008     it under the terms of the GNU General Public License as published by
00009     the Free Software Foundation, either version 3 of the License, or
00010     (at your option) any later version.
00011 
00012     This program is distributed in the hope that it will be useful,
00013     but WITHOUT ANY WARRANTY; without even the implied warranty of
00014     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015     GNU General Public License for more details.
00016 
00017     You should have received a copy of the GNU General Public License
00018     along with this program.  If not, see <http://www.gnu.org/licenses/>.
00019 */
00020 
00021 #include "Menu.h"
00022 
00023 #include "system.h"
00024 #include <GL/gl.h>
00025 #include <GL/glu.h>
00026 
00027 #include "Window.h"
00028 
00029 Menu::Menu (Window* p_window) : GLInterface (p_window)
00030 {
00031 }
00032 
00033 Menu::~Menu()
00034 {
00035 }
00036 
00037 std::string Menu::getLevelName(std::string p_directory, std::string p_mask)
00038 {
00039         std::string ret;
00040 
00041         // get all files in the current directory
00042         std::vector<std::string> level_files = this->getLevelFiles(p_directory + "//" + p_mask);
00043 
00044         ret = this->getSelectedItem(level_files);
00045         
00046         //if not "QUIT" add directory
00047         if (ret != "QUIT")
00048                 ret.insert(0, p_directory + "//");
00049         else //ret is empty
00050                 ret = "";
00051 
00052         return ret;
00053 }
00054 
00055 eGameMode Menu::getGameMode()
00056 {
00057         eGameMode ret = GM_None;
00058 
00059         std::vector<std::string> options;
00060 
00061         options.push_back("local");
00062         options.push_back("host");
00063         options.push_back("connect");
00064 
00065         //now the copyright notice
00066         options.push_back("");
00067         options.push_back("");
00068         options.push_back("c0re  Copyright (C) 2007  Stephan Hofmann");
00069         options.push_back("");
00070         options.push_back("This program comes with ABSOLUTELY NO");
00071         options.push_back("WARRANTY. This is free software, and ");
00072         options.push_back("you are welcome to redistribute it under");
00073         options.push_back("certain conditions; See license.txt");
00074 
00075         std::string result = this->getSelectedItem(options);
00076         if (result == "local")
00077                 ret = GM_Local;
00078         else if (result == "host")
00079                 ret = GM_Server;
00080         else if (result == "connect")
00081                 ret = GM_Client;
00082 
00083         return ret;
00084 }
00085 
00086 std::string Menu::getString(std::string p_display, std::string p_default)
00087 {
00088         std::string ret = p_default;
00089         initGL();
00090         
00091         bool run = true;
00092 
00093         do
00094         {
00095                 this->renderInputText(p_display, ret);
00096                 if (window->checkMessages() == 1)
00097                         run = false;
00098                 //get messages
00099                 std::string msg;
00100                 while (inputMessages.size() > 0)
00101                 {
00102                         msg = inputMessages.front();
00103                         inputMessages.pop_front();
00104                         if ((msg == "QUIT") || (msg == "+ESC"))
00105                         {
00106                                 ret = "";
00107                                 run = false;
00108                         }
00109                         else if (msg == "+RETURN")
00110                                 run = false;
00111                         else if (msg == "+SPACE")
00112                                 ret.append(" ");
00113                         else if ((msg == "+BACKSPACE") && (ret.length() > 0))
00114                                 ret.erase(ret.length() - 1, 1 );
00115                         else if ((msg.size() == 2) && (msg[0] == '+'))
00116                                 ret += msg[1];
00117                 }
00118 
00119         } while (run);
00120 
00121         window->clearCurrent();
00122         return ret;
00123 }
00124 
00125 std::string Menu::getSelectedItem(std::vector<std::string> &p_options, int p_maxOption)
00126 {
00127         if ((p_maxOption == -1) || (p_maxOption > (int)p_options.size()))
00128                 p_maxOption = (int)p_options.size();
00129                 
00130         std::string ret = "";
00131         
00132         initGL();
00133         
00134         //highlighting the first Element
00135         unsigned int currentElement = 0;
00136         
00137         //process user queries
00138         do
00139         {
00140                 //render the menu
00141                 this->renderSelectionList(p_options, currentElement);
00142                 if (window->checkMessages() == 1)
00143                         ret = "QUIT";
00144 
00145                 std::string msg;
00146                 while (inputMessages.size() > 0)
00147                 {
00148                         msg = inputMessages.front();
00149                         inputMessages.pop_front();
00150                         if ((msg == "QUIT") || (msg == "+ESC"))
00151                                 ret = "QUIT";
00152                         else if ((msg == "+SPACE") || (msg == "+RETURN"))
00153                         {
00154                                 if (currentElement != p_maxOption)
00155                                 {
00156                                         ret = p_options[currentElement];
00157                                 }
00158                                 else //but only if not "Exit" is selected
00159                                 {
00160                                         ret = "QUIT";
00161                                 }
00162                         }
00163                         else if (msg == "+UPARROW")
00164                         {
00165                                 if (currentElement != 0)
00166                                 {
00167                                         currentElement--;
00168                                 }
00169                         }
00170                         else if (msg == "+DOWNARROW")
00171                         {
00172                                 if (currentElement != p_maxOption)
00173                                 {
00174                                         currentElement++;
00175                                 }
00176                         }
00177                 }
00178                 sleep();
00179         } while (ret.length() == 0); //Repeat until something has been selected
00180 
00181         window->clearCurrent();
00182 
00183         return ret;
00184 }
00185 
00186 int Menu::renderSelectionList(std::vector<std::string> &p_options, unsigned int p_currentElement)
00187 {
00188         int ret = 0;
00189         writeText(p_options, p_currentElement + 1);
00190         return ret;
00191 }
00192 
00193 int Menu::renderInputText(std::string p_display, std::string p_input)
00194 {
00195         int ret = 0;
00196         std::vector<std::string> vec;
00197         vec.push_back(p_display);
00198         vec.push_back(std::string(">") + p_input);
00199         writeText(vec, 2);
00200         return ret;
00201 }
00202 
00203 std::vector<std::string> Menu::getLevelFiles(std::string p_filemask)
00204 {
00205         std::vector<std::string> ret;
00206 
00207         HANDLE fHandle; 
00208         WIN32_FIND_DATA wfd; 
00209 
00210         // fetch first file in the directory
00211         fHandle=FindFirstFile(p_filemask.c_str(),&wfd); 
00212         do
00213         { 
00214                 if (wfd.dwFileAttributes != FILE_ATTRIBUTE_DIRECTORY) 
00215                 {
00216                         ret.push_back(std::string(wfd.cFileName));
00217                 } 
00218         } while (FindNextFile(fHandle,&wfd));
00219         FindClose(fHandle);
00220 
00221         return ret;
00222 }
00223 
00224 int Menu::initGL()
00225 {
00226         GLInterface::initGL();
00227 
00228         return 0;
00229 }
00230 
00231 int Menu::writeText(std::vector<std::string> p_text, int p_highlightedLine)
00232 {
00233         int ret = 0;
00234         int begin = 0;
00235         int end = (int)p_text.size();
00236         if (p_highlightedLine < 1)
00237                 p_highlightedLine = 1;
00238         else if (p_highlightedLine > end)
00239                 p_highlightedLine = end;
00240         p_highlightedLine--;
00241 
00242         int count = 3;
00243 
00244         glMatrixMode(GL_MODELVIEW);
00245         glLoadIdentity();
00246         glMatrixMode(GL_PROJECTION);
00247         glLoadIdentity();
00248 
00249         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
00250         glViewport(0, 0, window->width, window->height);
00251         
00252         //perspective view 45deg fov
00253         gluPerspective(45.0, (GLfloat)window->width/(GLfloat)window->height, 0.125, 1048575.875);
00254 
00255         //now turn to look in thhe other direction
00256         glRotated(180.0, 0.0, 1.0, 0.0);
00257         //and set the postion fixed, to see the text
00258         glTranslated(1.0, 0.0, 2.0);
00259 
00260         double lineHeight = 0.1;
00261 
00262         int i;
00263         glColor3d(0.5, 0.5, 0.5);
00264         for (i = begin; i < p_highlightedLine; i++)
00265                 glPrint2d(0.0, (count - i) * (lineHeight), p_text[i].c_str());
00266         glColor3d(1.0, 1.0, 1.0);
00267         glPrint2d(0.0, (count - p_highlightedLine) * (lineHeight), p_text[p_highlightedLine].c_str());  
00268         glColor3d(0.5, 0.5, 0.5);
00269         for (i = p_highlightedLine + 1; i < end; i++)
00270                 glPrint2d(0.0, (count - i) * (lineHeight), p_text[i].c_str());
00271 
00272         glFlush();
00273         //swap buffer
00274         window->swapBuffers();
00275 
00276         return ret;
00277 }

Generated on Tue Jul 17 22:02:22 2007 for C0re by  doxygen 1.5.2