Fixing position of ofxGui

I have just started using openframeworks. How do I prevent the user from moving a gui? I want it to always be in one position.

Hi, instead of using an ofxPanel instance use on ofxGuiGroup instance and set the position of it.
cheers

1 Like

Thank you! Also, is it possible to prevent the user from changing the value of the variable? (I just want to show them a bar and number which changes as the program runs, not let them change the variable directly)

Hi, sort of.
There is the ofReadOnlyParameter which I think is only implemented in the ofxGui to use strings, thus creating labels, instead of an editable text field.

I made the following class to have a read only slider

Put the following code in a new file named ofxReadOnlySlider.h

//
//  ofxReadOnlySlider.h
//  example
//
//  Created by Roy Macdonald on 7/25/18.
//
//

#pragma once
#include "ofxSlider.h"
#include "ofxBaseGui.h"
#include "ofParameter.h"
#include "ofxInputField.h"

template<typename Type>
class ofxReadOnlySlider: public ofxSlider<Type>{
	friend class ofxPanel;
public:
	
	ofxReadOnlySlider<Type>();

	ofxReadOnlySlider<Type>(ofParameter<Type> _val, float width = 200, float height = 9);
	ofxReadOnlySlider* setup(ofParameter<Type> _val, float width = 200, float height = 9);
	ofxReadOnlySlider* setup(const std::string& sliderName, Type _val, Type _min, Type _max, float width = 200, float height = 9);

	
	
	virtual bool mouseMoved(ofMouseEventArgs & args){return false;}
	virtual bool mousePressed(ofMouseEventArgs & args){return false;}
	virtual bool mouseDragged(ofMouseEventArgs & args){return false;}
	virtual bool mouseReleased(ofMouseEventArgs & args){return false;}
	virtual bool mouseScrolled(ofMouseEventArgs & args){return false;}
	void setShowName(bool showName);
	void showName(){setShowName(true);}
	void hideName(){setShowName(false);}
	bool isShowingName(){return bShowName;}
protected:
	bool bShowName = false;
	virtual void generateText();
	
};

Then put what’s next in a new file named ofxReadOnlySlider.cpp

//
//  ofxReadOnlySlider.cpp
//  example
//
//  Created by Roy Macdonald on 7/25/18.
//
//

#include "ofxReadOnlySlider.h"


template<typename Type>
ofxReadOnlySlider<Type>::ofxReadOnlySlider():ofxSlider<Type>(){}



template<typename Type>
ofxReadOnlySlider<Type>::ofxReadOnlySlider(ofParameter<Type> _val, float width, float height){
	setup(_val,width,height);
}

template<typename Type>
ofxReadOnlySlider<Type>* ofxReadOnlySlider<Type>::setup(ofParameter<Type> _val, float width, float height){
	ofxSlider<Type>::setup(_val, width, height);
	this->unregisterMouseEvents();
	this->thisFillColor =  ofColor::cyan;
	return this;
}

template<typename Type>
ofxReadOnlySlider<Type>* ofxReadOnlySlider<Type>::setup(const std::string& sliderName, Type _val, Type _min, Type _max, float width, float height){
	this->value.set(sliderName,_val,_min,_max);
	return setup(this->value,width,height);
}

template<typename Type>
void ofxReadOnlySlider<Type>::generateText(){
	if(bShowName){
		ofxSlider<Type>::generateText();	
	}else{
	std::string valStr = ofToString((int)this->value.get());
	auto inputWidth = this->getTextBoundingBox(valStr,0,0).width;
//	auto label = getTextBoundingBox(getName(), b.x + textPadding, b.y + b.height / 2 + 4);
	auto value = this->getTextBoundingBox(valStr, this->b.x + this->b.width - this->textPadding - inputWidth, this->b.y + this->b.height / 2 + 4);
	this->overlappingLabel =false;// label.getMaxX() > value.x;
	
	this->textMesh.clear();
	this->textMesh.append(this->getTextMesh(valStr, this->b.x + this->b.width - this->textPadding - this->getTextBoundingBox(valStr,0,0).width, this->b.y + this->b.height / 2 + 4));
	}
}


template<typename Type>
void ofxReadOnlySlider<Type>::setShowName(bool showName){
	bShowName = showName;
	this->setNeedsRedraw();
}



template class ofxReadOnlySlider<int8_t>;
template class ofxReadOnlySlider<uint8_t>;
template class ofxReadOnlySlider<int16_t>;
template class ofxReadOnlySlider<uint16_t>;
template class ofxReadOnlySlider<int32_t>;
template class ofxReadOnlySlider<uint32_t>;
template class ofxReadOnlySlider<int64_t>;
template class ofxReadOnlySlider<uint64_t>;
template class ofxReadOnlySlider<float>;
template class ofxReadOnlySlider<double>;

Then just use in the same way as when using ofxSlider

let me know if how it goes.

cheers

1 Like

btw, I the code I posted above, I was using a default height of 9 as you can see for instance in the following line

ofxReadOnlySlider<Type>(ofParameter<Type> _val, float width = 200, float height = 9);

you might want to change this to something else.