1 module rpui.widgets.button.render_system;
2 
3 import std.container.array;
4 
5 import rpui.widgets.button.widget;
6 import rpui.widgets.button.transforms_system;
7 import rpui.render.components_factory;
8 import rpui.render.components;
9 import rpui.render.renderer;
10 import rpui.theme;
11 import rpui.math;
12 
13 import gapi.texture;
14 
15 struct RenderData {
16     StatefulChain background;
17     Chain focusGlow;
18     StatefulUiText captionText;
19     Array!TexAtlasTextureQuad icons;
20     bool textVisible = true;
21 }
22 
23 final class ButtonRenderSystem : RenderSystem {
24     private Button widget;
25     private Theme theme;
26     private RenderTransforms* transforms;
27     private RenderData* renderData;
28 
29     this(Button widget, RenderData* renderData, RenderTransforms* transforms) {
30         this.widget = widget;
31         this.theme = widget.view.theme;
32         this.transforms = transforms;
33         this.renderData = renderData;
34     }
35 
36     override void onRender() {
37         renderData.background.state = widget.state;
38         renderData.captionText.state = widget.state;
39 
40         // TODO: draw disabled
41         const alpha = widget.isEnabled ? 1.0 : 0.5;
42 
43         renderHorizontalChain(
44             theme,
45             renderData.background,
46             transforms.background,
47             widget.partDraws,
48             alpha
49         );
50 
51         if (renderData.textVisible && widget.caption != "") {
52             renderUiText(theme, renderData.captionText, transforms.captionText);
53         }
54 
55         if (widget.focusable && widget.isFocused && widget.isEnabled) {
56             renderHorizontalChain(
57                 theme,
58                 renderData.focusGlow,
59                 transforms.focusGlow,
60                 widget.partDraws
61             );
62         }
63 
64         renderIcons();
65     }
66 
67     private void renderIcons() {
68         for (int i = 0; i < widget.icons.length; ++i) {
69             const iconTransforms = transforms.icons[i];
70             const iconQuad = renderData.icons[i];
71 
72             renderTexAtlasQuad(theme, iconQuad, iconTransforms);
73         }
74     }
75 }