Open Source Tomb Raider Engine
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

imgui.h 33KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661
  1. // ImGui library
  2. // See .cpp file for commentary.
  3. // See ImGui::ShowTestWindow() for sample code.
  4. // Read 'Programmer guide' in .cpp for notes on how to setup ImGui in your codebase.
  5. // Get latest version at https://github.com/ocornut/imgui
  6. #pragma once
  7. struct ImDrawList;
  8. struct ImBitmapFont;
  9. struct ImGuiAabb;
  10. struct ImGuiIO;
  11. struct ImGuiStorage;
  12. struct ImGuiStyle;
  13. struct ImGuiWindow;
  14. #include "imconfig.h"
  15. #include <float.h> // FLT_MAX
  16. #include <stdarg.h> // va_list
  17. #include <stdlib.h> // NULL, malloc
  18. #ifndef IM_MALLOC
  19. #define IM_MALLOC(_SIZE) malloc((_SIZE))
  20. #endif
  21. #ifndef IM_FREE
  22. #define IM_FREE(_PTR) free((_PTR))
  23. #endif
  24. #ifndef IM_REALLOC
  25. #define IM_REALLOC(_PTR, _SIZE) realloc((_PTR), (_SIZE))
  26. #endif
  27. #ifndef IM_ASSERT
  28. #include <assert.h>
  29. #define IM_ASSERT(_EXPR) assert(_EXPR)
  30. #endif
  31. typedef unsigned int ImU32;
  32. typedef ImU32 ImGuiID;
  33. typedef int ImGuiCol; // enum ImGuiCol_
  34. typedef int ImGuiKey; // enum ImGuiKey_
  35. typedef int ImGuiColorEditMode; // enum ImGuiColorEditMode_
  36. typedef int ImGuiWindowFlags; // enum ImGuiWindowFlags_
  37. typedef int ImGuiInputTextFlags; // enum ImGuiInputTextFlags_
  38. typedef ImBitmapFont* ImFont;
  39. struct ImVec2
  40. {
  41. float x, y;
  42. ImVec2() {}
  43. ImVec2(float _x, float _y) { x = _x; y = _y; }
  44. #ifdef IM_VEC2_CLASS_EXTRA
  45. IM_VEC2_CLASS_EXTRA
  46. #endif
  47. };
  48. struct ImVec4
  49. {
  50. float x, y, z, w;
  51. ImVec4() {}
  52. ImVec4(float _x, float _y, float _z, float _w) { x = _x; y = _y; z = _z; w = _w; }
  53. #ifdef IM_VEC4_CLASS_EXTRA
  54. IM_VEC4_CLASS_EXTRA
  55. #endif
  56. };
  57. // std::vector<> like class to avoid dragging dependencies (also: windows implementation of STL with debug enabled is absurdly slow, so let's bypass it so our code runs fast in debug).
  58. // this implementation does NOT call c++ constructors! we don't need them! also only provide the minimum functionalities we need.
  59. #ifndef ImVector
  60. template<typename T>
  61. class ImVector
  62. {
  63. private:
  64. size_t Size;
  65. size_t Capacity;
  66. T* Data;
  67. public:
  68. typedef T value_type;
  69. typedef value_type* iterator;
  70. typedef const value_type* const_iterator;
  71. ImVector() { Size = Capacity = 0; Data = NULL; }
  72. ~ImVector() { if (Data) IM_FREE(Data); }
  73. inline bool empty() const { return Size == 0; }
  74. inline size_t size() const { return Size; }
  75. inline size_t capacity() const { return Capacity; }
  76. inline value_type& at(size_t i) { IM_ASSERT(i < Size); return Data[i]; }
  77. inline const value_type& at(size_t i) const { IM_ASSERT(i < Size); return Data[i]; }
  78. inline value_type& operator[](size_t i) { IM_ASSERT(i < Size); return Data[i]; }
  79. inline const value_type& operator[](size_t i) const { IM_ASSERT(i < Size); return Data[i]; }
  80. inline void clear() { if (Data) { Size = Capacity = 0; IM_FREE(Data); Data = NULL; } }
  81. inline iterator begin() { return Data; }
  82. inline const_iterator begin() const { return Data; }
  83. inline iterator end() { return Data + Size; }
  84. inline const_iterator end() const { return Data + Size; }
  85. inline value_type& front() { return at(0); }
  86. inline const value_type& front() const { return at(0); }
  87. inline value_type& back() { IM_ASSERT(Size > 0); return at(Size-1); }
  88. inline const value_type& back() const { IM_ASSERT(Size > 0); return at(Size-1); }
  89. inline void swap(ImVector<T>& rhs) { const size_t rhs_size = rhs.Size; rhs.Size = Size; Size = rhs_size; const size_t rhs_cap = rhs.Capacity; rhs.Capacity = Capacity; Capacity = rhs_cap; value_type* rhs_data = rhs.Data; rhs.Data = Data; Data = rhs_data; }
  90. inline void reserve(size_t new_capacity) { Data = (value_type*)IM_REALLOC(Data, new_capacity * sizeof(value_type)); Capacity = new_capacity; }
  91. inline void resize(size_t new_size) { if (new_size > Capacity) reserve(new_size); Size = new_size; }
  92. inline void push_back(const value_type& v) { if (Size == Capacity) reserve(Capacity ? Capacity * 2 : 4); Data[Size++] = v; }
  93. inline void pop_back() { IM_ASSERT(Size > 0); Size--; }
  94. inline iterator erase(const_iterator it) { IM_ASSERT(it >= begin() && it < end()); const int off = it - begin(); memmove(Data + off, Data + off + 1, (Size - off - 1) * sizeof(value_type)); Size--; return Data + off; }
  95. inline void insert(const_iterator it, const value_type& v) { IM_ASSERT(it >= begin() && it <= end()); const int off = it - begin(); if (Size == Capacity) reserve(Capacity ? Capacity * 2 : 4); if (off < (int)Size) memmove(Data + off + 1, Data + off, (Size - off) * sizeof(value_type)); Data[off] = v; Size++; }
  96. };
  97. #endif // #ifndef ImVector
  98. // Helpers at bottom of the file:
  99. // - if (IMGUI_ONCE_UPON_A_FRAME) // Execute a block of code once per frame only
  100. // - struct ImGuiTextFilter // Parse and apply text filters. In format "aaaaa[,bbbb][,ccccc]"
  101. // - struct ImGuiTextBuffer // Text buffer for logging/accumulating text
  102. // - struct ImGuiStorage // Custom key value storage (if you need to alter open/close states manually)
  103. // - struct ImDrawList // Draw command list
  104. // - struct ImBitmapFont // Bitmap font loader
  105. // ImGui End-user API
  106. // In a namespace so that user can add extra functions (e.g. Value() helpers for your vector or common types)
  107. namespace ImGui
  108. {
  109. // Main
  110. ImGuiIO& GetIO();
  111. ImGuiStyle& GetStyle();
  112. void NewFrame();
  113. void Render();
  114. void Shutdown();
  115. void ShowUserGuide();
  116. void ShowStyleEditor(ImGuiStyle* ref = NULL);
  117. void ShowTestWindow(bool* open = NULL);
  118. // Window
  119. bool Begin(const char* name = "Debug", bool* open = NULL, ImVec2 size = ImVec2(0,0), float fill_alpha = -1.0f, ImGuiWindowFlags flags = 0);
  120. void End();
  121. void BeginChild(const char* str_id, ImVec2 size = ImVec2(0,0), bool border = false, ImGuiWindowFlags extra_flags = 0);
  122. void EndChild();
  123. bool GetWindowIsFocused();
  124. float GetWindowWidth();
  125. ImVec2 GetWindowPos(); // you should rarely need/care about the window position, but it can be useful if you want to use your own drawing
  126. void SetWindowPos(const ImVec2& pos); // set current window pos
  127. ImVec2 GetWindowSize();
  128. ImVec2 GetWindowContentRegionMin();
  129. ImVec2 GetWindowContentRegionMax();
  130. ImDrawList* GetWindowDrawList();
  131. void SetFontScale(float scale);
  132. void SetScrollPosHere();
  133. void SetTreeStateStorage(ImGuiStorage* tree);
  134. ImGuiStorage* GetTreeStateStorage();
  135. void PushItemWidth(float item_width);
  136. void PopItemWidth();
  137. float GetItemWidth();
  138. void PushAllowKeyboardFocus(bool v);
  139. void PopAllowKeyboardFocus();
  140. void PushStyleColor(ImGuiCol idx, const ImVec4& col);
  141. void PopStyleColor();
  142. // Tooltip
  143. void SetTooltip(const char* fmt, ...); // set tooltip under mouse-cursor, typically use with ImGui::IsHovered(). last call wins.
  144. void BeginTooltip(); // use to create full-featured tooltip windows that aren't just text.
  145. void EndTooltip();
  146. // Layout
  147. void Separator(); // horizontal line
  148. void SameLine(int column_x = 0, int spacing_w = -1); // call between widgets to layout them horizontally
  149. void Spacing();
  150. void Columns(int count = 1, const char* id = NULL, bool border=true); // setup number of columns
  151. void NextColumn(); // next column
  152. float GetColumnOffset(int column_index = -1);
  153. void SetColumnOffset(int column_index, float offset);
  154. float GetColumnWidth(int column_index = -1);
  155. ImVec2 GetCursorPos(); // cursor position relative to window position
  156. void SetCursorPos(const ImVec2& pos); // "
  157. void SetCursorPosX(float x); // "
  158. void SetCursorPosY(float y); // "
  159. ImVec2 GetCursorScreenPos(); // cursor position in screen space
  160. void AlignFirstTextHeightToWidgets(); // call once if the first item on the line is a Text() item and you want to vertically lower it to match higher widgets.
  161. float GetTextLineSpacing();
  162. float GetTextLineHeight();
  163. // ID scopes
  164. void PushID(const char* str_id);
  165. void PushID(const void* ptr_id);
  166. void PushID(const int int_id);
  167. void PopID();
  168. // Widgets
  169. void Text(const char* fmt, ...);
  170. void TextV(const char* fmt, va_list args);
  171. void TextColored(const ImVec4& col, const char* fmt, ...); // shortcut to doing PushStyleColor(ImGuiCol_Text, col); Text(fmt, ...); PopStyleColor();
  172. void TextUnformatted(const char* text, const char* text_end = NULL); // doesn't require null terminated string if 'text_end' is specified. no copy done to any bounded stack buffer, better for long chunks of text.
  173. void LabelText(const char* label, const char* fmt, ...);
  174. void BulletText(const char* fmt, ...);
  175. bool Button(const char* label, ImVec2 size = ImVec2(0,0), bool repeat_when_held = false);
  176. bool SmallButton(const char* label);
  177. bool CollapsingHeader(const char* label, const char* str_id = NULL, const bool display_frame = true, const bool default_open = false);
  178. bool SliderFloat(const char* label, float* v, float v_min, float v_max, const char* display_format = "%.3f", float power = 1.0f);
  179. bool SliderFloat2(const char* label, float v[2], float v_min, float v_max, const char* display_format = "%.3f", float power = 1.0f);
  180. bool SliderFloat3(const char* label, float v[3], float v_min, float v_max, const char* display_format = "%.3f", float power = 1.0f);
  181. bool SliderFloat4(const char* label, float v[3], float v_min, float v_max, const char* display_format = "%.3f", float power = 1.0f);
  182. bool SliderAngle(const char* label, float* v, float v_degrees_min = -360.0f, float v_degrees_max = +360.0f); // *v in radians
  183. bool SliderInt(const char* label, int* v, int v_min, int v_max, const char* display_format = "%.0f");
  184. void PlotLines(const char* label, const float* values, int values_count, int values_offset = 0, const char* overlay_text = NULL, float scale_min = FLT_MAX, float scale_max = FLT_MAX, ImVec2 graph_size = ImVec2(0,0), size_t stride = sizeof(float));
  185. void PlotHistogram(const char* label, const float* values, int values_count, int values_offset = 0, const char* overlay_text = NULL, float scale_min = FLT_MAX, float scale_max = FLT_MAX, ImVec2 graph_size = ImVec2(0,0), size_t stride = sizeof(float));
  186. bool Checkbox(const char* label, bool* v);
  187. bool CheckboxFlags(const char* label, unsigned int* flags, unsigned int flags_value);
  188. bool RadioButton(const char* label, bool active);
  189. bool RadioButton(const char* label, int* v, int v_button);
  190. bool InputFloat(const char* label, float* v, float step = 0.0f, float step_fast = 0.0f, int decimal_precision = -1);
  191. bool InputFloat2(const char* label, float v[2], int decimal_precision = -1);
  192. bool InputFloat3(const char* label, float v[3], int decimal_precision = -1);
  193. bool InputFloat4(const char* label, float v[4], int decimal_precision = -1);
  194. bool InputInt(const char* label, int* v, int step = 1, int step_fast = 100);
  195. bool InputText(const char* label, char* buf, size_t buf_size, ImGuiInputTextFlags flags = 0);
  196. bool Combo(const char* label, int* current_item, const char** items, int items_count, int popup_height_items = 7);
  197. bool Combo(const char* label, int* current_item, const char* items_separated_by_zeros, int popup_height_items = 7); // Separate items with \0, end item-list with \0\0
  198. bool Combo(const char* label, int* current_item, bool (*items_getter)(void* data, int idx, const char** out_text), void* data, int items_count, int popup_height_items = 7);
  199. bool ColorButton(const ImVec4& col, bool small_height = false, bool outline_border = true);
  200. bool ColorEdit3(const char* label, float col[3]);
  201. bool ColorEdit4(const char* label, float col[4], bool show_alpha = true);
  202. void ColorEditMode(ImGuiColorEditMode mode);
  203. bool TreeNode(const char* str_label_id); // if returning 'true' the user is responsible for calling TreePop
  204. bool TreeNode(const char* str_id, const char* fmt, ...); // "
  205. bool TreeNode(const void* ptr_id, const char* fmt, ...); // "
  206. void TreePush(const char* str_id = NULL); // already called by TreeNode(), but you can call Push/Pop yourself for layout purpose
  207. void TreePush(const void* ptr_id = NULL); // "
  208. void TreePop();
  209. void OpenNextNode(bool open); // force open/close the next TreeNode or CollapsingHeader
  210. // Value helper output "name: value"
  211. // Freely declare your own in the ImGui namespace.
  212. void Value(const char* prefix, bool b);
  213. void Value(const char* prefix, int v);
  214. void Value(const char* prefix, unsigned int v);
  215. void Value(const char* prefix, float v, const char* float_format = NULL);
  216. void Color(const char* prefix, const ImVec4& v);
  217. void Color(const char* prefix, unsigned int v);
  218. // Logging
  219. void LogButtons();
  220. void LogToTTY(int max_depth = -1);
  221. void LogToFile(int max_depth = -1, const char* filename = NULL);
  222. void LogToClipboard(int max_depth = -1);
  223. // Utilities
  224. void SetNewWindowDefaultPos(const ImVec2& pos); // set position of window that do
  225. bool IsHovered(); // was the last item active area hovered by mouse?
  226. ImVec2 GetItemBoxMin(); // get bounding box of last item
  227. ImVec2 GetItemBoxMax(); // get bounding box of last item
  228. bool IsClipped(const ImVec2& item_size); // to perform coarse clipping on user's side (as an optimisation)
  229. bool IsKeyPressed(int key_index, bool repeat = true); // key_index into the keys_down[512] array, imgui doesn't know the semantic of each entry
  230. bool IsMouseClicked(int button, bool repeat = false);
  231. bool IsMouseDoubleClicked(int button);
  232. bool IsMouseHoveringBox(const ImVec2& box_min, const ImVec2& box_max);
  233. ImVec2 GetMousePos();
  234. float GetTime();
  235. int GetFrameCount();
  236. const char* GetStyleColorName(ImGuiCol idx);
  237. void GetDefaultFontData(const void** fnt_data, unsigned int* fnt_size, const void** png_data, unsigned int* png_size);
  238. ImVec2 CalcTextSize(const char* text, const char* text_end = NULL, const bool hide_text_after_hash = true);
  239. }; // namespace ImGui
  240. // Flags for ImGui::Begin()
  241. enum ImGuiWindowFlags_
  242. {
  243. // Default: 0
  244. ImGuiWindowFlags_ShowBorders = 1 << 0,
  245. ImGuiWindowFlags_NoTitleBar = 1 << 1,
  246. ImGuiWindowFlags_NoResize = 1 << 2,
  247. ImGuiWindowFlags_NoMove = 1 << 3,
  248. ImGuiWindowFlags_NoScrollbar = 1 << 4,
  249. ImGuiWindowFlags_ChildWindow = 1 << 5, // For internal use by BeginChild()
  250. ImGuiWindowFlags_ChildWindowAutoFitX = 1 << 6, // For internal use by BeginChild()
  251. ImGuiWindowFlags_ChildWindowAutoFitY = 1 << 7, // For internal use by BeginChild()
  252. ImGuiWindowFlags_ComboBox = 1 << 8, // For internal use by ComboBox()
  253. ImGuiWindowFlags_Tooltip = 1 << 9, // For internal use by Render() when using Tooltip
  254. };
  255. // Flags for ImGui::InputText()
  256. enum ImGuiInputTextFlags_
  257. {
  258. // Default: 0
  259. ImGuiInputTextFlags_CharsDecimal = 1 << 0,
  260. ImGuiInputTextFlags_CharsHexadecimal = 1 << 1,
  261. ImGuiInputTextFlags_AutoSelectAll = 1 << 2,
  262. ImGuiInputTextFlags_AlignCenter = 1 << 3,
  263. };
  264. // User fill ImGuiIO.KeyMap[] array with indices into the ImGuiIO.KeysDown[512] array
  265. enum ImGuiKey_
  266. {
  267. ImGuiKey_Tab,
  268. ImGuiKey_LeftArrow,
  269. ImGuiKey_RightArrow,
  270. ImGuiKey_UpArrow,
  271. ImGuiKey_DownArrow,
  272. ImGuiKey_Home,
  273. ImGuiKey_End,
  274. ImGuiKey_Delete,
  275. ImGuiKey_Backspace,
  276. ImGuiKey_Enter,
  277. ImGuiKey_Escape,
  278. ImGuiKey_A, // for CTRL+A: select all
  279. ImGuiKey_C, // for CTRL+C: copy
  280. ImGuiKey_V, // for CTRL+V: paste
  281. ImGuiKey_X, // for CTRL+X: cut
  282. ImGuiKey_Y, // for CTRL+Y: redo
  283. ImGuiKey_Z, // for CTRL+Z: undo
  284. ImGuiKey_COUNT,
  285. };
  286. enum ImGuiCol_
  287. {
  288. ImGuiCol_Text,
  289. ImGuiCol_WindowBg,
  290. ImGuiCol_Border,
  291. ImGuiCol_BorderShadow,
  292. ImGuiCol_FrameBg, // Background of checkbox, radio button, plot, slider, text input
  293. ImGuiCol_TitleBg,
  294. ImGuiCol_TitleBgCollapsed,
  295. ImGuiCol_ScrollbarBg,
  296. ImGuiCol_ScrollbarGrab,
  297. ImGuiCol_ScrollbarGrabHovered,
  298. ImGuiCol_ScrollbarGrabActive,
  299. ImGuiCol_ComboBg,
  300. ImGuiCol_CheckHovered,
  301. ImGuiCol_CheckActive,
  302. ImGuiCol_SliderGrab,
  303. ImGuiCol_SliderGrabActive,
  304. ImGuiCol_Button,
  305. ImGuiCol_ButtonHovered,
  306. ImGuiCol_ButtonActive,
  307. ImGuiCol_Header,
  308. ImGuiCol_HeaderHovered,
  309. ImGuiCol_HeaderActive,
  310. ImGuiCol_Column,
  311. ImGuiCol_ColumnHovered,
  312. ImGuiCol_ColumnActive,
  313. ImGuiCol_ResizeGrip,
  314. ImGuiCol_ResizeGripHovered,
  315. ImGuiCol_ResizeGripActive,
  316. ImGuiCol_CloseButton,
  317. ImGuiCol_CloseButtonHovered,
  318. ImGuiCol_CloseButtonActive,
  319. ImGuiCol_PlotLines,
  320. ImGuiCol_PlotLinesHovered,
  321. ImGuiCol_PlotHistogram,
  322. ImGuiCol_PlotHistogramHovered,
  323. ImGuiCol_TextSelectedBg,
  324. ImGuiCol_TooltipBg,
  325. ImGuiCol_COUNT,
  326. };
  327. enum ImGuiColorEditMode_
  328. {
  329. ImGuiColorEditMode_UserSelect = -1,
  330. ImGuiColorEditMode_RGB = 0,
  331. ImGuiColorEditMode_HSV = 1,
  332. ImGuiColorEditMode_HEX = 2,
  333. };
  334. struct ImGuiStyle
  335. {
  336. float Alpha; // Global alpha applies to everything in ImGui
  337. ImVec2 WindowPadding; // Padding within a window
  338. ImVec2 WindowMinSize; // Minimum window size
  339. ImVec2 FramePadding; // Padding within a framed rectangle (used by most widgets)
  340. ImVec2 ItemSpacing; // Horizontal and vertical spacing between widgets/lines
  341. ImVec2 ItemInnerSpacing; // Horizontal and vertical spacing between within elements of a composed widget (e.g. a slider and its label)
  342. ImVec2 TouchExtraPadding; // Expand bounding box for touch-based system where touch position is not accurate enough (unnecessary for mouse inputs). Unfortunately we don't sort widgets so priority on overlap will always be given to the first widget running. So dont grow this too much!
  343. ImVec2 AutoFitPadding; // Extra space after auto-fit (double-clicking on resize grip)
  344. float WindowFillAlphaDefault; // Default alpha of window background, if not specified in ImGui::Begin()
  345. float WindowRounding; // Radius of window corners rounding. Set to 0.0f to have rectangular windows
  346. float TreeNodeSpacing; // Horizontal spacing when entering a tree node
  347. float ColumnsMinSpacing; // Minimum horizontal spacing between two columns
  348. float ScrollBarWidth; // Width of the vertical scroll bar
  349. ImVec4 Colors[ImGuiCol_COUNT];
  350. ImGuiStyle();
  351. };
  352. // This is where your app communicate with ImGui. Call ImGui::GetIO() to access.
  353. // Read 'Programmer guide' section in .cpp file for general usage.
  354. struct ImGuiIO
  355. {
  356. // Settings (fill once) // Default value:
  357. ImVec2 DisplaySize; // <unset> // Display size, in pixels. For clamping windows positions.
  358. float DeltaTime; // = 1.0f/60.0f // Time elapsed since last frame, in seconds.
  359. float IniSavingRate; // = 5.0f // Maximum time between saving .ini file, in seconds. Set to a negative value to disable .ini saving.
  360. const char* IniFilename; // = "imgui.ini" // Absolute path to .ini file.
  361. const char* LogFilename; // = "imgui_log.txt" // Absolute path to .log file.
  362. float MouseDoubleClickTime; // = 0.30f // Time for a double-click, in seconds.
  363. float MouseDoubleClickMaxDist; // = 6.0f // Distance threshold to stay in to validate a double-click, in pixels.
  364. int KeyMap[ImGuiKey_COUNT]; // <unset> // Map of indices into the KeysDown[512] entries array
  365. ImFont Font; // <auto> // Gets passed to text functions. Typedef ImFont to the type you want (ImBitmapFont* or your own font).
  366. float FontYOffset; // = 0.0f // Offset font rendering by xx pixels in Y axis.
  367. ImVec2 FontTexUvForWhite; // = (0.0f,0.0f) // Font texture must have a white pixel at this UV coordinate. Adjust if you are using custom texture.
  368. bool FontAllowScaling; // = false // Set to allow scaling text with CTRL+Wheel.
  369. float PixelCenterOffset; // = 0.0f // Try to set to 0.5f or 0.375f if rendering is blurry
  370. // Settings - Rendering function (REQUIRED)
  371. // See example code if you are unsure of how to implement this.
  372. void (*RenderDrawListsFn)(ImDrawList** const draw_lists, int count);
  373. // Settings - Clipboard Support
  374. // Override to provide your clipboard handlers.
  375. // On Windows architecture, defaults to use the native Win32 clipboard, otherwise default to use a ImGui private clipboard.
  376. // NB- for SetClipboardTextFn, the string is *NOT* zero-terminated at 'text_end'
  377. const char* (*GetClipboardTextFn)();
  378. void (*SetClipboardTextFn)(const char* text, const char* text_end);
  379. // Input - Fill before calling NewFrame()
  380. ImVec2 MousePos; // Mouse position, in pixels (set to -1,-1 if no mouse / on another screen, etc.)
  381. bool MouseDown[5]; // Mouse buttons. ImGui itself only uses button 0 (left button) but you can use others as storage for convenience.
  382. int MouseWheel; // Mouse wheel: -1,0,+1
  383. bool KeyCtrl; // Keyboard modifier pressed: Control
  384. bool KeyShift; // Keyboard modifier pressed: Shift
  385. bool KeysDown[512]; // Keyboard keys that are pressed (in whatever order user naturally has access to keyboard data)
  386. char InputCharacters[16]; // List of characters input (translated by user from keypress+keyboard state). Fill using AddInputCharacter() helper.
  387. // Output - Retrieve after calling NewFrame(), you can use them to discard inputs or hide them from the rest of your application
  388. bool WantCaptureMouse; // ImGui is using your mouse input (= window is being hovered or widget is active).
  389. bool WantCaptureKeyboard; // imGui is using your keyboard input (= widget is active).
  390. // Function
  391. void AddInputCharacter(char c); // Helper to add a new character into InputCharacters[]
  392. // [Internal] ImGui will maintain those fields for you
  393. ImVec2 MousePosPrev;
  394. ImVec2 MouseDelta;
  395. bool MouseClicked[5];
  396. ImVec2 MouseClickedPos[5];
  397. float MouseClickedTime[5];
  398. bool MouseDoubleClicked[5];
  399. float MouseDownTime[5];
  400. float KeysDownTime[512];
  401. ImGuiIO();
  402. };
  403. //-----------------------------------------------------------------------------
  404. // Helpers
  405. //-----------------------------------------------------------------------------
  406. // Helper: execute a block of code once a frame only
  407. // Usage: if (IMGUI_ONCE_UPON_A_FRAME) {/*do something once a frame*/)
  408. #define IMGUI_ONCE_UPON_A_FRAME static ImGuiOncePerFrame im = ImGuiOncePerFrame()
  409. struct ImGuiOncePerFrame
  410. {
  411. ImGuiOncePerFrame() : LastFrame(-1) {}
  412. operator bool() const { return TryIsNewFrame(); }
  413. private:
  414. mutable int LastFrame;
  415. bool TryIsNewFrame() const { const int current_frame = ImGui::GetFrameCount(); if (LastFrame == current_frame) return false; LastFrame = current_frame; return true; }
  416. };
  417. // Helper: Parse and apply text filters. In format "aaaaa[,bbbb][,ccccc]"
  418. struct ImGuiTextFilter
  419. {
  420. struct TextRange
  421. {
  422. const char* b;
  423. const char* e;
  424. TextRange() { b = e = NULL; }
  425. TextRange(const char* _b, const char* _e) { b = _b; e = _e; }
  426. const char* begin() const { return b; }
  427. const char* end() const { return e; }
  428. bool empty() const { return b == e; }
  429. char front() const { return *b; }
  430. static bool isblank(char c) { return c == ' ' || c == '\t'; }
  431. void trim_blanks() { while (b < e && isblank(*b)) b++; while (e > b && isblank(*(e-1))) e--; }
  432. void split(char separator, ImVector<TextRange>& out);
  433. };
  434. char InputBuf[256];
  435. ImVector<TextRange> Filters;
  436. int CountGrep;
  437. ImGuiTextFilter();
  438. void Clear() { InputBuf[0] = 0; Build(); }
  439. void Draw(const char* label = "Filter (inc,-exc)", float width = -1.0f); // Helper calling InputText+Build
  440. bool PassFilter(const char* val) const;
  441. bool IsActive() const { return !Filters.empty(); }
  442. void Build();
  443. };
  444. // Helper: Text buffer for logging/accumulating text
  445. struct ImGuiTextBuffer
  446. {
  447. ImVector<char> Buf;
  448. ImGuiTextBuffer() { Buf.push_back(0); }
  449. const char* begin() const { return Buf.begin(); }
  450. const char* end() const { return Buf.end()-1; }
  451. size_t size() const { return Buf.size()-1; }
  452. bool empty() { return Buf.empty(); }
  453. void clear() { Buf.clear(); Buf.push_back(0); }
  454. void append(const char* fmt, ...);
  455. };
  456. // Helper: Key->value storage
  457. // - Store collapse state for a tree
  458. // - Store color edit options, etc.
  459. // Typically you don't have to worry about this since a storage is held within each Window.
  460. // Declare your own storage if you want to manipulate the open/close state of a particular sub-tree in your interface.
  461. struct ImGuiStorage
  462. {
  463. struct Pair { ImU32 key; int val; };
  464. ImVector<Pair> Data;
  465. void Clear();
  466. int GetInt(ImU32 key, int default_val = 0);
  467. void SetInt(ImU32 key, int val);
  468. void SetAllInt(int val);
  469. int* Find(ImU32 key);
  470. void Insert(ImU32 key, int val);
  471. };
  472. //-----------------------------------------------------------------------------
  473. // Draw List
  474. // Hold a series of drawing commands. The user provide a renderer for ImDrawList
  475. //-----------------------------------------------------------------------------
  476. struct ImDrawCmd
  477. {
  478. unsigned int vtx_count;
  479. ImVec4 clip_rect;
  480. };
  481. // sizeof() == 20
  482. struct ImDrawVert
  483. {
  484. ImVec2 pos;
  485. ImVec2 uv;
  486. ImU32 col;
  487. };
  488. // Draw command list
  489. // User is responsible for providing a renderer for this in ImGuiIO::RenderDrawListFn
  490. struct ImDrawList
  491. {
  492. // This is what you have to render
  493. ImVector<ImDrawCmd> commands; // commands
  494. ImVector<ImDrawVert> vtx_buffer; // each command consume ImDrawCmd::vtx_count of those
  495. // [Internal to ImGui]
  496. ImVector<ImVec4> clip_rect_stack; // [internal] clip rect stack while building the command-list (so text command can perform clipping early on)
  497. ImDrawVert* vtx_write; // [internal] point within vtx_buffer after each add command (to avoid using the ImVector<> operators too much)
  498. ImDrawList() { Clear(); }
  499. void Clear();
  500. void PushClipRect(const ImVec4& clip_rect);
  501. void PopClipRect();
  502. void ReserveVertices(unsigned int vtx_count);
  503. void AddVtx(const ImVec2& pos, ImU32 col);
  504. void AddVtxLine(const ImVec2& a, const ImVec2& b, ImU32 col);
  505. // Primitives
  506. void AddLine(const ImVec2& a, const ImVec2& b, ImU32 col);
  507. void AddRect(const ImVec2& a, const ImVec2& b, ImU32 col, float rounding = 0.0f, int rounding_corners=0x0F);
  508. void AddRectFilled(const ImVec2& a, const ImVec2& b, ImU32 col, float rounding = 0.0f, int rounding_corners=0x0F);
  509. void AddTriangleFilled(const ImVec2& a, const ImVec2& b, const ImVec2& c, ImU32 col);
  510. void AddCircle(const ImVec2& centre, float radius, ImU32 col, int num_segments = 12);
  511. void AddCircleFilled(const ImVec2& centre, float radius, ImU32 col, int num_segments = 12);
  512. void AddArc(const ImVec2& center, float rad, ImU32 col, int a_min, int a_max, bool tris=false, const ImVec2& third_point_offset = ImVec2(0,0));
  513. void AddText(ImFont font, float font_size, const ImVec2& pos, ImU32 col, const char* text_begin, const char* text_end);
  514. };
  515. // Optional bitmap font data loader & renderer into vertices
  516. // #define ImFont to ImBitmapFont to use
  517. // Using the .fnt format exported by BMFont
  518. // - tool: http://www.angelcode.com/products/bmfont
  519. // - file-format: http://www.angelcode.com/products/bmfont/doc/file_format.html
  520. // Assume valid file data (won't handle invalid/malicious data)
  521. // Handle a subset of parameters.
  522. // - kerning pair are not supported (because ImGui code does per-character CalcTextSize calls, need to turn it into something more stateful to allow kerning)
  523. struct ImBitmapFont
  524. {
  525. #pragma pack(push, 1)
  526. struct FntInfo
  527. {
  528. signed short FontSize;
  529. unsigned char BitField; // bit 0: smooth, bit 1: unicode, bit 2: italic, bit 3: bold, bit 4: fixedHeight, bits 5-7: reserved
  530. unsigned char CharSet;
  531. unsigned short StretchH;
  532. unsigned char AA;
  533. unsigned char PaddingUp, PaddingRight, PaddingDown, PaddingLeft;
  534. unsigned char SpacingHoriz, SpacingVert;
  535. unsigned char Outline;
  536. //char FontName[];
  537. };
  538. struct FntCommon
  539. {
  540. unsigned short LineHeight;
  541. unsigned short Base;
  542. unsigned short ScaleW, ScaleH;
  543. unsigned short Pages;
  544. unsigned char BitField;
  545. unsigned char Channels[4];
  546. };
  547. struct FntGlyph
  548. {
  549. unsigned int Id;
  550. unsigned short X, Y;
  551. unsigned short Width, Height;
  552. signed short XOffset, YOffset;
  553. signed short XAdvance;
  554. unsigned char Page;
  555. unsigned char Channel;
  556. };
  557. struct FntKerning
  558. {
  559. unsigned int IdFirst;
  560. unsigned int IdSecond;
  561. signed short Amount;
  562. };
  563. #pragma pack(pop)
  564. unsigned char* Data; // Raw data, content of .fnt file
  565. size_t DataSize; //
  566. bool DataOwned; //
  567. const FntInfo* Info; // (point into raw data)
  568. const FntCommon* Common; // (point into raw data)
  569. const FntGlyph* Glyphs; // (point into raw data)
  570. size_t GlyphsCount; //
  571. const FntKerning* Kerning; // (point into raw data)
  572. size_t KerningCount; //
  573. int TabCount; // FIXME: mishandled (add fixed amount instead of aligning to column)
  574. ImVector<const char*> Filenames; // (point into raw data)
  575. ImVector<int> IndexLookup; // (built)
  576. ImBitmapFont();
  577. ~ImBitmapFont() { Clear(); }
  578. bool LoadFromMemory(const void* data, size_t data_size);
  579. bool LoadFromFile(const char* filename);
  580. void Clear();
  581. void BuildLookupTable();
  582. const FntGlyph * FindGlyph(unsigned short c) const;
  583. float GetFontSize() const { return (float)Info->FontSize; }
  584. bool IsLoaded() const { return Info != NULL && Common != NULL && Glyphs != NULL; }
  585. ImVec2 CalcTextSize(float size, float max_width, const char* text_begin, const char* text_end, const char** remaining = NULL) const;
  586. void RenderText(float size, ImVec2 pos, ImU32 col, const ImVec4& clip_rect, const char* text_begin, const char* text_end, ImDrawVert*& out_vertices) const;
  587. };