aboutsummaryrefslogtreecommitdiff
path: root/vendor/zgui/libs/imgui_test_engine/imgui_te_utils.h
blob: 1eef55cc2cdec2857e2e36637ea9e72585bbb26c (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
// dear imgui test engine
// (helpers/utilities. do NOT use this as a general purpose library)

#pragma once

//-----------------------------------------------------------------------------
// Includes
//-----------------------------------------------------------------------------

#include <math.h>   // fabsf
#include <stdint.h> // uint64_t
#include <stdio.h>  // FILE*
#include "imgui.h"  // ImGuiID, ImGuiKey
class Str;          // Str<> from thirdparty/Str/Str.h

//-----------------------------------------------------------------------------
// Function Pointers
//-----------------------------------------------------------------------------

#if IMGUI_TEST_ENGINE_ENABLE_STD_FUNCTION
#include <functional>
#define ImFuncPtr(FUNC_TYPE)        std::function<FUNC_TYPE>
#else
#define ImFuncPtr(FUNC_TYPE)        FUNC_TYPE*
#endif

//-----------------------------------------------------------------------------
// Hashing Helpers
//-----------------------------------------------------------------------------

ImGuiID     ImHashDecoratedPath(const char* str, const char* str_end = NULL, ImGuiID seed = 0);
const char* ImFindNextDecoratedPartInPath(const char* str, const char* str_end = NULL);

//-----------------------------------------------------------------------------
// File/Directory Helpers
//-----------------------------------------------------------------------------

bool        ImFileExist(const char* filename);
bool        ImFileDelete(const char* filename);
bool        ImFileCreateDirectoryChain(const char* path, const char* path_end = NULL);
bool        ImFileFindInParents(const char* sub_path, int max_parent_count, Str* output);
bool        ImFileLoadSourceBlurb(const char* filename, int line_no_start, int line_no_end, ImGuiTextBuffer* out_buf);

//-----------------------------------------------------------------------------
// Path Helpers
//-----------------------------------------------------------------------------

// Those are strictly string manipulation functions
const char* ImPathFindFilename(const char* path, const char* path_end = NULL);      // Return value always between path and path_end
const char* ImPathFindExtension(const char* path, const char* path_end = NULL);     // Return value always between path and path_end
void        ImPathFixSeparatorsForCurrentOS(char* buf);

//-----------------------------------------------------------------------------
// String Helpers
//-----------------------------------------------------------------------------

void        ImStrReplace(Str* s, const char* find, const char* repl);
const char* ImStrchrRangeWithEscaping(const char* str, const char* str_end, char find_c);
void        ImStrXmlEscape(Str* s);
int         ImStrBase64Encode(const unsigned char* src, char* dst, int length);

//-----------------------------------------------------------------------------
// Parsing Helpers
//-----------------------------------------------------------------------------

void        ImParseExtractArgcArgvFromCommandLine(int* out_argc, char const*** out_argv, const char* cmd_line);
bool        ImParseFindIniSection(const char* ini_config, const char* header, ImVector<char>* result);

//-----------------------------------------------------------------------------
// Time Helpers
//-----------------------------------------------------------------------------

uint64_t    ImTimeGetInMicroseconds();
void        ImTimestampToISO8601(uint64_t timestamp, Str* out_date);

//-----------------------------------------------------------------------------
// Threading Helpers
//-----------------------------------------------------------------------------

void        ImThreadSleepInMilliseconds(int ms);
void        ImThreadSetCurrentThreadDescription(const char* description);

//-----------------------------------------------------------------------------
// Build Info helpers
//-----------------------------------------------------------------------------

// All the pointers are expect to be literals/persistent
struct ImBuildInfo
{
    const char*     Type = "";
    const char*     Cpu = "";
    const char*     OS = "";
    const char*     Compiler = "";
    char            Date[32];       // "YYYY-MM-DD"
    const char*     Time = "";
};

const ImBuildInfo*  ImBuildGetCompilationInfo();
bool                ImBuildFindGitBranchName(const char* git_repo_path, Str* branch_name);

//-----------------------------------------------------------------------------
// Operating System Helpers
//-----------------------------------------------------------------------------

enum ImOsConsoleStream
{
    ImOsConsoleStream_StandardOutput,
    ImOsConsoleStream_StandardError,
};

enum ImOsConsoleTextColor
{
    ImOsConsoleTextColor_Black,
    ImOsConsoleTextColor_White,
    ImOsConsoleTextColor_BrightWhite,
    ImOsConsoleTextColor_BrightRed,
    ImOsConsoleTextColor_BrightGreen,
    ImOsConsoleTextColor_BrightBlue,
    ImOsConsoleTextColor_BrightYellow,
};

bool        ImOsCreateProcess(const char* cmd_line);
FILE*       ImOsPOpen(const char* cmd_line, const char* mode);
void        ImOsPClose(FILE* fp);
void        ImOsOpenInShell(const char* path);
bool        ImOsIsDebuggerPresent();
void        ImOsOutputDebugString(const char* message);
void        ImOsConsoleSetTextColor(ImOsConsoleStream stream, ImOsConsoleTextColor color);

//-----------------------------------------------------------------------------
// Miscellaneous functions
//-----------------------------------------------------------------------------

// Tables functions
struct ImGuiTable;
ImGuiID     TableGetHeaderID(ImGuiTable* table, const char* column, int instance_no = 0);
ImGuiID     TableGetHeaderID(ImGuiTable* table, int column_n, int instance_no = 0);
void        TableDiscardInstanceAndSettings(ImGuiID table_id);

// DrawData functions
void        DrawDataVerifyMatchingBufferCount(ImDrawData* draw_data);

//-----------------------------------------------------------------------------
// Helper: maintain/calculate moving average
//-----------------------------------------------------------------------------

template<typename TYPE>
struct ImMovingAverage
{
    // Internal Fields
    ImVector<TYPE>  Samples;
    TYPE            Accum;
    int             Idx;
    int             FillAmount;

    // Functions
    ImMovingAverage()               { Accum = (TYPE)0; Idx = FillAmount = 0; }
    void    Init(int count)         { Samples.resize(count); memset(Samples.Data, 0, (size_t)Samples.Size * sizeof(TYPE)); Accum = (TYPE)0; Idx = FillAmount = 0; }
    void    AddSample(TYPE v)       { Accum += v - Samples[Idx]; Samples[Idx] = v; if (++Idx == Samples.Size) Idx = 0; if (FillAmount < Samples.Size) FillAmount++;  }
    TYPE    GetAverage() const      { return Accum / (TYPE)FillAmount; }
    int     GetSampleCount() const  { return Samples.Size; }
    bool    IsFull() const          { return FillAmount == Samples.Size; }
};

//-----------------------------------------------------------------------------
// Helper: Simple/dumb CSV parser
//-----------------------------------------------------------------------------

struct ImGuiCsvParser
{
    // Public fields
    int             Columns = 0;                    // Number of columns in CSV file.
    int             Rows = 0;                       // Number of rows in CSV file.

    // Internal fields
    char*           _Data = NULL;                   // CSV file data.
    ImVector<char*> _Index;                         // CSV table: _Index[row * _Columns + col].

    // Functions
    ImGuiCsvParser(int columns = -1)                { Columns = columns; }
    ~ImGuiCsvParser()                               { Clear(); }
    bool            Load(const char* file_name);    // Open and parse a CSV file.
    void            Clear();                        // Free allocated buffers.
    const char*     GetCell(int row, int col)       { IM_ASSERT(0 <= row && row < Rows && 0 <= col && col < Columns); return _Index[row * Columns + col]; }
};

//-----------------------------------------------------------------------------
// Misc Dear ImGui extensions
//-----------------------------------------------------------------------------

#if IMGUI_VERSION_NUM < 18924
struct ImGuiTabBar;
struct ImGuiTabItem;
#endif

namespace ImGui
{

IMGUI_API void      ItemErrorFrame(ImU32 col);

#if IMGUI_VERSION_NUM < 18927
ImGuiID             TableGetInstanceID(ImGuiTable* table, int instance_no = 0);
#endif

// Str support for InputText()
IMGUI_API bool      InputText(const char* label, Str* str, ImGuiInputTextFlags flags = 0, ImGuiInputTextCallback callback = NULL, void* user_data = NULL);
IMGUI_API bool      InputTextWithHint(const char* label, const char* hint, Str* str, ImGuiInputTextFlags flags = 0, ImGuiInputTextCallback callback = NULL, void* user_data = NULL);
IMGUI_API bool      InputTextMultiline(const char* label, Str* str, const ImVec2& size = ImVec2(0, 0), ImGuiInputTextFlags flags = 0, ImGuiInputTextCallback callback = NULL, void* user_data = NULL);

// Splitter
IMGUI_API bool      Splitter(const char* id, float* value_1, float* value_2, int axis, int anchor = 0, float min_size_0 = -1.0f, float min_size_1 = -1.0f);

// Misc
IMGUI_API ImFont*   FindFontByPrefix(const char* name);

// Legacy version support
#if IMGUI_VERSION_NUM < 18924
IMGUI_API const char* TabBarGetTabName(ImGuiTabBar* tab_bar, ImGuiTabItem* tab);
#endif

}