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

Author Topic: Mouse Click Selection rectangle  (Read 4573 times)

0 Members and 1 Guest are viewing this topic.

mzad@kol

  • Newbie
  • *
  • Posts: 4
    • View Profile
    • Email
Mouse Click Selection rectangle
« on: May 10, 2020, 09:35:45 pm »
Hi guys, this my first post to this forum. I am trying to learn sfml right now.I am using codeblock as an IDE for c++ ,in windows 10.

I am trying to use left mouse click to draw a selection rectangle box to select multiple sprites/object that lies inside the rectangle box or in touch with the box...
but got stuck at initial stage of rectangle generation.

I have google it and found some example but those were using old version of sfml and are not useful right now and I tried something but even that didn't work out. So have come to this forum to get some help and know-how.
This is a part of my code....
int main()
{
    // Create the main window
    sf::RenderWindow window(sf::VideoMode(800, 600), "SFML window");


    sf::Vector2i CurrentMousePosition;
    sf::Vector2i StartMousePosition;

        // Start the game loop
    while (window.isOpen())
    {
        // Process events
        sf::Event event;
        while (window.pollEvent(event))
        {
            // Close window : exit
            if (event.type == sf::Event::Closed)
                window.close();
           // TRYING RESIZING EVENT
            if (event.type == sf::Event::Resized)
               {
                   // update the view to the new size of the window
                   sf::FloatRect visibleArea(0, 0, event.size.width, event.size.height);
                 //  window.setView(sf::View(visibleArea));
                 // update the view to the new size of the window and keep the center
                    window.setView(sf::View(window.getView().getCenter(), sf::Vector2f((float)event.size.width, (float)event.size.height)));
               }

            if(event.type==sf::Event::MouseButtonEvent(sf::Mouse::Left))
            {
                // left click...
                      StartMousePosition = sf::Mouse::getPosition(window);
                      cout << "LEFT MOUSE BUTTON IS PRESSED ";

            }



            if (event.type == sf::Event::MouseMoved)
            {
                // left click...
                      CurrentMousePosition = sf::Mouse::getPosition(window);
                     //cout << "LEFT MOUSE BUTTON IS PRESSED and MOUSE MOVE";

            }



        }


        // Clear screen
        window.clear();





      // DRAW RECTANGLE WITH MOUSE CLICK
        if( sf::Mouse::isButtonPressed( sf::Mouse::Left ) )
             {
              //   sf::Vector2i StartMousePosition;
              //   StartMousePosition = sf::Mouse::getPosition(window);
                 int x1=0,y1=0,x2=0,y2=0,x3=0,y3=0,x4=0,y4=0 ;
                 x1=StartMousePosition.x;
                 y1=StartMousePosition.y;
                 x3=CurrentMousePosition.x;
                 y3=CurrentMousePosition.y;
                 x2=x3;
                 y2=y1;
                 x4=x1;
                 y4=y3;
                 // draw Rectangle with 4 corner coordinates
                draw_rectangle(window,x1,y1,x2,y2,x3,y3,x4,y4,12,Color1);

             }

        // Update the window
        window.display();
    }



    return EXIT_SUCCESS;
}

 

draw_rectangle is a function created by my, based on sfml.in this function i am using 4 lines to draw rectangle instead of built-in sfml rectangle with width-height. 


Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Mouse Click Selection rectangle
« Reply #1 on: May 10, 2020, 10:33:21 pm »
Which part are you having trouble with?
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

mzad@kol

  • Newbie
  • *
  • Posts: 4
    • View Profile
    • Email
Re: Mouse Click Selection rectangle
« Reply #2 on: May 11, 2020, 09:48:22 am »
I have done some modifications in the code and to some extent its working only few things are required

1) When I click outside the window, even then mousebutton click event is activated.

2) Selection rectangle always start from one single position, whereas it is supposed to start at mouse click position and continue to mousemove/drag position unless mouse button is released.
 My modified codes are
#include <string>
#include <iostream>
#include <SFML/Graphics.hpp>
#include <SFML/Window/Event.hpp>
using namespace std;



int main()
{
    // Create the main window
    sf::RenderWindow window(sf::VideoMode(800, 600), "SFML window");

    sf::Vector2i CurrentMousePosition;
    sf::Vector2i StartMousePosition;
    bool mouse_selection_status=false;
    int mouse_selection_moving=-1;
        // Start the game loop
    while (window.isOpen())
    {

        // Process events
        sf::Event event;
        while (window.pollEvent(event))
        {
            // Close window : exit
            if (event.type == sf::Event::Closed)
                window.close();

            if( (sf::Mouse::isButtonPressed( sf::Mouse::Left )) && (mouse_selection_status==false) && (mouse_selection_moving==-1))
            {
                // left click...
                       mouse_selection_status=true;
                      StartMousePosition = sf::Mouse::getPosition(window);
                      cout << "\nLEFT MOUSE BUTTON IS PRESSED \n";

            }

            if( (sf::Mouse::isButtonPressed( sf::Mouse::Left )) && (mouse_selection_status==true) && (mouse_selection_moving==0)  )
            {
                // left click...
                       mouse_selection_status=-1;
                       mouse_selection_status=false;
                      cout << "\nLEFT MOUSE BUTTON IS Released";

            }



            if ((event.type == sf::Event::MouseMoved) && (mouse_selection_status==true))
            {
                // left click...
                      mouse_selection_moving=1;
                      CurrentMousePosition = sf::Mouse::getPosition(window);

            }




        }


        // Clear screen
        window.clear();


      // DRAW RECTANGLE WITH MOUSE CLICK
        if( sf::Mouse::isButtonPressed( sf::Mouse::Left ) )
             {
                 int x1=0,y1=0,x2=0,y2=0,x3=0,y3=0,x4=0,y4=0 ;
                 x1=StartMousePosition.x;
                 y1=StartMousePosition.y;
                 x3=CurrentMousePosition.x;
                 y3=CurrentMousePosition.y;
                 x2=x3;
                 y2=y1;
                 x4=x1;
                 y4=y3;
                 // draw Rectangle with 4 corner coordinates
                 sf::Color Color1(125,125,75);
                draw_rectangle(window,x1,y1,x2,y2,x3,y3,x4,y4,12,Color1);

             }

        // Update the window
        window.display();
    }



    return EXIT_SUCCESS;
}
 



Following attached  images might be useful to understand what I want.


Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Mouse Click Selection rectangle
« Reply #3 on: May 11, 2020, 10:32:48 am »
You're querying real-time keyboard state in the event loop. That's not recommended, and not useful. There are pressed/released/moved events, that contain all the information that you need (mouse position, clicked button, ...). Your code should work fine after you switch to events only.

Have a look at the documentation, tutorials and examples available. The code in your first post was not correct (regarding mouse events).
Laurent Gomila - SFML developer

mzad@kol

  • Newbie
  • *
  • Posts: 4
    • View Profile
    • Email
Re: Mouse Click Selection rectangle
« Reply #4 on: May 11, 2020, 10:49:41 am »
I have seen the documents but unable to implement/understand the mouse button  release event a small example would be very helpful

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Mouse Click Selection rectangle
« Reply #5 on: May 11, 2020, 10:56:56 am »
https://www.sfml-dev.org/tutorials/2.5/window-events.php#the-mousebuttonpressed-and-mousebuttonreleased-events

There's really nothing more to know about these events. If you still have troubles using them then show some code and explain what problems you have ;)
Laurent Gomila - SFML developer

mzad@kol

  • Newbie
  • *
  • Posts: 4
    • View Profile
    • Email
Re: Mouse Click Selection rectangle
« Reply #6 on: May 11, 2020, 11:21:45 am »
Thanks got it....  :) ........would request the admin to close this post as solved