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

Author Topic: Changing drawable object on draw  (Read 1282 times)

0 Members and 1 Guest are viewing this topic.

ErA

  • Newbie
  • *
  • Posts: 4
    • View Profile
Changing drawable object on draw
« on: September 06, 2018, 11:42:01 am »
Hey,

im currently writing a Button class that derives from sf::Drawable and sf::Transformable. The basic shape of the Button is a sf::RectangleShape together with some other stuff thats not really relevant for the question.

I want the "OutlineColor" to change when the mouse is currently within the sf::RectangleShape. I got it to work within a public function of my Button class, but that means every time you draw a instance of a Button you have to call that function in advance so the color gets changed correctly.

Basicly i want to automaticly check the Mouse position and change the OutlineColor every time the Button gets drawn by the derived virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const; .

However because of the const any changes to the object from within that function are prevented. So i cannot call my function from there. Is there any way to create some sort of OnDraw event or get your object editable from within the const draw function?


Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Changing drawable object on draw
« Reply #1 on: September 06, 2018, 11:49:44 am »
We disallowed modifying the object in its draw function on purpose, because it is often a very bad design.

A cleaner design would be to react to the MouseMoved event in your event loop, and then check which buttons are highlighted and update their state from there.

Your other solution, calling an update function before drawing the button, is a typical one and is ok too.
Laurent Gomila - SFML developer