GameplayOptions_gump.cc

Go to the documentation of this file.
00001 /*
00002  *  Copyright (C) 2001  The Exult 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 #ifdef HAVE_CONFIG_H
00020 #  include <config.h>
00021 #endif
00022 
00023 #include <iostream>
00024 #include <cstring>
00025 
00026 #include "SDL_events.h"
00027 
00028 #include "Gump_manager.h"
00029 #include "Configuration.h"
00030 #include "Gump_button.h"
00031 #include "Gump_ToggleButton.h"
00032 #include "GameplayOptions_gump.h"
00033 #include "exult.h"
00034 #include "exult_flx.h"
00035 #include "game.h"
00036 #include "gamewin.h"
00037 #include "mouse.h"
00038 #include "cheat.h"
00039 #include "Face_stats.h"
00040 #include "Text_button.h"
00041 #include "Enabled_button.h"
00042 
00043 using std::cerr;
00044 using std::endl;
00045 using std::string;
00046 
00047 static const int rowy[] = { 4, 17, 134, 30, 43, 56, 69, 82, 95, 108, 121, 147 };
00048 static const int colx[] = { 35, 50, 120, 195, 192 };
00049 
00050 static const char* oktext = "OK";
00051 static const char* canceltext = "CANCEL";
00052 
00053 static int framerates[] = { 2, 4, 6, 8, 10, -1 };
00054  // -1 is placeholder for custom framerate
00055 static int num_default_rates = sizeof(framerates)/sizeof(framerates[0]) - 1;
00056 
00057 
00058 static string framestring(int fr)
00059 {
00060   char buf[100];
00061   sprintf(buf, "%i fps", fr);
00062   return buf;
00063 }
00064 
00065 class GameplayOptions_button : public Text_button {
00066 public:
00067   GameplayOptions_button(Gump *par, string text, int px, int py)
00068     : Text_button(par, text, px, py, 59, 11)
00069     { }
00070           // What to do when 'clicked':
00071   virtual void activate();
00072 };
00073 
00074 void GameplayOptions_button::activate()
00075 {
00076   if (text == canceltext) {
00077     ((GameplayOptions_gump*)parent)->cancel();
00078   } else if (text == oktext) {
00079     ((GameplayOptions_gump*)parent)->close();
00080   }
00081 }
00082 
00083 class GameplayTextToggle : public Gump_ToggleTextButton {
00084 public:
00085   GameplayTextToggle(Gump* par, std::string *s, int px, int py, int width,
00086              int selectionnum, int numsel)
00087     : Gump_ToggleTextButton(par, s, selectionnum, numsel, px, py, width)
00088   { }
00089 
00090   friend class GameplayOptions_gump;
00091   virtual void toggle(int state) { 
00092     ((GameplayOptions_gump*)parent)->toggle((Gump_button*)this, state);
00093   }
00094 };
00095 
00096 class GameplayEnabledToggle : public Enabled_button {
00097 public:
00098   GameplayEnabledToggle(Gump* par, int px, int py, int width, 
00099               int selectionnum)
00100     : Enabled_button(par, selectionnum, px, py, width)
00101   { }
00102 
00103   friend class GameplayOptions_gump;
00104   virtual void toggle(int state) {
00105     ((GameplayOptions_gump*)parent)->toggle((Gump_button*)this, state);
00106   }
00107 };  
00108 
00109 void GameplayOptions_gump::close()
00110 {
00111   save_settings();
00112   done = 1;
00113 }
00114 
00115 void GameplayOptions_gump::cancel()
00116 {
00117   done = 1;
00118 }
00119 
00120 void GameplayOptions_gump::toggle(Gump_button* btn, int state)
00121 {
00122   if (btn == buttons[0])
00123     facestats = state;
00124   else if (btn == buttons[1])
00125     fastmouse = state;
00126   else if (btn == buttons[2])
00127     mouse3rd = state;
00128   else if (btn == buttons[3])
00129     doubleclick = state;
00130   else if (btn == buttons[4])
00131     cheats = state;
00132   else if (btn == buttons[5])
00133     paperdolls = state;
00134   else if (btn == buttons[6])
00135     text_bg = state;
00136   else if (btn == buttons[8])
00137     frames = state;
00138   else if (btn == buttons[11])
00139     rightclick_close = state;
00140   else if (btn == buttons[12])
00141     doubleright_move = state;
00142   else if (btn == buttons[13])
00143     gumps_pause = state;
00144 }
00145 
00146 void GameplayOptions_gump::build_buttons()
00147 {
00148 #if 0
00149   std::string *enabledtext1 = new std::string[2];
00150   enabledtext1[0] = "Disabled";
00151   enabledtext1[1] = "Enabled";
00152 
00153   std::string *enabledtext2 = new std::string[2];
00154   enabledtext2[0] = "Disabled";
00155   enabledtext2[1] = "Enabled";
00156 
00157   std::string *enabledtext3 = new std::string[2];
00158   enabledtext3[0] = "Disabled";
00159   enabledtext3[1] = "Enabled";
00160 
00161   std::string *enabledtext4 = new std::string[2];
00162   enabledtext4[0] = "Disabled";
00163   enabledtext4[1] = "Enabled";
00164 
00165   std::string *enabledtext5 = new std::string[2];
00166   enabledtext5[0] = "Disabled";
00167   enabledtext5[1] = "Enabled";
00168 #endif
00169 
00170   std::string *stats = new std::string[4];
00171   stats[0] = "Disabled";
00172   stats[1] = "Left";
00173   stats[2] = "Middle";
00174   stats[3] = "Right";
00175 
00176   std::string *textbgcolor = new std::string[12];
00177   textbgcolor[0] = "Disabled";
00178   textbgcolor[1] = "Purple";
00179   textbgcolor[2] = "Orange";
00180   textbgcolor[3] = "Light Gray";
00181   textbgcolor[4] = "Green";
00182   textbgcolor[5] = "Yellow";
00183   textbgcolor[6] = "Pale Blue";
00184   textbgcolor[7] = "Dark Green";
00185   textbgcolor[8] = "Red";
00186   textbgcolor[9] = "Bright White";
00187   textbgcolor[10] = "Dark gray";
00188   textbgcolor[11] = "White";
00189 
00190   buttons[0] = new GameplayTextToggle (this, stats, colx[3], rowy[0], 59,
00191                      facestats, 4);
00192   buttons[6] = new GameplayTextToggle (this, textbgcolor, colx[3]-21, 
00193                      rowy[1], 80, text_bg, 12);
00194   if (GAME_BG)
00195     buttons[5] = new GameplayEnabledToggle(this, colx[3], rowy[2], 59,
00196                          paperdolls);
00197   buttons[1] = new GameplayEnabledToggle(this, colx[3], rowy[3],
00198                        59, fastmouse);
00199   buttons[2] = new GameplayEnabledToggle(this, colx[3], rowy[4],
00200                        59, mouse3rd);
00201   buttons[3] = new GameplayEnabledToggle(this, colx[3], rowy[5],
00202                        59, doubleclick);
00203   buttons[11] = new GameplayEnabledToggle(this, colx[3], rowy[6],
00204                        59, rightclick_close);
00205   buttons[12] = new GameplayEnabledToggle(this, colx[3], rowy[7],
00206                        59, doubleright_move);
00207   buttons[13] = new GameplayEnabledToggle(this, colx[3], rowy[8],
00208                        59, gumps_pause);
00209   buttons[4] = new GameplayEnabledToggle(this, colx[3], rowy[9],
00210                        59, cheats);
00211   buttons[8] = new GameplayTextToggle(this, frametext, colx[3], rowy[10], 
00212                     59, frames, num_framerates);
00213 }
00214 
00215 void GameplayOptions_gump::load_settings()
00216 {
00217   fastmouse = gwin->get_fastmouse();
00218   mouse3rd = gwin->get_mouse3rd();
00219   cheats = cheat();
00220   facestats = Face_stats::get_state() + 1;
00221   doubleclick = 0;
00222   paperdolls = false;
00223   string pdolls;
00224   paperdolls = sman->get_bg_paperdolls();
00225   doubleclick = gwin->get_double_click_closes_gumps();
00226   rightclick_close = gumpman->can_right_click_close();
00227   doubleright_move = gwin->get_allow_double_right_move();
00228   text_bg = gwin->get_text_bg()+1;
00229   gumps_pause = !gumpman->gumps_dont_pause_game();
00230   int realframes = 1000/gwin->get_std_delay();
00231   int i;
00232 
00233     frames = -1;
00234   framerates[num_default_rates] = realframes;
00235   for (i=0; i < num_default_rates; i++) {
00236     if (realframes == framerates[i]) {
00237       frames = i;
00238       break;
00239     }
00240   }
00241 
00242   num_framerates = num_default_rates;
00243   if (frames == -1) {
00244     num_framerates++;
00245     frames = num_default_rates;
00246   }
00247   frametext = new string[num_framerates];
00248   for (i=0; i < num_framerates; i++) {
00249     frametext[i] = framestring(framerates[i]);
00250   }
00251 }
00252 
00253 GameplayOptions_gump::GameplayOptions_gump() : Modal_gump(0, EXULT_FLX_GAMEPLAYOPTIONS_SHP, SF_EXULT_FLX)
00254 {
00255   set_object_area(Rectangle(0, 0, 0, 0), 8, 162);//++++++ ???
00256 
00257   for (int i = 0; i < sizeof(buttons)/sizeof(buttons[0]); i++)
00258     buttons[i] = 0;
00259 
00260   load_settings();
00261   
00262   build_buttons();
00263 
00264   // Ok
00265   buttons[9] = new GameplayOptions_button(this, oktext, colx[0], rowy[11]);
00266   // Cancel
00267   buttons[10] = new GameplayOptions_button(this, canceltext, 
00268                        colx[4], rowy[11]);
00269 }
00270 
00271 GameplayOptions_gump::~GameplayOptions_gump()
00272 {
00273   for (int i = 0; i < sizeof(buttons)/sizeof(buttons[0]); i++)
00274     if (buttons[i])
00275       delete buttons[i];
00276 }
00277 
00278 void GameplayOptions_gump::save_settings()
00279 {
00280   gwin->set_text_bg(text_bg-1);
00281   config->set("config/gameplay/textbackground", text_bg-1, true);
00282   int fps = framerates[frames];
00283   gwin->set_std_delay(1000/fps);
00284   config->set("config/video/fps", fps, true);
00285   gwin->set_fastmouse(fastmouse!=false);
00286   config->set("config/gameplay/fastmouse", fastmouse ? "yes" : "no", true);
00287   gwin->set_mouse3rd(mouse3rd!=false);
00288   config->set("config/gameplay/mouse3rd", mouse3rd ? "yes" : "no", true);
00289   gwin->set_double_click_closes_gumps(doubleclick!=false);
00290   config->set("config/gameplay/double_click_closes_gumps", 
00291         doubleclick ? "yes" : "no", true);
00292   gumpman->set_right_click_close(rightclick_close!=false);
00293   config->set("config/gameplay/right_click_closes_gumps", 
00294         rightclick_close ? "yes" : "no" , true);
00295   cheat.set_enabled(cheats!=false);
00296   while (facestats != Face_stats::get_state() + 1)
00297     Face_stats::AdvanceState();
00298   Face_stats::save_config(config);
00299   if (GAME_BG && sman->can_use_paperdolls())
00300     sman->set_bg_paperdolls(paperdolls!=false);
00301   config->set("config/gameplay/bg_paperdolls", 
00302         paperdolls ? "yes" : "no", true);
00303   gwin->set_allow_double_right_move(doubleright_move != false);
00304   config->set("config/gameplay/allow_double_right_move", doubleright_move?"yes":"no", true);
00305   gumpman->set_gumps_dont_pause_game(!gumps_pause);
00306   config->set("config/gameplay/gumps_dont_pause_game", gumps_pause?"no":"yes", true);
00307 
00308 }
00309 
00310 void GameplayOptions_gump::paint()
00311 {
00312   Gump::paint();
00313   for (int i = 0; i < sizeof(buttons)/sizeof(buttons[0]); i++)
00314     if (buttons[i])
00315       buttons[i]->paint();
00316 
00317   sman->paint_text(2, "Status Bars:", x + colx[0], y + rowy[0] + 1);
00318   sman->paint_text(2, "Text Background:", x + colx[0], y + rowy[1] + 1);
00319   if (GAME_BG)
00320     sman->paint_text(2, "Paperdolls:", x + colx[0], y + rowy[2] + 1);
00321   sman->paint_text(2, "Fast Mouse:", x + colx[0], y + rowy[3] + 1);
00322   sman->paint_text(2, "Use Middle Mouse Button:", x + colx[0], y + rowy[4] + 1);
00323   sman->paint_text(2, "Doubleclick closes Gumps:", x + colx[0], y + rowy[5] + 1);
00324   sman->paint_text(2, "Right click closes Gumps:", x + colx[0], y + rowy[6] + 1);
00325   sman->paint_text(2, "Double Right Pathfinds:", x + colx[0], y + rowy[7] + 1);
00326   sman->paint_text(2, "Gumps pause game:", x + colx[0], y + rowy[8] + 1);
00327   sman->paint_text(2, "Cheats:", x + colx[0], y + rowy[9] + 1);
00328   sman->paint_text(2, "Speed:", x + colx[0], y + rowy[10] + 1);
00329   gwin->set_painted();
00330 }
00331 
00332 void GameplayOptions_gump::mouse_down(int mx, int my)
00333 {
00334   pushed = Gump::on_button(mx, my);
00335           // First try checkmark.
00336   // Try buttons at bottom.
00337   if (!pushed)
00338     for (int i = 0; i < sizeof(buttons)/sizeof(buttons[0]); i++)
00339       if (buttons[i] && buttons[i]->on_button(mx, my)) {
00340         pushed = buttons[i];
00341         break;
00342       }
00343 
00344   if (pushed)     // On a button?
00345   {
00346     pushed->push();
00347     return;
00348   }
00349 }
00350 
00351 void GameplayOptions_gump::mouse_up(int mx, int my)
00352 {
00353   if (pushed)     // Pushing a button?
00354   {
00355     pushed->unpush();
00356     if (pushed->on_button(mx, my))
00357       ((Gump_button*)pushed)->activate();
00358     pushed = 0;
00359   }
00360 }

Generated on Mon Jul 9 14:42:46 2007 for ExultEngine by  doxygen 1.5.1