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.panels;
8 import creator.core.settings;
9 import bindbc.imgui;
10 import std.string;
11 import i18n;
12 
13 public import creator.panels.logger;
14 
15 /**
16     A Widget
17 */
18 abstract class Panel {
19 private:
20     string name_;
21     string displayName_;
22     bool defaultVisibility;
23     const(char)* windowID;
24     const(char)* displayNamePtr;
25 
26 protected:
27     ImVec2 panelSpace;
28     abstract void onUpdate();
29     ImGuiWindowFlags flags;
30 
31     void onBeginUpdate() {
32         igBegin(windowID, &visible, flags);
33         igGetContentRegionAvail(&panelSpace);
34     }
35     
36     void onEndUpdate() {
37         igEnd();
38     }
39 
40     void onInit() { }
41 
42 public:
43 
44     /**
45         Whether the panel is visible
46     */
47     bool visible;
48 
49     /**
50         Whether the panel is always visible
51     */
52     bool alwaysVisible = false;
53 
54     /**
55         Constructs a panel
56     */
57     this(string name, string displayName, bool defaultVisibility) {
58         this.name_ = name;
59         this.displayName_ = displayName;
60         this.visible = defaultVisibility;
61         this.defaultVisibility = defaultVisibility;
62     }
63 
64     /**
65         Initializes the Panel
66     */
67     final void init_() {
68         onInit();
69 
70         // Workaround for the fact that panels are initialized in shared static this
71         this.displayName_ = _(this.displayName_);
72         if (incSettingsCanGet(this.name_~".visible")) {
73             visible = incSettingsGet!bool(this.name_~".visible");
74         }
75 
76         windowID = "%s###%s".format(displayName_, name_).toStringz;
77         displayNamePtr = displayName_.toStringz;
78     }
79 
80     final string name() {
81         return name_;
82     }
83 
84     final string displayName() {
85         return displayName_;
86     }
87 
88     final const(char)* displayNameC() {
89         return displayNamePtr;
90     }
91 
92     /**
93         Draws the panel
94     */
95     final void update() {
96         this.onBeginUpdate();
97             this.onUpdate();
98         this.onEndUpdate();
99     }
100 
101     final bool getDefaultVisibility() {
102         return defaultVisibility;
103     }
104 }
105 
106 /**
107     Auto generate panel adder
108 */
109 template incPanel(T) {
110     static this() {
111         incAddPanel(new T);
112     }
113 }
114 
115 /**
116     Adds panel to panel list
117 */
118 void incAddPanel(Panel panel) {
119     incPanels ~= panel;
120 }
121 
122 /**
123     Draws panels
124 */
125 void incUpdatePanels() {
126     foreach(panel; incPanels) {
127         if (!panel.visible && !panel.alwaysVisible) continue;
128 
129         panel.update();
130     }
131 }
132 
133 /**
134     Draws panels
135 */
136 void incInitPanels() {
137     foreach(panel; incPanels) {
138         panel.init_();
139     }
140 }
141 
142 /**
143     Panel list
144 */
145 Panel[] incPanels;