Main Page | Class Hierarchy | Alphabetical List | Data Structures | Directories | File List | Data Fields | Globals

Slider.cpp

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2004 Ivo Danihelka (ivo@danihelka.net)
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 #include "Slider.h"
00010 
00011 #include "OptionAgent.h"
00012 #include "MouseStroke.h"
00013 #include "SurfaceTool.h"
00014 #include "minmax.h"
00015 
00016 //-----------------------------------------------------------------
00017 /**
00018  * Create slider for this param and optinaly specify min and max value.
00019  */
00020 Slider::Slider(const std::string &param, int minValue, int maxValue)
00021     : m_param(param)
00022 {
00023     m_min = minValue;
00024     m_max = maxValue;
00025 }
00026 //-----------------------------------------------------------------
00027 /**
00028  * Scale value to slider rate.
00029  * @return position on slider
00030  */
00031 int
00032 Slider::value2slide(int value)
00033 {
00034     int slide = value - m_min;
00035     slide = max(slide, m_min);
00036     slide = min(slide, m_max);
00037     return slide * PIXELS_PER_VALUE;
00038 }
00039 //-----------------------------------------------------------------
00040 /**
00041  * Scale slider rate to param value.
00042  * @return integer value
00043  */
00044 int
00045 Slider::slide2value(int slide)
00046 {
00047     int value = slide + m_min;
00048     double fraction = static_cast<double>(value) / PIXELS_PER_VALUE;
00049     return static_cast<int>(fraction + 0.5);
00050 }
00051 //-----------------------------------------------------------------
00052 void
00053 Slider::drawOn(SDL_Surface *screen)
00054 {
00055     int value = OptionAgent::agent()->getAsInt(m_param);
00056     SDL_Color gray = {0x00, 0x00, 0x00, 128};
00057     Uint32 green = SDL_MapRGB(screen->format, 0x00, 0xff, 0x00);
00058 
00059     SDL_Rect rect;
00060     rect.x = m_shift.getX();
00061     rect.y = m_shift.getY();
00062     rect.w = getW();
00063     rect.h = getH();
00064     SurfaceTool::alphaFill(screen, &rect, gray);
00065 
00066     rect.x = m_shift.getX();
00067     rect.y = m_shift.getY();
00068     rect.w = value2slide(value);
00069     rect.h = getH();
00070     SDL_FillRect(screen, &rect, green);
00071 }
00072 //-----------------------------------------------------------------
00073 void
00074 Slider::own_mouseButton(const MouseStroke &stroke)
00075 {
00076     if (stroke.isLeft()) {
00077         //TODO: play click
00078         V2 inside = stroke.getLoc().minus(m_shift);
00079         int value = slide2value(inside.getX());
00080         OptionAgent::agent()->setPersistent(m_param, value);
00081     }
00082 }
00083 

Generated on Wed Jun 1 09:54:31 2005 for Fish Fillets - Next Generation by  doxygen 1.4.2