Make image_view use texture_view instead of owning a texture

This commit is contained in:
Nikita Lisitsa 2022-02-17 18:54:14 +03:00
parent 39cbb8b60d
commit eb78e2cdb7
5 changed files with 19 additions and 18 deletions

View file

@ -23,13 +23,13 @@ namespace psemek::ui
{
virtual std::shared_ptr<rich_button> make_button();
virtual std::shared_ptr<rich_button> make_button(std::string text);
virtual std::shared_ptr<rich_button> make_button(std::shared_ptr<gfx::texture_2d> icon);
virtual std::shared_ptr<rich_button> make_button(gfx::texture_view_2d icon);
virtual std::shared_ptr<label> make_label(std::string text);
virtual std::shared_ptr<frame> make_frame();
virtual std::shared_ptr<window> make_window(std::string caption);
virtual std::shared_ptr<screen> make_screen();
virtual std::shared_ptr<grid_layout> make_grid_layout();
virtual std::shared_ptr<image_view> make_image_view(std::shared_ptr<gfx::texture_2d> image);
virtual std::shared_ptr<image_view> make_image_view(gfx::texture_view_2d image);
virtual std::shared_ptr<rich_image_view> make_rich_image_view(std::shared_ptr<gfx::texture_2d> image);
virtual std::shared_ptr<checkbox> make_checkbox(bool value);
virtual std::shared_ptr<slider> make_slider();

View file

@ -3,7 +3,7 @@
#include <psemek/ui/element.hpp>
#include <psemek/ui/box_shape.hpp>
#include <psemek/gfx/texture.hpp>
#include <psemek/gfx/texture_view.hpp>
namespace psemek::ui
{
@ -20,8 +20,8 @@ namespace psemek::ui
virtual bool keep_aspect_ratio() const { return keep_aspect_ratio_; }
virtual void set_keep_aspect_ratio(bool value);
virtual std::shared_ptr<gfx::texture_2d> image() const { return image_; }
virtual void set_image(std::shared_ptr<gfx::texture_2d> image);
virtual gfx::texture_view_2d image() const { return image_; }
virtual void set_image(gfx::texture_view_2d image);
virtual gfx::color_rgba const & color() const { return color_; }
virtual void set_color(gfx::color_rgba const & c) { color_ = c; }
@ -37,7 +37,7 @@ namespace psemek::ui
bool upscale_ = true;
bool downscale_ = true;
bool keep_aspect_ratio_ = true;
std::shared_ptr<gfx::texture_2d> image_;
gfx::texture_view_2d image_;
gfx::color_rgba color_{0, 0, 0, 0};
box_shape shape_;
};

View file

@ -207,11 +207,12 @@ namespace psemek::ui
: window
{
window_impl(std::shared_ptr<gfx::texture_2d> close_icon)
: caption_(std::make_shared<label>())
: close_texture_(std::move(close_icon))
, caption_(std::make_shared<label>())
, close_button_(std::make_shared<button_impl>())
{
close_button_->set_icon(std::make_shared<image_view>());
close_button_->icon()->set_image(close_icon);
close_button_->icon()->set_image({close_texture_.get()});
caption_->set_parent(this);
close_button_->set_parent(this);
@ -390,6 +391,7 @@ namespace psemek::ui
}
private:
std::shared_ptr<gfx::texture_2d> close_texture_;
std::shared_ptr<label> caption_;
std::shared_ptr<rich_button> close_button_;
std::shared_ptr<element> child_;

View file

@ -22,12 +22,12 @@ namespace psemek::ui
return b;
}
std::shared_ptr<rich_button> element_factory::make_button(std::shared_ptr<gfx::texture_2d> image)
std::shared_ptr<rich_button> element_factory::make_button(gfx::texture_view_2d icon)
{
auto b = make_button();
if (b)
{
auto i = make_image_view(std::move(image));
auto i = make_image_view(icon);
if (i)
{
i->set_downscale(false);
@ -36,7 +36,6 @@ namespace psemek::ui
}
}
return b;
}
std::shared_ptr<label> element_factory::make_label(std::string text)
@ -58,10 +57,10 @@ namespace psemek::ui
return std::make_shared<grid_layout>();
}
std::shared_ptr<image_view> element_factory::make_image_view(std::shared_ptr<gfx::texture_2d> image)
std::shared_ptr<image_view> element_factory::make_image_view(gfx::texture_view_2d image)
{
auto i = std::make_shared<image_view>();
i->set_image(std::move(image));
i->set_image(image);
return i;
}

View file

@ -21,9 +21,9 @@ namespace psemek::ui
post_reshape();
}
void image_view::set_image(std::shared_ptr<gfx::texture_2d> image)
void image_view::set_image(gfx::texture_view_2d image)
{
image_ = std::move(image);
image_ = image;
post_reshape();
}
@ -33,7 +33,7 @@ namespace psemek::ui
if (image_)
{
auto const size = image_->size();
auto const size = image_.size();
if (!downscale_)
{
@ -55,7 +55,7 @@ namespace psemek::ui
{
if (!image_) return;
auto const size = image_->size();
auto const size = image_.size();
auto box = shape_.box;
@ -81,7 +81,7 @@ namespace psemek::ui
}
}
p.draw_image(box, *image_, color_);
p.draw_image(box, image_, color_);
}
}