doom3-gpl
Doom 3 GPL source release
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
SliderWindow.cpp
Go to the documentation of this file.
1 /*
2 ===========================================================================
3 
4 Doom 3 GPL Source Code
5 Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company.
6 
7 This file is part of the Doom 3 GPL Source Code (?Doom 3 Source Code?).
8 
9 Doom 3 Source Code is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
13 
14 Doom 3 Source Code is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18 
19 You should have received a copy of the GNU General Public License
20 along with Doom 3 Source Code. If not, see <http://www.gnu.org/licenses/>.
21 
22 In addition, the Doom 3 Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 Source Code. If not, please request a copy in writing from id Software at the address below.
23 
24 If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
25 
26 ===========================================================================
27 */
28 
29 #include "../idlib/precompiled.h"
30 #pragma hdrstop
31 
32 #include "DeviceContext.h"
33 #include "Window.h"
34 #include "UserInterfaceLocal.h"
35 #include "SliderWindow.h"
36 
37 /*
38 ============
39 idSliderWindow::CommonInit
40 ============
41 */
43  value = 0.0;
44  low = 0.0;
45  high = 100.0;
46  stepSize = 1.0;
47  thumbMat = declManager->FindMaterial("_default");
48  buddyWin = NULL;
49 
50  cvar = NULL;
51  cvar_init = false;
52  liveUpdate = true;
53 
54  vertical = false;
55  scrollbar = false;
56 
57  verticalFlip = false;
58 }
59 
61  dc = d;
62  gui = g;
63  CommonInit();
64 }
65 
67  gui = g;
68  CommonInit();
69 }
70 
72 
73 }
74 
75 bool idSliderWindow::ParseInternalVar(const char *_name, idParser *src) {
76  if (idStr::Icmp(_name, "stepsize") == 0 || idStr::Icmp(_name, "step") == 0) {
77  stepSize = src->ParseFloat();
78  return true;
79  }
80  if (idStr::Icmp(_name, "low") == 0) {
81  low = src->ParseFloat();
82  return true;
83  }
84  if (idStr::Icmp(_name, "high") == 0) {
85  high = src->ParseFloat();
86  return true;
87  }
88  if (idStr::Icmp(_name, "vertical") == 0) {
89  vertical = src->ParseBool();
90  return true;
91  }
92  if (idStr::Icmp(_name, "verticalflip") == 0) {
93  verticalFlip = src->ParseBool();
94  return true;
95  }
96  if (idStr::Icmp(_name, "scrollbar") == 0) {
97  scrollbar = src->ParseBool();
98  return true;
99  }
100  if (idStr::Icmp(_name, "thumbshader") == 0) {
101  ParseString(src, thumbShader);
103  return true;
104  }
105  return idWindow::ParseInternalVar(_name, src);
106 }
107 
108 idWinVar *idSliderWindow::GetWinVarByName(const char *_name, bool fixup, drawWin_t** owner) {
109 
110  if (idStr::Icmp(_name, "value") == 0) {
111  return &value;
112  }
113  if (idStr::Icmp(_name, "cvar") == 0) {
114  return &cvarStr;
115  }
116  if ( idStr::Icmp( _name, "liveUpdate" ) == 0 ) {
117  return &liveUpdate;
118  }
119  if ( idStr::Icmp( _name, "cvarGroup" ) == 0 ) {
120  return &cvarGroup;
121  }
122 
123  return idWindow::GetWinVarByName(_name, fixup, owner);
124 }
125 
126 const char *idSliderWindow::HandleEvent(const sysEvent_t *event, bool *updateVisuals) {
127 
128  if (!(event->evType == SE_KEY && event->evValue2)) {
129  return "";
130  }
131 
132  int key = event->evValue;
133 
134  if ( event->evValue2 && key == K_MOUSE1 ) {
135  SetCapture(this);
136  RouteMouseCoords(0.0f, 0.0f);
137  return "";
138  }
139 
140  if ( key == K_RIGHTARROW || key == K_KP_RIGHTARROW || ( key == K_MOUSE2 && gui->CursorY() > thumbRect.y ) ) {
141  value = value + stepSize;
142  }
143 
144  if ( key == K_LEFTARROW || key == K_KP_LEFTARROW || ( key == K_MOUSE2 && gui->CursorY() < thumbRect.y ) ) {
145  value = value - stepSize;
146  }
147 
148  if (buddyWin) {
150  } else {
152  UpdateCvar( false );
153  }
154 
155  return "";
156 }
157 
158 
160  buddyWin = buddy;
161 }
162 
165  value = 0.0;
167  thumbMat->SetSort( SS_GUI );
170  //vertical = state.GetBool("vertical");
171  //scrollbar = state.GetBool("scrollbar");
173  InitCvar();
174 }
175 
176 void idSliderWindow::InitWithDefaults(const char *_name, const idRectangle &_rect, const idVec4 &_foreColor, const idVec4 &_matColor, const char *_background, const char *thumbShader, bool _vertical, bool _scrollbar) {
177  SetInitialState(_name);
178  rect = _rect;
179  foreColor = _foreColor;
180  matColor = _matColor;
181  thumbMat = declManager->FindMaterial(thumbShader);
182  thumbMat->SetSort( SS_GUI );
185  background = declManager->FindMaterial(_background);
187  vertical = _vertical;
188  scrollbar = _scrollbar;
190 }
191 
192 void idSliderWindow::SetRange(float _low, float _high, float _step) {
193  low = _low;
194  high = _high;
195  stepSize = _step;
196 }
197 
198 void idSliderWindow::SetValue(float _value) {
199  value = _value;
200 }
201 
202 void idSliderWindow::Draw(int time, float x, float y) {
204 
205  if ( !cvar && !buddyWin ) {
206  return;
207  }
208 
209  if ( !thumbWidth || !thumbHeight ) {
212  }
213 
214  UpdateCvar( true );
215  if ( value > high ) {
216  value = high;
217  } else if ( value < low ) {
218  value = low;
219  }
220 
221  float range = high - low;
222 
223  if ( range <= 0.0f ) {
224  return;
225  }
226 
227  float thumbPos = (range) ? (value - low) / range : 0.0;
228  if (vertical) {
229  if ( verticalFlip ) {
230  thumbPos = 1.f - thumbPos;
231  }
232  thumbPos *= drawRect.h - thumbHeight;
233  thumbPos += drawRect.y;
234  thumbRect.y = thumbPos;
235  thumbRect.x = drawRect.x;
236  } else {
237  thumbPos *= drawRect.w - thumbWidth;
238  thumbPos += drawRect.x;
239  thumbRect.x = thumbPos;
240  thumbRect.y = drawRect.y;
241  }
244 
245  if ( hover && !noEvents && Contains(gui->CursorX(), gui->CursorY()) ) {
246  color = hoverColor;
247  } else {
248  hover = false;
249  }
250  if ( flags & WIN_CAPTURE ) {
251  color = hoverColor;
252  hover = true;
253  }
254 
256  if ( flags & WIN_FOCUS ) {
257  dc->DrawRect(thumbRect.x+1.0f, thumbRect.y+1.0f, thumbRect.w-2.0f, thumbRect.h-2.0f, 1.0f, color);
258  }
259 }
260 
261 
263  if ( !cvar && !buddyWin ) {
264  return;
265  }
266 
267  if ( high - low <= 0.0f ) {
268  return;
269  }
270 
271  idRectangle r = _drawRect;
272  if (!scrollbar) {
273  if ( vertical ) {
274  r.y += thumbHeight / 2.f;
275  r.h -= thumbHeight;
276  } else {
277  r.x += thumbWidth / 2.0;
278  r.w -= thumbWidth;
279  }
280  }
282 }
283 
284 const char *idSliderWindow::RouteMouseCoords(float xd, float yd) {
285  float pct;
286 
287  if (!(flags & WIN_CAPTURE)) {
288  return "";
289  }
290 
292  r.x = actualX;
293  r.y = actualY;
294  r.x += thumbWidth / 2.0;
295  r.w -= thumbWidth;
296  if (vertical) {
297  r.y += thumbHeight / 2;
298  r.h -= thumbHeight;
299  if (gui->CursorY() >= r.y && gui->CursorY() <= r.Bottom()) {
300  pct = (gui->CursorY() - r.y) / r.h;
301  if ( verticalFlip ) {
302  pct = 1.f - pct;
303  }
304  value = low + (high - low) * pct;
305  } else if (gui->CursorY() < r.y) {
306  if ( verticalFlip ) {
307  value = high;
308  } else {
309  value = low;
310  }
311  } else {
312  if ( verticalFlip ) {
313  value = low;
314  } else {
315  value = high;
316  }
317  }
318  } else {
319  r.x += thumbWidth / 2;
320  r.w -= thumbWidth;
321  if (gui->CursorX() >= r.x && gui->CursorX() <= r.Right()) {
322  pct = (gui->CursorX() - r.x) / r.w;
323  value = low + (high - low) * pct;
324  } else if (gui->CursorX() < r.x) {
325  value = low;
326  } else {
327  value = high;
328  }
329  }
330 
331  if (buddyWin) {
333  } else {
335  }
336  UpdateCvar( false );
337 
338  return "";
339 }
340 
341 
342 void idSliderWindow::Activate(bool activate, idStr &act) {
343  idWindow::Activate(activate, act);
344  if ( activate ) {
345  UpdateCvar( true, true );
346  }
347 }
348 
349 /*
350 ============
351 idSliderWindow::InitCvar
352 ============
353 */
355  if ( cvarStr[0] == '\0' ) {
356  if ( !buddyWin ) {
357  common->Warning( "idSliderWindow::InitCvar: gui '%s' window '%s' has an empty cvar string", gui->GetSourceFile(), name.c_str() );
358  }
359  cvar_init = true;
360  cvar = NULL;
361  return;
362  }
363 
364  cvar = cvarSystem->Find( cvarStr );
365  if ( !cvar ) {
366  common->Warning( "idSliderWindow::InitCvar: gui '%s' window '%s' references undefined cvar '%s'", gui->GetSourceFile(), name.c_str(), cvarStr.c_str() );
367  cvar_init = true;
368  return;
369  }
370 }
371 
372 /*
373 ============
374 idSliderWindow::UpdateCvar
375 ============
376 */
377 void idSliderWindow::UpdateCvar( bool read, bool force ) {
378  if ( buddyWin || !cvar ) {
379  return;
380  }
381  if ( force || liveUpdate ) {
382  value = cvar->GetFloat();
383  if ( value != gui->State().GetFloat( cvarStr ) ) {
384  if ( read ) {
386  } else {
387  value = gui->State().GetFloat( cvarStr );
388  cvar->SetFloat( value );
389  }
390  }
391  }
392 }
393 
394 /*
395 ============
396 idSliderWindow::RunNamedEvent
397 ============
398 */
399 void idSliderWindow::RunNamedEvent( const char* eventName ) {
400  idStr event, group;
401 
402  if ( !idStr::Cmpn( eventName, "cvar read ", 10 ) ) {
403  event = eventName;
404  group = event.Mid( 10, event.Length() - 10 );
405  if ( !group.Cmp( cvarGroup ) ) {
406  UpdateCvar( true, true );
407  }
408  } else if ( !idStr::Cmpn( eventName, "cvar write ", 11 ) ) {
409  event = eventName;
410  group = event.Mid( 11, event.Length() - 11 );
411  if ( !group.Cmp( cvarGroup ) ) {
412  UpdateCvar( false, true );
413  }
414  }
415 }
416 
GLubyte g
Definition: glext.h:4662
byte color[4]
Definition: MegaTexture.cpp:54
float GetFloat(const char *key, const char *defaultString="0") const
Definition: Dict.h:248
GLsizei const GLfloat * value
Definition: glext.h:3614
idCVar * cvar
Definition: SliderWindow.h:86
idWinBool liveUpdate
Definition: SliderWindow.h:88
int Cmp(const char *text) const
Definition: Str.h:652
idCVarSystem * cvarSystem
Definition: CVarSystem.cpp:487
bool Contains(float x, float y)
Definition: Window.cpp:681
float GetFloat(void) const
Definition: CVarSystem.h:144
idWinStr cvarGroup
Definition: SliderWindow.h:89
const idMaterial * thumbMat
Definition: SliderWindow.h:78
unsigned int flags
Definition: Window.h:371
int Length(void) const
Definition: Str.h:702
void InitWithDefaults(const char *_name, const idRectangle &rect, const idVec4 &foreColor, const idVec4 &matColor, const char *_background, const char *thumbShader, bool _vertical, bool _scrollbar)
idWinBool noEvents
Definition: Window.h:398
virtual bool ParseInternalVar(const char *name, idParser *src)
Definition: Window.cpp:1915
GLenum GLint GLint y
Definition: glext.h:2849
idWinVec4 matColor
Definition: Window.h:401
idWinFloat value
Definition: SliderWindow.h:70
const int WIN_FOCUS
Definition: Window.h:44
void ParseString(idParser *src, idStr &out)
Definition: Window.cpp:1883
void UpdateCvar(bool read, bool force=false)
void RunNamedEvent(const char *eventName)
int GetImageHeight(void) const
Definition: Material.cpp:2526
idWinStr cvarStr
Definition: SliderWindow.h:85
idDeviceContext * dc
Definition: Window.h:425
virtual const idMaterial * FindMaterial(const char *name, bool makeDefault=true)=0
GLuint src
Definition: glext.h:5390
virtual ~idSliderWindow()
const idMaterial * background
Definition: Window.h:414
GLenum GLint x
Definition: glext.h:2849
int Cmpn(const char *text, int n) const
Definition: Str.h:657
GLsizei range
Definition: glext.h:4368
int Icmp(const char *text) const
Definition: Str.h:667
const int WIN_CAPTURE
Definition: Window.h:45
virtual void HandleBuddyUpdate(idWindow *buddy)
Definition: Window.h:271
void SetValue(float _value)
virtual idCVar * Find(const char *name)=0
bool ParseBool(void)
Definition: Parser.cpp:2797
virtual void Activate(bool activate, idStr &act)
idRectangle drawRect
Definition: Window.h:373
virtual void Activate(bool activate, idStr &act)
Definition: Window.cpp:481
virtual void DrawBackground(const idRectangle &drawRect)
Definition: Window.cpp:1107
virtual void PostParse()
Definition: Window.cpp:1709
Definition: Vector.h:808
virtual const char * HandleEvent(const sysEvent_t *event, bool *updateVisuals)
virtual const char * RouteMouseCoords(float xd, float yd)
virtual void SetBuddy(idWindow *buddy)
float y
Definition: Rectangle.h:37
idCommon * common
Definition: Common.cpp:206
#define NULL
Definition: Lib.h:88
float actualY
Definition: Window.h:369
int evValue2
Definition: sys_public.h:218
virtual void Draw(int time, float x, float y)
virtual float CursorY()
const char * GetSourceFile(void) const
idWinVec4 foreColor
Definition: Window.h:402
idWindow * SetCapture(idWindow *w)
Definition: Window.cpp:550
const int WIN_CANFOCUS
Definition: Window.h:50
virtual void SetStateFloat(const char *varName, const float value)
float ParseFloat(void)
Definition: Parser.cpp:2812
virtual const idDict & State() const
sysEventType_t evType
Definition: sys_public.h:216
idSliderWindow(idUserInterfaceLocal *gui)
idWinRectangle rect
Definition: Window.h:399
float x
Definition: Rectangle.h:36
idDeclManager * declManager
void SetInitialState(const char *_name)
Definition: Window.cpp:2114
float Right() const
Definition: Rectangle.h:43
virtual idWinVar * GetWinVarByName(const char *_name, bool winLookup=false, drawWin_t **owner=NULL)
GLdouble GLdouble GLdouble r
Definition: glext.h:2951
const char * Mid(int start, int len, idStr &result) const
Definition: Str.cpp:603
virtual void DrawBackground(const idRectangle &drawRect)
idUserInterfaceLocal * gui
Definition: Window.h:427
tuple f
Definition: idal.py:89
const GLcharARB * name
Definition: glext.h:3629
float Bottom() const
Definition: Rectangle.h:42
Definition: Str.h:116
int GetImageWidth(void) const
Definition: Material.cpp:2516
float w
Definition: Rectangle.h:38
virtual const char * c_str() const
Definition: Winvar.h:204
void SetSort(float s) const
Definition: Material.h:513
float h
Definition: Rectangle.h:39
virtual bool ParseInternalVar(const char *name, idParser *src)
bool hover
Definition: Window.h:423
void SetRange(float _low, float _high, float _step)
if(!ValidDisplayID(prefInfo.prefDisplayID)) prefInfo.prefDisplayID
idRectangle thumbRect
Definition: SliderWindow.h:77
const int WIN_HOLDCAPTURE
Definition: Window.h:53
void DrawMaterial(float x, float y, float w, float h, const idMaterial *mat, const idVec4 &color, float scalex=1.0, float scaley=1.0)
void SetFloat(const float value)
Definition: CVarSystem.h:149
virtual void PostParse()
virtual void virtual void Warning(const char *fmt,...) id_attribute((format(printf
virtual float CursorX()
virtual idWinVar * GetWinVarByName(const char *_name, bool winLookup=false, drawWin_t **owner=NULL)
Definition: Window.cpp:1776
idWindow * buddyWin
Definition: SliderWindow.h:82
void DrawRect(float x, float y, float width, float height, float size, const idVec4 &color)
idWinVec4 hoverColor
Definition: Window.h:403
float actualX
Definition: Window.h:368