Skip to content

Bugfix ofParameter change name #6614

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions addons/ofxGui/src/ofxBaseGui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -483,3 +483,15 @@ void ofxBaseGui::disableHiDpi(){
bool ofxBaseGui::isHiDpiEnabled(){
return hiDpiScale == 2;
}

void ofxBaseGui::nameChanged( std::string& )
{
auto& p = getParameter();

setNeedsRedraw();

}
void ofxBaseGui::setNameListener()
{
nameChangeListener = getParameter().nameChangedEvent().newListener(this, &ofxBaseGui::nameChanged);
}
7 changes: 7 additions & 0 deletions addons/ofxGui/src/ofxBaseGui.h
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,17 @@ class ofxBaseGui {

void setNeedsRedraw();
ofCoreEvents * events = nullptr;

void setNameListener();

private:
bool needsRedraw;
unsigned long currentFrame;
bool bRegisteredForMouseEvents;

ofEventListener nameChangeListener;

void nameChanged( std::string& );

//std::vector<ofEventListener> coreListeners;
};
4 changes: 2 additions & 2 deletions addons/ofxGui/src/ofxButton.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ ofxButton* ofxButton::setup(ofParameter<void> _bVal, float width, float height){
registerMouseEvents();

value.addListener(this,&ofxButton::valueChanged);

setNameListener();
return this;
}

Expand All @@ -43,7 +43,7 @@ ofxButton* ofxButton::setup(const std::string& toggleName, float width, float he
registerMouseEvents();

value.addListener(this,&ofxButton::valueChanged);

setNameListener();
return this;
}

Expand Down
2 changes: 2 additions & 0 deletions addons/ofxGui/src/ofxColorPicker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,8 @@ ofxColorPicker_<ColorType> * ofxColorPicker_<ColorType>::setup(ofParameter<ofCol
listener = color.newListener(colorChanged);
setShape(b.x, b.y, w, h);
colorChanged(color.get());//needs this so the color wheel shows the correct color once setup.
setNameListener();

return this;
}
template<class ColorType>
Expand Down
1 change: 1 addition & 0 deletions addons/ofxGui/src/ofxGuiGroup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ ofxGuiGroup * ofxGuiGroup::setup(const ofParameterGroup & _parameters, const std
parameters = _parameters;
registerMouseEvents();

setNameListener();
setNeedsRedraw();

return this;
Expand Down
1 change: 1 addition & 0 deletions addons/ofxGui/src/ofxInputField.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ ofxInputField<Type>* ofxInputField<Type>::setup(ofParameter<Type> _val, float wi
listeners.push(value.newListener(this,&ofxInputField::valueChanged));
listeners.push(ofEvents().charEvent.newListener(this, &ofxInputField<Type>::charPressed, OF_EVENT_ORDER_BEFORE_APP));
listeners.push(ofEvents().keyPressed.newListener(this, &ofxInputField<Type>::keyPressed, OF_EVENT_ORDER_BEFORE_APP));
setNameListener();
return this;
}

Expand Down
2 changes: 1 addition & 1 deletion addons/ofxGui/src/ofxSlider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ ofxSlider<Type>* ofxSlider<Type>::setup(ofParameter<Type> _val, float width, flo

value.addListener(this,&ofxSlider::valueChanged);
registerMouseEvents();

setNameListener();
input.setup(_val, width, height);
return this;
}
Expand Down
5 changes: 3 additions & 2 deletions addons/ofxGui/src/ofxSliderGroup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ ofxVecSlider_<VecType> * ofxVecSlider_<VecType>::setup(ofParameter<VecType> valu
}

sliderChanging = false;
setNameListener();
return this;

}
Expand Down Expand Up @@ -154,7 +155,7 @@ ofxColorSlider_<ColorType> * ofxColorSlider_<ColorType>::setup(ofParameter<ofCol
add(&picker);
listeners.push(picker.getParameter().template cast<ofColor_<ColorType>>().newListener(this, & ofxColorSlider_::changeValue));


setNameListener();
sliderChanging = false;
minimize();
return this;
Expand Down Expand Up @@ -294,7 +295,7 @@ ofxRectangleSlider * ofxRectangleSlider::setup(ofParameter<ofRectangle> value, f
add(createGuiElement<ofxSlider<float>>(h, width, height));
listeners.push(h.newListener(this, & ofxRectangleSlider::changeSlider));


setNameListener();
sliderChanging = false;
return this;

Expand Down
2 changes: 1 addition & 1 deletion addons/ofxGui/src/ofxToggle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ ofxToggle * ofxToggle::setup(ofParameter<bool> _bVal, float width, float height)
value.addListener(this,&ofxToggle::valueChanged);
registerMouseEvents();
setNeedsRedraw();

setNameListener();
return this;

}
Expand Down
27 changes: 17 additions & 10 deletions libs/openFrameworks/types/ofParameter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,21 @@ ofParameter<void>::ofParameter(const string& name)

}

void ofParameter<void>::setName(const string & name){
bool ofParameter<void>::setName(const string & name){
std::string oldName = getName();
if(escape(name) == escape(oldName)) return false;
if(!ofParameterGroup::changeChildName(this, obj->parents, escape(oldName), escape(name))){
setName(oldName);
return false;
}
obj->name = name;
return true;
}


ofEvent<std::string>& ofParameter<void>::nameChangedEvent()
{
return obj->nameChangedEvent;
}

string ofParameter<void>::getName() const{
Expand Down Expand Up @@ -110,11 +123,8 @@ void ofParameter<void>::trigger(){
// Notify all parents, if there are any.
if(!obj->parents.empty())
{
// Erase each invalid parent
obj->parents.erase(std::remove_if(obj->parents.begin(),
obj->parents.end(),
[](const std::weak_ptr<ofParameterGroup::Value> & p){ return p.expired(); }),
obj->parents.end());
// Erase each invalid parent
ofParameterGroup::checkAndRemoveExpiredParents(obj->parents);

// notify all leftover (valid) parents of this object's changed value.
// this can't happen in the same iterator as above, because a notified listener
Expand All @@ -135,10 +145,7 @@ void ofParameter<void>::trigger(const void * sender){
if(!obj->parents.empty())
{
// Erase each invalid parent
obj->parents.erase(std::remove_if(obj->parents.begin(),
obj->parents.end(),
[](const std::weak_ptr<ofParameterGroup::Value> & p){ return p.expired(); }),
obj->parents.end());
ofParameterGroup::checkAndRemoveExpiredParents(obj->parents);

// notify all leftover (valid) parents of this object's changed value.
// this can't happen in the same iterator as above, because a notified listener
Expand Down
75 changes: 58 additions & 17 deletions libs/openFrameworks/types/ofParameter.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class ofAbstractParameter{
public:
virtual ~ofAbstractParameter(){}
virtual std::string getName() const = 0;
virtual void setName(const std::string & name) = 0;
virtual bool setName(const std::string & name) = 0;
virtual std::string toString() const = 0;
virtual void fromString(const std::string & str) = 0;

Expand Down Expand Up @@ -67,11 +67,16 @@ class ofAbstractParameter{

virtual bool isReferenceTo(const ofAbstractParameter& other) const;

virtual ofEvent<std::string>& nameChangedEvent() = 0;

protected:
virtual const ofParameterGroup getFirstParent() const = 0;
virtual void setSerializable(bool serializable)=0;
virtual std::string escape(const std::string& str) const;
virtual const void* getInternalObject() const = 0;



};


Expand Down Expand Up @@ -223,7 +228,7 @@ class ofParameterGroup: public ofAbstractParameter {
friend std::ostream& operator<<(std::ostream& os, const ofParameterGroup& group);

std::string getName() const;
void setName(const std::string& name);
bool setName(const std::string& name);
std::string getEscapedName() const;
std::string toString() const;
void fromString(const std::string& name);
Expand All @@ -245,7 +250,10 @@ class ofParameterGroup: public ofAbstractParameter {
operator bool() const;

ofEvent<ofAbstractParameter> & parameterChangedE();

virtual ofEvent<std::string>& nameChangedEvent();
ofEvent<ofAbstractParameter>& childNameChangedEvent();


std::vector<std::shared_ptr<ofAbstractParameter> >::iterator begin();
std::vector<std::shared_ptr<ofAbstractParameter> >::iterator end();
std::vector<std::shared_ptr<ofAbstractParameter> >::const_iterator begin() const;
Expand All @@ -265,18 +273,27 @@ class ofParameterGroup: public ofAbstractParameter {
:serializable(true){}

void notifyParameterChanged(ofAbstractParameter & param);

bool updateParameterName(const std::string oldName, const std::string newName);
void notifyParameterNameChanged(ofAbstractParameter & param);

std::map<std::string,std::size_t> parametersIndex;
std::vector<std::shared_ptr<ofAbstractParameter> > parameters;
std::string name;
bool serializable;
std::vector<std::weak_ptr<Value>> parents;
ofEvent<ofAbstractParameter> parameterChangedE;
ofEvent<std::string> nameChangedEvent;
ofEvent<ofAbstractParameter> childNameChangedEvent;
};
std::shared_ptr<Value> obj;
ofParameterGroup(std::shared_ptr<Value> obj)
:obj(obj){}


static void checkAndRemoveExpiredParents(std::vector<std::weak_ptr<Value>> & parents);

static bool changeChildName(ofAbstractParameter* child, std::vector<std::weak_ptr<Value>> & parents, const std::string& oldName, std::string newName);

template<typename T>
friend class ofParameter;

Expand Down Expand Up @@ -500,7 +517,7 @@ class ofParameter: public ofAbstractParameter{
const ParameterType * operator->() const;
operator const ParameterType & () const;

void setName(const std::string & name);
bool setName(const std::string & name);
std::string getName() const;

ParameterType getMin() const;
Expand Down Expand Up @@ -580,16 +597,20 @@ class ofParameter: public ofAbstractParameter{
void setParent(ofParameterGroup & _parent);

const ofParameterGroup getFirstParent() const{
obj->parents.erase(std::remove_if(obj->parents.begin(),obj->parents.end(),
[](std::weak_ptr<ofParameterGroup::Value> p){return p.lock()==nullptr;}),
obj->parents.end());
// obj->parents.erase(std::remove_if(obj->parents.begin(),obj->parents.end(),
// [](std::weak_ptr<ofParameterGroup::Value> p){return p.lock()==nullptr;}),
// obj->parents.end());
//
ofParameterGroup::checkAndRemoveExpiredParents(obj->parents);
if(!obj->parents.empty()){
return obj->parents.front().lock();
}else{
return std::shared_ptr<ofParameterGroup::Value>(nullptr);
}
}

virtual ofEvent<std::string>& nameChangedEvent();

size_t getNumListeners() const;
const void* getInternalObject() const;

Expand Down Expand Up @@ -631,6 +652,7 @@ class ofParameter: public ofAbstractParameter{
ParameterType value;
ParameterType min, max;
ofEvent<ParameterType> changedE;
ofEvent<std::string> nameChangedEvent;
bool bInNotify;
bool serializable;
std::vector<std::weak_ptr<ofParameterGroup::Value>> parents;
Expand Down Expand Up @@ -745,10 +767,7 @@ inline void ofParameter<ParameterType>::eventsSetValue(const ParameterType & v){
if(!obj->parents.empty())
{
// Erase each invalid parent
obj->parents.erase(std::remove_if(obj->parents.begin(),
obj->parents.end(),
[](const std::weak_ptr<ofParameterGroup::Value> & p){ return p.expired(); }),
obj->parents.end());
ofParameterGroup::checkAndRemoveExpiredParents(obj->parents);

// notify all leftover (valid) parents of this object's changed value.
// this can't happen in the same iterator as above, because a notified listener
Expand Down Expand Up @@ -817,8 +836,20 @@ inline ofParameter<ParameterType>::operator const ParameterType & () const{
}

template<typename ParameterType>
void ofParameter<ParameterType>::setName(const std::string & name){
bool ofParameter<ParameterType>::setName(const std::string & name){
std::string oldName = getName();
if(escape(name) == escape(oldName)) return false;
if(!ofParameterGroup::changeChildName(this, obj->parents, escape(oldName), escape(name))){
setName(oldName);
return false;
}
obj->name = name;
return true;
}
template<typename ParameterType>
ofEvent<std::string>& ofParameter<ParameterType>::nameChangedEvent()
{
return obj->nameChangedEvent;
}

template<typename ParameterType>
Expand Down Expand Up @@ -998,7 +1029,7 @@ class ofParameter<void>: public ofAbstractParameter{

ofParameter<void>& set(const std::string & name);

void setName(const std::string & name);
bool setName(const std::string & name);
std::string getName() const;

std::string toString() const;
Expand Down Expand Up @@ -1048,6 +1079,9 @@ class ofParameter<void>: public ofAbstractParameter{
const void* getInternalObject() const{
return obj.get();
}

virtual ofEvent<std::string>& nameChangedEvent();

protected:

private:
Expand All @@ -1062,6 +1096,7 @@ class ofParameter<void>: public ofAbstractParameter{

std::string name;
ofEvent<void> changedE;
ofEvent<std::string> nameChangedEvent;
bool serializable;
std::vector<std::weak_ptr<ofParameterGroup::Value>> parents;
};
Expand Down Expand Up @@ -1118,7 +1153,8 @@ class ofReadOnlyParameter: public ofAbstractParameter{
std::string valueType() const;

protected:
void setName(const std::string & name);
virtual ofEvent<std::string>& nameChangedEvent();
bool setName(const std::string & name);
void enableEvents();
void disableEvents();
void setSerializable(bool s);
Expand Down Expand Up @@ -1280,8 +1316,13 @@ inline std::unique_ptr<of::priv::AbstractEventToken> ofReadOnlyParameter<Paramet
}

template<typename ParameterType,typename Friend>
inline void ofReadOnlyParameter<ParameterType,Friend>::setName(const std::string & name){
parameter.setName(name);
inline bool ofReadOnlyParameter<ParameterType,Friend>::setName(const std::string & name){
return parameter.setName(name);
}

template<typename ParameterType,typename Friend>
inline ofEvent<std::string>& ofReadOnlyParameter<ParameterType,Friend>::nameChangedEvent(){
return parameter.nameChangedEvent();
}

template<typename ParameterType,typename Friend>
Expand Down
Loading