Debian Codegraph @

src/widget/Slider.cpp

raw
1 /*
2 * Copyright (C) 2004 Ivo Danihelka ([email protected])
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 */
9 #include "Slider.h"
10
11 #include "OptionAgent.h"
12 #include "MouseStroke.h"
13 #include "SurfaceTool.h"
14 #include "minmax.h"
15
16 //-----------------------------------------------------------------
17 /**
18 * Create slider for this param and optinaly specify min and max value.
19 */
20 Slider::Slider(const std::string &param, int minValue, int maxValue)
21 : m_param(param)
22 {
23 m_min = minValue;
24 m_max = maxValue;
25 }
26 //-----------------------------------------------------------------
27 /**
28 * Scale value to slider rate.
29 * @return position on slider
30 */
31 int
32 Slider::value2slide(int value)
33 {
34 int slide = value - m_min;
35 slide = max(slide, m_min);
36 slide = min(slide, m_max);
37 return slide * PIXELS_PER_VALUE;
38 }
39 //-----------------------------------------------------------------
40 /**
41 * Scale slider rate to param value.
42 * @return integer value
43 */
44 int
45 Slider::slide2value(int slide)
46 {
47 int value = slide + m_min;
48 double fraction = static_cast<double>(value) / PIXELS_PER_VALUE;
49 return static_cast<int>(fraction + 0.5);
50 }
51 //-----------------------------------------------------------------
52 void
53 Slider::drawOn(SDL_Surface *screen)
54 {
55 int value = OptionAgent::agent()->getAsInt(m_param);
56 SDL_Color gray = {0x00, 0x00, 0x00, 129};
57 Uint32 green = SDL_MapRGB(screen->format, 0x00, 0xff, 0x00);
58
59 SDL_Rect rect;
60 rect.x = m_shift.getX();
61 rect.y = m_shift.getY();
62 rect.w = getW();
63 rect.h = getH();
64 SurfaceTool::alphaFill(screen, &rect, gray);
65
66 rect.x = m_shift.getX();
67 rect.y = m_shift.getY();
68 rect.w = value2slide(value);
69 rect.h = getH();
70 SDL_FillRect(screen, &rect, green);
71 }
72 //-----------------------------------------------------------------
73 void
74 Slider::own_mouseButton(const MouseStroke &stroke)
75 {
76 if (stroke.isLeft()) {
77 //TODO: play click
78 V2 inside = stroke.getLoc().minus(m_shift);
79 int value = slide2value(inside.getX());
80 OptionAgent::agent()->setPersistent(m_param, value);
81 }
82 }
83