PDA

View Full Version : How to build a Start/Stop Button




hoschi
Oct 13, 2005, 02:25 PM
I am using a normal cocoa opengl program in a double buffer mode, in order to run something... I use a button to start the program running and the program stops only when it has finished processing itself completely. I was wondering if it is possible to have this start button convert itself into pause button during the time when its running... and have another button for stopping the application and starting again..

Ok so there are two things..

1. How to convert this start button into a start/Pause button ... say something like a ball moving on the floor just pauses when re-press the button and moves again as I press it once more...-- this application which I am using the button for is a cocoa opengl program and also uses some c++ libraries..

2. I want to have another button which can stop the application completely. This would not quite is but just stop it. Say a ball moving on the floor is returned to its initial state so that it can restart.



HiRez
Oct 13, 2005, 07:54 PM
1. How to convert this start button into a start/Pause button ... say something like a ball moving on the floor just pauses when re-press the button and moves again as I press it once more...-- this application which I am using the button for is a cocoa opengl program and also uses some c++ libraries..If you're just using a Cocoa NSButton, sure, just set the alternate title and/or alternate image and set the button behavior to "Toggle" (you will have to choose a button type other than the default). Then just check the state in your action method to decide which action to take in your app.

2. I want to have another button which can stop the application completely. This would not quite is but just stop it. Say a ball moving on the floor is returned to its initial state so that it can restart.I don't really understand what you're asking here...

hoschi
Oct 13, 2005, 08:30 PM
If you're just using a Cocoa NSButton, sure, just set the alternate title and/or alternate image and set the button behavior to "Toggle" (you will have to choose a button type other than the default). Then just check the state in your action method to decide which action to take in your app.

I don't really understand what you're asking here...

What I mean is that I want a button that merely stops the whole application and brings it to its initial state..

caveman_uk
Oct 14, 2005, 03:19 AM
What I mean is that I want a button that merely stops the whole application and brings it to its initial state..
This is just another button. Not difficult. The main thing to worry about is that if you're program is computation intensive is that you do it right so as not to lock up the GUI. Check out threads.

You sound like you don't have much cocoa experience. Maybe you need to get a bit more familiar with it through simpler projects first?

AlmostThere
Oct 14, 2005, 02:46 PM
Your pause button should just set an internal variable (e.g. bool) so there is no update when you are calculating new positions for your moving objects. Just cache the time when the update function is called until your app is ready to un-pause, at which point you can start calculating the new positions again.

Your reset button should just call a function that sets your objects to some saved or initial state.

hoschi
Oct 14, 2005, 03:28 PM
Your pause button should just set an internal variable (e.g. bool) so there is no update when you are calculating new positions for your moving objects. Just cache the time when the update function is called until your app is ready to un-pause, at which point you can start calculating the new positions again.

Your reset button should just call a function that sets your objects to some saved or initial state.

To be moe specific now...I am posting this program I wrote sometime back, which makes a triangle using OpenGl. There are three text boxes, where the user can enter the initial velocities for each of the three triangle nodes. Then I specify the number of time steps this triangle should redraw itself doing what I ask it to do in the program (which normally is to change its angle randomly and move eaach node). I am using a button to start this process after I specify the velocities and the timesteps. This button is a "toggle" type NSButton which has its alternate title set to Pause. however once I start the program and once the program start redrawing the triangle, I cannot stop it in between. I am looking for ways in which I might be able to discontinue this operation/program by pressing the button again when it displays the "pause" state --------

#import "triangle.h"
#include <stdlib.h>
#include <time.h>
#include <OpenGL/gl.h>


@implementation triangle

- (id) initWithCoder: (NSCoder *) coder
{
NSOpenGLPixelFormatAttribute attrs[] =
{
NSOpenGLPFADoubleBuffer, nil
};

NSOpenGLPixelFormat* pixFmt ;
long swapInterval ;

self = [super initWithCoder: coder] ;
ver1.x = 0.556;
ver1.y = 0.2;
ver2.x = -0.343;
ver2.y = 0.443;
ver3.x = 0.02;
ver3.y = 0.66;
vel1 = 0.;
vel2 = 0.;
vel3 = 0.;
steps = 0;
flipped = NO;
a1 = time(NULL);
r = 0.0;
g = 1.0;
b = 0.0;
angle1 = 2*3.14*[self generateNumber];
angle2 = 2*3.14*[self generateNumber];
angle3 = 2*3.14*[self generateNumber];



pixFmt = [[NSOpenGLPixelFormat alloc] initWithAttributes: attrs] ;
[self setPixelFormat: pixFmt] ;
swapInterval = 1 ;
[[self openGLContext] setValues: &swapInterval forParameter: NSOpenGLCPSwapInterval ] ;
return self ;
}

-(void) awakeFromNib
{
[velocity1 setFloatValue:vel1];
[velocity2 setFloatValue:vel2];
[velocity3 setFloatValue:vel3];
[timesteps setFloatValue:steps];
}

-(float)generateNumber
{
srand(a1);
int a2,b2;
float c;
a2 = rand();
a1 = a2;
b2 = RAND_MAX;
float tempa, tempb;
tempa = a2;
tempb = b2;
c = tempa/tempb;
return c;
}


-(NSPoint)boundaryCondition:(NSPoint)var
{
NSPoint tempPoint;
tempPoint.x = var.x;
tempPoint.y = var.y;

if (tempPoint.x < -1.0)
{tempPoint.x = tempPoint.x +2.0;
flipped = YES;}
if (tempPoint.x > 1.0)
{ tempPoint.x = tempPoint.x -2.0;
flipped = YES;}

if (tempPoint.y < -1.0)
{ tempPoint.y = 2.0+ tempPoint.y;
flipped = YES;}
if (tempPoint.y > 1.0)
{ tempPoint.y = tempPoint.y - 2.0;
flipped = YES;}

return tempPoint;
}

- (IBAction)move:(id)sender
{
vel1 = [velocity1 floatValue];
vel2 = [velocity2 floatValue];
vel3 = [velocity3 floatValue];
steps = [timesteps floatValue];
int t;

for (t=0; t<=steps; t++)
{
float angle, interval;
flipped = NO;
interval =0.005;

NSLog(@"Time Step %d", t);

angle1 += [self generateNumber]*0.01;
ver1.x = ver1.x + interval*vel1*cos(angle1);
ver1.y = ver1.y + interval*vel1*sin(angle1);
ver1 = [self boundaryCondition:ver1];

angle2 += [self generateNumber]*0.01;
ver2.x = ver2.x + interval*vel2*cos(angle2);
ver2.y = ver2.y + interval*vel2*sin(angle2);
ver2 = [self boundaryCondition:ver2];

angle3 += [self generateNumber]*0.01;
ver3.x = ver3.x + interval*vel3*cos(angle3);
ver3.y = ver3.y + interval*vel3*sin(angle3);
ver3 = [self boundaryCondition:ver3];

[self lockFocus];
[self drawRect:[self frame]];
[self unlockFocus];
}
}

-(void)drawRect:(NSRect)rect
{
if (flipped == YES) {
r = [self generateNumber];
b = [self generateNumber];
g = [self generateNumber];
}

glClearColor( 0, 0, 0, 0 ) ;
glClear( GL_COLOR_BUFFER_BIT ) ;
glColor3f( r,g,b ) ;
glBegin( GL_TRIANGLES) ;
{
glVertex3f( ver1.x, ver1.y, 0.0 ) ;
glVertex3f( ver2.x, ver2.y, 0.0 ) ;
glVertex3f( ver3.x , ver3.y, 0.0 ) ;
}
glEnd() ;
glFinish() ;
[[self openGLContext] flushBuffer];
}
@end

AlmostThere
Oct 14, 2005, 04:42 PM
Can I suggest you (as a starting point) have a look at NSTimer.

http://developer.apple.com/documentation/Cocoa/Conceptual/Timers/index.html

hoschi
Oct 20, 2005, 03:54 PM
Can I suggest you (as a starting point) have a look at NSTimer.

http://developer.apple.com/documentation/Cocoa/Conceptual/Timers/index.html

Thanks for the incredible help... the program is running nice and good... Not only have I incorporated the toggle button but I was also able to solve some other problems...