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

Author Topic: Left button mouse but odd behaviour :(  (Read 1545 times)

0 Members and 1 Guest are viewing this topic.

Canvas

  • Full Member
  • ***
  • Posts: 107
    • View Profile
Left button mouse but odd behaviour :(
« on: July 20, 2012, 03:48:39 pm »
---FIXED---

Hey guys, i've just coded myself a very simple button system where you can change colours of a block, now it works but not 100%, if I hold my mouse over my block which is 40 by 40, and hold left click, it will add maybe 5 or 10 to red, but then stop, even if the mouse is still in collision with the block and im holding left click, what I would like it to do is just keeping adding red until I let go or the mouse isnt in collision with that block anymore.

here is my code for the colour adding

if(iEvent.type == sf::Event::MouseButtonPressed == true && iEvent.mouseButton.button == sf::Mouse::Left){leftMouseButton = true;}else{leftMouseButton = false;}
                if(leftMouseButton == true)
                {
                                for(int i=0;i<menuBtns.size();i++)
                                {
                                        if(math.pointInRect(sf::Mouse::getPosition(screen).x,sf::Mouse::getPosition(screen).y,int(menuBtns.at(i).pos.x),int(menuBtns.at(i).pos.y),menuBtns.at(i).pos.w,menuBtns.at(i).pos.h) == true)
                                        {
                                                switch(menuBtns.at(i).rtnID())
                                                {
                                                case 1:                 if(clickTimer == 0){clickTimer = 1; player.setHairId(player.rtnHairId()+1);}
                                                        break;
                                                case 2:                 if(clickTimer == 0){clickTimer = 1; player.setHairId(player.rtnHairId()-1);}
                                                        break;
                                                case 3: player.setColor('r',1); cout << "Red +1" << endl;
                                                        break;
                                                case 4: player.setColor('r',-1); cout << "Red -1" << endl;
                                                        break;
                                                case 5: player.setColor('b',1); cout << "Blue +1" << endl;
                                                        break;
                                                case 6: player.setColor('b',-1); cout << "Blue -1" << endl;
                                                        break;
                                                case 7: player.setColor('g',1); cout << "Green +1" << endl;
                                                        break;
                                                case 8: player.setColor('g',-1); cout << "Green -1" << endl;
                                                        break;
                                                }
                                        }
                                }
                }
 

the math.pointInRect is this piece of code
        bool pointInRect(int pnt_x, int pnt_y, int rect_x, int rect_y, int rect_w, int rect_h)
        {
        if ( (pnt_x >= rect_x) && (pnt_x <= rect_x + rect_w - 1) )
        {
                if ( (pnt_y >= rect_y) && (pnt_y <= rect_y + rect_h - 1) )
                {return true;}
        }
        return false;
        }
 

any reasons why it doesnt keep just adding?

ok for my code I have just changed this
if(math.rectTorectCollision(sf::Mouse::getPosition(screen).x,sf::Mouse::getPosition(screen).y,1,1,int(menuBtns.at(i).pos.x),int(menuBtns.at(i).pos.y),menuBtns.at(i).pos.w,menuBtns.at(i).pos.h) == true)
now it uses my rectTorectCollision which is this
        bool rectTorectCollision(int rect1_x, int rect1_y, int rect1_w, int rect1_h,
                                                                int rect2_x, int rect2_y, int rect2_w, int rect2_h)    
        {
if ( pointInRect(rect1_x, rect1_y, rect2_x, rect2_y, rect2_w, rect2_h) ){return true;}
        if ( pointInRect(rect1_x + rect1_w - 1, rect1_y, rect2_x, rect2_y, rect2_w, rect2_h) ){return true;}
        if ( pointInRect(rect1_x + rect1_w - 1, rect1_y + rect1_h - 1, rect2_x, rect2_y, rect2_w, rect2_h) ){return true;}
        if ( pointInRect(rect1_x, rect1_y + rect1_h - 1, rect2_x, rect2_y, rect2_w, rect2_h) ){return true;}
        if ( pointInRect(rect2_x, rect2_y, rect1_x, rect1_y, rect1_w, rect1_h) ){return true;}
        if ( pointInRect(rect2_x + rect2_w - 1, rect2_y, rect1_x, rect1_y, rect1_w, rect1_h) ){return true;}
        if ( pointInRect(rect2_x + rect2_w - 1, rect2_y + rect2_h - 1, rect1_x, rect1_y, rect1_w, rect1_h) ){return true;}
        if ( pointInRect(rect2_x, rect2_y + rect2_h - 1, rect1_x, rect1_y, rect1_w, rect1_h) ){return true;}
        return false;
        }
 
now if i hold left click it will keep adding red but if i move it will stop, how can i get it to keep going? :(
lol once again i have created a useless topic because i have fixed it once again...
in my rectorectcollision i put this at the top of my code

if(iEvent.type == sf::Event::MouseButtonPressed == true && iEvent.mouseButton.button == sf::Mouse::Left){leftMouseButton = true;}
                if(iEvent.type == sf::Event::MouseButtonReleased == true && iEvent.mouseButton.button == sf::Mouse::Left){leftMouseButton = false;}
 
it now works fine, it seems that when you press left click, after about a second it will just go back to 0, it seems the framerate would reset it, so now i just put another if statement and it works.
« Last Edit: July 20, 2012, 03:59:07 pm by Canvas »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11033
    • View Profile
    • development blog
    • Email
Re: Left button mouse but odd behaviour :(
« Reply #1 on: July 20, 2012, 04:02:31 pm »
The mousebuttonpressed event gets only triggered once, when you press the button. You could work with the corresponding MouseButtonReleased, but this would only complicate things. There's the easy to use sf::Mouse object which has a function to check wether a button is pressed or not.
Also you should keep inmind that a sf::Rect already has a function contains() that checks if a point is in a rectangle. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Canvas

  • Full Member
  • ***
  • Posts: 107
    • View Profile
Re: Left button mouse but odd behaviour :(
« Reply #2 on: July 20, 2012, 04:24:23 pm »
I did see that eXpl0it3r for the sf::Rect but i had a maths class already, so i thought i would just use that, what happened at the beginning i was using SDL, then i moved to SFML as its just...well alot better and easier to use, so i had to change alot of my code from SDL to SFML, but i wanted to keep some parts so i kept my own rect class :)

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11033
    • View Profile
    • development blog
    • Email
Re: Left button mouse but odd behaviour :(
« Reply #3 on: July 20, 2012, 04:34:57 pm »
That's okay. ;)
I just thought maybe you didn't see/know it.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/