00001 
00002 
00003 
00004 
00005 
00006 
00007 
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 
00019 
00020 Slider::Slider(const std::string ¶m, int minValue, int maxValue)
00021     : m_param(param)
00022 {
00023     m_min = minValue;
00024     m_max = maxValue;
00025 }
00026 
00027 
00028 
00029 
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 
00042 
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         
00078         V2 inside = stroke.getLoc().minus(m_shift);
00079         int value = slide2value(inside.getX());
00080         OptionAgent::agent()->setPersistent(m_param, value);
00081     }
00082 }
00083