Skip to content
This repository was archived by the owner on Jan 5, 2024. It is now read-only.

Commit 5280ac5

Browse files
authored
Merge pull request #198 from cortex-command-community/slider-wheel
GUI slider wheel
2 parents 27454b8 + 5103110 commit 5280ac5

File tree

8 files changed

+139
-124
lines changed

8 files changed

+139
-124
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
135135

136136
- Game window resolution can now be changed without restarting the game.
137137

138+
- GUI sliders (like for music volume) can now be adjusted with the mouse scroll wheel.
139+
138140
### Changed
139141

140142
- Codebase now uses the C++17 standard.

GUI/GUISlider.cpp

Lines changed: 60 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ GUISlider::GUISlider(GUIManager *Manager, GUIControlManager *ControlManager)
3838
m_Minimum = 0;
3939
m_Value = 0;
4040
m_Maximum = 100;
41+
m_ValueResolution = 1;
4142
}
4243

4344

@@ -117,10 +118,11 @@ void GUISlider::Create(GUIProperties *Props)
117118
Props->GetValue("Minimum", &m_Minimum);
118119
Props->GetValue("Maximum", &m_Maximum);
119120
Props->GetValue("Value", &m_Value);
121+
if (!Props->GetValue("ValueResolution", &m_ValueResolution)) {
122+
m_ValueResolution = std::max((m_Maximum - m_Minimum) / 100, 1);
123+
}
120124

121-
// Clamp the value
122-
m_Value = MAX(m_Value, m_Minimum);
123-
m_Value = MIN(m_Value, m_Maximum);
125+
m_Value = std::clamp(m_Value, m_Minimum, m_Maximum);
124126

125127
// Re-Calculate the knob info
126128
CalculateKnob();
@@ -356,9 +358,7 @@ void GUISlider::OnMouseDown(int X, int Y, int Buttons, int Modifier)
356358
Size = m_Height;
357359
}
358360

359-
// Clamp the knob position
360-
m_KnobPosition = MAX(m_KnobPosition, 0);
361-
m_KnobPosition = MIN(m_KnobPosition, Size-m_KnobSize);
361+
m_KnobPosition = std::clamp(m_KnobPosition, m_EndThickness, Size - m_KnobSize - m_EndThickness);
362362

363363
// Calculate the new value
364364
int Area = Size-m_KnobSize;
@@ -368,13 +368,7 @@ void GUISlider::OnMouseDown(int X, int Y, int Buttons, int Modifier)
368368
m_Value = (float)MaxRange * p + m_Minimum;
369369
}
370370

371-
// Clamp the value
372-
m_Value = MAX(m_Value, m_Minimum);
373-
m_Value = MIN(m_Value, m_Maximum);
374-
375-
// Clamp the knob position again for the graphics
376-
m_KnobPosition = MAX(m_KnobPosition, m_EndThickness);
377-
m_KnobPosition = MIN(m_KnobPosition, Size-m_KnobSize-m_EndThickness);
371+
m_Value = std::clamp(m_Value, m_Minimum, m_Maximum);
378372

379373
// If the value has changed, add the "Changed" notification
380374
if (m_Value != m_OldValue)
@@ -456,6 +450,23 @@ void GUISlider::OnMouseMove(int X, int Y, int Buttons, int Modifier)
456450
m_OldValue = m_Value;
457451
}
458452
}
453+
454+
455+
void GUISlider::OnMouseWheelChange(int x, int y, int modifier, int mouseWheelChange) {
456+
m_OldValue = m_Value;
457+
458+
if (mouseWheelChange < 0) {
459+
m_Value = std::max(m_Value - m_ValueResolution, m_Minimum);
460+
} else {
461+
m_Value = std::min(m_Value + m_ValueResolution, m_Maximum);
462+
}
463+
464+
if (m_Value != m_OldValue) {
465+
CalculateKnob();
466+
AddEvent(GUIEvent::Notification, Changed, 0);
467+
}
468+
}
469+
459470
/*
460471
461472
//////////////////////////////////////////////////////////////////////////////////////////
@@ -511,37 +522,18 @@ GUIPanel *GUISlider::GetPanel(void)
511522
//////////////////////////////////////////////////////////////////////////////////////////
512523
// Description: Calculates the knob position and size.
513524

514-
void GUISlider::CalculateKnob(void)
515-
{
516-
m_KnobPosition = 0;
517-
m_KnobSize = 0;
518-
519-
// Knob image not loaded yet
520-
if (!m_KnobImage)
521-
return;
522-
523-
if (m_Orientation == Horizontal) {
524-
// Horizontal
525-
m_KnobSize = m_KnobImage->GetWidth();
526-
527-
if (m_Maximum-m_Minimum > 0) {
528-
float V = (float)(m_Value-m_Minimum) / (float)(m_Maximum-m_Minimum);
529-
m_KnobPosition = (float)(m_Width-m_KnobSize)*V;
530-
}
531-
532-
} else {
533-
// Vertical
534-
m_KnobSize = m_KnobImage->GetHeight();
535-
536-
if (m_Maximum-m_Minimum > 0) {
537-
float V = (float)(m_Value-m_Minimum) / (float)(m_Maximum-m_Minimum);
538-
m_KnobPosition = (float)(m_Height-m_KnobSize)*V;
539-
}
540-
}
541-
542-
// Clamp the knob position again for the graphics
543-
m_KnobPosition = MAX(m_KnobPosition, m_EndThickness);
544-
m_KnobPosition = MIN(m_KnobPosition, (m_Orientation == Horizontal ? m_Width : m_Height) - m_KnobSize - m_EndThickness);
525+
void GUISlider::CalculateKnob(void) {
526+
if (!m_KnobImage) {
527+
return;
528+
}
529+
530+
if (m_Maximum > m_Minimum) {
531+
const bool horizontalOrientation = (m_Orientation == Horizontal);
532+
m_KnobSize = (horizontalOrientation) ? m_KnobImage->GetWidth() : m_KnobImage->GetHeight();
533+
const int size = (horizontalOrientation) ? m_Width : m_Height;
534+
const float valueRatio = static_cast<float>(m_Value - m_Minimum) / static_cast<float>(m_Maximum - m_Minimum);
535+
m_KnobPosition = m_EndThickness + static_cast<int>(static_cast<float>(size - m_KnobSize - (m_EndThickness * 2)) * valueRatio);
536+
}
545537
}
546538

547539

@@ -640,10 +632,13 @@ int GUISlider::GetTickDirection(void)
640632

641633
void GUISlider::SetMinimum(int Minimum)
642634
{
643-
m_Minimum = Minimum;
635+
if (Minimum != m_Minimum) {
636+
m_Minimum = Minimum;
637+
m_Value = std::max(m_Value, m_Minimum);
644638

645-
// Re-Calculate the knob info
646-
CalculateKnob();
639+
// Re-Calculate the knob info
640+
CalculateKnob();
641+
}
647642
}
648643

649644

@@ -665,10 +660,13 @@ int GUISlider::GetMinimum(void)
665660

666661
void GUISlider::SetMaximum(int Maximum)
667662
{
668-
m_Maximum = Maximum;
663+
if (Maximum != m_Maximum) {
664+
m_Maximum = Maximum;
665+
m_Value = std::min(m_Value, m_Maximum);
669666

670-
// Re-Calculate the knob info
671-
CalculateKnob();
667+
// Re-Calculate the knob info
668+
CalculateKnob();
669+
}
672670
}
673671

674672

@@ -692,17 +690,12 @@ void GUISlider::SetValue(int Value)
692690
{
693691
int OldValue = m_Value;
694692

695-
m_Value = Value;
696-
697-
// Clamp it
698-
m_Value = MAX(m_Value, m_Minimum);
699-
m_Value = MIN(m_Value, m_Maximum);
693+
m_Value = std::clamp(Value, m_Minimum, m_Maximum);
700694

701-
if (m_Value != OldValue)
702-
AddEvent(GUIEvent::Notification, Changed, 0);
703-
704-
// Re-Calculate the knob info
705-
CalculateKnob();
695+
if (m_Value != OldValue) {
696+
CalculateKnob();
697+
AddEvent(GUIEvent::Notification, Changed, 0);
698+
}
706699
}
707700

708701

@@ -765,4 +758,10 @@ void GUISlider::ApplyProperties(GUIProperties *Props)
765758
m_Value = MIN(m_Value, m_Maximum);
766759

767760
BuildBitmap();
768-
}
761+
}
762+
763+
void GUISlider::SetValueResolution(int valueRes) {
764+
if (valueRes >= 1 && valueRes <= m_Maximum - m_Minimum) {
765+
m_ValueResolution = valueRes;
766+
}
767+
}

GUI/GUISlider.h

Lines changed: 35 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,16 @@ class GUISlider :
132132
void OnMouseMove(int X, int Y, int Buttons, int Modifier) override;
133133

134134

135+
/// <summary>
136+
/// Called when the mouse scroll wheel is moved.
137+
/// </summary>
138+
/// <param name="x">Mouse X position.</param>
139+
/// <param name="y">Mouse Y position.</param>
140+
/// <param name="modifier">Activated modifier buttons.</param>
141+
/// <param name="mouseWheelChange">The amount of wheel movement. Positive is scroll up, negative is scroll down.</param>
142+
void OnMouseWheelChange(int x, int y, int modifier, int mouseWheelChange) override;
143+
144+
135145
//////////////////////////////////////////////////////////////////////////////////////////
136146
// Method: GetPanel
137147
//////////////////////////////////////////////////////////////////////////////////////////
@@ -286,6 +296,13 @@ class GUISlider :
286296
void ApplyProperties(GUIProperties *Props) override;
287297

288298

299+
/// <summary>
300+
/// Sets the value resolution for this slider.
301+
/// </summary>
302+
/// <param name="valueRes">The new value resolution</param>
303+
void SetValueResolution(int valueRes);
304+
305+
289306
//////////////////////////////////////////////////////////////////////////////////////////
290307
// Private member variable and method declarations
291308

@@ -321,24 +338,24 @@ class GUISlider :
321338

322339
// Members
323340

324-
GUIBitmap *m_DrawBitmap;
325-
GUIBitmap *m_KnobImage;
326-
327-
// Properties
328-
int m_Orientation;
329-
int m_TickDirection;
330-
int m_Minimum;
331-
int m_Maximum;
332-
int m_Value;
333-
334-
// Internal variables
335-
int m_KnobPosition;
336-
int m_KnobSize;
337-
bool m_KnobGrabbed;
338-
int m_KnobGrabPos;
339-
int m_EndThickness;
340-
int m_OldValue;
341-
341+
GUIBitmap *m_DrawBitmap;
342+
GUIBitmap *m_KnobImage;
343+
344+
// Properties
345+
int m_Orientation;
346+
int m_TickDirection;
347+
int m_Minimum;
348+
int m_Maximum;
349+
int m_Value;
350+
int m_ValueResolution;
351+
352+
// Internal variables
353+
int m_KnobPosition;
354+
int m_KnobSize;
355+
bool m_KnobGrabbed;
356+
int m_KnobGrabPos;
357+
int m_EndThickness;
358+
int m_OldValue;
342359
};
343360

344361

Managers/MetaMan.cpp

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,11 @@ int MetaMan::Create()
9090
//////////////////////////////////////////////////////////////////////////////////////////
9191
// Description: Wipes any current and sets up a new game based on a size parameter.
9292

93-
int MetaMan::NewGame(float gameSize)
93+
int MetaMan::NewGame(int gameSize)
9494
{
9595
// Grab a random selection of Scene presets from all available
9696
list<Scene *> scenePresets;
97-
SelectScenePresets(gameSize, m_Players.size(), &scenePresets);
97+
SelectScenePresets(gameSize, &scenePresets);
9898

9999
// Destroy and clear any pre-existing scenes from previous games
100100
for (vector<Scene *>::iterator sItr = m_Scenes.begin(); sItr != m_Scenes.end(); ++sItr)
@@ -1035,29 +1035,19 @@ int MetaMan::TotalScenePresets(std::list<Scene *> *pScenes)
10351035
//////////////////////////////////////////////////////////////////////////////////////////
10361036
// Description: Yields a set of randomly selected Scene presets for a new game.
10371037

1038-
int MetaMan::SelectScenePresets(float gameSize, int playerCount, list<Scene *> *pSelected)
1038+
int MetaMan::SelectScenePresets(int gameSize, list<Scene *> *pSelected)
10391039
{
10401040
// Get the list of ALL eligible read-in Scene presets
10411041
list<Scene *> scenePresets;
10421042
TotalScenePresets(&scenePresets);
10431043

1044-
// How many scenes the game should end up with, according to the specified game size.
1045-
// Note that it will never be all or none of all the available scenes!
1046-
// TODO: Hook these constants up to settings!!
1047-
int minCount = MAX(3, MIN(std::floor(playerCount * 1.5), scenePresets.size()));
1048-
int maxCount = MAX(std::floor(scenePresets.size() * 0.7), minCount);
1049-
// Determine the actual game size
1050-
int gameSceneCount = minCount + std::floor((maxCount - minCount) * gameSize);
1051-
// Clamp
1052-
gameSceneCount = MIN(gameSceneCount, maxCount);
1053-
gameSceneCount = MAX(gameSceneCount, minCount);
1054-
10551044
// If we need to actually fill the list, do so
10561045
if (pSelected)
10571046
{
10581047
// Go through the list and randomly knock out as many presets as necessary to reach the number we need for this game
1059-
int randomIndex, currentIndex;
1060-
while (scenePresets.size() > gameSceneCount)
1048+
int randomIndex;
1049+
int currentIndex;
1050+
while (scenePresets.size() > gameSize)
10611051
{
10621052
// Randomly select one of the scenes and remove it
10631053
currentIndex = 0;
@@ -1075,11 +1065,12 @@ int MetaMan::SelectScenePresets(float gameSize, int playerCount, list<Scene *> *
10751065

10761066
// Cast and copy (not deep!) to fill the provided list
10771067
pSelected->clear();
1078-
for (list<Scene *>::iterator pItr = scenePresets.begin(); pItr != scenePresets.end(); ++pItr)
1079-
pSelected->push_back(dynamic_cast<Scene *>(*pItr));
1068+
for (Scene *scenePointer : scenePresets) {
1069+
pSelected->push_back(scenePointer);
1070+
}
10801071
}
10811072

1082-
return gameSceneCount;
1073+
return gameSize;
10831074
}
10841075

10851076

Managers/MetaMan.h

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -120,12 +120,12 @@ friend class MetaSave;
120120
// Method: NewGame
121121
//////////////////////////////////////////////////////////////////////////////////////////
122122
// Description: Wipes any current and sets up a new game based on a size parameter.
123-
// Arguments: The size of the new Metagame, from 0 to 1.0, which will affect how
123+
// Arguments: The size of the new Metagame, which will affect how
124124
// many Scenes/Sites will ultimately be used.
125125
// Return value: An error return value signaling success or any particular failure.
126126
// Anything below 0 is an error signal.
127127

128-
int NewGame(float gameSize = 0.5);
128+
int NewGame(int gameSize = 3);
129129

130130

131131
//////////////////////////////////////////////////////////////////////////////////////////
@@ -535,14 +535,13 @@ friend class MetaSave;
535535
// Method: SelectScenePresets
536536
//////////////////////////////////////////////////////////////////////////////////////////
537537
// Description: Yields a set of randomly selected Scene presets for a new game.
538-
// Arguments: The normalized scalar from 0 to 1.0 that controls the size of the set.
539-
// The number of players that will be playing this new game.
538+
// Arguments: The size of the set.
540539
// The list to fill with the selected presets, depending on currently
541540
// set player numbers and loaded eligible scenes. If no list is passed
542541
// it will be ignored. Presets returned in list are NOT OWNED there.
543542
// Return value: The count of selected preset scenes.
544543

545-
int SelectScenePresets(float gameSize, int playerCount, std::list<Scene *> *pSelected = 0);
544+
int SelectScenePresets(int gameSize, std::list<Scene *> *pSelected = 0);
546545

547546

548547
//////////////////////////////////////////////////////////////////////////////////////////

0 commit comments

Comments
 (0)