1 /*
2     Copyright © 2020, Inochi2D Project
3     Distributed under the 2-Clause BSD License, see LICENSE file.
4     
5     Authors: Luna Nielsen
6 */
7 module creator.widgets.dummy;
8 import creator.widgets;
9 import std.math : abs;
10 
11 /**
12     More advanced dummy widget
13 */
14 void incDummy(ImVec2 size = ImVec2(0, 0)) {
15     ImVec2 avail = incAvailableSpace();
16     if (size.x <= 0) size.x = avail.x - abs(size.x);
17     if (size.y <= 0) size.y = avail.y - abs(size.y);
18     igDummy(size);
19 }
20 
21 /**
22     A dummy with a label over it.
23 */
24 void incDummyLabel(string text, ImVec2 area=ImVec2(0, 0)) {
25     ImVec2 avail = incAvailableSpace();
26     ImVec2 size = area;
27     if (size.x <= 0) size.x = avail.x;
28     if (size.y <= 0) size.y = avail.y;
29     incLabelOver(text, size);
30     incDummy(size);
31 }
32 
33 /**
34     Darkens an area and puts a label over it
35 */
36 void incLabelOver(string text, ImVec2 size = ImVec2(0, 0), bool entireWindow=false) {
37     auto window = igGetCurrentWindow();
38     auto dlist = igGetWindowDrawList();
39     auto style = igGetStyle();
40     ImVec2 origin;
41     ImVec2 textSize;
42 
43     igGetCursorScreenPos(&origin);
44     textSize = incMeasureString(text);
45     float xPadding = style.FramePadding.x;
46     float yPadding = style.FramePadding.y;
47 
48     if (entireWindow) { 
49         origin = window.OuterRectClipped.Max;
50         if (size.x <= 0) size.x = window.OuterRectClipped.Min.x-window.OuterRectClipped.Max.x;
51         if (size.y <= 0) size.y = window.OuterRectClipped.Min.y-window.OuterRectClipped.Max.y;
52         igPushClipRect(window.OuterRectClipped.Max, window.OuterRectClipped.Min, false);
53     } else {
54         if (size.x <= 0) size.x = window.WorkRect.Min.x-origin.x;
55         if (size.y <= 0) size.y = window.WorkRect.Min.y-origin.y;
56     }
57 
58     ImDrawList_AddRectFilled(dlist, origin, ImVec2(origin.x+size.x, origin.y+size.y), igGetColorU32(ImVec4(0, 0, 0, 0.20f)), style.WindowRounding*0.5);
59 
60     ImVec2 tl = ImVec2(
61         origin.x+((size.x-(xPadding+textSize.x))*0.5), 
62         origin.y+((size.y-(yPadding+textSize.y))*0.5)
63     );
64     
65     ImVec2 br = ImVec2(
66         origin.x+((size.x+(xPadding+textSize.x))*0.5), 
67         origin.y+((size.y+(yPadding+textSize.y))*0.5)
68     );
69     
70     ImVec2 tls = ImVec2(
71         tl.x-1,
72         tl.y-1
73     );
74     
75     ImVec2 brs = ImVec2(
76         br.x+1,
77         br.y+1
78     );
79     
80     // Draw outline
81     ImDrawList_AddRectFilled(dlist, tls, brs, igGetColorU32(ImGuiCol.Text, 0.15), 4);
82 
83     // Draw button
84     ImDrawList_AddRectFilled(dlist, tl, br, igGetColorU32(ImGuiCol.WindowBg), 4);
85 
86     // Draw text
87     ImDrawList_AddText(dlist, 
88         ImVec2(
89             origin.x+((size.x-textSize.x)*0.5),
90             origin.y+((size.y-textSize.y)*0.5),
91         ),
92         igGetColorU32(ImGuiCol.Text),
93         text.ptr,
94         text.ptr+text.length
95     );
96     if (entireWindow) igPopClipRect();
97 }
98 
99 /**
100     A same-line spacer
101 */
102 void incSpacer(ImVec2 size) {
103     igSameLine(0, 0);
104     incDummy(size);
105 }
106 
107 /**
108     Gets available space
109 */
110 ImVec2 incAvailableSpace() {
111     ImVec2 avail;
112     igGetContentRegionAvail(&avail);
113     return avail;
114 }
115 
116 /**
117     Measures a string in pixels
118 */
119 ImVec2 incMeasureString(string text) {
120     ImVec2 strLen;
121     igCalcTextSize(&strLen, text.ptr, text.ptr+text.length);
122     return strLen;
123 }