Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Detecting a mouse click inside a "Not Defined" Rectangle.  (Read 5864 times)

0 Members and 1 Guest are viewing this topic.

Chiggy_Playz

  • Newbie
  • *
  • Posts: 15
    • View Profile
Detecting a mouse click inside a "Not Defined" Rectangle.
« 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?

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: Detecting a mouse click inside a "Not Defined" Rectangle.
« Reply #1 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.

Chiggy_Playz

  • Newbie
  • *
  • Posts: 15
    • View Profile
Re: Detecting a mouse click inside a "Not Defined" Rectangle.
« Reply #2 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?

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: Detecting a mouse click inside a "Not Defined" Rectangle.
« Reply #3 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)
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
« Last Edit: April 16, 2019, 07:45:58 pm by G. »

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: Detecting a mouse click inside a "Not Defined" Rectangle.
« Reply #4 on: April 17, 2019, 12:12:41 am »
You can get the bounding rectangle of an sf::RectangleShape with getGlobalBounds.
Why the PM instead of asking here?

 

anything