Skip to content

Commit

Permalink
Allow option to edit text on standard slider value (3MwJ7gglI8)
Browse files Browse the repository at this point in the history
  • Loading branch information
Neko-Box-Coder committed Aug 29, 2023
1 parent 99e846b commit c0568cc
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 34 deletions.
59 changes: 33 additions & 26 deletions Include/ssGUI/GUIObjectClasses/CompositeClasses/StandardSlider.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include "ssGUI/GUIObjectClasses/CompositeClasses/Slider.hpp"
#include "ssGUI/GUIObjectClasses/Text.hpp"
#include "ssGUI/GUIObjectClasses/TextField.hpp"

//namespace: ssGUI
namespace ssGUI
Expand Down Expand Up @@ -40,7 +41,7 @@ namespace ssGUI
SetSize(glm::vec2(300, 30));
//Set layout preferred sizes
layout->AddPreferredSizeMultipliers(0.3f, 0.5f, 0.1f);
layout->AddPreferredSizeMultipliers(0.25f, 0.5f, 0.15f);
//Add components
auto* sliderTitle = AddChild<ssGUI::Text>();
Expand All @@ -57,11 +58,16 @@ namespace ssGUI
sliderAS->SetVerticalPixel(sliderHeight);
SliderObject = CurrentObjectsReferences.AddObjectReference(slider);
auto* sliderValue = AddChild<ssGUI::Text>();
auto* sliderValue = AddChild<ssGUI::TextField>();
sliderValue->RemoveExtension<ssGUI::Extensions::Outline>();
sliderValue->RemoveExtension<ssGUI::Extensions::BoxShadow>();
sliderValue->RemoveExtension<ssGUI::Extensions::RoundedCorners>();
sliderValue->SetBackgroundColor(glm::u8vec4());
sliderValue->SetText(ValueToString(DisplayInteger, DisplayDecimalPlaces, slider->GetSliderValue()));
SliderDisplayValueTextObject = CurrentObjectsReferences.AddObjectReference(sliderValue);
AddDisplayValueEventCallback();
AddTextModifiedEventCallback();
}
=================================================================
*/
Expand All @@ -85,6 +91,7 @@ namespace ssGUI
virtual void ConstructRenderInfo() override;

virtual void AddDisplayValueEventCallback();
virtual void AddTextModifiedEventCallback();
//virtual void RemoveDisplayValueEventCallback();
virtual void UpdateDisplayTextContent();

Expand All @@ -94,36 +101,36 @@ namespace ssGUI
StandardSlider();
virtual ~StandardSlider() override;

//function: SetTitleTextObject
//Sets the title text GUI Object.
//The text content of the old text GUI Object will be copied to the new one.
//The new text GUI Object will be inserted at the same place as the old one.
//Passing nullptr will unset the text GUI object.
/*function: SetTitleTextObject
Sets the title text GUI Object.
The text content of the old text GUI Object will be copied to the new one.
The old text GUI Object will be deleted and the new text GUI Object will be inserted at the same place as the old one.
Passing nullptr will unset the text GUI object.*/
virtual void SetTitleTextObject(ssGUI::Text* text);

//function: GetTitleTextObject
//Gets the title text GUI Object.
virtual ssGUI::Text* GetTitleTextObject() const;

//function: SetSliderObject
//Sets the actual slider GUI object.
//The new slider GUI Object will be inserted at the same place as the old one.
//Passing nullptr will unset the slider GUI object.
/*function: SetSliderObject
Sets the actual slider GUI object.
The old slider GUI Object will be deleted and the new slider GUI Object will be inserted at the same place as the old one.
Passing nullptr will unset the slider GUI object.*/
virtual void SetSliderObject(ssGUI::Slider* slider);

//function: GetSliderObject
//Gets the actual slider GUI object.
virtual ssGUI::Slider* GetSliderObject() const;

//function: SetDisplayValueTextObject
//Sets the display value text for showing the value of the slider.
//The new text GUI Object will be inserted at the same place as the old one.
//Passing nullptr will unset the display value text GUI object.
virtual void SetDisplayValueTextObject(ssGUI::Text* text);
/*function: SetDisplayValueTextObject
Sets the display value text for showing the value of the slider.
The old slider GUI Object will be deleted and the new text GUI Object will be inserted at the same place as the old one.
Passing nullptr will unset the display value text GUI object.*/
virtual void SetDisplayValueTextObject(ssGUI::TextField* text);

//function: GetDisplayValueTextObject
//Gets the display value text for showing the value of the slider.
virtual ssGUI::Text* GetDisplayValueTextObject() const;
virtual ssGUI::TextField* GetDisplayValueTextObject() const;

//function: SetMinDisplayValue
//Sets the minimum display value that is mapped to 0 for the actual slider value.
Expand Down Expand Up @@ -165,21 +172,21 @@ namespace ssGUI
//the mapped minimum and maximum display value.
virtual float GetDisplayStepValue() const;

//function: SetDisplayIntegerValue
//Sets if integer is shown instead of float.
//Please note that the underlying display value is still float,
//but just rounded to integer when displaying in value text GUI Object.
/*function: SetDisplayIntegerValue
Sets if integer is shown instead of float.
Please note that the underlying display value is still float,
but just rounded to integer when displaying in value text GUI Object.*/
virtual void SetDisplayIntegerValue(bool displayInteger);

//function: IsDisplayIntegerValue
//Returns if integer is shown instead of float.
virtual bool IsDisplayIntegerValue() const;

//function: SetDisplayDecimalPlaces
//Sets how many decimal places are shown.
//This has no effect if <IsDisplayIntegerValue> is false.
//Please note that the underlying display value is not affected by this,
//but just discarding the extra decimals when displaying in value text GUI Object.
/*function: SetDisplayDecimalPlaces
Sets how many decimal places are shown.
This has no effect if <IsDisplayIntegerValue> is false.
Please note that the underlying display value is not affected by this,
but just discarding the extra decimals when displaying in value text GUI Object.*/
virtual void SetDisplayDecimalPlaces(int decimalPlaces);

//function: GetDisplayDecimalPlaces
Expand Down
65 changes: 57 additions & 8 deletions Src/ssGUI/GUIObjectClasses/CompositeClasses/StandardSlider.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#include "ssGUI/GUIObjectClasses/CompositeClasses/StandardSlider.hpp"
#include "ssGUI/Extensions/AdvancedSize.hpp"
#include "ssGUI/Extensions/BoxShadow.hpp"
#include "ssGUI/Extensions/Layout.hpp"
#include "ssGUI/Extensions/RoundedCorners.hpp"
#include "ssGUI/Extensions/Outline.hpp"

namespace
{
Expand Down Expand Up @@ -48,15 +51,14 @@ namespace ssGUI
(
ListenerKey,
this,
[holderId, this](ssGUI::EventInfo& info)
[holderId](ssGUI::EventInfo& info)
{
auto* slider = static_cast<ssGUI::Slider*>(info.Container);
auto* standardSlider = info.References->GetObjectReference<ssGUI::StandardSlider>(holderId);

if(standardSlider == nullptr || standardSlider->GetDisplayValueTextObject() == nullptr)
{
ssGUI::EventCallback* ecb = slider->GetEventCallback(ssGUI::Enums::EventType::SLIDER_VALUE_CHANGED);
ecb->RemoveEventListener(ListenerKey, this);
info.DeleteCurrentListener = true;
return;
}

Expand All @@ -71,7 +73,48 @@ namespace ssGUI
slider->ForwardEvent(this, Enums::EventType::SLIDER_VALUE_CHANGED_VIA_GUI);
slider->ForwardEvent(this, Enums::EventType::SLIDER_VALUE_FINISHED_CHANGING);
}


void StandardSlider::AddTextModifiedEventCallback()
{
ssGUI::TextField* textfield = GetDisplayValueTextObject();
if(textfield == nullptr)
return;

auto* ecb = textfield->AddEventCallback(ssGUI::Enums::EventType::TEXT_FIELD_CONTENT_FINISHED_CHANGING_VIA_GUI);
ssGUIObjectIndex holderId = ecb->AddObjectReference(this);
ecb->AddEventListener
(
ListenerKey,
this,
[holderId](ssGUI::EventInfo& info)
{
auto* textfield = static_cast<ssGUI::TextField*>(info.Container);
auto* standardSlider = info.References->GetObjectReference<ssGUI::StandardSlider>(holderId);

if(standardSlider == nullptr || standardSlider->GetSliderObject() == nullptr)
{
info.DeleteCurrentListener = true;
return;
}

try
{
float result = std::stof(textfield->GetText());
standardSlider->SetDisplayValue(result);
}
catch(...)
{
standardSlider->SetDisplayValue(0);
textfield->SetText("0");
}
}
);

//Forward events from textfield to this
textfield->ForwardEvent(this, Enums::EventType::TEXT_FIELD_CONTENT_CHANGED_VIA_GUI);
textfield->ForwardEvent(this, Enums::EventType::TEXT_FIELD_CONTENT_FINISHED_CHANGING_VIA_GUI);
}

//void StandardSlider::RemoveDisplayValueEventCallback()
//{
// ssGUI::Slider* slider = GetSliderObject();
Expand Down Expand Up @@ -131,11 +174,16 @@ namespace ssGUI
sliderAS->SetVerticalPixel(sliderHeight);
SliderObject = CurrentObjectsReferences.AddObjectReference(slider);

auto* sliderValue = AddChild<ssGUI::Text>();
auto* sliderValue = AddChild<ssGUI::TextField>();
sliderValue->RemoveExtension<ssGUI::Extensions::Outline>();
sliderValue->RemoveExtension<ssGUI::Extensions::BoxShadow>();
sliderValue->RemoveExtension<ssGUI::Extensions::RoundedCorners>();
sliderValue->SetBackgroundColor(glm::u8vec4());
sliderValue->SetText(ValueToString(DisplayInteger, DisplayDecimalPlaces, slider->GetSliderValue()));
SliderDisplayValueTextObject = CurrentObjectsReferences.AddObjectReference(sliderValue);

AddDisplayValueEventCallback();
AddTextModifiedEventCallback();
}

StandardSlider::~StandardSlider()
Expand Down Expand Up @@ -213,7 +261,7 @@ namespace ssGUI
return CurrentObjectsReferences.GetObjectReference<ssGUI::Slider>(SliderObject);
}

void StandardSlider::SetDisplayValueTextObject(ssGUI::Text* text)
void StandardSlider::SetDisplayValueTextObject(ssGUI::TextField* text)
{
if(text == nullptr)
{
Expand All @@ -237,11 +285,12 @@ namespace ssGUI

SliderDisplayValueTextObject = CurrentObjectsReferences.AddObjectReference(text);
UpdateDisplayTextContent();
AddTextModifiedEventCallback();
}

ssGUI::Text* StandardSlider::GetDisplayValueTextObject() const
ssGUI::TextField* StandardSlider::GetDisplayValueTextObject() const
{
return CurrentObjectsReferences.GetObjectReference<ssGUI::Text>(SliderDisplayValueTextObject);
return CurrentObjectsReferences.GetObjectReference<ssGUI::TextField>(SliderDisplayValueTextObject);
}

void StandardSlider::SetMinDisplayValue(float min)
Expand Down

0 comments on commit c0568cc

Please sign in to comment.