I try to optimize a simple button class, and like to ask if there is something I am missing, for example unregistering the events or a clear method like in the old version (but it seems to work fine so far).
Maybe unregistering like this:
bang::~bang() {
ofRemoveListener(ofEvents().mousePressed, this, &bang::mousePressed);
ofRemoveListener(ofEvents().touchDown, this, &bang::touchDown);
}
And do I need this:
#ifndef _BANG
#define _BANG
#endif
In the bang.h file?
This is my optimization attempt:
#include "bang.h"
void bang::setup(float _xpos, float _ypos, float _size){
ofAddListener(ofEvents().mousePressed, this, &bang::mousePressed);
ofAddListener(ofEvents().touchDown, this, &bang::touchDown);
xpos = _xpos;
ypos = _ypos;
size = _size;
isTouch = false;
}
void bang::draw(){
ofPushMatrix();
ofPushStyle();
ofTranslate(xpos, ypos);
ofSetCircleResolution(100);
ofSetColor(200);
ofDrawRectangle(0, 0, size, size);
ofSetColor(100);
ofDrawRectangle(size / 40, size / 40, size - size / 20, size - size / 20);
ofSetColor(255, 200, 200);
if (now < ofGetElapsedTimeMillis()){
ofSetColor(200);
}
ofDrawCircle(size / 2, size / 2, size / 2.7);
ofPopMatrix();
ofPopStyle();
}
void bang::mousePressed(ofMouseEventArgs & args){
if (ofDist(args.x, args.y, xpos + size / 2, ypos + size / 2) <= size / 2.5 && !isTouch){
now = ofGetElapsedTimeMillis() + 200;
value = true;
}
}
void bang::touchDown(ofTouchEventArgs & args){
if (ofDist(args.x, args.y, xpos + size / 2, ypos + size / 2) <= size / 2.5){
isTouch = true;
now = ofGetElapsedTimeMillis() + 200;
value = true;
}
}
#include "ofMain.h"
class bang {
public:
void setup(float xpos, float ypos, float size);
void draw();
void mousePressed(ofMouseEventArgs & args);
void touchDown(ofTouchEventArgs & args);
float xpos;
float ypos;
float size;
float now;
bool value;
bool isTouch;
};
And this is the old version:
#include "bang.h"
bang::bang(){
bRegisteredEvents = false;
}
bang::~bang() {
clear();
}
void bang::setup(float sendXpos, float sendYpos, float sendSize){
if(!bRegisteredEvents) {
ofRegisterMouseEvents(this);
ofRegisterTouchEvents(this);
bRegisteredEvents = true;
}
xpos = sendXpos;
ypos = sendYpos;
size = sendSize;
isTouch = false;
}
void bang::update(){
}
void bang::draw(){
ofPushMatrix();
ofPushStyle();
ofTranslate(xpos, ypos);
ofSetCircleResolution(100);
ofSetColor(200);
ofDrawRectangle(0, 0, size, size);
ofSetColor(100);
ofDrawRectangle(size / 40, size / 40, size - size / 20, size - size / 20);
ofSetColor(255, 200, 200);
// ofDrawCircle(size / 2, size / 2, size / 2.5);
if (now < ofGetElapsedTimeMillis()){
ofSetColor(200);
}
ofDrawCircle(size / 2, size / 2, size / 2.7);
ofPopMatrix();
ofPopStyle();
}
void bang::clear() {
if(bRegisteredEvents) {
ofUnregisterMouseEvents(this);
ofUnregisterTouchEvents(this);
bRegisteredEvents = false;
}
}
void bang::mouseMoved(ofMouseEventArgs & args){
}
void bang::mouseDragged(ofMouseEventArgs & args){
}
void bang::mousePressed(ofMouseEventArgs & args){
if (ofDist(args.x, args.y, xpos + size / 2, ypos + size / 2) <= size / 2.5 && !isTouch){
now = ofGetElapsedTimeMillis() + 200;
value = true;
ofNotifyEvent(onMousePressed, value, this);
}
}
void bang::mouseReleased(ofMouseEventArgs & args){
}
void bang::mouseScrolled(ofMouseEventArgs & args){
}
void bang::mouseEntered(ofMouseEventArgs & args){
}
void bang::mouseExited(ofMouseEventArgs & args){
}
void bang::touchDown(ofTouchEventArgs & args){
if (ofDist(args.x, args.y, xpos + size / 2, ypos + size / 2) <= size / 2.5){
isTouch = true;
now = ofGetElapsedTimeMillis() + 200;
value = true;
ofNotifyEvent(onMousePressed, value, this);
}
}
void bang::touchMoved(ofTouchEventArgs & args){
}
void bang::touchUp(ofTouchEventArgs & args){
}
void bang::touchDoubleTap(ofTouchEventArgs & args){
}
void bang::touchCancelled(ofTouchEventArgs & args){
}
#ifndef _BANG
#define _BANG
#include "ofMain.h"
class bang {
public:
bang();
~bang();
void setup(float xpos, float ypos, float size);
void update();
void draw();
void clear();
void mouseMoved(ofMouseEventArgs & args);
void mouseDragged(ofMouseEventArgs & args);
void mousePressed(ofMouseEventArgs & args);
void mouseReleased(ofMouseEventArgs & args);
void mouseScrolled(ofMouseEventArgs & args);
void mouseEntered(ofMouseEventArgs & args);
void mouseExited(ofMouseEventArgs & args);
void touchDown(ofTouchEventArgs & args);
void touchMoved(ofTouchEventArgs & args);
void touchUp(ofTouchEventArgs & args);
void touchDoubleTap(ofTouchEventArgs & args);
void touchCancelled(ofTouchEventArgs & args);
float xpos;
float ypos;
float size;
float now;
bool value;
bool isTouch;
ofEvent<bool> onMousePressed;
protected:
bool bRegisteredEvents;
};
#endif