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

Author Topic: Touch Button position Problem  (Read 1992 times)

0 Members and 1 Guest are viewing this topic.

Jan666

  • Jr. Member
  • **
  • Posts: 64
    • View Profile
Touch Button position Problem
« on: May 11, 2022, 12:57:02 pm »
Hi, i search a solution maybe someone can help. I have three Buttons for a Controller on mobile phone. They turn the Color when i touch them(2 changing Sprites). The Problem is that the Touch not directly React on the Sprite just next to it or just on the half Sprite, how can i change this. Here is my Code. They are 3 Files main.cpp, buttons.h. buttons.cpp:

I think Buttons.cpp is just important fot the problem:


using namespace sf;
using namespace std;

float btnScaleX = 3.0f;
float btnScaleY = 2.6f;
float btnPosX = 100;
float btnLeftPosY = 200;
float btnRightPosY = 500;
float btnFirePosY =1300;
Vector2f btnScaler (btnScaleX, btnScaleY);
Vector2f btnPosLeft(btnPosX, btnLeftPosY);
Vector2f btnPosRight(btnPosX, btnRightPosY);
Vector2f btnPosFire(btnPosX, btnFirePosY);
FloatRect btnRect1;
FloatRect btnRect2;
FloatRect btnRect3;



buttons::buttons()
{
       
}

buttons::buttons(string btnPng, string btnPng2)

{
       
        btnText.loadFromFile(btnPng);
        btnText2.loadFromFile(btnPng2);

       
       
}

 void buttons::btnInit()
 
 {
 
        btnSprite1.setScale(btnScaler);
        btnSprite2.setScale(btnScaler);
        btnSprite3.setScale(btnScaler);
       
       
       
       
        btnSprite1.setPosition(btnPosLeft);
        btnSprite2.setPosition(btnPosRight);   
     btnSprite3.setPosition(btnPosFire);
     
     btnSprite3.getTextureRect(btnPosFire);
                       
 }
 
 void buttons::btnTouch(){
                       
btnRect1 = btnSprite1.getGlobalBounds();
                               
if(btnRect1.contains(::sf::Touch::getPosition(0).x, ::sf::Touch::getPosition(0).y)){


        btnSprite1.setTexture(btnText2);       
        }
        else{          
                btnSprite1.setTexture(btnText);
        }  
   
btnRect2 = btnSprite2.getGlobalBounds();
                       
if(btnRect2.contains(::sf::Touch::getPosition(0).x, ::sf::Touch::getPosition(0).y)){


        btnSprite2.setTexture(btnText2);       
        }
        else{          
                btnSprite2.setTexture(btnText);
        }  
       
btnRect3 = btnSprite3.getGlobalBounds();

                       
if(btnRect3.contains(::sf::Touch::getPosition(0).x, ::sf::Touch::getPosition(0).y)){


        btnSprite3.setTexture(btnText2);       
        }
        else{          
                btnSprite3.setTexture(btnText);
        }  
 
   
}
       
 
 void buttons::btnDraw(RenderWindow &window)
 
 {
       
        window.draw(btnSprite1);
        window.draw(btnSprite2);
        window.draw(btnSprite3);
       
 }

 


Jan666

  • Jr. Member
  • **
  • Posts: 64
    • View Profile
Re: Touch Button position Problem
« Reply #1 on: May 11, 2022, 12:58:30 pm »
Dont note the line btnSprite3.getTextureRect(btnPosFire);
I akready delete it

Jan666

  • Jr. Member
  • **
  • Posts: 64
    • View Profile
Re: Touch Button position Problem
« Reply #2 on: May 11, 2022, 03:10:20 pm »
I think the detail of my question is, that the FlorRect that is the touch field, and not at the position of the sprite, how can i fix it?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11029
    • View Profile
    • development blog
    • Email
Re: Touch Button position Problem
« Reply #3 on: May 12, 2022, 10:43:23 am »
Do you change anything with the view? If so, you'll have to call mapPixelToCoords() on the window for the touch position.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Jan666

  • Jr. Member
  • **
  • Posts: 64
    • View Profile
Re: Touch Button position Problem
« Reply #4 on: May 12, 2022, 12:59:31 pm »
Hi, i use

sf::RenderWindow window(sf::VideoMode::getDesktopMode(), "Hello SFML");

How i use mapPixelToCoords now in it?

RenderTarget target;
target.mapPixelToCoords(sprite)?

anything like that, or am i on the wrong way?

Jan666

  • Jr. Member
  • **
  • Posts: 64
    • View Profile
Re: Touch Button position Problem
« Reply #5 on: May 12, 2022, 08:53:38 pm »
I was on the wrong way, how i built this in my function:

sf::Vector2i pixelPos = sf::Touch::getPosition(?); 
 sf::Vector2f worldPos = window.mapPixelToCoords(pixelPos);

Jan666

  • Jr. Member
  • **
  • Posts: 64
    • View Profile
Re: Touch Button position Problem
« Reply #6 on: May 13, 2022, 11:04:20 pm »
I find the answer  :D
...
sf::Vector2i pixelPos = sf::Touch::getPosition(0, window);
sf::Vector2f worldPos = window.mapPixelToCoords(pixelPos);

sf::FloatRect frec = sprite.getGlobalBounds();

       
        if(frec.contains(worldPos)){ ...


 


Jan666

  • Jr. Member
  • **
  • Posts: 64
    • View Profile
Re: Touch Button position Problem
« Reply #7 on: May 14, 2022, 10:45:02 pm »
And now i need help to fix it with multi touch :-[

 

anything