Skip to content

Commit 1ebdb20

Browse files
authored
Merge pull request #111 from cschreib/cleaning
Fix various Lua management errors & add `animated_texture`
2 parents f12f953 + a891ea2 commit 1ebdb20

20 files changed

+556
-32
lines changed

.gitmodules

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[submodule "sol2"]
22
path = extern/sol2
3-
url = https://github.com/ThePhD/sol2.git
3+
url = https://github.com/cschreib/sol2.git
44
[submodule "utfcpp"]
55
path = extern/utfcpp
66
url = https://github.com/nemtrif/utfcpp.git

CMakeLists.txt

+5
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,8 @@ endif()
228228
set(SOL_ALL_SAFETIES_ON TRUE)
229229
set(SOL_PRINT_ERRORS FALSE)
230230
if(LXGUI_BUILD_SOL2)
231+
set(SOL2_BUILD_LUA FALSE)
232+
set(SOL2_ENABLE_INSTALL TRUE)
231233
add_subdirectory(extern/sol2)
232234
else()
233235
find_package(sol2 REQUIRED)
@@ -241,6 +243,9 @@ add_library(lxgui
241243
${PROJECT_SOURCE_DIR}/src/gui_addon_registry.cpp
242244
${PROJECT_SOURCE_DIR}/src/gui_addon_registry_parser.cpp
243245
${PROJECT_SOURCE_DIR}/src/gui_anchor.cpp
246+
${PROJECT_SOURCE_DIR}/src/gui_animated_texture.cpp
247+
${PROJECT_SOURCE_DIR}/src/gui_animated_texture_glues.cpp
248+
${PROJECT_SOURCE_DIR}/src/gui_animated_texture_parser.cpp
244249
${PROJECT_SOURCE_DIR}/src/gui_atlas.cpp
245250
${PROJECT_SOURCE_DIR}/src/gui_backdrop.cpp
246251
${PROJECT_SOURCE_DIR}/src/gui_button.cpp

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ There are plenty of different GUI libraries out there. They all have something t
3535
* **Platform independent**. The library is coded in standard C++17. Platform dependent concepts, such as rendering or input, are handled by back-end plugins (for rendering: SFML, SDL, or pure OpenGL; for input: SFML or SDL). Builds on Linux, MacOS, Windows, and WebAssembly.
3636
* **High-DPI aware**. The interface can be scaled by an arbitrary factor when rendered on the screen. This can be used to improve accessibility for visually-impaired users.
3737
* **Non intrusive**. The library will fit in your existing application without taking over your main loop. All it needs is being fed events, a call to `update()`, a call to `render()`, and nothing more.
38-
* **Fully extensible**. Except for the base GUI region types, every region type is designed to be used as a plugin: gui::texture, gui::font_string, gui::button, gui::edit_box, ... New region types can be added easily in your own code without modifying lxgui.
38+
* **Fully extensible**. Except for the base GUI region types, every region type is designed to be used as a plugin: `gui::texture`, `gui::font_string`, `gui::button`, `gui::edit_box`, ... New region types can be added easily in your own code without modifying lxgui.
3939
* **Fully documented**. Every class in the library is documented. Doxygen documentation is included (and available on-line [here](https://cschreib.github.io/lxgui/html/annotated.html) for the C++ API, and [here](https://cschreib.github.io/lxgui/lua/index.html) for the Lua API).
4040
* **Design with layout files and script files**. The library can use a combination of layout files (XML or YAML, defining the GUI layout) and script files (Lua, for event handling, etc.) to construct a fully functional GUI. One can also create everything directly in C++ if the flexibility of the layout+script files is not required.
4141
* **Internationalization and localization support**. The library supports translatable text with a fully flexible system, allowing correct display of the GUI in multiple languages. This is optional: if only one language is required, one can just hard-code strings without worrying about translations. At present, only left-to-right languages are supported.
@@ -48,6 +48,7 @@ There are plenty of different GUI libraries out there. They all have something t
4848
* **layered_region**: can be rendered on the screen as part of a draw layer.
4949
* **frame**: can contain layered_regions (sorted by layer) and other frames, and respond to events.
5050
* **texture**: can render a texture file, a gradient, or a plain color.
51+
* **animated_texture**: can render an animated texture (multiple frames stored in the same texture file).
5152
* **font_string**: can render text.
5253
* **button**: a click-able frame with several states: normal, pushed, highlight.
5354
* **check_button**: a button with a check box.

bin/interface/texture_test/addon.xml

+6
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@
2121
<MaxColor r="1" g="0.7"/>
2222
</Gradient>
2323
</Texture>
24+
<AnimatedTexture name="$parentTexture3" file="|animated_texture.png" speed="5">
25+
<Size><AbsDimension x="25" y="25"/></Size>
26+
<Anchors>
27+
<Anchor point="BOTTOM_LEFT" relativeTo="$parentTexture1" relativePoint="BOTTOM_RIGHT"/>
28+
</Anchors>
29+
</AnimatedTexture>
2430
</Layer>
2531
</Layers>
2632
</Frame>

bin/interface/texture_test/addon.yml

+12-4
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ ui:
22
frame:
33
name: TextureTestFrame
44
anchors:
5-
- point: TOPLEFT
6-
- point: BOTTOMRIGHT
5+
- point: TOP_LEFT
6+
- point: BOTTOM_RIGHT
77

88
layers:
99
layer:
@@ -12,14 +12,22 @@ ui:
1212
name: $parentTexture1
1313
file: "|texture.png"
1414
anchors:
15-
- point: BOTTOMLEFT
15+
- point: BOTTOM_LEFT
1616

1717
texture:
1818
name: $parentTexture2
1919
size: {abs_dimension: {x: 50, y: 50}}
2020
anchors:
21-
- {point: BOTTOMLEFT, relative_to: $parentTexture1, relative_point: TOPLEFT}
21+
- {point: BOTTOM_LEFT, relative_to: $parentTexture1, relative_point: TOP_LEFT}
2222
gradient:
2323
orientation: HORIZONTAL
2424
min_color: {r: 0.7, g: 1}
2525
max_color: {r: 1, g: 0.7}
26+
27+
animated_texture:
28+
name: $parentTexture3
29+
size: {abs_dimension: {x: 25, y: 25}}
30+
file: "|animated_texture.png"
31+
speed: 5
32+
anchors:
33+
- {point: BOTTOM_LEFT, relative_to: $parentTexture1, relative_point: BOTTOM_RIGHT}
5.63 KB
Loading

changelog.txt

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ Major changes:
1818
- gui: added texture atlases to speed up rendering
1919
- gui: added quad batching to speed up rendering
2020
- gui: added localizer class for handling languages, translations, and string formatting
21+
- gui: added animated_texture region type for animated textures
2122
- gui: font_string can now render icons/smileys mixed with the rendered text
2223
- gui: fonts are no longer limited to the first 255 unicode characters
2324
- gui: the order in which widgets are created from XML is now tightly specified. For any frame:

examples/common/examples_common.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#include <iostream>
44
#include <lxgui/extern_sol2_state.hpp>
5+
#include <lxgui/gui_animated_texture.hpp>
56
#include <lxgui/gui_button.hpp>
67
#include <lxgui/gui_check_button.hpp>
78
#include <lxgui/gui_edit_box.hpp>
@@ -57,6 +58,7 @@ void examples_setup_gui(gui::manager& manager) {
5758
// also where you would register your own region types, if any.
5859
gui::factory& factory = manager.get_factory();
5960
factory.register_region_type<gui::texture>();
61+
factory.register_region_type<gui::animated_texture>();
6062
factory.register_region_type<gui::font_string>();
6163
factory.register_region_type<gui::button>();
6264
factory.register_region_type<gui::check_button>();

extern/sol2

Submodule sol2 updated 513 files
+149
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
#ifndef LXGUI_GUI_ANIMATED_TEXTURE_HPP
2+
#define LXGUI_GUI_ANIMATED_TEXTURE_HPP
3+
4+
#include "lxgui/gui_layered_region.hpp"
5+
#include "lxgui/gui_quad.hpp"
6+
#include "lxgui/lxgui.hpp"
7+
#include "lxgui/utils.hpp"
8+
9+
namespace lxgui::gui {
10+
11+
class renderer;
12+
13+
/**
14+
* \brief A layered_region that can draw animated sequences.
15+
* \details This object contains an animated texture taken from a file.
16+
*/
17+
class animated_texture : public layered_region {
18+
using base = layered_region;
19+
20+
public:
21+
/// Constructor.
22+
explicit animated_texture(
23+
utils::control_block& block, manager& mgr, const region_core_attributes& attr);
24+
25+
/**
26+
* \brief Prints all relevant information about this region in a string.
27+
* \param tab The offset to give to all lines
28+
* \return All relevant information about this region
29+
*/
30+
std::string serialize(const std::string& tab) const override;
31+
32+
/// Renders this region on the current render target.
33+
void render() const override;
34+
35+
/**
36+
* \brief Updates this region's logic.
37+
* \param delta Time spent since last update
38+
*/
39+
void update(float delta) override;
40+
41+
/**
42+
* \brief Copies a region's parameters into this texture (inheritance).
43+
* \param obj The region to copy
44+
*/
45+
void copy_from(const region& obj) override;
46+
47+
/**
48+
* \brief Returns this animated_texture's animation speed (frame per second).
49+
* \return This animated_texture's animation speed
50+
*/
51+
float get_speed() const;
52+
53+
/**
54+
* \brief Returns this animated_texture's state (0: begin, 1: end).
55+
* \return This animated_texture's state
56+
*/
57+
float get_state() const;
58+
59+
/**
60+
* \brief Check if this animated_texture is paused
61+
* \return 'true' if paused, 'false' otherwise
62+
*/
63+
float is_paused() const;
64+
65+
/**
66+
* \brief Returns this texture's texture file.
67+
* \return This texture's texture file (empty string if none).
68+
*/
69+
const std::string& get_texture_file() const;
70+
71+
/**
72+
* \brief Returns this texture's vertex color.
73+
* \param index The vertex index (0 to 3 included)
74+
* \return This texture's vertex color
75+
* \note This color is used to filter the texture's colors:
76+
* for each pixel, the original color is multiplied by this vertex color.
77+
*/
78+
color get_vertex_color(std::size_t index) const;
79+
80+
/**
81+
* \brief Set this animated_texture's animation speed (frame per second).
82+
* \param speed The new animation speed
83+
*/
84+
void set_speed(float speed);
85+
86+
/**
87+
* \brief Returns this animated_texture's state (0: begin, 1: end).
88+
* \param state The new state
89+
*/
90+
void set_state(float state);
91+
92+
/**
93+
* \brief Check if this animated_texture is paused
94+
* \return 'true' if paused, 'false' otherwise
95+
*/
96+
void set_paused(bool is_paused);
97+
98+
/**
99+
* \brief Sets this texture's texture file.
100+
* \param file_name The file from which to read data
101+
* \note This function takes care of checking that the file can be opened.
102+
* \note This function will replace the solid color set by set_solid_color(). If you need
103+
* to blend the texture with a color, use set_vertex_color() instead.
104+
*/
105+
void set_texture(const std::string& file_name);
106+
107+
/**
108+
* \brief Sets this texture's vertex color.
109+
* \param c This texture's new vertex color
110+
* \param index The vertex index (-1: all vertices)
111+
* \note This color is used to filter the texture's colors:
112+
* for each pixel, the original color is multiplied
113+
* by this vertex color.
114+
*/
115+
void
116+
set_vertex_color(const color& c, std::size_t index = std::numeric_limits<std::size_t>::max());
117+
118+
/**
119+
* \brief Parses data from a layout_node.
120+
* \param node The layout node
121+
*/
122+
void parse_layout(const layout_node& node) override;
123+
124+
/// Registers this region class to the provided Lua state
125+
static void register_on_lua(sol::state& lua);
126+
127+
static constexpr const char* class_name = "AnimatedTexture";
128+
129+
private:
130+
void parse_attributes_(const layout_node& node) override;
131+
void parse_tex_coords_node_(const layout_node& node);
132+
void parse_gradient_node_(const layout_node& node);
133+
134+
void update_tex_coords_();
135+
void update_borders_() override;
136+
137+
std::string file_;
138+
139+
float speed_ = 1.0f;
140+
float state_ = 0.0f;
141+
bool is_paused_ = false;
142+
143+
renderer& renderer_;
144+
quad quad_;
145+
};
146+
147+
} // namespace lxgui::gui
148+
149+
#endif

include/lxgui/gui_region_tpl.hpp

+6-4
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,18 @@ struct unique_usertype_traits<lxgui::utils::observer_ptr<T>> {
3131
/** \endcond
3232
*/
3333

34+
namespace lxgui::gui {
35+
3436
template<typename T>
3537
void sol_lua_check_access(
3638
sol::types<T>, lua_State* lua, int index, sol::stack::record& /*tracking*/) {
3739
// NB: not sure why, but using tracking here leads to issues later on, so
3840
// ignore it for now.
3941

40-
sol::optional<lxgui::utils::observer_ptr<T>&> optional =
41-
sol::stack::check_get<lxgui::utils::observer_ptr<T>&>(
42+
using RegionType = std::remove_pointer_t<T>;
43+
44+
sol::optional<lxgui::utils::observer_ptr<RegionType>&> optional =
45+
sol::stack::check_get<lxgui::utils::observer_ptr<RegionType>&>(
4246
lua, index, sol::no_panic /*, tracking*/);
4347

4448
if (!optional.has_value())
@@ -48,8 +52,6 @@ void sol_lua_check_access(
4852
throw sol::error("object has been deleted");
4953
}
5054

51-
namespace lxgui::gui {
52-
5355
inline utils::observer_ptr<region>
5456
get_object(manager& mgr, const std::variant<std::string, region*>& parent) {
5557
return std::visit(

0 commit comments

Comments
 (0)