Yesno_gump.cc

Go to the documentation of this file.
00001 /*
00002 Copyright (C) 2000-2002 The Exult Team
00003 
00004 This program is free software; you can redistribute it and/or
00005 modify it under the terms of the GNU General Public License
00006 as published by the Free Software Foundation; either version 2
00007 of the License, or (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 "Yesno_gump.h"
00026 #include "gamewin.h"
00027 #include "mouse.h"
00028 #include "game.h"
00029 #include "Gump_button.h"
00030 #include "Gump_manager.h"
00031 
00032 /*
00033  *  Statics:
00034  */
00035 
00036 short Yesno_gump::yesx = 63;
00037 short Yesno_gump::yesnoy = 45;
00038 short Yesno_gump::nox = 84;
00039 
00040 
00041 /*
00042  *  A 'yes' or 'no' button.
00043  */
00044 
00045 class Yesno_button : public Gump_button
00046 {
00047   int isyes;      // 1 for 'yes', 0 for 'no'.
00048 public:
00049   Yesno_button(Gump *par, int px, int py, int yes)
00050     : Gump_button(par, yes ? 
00051       game->get_shape("gumps/yesbtn") 
00052       : game->get_shape("gumps/nobtn"), px, py),
00053       isyes(yes)
00054     {  }
00055           // What to do when 'clicked':
00056   virtual void activate();
00057 };
00058 
00059 
00060 /*
00061  *  Handle 'yes' or 'no' button.
00062  */
00063 
00064 void Yesno_button::activate
00065   (
00066   )
00067 {
00068   ((Yesno_gump *) parent)->set_answer(isyes);
00069 }
00070 
00071 
00072 /*
00073  *  Create a yes/no box.
00074  */
00075 
00076 Yesno_gump::Yesno_gump
00077   (
00078   const std::string &txt
00079   ) : Modal_gump(0, game->get_shape("gumps/yesnobox")), text(txt), answer(-1)
00080 {
00081   set_object_area(Rectangle(6, 6, 116, 30));
00082   yes_button = new Yesno_button(this, yesx, yesnoy, 1);
00083   no_button = new Yesno_button(this, nox, yesnoy, 0);
00084 }
00085 
00086 /*
00087  *  Done with yes/no box.
00088  */
00089 
00090 Yesno_gump::~Yesno_gump
00091   (
00092   )
00093 {
00094   delete yes_button;
00095   delete no_button;
00096 }
00097 
00098 /*
00099  *  Paint on screen.
00100  */
00101 
00102 void Yesno_gump::paint
00103   (
00104   )
00105 {
00106           // Paint the gump itself.
00107   paint_shape(x, y);
00108           // Paint buttons.
00109   yes_button->paint();
00110   no_button->paint();
00111           // Paint text.
00112   sman->paint_text_box(2, text.c_str(), x + object_area.x, 
00113       y + object_area.y, object_area.w, object_area.h, 2);
00114   gwin->set_painted();
00115 }
00116 
00117 /*
00118  *  Handle mouse-down events.
00119  */
00120 
00121 void Yesno_gump::mouse_down
00122   (
00123   int mx, int my      // Position in window.
00124   )
00125 {
00126           // Check buttons.
00127   if (yes_button->on_button(mx, my))
00128     pushed = yes_button;
00129   else if (no_button->on_button(mx, my))
00130     pushed = no_button;
00131   else
00132   {
00133     pushed = 0;
00134     return;
00135   }
00136   pushed->push();   // Show it.
00137 }
00138 
00139 /*
00140  *  Handle mouse-up events.
00141  */
00142 
00143 void Yesno_gump::mouse_up
00144   (
00145   int mx, int my      // Position in window.
00146   )
00147 {
00148   if (pushed)     // Pushing a button?
00149   {
00150     pushed->unpush();
00151     if (pushed->on_button(mx, my))
00152       pushed->activate();
00153     pushed = 0;
00154   }
00155 }
00156 
00157 /*
00158  *  Handle ASCII character typed.
00159  */
00160 
00161 void Yesno_gump::key_down(int chr)
00162 {
00163   if (chr == 'y' || chr == 'Y' || chr == SDLK_RETURN)
00164     set_answer(1);
00165   else if (chr == 'n' || chr == 'N' || chr == SDLK_ESCAPE)
00166     set_answer(0);
00167 }
00168 
00169 /*
00170  *  Get an answer to a question.
00171  *
00172  *  Output: 1 if yes, 0 if no or ESC.
00173  */
00174 
00175 int Yesno_gump::ask
00176   (
00177   const char *txt     // What to ask.
00178   )
00179 {
00180   Yesno_gump *dlg = new Yesno_gump(txt);
00181   int answer;
00182   if (!gumpman->do_modal_gump(dlg, Mouse::hand))
00183     answer = 0;
00184   else
00185     answer = dlg->get_answer();
00186   delete dlg;
00187   return (answer);
00188 }

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