Skip to content

Commit

Permalink
Fixed slider rendering invalid vertices (YNZP9Ue9W7), etc...
Browse files Browse the repository at this point in the history
Fixed slider rendering invalid vertices, causing RoundedCorners to fail,
Added slider Playground,
RoundedCorners now output GUI Object type when fails
  • Loading branch information
Neko-Box-Coder committed May 7, 2023
1 parent bec0af3 commit 566a26b
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 74 deletions.
12 changes: 6 additions & 6 deletions Include/ssGUI/GUIObjectClasses/CompositeClasses/Slider.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ namespace ssGUI
float KeyInputInterval; //See <GetKeyInputMoveInterval>
float EndPadding; //See <GetEndPadding>
glm::vec2 KnobGlobalPosition; //(Internal variable) This is used for syncing the slider value and knob position
glm::vec2 KnobGlobalOffset; //(Internal variable) This is used for syncing the slider value and knob position
float CursorKnobOffset; //(Internal variable) Offset between the cursor down position and the knob
bool LastSliderDragging; //(Internal variable) Used to see if the slider is being dragged last frame
bool SliderDragging; //(Internal variable) Flag if slider is being dragged right now
Expand All @@ -53,7 +53,7 @@ namespace ssGUI
ScrollInternal(0.05),
KeyInputInterval(0.05),
EndPadding(0),
KnobGlobalPosition(),
KnobGlobalOffset(),
CursorKnobOffset(0),
LastSliderDragging(false),
SliderDragging(false),
Expand Down Expand Up @@ -120,7 +120,7 @@ namespace ssGUI
);
KnobObject = CurrentObjectsReferences.GetObjectIndex(button);
UpdateKnobPosition(true);
UpdateKnobOffset();
}
=================================================================
*/
Expand All @@ -141,7 +141,7 @@ namespace ssGUI
float KeyInputInterval; //See <GetKeyInputMoveInterval>
float EndPadding; //See <GetEndPadding>

glm::vec2 KnobGlobalPosition; //(Internal variable) This is used for syncing the slider value and knob position
glm::vec2 KnobGlobalOffset; //(Internal variable) This is used for syncing the slider value and knob position
float CursorKnobOffset; //(Internal variable) Offset between the cursor down position and the knob
bool LastSliderDragging; //(Internal variable) Used to see if the slider is being dragged last frame
bool SliderDragging; //(Internal variable) Flag if slider is being dragged right now
Expand All @@ -160,9 +160,9 @@ namespace ssGUI

virtual void ApplySnapping();

virtual void UpdateKnobPosition(bool completeUpdate);
virtual void UpdateKnobOffset();

virtual void UpdateSliderValue();
virtual void UpdateSliderValueFromCursor(glm::vec2 cursorPos);

virtual void ConstructRenderInfo() override;

Expand Down
25 changes: 25 additions & 0 deletions Src/Examples/Playground/SliderPlayground.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include "ssGUI/Extensions/AdvancedPosition.hpp"
#include "ssGUI/HeaderGroups/StandardGroup.hpp"
#include "ssGUI/GUIObjectClasses/CompositeClasses/ImageCanvas.hpp"
#include "ssGUI/Extensions/Border.hpp"

#include "ssLogger/ssLog.hpp"
#include "ExamplesResources.h"

//Image Test
int main()
{
ssGUI::MainWindow mainWindow;

//Creating ssGUIManager and run it
ssGUI::ssGUIManager guiManager;
auto* window = mainWindow.AddChild<ssGUI::StandardWindow>();
auto* slider = window->AddChild<ssGUI::Slider>();
slider->AddExtension<ssGUI::Extensions::AdvancedPosition>();

guiManager.AddRootGUIObject((ssGUI::GUIObject*)&mainWindow);

guiManager.StartRunning();

return 0;
}
1 change: 1 addition & 0 deletions Src/ssGUI/Extensions/RoundedCorners.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ namespace Extensions
bool invalidAngle = false;
if(angleT1CirT2 < 0)
{
ssGUI_WARNING("Container type: "<<(int)Container->GetType());
ssGUI_WARNING(ssGUI_EXT_TAG, "anti-clockwise placements of vertices detected. Rounded corners failed.");
invalidAngle = true;
}
Expand Down
4 changes: 2 additions & 2 deletions Src/ssGUI/GUIObjectClasses/CompositeClasses/Scrollbar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ namespace ssGUI
if(IsVertical())
{
GetKnobObject()->SetSize(glm::vec2(GetSize().x - SidePadding * 2, GetSize().y * GetScrollbarSize()));
UpdateKnobPosition(true);
UpdateKnobOffset();
}
else
{
GetKnobObject()->SetSize(glm::vec2(GetSize().x * GetScrollbarSize(), GetSize().y - SidePadding * 2));
UpdateKnobPosition(true);
UpdateKnobOffset();
}
}

Expand Down
108 changes: 42 additions & 66 deletions Src/ssGUI/GUIObjectClasses/CompositeClasses/Slider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ namespace ssGUI
ScrollInternal = other.ScrollInternal;
KeyInputInterval = other.KeyInputInterval;

KnobGlobalPosition = other.KnobGlobalPosition;
KnobGlobalOffset = other.KnobGlobalOffset;
CursorKnobOffset = other.CursorKnobOffset;
LastSliderDragging = false;
SliderDragging = false;
Expand All @@ -44,7 +44,7 @@ namespace ssGUI
SliderValue = round(snapValue) * GetSnapInterval();
}

void Slider::UpdateKnobPosition(bool completeUpdate)
void Slider::UpdateKnobOffset()
{
auto knob = static_cast<ssGUI::Button*>(GetKnobObject());
glm::vec2 curKnobSize = knob == nullptr ? glm::vec2(KnobSize, KnobSize) : knob->GetSize();
Expand All @@ -54,42 +54,46 @@ namespace ssGUI
float leftX = GetGlobalPosition().x + GetEndPadding();
float rightX = GetGlobalPosition().x + GetSize().x - curKnobSize.x - GetEndPadding();

KnobGlobalPosition = glm::vec2
KnobGlobalOffset = glm::vec2
(
leftX + (rightX - leftX) * (IsReverse() ? (1 - GetSliderValue()) : GetSliderValue()),
completeUpdate ? GetGlobalPosition().y + (GetSize().y - curKnobSize.y) * 0.5 : KnobGlobalPosition.y
(rightX - leftX) * (IsReverse() ? (1 - GetSliderValue()) : GetSliderValue()),
(GetSize().y - curKnobSize.y) * 0.5
);
}
else
{
float topY = GetGlobalPosition().y + GetEndPadding();
float botY = GetGlobalPosition().y + GetSize().y - curKnobSize.y - GetEndPadding();

KnobGlobalPosition = glm::vec2
KnobGlobalOffset = glm::vec2
(
completeUpdate ? GetGlobalPosition().x + (GetSize().x - curKnobSize.x) * 0.5 : KnobGlobalPosition.x,
botY - (botY - topY) * (IsReverse() ? (1 - GetSliderValue()) : GetSliderValue())
(GetSize().x - curKnobSize.x) * 0.5,
(botY - topY) * (IsReverse() ? GetSliderValue() : (1 - GetSliderValue()))
);
}

if(knob != nullptr)
knob->SetGlobalPosition(KnobGlobalPosition);
knob->SetGlobalPosition(GetGlobalPosition() + KnobGlobalOffset);

RedrawObject();
//RedrawObject();
}

void Slider::UpdateSliderValue()
void Slider::UpdateSliderValueFromCursor(glm::vec2 cursorPos)
{
ssLOG_FUNC_ENTRY();
auto knob = static_cast<ssGUI::Button*>(CurrentObjectsReferences.GetObjectReference(KnobObject));
glm::vec2 curKnobSize = knob == nullptr ? glm::vec2(KnobSize, KnobSize) : knob->GetSize();

cursorPos -= CursorKnobOffset;
if(!IsVertical())
{
float leftX = GetGlobalPosition().x + GetEndPadding();
float rightX = GetGlobalPosition().x + GetSize().x - curKnobSize.x - GetEndPadding();

cursorPos.x = cursorPos.x < leftX ? leftX : cursorPos.x;
cursorPos.x = cursorPos.x > rightX ? rightX : cursorPos.x;

SetSliderValue((KnobGlobalPosition.x - leftX) / (rightX - leftX));
SetSliderValue((cursorPos.x - leftX) / (rightX - leftX));
if(IsReverse())
SetSliderValue(1 - GetSliderValue());
}
Expand All @@ -98,7 +102,10 @@ namespace ssGUI
float topY = GetGlobalPosition().y + GetEndPadding();
float botY = GetGlobalPosition().y + GetSize().y - curKnobSize.y - GetEndPadding();

SetSliderValue((botY - KnobGlobalPosition.y) / (botY - topY));
cursorPos.y = cursorPos.y < topY ? topY : cursorPos.y;
cursorPos.y = cursorPos.y > botY ? botY : cursorPos.y;

SetSliderValue((botY - cursorPos.y) / (botY - topY));
if(IsReverse())
SetSliderValue(1 - GetSliderValue());
}
Expand Down Expand Up @@ -133,30 +140,31 @@ namespace ssGUI
//Slider fill
auto knob = static_cast<ssGUI::Button*>(CurrentObjectsReferences.GetObjectReference(KnobObject));
glm::vec2 curKnobSize = knob == nullptr ? glm::vec2(KnobSize, KnobSize) : knob->GetSize();
glm::vec2 knobPos = drawPosition + KnobGlobalOffset;

if(!IsVertical())
{
if(!IsReverse())
{
DrawingVerticies.push_back(glm::vec2(drawPosition));
DrawingVerticies.push_back(glm::vec2(KnobGlobalPosition.x + curKnobSize.x / 2, drawPosition.y));
DrawingVerticies.push_back(glm::vec2(KnobGlobalPosition.x + curKnobSize.x / 2, drawPosition.y + GetSize().y));
DrawingVerticies.push_back(glm::vec2(knobPos.x + curKnobSize.x / 2, drawPosition.y));
DrawingVerticies.push_back(glm::vec2(knobPos.x + curKnobSize.x / 2, drawPosition.y + GetSize().y));
DrawingVerticies.push_back(glm::vec2(drawPosition.x, drawPosition.y + GetSize().y));
}
else
{
DrawingVerticies.push_back(glm::vec2(KnobGlobalPosition.x + curKnobSize.x / 2, drawPosition.y));
DrawingVerticies.push_back(glm::vec2(knobPos.x + curKnobSize.x / 2, drawPosition.y));
DrawingVerticies.push_back(glm::vec2(drawPosition.x + GetSize().x, drawPosition.y));
DrawingVerticies.push_back(glm::vec2(drawPosition.x + GetSize().x, drawPosition.y + GetSize().y));
DrawingVerticies.push_back(glm::vec2(KnobGlobalPosition.x + curKnobSize.x / 2, drawPosition.y + GetSize().y));
DrawingVerticies.push_back(glm::vec2(knobPos.x + curKnobSize.x / 2, drawPosition.y + GetSize().y));
}
}
else
{
if(!IsReverse())
{
DrawingVerticies.push_back(glm::vec2(drawPosition.x, KnobGlobalPosition.y + curKnobSize.y / 2));
DrawingVerticies.push_back(glm::vec2(drawPosition.x + GetSize().x, KnobGlobalPosition.y + curKnobSize.y / 2));
DrawingVerticies.push_back(glm::vec2(drawPosition.x, knobPos.y + curKnobSize.y / 2));
DrawingVerticies.push_back(glm::vec2(drawPosition.x + GetSize().x, knobPos.y + curKnobSize.y / 2));
DrawingVerticies.push_back(glm::vec2(drawPosition + GetSize()));
DrawingVerticies.push_back(glm::vec2(drawPosition.x, drawPosition.y + GetSize().y));

Expand All @@ -165,8 +173,8 @@ namespace ssGUI
{
DrawingVerticies.push_back(glm::vec2(drawPosition));
DrawingVerticies.push_back(glm::vec2(drawPosition.x + GetSize().x, drawPosition.y));
DrawingVerticies.push_back(glm::vec2(drawPosition.x + GetSize().x, KnobGlobalPosition.y + curKnobSize.y / 2));
DrawingVerticies.push_back(glm::vec2(drawPosition.x, KnobGlobalPosition.y + curKnobSize.y / 2));
DrawingVerticies.push_back(glm::vec2(drawPosition.x + GetSize().x, knobPos.y + curKnobSize.y / 2));
DrawingVerticies.push_back(glm::vec2(drawPosition.x, knobPos.y + curKnobSize.y / 2));
}
}

Expand Down Expand Up @@ -198,55 +206,18 @@ namespace ssGUI
{
CursorKnobOffset =
IsVertical() ?
mousePos.y - KnobGlobalPosition.y :
mousePos.x - KnobGlobalPosition.x;
mousePos.y - knob->GetGlobalPosition().y :
mousePos.x - knob->GetGlobalPosition().x;
}
//If the user is dragging the knob, update the position
else if(knob != nullptr && ((knob->GetButtonState() == ssGUI::Enums::ButtonState::CLICKING && IsInteractable() && IsBlockInput()) || SliderDragging))
{
if(IsVertical())
{
KnobGlobalPosition = glm::vec2
(
GetGlobalPosition().x + (GetSize().x - curKnobSize.x) * 0.5,
mousePos.y - CursorKnobOffset
);

float topY = GetGlobalPosition().y + GetEndPadding();
float botY = GetGlobalPosition().y + GetSize().y - curKnobSize.y - GetEndPadding();

//Do bound checks
if(KnobGlobalPosition.y < topY)
KnobGlobalPosition = glm::vec2(KnobGlobalPosition.x, topY);
else if(KnobGlobalPosition.y > botY)
KnobGlobalPosition = glm::vec2(KnobGlobalPosition.x, botY);
}
else
{
KnobGlobalPosition = glm::vec2
(
mousePos.x - CursorKnobOffset,
GetGlobalPosition().y + (GetSize().y - curKnobSize.y) * 0.5
);

float leftX = GetGlobalPosition().x + GetEndPadding();
float rightX = GetGlobalPosition().x + GetSize().x - curKnobSize.x - GetEndPadding();

//Do bound checks
if(KnobGlobalPosition.x < leftX)
KnobGlobalPosition = glm::vec2(leftX, KnobGlobalPosition.y);
else if(KnobGlobalPosition.x > rightX)
KnobGlobalPosition = glm::vec2(rightX, KnobGlobalPosition.y);
}

knob->SetGlobalPosition(KnobGlobalPosition);

knob->SetFocus(true);

float oriSliderValue = GetSliderValue();

//Update the slider value according to the slider position
UpdateSliderValue();
UpdateSliderValueFromCursor(mousePos);

//Record if slider value is changed via GUI
guiInteracted = oriSliderValue == GetSliderValue() ? false : true;
Expand All @@ -255,7 +226,7 @@ namespace ssGUI
ApplySnapping();

//Reposition the knob
UpdateKnobPosition(false);
UpdateKnobOffset();

inputStatus.MouseInputBlockedObject = this;
}
Expand Down Expand Up @@ -419,7 +390,12 @@ namespace ssGUI

//If there's any size or position change, we need to update the knob position
if(GetSize() != LastSize || GetGlobalPosition() != LastGlobalPosition)
UpdateKnobPosition(true);
{
UpdateKnobOffset();
auto knob = static_cast<ssGUI::Button*>(CurrentObjectsReferences.GetObjectReference(KnobObject));
glm::vec2 curKnobSize = knob == nullptr ? glm::vec2(KnobSize, KnobSize) : knob->GetSize();
glm::vec2 drawPosition = GetGlobalPosition();
}

LastSliderDragging = SliderDragging;
LastGlobalPosition = GetGlobalPosition();
Expand Down Expand Up @@ -466,7 +442,7 @@ namespace ssGUI
ScrollInternal(0.05),
KeyInputInterval(0.05),
EndPadding(0),
KnobGlobalPosition(),
KnobGlobalOffset(),
CursorKnobOffset(0),
LastSliderDragging(false),
SliderDragging(false),
Expand Down Expand Up @@ -533,7 +509,7 @@ namespace ssGUI
);

KnobObject = CurrentObjectsReferences.GetObjectIndex(button);
UpdateKnobPosition(true);
UpdateKnobOffset();
}

Slider::~Slider()
Expand Down Expand Up @@ -627,7 +603,7 @@ namespace ssGUI
SliderValue = SliderValue > 1 ? 1 : SliderValue;

ApplySnapping();
UpdateKnobPosition(false);
UpdateKnobOffset();

ssLOG_FUNC_EXIT();
}
Expand Down

0 comments on commit 566a26b

Please sign in to comment.