1 #include "lxgui/gui_texture.hpp"
3 #include "lxgui/gui_layered_region.hpp"
4 #include "lxgui/gui_manager.hpp"
5 #include "lxgui/gui_material.hpp"
6 #include "lxgui/gui_out.hpp"
7 #include "lxgui/gui_region_tpl.hpp"
8 #include "lxgui/gui_render_target.hpp"
9 #include "lxgui/gui_renderer.hpp"
10 #include "lxgui/utils_file_system.hpp"
23 std::ostringstream str;
27 [&](
const auto& data) {
28 using content_type = std::decay_t<decltype(data)>;
30 if constexpr (std::is_same_v<content_type, std::string>) {
31 str << tab <<
" # File : " << data <<
"\n";
32 }
else if constexpr (std::is_same_v<content_type, gradient>) {
33 str << tab <<
" # Gradient :\n";
34 str << tab <<
" #-###\n";
35 str << tab <<
" | # min color : " << data.min_color <<
"\n";
36 str << tab <<
" | # max color : " << data.max_color <<
"\n";
37 str << tab <<
" | # orientation: " << utils::to_string(data.orient) <<
"\n";
38 str << tab <<
" #-###\n";
39 }
else if constexpr (std::is_same_v<content_type, color>) {
40 str << tab <<
" # Color : " << data <<
"\n";
45 str << tab <<
" # Tex. coord. :\n";
46 str << tab <<
" #-###\n";
47 str << tab <<
" | # top-left : (" << quad_.
v[0].uvs <<
")\n";
48 str << tab <<
" | # top-right : (" << quad_.
v[1].uvs <<
")\n";
49 str << tab <<
" | # bottom-right: (" << quad_.
v[2].uvs <<
")\n";
50 str << tab <<
" | # bottom-left : (" << quad_.
v[3].uvs <<
")\n";
51 str << tab <<
" #-###\n";
52 str << tab <<
" # Stretching: " << is_texture_stretching_enabled_ <<
"\n";
53 str << tab <<
" # Blend mode : " << utils::to_string(blend_mode_) <<
"\n";
54 str << tab <<
" # Filter : " << utils::to_string(filter_) <<
"\n";
55 str << tab <<
" # Desaturated: " << is_desaturated_ <<
"\n";
69 quad blended_quad = quad_;
70 for (std::size_t i = 0; i < 4; ++i)
71 blended_quad.
v[i].col.a *= alpha;
82 const texture* tex_obj = down_cast<texture>(&obj);
108 return std::holds_alternative<color>(content_);
112 return std::get<color>(content_);
116 return std::holds_alternative<gradient>(content_);
120 return std::get<gradient>(content_);
124 std::array<float, 8> coords{};
127 for (std::size_t i = 0; i < 4; ++i) {
128 const vector2f uv = quad_.
mat->get_local_uv(quad_.
v[i].uvs,
true);
129 coords[2 * i + 0] = uv.
x;
130 coords[2 * i + 1] = uv.
y;
133 for (std::size_t i = 0; i < 4; ++i) {
134 coords[2 * i + 0] = quad_.
v[i].uvs.x;
135 coords[2 * i + 1] = quad_.
v[i].uvs.y;
143 return is_texture_stretching_enabled_;
147 return std::holds_alternative<std::string>(content_);
151 return std::get<std::string>(content_);
157 <<
"Vertex index out of bound (" << index <<
")." << std::endl;
161 return quad_.
v[index].col;
165 return is_desaturated_;
171 <<
"texture::set_blend_mode other than \"BLEND\" is not yet implemented."
176 if (blend_mode_ == mode)
190 if (std::holds_alternative<std::string>(content_)) {
192 std::string file_name = std::get<std::string>(content_);
193 content_ = std::string{};
205 <<
"Texture de-saturation is not yet implemented." << std::endl;
233 quad_.
v[0].uvs = quad_.
mat->get_canvas_uv(
vector2f(texture_rect[0], texture_rect[1]),
true);
234 quad_.
v[1].uvs = quad_.
mat->get_canvas_uv(
vector2f(texture_rect[2], texture_rect[1]),
true);
235 quad_.
v[2].uvs = quad_.
mat->get_canvas_uv(
vector2f(texture_rect[2], texture_rect[3]),
true);
236 quad_.
v[3].uvs = quad_.
mat->get_canvas_uv(
vector2f(texture_rect[0], texture_rect[3]),
true);
238 if (!is_texture_stretching_enabled_)
239 update_dimensions_from_tex_coord_();
241 quad_.
v[0].uvs =
vector2f(texture_rect[0], texture_rect[1]);
242 quad_.
v[1].uvs =
vector2f(texture_rect[2], texture_rect[1]);
243 quad_.
v[2].uvs =
vector2f(texture_rect[2], texture_rect[3]);
244 quad_.
v[3].uvs =
vector2f(texture_rect[0], texture_rect[3]);
253 quad_.
mat->get_canvas_uv(
vector2f(texture_coords[0], texture_coords[1]),
true);
255 quad_.
mat->get_canvas_uv(
vector2f(texture_coords[2], texture_coords[3]),
true);
257 quad_.
mat->get_canvas_uv(
vector2f(texture_coords[4], texture_coords[5]),
true);
259 quad_.
mat->get_canvas_uv(
vector2f(texture_coords[6], texture_coords[7]),
true);
261 if (!is_texture_stretching_enabled_)
262 update_dimensions_from_tex_coord_();
264 quad_.
v[0].uvs =
vector2f(texture_coords[0], texture_coords[1]);
265 quad_.
v[1].uvs =
vector2f(texture_coords[2], texture_coords[3]);
266 quad_.
v[2].uvs =
vector2f(texture_coords[4], texture_coords[5]);
267 quad_.
v[3].uvs =
vector2f(texture_coords[6], texture_coords[7]);
274 if (is_texture_stretching_enabled_ != texture_stretching) {
275 is_texture_stretching_enabled_ = texture_stretching;
277 if (!is_texture_stretching_enabled_ && quad_.
mat)
278 update_dimensions_from_tex_coord_();
282 void texture::update_dimensions_from_tex_coord_() {
283 vector2f extent = quad_.
v[2].uvs - quad_.
v[0].uvs;
289 content_ = parsed_file;
293 std::shared_ptr<gui::material> mat;
300 quad_.
v[0].uvs = quad_.
mat->get_canvas_uv(
vector2f(0, 0),
true);
301 quad_.
v[1].uvs = quad_.
mat->get_canvas_uv(
vector2f(1, 0),
true);
302 quad_.
v[2].uvs = quad_.
mat->get_canvas_uv(
vector2f(1, 1),
true);
303 quad_.
v[3].uvs = quad_.
mat->get_canvas_uv(
vector2f(0, 1),
true);
310 }
else if (!parsed_file.empty()) {
312 <<
"Cannot load file \"" << parsed_file <<
"\" for \"" <<
name_
313 <<
"\". Using white texture instead." << std::endl;
320 content_ = std::string{};
324 std::shared_ptr<gui::material> mat;
331 quad_.
v[0].uvs = quad_.
mat->get_canvas_uv(
vector2f(0, 0),
true);
332 quad_.
v[1].uvs = quad_.
mat->get_canvas_uv(
vector2f(1, 0),
true);
333 quad_.
v[2].uvs = quad_.
mat->get_canvas_uv(
vector2f(1, 1),
true);
334 quad_.
v[3].uvs = quad_.
mat->get_canvas_uv(
vector2f(0, 1),
true);
343 <<
"Cannot create a texture from render target. Using white texture instead."
363 content_ = std::string{};
366 vector2f extent = quad_.
v[2].pos - quad_.
v[0].pos;
373 if (index == std::numeric_limits<std::size_t>::max()) {
374 for (std::size_t i = 0; i < 4; ++i)
383 <<
"Vertex index out of bound (" << index <<
")." << std::endl;
387 quad_.
v[index].col = c;
392 void texture::update_borders_() {
401 const std::vector<std::string>& texture::get_type_list_()
const {
402 return get_type_list_impl_<texture>();
Holds a single color (float RGBA, 128 bits)
A region that can be rendered in a layer.
void notify_renderer_need_redraw() override
Notifies the renderer of this region that it needs to be redrawn.
std::string serialize(const std::string &tab) const override
Prints all relevant information about this region in a string.
Manages the user interface.
const renderer & get_renderer() const
Returns the renderer implementation.
The base class of all elements in the GUI.
virtual void set_width(float abs_width)
Changes this region's absolute width (in pixels).
const std::string & get_region_type() const
Returns the type of this region.
void initialize_(T &self, const region_core_attributes &attr)
Set up function to call in all derived class constructors.
float get_effective_alpha() const
Returns this region's effective alpha (opacity).
bool is_apparent_width_defined() const
Checks if this region's apparent width is defined.
bool is_visible() const
Checks if this region can be seen on the screen.
manager & get_manager()
Returns this region's manager.
virtual void render() const
Renders this region on the current render target.
virtual void set_height(float abs_height)
Changes this region's absolute height (in pixels).
virtual void set_dimensions(const vector2f &dimensions)
Changes this region's absolute dimensions (in pixels).
bool is_apparent_height_defined() const
Checks if this region's apparent height is defined.
virtual void copy_from(const region &obj)
Copies a region's parameters into this region (inheritance).
std::string parse_file_name(const std::string &file_name) const
Convert an addon-relative file path to a application-relative path.
virtual void update_borders_()
Abstract type for implementation specific management.
std::shared_ptr< material > create_atlas_material(const std::string &atlas_category, const std::string &file_name, material::filter filt=material::filter::none)
Creates a new material from a texture file.
void render_quad(const quad &q)
Renders a quad.
std::shared_ptr< material > create_material(const std::string &file_name, material::filter filt=material::filter::none)
Creates a new material from a texture file.
A layered_region that can draw images and colored rectangles.
bool get_texture_stretching() const
Checks if this texture can stretch to match the region dimensions.
std::string serialize(const std::string &tab) const override
Prints all relevant information about this region in a string.
void set_gradient(const gradient &g)
Adds a gradient effect to this texture.
void set_blend_mode(blend_mode mode)
Sets this texture's blending mode.
void copy_from(const region &obj) override
Copies a region's parameters into this texture (inheritance).
void set_tex_rect(const std::array< float, 4 > &texture_rect)
Sets this texture's texture coordinates.
const gradient & get_gradient() const
Returns this texture's gradient.
material::filter get_filter_mode() const
Returns this texture's filtering algorithm.
void set_solid_color(const color &c)
Sets this texture's color.
const color & get_solid_color() const
Returns this texture's color.
void set_desaturated(bool is_desaturated)
Makes this texture appear without any color.
void set_tex_coord(const std::array< float, 8 > &texture_coords)
Sets this texture's texture coordinates.
void set_texture_stretching(bool texture_stretching)
Sets whether this texture can stretch to match the region dimensions.
texture(utils::control_block &block, manager &mgr, const region_core_attributes &attr)
Constructor.
bool has_solid_color() const
Checks if this texture is defined as solid color.
color get_vertex_color(std::size_t index) const
Returns this texture's vertex color.
void set_filter_mode(material::filter filt)
Sets this texture's filtering mode.
blend_mode get_blend_mode() const
Returns this texture's blending mode.
const std::string & get_texture_file() const
Returns this texture's texture file.
void set_quad(const quad &q)
Directly sets this texture's underlying quad (vertices and material).
bool is_desaturated() const
Checks if this texture is desaturated.
void set_vertex_color(const color &c, std::size_t index=std::numeric_limits< std::size_t >::max())
Sets this texture's vertex color.
std::array< float, 8 > get_tex_coord() const
Returns this texture's texture coordinates.
bool has_texture_file() const
Checks if this texture is defined as a texture file.
void render() const override
Renders this region on the current render target.
bool has_gradient() const
Checks if this texture is defined as a gradient.
void set_texture(const std::string &file_name)
Sets this texture's texture file.
vector2< float > vector2f
Holds 2D coordinates (as floats)
const std::string warning
bool file_exists(const std::string &file)
vector2< T > bottom_left() const noexcept
vector2< T > bottom_right() const noexcept
vector2< T > top_right() const noexcept
vector2< T > top_left() const noexcept
Represents color gradients.
Simple structure holding four vertices and a material.
std::shared_ptr< material > mat
std::array< vertex, 4 > v
Struct holding all the core information about a region necessary for its creation.