I try to write my first (GUI) classes (or port them from ofxOfelia).
Here is for example the toggle class:
#include "toggle.h"
toggle::toggle(){
}
void toggle::setup(float sendXpos, float sendYpos, float sendSize){
xpos = sendXpos;
ypos = sendYpos;
size = sendSize;
isTouch = false;
value = false;
}
void toggle::update(){
}
void toggle::draw(){
ofPushMatrix();
ofPushStyle();
ofTranslate(xpos, ypos);
ofSetColor(0);
ofDrawRectangle(0, 0, size, size);
ofSetColor(255);
ofDrawRectangle(size / 40, size / 40, size - size / 20, size - size / 20);
ofSetColor(0);
if (value) {
ofSetLineWidth(size / 40);
ofDrawLine(size / 40, size / 40, size - size / 40, size - size / 40);
ofDrawLine(size - size / 40, size / 40, size / 40, size - size / 40);
}
ofPopMatrix();
ofPopStyle();
}
float toggle::mousePressed(float x, float y){
if (x > xpos && x < xpos + size && y > ypos && y < ypos + size && !isTouch){
value = !value;
return value;
}
else return -1;
}
float toggle::touchDown(float x, float y){
if (x > xpos && x < xpos + size && y > ypos && y < ypos + size){
isTouch = true;
value = !value;
return value;
}
else return -1;
}
#ifndef _TOGGLE // if this class hasn't been defined, the program can define it
#define _TOGGLE // by using this if statement you prevent the class to be called more than once which would confuse the compiler
#include "ofMain.h" // we need to include this to have a reference to the openFrameworks framework
class toggle {
public: // place public functions or variables declarations here
// methods, equivalent to specific functions of your class objects
void setup(float xpos, float ypos, float size); // setup method, use this to setup your object's initial state
void update(); // update method, used to refresh your objects properties
void draw(); // draw method, this where you'll do the object's drawing
float mousePressed(float x, float y);
float touchDown(float x, float y);
// variables
float xpos;
float ypos;
float size;
bool isTouch;
bool value;
toggle(); // constructor - used to initialize an object, if no properties are passed the program sets them to the default value
private: // place private functions or variables declarations here
}; // don't forget the semicolon!
#endif
The idea is, that it only returns a value if the mouse clicks inside the toggle. But it seems, with C++ I always have to return a value with non void functions…
Whats the best way to filter the wrong return values (currently it updates the slider with -1 if I click outside the toggle)?
My second question is: How I can put all the GUI elements in one class (or doesnt it make sense?)?
And because its my first class: Are there some things in my code that I do terribly wrong?