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