1 module rpui.widgets.button.widget;
2 
3 import std.container.array;
4 
5 import rpui.math;
6 import rpui.primitives;
7 import rpui.widget;
8 import rpui.view;
9 import rpui.events;
10 import rpui.render.renderer;
11 import rpui.widgets.button.renderer;
12 import rpui.widgets.button.theme_loader;
13 
14 class Button : Widget {
15     @field bool allowCheck = false;
16     @field Align textAlign = Align.center;
17     @field VerticalAlign textVerticalAlign = VerticalAlign.middle;
18     @field Array!string icons;
19     @field utf32string caption = "Button";
20 
21     struct Measure {
22         float textWidth = 0;
23         vec2 focusOffsets;
24         float focusResize;
25         float textLeftMargin;
26         float textRightMargin;
27         float iconGaps;
28         vec2 iconOffsets;
29         float iconsAreaSize = 0;
30     }
31 
32     package string iconsGroup;
33     public Measure measure;
34     package ButtonThemeLoader themeLoader;
35 
36     this(in string style = "Button", in string iconsGroup = "icons") {
37         super(style);
38 
39         this.drawChildren = false;
40         this.iconsGroup = iconsGroup;
41 
42         // TODO: rm hardcode
43         size = vec2(50, 21);
44         widthType = SizeType.wrapContent;
45         renderer = new ButtonRenderer();
46     }
47 
48     override void onProgress(in ProgressEvent event) {
49         locator.updateLocationAlign();
50         locator.updateVerticalLocationAlign();
51         locator.updateAbsolutePosition();
52         locator.updateRegionAlign();
53         updateSize();
54         renderer.onProgress(event);
55     }
56 
57     override void updateSize() {
58         super.updateSize();
59 
60         if (widthType == SizeType.wrapContent) {
61             if (!icons.empty) {
62                 size.x = measure.iconsAreaSize + measure.iconGaps + measure.iconOffsets.x * 2;
63             } else {
64                 size.x = measure.textLeftMargin + measure.textRightMargin;
65             }
66 
67             if (measure.textWidth != 0f) {
68                 size.x += measure.textWidth;
69 
70                 if (!icons.empty) {
71                     size.x += measure.textLeftMargin;
72                 }
73             }
74         }
75     }
76 
77     protected override void onCreate() {
78         super.onCreate();
79         measure = themeLoader.readMeasure(view.theme.tree.data, style);
80     }
81 }