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     @field int uselessIconArea = 0;
21 
22     struct Measure {
23         float textWidth = 0;
24         vec2 focusOffsets;
25         float focusResize;
26         float textLeftMargin;
27         float textRightMargin;
28         float iconGaps;
29         vec2 iconOffsets;
30         float iconsAreaSize = 0;
31         FrameRect uselessBorders;
32     }
33 
34     package string iconsGroup;
35     public Measure measure;
36     package ButtonThemeLoader themeLoader;
37 
38     this(in string style = "Button", in string iconsGroup = "icons") {
39         super(style);
40 
41         this.drawChildren = false;
42         this.iconsGroup = iconsGroup;
43 
44         // TODO: rm hardcode
45         size = vec2(50, 21);
46         widthType = SizeType.wrapContent;
47         renderer = new ButtonRenderer();
48     }
49 
50     override void onProgress(in ProgressEvent event) {
51         locator.updateLocationAlign();
52         locator.updateVerticalLocationAlign();
53         locator.updateAbsolutePosition();
54         locator.updateRegionAlign();
55         updateSize();
56         renderer.onProgress(event);
57     }
58 
59     override void updateSize() {
60         super.updateSize();
61 
62         if (widthType == SizeType.wrapContent) {
63             if (!icons.empty) {
64                 size.x = measure.iconsAreaSize + measure.iconGaps + measure.iconOffsets.x * 2;
65             } else {
66                 size.x = measure.textLeftMargin + measure.textRightMargin;
67             }
68 
69             if (measure.textWidth != 0f) {
70                 size.x += measure.textWidth;
71 
72                 if (!icons.empty) {
73                     size.x += measure.textLeftMargin;
74                 }
75             }
76         }
77     }
78 
79     protected override void onCreate() {
80         super.onCreate();
81         measure = themeLoader.readMeasure(view.theme.tree.data, style);
82     }
83 }