From a883d464f9bb7a56567d6bb3c28328648ca9cb8b Mon Sep 17 00:00:00 2001 From: cutealien Date: Wed, 28 Sep 2022 14:25:18 +0000 Subject: [PATCH] Add IGUISpinBox functions getValueFor and getOldValue Also documenting some missing feature (decimal places ignored with direct text input) getValueFor allows to check the value a given text would have getOldValue can be used to check the previous value in a EGET_SPINBOX_CHANGED event git-svn-id: svn://svn.code.sf.net/p/irrlicht/code/trunk@6429 dfc29bdd-3216-0410-991c-e03cc46cb475 --- changes.txt | 1 + include/IGUISpinBox.h | 15 +++++++++- source/Irrlicht/CGUISpinBox.cpp | 51 +++++++++++++++++++-------------- source/Irrlicht/CGUISpinBox.h | 12 +++++++- 4 files changed, 55 insertions(+), 24 deletions(-) diff --git a/changes.txt b/changes.txt index 5a35fae7..be5e83d3 100644 --- a/changes.txt +++ b/changes.txt @@ -1,6 +1,7 @@ -------------------------- Changes in 1.9 (not yet released) +- Add IGUISpinBox functions getValueFor and getOldValue - Bugfix: IGUIElement::getNextElement now passing includeInvisible and includeDisabled flags recursively instead of disabling those for children. This fixes problems that elements sometimes didn't get a tab order because some parent was invisible and so tab'ing for them failed completely. Also setTabOrder(-1) now also checks for disabled elements for the same reason (disabled parent causing children to not get a tab-order). diff --git a/include/IGUISpinBox.h b/include/IGUISpinBox.h index 6251d673..ab881545 100644 --- a/include/IGUISpinBox.h +++ b/include/IGUISpinBox.h @@ -51,6 +51,12 @@ namespace gui //! Get the current value of the spinbox virtual f32 getValue() const = 0; + //! Get the value the spinbox would have for the given text + /** Note: There is no rounding for decimal places going on here + The reason is that so far spinbox doesn't restrict entering longer + numbers (or any other text) (TODO)*/ + virtual f32 getValueFor(const wchar_t* text) const = 0; + //! set the range of values which can be used in the spinbox /** \param min: minimum value \param max: maximum value */ @@ -68,7 +74,8 @@ namespace gui virtual void setStepSize(f32 step=1.f) = 0; //! Sets the number of decimal places to display. - //! Note that this also rounds the range to the same number of decimal places. + //! Note: This also rounds the range to the same number of decimal places. + //! Note: This is only used for the buttons so far, text-input ignores it (TODO) /** \param places: The number of decimal places to display, use -1 to reset */ virtual void setDecimalPlaces(s32 places) = 0; @@ -82,6 +89,12 @@ namespace gui //! Gets when the spinbox has to validate entered text. /** \return A combination of EGUI_SPINBOX_VALIDATION bit flags */ virtual u32 getValidateOn() const = 0; + + //! Gets previous value in EGET_SPINBOX_CHANGED events + /** Note: That value changes as soon as a new value is set (to the new value). + So it's only useful to check for last value in the event and has no use otherwise. + Also it's possible to mess it up by setting text via the editbox sub-element directly. */ + virtual f32 getOldValue() const = 0; }; diff --git a/source/Irrlicht/CGUISpinBox.cpp b/source/Irrlicht/CGUISpinBox.cpp index 16e81656..06083777 100644 --- a/source/Irrlicht/CGUISpinBox.cpp +++ b/source/Irrlicht/CGUISpinBox.cpp @@ -23,7 +23,8 @@ CGUISpinBox::CGUISpinBox(const wchar_t* text, bool border,IGUIEnvironment* envir : IGUISpinBox(environment, parent, id, rectangle), EditBox(0), ButtonSpinUp(0), ButtonSpinDown(0), StepSize(1.f), RangeMin(-FLT_MAX), RangeMax(FLT_MAX), FormatString(L"%f"), - DecimalPlaces(-1), ValidateOn(EGUI_SBV_ENTER|EGUI_SBV_LOSE_FOCUS) + DecimalPlaces(-1), ValidateOn(EGUI_SBV_ENTER|EGUI_SBV_LOSE_FOCUS), + OldValue(0.f) { #ifdef _DEBUG setDebugName("CGUISpinBox"); @@ -55,6 +56,8 @@ CGUISpinBox::CGUISpinBox(const wchar_t* text, bool border,IGUIEnvironment* envir EditBox->setAlignment(EGUIA_UPPERLEFT, EGUIA_LOWERRIGHT, EGUIA_UPPERLEFT, EGUIA_LOWERRIGHT); refreshSprites(); + + OldValue = getValue(); } @@ -103,24 +106,28 @@ IGUIEditBox* CGUISpinBox::getEditBox() const void CGUISpinBox::setValue(f32 val) { - wchar_t str[100]; + OldValue = val; + wchar_t str[100]; swprintf_irr(str, 99, FormatString.c_str(), val); EditBox->setText(str); - verifyValueRange(); + verifyValueRange(getValue()); } f32 CGUISpinBox::getValue() const { - const wchar_t* val = EditBox->getText(); + return getValueFor(EditBox->getText()); +} + +f32 CGUISpinBox::getValueFor(const wchar_t* val) const +{ if ( !val ) return 0.f; core::stringc tmp(val); return core::fast_atof(tmp.c_str()); } - void CGUISpinBox::setRange(f32 min, f32 max) { if (maxOnEvent(e); + core::swap(oldValue, OldValue); + } if ( eatEvent ) return true; } @@ -286,17 +299,12 @@ void CGUISpinBox::draw() IGUISpinBox::draw(); } -void CGUISpinBox::verifyValueRange() +void CGUISpinBox::verifyValueRange(f32 val) { - f32 val = getValue(); if ( val+core::ROUNDING_ERROR_f32 < RangeMin ) - val = RangeMin; + setValue(RangeMin); else if ( val-core::ROUNDING_ERROR_f32 > RangeMax ) - val = RangeMax; - else - return; - - setValue(val); + setValue(RangeMax); } @@ -305,7 +313,6 @@ void CGUISpinBox::setText(const wchar_t* text) { EditBox->setText(text); setValue(getValue()); - verifyValueRange(); } diff --git a/source/Irrlicht/CGUISpinBox.h b/source/Irrlicht/CGUISpinBox.h index b412577e..5fe9afd7 100644 --- a/source/Irrlicht/CGUISpinBox.h +++ b/source/Irrlicht/CGUISpinBox.h @@ -41,6 +41,9 @@ namespace gui //! Get the current value of the spinbox virtual f32 getValue() const IRR_OVERRIDE; + //! Get the value the spinbox would have for the given text + virtual f32 getValueFor(const wchar_t* text) const IRR_OVERRIDE; + //! set the range of values which can be used in the spinbox /** \param min: minimum value \param max: maximum value */ @@ -83,6 +86,12 @@ namespace gui //! Gets when the spinbox has to validate entered text. virtual u32 getValidateOn() const IRR_OVERRIDE; + //! Gets previous value in EGET_SPINBOX_CHANGED events + virtual f32 getOldValue() const IRR_OVERRIDE + { + return OldValue; + } + //! Writes attributes of the element. virtual void serializeAttributes(io::IAttributes* out, io::SAttributeReadWriteOptions* options) const IRR_OVERRIDE; @@ -90,7 +99,7 @@ namespace gui virtual void deserializeAttributes(io::IAttributes* in, io::SAttributeReadWriteOptions* options) IRR_OVERRIDE; protected: - virtual void verifyValueRange(); + void verifyValueRange(f32 val); void refreshSprites(); IGUIEditBox * EditBox; @@ -104,6 +113,7 @@ namespace gui core::stringw FormatString; s32 DecimalPlaces; u32 ValidateOn; // combination of EGUI_SPINBOX_VALIDATION bit-flags + f32 OldValue; };