| // ImGui library |
| // See ImGui::ShowTestWindow() for sample code. |
| // Read 'Programmer guide' below for notes on how to setup ImGui in your codebase. |
| // Get latest version at https://github.com/ocornut/imgui |
| |
| /* |
| |
| MISSION STATEMENT |
| |
| - easy to use to create code-driven and data-driven tools |
| - easy to use to create adhoc short-lived tools and long-lived, more elaborate tools |
| - easy to hack and improve |
| - minimize screen real-estate usage |
| - minimize setup and maintainance |
| - minimize state storage on user side |
| - portable, minimize dependencies, run on target (consoles, etc.) |
| - efficient runtime (nb- we do allocate when "growing" content - creating a window / opening a tree node for the first time, etc. - but a typical frame won't allocate anything) |
| - read about immediate-mode GUI principles @ http://mollyrocket.com/861, http://mollyrocket.com/forums/index.html |
| |
| Designed for developers and content-creators, not the typical end-user! Some of the weaknesses includes: |
| - doesn't look fancy, doesn't animate |
| - limited layout features, intricate layouts are typically crafted in code |
| - assume ASCII text, using strlen() and [] operators, etc |
| - occasionally use statically sized buffers for string manipulations - won't crash, but some long text may be clipped |
| |
| USER GUIDE |
| |
| - double-click title bar to collapse window |
| - click upper right corner to close a window, available when 'bool* open' is passed to ImGui::Begin() |
| - click and drag on lower right corner to resize window |
| - click and drag on any empty space to move window |
| - double-click/double-tap on lower right corner grip to auto-fit to content |
| - TAB/SHIFT+TAB to cycle through keyboard editable fields |
| - use mouse wheel to scroll |
| - use CTRL+mouse wheel to zoom window contents (if IO.FontAllowScaling is true) |
| - CTRL+Click on a slider to input value as text |
| - text editor: |
| - Hold SHIFT or use mouse to select text. |
| - CTRL+Left/Right to word jump |
| - CTRL+Shift+Left/Right to select words |
| - CTRL+A our Double-Click to select all |
| - CTRL+X,CTRL+C,CTRL+V to use OS clipboard |
| - CTRL+Z,CTRL+Y to undo/redo |
| - ESCAPE to revert text to its original value |
| - You can apply arithmetic operators +,*,/ on numerical values. Use +- to subtract (because - would set a negative value!) |
| |
| PROGRAMMER GUIDE |
| |
| - your code creates the UI, if your code doesn't run the UI is gone! == dynamic UI, no construction step, less data retention on your side, no state duplication, less sync, less errors. |
| - see ImGui::ShowTestWindow() for user-side sample code |
| - getting started: |
| - initialisation: call ImGui::GetIO() and fill the 'Settings' data. |
| - every frame: |
| 1/ in your mainloop or right after you got your keyboard/mouse info, call ImGui::GetIO() and fill the 'Input' data, then call ImGui::NewFrame(). |
| 2/ use any ImGui function you want between NewFrame() and Render() |
| 3/ ImGui::Render() to render all the accumulated command-lists. it will cack your RenderDrawListFn handler set in the IO structure. |
| - all rendering information are stored into command-lists until ImGui::Render() is called. |
| - effectively it means you can create widgets at any time in your code, regardless of "update" vs "render" considerations. |
| - a typical application skeleton may be: |
| |
| // Application init |
| // TODO: Fill all 'Settings' fields of the io structure |
| ImGuiIO& io = ImGui::GetIO(); |
| io.DisplaySize.x = 1920.0f; |
| io.DisplaySize.y = 1280.0f; |
| io.DeltaTime = 1.0f/60.0f; |
| io.IniFilename = "imgui.ini"; |
| |
| // Application mainloop |
| while (true) |
| { |
| // 1/ get low-level input |
| // e.g. on Win32, GetKeyboardState(), or poll your events, etc. |
| |
| // 2/ TODO: Fill all 'Input' fields of io structure and call NewFrame |
| ImGuiIO& io = ImGui::GetIO(); |
| io.MousePos = ... |
| io.KeysDown[i] = ... |
| ImGui::NewFrame(); |
| |
| // 3/ most of your application code here - you can use any of ImGui::* functions between NewFrame() and Render() calls |
| GameUpdate(); |
| GameRender(); |
| |
| // 4/ render & swap video buffers |
| ImGui::Render(); |
| // swap video buffer, etc. |
| } |
| |
| - some widgets carry state and requires an unique ID to do so. |
| - unique ID are typically derived from a string label, an indice or a pointer. |
| - use PushID/PopID to easily create scopes and avoid ID conflicts. A Window is also an implicit scope. |
| - when creating trees, ID are particularly important because you want to preserve the opened/closed state of tree nodes. |
| depending on your use cases you may want to use strings, indices or pointers as ID. experiment and see what makes more sense! |
| e.g. When displaying a single object, using a static string as ID will preserve your node open/closed state when the targetted object change |
| e.g. When displaying a list of objects, using indices or pointers as ID will preserve the node open/closed state per object |
| - when passing a label you can optionally specify extra unique ID information within the same string using "##". This helps solving the simpler collision cases. |
| e.g. "Label" display "Label" and uses "Label" as ID |
| e.g. "Label##Foobar" display "Label" and uses "Label##Foobar" as ID |
| e.g. "##Foobar" display an empty label and uses "##Foobar" as ID |
| |
| - if you want to use a different font than the default |
| - create bitmap font data using BMFont. allocate ImGui::GetIO().Font and use ->LoadFromFile()/LoadFromMemory(), set ImGui::GetIO().FontHeight |
| - load your texture yourself. texture *MUST* have white pixel at UV coordinate 'IMDRAW_TEX_UV_FOR_WHITE' (you can #define it in imconfig.h), this is used by solid objects. |
| |
| - tip: the construct 'if (IMGUI_ONCE_UPON_A_FRAME)' will evaluate to true only once a frame, you can use it to add custom UI in the middle of a deep nested inner loop in your code. |
| - tip: you can call Render() multiple times (e.g for VR renders), up to you to communicate the extra state to your RenderDrawListFn function. |
| - tip: you can create widgets without a Begin/End block, they will go in an implicit window called "Debug" |
| |
| ISSUES AND TODO-LIST |
| |
| - misc: merge ImVec4 / ImGuiAabb, they are essentially duplicate containers |
| - main: make IsHovered() more consistent for various type of widgets, widgets with multiple components, etc. also effectively IsHovered() region sometimes differs from hot region, e.g tree nodes |
| - window: autofit is losing its purpose when user relies on any dynamic layout (window width multiplier, column). maybe just discard autofit? |
| - window: support horizontal scroll |
| - window: fix resize grip scaling along with Rounding style setting |
| - widgets: switching from "widget-label" to "label-widget" would make it more convenient to integrate widgets in trees |
| - widgets: clip text? hover clipped text shows it in a tooltip or in-place overlay |
| - scrollbar: use relative mouse movement when first-clicking inside of scroll grab box. |
| - input number: optional range min/max |
| - input number: holding [-]/[+] buttons should increase the step non-linearly |
| - input number: rename Input*() to Input(), Slider*() to Slider() ? |
| - layout: clean up the InputFloat3/SliderFloat3/ColorEdit4 horrible layout code. item width should include frame padding, then we can have a generic horizontal layout helper. |
| - add input2/4 helper (once above layout helpers are in they'll be smaller) |
| - columns: declare column set (each column: fixed size, %, fill, distribute default size among fills) |
| - columns: columns header to act as button (~sort op) and allow resize/reorder |
| - columns: user specify columns size |
| - combo: turn child handling code into popup helper |
| - list selection, concept of a selectable "block" (that can be multiple widgets) |
| - menubar, menus |
| - plot: add a stride parameter? |
| - plot: add a helper e.g. Plot(char* label, float value, float time_span=2.0f) that stores values and Plot them for you - probably another function name. and/or automatically allow to plot ANY displayed value (more reliance on stable ID) |
| - file selection widget -> build the tool in our codebase to improve model-dialog idioms (may or not lead to ImGui changes) |
| - slider: allow using the [-]/[+] buttons used by InputFloat()/InputInt() |
| - slider: initial absolute click is unprecise. change to relative movement slider? hide mouse cursor, allow more precise input using less screen-space. |
| - text edit: centered text for slider or input text to it matches typical positionning. |
| - text edit: flag to disable live update of the user buffer. |
| - text edit: field resize behaviour - field could stretch when being edited? hover tooltip shows more text? |
| - text edit: pasting text into a number box should filter the characters the same way direct input does |
| - text edit: allow code to catch user pressing Return (perhaps through disable live edit? so only Return apply the final value, also allow catching Return if value didn't changed) |
| - settings: write more decent code to allow saving/loading new fields |
| - log: be able to right-click and log a window or tree-node into tty/file/clipboard? |
| - filters: set a current filter that tree node can automatically query to hide themselves |
| - filters: handle wildcards (with implicit leading/trailing *), regexps |
| - shortcuts: add a shortcut api, e.g. parse "&Save" and/or "Save (CTRL+S)", pass in to widgets or provide simple ways to use (button=activate, input=focus) |
| - keyboard: full keyboard navigation and focus |
| - clipboard: add a default "local" implementation of clipboard functions (user will only need to override them to connect to OS clipboard) |
| - misc: not thread-safe |
| - optimisation/render: use indexed rendering |
| - optimisation/render: move clip-rect to vertex data? would allow merging all commands |
| - optimisation/render: merge command-list of all windows into one command-list? |
| - optimisation: turn some the various stack vectors into statically-sized arrays |
| - optimisation: better clipping for multi-component widgets |
| - optimisation: specialize for height based clipping first (assume widgets never go up + height tests before width tests?) |
| - optimisation/portability: provide ImVector style implementation |
| - optimisation/portability: remove dependency on <algorithm> |
| */ |
| |
| #include "imgui.h" |
| #include <ctype.h> // toupper |
| #include <math.h> // sqrt |
| #include <stdint.h> // intptr_t |
| #include <stdio.h> // vsnprintf |
| #include <string.h> // memset |
| |
| #ifdef _MSC_VER |
| #pragma warning (disable: 4996) // 'This function or variable may be unsafe': strcpy, strdup, sprintf, vsnprintf, sscanf, fopen |
| #endif |
| |
| //------------------------------------------------------------------------- |
| // Forward Declarations |
| //------------------------------------------------------------------------- |
| |
| namespace ImGui |
| { |
| |
| static bool ButtonBehaviour(const ImGuiAabb& bb, const ImGuiID& id, bool* out_hovered = NULL, bool* out_held = NULL, bool repeat = false); |
| static void RenderFrame(ImVec2 p_min, ImVec2 p_max, ImU32 fill_col, bool border = true, float rounding = 0.0f); |
| static void RenderText(ImVec2 pos, const char* text, const char* text_end = NULL, const bool hide_text_after_hash = true); |
| static ImVec2 CalcTextSize(const char* text, const char* text_end = NULL, const bool hide_text_after_hash = true); |
| static void LogText(const ImVec2& ref_pos, const char* text, const char* text_end = NULL); |
| |
| static void ItemSize(ImVec2 size, ImVec2* adjust_start_offset = NULL); |
| static void ItemSize(const ImGuiAabb& aabb, ImVec2* adjust_start_offset = NULL); |
| static void PushColumnClipRect(int column_index = -1); |
| static bool IsClipped(const ImGuiAabb& aabb); |
| static bool ClipAdvance(const ImGuiAabb& aabb); |
| |
| static bool IsMouseHoveringBox(const ImGuiAabb& box); |
| static bool IsKeyPressedMap(ImGuiKey key, bool repeat = true); |
| |
| static bool CloseWindowButton(bool* open = NULL); |
| static void FocusWindow(ImGuiWindow* window); |
| static ImGuiWindow* FindHoveredWindow(ImVec2 pos, bool excluding_childs); |
| |
| }; // namespace ImGui |
| |
| //----------------------------------------------------------------------------- |
| // User facing structures |
| //----------------------------------------------------------------------------- |
| |
| ImGuiStyle::ImGuiStyle() |
| { |
| WindowPadding = ImVec2(8,8); // Padding within a window |
| WindowMinSize = ImVec2(48,48); // Minimum window size |
| FramePadding = ImVec2(5,4); // Padding within a framed rectangle (used by most widgets) |
| ItemSpacing = ImVec2(10,5); // Horizontal and vertical spacing between widgets |
| ItemInnerSpacing = ImVec2(5,5); // Horizontal and vertical spacing between within elements of a composed widget (e.g. a slider and its label) |
| TouchExtraPadding = ImVec2(0,0); // 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! |
| AutoFitPadding = ImVec2(8,8); // Extra space after auto-fit (double-clicking on resize grip) |
| WindowFillAlphaDefault = 0.70f; |
| WindowRounding = 10.0f; |
| TreeNodeSpacing = 22.0f; |
| ColumnsMinSpacing = 6.0f; // Minimum space between two columns |
| ScrollBarWidth = 16.0f; |
| |
| Colors[ImGuiCol_Text] = ImVec4(0.90f, 0.90f, 0.90f, 1.00f); |
| Colors[ImGuiCol_WindowBg] = ImVec4(0.00f, 0.00f, 0.00f, 1.00f); |
| Colors[ImGuiCol_Border] = ImVec4(1.00f, 1.00f, 1.00f, 1.00f); |
| Colors[ImGuiCol_BorderShadow] = ImVec4(0.00f, 0.00f, 0.00f, 0.60f); |
| Colors[ImGuiCol_FrameBg] = ImVec4(0.80f, 0.80f, 0.80f, 0.30f); // Background of checkbox, radio button, plot, slider, text input |
| Colors[ImGuiCol_TitleBg] = ImVec4(0.50f, 0.50f, 1.00f, 0.45f); |
| Colors[ImGuiCol_TitleBgCollapsed] = ImVec4(0.40f, 0.40f, 0.80f, 0.20f); |
| Colors[ImGuiCol_ScrollbarBg] = ImVec4(0.40f, 0.40f, 0.80f, 0.15f); |
| Colors[ImGuiCol_ScrollbarGrab] = ImVec4(0.40f, 0.40f, 0.80f, 0.30f); |
| Colors[ImGuiCol_ScrollbarGrabHovered] = ImVec4(0.40f, 0.40f, 0.80f, 0.40f); |
| Colors[ImGuiCol_ScrollbarGrabActive] = ImVec4(0.80f, 0.50f, 0.50f, 0.40f); |
| Colors[ImGuiCol_ComboBg] = ImVec4(0.20f, 0.20f, 0.20f, 0.99f); |
| Colors[ImGuiCol_CheckHovered] = ImVec4(0.60f, 0.40f, 0.40f, 0.45f); |
| Colors[ImGuiCol_CheckActive] = ImVec4(0.90f, 0.90f, 0.90f, 0.50f); |
| Colors[ImGuiCol_SliderGrab] = ImVec4(1.00f, 1.00f, 1.00f, 0.30f); |
| Colors[ImGuiCol_SliderGrabActive] = ImVec4(0.80f, 0.50f, 0.50f, 1.00f); |
| Colors[ImGuiCol_Button] = ImVec4(0.67f, 0.40f, 0.40f, 0.60f); |
| Colors[ImGuiCol_ButtonHovered] = ImVec4(0.60f, 0.40f, 0.40f, 1.00f); |
| Colors[ImGuiCol_ButtonActive] = ImVec4(0.80f, 0.50f, 0.50f, 1.00f); |
| Colors[ImGuiCol_Header] = ImVec4(0.40f, 0.40f, 0.90f, 0.45f); |
| Colors[ImGuiCol_HeaderHovered] = ImVec4(0.45f, 0.45f, 0.90f, 0.80f); |
| Colors[ImGuiCol_HeaderActive] = ImVec4(0.60f, 0.60f, 0.80f, 1.00f); |
| Colors[ImGuiCol_Column] = ImVec4(1.00f, 1.00f, 1.00f, 1.00f); |
| Colors[ImGuiCol_ColumnHovered] = ImVec4(0.60f, 0.40f, 0.40f, 1.00f); |
| Colors[ImGuiCol_ColumnActive] = ImVec4(0.80f, 0.50f, 0.50f, 1.00f); |
| Colors[ImGuiCol_ResizeGrip] = ImVec4(1.00f, 1.00f, 1.00f, 0.30f); |
| Colors[ImGuiCol_ResizeGripHovered] = ImVec4(1.00f, 1.00f, 1.00f, 0.60f); |
| Colors[ImGuiCol_ResizeGripActive] = ImVec4(1.00f, 1.00f, 1.00f, 0.90f); |
| Colors[ImGuiCol_CloseButton] = ImVec4(0.50f, 0.50f, 0.90f, 0.50f); |
| Colors[ImGuiCol_CloseButtonHovered] = ImVec4(0.70f, 0.70f, 0.90f, 0.60f); |
| Colors[ImGuiCol_CloseButtonActive] = ImVec4(0.70f, 0.70f, 0.70f, 1.00f); |
| Colors[ImGuiCol_PlotLines] = ImVec4(1.00f, 1.00f, 1.00f, 1.00f); |
| Colors[ImGuiCol_PlotLinesHovered] = ImVec4(0.90f, 0.70f, 0.00f, 1.00f); |
| Colors[ImGuiCol_PlotHistogram] = ImVec4(0.90f, 0.70f, 0.00f, 1.00f); |
| Colors[ImGuiCol_PlotHistogramHovered] = ImVec4(1.00f, 0.60f, 0.00f, 1.00f); |
| Colors[ImGuiCol_TextSelectedBg] = ImVec4(0.00f, 0.00f, 1.00f, 0.35f); |
| Colors[ImGuiCol_TooltipBg] = ImVec4(0.05f, 0.05f, 0.10f, 0.90f); |
| } |
| |
| ImGuiIO::ImGuiIO() |
| { |
| memset(this, 0, sizeof(*this)); |
| DeltaTime = 1.0f/60.0f; |
| IniSavingRate = 5.0f; |
| IniFilename = "imgui.ini"; |
| LogFilename = "imgui_log.txt"; |
| Font = NULL; |
| FontAllowScaling = false; |
| PixelCenterOffset = 0.5f; |
| MousePos = ImVec2(-1,-1); |
| MousePosPrev = ImVec2(-1,-1); |
| MouseDoubleClickTime = 0.30f; |
| MouseDoubleClickMaxDist = 6.0f; |
| } |
| |
| // Pass in translated ASCII characters for text input. |
| // - with glfw you can get those from the callback set in glfwSetCharCallback() |
| // - on Windows you can get those using ToAscii+keyboard state, or via the VM_CHAR message |
| void ImGuiIO::AddInputCharacter(char c) |
| { |
| const size_t n = strlen(InputCharacters); |
| if (n < sizeof(InputCharacters) / sizeof(InputCharacters[0])) |
| { |
| InputCharacters[n] = c; |
| InputCharacters[n+1] = 0; |
| } |
| } |
| |
| //----------------------------------------------------------------------------- |
| // Helpers |
| //----------------------------------------------------------------------------- |
| |
| #undef ARRAYSIZE |
| #define ARRAYSIZE(_ARR) ((int)(sizeof(_ARR)/sizeof(*_ARR))) |
| |
| #undef PI |
| const float PI = 3.14159265358979323846f; |
| |
| #ifdef INT_MAX |
| #define IM_INT_MAX INT_MAX |
| #else |
| #define IM_INT_MAX 2147483647 |
| #endif |
| |
| // Math bits |
| // We are keeping those static in the .cpp file so as not to leak them outside, in the case the user has implicit cast operators between ImVec2 and its own types. |
| static inline ImVec2 operator*(const ImVec2& lhs, const float rhs) { return ImVec2(lhs.x*rhs, lhs.y*rhs); } |
| static inline ImVec2 operator/(const ImVec2& lhs, const float rhs) { return ImVec2(lhs.x/rhs, lhs.y/rhs); } |
| static inline ImVec2 operator+(const ImVec2& lhs, const ImVec2& rhs) { return ImVec2(lhs.x+rhs.x, lhs.y+rhs.y); } |
| static inline ImVec2 operator-(const ImVec2& lhs, const ImVec2& rhs) { return ImVec2(lhs.x-rhs.x, lhs.y-rhs.y); } |
| static inline ImVec2 operator*(const ImVec2& lhs, const ImVec2 rhs) { return ImVec2(lhs.x*rhs.x, lhs.y*rhs.y); } |
| static inline ImVec2 operator/(const ImVec2& lhs, const ImVec2 rhs) { return ImVec2(lhs.x/rhs.x, lhs.y/rhs.y); } |
| static inline ImVec2& operator+=(ImVec2& lhs, const ImVec2& rhs) { lhs.x += rhs.x; lhs.y += rhs.y; return lhs; } |
| static inline ImVec2& operator-=(ImVec2& lhs, const ImVec2& rhs) { lhs.x -= rhs.x; lhs.y -= rhs.y; return lhs; } |
| static inline ImVec2& operator*=(ImVec2& lhs, const float rhs) { lhs.x *= rhs; lhs.y *= rhs; return lhs; } |
| static inline ImVec2& operator/=(ImVec2& lhs, const float rhs) { lhs.x /= rhs; lhs.y /= rhs; return lhs; } |
| |
| static inline int ImMin(int lhs, int rhs) { return lhs < rhs ? lhs : rhs; } |
| static inline int ImMax(int lhs, int rhs) { return lhs >= rhs ? lhs : rhs; } |
| static inline float ImMin(float lhs, float rhs) { return lhs < rhs ? lhs : rhs; } |
| static inline float ImMax(float lhs, float rhs) { return lhs >= rhs ? lhs : rhs; } |
| static inline ImVec2 ImMin(const ImVec2& lhs, const ImVec2& rhs) { return ImVec2(ImMin(lhs.x,rhs.x), ImMin(lhs.y,rhs.y)); } |
| static inline ImVec2 ImMax(const ImVec2& lhs, const ImVec2& rhs) { return ImVec2(ImMax(lhs.x,rhs.x), ImMax(lhs.y,rhs.y)); } |
| static inline float ImClamp(float f, float mn, float mx) { return (f < mn) ? mn : (f > mx) ? mx : f; } |
| static inline ImVec2 ImClamp(const ImVec2& f, const ImVec2& mn, ImVec2 mx) { return ImVec2(ImClamp(f.x,mn.x,mx.x), ImClamp(f.y,mn.y,mx.y)); } |
| static inline float ImSaturate(float f) { return (f < 0.0f) ? 0.0f : (f > 1.0f) ? 1.0f : f; } |
| static inline float ImLerp(float a, float b, float t) { return a + (b - a) * t; } |
| static inline ImVec2 ImLerp(const ImVec2& a, const ImVec2& b, float t) { return a + (b - a) * t; } |
| static inline ImVec2 ImLerp(const ImVec2& a, const ImVec2& b, const ImVec2& t) { return ImVec2(a.x + (b.x - a.x) * t.x, a.y + (b.y - a.y) * t.y); } |
| static inline float ImLength(const ImVec2& lhs) { return sqrt(lhs.x*lhs.x + lhs.y*lhs.y); } |
| |
| static int ImStricmp(const char* str1, const char* str2) |
| { |
| int d; |
| while ((d = toupper(*str2) - toupper(*str1)) == 0 && *str1) { str1++; str2++; } |
| return d; |
| } |
| |
| static const char* ImStristr(const char* haystack, const char* needle, const char* needle_end) |
| { |
| if (!needle_end) |
| needle_end = needle + strlen(needle); |
| |
| const char un0 = (char)toupper(*needle); |
| while (*haystack) |
| { |
| if (toupper(*haystack) == un0) |
| { |
| const char* b = needle + 1; |
| for (const char* a = haystack + 1; b < needle_end; a++, b++) |
| if (toupper(*a) != toupper(*b)) |
| break; |
| if (b == needle_end) |
| return haystack; |
| } |
| haystack++; |
| } |
| return NULL; |
| } |
| |
| static ImU32 crc32(const void* data, size_t data_size, ImU32 seed = 0) |
| { |
| static ImU32 crc32_lut[256] = { 0 }; |
| if (!crc32_lut[1]) |
| { |
| const ImU32 polynomial = 0xEDB88320; |
| for (ImU32 i = 0; i < 256; i++) |
| { |
| ImU32 crc = i; |
| for (ImU32 j = 0; j < 8; j++) |
| crc = (crc >> 1) ^ (-int(crc & 1) & polynomial); |
| crc32_lut[i] = crc; |
| } |
| } |
| ImU32 crc = ~seed; |
| const unsigned char* current = (const unsigned char*)data; |
| while (data_size--) |
| crc = (crc >> 8) ^ crc32_lut[(crc & 0xFF) ^ *current++]; |
| return ~crc; |
| } |
| |
| static size_t ImFormatString(char* buf, size_t buf_size, const char* fmt, ...) |
| { |
| va_list args; |
| va_start(args, fmt); |
| int w = vsnprintf(buf, buf_size, fmt, args); |
| va_end(args); |
| buf[buf_size-1] = 0; |
| if (w == -1) w = (int)buf_size; |
| return w; |
| } |
| |
| static size_t ImFormatStringV(char* buf, size_t buf_size, const char* fmt, va_list args) |
| { |
| int w = vsnprintf(buf, buf_size, fmt, args); |
| buf[buf_size-1] = 0; |
| if (w == -1) w = (int)buf_size; |
| return w; |
| } |
| |
| static ImU32 ImConvertColorFloat4ToU32(const ImVec4& in) |
| { |
| ImU32 out = ((ImU32)(ImSaturate(in.x)*255.f)); |
| out |= ((ImU32)(ImSaturate(in.y)*255.f) << 8); |
| out |= ((ImU32)(ImSaturate(in.z)*255.f) << 16); |
| out |= ((ImU32)(ImSaturate(in.w)*255.f) << 24); |
| return out; |
| } |
| |
| // Convert rgb floats ([0-1],[0-1],[0-1]) to hsv floats ([0-1],[0-1],[0-1]), from Foley & van Dam p592 |
| // Optimized http://lolengine.net/blog/2013/01/13/fast-rgb-to-hsv |
| static void ImConvertColorRGBtoHSV(float r, float g, float b, float& out_h, float& out_s, float& out_v) |
| { |
| float K = 0.f; |
| if (g < b) |
| { |
| const float tmp = g; g = b; b = tmp; |
| K = -1.f; |
| } |
| if (r < g) |
| { |
| const float tmp = r; r = g; g = tmp; |
| K = -2.f / 6.f - K; |
| } |
| |
| const float chroma = r - (g < b ? g : b); |
| out_h = abs(K + (g - b) / (6.f * chroma + 1e-20f)); |
| out_s = chroma / (r + 1e-20f); |
| out_v = r; |
| } |
| |
| // Convert hsv floats ([0-1],[0-1],[0-1]) to rgb floats ([0-1],[0-1],[0-1]), from Foley & van Dam p593 |
| // also http://en.wikipedia.org/wiki/HSL_and_HSV |
| static void ImConvertColorHSVtoRGB(float h, float s, float v, float& out_r, float& out_g, float& out_b) |
| { |
| if (s == 0.0f) |
| { |
| // gray |
| out_r = out_g = out_b = v; |
| return; |
| } |
| |
| h = fmodf(h, 1.0f) / (60.0f/360.0f); |
| int i = (int)h; |
| float f = h - (float)i; |
| float p = v * (1.0f - s); |
| float q = v * (1.0f - s * f); |
| float t = v * (1.0f - s * (1.0f - f)); |
| |
| switch (i) |
| { |
| case 0: out_r = v; out_g = t; out_b = p; break; |
| case 1: out_r = q; out_g = v; out_b = p; break; |
| case 2: out_r = p; out_g = v; out_b = t; break; |
| case 3: out_r = p; out_g = q; out_b = v; break; |
| case 4: out_r = t; out_g = p; out_b = v; break; |
| case 5: default: out_r = v; out_g = p; out_b = q; break; |
| } |
| } |
| |
| //----------------------------------------------------------------------------- |
| |
| struct ImGuiColMod // Color/style modifier, backup of modified data so we can restore it |
| { |
| ImGuiCol Col; |
| ImVec4 PreviousValue; |
| }; |
| |
| struct ImGuiAabb // 2D axis aligned bounding-box |
| { |
| ImVec2 Min; |
| ImVec2 Max; |
| |
| ImGuiAabb() { Min = ImVec2(FLT_MAX,FLT_MAX); Max = ImVec2(-FLT_MAX,-FLT_MAX); } |
| ImGuiAabb(const ImVec2& min, const ImVec2& max) { Min = min; Max = max; } |
| ImGuiAabb(const ImVec4& v) { Min.x = v.x; Min.y = v.y; Max.x = v.z; Max.y = v.w; } |
| ImGuiAabb(float x1, float y1, float x2, float y2) { Min.x = x1; Min.y = y1; Max.x = x2; Max.y = y2; } |
| |
| ImVec2 GetCenter() const { return Min + (Max-Min)*0.5f; } |
| ImVec2 GetSize() const { return Max-Min; } |
| float GetWidth() const { return (Max-Min).x; } |
| float GetHeight() const { return (Max-Min).y; } |
| ImVec2 GetTL() const { return Min; } |
| ImVec2 GetTR() const { return ImVec2(Max.x,Min.y); } |
| ImVec2 GetBL() const { return ImVec2(Min.x,Max.y); } |
| ImVec2 GetBR() const { return Max; } |
| bool Contains(ImVec2 p) const { return p.x >= Min.x && p.y >= Min.y && p.x <= Max.x && p.y <= Max.y; } |
| bool Contains(const ImGuiAabb& r) const { return r.Min.x >= Min.x && r.Min.y >= Min.y && r.Max.x <= Max.x && r.Max.y <= Max.y; } |
| bool Overlaps(const ImGuiAabb& r) const { return r.Min.y <= Max.y && r.Max.y >= Min.y && r.Min.x <= Max.x && r.Max.x >= Min.x; } |
| void Expand(ImVec2 sz) { Min -= sz; Max += sz; } |
| void Clip(const ImGuiAabb& clip) { Min.x = ImMax(Min.x, clip.Min.x); Min.y = ImMax(Min.y, clip.Min.y); Max.x = ImMin(Max.x, clip.Max.x); Max.y = ImMin(Max.y, clip.Max.y); } |
| }; |
| |
| // Temporary per-window data, reset at the beginning of the frame |
| struct ImGuiDrawContext |
| { |
| ImVec2 CursorPos; |
| ImVec2 CursorPosPrevLine; |
| ImVec2 CursorStartPos; |
| float CurrentLineHeight; |
| float PrevLineHeight; |
| float LogLineHeight; |
| int TreeDepth; |
| bool LastItemHovered; |
| ImVector<ImGuiWindow*> ChildWindows; |
| ImVector<bool> AllowKeyboardFocus; |
| ImVector<float> ItemWidth; |
| ImVector<ImGuiColMod> ColorModifiers; |
| ImGuiColorEditMode ColorEditMode; |
| ImGuiStorage* StateStorage; |
| int OpenNextNode; |
| |
| float ColumnStartX; |
| int ColumnCurrent; |
| int ColumnsCount; |
| bool ColumnsShowBorders; |
| ImVec2 ColumnsStartCursorPos; |
| ImGuiID ColumnsSetID; |
| |
| ImGuiDrawContext() |
| { |
| CursorPos = CursorPosPrevLine = CursorStartPos = ImVec2(0.0f, 0.0f); |
| CurrentLineHeight = PrevLineHeight = 0.0f; |
| LogLineHeight = -1.0f; |
| TreeDepth = 0; |
| LastItemHovered = false; |
| StateStorage = NULL; |
| OpenNextNode = -1; |
| |
| ColumnStartX = 0.0f; |
| ColumnCurrent = 0; |
| ColumnsCount = 1; |
| ColumnsShowBorders = true; |
| ColumnsStartCursorPos = ImVec2(0,0); |
| } |
| }; |
| |
| struct ImGuiTextEditState; |
| #define STB_TEXTEDIT_STRING ImGuiTextEditState |
| #define STB_TEXTEDIT_CHARTYPE char |
| #include "stb_textedit.h" |
| |
| // State of the currently focused/edited text input box |
| struct ImGuiTextEditState |
| { |
| char Text[1024]; // edit buffer, we need to persist but can't guarantee the persistence of the user-provided buffer. so own buffer. |
| char InitialText[1024]; // backup of end-user buffer at focusing time, to ESC key can do a revert. Also used for arithmetic operations (but could use a pre-parsed float there). |
| size_t BufSize; // end-user buffer size, <= 1024 (or increase above) |
| float Width; // widget width |
| float ScrollX; |
| STB_TexteditState StbState; |
| float CursorAnim; |
| bool SelectedAllMouseLock; |
| ImFont Font; |
| float FontSize; |
| |
| ImGuiTextEditState() { memset(this, 0, sizeof(*this)); } |
| |
| void CursorAnimReset() { CursorAnim = -0.30f; } // After a user-input the cursor stays on for a while without blinking |
| bool CursorIsVisible() const { return CursorAnim <= 0.0f || fmodf(CursorAnim, 1.20f) <= 0.80f; } // Blinking |
| bool HasSelection() const { return StbState.select_start != StbState.select_end; } |
| void SelectAll() { StbState.select_start = 0; StbState.select_end = (int)strlen(Text); StbState.cursor = StbState.select_end; StbState.has_preferred_x = false; } |
| |
| void OnKeyboardPressed(int key); |
| void UpdateScrollOffset(); |
| ImVec2 CalcDisplayOffsetFromCharIdx(int i) const; |
| |
| // Static functions because they are used to render non-focused instances of a text input box |
| static const char* GetTextPointerClipped(ImFont font, float font_size, const char* text, float width, ImVec2* out_text_size = NULL); |
| static void RenderTextScrolledClipped(ImFont font, float font_size, const char* text, ImVec2 pos_base, float width, float scroll_x); |
| }; |
| |
| struct ImGuiIniData |
| { |
| char* Name; |
| ImVec2 Pos; |
| ImVec2 Size; |
| bool Collapsed; |
| |
| ImGuiIniData() { memset(this, 0, sizeof(*this)); } |
| ~ImGuiIniData() { if (Name) { free(Name); Name = NULL; } } |
| }; |
| |
| struct ImGuiState |
| { |
| bool Initialized; |
| ImGuiIO IO; |
| ImGuiStyle Style; |
| float Time; |
| int FrameCount; |
| int FrameCountRendered; |
| ImVector<ImGuiWindow*> Windows; |
| ImGuiWindow* CurrentWindow; // Being drawn into |
| ImVector<ImGuiWindow*> CurrentWindowStack; |
| ImGuiWindow* FocusedWindow; // Will catch keyboard inputs |
| ImGuiWindow* HoveredWindow; // Will catch mouse inputs |
| ImGuiWindow* HoveredWindowExcludingChilds; // Will catch mouse inputs (for focus/move only) |
| ImGuiID HoveredId; |
| ImGuiID ActiveId; |
| ImGuiID ActiveIdPreviousFrame; |
| bool ActiveIdIsAlive; |
| float SettingsDirtyTimer; |
| ImVector<ImGuiIniData*> Settings; |
| ImVec2 NewWindowDefaultPos; |
| |
| // Render |
| ImVector<ImDrawList*> RenderDrawLists; |
| |
| // Widget state |
| ImGuiTextEditState InputTextState; |
| ImGuiID SliderAsInputTextId; |
| ImGuiStorage ColorEditModeStorage; // for user selection |
| ImGuiID ActiveComboID; |
| char Tooltip[1024]; |
| |
| // Logging |
| bool LogEnabled; |
| FILE* LogFile; |
| ImGuiTextBuffer LogClipboard; |
| int LogAutoExpandMaxDepth; |
| |
| ImGuiState() |
| { |
| Initialized = false; |
| Time = 0.0f; |
| FrameCount = 0; |
| FrameCountRendered = -1; |
| CurrentWindow = NULL; |
| FocusedWindow = NULL; |
| HoveredWindow = NULL; |
| HoveredWindowExcludingChilds = NULL; |
| ActiveIdIsAlive = false; |
| SettingsDirtyTimer = 0.0f; |
| NewWindowDefaultPos = ImVec2(60, 60); |
| SliderAsInputTextId = 0; |
| ActiveComboID = 0; |
| memset(Tooltip, 0, sizeof(Tooltip)); |
| LogEnabled = false; |
| LogFile = NULL; |
| LogAutoExpandMaxDepth = 2; |
| } |
| }; |
| |
| static ImGuiState GImGui; |
| |
| struct ImGuiWindow |
| { |
| char* Name; |
| ImGuiID ID; |
| ImGuiWindowFlags Flags; |
| ImVec2 PosFloat; |
| ImVec2 Pos; // Position rounded-up to nearest pixel |
| ImVec2 Size; // Current size (==SizeFull or collapsed title bar size) |
| ImVec2 SizeFull; // Size when non collapsed |
| ImVec2 SizeContentsFit; // Size of contents (extents reach by the drawing cursor) - may not fit within Size. |
| float ScrollY; |
| float NextScrollY; |
| bool ScrollbarY; |
| bool Visible; |
| bool Collapsed; |
| bool Accessed; |
| int AutoFitFrames; |
| |
| ImGuiDrawContext DC; |
| ImVector<ImGuiID> IDStack; |
| ImVector<ImVec4> ClipRectStack; |
| int LastFrameDrawn; |
| float ItemWidthDefault; |
| ImGuiStorage StateStorage; |
| float FontScale; |
| |
| int FocusIdxCounter; // Start at -1 and increase as assigned via FocusItemRegister() |
| int FocusIdxRequestCurrent; // Item being requested for focus, rely on layout to be stable between the frame pressing TAB and the next frame |
| int FocusIdxRequestNext; // Item being requested for focus, for next update |
| |
| ImDrawList* DrawList; |
| |
| public: |
| ImGuiWindow(const char* name, ImVec2 default_pos, ImVec2 default_size); |
| ~ImGuiWindow(); |
| |
| ImGuiID GetID(const char* str); |
| ImGuiID GetID(const void* ptr); |
| |
| void AddToRenderList(); |
| bool FocusItemRegister(bool is_active, int* out_idx = NULL); // Return TRUE if focus is requested |
| void FocusItemUnregister(); |
| |
| ImGuiAabb Aabb() const { return ImGuiAabb(Pos, Pos+Size); } |
| ImFont Font() const { return GImGui.IO.Font; } |
| float FontSize() const { return GImGui.IO.FontHeight * FontScale; } |
| ImVec2 CursorPos() const { return DC.CursorPos; } |
| float TitleBarHeight() const { return (Flags & ImGuiWindowFlags_NoTitleBar) ? 0 : FontSize() + GImGui.Style.FramePadding.y * 2.0f; } |
| ImGuiAabb TitleBarAabb() const { return ImGuiAabb(Pos, Pos + ImVec2(SizeFull.x, TitleBarHeight())); } |
| ImVec2 WindowPadding() const { return ((Flags & ImGuiWindowFlags_ChildWindow) && !(Flags & ImGuiWindowFlags_ShowBorders)) ? ImVec2(1,1) : GImGui.Style.WindowPadding; } |
| ImU32 Color(ImGuiCol idx, float a=1.f) const { ImVec4 c = GImGui.Style.Colors[idx]; c.w *= a; return ImConvertColorFloat4ToU32(c); } |
| }; |
| |
| static ImGuiWindow* GetCurrentWindow() |
| { |
| IM_ASSERT(GImGui.CurrentWindow != NULL); // ImGui::NewFrame() hasn't been called yet? |
| GImGui.CurrentWindow->Accessed = true; |
| return GImGui.CurrentWindow; |
| } |
| |
| static void RegisterAliveId(const ImGuiID& id) |
| { |
| if (GImGui.ActiveId == id) |
| GImGui.ActiveIdIsAlive = true; |
| } |
| |
| //----------------------------------------------------------------------------- |
| |
| void ImGuiStorage::Clear() |
| { |
| Data.clear(); |
| } |
| |
| // std::lower_bound but without the bullshit |
| static ImVector<ImGuiStorage::Pair>::iterator LowerBound(ImVector<ImGuiStorage::Pair>& data, ImU32 key) |
| { |
| ImVector<ImGuiStorage::Pair>::iterator first = data.begin(); |
| ImVector<ImGuiStorage::Pair>::iterator last = data.end(); |
| int count = (int)(last - first); |
| while (count > 0) |
| { |
| int count2 = count / 2; |
| ImVector<ImGuiStorage::Pair>::iterator mid = first + count2; |
| if (mid->key < key) |
| { |
| first = ++mid; |
| count -= count2 + 1; |
| } |
| else |
| { |
| count = count2; |
| } |
| } |
| return first; |
| } |
| |
| int* ImGuiStorage::Find(ImU32 key) |
| { |
| ImVector<Pair>::iterator it = LowerBound(Data, key); |
| if (it == Data.end()) |
| return NULL; |
| if (it->key != key) |
| return NULL; |
| return &it->val; |
| } |
| |
| int ImGuiStorage::GetInt(ImU32 key, int default_val) |
| { |
| int* pval = Find(key); |
| if (!pval) |
| return default_val; |
| return *pval; |
| } |
| |
| // FIXME-OPT: We are wasting time because all SetInt() are preceeded by GetInt() calls so we should have the result from lower_bound already in place. |
| // However we only use SetInt() on explicit user action (so that's maximum once a frame) so the optimisation isn't much needed. |
| void ImGuiStorage::SetInt(ImU32 key, int val) |
| { |
| ImVector<Pair>::iterator it = LowerBound(Data, key); |
| if (it != Data.end() && it->key == key) |
| { |
| it->val = val; |
| } |
| else |
| { |
| Pair pair_key; |
| pair_key.key = key; |
| pair_key.val = val; |
| Data.insert(it, pair_key); |
| } |
| } |
| |
| void ImGuiStorage::SetAllInt(int v) |
| { |
| for (size_t i = 0; i < Data.size(); i++) |
| Data[i].val = v; |
| } |
| |
| //----------------------------------------------------------------------------- |
| |
| ImGuiTextFilter::ImGuiTextFilter() |
| { |
| InputBuf[0] = 0; |
| CountGrep = 0; |
| } |
| |
| void ImGuiTextFilter::Draw(const char* label, float width) |
| { |
| ImGuiWindow* window = GetCurrentWindow(); |
| if (width < 0.0f) |
| { |
| ImVec2 label_size = ImGui::CalcTextSize(label, NULL); |
| width = ImMax(window->Pos.x + ImGui::GetWindowContentRegionMax().x - window->DC.CursorPos.x - (label_size.x + GImGui.Style.ItemSpacing.x*4), 10.0f); |
| } |
| ImGui::PushItemWidth(width); |
| ImGui::InputText(label, InputBuf, ARRAYSIZE(InputBuf)); |
| ImGui::PopItemWidth(); |
| Build(); |
| } |
| |
| void ImGuiTextFilter::TextRange::split(char separator, ImVector<TextRange>& out) |
| { |
| out.resize(0); |
| const char* wb = b; |
| const char* we = wb; |
| while (we < e) |
| { |
| if (*we == separator) |
| { |
| out.push_back(TextRange(wb, we)); |
| wb = we + 1; |
| } |
| we++; |
| } |
| if (wb != we) |
| out.push_back(TextRange(wb, we)); |
| } |
| |
| void ImGuiTextFilter::Build() |
| { |
| Filters.resize(0); |
| TextRange input_range(InputBuf, InputBuf+strlen(InputBuf)); |
| input_range.split(',', Filters); |
| |
| CountGrep = 0; |
| for (size_t i = 0; i != Filters.size(); i++) |
| { |
| Filters[i].trim_blanks(); |
| if (Filters[i].empty()) |
| continue; |
| if (Filters[i].front() != '-') |
| CountGrep += 1; |
| } |
| } |
| |
| bool ImGuiTextFilter::PassFilter(const char* val) const |
| { |
| if (Filters.empty()) |
| return true; |
| |
| if (val == NULL) |
| val = ""; |
| |
| for (size_t i = 0; i != Filters.size(); i++) |
| { |
| const TextRange& f = Filters[i]; |
| if (f.empty()) |
| continue; |
| if (f.front() == '-') |
| { |
| // Subtract |
| if (ImStristr(val, f.begin()+1, f.end()) != NULL) |
| return false; |
| } |
| else |
| { |
| // Grep |
| if (ImStristr(val, f.begin(), f.end()) != NULL) |
| return true; |
| } |
| } |
| |
| // Implicit * grep |
| if (CountGrep == 0) |
| return true; |
| |
| return false; |
| } |
| |
| //----------------------------------------------------------------------------- |
| |
| void ImGuiTextBuffer::append(const char* fmt, ...) |
| { |
| va_list args; |
| va_start(args, fmt); |
| int len = vsnprintf(NULL, 0, fmt, args); |
| va_end(args); |
| if (len <= 0) |
| return; |
| |
| const size_t write_off = Buf.size(); |
| if (write_off + len >= Buf.capacity()) |
| Buf.reserve(Buf.capacity() * 2); |
| |
| Buf.resize(write_off + (size_t)len); |
| |
| va_start(args, fmt); |
| ImFormatStringV(&Buf[write_off] - 1, len+1, fmt, args); |
| va_end(args); |
| } |
| |
| //----------------------------------------------------------------------------- |
| |
| ImGuiWindow::ImGuiWindow(const char* name, ImVec2 default_pos, ImVec2 default_size) |
| { |
| Name = strdup(name); |
| ID = GetID(name); |
| IDStack.push_back(ID); |
| |
| PosFloat = default_pos; |
| Pos = ImVec2((float)(int)PosFloat.x, (float)(int)PosFloat.y); |
| Size = SizeFull = default_size; |
| SizeContentsFit = ImVec2(0.0f, 0.0f); |
| ScrollY = 0.0f; |
| NextScrollY = 0.0f; |
| ScrollbarY = false; |
| Visible = false; |
| Collapsed = false; |
| AutoFitFrames = -1; |
| LastFrameDrawn = -1; |
| ItemWidthDefault = 0.0f; |
| FontScale = 1.0f; |
| |
| if (ImLength(Size) < 0.001f) |
| AutoFitFrames = 3; |
| |
| FocusIdxCounter = -1; |
| FocusIdxRequestCurrent = IM_INT_MAX; |
| FocusIdxRequestNext = IM_INT_MAX; |
| |
| DrawList = new ImDrawList(); |
| } |
| |
| ImGuiWindow::~ImGuiWindow() |
| { |
| delete DrawList; |
| DrawList = NULL; |
| free(Name); |
| Name = NULL; |
| } |
| |
| ImGuiID ImGuiWindow::GetID(const char* str) |
| { |
| const ImGuiID seed = IDStack.empty() ? 0 : IDStack.back(); |
| const ImGuiID id = crc32(str, strlen(str), seed); |
| RegisterAliveId(id); |
| return id; |
| } |
| |
| ImGuiID ImGuiWindow::GetID(const void* ptr) |
| { |
| const ImGuiID seed = IDStack.empty() ? 0 : IDStack.back(); |
| const ImGuiID id = crc32(&ptr, sizeof(void*), seed); |
| RegisterAliveId(id); |
| return id; |
| } |
| |
| bool ImGuiWindow::FocusItemRegister(bool is_active, int* out_idx) |
| { |
| FocusIdxCounter++; |
| if (out_idx) |
| *out_idx = FocusIdxCounter; |
| |
| ImGuiState& g = GImGui; |
| ImGuiWindow* window = GetCurrentWindow(); |
| if (!window->DC.AllowKeyboardFocus.back()) |
| return false; |
| |
| // Process input at this point: TAB, Shift-TAB switch focus |
| if (FocusIdxRequestNext == IM_INT_MAX && is_active && ImGui::IsKeyPressedMap(ImGuiKey_Tab)) |
| { |
| // Modulo on index will be applied at the end of frame once we've got the total counter of items. |
| FocusIdxRequestNext = FocusIdxCounter + (g.IO.KeyShift ? -1 : +1); |
| } |
| |
| const bool focus_requested = (FocusIdxCounter == FocusIdxRequestCurrent); |
| return focus_requested; |
| } |
| |
| void ImGuiWindow::FocusItemUnregister() |
| { |
| FocusIdxCounter--; |
| } |
| |
| void ImGuiWindow::AddToRenderList() |
| { |
| ImGuiState& g = GImGui; |
| |
| if (!DrawList->commands.empty() && !DrawList->vtx_buffer.empty()) |
| { |
| if (DrawList->commands.back().vtx_count == 0) |
| DrawList->commands.pop_back(); |
| g.RenderDrawLists.push_back(DrawList); |
| } |
| for (size_t i = 0; i < DC.ChildWindows.size(); i++) |
| { |
| ImGuiWindow* child = DC.ChildWindows[i]; |
| IM_ASSERT(child->Visible); // Shouldn't be in this list if we are not active this frame |
| child->AddToRenderList(); |
| } |
| } |
| |
| //----------------------------------------------------------------------------- |
| |
| namespace ImGui |
| { |
| |
| static ImGuiIniData* FindWindowSettings(const char* name) |
| { |
| ImGuiState& g = GImGui; |
| |
| for (size_t i = 0; i != g.Settings.size(); i++) |
| { |
| ImGuiIniData* ini = g.Settings[i]; |
| if (ImStricmp(ini->Name, name) == 0) |
| return ini; |
| } |
| ImGuiIniData* ini = new ImGuiIniData(); |
| ini->Name = strdup(name); |
| ini->Collapsed = false; |
| ini->Pos = ImVec2(FLT_MAX,FLT_MAX); |
| ini->Size = ImVec2(0,0); |
| g.Settings.push_back(ini); |
| return ini; |
| } |
| |
| // Zero-tolerance, poor-man .ini parsing |
| // FIXME: Write something less rubbish |
| static void LoadSettings() |
| { |
| ImGuiState& g = GImGui; |
| const char* filename = g.IO.IniFilename; |
| if (!filename) |
| return; |
| |
| // Load file |
| FILE* f; |
| if ((f = fopen(filename, "rt")) == NULL) |
| return; |
| if (fseek(f, 0, SEEK_END)) |
| return; |
| long f_size = ftell(f); |
| if (f_size == -1) |
| return; |
| if (fseek(f, 0, SEEK_SET)) |
| return; |
| char* f_data = new char[f_size+1]; |
| f_size = fread(f_data, 1, f_size, f); // Text conversion alter read size so let's not be fussy about return value |
| fclose(f); |
| if (f_size == 0) |
| { |
| delete[] f_data; |
| return; |
| } |
| f_data[f_size] = 0; |
| |
| ImGuiIniData* settings = NULL; |
| const char* buf_end = f_data + f_size; |
| for (const char* line_start = f_data; line_start < buf_end; ) |
| { |
| const char* line_end = line_start; |
| while (line_end < buf_end && *line_end != '\n' && *line_end != '\r') |
| line_end++; |
| |
| if (line_start[0] == '[' && line_end > line_start && line_end[-1] == ']') |
| { |
| char name[64]; |
| ImFormatString(name, ARRAYSIZE(name), "%.*s", line_end-line_start-2, line_start+1); |
| settings = FindWindowSettings(name); |
| } |
| else if (settings) |
| { |
| float x, y; |
| int i; |
| if (sscanf(line_start, "Pos=%f,%f", &x, &y) == 2) |
| settings->Pos = ImVec2(x, y); |
| else if (sscanf(line_start, "Size=%f,%f", &x, &y) == 2) |
| settings->Size = ImMax(ImVec2(x, y), g.Style.WindowMinSize); |
| else if (sscanf(line_start, "Collapsed=%d", &i) == 1) |
| settings->Collapsed = (i != 0); |
| } |
| |
| line_start = line_end+1; |
| } |
| |
| delete[] f_data; |
| } |
| |
| static void SaveSettings() |
| { |
| ImGuiState& g = GImGui; |
| const char* filename = g.IO.IniFilename; |
| if (!filename) |
| return; |
| |
| // Gather data from windows that were active during this session |
| for (size_t i = 0; i != g.Windows.size(); i++) |
| { |
| ImGuiWindow* window = g.Windows[i]; |
| if (window->Flags & (ImGuiWindowFlags_ChildWindow | ImGuiWindowFlags_Tooltip)) |
| continue; |
| ImGuiIniData* settings = FindWindowSettings(window->Name); |
| settings->Pos = window->Pos; |
| settings->Size = window->SizeFull; |
| settings->Collapsed = window->Collapsed; |
| } |
| |
| // Write .ini file |
| // If a window wasn't opened in this session we preserve its settings |
| FILE* f = fopen(filename, "wt"); |
| if (!f) |
| return; |
| for (size_t i = 0; i != g.Settings.size(); i++) |
| { |
| const ImGuiIniData* ini = g.Settings[i]; |
| fprintf(f, "[%s]\n", ini->Name); |
| fprintf(f, "Pos=%d,%d\n", (int)ini->Pos.x, (int)ini->Pos.y); |
| fprintf(f, "Size=%d,%d\n", (int)ini->Size.x, (int)ini->Size.y); |
| fprintf(f, "Collapsed=%d\n", ini->Collapsed); |
| fprintf(f, "\n"); |
| } |
| |
| fclose(f); |
| } |
| |
| static void MarkSettingsDirty() |
| { |
| ImGuiState& g = GImGui; |
| |
| if (g.SettingsDirtyTimer <= 0.0f) |
| g.SettingsDirtyTimer = g.IO.IniSavingRate; |
| } |
| |
| ImGuiIO& GetIO() |
| { |
| return GImGui.IO; |
| } |
| |
| ImGuiStyle& GetStyle() |
| { |
| return GImGui.Style; |
| } |
| |
| void NewFrame() |
| { |
| ImGuiState& g = GImGui; |
| |
| // Check user inputs |
| IM_ASSERT(g.IO.DeltaTime > 0.0f); |
| IM_ASSERT(g.IO.DisplaySize.x > 0.0f && g.IO.DisplaySize.y > 0.0f); |
| IM_ASSERT(g.IO.RenderDrawListsFn != NULL); // Must be implemented |
| |
| if (!g.Initialized) |
| { |
| // Initialize on first frame |
| IM_ASSERT(g.Settings.empty()); |
| LoadSettings(); |
| if (!g.IO.Font) |
| { |
| // Default font |
| const void* fnt_data; |
| unsigned int fnt_size; |
| ImGui::GetDefaultFontData(&fnt_data, &fnt_size, NULL, NULL); |
| g.IO.Font = new ImBitmapFont(); |
| g.IO.Font->LoadFromMemory(fnt_data, fnt_size); |
| g.IO.FontHeight = g.IO.Font->GetFontSize(); |
| } |
| g.Initialized = true; |
| } |
| |
| g.Time += g.IO.DeltaTime; |
| g.FrameCount += 1; |
| g.Tooltip[0] = '\0'; |
| |
| // Update inputs state |
| if (g.IO.MousePos.x < 0 && g.IO.MousePos.y < 0) |
| g.IO.MousePos = ImVec2(-9999.0f, -9999.0f); |
| if ((g.IO.MousePos.x < 0 && g.IO.MousePos.y < 0) || (g.IO.MousePosPrev.x < 0 && g.IO.MousePosPrev.y < 0)) // if mouse just appeared or disappeared (negative coordinate) we cancel out movement in MouseDelta |
| g.IO.MouseDelta = ImVec2(0.0f, 0.0f); |
| else |
| g.IO.MouseDelta = g.IO.MousePos - g.IO.MousePosPrev; |
| g.IO.MousePosPrev = g.IO.MousePos; |
| for (size_t i = 0; i < ARRAYSIZE(g.IO.MouseDown); i++) |
| { |
| g.IO.MouseDownTime[i] = g.IO.MouseDown[i] ? (g.IO.MouseDownTime[i] < 0.0f ? 0.0f : g.IO.MouseDownTime[i] + g.IO.DeltaTime) : -1.0f; |
| g.IO.MouseClicked[i] = (g.IO.MouseDownTime[i] == 0.0f); |
| g.IO.MouseDoubleClicked[i] = false; |
| if (g.IO.MouseClicked[i]) |
| { |
| if (g.Time - g.IO.MouseClickedTime[i] < g.IO.MouseDoubleClickTime) |
| { |
| if (ImLength(g.IO.MousePos - g.IO.MouseClickedPos[i]) < g.IO.MouseDoubleClickMaxDist) |
| g.IO.MouseDoubleClicked[i] = true; |
| g.IO.MouseClickedTime[i] = -FLT_MAX; // so the third click isn't turned into a double-click |
| } |
| else |
| { |
| g.IO.MouseClickedTime[i] = g.Time; |
| g.IO.MouseClickedPos[i] = g.IO.MousePos; |
| } |
| } |
| } |
| for (size_t i = 0; i < ARRAYSIZE(g.IO.KeysDown); i++) |
| g.IO.KeysDownTime[i] = g.IO.KeysDown[i] ? (g.IO.KeysDownTime[i] < 0.0f ? 0.0f : g.IO.KeysDownTime[i] + g.IO.DeltaTime) : -1.0f; |
| |
| // Clear reference to active widget if the widget isn't alive anymore |
| g.HoveredId = 0; |
| if (!g.ActiveIdIsAlive && g.ActiveIdPreviousFrame == g.ActiveId && g.ActiveId != 0) |
| g.ActiveId = 0; |
| g.ActiveIdPreviousFrame = g.ActiveId; |
| g.ActiveIdIsAlive = false; |
| |
| // Delay saving settings so we don't spam disk too much |
| if (g.SettingsDirtyTimer > 0.0f) |
| { |
| g.SettingsDirtyTimer -= g.IO.DeltaTime; |
| if (g.SettingsDirtyTimer <= 0.0f) |
| SaveSettings(); |
| } |
| |
| g.HoveredWindow = ImGui::FindHoveredWindow(g.IO.MousePos, false); |
| g.HoveredWindowExcludingChilds = ImGui::FindHoveredWindow(g.IO.MousePos, true); |
| |
| // Are we snooping input? |
| g.IO.WantCaptureMouse = (g.HoveredWindow != NULL) || (g.ActiveId != 0); |
| g.IO.WantCaptureKeyboard = (g.ActiveId != 0); |
| |
| // Scale & Scrolling |
| if (g.HoveredWindow && g.IO.MouseWheel != 0) |
| { |
| ImGuiWindow* window = g.HoveredWindow; |
| if (g.IO.KeyCtrl) |
| { |
| if (g.IO.FontAllowScaling) |
| { |
| // Zoom / Scale window |
| float new_font_scale = ImClamp(window->FontScale + g.IO.MouseWheel * 0.10f, 0.50f, 2.50f); |
| float scale = new_font_scale / window->FontScale; |
| window->FontScale = new_font_scale; |
| |
| ImVec2 offset = window->Size * (1.0f - scale) * (g.IO.MousePos - window->Pos) / window->Size; |
| window->Pos += offset; |
| window->PosFloat += offset; |
| window->Size *= scale; |
| window->SizeFull *= scale; |
| } |
| } |
| else |
| { |
| // Scroll |
| window->NextScrollY -= g.IO.MouseWheel * window->FontSize() * 5.0f; |
| } |
| } |
| |
| // Pressing TAB activate widget focus |
| // NB: Don't discard FocusedWindow if it isn't active, so that a window that go on/off programatically won't lose its keyboard focus. |
| if (g.ActiveId == 0 && g.FocusedWindow != NULL && g.FocusedWindow->Visible && IsKeyPressedMap(ImGuiKey_Tab, false)) |
| { |
| g.FocusedWindow->FocusIdxRequestNext = 0; |
| } |
| |
| // Mark all windows as not visible |
| for (size_t i = 0; i != g.Windows.size(); i++) |
| g.Windows[i]->Visible = false; |
| |
| // Create implicit window |
| // We will only render it if the user has added something to it. |
| IM_ASSERT(g.CurrentWindowStack.empty()); // No window should be open at the beginning of the frame! |
| ImGui::Begin("Debug", NULL, ImVec2(400,400)); |
| } |
| |
| // NB: behaviour of ImGui after Shutdown() is not tested/guaranteed at the moment. This function is merely here to free heap allocations. |
| void Shutdown() |
| { |
| ImGuiState& g = GImGui; |
| if (!g.Initialized) |
| return; |
| |
| SaveSettings(); |
| |
| for (size_t i = 0; i < g.Windows.size(); i++) |
| delete g.Windows[i]; |
| g.Windows.clear(); |
| g.CurrentWindowStack.clear(); |
| g.FocusedWindow = NULL; |
| g.HoveredWindow = NULL; |
| g.HoveredWindowExcludingChilds = NULL; |
| for (size_t i = 0; i < g.Settings.size(); i++) |
| delete g.Settings[i]; |
| g.Settings.clear(); |
| g.ColorEditModeStorage.Clear(); |
| if (g.LogFile && g.LogFile != stdout) |
| { |
| fclose(g.LogFile); |
| g.LogFile = NULL; |
| } |
| if (g.IO.Font) |
| { |
| delete g.IO.Font; |
| g.IO.Font = NULL; |
| } |
| |
| g.Initialized = false; |
| } |
| |
| static void AddWindowToSortedBuffer(ImGuiWindow* window, ImVector<ImGuiWindow*>& sorted_windows) |
| { |
| sorted_windows.push_back(window); |
| if (window->Visible) |
| { |
| for (size_t i = 0; i < window->DC.ChildWindows.size(); i++) |
| { |
| ImGuiWindow* child = window->DC.ChildWindows[i]; |
| AddWindowToSortedBuffer(child, sorted_windows); |
| } |
| } |
| } |
| |
| static void PushClipRect(const ImVec4& clip_rect, bool clipped = true) |
| { |
| ImGuiWindow* window = GetCurrentWindow(); |
| |
| ImVec4 cr = clip_rect; |
| if (clipped && !window->ClipRectStack.empty()) |
| { |
| // Clip to new clip rect |
| const ImVec4 cur_cr = window->ClipRectStack.back(); |
| cr = ImVec4(ImMax(cr.x, cur_cr.x), ImMax(cr.y, cur_cr.y), ImMin(cr.z, cur_cr.z), ImMin(cr.w, cur_cr.w)); |
| } |
| |
| window->ClipRectStack.push_back(cr); |
| window->DrawList->PushClipRect(cr); |
| } |
| |
| static void PopClipRect() |
| { |
| ImGuiWindow* window = GetCurrentWindow(); |
| window->ClipRectStack.pop_back(); |
| window->DrawList->PopClipRect(); |
| } |
| |
| void Render() |
| { |
| ImGuiState& g = GImGui; |
| IM_ASSERT(g.Initialized); // Forgot to call ImGui::NewFrame() |
| |
| const bool first_render_of_the_frame = (g.FrameCountRendered != g.FrameCount); |
| g.FrameCountRendered = g.FrameCount; |
| |
| if (first_render_of_the_frame) |
| { |
| // Hide implicit window if it hasn't been used |
| IM_ASSERT(g.CurrentWindowStack.size() == 1); // Mismatched Begin/End |
| if (g.CurrentWindow && !g.CurrentWindow->Accessed) |
| g.CurrentWindow->Visible = false; |
| ImGui::End(); |
| |
| // Sort the window list so that all child windows are after their parent |
| // When cannot do that on FocusWindow() because childs may not exist yet |
| ImVector<ImGuiWindow*> sorted_windows; |
| sorted_windows.reserve(g.Windows.size()); |
| for (size_t i = 0; i != g.Windows.size(); i++) |
| { |
| ImGuiWindow* window = g.Windows[i]; |
| if (window->Flags & ImGuiWindowFlags_ChildWindow) // if a child is visible its parent will add it |
| if (window->Visible) |
| continue; |
| AddWindowToSortedBuffer(window, sorted_windows); |
| } |
| IM_ASSERT(g.Windows.size() == sorted_windows.size()); // We done something wrong |
| g.Windows.swap(sorted_windows); |
| |
| // Clear data for next frame |
| g.IO.MouseWheel = 0; |
| memset(g.IO.InputCharacters, 0, sizeof(g.IO.InputCharacters)); |
| } |
| |
| // Gather windows to render |
| g.RenderDrawLists.resize(0); |
| for (size_t i = 0; i != g.Windows.size(); i++) |
| { |
| ImGuiWindow* window = g.Windows[i]; |
| if (!window->Visible) |
| continue; |
| if (window->Flags & ImGuiWindowFlags_ChildWindow) |
| continue; |
| window->AddToRenderList(); |
| } |
| |
| // Render tooltip |
| if (g.Tooltip[0]) |
| { |
| // Use a dummy window to render the tooltip |
| ImGui::Begin("##Tooltip", NULL, ImVec2(0,0), 0.0f, ImGuiWindowFlags_NoTitleBar|ImGuiWindowFlags_NoResize|ImGuiWindowFlags_Tooltip); |
| ImGuiWindow* window = GetCurrentWindow(); |
| //window->DrawList->Clear(); |
| ImGui::PushClipRect(ImVec4(-9999,-9999,+9999,+9999), false); |
| const ImVec2 text_size = CalcTextSize(g.Tooltip, NULL, false); |
| const ImVec2 pos = g.IO.MousePos + ImVec2(32,16); |
| const ImGuiAabb bb(pos - g.Style.FramePadding*2, pos + text_size + g.Style.FramePadding*2); |
| ImGui::RenderFrame(bb.Min, bb.Max, window->Color(ImGuiCol_TooltipBg), false, g.Style.WindowRounding); |
| ImGui::RenderText(pos, g.Tooltip, NULL, false); |
| ImGui::PopClipRect(); |
| ImGui::End(); |
| window->AddToRenderList(); |
| } |
| |
| // Render |
| if (!g.RenderDrawLists.empty()) |
| g.IO.RenderDrawListsFn(&g.RenderDrawLists[0], (int)g.RenderDrawLists.size()); |
| g.RenderDrawLists.resize(0); |
| } |
| |
| // Find the optional ## from which we stop displaying text. |
| static const char* FindTextDisplayEnd(const char* text, const char* text_end = NULL) |
| { |
| const char* text_display_end = text; |
| while ((!text_end || text_display_end < text_end) && *text_display_end != '\0' && (text_display_end[0] != '#' || text_display_end[1] != '#')) |
| text_display_end++; |
| return text_display_end; |
| } |
| |
| static void LogText(const ImVec2& ref_pos, const char* text, const char* text_end) |
| { |
| ImGuiState& g = GImGui; |
| ImGuiWindow* window = GetCurrentWindow(); |
| |
| if (!text_end) |
| text_end = FindTextDisplayEnd(text, text_end); |
| |
| const bool log_new_line = ref_pos.y > window->DC.LogLineHeight+1; |
| window->DC.LogLineHeight = ref_pos.y; |
| |
| const char* text_remaining = text; |
| const int tree_depth = window->DC.TreeDepth; |
| while (true) |
| { |
| const char* line_end = text_remaining; |
| while (line_end < text_end) |
| if (*line_end == '\n') |
| break; |
| else |
| line_end++; |
| if (line_end >= text_end) |
| line_end = NULL; |
| |
| bool is_first_line = (text == text_remaining); |
| bool is_last_line = false; |
| if (line_end == NULL) |
| { |
| is_last_line = true; |
| line_end = text_end; |
| } |
| if (line_end != NULL && !(is_last_line && (line_end - text_remaining)==0)) |
| { |
| const int char_count = (int)(line_end - text_remaining); |
| if (g.LogFile) |
| { |
| if (log_new_line || !is_first_line) |
| fprintf(g.LogFile, "\n%*s%.*s", tree_depth*4, "", char_count, text_remaining); |
| else |
| fprintf(g.LogFile, " %.*s", char_count, text_remaining); |
| } |
| else |
| { |
| if (log_new_line || !is_first_line) |
| g.LogClipboard.append("\n%*s%.*s", tree_depth*4, "", char_count, text_remaining); |
| else |
| g.LogClipboard.append(" %.*s", char_count, text_remaining); |
| } |
| } |
| |
| if (is_last_line) |
| break; |
| text_remaining = line_end + 1; |
| } |
| } |
| |
| static void RenderText(ImVec2 pos, const char* text, const char* text_end, const bool hide_text_after_hash) |
| { |
| ImGuiState& g = GImGui; |
| ImGuiWindow* window = GetCurrentWindow(); |
| |
| // Hide anything after a '##' string |
| const char* text_display_end; |
| if (hide_text_after_hash) |
| { |
| text_display_end = FindTextDisplayEnd(text, text_end); |
| } |
| else |
| { |
| if (!text_end) |
| text_end = text + strlen(text); |
| text_display_end = text_end; |
| } |
| |
| const int text_len = (int)(text_display_end - text); |
| //IM_ASSERT(text_len >= 0 && text_len < 10000); // Suspicious text length |
| if (text_len > 0) |
| { |
| // Render |
| window->DrawList->AddText(window->Font(), window->FontSize(), pos, window->Color(ImGuiCol_Text), text, text + text_len); |
| |
| // Log as text. We split text into individual lines to add the tree level padding |
| if (g.LogEnabled) |
| LogText(pos, text, text_display_end); |
| } |
| } |
| |
| static void RenderFrame(ImVec2 p_min, ImVec2 p_max, ImU32 fill_col, bool border, float rounding) |
| { |
| ImGuiWindow* window = GetCurrentWindow(); |
| |
| window->DrawList->AddRectFilled(p_min, p_max, fill_col, rounding); |
| if (border && (window->Flags & ImGuiWindowFlags_ShowBorders)) |
| { |
| window->DrawList->AddRect(p_min+ImVec2(1,1), p_max+ImVec2(1,1), window->Color(ImGuiCol_BorderShadow), rounding); |
| window->DrawList->AddRect(p_min, p_max, window->Color(ImGuiCol_Border), rounding); |
| } |
| } |
| |
| static void RenderCollapseTriangle(ImVec2 p_min, bool open, float scale = 1.0f, bool shadow = false) |
| { |
| ImGuiWindow* window = GetCurrentWindow(); |
| |
| const float h = window->FontSize() * 1.00f; |
| const float r = h * 0.40f * scale; |
| ImVec2 center = p_min + ImVec2(h*0.50f, h*0.50f*scale); |
| |
| ImVec2 a, b, c; |
| if (open) |
| { |
| center.y -= r*0.25f; |
| a = center + ImVec2(0,1)*r; |
| b = center + ImVec2(-0.866f,-0.5f)*r; |
| c = center + ImVec2(0.866f,-0.5f)*r; |
| } |
| else |
| { |
| a = center + ImVec2(1,0)*r; |
| b = center + ImVec2(-0.500f,0.866f)*r; |
| c = center + ImVec2(-0.500f,-0.866f)*r; |
| } |
| |
| if (shadow) |
| window->DrawList->AddTriangleFilled(a+ImVec2(2,2), b+ImVec2(2,2), c+ImVec2(2,2), window->Color(ImGuiCol_BorderShadow)); |
| window->DrawList->AddTriangleFilled(a, b, c, window->Color(ImGuiCol_Border)); |
| } |
| |
| static ImVec2 CalcTextSize(const char* text, const char* text_end, const bool hide_text_after_hash) |
| { |
| ImGuiWindow* window = GetCurrentWindow(); |
| |
| const char* text_display_end; |
| if (hide_text_after_hash) |
| text_display_end = FindTextDisplayEnd(text, text_end); // Hide anything after a '##' string |
| else |
| text_display_end = text_end; |
| |
| const ImVec2 size = window->Font()->CalcTextSize(window->FontSize(), 0, text, text_display_end, NULL); |
| return size; |
| } |
| |
| static ImGuiWindow* FindHoveredWindow(ImVec2 pos, bool excluding_childs) |
| { |
| ImGuiState& g = GImGui; |
| for (int i = (int)g.Windows.size()-1; i >= 0; i--) |
| { |
| ImGuiWindow* window = g.Windows[(size_t)i]; |
| if (!window->Visible) |
| continue; |
| if (excluding_childs && (window->Flags & ImGuiWindowFlags_ChildWindow) != 0) |
| continue; |
| ImGuiAabb bb(window->Pos - g.Style.TouchExtraPadding, window->Pos+window->Size + g.Style.TouchExtraPadding); |
| if (bb.Contains(pos)) |
| return window; |
| } |
| return NULL; |
| } |
| |
| // - Box is clipped by our current clip setting |
| // - Expand to be generous on unprecise inputs systems (touch) |
| static bool IsMouseHoveringBox(const ImGuiAabb& box) |
| { |
| ImGuiState& g = GImGui; |
| ImGuiWindow* window = GetCurrentWindow(); |
| |
| // Clip |
| ImGuiAabb box_clipped = box; |
| if (!window->ClipRectStack.empty()) |
| { |
| const ImVec4 clip_rect = window->ClipRectStack.back(); |
| box_clipped.Clip(ImGuiAabb(ImVec2(clip_rect.x, clip_rect.y), ImVec2(clip_rect.z, clip_rect.w))); |
| } |
| |
| // Expand for touch input |
| ImGuiAabb box_for_touch(box_clipped.Min - g.Style.TouchExtraPadding, box_clipped.Max + g.Style.TouchExtraPadding); |
| return box_for_touch.Contains(g.IO.MousePos); |
| } |
| |
| bool IsMouseHoveringBox(const ImVec2& box_min, const ImVec2& box_max) |
| { |
| return IsMouseHoveringBox(ImGuiAabb(box_min, box_max)); |
| } |
| |
| static bool IsKeyPressedMap(ImGuiKey key, bool repeat) |
| { |
| ImGuiState& g = GImGui; |
| const int key_index = g.IO.KeyMap[key]; |
| return IsKeyPressed(key_index, repeat); |
| } |
| |
| bool IsKeyPressed(int key_index, bool repeat) |
| { |
| ImGuiState& g = GImGui; |
| IM_ASSERT(key_index >= 0 && key_index < ARRAYSIZE(g.IO.KeysDown)); |
| const float t = g.IO.KeysDownTime[key_index]; |
| if (t == 0.0f) |
| return true; |
| |
| // FIXME: Repeat rate should be provided elsewhere? |
| const float KEY_REPEAT_DELAY = 0.250f; |
| const float KEY_REPEAT_RATE = 0.020f; |
| if (repeat && t > KEY_REPEAT_DELAY) |
| if ((fmodf(t - KEY_REPEAT_DELAY, KEY_REPEAT_RATE) > KEY_REPEAT_RATE*0.5f) != (fmodf(t - KEY_REPEAT_DELAY - g.IO.DeltaTime, KEY_REPEAT_RATE) > KEY_REPEAT_RATE*0.5f)) |
| return true; |
| |
| return false; |
| } |
| |
| bool IsMouseClicked(int button, bool repeat) |
| { |
| ImGuiState& g = GImGui; |
| IM_ASSERT(button >= 0 && button < ARRAYSIZE(g.IO.MouseDown)); |
| const float t = g.IO.MouseDownTime[button]; |
| if (t == 0.0f) |
| return true; |
| |
| // FIXME: Repeat rate should be provided elsewhere? |
| const float MOUSE_REPEAT_DELAY = 0.250f; |
| const float MOUSE_REPEAT_RATE = 0.020f; |
| if (repeat && t > MOUSE_REPEAT_DELAY) |
| if ((fmodf(t - MOUSE_REPEAT_DELAY, MOUSE_REPEAT_RATE) > MOUSE_REPEAT_RATE*0.5f) != (fmodf(t - MOUSE_REPEAT_DELAY - g.IO.DeltaTime, MOUSE_REPEAT_RATE) > MOUSE_REPEAT_RATE*0.5f)) |
| return true; |
| |
| return false; |
| } |
| |
| bool IsMouseDoubleClicked(int button) |
| { |
| ImGuiState& g = GImGui; |
| IM_ASSERT(button >= 0 && button < ARRAYSIZE(g.IO.MouseDown)); |
| return g.IO.MouseDoubleClicked[button]; |
| } |
| |
| ImVec2 GetMousePos() |
| { |
| return GImGui.IO.MousePos; |
| } |
| |
| bool IsHovered() |
| { |
| ImGuiWindow* window = GetCurrentWindow(); |
| return window->DC.LastItemHovered; |
| } |
| |
| void SetTooltip(const char* fmt, ...) |
| { |
| ImGuiState& g = GImGui; |
| va_list args; |
| va_start(args, fmt); |
| ImFormatStringV(g.Tooltip, ARRAYSIZE(g.Tooltip), fmt, args); |
| va_end(args); |
| } |
| |
| void SetNewWindowDefaultPos(ImVec2 pos) |
| { |
| ImGuiState& g = GImGui; |
| g.NewWindowDefaultPos = pos; |
| } |
| |
| float GetTime() |
| { |
| return GImGui.Time; |
| } |
| |
| int GetFrameCount() |
| { |
| return GImGui.FrameCount; |
| } |
| |
| static ImGuiWindow* FindWindow(const char* name) |
| { |
| ImGuiState& g = GImGui; |
| for (size_t i = 0; i != g.Windows.size(); i++) |
| if (strcmp(g.Windows[i]->Name, name) == 0) |
| return g.Windows[i]; |
| return NULL; |
| } |
| |
| void BeginChild(const char* str_id, ImVec2 size, bool border, ImGuiWindowFlags extra_flags) |
| { |
| ImGuiState& g = GImGui; |
| ImGuiWindow* window = GetCurrentWindow(); |
| |
| ImGuiWindowFlags flags = ImGuiWindowFlags_NoTitleBar|ImGuiWindowFlags_NoMove|ImGuiWindowFlags_NoResize|ImGuiWindowFlags_ChildWindow; |
| |
| const ImVec2 content_max = window->Pos + ImGui::GetWindowContentRegionMax(); |
| const ImVec2 cursor_pos = window->Pos + ImGui::GetCursorPos(); |
| if (size.x <= 0.0f) |
| { |
| size.x = ImMax(content_max.x - cursor_pos.x, g.Style.WindowMinSize.x); |
| flags |= ImGuiWindowFlags_ChildWindowAutoFitX; |
| } |
| if (size.y <= 0.0f) |
| { |
| size.y = ImMax(content_max.y - cursor_pos.y, g.Style.WindowMinSize.y); |
| flags |= ImGuiWindowFlags_ChildWindowAutoFitY; |
| } |
| if (border) |
| flags |= ImGuiWindowFlags_ShowBorders; |
| flags |= extra_flags; |
| |
| char title[256]; |
| ImFormatString(title, ARRAYSIZE(title), "%s.%s", window->Name, str_id); |
| |
| const float alpha = (flags & ImGuiWindowFlags_ComboBox) ? 1.0f : 0.0f; |
| ImGui::Begin(title, NULL, size, alpha, flags); |
| |
| if (!(window->Flags & ImGuiWindowFlags_ShowBorders)) |
| g.CurrentWindow->Flags &= ~ImGuiWindowFlags_ShowBorders; |
| } |
| |
| void EndChild() |
| { |
| ImGuiWindow* window = GetCurrentWindow(); |
| |
| if (window->Flags & ImGuiWindowFlags_ComboBox) |
| { |
| ImGui::End(); |
| } |
| else |
| { |
| // When using filling child window, we don't provide the width/height to ItemSize so that it doesn't feed back into automatic fitting |
| ImVec2 sz = ImGui::GetWindowSize(); |
| if (window->Flags & ImGuiWindowFlags_ChildWindowAutoFitX) |
| sz.x = 0; |
| if (window->Flags & ImGuiWindowFlags_ChildWindowAutoFitY) |
| sz.y = 0; |
| |
| ImGui::End(); |
| ImGui::ItemSize(sz); |
| } |
| } |
| |
| bool Begin(const char* name, bool* open, ImVec2 size, float fill_alpha, ImGuiWindowFlags flags) |
| { |
| ImGuiState& g = GImGui; |
| const ImGuiStyle& style = g.Style; |
| |
| ImGuiWindow* window = FindWindow(name); |
| if (!window) |
| { |
| if (flags & ImGuiWindowFlags_ChildWindow) |
| { |
| window = new ImGuiWindow(name, ImVec2(0,0), size); |
| } |
| else |
| { |
| ImGuiIniData* settings = FindWindowSettings(name); |
| if (settings && ImLength(settings->Size) > 0.0f && !(flags & ImGuiWindowFlags_NoResize))// && ImLengthsize) == 0.0f) |
| size = settings->Size; |
| |
| window = new ImGuiWindow(name, g.NewWindowDefaultPos, size); |
| |
| if (settings->Pos.x != FLT_MAX) |
| { |
| window->PosFloat = settings->Pos; |
| window->Pos = ImVec2((float)(int)window->PosFloat.x, (float)(int)window->PosFloat.y); |
| window->Collapsed = settings->Collapsed; |
| } |
| } |
| g.Windows.push_back(window); |
| } |
| window->Flags = (ImGuiWindowFlags)flags; |
| |
| g.CurrentWindowStack.push_back(window); |
| g.CurrentWindow = window; |
| |
| // Default alpha |
| if (fill_alpha < 0.0f) |
| fill_alpha = style.WindowFillAlphaDefault; |
| |
| // When reusing window again multiple times a frame, just append content (don't need to setup again) |
| const int current_frame = ImGui::GetFrameCount(); |
| const bool first_begin_of_the_frame = (window->LastFrameDrawn != current_frame); |
| if (first_begin_of_the_frame) |
| { |
| // New windows appears in front |
| if (window->LastFrameDrawn < current_frame - 1) |
| ImGui::FocusWindow(window); |
| |
| window->DrawList->Clear(); |
| window->Visible = true; |
| window->LastFrameDrawn = current_frame; |
| window->ClipRectStack.resize(0); |
| |
| if (flags & ImGuiWindowFlags_ChildWindow) |
| { |
| ImGuiWindow* parent_window = g.CurrentWindowStack[g.CurrentWindowStack.size()-2]; |
| parent_window->DC.ChildWindows.push_back(window); |
| window->Pos = window->PosFloat = parent_window->DC.CursorPos; |
| window->SizeFull = size; |
| if (!(flags & ImGuiWindowFlags_ComboBox)) |
| ImGui::PushClipRect(parent_window->ClipRectStack.back()); |
| } |
| else |
| { |
| ImGui::PushClipRect(ImVec4(0.0f, 0.0f, g.IO.DisplaySize.x, g.IO.DisplaySize.y)); |
| } |
| |
| // ID stack |
| window->IDStack.resize(0); |
| ImGui::PushID(window); |
| |
| // Move window (at the beginning of the frame) |
| const ImGuiID move_id = window->GetID("#MOVE"); |
| RegisterAliveId(move_id); |
| if (g.ActiveId == move_id) |
| { |
| if (g.IO.MouseDown[0]) |
| { |
| if (!(window->Flags & ImGuiWindowFlags_NoMove)) |
| { |
| window->PosFloat += g.IO.MouseDelta; |
| MarkSettingsDirty(); |
| } |
| ImGui::FocusWindow(window); |
| } |
| else |
| { |
| g.ActiveId = 0; |
| } |
| } |
| |
| // Clamp into view |
| if (!(window->Flags & ImGuiWindowFlags_ChildWindow)) |
| { |
| const ImVec2 pad = ImVec2(window->FontSize()*2.0f, window->FontSize()*2.0f); |
| window->PosFloat = ImMax(window->PosFloat + window->Size, pad) - window->Size; |
| window->PosFloat = ImMin(window->PosFloat, ImVec2(g.IO.DisplaySize.x, g.IO.DisplaySize.y) - pad); |
| window->Pos = ImVec2((float)(int)window->PosFloat.x, (float)(int)window->PosFloat.y); |
| window->SizeFull = ImMax(window->SizeFull, pad); |
| } |
| else |
| { |
| window->Pos = ImVec2((float)(int)window->PosFloat.x, (float)(int)window->PosFloat.y); |
| } |
| window->ItemWidthDefault = (float)(int)(window->Size.x > 0.0f ? window->Size.x * 0.65f : 250.0f); |
| |
| // Prepare for focus requests |
| if (window->FocusIdxRequestNext == IM_INT_MAX || window->FocusIdxCounter == -1) |
| { |
| window->FocusIdxRequestCurrent = IM_INT_MAX; |
| } |
| else |
| { |
| const int mod = window->FocusIdxCounter+1; |
| window->FocusIdxRequestCurrent = (window->FocusIdxRequestNext + mod) % mod; |
| } |
| window->FocusIdxCounter = -1; |
| window->FocusIdxRequestNext = IM_INT_MAX; |
| |
| ImGuiAabb title_bar_aabb = window->TitleBarAabb(); |
| |
| // Apply and ImClamp scrolling |
| window->ScrollY = window->NextScrollY; |
| window->ScrollY = ImMax(window->ScrollY, 0.0f); |
| if (!window->Collapsed) |
| window->ScrollY = ImMin(window->ScrollY, ImMax(0.0f, (float)window->SizeContentsFit.y - window->SizeFull.y)); |
| window->NextScrollY = window->ScrollY; |
| |
| // NB- at this point we don't have a clipping rectangle setup yet! |
| // Collapse window by double-clicking on title bar |
| if (!(window->Flags & ImGuiWindowFlags_NoTitleBar)) |
| { |
| if (g.HoveredWindow == window && IsMouseHoveringBox(title_bar_aabb) && g.IO.MouseDoubleClicked[0]) |
| { |
| window->Collapsed = !window->Collapsed; |
| MarkSettingsDirty(); |
| ImGui::FocusWindow(window); |
| } |
| } |
| else |
| { |
| window->Collapsed = false; |
| } |
| |
| if (window->Collapsed) |
| { |
| // Title bar only |
| window->Size = title_bar_aabb.GetSize(); |
| window->DrawList->AddRectFilled(title_bar_aabb.GetTL(), title_bar_aabb.GetBR(), window->Color(ImGuiCol_TitleBgCollapsed), g.Style.WindowRounding); |
| if (window->Flags & ImGuiWindowFlags_ShowBorders) |
| { |
| window->DrawList->AddRect(title_bar_aabb.GetTL()+ImVec2(1,1), title_bar_aabb.GetBR()+ImVec2(1,1), window->Color(ImGuiCol_BorderShadow), g.Style.WindowRounding); |
| window->DrawList->AddRect(title_bar_aabb.GetTL(), title_bar_aabb.GetBR(), window->Color(ImGuiCol_Border), g.Style.WindowRounding); |
| } |
| } |
| else |
| { |
| window->Size = window->SizeFull; |
| |
| // Draw resize grip |
| ImU32 resize_col = 0; |
| if (!(window->Flags & ImGuiWindowFlags_NoResize)) |
| { |
| const ImGuiAabb resize_aabb(window->Aabb().GetBR()-ImVec2(18,18), window->Aabb().GetBR()); |
| const ImGuiID resize_id = window->GetID("#RESIZE"); |
| bool hovered, held; |
| ButtonBehaviour(resize_aabb, resize_id, &hovered, &held); |
| resize_col = window->Color(held ? ImGuiCol_ResizeGripActive : hovered ? ImGuiCol_ResizeGripHovered : ImGuiCol_ResizeGrip); |
| |
| const ImVec2 size_auto_fit = ImClamp(window->SizeContentsFit + style.AutoFitPadding, style.WindowMinSize, g.IO.DisplaySize - style.AutoFitPadding); |
| if (window->AutoFitFrames > 0) |
| { |
| // Auto-fit only grows during the first few frames |
| window->SizeFull = ImMax(window->SizeFull, size_auto_fit); |
| MarkSettingsDirty(); |
| } |
| else if (g.HoveredWindow == window && held && g.IO.MouseDoubleClicked[0]) |
| { |
| // Manual auto-fit |
| window->SizeFull = size_auto_fit; |
| window->Size = window->SizeFull; |
| MarkSettingsDirty(); |
| } |
| else if (held) |
| { |
| // Resize |
| window->SizeFull = ImMax(window->SizeFull + g.IO.MouseDelta, style.WindowMinSize); |
| window->Size = window->SizeFull; |
| MarkSettingsDirty(); |
| } |
| |
| // Update aabb immediately so that the rendering below isn't one frame late |
| title_bar_aabb = window->TitleBarAabb(); |
| } |
| |
| // Title bar + Window box |
| if (fill_alpha > 0.0f) |
| { |
| if ((window->Flags & ImGuiWindowFlags_ComboBox) != 0) |
| window->DrawList->AddRectFilled(window->Pos, window->Pos+window->Size, window->Color(ImGuiCol_ComboBg, fill_alpha), 0); |
| else |
| window->DrawList->AddRectFilled(window->Pos, window->Pos+window->Size, window->Color(ImGuiCol_WindowBg, fill_alpha), g.Style.WindowRounding); |
| } |
| |
| if (!(window->Flags & ImGuiWindowFlags_NoTitleBar)) |
| window->DrawList->AddRectFilled(title_bar_aabb.GetTL(), title_bar_aabb.GetBR(), window->Color(ImGuiCol_TitleBg), g.Style.WindowRounding, 1|2); |
| |
| if (window->Flags & ImGuiWindowFlags_ShowBorders) |
| { |
| const float rounding = (window->Flags & ImGuiWindowFlags_ComboBox) ? 0.0f : g.Style.WindowRounding; |
| window->DrawList->AddRect(window->Pos+ImVec2(1,1), window->Pos+window->Size+ImVec2(1,1), window->Color(ImGuiCol_BorderShadow), rounding); |
| window->DrawList->AddRect(window->Pos, window->Pos+window->Size, window->Color(ImGuiCol_Border), rounding); |
| if (!(window->Flags & ImGuiWindowFlags_NoTitleBar)) |
| window->DrawList->AddLine(title_bar_aabb.GetBL(), title_bar_aabb.GetBR(), window->Color(ImGuiCol_Border)); |
| } |
| |
| // Scrollbar |
| window->ScrollbarY = (window->SizeContentsFit.y > window->Size.y) && !(window->Flags & ImGuiWindowFlags_NoScrollbar); |
| if (window->ScrollbarY) |
| { |
| ImGuiAabb scrollbar_bb(window->Aabb().Max.x - style.ScrollBarWidth, title_bar_aabb.Max.y+1, window->Aabb().Max.x, window->Aabb().Max.y-1); |
| //window->DrawList->AddLine(scrollbar_bb.GetTL(), scrollbar_bb.GetBL(), g.Colors[ImGuiCol_Border]); |
| window->DrawList->AddRectFilled(scrollbar_bb.Min, scrollbar_bb.Max, window->Color(ImGuiCol_ScrollbarBg)); |
| scrollbar_bb.Expand(ImVec2(-3,-3)); |
| |
| const float grab_size_y_norm = ImSaturate(window->Size.y / ImMax(window->SizeContentsFit.y, window->Size.y)); |
| const float grab_size_y = scrollbar_bb.GetHeight() * grab_size_y_norm; |
| |
| // Handle input right away (none of the code above is relying on scrolling) |
| bool held = false; |
| bool hovered = false; |
| if (grab_size_y_norm < 1.0f) |
| { |
| const ImGuiID scrollbar_id = window->GetID("#SCROLLY"); |
| ButtonBehaviour(scrollbar_bb, scrollbar_id, &hovered, &held); |
| if (held) |
| { |
| g.HoveredId = scrollbar_id; |
| const float pos_y_norm = ImSaturate((g.IO.MousePos.y - (scrollbar_bb.Min.y + grab_size_y*0.5f)) / (scrollbar_bb.GetHeight() - grab_size_y)) * (1.0f - grab_size_y_norm); |
| window->ScrollY = pos_y_norm * window->SizeContentsFit.y; |
| window->NextScrollY = window->ScrollY; |
| } |
| } |
| |
| // Normalized height of the grab |
| const float pos_y_norm = ImSaturate(window->ScrollY / ImMax(0.0f, window->SizeContentsFit.y)); |
| const ImU32 grab_col = window->Color(held ? ImGuiCol_ScrollbarGrabActive : hovered ? ImGuiCol_ScrollbarGrabHovered : ImGuiCol_ScrollbarGrab); |
| window->DrawList->AddRectFilled( |
| ImVec2(scrollbar_bb.Min.x, ImLerp(scrollbar_bb.Min.y, scrollbar_bb.Max.y, pos_y_norm)), |
| ImVec2(scrollbar_bb.Max.x, ImLerp(scrollbar_bb.Min.y, scrollbar_bb.Max.y, pos_y_norm + grab_size_y_norm)), grab_col); |
| } |
| |
| // Render resize grip |
| // (after the input handling so we don't have a frame of latency) |
| if (!(window->Flags & ImGuiWindowFlags_NoResize)) |
| { |
| const float r = style.WindowRounding; |
| const ImVec2 br = window->Aabb().GetBR(); |
| if (r == 0.0f) |
| { |
| window->DrawList->AddTriangleFilled(br, br-ImVec2(0,14), br-ImVec2(14,0), resize_col); |
| } |
| else |
| { |
| // FIXME: We should draw 4 triangles and decide on a size that's not dependant on the rounding size (previously used 18) |
| window->DrawList->AddArc(br - ImVec2(r,r), r, resize_col, 6, 9, true); |
| window->DrawList->AddTriangleFilled(br+ImVec2(0,-2*r),br+ImVec2(0,-r),br+ImVec2(-r,-r), resize_col); |
| window->DrawList->AddTriangleFilled(br+ImVec2(-r,-r), br+ImVec2(-r,0),br+ImVec2(-2*r,0), resize_col); |
| } |
| } |
| } |
| |
| // Setup drawing context |
| window->DC.ColumnStartX = window->WindowPadding().x; |
| window->DC.CursorStartPos = window->Pos + ImVec2(window->DC.ColumnStartX, window->TitleBarHeight() + window->WindowPadding().y) - ImVec2(0.0f, window->ScrollY); |
| window->DC.CursorPos = window->DC.CursorStartPos; |
| window->DC.CursorPosPrevLine = window->DC.CursorPos; |
| window->DC.CurrentLineHeight = window->DC.PrevLineHeight = 0.0f; |
| window->DC.LogLineHeight = window->DC.CursorPos.y - 9999.0f; |
| window->DC.ChildWindows.resize(0); |
| window->DC.ItemWidth.resize(0); |
| window->DC.ItemWidth.push_back(window->ItemWidthDefault); |
| window->DC.AllowKeyboardFocus.resize(0); |
| window->DC.AllowKeyboardFocus.push_back(true); |
| window->DC.ColorModifiers.resize(0); |
| window->DC.ColorEditMode = ImGuiColorEditMode_UserSelect; |
| window->DC.ColumnCurrent = 0; |
| window->DC.ColumnsCount = 1; |
| window->DC.TreeDepth = 0; |
| window->DC.StateStorage = &window->StateStorage; |
| window->DC.OpenNextNode = -1; |
| |
| // Reset contents size for auto-fitting |
| window->SizeContentsFit = ImVec2(0.0f, 0.0f); |
| if (window->AutoFitFrames > 0) |
| window->AutoFitFrames--; |
| |
| // Title bar |
| if (!(window->Flags & ImGuiWindowFlags_NoTitleBar)) |
| { |
| RenderCollapseTriangle(window->Pos + style.FramePadding, !window->Collapsed, 1.0f, true); |
| RenderText(window->Pos + style.FramePadding + ImVec2(window->FontSize() + style.ItemInnerSpacing.x, 0), name); |
| if (open) |
| ImGui::CloseWindowButton(open); |
| } |
| } |
| |
| // Clip rectangle |
| // We set this up after processing the resize grip so that our clip rectangle doesn't lag by a frame |
| const ImGuiAabb title_bar_aabb = window->TitleBarAabb(); |
| ImVec4 clip_rect(title_bar_aabb.Min.x+0.5f, title_bar_aabb.Max.y+0.5f, window->Aabb().Max.x-1.5f, window->Aabb().Max.y-1.5f); |
| if (window->ScrollbarY) |
| clip_rect.z -= g.Style.ScrollBarWidth; |
| ImGui::PushClipRect(clip_rect); |
| |
| if (first_begin_of_the_frame) |
| { |
| // Clear 'accessed' flag last thing |
| window->Accessed = false; |
| } |
| |
| // Return collapsed so that user can perform an early out optimisation |
| return !window->Collapsed; |
| } |
| |
| void End() |
| { |
| ImGuiState& g = GImGui; |
| ImGuiWindow* window = g.CurrentWindow; |
| |
| ImGui::Columns(1, "#CloseColumns"); |
| ImGui::PopClipRect(); |
| if (window->Flags & ImGuiWindowFlags_ChildWindow) |
| { |
| if (!(window->Flags & ImGuiWindowFlags_ComboBox)) |
| ImGui::PopClipRect(); |
| } |
| else |
| { |
| ImGui::PopClipRect(); |
| } |
| |
| // Select window for move/focus when we're done with all our widgets |
| ImGuiAabb bb(window->Pos, window->Pos+window->Size); |
| if (g.ActiveId == 0 && g.HoveredId == 0 && g.HoveredWindowExcludingChilds == window && IsMouseHoveringBox(bb) && g.IO.MouseClicked[0]) |
| g.ActiveId = window->GetID("#MOVE"); |
| |
| // Stop logging |
| if (!(window->Flags & ImGuiWindowFlags_ChildWindow)) // FIXME: more options for scope of logging |
| { |
| g.LogEnabled = false; |
| if (g.LogFile != NULL) |
| { |
| fprintf(g.LogFile, "\n"); |
| if (g.LogFile == stdout) |
| fflush(g.LogFile); |
| else |
| fclose(g.LogFile); |
| g.LogFile = NULL; |
| } |
| if (g.LogClipboard.size() > 1) |
| { |
| g.LogClipboard.append("\n"); |
| if (g.IO.SetClipboardTextFn) |
| g.IO.SetClipboardTextFn(g.LogClipboard.begin(), g.LogClipboard.end()); |
| g.LogClipboard.clear(); |
| } |
| } |
| |
| // Pop |
| g.CurrentWindowStack.pop_back(); |
| g.CurrentWindow = g.CurrentWindowStack.empty() ? NULL : g.CurrentWindowStack.back(); |
| } |
| |
| static void FocusWindow(ImGuiWindow* window) |
| { |
| ImGuiState& g = GImGui; |
| g.FocusedWindow = window; |
| |
| // Move to front |
| for (size_t i = 0; i < g.Windows.size(); i++) |
| if (g.Windows[i] == window) |
| { |
| g.Windows.erase(g.Windows.begin() + i); |
| break; |
| } |
| g.Windows.push_back(window); |
| } |
| |
| void PushItemWidth(float item_width) |
| { |
| ImGuiWindow* window = GetCurrentWindow(); |
| item_width = (float)(int)item_width; |
| window->DC.ItemWidth.push_back(item_width > 0.0f ? item_width : window->ItemWidthDefault); |
| } |
| |
| void PopItemWidth() |
| { |
| ImGuiWindow* window = GetCurrentWindow(); |
| window->DC.ItemWidth.pop_back(); |
| } |
| |
| float GetItemWidth() |
| { |
| ImGuiWindow* window = GetCurrentWindow(); |
| return window->DC.ItemWidth.back(); |
| } |
| |
| void PushAllowKeyboardFocus(bool allow_keyboard_focus) |
| { |
| ImGuiWindow* window = GetCurrentWindow(); |
| window->DC.AllowKeyboardFocus.push_back(allow_keyboard_focus); |
| } |
| |
| void PopAllowKeyboardFocus() |
| { |
| ImGuiWindow* window = GetCurrentWindow(); |
| window->DC.AllowKeyboardFocus.pop_back(); |
| } |
| |
| void PushStyleColor(ImGuiCol idx, ImVec4 col) |
| { |
| ImGuiState& g = GImGui; |
| ImGuiWindow* window = GetCurrentWindow(); |
| |
| ImGuiColMod backup; |
| backup.Col = idx; |
| backup.PreviousValue = g.Style.Colors[idx]; |
| window->DC.ColorModifiers.push_back(backup); |
| g.Style.Colors[idx] = col; |
| } |
| |
| void PopStyleColor() |
| { |
| ImGuiState& g = GImGui; |
| ImGuiWindow* window = GetCurrentWindow(); |
| |
| ImGuiColMod& backup = window->DC.ColorModifiers.back(); |
| g.Style.Colors[backup.Col] = backup.PreviousValue; |
| window->DC.ColorModifiers.pop_back(); |
| } |
| |
| const char* GetStyleColorName(ImGuiCol idx) |
| { |
| // Create with regexp: ImGuiCol_{.*}, --> case ImGuiCol_\1: return "\1"; |
| switch (idx) |
| { |
| case ImGuiCol_Text: return "Text"; |
| case ImGuiCol_WindowBg: return "WindowBg"; |
| case ImGuiCol_Border: return "Border"; |
| case ImGuiCol_BorderShadow: return "BorderShadow"; |
| case ImGuiCol_FrameBg: return "FrameBg"; |
| case ImGuiCol_TitleBg: return "TitleBg"; |
| case ImGuiCol_TitleBgCollapsed: return "TitleBgCollapsed"; |
| case ImGuiCol_ScrollbarBg: return "ScrollbarBg"; |
| case ImGuiCol_ScrollbarGrab: return "ScrollbarGrab"; |
| case ImGuiCol_ScrollbarGrabHovered: return "ScrollbarGrabHovered"; |
| case ImGuiCol_ScrollbarGrabActive: return "ScrollbarGrabActive"; |
| case ImGuiCol_ComboBg: return "ComboBg"; |
| case ImGuiCol_CheckHovered: return "CheckHovered"; |
| case ImGuiCol_CheckActive: return "CheckActive"; |
| case ImGuiCol_SliderGrab: return "SliderGrab"; |
| case ImGuiCol_SliderGrabActive: return "SliderGrabActive"; |
| case ImGuiCol_Button: return "Button"; |
| case ImGuiCol_ButtonHovered: return "ButtonHovered"; |
| case ImGuiCol_ButtonActive: return "ButtonActive"; |
| case ImGuiCol_Header: return "Header"; |
| case ImGuiCol_HeaderHovered: return "HeaderHovered"; |
| case ImGuiCol_HeaderActive: return "HeaderActive"; |
| case ImGuiCol_Column: return "Column"; |
| case ImGuiCol_ColumnHovered: return "ColumnHovered"; |
| case ImGuiCol_ColumnActive: return "ColumnActive"; |
| case ImGuiCol_ResizeGrip: return "ResizeGrip"; |
| case ImGuiCol_ResizeGripHovered: return "ResizeGripHovered"; |
| case ImGuiCol_ResizeGripActive: return "ResizeGripActive"; |
| case ImGuiCol_CloseButton: return "CloseButton"; |
| case ImGuiCol_CloseButtonHovered: return "CloseButtonHovered"; |
| case ImGuiCol_CloseButtonActive: return "CloseButtonActive"; |
| case ImGuiCol_PlotLines: return "PlotLines"; |
| case ImGuiCol_PlotLinesHovered: return "PlotLinesHovered"; |
| case ImGuiCol_PlotHistogram: return "PlotHistogram"; |
| case ImGuiCol_PlotHistogramHovered: return "ImGuiCol_PlotHistogramHovered"; |
| case ImGuiCol_TextSelectedBg: return "TextSelectedBg"; |
| case ImGuiCol_TooltipBg: return "TooltipBg"; |
| } |
| IM_ASSERT(0); |
| return "Unknown"; |
| } |
| |
| bool GetWindowIsFocused() |
| { |
| ImGuiState& g = GImGui; |
| ImGuiWindow* window = GetCurrentWindow(); |
| return g.FocusedWindow == window; |
| } |
| |
| float GetWindowWidth() |
| { |
| ImGuiWindow* window = GetCurrentWindow(); |
| return window->Size.x; |
| } |
| |
| ImVec2 GetWindowPos() |
| { |
| ImGuiWindow* window = GetCurrentWindow(); |
| return window->Pos; |
| } |
| |
| void SetWindowPos(ImVec2 pos) |
| { |
| ImGuiWindow* window = GetCurrentWindow(); |
| window->Pos = pos; |
| } |
| |
| ImVec2 GetWindowSize() |
| { |
| ImGuiWindow* window = GetCurrentWindow(); |
| return window->Size; |
| } |
| |
| ImVec2 GetWindowContentRegionMin() |
| { |
| ImGuiWindow* window = GetCurrentWindow(); |
| return ImVec2(0, window->TitleBarHeight()) + window->WindowPadding(); |
| } |
| |
| ImVec2 GetWindowContentRegionMax() |
| { |
| ImGuiWindow* window = GetCurrentWindow(); |
| ImVec2 m = window->Size - window->WindowPadding(); |
| if (window->ScrollbarY) |
| m.x -= GImGui.Style.ScrollBarWidth; |
| return m; |
| } |
| |
| float GetTextLineHeight() |
| { |
| ImGuiWindow* window = GetCurrentWindow(); |
| return window->FontSize(); |
| } |
| |
| float GetTextLineSpacing() |
| { |
| ImGuiState& g = GImGui; |
| ImGuiWindow* window = GetCurrentWindow(); |
| return window->FontSize() + g.Style.ItemSpacing.y; |
| } |
| |
| ImDrawList* GetWindowDrawList() |
| { |
| ImGuiWindow* window = GetCurrentWindow(); |
| return window->DrawList; |
| } |
| |
| void SetFontScale(float scale) |
| { |
| ImGuiWindow* window = GetCurrentWindow(); |
| window->FontScale = scale; |
| } |
| |
| ImVec2 GetCursorPos() |
| { |
| ImGuiWindow* window = GetCurrentWindow(); |
| return window->DC.CursorPos - window->Pos; |
| } |
| |
| void SetCursorPos(ImVec2 p) |
| { |
| ImGuiWindow* window = GetCurrentWindow(); |
| window->DC.CursorPos = window->Pos + p; |
| } |
| |
| void SetScrollPosHere() |
| { |
| ImGuiWindow* window = GetCurrentWindow(); |
| window->NextScrollY = (window->DC.CursorPos.y + window->ScrollY) - (window->Pos.y + window->SizeFull.y * 0.5f) - (window->TitleBarHeight() + window->WindowPadding().y); |
| } |
| |
| void SetTreeStateStorage(ImGuiStorage* tree) |
| { |
| ImGuiWindow* window = GetCurrentWindow(); |
| window->DC.StateStorage = tree ? tree : &window->StateStorage; |
| } |
| |
| ImGuiStorage* GetTreeStateStorage() |
| { |
| ImGuiWindow* window = GetCurrentWindow(); |
| return window->DC.StateStorage; |
| } |
| |
| void TextV(const char* fmt, va_list args) |
| { |
| ImGuiWindow* window = GetCurrentWindow(); |
| if (window->Collapsed) |
| return; |
| |
| static char buf[1024]; |
| const char* text_end = buf + ImFormatStringV(buf, ARRAYSIZE(buf), fmt, args); |
| TextUnformatted(buf, text_end); |
| } |
| |
| void Text(const char* fmt, ...) |
| { |
| va_list args; |
| va_start(args, fmt); |
| TextV(fmt, args); |
| va_end(args); |
| } |
| |
| void TextUnformatted(const char* text, const char* text_end) |
| { |
| ImGuiState& g = GImGui; |
| ImGuiWindow* window = GetCurrentWindow(); |
| if (window->Collapsed) |
| return; |
| |
| const char* text_begin = text; |
| if (text_end == NULL) |
| text_end = text + strlen(text); |
| |
| if (text_end - text > 2000) |
| { |
| // Long text! |
| // Perform manual coarse clipping to optimize for long multi-line text |
| // From this point we will only compute the width of lines that are visible. |
| const char* line = text; |
| const float line_height = ImGui::GetTextLineHeight(); |
| const ImVec2 start_pos = window->DC.CursorPos; |
| const ImVec4 clip_rect = window->ClipRectStack.back(); |
| ImVec2 text_size(0,0); |
| |
| if (start_pos.y <= clip_rect.w) |
| { |
| ImVec2 pos = start_pos; |
| |
| // lines to skip (can't skip when logging text) |
| if (!g.LogEnabled) |
| { |
| int lines_skippable = (int)((clip_rect.y - start_pos.y) / line_height) - 1; |
| if (lines_skippable > 0) |
| { |
| int lines_skipped = 0; |
| while (line < text_end && lines_skipped <= lines_skippable) |
| { |
| const char* line_end = strchr(line, '\n'); |
| line = line_end + 1; |
| lines_skipped++; |
| } |
| pos.y += lines_skipped * line_height; |
| } |
| } |
| |
| // lines to render? |
| if (line < text_end) |
| { |
| ImGuiAabb line_box(pos, pos + ImVec2(ImGui::GetWindowWidth(), line_height)); |
| while (line < text_end) |
| { |
| const char* line_end = strchr(line, '\n'); |
| if (ImGui::IsClipped(line_box)) |
| break; |
| |
| const ImVec2 line_size = CalcTextSize(line, line_end, false); |
| text_size.x = ImMax(text_size.x, line_size.x); |
| RenderText(pos, line, line_end, false); |
| if (!line_end) |
| line_end = text_end; |
| line = line_end + 1; |
| line_box.Min.y += line_height; |
| line_box.Max.y += line_height; |
| pos.y += line_height; |
| } |
| |
| // count remaining lines |
| int lines_skipped = 0; |
| while (line < text_end) |
| { |
| const char* line_end = strchr(line, '\n'); |
| if (!line_end) |
| line_end = text_end; |
| line = line_end + 1; |
| lines_skipped++; |
| } |
| pos.y += lines_skipped * line_height; |
| } |
| |
| text_size.y += (pos - start_pos).y; |
| } |
| const ImGuiAabb bb(window->DC.CursorPos, window->DC.CursorPos + text_size); |
| ItemSize(bb); |
| ClipAdvance(bb); |
| } |
| else |
| { |
| const ImVec2 text_size = CalcTextSize(text_begin, text_end, false); |
| ImGuiAabb bb(window->DC.CursorPos, window->DC.CursorPos + text_size); |
| ItemSize(bb.GetSize(), &bb.Min); |
| |
| if (ClipAdvance(bb)) |
| return; |
| |
| // Render |
| // We don't hide text after # in this end-user function. |
| RenderText(bb.Min, text_begin, text_end, false); |
| } |
| } |
| |
| void AlignFirstTextHeightToWidgets() |
| { |
| ImGuiState& g = GImGui; |
| ImGuiWindow* window = GetCurrentWindow(); |
| if (window->Collapsed) |
| return; |
| |
| // Declare a dummy item size to that upcoming items that are smaller will center-align on the newly expanded line height. |
| ImGui::ItemSize(ImVec2(0, window->FontSize() + g.Style.FramePadding.y*2)); |
| ImGui::SameLine(0, 0); |
| } |
| |
| void LabelText(const char* label, const char* fmt, ...) |
| { |
| ImGuiState& g = GImGui; |
| ImGuiWindow* window = GetCurrentWindow(); |
| if (window->Collapsed) |
| return; |
| const ImGuiStyle& style = g.Style; |
| const float w = window->DC.ItemWidth.back(); |
| |
| static char buf[1024]; |
| va_list args; |
| va_start(args, fmt); |
| const char* text_begin = &buf[0]; |
| const char* text_end = text_begin + ImFormatStringV(buf, ARRAYSIZE(buf), fmt, args); |
| va_end(args); |
| |
| const ImVec2 text_size = CalcTextSize(label); |
| const ImGuiAabb value_bb(window->DC.CursorPos, window->DC.CursorPos + ImVec2(w + style.FramePadding.x*2, text_size.y)); |
| const ImGuiAabb bb(window->DC.CursorPos, window->DC.CursorPos + ImVec2(w + style.FramePadding.x*2 + style.ItemInnerSpacing.x, 0.0f) + text_size); |
| ItemSize(bb); |
| |
| if (ClipAdvance(value_bb)) |
| return; |
| |
| // Render |
| RenderText(value_bb.Min, text_begin, text_end); |
| RenderText(ImVec2(value_bb.Max.x + style.ItemInnerSpacing.x, value_bb.Min.y), label); |
| } |
| |
| static bool ButtonBehaviour(const ImGuiAabb& bb, const ImGuiID& id, bool* out_hovered, bool* out_held, bool repeat) |
| { |
| ImGuiState& g = GImGui; |
| ImGuiWindow* window = GetCurrentWindow(); |
| |
| const bool hovered = (g.HoveredWindow == window) && (g.HoveredId == 0) && IsMouseHoveringBox(bb); |
| bool pressed = false; |
| if (hovered) |
| { |
| g.HoveredId = id; |
| if (g.IO.MouseClicked[0]) |
| { |
| g.ActiveId = id; |
| } |
| else if (repeat && g.ActiveId && ImGui::IsMouseClicked(0, true)) |
| { |
| pressed = true; |
| } |
| } |
| |
| bool held = false; |
| if (g.ActiveId == id) |
| { |
| if (g.IO.MouseDown[0]) |
| { |
| held = true; |
| } |
| else |
| { |
| if (hovered) |
| pressed = true; |
| g.ActiveId = 0; |
| } |
| } |
| |
| if (out_hovered) *out_hovered = hovered; |
| if (out_held) *out_held = held; |
| |
| return pressed; |
| } |
| |
| bool Button(const char* label, ImVec2 size, bool repeat_when_held) |
| { |
| ImGuiState& g = GImGui; |
| ImGuiWindow* window = GetCurrentWindow(); |
| if (window->Collapsed) |
| return false; |
| |
| const ImGuiStyle& style = g.Style; |
| const ImGuiID id = window->GetID(label); |
| |
| const ImVec2 text_size = CalcTextSize(label); |
| if (size.x == 0.0f) |
| size.x = text_size.x; |
| if (size.y == 0.0f) |
| size.y = text_size.y; |
| |
| const ImGuiAabb bb(window->DC.CursorPos, window->DC.CursorPos+size + style.FramePadding*2.0f); |
| ItemSize(bb); |
| |
| if (ClipAdvance(bb)) |
| return false; |
| |
| bool hovered, held; |
| bool pressed = ButtonBehaviour(bb, id, &hovered, |