constness

This commit is contained in:
Andrea Blankenstijn 2021-08-08 14:30:56 +02:00
parent cab6fa0cf4
commit 4f82826bbc
22 changed files with 67 additions and 63 deletions

View file

@ -22,12 +22,12 @@ private:
void _handle_rendering() override
{
auto now = SDL_GetTicks();
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
const auto now = SDL_GetTicks();
const uint8_t r = 255 * (now % cycle_r / (float)cycle_r); // NOLINT
const uint8_t g = 255 * (now % cycle_g / (float)cycle_g); // NOLINT
const uint8_t b = 255 * (now % cycle_b / (float)cycle_b); // NOLINT
// NOLINTNEXTLINE(readability-magic-numbers)
Color base_color {r, g, b, 255};
const Color base_color {r, g, b, 255};
const int border = 10;
for (auto i = 0; i < border; i += 3) {

View file

@ -17,8 +17,8 @@ template<typename T>
concept WidgetType = std::derived_from<T, bwidgets::Widget>;
template<WidgetType W>
void run_example(std::function<void(W*, bwidgets::Font*, int, int)> setup, int w = 3,
int h = 3)
void run_example(const std::function<void(W*, bwidgets::Font*, int, int)>& setup,
int w = 3, int h = 3)
{
std::stack<std::function<void()>> cleaners;
try {
@ -70,7 +70,7 @@ void run_example(std::function<void(W*, bwidgets::Font*, int, int)> setup, int w
break;
case SDL_WINDOWEVENT:
if (ev.window.event == SDL_WINDOWEVENT_SIZE_CHANGED) {
auto size = renderer->output_size();
const auto size = renderer->output_size();
layout->viewport({0, 0, size.w, size.h});
}
break;

View file

@ -56,17 +56,17 @@ namespace bwidgets
auto operator=(const Font&) -> Font& = delete;
[[nodiscard]] auto hinting() -> Hinting;
[[nodiscard]] auto hinting() const -> Hinting;
auto hinting(Hinting) -> Font*;
[[nodiscard]] auto kerning() -> bool;
[[nodiscard]] auto kerning() const -> bool;
auto kerning(bool) -> Font*;
[[nodiscard]] auto outline() -> int;
[[nodiscard]] auto outline() const -> int;
auto outline(int) -> Font*;
auto render(RenderMode, const std::string&, const Color& fg = default_color_fg,
const Color& bg = default_color_bg) -> SDL_Surface*;
[[nodiscard]] auto style() -> uint8_t;
[[nodiscard]] auto style() const -> uint8_t;
auto style(uint8_t) -> Font*;
[[nodiscard]] auto text_size(const std::string&) -> Size;
[[nodiscard]] auto text_size(const std::string&) const -> Size;
[[nodiscard]] static auto find(const std::string&) -> std::string;
};

View file

@ -50,7 +50,7 @@ namespace bwidgets
}
// NOLINTNEXTLINE(clang-diagnostic-unused-function)
[[nodiscard]] static inline auto rect_in_rect(const SDL_Rect outer, const SDL_Rect inner) noexcept
[[nodiscard]] static inline auto rect_in_rect(const SDL_Rect& outer, const SDL_Rect& inner) noexcept
-> bool
{
SDL_Point top_left {inner.x, inner.y};

View file

@ -57,33 +57,33 @@ namespace bwidgets
inline auto* copy(Texture* t, const SDL_Rect* src, const SDL_Rect& dst)
{
auto d = dst;
const auto d = dst;
return copy(t, src, &d);
}
inline auto* copy(Texture* t, const SDL_Rect& src, const SDL_Rect* dst)
{
auto s = src;
const auto s = src;
return copy(t, &s, dst);
}
inline auto* copy(Texture* t, const SDL_Rect& src, const SDL_Rect& dst)
{
auto s = src;
auto d = dst;
const auto s = src;
const auto d = dst;
return copy(t, &s, &d);
}
inline auto* draw_rect(const SDL_Rect& r)
{
auto rect = r;
const auto rect = r;
return draw_rect(&rect);
}
inline auto* fill_rect(const SDL_Rect& r)
{
auto rect = r;
const auto rect = r;
return fill_rect(&rect);
}
inline auto* viewport(const SDL_Rect& vp)
{
auto v = vp;
const auto v = vp;
return viewport(&v);
}
};

View file

@ -26,8 +26,8 @@ namespace bwidgets
concept Exception = std::derived_from<T, BaseException>;
template<Exception E, typename T>
static inline auto ptr_or_throw(T* ptr, const char* file, const char* func,
const int l, const char* w = nullptr) -> T*
static inline auto ptr_or_throw(T* ptr, const char* file, const char* func, int l,
const char* w = nullptr) -> T*
{
if (ptr == nullptr) throw E(file, func, l, w);
return ptr;
@ -35,7 +35,7 @@ namespace bwidgets
template<Exception E, typename T>
static inline auto success_or_throw(
T code, const char* file, const char* func, const int l, const char* w = nullptr,
T code, const char* file, const char* func, int l, const char* w = nullptr,
std::function<bool(const T&)> success = [](const T& code) { return code == 0; })
-> T
{

View file

@ -7,7 +7,7 @@ namespace bwidgets
{
struct FCError final : BaseException
{
FCError(const char* file, const char* func, const int l, const char* w = nullptr)
FCError(const char* file, const char* func, int l, const char* w = nullptr)
: BaseException(file, func, l, w)
{}
};

View file

@ -18,7 +18,7 @@ namespace bwidgets
template<typename T>
[[nodiscard]] static inline auto ptr_or_throw(T* ptr, const char* file,
const char* func, const int l,
const char* func, int l,
const char* w = nullptr) -> T*
{
if (!ptr) {
@ -29,7 +29,7 @@ namespace bwidgets
}
static inline auto success_or_throw(int code, const char* file, const char* func,
const int l, const char* w = nullptr) -> int
int l, const char* w = nullptr) -> int
{
if (code < 0) {
if (w == nullptr) w = SDL_GetError();

View file

@ -119,15 +119,18 @@ namespace bwidgets
}
public:
static const int border_width = 3;
static const int default_border_width = 3;
inline static const Color default_color_bg = {200, 200, 200, SDL_ALPHA_OPAQUE};
inline static const Color default_color_bg_focused = {255, 255, 255,
SDL_ALPHA_OPAQUE};
static const int default_float_precision = 2;
static const int default_min_width = 1;
int border_width = default_border_width;
Color color_bg = default_color_bg;
Color color_bg_focused = default_color_bg_focused;
int float_precision = 2;
int input_min_width = 1;
int float_precision = default_float_precision;
int input_min_width = default_min_width;
char input_width_unit = 'W';
T value {};

View file

@ -37,6 +37,7 @@ namespace bwidgets
inline static const Color default_color_bg = {255, 255, 255, SDL_ALPHA_OPAQUE};
inline static const Color default_color_fg = {0, 0, 0, SDL_ALPHA_OPAQUE};
inline static const Size default_margins = {3, 3};
Alignment alignment {Alignment::LEFT};
Size margins {3, 3};

View file

@ -20,6 +20,7 @@ namespace bwidgets
inline static const Color default_font_color_bg {255, 255, 255,
SDL_ALPHA_OPAQUE};
inline static const Color default_font_color_fg {0, 0, 0, SDL_ALPHA_OPAQUE};
virtual ~FontHandler() = default;
virtual inline auto font(Font* f) -> FontHandler* final

View file

@ -151,8 +151,8 @@ namespace bwidgets
[[nodiscard]] inline auto size() const noexcept -> Size override
{
auto base = Input<T>::size();
auto btns_width = 4 * _increment_button.size().w;
const auto base = Input<T>::size();
const auto btns_width = 4 * _increment_button.size().w;
return {base.w + btns_width, base.h};
}

View file

@ -30,8 +30,6 @@ libbasic_widgets = static_library('basic_widgets',
'src/w/base/widget.cpp',
'src/w/button.cpp',
'src/w/caption.cpp',
'src/w/feat/font_handler.cpp',
'src/w/feat/keyboard_handler.cpp',
'src/w/feat/mouse_handler.cpp',
dependencies : [sdl, fontconfig],
include_directories : pub_api,

View file

@ -21,7 +21,7 @@ namespace bwidgets
style_name(TTF_FontFaceStyleName(f))
{}
Font::Font(const std::string& file, int size)
Font::Font(const std::string& file, const int size)
: Font(ptr_or_throw<FCError>(TTF_OpenFont(file.c_str(), size), __FILE__,
__FUNCTION__, __LINE__, "failed to open font"))
{}
@ -31,40 +31,40 @@ namespace bwidgets
TTF_CloseFont(_c_pod);
}
auto Font::hinting() -> Font::Hinting
auto Font::hinting() const -> Font::Hinting
{
return (Hinting)TTF_GetFontHinting(_c_pod);
}
auto Font::hinting(Font::Hinting h) -> Font*
auto Font::hinting(const Font::Hinting h) -> Font*
{
TTF_SetFontHinting(_c_pod, (int)h);
return this;
}
auto Font::kerning() -> bool
auto Font::kerning() const -> bool
{
return TTF_GetFontKerning(_c_pod) != 0;
}
auto Font::kerning(bool allowed) -> Font*
auto Font::kerning(const bool allowed) -> Font*
{
TTF_SetFontKerning(_c_pod, static_cast<int>(allowed));
return this;
}
auto Font::outline() -> int
auto Font::outline() const -> int
{
return TTF_GetFontOutline(_c_pod);
}
auto Font::outline(int size) -> Font*
auto Font::outline(const int size) -> Font*
{
TTF_SetFontOutline(_c_pod, size);
return this;
}
auto Font::render(RenderMode m, const std::string& str, const Color& fg,
auto Font::render(const RenderMode m, const std::string& str, const Color& fg,
const Color& bg) -> SDL_Surface*
{
std::function<SDL_Surface*()> renderer;
@ -91,18 +91,18 @@ namespace bwidgets
return ptr_or_throw<SDLError>(renderer(), __FILE__, __FUNCTION__, __LINE__);
}
auto Font::style() -> uint8_t
auto Font::style() const -> uint8_t
{
return TTF_GetFontStyle(_c_pod);
}
auto Font::style(uint8_t s) -> Font*
auto Font::style(const uint8_t s) -> Font*
{
TTF_SetFontStyle(_c_pod, s);
return this;
}
auto Font::text_size(const std::string& str) -> Size
auto Font::text_size(const std::string& str) const -> Size
{
Size s {};
TTF_SizeUTF8(_c_pod, str.c_str(), &s.w, &s.h);

View file

@ -5,7 +5,7 @@ using namespace bwidgets;
Renderer::Renderer(SDL_Renderer* r) : OpaqueStruct(r), info(_info(r)) {}
Renderer::Renderer(SDL_Window* w, int index, uint32_t flags = 0)
Renderer::Renderer(SDL_Window* w, const int index, const uint32_t flags = 0)
: Renderer(ptr_or_throw<SDLError>(SDL_CreateRenderer(w, index, flags), __FILE__,
__FUNCTION__, __LINE__))
{}
@ -24,7 +24,7 @@ auto Renderer::blend_mode() -> SDL_BlendMode
return mode;
}
auto Renderer::blend_mode(SDL_BlendMode mode) -> Renderer*
auto Renderer::blend_mode(const SDL_BlendMode mode) -> Renderer*
{
SDLError::success_or_throw(SDL_SetRenderDrawBlendMode(_c_pod, mode), __FILE__,
__FUNCTION__, __LINE__);

View file

@ -8,7 +8,8 @@ Texture::Texture(SDL_Texture* t) : OpaqueStruct(t)
_attributes = attributes(t);
}
Texture::Texture(Renderer* r, SDL_PixelFormatEnum f, SDL_TextureAccess a, int w, int h)
Texture::Texture(Renderer* r, const SDL_PixelFormatEnum f, const SDL_TextureAccess a,
int w, int h)
: Texture(ptr_or_throw<SDLError>(SDL_CreateTexture((*r)(), f, a, w, h), __FILE__,
__FUNCTION__, __LINE__))
{}
@ -35,7 +36,7 @@ auto Texture::alpha_mode() -> uint8_t
return mode;
}
auto Texture::alpha_mode(uint8_t m) -> Texture*
auto Texture::alpha_mode(const uint8_t m) -> Texture*
{
SDLError::success_or_throw(SDL_SetTextureAlphaMod(_c_pod, m), __FILE__, __FUNCTION__,
__LINE__);
@ -52,7 +53,7 @@ auto Texture::blend_mode() -> SDL_BlendMode
return mode;
}
auto Texture::blend_mode(SDL_BlendMode m) -> Texture*
auto Texture::blend_mode(const SDL_BlendMode m) -> Texture*
{
SDLError::success_or_throw(SDL_SetTextureBlendMode(_c_pod, m), __FILE__,
__FUNCTION__, __LINE__);
@ -87,7 +88,7 @@ auto Texture::scale_mode() -> SDL_ScaleMode
return mode;
}
auto Texture::scale_mode(SDL_ScaleMode m) -> Texture*
auto Texture::scale_mode(const SDL_ScaleMode m) -> Texture*
{
SDLError::success_or_throw(SDL_SetTextureScaleMode(_c_pod, m), __FILE__,
__FUNCTION__, __LINE__);
@ -95,7 +96,7 @@ auto Texture::scale_mode(SDL_ScaleMode m) -> Texture*
return this;
}
auto Texture::update(SDL_Rect* r, const void* pixels, int pitch) -> Texture*
auto Texture::update(SDL_Rect* r, const void* pixels, const int pitch) -> Texture*
{
SDLError::success_or_throw(SDL_UpdateTexture(_c_pod, r, pixels, pitch), __FILE__,
__FUNCTION__, __LINE__);

View file

@ -38,7 +38,6 @@ auto Widget::handle_event(const SDL_Event& ev) -> Widget*
auto Widget::render() -> Widget*
{
SDL_assert_release(_renderer); // NOLINT
if (_renderer == nullptr) return this;
#ifndef _NDEBUG

View file

@ -59,11 +59,11 @@ void Button::_handle_font_color_change(const Color& fg, const Color&)
void Button::_handle_geometry_change(const SDL_Rect& vp) noexcept
{
int h = _caption.size().h + 2 * border_size.h;
const auto h = _caption.size().h + 2 * border_size.h;
_widget_area = {0, center_line(vp.h, _caption.size().h) - border_size.h, vp.w, h};
auto txt_size = _caption.size();
const auto txt_size = _caption.size();
_caption_area = {center_line(vp.w, txt_size.w), center_line(vp.h, txt_size.h),
txt_size.w, txt_size.h};

View file

@ -31,7 +31,7 @@ auto Caption::size() const noexcept -> Size
{
if (_font == nullptr) return {0, 0};
Size size = _font->text_size(_text);
const Size size = _font->text_size(_text);
return {size.w + 2 * margins.w, size.h + 2 * margins.h};
}
@ -86,9 +86,10 @@ void Caption::_handle_rendering()
->fill_rect({0, 0, _viewport.w, _viewport.h});
}
Size size_dst {(int)((float)_text_texture->attributes().w
/ (float)_text_texture->attributes().h * (float)_widget_area.h),
_widget_area.h};
const Size size_dst {
(int)((float)_text_texture->attributes().w / (float)_text_texture->attributes().h
* (float)_widget_area.h),
_widget_area.h};
SDL_Rect texture_dst {margins.w, margins.h, size_dst.w, size_dst.h};
switch (alignment) {
case Alignment::CENTER:

View file

@ -11,8 +11,8 @@ auto MouseHandler::handle_mouse(const SDL_MouseButtonEvent& ev, const SDL_Rect&
{
if (_click_area == nullptr) return this;
SDL_Point p {ev.x, ev.y};
SDL_Rect vp {rect_offset(*_click_area, orig)};
const SDL_Point p {ev.x, ev.y};
const SDL_Rect vp {rect_offset(*_click_area, orig)};
if (ev.type == SDL_MOUSEBUTTONDOWN) {
if (SDL_PointInRect(&p, &vp) == SDL_TRUE) {
@ -41,8 +41,8 @@ auto MouseHandler::handle_mouse(const SDL_MouseMotionEvent& ev, const SDL_Rect&
{
if (_click_area == nullptr) return this;
SDL_Point p {ev.x, ev.y};
SDL_Rect vp {rect_offset(*_click_area, orig)};
const SDL_Point p {ev.x, ev.y};
const SDL_Rect vp {rect_offset(*_click_area, orig)};
_is_hovered = SDL_PointInRect(&p, &vp) != 0;