ConsoleGump.cpp

Go to the documentation of this file.
00001 /*
00002  *  Copyright (C) 2003-2004  The Pentagram Team
00003  *
00004  *  This program is free software; you can redistribute it and/or modify
00005  *  it under the terms of the GNU General Public License as published by
00006  *  the Free Software Foundation; either version 2 of the License, or
00007  *  (at your option) any later version.
00008  *
00009  *  This program is distributed in the hope that it will be useful,
00010  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *  GNU General Public License for more details.
00013  *
00014  *  You should have received a copy of the GNU General Public License
00015  *  along with this program; if not, write to the Free Software
00016  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00017  */
00018 
00019 #include "pent_include.h"
00020 #include "ConsoleGump.h"
00021 #include "IDataSource.h"
00022 #include "ODataSource.h"
00023 
00024 #include "GUIApp.h"
00025 
00026 DEFINE_RUNTIME_CLASSTYPE_CODE(ConsoleGump,Gump);
00027 
00028 using Pentagram::istring;
00029 
00030 //Pentagram::istring    ConsoleGump::commandBuffer;
00031 //std::map<Pentagram::istring,ConsoleGump::ConsoleFunction> ConsoleGump::ConsoleCommands;
00032 
00033 ConsoleGump::ConsoleGump()
00034         : Gump()
00035 {
00036         con.AddConsoleCommand("ConsoleGump::toggle",
00037                                                   ConsoleGump::ConCmd_toggle);
00038 }
00039 
00040 ConsoleGump::ConsoleGump(int X, int Y, int Width, int Height) :
00041         Gump(X,Y,Width,Height, 0, FLAG_DONT_SAVE | FLAG_CORE_GUMP, LAYER_CONSOLE),
00042         scroll_state(NORMAL_DISPLAY)
00043 {
00044         con.ClearCommandBuffer();
00045 
00046         // Resize it
00047         con.CheckResize(Width);
00048 
00049         con.AddConsoleCommand("ConsoleGump::toggle",
00050                                                   ConsoleGump::ConCmd_toggle);
00051 }
00052 
00053 ConsoleGump::~ConsoleGump()
00054 {
00055         con.RemoveConsoleCommand(ConsoleGump::ConCmd_toggle);
00056 }
00057 
00058 void ConsoleGump::RenderSurfaceChanged()
00059 {
00060         // Resize the desktop gump to match the RenderSurface
00061         Pentagram::Rect new_dims;
00062         parent->GetDims(new_dims);
00063         con.CheckResize(new_dims.w);
00064         dims.w = new_dims.w;
00065         dims.h = new_dims.h;
00066 
00067         Gump::RenderSurfaceChanged();
00068 }
00069 
00070 void ConsoleGump::PaintThis(RenderSurface *surf, sint32 lerp_factor, bool scaled)
00071 {
00072         Gump::PaintThis(surf,lerp_factor, scaled);
00073 
00074         if (scroll_state == NOTIFY_OVERLAY)
00075         {
00076 #ifdef DEBUG
00077                 con.DrawConsoleNotify(surf);
00078 #endif
00079         }
00080         else if (scroll_state != WAITING_TO_SHOW)
00081         {
00082                 int h = dims.h;
00083                 if (scroll_state == SCROLLING_TO_SHOW_1) 
00084                         h = (h*(000+lerp_factor))/1024;
00085                 else if (scroll_state == SCROLLING_TO_SHOW_2) 
00086                         h = (h*(256+lerp_factor))/1024;
00087                 else if (scroll_state == SCROLLING_TO_SHOW_3) 
00088                         h = (h*(512+lerp_factor))/1024;
00089                 else if (scroll_state == SCROLLING_TO_SHOW_4) 
00090                         h = (h*(768+lerp_factor))/1024;
00091 
00092                 else if (scroll_state == SCROLLING_TO_HIDE_1) 
00093                         h = (h*(1024-lerp_factor))/1024;
00094                 else if (scroll_state == SCROLLING_TO_HIDE_2)
00095                         h = (h*(768-lerp_factor))/1024;
00096                 else if (scroll_state == SCROLLING_TO_HIDE_3)
00097                         h = (h*(512-lerp_factor))/1024;
00098                 else if (scroll_state == SCROLLING_TO_HIDE_4)
00099                         h = (h*(256-lerp_factor))/1024;
00100 
00101                 con.DrawConsole(surf,h);
00102         }
00103 }
00104 
00105 void ConsoleGump::ToggleConsole()
00106 {
00107         switch (scroll_state)
00108         {
00109         case WAITING_TO_HIDE:
00110                 scroll_state = SCROLLING_TO_SHOW_4;
00111                 break;
00112 
00113         case SCROLLING_TO_HIDE_1:
00114                 scroll_state = SCROLLING_TO_SHOW_3;
00115                 break;
00116 
00117         case SCROLLING_TO_HIDE_2:
00118                 scroll_state = SCROLLING_TO_SHOW_2;
00119                 break;
00120 
00121         case SCROLLING_TO_HIDE_3:
00122                 scroll_state = SCROLLING_TO_SHOW_1;
00123                 break;
00124 
00125         case SCROLLING_TO_HIDE_4:
00126                 scroll_state = WAITING_TO_SHOW;
00127                 break;
00128 
00129         case NOTIFY_OVERLAY:
00130                 scroll_state = WAITING_TO_SHOW;
00131                 break;
00132 
00133         case WAITING_TO_SHOW:
00134                 scroll_state = SCROLLING_TO_HIDE_4;
00135                 break;
00136 
00137         case SCROLLING_TO_SHOW_1:
00138                 scroll_state = SCROLLING_TO_HIDE_3;
00139                 break;
00140 
00141         case SCROLLING_TO_SHOW_2:
00142                 scroll_state = SCROLLING_TO_HIDE_2;
00143                 break;
00144 
00145         case SCROLLING_TO_SHOW_3:
00146                 scroll_state = SCROLLING_TO_HIDE_1;
00147                 break;
00148 
00149         case SCROLLING_TO_SHOW_4:
00150                 scroll_state = WAITING_TO_HIDE;
00151                 break;
00152 
00153         case NORMAL_DISPLAY:
00154                 scroll_state = WAITING_TO_HIDE;
00155                 GUIApp::get_instance()->leaveTextMode(this);
00156                 con.ClearCommandBuffer();
00157                 break;
00158 
00159         default:
00160                 break;
00161         }
00162 }
00163 
00164 
00165 void ConsoleGump::HideConsole()
00166 {
00167         switch (scroll_state)
00168         {
00169         case WAITING_TO_SHOW:
00170                 scroll_state = SCROLLING_TO_HIDE_4;
00171                 break;
00172 
00173         case SCROLLING_TO_SHOW_1:
00174                 scroll_state = SCROLLING_TO_HIDE_3;
00175                 break;
00176 
00177         case SCROLLING_TO_SHOW_2:
00178                 scroll_state = SCROLLING_TO_HIDE_2;
00179                 break;
00180 
00181         case SCROLLING_TO_SHOW_3:
00182                 scroll_state = SCROLLING_TO_HIDE_1;
00183                 break;
00184 
00185         case SCROLLING_TO_SHOW_4:
00186                 scroll_state = WAITING_TO_HIDE;
00187                 break;
00188 
00189         case NORMAL_DISPLAY:
00190                 scroll_state = WAITING_TO_HIDE;
00191                 GUIApp::get_instance()->leaveTextMode(this);
00192                 con.ClearCommandBuffer();
00193                 break;
00194 
00195         default:
00196                 break;
00197         }
00198 }
00199 
00200 
00201 void ConsoleGump::ShowConsole()
00202 {
00203         switch (scroll_state)
00204         {
00205         case WAITING_TO_HIDE:
00206                 scroll_state = SCROLLING_TO_SHOW_4;
00207                 break;
00208 
00209         case SCROLLING_TO_HIDE_1:
00210                 scroll_state = SCROLLING_TO_SHOW_3;
00211                 break;
00212 
00213         case SCROLLING_TO_HIDE_2:
00214                 scroll_state = SCROLLING_TO_SHOW_2;
00215                 break;
00216 
00217         case SCROLLING_TO_HIDE_3:
00218                 scroll_state = SCROLLING_TO_SHOW_1;
00219                 break;
00220 
00221         case SCROLLING_TO_HIDE_4:
00222                 scroll_state = WAITING_TO_SHOW;
00223                 break;
00224 
00225         case NOTIFY_OVERLAY:
00226                 scroll_state = WAITING_TO_SHOW;
00227                 break;
00228 
00229         default:
00230                 break;
00231         }
00232 }
00233 
00234 bool ConsoleGump::ConsoleIsVisible()
00235 {
00236         return scroll_state != NOTIFY_OVERLAY;
00237 }
00238 
00239 bool ConsoleGump::Run(const uint32 framenum)
00240 {
00241         Gump::Run(framenum);
00242 
00243         con.setFrameNum(framenum);
00244 
00245         switch (scroll_state)
00246         {
00247         case WAITING_TO_HIDE:
00248                 scroll_state = SCROLLING_TO_HIDE_1;
00249                 break;
00250 
00251         case SCROLLING_TO_HIDE_1:
00252                 scroll_state = SCROLLING_TO_HIDE_2;
00253                 break;
00254 
00255         case SCROLLING_TO_HIDE_2:
00256                 scroll_state = SCROLLING_TO_HIDE_3;
00257                 break;
00258 
00259         case SCROLLING_TO_HIDE_3:
00260                 scroll_state = SCROLLING_TO_HIDE_4;
00261                 break;
00262 
00263         case SCROLLING_TO_HIDE_4:
00264                 scroll_state = NOTIFY_OVERLAY;
00265                 break;
00266 
00267         case WAITING_TO_SHOW:
00268                 scroll_state = SCROLLING_TO_SHOW_1;
00269                 break;
00270 
00271         case SCROLLING_TO_SHOW_1:
00272                 scroll_state = SCROLLING_TO_SHOW_2;
00273                 break;
00274 
00275         case SCROLLING_TO_SHOW_2:
00276                 scroll_state = SCROLLING_TO_SHOW_3;
00277                 break;
00278 
00279         case SCROLLING_TO_SHOW_3:
00280                 scroll_state = SCROLLING_TO_SHOW_4;
00281                 break;
00282 
00283         case SCROLLING_TO_SHOW_4:
00284                 scroll_state = NORMAL_DISPLAY;
00285                 GUIApp::get_instance()->enterTextMode(this);
00286                 con.ClearCommandBuffer();
00287                 break;
00288 
00289         default:
00290                 break;
00291         }
00292 
00293         return true;    // Always repaint, even though we really could just try to detect it
00294 }
00295 
00296 void ConsoleGump::ConCmd_toggle(const Console::ArgvType &argv)
00297 {
00298         ConsoleGump * consoleGump = GUIApp::get_instance()->getConsoleGump();
00299         consoleGump->ToggleConsole();
00300 }
00301 
00302 void ConsoleGump::saveData(ODataSource* ods)
00303 {
00304         CANT_HAPPEN_MSG("Trying to save ConsoleGump");
00305 }
00306 
00307 bool ConsoleGump::loadData(IDataSource* ids, uint32 version)
00308 {
00309         CANT_HAPPEN_MSG("Trying to save ConsoleGump");
00310 
00311         return false;
00312 }
00313 
00314 bool ConsoleGump::OnTextInput(int unicode)
00315 {
00316         bool handled = false;
00317         if (scroll_state == NORMAL_DISPLAY) {
00318 
00319                 con.AddCharacterToCommandBuffer(unicode);
00320                 handled = true;
00321         }
00322         return handled;
00323 }
00324 
00325 
00326 bool ConsoleGump::OnKeyDown(int key, int mod)
00327 {
00328         bool handled = false;
00329         if (scroll_state == NORMAL_DISPLAY)
00330         {
00331                 switch(key)
00332                 {
00333                         // Command completion
00334                 case SDLK_TAB:
00335                         con.AddCharacterToCommandBuffer(Console::Tab);
00336                         break;
00337 
00338                 case SDLK_RETURN:
00339                 case SDLK_KP_ENTER:
00340                         con.AddCharacterToCommandBuffer(Console::Enter);
00341                         break;
00342 
00343                 case SDLK_BACKSPACE:
00344                         con.DeleteCommandBufferChars(-1);
00345                         break;
00346 
00347                 case SDLK_DELETE:
00348                         con.DeleteCommandBufferChars(1);
00349                         break;
00350 
00351                 case SDLK_PAGEUP:
00352                         con.ScrollConsole(-3);
00353                         break;
00354 
00355                 case SDLK_PAGEDOWN:
00356                         con.ScrollConsole(3); 
00357                         break;
00358 
00359                 case SDLK_UP:
00360                         con.ScrollCommandHistory(-1);
00361                         break;
00362 
00363                 case SDLK_DOWN:
00364                         con.ScrollCommandHistory(1);
00365                         break;
00366 
00367                 case SDLK_LEFT:
00368                         con.MoveCommandCursor(-1);
00369                         break;
00370 
00371                 case SDLK_RIGHT:
00372                         con.MoveCommandCursor(1);
00373                         break;
00374 
00375                 case SDLK_INSERT:
00376                         con.ToggleCommandInsert();
00377                         break;
00378 
00379                 case SDLK_KP0:
00380                 case SDLK_KP1:
00381                 case SDLK_KP2:
00382                 case SDLK_KP3:
00383                 case SDLK_KP4:
00384                 case SDLK_KP5:
00385                 case SDLK_KP6:
00386                 case SDLK_KP7:
00387                 case SDLK_KP8:
00388                 case SDLK_KP9:
00389                         OnTextInput(key - SDLK_KP0 + '0');
00390                         break;
00391 
00392                 default:
00393                         break;
00394                 }
00395                 handled = true;
00396         }
00397         return handled;
00398 }
00399 
00400 void ConsoleGump::OnFocus(bool gain)
00401 {
00402         /*
00403         if (scroll_state == NORMAL_DISPLAY) {
00404                 if (gain)
00405                         GUIApp::get_instance()->enterTextMode(this);
00406                 else
00407                         GUIApp::get_instance()->leaveTextMode(this);
00408         }
00409         */
00410 
00411 }
00412 
00413 // Colourless Protection

Generated on Fri Jul 27 22:27:12 2007 for pentagram by  doxygen 1.4.7