imgui.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /*
  2. * Copyright 2011-2015 Branimir Karadzic. All rights reserved.
  3. * License: http://www.opensource.org/licenses/BSD-2-Clause
  4. */
  5. // This code is based on:
  6. //
  7. // Copyright (c) 2009-2010 Mikko Mononen [email protected]
  8. //
  9. // This software is provided 'as-is', without any express or implied
  10. // warranty. In no event will the authors be held liable for any damages
  11. // arising from the use of this software.
  12. // Permission is granted to anyone to use this software for any purpose,
  13. // including commercial applications, and to alter it and redistribute it
  14. // freely, subject to the following restrictions:
  15. // 1. The origin of this software must not be misrepresented; you must not
  16. // claim that you wrote the original software. If you use this software
  17. // in a product, an acknowledgment in the product documentation would be
  18. // appreciated but is not required.
  19. // 2. Altered source versions must be plainly marked as such, and must not be
  20. // misrepresented as being the original software.
  21. // 3. This notice may not be removed or altered from any source distribution.
  22. //
  23. // Source altered and distributed from https://github.com/AdrienHerubel/imgui
  24. #ifndef IMGUI_H_HEADER_GUARD
  25. #define IMGUI_H_HEADER_GUARD
  26. #include <bgfx.h>
  27. #include <ocornut-imgui/imgui.h>
  28. #define IMGUI_MBUT_LEFT 0x01
  29. #define IMGUI_MBUT_RIGHT 0x02
  30. /// For custom values, define these macros before including imgui.h
  31. #ifndef IMGUI_SCROLL_AREA_R
  32. # define IMGUI_SCROLL_AREA_R 6
  33. #endif //IMGUI_SCROLL_AREA_R
  34. #ifndef IMGUI_SCROLL_BAR_R
  35. # define IMGUI_SCROLL_BAR_R 5
  36. #endif //IMGUI_SCROLL_BAR_R
  37. #ifndef IMGUI_BUTTON_R
  38. # define IMGUI_BUTTON_R 9
  39. #endif //IMGUI_BUTTON_R
  40. #ifndef IMGUI_BUTTON_RGB0
  41. # define IMGUI_BUTTON_RGB0 imguiRGBA(128, 128, 128, 0)
  42. #endif //IMGUI_BUTTON_RGB0
  43. #ifndef IMGUI_INPUT_R
  44. # define IMGUI_INPUT_R 4
  45. #endif //IMGUI_INPUT_R
  46. #ifndef IMGUI_TABS_HEIGHT
  47. # define IMGUI_TABS_HEIGHT 20
  48. #endif //IMGUI_TABS_HEIGHT
  49. #ifndef IMGUI_TABS_R
  50. # define IMGUI_TABS_R 9
  51. #endif //IMGUI_TABS_R
  52. #ifndef IMGUI_INDENT_VALUE
  53. # define IMGUI_INDENT_VALUE 16
  54. #endif //IMGUI_INDENT_VALUE
  55. #ifndef IMGUI_SEPARATOR_VALUE
  56. # define IMGUI_SEPARATOR_VALUE 12
  57. #endif //IMGUI_SEPARATOR_VALUE
  58. struct ImguiTextAlign
  59. {
  60. enum Enum
  61. {
  62. Left,
  63. Center,
  64. Right,
  65. Count
  66. };
  67. };
  68. struct ImguiAlign
  69. {
  70. enum Enum
  71. {
  72. Left,
  73. LeftIndented,
  74. Center,
  75. CenterIndented,
  76. Right,
  77. };
  78. };
  79. struct ImguiCubemap
  80. {
  81. enum Enum
  82. {
  83. Cross,
  84. Latlong,
  85. Hex,
  86. Count,
  87. };
  88. };
  89. struct ImguiBorder
  90. {
  91. enum Enum
  92. {
  93. Left,
  94. Right,
  95. Top,
  96. Bottom
  97. };
  98. };
  99. inline uint32_t imguiRGBA(uint8_t _r, uint8_t _g, uint8_t _b, uint8_t _a = 255)
  100. {
  101. return 0
  102. | (uint32_t(_r) << 0)
  103. | (uint32_t(_g) << 8)
  104. | (uint32_t(_b) << 16)
  105. | (uint32_t(_a) << 24)
  106. ;
  107. }
  108. BGFX_HANDLE(ImguiFontHandle);
  109. ImguiFontHandle imguiCreateFont(const void* _data, float _fontSize=15.0f);
  110. void imguiSetFont(ImguiFontHandle _handle);
  111. ImguiFontHandle imguiGetCurrentFont();
  112. namespace bx { struct AllocatorI; }
  113. ImguiFontHandle imguiCreate(const void* _data = NULL, uint32_t _size = 0, float _fontSize = 15.0f, bx::AllocatorI* _allocator = NULL);
  114. void imguiDestroy();
  115. void imguiBeginFrame(int32_t _mx, int32_t _my, uint8_t _button, int32_t _scroll, uint16_t _width, uint16_t _height, char _inputChar = 0, uint8_t _view = 255);
  116. void imguiBeginFrame(int32_t _mx, int32_t _my, uint8_t _button, int32_t _scroll, uint16_t _width, uint16_t _height, uint16_t _surfaceWidth, uint16_t _surfaceHeight, char _inputChar = 0, uint8_t _view = 255);
  117. void imguiEndFrame();
  118. void imguiDrawText(int _x, int _y, ImguiTextAlign::Enum _align, const char* _text, uint32_t _argb);
  119. void imguiDrawLine(float _x0, float _y0, float _x1, float _y1, float _r, uint32_t _argb);
  120. void imguiDrawRoundedRect(float _x, float _y, float _w, float _h, float _r, uint32_t _argb);
  121. void imguiDrawRect(float _x, float _y, float _w, float _h, uint32_t _argb);
  122. /// Notice: this function is not to be called between imguiBeginArea() and imguiEndArea().
  123. bool imguiBorderButton(ImguiBorder::Enum _border, bool _checked, bool _enabled = true);
  124. bool imguiBeginArea(const char* _name, int _x, int _y, int _width, int _height, bool _enabled = true, int32_t _r = IMGUI_SCROLL_AREA_R);
  125. void imguiEndArea();
  126. bool imguiBeginScroll(int32_t _height, int32_t* _scroll, bool _enabled = true);
  127. void imguiEndScroll(int32_t _r = IMGUI_SCROLL_BAR_R);
  128. bool imguiBeginScrollArea(const char* _name, int _x, int _y, int _width, int _height, int* _scroll, bool _enabled = true, int32_t _r = IMGUI_SCROLL_AREA_R);
  129. void imguiEndScrollArea(int32_t _r = IMGUI_SCROLL_BAR_R);
  130. void imguiIndent(uint16_t _width = IMGUI_INDENT_VALUE);
  131. void imguiUnindent(uint16_t _width = IMGUI_INDENT_VALUE);
  132. void imguiSeparator(uint16_t _height = IMGUI_SEPARATOR_VALUE);
  133. void imguiSeparatorLine(uint16_t _height = IMGUI_SEPARATOR_VALUE, ImguiAlign::Enum = ImguiAlign::LeftIndented);
  134. int32_t imguiGetWidgetX();
  135. int32_t imguiGetWidgetY();
  136. int32_t imguiGetWidgetW();
  137. void imguiSetCurrentScissor(); // Call before drawing custom widgets over imgui area.
  138. bool imguiButton(const char* _text, bool _enabled = true, ImguiAlign::Enum _align = ImguiAlign::LeftIndented, uint32_t _rgb0 = IMGUI_BUTTON_RGB0, int32_t _r = IMGUI_BUTTON_R);
  139. bool imguiItem(const char* _text, bool _enabled = true);
  140. bool imguiCheck(const char* _text, bool _checked, bool _enabled = true);
  141. void imguiBool(const char* _text, bool& _flag, bool _enabled = true);
  142. bool imguiCollapse(const char* _text, const char* _subtext, bool _checked, bool _enabled = true);
  143. void imguiLabel(const char* _format, ...);
  144. void imguiLabel(uint32_t _rgba, const char* _format, ...);
  145. void imguiValue(const char* _text);
  146. bool imguiSlider(const char* _text, float& _val, float _vmin, float _vmax, float _vinc, bool _enabled = true, ImguiAlign::Enum _align = ImguiAlign::LeftIndented);
  147. bool imguiSlider(const char* _text, int32_t& _val, int32_t _vmin, int32_t _vmax, bool _enabled = true, ImguiAlign::Enum _align = ImguiAlign::LeftIndented);
  148. void imguiInput(const char* _label, char* _str, uint32_t _len, bool _enabled = true, ImguiAlign::Enum _align = ImguiAlign::LeftIndented, int32_t _r = IMGUI_INPUT_R);
  149. /// Usage example:
  150. /// imguiTabs(0, true, ImguiAlign::CenterIndented, 20, 0, 3, 2, "Tab0", "Tab1", "Tab2", true, false);
  151. /// _nTabs - Number of tabs (in the above example, 3, and their labes are 'Tab0', 'Tab1' and 'Tab2'.
  152. /// _nEnabled - Number of specified 'enabled' flags. All other unspecified tabs are considered enabled by default.
  153. /// In the above example, there are 2 enabled flags: 'Tab0' is specified as enabled and 'Tab1' is specified as disabled.
  154. /// Tab2 is unspecified and therefore is treated as enabled.
  155. uint8_t imguiTabs(uint8_t _selected, bool _enabled, ImguiAlign::Enum _align, int32_t _height, int32_t _r, uint8_t _nTabs, uint8_t _nEnabled, ...);
  156. uint8_t imguiTabs(uint8_t _selected, bool _enabled, ImguiAlign::Enum _align, int32_t _height, int32_t _r, uint8_t _nTabs, ...);
  157. uint32_t imguiChooseUseMacroInstead(uint32_t _selected, ...);
  158. #define imguiChoose(...) imguiChooseUseMacroInstead(__VA_ARGS__, NULL)
  159. void imguiColorWheel(float _rgb[3], bool _respectIndentation = false, float _size = 0.8f, bool _enabled = true);
  160. void imguiColorWheel(const char* _str, float _rgb[3], bool& _activated, float _size = 0.8f, bool _enabled = true);
  161. bool imguiImage(bgfx::TextureHandle _image, float _lod, int32_t _width, int32_t _height, ImguiAlign::Enum _align = ImguiAlign::LeftIndented, bool _enabled = true, bool _originBottomLeft = false);
  162. bool imguiImage(bgfx::TextureHandle _image, float _lod, float _scale, float _aspect, ImguiAlign::Enum _align = ImguiAlign::LeftIndented, bool _enabled = true, bool _originBottomLeft = false);
  163. bool imguiImageChannel(bgfx::TextureHandle _image, uint8_t _channel, float _lod, int32_t _width, int32_t _height, ImguiAlign::Enum _align = ImguiAlign::LeftIndented, bool _enabled = true);
  164. bool imguiImageChannel(bgfx::TextureHandle _image, uint8_t _channel, float _lod, float _scale, float _aspect, ImguiAlign::Enum _align = ImguiAlign::LeftIndented, bool _enabled = true);
  165. bool imguiCube(bgfx::TextureHandle _cubemap, float _lod = 0.0f, ImguiCubemap::Enum _display = ImguiCubemap::Cross, bool _sameHeight = false, ImguiAlign::Enum _align = ImguiAlign::LeftIndented, bool _enabled = true);
  166. float imguiGetTextLength(const char* _text, ImguiFontHandle _handle);
  167. bool imguiMouseOverArea();
  168. namespace ImGui
  169. {
  170. // Helper function for passing bgfx::TextureHandle to ImGui::Image.
  171. inline void Image(bgfx::TextureHandle _handle, const ImVec2& _size, const ImVec2& _uv0 = ImVec2(0, 0), const ImVec2& _uv1 = ImVec2(1, 1), const ImVec4& _tint_col = ImVec4(1, 1, 1, 1), const ImVec4& _border_col = ImVec4(0, 0, 0, 0) )
  172. {
  173. union { bgfx::TextureHandle handle; ImTextureID ptr; } texture;
  174. texture.handle = _handle;
  175. Image(texture.ptr, _size, _uv0, _uv1, _tint_col, _border_col);
  176. }
  177. } // namespace ImGui
  178. #endif // IMGUI_H_HEADER_GUARD