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 - unixorn

Pages: [1]
1
Graphics / Re: Invisible vertical padding on text
« on: May 16, 2021, 03:25:40 pm »
Hi. i think this is the most asked question ever in this forum  :P
you need to use the text's 'top' variable. yes, it is 'normal'. i don't remember the exact explanation, but it has something to do with text alignment in the font itself. have a look:

https://en.sfml-dev.org/forums/index.php?topic=27840.msg175189#msg175189
https://en.sfml-dev.org/forums/index.php?topic=24985.msg166441#msg166441
https://en.sfml-dev.org/forums/index.php?topic=19004.msg136987#msg136987
Yes, thank you! XD

sf::Text, unlike other SFML entities, is aligned on its baseline, not on its top. I'm pretty sure it is explained somewhere in the documentation. So no, you can't expect "top" to be always zero for this class.

Quote
It actually behaves funny with width when there are leading or trailing spaces. Laurent?
How exactly does it behave in this case?

I don't know how I missed that XD.
Gotta read the docs more carefully :)
Cheers!



2
Graphics / Invisible vertical padding on text
« on: May 15, 2021, 08:57:01 pm »
So there I was trying to display a snippet of text XD
Unfortunately, there seems to be some "invisible" padding that does not display the text at the requested position (0,0).
The text seems to be shifted downwards like maybe 10px? (Even though it says its position is what I made it)
The font is mono spaced.
Here is some code that reproduces the "weird" behavior :)

    sf::Text text;
    text.setFont(Resources::getGameFont());
    text.setCharacterSize(50);
    text.setString("Hand");
    text.setPosition(0,0);
    text.setFillColor(sf::Color::Black);
    while (window.isOpen())
    {
        while (window.pollEvent(event))
            if (event.type == sf::Event::Closed)
                window.close();
        window.clear(sf::Color::Yellow);
        window.draw(text);
        window.display();
    }

From what I understand, the default origin of text objects is their top left corner?
I've also tried 3 other fonts and they all seem to have the same problem :(

I've linked an image that shows this strange behavior on one of the fonts :)

I edited the code a little (I inserted the following just before the while loop):
    std::cout << "gb_box.left = " << text.getGlobalBounds().left << std::endl;
    std::cout << "gb_box.top = " << text.getGlobalBounds().left << std::endl;
    std::cout << "lb_box.left = " << text.getLocalBounds().left << std::endl;
    std::cout << "lb_box.top = " << text.getLocalBounds().top << std::endl;

And I got:
gb_box.left = 1
gb_box.top = 1
lb_box.left = 1
lb_box.top = 19

Is this normal? I mean, shouldn't the 'left' and 'top' members be equivalent to the 'x' and 'y' positions of the text?

Okay, after playing around a little, I found a somewhat funny "solution" :)
The following snippet was also inserted right before the main while loop (or in other words, after the last insertion).

    while (text.getGlobalBounds().left != 0)
        text.move(-1,0);
    std::cout << "text_pos.x = " << text.getPosition().x << std::endl;
    while (text.getGlobalBounds().top != 0)
        text.move(0,-1);
    std::cout << "text_pos.y = " << text.getPosition().y << std::endl;

The interesting thing is the output though :)
gb_box.left = 1
gb_box.top = 1
lb_box.left = 1
lb_box.top = 19
text_pos.x = -1
text_pos.y = -19

I'm just really curious whether this is supposed to happen XD or if I'm doing something wrong.
So I learned that the position of text is not always equivalent to the position of its bounding box, which was a tiny bit of a shock to my eyes XD.
So I guess, It's good to know that if I want to set the position of text to (x,y), what I'm really trying to do is set the position of the bounding box of that text to (x,y).

3
Graphics / Re: Displaced bounding box of Node
« on: December 15, 2019, 11:04:18 pm »
You are a GENIUS!!!! Thanks so much! I couldn't figure that out for four hours!
(For anyone wanting in on the working code:)
  void Button::listen_current()
    {
        m_clicked = false;
        if (ASys<ListenerType>::was_triggered(ListenerType::PrimaryMouseInput))
        {
            if (m_object->getGlobalBounds().contains((sf::Vector2f)sf::Mouse::getPosition(*WindowPTR->get())))
                m_clicked = true;
        }
    }

 
Thanks again.

4
Graphics / Displaced bounding box of Node
« on: December 15, 2019, 08:16:29 pm »
Hello.
I have a class called Node which is derived from a class called NodePrototype which is derived from sf::Drawable and sf::Transformable. I've managed to create a root node and plug a Button (a class that is derived from Node) object into the root's children container. The button is displayed, in the specified coordinates moreover, but when I attempt to check if the mouse is clicked while hovering over the button something very strange happens. So far I have been unable to pinpoint the cause of this behavior, but here is what I have figured out so far:
  • The bounding box of the object is always located at the top left corner of the monitor (not the app window) even when I move the window. Even when the window is launched at different coordinates.
  • The click verification detects the click provided it is done within the specified area (top left corner of the monitor with the width and height = object width and height)
In the attachment the situation is presented. It seems that no matter where the button is, its bounds are always in the top left corner.
Here is the class definition and currently used constructor of the button:
struct TextProps { sf::String text; unsigned text_size; sf::Color text_fill_color; sf::Font* fptr; };
    struct ButtonProps { union { sf::Color bg_color; sf::Texture* bg_texture; }; std::unique_ptr<sf::Text> btext; unsigned bwidth; unsigned bheight; };
    const sf::Vector2f DEFPOS{500,100};
    class Button : public Node<sf::RectangleShape>
    {  
        public:
            Button(sf::Color);
            Button(sf::Color, TextProps&);
            Button(sf::Texture&);
            Button(sf::Texture&, TextProps&);
            Button(ButtonProps& bprops) : m_properties{ std::move(bprops) } {}
            ButtonProps& get_handle() { return m_properties; }
            virtual void render_current(sf::RenderTarget&, sf::RenderStates) const;
            virtual void listen_current(); //For testing
            virtual void update_current();
            virtual void on_click();
        private:
            ButtonProps m_properties;
            bool m_clicked;
            // indirectly the class inherits m_object which is of type: std::unique_ptr<sf::RectangleShape>
    };  
Button::Button(sf::Color color, TextProps& textp) : m_properties{ {color}, std::unique_ptr<sf::Text>(new sf::Text), 80, 60}, m_clicked{false}
    {  
        m_properties.btext->setFont(*textp.fptr);
        m_properties.btext->setString(textp.text);
        m_properties.btext->setCharacterSize(textp.text_size);
        m_properties.btext->setFillColor(textp.text_fill_color);
        m_properties.btext->setOrigin(m_properties.btext->getGlobalBounds().width / 2, m_properties.btext->getGlobalBounds().height / 2);
        m_object->setFillColor(m_properties.bg_color);
        m_object->setSize(sf::Vector2f(m_properties.bwidth, m_properties.bheight));
        m_object->setPosition(DEFPOS);
        m_properties.btext->setPosition(m_object->getGlobalBounds().width / 2, m_object->getGlobalBounds().height / 2);
    }  

 
Also, i used the following approach to check for a hover over + button click:
void Button::listen_current()
    {
        m_clicked = false;
        if (ASys<ListenerType>::was_triggered(ListenerType::PrimaryMouseInput))
        {
            sf::Vector2f mp(sf::Mouse::getPosition());
            if (m_object->getLocalBounds().contains(mp))
                m_clicked = true;
        }

 
However I doubt that changes anything because the click verification works everywhere. No matter where I click, the solo click detection mechanism works fine. This implies the problem is somewhere with the button rect shape m_object.
I have no idea where. I place it at DEFPOS(500,100) to see if the area where the click+hover detection works has changed, but no. So I wonder what is going on. Perhaps this is happening because Node indirectly inherits the sf::Drawable class?
I don't know how that could be connected in any obvious way, especially since I am running the check directly on m_object which is not a node, but simply a sf::RectangleShape.
I had a look at the following thread:
https://en.sfml-dev.org/forums/index.php?topic=14914.0
From there I adapted the idea to create an sf::RectangleShape that is equal to the globalbounds.
And I when it gets drawn (i set the fill color to green) it is directly on top of the red button.
So I don't understand what is going on. I thought the globalBounds function wasn't working correctly, but now I have no idea how to explain the app's behavior.





I have found a partial solution:
The globalBounds returns a FloatRect meanwhile the Mouse::getPosition returns a Vector2i; so after casting the mp(Vector2i holding the mouse position) as a Vector2f the area where the click+hover detection works is a rectangle about the size of the button, but about half-height displaced upwards from the button. Any ideas? Could this be the result of bad approximation from int to float?

Pages: [1]
anything