imgui.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /*
  2. * Copyright 2011-2016 Branimir Karadzic. All rights reserved.
  3. * License: https://github.com/bkaradzic/bgfx#license-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/bgfx.h>
  27. #include <ocornut-imgui/imgui.h>
  28. #include <ocornut-imgui/imgui_wm.h>
  29. #include <iconfontheaders/icons_kenney.h>
  30. #include <iconfontheaders/icons_font_awesome.h>
  31. #define IMGUI_MBUT_LEFT 0x01
  32. #define IMGUI_MBUT_RIGHT 0x02
  33. #define IMGUI_MBUT_MIDDLE 0x04
  34. /// For custom values, define these macros before including imgui.h
  35. #ifndef IMGUI_SCROLL_AREA_R
  36. # define IMGUI_SCROLL_AREA_R 6
  37. #endif //IMGUI_SCROLL_AREA_R
  38. #ifndef IMGUI_SCROLL_BAR_R
  39. # define IMGUI_SCROLL_BAR_R 5
  40. #endif //IMGUI_SCROLL_BAR_R
  41. #ifndef IMGUI_BUTTON_R
  42. # define IMGUI_BUTTON_R 9
  43. #endif //IMGUI_BUTTON_R
  44. #ifndef IMGUI_BUTTON_RGB0
  45. # define IMGUI_BUTTON_RGB0 imguiRGBA(128, 128, 128, 0)
  46. #endif //IMGUI_BUTTON_RGB0
  47. #ifndef IMGUI_INPUT_R
  48. # define IMGUI_INPUT_R 4
  49. #endif //IMGUI_INPUT_R
  50. #ifndef IMGUI_TABS_HEIGHT
  51. # define IMGUI_TABS_HEIGHT 20
  52. #endif //IMGUI_TABS_HEIGHT
  53. #ifndef IMGUI_TABS_R
  54. # define IMGUI_TABS_R 9
  55. #endif //IMGUI_TABS_R
  56. #ifndef IMGUI_INDENT_VALUE
  57. # define IMGUI_INDENT_VALUE 16
  58. #endif //IMGUI_INDENT_VALUE
  59. #ifndef IMGUI_SEPARATOR_VALUE
  60. # define IMGUI_SEPARATOR_VALUE 12
  61. #endif //IMGUI_SEPARATOR_VALUE
  62. struct ImguiTextAlign
  63. {
  64. enum Enum
  65. {
  66. Left,
  67. Center,
  68. Right,
  69. Count
  70. };
  71. };
  72. struct ImguiAlign
  73. {
  74. enum Enum
  75. {
  76. Left,
  77. LeftIndented,
  78. Center,
  79. CenterIndented,
  80. Right,
  81. };
  82. };
  83. struct ImguiCubemap
  84. {
  85. enum Enum
  86. {
  87. Cross,
  88. Latlong,
  89. Hex,
  90. Count,
  91. };
  92. };
  93. struct ImguiBorder
  94. {
  95. enum Enum
  96. {
  97. Left,
  98. Right,
  99. Top,
  100. Bottom
  101. };
  102. };
  103. inline uint32_t imguiRGBA(uint8_t _r, uint8_t _g, uint8_t _b, uint8_t _a = 255)
  104. {
  105. return 0
  106. | (uint32_t(_r) << 0)
  107. | (uint32_t(_g) << 8)
  108. | (uint32_t(_b) << 16)
  109. | (uint32_t(_a) << 24)
  110. ;
  111. }
  112. BGFX_HANDLE(ImguiFontHandle);
  113. ImguiFontHandle imguiCreateFont(const void* _data, float _fontSize = 18.0f);
  114. void imguiSetFont(ImguiFontHandle _handle);
  115. ImguiFontHandle imguiGetCurrentFont();
  116. namespace bx { struct AllocatorI; }
  117. ImguiFontHandle imguiCreate(const void* _data = NULL, uint32_t _size = 0, float _fontSize = 18.0f, bx::AllocatorI* _allocator = NULL);
  118. void imguiDestroy();
  119. 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);
  120. 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);
  121. void imguiEndFrame();
  122. void imguiDrawText(int _x, int _y, ImguiTextAlign::Enum _align, const char* _text, uint32_t _argb);
  123. void imguiDrawLine(float _x0, float _y0, float _x1, float _y1, float _r, uint32_t _argb);
  124. void imguiDrawRoundedRect(float _x, float _y, float _w, float _h, float _r, uint32_t _argb);
  125. void imguiDrawRect(float _x, float _y, float _w, float _h, uint32_t _argb);
  126. /// Notice: this function is not to be called between imguiBeginArea() and imguiEndArea().
  127. bool imguiBorderButton(ImguiBorder::Enum _border, bool _checked, bool _enabled = true);
  128. bool imguiBeginArea(const char* _name, int _x, int _y, int _width, int _height, bool _enabled = true, int32_t _r = IMGUI_SCROLL_AREA_R);
  129. void imguiEndArea();
  130. bool imguiBeginScroll(int32_t _height, int32_t* _scroll, bool _enabled = true);
  131. void imguiEndScroll(int32_t _r = IMGUI_SCROLL_BAR_R);
  132. 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);
  133. void imguiEndScrollArea(int32_t _r = IMGUI_SCROLL_BAR_R);
  134. void imguiIndent(uint16_t _width = IMGUI_INDENT_VALUE);
  135. void imguiUnindent(uint16_t _width = IMGUI_INDENT_VALUE);
  136. void imguiSeparator(uint16_t _height = IMGUI_SEPARATOR_VALUE);
  137. void imguiSeparatorLine(uint16_t _height = IMGUI_SEPARATOR_VALUE, ImguiAlign::Enum = ImguiAlign::LeftIndented);
  138. int32_t imguiGetWidgetX();
  139. int32_t imguiGetWidgetY();
  140. int32_t imguiGetWidgetW();
  141. void imguiSetCurrentScissor(); // Call before drawing custom widgets over imgui area.
  142. 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);
  143. bool imguiItem(const char* _text, bool _enabled = true);
  144. bool imguiCheck(const char* _text, bool _checked, bool _enabled = true);
  145. bool imguiBool(const char* _text, bool& _flag, bool _enabled = true);
  146. bool imguiCollapse(const char* _text, const char* _subtext, bool _checked, bool _enabled = true);
  147. void imguiLabel(const char* _format, ...);
  148. void imguiLabel(uint32_t _rgba, const char* _format, ...);
  149. void imguiValue(const char* _text);
  150. bool imguiSlider(const char* _text, float& _val, float _vmin, float _vmax, float _vinc, bool _enabled = true, ImguiAlign::Enum _align = ImguiAlign::LeftIndented);
  151. bool imguiSlider(const char* _text, int32_t& _val, int32_t _vmin, int32_t _vmax, bool _enabled = true, ImguiAlign::Enum _align = ImguiAlign::LeftIndented);
  152. void imguiInput(const char* _label, char* _str, uint32_t _len, bool _enabled = true, ImguiAlign::Enum _align = ImguiAlign::LeftIndented, int32_t _r = IMGUI_INPUT_R);
  153. /// Usage example:
  154. /// imguiTabs(0, true, ImguiAlign::CenterIndented, 20, 0, 3, 2, "Tab0", "Tab1", "Tab2", true, false);
  155. /// _nTabs - Number of tabs (in the above example, 3, and their labes are 'Tab0', 'Tab1' and 'Tab2'.
  156. /// _nEnabled - Number of specified 'enabled' flags. All other unspecified tabs are considered enabled by default.
  157. /// In the above example, there are 2 enabled flags: 'Tab0' is specified as enabled and 'Tab1' is specified as disabled.
  158. /// Tab2 is unspecified and therefore is treated as enabled.
  159. uint8_t imguiTabs(uint8_t _selected, bool _enabled, ImguiAlign::Enum _align, int32_t _height, int32_t _r, uint8_t _nTabs, uint8_t _nEnabled, ...);
  160. uint8_t imguiTabs(uint8_t _selected, bool _enabled, ImguiAlign::Enum _align, int32_t _height, int32_t _r, uint8_t _nTabs, ...);
  161. uint32_t imguiChooseUseMacroInstead(uint32_t _selected, ...);
  162. #define imguiChoose(...) imguiChooseUseMacroInstead(__VA_ARGS__, NULL)
  163. void imguiColorWheel(float _rgb[3], bool _respectIndentation = false, float _size = 0.8f, bool _enabled = true);
  164. void imguiColorWheel(const char* _str, float _rgb[3], bool& _activated, float _size = 0.8f, bool _enabled = true);
  165. bool imguiImage(bgfx::TextureHandle _image, float _lod, int32_t _width, int32_t _height, ImguiAlign::Enum _align = ImguiAlign::LeftIndented, bool _enabled = true, bool _originBottomLeft = false);
  166. bool imguiImage(bgfx::TextureHandle _image, float _lod, float _scale, float _aspect, ImguiAlign::Enum _align = ImguiAlign::LeftIndented, bool _enabled = true, bool _originBottomLeft = false);
  167. bool imguiImageChannel(bgfx::TextureHandle _image, uint8_t _channel, float _lod, int32_t _width, int32_t _height, ImguiAlign::Enum _align = ImguiAlign::LeftIndented, bool _enabled = true);
  168. bool imguiImageChannel(bgfx::TextureHandle _image, uint8_t _channel, float _lod, float _scale, float _aspect, ImguiAlign::Enum _align = ImguiAlign::LeftIndented, bool _enabled = true);
  169. 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);
  170. float imguiGetTextLength(const char* _text, ImguiFontHandle _handle);
  171. bool imguiMouseOverArea();
  172. namespace ImGui
  173. {
  174. #define IMGUI_FLAGS_NONE UINT8_C(0x00)
  175. #define IMGUI_FLAGS_ALPHA_BLEND UINT8_C(0x01)
  176. // Helper function for passing bgfx::TextureHandle to ImGui::Image.
  177. inline void Image(bgfx::TextureHandle _handle
  178. , uint8_t _flags
  179. , uint8_t _mip
  180. , const ImVec2& _size
  181. , const ImVec2& _uv0 = ImVec2(0.0f, 0.0f)
  182. , const ImVec2& _uv1 = ImVec2(1.0f, 1.0f)
  183. , const ImVec4& _tintCol = ImVec4(1.0f, 1.0f, 1.0f, 1.0f)
  184. , const ImVec4& _borderCol = ImVec4(0.0f, 0.0f, 0.0f, 0.0f)
  185. )
  186. {
  187. union { struct { bgfx::TextureHandle handle; uint8_t flags; uint8_t mip; } s; ImTextureID ptr; } texture;
  188. texture.s.handle = _handle;
  189. texture.s.flags = _flags;
  190. texture.s.mip = _mip;
  191. Image(texture.ptr, _size, _uv0, _uv1, _tintCol, _borderCol);
  192. }
  193. // Helper function for passing bgfx::TextureHandle to ImGui::Image.
  194. inline void Image(bgfx::TextureHandle _handle
  195. , const ImVec2& _size
  196. , const ImVec2& _uv0 = ImVec2(0.0f, 0.0f)
  197. , const ImVec2& _uv1 = ImVec2(1.0f, 1.0f)
  198. , const ImVec4& _tintCol = ImVec4(1.0f, 1.0f, 1.0f, 1.0f)
  199. , const ImVec4& _borderCol = ImVec4(0.0f, 0.0f, 0.0f, 0.0f)
  200. )
  201. {
  202. Image(_handle, IMGUI_FLAGS_ALPHA_BLEND, 0, _size, _uv0, _uv1, _tintCol, _borderCol);
  203. }
  204. // Helper function for passing bgfx::TextureHandle to ImGui::ImageButton.
  205. inline bool ImageButton(bgfx::TextureHandle _handle
  206. , uint8_t _flags
  207. , uint8_t _mip
  208. , const ImVec2& _size
  209. , const ImVec2& _uv0 = ImVec2(0.0f, 0.0f)
  210. , const ImVec2& _uv1 = ImVec2(1.0f, 1.0f)
  211. , int _framePadding = -1
  212. , const ImVec4& _bgCol = ImVec4(0.0f, 0.0f, 0.0f, 0.0f)
  213. , const ImVec4& _tintCol = ImVec4(1.0f, 1.0f, 1.0f, 1.0f)
  214. )
  215. {
  216. union { struct { bgfx::TextureHandle handle; uint8_t flags; uint8_t mip; } s; ImTextureID ptr; } texture;
  217. texture.s.handle = _handle;
  218. texture.s.flags = _flags;
  219. texture.s.mip = _mip;
  220. return ImageButton(texture.ptr, _size, _uv0, _uv1, _framePadding, _bgCol, _tintCol);
  221. }
  222. // Helper function for passing bgfx::TextureHandle to ImGui::ImageButton.
  223. inline bool ImageButton(bgfx::TextureHandle _handle
  224. , const ImVec2& _size
  225. , const ImVec2& _uv0 = ImVec2(0.0f, 0.0f)
  226. , const ImVec2& _uv1 = ImVec2(1.0f, 1.0f)
  227. , int _framePadding = -1
  228. , const ImVec4& _bgCol = ImVec4(0.0f, 0.0f, 0.0f, 0.0f)
  229. , const ImVec4& _tintCol = ImVec4(1.0f, 1.0f, 1.0f, 1.0f)
  230. )
  231. {
  232. return ImageButton(_handle, IMGUI_FLAGS_ALPHA_BLEND, 0, _size, _uv0, _uv1, _framePadding, _bgCol, _tintCol);
  233. }
  234. inline void NextLine()
  235. {
  236. SetCursorPosY(GetCursorPosY() + GetTextLineHeightWithSpacing() );
  237. }
  238. } // namespace ImGui
  239. #endif // IMGUI_H_HEADER_GUARD