1 module rpui.widgets.list_menu_item.renderer;
2 
3 import std.conv;
4 
5 import rpui.primitives;
6 import rpui.theme;
7 import rpui.events;
8 import rpui.widget;
9 import rpui.math;
10 import rpui.widgets.button.renderer;
11 import rpui.widgets.list_menu_item.widget;
12 import rpui.render.components;
13 import rpui.render.components_factory;
14 import rpui.render.renderer;
15 import rpui.render.transforms;
16 
17 final class ListMenuItemRenderer : ButtonRenderer {
18     private ListMenuItem widget;
19     private Theme theme;
20 
21     private vec2 submenuArrowOffset;
22     private float arrowAreaWidth;
23     private StatefulTexAtlasTextureQuad arrow;
24     private QuadTransforms arrowTransforms;
25     private StatefulUiText shortcutText;
26     private UiTextTransforms shortcutTextTransforms;
27 
28     override void onCreate(Widget widget, in string style) {
29         super.onCreate(widget, style);
30 
31         this.theme = widget.view.theme;
32         this.widget = cast(ListMenuItem) widget;
33 
34         arrow = createStatefulTexAtlasTextureQuadFromRdpl(theme, style, "submenuArrow");
35         submenuArrowOffset = theme.tree.data.getVec2f(style ~ ".submenuArrowOffset");
36         arrowAreaWidth = theme.tree.data.getNumber(style ~ ".arrowAreaWidth.0");
37         shortcutText = createStatefulUiTextFromRdpl(theme, style, "ShortcutText");
38     }
39 
40     override void onRender() {
41         super.onRender();
42         shortcutText.state = widget.state;
43 
44         if (widget.shortcut != "") {
45             renderUiText(theme, shortcutText, shortcutTextTransforms);
46         }
47 
48         if (widget.menu is null)
49             return;
50 
51         renderTexAtlasQuad(theme, arrow, arrowTransforms);
52     }
53 
54     override void onProgress(in ProgressEvent event) {
55         super.onProgress(event);
56 
57         updateShortcutText();
58 
59         if (widget.menu is null)
60             return;
61 
62         const arrowSize = arrow.currentTexCoords.originalTexCoords.size;
63         const arrowPosition = widget.absolutePosition + vec2(widget.size.x - arrowSize.x, 0);
64 
65         arrow.state = widget.state;
66         arrowTransforms = updateQuadTransforms(
67             widget.view.cameraView,
68             arrowPosition,
69             arrowSize
70         );
71     }
72 
73     private void updateShortcutText() {
74         if (widget.shortcut.length == 0)
75             return;
76 
77         with (shortcutText.attrs[widget.state]) {
78             if (widget.shortcut[0] == '@') {
79                 const shortcutPath = widget.shortcut[1 .. $];
80                 caption = to!dstring(widget.view.shortcuts.getShourtcutString(shortcutPath));
81             } else {
82                 caption = to!dstring(widget.shortcut);
83             }
84 
85             textAlign = Align.right;
86             textVerticalAlign = widget.textVerticalAlign;
87         }
88 
89         auto shortcutTransforms = transformSystem.getCaptonTransforms(Align.right);
90 
91         if (widget.menu !is null) {
92             shortcutTransforms.position.x -= arrowAreaWidth;
93         }
94 
95         shortcutTextTransforms = updateUiTextTransforms(
96             &shortcutText.render,
97             &theme.regularFont,
98             shortcutTextTransforms,
99             shortcutText.attrs[widget.state],
100             widget.view.cameraView,
101             shortcutTransforms.position,
102             shortcutTransforms.size
103         );
104     }
105 }