AudioOptions_gump.cc

Go to the documentation of this file.
00001 /*
00002  *  Copyright (C) 2003-2004  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 "SDL_events.h"
00024 
00025 #include <iostream>
00026 
00027 #include "Audio.h"
00028 #include "AudioOptions_gump.h"
00029 #include "Configuration.h"
00030 #include "Gump_button.h"
00031 #include "Gump_ToggleButton.h"
00032 #include "exult.h"
00033 #include "exult_flx.h"
00034 #include "gamewin.h"
00035 #include "mouse.h"
00036 #include "xmidi.h"
00037 #include "Enabled_button.h"
00038 
00039 using std::cerr;
00040 using std::endl;
00041 
00042 static const int rowy[] = { 5,  
00043           19, 29, 41, 53, 65, 77,  
00044           89, 101,  
00045           115, 125,  
00046           135, 156 };
00047 static const int colx[] = { 35, 55, 130 };
00048 
00049 static const char* oktext = "OK";
00050 static const char* canceltext = "CANCEL";
00051 
00052 class AudioOptions_button : public Text_button {
00053 public:
00054   AudioOptions_button(Gump *par, const std::string &text, int px, int py)
00055     : Text_button(par, text, px, py, 59, 11)
00056     {  }
00057           // What to do when 'clicked':
00058   virtual void activate();
00059 };
00060 
00061 void AudioOptions_button::activate()
00062 {
00063   if (text == canceltext) {
00064     ((AudioOptions_gump*)parent)->cancel();
00065   } else if (text == oktext) {
00066     ((AudioOptions_gump*)parent)->close();
00067   }
00068 }
00069 
00070 class AudioTextToggle : public Gump_ToggleTextButton {
00071 public:
00072   AudioTextToggle(Gump* par, std::string *s, int px, int py, int width,
00073              int selectionnum, int numsel)
00074     : Gump_ToggleTextButton(par, s, selectionnum, numsel, px, py, width)
00075   { }
00076 
00077   friend class AudioOptions_gump;
00078   virtual void toggle(int state) { 
00079     ((AudioOptions_gump*)parent)->toggle((Gump_button*)this, state);
00080   }
00081 };
00082 
00083 
00084 class AudioEnabledToggle : public Enabled_button {
00085 public:
00086   AudioEnabledToggle(Gump* par, int px, int py, int selectionnum)
00087     : Enabled_button(par, selectionnum, px, py, 59)
00088   { }
00089 
00090   friend class AudioOptions_gump;
00091   virtual void toggle(int state) {
00092     ((AudioOptions_gump*)parent)->toggle((Gump_button*)this, state);
00093   }
00094 };  
00095 
00096 void AudioOptions_gump::close()
00097 {
00098   save_settings();
00099   done = 1;
00100 }
00101 
00102 void AudioOptions_gump::cancel()
00103 {
00104   done = 1;
00105 }
00106 
00107 void AudioOptions_gump::toggle(Gump_button* btn, int state)
00108 {
00109   if (btn == buttons[0]) {    // audio on/off
00110     audio_enabled = state;
00111     rebuild_buttons();
00112     paint();
00113   } else if (btn == buttons[1]) { // midi on/off 
00114     midi_enabled = state;
00115     rebuild_midi_buttons();
00116     paint();
00117   } else if (btn == buttons[2]) { // midi driver
00118     midi_driver = state;
00119     rebuild_mididriveroption_buttons();
00120     paint();
00121   } else if (btn == buttons[3]) { // midi conversion
00122     midi_conversion = state;
00123   } else if (btn == buttons[4]) { // midi reverb/chorus
00124     midi_reverb_chorus = state;
00125   } else if (btn == buttons[5]) { // midi looping
00126     midi_looping = state;
00127   } else if (btn == buttons[6]) { // sfx on/off
00128     sfx_enabled = state;
00129     rebuild_sfx_buttons();
00130     paint();
00131 #ifdef ENABLE_MIDISFX
00132   } else if (btn == buttons[7]) { // sfx conversion
00133     if (state == 1) {
00134       sfx_conversion = XMIDI_CONVERT_GS127_TO_GS;
00135     } else {
00136       sfx_conversion = XMIDI_CONVERT_NOCONVERSION;
00137     }
00138 #endif
00139   } else if (btn == buttons[8]) { // speech on/off
00140     speech_enabled = state;
00141   }
00142 }
00143 
00144 void AudioOptions_gump::rebuild_buttons()
00145 {
00146   for (unsigned int i = 1; i < 9; ++i) {
00147     delete buttons[i];
00148     buttons[i] = 0;
00149   }
00150 
00151   if (!audio_enabled) return;
00152 
00153   // midi on/off
00154   buttons[1] = new AudioEnabledToggle(this, colx[2], rowy[2], midi_enabled);
00155   if (midi_enabled)
00156     rebuild_midi_buttons();
00157   
00158   // sfx on/off
00159   buttons[6] = new AudioEnabledToggle(this, colx[2], rowy[8], sfx_enabled);
00160   if (sfx_enabled)
00161     rebuild_sfx_buttons();
00162   
00163   // speech on/off
00164   buttons[8] = new AudioEnabledToggle(this,colx[2],rowy[11], speech_enabled);
00165 }
00166 
00167 void AudioOptions_gump::rebuild_midi_buttons()
00168 {
00169   for (unsigned int i = 2; i < 6; ++i) {
00170     delete buttons[i];
00171     buttons[i] = 0;
00172   }
00173 
00174   if (!midi_enabled) return;
00175 
00176   std::string* midi_drivertext = new std::string[NUM_MIDI_DRIVER_TYPES];
00177   midi_drivertext[MIDI_DRIVER_NORMAL] = std::string("Normal");
00178   midi_drivertext[MIDI_DRIVER_OGG] = std::string("Digital");
00179 #ifdef USE_FMOPL_MIDI
00180   midi_drivertext[MIDI_DRIVER_FMSYNTH] = std::string("FMSynth");
00181 #endif
00182 #ifdef USE_MT32EMU_MIDI
00183   midi_drivertext[MIDI_DRIVER_MT32EMU] = std::string("MT32Emu");
00184 #endif
00185 
00186 
00187   // midi driver
00188   buttons[2] = new AudioTextToggle(this, midi_drivertext, 
00189                    colx[2], rowy[3], 59, midi_driver, NUM_MIDI_DRIVER_TYPES);
00190 
00191   rebuild_mididriveroption_buttons();
00192 
00193   // looping on/off
00194   buttons[5] = new AudioEnabledToggle(this, colx[2], rowy[6], midi_looping);
00195 
00196 }
00197 
00198 
00199 void AudioOptions_gump::rebuild_sfx_buttons()
00200 {
00201   delete buttons[7];
00202   buttons[7] = 0;
00203 
00204   if (!sfx_enabled)
00205     return;
00206 
00207 #ifdef ENABLE_MIDISFX
00208   std::string* sfx_conversiontext = new std::string[2];
00209   sfx_conversiontext[0] = "None";
00210   sfx_conversiontext[1] = "GS";
00211 
00212   // sfx conversion
00213   buttons[7] = new AudioTextToggle(this, sfx_conversiontext, colx[2], rowy[9],
00214                    59, sfx_conversion/4,2);
00215 #endif
00216 }
00217 
00218 void AudioOptions_gump::rebuild_mididriveroption_buttons()
00219 {
00220   delete buttons[3];
00221   buttons[3] = 0;
00222   delete buttons[4];
00223   buttons[4] = 0;
00224 
00225   std::string* midi_conversiontext = new std::string[4];
00226   midi_conversiontext[0] = std::string("None");
00227   midi_conversiontext[1] = std::string("GM");
00228   midi_conversiontext[2] = std::string("GS");
00229   midi_conversiontext[3] = std::string("GS127");
00230 
00231   std::string* midi_reverbchorustext = new std::string[4];
00232   midi_reverbchorustext[0] = std::string("Disabled");
00233   midi_reverbchorustext[1] = std::string("Reverb");
00234   midi_reverbchorustext[2] = std::string("Chorus");
00235   midi_reverbchorustext[3] = std::string("Both");
00236 
00237   if (midi_driver == MIDI_DRIVER_NORMAL) {
00238     // midi conversion
00239     buttons[3] = new AudioTextToggle(this, midi_conversiontext, 
00240                      colx[2], rowy[4], 59,
00241                      midi_conversion, 4);
00242     // reverb/chorus combo
00243     buttons[4] = new AudioTextToggle(this, midi_reverbchorustext, 
00244                      colx[2], rowy[5], 59,
00245                      midi_reverb_chorus, 4);
00246   }
00247 }
00248 
00249 void AudioOptions_gump::load_settings()
00250 {
00251   std::string s;
00252   audio_enabled = (Audio::get_ptr()->is_audio_enabled() ? 1 : 0);
00253   midi_enabled = (Audio::get_ptr()->is_music_enabled() ? 1 : 0);
00254   sfx_enabled = (Audio::get_ptr()->are_effects_enabled() ? 1 : 0);
00255   speech_enabled = (Audio::get_ptr()->is_speech_enabled() ? 1 : 0);
00256   midi_looping = (Audio::get_ptr()->is_music_looping_allowed() ? 1 : 0);
00257 
00258   if (Audio::get_ptr()->get_midi()) {
00259     midi_conversion = Audio::get_ptr()->get_midi()->get_music_conversion();
00260     midi_driver = Audio::get_ptr()->get_midi()->get_output_driver_type();
00261 #ifdef ENABLE_MIDISFX
00262     sfx_conversion =Audio::get_ptr()->get_midi()->get_effects_conversion();
00263 #endif
00264   } else {
00265     // String for default value for driver type
00266     std::string driver_default = "normal";
00267 
00268     config->value("config/audio/midi/convert",s,"gm");
00269     if (s == "gs")
00270       midi_conversion = XMIDI_CONVERT_MT32_TO_GS;
00271     else if (s == "none")
00272       midi_conversion = XMIDI_CONVERT_NOCONVERSION;
00273     else if (s == "gs127")
00274       midi_conversion = XMIDI_CONVERT_MT32_TO_GS127;
00275     else if (s == "gs127drum")
00276       midi_conversion = XMIDI_CONVERT_MT32_TO_GS;
00277     else
00278     {
00279       midi_conversion = XMIDI_CONVERT_MT32_TO_GM;
00280       config->set("config/audio/midi/convert","gm",true);
00281 
00282       driver_default = "s";
00283     }
00284 
00285     config->value("config/audio/midi/driver",s,driver_default.c_str());
00286     if (s == "digital") 
00287     {
00288       midi_driver = MIDI_DRIVER_OGG;
00289       config->set("config/audio/effects/driver","digital",true);
00290     }
00291 #ifdef USE_FMOPL_MIDI
00292     else if (s == "fmsynth")
00293     {
00294       midi_driver = MIDI_DRIVER_FMSYNTH;
00295       config->set("config/audio/effects/driver","fmsynth",true);
00296     }
00297 #endif
00298 #ifdef USE_MT32EMU_MIDI
00299     else if (s == "mt32emu")
00300       midi_driver = MIDI_DRIVER_MT32EMU;
00301 #endif
00302     else
00303       midi_driver = MIDI_DRIVER_NORMAL;
00304 
00305 
00306 #ifdef ENABLE_MIDISFX
00307     config->value("config/audio/effects/convert",s,"gs");
00308     if (s == "none")
00309       sfx_conversion = XMIDI_CONVERT_NOCONVERSION;
00310     else if (s == "gs127")
00311       sfx_conversion = XMIDI_CONVERT_NOCONVERSION;
00312     else
00313       sfx_conversion = XMIDI_CONVERT_GS127_TO_GS;
00314 #endif
00315   }
00316 
00317   config->value("config/audio/midi/reverb/enabled",s,"no");
00318   midi_reverb_chorus = (s == "yes" ? 1 : 0);
00319 
00320   config->value("config/audio/midi/chorus/enabled",s,"no");
00321   midi_reverb_chorus |= (s == "yes" ? 2 : 0);
00322   
00323   
00324 }
00325 
00326 AudioOptions_gump::AudioOptions_gump() : Modal_gump(0, EXULT_FLX_AUDIOOPTIONS_SHP, SF_EXULT_FLX)
00327 {
00328   set_object_area(Rectangle(0,0,0,0), 8, 172);//++++++ ???
00329 
00330   for (int i=0; i<12; i++) buttons[i] = 0;
00331 
00332   load_settings();
00333 
00334   rebuild_buttons();
00335 
00336 
00337   // audio on/off
00338     buttons[0] = new AudioEnabledToggle(this, colx[2], rowy[0], audio_enabled);
00339   // Ok
00340   buttons[9] = new AudioOptions_button(this, oktext, colx[0], rowy[12]);
00341   // Cancel
00342   buttons[10] = new AudioOptions_button(this, canceltext, colx[2], rowy[12]);
00343 }
00344 
00345 AudioOptions_gump::~AudioOptions_gump()
00346 {
00347   for (int i=0; i<12; i++)
00348     if (buttons[i]) delete buttons[i];
00349 }
00350 
00351 void AudioOptions_gump::save_settings()
00352 {
00353   Audio::get_ptr()->set_audio_enabled(audio_enabled == 1);
00354   Audio::get_ptr()->set_music_enabled(midi_enabled == 1);
00355   if (!midi_enabled)    // Stop what's playing.
00356     Audio::get_ptr()->stop_music();
00357   Audio::get_ptr()->set_effects_enabled(sfx_enabled == 1);
00358   if (!sfx_enabled)   // Stop what's playing.
00359     Audio::get_ptr()->stop_sound_effects();
00360   Audio::get_ptr()->set_speech_enabled(speech_enabled == 1);
00361   Audio::get_ptr()->set_allow_music_looping(midi_looping == 1);
00362 
00363   config->set("config/audio/enabled", audio_enabled ? "yes" : "no", true);
00364   config->set("config/audio/midi/enabled",midi_enabled ? "yes" : "no", true);
00365   config->set("config/audio/effects/enabled",sfx_enabled?"yes":"no", true);
00366   config->set("config/audio/speech/enabled",speech_enabled?"yes":"no", true);
00367 
00368   config->set("config/audio/midi/chorus/enabled", (midi_reverb_chorus&2) ? "yes" : "no", true);
00369   config->set("config/audio/midi/reverb/enabled", (midi_reverb_chorus&1)? "yes" : "no", true);
00370   config->set("config/audio/midi/looping", midi_looping ? "yes" : "no", true);
00371 
00372   if (Audio::get_ptr()->get_midi()) {
00373     Audio::get_ptr()->get_midi()->set_music_conversion(midi_conversion);
00374     Audio::get_ptr()->get_midi()->set_effects_conversion(sfx_conversion);
00375     Audio::get_ptr()->get_midi()->set_output_driver_type(midi_driver);
00376   } else {
00377     switch(midi_conversion) {
00378     case XMIDI_CONVERT_MT32_TO_GS:
00379       config->set("config/audio/midi/convert","gs",true);
00380       break;
00381     case XMIDI_CONVERT_NOCONVERSION:
00382       config->set("config/audio/midi/convert","none",true);
00383       break;
00384     case XMIDI_CONVERT_MT32_TO_GS127:
00385       config->set("config/audio/midi/convert","gs127",true);
00386       break;
00387     default:
00388       config->set("config/audio/midi/convert","gm",true);
00389       break;
00390     }
00391 
00392     switch(midi_driver) {
00393     case MIDI_DRIVER_OGG:
00394       config->set("config/audio/midi/driver","digital",true);
00395       break;
00396 #ifdef USE_FMOPL_MIDI
00397     case MIDI_DRIVER_FMSYNTH:
00398       config->set("config/audio/midi/driver","fmsynth",true);
00399       break;
00400 #endif
00401 #ifdef USE_MT32EMU_MIDI
00402     case MIDI_DRIVER_MT32EMU:
00403       config->set("config/audio/midi/driver","mt32emu",true);
00404       break;
00405 #endif
00406     default:
00407       config->set("config/audio/midi/driver","normal",true);
00408       break;
00409     }
00410 
00411 #ifdef ENABLE_MIDISFX
00412     switch(sfx_conversion) {
00413     case XMIDI_CONVERT_NOCONVERSION:
00414       config->set("config/audio/effects/convert","none",true);
00415       break;
00416     default:
00417       config->set("config/audio/effects/convert","gs",true);
00418       break;
00419     }
00420 #endif
00421   }
00422 }
00423 
00424 void AudioOptions_gump::paint()
00425 {
00426   Gump::paint();
00427   for (int i=0; i<11; i++)
00428     if (buttons[i])
00429       buttons[i]->paint();
00430 
00431   sman->paint_text(2, "Audio:", x + colx[0], y + rowy[0] + 1);
00432   if (audio_enabled) {
00433     sman->paint_text(2, "Music options:", x + colx[0], y + rowy[1] + 1);
00434     sman->paint_text(2, "music", x + colx[1], y + rowy[2] + 1);
00435     if (midi_enabled) {
00436       sman->paint_text(2, "driver", x + colx[1], y + rowy[3] + 1);
00437       if (midi_driver == MIDI_DRIVER_NORMAL) {
00438         sman->paint_text(2, "conversion", x+colx[1], y+rowy[4] + 1);
00439         sman->paint_text(2, "effects", x + colx[1], y + rowy[5] + 1);
00440       }
00441       sman->paint_text(2, "looping", x + colx[1], y + rowy[6] + 1);
00442     }
00443     sman->paint_text(2, "SFX options:", x + colx[0], y + rowy[7] + 1);
00444     sman->paint_text(2, "SFX", x + colx[1], y + rowy[8] + 1);
00445 #ifdef ENABLE_MIDISFX
00446     if (sfx_enabled) {
00447       sman->paint_text(2, "conversion", x + colx[1], y + rowy[9] + 1);
00448     }
00449 #endif
00450     sman->paint_text(2, "Speech options:", x + colx[0], y + rowy[10] + 1);
00451     sman->paint_text(2, "speech", x + colx[1], y + rowy[11] + 1);
00452   }
00453   gwin->set_painted();
00454 }
00455 
00456 void AudioOptions_gump::mouse_down(int mx, int my)
00457 {
00458   pushed = Gump::on_button(mx, my);
00459           // First try checkmark.
00460   // Try buttons at bottom.
00461   if (!pushed)
00462     for (int i=0; i<11; i++)
00463       if (buttons[i] && buttons[i]->on_button(mx, my)) {
00464         pushed = buttons[i];
00465         break;
00466       }
00467 
00468   if (pushed)     // On a button?
00469   {
00470     pushed->push();
00471     return;
00472   }
00473 }
00474 
00475 void AudioOptions_gump::mouse_up(int mx, int my)
00476 {
00477   if (pushed)     // Pushing a button?
00478   {
00479     pushed->unpush();
00480     if (pushed->on_button(mx, my))
00481       ((Gump_button*)pushed)->activate();
00482     pushed = 0;
00483   }
00484 }

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