imgui.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676
  1. //
  2. // Copyright (c) 2009-2010 Mikko Mononen memon@inside.org
  3. //
  4. // This software is provided 'as-is', without any express or implied
  5. // warranty. In no event will the authors be held liable for any damages
  6. // arising from the use of this software.
  7. // Permission is granted to anyone to use this software for any purpose,
  8. // including commercial applications, and to alter it and redistribute it
  9. // freely, subject to the following restrictions:
  10. // 1. The origin of this software must not be misrepresented; you must not
  11. // claim that you wrote the original software. If you use this software
  12. // in a product, an acknowledgment in the product documentation would be
  13. // appreciated but is not required.
  14. // 2. Altered source versions must be plainly marked as such, and must not be
  15. // misrepresented as being the original software.
  16. // 3. This notice may not be removed or altered from any source distribution.
  17. //
  18. #include <stdio.h>
  19. #include <string.h>
  20. #define _USE_MATH_DEFINES
  21. #include <math.h>
  22. #include "imgui.h"
  23. #ifdef WIN32
  24. # define snprintf _snprintf
  25. #endif
  26. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  27. static const unsigned TEXT_POOL_SIZE = 50000;
  28. static char g_textPool[TEXT_POOL_SIZE];
  29. static unsigned g_textPoolSize = 0;
  30. static const char* allocText(const char* text)
  31. {
  32. unsigned len = static_cast<unsigned>(strlen(text)+1);
  33. if (g_textPoolSize + len >= TEXT_POOL_SIZE)
  34. return 0;
  35. char* dst = &g_textPool[g_textPoolSize];
  36. memcpy(dst, text, len);
  37. g_textPoolSize += len;
  38. return dst;
  39. }
  40. static const unsigned GFXCMD_QUEUE_SIZE = 5000;
  41. static imguiGfxCmd g_gfxCmdQueue[GFXCMD_QUEUE_SIZE];
  42. static unsigned g_gfxCmdQueueSize = 0;
  43. static void resetGfxCmdQueue()
  44. {
  45. g_gfxCmdQueueSize = 0;
  46. g_textPoolSize = 0;
  47. }
  48. static void addGfxCmdScissor(int x, int y, int w, int h)
  49. {
  50. if (g_gfxCmdQueueSize >= GFXCMD_QUEUE_SIZE)
  51. return;
  52. imguiGfxCmd& cmd = g_gfxCmdQueue[g_gfxCmdQueueSize++];
  53. cmd.type = IMGUI_GFXCMD_SCISSOR;
  54. cmd.flags = x < 0 ? 0 : 1; // on/off flag.
  55. cmd.col = 0;
  56. cmd.rect.x = (short)x;
  57. cmd.rect.y = (short)y;
  58. cmd.rect.w = (short)w;
  59. cmd.rect.h = (short)h;
  60. }
  61. static void addGfxCmdRect(float x, float y, float w, float h, unsigned int color)
  62. {
  63. if (g_gfxCmdQueueSize >= GFXCMD_QUEUE_SIZE)
  64. return;
  65. imguiGfxCmd& cmd = g_gfxCmdQueue[g_gfxCmdQueueSize++];
  66. cmd.type = IMGUI_GFXCMD_RECT;
  67. cmd.flags = 0;
  68. cmd.col = color;
  69. cmd.rect.x = (short)(x*8.0f);
  70. cmd.rect.y = (short)(y*8.0f);
  71. cmd.rect.w = (short)(w*8.0f);
  72. cmd.rect.h = (short)(h*8.0f);
  73. cmd.rect.r = 0;
  74. }
  75. static void addGfxCmdLine(float x0, float y0, float x1, float y1, float r, unsigned int color)
  76. {
  77. if (g_gfxCmdQueueSize >= GFXCMD_QUEUE_SIZE)
  78. return;
  79. imguiGfxCmd& cmd = g_gfxCmdQueue[g_gfxCmdQueueSize++];
  80. cmd.type = IMGUI_GFXCMD_LINE;
  81. cmd.flags = 0;
  82. cmd.col = color;
  83. cmd.line.x0 = (short)(x0*8.0f);
  84. cmd.line.y0 = (short)(y0*8.0f);
  85. cmd.line.x1 = (short)(x1*8.0f);
  86. cmd.line.y1 = (short)(y1*8.0f);
  87. cmd.line.r = (short)(r*8.0f);
  88. }
  89. static void addGfxCmdRoundedRect(float x, float y, float w, float h, float r, unsigned int color)
  90. {
  91. if (g_gfxCmdQueueSize >= GFXCMD_QUEUE_SIZE)
  92. return;
  93. imguiGfxCmd& cmd = g_gfxCmdQueue[g_gfxCmdQueueSize++];
  94. cmd.type = IMGUI_GFXCMD_RECT;
  95. cmd.flags = 0;
  96. cmd.col = color;
  97. cmd.rect.x = (short)(x*8.0f);
  98. cmd.rect.y = (short)(y*8.0f);
  99. cmd.rect.w = (short)(w*8.0f);
  100. cmd.rect.h = (short)(h*8.0f);
  101. cmd.rect.r = (short)(r*8.0f);
  102. }
  103. static void addGfxCmdTriangle(int x, int y, int w, int h, int flags, unsigned int color)
  104. {
  105. if (g_gfxCmdQueueSize >= GFXCMD_QUEUE_SIZE)
  106. return;
  107. imguiGfxCmd& cmd = g_gfxCmdQueue[g_gfxCmdQueueSize++];
  108. cmd.type = IMGUI_GFXCMD_TRIANGLE;
  109. cmd.flags = (char)flags;
  110. cmd.col = color;
  111. cmd.rect.x = (short)(x*8.0f);
  112. cmd.rect.y = (short)(y*8.0f);
  113. cmd.rect.w = (short)(w*8.0f);
  114. cmd.rect.h = (short)(h*8.0f);
  115. }
  116. static void addGfxCmdText(int x, int y, int align, const char* text, unsigned int color)
  117. {
  118. if (g_gfxCmdQueueSize >= GFXCMD_QUEUE_SIZE)
  119. return;
  120. imguiGfxCmd& cmd = g_gfxCmdQueue[g_gfxCmdQueueSize++];
  121. cmd.type = IMGUI_GFXCMD_TEXT;
  122. cmd.flags = 0;
  123. cmd.col = color;
  124. cmd.text.x = (short)x;
  125. cmd.text.y = (short)y;
  126. cmd.text.align = (short)align;
  127. cmd.text.text = allocText(text);
  128. }
  129. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  130. struct GuiState
  131. {
  132. GuiState() :
  133. left(false), leftPressed(false), leftReleased(false),
  134. mx(-1), my(-1), scroll(0),
  135. active(0), hot(0), hotToBe(0), isHot(false), isActive(false), wentActive(false),
  136. dragX(0), dragY(0), dragOrig(0), widgetX(0), widgetY(0), widgetW(100),
  137. insideCurrentScroll(false), areaId(0), widgetId(0)
  138. {
  139. }
  140. bool left;
  141. bool leftPressed, leftReleased;
  142. int mx,my;
  143. int scroll;
  144. unsigned int active;
  145. unsigned int hot;
  146. unsigned int hotToBe;
  147. bool isHot;
  148. bool isActive;
  149. bool wentActive;
  150. int dragX, dragY;
  151. float dragOrig;
  152. int widgetX, widgetY, widgetW;
  153. bool insideCurrentScroll;
  154. unsigned int areaId;
  155. unsigned int widgetId;
  156. };
  157. static GuiState g_state;
  158. inline bool anyActive()
  159. {
  160. return g_state.active != 0;
  161. }
  162. inline bool isActive(unsigned int id)
  163. {
  164. return g_state.active == id;
  165. }
  166. inline bool isHot(unsigned int id)
  167. {
  168. return g_state.hot == id;
  169. }
  170. inline bool inRect(int x, int y, int w, int h, bool checkScroll = true)
  171. {
  172. return (!checkScroll || g_state.insideCurrentScroll) && g_state.mx >= x && g_state.mx <= x+w && g_state.my >= y && g_state.my <= y+h;
  173. }
  174. inline void clearInput()
  175. {
  176. g_state.leftPressed = false;
  177. g_state.leftReleased = false;
  178. g_state.scroll = 0;
  179. }
  180. inline void clearActive()
  181. {
  182. g_state.active = 0;
  183. // mark all UI for this frame as processed
  184. clearInput();
  185. }
  186. inline void setActive(unsigned int id)
  187. {
  188. g_state.active = id;
  189. g_state.wentActive = true;
  190. }
  191. inline void setHot(unsigned int id)
  192. {
  193. g_state.hotToBe = id;
  194. }
  195. static bool buttonLogic(unsigned int id, bool over)
  196. {
  197. bool res = false;
  198. // process down
  199. if (!anyActive())
  200. {
  201. if (over)
  202. setHot(id);
  203. if (isHot(id) && g_state.leftPressed)
  204. setActive(id);
  205. }
  206. // if button is active, then react on left up
  207. if (isActive(id))
  208. {
  209. g_state.isActive = true;
  210. if (over)
  211. setHot(id);
  212. if (g_state.leftReleased)
  213. {
  214. if (isHot(id))
  215. res = true;
  216. clearActive();
  217. }
  218. }
  219. if (isHot(id))
  220. g_state.isHot = true;
  221. return res;
  222. }
  223. static void updateInput(int mx, int my, unsigned char mbut, int scroll)
  224. {
  225. bool left = (mbut & IMGUI_MBUT_LEFT) != 0;
  226. g_state.mx = mx;
  227. g_state.my = my;
  228. g_state.leftPressed = !g_state.left && left;
  229. g_state.leftReleased = g_state.left && !left;
  230. g_state.left = left;
  231. g_state.scroll = scroll;
  232. }
  233. void imguiBeginFrame(int mx, int my, unsigned char mbut, int scroll)
  234. {
  235. updateInput(mx,my,mbut,scroll);
  236. g_state.hot = g_state.hotToBe;
  237. g_state.hotToBe = 0;
  238. g_state.wentActive = false;
  239. g_state.isActive = false;
  240. g_state.isHot = false;
  241. g_state.widgetX = 0;
  242. g_state.widgetY = 0;
  243. g_state.widgetW = 0;
  244. g_state.areaId = 1;
  245. g_state.widgetId = 1;
  246. resetGfxCmdQueue();
  247. }
  248. void imguiEndFrame()
  249. {
  250. clearInput();
  251. }
  252. const imguiGfxCmd* imguiGetRenderQueue()
  253. {
  254. return g_gfxCmdQueue;
  255. }
  256. int imguiGetRenderQueueSize()
  257. {
  258. return g_gfxCmdQueueSize;
  259. }
  260. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  261. static const int BUTTON_HEIGHT = 20;
  262. static const int SLIDER_HEIGHT = 20;
  263. static const int SLIDER_MARKER_WIDTH = 10;
  264. static const int CHECK_SIZE = 8;
  265. static const int DEFAULT_SPACING = 4;
  266. static const int TEXT_HEIGHT = 8;
  267. static const int SCROLL_AREA_PADDING = 6;
  268. static const int INDENT_SIZE = 16;
  269. static const int AREA_HEADER = 28;
  270. static int g_scrollTop = 0;
  271. static int g_scrollBottom = 0;
  272. static int g_scrollRight = 0;
  273. static int g_scrollAreaTop = 0;
  274. static int* g_scrollVal = 0;
  275. static int g_focusTop = 0;
  276. static int g_focusBottom = 0;
  277. static unsigned int g_scrollId = 0;
  278. static bool g_insideScrollArea = false;
  279. bool imguiBeginScrollArea(const char* name, int x, int y, int w, int h, int* scroll)
  280. {
  281. g_state.areaId++;
  282. g_state.widgetId = 0;
  283. g_scrollId = (g_state.areaId<<16) | g_state.widgetId;
  284. g_state.widgetX = x + SCROLL_AREA_PADDING;
  285. g_state.widgetY = y+h-AREA_HEADER + (*scroll);
  286. g_state.widgetW = w - SCROLL_AREA_PADDING*4;
  287. g_scrollTop = y-AREA_HEADER+h;
  288. g_scrollBottom = y+SCROLL_AREA_PADDING;
  289. g_scrollRight = x+w - SCROLL_AREA_PADDING*3;
  290. g_scrollVal = scroll;
  291. g_scrollAreaTop = g_state.widgetY;
  292. g_focusTop = y-AREA_HEADER;
  293. g_focusBottom = y-AREA_HEADER+h;
  294. g_insideScrollArea = inRect(x, y, w, h, false);
  295. g_state.insideCurrentScroll = g_insideScrollArea;
  296. addGfxCmdRoundedRect((float)x, (float)y, (float)w, (float)h, 6, imguiRGBA(0,0,0,192));
  297. addGfxCmdText(x+AREA_HEADER/2, y+h-AREA_HEADER/2-TEXT_HEIGHT/2, IMGUI_ALIGN_LEFT, name, imguiRGBA(255,255,255,128));
  298. addGfxCmdScissor(x+SCROLL_AREA_PADDING, y+SCROLL_AREA_PADDING, w-SCROLL_AREA_PADDING*4, h-AREA_HEADER-SCROLL_AREA_PADDING);
  299. return g_insideScrollArea;
  300. }
  301. void imguiEndScrollArea()
  302. {
  303. // Disable scissoring.
  304. addGfxCmdScissor(-1,-1,-1,-1);
  305. // Draw scroll bar
  306. int x = g_scrollRight+SCROLL_AREA_PADDING/2;
  307. int y = g_scrollBottom;
  308. int w = SCROLL_AREA_PADDING*2;
  309. int h = g_scrollTop - g_scrollBottom;
  310. int stop = g_scrollAreaTop;
  311. int sbot = g_state.widgetY;
  312. int sh = stop - sbot; // The scrollable area height.
  313. float barHeight = (float)h/(float)sh;
  314. if (barHeight < 1)
  315. {
  316. float barY = (float)(y - sbot)/(float)sh;
  317. if (barY < 0) barY = 0;
  318. if (barY > 1) barY = 1;
  319. // Handle scroll bar logic.
  320. unsigned int hid = g_scrollId;
  321. int hx = x;
  322. int hy = y + (int)(barY*h);
  323. int hw = w;
  324. int hh = (int)(barHeight*h);
  325. const int range = h - (hh-1);
  326. bool over = inRect(hx, hy, hw, hh);
  327. buttonLogic(hid, over);
  328. if (isActive(hid))
  329. {
  330. float u = (float)(hy-y) / (float)range;
  331. if (g_state.wentActive)
  332. {
  333. g_state.dragY = g_state.my;
  334. g_state.dragOrig = u;
  335. }
  336. if (g_state.dragY != g_state.my)
  337. {
  338. u = g_state.dragOrig + (g_state.my - g_state.dragY) / (float)range;
  339. if (u < 0) u = 0;
  340. if (u > 1) u = 1;
  341. *g_scrollVal = (int)((1-u) * (sh - h));
  342. }
  343. }
  344. // BG
  345. addGfxCmdRoundedRect((float)x, (float)y, (float)w, (float)h, (float)w/2-1, imguiRGBA(0,0,0,196));
  346. // Bar
  347. if (isActive(hid))
  348. addGfxCmdRoundedRect((float)hx, (float)hy, (float)hw, (float)hh, (float)w/2-1, imguiRGBA(255,196,0,196));
  349. else
  350. addGfxCmdRoundedRect((float)hx, (float)hy, (float)hw, (float)hh, (float)w/2-1, isHot(hid) ? imguiRGBA(255,196,0,96) : imguiRGBA(255,255,255,64));
  351. // Handle mouse scrolling.
  352. if (g_insideScrollArea) // && !anyActive())
  353. {
  354. if (g_state.scroll)
  355. {
  356. *g_scrollVal += 20*g_state.scroll;
  357. if (*g_scrollVal < 0) *g_scrollVal = 0;
  358. if (*g_scrollVal > (sh - h)) *g_scrollVal = (sh - h);
  359. }
  360. }
  361. }
  362. g_state.insideCurrentScroll = false;
  363. }
  364. bool imguiButton(const char* text, bool enabled)
  365. {
  366. g_state.widgetId++;
  367. unsigned int id = (g_state.areaId<<16) | g_state.widgetId;
  368. int x = g_state.widgetX;
  369. int y = g_state.widgetY - BUTTON_HEIGHT;
  370. int w = g_state.widgetW;
  371. int h = BUTTON_HEIGHT;
  372. g_state.widgetY -= BUTTON_HEIGHT + DEFAULT_SPACING;
  373. bool over = enabled && inRect(x, y, w, h);
  374. bool res = buttonLogic(id, over);
  375. addGfxCmdRoundedRect((float)x, (float)y, (float)w, (float)h, (float)BUTTON_HEIGHT/2-1, imguiRGBA(128,128,128, isActive(id)?196:96));
  376. if (enabled)
  377. addGfxCmdText(x+BUTTON_HEIGHT/2, y+BUTTON_HEIGHT/2-TEXT_HEIGHT/2, IMGUI_ALIGN_LEFT, text, isHot(id) ? imguiRGBA(255,196,0,255) : imguiRGBA(255,255,255,200));
  378. else
  379. addGfxCmdText(x+BUTTON_HEIGHT/2, y+BUTTON_HEIGHT/2-TEXT_HEIGHT/2, IMGUI_ALIGN_LEFT, text, imguiRGBA(128,128,128,200));
  380. return res;
  381. }
  382. bool imguiItem(const char* text, bool enabled)
  383. {
  384. g_state.widgetId++;
  385. unsigned int id = (g_state.areaId<<16) | g_state.widgetId;
  386. int x = g_state.widgetX;
  387. int y = g_state.widgetY - BUTTON_HEIGHT;
  388. int w = g_state.widgetW;
  389. int h = BUTTON_HEIGHT;
  390. g_state.widgetY -= BUTTON_HEIGHT + DEFAULT_SPACING;
  391. bool over = enabled && inRect(x, y, w, h);
  392. bool res = buttonLogic(id, over);
  393. if (isHot(id))
  394. addGfxCmdRoundedRect((float)x, (float)y, (float)w, (float)h, 2.0f, imguiRGBA(255,196,0,isActive(id)?196:96));
  395. if (enabled)
  396. addGfxCmdText(x+BUTTON_HEIGHT/2, y+BUTTON_HEIGHT/2-TEXT_HEIGHT/2, IMGUI_ALIGN_LEFT, text, imguiRGBA(255,255,255,200));
  397. else
  398. addGfxCmdText(x+BUTTON_HEIGHT/2, y+BUTTON_HEIGHT/2-TEXT_HEIGHT/2, IMGUI_ALIGN_LEFT, text, imguiRGBA(128,128,128,200));
  399. return res;
  400. }
  401. bool imguiCheck(const char* text, bool checked, bool enabled)
  402. {
  403. g_state.widgetId++;
  404. unsigned int id = (g_state.areaId<<16) | g_state.widgetId;
  405. int x = g_state.widgetX;
  406. int y = g_state.widgetY - BUTTON_HEIGHT;
  407. int w = g_state.widgetW;
  408. int h = BUTTON_HEIGHT;
  409. g_state.widgetY -= BUTTON_HEIGHT + DEFAULT_SPACING;
  410. bool over = enabled && inRect(x, y, w, h);
  411. bool res = buttonLogic(id, over);
  412. const int cx = x+BUTTON_HEIGHT/2-CHECK_SIZE/2;
  413. const int cy = y+BUTTON_HEIGHT/2-CHECK_SIZE/2;
  414. addGfxCmdRoundedRect((float)cx-3, (float)cy-3, (float)CHECK_SIZE+6, (float)CHECK_SIZE+6, 4, imguiRGBA(128,128,128, isActive(id)?196:96));
  415. if (checked)
  416. {
  417. if (enabled)
  418. addGfxCmdRoundedRect((float)cx, (float)cy, (float)CHECK_SIZE, (float)CHECK_SIZE, (float)CHECK_SIZE/2-1, imguiRGBA(255,255,255,isActive(id)?255:200));
  419. else
  420. addGfxCmdRoundedRect((float)cx, (float)cy, (float)CHECK_SIZE, (float)CHECK_SIZE, (float)CHECK_SIZE/2-1, imguiRGBA(128,128,128,200));
  421. }
  422. if (enabled)
  423. addGfxCmdText(x+BUTTON_HEIGHT, y+BUTTON_HEIGHT/2-TEXT_HEIGHT/2, IMGUI_ALIGN_LEFT, text, isHot(id) ? imguiRGBA(255,196,0,255) : imguiRGBA(255,255,255,200));
  424. else
  425. addGfxCmdText(x+BUTTON_HEIGHT, y+BUTTON_HEIGHT/2-TEXT_HEIGHT/2, IMGUI_ALIGN_LEFT, text, imguiRGBA(128,128,128,200));
  426. return res;
  427. }
  428. bool imguiCollapse(const char* text, const char* subtext, bool checked, bool enabled)
  429. {
  430. g_state.widgetId++;
  431. unsigned int id = (g_state.areaId<<16) | g_state.widgetId;
  432. int x = g_state.widgetX;
  433. int y = g_state.widgetY - BUTTON_HEIGHT;
  434. int w = g_state.widgetW;
  435. int h = BUTTON_HEIGHT;
  436. g_state.widgetY -= BUTTON_HEIGHT; // + DEFAULT_SPACING;
  437. const int cx = x+BUTTON_HEIGHT/2-CHECK_SIZE/2;
  438. const int cy = y+BUTTON_HEIGHT/2-CHECK_SIZE/2;
  439. bool over = enabled && inRect(x, y, w, h);
  440. bool res = buttonLogic(id, over);
  441. if (checked)
  442. addGfxCmdTriangle(cx, cy, CHECK_SIZE, CHECK_SIZE, 2, imguiRGBA(255,255,255,isActive(id)?255:200));
  443. else
  444. addGfxCmdTriangle(cx, cy, CHECK_SIZE, CHECK_SIZE, 1, imguiRGBA(255,255,255,isActive(id)?255:200));
  445. if (enabled)
  446. addGfxCmdText(x+BUTTON_HEIGHT, y+BUTTON_HEIGHT/2-TEXT_HEIGHT/2, IMGUI_ALIGN_LEFT, text, isHot(id) ? imguiRGBA(255,196,0,255) : imguiRGBA(255,255,255,200));
  447. else
  448. addGfxCmdText(x+BUTTON_HEIGHT, y+BUTTON_HEIGHT/2-TEXT_HEIGHT/2, IMGUI_ALIGN_LEFT, text, imguiRGBA(128,128,128,200));
  449. if (subtext)
  450. addGfxCmdText(x+w-BUTTON_HEIGHT/2, y+BUTTON_HEIGHT/2-TEXT_HEIGHT/2, IMGUI_ALIGN_RIGHT, subtext, imguiRGBA(255,255,255,128));
  451. return res;
  452. }
  453. void imguiLabel(const char* text)
  454. {
  455. int x = g_state.widgetX;
  456. int y = g_state.widgetY - BUTTON_HEIGHT;
  457. g_state.widgetY -= BUTTON_HEIGHT;
  458. addGfxCmdText(x, y+BUTTON_HEIGHT/2-TEXT_HEIGHT/2, IMGUI_ALIGN_LEFT, text, imguiRGBA(255,255,255,255));
  459. }
  460. void imguiValue(const char* text)
  461. {
  462. const int x = g_state.widgetX;
  463. const int y = g_state.widgetY - BUTTON_HEIGHT;
  464. const int w = g_state.widgetW;
  465. g_state.widgetY -= BUTTON_HEIGHT;
  466. addGfxCmdText(x+w-BUTTON_HEIGHT/2, y+BUTTON_HEIGHT/2-TEXT_HEIGHT/2, IMGUI_ALIGN_RIGHT, text, imguiRGBA(255,255,255,200));
  467. }
  468. bool imguiSlider(const char* text, float* val, float vmin, float vmax, float vinc, bool enabled)
  469. {
  470. g_state.widgetId++;
  471. unsigned int id = (g_state.areaId<<16) | g_state.widgetId;
  472. int x = g_state.widgetX;
  473. int y = g_state.widgetY - BUTTON_HEIGHT;
  474. int w = g_state.widgetW;
  475. int h = SLIDER_HEIGHT;
  476. g_state.widgetY -= SLIDER_HEIGHT + DEFAULT_SPACING;
  477. addGfxCmdRoundedRect((float)x, (float)y, (float)w, (float)h, 4.0f, imguiRGBA(0,0,0,128));
  478. const int range = w - SLIDER_MARKER_WIDTH;
  479. float u = (*val - vmin) / (vmax-vmin);
  480. if (u < 0) u = 0;
  481. if (u > 1) u = 1;
  482. int m = (int)(u * range);
  483. bool over = enabled && inRect(x+m, y, SLIDER_MARKER_WIDTH, SLIDER_HEIGHT);
  484. bool res = buttonLogic(id, over);
  485. bool valChanged = false;
  486. if (isActive(id))
  487. {
  488. if (g_state.wentActive)
  489. {
  490. g_state.dragX = g_state.mx;
  491. g_state.dragOrig = u;
  492. }
  493. if (g_state.dragX != g_state.mx)
  494. {
  495. u = g_state.dragOrig + (float)(g_state.mx - g_state.dragX) / (float)range;
  496. if (u < 0) u = 0;
  497. if (u > 1) u = 1;
  498. *val = vmin + u*(vmax-vmin);
  499. *val = floorf(*val/vinc+0.5f)*vinc; // Snap to vinc
  500. m = (int)(u * range);
  501. valChanged = true;
  502. }
  503. }
  504. if (isActive(id))
  505. addGfxCmdRoundedRect((float)(x+m), (float)y, (float)SLIDER_MARKER_WIDTH, (float)SLIDER_HEIGHT, 4.0f, imguiRGBA(255,255,255,255));
  506. else
  507. addGfxCmdRoundedRect((float)(x+m), (float)y, (float)SLIDER_MARKER_WIDTH, (float)SLIDER_HEIGHT, 4.0f, isHot(id) ? imguiRGBA(255,196,0,128) : imguiRGBA(255,255,255,64));
  508. // TODO: fix this, take a look at 'nicenum'.
  509. int digits = (int)(ceilf(log10f(vinc)));
  510. char fmt[16];
  511. snprintf(fmt, 16, "%%.%df", digits >= 0 ? 0 : -digits);
  512. char msg[128];
  513. snprintf(msg, 128, fmt, *val);
  514. if (enabled)
  515. {
  516. addGfxCmdText(x+SLIDER_HEIGHT/2, y+SLIDER_HEIGHT/2-TEXT_HEIGHT/2, IMGUI_ALIGN_LEFT, text, isHot(id) ? imguiRGBA(255,196,0,255) : imguiRGBA(255,255,255,200));
  517. addGfxCmdText(x+w-SLIDER_HEIGHT/2, y+SLIDER_HEIGHT/2-TEXT_HEIGHT/2, IMGUI_ALIGN_RIGHT, msg, isHot(id) ? imguiRGBA(255,196,0,255) : imguiRGBA(255,255,255,200));
  518. }
  519. else
  520. {
  521. addGfxCmdText(x+SLIDER_HEIGHT/2, y+SLIDER_HEIGHT/2-TEXT_HEIGHT/2, IMGUI_ALIGN_LEFT, text, imguiRGBA(128,128,128,200));
  522. addGfxCmdText(x+w-SLIDER_HEIGHT/2, y+SLIDER_HEIGHT/2-TEXT_HEIGHT/2, IMGUI_ALIGN_RIGHT, msg, imguiRGBA(128,128,128,200));
  523. }
  524. return res || valChanged;
  525. }
  526. void imguiIndent()
  527. {
  528. g_state.widgetX += INDENT_SIZE;
  529. g_state.widgetW -= INDENT_SIZE;
  530. }
  531. void imguiUnindent()
  532. {
  533. g_state.widgetX -= INDENT_SIZE;
  534. g_state.widgetW += INDENT_SIZE;
  535. }
  536. void imguiSeparator()
  537. {
  538. g_state.widgetY -= DEFAULT_SPACING*3;
  539. }
  540. void imguiSeparatorLine()
  541. {
  542. int x = g_state.widgetX;
  543. int y = g_state.widgetY - DEFAULT_SPACING*2;
  544. int w = g_state.widgetW;
  545. int h = 1;
  546. g_state.widgetY -= DEFAULT_SPACING*4;
  547. addGfxCmdRect((float)x, (float)y, (float)w, (float)h, imguiRGBA(255,255,255,32));
  548. }
  549. void imguiDrawText(int x, int y, int align, const char* text, unsigned int color)
  550. {
  551. addGfxCmdText(x, y, align, text, color);
  552. }
  553. void imguiDrawLine(float x0, float y0, float x1, float y1, float r, unsigned int color)
  554. {
  555. addGfxCmdLine(x0, y0, x1, y1, r, color);
  556. }
  557. void imguiDrawRect(float x, float y, float w, float h, unsigned int color)
  558. {
  559. addGfxCmdRect(x, y, w, h, color);
  560. }
  561. void imguiDrawRoundedRect(float x, float y, float w, float h, float r, unsigned int color)
  562. {
  563. addGfxCmdRoundedRect(x, y, w, h, r, color);
  564. }