various changes and fixes

This commit is contained in:
Andrea Blankenstijn 2021-07-23 20:08:52 +02:00
parent e766162b1e
commit 4b87af7bc4
14 changed files with 105 additions and 46 deletions

View file

@ -13,11 +13,11 @@ namespace bwidgets::core
class Renderer;
class Texture;
SDL_Color aa(const SDL_Color&, const int, const float) noexcept;
core::Texture filled_circle(const SDL_Color&, int resolution,
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*)>);
void set_pixels_color(Texture*, std::function<uint32_t (const SDL_Point&,
const SDL_PixelFormat*)>);
}
#endif

View file

@ -45,7 +45,7 @@ namespace bwidgets::core
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_rect(const SDL_Rect*);
Renderer* fill_rects(const std::vector<SDL_Rect>);
Size output_size();
void present();
@ -67,6 +67,11 @@ namespace bwidgets::core
auto s = src, d = dst;
return copy(t, &s, &d);
}
inline Renderer* fill_rect(const SDL_Rect& r)
{
auto rect = r;
return fill_rect(&rect);
}
inline Renderer* viewport(const SDL_Rect& vp)
{
auto v = vp;

View file

@ -150,6 +150,8 @@ namespace bwidgets::widget
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 {};
virtual Input<T>* color_fg(const SDL_Color& c) noexcept
@ -179,6 +181,16 @@ namespace bwidgets::widget
return x;
}
virtual inline core::Size size() const noexcept override
{
return {
_font->text_size(
std::string(input_min_width, input_width_unit)
).w + 2 * _border_width,
_font->height + 2 * _border_width
};
}
T value_from_string(std::string s)
{
T v;

View file

@ -1,6 +1,7 @@
#ifndef BWIDGETS_LAYOUT_HPP_
#define BWIDGETS_LAYOUT_HPP_
#include <functional>
#include <vector>
#include <basic_widgets/core/type/size.hpp>
@ -28,6 +29,7 @@ namespace bwidgets::widget
virtual ~Layout() noexcept;
virtual Layout* add_widget(Widget*);
virtual void for_widgets(std::function<void (Widget*)>);
virtual Layout* handle_event(const SDL_Event&) override;
virtual core::Size size() const noexcept override = 0;

View file

@ -29,9 +29,7 @@ namespace bwidgets::widget
virtual ~Widget() noexcept = default;
virtual Widget* handle_event(const SDL_Event&);
inline virtual core::Size size() const noexcept {
return {_widget_area.w, _widget_area.h};
}
inline virtual core::Size size() const noexcept = 0;
virtual Widget* render() final;

View file

@ -24,6 +24,7 @@ namespace bwidgets::widget
private:
SDL_Color _color_fg {0, 0, 0, SDL_ALPHA_OPAQUE};
SDL_Color _color_bg {255, 255, 255, SDL_ALPHA_TRANSPARENT};
core::Font::RenderMode _render_mode {core::Font::RenderMode::SHADED};
std::string _text;
core::Texture* _text_texture {nullptr};
@ -44,9 +45,11 @@ namespace bwidgets::widget
virtual Caption* color_bg(const SDL_Color&);
virtual Caption* color_fg(const SDL_Color&);
virtual core::Size size() const noexcept override;
virtual Caption* render_mode(core::Font::RenderMode) noexcept;
virtual const std::string& text() const noexcept;
virtual Caption* text(const std::string&);
virtual core::Size size() const noexcept override;
};
}

View file

@ -110,6 +110,9 @@ namespace bwidgets::widget
Input<T>::_input_caption.alignment =
widget::Caption::Alignment::RIGHT;
Input<T>::input_min_width = 10;
Input<T>::input_width_unit = '0';
_increment_button.text("+");
_increment_button.click_handler = [this](const SDL_MouseButtonEvent&) {
@ -171,6 +174,16 @@ namespace bwidgets::widget
return value;
}
virtual inline core::Size size() const noexcept 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
{
return _value_range;

View file

@ -26,17 +26,17 @@ namespace bwidgets::core
};
}
core::Texture filled_circle(
core::Texture* filled_circle(
const SDL_Color& c,
const int resolution,
core::Renderer* r,
const int aa_pixels
)
{
core::Texture texture (r,
auto* texture {new core::Texture(r,
SDL_PIXELFORMAT_RGBA32,
SDL_TEXTUREACCESS_STATIC,
resolution, resolution);
resolution, resolution)};
const auto radius {resolution / 2};
const SDL_Point center {radius, radius};
@ -52,18 +52,18 @@ namespace bwidgets::core
aa_color.a);
}
);
texture.blend_mode(SDL_BLENDMODE_BLEND);
texture.scale_mode(SDL_ScaleModeNearest);
texture->blend_mode(SDL_BLENDMODE_BLEND);
texture->scale_mode(SDL_ScaleModeNearest);
return texture;
}
void set_pixels_color(
core::Texture& t,
core::Texture* t,
std::function<Uint32 (const SDL_Point&, const SDL_PixelFormat*)> pixel_color
)
{
auto attr = t.attributes();
auto attr = t->attributes();
auto pitch = attr.w * attr.format->BytesPerPixel;
std::vector<Uint32> pixels;
@ -76,6 +76,6 @@ namespace bwidgets::core
}
}
t.update(nullptr, (const void*)pixels.data(), pitch);
t->update(nullptr, (const void*)pixels.data(), pitch);
}
}

View file

@ -126,10 +126,9 @@ core::Renderer* core::Renderer::draw_rects(const std::vector<SDL_Rect> rs)
return this;
}
core::Renderer* core::Renderer::fill_rect(const SDL_Rect& r)
core::Renderer* core::Renderer::fill_rect(const SDL_Rect* r)
{
SDL_Rect rect = r;
SDLError::success_or_throw(SDL_RenderFillRect(c_pod, &rect),
SDLError::success_or_throw(SDL_RenderFillRect(c_pod, r),
__FILE__, __FUNCTION__, __LINE__);
return this;

View file

@ -10,10 +10,17 @@ widget::Layout::~Layout() noexcept
widget::Layout* widget::Layout::add_widget(Widget *widget_ptr)
{
widget_ptr->renderer(_renderer);
_widgets.push_back(widget_ptr);
return this;
}
void widget::Layout::for_widgets(std::function<void (Widget*)> f)
{
for (auto* w : _widgets)
f(w);
}
widget::Layout* widget::Layout::handle_event(const SDL_Event& ev)
{
for (Widget* widget_ptr : _widgets)
@ -28,8 +35,8 @@ void widget::Layout::_handle_geometry_change(const SDL_Rect& vp) noexcept
void widget::Layout::_handle_renderer_change(core::Renderer* r)
{
for (auto* widget : _widgets)
widget->renderer(r);
for (auto* w : _widgets)
w->renderer(r);
}
void widget::Layout::_handle_rendering()

View file

@ -1,3 +1,4 @@
#include <SDL2/SDL_assert.h>
#include <SDL2/SDL_events.h>
#include <basic_widgets/w/feat/keyboard_handler.hpp>
@ -43,7 +44,8 @@ widget::Widget* widget::Widget::handle_event(const SDL_Event &ev)
widget::Widget* widget::Widget::render()
{
if (_renderer == nullptr)
SDL_assert_release(_renderer);
if (!_renderer)
return this;
#ifndef _NDEBUG

View file

@ -26,6 +26,13 @@ widget::Caption* widget::Caption::color_fg(const SDL_Color& c)
return this;
}
widget::Caption* widget::Caption::render_mode(core::Font::RenderMode m) noexcept
{
_render_mode = m;
core::OpaqueStruct<core::Texture>::discard(_text_texture);
return this;
}
core::Size widget::Caption::size() const noexcept
{
if (_font == nullptr)
@ -115,8 +122,17 @@ void widget::Caption::_handle_rendering()
void widget::Caption::_handle_texture_update()
{
SDL_assert_release(_font);
core::OpaqueStruct<core::Texture>::discard(_text_texture);
auto txt_surface = _font->render(core::Font::RenderMode::SHADED,
_text, _color_fg, _color_bg);
_text_texture = new core::Texture(_renderer, txt_surface);
SDL_Surface* s;
switch (_render_mode)
{
case core::Font::RenderMode::SHADED:
s = _font->render(_render_mode, _text, _color_fg, _color_bg);
break;
default:
s = _font->render(_render_mode, _text, _color_fg);
break;
}
_text_texture = new core::Texture(_renderer, s);
}

View file

@ -1,3 +1,4 @@
#include <basic_widgets/core/math.hpp>
#include <basic_widgets/w/horizontal_layout.hpp>
using namespace bwidgets;
@ -27,11 +28,12 @@ void widget::HorizontalLayout::_update_layout(const SDL_Rect& vp) noexcept
for (std::vector<Widget*>::size_type i = 0; i < _widgets.size(); i++)
{
_widgets.at(i)->viewport({
auto* w = _widgets[i];
w->viewport({
vp.x + margins.w + (int)i * (widget_size + margins.w),
vp.y,
vp.y + core::center_rect(vp.h, w->size().h),
widget_size,
vp.h
w->size().h
});
}
}

View file

@ -1,38 +1,38 @@
#include <basic_widgets/core/math.hpp>
#include <basic_widgets/w/vertical_layout.hpp>
using namespace bwidgets;
core::Size widget::VerticalLayout::size() const noexcept
{
core::Size max {0, 0};
core::Size size {0, 0};
for (const auto* w : _widgets)
{
if (w->size().w > max.w)
max.w = w->size().w;
if (w->size().h > max.h)
max.h = w->size().h;
if (w->size().w > size.w)
size.w = w->size().w;
size.h += w->size().h;
}
return {
max.w + 2 * margins.w,
(int)_widgets.size() * max.h + ((int)_widgets.size() + 1) * margins.h
size.w + 2 * margins.w,
size.h + ((int)_widgets.size() + 1) * margins.h
};
}
void widget::VerticalLayout::_update_layout(const SDL_Rect& vp) noexcept
{
int widget_size = (vp.h - (_widgets.size() + 1) * margins.h)
/ _widgets.size();
int offset = 0;
for (std::vector<Widget*>::size_type i = 0; i < _widgets.size(); i++)
{
SDL_Rect widget_vp = {
vp.x,
vp.y + ((int)i + 1) * margins.h + (int)i * widget_size,
vp.w,
widget_size,
};
_widgets.at(i)->viewport(widget_vp);
auto* w = _widgets[i];
w->viewport({
vp.x,
vp.y + ((int)i + 1) * margins.h + offset,
vp.w,
w->size().h,
});
offset += w->size().h;
}
}