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

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - mzad@kol

Pages: [1]
1
General / Re: Mouse Click Selection rectangle
« on: May 11, 2020, 11:21:45 am »
Thanks got it....  :) ........would request the admin to close this post as solved

2
General / Re: Mouse Click Selection rectangle
« 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

3
General / Re: Mouse Click Selection rectangle
« 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.


4
General / 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. 


Pages: [1]