config clang-tidy linter and fix code based on linting err/warn

This commit is contained in:
Andrea Blankenstijn 2021-07-30 20:41:47 +02:00
parent 3b6e900a22
commit 13bdb970c0
38 changed files with 366 additions and 331 deletions

2
.clang-tidy Normal file
View File

@ -0,0 +1,2 @@
Checks: 'bugprone-*,cert-*,misc-*,modernize-*,performance-*,portability-*,readability-*,-misc-non-private-member-variables-in-classes,-bugprone-easily-swappable-parameters,-readability-braces-around-statements,-cppcoreguidelines-non-private-member-variables-in-classes,-readability-named-parameter'
FormatStyle: file

4
compile_flags.txt Normal file
View File

@ -0,0 +1,4 @@
-xc++
-I
inc
-std=c++2a

View File

@ -6,7 +6,7 @@
using widget::Button;
int main()
auto main() -> int
{
run_example<Button>([](Button* w, core::Font* f, int x, int y) {
w->click_handler = [x, y](const SDL_MouseButtonEvent&) {

View File

@ -4,7 +4,7 @@
using widget::Caption;
int main()
auto main() -> int
{
run_example<Caption>(
[](Caption* w, core::Font* f, int, int) {
@ -12,6 +12,6 @@ int main()
w->text("¡jello!");
w->font(f);
},
4, 8);
4, 8); // NOLINT(readability-magic-numbers)
return 0;
}

View File

@ -1,11 +1,11 @@
#include "example_widget.hpp"
#include "run.hpp"
int main()
auto main() -> int
{
run_example<Example>([](Example* w, core::Font*, int x, int y) {
w->cycle_r = (x + 1) * 3000;
w->cycle_b = (y + 1) * 3000;
w->cycle_b = (1 + x + y) * (y + 1) * 400;
w->cycle_r = (x + 1) * 3000; // NOLINT(readability-magic-numbers)
w->cycle_b = (y + 1) * 3000; // NOLINT(readability-magic-numbers)
w->cycle_b = (1 + x + y) * (y + 1) * 400; // NOLINT(readability-magic-numbers)
});
}

View File

@ -8,29 +8,28 @@
#include <basic_widgets/w/base/widget.hpp>
using bwidgets::core::rect_margin;
using bwidgets::core::Renderer;
using bwidgets::core::Size;
using bwidgets::widget::Widget;
class Example final : public Widget
{
private:
virtual void _handle_geometry_change(const SDL_Rect& vp) noexcept override
void _handle_geometry_change(const SDL_Rect& vp) noexcept override
{
_widget_area = {2, 2, vp.w - 4, vp.h - 4};
}
virtual void _handle_rendering() override
void _handle_rendering() override
{
auto now = SDL_GetTicks();
uint8_t r = 255 * (now % cycle_r / (float)cycle_r);
uint8_t g = 255 * (now % cycle_g / (float)cycle_g);
uint8_t b = 255 * (now % cycle_b / (float)cycle_b);
SDL_Color base_color {r, g, b, 255};
uint8_t r = 255 * (now % cycle_r / (float)cycle_r); // NOLINT
uint8_t g = 255 * (now % cycle_g / (float)cycle_g); // NOLINT
uint8_t b = 255 * (now % cycle_b / (float)cycle_b); // NOLINT
SDL_Color base_color {r, g, b, 255}; // NOLINT
int border = 10;
int border = 10; // NOLINT
for (auto i = 0; i < border; i += 3) {
uint8_t alpha = 255 * i / border;
uint8_t alpha = 255 * i / border; // NOLINT
_renderer->draw_color({base_color.r, base_color.g, base_color.b, alpha})
->draw_rect(rect_margin(_widget_area, {i, i}));
}
@ -41,13 +40,13 @@ private:
}
public:
unsigned int cycle_r {3500};
unsigned int cycle_g {3500};
unsigned int cycle_b {3500};
unsigned int cycle_r {3500}; // NOLINT
unsigned int cycle_g {3500}; // NOLINT
unsigned int cycle_b {3500}; // NOLINT
Size size() const noexcept override
[[nodiscard]] auto size() const noexcept -> Size override
{
return {128, 64};
return {128, 64}; // NOLINT
}
};

View File

@ -4,13 +4,13 @@
using widget::NumericInput;
int main()
auto main() -> int
{
run_example<NumericInput<float>>(
[](NumericInput<float>* w, core::Font* f, int, int) {
w->font(f);
w->button_step = 0.5;
w->value_range(-3.14, 8.5);
w->button_step = 0.5; // NOLINT(readability-magic-numbers)
w->value_range(-3.14, 8.5); // NOLINT(readability-magic-numbers)
});
return 0;
}

View File

@ -29,7 +29,7 @@ void run_example(std::function<void(W*, core::Font*, int, int)> setup, int w = 3
core::SDLError::success_or_throw(TTF_Init(), __FILE__, __FUNCTION__, __LINE__);
const core::Size size_init {854, 480};
auto* font {new core::Font(core::Font::find("Monospace"), 16)};
auto* font {new core::Font(core::Font::find("Monospace"), 16)}; // NOLINT
auto* win {SDL_CreateWindow("basic_widgets example", SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED, size_init.w, size_init.h,
SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE
@ -70,7 +70,7 @@ void run_example(std::function<void(W*, core::Font*, int, int)> setup, int w = 3
layout->handle_event(ev);
}
renderer->draw_color({50, 60, 70, SDL_ALPHA_OPAQUE});
renderer->draw_color({50, 60, 70, SDL_ALPHA_OPAQUE}); // NOLINT
renderer->clear();
layout->render();

View File

@ -13,12 +13,12 @@ namespace bwidgets::core
struct Renderer;
struct Texture;
SDL_Color aa(const SDL_Color&, const int, const float) noexcept;
core::Texture* filled_circle(const SDL_Color&, int resolution, Renderer*,
int aa_pixels = 3);
void
set_pixels_color(Texture*,
std::function<uint32_t(const SDL_Point&, const SDL_PixelFormat*)>);
auto aa(const SDL_Color&, int, float) noexcept -> SDL_Color;
auto filled_circle(const SDL_Color&, int resolution, Renderer*, int aa_pixels = 3)
-> core::Texture*;
void set_pixels_color(
Texture*,
const std::function<uint32_t(const SDL_Point&, const SDL_PixelFormat*)>&);
}
#endif

View File

@ -41,7 +41,7 @@ namespace bwidgets::core
const int ascent;
const int descent;
const int faces;
const long faces;
const std::string family_name;
const bool fixed_width;
const int height;
@ -51,25 +51,29 @@ namespace bwidgets::core
Font(TTF_Font*);
Font(const std::string&, int);
Font(const Font&) = delete;
virtual ~Font() override;
~Font() override;
Font& operator=(const Font) = delete;
Font& operator=(const Font&) = delete;
auto operator=(const Font) -> Font& = delete;
auto operator=(const Font&) -> Font& = delete;
Hinting hinting();
Font* hinting(Hinting);
bool kerning();
Font* kerning(bool);
int outline();
Font* outline(int);
SDL_Surface* render(RenderMode, const std::string&,
const SDL_Color& fg = {0, 0, 0, 255},
const SDL_Color& bg = {255, 255, 255, 255});
uint8_t style();
Font* style(uint8_t);
Size text_size(const std::string&);
auto hinting() -> Hinting;
auto hinting(Hinting) -> Font*;
auto kerning() -> bool;
auto kerning(bool) -> Font*;
auto outline() -> int;
auto outline(int) -> Font*;
auto render(RenderMode, const std::string&,
const SDL_Color& fg = {0, 0, 0,
255}, // NOLINT(readability-magic-numbers)
// NOLINTNEXTLINE(readability-magic-numbers)
const SDL_Color& bg = {255, 255, 255,
255}) // NOLINT(readability-magic-numbers)
-> SDL_Surface*;
auto style() -> uint8_t;
auto style(uint8_t) -> Font*;
auto text_size(const std::string&) -> Size;
static std::string find(const std::string&);
static auto find(const std::string&) -> std::string;
};
}

View File

@ -9,37 +9,41 @@
namespace bwidgets::core
{
static inline int center_rect(int container_size, int rect_size) noexcept
static inline auto center_rect(int container_size, int rect_size) noexcept -> int
{
return (container_size - rect_size) / 2;
}
static inline float distance_sqrd(const SDL_Point& a, const SDL_Point& b) noexcept
static inline auto distance_sqrd(const SDL_Point& a, const SDL_Point& b) noexcept
-> float
{
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) // NOLINT(bugprone-narrowing-conversions)
+ (a.y - b.y) * (a.y - b.y);
}
static inline float distance(const SDL_Point& a, const SDL_Point& b) noexcept
static inline auto distance(const SDL_Point& a, const SDL_Point& b) noexcept -> float
{
return std::sqrt(distance_sqrd(a, b));
}
static inline bool rect_in_rect(const SDL_Rect outer, const SDL_Rect inner) noexcept
static inline auto rect_in_rect(const SDL_Rect outer, const SDL_Rect inner) noexcept
-> bool
{
SDL_Point top_left {inner.x, inner.y};
SDL_Point bottom_right {inner.x + inner.w, inner.y + inner.h};
return SDL_PointInRect(&top_left, &outer)
&& SDL_PointInRect(&bottom_right, &outer);
return (SDL_PointInRect(&top_left, &outer) == SDL_TRUE)
&& (SDL_PointInRect(&bottom_right, &outer) == SDL_TRUE);
}
static inline SDL_Rect rect_margin(const SDL_Rect& r, const Size& margin) noexcept
static inline auto rect_margin(const SDL_Rect& r, const Size& margin) noexcept
-> SDL_Rect
{
return {r.x + margin.w, r.y + margin.h, r.w - 2 * margin.w, r.h - 2 * margin.h};
}
static inline SDL_Rect rect_offset(const SDL_Rect& r,
const SDL_Rect& offset) noexcept
static inline auto rect_offset(const SDL_Rect& r, const SDL_Rect& offset) noexcept
-> SDL_Rect
{
return {r.x + offset.x, r.y + offset.y, r.w, r.h};
}

View File

@ -17,7 +17,7 @@ namespace bwidgets::core
struct Renderer final : OpaqueStruct<SDL_Renderer>
{
private:
static inline SDL_RendererInfo _info(SDL_Renderer* r)
static inline auto _info(SDL_Renderer* r) -> SDL_RendererInfo
{
SDL_RendererInfo info;
SDLError::success_or_throw(SDL_GetRendererInfo(r, &info), __FILE__,
@ -29,55 +29,59 @@ namespace bwidgets::core
public:
Renderer(SDL_Renderer*);
Renderer(SDL_Window*, int, uint32_t);
virtual ~Renderer() override;
~Renderer() override;
const SDL_RendererInfo info;
SDL_BlendMode blend_mode();
Renderer* blend_mode(SDL_BlendMode);
Renderer* clear();
Renderer* copy(Texture*, const SDL_Rect*, const SDL_Rect*);
SDL_Color draw_color();
Renderer* draw_color(const SDL_Color&);
Renderer* draw_line(const SDL_Point&, const SDL_Point&);
Renderer* draw_lines(const std::vector<SDL_Point>);
Renderer* draw_point(const SDL_Point&);
Renderer* draw_points(const std::vector<SDL_Point>);
Renderer* draw_rect(const SDL_Rect*);
Renderer* draw_rects(const std::vector<SDL_Rect>);
Renderer* fill_rect(const SDL_Rect*);
Renderer* fill_rects(const std::vector<SDL_Rect>);
Size output_size();
void present();
SDL_Rect viewport();
Renderer* viewport(const SDL_Rect*);
auto blend_mode() -> SDL_BlendMode;
auto blend_mode(SDL_BlendMode) -> Renderer*;
auto clear() -> Renderer*;
auto copy(Texture*, const SDL_Rect*, const SDL_Rect*) -> Renderer*;
auto draw_color() -> SDL_Color;
auto draw_color(const SDL_Color&) -> Renderer*;
auto draw_line(const SDL_Point&, const SDL_Point&) -> Renderer*;
auto draw_lines(const std::vector<SDL_Point>&) -> Renderer*;
auto draw_point(const SDL_Point&) -> Renderer*;
auto draw_points(const std::vector<SDL_Point>&) -> Renderer*;
auto draw_rect(const SDL_Rect*) -> Renderer*;
auto draw_rects(const std::vector<SDL_Rect>&) -> Renderer*;
auto fill_rect(const SDL_Rect*) -> Renderer*;
auto fill_rects(const std::vector<SDL_Rect>&) -> Renderer*;
auto output_size() -> Size;
void present();
auto viewport() -> SDL_Rect;
auto viewport(const SDL_Rect*) -> Renderer*;
inline Renderer* copy(Texture* t, const SDL_Rect* src, const SDL_Rect& dst)
inline auto copy(Texture* t, const SDL_Rect* src, const SDL_Rect& dst)
-> Renderer*
{
auto d = dst;
return copy(t, src, &d);
}
inline Renderer* copy(Texture* t, const SDL_Rect& src, const SDL_Rect* dst)
inline auto copy(Texture* t, const SDL_Rect& src, const SDL_Rect* dst)
-> Renderer*
{
auto s = src;
return copy(t, &s, dst);
}
inline Renderer* copy(Texture* t, const SDL_Rect& src, const SDL_Rect& dst)
inline auto copy(Texture* t, const SDL_Rect& src, const SDL_Rect& dst)
-> Renderer*
{
auto s = src, d = dst;
auto s = src;
auto d = dst;
return copy(t, &s, &d);
}
inline Renderer* draw_rect(const SDL_Rect& r)
inline auto draw_rect(const SDL_Rect& r) -> Renderer*
{
auto rect = r;
return draw_rect(&rect);
}
inline Renderer* fill_rect(const SDL_Rect& r)
inline auto fill_rect(const SDL_Rect& r) -> Renderer*
{
auto rect = r;
return fill_rect(&rect);
}
inline Renderer* viewport(const SDL_Rect& vp)
inline auto viewport(const SDL_Rect& vp) -> Renderer*
{
auto v = vp;
return viewport(&v);

View File

@ -21,31 +21,31 @@ namespace bwidgets::core
SDL_PixelFormat* format;
SDL_TextureAccess access;
int w, h;
} _attributes;
} _attributes {};
public:
Texture(SDL_Texture*);
Texture(Renderer* r, SDL_PixelFormatEnum f, SDL_TextureAccess a, int w, int h);
Texture(Renderer*, SDL_Surface*);
virtual ~Texture() noexcept override;
~Texture() noexcept override;
uint8_t alpha_mode();
Texture* alpha_mode(uint8_t);
SDL_BlendMode blend_mode();
Texture* blend_mode(SDL_BlendMode);
SDL_Color color_mode();
Texture* color_mode(const SDL_Color&);
SDL_ScaleMode scale_mode();
Texture* scale_mode(SDL_ScaleMode);
Texture* update(SDL_Rect*, const void*, int);
auto alpha_mode() -> uint8_t;
auto alpha_mode(uint8_t) -> Texture*;
auto blend_mode() -> SDL_BlendMode;
auto blend_mode(SDL_BlendMode) -> Texture*;
auto color_mode() -> SDL_Color;
auto color_mode(const SDL_Color&) -> Texture*;
auto scale_mode() -> SDL_ScaleMode;
auto scale_mode(SDL_ScaleMode) -> Texture*;
auto update(SDL_Rect*, const void*, int) -> Texture*;
inline const Attr& attributes()
[[nodiscard]] inline auto attributes() const -> const Attr&
{
return _attributes;
}
inline Texture* update(const SDL_Rect& r, const void* pix, int pitch)
inline auto update(const SDL_Rect& r, const void* pix, int pitch) -> Texture*
{
SDL_Rect rect = r;
update(&rect, pix, pitch);
@ -53,7 +53,7 @@ namespace bwidgets::core
return this;
}
static inline const Attr attributes(SDL_Texture* t)
static inline auto attributes(SDL_Texture* t) -> Attr
{
Attr attr {};
SDLError::success_or_throw(SDL_QueryTexture(t, &attr.format_raw,

View File

@ -1,5 +1,5 @@
#ifndef BWIDGETS_EXCEPTION_HPP_
#define BWIDGETS_EXCEPTION_HPP_
#ifndef BWIDGETS_EXCEPTION_HPP
#define BWIDGETS_EXCEPTION_HPP
#include <any>
#include <cstdlib>
@ -27,19 +27,19 @@ namespace bwidgets::core
concept Exception = std::derived_from<T, BaseException>;
template<Exception E, typename T>
static inline T* ptr_or_throw(T* ptr, const char* file, const char* func,
const int l, const char* w = nullptr)
static inline auto ptr_or_throw(T* ptr, const char* file, const char* func,
const int l, const char* w = nullptr) -> T*
{
if (ptr == nullptr) throw E(file, func, l, w);
return ptr;
}
template<Exception E, typename T>
static inline T success_or_throw(
static inline auto success_or_throw(
T code, const char* file, const char* func, const int l, const char* w = nullptr,
std::function<bool(const T&)> success = [](const T& code) {
return code == EXIT_SUCCESS;
})
}) -> T
{
if (!success(code)) throw E(file, func, l, w);
return code;

View File

@ -1,5 +1,5 @@
#ifndef BWIDGETS_OPAQUE_STRUCT_HPP_
#define BWIDGETS_OPAQUE_STRUCT_HPP_
#ifndef BWIDGETS_OPAQUE_STRUCT_HPP
#define BWIDGETS_OPAQUE_STRUCT_HPP
namespace bwidgets::core
{

View File

@ -4,7 +4,7 @@
#include <basic_widgets/core/type/exception.hpp>
extern "C" {
const char* SDL_GetError();
auto SDL_GetError() -> const char*;
}
namespace bwidgets::core
@ -17,18 +17,18 @@ namespace bwidgets::core
{}
template<typename T>
static inline T* ptr_or_throw(T* ptr, const char* file, const char* func,
const int l, const char* w = nullptr)
static inline auto ptr_or_throw(T* ptr, const char* file, const char* func,
const int l, const char* w = nullptr) -> T*
{
if (ptr == nullptr) {
if (w == nullptr) w = SDL_GetError();
SDLError(file, func, l, w);
throw SDLError(file, func, l, w);
}
return ptr;
}
static inline int success_or_throw(int code, const char* file, const char* func,
const int l, const char* w = nullptr)
static inline auto success_or_throw(int code, const char* file, const char* func,
const int l, const char* w = nullptr) -> int
{
if (code < 0) {
if (w == nullptr) w = SDL_GetError();

View File

@ -10,21 +10,21 @@ namespace bwidgets::core
int w;
int h;
inline Size operator-()
inline auto operator-() const -> Size
{
return {-w, -h};
}
inline Size operator+(const Size& s)
inline auto operator+(const Size& s) const -> Size
{
return {w + s.w, h + s.h};
}
inline Size operator-(const Size& s)
inline auto operator-(const Size& s) const -> Size
{
return {w - s.w, h - s.h};
}
template<Numeric T>
inline Size operator*(T a)
inline auto operator*(T a) -> Size
{
return {a * w, a * h};
}

View File

@ -35,7 +35,7 @@ namespace bwidgets::widget
_input_caption.text(value_to_string(value));
}
virtual void _handle_focus_change(bool focus) override
void _handle_focus_change(bool focus) override
{
if (focus) {
SDL_StartTextInput();
@ -47,19 +47,18 @@ namespace bwidgets::widget
}
}
virtual void _handle_font_change(core::Font* f) override
void _handle_font_change(core::Font* f) override
{
_input_caption.font(f);
_handle_geometry_change(_viewport);
}
virtual void _handle_font_color_change(const SDL_Color& fg,
const SDL_Color& bg) override
void _handle_font_color_change(const SDL_Color& fg, const SDL_Color& bg) override
{
_input_caption.color_bg(bg)->color_fg(fg);
}
virtual void _handle_geometry_change(const SDL_Rect& vp) noexcept override
void _handle_geometry_change(const SDL_Rect& vp) noexcept override
{
const auto input_h = _input_caption.size().h + 2 * _border_width;
_widget_area = {0, core::center_rect(vp.h, input_h), vp.w, input_h};
@ -68,7 +67,7 @@ namespace bwidgets::widget
{core::rect_margin(_widget_area, {_border_width, _border_width})}, vp));
}
virtual void _handle_key(const SDL_KeyboardEvent& key) override
void _handle_key(const SDL_KeyboardEvent& key) override
{
if (key.type == SDL_KEYDOWN) {
switch (key.keysym.sym) {
@ -90,12 +89,12 @@ namespace bwidgets::widget
}
}
virtual void _handle_renderer_change(core::Renderer* r) override
void _handle_renderer_change(core::Renderer* r) override
{
_input_caption.renderer(r);
}
virtual void _handle_rendering() override
void _handle_rendering() override
{
// TODO: smoothstep
for (int i = _border_width - 1; i >= 0; i--) {
@ -114,7 +113,7 @@ namespace bwidgets::widget
_input_caption.render();
}
virtual void _handle_text_input(const SDL_TextInputEvent& input) override
void _handle_text_input(const SDL_TextInputEvent& input) override
{
if (is_valid_input(input.text)) {
input_text(input_text() + input.text);
@ -122,42 +121,46 @@ namespace bwidgets::widget
}
public:
SDL_Color color_border {160, 160, 160, SDL_ALPHA_OPAQUE};
SDL_Color color_bg {200, 200, 200, SDL_ALPHA_OPAQUE};
SDL_Color color_bg_focused {255, 255, 255, SDL_ALPHA_OPAQUE};
int float_precision {2};
int input_min_width {1};
char input_width_unit {'W'};
T value {};
SDL_Color color_border {160, 160, 160, // NOLINT(readability-magic-numbers)
SDL_ALPHA_OPAQUE};
SDL_Color color_bg {200, 200, 200, // NOLINT(readability-magic-numbers)
SDL_ALPHA_OPAQUE};
SDL_Color color_bg_focused {
255, 255, 255, SDL_ALPHA_OPAQUE}; // NOLINT(readability-magic-numbers)
int float_precision {2};
int input_min_width {1};
char input_width_unit {'W'};
T value {};
virtual Input<T>* color_fg(const SDL_Color& c) noexcept
virtual auto color_fg(const SDL_Color& c) noexcept -> Input<T>*
{
_input_caption.color_fg(c);
return this;
}
virtual const std::string& input_text() const noexcept
[[nodiscard]] virtual auto input_text() const noexcept -> const std::string&
{
return _input_caption.text();
}
virtual Input<T>* input_text(std::string txt) noexcept
virtual auto input_text(const std::string& txt) noexcept -> Input<T>*
{
_input_caption.text(txt);
return this;
}
virtual bool is_valid_input(const std::string) const noexcept
[[nodiscard]] virtual auto
is_valid_input(const std::string& /*unused*/) const noexcept -> bool
{
return true;
}
virtual T process_value(T x) const noexcept
virtual auto process_value(T x) const noexcept -> T
{
return x;
}
virtual inline core::Size size() const noexcept override
[[nodiscard]] inline auto size() const noexcept -> core::Size override
{
return {
_font->text_size(std::string(input_min_width, input_width_unit)).w
@ -166,7 +169,7 @@ namespace bwidgets::widget
};
}
T value_from_string(std::string s)
auto value_from_string(std::string s) -> T
{
T v;
@ -179,13 +182,14 @@ namespace bwidgets::widget
T, std::string> || std::convertible_to<std::string, T>)
v = s;
else
static_assert(sizeof(T) && false,
static_assert((bool)sizeof(T)
&& false, // NOLINT(readability-simplify-boolean-expr)
"string cannot be converted to v type T.");
return process_value(v);
}
std::string value_to_string(T value)
auto value_to_string(T value) -> std::string
{
std::string s;
@ -203,7 +207,8 @@ namespace bwidgets::widget
s = std::move(ss).str();
}
else
static_assert(sizeof(T) && false,
static_assert((bool)sizeof(T)
&& false, // NOLINT(readability-simplify-boolean-expr)
"value cannot be converted to string.");
return s;

View File

@ -19,21 +19,22 @@ namespace bwidgets::widget
protected:
std::vector<Widget*> _widgets;
virtual void _handle_geometry_change(const SDL_Rect&) noexcept override;
virtual void _handle_renderer_change(core::Renderer*) override;
virtual void _handle_rendering() override;
void _handle_geometry_change(const SDL_Rect& /*unused*/) noexcept override;
void _handle_renderer_change(core::Renderer* /*unused*/) override;
void _handle_rendering() override;
virtual void _update_layout(const SDL_Rect&) noexcept = 0;
public:
core::Size margins {8, 8};
core::Size margins {8, 8}; // NOLINT(readability-magic-numbers)
virtual ~Layout() noexcept;
~Layout() noexcept override;
virtual Layout* add_widget(Widget*);
virtual void for_widgets(std::function<void(Widget*)>);
virtual Layout* handle_event(const SDL_Event&) override;
virtual auto add_widget(Widget*) -> Layout*;
virtual void for_widgets(const std::function<void(Widget*)>&);
auto handle_event(const SDL_Event&) // NOLINT(readability-named-parameter)
-> Layout* override;
virtual core::Size size() const noexcept override = 0;
[[nodiscard]] auto size() const noexcept -> core::Size override = 0;
};
}

View File

@ -18,7 +18,7 @@ namespace bwidgets::widget
SDL_Rect _widget_area {0, 0, 0, 0};
virtual void _handle_geometry_change(const SDL_Rect&) noexcept = 0;
virtual void _handle_renderer_change(core::Renderer*) {}
virtual void _handle_renderer_change(core::Renderer* /*unused*/) {}
virtual void _handle_rendering() = 0;
public:
@ -27,24 +27,25 @@ namespace bwidgets::widget
Widget(Widget* p = nullptr) : parent(p) {}
virtual ~Widget() noexcept = default;
virtual Widget* handle_event(const SDL_Event&);
virtual auto handle_event(const SDL_Event&) -> Widget*;
inline virtual core::Size size() const noexcept = 0;
[[nodiscard]] inline virtual auto size() const noexcept -> core::Size = 0;
virtual Widget* render() final;
virtual auto render() -> Widget* final;
virtual inline Widget* renderer(core::Renderer* r) final
virtual inline auto renderer(core::Renderer* r) -> Widget* final
{
_handle_renderer_change(r);
_renderer = r;
return this;
}
virtual inline const SDL_Rect& viewport() const noexcept final
[[nodiscard]] virtual inline auto viewport() const noexcept
-> const SDL_Rect& final
{
return _viewport;
}
virtual inline Widget* viewport(const SDL_Rect& vp) noexcept final
virtual inline auto viewport(const SDL_Rect& vp) noexcept -> Widget* final
{
_handle_geometry_change(vp);
_viewport = vp;

View File

@ -14,27 +14,29 @@ namespace bwidgets::widget
{
protected:
Caption _caption;
SDL_Rect _caption_area;
SDL_Rect _caption_area {};
SDL_Color _color_foreground {0, 0, 0, SDL_ALPHA_OPAQUE};
virtual void _handle_focus_change(bool) override {}
virtual void _handle_font_change(core::Font*) override;
virtual void _handle_font_color_change(const SDL_Color&,
const SDL_Color&) override;
virtual void _handle_geometry_change(const SDL_Rect&) noexcept override;
virtual void _handle_renderer_change(core::Renderer*) override;
virtual void _handle_rendering() override;
void _handle_focus_change(bool /*unused*/) override {}
void _handle_font_change(core::Font* /*unused*/) override;
void _handle_font_color_change(const SDL_Color& /*unused*/,
const SDL_Color& /*unused*/) override;
void _handle_geometry_change(const SDL_Rect& /*unused*/) noexcept override;
void _handle_renderer_change(core::Renderer* /*unused*/) override;
void _handle_rendering() override;
public:
core::Size border_size {3, 3};
SDL_Color color_bg {150, 150, 150, SDL_ALPHA_OPAQUE};
SDL_Color color_bg_hover {175, 175, 175, SDL_ALPHA_OPAQUE};
SDL_Color color_bg {150, 150, 150, // NOLINT(readability-magic-numbers)
SDL_ALPHA_OPAQUE};
SDL_Color color_bg_hover {175, 175, 175, // NOLINT(readability-magic-numbers)
SDL_ALPHA_OPAQUE};
Button(Widget* parent = nullptr);
virtual core::Size size() const noexcept override;
virtual const std::string& text() const noexcept;
virtual Button* text(std::string);
[[nodiscard]] auto size() const noexcept -> core::Size override;
[[nodiscard]] virtual auto text() const noexcept -> const std::string&;
virtual auto text(const std::string&) -> Button*;
};
}

View File

@ -24,34 +24,35 @@ namespace bwidgets::widget
};
protected:
SDL_Color _color_fg {0, 0, 0, SDL_ALPHA_OPAQUE};
SDL_Color _color_bg {255, 255, 255, SDL_ALPHA_TRANSPARENT};
SDL_Color _color_fg {0, 0, 0, SDL_ALPHA_OPAQUE};
SDL_Color _color_bg {255, 255, 255, // NOLINT(readability-magic-numbers)
SDL_ALPHA_TRANSPARENT};
core::Font::RenderMode _render_mode {core::Font::RenderMode::SHADED};
std::string _text;
core::Texture* _text_texture {nullptr};
virtual void _handle_font_change(core::Font*) override;
virtual void _handle_font_color_change(const SDL_Color&,
const SDL_Color&) override;
virtual void _handle_geometry_change(const SDL_Rect&) noexcept override;
virtual void _handle_renderer_change(core::Renderer*) override;
virtual void _handle_rendering() override;
virtual void _handle_texture_update() override;
void _handle_font_change(core::Font* /*unused*/) override;
void _handle_font_color_change(const SDL_Color& /*unused*/,
const SDL_Color& /*unused*/) override;
void _handle_geometry_change(const SDL_Rect& /*unused*/) noexcept override;
void _handle_renderer_change(core::Renderer* /*unused*/) override;
void _handle_rendering() override;
void _handle_texture_update() override;
public:
Alignment alignment {Alignment::LEFT};
core::Size margins {2, 2};
Caption(Widget* parent = nullptr) : Widget(parent) {}
~Caption() noexcept;
~Caption() noexcept override;
virtual Caption* color_bg(const SDL_Color&);
virtual Caption* color_fg(const SDL_Color&);
virtual Caption* render_mode(core::Font::RenderMode) noexcept;
virtual const std::string& text() const noexcept;
virtual Caption* text(const std::string&);
virtual auto color_bg(const SDL_Color&) -> Caption*;
virtual auto color_fg(const SDL_Color&) -> Caption*;
virtual auto render_mode(core::Font::RenderMode) noexcept -> Caption*;
[[nodiscard]] virtual auto text() const noexcept -> const std::string&;
virtual auto text(const std::string&) -> Caption*;
virtual core::Size size() const noexcept override;
[[nodiscard]] auto size() const noexcept -> core::Size override;
};
}

View File

@ -8,9 +8,10 @@ namespace bwidgets::widget
class FontHandler
{
protected:
core::Font* _font {nullptr};
SDL_Color _font_color_bg {255, 255, 255, SDL_ALPHA_OPAQUE};
SDL_Color _font_color_fg {0, 0, 0, SDL_ALPHA_OPAQUE};
core::Font* _font {nullptr};
SDL_Color _font_color_bg {255, 255, 255, // NOLINT(readability-magic-numbers)
SDL_ALPHA_OPAQUE};
SDL_Color _font_color_fg {0, 0, 0, SDL_ALPHA_OPAQUE};
core::Font::RenderMode _font_render_mode {core::Font::RenderMode::SHADED};
virtual void _handle_font_change(core::Font*) = 0;
@ -19,21 +20,21 @@ namespace bwidgets::widget
public:
virtual ~FontHandler() = default;
virtual inline FontHandler* font(core::Font* f) final
virtual inline auto font(core::Font* f) -> FontHandler* final
{
_handle_font_change(f);
_font = f;
return this;
}
virtual inline FontHandler* font_color_bg(const SDL_Color& c) final
virtual inline auto font_color_bg(const SDL_Color& c) -> FontHandler* final
{
_handle_font_color_change(_font_color_bg, c);
_font_color_bg = c;
return this;
}
virtual inline FontHandler* font_color_fg(const SDL_Color& c) final
virtual inline auto font_color_fg(const SDL_Color& c) -> FontHandler* final
{
_handle_font_color_change(c, _font_color_bg);
_font_color_fg = c;

View File

@ -17,10 +17,12 @@ namespace bwidgets::widget
bool _is_hovered = false;
bool _is_pushed = false;
virtual void _handle_mouse_button(const SDL_MouseButtonEvent&,
const SDL_Rect&) {};
virtual void _handle_mouse_motion(const SDL_MouseMotionEvent&,
const SDL_Rect&) {};
virtual void _handle_mouse_button(
const SDL_MouseButtonEvent&, // NOLINT(readability-named-parameter)
const SDL_Rect&) {};
virtual void _handle_mouse_motion(
const SDL_MouseMotionEvent&, // NOLINT(readability-named-parameter)
const SDL_Rect&) {};
public:
std::function<void(const SDL_MouseButtonEvent&)> click_handler = nullptr;

View File

@ -8,10 +8,10 @@ namespace bwidgets::widget
class HorizontalLayout final : public Layout
{
private:
void _update_layout(const SDL_Rect&) noexcept override;
void _update_layout(const SDL_Rect& /*unused*/) noexcept override;
public:
core::Size size() const noexcept override;
[[nodiscard]] auto size() const noexcept -> core::Size override;
};
}

View File

@ -17,32 +17,31 @@ namespace bwidgets::widget
{
private:
Button _increment_button;
SDL_Rect _increment_button_area;
SDL_Rect _increment_button_area {};
Button _decrement_button;
SDL_Rect _decrement_button_area;
SDL_Rect _decrement_button_area {};
std::pair<T, T> _value_range {std::numeric_limits<T>::lowest(),
std::numeric_limits<T>::max()};
virtual void _handle_font_change(core::Font* f) override
void _handle_font_change(core::Font* f) override
{
_decrement_button.font(f);
_increment_button.font(f);
Input<T>::_handle_font_change(f);
}
virtual void _handle_font_color_change(const SDL_Color& fg,
const SDL_Color& bg) override
void _handle_font_color_change(const SDL_Color& fg, const SDL_Color& bg) override
{
Input<T>::_handle_font_color_change(fg, bg);
_decrement_button.font_color_bg(bg)->font_color_fg(fg);
_increment_button.font_color_bg(bg)->font_color_fg(fg);
}
virtual void _handle_geometry_change(const SDL_Rect& vp) noexcept override
void _handle_geometry_change(const SDL_Rect& vp) noexcept override
{
Input<T>::_handle_geometry_change(vp);
const int widest_button = _increment_button.size().w
const int widest_button = _increment_button.size().w != 0
? _increment_button.size().w
: _decrement_button.size().w;
const int button_area_width = 2 * widest_button;
@ -68,14 +67,14 @@ namespace bwidgets::widget
_decrement_button.viewport(core::rect_offset(_decrement_button_area, vp));
}
virtual void _handle_renderer_change(core::Renderer* r) override
void _handle_renderer_change(core::Renderer* r) override
{
Input<T>::_handle_renderer_change(r);
_decrement_button.renderer(r);
_increment_button.renderer(r);
}
virtual void _handle_rendering() override
void _handle_rendering() override
{
Input<T>::_handle_rendering();
_increment_button.render();
@ -90,7 +89,7 @@ namespace bwidgets::widget
{
Input<T>::_input_caption.alignment = widget::Caption::Alignment::RIGHT;
Input<T>::input_min_width = 10;
Input<T>::input_min_width = 10; // NOLINT(readability-magic-numbers)
Input<T>::input_width_unit = '0';
_increment_button.text("+");
@ -116,7 +115,7 @@ namespace bwidgets::widget
};
}
virtual Widget* handle_event(const SDL_Event& ev) override
auto handle_event(const SDL_Event& ev) -> Widget* override
{
Widget::handle_event(ev);
_increment_button.handle_event(ev);
@ -124,7 +123,8 @@ namespace bwidgets::widget
return this;
}
virtual bool is_valid_input(const std::string input) const noexcept override
[[nodiscard]] auto is_valid_input(const std::string& input) const noexcept
-> bool override
{
bool valid = false;
@ -140,7 +140,7 @@ namespace bwidgets::widget
return valid;
}
virtual T process_value(T x) const noexcept override
auto process_value(T x) const noexcept -> T override
{
T value = x;
if (x < _value_range.first) value = _value_range.first;
@ -149,19 +149,19 @@ namespace bwidgets::widget
return value;
}
virtual inline core::Size size() const noexcept override
[[nodiscard]] inline auto size() const noexcept -> core::Size override
{
auto base = Input<T>::size();
auto btns_width = 4 * _increment_button.size().w;
return {base.w + btns_width, base.h};
}
virtual std::pair<T, T> value_range() const noexcept
virtual auto value_range() const noexcept -> std::pair<T, T>
{
return _value_range;
}
virtual NumericInput<T>* value_range(T min, T max)
virtual auto value_range(T min, T max) -> NumericInput<T>*
{
if (min > max) throw std::invalid_argument("min cannot be greater than max");
_value_range = {min, max};

View File

@ -8,10 +8,10 @@ namespace bwidgets::widget
class VerticalLayout final : public Layout
{
private:
void _update_layout(const SDL_Rect&) noexcept override;
void _update_layout(const SDL_Rect& /*unused*/) noexcept override;
public:
core::Size size() const noexcept override;
[[nodiscard]] auto size() const noexcept -> core::Size override;
};
}

View File

@ -10,26 +10,26 @@
namespace bwidgets::core
{
SDL_Color aa(const SDL_Color& base_color, const int aa_pixels,
const float d) noexcept
auto aa(const SDL_Color& base_color, const int aa_pixels, const float d) noexcept
-> SDL_Color
{
SDL_Color c {base_color};
if (aa_pixels == 0) {
if (d > 0) c.a = 0;
}
else {
const auto d_clamp {std::abs(std::clamp<float>(d, -aa_pixels, 0))};
const auto d_norm {d_clamp / aa_pixels};
const auto d_clamp {std::abs(std::clamp<float>(d, (float)-aa_pixels, 0))};
const auto d_norm {d_clamp / (float)aa_pixels};
const auto factor {3 * d_norm * d_norm - 2 * d_norm * d_norm * d_norm};
c.a *= factor;
c.a *= (uint8_t)factor;
}
return c;
}
core::Texture* filled_circle(const SDL_Color& c, const int resolution,
core::Renderer* r, const int aa_pixels)
auto filled_circle(const SDL_Color& c, const int resolution, core::Renderer* r,
const int aa_pixels) -> core::Texture*
{
auto* texture {new core::Texture(
r, SDL_PIXELFORMAT_RGBA32, SDL_TEXTUREACCESS_STATIC, resolution, resolution)};
@ -40,7 +40,7 @@ namespace bwidgets::core
texture,
[aa_pixels, c, center, radius](const SDL_Point& p,
const SDL_PixelFormat* format) -> uint32_t {
const auto d_delta = core::distance(center, p) - radius;
const auto d_delta = core::distance(center, p) - (float)radius;
const auto aa_color = aa(c, aa_pixels, d_delta);
return SDL_MapRGBA(format, aa_color.r, aa_color.g, aa_color.b, aa_color.a);
@ -52,14 +52,14 @@ namespace bwidgets::core
}
void set_pixels_color(
core::Texture* t,
std::function<Uint32(const SDL_Point&, const SDL_PixelFormat*)> pixel_color)
core::Texture* t,
const std::function<Uint32(const SDL_Point&, const SDL_PixelFormat*)>& pixel_color)
{
auto attr = t->attributes();
auto pitch = attr.w * attr.format->BytesPerPixel;
std::vector<Uint32> pixels;
pixels.reserve(attr.h * pitch);
std::vector<uint32_t> pixels;
pixels.reserve(attr.h * (std::vector<uint32_t>::size_type)pitch);
for (auto y = 0; y < attr.h; y++) {
for (auto x = 0; x < attr.w; x++) {
pixels.push_back(pixel_color({x, y}, attr.format));

View File

@ -16,7 +16,7 @@ namespace bwidgets::core
descent(TTF_FontDescent(f)),
faces(TTF_FontFaces(f)),
family_name(TTF_FontFaceFamilyName(f)),
fixed_width(TTF_FontFaceIsFixedWidth(f)),
fixed_width(TTF_FontFaceIsFixedWidth(f) != 0),
height(TTF_FontHeight(f)),
line_skip(TTF_FontLineSkip(f)),
style_name(TTF_FontFaceStyleName(f))
@ -32,41 +32,41 @@ namespace bwidgets::core
TTF_CloseFont(c_pod);
}
Font::Hinting Font::hinting()
auto Font::hinting() -> Font::Hinting
{
return (Hinting)TTF_GetFontHinting(c_pod);
}
Font* Font::hinting(Font::Hinting h)
auto Font::hinting(Font::Hinting h) -> Font*
{
TTF_SetFontHinting(c_pod, (int)h);
return this;
}
bool Font::kerning()
auto Font::kerning() -> bool
{
return TTF_GetFontHinting(c_pod);
return TTF_GetFontHinting(c_pod) != 0;
}
Font* Font::kerning(bool allowed)
auto Font::kerning(bool allowed) -> Font*
{
TTF_SetFontKerning(c_pod, allowed);
TTF_SetFontKerning(c_pod, static_cast<int>(allowed));
return this;
}
int Font::outline()
auto Font::outline() -> int
{
return TTF_GetFontOutline(c_pod);
}
Font* Font::outline(int size)
auto Font::outline(int size) -> Font*
{
TTF_SetFontOutline(c_pod, size);
return this;
}
SDL_Surface* Font::render(RenderMode m, const std::string& str, const SDL_Color& fg,
const SDL_Color& bg)
auto Font::render(RenderMode m, const std::string& str, const SDL_Color& fg,
const SDL_Color& bg) -> SDL_Surface*
{
std::function<SDL_Surface*()> renderer;
const char* c_str = str.empty() ? " " : str.c_str();
@ -92,25 +92,25 @@ namespace bwidgets::core
return ptr_or_throw<SDLError>(renderer(), __FILE__, __FUNCTION__, __LINE__);
}
uint8_t Font::style()
auto Font::style() -> uint8_t
{
return TTF_GetFontStyle(c_pod);
}
Font* Font::style(uint8_t s)
auto Font::style(uint8_t s) -> Font*
{
TTF_SetFontStyle(c_pod, s);
return this;
}
Size Font::text_size(const std::string& str)
auto Font::text_size(const std::string& str) -> Size
{
Size s;
Size s {};
TTF_SizeUTF8(c_pod, str.c_str(), &s.w, &s.h);
return s;
}
std::string Font::find(const std::string& pat)
auto Font::find(const std::string& pat) -> std::string
{
std::vector<std::any> ptrs;
@ -127,27 +127,27 @@ namespace bwidgets::core
FcConfig* conf = ptr_or_throw<FCError>(FcInitLoadConfigAndFonts(), __FILE__,
__FUNCTION__, __LINE__, "init failed");
ptrs.push_back(conf);
ptrs.emplace_back(conf);
FcPattern* pattern;
FcPattern* pattern = nullptr;
try {
pattern =
ptr_or_throw<FCError>(FcNameParse((FcChar8*)pat.c_str()), __FILE__,
__FUNCTION__, __LINE__, "pattern parsing failed");
ptrs.push_back(pattern);
ptrs.emplace_back(pattern);
if (FcConfigSubstitute(conf, pattern, FcMatchPattern) == FcFalse)
throw FCError {__FILE__, __FUNCTION__, __LINE__,
"FcConfigSubstitute failed"};
} catch (const std::exception& e) {
clean(ptrs.size());
clean((int)ptrs.size());
throw e;
}
FcDefaultSubstitute(pattern);
std::string file_path;
FcResult res;
FcResult res {};
FcPattern* font = FcFontMatch(conf, pattern, &res);
if (font != nullptr) {
FcChar8* file = nullptr;
@ -157,7 +157,7 @@ namespace bwidgets::core
FcPatternDestroy(font);
}
clean(ptrs.size());
clean((int)ptrs.size());
if (file_path.empty())
throw FCError {__FILE__, __FUNCTION__, __LINE__, "no font found"};

View File

@ -15,16 +15,16 @@ core::Renderer::~Renderer()
SDL_DestroyRenderer(c_pod);
}
SDL_BlendMode core::Renderer::blend_mode()
auto core::Renderer::blend_mode() -> SDL_BlendMode
{
SDL_BlendMode mode;
SDL_BlendMode mode {};
SDLError::success_or_throw(SDL_GetRenderDrawBlendMode(c_pod, &mode), __FILE__,
__FUNCTION__, __LINE__);
return mode;
}
core::Renderer* core::Renderer::blend_mode(SDL_BlendMode mode)
auto core::Renderer::blend_mode(SDL_BlendMode mode) -> core::Renderer*
{
SDLError::success_or_throw(SDL_SetRenderDrawBlendMode(c_pod, mode), __FILE__,
__FUNCTION__, __LINE__);
@ -32,15 +32,15 @@ core::Renderer* core::Renderer::blend_mode(SDL_BlendMode mode)
return this;
}
core::Renderer* core::Renderer::clear()
auto core::Renderer::clear() -> core::Renderer*
{
SDLError::success_or_throw(SDL_RenderClear(c_pod), __FILE__, __FUNCTION__, __LINE__);
return this;
}
core::Renderer* core::Renderer::copy(core::Texture* t, const SDL_Rect* src,
const SDL_Rect* dst)
auto core::Renderer::copy(core::Texture* t, const SDL_Rect* src, const SDL_Rect* dst)
-> core::Renderer*
{
SDLError::success_or_throw(SDL_RenderCopy(c_pod, t->c_pod, src, dst), __FILE__,
__FUNCTION__, __LINE__);
@ -48,7 +48,7 @@ core::Renderer* core::Renderer::copy(core::Texture* t, const SDL_Rect* src,
return this;
}
SDL_Color core::Renderer::draw_color()
auto core::Renderer::draw_color() -> SDL_Color
{
SDL_Color c;
SDLError::success_or_throw(SDL_GetRenderDrawColor(c_pod, &c.r, &c.g, &c.b, &c.a),
@ -57,7 +57,7 @@ SDL_Color core::Renderer::draw_color()
return c;
}
core::Renderer* core::Renderer::draw_color(const SDL_Color& c)
auto core::Renderer::draw_color(const SDL_Color& c) -> core::Renderer*
{
SDLError::success_or_throw(SDL_SetRenderDrawColor(c_pod, c.r, c.g, c.b, c.a),
__FILE__, __FUNCTION__, __LINE__);
@ -65,7 +65,7 @@ core::Renderer* core::Renderer::draw_color(const SDL_Color& c)
return this;
}
core::Renderer* core::Renderer::draw_line(const SDL_Point& a, const SDL_Point& b)
auto core::Renderer::draw_line(const SDL_Point& a, const SDL_Point& b) -> core::Renderer*
{
SDLError::success_or_throw(SDL_RenderDrawLine(c_pod, a.x, a.y, b.x, b.y), __FILE__,
__FUNCTION__, __LINE__);
@ -73,15 +73,15 @@ core::Renderer* core::Renderer::draw_line(const SDL_Point& a, const SDL_Point& b
return this;
}
core::Renderer* core::Renderer::draw_lines(std::vector<SDL_Point> pts)
auto core::Renderer::draw_lines(const std::vector<SDL_Point>& pts) -> core::Renderer*
{
SDLError::success_or_throw(SDL_RenderDrawLines(c_pod, pts.data(), pts.size()),
SDLError::success_or_throw(SDL_RenderDrawLines(c_pod, pts.data(), (int)pts.size()),
__FILE__, __FUNCTION__, __LINE__);
return this;
}
core::Renderer* core::Renderer::draw_point(const SDL_Point& p)
auto core::Renderer::draw_point(const SDL_Point& p) -> core::Renderer*
{
SDLError::success_or_throw(SDL_RenderDrawPoint(c_pod, p.x, p.y), __FILE__,
__FUNCTION__, __LINE__);
@ -89,25 +89,26 @@ core::Renderer* core::Renderer::draw_point(const SDL_Point& p)
return this;
}
core::Renderer* core::Renderer::draw_points(const std::vector<SDL_Point> pts)
auto core::Renderer::draw_points(const std::vector<SDL_Point>& pts) -> core::Renderer*
{
SDLError::success_or_throw(SDL_RenderDrawPoints(c_pod, pts.data(), pts.size()),
SDLError::success_or_throw(SDL_RenderDrawPoints(c_pod, pts.data(), (int)pts.size()),
__FILE__, __FUNCTION__, __LINE__);
return this;
}
core::Renderer* core::Renderer::draw_rect(const SDL_Rect* r)
auto core::Renderer::draw_rect(const SDL_Rect* r) -> core::Renderer*
{
auto vp = viewport();
// Has glitch at top-left and bottom-right corner.
// The first corner has an extra pixel at y-1 and surimpression.
// The second corner is missing a pixel.
if (r) viewport({vp.x + r->x, vp.y + r->y, r->w - 1, r->h - 1}); // crop extra pixel
if (r != nullptr)
viewport({vp.x + r->x, vp.y + r->y, r->w - 1, r->h - 1}); // crop extra pixel
SDLError::success_or_throw(SDL_RenderDrawRect(c_pod, nullptr), __FILE__,
__FUNCTION__, __LINE__);
if (r) draw_point({r->w - 1, r->h - 1}); // add missing pixel
if (r != nullptr) draw_point({r->w - 1, r->h - 1}); // add missing pixel
else draw_point({vp.w - 1, vp.h - 1});
viewport(vp);
@ -115,15 +116,15 @@ core::Renderer* core::Renderer::draw_rect(const SDL_Rect* r)
return this;
}
core::Renderer* core::Renderer::draw_rects(const std::vector<SDL_Rect> rs)
auto core::Renderer::draw_rects(const std::vector<SDL_Rect>& rs) -> core::Renderer*
{
SDLError::success_or_throw(SDL_RenderDrawRects(c_pod, rs.data(), rs.size()),
SDLError::success_or_throw(SDL_RenderDrawRects(c_pod, rs.data(), (int)rs.size()),
__FILE__, __FUNCTION__, __LINE__);
return this;
}
core::Renderer* core::Renderer::fill_rect(const SDL_Rect* r)
auto core::Renderer::fill_rect(const SDL_Rect* r) -> core::Renderer*
{
SDLError::success_or_throw(SDL_RenderFillRect(c_pod, r), __FILE__, __FUNCTION__,
__LINE__);
@ -131,17 +132,17 @@ core::Renderer* core::Renderer::fill_rect(const SDL_Rect* r)
return this;
}
core::Renderer* core::Renderer::fill_rects(const std::vector<SDL_Rect> rs)
auto core::Renderer::fill_rects(const std::vector<SDL_Rect>& rs) -> core::Renderer*
{
SDLError::success_or_throw(SDL_RenderFillRects(c_pod, rs.data(), rs.size()),
SDLError::success_or_throw(SDL_RenderFillRects(c_pod, rs.data(), (int)rs.size()),
__FILE__, __FUNCTION__, __LINE__);
return this;
}
core::Size core::Renderer::output_size()
auto core::Renderer::output_size() -> core::Size
{
Size s;
Size s {};
SDLError::success_or_throw(SDL_GetRendererOutputSize(c_pod, &s.w, &s.h), __FILE__,
__FUNCTION__, __LINE__);
@ -153,7 +154,7 @@ void core::Renderer::present()
SDL_RenderPresent(c_pod);
}
SDL_Rect core::Renderer::viewport()
auto core::Renderer::viewport() -> SDL_Rect
{
SDL_Rect vp;
SDL_RenderGetViewport(c_pod, &vp);
@ -161,7 +162,7 @@ SDL_Rect core::Renderer::viewport()
return vp;
}
core::Renderer* core::Renderer::viewport(const SDL_Rect* vp)
auto core::Renderer::viewport(const SDL_Rect* vp) -> core::Renderer*
{
SDLError::success_or_throw(SDL_RenderSetViewport(c_pod, vp), __FILE__, __FUNCTION__,
__LINE__);

View File

@ -27,16 +27,16 @@ core::Texture::~Texture() noexcept
c_pod = nullptr;
}
uint8_t core::Texture::alpha_mode()
auto core::Texture::alpha_mode() -> uint8_t
{
uint8_t mode;
uint8_t mode = 0;
SDLError::success_or_throw(SDL_GetTextureAlphaMod(c_pod, &mode), __FILE__,
__FUNCTION__, __LINE__);
return mode;
}
core::Texture* core::Texture::alpha_mode(uint8_t m)
auto core::Texture::alpha_mode(uint8_t m) -> core::Texture*
{
SDLError::success_or_throw(SDL_SetTextureAlphaMod(c_pod, m), __FILE__, __FUNCTION__,
__LINE__);
@ -44,16 +44,16 @@ core::Texture* core::Texture::alpha_mode(uint8_t m)
return this;
}
SDL_BlendMode core::Texture::blend_mode()
auto core::Texture::blend_mode() -> SDL_BlendMode
{
SDL_BlendMode mode;
SDL_BlendMode mode {};
SDLError::success_or_throw(SDL_GetTextureBlendMode(c_pod, &mode), __FILE__,
__FUNCTION__, __LINE__);
return mode;
}
core::Texture* core::Texture::blend_mode(SDL_BlendMode m)
auto core::Texture::blend_mode(SDL_BlendMode m) -> core::Texture*
{
SDLError::success_or_throw(SDL_SetTextureBlendMode(c_pod, m), __FILE__, __FUNCTION__,
__LINE__);
@ -61,7 +61,7 @@ core::Texture* core::Texture::blend_mode(SDL_BlendMode m)
return this;
}
SDL_Color core::Texture::color_mode()
auto core::Texture::color_mode() -> SDL_Color
{
SDL_Color mode;
SDLError::success_or_throw(SDL_GetTextureColorMod(c_pod, &mode.r, &mode.g, &mode.b),
@ -70,7 +70,7 @@ SDL_Color core::Texture::color_mode()
return mode;
}
core::Texture* core::Texture::color_mode(const SDL_Color& m)
auto core::Texture::color_mode(const SDL_Color& m) -> core::Texture*
{
SDLError::success_or_throw(SDL_SetTextureColorMod(c_pod, m.r, m.g, m.b), __FILE__,
__FUNCTION__, __LINE__);
@ -78,16 +78,16 @@ core::Texture* core::Texture::color_mode(const SDL_Color& m)
return this;
}
SDL_ScaleMode core::Texture::scale_mode()
auto core::Texture::scale_mode() -> SDL_ScaleMode
{
SDL_ScaleMode mode;
SDL_ScaleMode mode {};
SDLError::success_or_throw(SDL_GetTextureScaleMode(c_pod, &mode), __FILE__,
__FUNCTION__, __LINE__);
return mode;
}
core::Texture* core::Texture::scale_mode(SDL_ScaleMode m)
auto core::Texture::scale_mode(SDL_ScaleMode m) -> core::Texture*
{
SDLError::success_or_throw(SDL_SetTextureScaleMode(c_pod, m), __FILE__, __FUNCTION__,
__LINE__);
@ -95,7 +95,7 @@ core::Texture* core::Texture::scale_mode(SDL_ScaleMode m)
return this;
}
core::Texture* core::Texture::update(SDL_Rect* r, const void* pixels, int pitch)
auto core::Texture::update(SDL_Rect* r, const void* pixels, int pitch) -> core::Texture*
{
SDLError::success_or_throw(SDL_UpdateTexture(c_pod, r, pixels, pitch), __FILE__,
__FUNCTION__, __LINE__);

View File

@ -7,19 +7,19 @@ widget::Layout::~Layout() noexcept
for (Widget* widget_ptr : _widgets) delete widget_ptr;
}
widget::Layout* widget::Layout::add_widget(Widget* widget_ptr)
auto widget::Layout::add_widget(Widget* widget_ptr) -> widget::Layout*
{
widget_ptr->renderer(_renderer);
_widgets.push_back(widget_ptr);
return this;
}
void widget::Layout::for_widgets(std::function<void(Widget*)> f)
void widget::Layout::for_widgets(const std::function<void(Widget*)>& f)
{
for (auto* w : _widgets) f(w);
}
widget::Layout* widget::Layout::handle_event(const SDL_Event& ev)
auto widget::Layout::handle_event(const SDL_Event& ev) -> widget::Layout*
{
for (Widget* widget_ptr : _widgets) widget_ptr->handle_event(ev);
return this;

View File

@ -7,9 +7,9 @@
using namespace bwidgets;
widget::Widget* widget::Widget::handle_event(const SDL_Event& ev)
auto widget::Widget::handle_event(const SDL_Event& ev) -> widget::Widget*
{
if (auto handler = dynamic_cast<KeyboardHandler*>(this); handler != nullptr) {
if (auto* handler = dynamic_cast<KeyboardHandler*>(this); handler != nullptr) {
switch (ev.type) {
case SDL_KEYDOWN:
case SDL_KEYUP:
@ -21,7 +21,7 @@ widget::Widget* widget::Widget::handle_event(const SDL_Event& ev)
}
}
if (auto handler = dynamic_cast<MouseHandler*>(this); handler != nullptr) {
if (auto* handler = dynamic_cast<MouseHandler*>(this); handler != nullptr) {
switch (ev.type) {
case SDL_MOUSEBUTTONDOWN:
case SDL_MOUSEBUTTONUP:
@ -36,10 +36,10 @@ widget::Widget* widget::Widget::handle_event(const SDL_Event& ev)
return this;
}
widget::Widget* widget::Widget::render()
auto widget::Widget::render() -> widget::Widget*
{
SDL_assert_release(_renderer);
if (!_renderer) return this;
SDL_assert_release(_renderer); // NOLINT
if (_renderer == nullptr) return this;
#ifdef _NDEBUG
_renderer->draw_color({0, 255, 0, SDL_ALPHA_TRANSPARENT})->draw_rect(nullptr);

View File

@ -11,18 +11,18 @@ widget::Button::Button(Widget* parent) : Widget(parent), _caption(this)
_caption.alignment = Caption::Alignment::CENTER;
}
core::Size widget::Button::size() const noexcept
auto widget::Button::size() const noexcept -> core::Size
{
return {_caption.size().w + 2 * border_size.w,
_caption.size().h + 2 * border_size.h};
}
const std::string& widget::Button::text() const noexcept
auto widget::Button::text() const noexcept -> const std::string&
{
return _caption.text();
}
widget::Button* widget::Button::text(std::string txt)
auto widget::Button::text(const std::string& txt) -> widget::Button*
{
_caption.text(txt);
_handle_geometry_change(_viewport);
@ -59,17 +59,19 @@ void widget::Button::_handle_renderer_change(core::Renderer* r)
_caption.renderer(r);
}
// NOLINTNEXTLINE(readability-function-cognitive-complexity)
void widget::Button::_handle_rendering()
{
// TODO: smoothstep it
auto color_shade = [](int base, int step, int total_steps) -> uint8_t {
int steps_to_max = 255 - base;
int steps_to_max = 255 - base; // NOLINT(readability-magic-numbers)
return base + step * steps_to_max / total_steps;
};
SDL_Color c = _is_hovered ? color_bg_hover : color_bg;
if (_is_pushed) {
auto x = 0, y = 0;
auto x = 0;
auto y = 0;
auto biggest = border_size.w > border_size.h ? border_size.w : border_size.h;
while (x < border_size.w || y < border_size.h) {
auto max = x > y ? x : y;
@ -84,7 +86,8 @@ void widget::Button::_handle_rendering()
}
}
else {
auto x = 0, y = 0;
auto x = 0;
auto y = 0;
auto biggest = border_size.w > border_size.h ? border_size.w : border_size.h;
while (x < border_size.w || y < border_size.h) {
auto max = x > y ? x : y;

View File

@ -11,28 +11,28 @@ widget::Caption::~Caption() noexcept
core::discard(_text_texture);
}
widget::Caption* widget::Caption::color_bg(const SDL_Color& c)
auto widget::Caption::color_bg(const SDL_Color& c) -> widget::Caption*
{
_color_bg = c;
core::discard(_text_texture);
return this;
}
widget::Caption* widget::Caption::color_fg(const SDL_Color& c)
auto widget::Caption::color_fg(const SDL_Color& c) -> widget::Caption*
{
_color_fg = c;
core::discard(_text_texture);
return this;
}
widget::Caption* widget::Caption::render_mode(core::Font::RenderMode m) noexcept
auto widget::Caption::render_mode(core::Font::RenderMode m) noexcept -> widget::Caption*
{
_render_mode = m;
core::discard(_text_texture);
return this;
}
core::Size widget::Caption::size() const noexcept
auto widget::Caption::size() const noexcept -> core::Size
{
if (_font == nullptr) return {-1, -1};
@ -41,24 +41,24 @@ core::Size widget::Caption::size() const noexcept
return {size.w + 2 * margins.w, size.h + 2 * margins.h};
}
const std::string& widget::Caption::text() const noexcept
auto widget::Caption::text() const noexcept -> const std::string&
{
return _text;
}
widget::Caption* widget::Caption::text(const std::string& t)
auto widget::Caption::text(const std::string& t) -> widget::Caption*
{
_text = t;
core::discard(_text_texture);
return this;
}
void widget::Caption::_handle_font_change(core::Font*)
void widget::Caption::_handle_font_change(core::Font* /*unused*/)
{
core::discard(_text_texture);
}
static inline bool operator!=(const SDL_Color& a, const SDL_Color& b)
static inline auto operator!=(const SDL_Color& a, const SDL_Color& b) -> bool
{
return a.r != b.r || a.g != b.g || a.b != b.b || a.a != b.a;
}
@ -79,24 +79,25 @@ void widget::Caption::_handle_geometry_change(const SDL_Rect& vp) noexcept
}
}
void widget::Caption::_handle_renderer_change(core::Renderer*)
void widget::Caption::_handle_renderer_change(core::Renderer* /*unused*/)
{
core::discard(_text_texture);
}
void widget::Caption::_handle_rendering()
{
if (!_text_texture) _handle_texture_update();
if (_text_texture == nullptr) _handle_texture_update();
if (_render_mode == core::Font::RenderMode::SHADED)
_renderer->draw_color(_color_bg)->fill_rect({0, 0, _viewport.w, _viewport.h});
if (!_text_texture) _handle_texture_update();
if (_text_texture == nullptr) _handle_texture_update();
core::Size size_dst {(int)((float)_text_texture->attributes().w
/ _text_texture->attributes().h * _widget_area.h),
_widget_area.h};
SDL_Rect texture_dst {margins.w, margins.h, size_dst.w, size_dst.h};
core::Size size_dst {
(int)((float)_text_texture->attributes().w / (float)_text_texture->attributes().h
* _widget_area.h), // NOLINT(bugprone-narrowing-conversions)
_widget_area.h};
SDL_Rect texture_dst {margins.w, margins.h, size_dst.w, size_dst.h};
switch (alignment) {
case Alignment::CENTER:
texture_dst.x =
@ -108,14 +109,14 @@ void widget::Caption::_handle_rendering()
texture_dst.x = _widget_area.w - texture_dst.w - margins.w + _widget_area.x;
break;
}
_renderer->copy(_text_texture, NULL, texture_dst);
_renderer->copy(_text_texture, nullptr, texture_dst);
}
void widget::Caption::_handle_texture_update()
{
SDL_assert_release(_font);
SDL_assert_release(_font); // NOLINT
core::discard(_text_texture);
SDL_Surface* s;
SDL_Surface* s = nullptr;
switch (_render_mode) {
case core::Font::RenderMode::SHADED:
s = _font->render(_render_mode, _text, _color_fg, _color_bg);

View File

@ -15,7 +15,7 @@ void widget::MouseHandler::handle_mouse(const SDL_MouseButtonEvent& ev,
SDL_Rect vp {core::rect_offset(*_click_area, orig)};
if (ev.type == SDL_MOUSEBUTTONDOWN) {
if (SDL_PointInRect(&p, &vp)) {
if (SDL_PointInRect(&p, &vp) == SDL_TRUE) {
_is_pushed = true;
_handle_mouse_button(ev, vp);
}
@ -23,7 +23,7 @@ void widget::MouseHandler::handle_mouse(const SDL_MouseButtonEvent& ev,
}
else {
if (_is_pushed) {
if (SDL_PointInRect(&p, &vp)) {
if (SDL_PointInRect(&p, &vp) == SDL_TRUE) {
focus(true);
if (click_handler != nullptr) click_handler(ev);
@ -42,8 +42,7 @@ void widget::MouseHandler::handle_mouse(const SDL_MouseMotionEvent& ev,
SDL_Point p {ev.x, ev.y};
SDL_Rect vp {core::rect_offset(*_click_area, orig)};
if (SDL_PointInRect(&p, &vp)) _is_hovered = true;
else _is_hovered = false;
_is_hovered = SDL_PointInRect(&p, &vp) != 0;
if (_is_hovered) _handle_mouse_motion(ev, orig);
}

View File

@ -3,7 +3,7 @@
using namespace bwidgets;
core::Size widget::HorizontalLayout::size() const noexcept
auto widget::HorizontalLayout::size() const noexcept -> core::Size
{
core::Size max {0, 0};
@ -18,7 +18,8 @@ core::Size widget::HorizontalLayout::size() const noexcept
void widget::HorizontalLayout::_update_layout(const SDL_Rect& vp) noexcept
{
int widget_size = (vp.w - (_widgets.size() + 1) * margins.w) / _widgets.size();
int widget_size =
(int)((vp.w - (_widgets.size() + 1) * margins.w) / _widgets.size());
for (std::vector<Widget*>::size_type i = 0; i < _widgets.size(); i++) {
auto* w = _widgets[i];

View File

@ -3,7 +3,7 @@
using namespace bwidgets;
core::Size widget::VerticalLayout::size() const noexcept
auto widget::VerticalLayout::size() const noexcept -> core::Size
{
core::Size size {0, 0};