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: public:
virtual ~FocusHandler(); virtual ~FocusHandler();
inline virtual void focus(bool focus) inline void focus(bool focus)
{ {
_has_focus = focus; _has_focus = focus;
_handle_focus_change(); _handle_focus_change();

View file

@ -14,7 +14,8 @@ namespace bwidgets::abstract
public: public:
virtual ~FontHandler(); 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}; const int _border_width {3};
SDL_Rect _input_area {}; SDL_Rect _input_area {};
widget::Caption _input_caption; widget::Caption _input_caption;
T _value {};
Input(Widget* parent=nullptr) Input(Widget* parent=nullptr)
: Widget(parent) : Widget(parent)
{ {
FocusHandler::_focus_area = &_input_area; FocusHandler::_focus_area = &_input_area;
MouseHandler::_click_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) if (FocusHandler::_has_focus)
{ {
@ -51,19 +50,19 @@ namespace bwidgets::abstract
} }
else else
{ {
value(value_from_string(input_text())); value = value_from_string(input_text());
input_text(value_to_string(_value)); input_text(value_to_string(value));
SDL_StopTextInput(); SDL_StopTextInput();
} }
} }
virtual void _handle_font_change() virtual void _handle_font_change() override
{ {
_input_caption.font(_font); _input_caption.font(_font);
_update_widget_area(); _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) if (key.type == SDL_KEYDOWN)
{ {
@ -82,14 +81,14 @@ namespace bwidgets::abstract
case SDLK_RETURN: case SDLK_RETURN:
case SDLK_RETURN2: // what is return2 btw? case SDLK_RETURN2: // what is return2 btw?
case SDLK_KP_ENTER: case SDLK_KP_ENTER:
value(value_from_string(input_text())); value = value_from_string(input_text());
input_text(value_to_string(_value)); input_text(value_to_string(value));
break; 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)) 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 = { _widget_area = {
0, 0,
@ -118,35 +117,36 @@ namespace bwidgets::abstract
public: public:
SDL_Color color_border {160, 160, 160, SDL_ALPHA_OPAQUE}; SDL_Color color_border {160, 160, 160, SDL_ALPHA_OPAQUE};
SDL_Color color_bg {200, 200, 200, 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}; 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); _input_caption.color_fg(c);
} }
virtual const std::string& input_text() const virtual const std::string& input_text() const noexcept
{ {
return _input_caption.text(); return _input_caption.text();
} }
virtual void input_text(std::string txt) virtual void input_text(std::string txt) noexcept
{ {
_input_caption.text(txt); _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; return true;
} }
virtual T process_value(T x) const virtual T process_value(T x) const noexcept
{ {
return x; return x;
} }
virtual void render(SDL_Renderer* r) virtual void render(SDL_Renderer* r) override
{ {
Widget::render(r); Widget::render(r);
@ -179,16 +179,6 @@ namespace bwidgets::abstract
_input_caption.render(r); _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 value_from_string(std::string s)
{ {
T v; T v;
@ -205,7 +195,7 @@ namespace bwidgets::abstract
} }
catch(std::exception&) catch(std::exception&)
{ {
v = value(); v = value;
} }
} }
else if constexpr(std::is_same_v<T, std::string> || std::convertible_to<std::string, T>) 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(); s = std::move(ss).str();
} }
else 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; return s;
} }
virtual void viewport(const SDL_Rect& vp) virtual void viewport(const SDL_Rect& vp) noexcept override
{ {
Widget::viewport(vp); Widget::viewport(vp);

View file

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

View file

@ -12,18 +12,19 @@ namespace bwidgets::abstract
protected: protected:
std::vector<Widget*> _widgets; std::vector<Widget*> _widgets;
virtual void _update_layout() = 0; virtual void _update_layout() noexcept = 0;
public: public:
Widget::Size margins {4, 4}; Widget::Size margins {4, 4};
virtual ~Layout(); virtual ~Layout() noexcept;
virtual void add_widget(Widget*);
virtual void handle_event(const SDL_Event&);
virtual void render(SDL_Renderer*);
virtual void viewport(const SDL_Rect&);
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: public:
std::function<void (const SDL_MouseButtonEvent&)> click_handler = nullptr; std::function<void (const SDL_MouseButtonEvent&)> click_handler = nullptr;
virtual void handle_mouse(const SDL_MouseButtonEvent&, void handle_mouse(const SDL_MouseButtonEvent&,
const SDL_Rect&); const SDL_Rect&);
virtual void handle_mouse(const SDL_MouseMotionEvent&, void handle_mouse(const SDL_MouseMotionEvent&,
const SDL_Rect&); const SDL_Rect&);
}; };
} }

View file

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

View file

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

View file

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

View file

@ -8,10 +8,10 @@ namespace bwidgets::widget
class Horizontal_Layout final : public abstract::Layout class Horizontal_Layout final : public abstract::Layout
{ {
private: private:
void _update_layout(); void _update_layout() noexcept override;
public: 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() std::numeric_limits<T>::max()
}; };
void _handle_font_change() virtual void _handle_font_change() override
{ {
_decrement_button.font(abstract::FontHandler::_font); _decrement_button.font(abstract::FontHandler::_font);
_increment_button.font(abstract::FontHandler::_font); _increment_button.font(abstract::FontHandler::_font);
abstract::Input<T>::_handle_font_change(); abstract::Input<T>::_handle_font_change();
} }
void _update_widget_area() virtual void _update_widget_area() noexcept override
{ {
abstract::Input<T>::_update_widget_area(); abstract::Input<T>::_update_widget_area();
@ -80,34 +80,34 @@ namespace bwidgets::widget
_increment_button.text("+"); _increment_button.text("+");
_increment_button.click_handler = [this](const SDL_MouseButtonEvent&) { _increment_button.click_handler = [this](const SDL_MouseButtonEvent&) {
T new_value = this->value() + button_step; T new_value = this->value + button_step;
if (_value_range.second - this->value() < button_step) if (_value_range.second - this->value < button_step)
new_value = _value_range.second; new_value = _value_range.second;
this->value(new_value); this->value = new_value;
this->input_text(this->value_to_string(new_value)); this->input_text(this->value_to_string(new_value));
}; };
_decrement_button.text("-"); _decrement_button.text("-");
_decrement_button.click_handler = [this](const SDL_MouseButtonEvent&) { _decrement_button.click_handler = [this](const SDL_MouseButtonEvent&) {
T new_value = this->value() - button_step; T new_value = this->value - button_step;
if (this->value() - _value_range.first < button_step) if (this->value - _value_range.first < button_step)
new_value = _value_range.first; new_value = _value_range.first;
this->value(new_value); this->value = new_value;
this->input_text(this->value_to_string(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); abstract::Widget::handle_event(ev);
_increment_button.handle_event(ev); _increment_button.handle_event(ev);
_decrement_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; bool valid = false;
@ -130,7 +130,7 @@ namespace bwidgets::widget
return valid; return valid;
} }
T process_value(T x) const virtual T process_value(T x) const noexcept override
{ {
T value = x; T value = x;
if (x < _value_range.first) if (x < _value_range.first)
@ -141,19 +141,19 @@ namespace bwidgets::widget
return value; return value;
} }
void render(SDL_Renderer* r) virtual void render(SDL_Renderer* r) override
{ {
abstract::Input<T>::render(r); abstract::Input<T>::render(r);
_increment_button.render(r); _increment_button.render(r);
_decrement_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; return _value_range;
} }
void value_range(T min, T max) virtual void value_range(T min, T max)
{ {
if (min > max) if (min > max)
@ -161,7 +161,7 @@ namespace bwidgets::widget
_value_range = {min, max}; _value_range = {min, max};
} }
void viewport(const SDL_Rect& vp) virtual void viewport(const SDL_Rect& vp) noexcept override
{ {
abstract::Input<T>::viewport(vp); abstract::Input<T>::viewport(vp);

View file

@ -9,22 +9,22 @@ namespace bwidgets::utils::math
{ {
struct RectOverflow : std::exception {}; 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; 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); 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)); 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 top_left {inner.x, inner.y};
SDL_Point bottom_right {inner.x + inner.w, inner.y + inner.h}; 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); 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 { return {
r.x + offset.x, r.x + offset.x,

View file

@ -8,10 +8,10 @@ namespace bwidgets::widget
class Vertical_Layout final : public abstract::Layout class Vertical_Layout final : public abstract::Layout
{ {
private: private:
void _update_layout(); void _update_layout() noexcept override;
public: 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); _caption.render(r);
} }
abstract::Widget::Size widget::Button::size() const abstract::Widget::Size widget::Button::size() const noexcept
{ {
return { return {
(int)(_caption.size().w * _caption_height_multiplicator) (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(); return _caption.text();
} }
void widget::Button::text(std::string txt) void widget::Button::text(std::string txt) noexcept
{ {
_caption.text(txt); _caption.text(txt);
_update_widget_area(); _update_widget_area();
@ -100,7 +100,7 @@ void widget::Button::_handle_font_change()
_update_widget_area(); _update_widget_area();
} }
void widget::Button::_update_widget_area() void widget::Button::_update_widget_area() noexcept
{ {
int h = _caption.size().h * _caption_height_multiplicator int h = _caption.size().h * _caption_height_multiplicator
+ 2 * border_width; + 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_FreeSurface(_text_surface);
SDL_DestroyTexture(_text_texture); 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) if (_font == nullptr)
return {-1, -1}; return {-1, -1};
@ -95,7 +95,7 @@ abstract::Widget::Size widget::Caption::size() const
return size; return size;
} }
const std::string& widget::Caption::text() const const std::string& widget::Caption::text() const noexcept
{ {
return _text; return _text;
} }

View file

@ -2,7 +2,7 @@
using namespace bwidgets; 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}; 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) int widget_size = (_viewport.w - (_widgets.size() + 1) * margins.w)
/ _widgets.size(); / _widgets.size();

View file

@ -4,7 +4,7 @@
using namespace bwidgets; using namespace bwidgets;
abstract::Layout::~Layout() abstract::Layout::~Layout() noexcept
{ {
for (Widget* widget_ptr : _widgets) for (Widget* widget_ptr : _widgets)
delete widget_ptr; delete widget_ptr;
@ -29,7 +29,7 @@ void abstract::Layout::render(SDL_Renderer* r)
widget_ptr->render(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); Widget::viewport(vp);
_update_layout(); _update_layout();

View file

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

View file

@ -2,7 +2,7 @@
using namespace bwidgets; 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}; 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) int widget_size = (_viewport.h - (_widgets.size() + 1) * margins.h)
/ _widgets.size(); / _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); SDL_RenderDrawRect(r, &dbg_outline);
} }
void abstract::Widget::viewport(const SDL_Rect& vp) void abstract::Widget::viewport(const SDL_Rect& vp) noexcept
{ {
_viewport = vp; _viewport = vp;
_update_widget_area(); _update_widget_area();
} }
void abstract::Widget::_update_widget_area() void abstract::Widget::_update_widget_area() noexcept
{ {
_widget_area = { _widget_area = {
0, 0,