Slider_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 #include "SDL_keyboard.h"
00025 #include "game.h"
00026 #include "gamewin.h"
00027 #include "Gump_button.h"
00028 #include "misc_buttons.h"
00029 #include "Slider_gump.h"
00030 #include "Gump_manager.h"
00031 
00032 
00033 using std::cout;
00034 using std::endl;
00035 
00036 /*
00037  *  Statics:
00038  */
00039 
00040 short Slider_gump::leftbtnx = 31;
00041 short Slider_gump::rightbtnx = 103;
00042 short Slider_gump::btny = 14;
00043 short Slider_gump::diamondy = 6;
00044 short Slider_gump::xmin = 35; // Min., max. positions of diamond.
00045 short Slider_gump::xmax = 93;
00046 
00047 /*
00048  *  One of the two arrow button on the slider:
00049  */
00050 class Slider_button : public Gump_button
00051 {
00052 public:
00053   Slider_button(Gump *par, int px, int py, int shapenum)
00054     : Gump_button(par, shapenum, px, py)
00055     {  }
00056           // What to do when 'clicked':
00057   virtual void activate();
00058 };
00059 
00060 /*
00061  *  Handle click on a slider's arrow.
00062  */
00063 
00064 void Slider_button::activate
00065   (
00066   )
00067 {
00068   ((Slider_gump *) parent)->clicked_arrow(this);
00069 }
00070 
00071 /*
00072  *  Set slider value.
00073  */
00074 
00075 void Slider_gump::set_val
00076   (
00077   int newval
00078   )
00079 {
00080   val = newval;
00081   static int xdist = xmax - xmin;
00082   if(max_val-min_val==0)
00083   {
00084     val=0;
00085     diamondx=xmin;
00086   }
00087   else
00088     diamondx = xmin + ((val - min_val)*xdist)/(max_val - min_val);
00089 }
00090 
00091 /*
00092  *  Create a slider.
00093  */
00094 
00095 Slider_gump::Slider_gump
00096   (
00097   int mival, int mxval,   // Value range.
00098   int step,     // Amt. to change by.
00099   int defval      // Default value.
00100   ) : Modal_gump(0, game->get_shape("gumps/slider")),
00101       min_val(mival), max_val(mxval), step_val(step),
00102       val(defval), dragging(0), prev_dragx(0)
00103 {
00104   diamond = ShapeID(game->get_shape("gumps/slider_diamond"), 0, SF_GUMPS_VGA);
00105   set_object_area(Rectangle(0, 0, 0, 0), 6, 30);
00106 
00107 #ifdef DEBUG
00108   cout << "Slider:  " << min_val << " to " << max_val << " by " << step << endl;
00109 #endif
00110   left_arrow = new Slider_button(this, leftbtnx, btny, 
00111                game->get_shape("gumps/slider_left"));
00112   right_arrow = new Slider_button(this, rightbtnx, btny, 
00113           game->get_shape("gumps/slider_right"));
00114           // Init. to middle value.
00115   if (defval < min_val)
00116     defval = min_val;
00117   else if (defval > max_val)
00118     defval = max_val;
00119   set_val(defval);
00120 }
00121 
00122 /*
00123  *  Delete slider.
00124  */
00125 
00126 Slider_gump::~Slider_gump
00127   (
00128   )
00129 {
00130   delete left_arrow;
00131   delete right_arrow;
00132 }
00133 
00134 /*
00135  *  An arrow on the slider was clicked.
00136  */
00137 
00138 void Slider_gump::clicked_arrow
00139   (
00140   Slider_button *arrow  // What was clicked.
00141   )
00142 {
00143   if (arrow == left_arrow)
00144     move_diamond(-step_val);
00145   else if (arrow == right_arrow)
00146     move_diamond(step_val);
00147 }
00148 
00149 void Slider_gump::move_diamond(int dir)
00150 {
00151   int newval = val;
00152   newval += dir;
00153   if (newval < min_val)
00154     newval = min_val;
00155   if (newval > max_val)
00156     newval = max_val;
00157 
00158   set_val(newval);
00159   paint();
00160   gwin->set_painted();
00161 }
00162 
00163 
00164 /*
00165  *  Paint on screen.
00166  */
00167 
00168 void Slider_gump::paint
00169   (
00170   )
00171 {
00172   const int textx = 128, texty = 7;
00173           // Paint the gump itself.
00174   paint_shape(x, y);
00175           // Paint red "checkmark".
00176   check_button->paint();
00177           // Paint buttons.
00178   left_arrow->paint();
00179   right_arrow->paint();
00180           // Paint slider diamond.
00181   diamond.paint_shape(x + diamondx, y + diamondy);
00182           // Print value.
00183     gumpman->paint_num(val, x + textx, y + texty);
00184   gwin->set_painted();
00185 }
00186 
00187 /*
00188  *  Handle mouse-down events.
00189  */
00190 
00191 void Slider_gump::mouse_down
00192   (
00193   int mx, int my      // Position in window.
00194   )
00195 {
00196   dragging = 0;
00197   Gump_button *btn = Gump::on_button(mx, my);
00198   if (btn)
00199     pushed = btn;
00200   else if (left_arrow->on_button(mx, my))
00201     pushed = left_arrow;
00202   else if (right_arrow->on_button(mx, my))
00203     pushed = right_arrow;
00204   else
00205     pushed = 0;
00206   if (pushed)
00207   {
00208     pushed->push();
00209     return;
00210   }
00211           // See if on diamond.
00212   Shape_frame *d_shape = diamond.get_shape();
00213   if (d_shape->has_point(mx - (x + diamondx), my - (y + diamondy)))
00214   {     // Start to drag it.
00215     dragging = 1;
00216     prev_dragx = mx;
00217   } else {
00218     if(my-get_y()<diamondy || my-get_y()>diamondy+d_shape->get_height())
00219       return;
00220     diamondx = mx-get_x();
00221     if(diamondx<xmin)
00222       diamondx = xmin;
00223     if(diamondx>xmax)
00224       diamondx = xmax;
00225     static int xdist = xmax - xmin;
00226     int delta = (diamondx - xmin)*(max_val - min_val)/xdist;
00227           // Round down to nearest step.
00228     delta -= delta%step_val;
00229     int newval = min_val + delta;
00230     if (newval != val)    // Set value.
00231       val = newval;
00232     paint();
00233   }
00234 }
00235 
00236 /*
00237  *  Handle mouse-up events.
00238  */
00239 
00240 void Slider_gump::mouse_up
00241   (
00242   int mx, int my      // Position in window.
00243   )
00244 {
00245   if (dragging)     // Done dragging?
00246   {
00247     set_val(val);   // Set diamond in correct pos.
00248     paint();
00249     gwin->set_painted();
00250     dragging = 0;
00251   }
00252   if (!pushed)
00253     return;
00254   pushed->unpush();
00255   if (pushed->on_button(mx, my))
00256     pushed->activate();
00257   pushed = 0;
00258 }
00259 
00260 /*
00261  *  Mouse was dragged with left button down.
00262  */
00263 
00264 void Slider_gump::mouse_drag
00265   (
00266   int mx, int my      // Where mouse is.
00267   )
00268 {
00269   if (!dragging)
00270     return;
00271   diamondx += mx - prev_dragx;
00272   prev_dragx = mx;
00273   if (diamondx < xmin)    // Stop at ends.
00274     diamondx = xmin;
00275   else if (diamondx > xmax)
00276     diamondx = xmax;
00277   static int xdist = xmax - xmin;
00278   int delta = (diamondx - xmin)*(max_val - min_val)/xdist;
00279           // Round down to nearest step.
00280   delta -= delta%step_val;
00281   int newval = min_val + delta;
00282   if (newval != val)    // Set value.
00283     val = newval;
00284   paint();
00285 }
00286 
00287 /*
00288  *  Handle ASCII character typed.
00289  */
00290 
00291 void Slider_gump::key_down(int chr)
00292 {
00293   switch(chr) {
00294   case SDLK_RETURN:
00295     done = 1;
00296     break;
00297   case SDLK_LEFT:
00298     clicked_arrow(left_arrow);
00299     break;
00300   case SDLK_RIGHT:
00301     clicked_arrow(right_arrow);
00302     break;
00303   } 
00304 }
00305 
00306 void Slider_gump::mousewheel_up()
00307 {
00308   SDLMod mod = SDL_GetModState();
00309   if (mod & KMOD_ALT)
00310     move_diamond(-10*step_val);
00311   else
00312     move_diamond(-step_val);
00313 }
00314 
00315 void Slider_gump::mousewheel_down()
00316 {
00317   SDLMod mod = SDL_GetModState();
00318   if (mod & KMOD_ALT)
00319     move_diamond(10*step_val);
00320   else
00321     move_diamond(step_val);
00322 }

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