CombatOptions_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 "CombatOptions_gump.h"
00033 #include "exult.h"
00034 #include "exult_flx.h"
00035 #include "gamewin.h"
00036 #include "combat_opts.h"
00037 #include "Text_button.h"
00038 #include "Enabled_button.h"
00039 
00040 using std::cerr;
00041 using std::endl;
00042 using std::string;
00043 
00044 static const int rowy[] = { 17, 43, 69, 130 };
00045 static const int colx[] = { 35, 50, 120, 170, 192 };
00046 
00047 static const char* oktext = "OK";
00048 static const char* canceltext = "CANCEL";
00049 
00050 static int framerates[] = { 2, 4, 6, 8, 10, -1 };
00051  // -1 is placeholder for custom framerate
00052 static int num_default_rates = sizeof(framerates)/sizeof(framerates[0]) - 1;
00053 
00054 
00055 class CombatOptions_button : public Text_button {
00056 public:
00057   CombatOptions_button(Gump *par, string text, int px, int py)
00058     : Text_button(par, text, px, py, 59, 11)
00059     { }
00060           // What to do when 'clicked':
00061   virtual void activate();
00062 };
00063 
00064 void CombatOptions_button::activate()
00065 {
00066   if (text == canceltext) {
00067     ((CombatOptions_gump*)parent)->cancel();
00068   } else if (text == oktext) {
00069     ((CombatOptions_gump*)parent)->close();
00070   }
00071 }
00072 
00073 class CombatTextToggle : public Gump_ToggleTextButton {
00074 public:
00075   CombatTextToggle(Gump* par, std::string *s, int px, int py, int width,
00076              int selectionnum, int numsel)
00077   : Gump_ToggleTextButton(par, s, selectionnum, numsel, px, py, width)
00078   { }
00079 
00080   friend class CombatOptions_gump;
00081   virtual void toggle(int state) { 
00082     ((CombatOptions_gump*)parent)->toggle((Gump_button*)this, 
00083                   state);
00084   }
00085 };
00086 
00087 class CombatEnabledToggle : public Enabled_button {
00088 public:
00089   CombatEnabledToggle(Gump* par, int px, int py, int width, 
00090               int selectionnum)
00091     : Enabled_button(par, selectionnum, px, py, width)
00092   { }
00093 
00094   friend class CombatOptions_gump;
00095   virtual void toggle(int state) {
00096     ((CombatOptions_gump*)parent)->toggle((Gump_button*)this, 
00097                   state);
00098   }
00099 };  
00100 
00101 void CombatOptions_gump::close()
00102 {
00103   save_settings();
00104   done = 1;
00105 }
00106 
00107 void CombatOptions_gump::cancel()
00108 {
00109   done = 1;
00110 }
00111 
00112 void CombatOptions_gump::toggle(Gump_button* btn, int state)
00113 {
00114   if (btn == buttons[0])
00115     difficulty = state;
00116   else if (btn == buttons[1])
00117     show_hits = state;
00118   else if (btn == buttons[2])
00119     mode = state;
00120 }
00121 
00122 void CombatOptions_gump::build_buttons()
00123 {
00124   std::string *diffs = new std::string[7];
00125   diffs[0] = "Easiest (-3)";
00126   diffs[1] = "Easier (-2)";
00127   diffs[2] = "Easier (-1)";
00128   diffs[3] = "Normal";
00129   diffs[4] = "Harder (+1)";
00130   diffs[5] = "Harder (+2)";
00131   diffs[6] = "Hardest (+3)";
00132   buttons[0] = new CombatTextToggle (this, diffs, colx[3], rowy[0], 
00133                      85, difficulty, 7);
00134   buttons[1] = new CombatEnabledToggle(this, colx[3], rowy[1],
00135                      85, show_hits);
00136   std::string *modes = new std::string[2];
00137   modes[0] = "Original";
00138   modes[1] = "Space pauses";
00139   buttons[2] = new CombatTextToggle (this, modes, colx[3], rowy[2],
00140                      85, mode, 2);
00141 }
00142 
00143 void CombatOptions_gump::load_settings()
00144 {
00145   difficulty = Combat::difficulty;
00146   if (difficulty < -3)
00147     difficulty = -3;
00148   else if (difficulty > 3)
00149     difficulty = 3;
00150   difficulty += 3;    // Scale to choices (0-6).
00151   show_hits = Combat::show_hits ? 1 : 0;
00152   mode = (int) Combat::mode;
00153   if (mode < 0 || mode > 1)
00154     mode = 0;
00155 }
00156 
00157 CombatOptions_gump::CombatOptions_gump() 
00158   : Modal_gump(0, EXULT_FLX_GAMEPLAYOPTIONS_SHP, SF_EXULT_FLX)
00159 {
00160   set_object_area(Rectangle(0, 0, 0, 0), 8, 162);//++++++ ???
00161   const int nbuttons = sizeof(buttons)/sizeof(buttons[0]);
00162   for (int i = 0; i < nbuttons; i++)
00163     buttons[i] = 0;
00164 
00165   load_settings();
00166   
00167   build_buttons();
00168 
00169   // Ok
00170   buttons[nbuttons - 2] = 
00171     new CombatOptions_button(this, oktext, colx[0], rowy[3]);
00172   // Cancel
00173   buttons[nbuttons - 1] = 
00174     new CombatOptions_button(this, canceltext, colx[4], rowy[3]);
00175 }
00176 
00177 CombatOptions_gump::~CombatOptions_gump()
00178 {
00179   for (int i = 0; i < sizeof(buttons)/sizeof(buttons[0]); i++)
00180     if (buttons[i])
00181       delete buttons[i];
00182 }
00183 
00184 void CombatOptions_gump::save_settings()
00185 {
00186   Combat::difficulty = difficulty - 3;
00187   config->set("config/gameplay/combat/difficulty",
00188             Combat::difficulty, true);
00189   Combat::show_hits = (show_hits != 0);
00190   config->set("config/gameplay/combat/show_hits", 
00191           show_hits ? "yes" : "no", true);
00192   Combat::mode = (Combat::Mode) mode;
00193   std::string str = Combat::mode == Combat::keypause ? "keypause"
00194           : "original";
00195   config->set("config/gameplay/combat/mode", str, true);
00196 }
00197 
00198 void CombatOptions_gump::paint()
00199 {
00200   Gump::paint();
00201   for (int i = 0; i < sizeof(buttons)/sizeof(buttons[0]); i++)
00202     if (buttons[i])
00203       buttons[i]->paint();
00204 
00205   sman->paint_text(2, "Difficulty:", x + colx[0], y + rowy[0] + 1);
00206   sman->paint_text(2, "Show Hits:", x + colx[0], y + rowy[1] + 1);
00207   sman->paint_text(2, "Mode:", x + colx[0], y + rowy[2] + 1);
00208   gwin->set_painted();
00209 }
00210 
00211 void CombatOptions_gump::mouse_down(int mx, int my)
00212 {
00213   pushed = Gump::on_button(mx, my);
00214           // First try checkmark.
00215   // Try buttons at bottom.
00216   if (!pushed)
00217     for (int i = 0; i < sizeof(buttons)/sizeof(buttons[0]); i++)
00218       if (buttons[i] && buttons[i]->on_button(mx, my)) {
00219         pushed = buttons[i];
00220         break;
00221       }
00222 
00223   if (pushed)     // On a button?
00224   {
00225     pushed->push();
00226     return;
00227   }
00228 }
00229 
00230 void CombatOptions_gump::mouse_up(int mx, int my)
00231 {
00232   if (pushed)     // Pushing a button?
00233   {
00234     pushed->unpush();
00235     if (pushed->on_button(mx, my))
00236       ((Gump_button*)pushed)->activate();
00237     pushed = 0;
00238   }
00239 }

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