SliderGump.cpp

Go to the documentation of this file.
00001 /*
00002  *  Copyright (C) 2003-2005  The Pentagram 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 #include "pent_include.h"
00020 #include "SliderGump.h"
00021 
00022 #include "GameData.h"
00023 #include "ShapeFrame.h"
00024 #include "GumpShapeArchive.h"
00025 #include "Shape.h"
00026 #include "SlidingWidget.h"
00027 #include "Font.h"
00028 #include "RenderedText.h"
00029 #include "FontManager.h"
00030 #include "ButtonWidget.h"
00031 #include "UCProcess.h"
00032 #include "Kernel.h"
00033 #include "GUIApp.h"
00034 
00035 #include "IDataSource.h"
00036 #include "ODataSource.h"
00037 
00038 DEFINE_RUNTIME_CLASSTYPE_CODE(SliderGump,ModalGump);
00039 
00040 SliderGump::SliderGump() : ModalGump()
00041 {
00042         renderedtext = 0;
00043 }
00044 
00045 
00046 SliderGump::SliderGump(int x, int y, sint16 min_, sint16 max_,
00047                                            sint16 value_, sint16 delta_)
00048         : ModalGump(x, y, 5, 5), min(min_), max(max_), delta(delta_), value(value_)
00049 {
00050         usecodeNotifyPID = 0;
00051         renderedtext = 0;
00052 }
00053 
00054 SliderGump::~SliderGump()
00055 {
00056 
00057 }
00058 
00059 /*
00060   41:0 = slider gump
00061   42:0,1 = ok button
00062   43:0,1 = left button
00063   44:0,1 = right button
00064   45:0 = slider
00065 */
00066 
00067 static const int gumpshape = 41;
00068 static const int gumpframe = 0;
00069 static const int okshape = 42;
00070 static const int leftshape = 43;
00071 static const int rightshape = 44;
00072 static const int slidershape = 45;
00073 static const int sliderframe = 0;
00074 static const int slidery = 17;
00075 static const int sliderminx = 55;
00076 static const int slidermaxx = 130;
00077 static const int labelx = 161;
00078 static const int labely = 26;
00079 static const int labelfont = 0;
00080 
00081 static const int OK_INDEX = 1;
00082 static const int LEFT_INDEX = 2;
00083 static const int RIGHT_INDEX = 3;
00084 static const int SLIDER_INDEX = 4;
00085 
00086 int SliderGump::getSliderPos()
00087 {
00088         return sliderminx + (value-min)*(slidermaxx-sliderminx)/(max-min);
00089 }
00090 
00091 void SliderGump::setValueFromSlider(int sliderx)
00092 {
00093         int val = (sliderx-sliderminx)*(max-min)/(slidermaxx-sliderminx) + min;
00094         if (val < min) val = min;
00095         if (val > max) val = max;
00096         value = min + delta*(static_cast<sint16>(val/delta));
00097 }
00098 
00099 void SliderGump::setSliderPos()
00100 {
00101         Gump* slider = Gump::FindGump(SlidingWidget::ClassType);
00102         assert(slider);
00103         slider->Move(getSliderPos(), slidery);
00104 }
00105 
00106 void SliderGump::drawText(RenderSurface* surf)
00107 {
00108         if (!renderedtext || value != renderedvalue) {
00109                 Pentagram::Font *font;
00110                 font = FontManager::get_instance()->getGameFont(labelfont);
00111                 char buf[10]; // more than enough for a sint16
00112                 sprintf(buf, "%d", value);
00113 
00114                 unsigned int remaining;
00115                 delete renderedtext;
00116                 renderedtext = font->renderText(buf, remaining);
00117                 renderedvalue = value;
00118         }
00119 
00120         renderedtext->draw(surf, labelx, labely);
00121 }
00122 
00123 void SliderGump::PaintThis(RenderSurface* surf, sint32 lerp_factor, bool scaled)
00124 {
00125         Gump::PaintThis(surf, lerp_factor, scaled);
00126 
00127         drawText(surf);
00128 }
00129 
00130 
00131 void SliderGump::InitGump(Gump* newparent, bool take_focus)
00132 {
00133         ModalGump::InitGump(newparent, take_focus);
00134 
00135         shape = GameData::get_instance()->getGumps()->getShape(gumpshape);
00136         ShapeFrame* sf = shape->getFrame(0);
00137         assert(sf);
00138 
00139         dims.w = sf->width;
00140         dims.h = sf->height;
00141 
00142         Shape* childshape = GameData::get_instance()->
00143                 getGumps()->getShape(slidershape);
00144 
00145         // Create the SlidingWidget
00146         Gump *widget = new SlidingWidget(getSliderPos(),slidery,
00147                                                                          childshape,sliderframe);
00148         widget->SetIndex(SLIDER_INDEX);
00149         widget->InitGump(this);
00150 
00151         FrameID button_up(GameData::GUMPS, okshape, 0);
00152         FrameID button_down(GameData::GUMPS, okshape, 1);
00153 
00154         widget = new ButtonWidget(14, 17, button_up, button_down);
00155         widget->SetIndex(OK_INDEX);
00156         widget->InitGump(this);
00157 
00158         FrameID buttonleft_up(GameData::GUMPS, leftshape, 0);
00159         FrameID buttonleft_down(GameData::GUMPS, leftshape, 1);
00160 
00161         widget = new ButtonWidget(36, 17, buttonleft_up, buttonleft_down);
00162         widget->SetIndex(LEFT_INDEX);
00163         widget->InitGump(this);
00164 
00165 
00166         FrameID buttonright_up(GameData::GUMPS, rightshape, 0);
00167         FrameID buttonright_down(GameData::GUMPS, rightshape, 1);
00168 
00169         widget = new ButtonWidget(141, 17, buttonright_up, buttonright_down);
00170         widget->SetIndex(RIGHT_INDEX);
00171         widget->InitGump(this);
00172 }
00173 
00174 void SliderGump::ChildNotify(Gump *child, uint32 message)
00175 {
00176         if (message == ButtonWidget::BUTTON_CLICK) {
00177                 switch (child->GetIndex()) {
00178                 case OK_INDEX:
00179                         Close();
00180                         break;
00181                 case LEFT_INDEX:
00182                         value -= delta;
00183                         if (value < min) value = min;
00184                         setSliderPos();
00185                         break;
00186                 case RIGHT_INDEX:
00187                         value += delta;
00188                         if (value > max) value = max;
00189                         setSliderPos();
00190                         break;
00191                 }
00192         }
00193 }
00194 
00195 
00196 void SliderGump::Close(bool no_del)
00197 {
00198         process_result = value;
00199 
00200         if (usecodeNotifyPID) {
00201                 UCProcess* ucp = p_dynamic_cast<UCProcess*>(Kernel::get_instance()->getProcess(usecodeNotifyPID));
00202                 assert(ucp);
00203                 ucp->setReturnValue(value);
00204                 ucp->wakeUp(value);
00205         }
00206 
00207         ModalGump::Close(no_del);
00208 }
00209 
00210 bool SliderGump::StartDraggingChild(Gump* gump, int mx, int my)
00211 {
00212         if (gump->GetIndex() == SLIDER_INDEX) {
00213                 gump->ParentToGump(mx, my);
00214                 GUIApp::get_instance()->setDraggingOffset(mx, 0);
00215                 return true;
00216         }
00217 
00218         return false;
00219 }
00220 
00221 void SliderGump::DraggingChild(Gump* gump, int mx, int my)
00222 {
00223         if (gump->GetIndex() == SLIDER_INDEX) {
00224                 int dox, doy;
00225                 GUIApp::get_instance()->getDraggingOffset(dox, doy);
00226                 setValueFromSlider(mx-dox);
00227                 gump->Move(getSliderPos(), slidery);
00228         }
00229 }
00230 
00231 void SliderGump::StopDraggingChild(Gump* gump)
00232 {
00233 }
00234 
00235 bool SliderGump::OnKeyDown(int key, int mod)
00236 {
00237         switch(key)
00238         {
00239         case SDLK_LEFT:
00240                 value -= delta;
00241                 if (value < min) value = min;
00242                 setSliderPos();
00243                 break;
00244         case SDLK_RIGHT:
00245                 value += delta;
00246                 if (value > max) value = max;
00247                 setSliderPos();
00248                 break;
00249         case SDLK_RETURN:
00250                 Close();
00251                 break;
00252         default:
00253                 break;
00254         }
00255 
00256         return true;
00257 }
00258 
00259 void SliderGump::setUsecodeNotify(UCProcess* ucp)
00260 {
00261         assert(ucp);
00262         usecodeNotifyPID = ucp->getPid();
00263 }
00264 
00265 void SliderGump::saveData(ODataSource* ods)
00266 {
00267         CANT_HAPPEN_MSG("Trying to save ModalGump");
00268 }
00269 
00270 bool SliderGump::loadData(IDataSource* ids, uint32 version)
00271 {
00272         CANT_HAPPEN_MSG("Trying to load ModalGump");
00273 
00274         return false;
00275 }

Generated on Fri Jul 27 22:27:38 2007 for pentagram by  doxygen 1.4.7