SFML community forums

Help => System => Topic started by: Chiggy_Playz on April 16, 2019, 07:08:15 pm

Title: Detecting a mouse click inside a "Not Defined" Rectangle.
Post by: Chiggy_Playz on April 16, 2019, 07:08:15 pm
It sounds weird but i have created a big box with three rectangles. The three rectangles are shown in picture.
their starting points are :
1.Halfway of the box vertically.                                        Size :  750,200  (Purple colored)
2.Halfway of the box horizontally                                     Size :  200,600  (Red colored)
3.In the middle where the 2 rectangles collide                  Size :  250,200  (Orange colored)

now i have made a grid for TIC TAC TOE  without defining all the 9 cells one by one. Now I have detect when the user clicks on cell 1 (look at the picture) and so on. How can I detect that?
Title: Re: Detecting a mouse click inside a "Not Defined" Rectangle.
Post by: G. on April 16, 2019, 07:19:32 pm
If you have 9 rectangular areas to detect then make 9 rectangular areas?
You can create an sf::FloatRect for area 1, another for area 2, another for area 3, etc. and check in which rect the mouse is.
Title: Re: Detecting a mouse click inside a "Not Defined" Rectangle.
Post by: Chiggy_Playz on April 16, 2019, 07:25:09 pm
If you have 9 rectangular areas to detect then make 9 rectangular areas?
You can create an sf::FloatRect for area 1, another for area 2, another for area 3, etc. and check in which rect the mouse is.

I read the actual documentation but I still dont understand how it is used. Can you explain me please?
Title: Re: Detecting a mouse click inside a "Not Defined" Rectangle.
Post by: G. on April 16, 2019, 07:35:39 pm
For example
sf::FloatRect area1(50, 400, 250, 200);
// 50 and 400 are the x and y, 250 and 200 are the width and height, approximately, I don't know your real values
sf::FloatRect area2(300, 400, 250, 200);
// etc.
then (using loc_x and loc_y like in your other thread (https://en.sfml-dev.org/forums/index.php?topic=25281))
if (area1.contains(loc_x, loc_y)) {
   std::cout << "1 clicked" << endl;
}
if (area2.contains(loc_x, loc_y)) {
   std::cout << "2 clicked" << endl;
}
//etc.
(and then you could possibly modify it to use an sf::Vector or array and not use 9 if, and maybe use an sf::Event)

I don't understand, is there any reason why you would absolutely want to do it "without defining all the 9 cells one by one"? Because your 3 rectangles thing is weird as hell.

Alternatively, if you really don't want to define the 9 cells there is a way to compute what cells you're on.
Get the mouse position relative to the big rectangle surrounding all 9 cells.
Divide mouse x position by (big rectangle width / 3) and floor it to get the x position of the cell
Divide mouse y position by (big rectangle height / 3) and floor it to get the y position of the cell
Then if you want, do y * 3 + x + 1 to get the cell number
Title: Re: Detecting a mouse click inside a "Not Defined" Rectangle.
Post by: G. on April 17, 2019, 12:12:41 am
You can get the bounding rectangle of an sf::RectangleShape with getGlobalBounds (https://www.sfml-dev.org/documentation/2.5.1/classsf_1_1Shape.php#ac0e29425d908d5442060cc44790fe4da).
Why the PM instead of asking here?