MFMv2.0.10
Movable Feast Machine Simulator 2.0.10
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Slider.h
Go to the documentation of this file.
1 /* -*- mode:C++ -*-
2  Slider.h GUI Slider for tweaking numeric values
3  Copyright (C) 2014 The Regents of the University of New Mexico. All rights reserved.
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Lesser General Public
7  License as published by the Free Software Foundation; either
8  version 2.1 of the License, or (at your option) any later version.
9 
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this library; if not, write to the Free Software
17  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
18  USA
19 */
20 
27 #ifndef SLIDER_H
28 #define SLIDER_H
29 
30 #include "AssetManager.h"
31 #include "CharBufferByteSink.h"
32 #include "Drawing.h"
33 #include "Util.h"
34 #include "Parameter.h"
35 #include "ParameterController.h"
36 
37 namespace MFM
38 {
39  template <class CC>
40  class Slider : public ParameterController<CC>
41  {
42  private:
43  typedef typename CC::ATOM_TYPE T;
44 
45  bool m_dragging;
46  s32 m_dragStartX;
47  s32 m_preDragVal;
48 
49  public:
50 
51  enum
52  {
53  SLIDER_WIDTH = 150,
54  SLIDER_HEIGHT = 32,
55 
56  SLIDER_WIDTH_BIG = 300,
57  SLIDER_HEIGHT_BIG = 64,
58 
59  SLIDER_HALF_HEIGHT = (SLIDER_HEIGHT / 2),
60  SLIDER_HALF_HEIGHT_BIG = (SLIDER_HEIGHT_BIG / 2)
61  };
62 
63  Slider() :
64  m_dragging(false)
65  {
66  Init();
67  }
68 
69  inline u32 GetSliderWidth() const
70  {
71  return ParameterController<CC>::m_bigText ? SLIDER_WIDTH_BIG : SLIDER_WIDTH;
72  }
73 
74  inline u32 GetSliderHeight() const
75  {
76  return ParameterController<CC>::m_bigText ? SLIDER_HEIGHT_BIG : SLIDER_HEIGHT;
77  }
78 
79  inline u32 GetSliderHalfHeight() const
80  {
81  return ParameterController<CC>::m_bigText ? SLIDER_HALF_HEIGHT_BIG : SLIDER_HALF_HEIGHT;
82  }
83 
84  inline FontAsset GetRenderFont() const
85  {
86  return ParameterController<CC>::m_bigText ? FONT_ASSET_ELEMENT :
87  FONT_ASSET_HELPPANEL_SMALL;
88  }
89 
90  void Init()
91  {
92  Panel::SetDimensions(GetSliderWidth() * 2, GetSliderHeight());
93  Panel::SetDesiredSize(10000, GetSliderHeight());
94  }
95 
96  // We standardize on s32 for all values. We blithely assume that,
97  // for a slider, the range of u32 will not be large enough for
98  // that to be a problem.
99  s32 GetValue() const
100  {
101  MFM_API_ASSERT_NONNULL(this->m_parameter);
102  MFM_API_ASSERT_NONNULL(this->m_patom);
103  return this->m_parameter->GetBitsAsS32(*this->m_patom);
104  }
105 
106  void SetValue(s32 val)
107  {
108  MFM_API_ASSERT_NONNULL(this->m_parameter);
109  MFM_API_ASSERT_NONNULL(this->m_patom);
110  return this->m_parameter->SetBitsAsS32(*this->m_patom, val);
111  }
112 
113  void SetSliderTarget(Parameter<CC>* parameter, Atom<CC> * patom)
114  {
115  if (!parameter && !patom)
116  {
117  this->m_parameter = NULL;
118  this->m_patom = NULL;
119  return;
120  }
121 
122  if (!parameter || !patom)
123  {
124  FAIL(ILLEGAL_ARGUMENT);
125  }
126 
127  this->m_parameter = parameter;
128  this->m_patom = patom;
129  }
130 
131  virtual void PaintBorder(Drawing& d)
132  { /* No border*/ }
133 
134  virtual bool Handle(MouseButtonEvent& event)
135  {
136  if(event.m_event.button.type == SDL_MOUSEBUTTONDOWN)
137  {
138  if(event.m_event.button.button == SDL_BUTTON_LEFT)
139  {
140  if(GetSliderRect().Contains(event.GetAt() - this->GetAbsoluteLocation()))
141  {
142  if(this->m_parameter)
143  {
144  m_dragging = true;
145  m_dragStartX = event.GetAt().GetX();
146  m_preDragVal = GetValue();
147  return true;
148  }
149  }
150  }
151  }
152  else
153  {
154  m_dragging = false;
155  /* Might want the things underneath to let up too. */
156  return false;
157  }
158  return false;
159  }
160 
161  virtual bool Handle(MouseMotionEvent& event)
162  {
163  if(m_dragging && this->m_parameter)
164  {
165  s32 delta = event.m_event.motion.x - m_dragStartX;
166  s32 valDelta = delta * ((double)this->m_parameter->GetMax() / GetSliderWidth());
167 
168  if(delta < 0)
169  {
170  if(valDelta >= 0)
171  {
172  valDelta = 0;
173  }
174  }
175  if (this->m_parameter)
176  {
177  SetValue(m_preDragVal + valDelta);
178  }
179  }
180  return m_dragging;
181  }
182 
183  virtual void OnMouseExit()
184  {
185  m_dragging = false;
186  }
187 
188  virtual void PaintComponent(Drawing & d)
189  {
190  if (!this->m_parameter)
191  {
192  return;
193  }
194 
195  d.SetForeground(Drawing::BLACK);
196 
197  d.SetFont(AssetManager::Get(GetRenderFont()));
198 
199  d.DrawHLine(16, 7, GetSliderWidth() - 7);
200 
201  d.DrawVLine(7, 16, 20);
202  d.DrawVLine(GetSliderWidth() - 7, 16, 20);
203 
204  CharBufferByteSink<16> numBuffer;
205  numBuffer.Printf("%d", this->m_parameter->GetMin());
206  d.BlitText(numBuffer.GetZString(),
207  UPoint(3, 16),
208  UPoint(48, 16));
209 
210  numBuffer.Reset();
211  numBuffer.Printf("%d", this->m_parameter->GetMax());
212  d.BlitText(numBuffer.GetZString(),
213  UPoint(GetSliderWidth() - 32, GetSliderHalfHeight()),
214  UPoint(48, GetSliderHalfHeight()));
215 
216  numBuffer.Reset();
217  this->m_parameter->PrintValue(numBuffer, *this->m_patom);
218 
219  d.SetBackground(Drawing::GREY70);
220  d.SetForeground(Drawing::WHITE);
221  d.BlitBackedText(numBuffer.GetZString(),
222  UPoint(GetSliderWidth() / 2 - 16, GetSliderHalfHeight()),
223  UPoint(48, GetSliderHalfHeight()));
224 
225  Rect sliderRect = GetSliderRect();
226  d.BlitAsset(ASSET_SLIDER_HANDLE,
227  MakeUnsigned(sliderRect.GetPosition()),
228  sliderRect.GetSize());
229 
230  d.BlitBackedText(this->m_parameter->GetName(),
231  UPoint(GetSliderWidth(), 7),
232  UPoint(GetSliderWidth(), GetSliderHalfHeight()));
233  }
234 
235  private:
236 
237  inline Rect GetSliderRect() const
238  {
239  s32 xpos = 0;
240  if (this->m_parameter)
241  {
242  s32 val = GetValue();
243  xpos = this->m_parameter->MapValue((GetSliderWidth() - 7), val) - 7;
244  }
245  return Rect(xpos > 0 ? xpos : 0, 0, 17, GetSliderHalfHeight());
246  }
247  };
248 }
249 
250 #endif /* SLIDER_H */
Definition: Rect.h:40
const SPoint & GetPosition() const
Definition: Rect.h:137
Definition: Panel.h:91
void BlitAsset(Asset asset, UPoint loc, UPoint maxSize) const
Definition: Drawing.cpp:167
virtual void PaintBorder(Drawing &d)
Definition: Slider.h:131
u32 SetForeground(const u32 color)
Definition: Drawing.h:188
Definition: Atom.h:71
Definition: Panel.h:74
virtual bool Handle(MouseButtonEvent &event)
Definition: Slider.h:134
Definition: ParameterController.h:40
virtual void PaintComponent(Drawing &d)
Definition: Slider.h:188
SPoint GetAbsoluteLocation()
Definition: Panel.cpp:173
void BlitBackedText(const char *message, UPoint loc, UPoint size)
Definition: Drawing.cpp:233
const UPoint & GetSize() const
Definition: Rect.h:199
void Reset()
Definition: CharBufferByteSink.h:144
u32 SetBackground(const u32 color)
Definition: Drawing.h:170
virtual void OnMouseExit()
Definition: Slider.h:183
void DrawHLine(int y, int startX, int endX) const
Definition: Drawing.cpp:96
Definition: Slider.h:40
Definition: CharBufferByteSink.h:44
void DrawVLine(int x, int startY, int endY) const
Definition: Drawing.cpp:101
Definition: Parameter.h:51
const char * GetZString()
Definition: CharBufferByteSink.h:112
virtual bool Handle(MouseMotionEvent &event)
Definition: Slider.h:161
TTF_Font * SetFont(TTF_Font *newFont)
Definition: Drawing.h:209
void BlitText(const char *message, UPoint loc, UPoint size) const
Definition: Drawing.cpp:172
Definition: Drawing.h:44