Not returning something if the condition is not met

I try to write my first (GUI) classes (or port them from ofxOfelia).
Here is for example the toggle class:

#include "toggle.h"



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(){
	ofTranslate(xpos, ypos);
	ofDrawRectangle(0, 0, size, size);
	ofDrawRectangle(size / 40, size / 40, size - size / 20, size - size / 20);
	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);

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!

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?

I found a good solution with ofNotifyEvent, will post the result soon…

So, here are my first 5 OF GUI elements:

Hi, yes, you need to use ofEvents for that rather than a function.
Check the ofEvents examples.
Imagine that you have a bunch of elements in your gui, if you had this model where you use the return of the function, you would need to be constantly checking for ALL your gui elements, calling this function on each, and if it returned a value then take action. That becomes really easily into a nightmare.

Read this
I wrote it several years ago but for some reason never made it into the book.

1 Like