From ae541ffaf4699dc1dc66baa125830144a443c172 Mon Sep 17 00:00:00 2001 From: cutealien Date: Fri, 4 Jan 2013 14:35:28 +0000 Subject: [PATCH] - IGUISpinBox now passes on the EGET_BUTTON_CLICKED, EGET_EDITBOX_CHANGED and EGET_EDITBOX_ENTER events from it's sub-elements. - IGUISpinBox no longer validates values after each character type but only on KEY_ENTER and when losing focus. New behavior can be set with IGUISpinBox::setValidateOn git-svn-id: svn://svn.code.sf.net/p/irrlicht/code/trunk@4428 dfc29bdd-3216-0410-991c-e03cc46cb475 --- changes.txt | 2 ++ include/IGUISpinBox.h | 23 +++++++++++++++++++++++ source/Irrlicht/CGUISpinBox.cpp | 28 +++++++++++++++++++++++----- source/Irrlicht/CGUISpinBox.h | 8 ++++++++ 4 files changed, 56 insertions(+), 5 deletions(-) diff --git a/changes.txt b/changes.txt index 7470b7fb..bff6d570 100644 --- a/changes.txt +++ b/changes.txt @@ -1,6 +1,8 @@ -------------------------- Changes in 1.9 (not yet released) +- IGUISpinBox now passes on the EGET_BUTTON_CLICKED, EGET_EDITBOX_CHANGED and EGET_EDITBOX_ENTER events from it's sub-elements. +- IGUISpinBox no longer validates values after each character type but only on KEY_ENTER and when losing focus. New behavior can be set with IGUISpinBox::setValidateOn - IAttributes::getAttributeAs functions now can have a customizable default-parameter to return when attributeName is not found - Added ECFN_DISABLED value (it works like ECFN_NEVER worked before) and changed ECFN_NEVER behaviour (it works like its equivalent value in OpenGL/Direct3D). diff --git a/include/IGUISpinBox.h b/include/IGUISpinBox.h index bef139d8..4ce788ba 100644 --- a/include/IGUISpinBox.h +++ b/include/IGUISpinBox.h @@ -13,6 +13,21 @@ namespace gui { class IGUIEditBox; + //! Enumeration bitflag for when to validate the text typed into the spinbox + //! Default used by Irrlicht is: (EGUI_SBV_ENTER|EGUI_SBV_LOSE_FOCUS) + enum EGUI_SPINBOX_VALIDATION + { + //! Does not validate typed text, probably a bad idea setting this usually. + EGUI_SBV_NEVER = 0, + //! Validate on each change. Was default up to Irrlicht 1.8 + EGUI_SBV_CHANGE = 1, + //! Validate when enter was pressed + EGUI_SBV_ENTER = 2, + //! Validate when the editbox loses the focus + EGUI_SBV_LOSE_FOCUS = 4, + }; + + //! Single line edit box + spin buttons /** \par This element can create the following events of type EGUI_EVENT_TYPE: \li EGET_SPINBOX_CHANGED @@ -59,6 +74,14 @@ namespace gui //! get the current step size virtual f32 getStepSize() const = 0; + + //! Sets when the spinbox has to validate entered text. + /** \param validateOn Can be any combination of EGUI_SPINBOX_VALIDATION bit flags */ + virtual void setValidateOn(u32 validateOn) = 0; + + //! Gets when the spinbox has to validate entered text. + /** \return A combination of EGUI_SPINBOX_VALIDATION bit flags */ + virtual u32 getValidateOn() const = 0; }; diff --git a/source/Irrlicht/CGUISpinBox.cpp b/source/Irrlicht/CGUISpinBox.cpp index b4406999..6630878d 100644 --- a/source/Irrlicht/CGUISpinBox.cpp +++ b/source/Irrlicht/CGUISpinBox.cpp @@ -23,7 +23,7 @@ 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) + DecimalPlaces(-1), ValidateOn(EGUI_SBV_ENTER|EGUI_SBV_LOSE_FOCUS) { #ifdef _DEBUG setDebugName("CGUISpinBox"); @@ -156,7 +156,6 @@ f32 CGUISpinBox::getStepSize() const return StepSize; } - void CGUISpinBox::setStepSize(f32 step) { StepSize = step; @@ -179,12 +178,24 @@ void CGUISpinBox::setDecimalPlaces(s32 places) setValue(getValue()); } +//! Sets when the spinbox has to validate entered text. +void CGUISpinBox::setValidateOn(u32 validateOn) +{ + ValidateOn = validateOn; +} + +//! Gets when the spinbox has to validate entered text. +u32 CGUISpinBox::getValidateOn() const +{ + return ValidateOn; +} bool CGUISpinBox::OnEvent(const SEvent& event) { if (IsEnabled) { bool changeEvent = false; + bool eatEvent = false; switch(event.EventType) { case EET_MOUSE_INPUT_EVENT: @@ -195,6 +206,7 @@ bool CGUISpinBox::OnEvent(const SEvent& event) f32 val = getValue() + (StepSize * (event.MouseInput.Wheel < 0 ? -1.f : 1.f)); setValue(val); changeEvent = true; + eatEvent = true; } break; default: @@ -203,6 +215,7 @@ bool CGUISpinBox::OnEvent(const SEvent& event) break; case EET_GUI_EVENT: + if (event.GUIEvent.EventType == EGET_BUTTON_CLICKED) { if (event.GUIEvent.Caller == ButtonSpinUp) @@ -220,9 +233,12 @@ bool CGUISpinBox::OnEvent(const SEvent& event) changeEvent = true; } } - if (event.GUIEvent.EventType == EGET_EDITBOX_CHANGED || event.GUIEvent.EventType == EGET_EDITBOX_ENTER) + if (event.GUIEvent.Caller == EditBox) { - if (event.GUIEvent.Caller == EditBox) + if ( (event.GUIEvent.EventType == EGET_EDITBOX_CHANGED && ValidateOn & EGUI_SBV_CHANGE) + || (event.GUIEvent.EventType == EGET_EDITBOX_ENTER && ValidateOn & EGUI_SBV_ENTER) + || (event.GUIEvent.EventType == EGET_ELEMENT_FOCUS_LOST && ValidateOn & EGUI_SBV_LOSE_FOCUS) + ) { verifyValueRange(); changeEvent = true; @@ -243,7 +259,7 @@ bool CGUISpinBox::OnEvent(const SEvent& event) e.GUIEvent.EventType = EGET_SPINBOX_CHANGED; if ( Parent ) Parent->OnEvent(e); - return true; + return eatEvent; } } @@ -307,6 +323,7 @@ void CGUISpinBox::serializeAttributes(io::IAttributes* out, io::SAttributeReadWr out->addFloat("Max", getMax()); out->addFloat("Step", getStepSize()); out->addInt("DecimalPlaces", DecimalPlaces); + out->addInt("ValidateOn", (s32)ValidateOn); } @@ -317,6 +334,7 @@ void CGUISpinBox::deserializeAttributes(io::IAttributes* in, io::SAttributeReadW setRange(in->getAttributeAsFloat("Min"), in->getAttributeAsFloat("Max")); setStepSize(in->getAttributeAsFloat("Step")); setDecimalPlaces(in->getAttributeAsInt("DecimalPlaces")); + setValidateOn((u32)in->getAttributeAsInt("ValidateOn", (s32)ValidateOn) ); } diff --git a/source/Irrlicht/CGUISpinBox.h b/source/Irrlicht/CGUISpinBox.h index e487c0b3..e41e2c07 100644 --- a/source/Irrlicht/CGUISpinBox.h +++ b/source/Irrlicht/CGUISpinBox.h @@ -76,6 +76,13 @@ namespace gui /** \param places: The number of decimal places to display, use -1 to reset */ virtual void setDecimalPlaces(s32 places); + //! Sets when the spinbox has to validate entered text. + /** \param validateOn Can be any combination of EGUI_SPINBOX_VALIDATION bit flags */ + virtual void setValidateOn(u32 validateOn); + + //! Gets when the spinbox has to validate entered text. + virtual u32 getValidateOn() const; + //! Writes attributes of the element. virtual void serializeAttributes(io::IAttributes* out, io::SAttributeReadWriteOptions* options) const; @@ -96,6 +103,7 @@ namespace gui core::stringw FormatString; s32 DecimalPlaces; + u32 ValidateOn; // combination of EGUI_SPINBOX_VALIDATION bit-flags };