1 module creator.widgets.texture;
2 import creator.widgets;
3 import creator.core;
4 import bindbc.imgui;
5 import inmath;
6 import inochi2d;
7 import std.math : quantize;
8 import bindbc.opengl : GL_RGBA;
9 
10 /**
11     Renders a texture slot with specified size
12 */
13 void incTextureSlot(string text, Texture texture, ImVec2 size = ImVec2(92, 92), float gridSize = 32, ImGuiWindowFlags flags = ImGuiWindowFlags.None) {
14     if (igBeginChildFrame(igGetID(text.ptr, text.ptr+text.length), size, flags | ImGuiWindowFlags.NoScrollbar | ImGuiWindowFlags.NoScrollWithMouse)) {
15 
16         ImVec2 startPos;
17         igGetCursorPos(&startPos);
18 
19         float paddingX = igGetStyle().FramePadding.x;
20         float paddingY = igGetStyle().FramePadding.y;
21 
22 
23         igBeginGroup();
24 
25             // Only draw texture stuff if we actually have a texture
26             if (texture) {
27 
28                 // NOTE: These variables may be reused if the texture is square.
29                 float qsizex = quantize(size.x, gridSize/2);
30                 float qsizey = quantize(size.y, gridSize/2);
31 
32                 ImVec2 screenStart;
33                 auto drawList = igGetWindowDrawList();
34                 igGetCursorScreenPos(&screenStart);
35 
36                 // Draw background grid
37                 ImVec2 gridMin = ImVec2(screenStart.x-(paddingX/2), screenStart.y-(paddingY/2));
38                 ImVec2 gridMax = ImVec2(gridMin.x+size.x-paddingX, gridMin.y+size.y-paddingY);
39 
40                 // Draw transparent background if there is any transparency
41                 if (texture.colorMode == GL_RGBA) {
42                     ImDrawList_AddImageRounded(
43                         drawList,
44                         cast(ImTextureID)incGetGrid().getTextureId(),
45                         gridMin,
46                         gridMax,
47                         ImVec2(0, 0),
48                         ImVec2(
49                             clamp(quantize((qsizex/gridSize), 0.5), 1, float.max),
50                             clamp(quantize((qsizey/gridSize), 0.5), 1, float.max),
51                         ),
52                         0xFFFFFFFF,
53                         igGetStyle().FrameRounding
54                     );
55                 } else {
56                     ImDrawList_AddRectFilled(
57                         drawList,
58                         gridMin,
59                         gridMax,
60                         0xFF000000,
61                         igGetStyle().FrameRounding
62                     );
63                 }
64 
65                 // Calculate padded size
66                 float paddedSizeX = size.x-(paddingX*2);
67                 float paddedSizeY = size.y-(paddingY*2);
68 
69                 // Calculate render size
70                 float widthScale = paddedSizeX / cast(float)texture.width;
71                 float heightScale = paddedSizeY / cast(float)texture.height;
72                 float scale = min(widthScale, heightScale);
73 
74                 if (widthScale == heightScale) {
75 
76                     // Texture is square
77                     ImDrawList_AddImageRounded(
78                         drawList,
79                         cast(ImTextureID)texture.getTextureId(),
80                         gridMin,
81                         gridMax,
82                         ImVec2(0, 0),
83                         ImVec2(1, 1),
84                         0xFFFFFFFF,
85                         igGetStyle().FrameRounding
86                     );
87 
88                 } else {
89 
90                     // Texture is non-square
91                     igSetCursorPos(startPos);
92                 
93                     vec4 bounds = vec4(0, 0, texture.width*scale, texture.height*scale);
94                     if (widthScale > heightScale) bounds.x = (paddedSizeX-bounds.z)/2;
95                     else if (widthScale < heightScale) bounds.y = (paddedSizeY-bounds.w)/2;
96 
97                     // Draw texture preview
98                     igSetCursorPos(ImVec2(startPos.x+bounds.x, startPos.y+bounds.y));
99                     igImage(
100                         cast(ImTextureID)texture.getTextureId(),
101                         ImVec2(bounds.z, bounds.w)
102                     );
103                 }
104             }
105         igEndGroup();
106         
107         auto origFrameBG = igGetStyle().Colors[ImGuiCol.FrameBg];
108         igPushStyleColor(ImGuiCol.FrameBg, ImVec4(origFrameBG.x, origFrameBG.y, origFrameBG.z, 0.5));
109             // Draw text
110             igSetCursorPos(startPos);
111             ImVec2 textSize = incMeasureString(text);
112             if (igBeginChildFrame(igGetID("LABEL"), ImVec2(clamp(textSize.x+paddingX*2, 8, size.x-(paddingX*2)), textSize.y+paddingY*2), ImGuiWindowFlags.NoScrollbar | ImGuiWindowFlags.NoScrollWithMouse)) {
113                 incText(text);
114             }
115             igEndChildFrame();
116         igPopStyleColor();
117     }
118     igEndChildFrame();
119 }
120 
121 /**
122     Renders a texture slot with specified size
123 */
124 void incTextureSlotUntitled(string name, Texture texture, ImVec2 size = ImVec2(92, 92), float gridSize = 32, ImGuiWindowFlags flags = ImGuiWindowFlags.None) {
125     if (igBeginChildFrame(igGetID(name.ptr, name.ptr+name.length), size, flags | ImGuiWindowFlags.NoScrollbar | ImGuiWindowFlags.NoScrollWithMouse)) {
126 
127         ImVec2 startPos;
128         igGetCursorPos(&startPos);
129 
130         float paddingX = igGetStyle().FramePadding.x;
131         float paddingY = igGetStyle().FramePadding.y;
132 
133 
134         igBeginGroup();
135 
136             // Only draw texture stuff if we actually have a texture
137             if (texture) {
138 
139                 // NOTE: These variables may be reused if the texture is square.
140                 float qsizex = quantize(size.x, gridSize/2);
141                 float qsizey = quantize(size.y, gridSize/2);
142 
143                 ImVec2 screenStart;
144                 auto drawList = igGetWindowDrawList();
145                 igGetCursorScreenPos(&screenStart);
146 
147                 // Draw background grid
148                 ImVec2 gridMin = ImVec2(screenStart.x-(paddingX/2), screenStart.y-(paddingY/2));
149                 ImVec2 gridMax = ImVec2(gridMin.x+size.x-paddingX, gridMin.y+size.y-paddingY);
150 
151                 // Draw transparent background if there is any transparency
152                 if (texture.colorMode == GL_RGBA) {
153                     ImDrawList_AddImageRounded(
154                         drawList,
155                         cast(ImTextureID)incGetGrid().getTextureId(),
156                         gridMin,
157                         gridMax,
158                         ImVec2(0, 0),
159                         ImVec2(
160                             clamp(quantize((qsizex/gridSize), 0.5), 1, float.max),
161                             clamp(quantize((qsizey/gridSize), 0.5), 1, float.max),
162                         ),
163                         0xFFFFFFFF,
164                         igGetStyle().FrameRounding
165                     );
166                 } else {
167                     ImDrawList_AddRectFilled(
168                         drawList,
169                         gridMin,
170                         gridMax,
171                         0xFF000000,
172                         igGetStyle().FrameRounding
173                     );
174                 }
175 
176                 // Calculate padded size
177                 float paddedSizeX = size.x-(paddingX*2);
178                 float paddedSizeY = size.y-(paddingY*2);
179 
180                 // Calculate render size
181                 float widthScale = paddedSizeX / cast(float)texture.width;
182                 float heightScale = paddedSizeY / cast(float)texture.height;
183                 float scale = min(widthScale, heightScale);
184 
185                 if (widthScale == heightScale) {
186 
187                     // Texture is square
188                     ImDrawList_AddImageRounded(
189                         drawList,
190                         cast(ImTextureID)texture.getTextureId(),
191                         gridMin,
192                         gridMax,
193                         ImVec2(0, 0),
194                         ImVec2(1, 1),
195                         0xFFFFFFFF,
196                         igGetStyle().FrameRounding
197                     );
198 
199                 } else {
200 
201                     // Texture is non-square
202                     igSetCursorPos(startPos);
203                 
204                     vec4 bounds = vec4(0, 0, texture.width*scale, texture.height*scale);
205                     if (widthScale > heightScale) bounds.x = (paddedSizeX-bounds.z)/2;
206                     else if (widthScale < heightScale) bounds.y = (paddedSizeY-bounds.w)/2;
207 
208                     // Draw texture preview
209                     igSetCursorPos(ImVec2(startPos.x+bounds.x, startPos.y+bounds.y));
210                     igImage(
211                         cast(ImTextureID)texture.getTextureId(),
212                         ImVec2(bounds.z, bounds.w)
213                     );
214                 }
215             }
216         igEndGroup();
217     }
218     igEndChildFrame();
219 }