View Single Post
Old 03-19-2003, 07:46 PM   #11 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
Lightbulb WAAAAA lololololol

And finally the answer to your original question wich was: How to do stuff when a button is pressed with a mouseclick.

Re-download the main.cpp file at given previous link and jolly run it!!!

Also lots of comments added to help understand and move on. The code is also completely reviewed to correct a few ugly comments.
When running the program, click on the upper left square a few times and enjoy .

Press esc key and click twice with mouse to end program.

For the sake of completeness, I post the final code below.
Hehe, I had fun fun fun with CMU though totally useless for me :p .
Watch out for the comments. Tabs are messed up. Too much work on organizing it. Downloadable main.cpp file is obviously correct.
-----------------
-----------------

Code:
#include <iostream.h>
#include <CMUgraphics.h>
#include <string.h>


//These variables are added by Valmont. This is by no means correct programming but it will do fine for now.
char chKeyData;
int ix,iy;//This is a bad naming example... What does ix and iy?
bool bQuit=false;//...but this one is nice. You have a clear hint of what it means and wich type it is (b=boolean).
bool bOriginalColorState =true;//This one will show its purpose once you see when it is used.Naming is good but placement is conceptual bad.

int main()
{
	//****************
	// Variables    **
	int sizebutx = 35;//Idea: Call it "intButtonSizeX".
	int counter2 = 0;//Idea: "intColCounter"
	int counter = 0;//...and so forth.
	int sizebuty = 35;
	int spacebutx = 17;
	int spacebuty = 34;
	
	int buttonx1 = (20+spacebutx);
	int buttony1 = (500-spacebuty);
	int buttonx2 = (20+spacebutx+sizebutx);
	int buttony2 = (500-spacebuty-sizebuty);
		
	//*******************
	window testWindow(650,520,0,0);//520 was 500, looks more friendly like this.
	testWindow.SetPen(RED);
	testWindow.SetBrush(BLUE);
	testWindow.DrawRectangle(20,20,300,500,FILLED);
	
	testWindow.SetPen(GREEN);
	testWindow.SetBrush(RED);

	//Start drawing the buttons on testWindow.
	do
	{
		
		//Draws Columns
		counter2++;
		do
		{
			//Draws Rows
			testWindow. DrawRectangle(buttonx1,buttony1,buttonx2,buttony2,FILLED);
			buttonx1 = buttonx1+sizebutx+spacebutx;
			buttonx2 = buttonx2+sizebutx+spacebutx;
			counter++;
		}
		while (counter < 5);
		
		buttony1 = buttony1-sizebuty-spacebuty;
		buttony2 = buttony2-spacebuty-sizebuty;
		buttonx1 = (20+spacebutx);
		buttonx2 = (20+spacebutx+sizebutx);
		counter = 0;
	}
	while (counter2 < 5);

	//By Valmont:
	//We start here with the ESC option and mouseclick demo. YOU MUST PRESS
	//ESC THEN 2 TIMES LEFT CLICK TO END PROGRAM ENTIRELY!!
	
	// Flush out the input queues before beginning
    testWindow.FlushMouseQueue();
    testWindow.FlushKeyQueue();
	//Few vars to demonstrate co-ordinates in window title.
	char chNum[5]; //Make a char for upcomimg int-string conversion. This is also a demonstration of where to declare a variable like this one.
	string szX,szY,szComb;//Use strings to make a pretty output.And ChangeTitle() method demands a string.
	
	//Start and run main events loop until esc key is pressed. bool value bQuit will be true then. It is now false.
	do
	{
		if(testWindow.GetKeyPress(chKeyData) == ESCAPE) //Is the esc key pressed?
		{
			bQuit=true; //yes, the esc key is pressed.
			break;//Get out of loop then. So click twice to end program entirely.
		}
		
		testWindow.WaitMouseClick(ix,iy);//Right mouse button or left one... doesn't matter, but program will not move on until a mouseclick OR the ESC key is pressed.
		
		itoa(ix,chNum,10); //conversion from int to char. Find more about itoa on the net if u like.
		szX=chNum;//auto conversion from char to string. Must do since ChangeTitle needs a string, NOT a char*!
		itoa(iy,chNum,10);
		szY=chNum;

		szComb="Coordinates are: ("+szX+","+szY+")";//Coordinates are: (szX,szY)
		testWindow.ChangeTitle(szComb); //Update the current windows title area.

		//AND FINALLY THE SOLUTION TO YOUR ORIGINAL PROBLEM!!!
		//Keep clicking on first square.

		//If mouseclick was on first square (upper left) then do fun stuff.
		if(ix>36 && ix<73 && iy>154 && iy<191)//The first square I did for you. You need to do the rest of the squares.
		{
			if (bOriginalColorState)
			{
				testWindow.SetPen(RED);
				testWindow.SetBrush(GREEN);
				testWindow.DrawRectangle(37,155,72,190);
				bOriginalColorState = false;
			}
			else
			{
				testWindow.SetPen(GREEN);
				testWindow.SetBrush(RED);
				testWindow.DrawRectangle(37,155,72,190);
				bOriginalColorState = true;
			}
		}
	}
	while (!bQuit); //And finally we exit the ESC loop because the ESC key was pressed.	

//End by Valmont.
return 0;
}
Valmont is offline   Reply With Quote