Jump to content

This is the header of the button:

#pragma once#include <memory>#include <SFML/Graphics.hpp>#include <SFML/System.hpp>#include "Command.h"#include <iostream>namespace GUI{	struct ButtonStyle	{		ButtonStyle(sf::Color buttonColor, sf::Color textColor) : ButtonColor(buttonColor), TextColor(textColor) {}		sf::Color ButtonColor;		sf::Color TextColor;	};	class Button	{	public:		Button(sf::RenderWindow* windowPointer, std::unique_ptr<Command> command);		Button(sf::RenderWindow* windowPointer, std::unique_ptr<Command> command, sf::Texture* texture);		Button(sf::RenderWindow* windowPointer, std::unique_ptr<Command> command, sf::Texture* texture, sf::String text);		Button(sf::RenderWindow* windowPointer, std::unique_ptr<Command> command, sf::Texture* texture, sf::String text, ButtonStyle style);		void SetEnabled(bool isEnabled);		bool IsEnabled();		void OnClick();		void OnMouseOver();		void OnMouseExit();		void Update();		void Draw();		void SetPosition(sf::Vector2f position);		sf::Vector2f GetPosition();	private:		sf::RenderWindow* mWindowPointer;		std::unique_ptr<Command> mCommand;		sf::Sprite mSprite;		sf::Text mText;		sf::Font mFont;		sf::Time mCDTime = sf::Time::Zero;		sf::Clock mCDClock;		bool mEnabled = true;		bool mMouseIn = false;	};}

This is an example of a "Command"

class MyCommand : public Command{public:	void OnClick() { std::cout << "MouseClick\n"; }	void OnMouseOver() { }	void OnMouseExit() { std::cout << "MouseExit\n"; }private:};

And this is what the result is:

Button myButton1(&window,std::unique_ptr<Command>(new MyCommand()),&buttonTexture, sf::String("Button"), ButtonStyle(sf::Color::White, sf::Color::Red));myButton1.SetPosition(sf::Vector2f(myButton1.GetPosition().x + 20.f, myButton1.GetPosition().y));

S9OrQxm.png?1

 

The "Command" class has just 3 pure virtual methods nothing more.

 

Any suggestions? Using a class for the events (the Command class) looked like a good idea at first now I'm not too sure though, what do you think?

Link to comment
https://linustechtips.com/topic/315261-what-do-you-think/
Share on other sites

Link to post
Share on other sites

A better way might be to use interfaces and abstract classes. When controls have common functionality, they will likely implement some of the same interfaces or inherit some of the same abstract classes. 

 

To show an example, here's how .NET organizes things.

// The button class inherits ButtonBase and implements IButtonControlButton : ButtonBase, IButtonControl// The button base inherits ControlButtonBase : Control// Control inherits and implements lots of things and some of those also continue on with more stuffControl : // Lots of stuff// Some other things that inherit ButtonBase are Checkbox and RadioButton. But these don't implement IButtonControlCheckbox : ButtonBaseRadioButton : ButtonBase// Some things that inherit Control directly instead of ButtonBase areLabel : ControlListView : Control// And take LinkLabel, it inherits Label but also implements IButtonControlLinkLabel : Label, IButtonControl

So all of these classes have the functionality described in Control while only some of them need the functionality in ButtonBase or need to implement the methods in IButtonControl

Link to comment
https://linustechtips.com/topic/315261-what-do-you-think/#findComment-4285010
Share on other sites

Link to post
Share on other sites

I am not sure about your implementation of you constructors, but you could probably generalize them into 1 constructor using default parameters.

Personally I think it would be better to pass a pointer to the window in there draw function, since the data is (probably) not needed anywhere else. I like the data classes keep as small as possible. I am also not sure why your keep a time and clock object in your button.

Other than that, you gotta add some getters and setters for your texture, text, font, etc.

I like how you abstracted away what the button does with a command interface.

Link to comment
https://linustechtips.com/topic/315261-what-do-you-think/#findComment-4291033
Share on other sites

Link to post
Share on other sites

I am not sure about your implementation of you constructors, but you could probably generalize them into 1 constructor using default parameters.

Personally I think it would be better to pass a pointer to the window in there draw function, since the data is (probably) not needed anywhere else. I like the data classes keep as small as possible. I am also not sure why your keep a time and clock object in your button.

Other than that, you gotta add some getters and setters for your texture, text, font, etc.

I like how you abstracted away what the button does with a command interface.

I need the pointer to the window to check the mouse position relative to it, it's not just for the draw function.

Link to comment
https://linustechtips.com/topic/315261-what-do-you-think/#findComment-4292247
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×