Hi
At the moment I have written a bunch of Button classes for iOS use.
My Button class has a TouchDown,TouchUp method which I then call in the TestApp like this:
#include "testApp.h"
//--------------------------------------------------------------
void testApp::setup(){
// initialize the accelerometer
ofxAccelerometer.setup();
//If you want a landscape oreintation
//iPhoneSetOrientation(OFXIPHONE_ORIENTATION_LANDSCAPE_RIGHT);
ofBackground(127,127,127);
mygButton =gButton(20, 20, 80);
}
//--------------------------------------------------------------
void testApp::update(){
}
//--------------------------------------------------------------
void testApp::draw(){
mygButton.draw();
}
//--------------------------------------------------------------
void testApp::exit(){
}
//--------------------------------------------------------------
void testApp::touchDown(ofTouchEventArgs & touch){
mygButton.touchDown(touch.x, touch.y);
}
//--------------------------------------------------------------
void testApp::touchMoved(ofTouchEventArgs & touch){
mygButton.touchDown(touch.x, touch.y);
}
//--------------------------------------------------------------
void testApp::touchUp(ofTouchEventArgs & touch){
mygButton.touchUp(touch.x, touch.y);
}
As I am always going to want to use the .touchDown and touchUp methods for every mygButton that I create is there a way to parse the touch events into my button class so I can just instanstiate a mygbutton and then call mygbutton.draw without having to call the touchdown and touch up methods by writing them into the relavent sections in the testApp.mm
Here is my gButton .cpp
#include "gButton.h"
gButton::gButton()
{
}
gButton::gButton(int _x, int _y, int _size)
{
x=_x;
y=_y;
height= _size;
width= _size;
has_switched = false;
}
gButton::gButton(int _x, int _y, int _height, int _width)
{
x=_x;
y=_y;
height = _height;
width =_width;
has_switched = false;
}
//----------------------------------------------------------
void gButton::draw()
{
if (button_state==0) {
ofSetColor(100, 0, 100); // color for state one
} else ofSetColor(0, 100, 0); // color for state two
ofRect(x, y, width, height); // what our button will look like i.e. a rectangle
}
//----------------------------------------------------------
void gButton::touchDown(float _xPos,float _yPos)
{
xPos=_xPos;
yPos=_yPos;
//cout<<"x="<<x<<endl;
if (has_switched) {
return;
}
if (xPos>x&&xPos<x+width&&yPos>y&&yPos<y+height) { // check bounds
button_state = !button_state;
has_switched = true;
}
cout<<"button state="<<button_state<<endl;
}
//----------------------------------------------------------
void gButton::touchUp(float _xPos, float _yPos) {
//reset all the buttons...
has_switched = false;
}
I am not trying to do this out of lazyness just curious to see if there is a better way to embed the touch functions that I will always be using into my button class.
I am obviously a beginner to open frameworks and C++ any guidance appreciated
Thanks
GEoff