Welcome to mirror list, hosted at ThFree Co, Russian Federation.

imgui.h « IMUI « GUI « Source - github.com/WolfireGames/overgrowth.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: e567907ee683feb251ca3cd6c12c6b3969b523a7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
//-----------------------------------------------------------------------------
//           Name: imgui.h
//      Developer: Wolfire Games LLC
//    Description: Main class for creating adhoc GUIs as part of the UI tools
//        License: Read below
//-----------------------------------------------------------------------------
//
//
//   Copyright 2022 Wolfire Games LLC
//
//   Licensed under the Apache License, Version 2.0 (the "License");
//   you may not use this file except in compliance with the License.
//   You may obtain a copy of the License at
//
//       http://www.apache.org/licenses/LICENSE-2.0
//
//   Unless required by applicable law or agreed to in writing, software
//   distributed under the License is distributed on an "AS IS" BASIS,
//   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//   See the License for the specific language governing permissions and
//   limitations under the License.
//
//-----------------------------------------------------------------------------
#pragma once

#include <GUI/IMUI/imui.h>
#include <GUI/IMUI/im_element.h>
#include <GUI/IMUI/im_message.h>
#include <GUI/IMUI/im_support.h>
#include <GUI/IMUI/im_container.h>
#include <GUI/IMUI/im_events.h>

/*******************************************************************************************/
/**
 * @brief One thing to rule them all
 *
 */
class IMGUI : public IMEventListener {
    uint64_t lastUpdateTime;     // When was this last updated (ms)
    unsigned int elementCount;   // Counter for naming unnamed elements
    bool needsRelayout;          // has a relayout signal been fired?
    std::string reportedErrors;  // have any errors been reported?
    bool showGuides;             // overlay some extra lines to aid layout

    vec2 mousePosition;                       // Where is the mouse?
    IMUIContext::ButtonState leftMouseState;  // What's the state of the left mouse button?
    std::vector<IMMessage*> messageQueue;     // Messages waiting to process
    std::vector<IMContainer*> backgrounds;    // Containers for background layers, if any
    std::vector<IMContainer*> foregrounds;    // Containers for foreground layers, if any

    std::vector<IMContainer*> header;  // header panels
    std::vector<IMContainer*> footer;  // footer panels
    std::vector<IMContainer*> main;    // main panels

    float mainOffset;    // y offset for the main panel
    float footerOffset;  // y offset for the footer panel

    float headerHeight;  // size of the header (0 if none)
    float footerHeight;  // size of the footer (0 if none)
    float mainHeight;    // size of the main panel (0 if none)

    std::vector<float> headerPanelWidths;  // Width of the header panels
    std::vector<float> footerPanelWidths;  // Width of the header panels
    std::vector<float> mainPanelWidths;    // Width of the header panels

    float headerPanelGap;  // How much space between header panels
    float footerPanelGap;  // How much space between footer panels
    float mainPanelGap;    // How much space between main panels

    unsigned int pendingFGLayers;  // Foreground layers to create on 'setup'
    unsigned int pendingBGLayers;  // Background layers to create on 'setup'
    float pendingHeaderHeight;     // Size of header to create on 'setup'
    float pendingFooterHeight;     // Size of footer to create on 'setup'

    std::vector<float> pendingHeaderPanels;  // Sizes of header panels
    std::vector<float> pendingMainPanels;    // Sizes of main panels
    std::vector<float> pendingFooterPanels;  // Sizes of footer panels

    vec2 screenSize;  // for detecting changes

    int refCount;  // for AS reference counting

    /*******************************************************************************************/
    /**
     * @brief  If a relayout is requested, perform it
     *
     */
    void doRelayout();

    /*******************************************************************************************/
    /**
     * @brief  Figure out where we’re going to render the main panels - used internally
     *
     */
    void derivePanelOffsets();

   private:
    IMGUI(IMGUI const& other);

   public:
    int guiid;

    GUIState guistate;  // current state of the GUI, updated at 'update'

    IMUIContext* IMGUI_IMUIContext;  // UI features from the engine

    /*******************************************************************************************/
    /**
     * @brief Constructor
     *
     * @param mainOrientation The orientation of the container
     *
     */
    IMGUI();

    /*******
     *
     * Angelscript memory management boilerplate
     *
     */
    void AddRef();
    void Release();

    /*******************************************************************************************/
    /**
     * @brief  Set the number of background layers
     *
     *  NOTE: This will take effect when setup is called next
     *
     *  Background layers are rendered highest index value first
     *
     * @param numLayers number of layers required
     *
     */
    void setBackgroundLayers(unsigned int numLayers);

    /*******************************************************************************************/
    /**
     * @brief  Set the number of foreground layers
     *
     *  NOTE: This will take effect when setup is called next
     *
     *  Foreground layers are rendered lowest index value first
     *
     * @param numLayers number of layers required
     *
     */
    void setForegroundLayers(unsigned int numLayers);

    /*******************************************************************************************/
    /**
     * @brief  Set the size of the header (0 for none)
     *
     *  NOTE: This will take effect when setup is called next
     *
     * @param _headerSize size in GUI space
     *
     */
    void setHeaderHeight(float _headerSize);

    /*******************************************************************************************/
    /**
     * @brief  Set the size of the footer (0 for none)
     *
     *  NOTE: This will take effect when setup is called next
     *
     * @param _footerSize size in GUI space
     *
     */
    void setFooterHeight(float _footerSize);

    /*******************************************************************************************/
    /**
     * @brief  Sets the sizes of the various header panels
     *
     * If not specified (specified means non-zero) there will be one panel, full width
     * If one is specified, it will be centered
     * If two are specified, they will be left and right justified
     * Three will be left, center, right justified
     *
     */
    void setHeaderPanels(float first = 0, float second = 0, float third = 0);

    /*******************************************************************************************/
    /**
     * @brief  Sets the sizes of the various main panels
     *
     * If not specified (specified means non-zero) there will be one panel, full width
     * If one is specified, it will be centered
     * If two are specified, they will be left and right justified
     * Three will be left, center, right justified
     *
     */
    void setMainPanels(float first = 0, float second = 0, float third = 0);

    /*******************************************************************************************/
    /**
     * @brief  Sets the sizes of the various footer panels
     *
     * If not specified (specified means non-zero) there will be one panel, full width
     * If one is specified, it will be centered
     * If two are specified, they will be left and right justified
     * Three will be left, center, right justified
     *
     */
    void setFooterPanels(float first = 0, float second = 0, float third = 0);

    /*******************************************************************************************/
    /**
     * @brief  Sets up the GUI components: header, footer, main panel and foreground/background
     *
     */
    void setup();

    /*******************************************************************************************/
    /**
     * @brief  Clear the GUI - you probably want resetMainLayer though
     *
     * @param mainOrientation The orientation of the container
     *
     */
    void clear();

    /*******************************************************************************************/
    /**
     * @brief  Turns on (or off) the visual guides
     *
     * @param setting True to turn on guides, false otherwise
     *
     */
    void setGuides(bool setting);

    /*******************************************************************************************/
    /**
     * @brief  Records all the errors reported by child elements
     *
     * @param newError Newly reported error string
     *
     */
    void reportError(std::string const& newError);

    /*******************************************************************************************/
    /**
     * @brief  Receives a message - used internally
     *
     * @param message The message in question
     *
     */
    void receiveMessage(IMMessage* message);

    /*******************************************************************************************/
    /**
     * @brief  Gets the size of the waiting message queue
     *
     * @returns size of the queue (as an unsigned integer)
     *
     */
    unsigned int getMessageQueueSize();

    /*******************************************************************************************/
    /**
     * @brief  Gets the next available message in the queue (NULL if none)
     *
     * @returns A copy of the message (NULL if none)
     *
     */
    IMMessage* getNextMessage();

    /*******************************************************************************************/
    /**
     * @brief  Retrieves a reference to the main panel
     *
     */
    IMContainer* getMain(unsigned int panel = 0) {
        if (panel < main.size()) {
            main[panel]->AddRef();
            return main[panel];
        } else {
            return NULL;
        }
    }

    /*******************************************************************************************/
    /**
     * @brief  Retrieves a reference to the footer panel
     *
     */
    IMContainer* getFooter(unsigned int panel = 0) {
        if (panel < footer.size()) {
            footer[panel]->AddRef();
            return footer[panel];
        } else {
            return NULL;
        }
    }

    /*******************************************************************************************/
    /**
     * @brief  Retrieves a reference to the header panel
     *
     */
    IMContainer* getHeader(unsigned int panel = 0) {
        if (panel < header.size()) {
            header[panel]->AddRef();
            return header[panel];
        } else {
            return NULL;
        }
    }

    /*******************************************************************************************/
    /**
     * @brief  Retrieves a reference to a specified background layer
     *
     * @param layerNum index of the background layer (starting at 0)
     *
     */
    IMContainer* getBackgroundLayer(unsigned int layerNum = 0);

    /*******************************************************************************************/
    /**
     * @brief  Retrieves a reference to a specified foreground layer
     *
     * @param layerNum index of the foreground layer (starting at 0)
     *
     */
    IMContainer* getForegroundLayer(unsigned int layerNum = 0);

    /*******************************************************************************************/
    /**
     * @brief  When this element is resized, moved, etc propagate this signal upwards
     *
     */
    void onRelayout();

    /*******************************************************************************************/
    /**
     * @brief  Updates the gui
     *
     */
    void update();

    /*******************************************************************************************/
    /**
     * @brief  Fired when the screen resizes
     *
     */
    void doScreenResize();

    /*******************************************************************************************/
    /**
     * @brief  Render the GUI
     *
     */
    void render();

    /*******************************************************************************************/
    /**
     * @brief  Draw a box (in *screen* coordinates) -- used internally
     *
     */
    void drawBox(vec2 boxPos, vec2 boxSize, vec4 boxColor, int zOrder, bool shouldClip = false,
                 vec2 currentClipPos = vec2(UNDEFINEDSIZE, UNDEFINEDSIZE),
                 vec2 currentClipSize = vec2(UNDEFINEDSIZE, UNDEFINEDSIZE));

    /*******************************************************************************************/
    /**
     * @brief  Finds an element in the gui by a given name
     *
     * @param elementName the name of the element
     *
     * @returns handle to the element (NULL if not found)
     *
     */
    IMElement* findElement(std::string const& elementName);

    /*******************************************************************************************/
    /**
     * @brief  Gets a unique name for assigning to unnamed elements (used internally)
     *
     * @returns Unique name as string
     *
     */
    std::string getUniqueName(std::string const& type = "Unkowntype");

    /*******************************************************************************************/
    /**
     * @brief  Remove all referenced object without releaseing references
     *
     */
    virtual void clense();

    /*******************************************************************************************/
    /**
     * @brief Destructor
     *
     */
    virtual ~IMGUI();

    void DestroyedIMElement(IMElement* element) override;
    void DestroyedIMGUI(IMGUI* IMGUI) override;
};