| // dear imgui, v1.51 WIP |
| // (main code and documentation) |
| |
| // See ImGui::ShowTestWindow() in imgui_demo.cpp for demo code. |
| // Newcomers, read 'Programmer guide' below for notes on how to setup ImGui in your codebase. |
| // Get latest version at https://github.com/ocornut/imgui |
| // Releases change-log at https://github.com/ocornut/imgui/releases |
| // Gallery (please post your screenshots/video there!): https://github.com/ocornut/imgui/issues/772 |
| // Developed by Omar Cornut and every direct or indirect contributors to the GitHub. |
| // This library is free but I need your support to sustain development and maintenance. |
| // If you work for a company, please consider financial support, e.g: https://www.patreon.com/imgui |
| |
| /* |
| |
| Index |
| - MISSION STATEMENT |
| - END-USER GUIDE |
| - PROGRAMMER GUIDE (read me!) |
| - Read first |
| - How to update to a newer version of ImGui |
| - Getting started with integrating ImGui in your code/engine |
| - API BREAKING CHANGES (read me when you update!) |
| - ISSUES & TODO LIST |
| - FREQUENTLY ASKED QUESTIONS (FAQ), TIPS |
| - How can I help? |
| - What is ImTextureID and how do I display an image? |
| - I integrated ImGui in my engine and the text or lines are blurry.. |
| - I integrated ImGui in my engine and some elements are clipping or disappearing when I move windows around.. |
| - How can I have multiple widgets with the same label? Can I have widget without a label? (Yes). A primer on labels/IDs. |
| - How can I tell when ImGui wants my mouse/keyboard inputs VS when I can pass them to my application? |
| - How can I load a different font than the default? |
| - How can I easily use icons in my application? |
| - How can I load multiple fonts? |
| - How can I display and input non-latin characters such as Chinese, Japanese, Korean, Cyrillic? |
| - How can I preserve my ImGui context across reloading a DLL? (loss of the global/static variables) |
| - How can I use the drawing facilities without an ImGui window? (using ImDrawList API) |
| - ISSUES & TODO-LIST |
| - CODE |
| |
| |
| MISSION STATEMENT |
| ================= |
| |
| - Easy to use to create code-driven and data-driven tools |
| - Easy to use to create ad hoc short-lived tools and long-lived, more elaborate tools |
| - Easy to hack and improve |
| - Minimize screen real-estate usage |
| - Minimize setup and maintenance |
| - Minimize state storage on user side |
| - Portable, minimize dependencies, run on target (consoles, phones, etc.) |
| - Efficient runtime and memory consumption (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) |
| |
| 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 |
| |
| |
| END-USER GUIDE |
| ============== |
| |
| - Double-click title bar to collapse window |
| - Click upper right corner to close a window, available when 'bool* p_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 or drag box 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!) |
| - Controls are automatically adjusted for OSX to match standard OSX text editing operations. |
| |
| |
| PROGRAMMER GUIDE |
| ================ |
| |
| READ FIRST |
| |
| - Read the FAQ below this section! |
| - Your code creates the UI, if your code doesn't run the UI is gone! == very dynamic UI, no construction/destructions steps, less data retention on your side, no state duplication, less sync, less bugs. |
| - Call and read ImGui::ShowTestWindow() for demo code demonstrating most features. |
| - You can learn about immediate-mode gui principles at http://www.johno.se/book/imgui.html or watch http://mollyrocket.com/861 |
| |
| HOW TO UPDATE TO A NEWER VERSION OF IMGUI |
| |
| - Overwrite all the sources files except for imconfig.h (if you have made modification to your copy of imconfig.h) |
| - Read the "API BREAKING CHANGES" section (below). This is where we list occasional API breaking changes. |
| If a function/type has been renamed / or marked obsolete, try to fix the name in your code before it is permanently removed from the public API. |
| If you have a problem with a missing function/symbols, search for its name in the code, there will likely be a comment about it. |
| Please report any issue to the GitHub page! |
| - Try to keep your copy of dear imgui reasonably up to date. |
| |
| GETTING STARTED WITH INTEGRATING IMGUI IN YOUR CODE/ENGINE |
| |
| - Add the ImGui source files to your projects, using your preferred build system. It is recommended you build the .cpp files as part of your project and not as a library. |
| - You can later customize the imconfig.h file to tweak some compilation time behavior, such as integrating imgui types with your own maths types. |
| - See examples/ folder for standalone sample applications. To understand the integration process, you can read examples/opengl2_example/ because it is short, |
| then switch to the one more appropriate to your use case. |
| - You may be able to grab and copy a ready made imgui_impl_*** file from the examples/. |
| - When using ImGui, your programming IDE if your friend: follow the declaration of variables, functions and types to find comments about them. |
| |
| - Init: retrieve the ImGuiIO structure with ImGui::GetIO() and fill the fields marked 'Settings': at minimum you need to set io.DisplaySize (application resolution). |
| Later on you will fill your keyboard mapping, clipboard handlers, and other advanced features but for a basic integration you don't need to worry about it all. |
| - Init: call io.Fonts->GetTexDataAsRGBA32(...), it will build the font atlas texture, then load the texture pixels into graphics memory. |
| - Every frame: |
| - In your main loop as early a possible, fill the IO fields marked 'Input' (e.g. mouse position, buttons, keyboard info, etc.) |
| - Call ImGui::NewFrame() to begin the imgui frame |
| - You can use any ImGui function you want between NewFrame() and Render() |
| - Call ImGui::Render() as late as you can to end the frame and finalize render data. it will call your io.RenderDrawListFn handler. |
| (if you don't need to render, you still need to call Render() and ignore the callback, or call EndFrame() instead. if you call neither some aspects of windows focusing/moving will appear broken.) |
| - All rendering information are stored into command-lists until ImGui::Render() is called. |
| - ImGui never touches or knows about your GPU state. the only function that knows about GPU is the RenderDrawListFn handler that you provide. |
| - Effectively it means you can create widgets at any time in your code, regardless of considerations of being in "update" vs "render" phases of your own application. |
| - Refer to the examples applications in the examples/ folder for instruction on how to setup your code. |
| - A minimal application skeleton may be: |
| |
| // Application init |
| ImGuiIO& io = ImGui::GetIO(); |
| io.DisplaySize.x = 1920.0f; |
| io.DisplaySize.y = 1280.0f; |
| io.RenderDrawListsFn = MyRenderFunction; // Setup a render function, or set to NULL and call GetDrawData() after Render() to access the render data. |
| // TODO: Fill others settings of the io structure later. |
| |
| // Load texture atlas (there is a default font so you don't need to care about choosing a font yet) |
| unsigned char* pixels; |
| int width, height; |
| io.Fonts->GetTexDataAsRGBA32(pixels, &width, &height); |
| // TODO: At this points you've got the texture data and you need to upload that your your graphic system: |
| MyTexture* texture = MyEngine::CreateTextureFromMemoryPixels(pixels, width, height, TEXTURE_TYPE_RGBA) |
| // TODO: Store your texture pointer/identifier (whatever your engine uses) in 'io.Fonts->TexID'. This will be passed back to your via the renderer. |
| io.Fonts->TexID = (void*)texture; |
| |
| // Application main loop |
| while (true) |
| { |
| // Setup low-level inputs (e.g. on Win32, GetKeyboardState(), or write to those fields from your Windows message loop handlers, etc.) |
| ImGuiIO& io = ImGui::GetIO(); |
| io.DeltaTime = 1.0f/60.0f; |
| io.MousePos = mouse_pos; |
| io.MouseDown[0] = mouse_button_0; |
| io.MouseDown[1] = mouse_button_1; |
| |
| // Call NewFrame(), after this point you can use ImGui::* functions anytime |
| ImGui::NewFrame(); |
| |
| // Most of your application code here |
| MyGameUpdate(); // may use any ImGui functions, e.g. ImGui::Begin("My window"); ImGui::Text("Hello, world!"); ImGui::End(); |
| MyGameRender(); // may use any ImGui functions as well! |
| |
| // Render & swap video buffers |
| ImGui::Render(); |
| SwapBuffers(); |
| } |
| |
| - A minimal render function skeleton may be: |
| |
| void void MyRenderFunction(ImDrawData* draw_data)(ImDrawData* draw_data) |
| { |
| // TODO: Setup render state: alpha-blending enabled, no face culling, no depth testing, scissor enabled |
| // TODO: Setup viewport, orthographic projection matrix |
| // TODO: Setup shader: vertex { float2 pos, float2 uv, u32 color }, fragment shader sample color from 1 texture, multiply by vertex color. |
| for (int n = 0; n < draw_data->CmdListsCount; n++) |
| { |
| const ImDrawVert* vtx_buffer = cmd_list->VtxBuffer.Data; // vertex buffer generated by ImGui |
| const ImDrawIdx* idx_buffer = cmd_list->IdxBuffer.Data; // index buffer generated by ImGui |
| for (int cmd_i = 0; cmd_i < cmd_list->CmdBuffer.Size; cmd_i++) |
| { |
| const ImDrawCmd* pcmd = &cmd_list->CmdBuffer[cmd_i]; |
| if (pcmd->UserCallback) |
| { |
| pcmd->UserCallback(cmd_list, pcmd); |
| } |
| else |
| { |
| // Render 'pcmd->ElemCount/3' texture triangles |
| MyEngineBindTexture(pcmd->TextureId); |
| MyEngineScissor((int)pcmd->ClipRect.x, (int)pcmd->ClipRect.y, (int)(pcmd->ClipRect.z - pcmd->ClipRect.x), (int)(pcmd->ClipRect.w - pcmd->ClipRect.y)); |
| MyEngineDrawIndexedTriangles(pcmd->ElemCount, sizeof(ImDrawIdx) == 2 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT, idx_buffer, vtx_buffer); |
| } |
| idx_buffer += pcmd->ElemCount; |
| } |
| } |
| } |
| |
| - The examples/ folders contains many functional implementation of the pseudo-code above. |
| - When calling NewFrame(), the 'io.WantCaptureMouse'/'io.WantCaptureKeyboard'/'io.WantTextInput' flags are updated. |
| They tell you if ImGui intends to use your inputs. So for example, if 'io.WantCaptureMouse' is set you would typically want to hide |
| mouse inputs from the rest of your application. Read the FAQ below for more information about those flags. |
| |
| |
| |
| API BREAKING CHANGES |
| ==================== |
| |
| Occasionally introducing changes that are breaking the API. The breakage are generally minor and easy to fix. |
| Here is a change-log of API breaking changes, if you are using one of the functions listed, expect to have to fix some code. |
| Also read releases logs https://github.com/ocornut/imgui/releases for more details. |
| |
| - 2017/08/22 (1.51) - renamed IsItemHoveredRect() to IsItemRectHovered(). Kept inline redirection function (will obsolete). |
| - renamed IsMouseHoveringAnyWindow() to IsAnyWindowHovered() for consistency. Kept inline redirection function (will obsolete). |
| - renamed IsMouseHoveringWindow() to IsWindowRectHovered() for consistency. Kept inline redirection function (will obsolete). |
| - 2017/08/20 (1.51) - renamed GetStyleColName() to GetStyleColorName() for consistency. |
| - 2017/08/20 (1.51) - added PushStyleColor(ImGuiCol idx, ImU32 col) overload, which _might_ cause an "ambiguous call" compilation error if you are using ImColor() with implicit cast. Cast to ImU32 or ImVec4 explicily to fix. |
| - 2017/08/15 (1.51) - marked the weird IMGUI_ONCE_UPON_A_FRAME helper macro as obsolete. prefer using the more explicit ImGuiOnceUponAFrame. |
| - 2017/08/15 (1.51) - changed parameter order for BeginPopupContextWindow() from (const char*,int buttons,bool also_over_items) to (const char*,int buttons,bool also_over_items). Note that most calls relied on default parameters completely. |
| - 2017/08/13 (1.51) - renamed ImGuiCol_Columns*** to ImGuiCol_Separator***. Kept redirection enums (will obsolete). |
| - 2017/08/11 (1.51) - renamed ImGuiSetCond_*** types and flags to ImGuiCond_***. Kept redirection enums (will obsolete). |
| - 2017/08/09 (1.51) - removed ValueColor() helpers, they are equivalent to calling Text(label) + SameLine() + ColorButton(). |
| - 2017/08/08 (1.51) - removed ColorEditMode() and ImGuiColorEditMode in favor of ImGuiColorEditFlags and parameters to the various Color*() functions. The SetColorEditOptions() allows to initialize default but the user can still change them with right-click context menu. |
| - changed prototype of 'ColorEdit4(const char* label, float col[4], bool show_alpha = true)' to 'ColorEdit4(const char* label, float col[4], ImGuiColorEditFlags flags = 0)', where passing flags = 0x01 is a safe no-op (hello dodgy backward compatibility!). - check and run the demo window, under "Color/Picker Widgets", to understand the various new options. |
| - changed prototype of rarely used 'ColorButton(ImVec4 col, bool small_height = false, bool outline_border = true)' to 'ColorButton(const char* desc_id, ImVec4 col, ImGuiColorEditFlags flags = 0, ImVec2 size = ImVec2(0,0))' |
| - 2017/07/20 (1.51) - removed IsPosHoveringAnyWindow(ImVec2), which was partly broken and misleading. ASSERT + redirect user to io.WantCaptureMouse |
| - 2017/05/26 (1.50) - removed ImFontConfig::MergeGlyphCenterV in favor of a more multipurpose ImFontConfig::GlyphOffset. |
| - 2017/05/01 (1.50) - renamed ImDrawList::PathFill() (rarely used directly) to ImDrawList::PathFillConvex() for clarity. |
| - 2016/11/06 (1.50) - BeginChild(const char*) now applies the stack id to the provided label, consistently with other functions as it should always have been. It shouldn't affect you unless (extremely unlikely) you were appending multiple times to a same child from different locations of the stack id. If that's the case, generate an id with GetId() and use it instead of passing string to BeginChild(). |
| - 2016/10/15 (1.50) - avoid 'void* user_data' parameter to io.SetClipboardTextFn/io.GetClipboardTextFn pointers. We pass io.ClipboardUserData to it. |
| - 2016/09/25 (1.50) - style.WindowTitleAlign is now a ImVec2 (ImGuiAlign enum was removed). set to (0.5f,0.5f) for horizontal+vertical centering, (0.0f,0.0f) for upper-left, etc. |
| - 2016/07/30 (1.50) - SameLine(x) with x>0.0f is now relative to left of column/group if any, and not always to left of window. This was sort of always the intent and hopefully breakage should be minimal. |
| - 2016/05/12 (1.49) - title bar (using ImGuiCol_TitleBg/ImGuiCol_TitleBgActive colors) isn't rendered over a window background (ImGuiCol_WindowBg color) anymore. |
| If your TitleBg/TitleBgActive alpha was 1.0f or you are using the default theme it will not affect you. |
| However if your TitleBg/TitleBgActive alpha was <1.0f you need to tweak your custom theme to readjust for the fact that we don't draw a WindowBg background behind the title bar. |
| This helper function will convert an old TitleBg/TitleBgActive color into a new one with the same visual output, given the OLD color and the OLD WindowBg color. |
| ImVec4 ConvertTitleBgCol(const ImVec4& win_bg_col, const ImVec4& title_bg_col) |
| { |
| float new_a = 1.0f - ((1.0f - win_bg_col.w) * (1.0f - title_bg_col.w)), k = title_bg_col.w / new_a; |
| return ImVec4((win_bg_col.x * win_bg_col.w + title_bg_col.x) * k, (win_bg_col.y * win_bg_col.w + title_bg_col.y) * k, (win_bg_col.z * win_bg_col.w + title_bg_col.z) * k, new_a); |
| } |
| If this is confusing, pick the RGB value from title bar from an old screenshot and apply this as TitleBg/TitleBgActive. Or you may just create TitleBgActive from a tweaked TitleBg color. |
| - 2016/05/07 (1.49) - removed confusing set of GetInternalState(), GetInternalStateSize(), SetInternalState() functions. Now using CreateContext(), DestroyContext(), GetCurrentContext(), SetCurrentContext(). |
| - 2016/05/02 (1.49) - renamed SetNextTreeNodeOpened() to SetNextTreeNodeOpen(), no redirection. |
| - 2016/05/01 (1.49) - obsoleted old signature of CollapsingHeader(const char* label, const char* str_id = NULL, bool display_frame = true, bool default_open = false) as extra parameters were badly designed and rarely used. You can replace the "default_open = true" flag in new API with CollapsingHeader(label, ImGuiTreeNodeFlags_DefaultOpen). |
| - 2016/04/26 (1.49) - changed ImDrawList::PushClipRect(ImVec4 rect) to ImDraw::PushClipRect(Imvec2 min,ImVec2 max,bool intersect_with_current_clip_rect=false). Note that higher-level ImGui::PushClipRect() is preferable because it will clip at logic/widget level, whereas ImDrawList::PushClipRect() only affect your renderer. |
| - 2016/04/03 (1.48) - removed style.WindowFillAlphaDefault setting which was redundant. Bake default BG alpha inside style.Colors[ImGuiCol_WindowBg] and all other Bg color values. (ref github issue #337). |
| - 2016/04/03 (1.48) - renamed ImGuiCol_TooltipBg to ImGuiCol_PopupBg, used by popups/menus and tooltips. popups/menus were previously using ImGuiCol_WindowBg. (ref github issue #337) |
| - 2016/03/21 (1.48) - renamed GetWindowFont() to GetFont(), GetWindowFontSize() to GetFontSize(). Kept inline redirection function (will obsolete). |
| - 2016/03/02 (1.48) - InputText() completion/history/always callbacks: if you modify the text buffer manually (without using DeleteChars()/InsertChars() helper) you need to maintain the BufTextLen field. added an assert. |
| - 2016/01/23 (1.48) - fixed not honoring exact width passed to PushItemWidth(), previously it would add extra FramePadding.x*2 over that width. if you had manual pixel-perfect alignment in place it might affect you. |
| - 2015/12/27 (1.48) - fixed ImDrawList::AddRect() which used to render a rectangle 1 px too large on each axis. |
| - 2015/12/04 (1.47) - renamed Color() helpers to ValueColor() - dangerously named, rarely used and probably to be made obsolete. |
| - 2015/08/29 (1.45) - with the addition of horizontal scrollbar we made various fixes to inconsistencies with dealing with cursor position. |
| GetCursorPos()/SetCursorPos() functions now include the scrolled amount. It shouldn't affect the majority of users, but take note that SetCursorPosX(100.0f) puts you at +100 from the starting x position which may include scrolling, not at +100 from the window left side. |
| GetContentRegionMax()/GetWindowContentRegionMin()/GetWindowContentRegionMax() functions allow include the scrolled amount. Typically those were used in cases where no scrolling would happen so it may not be a problem, but watch out! |
| - 2015/08/29 (1.45) - renamed style.ScrollbarWidth to style.ScrollbarSize |
| - 2015/08/05 (1.44) - split imgui.cpp into extra files: imgui_demo.cpp imgui_draw.cpp imgui_internal.h that you need to add to your project. |
| - 2015/07/18 (1.44) - fixed angles in ImDrawList::PathArcTo(), PathArcToFast() (introduced in 1.43) being off by an extra PI for no justifiable reason |
| - 2015/07/14 (1.43) - add new ImFontAtlas::AddFont() API. For the old AddFont***, moved the 'font_no' parameter of ImFontAtlas::AddFont** functions to the ImFontConfig structure. |
| you need to render your textured triangles with bilinear filtering to benefit from sub-pixel positioning of text. |
| - 2015/07/08 (1.43) - switched rendering data to use indexed rendering. this is saving a fair amount of CPU/GPU and enables us to get anti-aliasing for a marginal cost. |
| this necessary change will break your rendering function! the fix should be very easy. sorry for that :( |
| - if you are using a vanilla copy of one of the imgui_impl_XXXX.cpp provided in the example, you just need to update your copy and you can ignore the rest. |
| - the signature of the io.RenderDrawListsFn handler has changed! |
| ImGui_XXXX_RenderDrawLists(ImDrawList** const cmd_lists, int cmd_lists_count) |
| became: |
| ImGui_XXXX_RenderDrawLists(ImDrawData* draw_data). |
| argument 'cmd_lists' -> 'draw_data->CmdLists' |
| argument 'cmd_lists_count' -> 'draw_data->CmdListsCount' |
| ImDrawList 'commands' -> 'CmdBuffer' |
| ImDrawList 'vtx_buffer' -> 'VtxBuffer' |
| ImDrawList n/a -> 'IdxBuffer' (new) |
| ImDrawCmd 'vtx_count' -> 'ElemCount' |
| ImDrawCmd 'clip_rect' -> 'ClipRect' |
| ImDrawCmd 'user_callback' -> 'UserCallback' |
| ImDrawCmd 'texture_id' -> 'TextureId' |
| - each ImDrawList now contains both a vertex buffer and an index buffer. For each command, render ElemCount/3 triangles using indices from the index buffer. |
| - if you REALLY cannot render indexed primitives, you can call the draw_data->DeIndexAllBuffers() method to de-index the buffers. This is slow and a waste of CPU/GPU. Prefer using indexed rendering! |
| - refer to code in the examples/ folder or ask on the GitHub if you are unsure of how to upgrade. please upgrade! |
| - 2015/07/10 (1.43) - changed SameLine() parameters from int to float. |
| - 2015/07/02 (1.42) - renamed SetScrollPosHere() to SetScrollFromCursorPos(). Kept inline redirection function (will obsolete). |
| - 2015/07/02 (1.42) - renamed GetScrollPosY() to GetScrollY(). Necessary to reduce confusion along with other scrolling functions, because positions (e.g. cursor position) are not equivalent to scrolling amount. |
| - 2015/06/14 (1.41) - changed ImageButton() default bg_col parameter from (0,0,0,1) (black) to (0,0,0,0) (transparent) - makes a difference when texture have transparence |
| - 2015/06/14 (1.41) - changed Selectable() API from (label, selected, size) to (label, selected, flags, size). Size override should have been rarely be used. Sorry! |
| - 2015/05/31 (1.40) - renamed GetWindowCollapsed() to IsWindowCollapsed() for consistency. Kept inline redirection function (will obsolete). |
| - 2015/05/31 (1.40) - renamed IsRectClipped() to IsRectVisible() for consistency. Note that return value is opposite! Kept inline redirection function (will obsolete). |
| - 2015/05/27 (1.40) - removed the third 'repeat_if_held' parameter from Button() - sorry! it was rarely used and inconsistent. Use PushButtonRepeat(true) / PopButtonRepeat() to enable repeat on desired buttons. |
| - 2015/05/11 (1.40) - changed BeginPopup() API, takes a string identifier instead of a bool. ImGui needs to manage the open/closed state of popups. Call OpenPopup() to actually set the "open" state of a popup. BeginPopup() returns true if the popup is opened. |
| - 2015/05/03 (1.40) - removed style.AutoFitPadding, using style.WindowPadding makes more sense (the default values were already the same). |
| - 2015/04/13 (1.38) - renamed IsClipped() to IsRectClipped(). Kept inline redirection function until 1.50. |
| - 2015/04/09 (1.38) - renamed ImDrawList::AddArc() to ImDrawList::AddArcFast() for compatibility with future API |
| - 2015/04/03 (1.38) - removed ImGuiCol_CheckHovered, ImGuiCol_CheckActive, replaced with the more general ImGuiCol_FrameBgHovered, ImGuiCol_FrameBgActive. |
| - 2014/04/03 (1.38) - removed support for passing -FLT_MAX..+FLT_MAX as the range for a SliderFloat(). Use DragFloat() or Inputfloat() instead. |
| - 2015/03/17 (1.36) - renamed GetItemBoxMin()/GetItemBoxMax()/IsMouseHoveringBox() to GetItemRectMin()/GetItemRectMax()/IsMouseHoveringRect(). Kept inline redirection function until 1.50. |
| - 2015/03/15 (1.36) - renamed style.TreeNodeSpacing to style.IndentSpacing, ImGuiStyleVar_TreeNodeSpacing to ImGuiStyleVar_IndentSpacing |
| - 2015/03/13 (1.36) - renamed GetWindowIsFocused() to IsWindowFocused(). Kept inline redirection function until 1.50. |
| - 2015/03/08 (1.35) - renamed style.ScrollBarWidth to style.ScrollbarWidth (casing) |
| - 2015/02/27 (1.34) - renamed OpenNextNode(bool) to SetNextTreeNodeOpened(bool, ImGuiSetCond). Kept inline redirection function until 1.50. |
| - 2015/02/27 (1.34) - renamed ImGuiSetCondition_*** to ImGuiSetCond_***, and _FirstUseThisSession becomes _Once. |
| - 2015/02/11 (1.32) - changed text input callback ImGuiTextEditCallback return type from void-->int. reserved for future use, return 0 for now. |
| - 2015/02/10 (1.32) - renamed GetItemWidth() to CalcItemWidth() to clarify its evolving behavior |
| - 2015/02/08 (1.31) - renamed GetTextLineSpacing() to GetTextLineHeightWithSpacing() |
| - 2015/02/01 (1.31) - removed IO.MemReallocFn (unused) |
| - 2015/01/19 (1.30) - renamed ImGuiStorage::GetIntPtr()/GetFloatPtr() to GetIntRef()/GetIntRef() because Ptr was conflicting with actual pointer storage functions. |
| - 2015/01/11 (1.30) - big font/image API change! now loads TTF file. allow for multiple fonts. no need for a PNG loader. |
| (1.30) - removed GetDefaultFontData(). uses io.Fonts->GetTextureData*() API to retrieve uncompressed pixels. |
| this sequence: |
| const void* png_data; |
| unsigned int png_size; |
| ImGui::GetDefaultFontData(NULL, NULL, &png_data, &png_size); |
| // <Copy to GPU> |
| became: |
| unsigned char* pixels; |
| int width, height; |
| io.Fonts->GetTexDataAsRGBA32(&pixels, &width, &height); |
| // <Copy to GPU> |
| io.Fonts->TexID = (your_texture_identifier); |
| you now have much more flexibility to load multiple TTF fonts and manage the texture buffer for internal needs. |
| it is now recommended that you sample the font texture with bilinear interpolation. |
| (1.30) - added texture identifier in ImDrawCmd passed to your render function (we can now render images). make sure to set io.Fonts->TexID. |
| (1.30) - removed IO.PixelCenterOffset (unnecessary, can be handled in user projection matrix) |
| (1.30) - removed ImGui::IsItemFocused() in favor of ImGui::IsItemActive() which handles all widgets |
| - 2014/12/10 (1.18) - removed SetNewWindowDefaultPos() in favor of new generic API SetNextWindowPos(pos, ImGuiSetCondition_FirstUseEver) |
| - 2014/11/28 (1.17) - moved IO.Font*** options to inside the IO.Font-> structure (FontYOffset, FontTexUvForWhite, FontBaseScale, FontFallbackGlyph) |
| - 2014/11/26 (1.17) - reworked syntax of IMGUI_ONCE_UPON_A_FRAME helper macro to increase compiler compatibility |
| - 2014/11/07 (1.15) - renamed IsHovered() to IsItemHovered() |
| - 2014/10/02 (1.14) - renamed IMGUI_INCLUDE_IMGUI_USER_CPP to IMGUI_INCLUDE_IMGUI_USER_INL and imgui_user.cpp to imgui_user.inl (more IDE friendly) |
| - 2014/09/25 (1.13) - removed 'text_end' parameter from IO.SetClipboardTextFn (the string is now always zero-terminated for simplicity) |
| - 2014/09/24 (1.12) - renamed SetFontScale() to SetWindowFontScale() |
| - 2014/09/24 (1.12) - moved IM_MALLOC/IM_REALLOC/IM_FREE preprocessor defines to IO.MemAllocFn/IO.MemReallocFn/IO.MemFreeFn |
| - 2014/08/30 (1.09) - removed IO.FontHeight (now computed automatically) |
| - 2014/08/30 (1.09) - moved IMGUI_FONT_TEX_UV_FOR_WHITE preprocessor define to IO.FontTexUvForWhite |
| - 2014/08/28 (1.09) - changed the behavior of IO.PixelCenterOffset following various rendering fixes |
| |
| |
| ISSUES & TODO-LIST |
| ================== |
| See TODO.txt |
| |
| |
| FREQUENTLY ASKED QUESTIONS (FAQ), TIPS |
| ====================================== |
| |
| Q: How can I help? |
| A: - If you are experienced enough with ImGui and with C/C++, look at the todo list and see how you want/can help! |
| - Become a Patron/donate! Convince your company to become a Patron or provide serious funding for development time! See http://www.patreon.com/imgui |
| |
| Q: What is ImTextureID and how do I display an image? |
| A: ImTextureID is a void* used to pass renderer-agnostic texture references around until it hits your render function. |
| ImGui knows nothing about what those bits represent, it just passes them around. It is up to you to decide what you want the void* to carry! |
| It could be an identifier to your OpenGL texture (cast GLuint to void*), a pointer to your custom engine material (cast MyMaterial* to void*), etc. |
| At the end of the chain, your renderer takes this void* to cast it back into whatever it needs to select a current texture to render. |
| Refer to examples applications, where each renderer (in a imgui_impl_xxxx.cpp file) is treating ImTextureID as a different thing. |
| (c++ tip: OpenGL uses integers to identify textures. You can safely store an integer into a void*, just cast it to void*, don't take it's address!) |
| To display a custom image/texture within an ImGui window, you may use ImGui::Image(), ImGui::ImageButton(), ImDrawList::AddImage() functions. |
| ImGui will generate the geometry and draw calls using the ImTextureID that you passed and which your renderer can use. |
| It is your responsibility to get textures uploaded to your GPU. |
| |
| Q: I integrated ImGui in my engine and the text or lines are blurry.. |
| A: In your Render function, try translating your projection matrix by (0.5f,0.5f) or (0.375f,0.375f). |
| Also make sure your orthographic projection matrix and io.DisplaySize matches your actual framebuffer dimension. |
| |
| Q: I integrated ImGui in my engine and some elements are clipping or disappearing when I move windows around.. |
| A: Most likely you are mishandling the clipping rectangles in your render function. Rectangles provided by ImGui are defined as (x1=left,y1=top,x2=right,y2=bottom) and NOT as (x1,y1,width,height). |
| |
| Q: Can I have multiple widgets with the same label? Can I have widget without a label? |
| A: Yes. A primer on the use of labels/IDs in ImGui.. |
| |
| - Elements that are not clickable, such as Text() items don't need an ID. |
| |
| - Interactive widgets require state to be carried over multiple frames (most typically ImGui often needs to remember what is the "active" widget). |
| to do so they need a unique ID. unique ID are typically derived from a string label, an integer index or a pointer. |
| |
| Button("OK"); // Label = "OK", ID = hash of "OK" |
| Button("Cancel"); // Label = "Cancel", ID = hash of "Cancel" |
| |
| - ID are uniquely scoped within windows, tree nodes, etc. so no conflict can happen if you have two buttons called "OK" in two different windows |
| or in two different locations of a tree. |
| |
| - If you have a same ID twice in the same location, you'll have a conflict: |
| |
| Button("OK"); |
| Button("OK"); // ID collision! Both buttons will be treated as the same. |
| |
| Fear not! this is easy to solve and there are many ways to solve it! |
| |
| - When passing a label you can optionally specify extra unique ID information within string itself. This helps solving the simpler collision cases. |
| use "##" to pass a complement to the ID that won't be visible to the end-user: |
| |
| Button("Play"); // Label = "Play", ID = hash of "Play" |
| Button("Play##foo1"); // Label = "Play", ID = hash of "Play##foo1" (different from above) |
| Button("Play##foo2"); // Label = "Play", ID = hash of "Play##foo2" (different from above) |
| |
| - If you want to completely hide the label, but still need an ID: |
| |
| Checkbox("##On", &b); // Label = "", ID = hash of "##On" (no label!) |
| |
| - Occasionally/rarely you might want change a label while preserving a constant ID. This allows you to animate labels. |
| For example you may want to include varying information in a window title bar (and windows are uniquely identified by their ID.. obviously) |
| Use "###" to pass a label that isn't part of ID: |
| |
| Button("Hello###ID"; // Label = "Hello", ID = hash of "ID" |
| Button("World###ID"; // Label = "World", ID = hash of "ID" (same as above) |
| |
| sprintf(buf, "My game (%f FPS)###MyGame"); |
| Begin(buf); // Variable label, ID = hash of "MyGame" |
| |
| - Use PushID() / PopID() to create scopes and avoid ID conflicts within the same Window. |
| This is the most convenient way of distinguishing ID if you are iterating and creating many UI elements. |
| You can push a pointer, a string or an integer value. Remember that ID are formed from the concatenation of everything in the ID stack! |
| |
| for (int i = 0; i < 100; i++) |
| { |
| PushID(i); |
| Button("Click"); // Label = "Click", ID = hash of integer + "label" (unique) |
| PopID(); |
| } |
| |
| for (int i = 0; i < 100; i++) |
| { |
| MyObject* obj = Objects[i]; |
| PushID(obj); |
| Button("Click"); // Label = "Click", ID = hash of pointer + "label" (unique) |
| PopID(); |
| } |
| |
| for (int i = 0; i < 100; i++) |
| { |
| MyObject* obj = Objects[i]; |
| PushID(obj->Name); |
| Button("Click"); // Label = "Click", ID = hash of string + "label" (unique) |
| PopID(); |
| } |
| |
| - More example showing that you can stack multiple prefixes into the ID stack: |
| |
| Button("Click"); // Label = "Click", ID = hash of "Click" |
| PushID("node"); |
| Button("Click"); // Label = "Click", ID = hash of "node" + "Click" |
| PushID(my_ptr); |
| Button("Click"); // Label = "Click", ID = hash of "node" + ptr + "Click" |
| PopID(); |
| PopID(); |
| |
| - Tree nodes implicitly creates a scope for you by calling PushID(). |
| |
| Button("Click"); // Label = "Click", ID = hash of "Click" |
| if (TreeNode("node")) |
| { |
| Button("Click"); // Label = "Click", ID = hash of "node" + "Click" |
| TreePop(); |
| } |
| |
| - When working with trees, ID are used to preserve the open/close state of each tree node. |
| Depending on your use cases you may want to use strings, indices or pointers as ID. |
| e.g. when displaying a single object that may change over time (1-1 relationship), using a static string as ID will preserve your node open/closed state when the targeted object change. |
| e.g. when displaying a list of objects, using indices or pointers as ID will preserve the node open/closed state differently. experiment and see what makes more sense! |
| |
| Q: How can I tell when ImGui wants my mouse/keyboard inputs VS when I can pass them to my application? |
| A: You can read the 'io.WantCaptureMouse'/'io.WantCaptureKeyboard'/'ioWantTextInput' flags from the ImGuiIO structure. |
| - When 'io.WantCaptureMouse' or 'io.WantCaptureKeyboard' flags are set you may want to discard/hide the inputs from the rest of your application. |
| - When 'io.WantTextInput' is set to may want to notify your OS to popup an on-screen keyboard, if available (e.g. on a mobile phone, or console without a keyboard). |
| Preferably read the flags after calling ImGui::NewFrame() to avoid them lagging by one frame. But reading those flags before calling NewFrame() is also generally ok, |
| as the bool toggles fairly rarely and you don't generally expect to interact with either ImGui or your application during the same frame when that transition occurs. |
| ImGui is tracking dragging and widget activity that may occur outside the boundary of a window, so 'io.WantCaptureMouse' is more accurate and correct than checking if a window is hovered. |
| (Advanced note: text input releases focus on Return 'KeyDown', so the following Return 'KeyUp' event that your application receive will typically have 'io.WantCaptureKeyboard=false'. |
| Depending on your application logic it may or not be inconvenient. You might want to track which key-downs were for ImGui (e.g. with an array of bool) and filter out the corresponding key-ups.) |
| |
| Q: How can I load a different font than the default? (default is an embedded version of ProggyClean.ttf, rendered at size 13) |
| A: Use the font atlas to load the TTF/OTF file you want: |
| |
| ImGuiIO& io = ImGui::GetIO(); |
| io.Fonts->AddFontFromFileTTF("myfontfile.ttf", size_in_pixels); |
| io.Fonts->GetTexDataAsRGBA32() or GetTexDataAsAlpha8() |
| |
| Q: How can I easily use icons in my application? |
| A: The most convenient and practical way is to merge an icon font such as FontAwesome inside you main font. Then you can refer to icons within your strings. |
| Read 'How can I load multiple fonts?' and the file 'extra_fonts/README.txt' for instructions. |
| |
| Q: How can I load multiple fonts? |
| A: Use the font atlas to pack them into a single texture: |
| (Read extra_fonts/README.txt and the code in ImFontAtlas for more details.) |
| |
| ImGuiIO& io = ImGui::GetIO(); |
| ImFont* font0 = io.Fonts->AddFontDefault(); |
| ImFont* font1 = io.Fonts->AddFontFromFileTTF("myfontfile.ttf", size_in_pixels); |
| ImFont* font2 = io.Fonts->AddFontFromFileTTF("myfontfile2.ttf", size_in_pixels); |
| io.Fonts->GetTexDataAsRGBA32() or GetTexDataAsAlpha8() |
| // the first loaded font gets used by default |
| // use ImGui::PushFont()/ImGui::PopFont() to change the font at runtime |
| |
| // Options |
| ImFontConfig config; |
| config.OversampleH = 3; |
| config.OversampleV = 1; |
| config.GlyphOffset.y -= 2.0f; // Move everything by 2 pixels up |
| config.GlyphExtraSpacing.x = 1.0f; // Increase spacing between characters |
| io.Fonts->LoadFromFileTTF("myfontfile.ttf", size_pixels, &config); |
| |
| // Combine multiple fonts into one (e.g. for icon fonts) |
| ImWchar ranges[] = { 0xf000, 0xf3ff, 0 }; |
| ImFontConfig config; |
| config.MergeMode = true; |
| io.Fonts->AddFontDefault(); |
| io.Fonts->LoadFromFileTTF("fontawesome-webfont.ttf", 16.0f, &config, ranges); // Merge icon font |
| io.Fonts->LoadFromFileTTF("myfontfile.ttf", size_pixels, NULL, &config, io.Fonts->GetGlyphRangesJapanese()); // Merge japanese glyphs |
| |
| Q: How can I display and input non-Latin characters such as Chinese, Japanese, Korean, Cyrillic? |
| A: When loading a font, pass custom Unicode ranges to specify the glyphs to load. |
| |
| // Add default Japanese ranges |
| io.Fonts->AddFontFromFileTTF("myfontfile.ttf", size_in_pixels, NULL, io.Fonts->GetGlyphRangesJapanese()); |
| |
| // Or create your own custom ranges (e.g. for a game you can feed your entire game script and only build the characters the game need) |
| ImVector<ImWchar> ranges; |
| ImFontAtlas::GlyphRangesBuilder builder; |
| builder.AddText("Hello world"); // Add a string (here "Hello world" contains 7 unique characters) |
| builder.AddChar(0x7262); // Add a specific character |
| builder.AddRanges(io.Fonts->GetGlyphRangesJapanese()); // Add one of the default ranges |
| builder.BuildRanges(&ranges); // Build the final result (ordered ranges with all the unique characters submitted) |
| io.Fonts->AddFontFromFileTTF("myfontfile.ttf", size_in_pixels, NULL, ranges.Data); |
| |
| All your strings needs to use UTF-8 encoding. In C++11 you can encode a string literal in UTF-8 by using the u8"hello" syntax. |
| Specifying literal in your source code using a local code page (such as CP-923 for Japanese or CP-1251 for Cyrillic) will NOT work! |
| Otherwise you can convert yourself to UTF-8 or load text data from file already saved as UTF-8. |
| |
| Text input: it is up to your application to pass the right character code to io.AddInputCharacter(). The applications in examples/ are doing that. |
| For languages using IME, on Windows you can copy the Hwnd of your application to io.ImeWindowHandle. The default implementation of io.ImeSetInputScreenPosFn() on Windows will set your IME position correctly. |
| |
| Q: How can I preserve my ImGui context across reloading a DLL? (loss of the global/static variables) |
| A: Create your own context 'ctx = CreateContext()' + 'SetCurrentContext(ctx)' and your own font atlas 'ctx->GetIO().Fonts = new ImFontAtlas()' so you don't rely on the default globals. |
| |
| Q: How can I use the drawing facilities without an ImGui window? (using ImDrawList API) |
| A: The easiest way is to create a dummy window. Call Begin() with NoTitleBar|NoResize|NoMove|NoScrollbar|NoSavedSettings|NoInputs flag, zero background alpha, |
| then retrieve the ImDrawList* via GetWindowDrawList() and draw to it in any way you like. |
| You can also perfectly create a standalone ImDrawList instance _but_ you need ImGui to be initialized because ImDrawList pulls from ImGui data to retrieve the coordinates of the white pixel. |
| |
| - tip: the construct 'IMGUI_ONCE_UPON_A_FRAME { ... }' will run the block of code only once a frame. You can use it to quickly add custom UI in the middle of a deep nested inner loop in your code. |
| - tip: you can create widgets without a Begin()/End() block, they will go in an implicit window called "Debug" |
| - tip: you can call Begin() multiple times with the same name during the same frame, it will keep appending to the same window. this is also useful to set yourself in the context of another window (to get/set other settings) |
| - tip: you can call Render() multiple times (e.g for VR renders). |
| - tip: call and read the ShowTestWindow() code in imgui_demo.cpp for more example of how to use ImGui! |
| |
| */ |
| |
| #if defined(_MSC_VER) && !defined(_CRT_SECURE_NO_WARNINGS) |
| #define _CRT_SECURE_NO_WARNINGS |
| #endif |
| |
| #include "imgui.h" |
| #define IMGUI_DEFINE_MATH_OPERATORS |
| #define IMGUI_DEFINE_PLACEMENT_NEW |
| #include "imgui_internal.h" |
| |
| #include <ctype.h> // toupper, isprint |
| #include <stdlib.h> // NULL, malloc, free, qsort, atoi |
| #include <stdio.h> // vsnprintf, sscanf, printf |
| #include <limits.h> // INT_MIN, INT_MAX |
| #if defined(_MSC_VER) && _MSC_VER <= 1500 // MSVC 2008 or earlier |
| #include <stddef.h> // intptr_t |
| #else |
| #include <stdint.h> // intptr_t |
| #endif |
| |
| #ifdef _MSC_VER |
| #pragma warning (disable: 4127) // condition expression is constant |
| #pragma warning (disable: 4505) // unreferenced local function has been removed (stb stuff) |
| #pragma warning (disable: 4996) // 'This function or variable may be unsafe': strcpy, strdup, sprintf, vsnprintf, sscanf, fopen |
| #endif |
| |
| // Clang warnings with -Weverything |
| #ifdef __clang__ |
| #pragma clang diagnostic ignored "-Wunknown-pragmas" // warning : unknown warning group '-Wformat-pedantic *' // not all warnings are known by all clang versions.. so ignoring warnings triggers new warnings on some configuration. great! |
| #pragma clang diagnostic ignored "-Wold-style-cast" // warning : use of old-style cast // yes, they are more terse. |
| #pragma clang diagnostic ignored "-Wfloat-equal" // warning : comparing floating point with == or != is unsafe // storing and comparing against same constants (typically 0.0f) is ok. |
| #pragma clang diagnostic ignored "-Wformat-nonliteral" // warning : format string is not a string literal // passing non-literal to vsnformat(). yes, user passing incorrect format strings can crash the code. |
| #pragma clang diagnostic ignored "-Wexit-time-destructors" // warning : declaration requires an exit-time destructor // exit-time destruction order is undefined. if MemFree() leads to users code that has been disabled before exit it might cause problems. ImGui coding style welcomes static/globals. |
| #pragma clang diagnostic ignored "-Wglobal-constructors" // warning : declaration requires a global destructor // similar to above, not sure what the exact difference it. |
| #pragma clang diagnostic ignored "-Wsign-conversion" // warning : implicit conversion changes signedness // |
| #pragma clang diagnostic ignored "-Wformat-pedantic" // warning : format specifies type 'void *' but the argument has type 'xxxx *' // unreasonable, would lead to casting every %p arg to void*. probably enabled by -pedantic. |
| #pragma clang diagnostic ignored "-Wint-to-void-pointer-cast" // warning : cast to 'void *' from smaller integer type 'int' // |
| #elif defined(__GNUC__) |
| #pragma GCC diagnostic ignored "-Wunused-function" // warning: 'xxxx' defined but not used |
| #pragma GCC diagnostic ignored "-Wint-to-pointer-cast" // warning: cast to pointer from integer of different size |
| #pragma GCC diagnostic ignored "-Wformat" // warning: format '%p' expects argument of type 'void*', but argument 6 has type 'ImGuiWindow*' |
| #pragma GCC diagnostic ignored "-Wdouble-promotion" // warning: implicit conversion from 'float' to 'double' when passing argument to function |
| #pragma GCC diagnostic ignored "-Wconversion" // warning: conversion to 'xxxx' from 'xxxx' may alter its value |
| #pragma GCC diagnostic ignored "-Wcast-qual" // warning: cast from type 'xxxx' to type 'xxxx' casts away qualifiers |
| #pragma GCC diagnostic ignored "-Wformat-nonliteral" // warning: format not a string literal, format string not checked |
| #pragma GCC diagnostic ignored "-Wsuggest-attribute=format" |
| #endif |
| |
| //------------------------------------------------------------------------- |
| // Forward Declarations |
| //------------------------------------------------------------------------- |
| |
| static void LogRenderedText(const ImVec2& ref_pos, const char* text, const char* text_end = NULL); |
| |
| static void PushMultiItemsWidths(int components, float w_full = 0.0f); |
| static float GetDraggedColumnOffset(int column_index); |
| |
| static bool IsKeyPressedMap(ImGuiKey key, bool repeat = true); |
| |
| static ImFont* GetDefaultFont(); |
| static void SetCurrentFont(ImFont* font); |
| static void SetCurrentWindow(ImGuiWindow* window); |
| static void SetWindowScrollY(ImGuiWindow* window, float new_scroll_y); |
| static void SetWindowPos(ImGuiWindow* window, const ImVec2& pos, ImGuiCond cond); |
| static void SetWindowSize(ImGuiWindow* window, const ImVec2& size, ImGuiCond cond); |
| static void SetWindowCollapsed(ImGuiWindow* window, bool collapsed, ImGuiCond cond); |
| static ImGuiWindow* FindHoveredWindow(ImVec2 pos, bool excluding_childs); |
| static ImGuiWindow* CreateNewWindow(const char* name, ImVec2 size, ImGuiWindowFlags flags); |
| static inline bool IsWindowContentHoverable(ImGuiWindow* window); |
| static void ClearSetNextWindowData(); |
| static void CheckStacksSize(ImGuiWindow* window, bool write); |
| static void Scrollbar(ImGuiWindow* window, bool horizontal); |
| |
| static void AddDrawListToRenderList(ImVector<ImDrawList*>& out_render_list, ImDrawList* draw_list); |
| static void AddWindowToRenderList(ImVector<ImDrawList*>& out_render_list, ImGuiWindow* window); |
| static void AddWindowToSortedBuffer(ImVector<ImGuiWindow*>& out_sorted_windows, ImGuiWindow* window); |
| |
| static ImGuiIniData* FindWindowSettings(const char* name); |
| static ImGuiIniData* AddWindowSettings(const char* name); |
| static void LoadIniSettingsFromDisk(const char* ini_filename); |
| static void SaveIniSettingsToDisk(const char* ini_filename); |
| static void MarkIniSettingsDirty(ImGuiWindow* window); |
| |
| static ImRect GetVisibleRect(); |
| |
| static bool BeginPopupEx(ImGuiID id, ImGuiWindowFlags extra_flags); |
| static void CloseInactivePopups(); |
| static void ClosePopupToLevel(int remaining); |
| static void ClosePopup(ImGuiID id); |
| static ImGuiWindow* GetFrontMostModalRootWindow(); |
| static ImVec2 FindBestPopupWindowPos(const ImVec2& base_pos, const ImVec2& size, int* last_dir, const ImRect& rect_to_avoid); |
| |
| static bool InputTextFilterCharacter(unsigned int* p_char, ImGuiInputTextFlags flags, ImGuiTextEditCallback callback, void* user_data); |
| static int InputTextCalcTextLenAndLineCount(const char* text_begin, const char** out_text_end); |
| static ImVec2 InputTextCalcTextSizeW(const ImWchar* text_begin, const ImWchar* text_end, const ImWchar** remaining = NULL, ImVec2* out_offset = NULL, bool stop_on_new_line = false); |
| |
| static inline void DataTypeFormatString(ImGuiDataType data_type, void* data_ptr, const char* display_format, char* buf, int buf_size); |
| static inline void DataTypeFormatString(ImGuiDataType data_type, void* data_ptr, int decimal_precision, char* buf, int buf_size); |
| static void DataTypeApplyOp(ImGuiDataType data_type, int op, void* value1, const void* value2); |
| static bool DataTypeApplyOpFromText(const char* buf, const char* initial_value_buf, ImGuiDataType data_type, void* data_ptr, const char* scalar_format); |
| |
| //----------------------------------------------------------------------------- |
| // Platform dependent default implementations |
| //----------------------------------------------------------------------------- |
| |
| static const char* GetClipboardTextFn_DefaultImpl(void* user_data); |
| static void SetClipboardTextFn_DefaultImpl(void* user_data, const char* text); |
| static void ImeSetInputScreenPosFn_DefaultImpl(int x, int y); |
| |
| //----------------------------------------------------------------------------- |
| // Context |
| //----------------------------------------------------------------------------- |
| |
| // Default font atlas storage. |
| // New contexts always point by default to this font atlas. It can be changed by reassigning the GetIO().Fonts variable. |
| static ImFontAtlas GImDefaultFontAtlas; |
| |
| // Default context storage + current context pointer. |
| // Implicitely used by all ImGui functions. Always assumed to be != NULL. Change to a different context by calling ImGui::SetCurrentContext() |
| // If you are hot-reloading this code in a DLL you will lose the static/global variables. Create your own context+font atlas instead of relying on those default (see FAQ entry "How can I preserve my ImGui context across reloading a DLL?"). |
| // ImGui is currently not thread-safe because of this variable. If you want thread-safety to allow N threads to access N different contexts, you might work around it by: |
| // - Having multiple instances of the ImGui code compiled inside different namespace (easiest/safest, if you have a finite number of contexts) |
| // - or: Changing this variable to be TLS. You may #define GImGui in imconfig.h for further custom hackery. Future development aim to make this context pointer explicit to all calls. Also read https://github.com/ocornut/imgui/issues/586 |
| #ifndef GImGui |
| static ImGuiContext GImDefaultContext; |
| ImGuiContext* GImGui = &GImDefaultContext; |
| #endif |
| |
| //----------------------------------------------------------------------------- |
| // User facing structures |
| //----------------------------------------------------------------------------- |
| |
| ImGuiStyle::ImGuiStyle() |
| { |
| Alpha = 1.0f; // Global alpha applies to everything in ImGui |
| WindowPadding = ImVec2(8,8); // Padding within a window |
| WindowMinSize = ImVec2(32,32); // Minimum window size |
| WindowRounding = 9.0f; // Radius of window corners rounding. Set to 0.0f to have rectangular windows |
| WindowTitleAlign = ImVec2(0.0f,0.5f);// Alignment for title bar text |
| ChildWindowRounding = 0.0f; // Radius of child window corners rounding. Set to 0.0f to have rectangular child windows |
| FramePadding = ImVec2(4,3); // Padding within a framed rectangle (used by most widgets) |
| FrameRounding = 0.0f; // Radius of frame corners rounding. Set to 0.0f to have rectangular frames (used by most widgets). |
| ItemSpacing = ImVec2(8,4); // Horizontal and vertical spacing between widgets/lines |
| ItemInnerSpacing = ImVec2(4,4); // Horizontal and vertical spacing between within elements of a composed widget (e.g. a slider and its label) |
| TouchExtraPadding = ImVec2(0,0); // Expand reactive bounding box for touch-based system where touch position is not accurate enough. Unfortunately we don't sort widgets so priority on overlap will always be given to the first widget. So don't grow this too much! |
| IndentSpacing = 21.0f; // Horizontal spacing when e.g. entering a tree node. Generally == (FontSize + FramePadding.x*2). |
| ColumnsMinSpacing = 6.0f; // Minimum horizontal spacing between two columns |
| ScrollbarSize = 16.0f; // Width of the vertical scrollbar, Height of the horizontal scrollbar |
| ScrollbarRounding = 9.0f; // Radius of grab corners rounding for scrollbar |
| GrabMinSize = 10.0f; // Minimum width/height of a grab box for slider/scrollbar |
| GrabRounding = 0.0f; // Radius of grabs corners rounding. Set to 0.0f to have rectangular slider grabs. |
| ButtonTextAlign = ImVec2(0.5f,0.5f);// Alignment of button text when button is larger than text. |
| DisplayWindowPadding = ImVec2(22,22); // Window positions are clamped to be visible within the display area by at least this amount. Only covers regular windows. |
| DisplaySafeAreaPadding = ImVec2(4,4); // If you cannot see the edge of your screen (e.g. on a TV) increase the safe area padding. Covers popups/tooltips as well regular windows. |
| AntiAliasedLines = true; // Enable anti-aliasing on lines/borders. Disable if you are really short on CPU/GPU. |
| AntiAliasedShapes = true; // Enable anti-aliasing on filled shapes (rounded rectangles, circles, etc.) |
| CurveTessellationTol = 1.25f; // Tessellation tolerance. Decrease for highly tessellated curves (higher quality, more polygons), increase to reduce quality. |
| |
| Colors[ImGuiCol_Text] = ImVec4(0.90f, 0.90f, 0.90f, 1.00f); |
| Colors[ImGuiCol_TextDisabled] = ImVec4(0.60f, 0.60f, 0.60f, 1.00f); |
| Colors[ImGuiCol_WindowBg] = ImVec4(0.00f, 0.00f, 0.00f, 0.70f); |
| Colors[ImGuiCol_ChildWindowBg] = ImVec4(0.00f, 0.00f, 0.00f, 0.00f); |
| Colors[ImGuiCol_PopupBg] = ImVec4(0.05f, 0.05f, 0.10f, 0.90f); |
| Colors[ImGuiCol_Border] = ImVec4(0.70f, 0.70f, 0.70f, 0.40f); |
| Colors[ImGuiCol_BorderShadow] = ImVec4(0.00f, 0.00f, 0.00f, 0.00f); |
| Colors[ImGuiCol_FrameBg] = ImVec4(0.80f, 0.80f, 0.80f, 0.30f); // Background of checkbox, radio button, plot, slider, text input |
| Colors[ImGuiCol_FrameBgHovered] = ImVec4(0.90f, 0.80f, 0.80f, 0.40f); |
| Colors[ImGuiCol_FrameBgActive] = ImVec4(0.90f, 0.65f, 0.65f, 0.45f); |
| Colors[ImGuiCol_TitleBg] = ImVec4(0.27f, 0.27f, 0.54f, 0.83f); |
| Colors[ImGuiCol_TitleBgCollapsed] = ImVec4(0.40f, 0.40f, 0.80f, 0.20f); |
| Colors[ImGuiCol_TitleBgActive] = ImVec4(0.32f, 0.32f, 0.63f, 0.87f); |
| Colors[ImGuiCol_MenuBarBg] = ImVec4(0.40f, 0.40f, 0.55f, 0.80f); |
| Colors[ImGuiCol_ScrollbarBg] = ImVec4(0.20f, 0.25f, 0.30f, 0.60f); |
| 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_CheckMark] = 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.67f, 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.53f, 0.53f, 0.87f, 0.80f); |
| Colors[ImGuiCol_Separator] = ImVec4(0.50f, 0.50f, 0.50f, 1.00f); |
| Colors[ImGuiCol_SeparatorHovered] = ImVec4(0.60f, 0.60f, 0.70f, 1.00f); |
| Colors[ImGuiCol_SeparatorActive] = ImVec4(0.70f, 0.70f, 0.90f, 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_ModalWindowDarkening] = ImVec4(0.20f, 0.20f, 0.20f, 0.35f); |
| } |
| |
| ImGuiIO::ImGuiIO() |
| { |
| // Most fields are initialized with zero |
| memset(this, 0, sizeof(*this)); |
| |
| // Settings |
| DisplaySize = ImVec2(-1.0f, -1.0f); |
| DeltaTime = 1.0f/60.0f; |
| IniSavingRate = 5.0f; |
| IniFilename = "imgui.ini"; |
| LogFilename = "imgui_log.txt"; |
| Fonts = &GImDefaultFontAtlas; |
| FontGlobalScale = 1.0f; |
| FontDefault = NULL; |
| DisplayFramebufferScale = ImVec2(1.0f, 1.0f); |
| MousePos = ImVec2(-1,-1); |
| MousePosPrev = ImVec2(-1,-1); |
| MouseDoubleClickTime = 0.30f; |
| MouseDoubleClickMaxDist = 6.0f; |
| MouseDragThreshold = 6.0f; |
| for (int i = 0; i < IM_ARRAYSIZE(MouseDownDuration); i++) |
| MouseDownDuration[i] = MouseDownDurationPrev[i] = -1.0f; |
| for (int i = 0; i < IM_ARRAYSIZE(KeysDownDuration); i++) |
| KeysDownDuration[i] = KeysDownDurationPrev[i] = -1.0f; |
| for (int i = 0; i < ImGuiKey_COUNT; i++) |
| KeyMap[i] = -1; |
| KeyRepeatDelay = 0.250f; |
| KeyRepeatRate = 0.050f; |
| UserData = NULL; |
| |
| // User functions |
| RenderDrawListsFn = NULL; |
| MemAllocFn = malloc; |
| MemFreeFn = free; |
| GetClipboardTextFn = GetClipboardTextFn_DefaultImpl; // Platform dependent default implementations |
| SetClipboardTextFn = SetClipboardTextFn_DefaultImpl; |
| ClipboardUserData = NULL; |
| ImeSetInputScreenPosFn = ImeSetInputScreenPosFn_DefaultImpl; |
| ImeWindowHandle = NULL; |
| |
| // Set OS X style defaults based on __APPLE__ compile time flag |
| #ifdef __APPLE__ |
| OSXBehaviors = true; |
| #endif |
| } |
| |
| // 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 WM_CHAR message |
| void ImGuiIO::AddInputCharacter(ImWchar c) |
| { |
| const int n = ImStrlenW(InputCharacters); |
| if (n + 1 < IM_ARRAYSIZE(InputCharacters)) |
| { |
| InputCharacters[n] = c; |
| InputCharacters[n+1] = '\0'; |
| } |
| } |
| |
| void ImGuiIO::AddInputCharactersUTF8(const char* utf8_chars) |
| { |
| // We can't pass more wchars than ImGuiIO::InputCharacters[] can hold so don't convert more |
| const int wchars_buf_len = sizeof(ImGuiIO::InputCharacters) / sizeof(ImWchar); |
| ImWchar wchars[wchars_buf_len]; |
| ImTextStrFromUtf8(wchars, wchars_buf_len, utf8_chars, NULL); |
| for (int i = 0; i < wchars_buf_len && wchars[i] != 0; i++) |
| AddInputCharacter(wchars[i]); |
| } |
| |
| //----------------------------------------------------------------------------- |
| // HELPERS |
| //----------------------------------------------------------------------------- |
| |
| #define IM_F32_TO_INT8_UNBOUND(_VAL) ((int)((_VAL) * 255.0f + ((_VAL)>=0 ? 0.5f : -0.5f))) // Unsaturated, for display purpose |
| #define IM_F32_TO_INT8_SAT(_VAL) ((int)(ImSaturate(_VAL) * 255.0f + 0.5f)) // Saturated, always output 0..255 |
| |
| // Play it nice with Windows users. Notepad in 2015 still doesn't display text data with Unix-style \n. |
| #ifdef _WIN32 |
| #define IM_NEWLINE "\r\n" |
| #else |
| #define IM_NEWLINE "\n" |
| #endif |
| |
| ImVec2 ImLineClosestPoint(const ImVec2& a, const ImVec2& b, const ImVec2& p) |
| { |
| ImVec2 ap = p - a; |
| ImVec2 ab_dir = b - a; |
| float ab_len = sqrtf(ab_dir.x * ab_dir.x + ab_dir.y * ab_dir.y); |
| ab_dir *= 1.0f / ab_len; |
| float dot = ap.x * ab_dir.x + ap.y * ab_dir.y; |
| if (dot < 0.0f) |
| return a; |
| if (dot > ab_len) |
| return b; |
| return a + ab_dir * dot; |
| } |
| |
| bool ImTriangleContainsPoint(const ImVec2& a, const ImVec2& b, const ImVec2& c, const ImVec2& p) |
| { |
| bool b1 = ((p.x - b.x) * (a.y - b.y) - (p.y - b.y) * (a.x - b.x)) < 0.0f; |
| bool b2 = ((p.x - c.x) * (b.y - c.y) - (p.y - c.y) * (b.x - c.x)) < 0.0f; |
| bool b3 = ((p.x - a.x) * (c.y - a.y) - (p.y - a.y) * (c.x - a.x)) < 0.0f; |
| return ((b1 == b2) && (b2 == b3)); |
| } |
| |
| void ImTriangleBarycentricCoords(const ImVec2& a, const ImVec2& b, const ImVec2& c, const ImVec2& p, float& out_u, float& out_v, float& out_w) |
| { |
| ImVec2 v0 = b - a; |
| ImVec2 v1 = c - a; |
| ImVec2 v2 = p - a; |
| const float denom = v0.x * v1.y - v1.x * v0.y; |
| out_v = (v2.x * v1.y - v1.x * v2.y) / denom; |
| out_w = (v0.x * v2.y - v2.x * v0.y) / denom; |
| out_u = 1.0f - out_v - out_w; |
| } |
| |
| ImVec2 ImTriangleClosestPoint(const ImVec2& a, const ImVec2& b, const ImVec2& c, const ImVec2& p) |
| { |
| ImVec2 proj_ab = ImLineClosestPoint(a, b, p); |
| ImVec2 proj_bc = ImLineClosestPoint(b, c, p); |
| ImVec2 proj_ca = ImLineClosestPoint(c, a, p); |
| float dist2_ab = ImLengthSqr(p - proj_ab); |
| float dist2_bc = ImLengthSqr(p - proj_bc); |
| float dist2_ca = ImLengthSqr(p - proj_ca); |
| float m = ImMin(dist2_ab, ImMin(dist2_bc, dist2_ca)); |
| if (m == dist2_ab) |
| return proj_ab; |
| if (m == dist2_bc) |
| return proj_bc; |
| return proj_ca; |
| } |
| |
| int ImStricmp(const char* str1, const char* str2) |
| { |
| int d; |
| while ((d = toupper(*str2) - toupper(*str1)) == 0 && *str1) { str1++; str2++; } |
| return d; |
| } |
| |
| int ImStrnicmp(const char* str1, const char* str2, int count) |
| { |
| int d = 0; |
| while (count > 0 && (d = toupper(*str2) - toupper(*str1)) == 0 && *str1) { str1++; str2++; count--; } |
| return d; |
| } |
| |
| void ImStrncpy(char* dst, const char* src, int count) |
| { |
| if (count < 1) return; |
| strncpy(dst, src, (size_t)count); |
| dst[count-1] = 0; |
| } |
| |
| char* ImStrdup(const char *str) |
| { |
| size_t len = strlen(str) + 1; |
| void* buff = ImGui::MemAlloc(len); |
| return (char*)memcpy(buff, (const void*)str, len); |
| } |
| |
| int ImStrlenW(const ImWchar* str) |
| { |
| int n = 0; |
| while (*str++) n++; |
| return n; |
| } |
| |
| const ImWchar* ImStrbolW(const ImWchar* buf_mid_line, const ImWchar* buf_begin) // find beginning-of-line |
| { |
| while (buf_mid_line > buf_begin && buf_mid_line[-1] != '\n') |
| buf_mid_line--; |
| return buf_mid_line; |
| } |
| |
| const char* ImStristr(const char* haystack, const char* haystack_end, const char* needle, const char* needle_end) |
| { |
| if (!needle_end) |
| needle_end = needle + strlen(needle); |
| |
| const char un0 = (char)toupper(*needle); |
| while ((!haystack_end && *haystack) || (haystack_end && haystack < haystack_end)) |
| { |
| 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; |
| } |
| |
| // MSVC version appears to return -1 on overflow, whereas glibc appears to return total count (which may be >= buf_size). |
| // Ideally we would test for only one of those limits at runtime depending on the behavior the vsnprintf(), but trying to deduct it at compile time sounds like a pandora can of worm. |
| int ImFormatString(char* buf, int buf_size, const char* fmt, ...) |
| { |
| IM_ASSERT(buf_size > 0); |
| va_list args; |
| va_start(args, fmt); |
| int w = vsnprintf(buf, buf_size, fmt, args); |
| va_end(args); |
| if (w == -1 || w >= buf_size) |
| w = buf_size - 1; |
| buf[w] = 0; |
| return w; |
| } |
| |
| int ImFormatStringV(char* buf, int buf_size, const char* fmt, va_list args) |
| { |
| IM_ASSERT(buf_size > 0); |
| int w = vsnprintf(buf, buf_size, fmt, args); |
| if (w == -1 || w >= buf_size) |
| w = buf_size - 1; |
| buf[w] = 0; |
| return w; |
| } |
| |
| // Pass data_size==0 for zero-terminated strings |
| // FIXME-OPT: Replace with e.g. FNV1a hash? CRC32 pretty much randomly access 1KB. Need to do proper measurements. |
| ImU32 ImHash(const void* data, int data_size, ImU32 seed) |
| { |
| 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) ^ (ImU32(-int(crc & 1)) & polynomial); |
| crc32_lut[i] = crc; |
| } |
| } |
| |
| seed = ~seed; |
| ImU32 crc = seed; |
| const unsigned char* current = (const unsigned char*)data; |
| |
| if (data_size > 0) |
| { |
| // Known size |
| while (data_size--) |
| crc = (crc >> 8) ^ crc32_lut[(crc & 0xFF) ^ *current++]; |
| } |
| else |
| { |
| // Zero-terminated string |
| while (unsigned char c = *current++) |
| { |
| // We support a syntax of "label###id" where only "###id" is included in the hash, and only "label" gets displayed. |
| // Because this syntax is rarely used we are optimizing for the common case. |
| // - If we reach ### in the string we discard the hash so far and reset to the seed. |
| // - We don't do 'current += 2; continue;' after handling ### to keep the code smaller. |
| if (c == '#' && current[0] == '#' && current[1] == '#') |
| crc = seed; |
| crc = (crc >> 8) ^ crc32_lut[(crc & 0xFF) ^ c]; |
| } |
| } |
| return ~crc; |
| } |
| |
| //----------------------------------------------------------------------------- |
| // ImText* helpers |
| //----------------------------------------------------------------------------- |
| |
| // Convert UTF-8 to 32-bits character, process single character input. |
| // Based on stb_from_utf8() from github.com/nothings/stb/ |
| // We handle UTF-8 decoding error by skipping forward. |
| int ImTextCharFromUtf8(unsigned int* out_char, const char* in_text, const char* in_text_end) |
| { |
| unsigned int c = (unsigned int)-1; |
| const unsigned char* str = (const unsigned char*)in_text; |
| if (!(*str & 0x80)) |
| { |
| c = (unsigned int)(*str++); |
| *out_char = c; |
| return 1; |
| } |
| if ((*str & 0xe0) == 0xc0) |
| { |
| *out_char = 0xFFFD; // will be invalid but not end of string |
| if (in_text_end && in_text_end - (const char*)str < 2) return 1; |
| if (*str < 0xc2) return 2; |
| c = (unsigned int)((*str++ & 0x1f) << 6); |
| if ((*str & 0xc0) != 0x80) return 2; |
| c += (*str++ & 0x3f); |
| *out_char = c; |
| return 2; |
| } |
| if ((*str & 0xf0) == 0xe0) |
| { |
| *out_char = 0xFFFD; // will be invalid but not end of string |
| if (in_text_end && in_text_end - (const char*)str < 3) return 1; |
| if (*str == 0xe0 && (str[1] < 0xa0 || str[1] > 0xbf)) return 3; |
| if (*str == 0xed && str[1] > 0x9f) return 3; // str[1] < 0x80 is checked below |
| c = (unsigned int)((*str++ & 0x0f) << 12); |
| if ((*str & 0xc0) != 0x80) return 3; |
| c += (unsigned int)((*str++ & 0x3f) << 6); |
| if ((*str & 0xc0) != 0x80) return 3; |
| c += (*str++ & 0x3f); |
| *out_char = c; |
| return 3; |
| } |
| if ((*str & 0xf8) == 0xf0) |
| { |
| *out_char = 0xFFFD; // will be invalid but not end of string |
| if (in_text_end && in_text_end - (const char*)str < 4) return 1; |
| if (*str > 0xf4) return 4; |
| if (*str == 0xf0 && (str[1] < 0x90 || str[1] > 0xbf)) return 4; |
| if (*str == 0xf4 && str[1] > 0x8f) return 4; // str[1] < 0x80 is checked below |
| c = (unsigned int)((*str++ & 0x07) << 18); |
| if ((*str & 0xc0) != 0x80) return 4; |
| c += (unsigned int)((*str++ & 0x3f) << 12); |
| if ((*str & 0xc0) != 0x80) return 4; |
| c += (unsigned int)((*str++ & 0x3f) << 6); |
| if ((*str & 0xc0) != 0x80) return 4; |
| c += (*str++ & 0x3f); |
| // utf-8 encodings of values used in surrogate pairs are invalid |
| if ((c & 0xFFFFF800) == 0xD800) return 4; |
| *out_char = c; |
| return 4; |
| } |
| *out_char = 0; |
| return 0; |
| } |
| |
| int ImTextStrFromUtf8(ImWchar* buf, int buf_size, const char* in_text, const char* in_text_end, const char** in_text_remaining) |
| { |
| ImWchar* buf_out = buf; |
| ImWchar* buf_end = buf + buf_size; |
| while (buf_out < buf_end-1 && (!in_text_end || in_text < in_text_end) && *in_text) |
| { |
| unsigned int c; |
| in_text += ImTextCharFromUtf8(&c, in_text, in_text_end); |
| if (c == 0) |
| break; |
| if (c < 0x10000) // FIXME: Losing characters that don't fit in 2 bytes |
| *buf_out++ = (ImWchar)c; |
| } |
| *buf_out = 0; |
| if (in_text_remaining) |
| *in_text_remaining = in_text; |
| return (int)(buf_out - buf); |
| } |
| |
| int ImTextCountCharsFromUtf8(const char* in_text, const char* in_text_end) |
| { |
| int char_count = 0; |
| while ((!in_text_end || in_text < in_text_end) && *in_text) |
| { |
| unsigned int c; |
| in_text += ImTextCharFromUtf8(&c, in_text, in_text_end); |
| if (c == 0) |
| break; |
| if (c < 0x10000) |
| char_count++; |
| } |
| return char_count; |
| } |
| |
| // Based on stb_to_utf8() from github.com/nothings/stb/ |
| static inline int ImTextCharToUtf8(char* buf, int buf_size, unsigned int c) |
| { |
| if (c < 0x80) |
| { |
| buf[0] = (char)c; |
| return 1; |
| } |
| if (c < 0x800) |
| { |
| if (buf_size < 2) return 0; |
| buf[0] = (char)(0xc0 + (c >> 6)); |
| buf[1] = (char)(0x80 + (c & 0x3f)); |
| return 2; |
| } |
| if (c >= 0xdc00 && c < 0xe000) |
| { |
| return 0; |
| } |
| if (c >= 0xd800 && c < 0xdc00) |
| { |
| if (buf_size < 4) return 0; |
| buf[0] = (char)(0xf0 + (c >> 18)); |
| buf[1] = (char)(0x80 + ((c >> 12) & 0x3f)); |
| buf[2] = (char)(0x80 + ((c >> 6) & 0x3f)); |
| buf[3] = (char)(0x80 + ((c ) & 0x3f)); |
| return 4; |
| } |
| //else if (c < 0x10000) |
| { |
| if (buf_size < 3) return 0; |
| buf[0] = (char)(0xe0 + (c >> 12)); |
| buf[1] = (char)(0x80 + ((c>> 6) & 0x3f)); |
| buf[2] = (char)(0x80 + ((c ) & 0x3f)); |
| return 3; |
| } |
| } |
| |
| static inline int ImTextCountUtf8BytesFromChar(unsigned int c) |
| { |
| if (c < 0x80) return 1; |
| if (c < 0x800) return 2; |
| if (c >= 0xdc00 && c < 0xe000) return 0; |
| if (c >= 0xd800 && c < 0xdc00) return 4; |
| return 3; |
| } |
| |
| int ImTextStrToUtf8(char* buf, int buf_size, const ImWchar* in_text, const ImWchar* in_text_end) |
| { |
| char* buf_out = buf; |
| const char* buf_end = buf + buf_size; |
| while (buf_out < buf_end-1 && (!in_text_end || in_text < in_text_end) && *in_text) |
| { |
| unsigned int c = (unsigned int)(*in_text++); |
| if (c < 0x80) |
| *buf_out++ = (char)c; |
| else |
| buf_out += ImTextCharToUtf8(buf_out, (int)(buf_end-buf_out-1), c); |
| } |
| *buf_out = 0; |
| return (int)(buf_out - buf); |
| } |
| |
| int ImTextCountUtf8BytesFromStr(const ImWchar* in_text, const ImWchar* in_text_end) |
| { |
| int bytes_count = 0; |
| while ((!in_text_end || in_text < in_text_end) && *in_text) |
| { |
| unsigned int c = (unsigned int)(*in_text++); |
| if (c < 0x80) |
| bytes_count++; |
| else |
| bytes_count += ImTextCountUtf8BytesFromChar(c); |
| } |
| return bytes_count; |
| } |
| |
| ImVec4 ImGui::ColorConvertU32ToFloat4(ImU32 in) |
| { |
| float s = 1.0f/255.0f; |
| return ImVec4( |
| ((in >> IM_COL32_R_SHIFT) & 0xFF) * s, |
| ((in >> IM_COL32_G_SHIFT) & 0xFF) * s, |
| ((in >> IM_COL32_B_SHIFT) & 0xFF) * s, |
| ((in >> IM_COL32_A_SHIFT) & 0xFF) * s); |
| } |
| |
| ImU32 ImGui::ColorConvertFloat4ToU32(const ImVec4& in) |
| { |
| ImU32 out; |
| out = ((ImU32)IM_F32_TO_INT8_SAT(in.x)) << IM_COL32_R_SHIFT; |
| out |= ((ImU32)IM_F32_TO_INT8_SAT(in.y)) << IM_COL32_G_SHIFT; |
| out |= ((ImU32)IM_F32_TO_INT8_SAT(in.z)) << IM_COL32_B_SHIFT; |
| out |= ((ImU32)IM_F32_TO_INT8_SAT(in.w)) << IM_COL32_A_SHIFT; |
| return out; |
| } |
| |
| ImU32 ImGui::GetColorU32(ImGuiCol idx, float alpha_mul) |
| { |
| ImGuiStyle& style = GImGui->Style; |
| ImVec4 c = style.Colors[idx]; |
| c.w *= style.Alpha * alpha_mul; |
| return ColorConvertFloat4ToU32(c); |
| } |
| |
| ImU32 ImGui::GetColorU32(const ImVec4& col) |
| { |
| ImGuiStyle& style = GImGui->Style; |
| ImVec4 c = col; |
| c.w *= style.Alpha; |
| return ColorConvertFloat4ToU32(c); |
| } |
| |
| const ImVec4& ImGui::GetStyleColorVec4(ImGuiCol idx) |
| { |
| ImGuiStyle& style = GImGui->Style; |
| return style.Colors[idx]; |
| } |
| |
| ImU32 ImGui::GetColorU32(ImU32 col) |
| { |
| float style_alpha = GImGui->Style.Alpha; |
| if (style_alpha >= 1.0f) |
| return col; |
| int a = (col & IM_COL32_A_MASK) >> IM_COL32_A_SHIFT; |
| a = (int)(a * style_alpha); // We don't need to clamp 0..255 because Style.Alpha is in 0..1 range. |
| return (col & ~IM_COL32_A_MASK) | (a << IM_COL32_A_SHIFT); |
| } |
| |
| // 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 |
| void ImGui::ColorConvertRGBtoHSV(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 = fabsf(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 |
| void ImGui::ColorConvertHSVtoRGB(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; |
| } |
| } |
| |
| FILE* ImFileOpen(const char* filename, const char* mode) |
| { |
| #if defined(_WIN32) && !defined(__CYGWIN__) |
| // We need a fopen() wrapper because MSVC/Windows fopen doesn't handle UTF-8 filenames. Converting both strings from UTF-8 to wchar format (using a single allocation, because we can) |
| const int filename_wsize = ImTextCountCharsFromUtf8(filename, NULL) + 1; |
| const int mode_wsize = ImTextCountCharsFromUtf8(mode, NULL) + 1; |
| ImVector<ImWchar> buf; |
| buf.resize(filename_wsize + mode_wsize); |
| ImTextStrFromUtf8(&buf[0], filename_wsize, filename, NULL); |
| ImTextStrFromUtf8(&buf[filename_wsize], mode_wsize, mode, NULL); |
| return _wfopen((wchar_t*)&buf[0], (wchar_t*)&buf[filename_wsize]); |
| #else |
| return fopen(filename, mode); |
| #endif |
| } |
| |
| // Load file content into memory |
| // Memory allocated with ImGui::MemAlloc(), must be freed by user using ImGui::MemFree() |
| void* ImFileLoadToMemory(const char* filename, const char* file_open_mode, int* out_file_size, int padding_bytes) |
| { |
| IM_ASSERT(filename && file_open_mode); |
| if (out_file_size) |
| *out_file_size = 0; |
| |
| FILE* f; |
| if ((f = ImFileOpen(filename, file_open_mode)) == NULL) |
| return NULL; |
| |
| long file_size_signed; |
| if (fseek(f, 0, SEEK_END) || (file_size_signed = ftell(f)) == -1 || fseek(f, 0, SEEK_SET)) |
| { |
| fclose(f); |
| return NULL; |
| } |
| |
| int file_size = (int)file_size_signed; |
| void* file_data = ImGui::MemAlloc(file_size + padding_bytes); |
| if (file_data == NULL) |
| { |
| fclose(f); |
| return NULL; |
| } |
| if (fread(file_data, 1, (size_t)file_size, f) != (size_t)file_size) |
| { |
| fclose(f); |
| ImGui::MemFree(file_data); |
| return NULL; |
| } |
| if (padding_bytes > 0) |
| memset((void *)(((char*)file_data) + file_size), 0, padding_bytes); |
| |
| fclose(f); |
| if (out_file_size) |
| *out_file_size = file_size; |
| |
| return file_data; |
| } |
| |
| //----------------------------------------------------------------------------- |
| // ImGuiStorage |
| //----------------------------------------------------------------------------- |
| |
| // Helper: Key->value storage |
| void ImGuiStorage::Clear() |
| { |
| Data.clear(); |
| } |
| |
| // std::lower_bound but without the bullshit |
| static ImVector<ImGuiStorage::Pair>::iterator LowerBound(ImVector<ImGuiStorage::Pair>& data, ImGuiID 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::GetInt(ImGuiID key, int default_val) const |
| { |
| ImVector<Pair>::iterator it = LowerBound(const_cast<ImVector<ImGuiStorage::Pair>&>(Data), key); |
| if (it == Data.end() || it->key != key) |
| return default_val; |
| return it->val_i; |
| } |
| |
| bool ImGuiStorage::GetBool(ImGuiID key, bool default_val) const |
| { |
| return GetInt(key, default_val ? 1 : 0) != 0; |
| } |
| |
| float ImGuiStorage::GetFloat(ImGuiID key, float default_val) const |
| { |
| ImVector<Pair>::iterator it = LowerBound(const_cast<ImVector<ImGuiStorage::Pair>&>(Data), key); |
| if (it == Data.end() || it->key != key) |
| return default_val; |
| return it->val_f; |
| } |
| |
| void* ImGuiStorage::GetVoidPtr(ImGuiID key) const |
| { |
| ImVector<Pair>::iterator it = LowerBound(const_cast<ImVector<ImGuiStorage::Pair>&>(Data), key); |
| if (it == Data.end() || it->key != key) |
| return NULL; |
| return it->val_p; |
| } |
| |
| // References are only valid until a new value is added to the storage. Calling a Set***() function or a Get***Ref() function invalidates the pointer. |
| int* ImGuiStorage::GetIntRef(ImGuiID key, int default_val) |
| { |
| ImVector<Pair>::iterator it = LowerBound(Data, key); |
| if (it == Data.end() || it->key != key) |
| it = Data.insert(it, Pair(key, default_val)); |
| return &it->val_i; |
| } |
| |
| bool* ImGuiStorage::GetBoolRef(ImGuiID key, bool default_val) |
| { |
| return (bool*)GetIntRef(key, default_val ? 1 : 0); |
| } |
| |
| float* ImGuiStorage::GetFloatRef(ImGuiID key, float default_val) |
| { |
| ImVector<Pair>::iterator it = LowerBound(Data, key); |
| if (it == Data.end() || it->key != key) |
| it = Data.insert(it, Pair(key, default_val)); |
| return &it->val_f; |
| } |
| |
| void** ImGuiStorage::GetVoidPtrRef(ImGuiID key, void* default_val) |
| { |
| ImVector<Pair>::iterator it = LowerBound(Data, key); |
| if (it == Data.end() || it->key != key) |
| it = Data.insert(it, Pair(key, default_val)); |
| return &it->val_p; |
| } |
| |
| // FIXME-OPT: Need a way to reuse the result of lower_bound when doing GetInt()/SetInt() - not too bad because it only happens on explicit interaction (maximum one a frame) |
| void ImGuiStorage::SetInt(ImGuiID key, int val) |
| { |
| ImVector<Pair>::iterator it = LowerBound(Data, key); |
| if (it == Data.end() || it->key != key) |
| { |
| Data.insert(it, Pair(key, val)); |
| return; |
| } |
| it->val_i = val; |
| } |
| |
| void ImGuiStorage::SetBool(ImGuiID key, bool val) |
| { |
| SetInt(key, val ? 1 : 0); |
| } |
| |
| void ImGuiStorage::SetFloat(ImGuiID key, float val) |
| { |
| ImVector<Pair>::iterator it = LowerBound(Data, key); |
| if (it == Data.end() || it->key != key) |
| { |
| Data.insert(it, Pair(key, val)); |
| return; |
| } |
| it->val_f = val; |
| } |
| |
| void ImGuiStorage::SetVoidPtr(ImGuiID key, void* val) |
| { |
| ImVector<Pair>::iterator it = LowerBound(Data, key); |
| if (it == Data.end() || it->key != key) |
| { |
| Data.insert(it, Pair(key, val)); |
| return; |
| } |
| it->val_p = val; |
| } |
| |
| void ImGuiStorage::SetAllInt(int v) |
| { |
| for (int i = 0; i < Data.Size; i++) |
| Data[i].val_i = v; |
| } |
| |
| //----------------------------------------------------------------------------- |
| // ImGuiTextFilter |
| //----------------------------------------------------------------------------- |
| |
| // Helper: Parse and apply text filters. In format "aaaaa[,bbbb][,ccccc]" |
| ImGuiTextFilter::ImGuiTextFilter(const char* default_filter) |
| { |
| if (default_filter) |
| { |
| ImStrncpy(InputBuf, default_filter, IM_ARRAYSIZE(InputBuf)); |
| Build(); |
| } |
| else |
| { |
| InputBuf[0] = 0; |
| CountGrep = 0; |
| } |
| } |
| |
| bool ImGuiTextFilter::Draw(const char* label, float width) |
| { |
| if (width != 0.0f) |
| ImGui::PushItemWidth(width); |
| bool value_changed = ImGui::InputText(label, InputBuf, IM_ARRAYSIZE(InputBuf)); |
| if (width != 0.0f) |
| ImGui::PopItemWidth(); |
| if (value_changed) |
| Build(); |
| return value_changed; |
| } |
| |
| 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 (int 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* text, const char* text_end) const |
| { |
| if (Filters.empty()) |
| return true; |
| |
| if (text == NULL) |
| text = ""; |
| |
| for (int i = 0; i != Filters.Size; i++) |
| { |
| const TextRange& f = Filters[i]; |
| if (f.empty()) |
| continue; |
| if (f.front() == '-') |
| { |
| // Subtract |
| if (ImStristr(text, text_end, f.begin()+1, f.end()) != NULL) |
| return false; |
| } |
| else |
| { |
| // Grep |
| if (ImStristr(text, text_end, f.begin(), f.end()) != NULL) |
| return true; |
| } |
| } |
| |
| // Implicit * grep |
| if (CountGrep == 0) |
| return true; |
| |
| return false; |
| } |
| |
| //----------------------------------------------------------------------------- |
| // ImGuiTextBuffer |
| //----------------------------------------------------------------------------- |
| |
| // On some platform vsnprintf() takes va_list by reference and modifies it. |
| // va_copy is the 'correct' way to copy a va_list but Visual Studio prior to 2013 doesn't have it. |
| #ifndef va_copy |
| #define va_copy(dest, src) (dest = src) |
| #endif |
| |
| // Helper: Text buffer for logging/accumulating text |
| void ImGuiTextBuffer::appendv(const char* fmt, va_list args) |
| { |
| va_list args_copy; |
| va_copy(args_copy, args); |
| |
| int len = vsnprintf(NULL, 0, fmt, args); // FIXME-OPT: could do a first pass write attempt, likely successful on first pass. |
| if (len <= 0) |
| return; |
| |
| const int write_off = Buf.Size; |
| const int needed_sz = write_off + len; |
| if (write_off + len >= Buf.Capacity) |
| { |
| int double_capacity = Buf.Capacity * 2; |
| Buf.reserve(needed_sz > double_capacity ? needed_sz : double_capacity); |
| } |
| |
| Buf.resize(needed_sz); |
| ImFormatStringV(&Buf[write_off] - 1, len+1, fmt, args_copy); |
| } |
| |
| void ImGuiTextBuffer::append(const char* fmt, ...) |
| { |
| va_list args; |
| va_start(args, fmt); |
| appendv(fmt, args); |
| va_end(args); |
| } |
| |
| //----------------------------------------------------------------------------- |
| // ImGuiSimpleColumns |
| //----------------------------------------------------------------------------- |
| |
| ImGuiSimpleColumns::ImGuiSimpleColumns() |
| { |
| Count = 0; |
| Spacing = Width = NextWidth = 0.0f; |
| memset(Pos, 0, sizeof(Pos)); |
| memset(NextWidths, 0, sizeof(NextWidths)); |
| } |
| |
| void ImGuiSimpleColumns::Update(int count, float spacing, bool clear) |
| { |
| IM_ASSERT(Count <= IM_ARRAYSIZE(Pos)); |
| Count = count; |
| Width = NextWidth = 0.0f; |
| Spacing = spacing; |
| if (clear) memset(NextWidths, 0, sizeof(NextWidths)); |
| for (int i = 0; i < Count; i++) |
| { |
| if (i > 0 && NextWidths[i] > 0.0f) |
| Width += Spacing; |
| Pos[i] = (float)(int)Width; |
| Width += NextWidths[i]; |
| NextWidths[i] = 0.0f; |
| } |
| } |
| |
| float ImGuiSimpleColumns::DeclColumns(float w0, float w1, float w2) // not using va_arg because they promote float to double |
| { |
| NextWidth = 0.0f; |
| NextWidths[0] = ImMax(NextWidths[0], w0); |
| NextWidths[1] = ImMax(NextWidths[1], w1); |
| NextWidths[2] = ImMax(NextWidths[2], w2); |
| for (int i = 0; i < 3; i++) |
| NextWidth += NextWidths[i] + ((i > 0 && NextWidths[i] > 0.0f) ? Spacing : 0.0f); |
| return ImMax(Width, NextWidth); |
| } |
| |
| float ImGuiSimpleColumns::CalcExtraSpace(float avail_w) |
| { |
| return ImMax(0.0f, avail_w - Width); |
| } |
| |
| //----------------------------------------------------------------------------- |
| // ImGuiListClipper |
| //----------------------------------------------------------------------------- |
| |
| static void SetCursorPosYAndSetupDummyPrevLine(float pos_y, float line_height) |
| { |
| // Set cursor position and a few other things so that SetScrollHere() and Columns() can work when seeking cursor. |
| // FIXME: It is problematic that we have to do that here, because custom/equivalent end-user code would stumble on the same issue. Consider moving within SetCursorXXX functions? |
| ImGui::SetCursorPosY(pos_y); |
| ImGuiWindow* window = ImGui::GetCurrentWindow(); |
| window->DC.CursorPosPrevLine.y = window->DC.CursorPos.y - line_height; // Setting those fields so that SetScrollHere() can properly function after the end of our clipper usage. |
| window->DC.PrevLineHeight = (line_height - GImGui->Style.ItemSpacing.y); // If we end up needing more accurate data (to e.g. use SameLine) we may as well make the clipper have a fourth step to let user process and display the last item in their list. |
| if (window->DC.ColumnsCount > 1) |
| window->DC.ColumnsCellMinY = window->DC.CursorPos.y; // Setting this so that cell Y position are set properly |
| } |
| |
| // Use case A: Begin() called from constructor with items_height<0, then called again from Sync() in StepNo 1 |
| // Use case B: Begin() called from constructor with items_height>0 |
| // FIXME-LEGACY: Ideally we should remove the Begin/End functions but they are part of the legacy API we still support. This is why some of the code in Step() calling Begin() and reassign some fields, spaghetti style. |
| void ImGuiListClipper::Begin(int count, float items_height) |
| { |
| StartPosY = ImGui::GetCursorPosY(); |
| ItemsHeight = items_height; |
| ItemsCount = count; |
| StepNo = 0; |
| DisplayEnd = DisplayStart = -1; |
| if (ItemsHeight > 0.0f) |
| { |
| ImGui::CalcListClipping(ItemsCount, ItemsHeight, &DisplayStart, &DisplayEnd); // calculate how many to clip/display |
| if (DisplayStart > 0) |
| SetCursorPosYAndSetupDummyPrevLine(StartPosY + DisplayStart * ItemsHeight, ItemsHeight); // advance cursor |
| StepNo = 2; |
| } |
| } |
| |
| void ImGuiListClipper::End() |
| { |
| if (ItemsCount < 0) |
| return; |
| // In theory here we should assert that ImGui::GetCursorPosY() == StartPosY + DisplayEnd * ItemsHeight, but it feels saner to just seek at the end and not assert/crash the user. |
| if (ItemsCount < INT_MAX) |
| SetCursorPosYAndSetupDummyPrevLine(StartPosY + ItemsCount * ItemsHeight, ItemsHeight); // advance cursor |
| ItemsCount = -1; |
| StepNo = 3; |
| } |
| |
| bool ImGuiListClipper::Step() |
| { |
| if (ItemsCount == 0 || ImGui::GetCurrentWindowRead()->SkipItems) |
| { |
| ItemsCount = -1; |
| return false; |
| } |
| if (StepNo == 0) // Step 0: the clipper let you process the first element, regardless of it being visible or not, so we can measure the element height. |
| { |
| DisplayStart = 0; |
| DisplayEnd = 1; |
| StartPosY = ImGui::GetCursorPosY(); |
| StepNo = 1; |
| return true; |
| } |
| if (StepNo == 1) // Step 1: the clipper infer height from first element, calculate the actual range of elements to display, and position the cursor before the first element. |
| { |
| if (ItemsCount == 1) { ItemsCount = -1; return false; } |
| float items_height = ImGui::GetCursorPosY() - StartPosY; |
| IM_ASSERT(items_height > 0.0f); // If this triggers, it means Item 0 hasn't moved the cursor vertically |
| Begin(ItemsCount-1, items_height); |
| DisplayStart++; |
| DisplayEnd++; |
| StepNo = 3; |
| return true; |
| } |
| if (StepNo == 2) // Step 2: dummy step only required if an explicit items_height was passed to constructor or Begin() and user still call Step(). Does nothing and switch to Step 3. |
| { |
| IM_ASSERT(DisplayStart >= 0 && DisplayEnd >= 0); |
| StepNo = 3; |
| return true; |
| } |
| if (StepNo == 3) // Step 3: the clipper validate that we have reached the expected Y position (corresponding to element DisplayEnd), advance the cursor to the end of the list and then returns 'false' to end the loop. |
| End(); |
| return false; |
| } |
| |
| //----------------------------------------------------------------------------- |
| // ImGuiWindow |
| //----------------------------------------------------------------------------- |
| |
| ImGuiWindow::ImGuiWindow(const char* name) |
| { |
| Name = ImStrdup(name); |
| ID = ImHash(name, 0); |
| IDStack.push_back(ID); |
| MoveId = GetID("#MOVE"); |
| |
| Flags = 0; |
| OrderWithinParent = 0; |
| PosFloat = Pos = ImVec2(0.0f, 0.0f); |
| Size = SizeFull = ImVec2(0.0f, 0.0f); |
| SizeContents = SizeContentsExplicit = ImVec2(0.0f, 0.0f); |
| WindowPadding = ImVec2(0.0f, 0.0f); |
| Scroll = ImVec2(0.0f, 0.0f); |
| ScrollTarget = ImVec2(FLT_MAX, FLT_MAX); |
| ScrollTargetCenterRatio = ImVec2(0.5f, 0.5f); |
| ScrollbarX = ScrollbarY = false; |
| ScrollbarSizes = ImVec2(0.0f, 0.0f); |
| BorderSize = 0.0f; |
| Active = WasActive = false; |
| Accessed = false; |
| Collapsed = false; |
| SkipItems = false; |
| BeginCount = 0; |
| PopupId = 0; |
| AutoFitFramesX = AutoFitFramesY = -1; |
| AutoFitOnlyGrows = false; |
| AutoFitChildAxises = 0x00; |
| AutoPosLastDirection = -1; |
| HiddenFrames = 0; |
| SetWindowPosAllowFlags = SetWindowSizeAllowFlags = SetWindowCollapsedAllowFlags = ImGuiCond_Always | ImGuiCond_Once | ImGuiCond_FirstUseEver | ImGuiCond_Appearing; |
| SetWindowPosCenterWanted = false; |
| |
| LastFrameActive = -1; |
| ItemWidthDefault = 0.0f; |
| FontWindowScale = 1.0f; |
| |
| DrawList = (ImDrawList*)ImGui::MemAlloc(sizeof(ImDrawList)); |
| IM_PLACEMENT_NEW(DrawList) ImDrawList(); |
| DrawList->_OwnerName = Name; |
| RootWindow = NULL; |
| RootNonPopupWindow = NULL; |
| ParentWindow = NULL; |
| |
| FocusIdxAllCounter = FocusIdxTabCounter = -1; |
| FocusIdxAllRequestCurrent = FocusIdxTabRequestCurrent = INT_MAX; |
| FocusIdxAllRequestNext = FocusIdxTabRequestNext = INT_MAX; |
| } |
| |
| ImGuiWindow::~ImGuiWindow() |
| { |
| DrawList->~ImDrawList(); |
| ImGui::MemFree(DrawList); |
| DrawList = NULL; |
| ImGui::MemFree(Name); |
| Name = NULL; |
| } |
| |
| ImGuiID ImGuiWindow::GetID(const char* str, const char* str_end) |
| { |
| ImGuiID seed = IDStack.back(); |
| ImGuiID id = ImHash(str, str_end ? (int)(str_end - str) : 0, seed); |
| ImGui::KeepAliveID(id); |
| return id; |
| } |
| |
| ImGuiID ImGuiWindow::GetID(const void* ptr) |
| { |
| ImGuiID seed = IDStack.back(); |
| ImGuiID id = ImHash(&ptr, sizeof(void*), seed); |
| ImGui::KeepAliveID(id); |
| return id; |
| } |
| |
| ImGuiID ImGuiWindow::GetIDNoKeepAlive(const char* str, const char* str_end) |
| { |
| ImGuiID seed = IDStack.back(); |
| return ImHash(str, str_end ? (int)(str_end - str) : 0, seed); |
| } |
| |
| //----------------------------------------------------------------------------- |
| // Internal API exposed in imgui_internal.h |
| //----------------------------------------------------------------------------- |
| |
| static void SetCurrentWindow(ImGuiWindow* window) |
| { |
| ImGuiContext& g = *GImGui; |
| g.CurrentWindow = window; |
| if (window) |
| g.FontSize = window->CalcFontSize(); |
| } |
| |
| ImGuiWindow* ImGui::GetParentWindow() |
| { |
| ImGuiContext& g = *GImGui; |
| IM_ASSERT(g.CurrentWindowStack.Size >= 2); |
| return g.CurrentWindowStack[(unsigned int)g.CurrentWindowStack.Size - 2]; |
| } |
| |
| void ImGui::SetActiveID(ImGuiID id, ImGuiWindow* window) |
| { |
| ImGuiContext& g = *GImGui; |
| g.ActiveId = id; |
| g.ActiveIdAllowOverlap = false; |
| g.ActiveIdIsJustActivated = true; |
| if (id) |
| g.ActiveIdIsAlive = true; |
| g.ActiveIdWindow = window; |
| } |
| |
| void ImGui::ClearActiveID() |
| { |
| SetActiveID(0, NULL); |
| } |
| |
| void ImGui::SetHoveredID(ImGuiID id) |
| { |
| ImGuiContext& g = *GImGui; |
| g.HoveredId = id; |
| g.HoveredIdAllowOverlap = false; |
| } |
| |
| void ImGui::KeepAliveID(ImGuiID id) |
| { |
| ImGuiContext& g = *GImGui; |
| if (g.ActiveId == id) |
| g.ActiveIdIsAlive = true; |
| } |
| |
| // Advance cursor given item size for layout. |
| void ImGui::ItemSize(const ImVec2& size, float text_offset_y) |
| { |
| ImGuiWindow* window = GetCurrentWindow(); |
| if (window->SkipItems) |
| return; |
| |
| // Always align ourselves on pixel boundaries |
| ImGuiContext& g = *GImGui; |
| const float line_height = ImMax(window->DC.CurrentLineHeight, size.y); |
| const float text_base_offset = ImMax(window->DC.CurrentLineTextBaseOffset, text_offset_y); |
| window->DC.CursorPosPrevLine = ImVec2(window->DC.CursorPos.x + size.x, window->DC.CursorPos.y); |
| window->DC.CursorPos = ImVec2((float)(int)(window->Pos.x + window->DC.IndentX + window->DC.ColumnsOffsetX), (float)(int)(window->DC.CursorPos.y + line_height + g.Style.ItemSpacing.y)); |
| window->DC.CursorMaxPos.x = ImMax(window->DC.CursorMaxPos.x, window->DC.CursorPosPrevLine.x); |
| window->DC.CursorMaxPos.y = ImMax(window->DC.CursorMaxPos.y, window->DC.CursorPos.y); |
| |
| //window->DrawList->AddCircle(window->DC.CursorMaxPos, 3.0f, IM_COL32(255,0,0,255), 4); // Debug |
| |
| window->DC.PrevLineHeight = line_height; |
| window->DC.PrevLineTextBaseOffset = text_base_offset; |
| window->DC.CurrentLineHeight = window->DC.CurrentLineTextBaseOffset = 0.0f; |
| } |
| |
| void ImGui::ItemSize(const ImRect& bb, float text_offset_y) |
| { |
| ItemSize(bb.GetSize(), text_offset_y); |
| } |
| |
| // Declare item bounding box for clipping and interaction. |
| // Note that the size can be different than the one provided to ItemSize(). Typically, widgets that spread over available surface |
| // declares their minimum size requirement to ItemSize() and then use a larger region for drawing/interaction, which is passed to ItemAdd(). |
| bool ImGui::ItemAdd(const ImRect& bb, const ImGuiID* id) |
| { |
| ImGuiWindow* window = GetCurrentWindow(); |
| window->DC.LastItemId = id ? *id : 0; |
| window->DC.LastItemRect = bb; |
| window->DC.LastItemHoveredAndUsable = window->DC.LastItemHoveredRect = false; |
| if (IsClippedEx(bb, id, false)) |
| return false; |
| |
| // This is a sensible default, but widgets are free to override it after calling ItemAdd() |
| ImGuiContext& g = *GImGui; |
| if (IsMouseHoveringRect(bb.Min, bb.Max)) |
| { |
| // Matching the behavior of IsHovered() but allow if ActiveId==window->MoveID (we clicked on the window background) |
| // So that clicking on items with no active id such as Text() still returns true with IsItemHovered() |
| window->DC.LastItemHoveredRect = true; |
| if (g.HoveredRootWindow == window->RootWindow) |
| if (g.ActiveId == 0 || (id && g.ActiveId == *id) || g.ActiveIdAllowOverlap || (g.ActiveId == window->MoveId)) |
| if (IsWindowContentHoverable(window)) |
| window->DC.LastItemHoveredAndUsable = true; |
| } |
| |
| return true; |
| } |
| |
| bool ImGui::IsClippedEx(const ImRect& bb, const ImGuiID* id, bool clip_even_when_logged) |
| { |
| ImGuiContext& g = *GImGui; |
| ImGuiWindow* window = GetCurrentWindowRead(); |
| if (!bb.Overlaps(window->ClipRect)) |
| if (!id || *id != GImGui->ActiveId) |
| if (clip_even_when_logged || !g.LogEnabled) |
| return true; |
| return false; |
| } |
| |
| // NB: This is an internal helper. The user-facing IsItemHovered() is using data emitted from ItemAdd(), with a slightly different logic. |
| bool ImGui::IsHovered(const ImRect& bb, ImGuiID id, bool flatten_childs) |
| { |
| ImGuiContext& g = *GImGui; |
| if (g.HoveredId == 0 || g.HoveredId == id || g.HoveredIdAllowOverlap) |
| { |
| ImGuiWindow* window = GetCurrentWindowRead(); |
| if (g.HoveredWindow == window || (flatten_childs && g.HoveredRootWindow == window->RootWindow)) |
| if ((g.ActiveId == 0 || g.ActiveId == id || g.ActiveIdAllowOverlap) && IsMouseHoveringRect(bb.Min, bb.Max)) |
| if (IsWindowContentHoverable(g.HoveredRootWindow)) |
| return true; |
| } |
| return false; |
| } |
| |
| bool ImGui::FocusableItemRegister(ImGuiWindow* window, bool is_active, bool tab_stop) |
| { |
| ImGuiContext& g = *GImGui; |
| |
| const bool allow_keyboard_focus = window->DC.AllowKeyboardFocus; |
| window->FocusIdxAllCounter++; |
| if (allow_keyboard_focus) |
| window->FocusIdxTabCounter++; |
| |
| // Process keyboard input at this point: TAB, Shift-TAB switch focus |
| // We can always TAB out of a widget that doesn't allow tabbing in. |
| if (tab_stop && window->FocusIdxAllRequestNext == INT_MAX && window->FocusIdxTabRequestNext == INT_MAX && is_active && IsKeyPressedMap(ImGuiKey_Tab)) |
| { |
| // Modulo on index will be applied at the end of frame once we've got the total counter of items. |
| window->FocusIdxTabRequestNext = window->FocusIdxTabCounter + (g.IO.KeyShift ? (allow_keyboard_focus ? -1 : 0) : +1); |
| } |
| |
| if (window->FocusIdxAllCounter == window->FocusIdxAllRequestCurrent) |
| return true; |
| |
| if (allow_keyboard_focus) |
| if (window->FocusIdxTabCounter == window->FocusIdxTabRequestCurrent) |
| return true; |
| |
| return false; |
| } |
| |
| void ImGui::FocusableItemUnregister(ImGuiWindow* window) |
| { |
| window->FocusIdxAllCounter--; |
| window->FocusIdxTabCounter--; |
| } |
| |
| ImVec2 ImGui::CalcItemSize(ImVec2 size, float default_x, float default_y) |
| { |
| ImGuiContext& g = *GImGui; |
| ImVec2 content_max; |
| if (size.x < 0.0f || size.y < 0.0f) |
| content_max = g.CurrentWindow->Pos + GetContentRegionMax(); |
| if (size.x <= 0.0f) |
| size.x = (size.x == 0.0f) ? default_x : ImMax(content_max.x - g.CurrentWindow->DC.CursorPos.x, 4.0f) + size.x; |
| if (size.y <= 0.0f) |
| size.y = (size.y == 0.0f) ? default_y : ImMax(content_max.y - g.CurrentWindow->DC.CursorPos.y, 4.0f) + size.y; |
| return size; |
| } |
| |
| float ImGui::CalcWrapWidthForPos(const ImVec2& pos, float wrap_pos_x) |
| { |
| if (wrap_pos_x < 0.0f) |
| return 0.0f; |
| |
| ImGuiWindow* window = GetCurrentWindowRead(); |
| if (wrap_pos_x == 0.0f) |
| wrap_pos_x = GetContentRegionMax().x + window->Pos.x; |
| else if (wrap_pos_x > 0.0f) |
| wrap_pos_x += window->Pos.x - window->Scroll.x; // wrap_pos_x is provided is window local space |
| |
| return ImMax(wrap_pos_x - pos.x, 1.0f); |
| } |
| |
| //----------------------------------------------------------------------------- |
| |
| void* ImGui::MemAlloc(size_t sz) |
| { |
| GImGui->IO.MetricsAllocs++; |
| return GImGui->IO.MemAllocFn(sz); |
| } |
| |
| void ImGui::MemFree(void* ptr) |
| { |
| if (ptr) GImGui->IO.MetricsAllocs--; |
| return GImGui->IO.MemFreeFn(ptr); |
| } |
| |
| const char* ImGui::GetClipboardText() |
| { |
| return GImGui->IO.GetClipboardTextFn ? GImGui->IO.GetClipboardTextFn(GImGui->IO.ClipboardUserData) : ""; |
| } |
| |
| void ImGui::SetClipboardText(const char* text) |
| { |
| if (GImGui->IO.SetClipboardTextFn) |
| GImGui->IO.SetClipboardTextFn(GImGui->IO.ClipboardUserData, text); |
| } |
| |
| const char* ImGui::GetVersion() |
| { |
| return IMGUI_VERSION; |
| } |
| |
| // Internal state access - if you want to share ImGui state between modules (e.g. DLL) or allocate it yourself |
| // Note that we still point to some static data and members (such as GFontAtlas), so the state instance you end up using will point to the static data within its module |
| ImGuiContext* ImGui::GetCurrentContext() |
| { |
| return GImGui; |
| } |
| |
| void ImGui::SetCurrentContext(ImGuiContext* ctx) |
| { |
| #ifdef IMGUI_SET_CURRENT_CONTEXT_FUNC |
| IMGUI_SET_CURRENT_CONTEXT_FUNC(ctx); // For custom thread-based hackery you may want to have control over this. |
| #else |
| GImGui = ctx; |
| #endif |
| } |
| |
| ImGuiContext* ImGui::CreateContext(void* (*malloc_fn)(size_t), void (*free_fn)(void*)) |
| { |
| if (!malloc_fn) malloc_fn = malloc; |
| ImGuiContext* ctx = (ImGuiContext*)malloc_fn(sizeof(ImGuiContext)); |
| IM_PLACEMENT_NEW(ctx) ImGuiContext(); |
| ctx->IO.MemAllocFn = malloc_fn; |
| ctx->IO.MemFreeFn = free_fn ? free_fn : free; |
| return ctx; |
| } |
| |
| void ImGui::DestroyContext(ImGuiContext* ctx) |
| { |
| void (*free_fn)(void*) = ctx->IO.MemFreeFn; |
| ctx->~ImGuiContext(); |
| free_fn(ctx); |
| if (GImGui == ctx) |
| SetCurrentContext(NULL); |
| } |
| |
| ImGuiIO& ImGui::GetIO() |
| { |
| return GImGui->IO; |
| } |
| |
| ImGuiStyle& ImGui::GetStyle() |
| { |
| return GImGui->Style; |
| } |
| |
| // Same value as passed to your RenderDrawListsFn() function. valid after Render() and until the next call to NewFrame() |
| ImDrawData* ImGui::GetDrawData() |
| { |
| return GImGui->RenderDrawData.Valid ? &GImGui->RenderDrawData : NULL; |
| } |
| |
| float ImGui::GetTime() |
| { |
| return GImGui->Time; |
| } |
| |
| int ImGui::GetFrameCount() |
| { |
| return GImGui->FrameCount; |
| } |
| |
| void ImGui::NewFrame() |
| { |
| ImGuiContext& g = *GImGui; |
| |
| // Check user data |
| IM_ASSERT(g.IO.DeltaTime >= 0.0f); // Need a positive DeltaTime (zero is tolerated but will cause some timing issues) |
| IM_ASSERT(g.IO.DisplaySize.x >= 0.0f && g.IO.DisplaySize.y >= 0.0f); |
| IM_ASSERT(g.IO.Fonts->Fonts.Size > 0); // Font Atlas not created. Did you call io.Fonts->GetTexDataAsRGBA32 / GetTexDataAsAlpha8 ? |
| IM_ASSERT(g.IO.Fonts->Fonts[0]->IsLoaded()); // Font Atlas not created. Did you call io.Fonts->GetTexDataAsRGBA32 / GetTexDataAsAlpha8 ? |
| IM_ASSERT(g.Style.CurveTessellationTol > 0.0f); // Invalid style setting |
| IM_ASSERT(g.Style.Alpha >= 0.0f && g.Style.Alpha <= 1.0f); // Invalid style setting. Alpha cannot be negative (allows us to avoid a few clamps in color computations) |
| |
| if (!g.Initialized) |
| { |
| // Initialize on first frame |
| g.LogClipboard = (ImGuiTextBuffer*)ImGui::MemAlloc(sizeof(ImGuiTextBuffer)); |
| IM_PLACEMENT_NEW(g.LogClipboard) ImGuiTextBuffer(); |
| |
| IM_ASSERT(g.Settings.empty()); |
| LoadIniSettingsFromDisk(g.IO.IniFilename); |
| g.Initialized = true; |
| } |
| |
| SetCurrentFont(GetDefaultFont()); |
| IM_ASSERT(g.Font->IsLoaded()); |
| |
| g.Time += g.IO.DeltaTime; |
| g.FrameCount += 1; |
| g.TooltipOverrideCount = 0; |
| g.OverlayDrawList.Clear(); |
| g.OverlayDrawList.PushTextureID(g.IO.Fonts->TexID); |
| g.OverlayDrawList.PushClipRectFullScreen(); |
| |
| // Mark rendering data as invalid to prevent user who may have a handle on it to use it |
| g.RenderDrawData.Valid = false; |
| g.RenderDrawData.CmdLists = NULL; |
| g.RenderDrawData.CmdListsCount = g.RenderDrawData.TotalVtxCount = g.RenderDrawData.TotalIdxCount = 0; |
| |
| |
| // Update mouse input 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 (int i = 0; i < IM_ARRAYSIZE(g.IO.MouseDown); i++) |
| { |
| g.IO.MouseClicked[i] = g.IO.MouseDown[i] && g.IO.MouseDownDuration[i] < 0.0f; |
| g.IO.MouseReleased[i] = !g.IO.MouseDown[i] && g.IO.MouseDownDuration[i] >= 0.0f; |
| g.IO.MouseDownDurationPrev[i] = g.IO.MouseDownDuration[i]; |
| g.IO.MouseDownDuration[i] = g.IO.MouseDown[i] ? (g.IO.MouseDownDuration[i] < 0.0f ? 0.0f : g.IO.MouseDownDuration[i] + g.IO.DeltaTime) : -1.0f; |
| g.IO.MouseDoubleClicked[i] = false; |
| if (g.IO.MouseClicked[i]) |
| { |
| if (g.Time - g.IO.MouseClickedTime[i] < g.IO.MouseDoubleClickTime) |
| { |
| if (ImLengthSqr(g.IO.MousePos - g.IO.MouseClickedPos[i]) < g.IO.MouseDoubleClickMaxDist * 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; |
| g.IO.MouseDragMaxDistanceSqr[i] = 0.0f; |
| } |
| else if (g.IO.MouseDown[i]) |
| { |
| g.IO.MouseDragMaxDistanceSqr[i] = ImMax(g.IO.MouseDragMaxDistanceSqr[i], ImLengthSqr(g.IO.MousePos - g.IO.MouseClickedPos[i])); |
| } |
| } |
| memcpy(g.IO.KeysDownDurationPrev, g.IO.KeysDownDuration, sizeof(g.IO.KeysDownDuration)); |
| for (int i = 0; i < IM_ARRAYSIZE(g.IO.KeysDown); i++) |
| g.IO.KeysDownDuration[i] = g.IO.KeysDown[i] ? (g.IO.KeysDownDuration[i] < 0.0f ? 0.0f : g.IO.KeysDownDuration[i] + g.IO.DeltaTime) : -1.0f; |
| |
| // Calculate frame-rate for the user, as a purely luxurious feature |
| g.FramerateSecPerFrameAccum += g.IO.DeltaTime - g.FramerateSecPerFrame[g.FramerateSecPerFrameIdx]; |
| g.FramerateSecPerFrame[g.FramerateSecPerFrameIdx] = g.IO.DeltaTime; |
| g.FramerateSecPerFrameIdx = (g.FramerateSecPerFrameIdx + 1) % IM_ARRAYSIZE(g.FramerateSecPerFrame); |
| g.IO.Framerate = 1.0f / (g.FramerateSecPerFrameAccum / (float)IM_ARRAYSIZE(g.FramerateSecPerFrame)); |
| |
| // Clear reference to active widget if the widget isn't alive anymore |
| g.HoveredIdPreviousFrame = g.HoveredId; |
| g.HoveredId = 0; |
| g.HoveredIdAllowOverlap = false; |
| if (!g.ActiveIdIsAlive && g.ActiveIdPreviousFrame == g.ActiveId && g.ActiveId != 0) |
| ClearActiveID(); |
| g.ActiveIdPreviousFrame = g.ActiveId; |
| g.ActiveIdIsAlive = false; |
| g.ActiveIdIsJustActivated = false; |
| |
| // Handle user moving window (at the beginning of the frame to avoid input lag or sheering). Only valid for root windows. |
| if (g.MovedWindowMoveId && g.MovedWindowMoveId == g.ActiveId) |
| { |
| KeepAliveID(g.MovedWindowMoveId); |
| IM_ASSERT(g.MovedWindow && g.MovedWindow->RootWindow); |
| IM_ASSERT(g.MovedWindow->RootWindow->MoveId == g.MovedWindowMoveId); |
| if (g.IO.MouseDown[0]) |
| { |
| if (!(g.MovedWindow->Flags & ImGuiWindowFlags_NoMove)) |
| { |
| g.MovedWindow->PosFloat += g.IO.MouseDelta; |
| if (!(g.MovedWindow->Flags & ImGuiWindowFlags_NoSavedSettings) && (g.IO.MouseDelta.x != 0.0f || g.IO.MouseDelta.y != 0.0f)) |
| MarkIniSettingsDirty(g.MovedWindow); |
| } |
| FocusWindow(g.MovedWindow); |
| } |
| else |
| { |
| ClearActiveID(); |
| g.MovedWindow = NULL; |
| g.MovedWindowMoveId = 0; |
| } |
| } |
| else |
| { |
| g.MovedWindow = NULL; |
| g.MovedWindowMoveId = 0; |
| } |
| |
| // 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) |
| SaveIniSettingsToDisk(g.IO.IniFilename); |
| } |
| |
| // Find the window we are hovering. Child windows can extend beyond the limit of their parent so we need to derive HoveredRootWindow from HoveredWindow |
| g.HoveredWindow = g.MovedWindow ? g.MovedWindow : FindHoveredWindow(g.IO.MousePos, false); |
| if (g.HoveredWindow && (g.HoveredWindow->Flags & ImGuiWindowFlags_ChildWindow)) |
| g.HoveredRootWindow = g.HoveredWindow->RootWindow; |
| else |
| g.HoveredRootWindow = g.MovedWindow ? g.MovedWindow->RootWindow : FindHoveredWindow(g.IO.MousePos, true); |
| |
| if (ImGuiWindow* modal_window = GetFrontMostModalRootWindow()) |
| { |
| g.ModalWindowDarkeningRatio = ImMin(g.ModalWindowDarkeningRatio + g.IO.DeltaTime * 6.0f, 1.0f); |
| ImGuiWindow* window = g.HoveredRootWindow; |
| while (window && window != modal_window) |
| window = window->ParentWindow; |
| if (!window) |
| g.HoveredRootWindow = g.HoveredWindow = NULL; |
| } |
| else |
| { |
| g.ModalWindowDarkeningRatio = 0.0f; |
| } |
| |
| // Are we using inputs? Tell user so they can capture/discard the inputs away from the rest of their application. |
| // When clicking outside of a window we assume the click is owned by the application and won't request capture. We need to track click ownership. |
| int mouse_earliest_button_down = -1; |
| bool mouse_any_down = false; |
| for (int i = 0; i < IM_ARRAYSIZE(g.IO.MouseDown); i++) |
| { |
| if (g.IO.MouseClicked[i]) |
| g.IO.MouseDownOwned[i] = (g.HoveredWindow != NULL) || (!g.OpenPopupStack.empty()); |
| mouse_any_down |= g.IO.MouseDown[i]; |
| if (g.IO.MouseDown[i]) |
| if (mouse_earliest_button_down == -1 || g.IO.MouseClickedTime[mouse_earliest_button_down] > g.IO.MouseClickedTime[i]) |
| mouse_earliest_button_down = i; |
| } |
| bool mouse_avail_to_imgui = (mouse_earliest_button_down == -1) || g.IO.MouseDownOwned[mouse_earliest_button_down]; |
| if (g.CaptureMouseNextFrame != -1) |
| g.IO.WantCaptureMouse = (g.CaptureMouseNextFrame != 0); |
| else |
| g.IO.WantCaptureMouse = (mouse_avail_to_imgui && (g.HoveredWindow != NULL || mouse_any_down)) || (g.ActiveId != 0) || (!g.OpenPopupStack.empty()); |
| g.IO.WantCaptureKeyboard = (g.CaptureKeyboardNextFrame != -1) ? (g.CaptureKeyboardNextFrame != 0) : (g.ActiveId != 0); |
| g.IO.WantTextInput = (g.ActiveId != 0 && g.InputTextState.Id == g.ActiveId); |
| g.MouseCursor = ImGuiMouseCursor_Arrow; |
| g.CaptureMouseNextFrame = g.CaptureKeyboardNextFrame = -1; |
| g.OsImePosRequest = ImVec2(1.0f, 1.0f); // OS Input Method Editor showing on top-left of our window by default |
| |
| // If mouse was first clicked outside of ImGui bounds we also cancel out hovering. |
| if (!mouse_avail_to_imgui) |
| g.HoveredWindow = g.HoveredRootWindow = NULL; |
| |
| // Scale & Scrolling |
| if (g.HoveredWindow && g.IO.MouseWheel != 0.0f && !g.HoveredWindow->Collapsed) |
| { |
| ImGuiWindow* window = g.HoveredWindow; |
| if (g.IO.KeyCtrl && g.IO.FontAllowUserScaling) |
| { |
| // Zoom / Scale window |
| const float new_font_scale = ImClamp(window->FontWindowScale + g.IO.MouseWheel * 0.10f, 0.50f, 2.50f); |
| const float scale = new_font_scale / window->FontWindowScale; |
| window->FontWindowScale = new_font_scale; |
| |
| const 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 if (!g.IO.KeyCtrl && !(window->Flags & ImGuiWindowFlags_NoScrollWithMouse)) |
| { |
| // Scroll |
| const int scroll_lines = (window->Flags & ImGuiWindowFlags_ComboBox) ? 3 : 5; |
| SetWindowScrollY(window, window->Scroll.y - g.IO.MouseWheel * window->CalcFontSize() * scroll_lines); |
| } |
| } |
| |
| // 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.NavWindow != NULL && g.NavWindow->Active && IsKeyPressedMap(ImGuiKey_Tab, false)) |
| g.NavWindow->FocusIdxTabRequestNext = 0; |
| |
| // Mark all windows as not visible |
| for (int i = 0; i != g.Windows.Size; i++) |
| { |
| ImGuiWindow* window = g.Windows[i]; |
| window->WasActive = window->Active; |
| window->Active = false; |
| window->Accessed = false; |
| } |
| |
| // Closing the focused window restore focus to the first active root window in descending z-order |
| if (g.NavWindow && !g.NavWindow->WasActive) |
| for (int i = g.Windows.Size-1; i >= 0; i--) |
| if (g.Windows[i]->WasActive && !(g.Windows[i]->Flags & ImGuiWindowFlags_ChildWindow)) |
| { |
| FocusWindow(g.Windows[i]); |
| break; |
| } |
| |
| // No window should be open at the beginning of the frame. |
| // But in order to allow the user to call NewFrame() multiple times without calling Render(), we are doing an explicit clear. |
| g.CurrentWindowStack.resize(0); |
| g.CurrentPopupStack.resize(0); |
| CloseInactivePopups(); |
| |
| // Create implicit window - we will only render it if the user has added something to it. |
| ImGui::SetNextWindowSize(ImVec2(400,400), ImGuiCond_FirstUseEver); |
| ImGui::Begin("Debug"); |
| } |
| |
| // NB: behavior of ImGui after Shutdown() is not tested/guaranteed at the moment. This function is merely here to free heap allocations. |
| void ImGui::Shutdown() |
| { |
| ImGuiContext& g = *GImGui; |
| |
| // The fonts atlas can be used prior to calling NewFrame(), so we clear it even if g.Initialized is FALSE (which would happen if we never called NewFrame) |
| if (g.IO.Fonts) // Testing for NULL to allow user to NULLify in case of running Shutdown() on multiple contexts. Bit hacky. |
| g.IO.Fonts->Clear(); |
| |
| // Cleanup of other data are conditional on actually having used ImGui. |
| if (!g.Initialized) |
| return; |
| |
| SaveIniSettingsToDisk(g.IO.IniFilename); |
| |
| for (int i = 0; i < g.Windows.Size; i++) |
| { |
| g.Windows[i]->~ImGuiWindow(); |
| ImGui::MemFree(g.Windows[i]); |
| } |
| g.Windows.clear(); |
| g.WindowsSortBuffer.clear(); |
| g.CurrentWindow = NULL; |
| g.CurrentWindowStack.clear(); |
| g.NavWindow = NULL; |
| g.HoveredWindow = NULL; |
| g.HoveredRootWindow = NULL; |
| g.ActiveIdWindow = NULL; |
| g.MovedWindow = NULL; |
| for (int i = 0; i < g.Settings.Size; i++) |
| ImGui::MemFree(g.Settings[i].Name); |
| g.Settings.clear(); |
| g.ColorModifiers.clear(); |
| g.StyleModifiers.clear(); |
| g.FontStack.clear(); |
| g.OpenPopupStack.clear(); |
| g.CurrentPopupStack.clear(); |
| g.SetNextWindowSizeConstraintCallback = NULL; |
| g.SetNextWindowSizeConstraintCallbackUserData = NULL; |
| for (int i = 0; i < IM_ARRAYSIZE(g.RenderDrawLists); i++) |
| g.RenderDrawLists[i].clear(); |
| g.OverlayDrawList.ClearFreeMemory(); |
| g.PrivateClipboard.clear(); |
| g.InputTextState.Text.clear(); |
| g.InputTextState.InitialText.clear(); |
| g.InputTextState.TempTextBuffer.clear(); |
| |
| if (g.LogFile && g.LogFile != stdout) |
| { |
| fclose(g.LogFile); |
| g.LogFile = NULL; |
| } |
| if (g.LogClipboard) |
| { |
| g.LogClipboard->~ImGuiTextBuffer(); |
| ImGui::MemFree(g.LogClipboard); |
| } |
| |
| g.Initialized = false; |
| } |
| |
| static ImGuiIniData* FindWindowSettings(const char* name) |
| { |
| ImGuiContext& g = *GImGui; |
| ImGuiID id = ImHash(name, 0); |
| for (int i = 0; i != g.Settings.Size; i++) |
| { |
| ImGuiIniData* ini = &g.Settings[i]; |
| if (ini->Id == id) |
| return ini; |
| } |
| return NULL; |
| } |
| |
| static ImGuiIniData* AddWindowSettings(const char* name) |
| { |
| GImGui->Settings.resize(GImGui->Settings.Size + 1); |
| ImGuiIniData* ini = &GImGui->Settings.back(); |
| ini->Name = ImStrdup(name); |
| ini->Id = ImHash(name, 0); |
| ini->Collapsed = false; |
| ini->Pos = ImVec2(FLT_MAX,FLT_MAX); |
| ini->Size = ImVec2(0,0); |
| return ini; |
| } |
| |
| // Zero-tolerance, poor-man .ini parsing |
| // FIXME: Write something less rubbish |
| static void LoadIniSettingsFromDisk(const char* ini_filename) |
| { |
| ImGuiContext& g = *GImGui; |
| if (!ini_filename) |
| return; |
| |
| int file_size; |
| char* file_data = (char*)ImFileLoadToMemory(ini_filename, "rb", &file_size, 1); |
| if (!file_data) |
| return; |
| |
| ImGuiIniData* settings = NULL; |
| const char* buf_end = file_data + file_size; |
| for (const char* line_start = file_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, IM_ARRAYSIZE(name), "%.*s", (int)(line_end-line_start-2), line_start+1); |
| settings = FindWindowSettings(name); |
| if (!settings) |
| settings = AddWindowSettings(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; |
| } |
| |
| ImGui::MemFree(file_data); |
| } |
| |
| static void SaveIniSettingsToDisk(const char* ini_filename) |
| { |
| ImGuiContext& g = *GImGui; |
| g.SettingsDirtyTimer = 0.0f; |
| if (!ini_filename) |
| return; |
| |
| // Gather data from windows that were active during this session |
| for (int i = 0; i != g.Windows.Size; i++) |
| { |
| ImGuiWindow* window = g.Windows[i]; |
| if (window->Flags & ImGuiWindowFlags_NoSavedSettings) |
| continue; |
| ImGuiIniData* settings = FindWindowSettings(window->Name); |
| if (!settings) // This will only return NULL in the rare instance where the window was first created with ImGuiWindowFlags_NoSavedSettings then had the flag disabled later on. We don't bind settings in this case (bug #1000). |
| continue; |
| 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 = ImFileOpen(ini_filename, "wt"); |
| if (!f) |
| return; |
| for (int i = 0; i != g.Settings.Size; i++) |
| { |
| const ImGuiIniData* settings = &g.Settings[i]; |
| if (settings->Pos.x == FLT_MAX) |
| continue; |
| const char* name = settings->Name; |
| if (const char* p = strstr(name, "###")) // Skip to the "###" marker if any. We don't skip past to match the behavior of GetID() |
| name = p; |
| fprintf(f, "[%s]\n", name); |
| fprintf(f, "Pos=%d,%d\n", (int)settings->Pos.x, (int)settings->Pos.y); |
| fprintf(f, "Size=%d,%d\n", (int)settings->Size.x, (int)settings->Size.y); |
| fprintf(f, "Collapsed=%d\n", settings->Collapsed); |
| fprintf(f, "\n"); |
| } |
| |
| fclose(f); |
| } |
| |
| static void MarkIniSettingsDirty(ImGuiWindow* window) |
| { |
| ImGuiContext& g = *GImGui; |
| if (!(window->Flags & ImGuiWindowFlags_NoSavedSettings)) |
| if (g.SettingsDirtyTimer <= 0.0f) |
| g.SettingsDirtyTimer = g.IO.IniSavingRate; |
| } |
| |
| // FIXME: Add a more explicit sort order in the window structure. |
| static int ChildWindowComparer(const void* lhs, const void* rhs) |
| { |
| const ImGuiWindow* a = *(const ImGuiWindow**)lhs; |
| const ImGuiWindow* b = *(const ImGuiWindow**)rhs; |
| if (int d = (a->Flags & ImGuiWindowFlags_Popup) - (b->Flags & ImGuiWindowFlags_Popup)) |
| return d; |
| if (int d = (a->Flags & ImGuiWindowFlags_Tooltip) - (b->Flags & ImGuiWindowFlags_Tooltip)) |
| return d; |
| if (int d = (a->Flags & ImGuiWindowFlags_ComboBox) - (b->Flags & ImGuiWindowFlags_ComboBox)) |
| return d; |
| return (a->OrderWithinParent - b->OrderWithinParent); |
| } |
| |
| static void AddWindowToSortedBuffer(ImVector<ImGuiWindow*>& out_sorted_windows, ImGuiWindow* window) |
| { |
| out_sorted_windows.push_back(window); |
| if (window->Active) |
| { |
| int count = window->DC.ChildWindows.Size; |
| if (count > 1) |
| qsort(window->DC.ChildWindows.begin(), (size_t)count, sizeof(ImGuiWindow*), ChildWindowComparer); |
| for (int i = 0; i < count; i++) |
| { |
| ImGuiWindow* child = window->DC.ChildWindows[i]; |
| if (child->Active) |
| AddWindowToSortedBuffer(out_sorted_windows, child); |
| } |
| } |
| } |
| |
| static void AddDrawListToRenderList(ImVector<ImDrawList*>& out_render_list, ImDrawList* draw_list) |
| { |
| if (draw_list->CmdBuffer.empty()) |
| return; |
| |
| // Remove trailing command if unused |
| ImDrawCmd& last_cmd = draw_list->CmdBuffer.back(); |
| if (last_cmd.ElemCount == 0 && last_cmd.UserCallback == NULL) |
| { |
| draw_list->CmdBuffer.pop_back(); |
| if (draw_list->CmdBuffer.empty()) |
| return; |
| } |
| |
| // Draw list sanity check. Detect mismatch between PrimReserve() calls and incrementing _VtxCurrentIdx, _VtxWritePtr etc. May trigger for you if you are using PrimXXX functions incorrectly. |
| IM_ASSERT(draw_list->VtxBuffer.Size == 0 || draw_list->_VtxWritePtr == draw_list->VtxBuffer.Data + draw_list->VtxBuffer.Size); |
| IM_ASSERT(draw_list->IdxBuffer.Size == 0 || draw_list->_IdxWritePtr == draw_list->IdxBuffer.Data + draw_list->IdxBuffer.Size); |
| IM_ASSERT((int)draw_list->_VtxCurrentIdx == draw_list->VtxBuffer.Size); |
| |
| // Check that draw_list doesn't use more vertices than indexable in a single draw call (default ImDrawIdx = unsigned short = 2 bytes = 64K vertices per window) |
| // If this assert triggers because you are drawing lots of stuff manually, you can: |
| // A) Add '#define ImDrawIdx unsigned int' in imconfig.h to set the index size to 4 bytes. You'll need to handle the 4-bytes indices to your renderer. |
| // For example, the OpenGL example code detect index size at compile-time by doing: |
| // 'glDrawElements(GL_TRIANGLES, (GLsizei)pcmd->ElemCount, sizeof(ImDrawIdx) == 2 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT, idx_buffer_offset);' |
| // Your own engine or render API may use different parameters or function calls to specify index sizes. 2 and 4 bytes indices are generally supported by most API. |
| // B) If for some reason you cannot use 4 bytes indices or don't want to, a workaround is to call BeginChild()/EndChild() before reaching the 64K limit to split your draw commands in multiple draw lists. |
| IM_ASSERT(((ImU64)draw_list->_VtxCurrentIdx >> (sizeof(ImDrawIdx)*8)) == 0); // Too many vertices in same ImDrawList. See comment above. |
| |
| out_render_list.push_back(draw_list); |
| GImGui->IO.MetricsRenderVertices += draw_list->VtxBuffer.Size; |
| GImGui->IO.MetricsRenderIndices += draw_list->IdxBuffer.Size; |
| } |
| |
| static void AddWindowToRenderList(ImVector<ImDrawList*>& out_render_list, ImGuiWindow* window) |
| { |
| AddDrawListToRenderList(out_render_list, window->DrawList); |
| for <
|