patterncppMinor
Custom GUI Slider class
Viewed 0 times
customguiclassslider
Problem
I want to improve my slider class as much as possible. Is this acceptable code for a slider?
I've tried to comment as much as I can. If you want to find out more about the
slider.h:
Implementation:
```
#include "Slider.h"
#include "Textbox.h"
#include "RichLabel.h"
#include "../Pen.h"
begin_UI
Slider::Slider()
: Component( )
{
// Textbox to change the value of the slider
// by using a textbox instead of the wheel.
textbox_ = std::shared_ptr( new Textbox( ) );
textbox_->setUIID( String("Slider_component_textbox").hash( ) );
textbox_->setText( "0%" );
textbox_->setFilter( "1234567890" );
textbox_->setRightOf(
I've tried to comment as much as I can. If you want to find out more about the
Component class, check out my repository: dxLib.slider.h:
#pragma once
#include "Component.h"
begin_UI // Namespace define
class Textbox;
class RichLabel;
class Slider : public Component
{
public:
Slider( );
void Paint( Window *sender, BasePainter *painter ) override;
void KeyDown( Window *sender, KeyDownArgs &_Args ) override;
void KeyUp( Window *sender, KeyUpArgs &_Args ) override;
void KeyDownChar( Window *sender, KeyDownCharArgs &args ) override;
void MouseMoved( Window *sender, MouseMovedArgs &args ) override;
void MouseClicked( Window *sender, MouseClickedArgs &args ) override;
void MouseReleased( Window *sender, MouseReleasedArgs &args ) override;
__MATH Vector2 getDelta( ) const;
void setMaxDelta( const float &delta );
float getMaxDelta( ) const;
void setWheel( const float &delta );
float getWheel( ) const;
void setWheelSize( const __MATH Vector2 &size );
__MATH Vector2 getWheelSize( ) const;
bool CollidesWheel( const __MATH Vector2 &position );
bool inScrollableRegion( const __MATH Vector2 &cursor ) const;
std::shared_ptr getTextbox( ) const;
private:
void moveWheelToDelta( );
__MATH Vector2 moved_, wheel_, wheelSize_, delta_;
std::shared_ptr textbox_;
bool changed_, dragging_;
float maxDelta_;
};
end_UIImplementation:
```
#include "Slider.h"
#include "Textbox.h"
#include "RichLabel.h"
#include "../Pen.h"
begin_UI
Slider::Slider()
: Component( )
{
// Textbox to change the value of the slider
// by using a textbox instead of the wheel.
textbox_ = std::shared_ptr( new Textbox( ) );
textbox_->setUIID( String("Slider_component_textbox").hash( ) );
textbox_->setText( "0%" );
textbox_->setFilter( "1234567890" );
textbox_->setRightOf(
Solution
Looks pretty neat. I only have a couple comments:
At first I was puzzled by those
This might seem like silly nitpicking, but it can actually be a serious problem. Names starting with double underscore are reserved in C++ (ref), but we can usually get aways with it because our code rarely uses a reserved name, except when it does! I would not be surprised at all to find that same
If you can't be bothered spelling out the names of the namespaces you've created, then you can alternatively define a shorthand namespace alias, which would be The Right Way™ of doing it.
Not being a huge fan of macros either, I'd prefer to see the explicit:
Rather than
You've misspelled
At first I was puzzled by those
__SOMETHING macros, then I looked into your repo and they are actually aliasing a namespace:#ifndef __MATH
#define __MATH ::dx::lib::Math::
#endifThis might seem like silly nitpicking, but it can actually be a serious problem. Names starting with double underscore are reserved in C++ (ref), but we can usually get aways with it because our code rarely uses a reserved name, except when it does! I would not be surprised at all to find that same
__MATH macro defined in math.h or __MEMORY defined in the C++ memory header. You are walking on thin ice over there.If you can't be bothered spelling out the names of the namespaces you've created, then you can alternatively define a shorthand namespace alias, which would be The Right Way™ of doing it.
Not being a huge fan of macros either, I'd prefer to see the explicit:
namespace dx { namespace lib { namespace Graphics { namespace UI {Rather than
begin_UI. Users might miss that detail and attempt to use the contents of the namespace directly in code, thinking that the class is defined globally, only to result in annoying compilation errors.You've misspelled
Allignment and Alligned. It is with one L as in setAlignment and setAlignedOf.Code Snippets
#ifndef __MATH
#define __MATH ::dx::lib::Math::
#endifnamespace dx { namespace lib { namespace Graphics { namespace UI {Context
StackExchange Code Review Q#126904, answer score: 4
Revisions (0)
No revisions yet.