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.toolbar;
8 import creator.viewport;
9 import creator.widgets;
10 import creator.core;
11 import creator;
12 import i18n;
13 
14 void incToolbar() {
15     auto flags = 
16         ImGuiWindowFlags.NoSavedSettings |
17         ImGuiWindowFlags.NoScrollbar |
18         ImGuiWindowFlags.MenuBar;
19 
20     igPushStyleColor(ImGuiCol.Border, ImVec4(0, 0, 0, 0));
21     igPushStyleColor(ImGuiCol.BorderShadow, ImVec4(0, 0, 0, 0));
22     igPushStyleColor(ImGuiCol.Separator, ImVec4(0, 0, 0, 0));
23         igPushStyleVar(ImGuiStyleVar.FramePadding, ImVec2(0, 10));
24         if (igBeginViewportSideBar("##Toolbar", igGetMainViewport(), ImGuiDir.Up, 32, flags)) {
25             if (igBeginMenuBar()) {
26                 igPopStyleVar();
27                 
28                 ImVec2 pos;
29                 igGetCursorPos(&pos);
30                 igSetCursorPos(ImVec2(pos.x-igGetStyle().WindowPadding.x, pos.y));
31 
32                 // Render toolbar
33                 igPushStyleVar(ImGuiStyleVar.FramePadding, ImVec2(0, 0));
34                 igPushStyleVar(ImGuiStyleVar.FrameRounding, 0);
35 
36                     if (incButtonColored("", ImVec2(32, 32), incActivePuppet().enableDrivers ? ImVec4.init : ImVec4(0.6f, 0.6f, 0.6f, 1f))) {
37                         incActivePuppet().enableDrivers = !incActivePuppet().enableDrivers;
38                     }
39                     incTooltip(_("Enable physics"));
40 
41                     igSameLine(0, 0);
42 
43                     if (incButtonColored("", ImVec2(32, 32), incShouldPostProcess ? ImVec4.init : ImVec4(0.6f, 0.6f, 0.6f, 1f))) {
44                         incShouldPostProcess = !incShouldPostProcess;
45                     }
46                     incTooltip(_("Enable post processing"));
47 
48                     if (incButtonColored("", ImVec2(32, 32), ImVec4.init)) {
49                         incActivePuppet().resetDrivers();
50                     }
51                     incTooltip(_("Reset physics"));
52 
53                     // Draw the toolbar relevant for that viewport
54                     incViewportToolbar();
55                 igPopStyleVar(2);
56 
57                 // Render mode switch buttons
58                 ImVec2 avail;
59                 igGetContentRegionAvail(&avail);
60                 debug(InExperimental) igDummy(ImVec2(avail.x-(32*3), 0));
61                 else igDummy(ImVec2(avail.x-(32*2), 0));
62                 igPushStyleVar(ImGuiStyleVar.FramePadding, ImVec2(0, 0));
63                 igPushStyleVar(ImGuiStyleVar.FrameRounding, 0);
64                     igPushStyleVar(ImGuiStyleVar.ItemSpacing, ImVec2(0, 0));
65                         if(incEditMode != EditMode.VertexEdit) {
66                             if (incButtonColored("", ImVec2(32, 32), incEditMode == EditMode.ModelEdit ? ImVec4.init : ImVec4(0.6f, 0.6f, 0.6f, 1f))) {
67                                 incSetEditMode(EditMode.ModelEdit);
68                             }
69                             incTooltip(_("Edit Puppet"));
70 
71                             if (incButtonColored("", ImVec2(32, 32), incEditMode == EditMode.AnimEdit ? ImVec4.init : ImVec4(0.6f, 0.6f, 0.6f, 1f))) {
72                                 incSetEditMode(EditMode.AnimEdit);
73                             }
74                             incTooltip(_("Edit Animation"));
75                             debug(InExperimental) {
76                                 if (incButtonColored("", ImVec2(32, 32), incEditMode == EditMode.ModelTest ? ImVec4.init : ImVec4(0.6f, 0.6f, 0.6f, 1f))) {
77                                     incSetEditMode(EditMode.ModelTest);
78                                 }
79                                 incTooltip(_("Test Puppet"));
80                             }
81                         }
82                     igPopStyleVar();
83                 igPopStyleVar(2);
84 
85                 igEndMenuBar();
86             } else {
87                 igPopStyleVar();
88             }
89 
90             incEnd();
91         } else {
92             igPopStyleVar();
93         }
94     igPopStyleColor();
95     igPopStyleColor();
96     igPopStyleColor();
97 }
98 
99 bool incBeginInnerToolbar(float height, bool matchTitlebar=false) {
100 
101     auto style = igGetStyle();
102     auto window = igGetCurrentWindow();
103 
104     auto barColor = matchTitlebar ? (
105         igIsWindowFocused(ImGuiFocusedFlags.RootAndChildWindows) ? 
106             style.Colors[ImGuiCol.TitleBgActive] : 
107             style.Colors[ImGuiCol.TitleBg]
108     ) : style.Colors[ImGuiCol.MenuBarBg];
109 
110     igPushStyleVar(ImGuiStyleVar.FrameRounding, 0);
111     igPushStyleVar(ImGuiStyleVar.ChildBorderSize, 0);
112     igPushStyleVar(ImGuiStyleVar.WindowPadding, ImVec2(0, 0));
113     igPushStyleVar(ImGuiStyleVar.FrameBorderSize, 0);
114     igPushStyleColor(ImGuiCol.ChildBg, barColor);
115     igPushStyleColor(ImGuiCol.Button, barColor);
116 
117     if (!window.IsExplicitChild) {
118         igPushClipRect(ImVec2(window.OuterRectClipped.Max.x, window.OuterRectClipped.Max.y-1), ImVec2(window.OuterRectClipped.Min.x, window.OuterRectClipped.Min.y), false);
119         igSetCursorPosY(igGetCursorPosY()-1);
120     }
121     
122     bool visible = igBeginChild("###Toolbar", ImVec2(0, height), false, ImGuiWindowFlags.NoScrollbar | ImGuiWindowFlags.NoScrollWithMouse);
123     if (visible) igSetCursorPosX(igGetCursorPosX()+style.FramePadding.x);
124     return visible;
125 }
126 
127 void incEndInnerToolbar() {
128     igPopClipRect();
129 
130     igEndChild();
131     igPopStyleColor(2);
132     igPopStyleVar(4);
133 
134     // Move cursor up
135     igSetCursorPosY(igGetCursorPosY()-igGetStyle().ItemSpacing.y);
136 }
137 
138 /**
139     A toolbar button
140 */
141 bool incToolbarButton(const(char)* text, float width = 0) {
142     bool clicked = igButton(text, ImVec2(width, incAvailableSpace().y));
143     igSameLine(0, 0);
144     return clicked;
145 }
146 
147 /**
148     A toolbar button
149 */
150 void incToolbarSpacer(float space) {
151     incDummy(ImVec2(space, 0));
152     igSameLine(0, 0);
153 }
154 
155 /**
156     Vertical separator for toolbar
157 */
158 void incToolbarSeparator() {
159     igSeparatorEx(ImGuiSeparatorFlags.Vertical);
160 }