add noexcept and override specifiers. make some members public instead of using [gs]etters.

This commit is contained in:
Andrea Blankenstijn 2021-07-13 13:22:11 +02:00
parent 64fc355270
commit 03c6f51e16
20 changed files with 96 additions and 103 deletions

View file

@ -16,7 +16,7 @@ namespace bwidgets::abstract
public:
virtual ~FocusHandler();
inline virtual void focus(bool focus)
inline void focus(bool focus)
{
_has_focus = focus;
_handle_focus_change();

View file

@ -14,7 +14,8 @@ namespace bwidgets::abstract
public:
virtual ~FontHandler();
virtual void font(TTF_Font*);
void font(TTF_Font*);
};
}

View file

@ -33,17 +33,16 @@ namespace bwidgets::abstract
const int _border_width {3};
SDL_Rect _input_area {};
widget::Caption _input_caption;
T _value {};
Input(Widget* parent=nullptr)
: Widget(parent)
{
FocusHandler::_focus_area = &_input_area;
MouseHandler::_click_area = &_input_area;
_input_caption.text(value_to_string(_value));
_input_caption.text(value_to_string(value));
}
virtual void _handle_focus_change()
virtual void _handle_focus_change() override
{
if (FocusHandler::_has_focus)
{
@ -51,19 +50,19 @@ namespace bwidgets::abstract
}
else
{
value(value_from_string(input_text()));
input_text(value_to_string(_value));
value = value_from_string(input_text());
input_text(value_to_string(value));
SDL_StopTextInput();
}
}
virtual void _handle_font_change()
virtual void _handle_font_change() override
{
_input_caption.font(_font);
_update_widget_area();
}
virtual void _handle_key(const SDL_KeyboardEvent& key)
virtual void _handle_key(const SDL_KeyboardEvent& key) override
{
if (key.type == SDL_KEYDOWN)
{
@ -82,14 +81,14 @@ namespace bwidgets::abstract
case SDLK_RETURN:
case SDLK_RETURN2: // what is return2 btw?
case SDLK_KP_ENTER:
value(value_from_string(input_text()));
input_text(value_to_string(_value));
value = value_from_string(input_text());
input_text(value_to_string(value));
break;
}
}
}
virtual void _handle_text_input(const SDL_TextInputEvent& input)
virtual void _handle_text_input(const SDL_TextInputEvent& input) override
{
if (is_valid_input(input.text))
{
@ -98,7 +97,7 @@ namespace bwidgets::abstract
}
}
virtual void _update_widget_area()
virtual void _update_widget_area() noexcept override
{
_widget_area = {
0,
@ -118,35 +117,36 @@ namespace bwidgets::abstract
public:
SDL_Color color_border {160, 160, 160, SDL_ALPHA_OPAQUE};
SDL_Color color_bg {200, 200, 200, SDL_ALPHA_OPAQUE};
int float_precision {2};
SDL_Color color_bg_focused {255, 255, 255, SDL_ALPHA_OPAQUE};
int float_precision {2};
T value {};
virtual void color_fg(const SDL_Color& c)
virtual void color_fg(const SDL_Color& c) noexcept
{
_input_caption.color_fg(c);
}
virtual const std::string& input_text() const
virtual const std::string& input_text() const noexcept
{
return _input_caption.text();
}
virtual void input_text(std::string txt)
virtual void input_text(std::string txt) noexcept
{
_input_caption.text(txt);
}
virtual bool is_valid_input(const std::string) const
virtual bool is_valid_input(const std::string) const noexcept
{
return true;
}
virtual T process_value(T x) const
virtual T process_value(T x) const noexcept
{
return x;
}
virtual void render(SDL_Renderer* r)
virtual void render(SDL_Renderer* r) override
{
Widget::render(r);
@ -179,16 +179,6 @@ namespace bwidgets::abstract
_input_caption.render(r);
}
T value() const
{
return _value;
}
void value(T new_value)
{
_value = new_value;
}
T value_from_string(std::string s)
{
T v;
@ -205,7 +195,7 @@ namespace bwidgets::abstract
}
catch(std::exception&)
{
v = value();
v = value;
}
}
else if constexpr(std::is_same_v<T, std::string> || std::convertible_to<std::string, T>)
@ -237,12 +227,12 @@ namespace bwidgets::abstract
s = std::move(ss).str();
}
else
static_assert(sizeof(T) && false, "_value cannot be converted to string.");
static_assert(sizeof(T) && false, "value cannot be converted to string.");
return s;
}
virtual void viewport(const SDL_Rect& vp)
virtual void viewport(const SDL_Rect& vp) noexcept override
{
Widget::viewport(vp);

View file

@ -16,13 +16,13 @@ namespace bwidgets::abstract
virtual void _handle_text_input(const SDL_TextInputEvent&);
public:
inline virtual void handle_keyboard(const SDL_KeyboardEvent& ev)
inline void handle_keyboard(const SDL_KeyboardEvent& ev)
{
if (_has_focus)
_handle_key(ev);
}
inline virtual void handle_keyboard(const SDL_TextInputEvent& ev)
inline void handle_keyboard(const SDL_TextInputEvent& ev)
{
if (_has_focus)
_handle_text_input(ev);

View file

@ -12,18 +12,19 @@ namespace bwidgets::abstract
protected:
std::vector<Widget*> _widgets;
virtual void _update_layout() = 0;
virtual void _update_layout() noexcept = 0;
public:
Widget::Size margins {4, 4};
virtual ~Layout();
virtual void add_widget(Widget*);
virtual void handle_event(const SDL_Event&);
virtual void render(SDL_Renderer*);
virtual void viewport(const SDL_Rect&);
virtual ~Layout() noexcept;
virtual Widget::Size size() const = 0;
virtual void add_widget(Widget*);
virtual void handle_event(const SDL_Event&) override;
virtual void render(SDL_Renderer*) override;
virtual void viewport(const SDL_Rect&) noexcept override;
virtual Widget::Size size() const noexcept override = 0;
};
}

View file

@ -26,10 +26,10 @@ namespace bwidgets::abstract
public:
std::function<void (const SDL_MouseButtonEvent&)> click_handler = nullptr;
virtual void handle_mouse(const SDL_MouseButtonEvent&,
const SDL_Rect&);
virtual void handle_mouse(const SDL_MouseMotionEvent&,
const SDL_Rect&);
void handle_mouse(const SDL_MouseButtonEvent&,
const SDL_Rect&);
void handle_mouse(const SDL_MouseMotionEvent&,
const SDL_Rect&);
};
}

View file

@ -16,7 +16,7 @@ namespace bwidgets::abstract
SDL_Rect _viewport {0, 0, 0, 0};
SDL_Rect _widget_area {0, 0, 0, 0};
virtual void _update_widget_area();
virtual void _update_widget_area() noexcept;
public:
struct Size {
@ -27,13 +27,13 @@ namespace bwidgets::abstract
Widget* parent;
Widget(Widget* parent=nullptr);
virtual ~Widget();
virtual ~Widget() noexcept;
virtual void handle_event(const SDL_Event&);
virtual void render(SDL_Renderer*);
virtual void viewport(const SDL_Rect&);
virtual void viewport(const SDL_Rect&) noexcept;
inline virtual Size size() const
inline virtual Size size() const noexcept
{
return {_widget_area.w, _widget_area.h};
}

View file

@ -18,8 +18,8 @@ namespace bwidgets::widget
float _caption_height_multiplicator {1.2};
SDL_Color _color_foreground {0, 0, 0, SDL_ALPHA_OPAQUE};
void _handle_font_change();
void _update_widget_area();
void _handle_font_change() override;
void _update_widget_area() noexcept override;
public:
int border_width {3};
@ -28,10 +28,10 @@ namespace bwidgets::widget
Button(Widget* parent=nullptr);
void render(SDL_Renderer*);
abstract::Widget::Size size() const;
const std::string& text() const;
void text(std::string);
virtual void render(SDL_Renderer*) override;
Widget::Size size() const noexcept override;
virtual const std::string& text() const noexcept;
virtual void text(std::string) noexcept;
};
}

View file

@ -33,7 +33,7 @@ namespace bwidgets::widget
SDL_Texture* _text_texture {nullptr};
SDL_Renderer* _renderer {nullptr};
void _handle_font_change();
void _handle_font_change() override;
void _render_text();
public:
@ -45,13 +45,13 @@ namespace bwidgets::widget
abstract::Widget::Size margins {3, 0};
Caption(Widget* parent=nullptr);
~Caption();
~Caption() noexcept;
void color_fg(const SDL_Color&);
void render(SDL_Renderer*);
abstract::Widget::Size size() const;
const std::string& text() const;
void text(const std::string&);
virtual void color_fg(const SDL_Color&);
virtual void render(SDL_Renderer*) override;
virtual Widget::Size size() const noexcept override;
virtual const std::string& text() const noexcept;
virtual void text(const std::string&);
};
}

View file

@ -8,10 +8,10 @@ namespace bwidgets::widget
class Horizontal_Layout final : public abstract::Layout
{
private:
void _update_layout();
void _update_layout() noexcept override;
public:
abstract::Widget::Size size() const;
Widget::Size size() const noexcept override;
};
}

View file

@ -28,14 +28,14 @@ namespace bwidgets::widget
std::numeric_limits<T>::max()
};
void _handle_font_change()
virtual void _handle_font_change() override
{
_decrement_button.font(abstract::FontHandler::_font);
_increment_button.font(abstract::FontHandler::_font);
abstract::Input<T>::_handle_font_change();
}
void _update_widget_area()
virtual void _update_widget_area() noexcept override
{
abstract::Input<T>::_update_widget_area();
@ -80,34 +80,34 @@ namespace bwidgets::widget
_increment_button.text("+");
_increment_button.click_handler = [this](const SDL_MouseButtonEvent&) {
T new_value = this->value() + button_step;
if (_value_range.second - this->value() < button_step)
T new_value = this->value + button_step;
if (_value_range.second - this->value < button_step)
new_value = _value_range.second;
this->value(new_value);
this->value = new_value;
this->input_text(this->value_to_string(new_value));
};
_decrement_button.text("-");
_decrement_button.click_handler = [this](const SDL_MouseButtonEvent&) {
T new_value = this->value() - button_step;
if (this->value() - _value_range.first < button_step)
T new_value = this->value - button_step;
if (this->value - _value_range.first < button_step)
new_value = _value_range.first;
this->value(new_value);
this->value = new_value;
this->input_text(this->value_to_string(new_value));
};
}
void handle_event(const SDL_Event& ev)
virtual void handle_event(const SDL_Event& ev) override
{
abstract::Widget::handle_event(ev);
_increment_button.handle_event(ev);
_decrement_button.handle_event(ev);
}
bool is_valid_input(const std::string input) const
virtual bool is_valid_input(const std::string input) const noexcept override
{
bool valid = false;
@ -130,7 +130,7 @@ namespace bwidgets::widget
return valid;
}
T process_value(T x) const
virtual T process_value(T x) const noexcept override
{
T value = x;
if (x < _value_range.first)
@ -141,19 +141,19 @@ namespace bwidgets::widget
return value;
}
void render(SDL_Renderer* r)
virtual void render(SDL_Renderer* r) override
{
abstract::Input<T>::render(r);
_increment_button.render(r);
_decrement_button.render(r);
}
std::pair<T, T> value_range() const
virtual std::pair<T, T> value_range() const noexcept
{
return _value_range;
}
void value_range(T min, T max)
virtual void value_range(T min, T max)
{
if (min > max)
@ -161,7 +161,7 @@ namespace bwidgets::widget
_value_range = {min, max};
}
void viewport(const SDL_Rect& vp)
virtual void viewport(const SDL_Rect& vp) noexcept override
{
abstract::Input<T>::viewport(vp);

View file

@ -9,22 +9,22 @@ namespace bwidgets::utils::math
{
struct RectOverflow : std::exception {};
static inline int center_rect(int container_size, int rect_size)
static inline int center_rect(int container_size, int rect_size) noexcept
{
return (container_size - rect_size) / 2;
}
static inline float distance_sqrd(const SDL_Point& a, const SDL_Point& b)
static inline float distance_sqrd(const SDL_Point& a, const SDL_Point& b) noexcept
{
return (a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y);
}
static inline float distance(const SDL_Point& a, const SDL_Point& b)
static inline float distance(const SDL_Point& a, const SDL_Point& b) noexcept
{
return std::sqrt(distance_sqrd(a, b));
}
static inline bool rect_in_rect(const SDL_Rect outer, const SDL_Rect inner)
static inline bool rect_in_rect(const SDL_Rect outer, const SDL_Rect inner) noexcept
{
SDL_Point top_left {inner.x, inner.y};
SDL_Point bottom_right {inner.x + inner.w, inner.y + inner.h};
@ -34,7 +34,7 @@ namespace bwidgets::utils::math
SDL_PointInRect(&bottom_right, &outer);
}
static inline SDL_Rect rect_offset(const SDL_Rect& r, const SDL_Rect& offset)
static inline SDL_Rect rect_offset(const SDL_Rect& r, const SDL_Rect& offset) noexcept
{
return {
r.x + offset.x,

View file

@ -8,10 +8,10 @@ namespace bwidgets::widget
class Vertical_Layout final : public abstract::Layout
{
private:
void _update_layout();
void _update_layout() noexcept override;
public:
abstract::Widget::Size size() const;
Widget::Size size() const noexcept override;
};
}

View file

@ -73,7 +73,7 @@ void widget::Button::render(SDL_Renderer* r)
_caption.render(r);
}
abstract::Widget::Size widget::Button::size() const
abstract::Widget::Size widget::Button::size() const noexcept
{
return {
(int)(_caption.size().w * _caption_height_multiplicator)
@ -83,12 +83,12 @@ abstract::Widget::Size widget::Button::size() const
};
}
const std::string& widget::Button::text() const
const std::string& widget::Button::text() const noexcept
{
return _caption.text();
}
void widget::Button::text(std::string txt)
void widget::Button::text(std::string txt) noexcept
{
_caption.text(txt);
_update_widget_area();
@ -100,7 +100,7 @@ void widget::Button::_handle_font_change()
_update_widget_area();
}
void widget::Button::_update_widget_area()
void widget::Button::_update_widget_area() noexcept
{
int h = _caption.size().h * _caption_height_multiplicator
+ 2 * border_width;

View file

@ -13,7 +13,7 @@ widget::Caption::Caption(abstract::Widget* parent)
{
}
widget::Caption::~Caption()
widget::Caption::~Caption() noexcept
{
SDL_FreeSurface(_text_surface);
SDL_DestroyTexture(_text_texture);
@ -82,7 +82,7 @@ void widget::Caption::render(SDL_Renderer* r)
);
}
abstract::Widget::Size widget::Caption::size() const
abstract::Widget::Size widget::Caption::size() const noexcept
{
if (_font == nullptr)
return {-1, -1};
@ -95,7 +95,7 @@ abstract::Widget::Size widget::Caption::size() const
return size;
}
const std::string& widget::Caption::text() const
const std::string& widget::Caption::text() const noexcept
{
return _text;
}

View file

@ -2,7 +2,7 @@
using namespace bwidgets;
abstract::Widget::Size widget::Horizontal_Layout::size() const
abstract::Widget::Size widget::Horizontal_Layout::size() const noexcept
{
abstract::Widget::Size max {0, 0};
@ -20,7 +20,7 @@ abstract::Widget::Size widget::Horizontal_Layout::size() const
};
}
void widget::Horizontal_Layout::_update_layout()
void widget::Horizontal_Layout::_update_layout() noexcept
{
int widget_size = (_viewport.w - (_widgets.size() + 1) * margins.w)
/ _widgets.size();

View file

@ -4,7 +4,7 @@
using namespace bwidgets;
abstract::Layout::~Layout()
abstract::Layout::~Layout() noexcept
{
for (Widget* widget_ptr : _widgets)
delete widget_ptr;
@ -29,7 +29,7 @@ void abstract::Layout::render(SDL_Renderer* r)
widget_ptr->render(r);
}
void abstract::Layout::viewport(const SDL_Rect& vp)
void abstract::Layout::viewport(const SDL_Rect& vp) noexcept
{
Widget::viewport(vp);
_update_layout();

View file

@ -10,6 +10,7 @@ namespace bwidgets::utils::render
const int aa_pixels,
const float d
)
noexcept
{
const auto d_clamp {std::abs(std::clamp<float>(d, -aa_pixels, 0))};
const auto d_norm {d_clamp / aa_pixels};

View file

@ -2,7 +2,7 @@
using namespace bwidgets;
abstract::Widget::Size widget::Vertical_Layout::size() const
abstract::Widget::Size widget::Vertical_Layout::size() const noexcept
{
abstract::Widget::Size max {0, 0};
@ -20,7 +20,7 @@ abstract::Widget::Size widget::Vertical_Layout::size() const
};
}
void widget::Vertical_Layout::_update_layout()
void widget::Vertical_Layout::_update_layout() noexcept
{
int widget_size = (_viewport.h - (_widgets.size() + 1) * margins.h)
/ _widgets.size();

View file

@ -12,7 +12,7 @@ abstract::Widget::Widget(Widget* p)
{
}
abstract::Widget::~Widget()
abstract::Widget::~Widget() noexcept
{
}
@ -60,13 +60,13 @@ void abstract::Widget::render(SDL_Renderer* r)
SDL_RenderDrawRect(r, &dbg_outline);
}
void abstract::Widget::viewport(const SDL_Rect& vp)
void abstract::Widget::viewport(const SDL_Rect& vp) noexcept
{
_viewport = vp;
_update_widget_area();
}
void abstract::Widget::_update_widget_area()
void abstract::Widget::_update_widget_area() noexcept
{
_widget_area = {
0,