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