2015-07-27 10:18:04 +08:00
// ImGui library v1.44 WIP
2015-07-22 07:00:28 +08:00
// Main code & documentation
// See ImGui::ShowTestWindow() in imgui_demo.cpp for demo code.
2014-08-11 04:58:11 +08:00
// Read 'Programmer guide' below for notes on how to setup ImGui in your codebase.
// Get latest version at https://github.com/ocornut/imgui
2015-08-06 01:04:13 +08:00
// Releases change-log at https://github.com/ocornut/imgui/releases
2015-04-14 19:00:12 +08:00
// Developed by Omar Cornut and ImGui contributors.
2014-08-11 04:58:11 +08:00
/*
2014-11-10 09:29:01 +08:00
Index
- MISSION STATEMENT
- END - USER GUIDE
2015-03-08 05:54:46 +08:00
- PROGRAMMER GUIDE ( read me ! )
- API BREAKING CHANGES ( read me when you update ! )
2015-03-22 07:14:21 +08:00
- FREQUENTLY ASKED QUESTIONS ( FAQ ) , TIPS
2015-04-18 16:19:41 +08:00
- How do I update to a newer version of ImGui ?
2015-05-20 06:33:55 +08:00
- Can I have multiple widgets with the same label ? Can I have widget without a label ? ( Yes )
2015-03-22 07:14:21 +08:00
- Why is my text output blurry ?
- How can I load a different font than the default ?
- How can I load multiple fonts ?
2015-06-24 04:13:22 +08:00
- How can I display and input non - latin characters such as Chinese , Japanese , Korean , Cyrillic ?
2014-11-10 09:29:01 +08:00
- ISSUES & TODO - LIST
- CODE
2014-08-11 04:58:11 +08:00
MISSION STATEMENT
2014-09-24 21:06:34 +08:00
= = = = = = = = = = = = = = = = =
2014-08-11 04:58:11 +08:00
- easy to use to create code - driven and data - driven tools
2014-10-25 18:00:24 +08:00
- easy to use to create ad hoc short - lived tools and long - lived , more elaborate tools
2014-08-11 04:58:11 +08:00
- easy to hack and improve
- minimize screen real - estate usage
2014-11-10 09:29:01 +08:00
- minimize setup and maintenance
2014-08-11 04:58:11 +08:00
- minimize state storage on user side
2015-03-08 05:54:46 +08:00
- portable , minimize dependencies , run on target ( consoles , phones , etc . )
2014-10-25 18:00:24 +08:00
- efficient runtime ( NB - we do allocate when " growing " content - creating a window / opening a tree node for the first time , etc . - but a typical frame won ' t allocate anything )
2015-03-08 05:54:46 +08:00
- read about immediate - mode gui principles @ http : //mollyrocket.com/861, http://mollyrocket.com/forums/index.html
2014-08-11 04:58:11 +08:00
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
2015-03-19 03:00:03 +08:00
- occasionally uses statically sized buffers for string manipulations - won ' t crash , but some very long pieces of text may be clipped . functions like ImGui : : TextUnformatted ( ) don ' t have such restriction .
2015-03-08 05:54:46 +08:00
2014-12-04 19:42:13 +08:00
2014-09-24 21:06:34 +08:00
END - USER GUIDE
= = = = = = = = = = = = = =
2014-08-11 04:58:11 +08:00
- double - click title bar to collapse window
2014-12-22 21:29:39 +08:00
- click upper right corner to close a window , available when ' bool * p_opened ' is passed to ImGui : : Begin ( )
2014-08-11 04:58:11 +08:00
- 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 )
2015-05-27 04:36:44 +08:00
- CTRL + Click on a slider or drag box to input value as text
2014-08-11 04:58:11 +08:00
- 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 ! )
2014-12-04 19:42:13 +08:00
2014-08-11 04:58:11 +08:00
PROGRAMMER GUIDE
2014-09-24 21:06:34 +08:00
= = = = = = = = = = = = = = = =
2014-08-11 04:58:11 +08:00
2015-03-08 05:54:46 +08:00
- 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 .
2015-07-22 07:00:28 +08:00
- call and read ImGui : : ShowTestWindow ( ) for demo code demonstrating most features .
2015-03-19 03:00:03 +08:00
- see examples / folder for standalone sample applications . e . g . examples / opengl_example /
- customization : PushStyleColor ( ) / PushStyleVar ( ) or the style editor to tweak the look of the interface ( e . g . if you want a more compact UI or a different color scheme ) .
2015-02-04 16:01:00 +08:00
2014-08-11 04:58:11 +08:00
- getting started :
2015-03-19 03:00:03 +08:00
- initialisation : call ImGui : : GetIO ( ) to retrieve the ImGuiIO structure and fill the ' Settings ' data .
2014-08-11 04:58:11 +08:00
- every frame :
1 / in your mainloop or right after you got your keyboard / mouse info , call ImGui : : GetIO ( ) and fill the ' Input ' data , then call ImGui : : NewFrame ( ) .
2 / use any ImGui function you want between NewFrame ( ) and Render ( )
2014-08-27 01:27:10 +08:00
3 / ImGui : : Render ( ) to render all the accumulated command - lists . it will call your RenderDrawListFn handler that you set in the IO structure .
2015-03-19 03:00:03 +08:00
- all rendering information are stored into command - lists until ImGui : : Render ( ) is called .
- ImGui never touches or know about your GPU state . the only function that knows about GPU is the RenderDrawListFn handler that you must provide .
2014-08-11 04:58:11 +08:00
- effectively it means you can create widgets at any time in your code , regardless of " update " vs " render " considerations .
2014-08-31 15:23:55 +08:00
- refer to the examples applications in the examples / folder for instruction on how to setup your code .
2014-08-11 04:58:11 +08:00
- a typical application skeleton may be :
2014-08-19 19:09:13 +08:00
// Application init
ImGuiIO & io = ImGui : : GetIO ( ) ;
io . DisplaySize . x = 1920.0f ;
io . DisplaySize . y = 1280.0f ;
io . DeltaTime = 1.0f / 60.0f ;
io . IniFilename = " imgui.ini " ;
2015-01-12 05:24:12 +08:00
// TODO: Fill others settings of the io structure
// Load texture
unsigned char * pixels ;
2015-01-22 18:03:24 +08:00
int width , height , bytes_per_pixels ;
io . Fonts - > GetTexDataAsRGBA32 ( pixels , & width , & height , & bytes_per_pixels ) ;
2015-01-18 06:38:10 +08:00
// TODO: copy texture to graphics memory.
2015-01-18 18:46:49 +08:00
// TODO: store your texture pointer/identifier in 'io.Fonts->TexID'
2014-08-19 19:09:13 +08:00
2014-10-27 05:31:34 +08:00
// Application main loop
2014-08-19 19:09:13 +08:00
while ( true )
{
2015-01-18 06:38:10 +08:00
// 1) get low-level input
2014-08-19 19:09:13 +08:00
// e.g. on Win32, GetKeyboardState(), or poll your events, etc.
2015-01-18 06:38:10 +08:00
// 2) TODO: fill all fields of IO structure and call NewFrame
2014-08-19 19:09:13 +08:00
ImGuiIO & io = ImGui : : GetIO ( ) ;
2015-02-04 16:01:00 +08:00
io . MousePos = mouse_pos ;
io . MouseDown [ 0 ] = mouse_button_0 ;
2014-08-19 19:09:13 +08:00
io . KeysDown [ i ] = . . .
ImGui : : NewFrame ( ) ;
2015-01-18 06:38:10 +08:00
// 3) most of your application code here - you can use any of ImGui::* functions at any point in the frame
ImGui : : Begin ( " My window " ) ;
ImGui : : Text ( " Hello, world. " ) ;
ImGui : : End ( ) ;
2014-08-19 19:09:13 +08:00
GameUpdate ( ) ;
GameRender ( ) ;
2015-01-18 06:38:10 +08:00
// 4) render & swap video buffers
2014-08-19 19:09:13 +08:00
ImGui : : Render ( ) ;
// swap video buffer, etc.
}
2014-08-11 04:58:11 +08:00
2015-02-04 16:01:00 +08:00
- after calling ImGui : : NewFrame ( ) you can read back ' io . WantCaptureMouse ' and ' io . WantCaptureKeyboard ' to tell if ImGui
wants to use your inputs . if it does you can discard / hide the inputs from the rest of your application .
2014-12-04 19:42:13 +08:00
2014-11-27 06:27:48 +08:00
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 .
2015-08-06 01:04:13 +08:00
Also read releases logs https : //github.com/ocornut/imgui/releases for more details.
2015-01-29 17:22:32 +08:00
2015-08-05 23:34:07 +08:00
- 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-19 08:45:37 +08:00
- 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-15 21:01:21 +08:00
- 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 .
2015-07-19 08:45:37 +08:00
you need to render your textured triangles with bilinear filtering to benefit from sub - pixel positioning of text .
2015-07-09 07:10:14 +08:00
- 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 .
2015-07-08 08:31:41 +08:00
this necessary change will break your rendering function ! the fix should be very easy . sorry for that : (
2015-07-06 12:26:01 +08:00
- 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 .
2015-07-08 10:17:07 +08:00
- the signature of the io . RenderDrawListsFn handler has changed !
2015-07-06 12:26:01 +08:00
ImGui_XXXX_RenderDrawLists ( ImDrawList * * const cmd_lists , int cmd_lists_count )
became :
ImGui_XXXX_RenderDrawLists ( ImDrawData * draw_data ) .
2015-07-08 10:17:07 +08:00
argument ' cmd_lists ' - > ' draw_data - > CmdLists '
argument ' cmd_lists_count ' - > ' draw_data - > CmdListsCount '
ImDrawList ' commands ' - > ' CmdBuffer '
ImDrawList ' vtx_buffer ' - > ' VtxBuffer '
2015-07-08 10:30:46 +08:00
ImDrawList n / a - > ' IdxBuffer ' ( new )
2015-07-08 10:17:07 +08:00
ImDrawCmd ' vtx_count ' - > ' ElemCount '
2015-07-08 10:30:46 +08:00
ImDrawCmd ' clip_rect ' - > ' ClipRect '
ImDrawCmd ' user_callback ' - > ' UserCallback '
ImDrawCmd ' texture_id ' - > ' TextureId '
2015-07-08 10:17:07 +08:00
- 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 .
2015-07-15 22:45:10 +08:00
- 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 !
2015-07-08 10:17:07 +08:00
- refer to code in the examples / folder or ask on the GitHub if you are unsure of how to upgrade . please upgrade !
2015-07-11 09:36:34 +08:00
- 2015 / 07 / 10 ( 1.43 ) - changed SameLine ( ) parameters from int to float .
2015-07-03 10:53:45 +08:00
- 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-15 06:18:22 +08:00
- 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-06-15 05:19:47 +08:00
- 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 " opened " 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-14 05:04:28 +08:00
- 2015 / 04 / 13 ( 1.38 ) - renamed IsClipped ( ) to IsRectClipped ( ) . Kept inline redirection function ( will obsolete ) .
2015-04-09 23:45:14 +08:00
- 2015 / 04 / 09 ( 1.38 ) - renamed ImDrawList : : AddArc ( ) to ImDrawList : : AddArcFast ( ) for compatibility with future API
2015-04-03 21:07:52 +08:00
- 2015 / 04 / 03 ( 1.38 ) - removed ImGuiCol_CheckHovered , ImGuiCol_CheckActive , replaced with the more general ImGuiCol_FrameBgHovered , ImGuiCol_FrameBgActive .
2015-04-03 22:10:07 +08:00
- 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-05-31 18:01:22 +08:00
- 2015 / 03 / 17 ( 1.36 ) - renamed GetItemBoxMin ( ) / GetItemBoxMax ( ) / IsMouseHoveringBox ( ) to GetItemRectMin ( ) / GetItemRectMax ( ) / IsMouseHoveringRect ( ) . Kept inline redirection function ( will obsolete ) .
2015-03-15 19:10:48 +08:00
- 2015 / 03 / 15 ( 1.36 ) - renamed style . TreeNodeSpacing to style . IndentSpacing , ImGuiStyleVar_TreeNodeSpacing to ImGuiStyleVar_IndentSpacing
2015-03-18 06:36:54 +08:00
- 2015 / 03 / 13 ( 1.36 ) - renamed GetWindowIsFocused ( ) to IsWindowFocused ( ) . Kept inline redirection function ( will obsolete ) .
2015-03-08 23:03:55 +08:00
- 2015 / 03 / 08 ( 1.35 ) - renamed style . ScrollBarWidth to style . ScrollbarWidth
2015-03-18 06:36:54 +08:00
- 2015 / 02 / 27 ( 1.34 ) - renamed OpenNextNode ( bool ) to SetNextTreeNodeOpened ( bool , ImGuiSetCond ) . Kept inline redirection function ( will obsolete ) .
2015-02-27 17:51:11 +08:00
- 2015 / 02 / 27 ( 1.34 ) - renamed ImGuiSetCondition_ * * * to ImGuiSetCond_ * * * , and _FirstUseThisSession becomes _Once .
2015-02-12 01:39:13 +08:00
- 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-11 05:07:26 +08:00
- 2015 / 02 / 10 ( 1.32 ) - renamed GetItemWidth ( ) to CalcItemWidth ( ) to clarify its evolving behavior
2015-02-08 21:37:20 +08:00
- 2015 / 02 / 08 ( 1.31 ) - renamed GetTextLineSpacing ( ) to GetTextLineHeightWithSpacing ( )
2015-02-01 22:39:13 +08:00
- 2015 / 02 / 01 ( 1.31 ) - removed IO . MemReallocFn ( unused )
2015-01-29 17:22:32 +08:00
- 2015 / 01 / 19 ( 1.30 ) - renamed ImGuiStorage : : GetIntPtr ( ) / GetFloatPtr ( ) to GetIntRef ( ) / GetIntRef ( ) because Ptr was conflicting with actual pointer storage functions .
2015-01-18 06:38:10 +08:00
- 2015 / 01 / 11 ( 1.30 ) - big font / image API change ! now loads TTF file . allow for multiple fonts . no need for a PNG loader .
2015-01-18 18:46:49 +08:00
( 1.30 ) - removed GetDefaultFontData ( ) . uses io . Fonts - > GetTextureData * ( ) API to retrieve uncompressed pixels .
2015-01-18 06:38:10 +08:00
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 ;
2015-01-18 18:46:49 +08:00
io . Fonts - > GetTexDataAsRGBA32 ( & pixels , & width , & height ) ;
2015-01-18 06:38:10 +08:00
// <Copy to GPU>
2015-01-18 18:46:49 +08:00
io . Fonts - > TexID = ( your_texture_identifier ) ;
2015-01-18 20:22:17 +08:00
you now have much more flexibility to load multiple TTF fonts and manage the texture buffer for internal needs .
2015-07-22 07:00:28 +08:00
it is now recommended that you sample the font texture with bilinear interpolation .
2015-01-18 18:46:49 +08:00
( 1.30 ) - added texture identifier in ImDrawCmd passed to your render function ( we can now render images ) . make sure to set io . Fonts - > TexID .
2015-01-18 06:38:10 +08:00
( 1.30 ) - removed IO . PixelCenterOffset ( unnecessary , can be handled in user projection matrix )
2015-01-23 06:54:05 +08:00
( 1.30 ) - removed ImGui : : IsItemFocused ( ) in favor of ImGui : : IsItemActive ( ) which handles all widgets
2015-01-18 06:38:10 +08:00
- 2014 / 12 / 10 ( 1.18 ) - removed SetNewWindowDefaultPos ( ) in favor of new generic API SetNextWindowPos ( pos , ImGuiSetCondition_FirstUseEver )
2015-01-21 05:37:18 +08:00
- 2014 / 11 / 28 ( 1.17 ) - moved IO . Font * * * options to inside the IO . Font - > structure ( FontYOffset , FontTexUvForWhite , FontBaseScale , FontFallbackGlyph )
2015-01-18 06:38:10 +08:00
- 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
2014-11-27 06:27:48 +08:00
2014-12-04 19:42:13 +08:00
2015-03-22 07:14:21 +08:00
FREQUENTLY ASKED QUESTIONS ( FAQ ) , TIPS
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
2014-08-31 15:23:55 +08:00
2015-05-14 18:32:12 +08:00
Q : How do I update to a newer version of ImGui ?
A : Overwrite the following files :
imgui . cpp
imgui . h
2015-07-22 07:19:11 +08:00
imgui_demo . cpp
imgui_draw . cpp
imgui_internal . h
2015-05-14 18:32:12 +08:00
stb_rect_pack . h
stb_textedit . h
stb_truetype . h
Check the " API BREAKING CHANGES " sections for a list of occasional API breaking changes . If you have a problem with a function , search for its name
in the code , there will likely be a comment about it . Please report any issue to the GitHub page !
2015-05-20 06:33:55 +08:00
Q : Can I have multiple widgets with the same label ? Can I have widget without a label ? ( Yes )
2015-03-22 07:14:21 +08:00
A : Yes . A primer on the use of labels / IDs in ImGui . .
2015-03-08 05:54:46 +08:00
2015-05-20 06:33:55 +08:00
- Elements that are not clickable , such as Text ( ) items don ' t need an ID .
2015-03-22 07:14:21 +08:00
- Interactive widgets require state to be carried over multiple frames ( most typically ImGui often needs to remember what is the " active " widget ) .
2015-03-08 05:54:46 +08:00
to do so they need an 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"
2015-03-22 07:14:21 +08:00
- 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 :
2015-03-19 03:00:03 +08:00
2015-03-22 07:14:21 +08:00
Button ( " OK " ) ;
2015-05-20 06:33:55 +08:00
Button ( " OK " ) ; // ID collision! Both buttons will be treated as the same.
2015-03-19 03:00:03 +08:00
2015-03-22 07:14:21 +08:00
Fear not ! this is easy to solve and there are many ways to solve it !
2015-03-08 05:54:46 +08:00
- 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 :
2015-05-20 06:33:55 +08:00
Button ( " Play##0 " ) ; // Label = "Play", ID = hash of "Play##0"
Button ( " Play##1 " ) ; // Label = "Play", ID = hash of "Play##1" (different from above)
- so if you want to hide the label but need an ID :
Checkbox ( " ##On " , & b ) ; // Label = "", ID = hash of "##On"
2015-03-08 05:54:46 +08:00
2015-03-22 07:14:21 +08:00
- occasionally ( rarely ) you might want change a label while preserving a constant ID . This allows you to animate labels .
use " ### " to pass a label that isn ' t part of ID :
2015-03-08 05:54:46 +08:00
2015-05-20 06:33:55 +08:00
Button ( " Hello###ID " ; // Label = "Hello", ID = hash of "ID"
Button ( " World###ID " ; // Label = "World", ID = hash of "ID" (same as above)
2015-03-08 05:54:46 +08:00
2015-03-22 07:14:21 +08:00
- use PushID ( ) / PopID ( ) to create scopes and avoid ID conflicts within the same Window .
this is the most convenient way of distinguish 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 addition 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 :
2015-03-08 05:54:46 +08:00
Button ( " Click " ) ; // Label = "Click", ID = hash of "Click"
PushID ( " node " ) ;
2015-03-19 03:00:03 +08:00
Button ( " Click " ) ; // Label = "Click", ID = hash of "node" + "Click"
2015-03-22 07:14:21 +08:00
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 " ) )
2015-03-08 05:54:46 +08:00
{
2015-03-22 07:14:21 +08:00
Button ( " Click " ) ; // Label = "Click", ID = hash of "node" + "Click"
TreePop ( ) ;
2015-03-08 05:54:46 +08:00
}
2015-03-22 07:14:21 +08:00
- when working with trees , ID are used to preserve the opened / closed state of each tree node .
2015-03-08 05:54:46 +08:00
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 .
2015-03-08 06:24:45 +08:00
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 !
2014-08-11 04:58:11 +08:00
2015-03-22 07:14:21 +08:00
Q : Why is my text output blurry ?
A : In your Render function , try translating your projection matrix by ( 0.5f , 0.5f ) or ( 0.375f , 0.375f )
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 file you want :
2015-01-12 05:24:12 +08:00
2015-01-18 18:46:49 +08:00
io . Fonts - > AddFontFromFileTTF ( " myfontfile.ttf " , size_in_pixels ) ;
2015-01-18 23:12:41 +08:00
io . Fonts - > GetTexDataAsRGBA32 ( ) or GetTexDataAsAlpha8 ( )
2015-01-18 06:38:10 +08:00
2015-03-22 07:14:21 +08:00
Q : How can I load multiple fonts ?
A : Use the font atlas to pack them into a single texture :
2015-01-18 06:38:10 +08:00
2015-01-18 18:46:49 +08:00
ImFont * font0 = io . Fonts - > AddFontDefault ( ) ;
ImFont * font1 = io . Fonts - > AddFontFromFileTTF ( " myfontfile.ttf " , size_in_pixels ) ;
2015-02-04 16:01:00 +08:00
ImFont * font2 = io . Fonts - > AddFontFromFileTTF ( " myfontfile2.ttf " , size_in_pixels ) ;
2015-01-18 23:12:41 +08:00
io . Fonts - > GetTexDataAsRGBA32 ( ) or GetTexDataAsAlpha8 ( )
2015-03-19 03:00:03 +08:00
// the first loaded font gets used by default
// use ImGui::PushFont()/ImGui::PopFont() to change the font at runtime
2015-01-18 06:38:10 +08:00
2015-07-15 21:01:21 +08:00
// Options
ImFontConfig config ;
config . OversampleH = 3 ;
config . OversampleV = 3 ;
config . GlyphExtraSpacing . x = 1.0f ;
io . Fonts - > LoadFromFileTTF ( " myfontfile.ttf " , size_pixels , & config ) ;
// Merging input from different fonts into one
ImWchar ranges [ ] = { 0xf000 , 0xf3ff , 0 } ;
ImFontConfig config ;
config . MergeMode = true ;
io . Fonts - > AddFontDefault ( ) ;
io . Fonts - > LoadFromFileTTF ( " fontawesome-webfont.ttf " , 16.0f , & config , ranges ) ;
io . Fonts - > LoadFromFileTTF ( " myfontfile.ttf " , size_pixels , NULL , & config , io . Fonts - > GetGlyphRangesJapanese ( ) ) ;
2015-06-24 04:13:22 +08:00
Q : How can I display and input non - latin characters such as Chinese , Japanese , Korean , Cyrillic ?
2015-03-22 07:14:21 +08:00
A : When loading a font , pass custom Unicode ranges to specify the glyphs to load . ImGui will support UTF - 8 encoding across the board .
Character input depends on you passing the right character code to io . AddInputCharacter ( ) . The example applications do that .
2015-01-18 06:38:10 +08:00
2015-07-15 21:01:21 +08:00
io . Fonts - > AddFontFromFileTTF ( " myfontfile.ttf " , size_in_pixels , NULL , io . Fonts - > GetGlyphRangesJapanese ( ) ) ; // Load Japanese characters
2015-01-18 23:12:41 +08:00
io . Fonts - > GetTexDataAsRGBA32 ( ) or GetTexDataAsAlpha8 ( )
2015-03-07 05:11:14 +08:00
io . ImeWindowHandle = MY_HWND ; // To input using Microsoft IME, give ImGui the hwnd of your application
2014-11-30 23:54:58 +08:00
2015-02-04 16:01:00 +08:00
- 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 .
2014-08-31 15:23:55 +08:00
- tip : you can create widgets without a Begin ( ) / End ( ) block , they will go in an implicit window called " Debug "
2015-08-06 01:04:13 +08:00
- 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 a window ( to get / set other settings )
2015-02-04 16:01:00 +08:00
- tip : you can call Render ( ) multiple times ( e . g for VR renders ) .
2015-01-18 06:38:10 +08:00
- tip : call and read the ShowTestWindow ( ) code for more example of how to use ImGui !
2014-08-11 04:58:11 +08:00
2014-12-04 19:42:13 +08:00
2014-09-24 21:06:34 +08:00
ISSUES & TODO - LIST
= = = = = = = = = = = = = = = = = =
2015-06-27 10:48:53 +08:00
Issue numbers ( # ) refer to github issues .
2014-08-11 04:58:11 +08:00
2015-03-19 03:00:03 +08:00
- misc : merge or clarify ImVec4 vs ImRect ?
2014-12-29 00:09:19 +08:00
- window : autofit feedback loop when user relies on any dynamic layout ( window width multiplier , column ) . maybe just clearly drop manual autofit ?
2014-12-11 18:12:52 +08:00
- window : add a way for very transient windows ( non - saved , temporary overlay over hundreds of objects ) to " clean " up from the global window list .
2015-01-14 21:43:41 +08:00
- window : allow resizing of child windows ( possibly given min / max for each axis ? )
2015-01-23 06:54:05 +08:00
- window : background options for child windows , border option ( disable rounding )
2015-01-15 05:58:30 +08:00
- window : resizing from any sides ? + mouse cursor directives for app .
2015-06-27 10:48:53 +08:00
- window : get size / pos helpers given names ( see discussion in # 249 )
2015-08-06 01:04:13 +08:00
- window : a collapsed window can be stuck behind the main menu bar ?
2015-06-01 04:15:35 +08:00
- scrolling : add horizontal scroll
! - scrolling : allow immediately effective change of scroll if we haven ' t appended items yet
2015-01-20 17:37:30 +08:00
- widgets : display mode : widget - label , label - widget ( aligned on column or using fixed size ) , label - newline - tab - widget etc .
2015-03-19 03:00:03 +08:00
- widgets : clean up widgets internal toward exposing everything .
2015-07-31 12:18:07 +08:00
- widgets : add a disabled / read - only mode ( # 211 )
2015-02-04 16:01:00 +08:00
- main : considering adding EndFrame ( ) / Init ( ) . some constructs are awkward in the implementation because of the lack of them .
2015-01-14 21:43:41 +08:00
- main : IsItemHovered ( ) make it more consistent for various type of widgets , widgets with multiple components , etc . also effectively IsHovered ( ) region sometimes differs from hot region , e . g tree nodes
- main : IsItemHovered ( ) info stored in a stack ? so that ' if TreeNode ( ) { Text ; TreePop ; } if IsHovered ' return the hover state of the TreeNode ?
2015-06-27 10:48:53 +08:00
- input text : add ImGuiInputTextFlags_EnterToApply ? ( off # 218 )
- input text multi - line : way to dynamically grow the buffer without forcing the user to initially allocate for worse case ( follow up on # 200 )
- input text multi - line : line numbers ? status bar ? ( follow up on # 200 )
2015-03-19 03:00:03 +08:00
! - input number : large int not reliably supported because of int < > float conversions .
2014-12-05 20:34:14 +08:00
- input number : optional range min / max for Input * ( ) functions
2014-12-29 00:09:19 +08:00
- input number : holding [ - ] / [ + ] buttons could increase the step speed non - linearly ( or user - controlled )
2014-08-19 02:13:18 +08:00
- input number : use mouse wheel to step up / down
2014-12-31 17:49:47 +08:00
- input number : non - decimal input .
2015-08-06 05:13:13 +08:00
- input number : display and input as hexadecimal
- input number : applying arithmetics ops ( + , - , * , / ) messes up with text edit undo stack .
2015-03-19 03:00:03 +08:00
- text : proper alignment options
2015-06-27 10:48:53 +08:00
- layout : horizontal layout helper ( # 97 )
2015-02-03 03:50:28 +08:00
- layout : more generic alignment state ( left / right / centered ) for single items ?
2015-01-14 21:43:41 +08:00
- layout : clean up the InputFloatN / SliderFloatN / ColorEdit4 layout code . item width should include frame padding .
2015-06-27 10:48:53 +08:00
- columns : separator function or parameter that works within the column ( currently Separator ( ) bypass all columns ) ( # 125 )
- columns : declare column set ( each column : fixed size , % , fill , distribute default size among fills ) ( # 125 )
- columns : columns header to act as button ( ~ sort op ) and allow resize / reorder ( # 125 )
- columns : user specify columns size ( # 125 )
- popup : border options . richer api like BeginChild ( ) perhaps ? ( # 197 )
2015-05-12 21:53:29 +08:00
- combo : sparse combo boxes ( via function call ? )
2015-02-08 23:57:06 +08:00
- combo : contents should extends to fit label if combo widget is small
2015-06-27 10:48:53 +08:00
- combo / listbox : keyboard control . need inputtext like non - active focus + key handling . considering keybord for custom listbox ( pr # 203 )
2015-02-11 08:21:27 +08:00
- listbox : multiple selection
- listbox : user may want to initial scroll to focus on the one selected value ?
2015-06-01 04:15:35 +08:00
- listbox : keyboard navigation .
- listbox : scrolling should track modified selection .
2015-06-27 10:48:53 +08:00
- menus : local shortcuts , global shortcuts ( # 126 )
2015-05-27 22:51:50 +08:00
- menus : icons
2015-06-01 04:15:35 +08:00
- menus : menubars : some sort of priority / effect of main menu - bar on desktop size ?
2015-01-23 06:54:05 +08:00
- tabs
2015-05-29 17:24:03 +08:00
- separator : separator on the initial position of a window is not visible ( cursorpos . y < = clippos . y )
2015-01-22 06:45:21 +08:00
- gauge : various forms of gauge / loading bars widgets
2015-07-31 12:18:07 +08:00
- color : add a better color picker ( perhaps a popup ) .
2015-07-08 10:56:58 +08:00
- plot : plotlines should use the polygon - stroke facilities ( currently issues with averaging normals )
2015-02-04 16:01:00 +08:00
- plot : make it easier for user to draw extra stuff into the graph ( e . g : draw basis , highlight certain points , 2 d plots , multiple plots )
- plot : " smooth " automatic scale over time , user give an input 0.0 ( full user scale ) 1.0 ( full derived from value )
2014-08-11 04:58:11 +08:00
- plot : add a helper e . g . Plot ( char * label , float value , float time_span = 2.0f ) that stores values and Plot them for you - probably another function name . and / or automatically allow to plot ANY displayed value ( more reliance on stable ID )
2015-02-04 16:01:00 +08:00
- file selection widget - > build the tool in our codebase to improve model - dialog idioms
2014-08-11 04:58:11 +08:00
- slider : allow using the [ - ] / [ + ] buttons used by InputFloat ( ) / InputInt ( )
2015-03-19 03:00:03 +08:00
- slider : initial absolute click is imprecise . change to relative movement slider ( same as scrollbar ) .
- slider : add dragging - based widgets to edit values with mouse ( on 2 axises ) , saving screen real - estate .
2015-07-31 12:18:07 +08:00
- slider : tint background based on value ( e . g . v_min - > v_max , or use 0.0f either side of the sign )
2015-07-03 01:26:35 +08:00
- dragfloat : up / down axis
2015-02-11 20:10:45 +08:00
- text edit : clean up the mess caused by converting UTF - 8 < > wchar . the code is rather inefficient right now .
2015-02-04 16:01:00 +08:00
- text edit : centered text for slider as input text so it matches typical positioning .
2014-08-11 04:58:11 +08:00
- text edit : flag to disable live update of the user buffer .
2014-10-27 05:31:34 +08:00
- text edit : field resize behavior - field could stretch when being edited ? hover tooltip shows more text ?
2015-01-23 06:54:05 +08:00
- tree : add treenode / treepush int variants ? because ( void * ) cast from int warns on some platforms / settings
2015-06-27 10:48:53 +08:00
- textwrapped : figure out better way to use TextWrapped ( ) in an always auto - resize context ( tooltip , etc . ) ( git issue # 249 )
2014-08-11 04:58:11 +08:00
- settings : write more decent code to allow saving / loading new fields
2015-03-19 03:00:03 +08:00
- settings : api for per - tool simple persistent data ( bool , int , float , columns sizes , etc . ) in . ini file
2015-06-01 04:15:35 +08:00
- style : store rounded corners in texture to use 1 quad per corner ( filled and wireframe ) . so rounding have minor cost .
2015-01-15 05:58:30 +08:00
- style : colorbox not always square ?
2015-02-19 01:17:06 +08:00
- text : simple markup language for color change ?
2015-01-20 17:37:30 +08:00
- log : LogButtons ( ) options for specifying depth and / or hiding depth slider
2014-11-30 20:21:50 +08:00
- log : have more control over the log scope ( e . g . stop logging when leaving current tree node scope )
2015-03-19 03:00:03 +08:00
- log : be able to log anything ( e . g . right - click on a window / tree - node , shows context menu ? log into tty / file / clipboard )
- log : let user copy any window content to clipboard easily ( CTRL + C on windows ? while moving it ? context menu ? ) . code is commented because it fails with multiple Begin / End pairs .
2014-08-11 04:58:11 +08:00
- filters : set a current filter that tree node can automatically query to hide themselves
- filters : handle wildcards ( with implicit leading / trailing * ) , regexps
- shortcuts : add a shortcut api , e . g . parse " &Save " and / or " Save (CTRL+S) " , pass in to widgets or provide simple ways to use ( button = activate , input = focus )
2015-06-01 04:15:35 +08:00
! - keyboard : tooltip & combo boxes are messing up / not honoring keyboard tabbing
2014-09-30 18:57:37 +08:00
- keyboard : full keyboard navigation and focus .
2015-07-31 12:18:07 +08:00
- focus : SetKeyboardFocusHere ( ) on with > = 0 offset could be done on same frame ( else latch and modulate on beginning of next frame )
2014-10-27 05:31:34 +08:00
- input : rework IO to be able to pass actual events to fix temporal aliasing issues .
- input : support track pad style scrolling & slider edit .
2015-06-27 10:48:53 +08:00
- portability : big - endian test / support ( # 81 )
2015-05-15 19:45:35 +08:00
- memory : add a way to discard allocs of unused / transient windows . with the current architecture new windows ( including popup , opened combos , listbox ) perform at least 3 allocs .
2015-01-23 06:54:05 +08:00
- misc : mark printf compiler attributes on relevant functions
2014-12-29 00:09:19 +08:00
- misc : provide a way to compile out the entire implementation while providing a dummy API ( e . g . # define IMGUI_DUMMY_IMPL )
2014-08-17 01:47:59 +08:00
- misc : double - clicking on title bar to minimize isn ' t consistent , perhaps move to single - click on left - most collapse icon ?
2015-02-03 03:50:28 +08:00
- style editor : have a more global HSV setter ( e . g . alter hue on all elements ) . consider replacing active / hovered by offset in HSV space ?
2015-01-15 05:58:30 +08:00
- style editor : color child window height expressed in multiple of line height .
2014-10-27 05:31:34 +08:00
- optimization / render : merge command - lists with same clip - rect into one even if they aren ' t sequential ? ( as long as in - between clip rectangle don ' t overlap ) ?
- optimization : turn some the various stack vectors into statically - sized arrays
- optimization : better clipping for multi - component widgets
2014-08-11 04:58:11 +08:00
*/
2014-11-18 17:55:31 +08:00
# if defined(_MSC_VER) && !defined(_CRT_SECURE_NO_WARNINGS)
2014-11-12 15:56:42 +08:00
# define _CRT_SECURE_NO_WARNINGS
# endif
2014-08-11 04:58:11 +08:00
# include "imgui.h"
2015-07-22 07:00:28 +08:00
# define IMGUI_DEFINE_MATH_OPERATORS
# include "imgui_internal.h"
2015-03-22 03:27:29 +08:00
# include <ctype.h> // toupper, isprint
# include <math.h> // sqrtf, fabsf, fmodf, powf, cosf, sinf, floorf, ceilf
2015-06-30 00:39:17 +08:00
# include <stdio.h> // vsnprintf, sscanf, printf
2014-08-18 16:49:35 +08:00
# include <new> // new (ptr)
2015-04-24 15:22:29 +08:00
# if defined(_MSC_VER) && _MSC_VER <= 1500 // MSVC 2008 or earlier
# include <stddef.h> // intptr_t
# else
# include <stdint.h> // intptr_t
# endif
2014-08-11 04:58:11 +08:00
2014-11-18 17:55:31 +08:00
# ifdef _MSC_VER
2015-02-02 17:06:51 +08:00
# pragma warning (disable: 4505) // unreferenced local function has been removed (stb stuff)
2014-11-18 17:55:31 +08:00
# pragma warning (disable: 4996) // 'This function or variable may be unsafe': strcpy, strdup, sprintf, vsnprintf, sscanf, fopen
2015-07-20 01:45:03 +08:00
# define snprintf _snprintf
2014-11-18 17:55:31 +08:00
# endif
2014-12-29 19:23:03 +08:00
// Clang warnings with -Weverything
2014-11-18 17:55:31 +08:00
# ifdef __clang__
2014-12-29 19:23:03 +08:00
# pragma clang diagnostic ignored "-Wold-style-cast" // warning : use of old-style cast // yes, they are more terse.
2014-11-18 17:55:31 +08:00
# pragma clang diagnostic ignored "-Wfloat-equal" // warning : comparing floating point with == or != is unsafe // storing and comparing against same constants ok.
2014-12-29 19:23:03 +08:00
# 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.
2014-11-18 17:55:31 +08:00
# 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.
2015-01-24 18:18:31 +08:00
# pragma clang diagnostic ignored "-Wsign-conversion" // warning : implicit conversion changes signedness //
2015-03-21 18:14:10 +08:00
# pragma clang diagnostic ignored "-Wmissing-noreturn" // warning : function xx could be declared with attribute 'noreturn' warning // GetDefaultFontData() asserts which some implementation makes it never return.
2015-07-08 02:05:15 +08:00
# pragma clang diagnostic ignored "-Wdeprecated-declarations" // warning : 'xx' is deprecated: The POSIX name for this item.. // for strdup used in demo code (so user can copy & paste the code)
2015-07-08 11:47:33 +08:00
# pragma clang diagnostic ignored "-Wint-to-void-pointer-cast" // warning : cast to 'void *' from smaller integer type 'int'
2014-11-18 17:55:31 +08:00
# endif
2015-01-24 18:30:40 +08:00
# ifdef __GNUC__
# pragma GCC diagnostic ignored "-Wunused-function" // warning: 'xxxx' defined but not used
2015-07-08 11:47:33 +08:00
# pragma GCC diagnostic ignored "-Wint-to-pointer-cast" // warning: cast to pointer from integer of different size
2015-01-24 18:30:40 +08:00
# endif
2014-11-18 17:55:31 +08:00
2014-08-11 04:58:11 +08:00
//-------------------------------------------------------------------------
// Forward Declarations
//-------------------------------------------------------------------------
2015-07-31 13:48:59 +08:00
static void LogRenderedText ( const ImVec2 & ref_pos , const char * text , const char * text_end = NULL ) ;
2015-07-22 07:00:28 +08:00
static const char * FindTextDisplayEnd ( 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 ) ;
2015-08-02 11:57:24 +08:00
static void SetCurrentFont ( ImFont * font ) ;
2015-07-22 10:06:34 +08:00
static void SetCurrentWindow ( ImGuiWindow * window ) ;
2015-07-22 07:00:28 +08:00
static void SetWindowScrollY ( ImGuiWindow * window , float new_scroll_y ) ;
static void SetWindowPos ( ImGuiWindow * window , const ImVec2 & pos , ImGuiSetCond cond ) ;
static void SetWindowSize ( ImGuiWindow * window , const ImVec2 & size , ImGuiSetCond cond ) ;
static void SetWindowCollapsed ( ImGuiWindow * window , bool collapsed , ImGuiSetCond cond ) ;
static ImGuiWindow * FindWindowByName ( const char * name ) ;
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 ) ;
2015-07-22 10:06:34 +08:00
static bool CloseWindowButton ( bool * p_opened ) ;
2015-07-22 07:00:28 +08:00
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 LoadSettings ( ) ;
static void SaveSettings ( ) ;
static void MarkSettingsDirty ( ) ;
2015-08-02 11:57:24 +08:00
static void PushClipRect ( const ImVec4 & clip_rect , bool clipped_by_current = true ) ;
2015-07-22 07:00:28 +08:00
static void PushColumnClipRect ( int column_index = - 1 ) ;
static void PopClipRect ( ) ;
static ImVec4 GetVisibleRect ( ) ;
static bool BeginPopupEx ( const char * str_id , ImGuiWindowFlags extra_flags ) ;
static void CloseInactivePopups ( ) ;
static void ClosePopupToLevel ( int remaining ) ;
static void ClosePopup ( ImGuiID id ) ;
static bool IsPopupOpen ( ImGuiID id ) ;
2015-07-22 10:06:34 +08:00
static ImGuiWindow * GetFrontMostModalRootWindow ( ) ;
2015-07-22 07:00:28 +08:00
static ImVec2 FindBestPopupWindowPos ( const ImVec2 & base_pos , const ImVec2 & size , ImGuiWindowFlags flags , int * last_dir , const ImRect & r_inner ) ;
static bool InputTextFilterCharacter ( unsigned int * p_char , ImGuiInputTextFlags flags , ImGuiTextEditCallback callback , void * user_data ) ;
2015-07-31 13:59:51 +08:00
static void InputTextApplyArithmeticOp ( const char * buf , const char * initial_value_buf , float * v ) ;
2015-07-22 07:00:28 +08:00
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 ) ;
2014-12-29 19:23:03 +08:00
2014-08-14 02:53:26 +08:00
//-----------------------------------------------------------------------------
2014-11-10 09:29:01 +08:00
// Platform dependent default implementations
2014-08-14 02:53:26 +08:00
//-----------------------------------------------------------------------------
2015-07-22 07:08:20 +08:00
static const char * GetClipboardTextFn_DefaultImpl ( ) ;
static void SetClipboardTextFn_DefaultImpl ( const char * text ) ;
static void ImeSetInputScreenPosFn_DefaultImpl ( int x , int y ) ;
2014-08-14 02:53:26 +08:00
2015-07-22 07:00:28 +08:00
//-----------------------------------------------------------------------------
// Context
//-----------------------------------------------------------------------------
// We access everything through this pointer (always assumed to be != NULL)
// You can swap the pointer to a different context by calling ImGui::SetInternalState()
2015-07-22 07:08:20 +08:00
static ImGuiState GImDefaultState ;
ImGuiState * GImGui = & GImDefaultState ;
2015-07-22 07:00:28 +08:00
// Statically allocated default font atlas. This is merely a maneuver to keep ImFontAtlas definition at the bottom of the .h file (otherwise it'd be inside ImGuiIO)
// Also we wouldn't be able to new() one at this point, before users may define IO.MemAllocFn.
2015-07-22 07:08:20 +08:00
static ImFontAtlas GImDefaultFontAtlas ;
2015-07-22 07:00:28 +08:00
2014-08-11 04:58:11 +08:00
//-----------------------------------------------------------------------------
// User facing structures
//-----------------------------------------------------------------------------
ImGuiStyle : : ImGuiStyle ( )
{
2014-08-19 19:09:13 +08:00
Alpha = 1.0f ; // Global alpha applies to everything in ImGui
WindowPadding = ImVec2 ( 8 , 8 ) ; // Padding within a window
2015-01-15 06:10:48 +08:00
WindowMinSize = ImVec2 ( 32 , 32 ) ; // Minimum window size
2014-12-30 18:00:00 +08:00
WindowRounding = 9.0f ; // Radius of window corners rounding. Set to 0.0f to have rectangular windows
2015-05-12 18:49:24 +08:00
WindowTitleAlign = ImGuiAlign_Left ; // Alignment for title bar text
2015-02-10 21:00:22 +08:00
ChildWindowRounding = 0.0f ; // Radius of child window corners rounding. Set to 0.0f to have rectangular windows
2014-12-30 18:00:00 +08:00
FramePadding = ImVec2 ( 4 , 3 ) ; // Padding within a framed rectangle (used by most widgets)
2015-01-15 17:14:44 +08:00
FrameRounding = 0.0f ; // Radius of frame corners rounding. Set to 0.0f to have rectangular frames (used by most widgets).
2014-12-30 18:00:00 +08:00
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)
2015-03-18 07:41:20 +08:00
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!
2014-08-19 19:09:13 +08:00
WindowFillAlphaDefault = 0.70f ; // Default alpha of window background, if not specified in ImGui::Begin()
2015-03-15 19:10:48 +08:00
IndentSpacing = 22.0f ; // Horizontal spacing when e.g. entering a tree node
2014-08-19 19:09:13 +08:00
ColumnsMinSpacing = 6.0f ; // Minimum horizontal spacing between two columns
2015-03-08 23:03:55 +08:00
ScrollbarWidth = 16.0f ; // Width of the vertical scrollbar
2015-05-09 17:13:56 +08:00
ScrollbarRounding = 0.0f ; // Radius of grab corners rounding for scrollbar
GrabMinSize = 10.0f ; // Minimum width/height of a grab box for slider/scrollbar
2015-07-01 05:02:59 +08:00
GrabRounding = 0.0f ; // Radius of grabs corners rounding. Set to 0.0f to have rectangular slider grabs.
2015-05-02 03:35:44 +08:00
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.
2015-07-06 09:09:53 +08:00
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.)
2014-08-19 19:09:13 +08:00
Colors [ ImGuiCol_Text ] = ImVec4 ( 0.90f , 0.90f , 0.90f , 1.00f ) ;
2015-05-12 02:36:32 +08:00
Colors [ ImGuiCol_TextDisabled ] = ImVec4 ( 0.60f , 0.60f , 0.60f , 1.00f ) ;
2014-08-19 19:09:13 +08:00
Colors [ ImGuiCol_WindowBg ] = ImVec4 ( 0.00f , 0.00f , 0.00f , 1.00f ) ;
2015-02-10 21:00:22 +08:00
Colors [ ImGuiCol_ChildWindowBg ] = ImVec4 ( 0.00f , 0.00f , 0.00f , 0.00f ) ;
2015-05-03 19:17:53 +08:00
Colors [ ImGuiCol_Border ] = ImVec4 ( 0.70f , 0.70f , 0.70f , 0.65f ) ;
2015-03-07 08:26:53 +08:00
Colors [ ImGuiCol_BorderShadow ] = ImVec4 ( 0.00f , 0.00f , 0.00f , 0.00f ) ;
2014-08-19 19:09:13 +08:00
Colors [ ImGuiCol_FrameBg ] = ImVec4 ( 0.80f , 0.80f , 0.80f , 0.30f ) ; // Background of checkbox, radio button, plot, slider, text input
2015-04-03 21:07:52 +08:00
Colors [ ImGuiCol_FrameBgHovered ] = ImVec4 ( 0.90f , 0.80f , 0.80f , 0.40f ) ;
Colors [ ImGuiCol_FrameBgActive ] = ImVec4 ( 0.90f , 0.65f , 0.65f , 0.45f ) ;
2014-08-19 19:09:13 +08:00
Colors [ ImGuiCol_TitleBg ] = ImVec4 ( 0.50f , 0.50f , 1.00f , 0.45f ) ;
Colors [ ImGuiCol_TitleBgCollapsed ] = ImVec4 ( 0.40f , 0.40f , 0.80f , 0.20f ) ;
2015-07-01 03:40:13 +08:00
Colors [ ImGuiCol_TitleBgActive ] = ImVec4 ( 0.50f , 0.50f , 1.00f , 0.55f ) ;
2015-05-25 04:11:11 +08:00
Colors [ ImGuiCol_MenuBarBg ] = ImVec4 ( 0.40f , 0.40f , 0.55f , 0.60f ) ;
2014-08-19 19:09:13 +08:00
Colors [ ImGuiCol_ScrollbarBg ] = ImVec4 ( 0.40f , 0.40f , 0.80f , 0.15f ) ;
Colors [ ImGuiCol_ScrollbarGrab ] = ImVec4 ( 0.40f , 0.40f , 0.80f , 0.30f ) ;
Colors [ ImGuiCol_ScrollbarGrabHovered ] = ImVec4 ( 0.40f , 0.40f , 0.80f , 0.40f ) ;
Colors [ ImGuiCol_ScrollbarGrabActive ] = ImVec4 ( 0.80f , 0.50f , 0.50f , 0.40f ) ;
Colors [ ImGuiCol_ComboBg ] = ImVec4 ( 0.20f , 0.20f , 0.20f , 0.99f ) ;
2015-01-05 03:37:24 +08:00
Colors [ ImGuiCol_CheckMark ] = ImVec4 ( 0.90f , 0.90f , 0.90f , 0.50f ) ;
2014-08-19 19:09:13 +08:00
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 ) ;
2015-02-11 01:43:58 +08:00
Colors [ ImGuiCol_HeaderActive ] = ImVec4 ( 0.53f , 0.53f , 0.87f , 0.80f ) ;
2015-03-18 00:16:15 +08:00
Colors [ ImGuiCol_Column ] = ImVec4 ( 0.50f , 0.50f , 0.50f , 1.00f ) ;
Colors [ ImGuiCol_ColumnHovered ] = ImVec4 ( 0.70f , 0.60f , 0.60f , 1.00f ) ;
Colors [ ImGuiCol_ColumnActive ] = ImVec4 ( 0.90f , 0.70f , 0.70f , 1.00f ) ;
2014-08-19 19:09:13 +08:00
Colors [ ImGuiCol_ResizeGrip ] = ImVec4 ( 1.00f , 1.00f , 1.00f , 0.30f ) ;
Colors [ ImGuiCol_ResizeGripHovered ] = ImVec4 ( 1.00f , 1.00f , 1.00f , 0.60f ) ;
Colors [ ImGuiCol_ResizeGripActive ] = ImVec4 ( 1.00f , 1.00f , 1.00f , 0.90f ) ;
Colors [ ImGuiCol_CloseButton ] = ImVec4 ( 0.50f , 0.50f , 0.90f , 0.50f ) ;
Colors [ ImGuiCol_CloseButtonHovered ] = ImVec4 ( 0.70f , 0.70f , 0.90f , 0.60f ) ;
Colors [ ImGuiCol_CloseButtonActive ] = ImVec4 ( 0.70f , 0.70f , 0.70f , 1.00f ) ;
Colors [ ImGuiCol_PlotLines ] = ImVec4 ( 1.00f , 1.00f , 1.00f , 1.00f ) ;
Colors [ ImGuiCol_PlotLinesHovered ] = ImVec4 ( 0.90f , 0.70f , 0.00f , 1.00f ) ;
Colors [ ImGuiCol_PlotHistogram ] = ImVec4 ( 0.90f , 0.70f , 0.00f , 1.00f ) ;
Colors [ ImGuiCol_PlotHistogramHovered ] = ImVec4 ( 1.00f , 0.60f , 0.00f , 1.00f ) ;
Colors [ ImGuiCol_TextSelectedBg ] = ImVec4 ( 0.00f , 0.00f , 1.00f , 0.35f ) ;
Colors [ ImGuiCol_TooltipBg ] = ImVec4 ( 0.05f , 0.05f , 0.10f , 0.90f ) ;
2015-06-24 02:34:15 +08:00
Colors [ ImGuiCol_ModalWindowDarkening ] = ImVec4 ( 0.20f , 0.20f , 0.20f , 0.35f ) ;
2014-08-11 04:58:11 +08:00
}
ImGuiIO : : ImGuiIO ( )
{
2015-02-01 02:55:05 +08:00
// Most fields are initialized with zero
2014-08-19 19:09:13 +08:00
memset ( this , 0 , sizeof ( * this ) ) ;
2015-02-01 02:55:05 +08:00
2014-12-22 09:45:13 +08:00
DisplaySize = ImVec2 ( - 1.0f , - 1.0f ) ;
2014-08-19 19:09:13 +08:00
DeltaTime = 1.0f / 60.0f ;
IniSavingRate = 5.0f ;
IniFilename = " imgui.ini " ;
LogFilename = " imgui_log.txt " ;
2015-07-22 07:00:28 +08:00
Fonts = & GImDefaultFontAtlas ;
2014-11-29 08:02:46 +08:00
FontGlobalScale = 1.0f ;
2014-08-19 19:09:13 +08:00
MousePos = ImVec2 ( - 1 , - 1 ) ;
MousePosPrev = ImVec2 ( - 1 , - 1 ) ;
MouseDoubleClickTime = 0.30f ;
MouseDoubleClickMaxDist = 6.0f ;
2015-03-20 19:13:18 +08:00
MouseDragThreshold = 6.0f ;
2015-07-03 11:32:29 +08:00
for ( int i = 0 ; i < ImGuiKey_COUNT ; i + + )
KeyMap [ i ] = - 1 ;
2015-05-27 22:17:44 +08:00
KeyRepeatDelay = 0.250f ;
2015-06-18 06:02:50 +08:00
KeyRepeatRate = 0.050f ;
2014-10-05 16:11:02 +08:00
UserData = NULL ;
2014-08-14 02:53:26 +08:00
2014-09-25 21:51:06 +08:00
// User functions
RenderDrawListsFn = NULL ;
2014-09-25 03:39:39 +08:00
MemAllocFn = malloc ;
MemFreeFn = free ;
2014-12-29 19:23:03 +08:00
GetClipboardTextFn = GetClipboardTextFn_DefaultImpl ; // Platform dependent default implementations
2014-08-19 19:09:13 +08:00
SetClipboardTextFn = SetClipboardTextFn_DefaultImpl ;
2015-03-07 05:11:14 +08:00
ImeSetInputScreenPosFn = ImeSetInputScreenPosFn_DefaultImpl ;
2014-08-11 04:58:11 +08:00
}
2015-07-08 02:09:25 +08:00
// 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 ' ;
}
}
2015-07-16 12:14:04 +08:00
void ImGuiIO : : AddInputCharactersUTF8 ( const char * utf8_chars )
2015-07-16 11:15:03 +08:00
{
2015-07-16 12:14:04 +08:00
// 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 ; wchars [ i ] ! = 0 & & i < wchars_buf_len ; i + + )
2015-07-16 11:15:03 +08:00
AddInputCharacter ( wchars [ i ] ) ;
}
2015-07-22 07:00:28 +08:00
//-----------------------------------------------------------------------------
// HELPERS
//-----------------------------------------------------------------------------
# define IM_INT_MIN (-2147483647-1)
# define IM_INT_MAX (2147483647)
// Play it nice with Windows users. Notepad in 2015 still doesn't display text data with Unix-style \n.
# ifdef _MSC_VER
# define IM_NEWLINE "\r\n"
# else
# define IM_NEWLINE "\n"
# endif
bool ImIsPointInTriangle ( const ImVec2 & p , const ImVec2 & a , const ImVec2 & b , const ImVec2 & c )
2015-05-27 00:09:21 +08:00
{
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 ) ) ;
}
2015-07-22 07:00:28 +08:00
int ImStricmp ( const char * str1 , const char * str2 )
2014-08-11 04:58:11 +08:00
{
2014-08-19 19:09:13 +08:00
int d ;
while ( ( d = toupper ( * str2 ) - toupper ( * str1 ) ) = = 0 & & * str1 ) { str1 + + ; str2 + + ; }
return d ;
2014-08-11 04:58:11 +08:00
}
2015-07-22 07:00:28 +08:00
int ImStrnicmp ( const char * str1 , const char * str2 , int count )
2014-11-24 07:35:26 +08:00
{
2014-11-30 20:21:50 +08:00
int d = 0 ;
2014-11-24 07:35:26 +08:00
while ( count > 0 & & ( d = toupper ( * str2 ) - toupper ( * str1 ) ) = = 0 & & * str1 ) { str1 + + ; str2 + + ; count - - ; }
2014-11-30 20:31:43 +08:00
return d ;
2014-11-24 07:35:26 +08:00
}
2015-07-22 07:00:28 +08:00
char * ImStrdup ( const char * str )
2014-08-31 18:28:21 +08:00
{
2014-09-25 01:01:07 +08:00
char * buff = ( char * ) ImGui : : MemAlloc ( strlen ( str ) + 1 ) ;
2014-08-31 18:28:21 +08:00
IM_ASSERT ( buff ) ;
strcpy ( buff , str ) ;
return buff ;
2014-08-11 04:58:11 +08:00
}
2015-07-22 07:00:28 +08:00
int ImStrlenW ( const ImWchar * str )
2014-09-25 21:51:06 +08:00
{
2015-07-08 02:09:25 +08:00
int n = 0 ;
2015-02-21 21:36:24 +08:00
while ( * str + + ) n + + ;
2014-09-25 21:51:06 +08:00
return n ;
}
2015-07-22 07:00:28 +08:00
const ImWchar * ImStrbolW ( const ImWchar * buf_mid_line , const ImWchar * buf_begin ) // find beginning-of-line
2015-06-19 13:23:20 +08:00
{
while ( buf_mid_line > buf_begin & & buf_mid_line [ - 1 ] ! = ' \n ' )
buf_mid_line - - ;
return buf_mid_line ;
}
2015-07-22 07:00:28 +08:00
const char * ImStristr ( const char * haystack , const char * needle , const char * needle_end )
2014-08-11 04:58:11 +08:00
{
2014-08-19 19:09:13 +08:00
if ( ! needle_end )
needle_end = needle + strlen ( needle ) ;
const char un0 = ( char ) toupper ( * needle ) ;
while ( * haystack )
{
if ( toupper ( * haystack ) = = un0 )
{
const char * b = needle + 1 ;
for ( const char * a = haystack + 1 ; b < needle_end ; a + + , b + + )
if ( toupper ( * a ) ! = toupper ( * b ) )
break ;
if ( b = = needle_end )
return haystack ;
}
haystack + + ;
}
return NULL ;
2014-08-11 04:58:11 +08:00
}
2015-07-22 07:00:28 +08:00
int ImFormatString ( char * buf , int buf_size , const char * fmt , . . . )
{
va_list args ;
va_start ( args , fmt ) ;
int w = vsnprintf ( buf , buf_size , fmt , args ) ;
va_end ( args ) ;
buf [ buf_size - 1 ] = 0 ;
return ( w = = - 1 ) ? buf_size : w ;
}
int ImFormatStringV ( char * buf , int buf_size , const char * fmt , va_list args )
{
int w = vsnprintf ( buf , buf_size , fmt , args ) ;
buf [ buf_size - 1 ] = 0 ;
return ( w = = - 1 ) ? buf_size : w ;
}
2014-12-30 23:18:21 +08:00
// Pass data_size==0 for zero-terminated string
2015-07-22 07:00:28 +08:00
// 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 = 0 )
2014-08-11 04:58:11 +08:00
{
2014-08-19 19:09:13 +08:00
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 + + )
2014-08-20 17:40:31 +08:00
crc = ( crc > > 1 ) ^ ( ImU32 ( - int ( crc & 1 ) ) & polynomial ) ;
2014-08-19 19:09:13 +08:00
crc32_lut [ i ] = crc ;
}
}
2015-03-08 05:53:33 +08:00
seed = ~ seed ;
ImU32 crc = seed ;
2014-12-30 23:18:21 +08:00
const unsigned char * current = ( const unsigned char * ) data ;
2014-12-30 23:38:28 +08:00
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 + + )
2015-03-08 05:53:33 +08:00
{
// 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 ] ;
}
2014-12-30 23:38:28 +08:00
}
2014-08-19 19:09:13 +08:00
return ~ crc ;
2014-08-11 04:58:11 +08:00
}
2015-07-22 07:00:28 +08:00
//-----------------------------------------------------------------------------
// 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 )
2014-08-11 04:58:11 +08:00
{
2015-07-22 07:00:28 +08:00
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 = 0 ;
if ( in_text_end & & in_text_end - ( const char * ) str < 2 ) return 0 ;
if ( * str < 0xc2 ) return 0 ;
c = ( unsigned int ) ( ( * str + + & 0x1f ) < < 6 ) ;
if ( ( * str & 0xc0 ) ! = 0x80 ) return 0 ;
c + = ( * str + + & 0x3f ) ;
* out_char = c ;
return 2 ;
}
if ( ( * str & 0xf0 ) = = 0xe0 )
{
* out_char = 0 ;
if ( in_text_end & & in_text_end - ( const char * ) str < 3 ) return 0 ;
if ( * str = = 0xe0 & & ( str [ 1 ] < 0xa0 | | str [ 1 ] > 0xbf ) ) return 0 ;
if ( * str = = 0xed & & str [ 1 ] > 0x9f ) return 0 ; // str[1] < 0x80 is checked below
c = ( unsigned int ) ( ( * str + + & 0x0f ) < < 12 ) ;
if ( ( * str & 0xc0 ) ! = 0x80 ) return 0 ;
c + = ( unsigned int ) ( ( * str + + & 0x3f ) < < 6 ) ;
if ( ( * str & 0xc0 ) ! = 0x80 ) return 0 ;
c + = ( * str + + & 0x3f ) ;
* out_char = c ;
return 3 ;
}
if ( ( * str & 0xf8 ) = = 0xf0 )
{
* out_char = 0 ;
if ( in_text_end & & in_text_end - ( const char * ) str < 4 ) return 0 ;
if ( * str > 0xf4 ) return 0 ;
if ( * str = = 0xf0 & & ( str [ 1 ] < 0x90 | | str [ 1 ] > 0xbf ) ) return 0 ;
if ( * str = = 0xf4 & & str [ 1 ] > 0x8f ) return 0 ; // str[1] < 0x80 is checked below
c = ( unsigned int ) ( ( * str + + & 0x07 ) < < 18 ) ;
if ( ( * str & 0xc0 ) ! = 0x80 ) return 0 ;
c + = ( unsigned int ) ( ( * str + + & 0x3f ) < < 12 ) ;
if ( ( * str & 0xc0 ) ! = 0x80 ) return 0 ;
c + = ( unsigned int ) ( ( * str + + & 0x3f ) < < 6 ) ;
if ( ( * str & 0xc0 ) ! = 0x80 ) return 0 ;
c + = ( * str + + & 0x3f ) ;
// utf-8 encodings of values used in surrogate pairs are invalid
if ( ( c & 0xFFFFF800 ) = = 0xD800 ) return 0 ;
* out_char = c ;
return 4 ;
}
* out_char = 0 ;
return 0 ;
2014-08-11 04:58:11 +08:00
}
2015-07-22 07:00:28 +08:00
int ImTextStrFromUtf8 ( ImWchar * buf , int buf_size , const char * in_text , const char * in_text_end , const char * * in_text_remaining )
2014-08-11 04:58:11 +08:00
{
2015-07-22 07:00:28 +08:00
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 ;
2014-08-11 04:58:11 +08:00
}
2015-02-01 00:36:12 +08:00
ImU32 ImGui : : ColorConvertFloat4ToU32 ( const ImVec4 & in )
2014-08-11 04:58:11 +08:00
{
2014-08-19 19:09:13 +08:00
ImU32 out = ( ( ImU32 ) ( ImSaturate ( in . x ) * 255.f ) ) ;
out | = ( ( ImU32 ) ( ImSaturate ( in . y ) * 255.f ) < < 8 ) ;
out | = ( ( ImU32 ) ( ImSaturate ( in . z ) * 255.f ) < < 16 ) ;
out | = ( ( ImU32 ) ( ImSaturate ( in . w ) * 255.f ) < < 24 ) ;
return out ;
2014-08-11 04:58:11 +08:00
}
// 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
2015-02-01 00:36:12 +08:00
void ImGui : : ColorConvertRGBtoHSV ( float r , float g , float b , float & out_h , float & out_s , float & out_v )
2014-08-11 04:58:11 +08:00
{
2014-08-19 19:09:13 +08:00
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 ;
}
2014-08-11 04:58:11 +08:00
2014-08-19 19:09:13 +08:00
const float chroma = r - ( g < b ? g : b ) ;
out_h = fabsf ( K + ( g - b ) / ( 6.f * chroma + 1e-20 f ) ) ;
out_s = chroma / ( r + 1e-20 f ) ;
out_v = r ;
2014-08-11 04:58:11 +08:00
}
// 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
2015-02-01 00:36:12 +08:00
void ImGui : : ColorConvertHSVtoRGB ( float h , float s , float v , float & out_r , float & out_g , float & out_b )
2014-08-11 04:58:11 +08:00
{
2014-08-19 19:09:13 +08:00
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 ;
}
2014-08-11 04:58:11 +08:00
}
2014-12-29 19:23:03 +08:00
// Load file content into memory
// Memory allocated with ImGui::MemAlloc(), must be freed by user using ImGui::MemFree()
2015-07-22 07:00:28 +08:00
bool ImLoadFileToMemory ( const char * filename , const char * file_open_mode , void * * out_file_data , int * out_file_size , int padding_bytes )
2014-12-29 19:23:03 +08:00
{
IM_ASSERT ( filename & & file_open_mode & & out_file_data & & out_file_size ) ;
* out_file_data = NULL ;
* out_file_size = 0 ;
FILE * f ;
if ( ( f = fopen ( filename , file_open_mode ) ) = = NULL )
return false ;
long file_size_signed ;
if ( fseek ( f , 0 , SEEK_END ) | | ( file_size_signed = ftell ( f ) ) = = - 1 | | fseek ( f , 0 , SEEK_SET ) )
{
fclose ( f ) ;
return false ;
}
2015-07-08 02:09:25 +08:00
int file_size = ( int ) file_size_signed ;
2014-12-30 02:05:22 +08:00
void * file_data = ImGui : : MemAlloc ( file_size + padding_bytes ) ;
2014-12-29 19:23:03 +08:00
if ( file_data = = NULL )
{
fclose ( f ) ;
return false ;
}
2015-07-08 02:09:25 +08:00
if ( fread ( file_data , 1 , ( size_t ) file_size , f ) ! = ( size_t ) file_size )
2014-12-29 19:23:03 +08:00
{
fclose ( f ) ;
ImGui : : MemFree ( file_data ) ;
return false ;
}
2014-12-30 02:13:41 +08:00
if ( padding_bytes > 0 )
memset ( ( void * ) ( ( ( char * ) file_data ) + file_size ) , 0 , padding_bytes ) ;
2014-12-29 19:23:03 +08:00
fclose ( f ) ;
* out_file_data = file_data ;
2015-07-22 07:46:49 +08:00
if ( out_file_size )
* out_file_size = file_size ;
2014-12-29 19:23:03 +08:00
return true ;
}
2015-07-22 07:00:28 +08:00
//-----------------------------------------------------------------------------
// ImGuiStorage
2014-08-11 04:58:11 +08:00
//-----------------------------------------------------------------------------
2015-07-22 07:00:28 +08:00
// Helper: Key->value storage
void ImGuiStorage : : Clear ( )
2014-08-11 04:58:11 +08:00
{
2014-08-19 19:09:13 +08:00
Data . clear ( ) ;
2014-08-11 04:58:11 +08:00
}
// std::lower_bound but without the bullshit
static ImVector < ImGuiStorage : : Pair > : : iterator LowerBound ( ImVector < ImGuiStorage : : Pair > & data , ImU32 key )
{
2014-08-19 19:09:13 +08:00
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 ;
2014-08-11 04:58:11 +08:00
}
2014-12-09 01:14:54 +08:00
int ImGuiStorage : : GetInt ( ImU32 key , int default_val ) const
2014-08-11 04:58:11 +08:00
{
2014-12-09 01:14:54 +08:00
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 ;
2014-08-11 04:58:11 +08:00
}
2014-12-09 01:14:54 +08:00
float ImGuiStorage : : GetFloat ( ImU32 key , float default_val ) const
2014-08-11 04:58:11 +08:00
{
2014-12-09 01:14:54 +08:00
ImVector < Pair > : : iterator it = LowerBound ( const_cast < ImVector < ImGuiStorage : : Pair > & > ( Data ) , key ) ;
if ( it = = Data . end ( ) | | it - > key ! = key )
2014-08-19 19:09:13 +08:00
return default_val ;
2014-12-09 01:14:54 +08:00
return it - > val_f ;
2014-08-11 04:58:11 +08:00
}
2015-01-29 17:22:32 +08:00
void * ImGuiStorage : : GetVoidPtr ( ImGuiID key ) const
2014-12-09 01:14:54 +08:00
{
2015-01-29 17:22:32 +08:00
ImVector < Pair > : : iterator it = LowerBound ( const_cast < ImVector < ImGuiStorage : : Pair > & > ( Data ) , key ) ;
2014-12-09 01:14:54 +08:00
if ( it = = Data . end ( ) | | it - > key ! = key )
2015-01-29 17:22:32 +08:00
return NULL ;
return it - > val_p ;
2014-12-09 01:14:54 +08:00
}
2015-01-29 17:22:32 +08:00
// 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 )
2014-12-09 01:14:54 +08:00
{
ImVector < Pair > : : iterator it = LowerBound ( Data , key ) ;
if ( it = = Data . end ( ) | | it - > key ! = key )
it = Data . insert ( it , Pair ( key , default_val ) ) ;
2015-01-29 17:22:32 +08:00
return & it - > val_i ;
2014-12-09 01:14:54 +08:00
}
2015-01-29 17:22:32 +08:00
float * ImGuiStorage : : GetFloatRef ( ImGuiID key , float default_val )
2015-01-29 04:31:22 +08:00
{
ImVector < Pair > : : iterator it = LowerBound ( Data , key ) ;
if ( it = = Data . end ( ) | | it - > key ! = key )
2015-01-29 17:22:32 +08:00
it = Data . insert ( it , Pair ( key , default_val ) ) ;
return & it - > val_f ;
2015-01-29 04:31:22 +08:00
}
2015-04-21 17:12:17 +08:00
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 ;
}
2015-05-11 22:57:02 +08:00
// 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)
2014-08-11 04:58:11 +08:00
void ImGuiStorage : : SetInt ( ImU32 key , int val )
{
2014-08-19 19:09:13 +08:00
ImVector < Pair > : : iterator it = LowerBound ( Data , key ) ;
2014-12-09 01:14:54 +08:00
if ( it = = Data . end ( ) | | it - > key ! = key )
2014-08-19 19:09:13 +08:00
{
2014-12-09 01:14:54 +08:00
Data . insert ( it , Pair ( key , val ) ) ;
return ;
2014-08-19 19:09:13 +08:00
}
2014-12-09 01:14:54 +08:00
it - > val_i = val ;
}
void ImGuiStorage : : SetFloat ( ImU32 key , float val )
{
ImVector < Pair > : : iterator it = LowerBound ( Data , key ) ;
if ( it = = Data . end ( ) | | it - > key ! = key )
2014-08-19 19:09:13 +08:00
{
2014-12-09 01:14:54 +08:00
Data . insert ( it , Pair ( key , val ) ) ;
return ;
2014-08-19 19:09:13 +08:00
}
2014-12-09 01:14:54 +08:00
it - > val_f = val ;
2014-08-11 04:58:11 +08:00
}
2015-01-29 04:31:22 +08:00
void ImGuiStorage : : SetVoidPtr ( ImU32 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 ;
}
2014-08-11 04:58:11 +08:00
void ImGuiStorage : : SetAllInt ( int v )
{
2015-07-08 02:39:08 +08:00
for ( int i = 0 ; i < Data . Size ; i + + )
2014-12-09 01:14:54 +08:00
Data [ i ] . val_i = v ;
2014-08-11 04:58:11 +08:00
}
2015-07-22 07:00:28 +08:00
//-----------------------------------------------------------------------------
// ImGuiTextFilter
2014-08-11 04:58:11 +08:00
//-----------------------------------------------------------------------------
2014-08-26 00:19:04 +08:00
// Helper: Parse and apply text filters. In format "aaaaa[,bbbb][,ccccc]"
2015-02-15 21:50:04 +08:00
ImGuiTextFilter : : ImGuiTextFilter ( const char * default_filter )
2014-08-11 04:58:11 +08:00
{
2015-02-15 21:50:04 +08:00
if ( default_filter )
{
ImFormatString ( InputBuf , IM_ARRAYSIZE ( InputBuf ) , " %s " , default_filter ) ;
Build ( ) ;
}
else
{
InputBuf [ 0 ] = 0 ;
CountGrep = 0 ;
}
2014-08-11 04:58:11 +08:00
}
void ImGuiTextFilter : : Draw ( const char * label , float width )
{
2015-02-13 03:34:14 +08:00
if ( width > 0.0f )
ImGui : : PushItemWidth ( width ) ;
2014-08-19 19:09:13 +08:00
ImGui : : InputText ( label , InputBuf , IM_ARRAYSIZE ( InputBuf ) ) ;
2015-02-13 03:34:14 +08:00
if ( width > 0.0f )
ImGui : : PopItemWidth ( ) ;
2014-08-19 19:09:13 +08:00
Build ( ) ;
2014-08-11 04:58:11 +08:00
}
void ImGuiTextFilter : : TextRange : : split ( char separator , ImVector < TextRange > & out )
{
2014-08-19 19:09:13 +08:00
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 ) ) ;
2014-08-11 04:58:11 +08:00
}
void ImGuiTextFilter : : Build ( )
{
2014-08-19 19:09:13 +08:00
Filters . resize ( 0 ) ;
TextRange input_range ( InputBuf , InputBuf + strlen ( InputBuf ) ) ;
input_range . split ( ' , ' , Filters ) ;
2014-08-11 04:58:11 +08:00
2014-08-19 19:09:13 +08:00
CountGrep = 0 ;
2015-07-08 02:39:08 +08:00
for ( int i = 0 ; i ! = Filters . Size ; i + + )
2014-08-19 19:09:13 +08:00
{
Filters [ i ] . trim_blanks ( ) ;
if ( Filters [ i ] . empty ( ) )
continue ;
if ( Filters [ i ] . front ( ) ! = ' - ' )
CountGrep + = 1 ;
}
2014-08-11 04:58:11 +08:00
}
bool ImGuiTextFilter : : PassFilter ( const char * val ) const
{
2014-08-19 19:09:13 +08:00
if ( Filters . empty ( ) )
return true ;
if ( val = = NULL )
val = " " ;
2015-07-08 02:39:08 +08:00
for ( int i = 0 ; i ! = Filters . Size ; i + + )
2014-08-19 19:09:13 +08:00
{
const TextRange & f = Filters [ i ] ;
if ( f . empty ( ) )
continue ;
if ( f . front ( ) = = ' - ' )
{
// Subtract
if ( ImStristr ( val , f . begin ( ) + 1 , f . end ( ) ) ! = NULL )
return false ;
}
else
{
// Grep
if ( ImStristr ( val , f . begin ( ) , f . end ( ) ) ! = NULL )
return true ;
}
}
// Implicit * grep
if ( CountGrep = = 0 )
return true ;
return false ;
2014-08-11 04:58:11 +08:00
}
2015-07-22 07:00:28 +08:00
//-----------------------------------------------------------------------------
// ImGuiTextBuffer
//-----------------------------------------------------------------------------
2015-01-14 21:01:53 +08:00
// 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
2014-08-26 00:19:04 +08:00
// Helper: Text buffer for logging/accumulating text
2014-12-30 04:18:52 +08:00
void ImGuiTextBuffer : : appendv ( const char * fmt , va_list args )
2014-08-11 04:58:11 +08:00
{
2015-01-14 21:01:53 +08:00
va_list args_copy ;
va_copy ( args_copy , args ) ;
2014-12-30 04:18:52 +08:00
int len = vsnprintf ( NULL , 0 , fmt , args ) ; // FIXME-OPT: could do a first pass write attempt, likely successful on first pass.
2014-08-19 19:09:13 +08:00
if ( len < = 0 )
return ;
2015-01-14 21:04:33 +08:00
2015-07-08 02:39:08 +08:00
const int write_off = Buf . Size ;
2015-07-08 02:09:25 +08:00
const int needed_sz = write_off + len ;
2015-07-08 02:39:08 +08:00
if ( write_off + len > = Buf . Capacity )
2015-01-14 21:04:33 +08:00
{
2015-07-08 02:39:08 +08:00
int double_capacity = Buf . Capacity * 2 ;
2015-01-14 21:04:33 +08:00
Buf . reserve ( needed_sz > double_capacity ? needed_sz : double_capacity ) ;
}
2014-08-11 04:58:11 +08:00
2015-01-14 21:04:33 +08:00
Buf . resize ( needed_sz ) ;
2015-07-08 02:09:25 +08:00
ImFormatStringV ( & Buf [ write_off ] - 1 , len + 1 , fmt , args_copy ) ;
2014-12-30 04:18:52 +08:00
}
2014-08-11 04:58:11 +08:00
2014-12-30 04:18:52 +08:00
void ImGuiTextBuffer : : append ( const char * fmt , . . . )
{
va_list args ;
2014-08-19 19:09:13 +08:00
va_start ( args , fmt ) ;
2014-12-30 04:18:52 +08:00
appendv ( fmt , args ) ;
2014-08-19 19:09:13 +08:00
va_end ( args ) ;
2014-08-11 04:58:11 +08:00
}
2015-07-22 07:00:28 +08:00
//-----------------------------------------------------------------------------
// 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 ) ;
}
//-----------------------------------------------------------------------------
// ImGuiWindow
2014-08-11 04:58:11 +08:00
//-----------------------------------------------------------------------------
2014-12-11 03:22:30 +08:00
ImGuiWindow : : ImGuiWindow ( const char * name )
2014-08-11 04:58:11 +08:00
{
2014-08-31 18:28:21 +08:00
Name = ImStrdup ( name ) ;
2015-03-08 07:53:03 +08:00
ID = ImHash ( name , 0 ) ;
2015-03-08 07:52:26 +08:00
IDStack . push_back ( ID ) ;
2015-03-18 20:10:26 +08:00
MoveID = GetID ( " #MOVE " ) ;
2015-03-08 07:52:26 +08:00
2015-02-22 20:05:38 +08:00
Flags = 0 ;
2014-12-11 03:22:30 +08:00
PosFloat = Pos = ImVec2 ( 0.0f , 0.0f ) ;
Size = SizeFull = ImVec2 ( 0.0f , 0.0f ) ;
2015-03-18 06:03:02 +08:00
SizeContents = ImVec2 ( 0.0f , 0.0f ) ;
2015-07-02 08:55:42 +08:00
WindowPadding = ImVec2 ( 0.0f , 0.0f ) ;
2014-08-19 19:09:13 +08:00
ScrollY = 0.0f ;
2015-07-03 10:53:45 +08:00
ScrollTargetRelY = FLT_MAX ;
2015-07-02 07:51:44 +08:00
ScrollTargetCenterRatioY = 0.5f ;
2014-08-19 19:09:13 +08:00
ScrollbarY = false ;
2015-05-02 20:20:50 +08:00
Active = WasActive = false ;
2015-03-20 18:54:21 +08:00
Accessed = false ;
2014-08-19 19:09:13 +08:00
Collapsed = false ;
SkipItems = false ;
2015-05-22 05:21:00 +08:00
BeginCount = 0 ;
2015-05-11 22:57:02 +08:00
PopupID = 0 ;
2015-06-25 10:08:08 +08:00
AutoFitFramesX = AutoFitFramesY = - 1 ;
2014-11-22 00:29:56 +08:00
AutoFitOnlyGrows = false ;
2015-05-03 01:22:46 +08:00
AutoPosLastDirection = - 1 ;
HiddenFrames = 0 ;
2015-05-08 01:39:23 +08:00
SetWindowPosAllowFlags = SetWindowSizeAllowFlags = SetWindowCollapsedAllowFlags = ImGuiSetCond_Always | ImGuiSetCond_Once | ImGuiSetCond_FirstUseEver | ImGuiSetCond_Appearing ;
2015-06-24 05:45:34 +08:00
SetWindowPosCenterWanted = false ;
2014-12-11 03:22:30 +08:00
2014-08-19 19:09:13 +08:00
LastFrameDrawn = - 1 ;
ItemWidthDefault = 0.0f ;
2014-09-24 21:03:42 +08:00
FontWindowScale = 1.0f ;
2014-08-11 04:58:11 +08:00
2014-09-30 19:31:35 +08:00
DrawList = ( ImDrawList * ) ImGui : : MemAlloc ( sizeof ( ImDrawList ) ) ;
new ( DrawList ) ImDrawList ( ) ;
2015-07-08 10:17:07 +08:00
DrawList - > _OwnerName = Name ;
2015-02-22 20:05:38 +08:00
RootWindow = NULL ;
2015-07-01 03:52:00 +08:00
RootNonPopupWindow = NULL ;
2014-09-30 19:31:35 +08:00
2014-09-30 19:22:58 +08:00
FocusIdxAllCounter = FocusIdxTabCounter = - 1 ;
FocusIdxAllRequestCurrent = FocusIdxTabRequestCurrent = IM_INT_MAX ;
FocusIdxAllRequestNext = FocusIdxTabRequestNext = IM_INT_MAX ;
2014-08-11 04:58:11 +08:00
}
ImGuiWindow : : ~ ImGuiWindow ( )
{
2014-08-16 16:28:29 +08:00
DrawList - > ~ ImDrawList ( ) ;
2014-09-25 01:01:07 +08:00
ImGui : : MemFree ( DrawList ) ;
2014-08-19 19:09:13 +08:00
DrawList = NULL ;
2014-09-25 01:01:07 +08:00
ImGui : : MemFree ( Name ) ;
2014-08-19 19:09:13 +08:00
Name = NULL ;
2014-08-11 04:58:11 +08:00
}
2015-08-02 11:57:24 +08:00
void ImGui : : SetActiveID ( ImGuiID id , ImGuiWindow * window = NULL )
2015-07-22 07:00:28 +08:00
{
ImGuiState & g = * GImGui ;
g . ActiveId = id ;
g . ActiveIdIsFocusedOnly = false ;
g . ActiveIdIsJustActivated = true ;
g . ActiveIdWindow = window ;
}
2015-08-02 11:57:24 +08:00
void ImGui : : KeepAliveID ( ImGuiID id )
2015-07-22 07:00:28 +08:00
{
ImGuiState & g = * GImGui ;
if ( g . ActiveId = = id )
g . ActiveIdIsAlive = true ;
}
2015-04-13 21:45:27 +08:00
ImGuiID ImGuiWindow : : GetID ( const char * str , const char * str_end )
2014-08-11 04:58:11 +08:00
{
2015-03-08 07:52:26 +08:00
ImGuiID seed = IDStack . back ( ) ;
2015-07-08 02:09:25 +08:00
ImGuiID id = ImHash ( str , str_end ? ( int ) ( str_end - str ) : 0 , seed ) ;
2015-08-02 11:57:24 +08:00
ImGui : : KeepAliveID ( id ) ;
2014-08-19 19:09:13 +08:00
return id ;
2014-08-11 04:58:11 +08:00
}
ImGuiID ImGuiWindow : : GetID ( const void * ptr )
{
2015-03-08 07:52:26 +08:00
ImGuiID seed = IDStack . back ( ) ;
2015-07-08 02:09:25 +08:00
ImGuiID id = ImHash ( & ptr , sizeof ( void * ) , seed ) ;
2015-08-02 11:57:24 +08:00
ImGui : : KeepAliveID ( id ) ;
2014-08-19 19:09:13 +08:00
return id ;
2014-08-11 04:58:11 +08:00
}
2015-08-02 11:57:24 +08:00
inline ImGuiWindow * ImGui : : GetCurrentWindow ( )
2015-07-22 07:00:28 +08:00
{
// If this ever crash it probably means that ImGui::NewFrame() has never been called (which is illegal). We should always have a CurrentWindow in the stack (there is an implicit "Debug" window)
ImGuiState & g = * GImGui ;
g . CurrentWindow - > Accessed = true ;
return g . CurrentWindow ;
}
2015-07-22 10:06:34 +08:00
static void SetCurrentWindow ( ImGuiWindow * window )
2015-07-22 07:00:28 +08:00
{
ImGuiState & g = * GImGui ;
g . CurrentWindow = window ;
if ( window )
g . FontSize = window - > CalcFontSize ( ) ;
}
2015-08-02 11:57:24 +08:00
ImGuiWindow * ImGui : : GetParentWindow ( )
2015-07-22 07:00:28 +08:00
{
ImGuiState & g = * GImGui ;
IM_ASSERT ( g . CurrentWindowStack . Size > = 2 ) ;
return g . CurrentWindowStack [ g . CurrentWindowStack . Size - 2 ] ;
}
2015-08-02 11:57:24 +08:00
bool ImGui : : FocusableItemRegister ( ImGuiWindow * window , bool is_active , bool tab_stop )
2014-08-11 04:58:11 +08:00
{
2015-02-01 12:28:24 +08:00
ImGuiState & g = * GImGui ;
2014-09-30 18:57:37 +08:00
2015-05-27 22:35:49 +08:00
const bool allow_keyboard_focus = window - > DC . AllowKeyboardFocus ;
2015-07-22 07:00:28 +08:00
window - > FocusIdxAllCounter + + ;
2014-09-30 18:57:37 +08:00
if ( allow_keyboard_focus )
2015-07-22 07:00:28 +08:00
window - > FocusIdxTabCounter + + ;
2014-08-11 04:58:11 +08:00
2014-09-30 19:22:58 +08:00
// Process keyboard input at this point: TAB, Shift-TAB switch focus
2014-09-30 18:57:37 +08:00
// We can always TAB out of a widget that doesn't allow tabbing in.
2015-07-22 07:00:28 +08:00
if ( tab_stop & & window - > FocusIdxAllRequestNext = = IM_INT_MAX & & window - > FocusIdxTabRequestNext = = IM_INT_MAX & & is_active & & IsKeyPressedMap ( ImGuiKey_Tab ) )
2014-08-19 19:09:13 +08:00
{
// Modulo on index will be applied at the end of frame once we've got the total counter of items.
2015-07-22 07:00:28 +08:00
window - > FocusIdxTabRequestNext = window - > FocusIdxTabCounter + ( g . IO . KeyShift ? ( allow_keyboard_focus ? - 1 : 0 ) : + 1 ) ;
2014-08-19 19:09:13 +08:00
}
2014-08-11 04:58:11 +08:00
2015-07-22 07:00:28 +08:00
if ( window - > FocusIdxAllCounter = = window - > FocusIdxAllRequestCurrent )
2014-09-30 19:31:35 +08:00
return true ;
2014-09-30 19:22:58 +08:00
if ( allow_keyboard_focus )
2015-07-22 07:00:28 +08:00
if ( window - > FocusIdxTabCounter = = window - > FocusIdxTabRequestCurrent )
2014-09-30 19:31:35 +08:00
return true ;
2014-09-30 18:57:37 +08:00
2014-09-30 19:22:58 +08:00
return false ;
2014-08-11 04:58:11 +08:00
}
2015-08-02 11:57:24 +08:00
void ImGui : : FocusableItemUnregister ( ImGuiWindow * window )
2015-03-21 04:50:55 +08:00
{
2015-07-22 07:00:28 +08:00
window - > FocusIdxAllCounter - - ;
window - > FocusIdxTabCounter - - ;
2014-08-11 04:58:11 +08:00
}
//-----------------------------------------------------------------------------
2014-11-10 09:29:01 +08:00
void * ImGui : : MemAlloc ( size_t sz )
2014-09-25 01:01:07 +08:00
{
2015-05-15 19:05:05 +08:00
GImGui - > IO . MetricsAllocs + + ;
2015-02-01 12:28:24 +08:00
return GImGui - > IO . MemAllocFn ( sz ) ;
2014-09-25 01:01:07 +08:00
}
2014-11-10 09:29:01 +08:00
void ImGui : : MemFree ( void * ptr )
2014-09-25 01:01:07 +08:00
{
2015-05-15 19:45:35 +08:00
if ( ptr ) GImGui - > IO . MetricsAllocs - - ;
2015-02-01 12:28:24 +08:00
return GImGui - > IO . MemFreeFn ( ptr ) ;
2014-09-25 01:01:07 +08:00
}
2014-09-25 03:39:39 +08:00
2014-08-11 04:58:11 +08:00
static ImGuiIniData * FindWindowSettings ( const char * name )
{
2015-02-01 12:28:24 +08:00
ImGuiState & g = * GImGui ;
2015-03-08 07:53:03 +08:00
ImGuiID id = ImHash ( name , 0 ) ;
2015-07-08 02:39:08 +08:00
for ( int i = 0 ; i ! = g . Settings . Size ; i + + )
2014-08-19 19:09:13 +08:00
{
2015-05-15 19:45:35 +08:00
ImGuiIniData * ini = & g . Settings [ i ] ;
2015-03-08 07:35:36 +08:00
if ( ini - > ID = = id )
2014-08-19 19:09:13 +08:00
return ini ;
}
2014-12-11 03:22:30 +08:00
return NULL ;
}
static ImGuiIniData * AddWindowSettings ( const char * name )
{
2015-07-08 02:39:08 +08:00
GImGui - > Settings . resize ( GImGui - > Settings . Size + 1 ) ;
2015-05-15 19:45:35 +08:00
ImGuiIniData * ini = & GImGui - > Settings . back ( ) ;
2014-08-31 18:28:21 +08:00
ini - > Name = ImStrdup ( name ) ;
2015-03-08 07:53:03 +08:00
ini - > ID = ImHash ( name , 0 ) ;
2014-08-16 16:28:29 +08:00
ini - > Collapsed = false ;
2014-08-19 19:09:13 +08:00
ini - > Pos = ImVec2 ( FLT_MAX , FLT_MAX ) ;
ini - > Size = ImVec2 ( 0 , 0 ) ;
return ini ;
2014-08-11 04:58:11 +08:00
}
// Zero-tolerance, poor-man .ini parsing
// FIXME: Write something less rubbish
static void LoadSettings ( )
{
2015-02-01 12:28:24 +08:00
ImGuiState & g = * GImGui ;
2014-08-19 19:09:13 +08:00
const char * filename = g . IO . IniFilename ;
if ( ! filename )
return ;
2014-12-29 19:23:03 +08:00
char * file_data ;
2015-07-08 02:09:25 +08:00
int file_size ;
2014-12-30 02:05:22 +08:00
if ( ! ImLoadFileToMemory ( filename , " rb " , ( void * * ) & file_data , & file_size , 1 ) )
2014-08-19 19:09:13 +08:00
return ;
ImGuiIniData * settings = NULL ;
2014-12-29 19:23:03 +08:00
const char * buf_end = file_data + file_size ;
for ( const char * line_start = file_data ; line_start < buf_end ; )
2014-08-19 19:09:13 +08:00
{
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 " , line_end - line_start - 2 , line_start + 1 ) ;
settings = FindWindowSettings ( name ) ;
2014-12-11 03:22:30 +08:00
if ( ! settings )
settings = AddWindowSettings ( name ) ;
2014-08-19 19:09:13 +08:00
}
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 ;
}
2014-08-11 04:58:11 +08:00
2014-12-29 19:23:03 +08:00
ImGui : : MemFree ( file_data ) ;
2014-08-11 04:58:11 +08:00
}
static void SaveSettings ( )
{
2015-02-01 12:28:24 +08:00
ImGuiState & g = * GImGui ;
2014-08-19 19:09:13 +08:00
const char * filename = g . IO . IniFilename ;
if ( ! filename )
return ;
// Gather data from windows that were active during this session
2015-07-08 02:39:08 +08:00
for ( int i = 0 ; i ! = g . Windows . Size ; i + + )
2014-08-19 19:09:13 +08:00
{
ImGuiWindow * window = g . Windows [ i ] ;
2014-12-11 01:13:45 +08:00
if ( window - > Flags & ImGuiWindowFlags_NoSavedSettings )
2014-08-19 19:09:13 +08:00
continue ;
ImGuiIniData * settings = FindWindowSettings ( window - > Name ) ;
settings - > Pos = window - > Pos ;
settings - > Size = window - > SizeFull ;
settings - > Collapsed = window - > Collapsed ;
}
// Write .ini file
// If a window wasn't opened in this session we preserve its settings
FILE * f = fopen ( filename , " wt " ) ;
if ( ! f )
return ;
2015-07-08 02:39:08 +08:00
for ( int i = 0 ; i ! = g . Settings . Size ; i + + )
2014-08-19 19:09:13 +08:00
{
2015-05-15 19:45:35 +08:00
const ImGuiIniData * settings = & g . Settings [ i ] ;
2014-08-19 19:09:13 +08:00
if ( settings - > Pos . x = = FLT_MAX )
continue ;
2015-03-08 07:35:36 +08:00
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 ) ;
2014-08-19 19:09:13 +08:00
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 ) ;
2014-08-11 04:58:11 +08:00
}
static void MarkSettingsDirty ( )
{
2015-02-01 12:28:24 +08:00
ImGuiState & g = * GImGui ;
2014-08-19 19:09:13 +08:00
if ( g . SettingsDirtyTimer < = 0.0f )
g . SettingsDirtyTimer = g . IO . IniSavingRate ;
2014-08-11 04:58:11 +08:00
}
2015-03-12 00:44:54 +08:00
const char * ImGui : : GetVersion ( )
{
return IMGUI_VERSION ;
2014-08-11 04:58:11 +08:00
}
2015-02-04 16:43:11 +08:00
// 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
2015-02-01 12:28:24 +08:00
void * ImGui : : GetInternalState ( )
{
2015-02-04 16:30:03 +08:00
return GImGui ;
2015-02-01 12:28:24 +08:00
}
2015-02-04 16:43:11 +08:00
size_t ImGui : : GetInternalStateSize ( )
2015-02-01 12:28:24 +08:00
{
2015-02-04 16:30:03 +08:00
return sizeof ( ImGuiState ) ;
2015-02-01 13:55:19 +08:00
}
void ImGui : : SetInternalState ( void * state , bool construct )
{
2015-02-04 16:43:11 +08:00
if ( construct )
new ( state ) ImGuiState ( ) ;
2015-02-04 16:30:03 +08:00
GImGui = ( ImGuiState * ) state ;
2015-02-01 12:28:24 +08:00
}
2014-11-10 09:29:01 +08:00
ImGuiIO & ImGui : : GetIO ( )
2014-08-11 04:58:11 +08:00
{
2015-02-01 12:28:24 +08:00
return GImGui - > IO ;
2014-08-11 04:58:11 +08:00
}
2014-11-10 09:29:01 +08:00
ImGuiStyle & ImGui : : GetStyle ( )
2014-08-11 04:58:11 +08:00
{
2015-02-01 12:28:24 +08:00
return GImGui - > Style ;
2014-08-11 04:58:11 +08:00
}
2014-11-10 09:29:01 +08:00
void ImGui : : NewFrame ( )
2014-08-11 04:58:11 +08:00
{
2015-02-01 12:28:24 +08:00
ImGuiState & g = * GImGui ;
2014-08-11 04:58:11 +08:00
2015-01-09 07:35:01 +08:00
// Check user data
2015-07-08 23:48:10 +08:00
IM_ASSERT ( g . IO . DeltaTime > = 0.0f ) ;
2014-12-22 09:45:13 +08:00
IM_ASSERT ( g . IO . DisplaySize . x > = 0.0f & & g . IO . DisplaySize . y > = 0.0f ) ;
2015-01-18 06:15:29 +08:00
IM_ASSERT ( g . IO . RenderDrawListsFn ! = NULL ) ; // Must be implemented
2015-07-08 23:48:10 +08:00
IM_ASSERT ( g . IO . Fonts - > Fonts . Size > 0 ) ; // Font Atlas not created. Did you call io.Fonts->GetTexDataAsRGBA32 / GetTexDataAsAlpha8 ?
2015-01-18 18:46:49 +08:00
IM_ASSERT ( g . IO . Fonts - > Fonts [ 0 ] - > IsLoaded ( ) ) ; // Font Atlas not created. Did you call io.Fonts->GetTexDataAsRGBA32 / GetTexDataAsAlpha8 ?
2014-08-11 04:58:11 +08:00
2014-08-19 19:09:13 +08:00
if ( ! g . Initialized )
{
2014-08-16 16:28:29 +08:00
// Initialize on first frame
2014-09-25 01:01:07 +08:00
g . LogClipboard = ( ImGuiTextBuffer * ) ImGui : : MemAlloc ( sizeof ( ImGuiTextBuffer ) ) ;
2014-08-31 18:28:21 +08:00
new ( g . LogClipboard ) ImGuiTextBuffer ( ) ;
2014-08-19 19:09:13 +08:00
IM_ASSERT ( g . Settings . empty ( ) ) ;
LoadSettings ( ) ;
g . Initialized = true ;
}
2014-11-29 08:02:46 +08:00
2015-07-22 07:00:28 +08:00
SetCurrentFont ( g . IO . Fonts - > Fonts [ 0 ] ) ;
2014-08-19 19:09:13 +08:00
g . Time + = g . IO . DeltaTime ;
g . FrameCount + = 1 ;
g . Tooltip [ 0 ] = ' \0 ' ;
2015-07-07 00:52:49 +08:00
g . OverlayDrawList . Clear ( ) ;
2015-07-15 23:26:03 +08:00
g . OverlayDrawList . PushTextureID ( g . IO . Fonts - > TexID ) ;
g . OverlayDrawList . PushClipRectFullScreen ( ) ;
2015-07-14 05:52:57 +08:00
g . OverlayDrawList . AddDrawCmd ( ) ;
2014-08-19 19:09:13 +08:00
// Update inputs state
if ( g . IO . MousePos . x < 0 & & g . IO . MousePos . y < 0 )
g . IO . MousePos = ImVec2 ( - 9999.0f , - 9999.0f ) ;
if ( ( g . IO . MousePos . x < 0 & & g . IO . MousePos . y < 0 ) | | ( g . IO . MousePosPrev . x < 0 & & g . IO . MousePosPrev . y < 0 ) ) // if mouse just appeared or disappeared (negative coordinate) we cancel out movement in MouseDelta
g . IO . MouseDelta = ImVec2 ( 0.0f , 0.0f ) ;
else
g . IO . MouseDelta = g . IO . MousePos - g . IO . MousePosPrev ;
g . IO . MousePosPrev = g . IO . MousePos ;
2015-07-08 02:09:25 +08:00
for ( int i = 0 ; i < IM_ARRAYSIZE ( g . IO . MouseDown ) ; i + + )
2014-08-19 19:09:13 +08:00
{
2015-06-22 08:15:31 +08:00
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 . MouseClicked [ i ] = g . IO . MouseDownDuration [ i ] = = 0.0f ;
g . IO . MouseReleased [ i ] = g . IO . MouseDownDurationPrev [ i ] > = 0.0f & & ! g . IO . MouseDown [ i ] ;
2014-08-19 19:09:13 +08:00
g . IO . MouseDoubleClicked [ i ] = false ;
if ( g . IO . MouseClicked [ i ] )
{
if ( g . Time - g . IO . MouseClickedTime [ i ] < g . IO . MouseDoubleClickTime )
{
2014-12-29 19:23:03 +08:00
if ( ImLengthSqr ( g . IO . MousePos - g . IO . MouseClickedPos [ i ] ) < g . IO . MouseDoubleClickMaxDist * g . IO . MouseDoubleClickMaxDist )
2014-08-19 19:09:13 +08:00
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 ;
}
2015-03-20 18:46:07 +08:00
g . IO . MouseClickedPos [ i ] = g . IO . MousePos ;
2015-03-20 19:13:18 +08:00
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 ] ) ) ;
2014-08-19 19:09:13 +08:00
}
}
2015-06-22 09:04:43 +08:00
memcpy ( g . IO . KeysDownDurationPrev , g . IO . KeysDownDuration , sizeof ( g . IO . KeysDownDuration ) ) ;
2015-07-08 02:09:25 +08:00
for ( int i = 0 ; i < IM_ARRAYSIZE ( g . IO . KeysDown ) ; i + + )
2015-06-22 09:04:43 +08:00
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 ;
2014-08-19 19:09:13 +08:00
2015-02-12 02:28:17 +08:00
// 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 ) ) ;
2014-08-19 19:09:13 +08:00
// Clear reference to active widget if the widget isn't alive anymore
2015-05-18 00:13:45 +08:00
g . HoveredIdPreviousFrame = g . HoveredId ;
2014-08-19 19:09:13 +08:00
g . HoveredId = 0 ;
if ( ! g . ActiveIdIsAlive & & g . ActiveIdPreviousFrame = = g . ActiveId & & g . ActiveId ! = 0 )
2015-07-22 07:00:28 +08:00
SetActiveID ( 0 ) ;
2014-08-19 19:09:13 +08:00
g . ActiveIdPreviousFrame = g . ActiveId ;
g . ActiveIdIsAlive = false ;
2015-04-13 02:15:55 +08:00
g . ActiveIdIsJustActivated = false ;
2015-02-28 18:25:04 +08:00
if ( ! g . ActiveId )
g . MovedWindow = NULL ;
2014-08-19 19:09:13 +08:00
// Delay saving settings so we don't spam disk too much
if ( g . SettingsDirtyTimer > 0.0f )
{
g . SettingsDirtyTimer - = g . IO . DeltaTime ;
if ( g . SettingsDirtyTimer < = 0.0f )
SaveSettings ( ) ;
}
2015-01-05 06:07:37 +08:00
// Find the window we are hovering. Child windows can extend beyond the limit of their parent so we need to derive HoveredRootWindow from HoveredWindow
2014-11-10 09:29:01 +08:00
g . HoveredWindow = FindHoveredWindow ( g . IO . MousePos , false ) ;
2015-01-05 06:07:37 +08:00
if ( g . HoveredWindow & & ( g . HoveredWindow - > Flags & ImGuiWindowFlags_ChildWindow ) )
g . HoveredRootWindow = g . HoveredWindow - > RootWindow ;
else
g . HoveredRootWindow = FindHoveredWindow ( g . IO . MousePos , true ) ;
2014-08-19 19:09:13 +08:00
2015-06-24 00:02:56 +08:00
if ( ImGuiWindow * modal_window = GetFrontMostModalRootWindow ( ) )
2015-06-24 02:34:15 +08:00
{
g . ModalWindowDarkeningRatio = ImMin ( g . ModalWindowDarkeningRatio + g . IO . DeltaTime * 6.0f , 1.0f ) ;
2015-06-24 00:02:56 +08:00
if ( g . HoveredRootWindow ! = modal_window )
g . HoveredRootWindow = g . HoveredWindow = NULL ;
2015-06-24 02:34:15 +08:00
}
else
{
g . ModalWindowDarkeningRatio = 0.0f ;
}
2014-08-19 19:09:13 +08:00
2015-02-03 17:56:19 +08:00
// 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.
int mouse_earliest_button_down = - 1 ;
2015-05-29 18:42:35 +08:00
bool mouse_any_down = false ;
2015-03-10 02:36:03 +08:00
for ( int i = 0 ; i < IM_ARRAYSIZE ( g . IO . MouseDown ) ; i + + )
2015-02-03 17:56:19 +08:00
{
if ( g . IO . MouseClicked [ i ] )
2015-05-29 18:42:35 +08:00
g . IO . MouseDownOwned [ i ] = ( g . HoveredWindow ! = NULL ) | | ( ! g . OpenedPopupStack . empty ( ) ) ;
mouse_any_down | = g . IO . MouseDown [ i ] ;
2015-02-03 17:56:19 +08:00
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_owned_by_application = mouse_earliest_button_down ! = - 1 & & ! g . IO . MouseDownOwned [ mouse_earliest_button_down ] ;
2015-07-02 23:10:31 +08:00
g . IO . WantCaptureMouse = ( ! mouse_owned_by_application & & g . HoveredWindow ! = NULL ) | | ( ! mouse_owned_by_application & & mouse_any_down ) | | ( g . ActiveId ! = 0 ) | | ( ! g . OpenedPopupStack . empty ( ) ) | | ( g . CaptureMouseNextFrame ) ;
g . IO . WantCaptureKeyboard = ( g . ActiveId ! = 0 ) | | ( g . CaptureKeyboardNextFrame ) ;
2015-03-21 21:59:19 +08:00
g . MouseCursor = ImGuiMouseCursor_Arrow ;
2015-07-02 23:10:31 +08:00
g . CaptureMouseNextFrame = g . CaptureKeyboardNextFrame = false ;
2014-08-19 19:09:13 +08:00
2015-02-03 18:35:43 +08:00
// If mouse was first clicked outside of ImGui bounds we also cancel out hovering.
if ( mouse_owned_by_application )
g . HoveredWindow = g . HoveredRootWindow = NULL ;
2014-08-19 19:09:13 +08:00
// Scale & Scrolling
2015-06-20 07:43:51 +08:00
if ( g . HoveredWindow & & g . IO . MouseWheel ! = 0.0f & & ! g . HoveredWindow - > Collapsed )
2014-08-19 19:09:13 +08:00
{
ImGuiWindow * window = g . HoveredWindow ;
if ( g . IO . KeyCtrl )
{
2014-09-24 21:03:42 +08:00
if ( g . IO . FontAllowUserScaling )
2014-08-19 19:09:13 +08:00
{
// Zoom / Scale window
2014-11-30 18:20:25 +08:00
float new_font_scale = ImClamp ( window - > FontWindowScale + g . IO . MouseWheel * 0.10f , 0.50f , 2.50f ) ;
2014-09-24 21:03:42 +08:00
float scale = new_font_scale / window - > FontWindowScale ;
window - > FontWindowScale = new_font_scale ;
2014-08-19 19:09:13 +08:00
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
{
// Scroll
2014-12-04 00:54:04 +08:00
if ( ! ( window - > Flags & ImGuiWindowFlags_NoScrollWithMouse ) )
{
const int scroll_lines = ( window - > Flags & ImGuiWindowFlags_ComboBox ) ? 3 : 5 ;
2015-06-20 08:01:13 +08:00
SetWindowScrollY ( window , window - > ScrollY - g . IO . MouseWheel * window - > CalcFontSize ( ) * scroll_lines ) ;
2014-12-04 00:54:04 +08:00
}
2014-08-19 19:09:13 +08:00
}
}
// 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.
2015-05-02 20:20:50 +08:00
if ( g . ActiveId = = 0 & & g . FocusedWindow ! = NULL & & g . FocusedWindow - > Active & & IsKeyPressedMap ( ImGuiKey_Tab , false ) )
2014-09-30 19:22:58 +08:00
g . FocusedWindow - > FocusIdxTabRequestNext = 0 ;
2014-08-19 19:09:13 +08:00
// Mark all windows as not visible
2015-07-08 02:39:08 +08:00
for ( int i = 0 ; i ! = g . Windows . Size ; i + + )
2014-08-19 19:09:13 +08:00
{
ImGuiWindow * window = g . Windows [ i ] ;
2015-05-02 20:20:50 +08:00
window - > WasActive = window - > Active ;
window - > Active = false ;
2015-03-20 18:54:21 +08:00
window - > Accessed = false ;
2014-08-19 19:09:13 +08:00
}
// 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.
2015-02-01 22:39:13 +08:00
g . CurrentWindowStack . resize ( 0 ) ;
2015-05-11 22:57:02 +08:00
g . CurrentPopupStack . resize ( 0 ) ;
CloseInactivePopups ( ) ;
2014-08-19 19:09:13 +08:00
// Create implicit window - we will only render it if the user has added something to it.
2015-03-24 06:45:05 +08:00
ImGui : : SetNextWindowSize ( ImVec2 ( 400 , 400 ) , ImGuiSetCond_FirstUseEver ) ;
ImGui : : Begin ( " Debug " ) ;
2014-08-11 04:58:11 +08:00
}
2015-03-15 09:48:59 +08:00
// NB: behavior of ImGui after Shutdown() is not tested/guaranteed at the moment. This function is merely here to free heap allocations.
2014-11-10 09:29:01 +08:00
void ImGui : : Shutdown ( )
2014-08-11 04:58:11 +08:00
{
2015-02-01 12:28:24 +08:00
ImGuiState & g = * GImGui ;
2014-08-19 19:09:13 +08:00
if ( ! g . Initialized )
return ;
2014-08-11 04:58:11 +08:00
2014-08-19 19:09:13 +08:00
SaveSettings ( ) ;
2014-08-11 04:58:11 +08:00
2015-07-08 02:39:08 +08:00
for ( int i = 0 ; i < g . Windows . Size ; i + + )
2014-08-31 18:28:21 +08:00
{
2014-08-16 16:28:29 +08:00
g . Windows [ i ] - > ~ ImGuiWindow ( ) ;
2014-09-25 01:01:07 +08:00
ImGui : : MemFree ( g . Windows [ i ] ) ;
2014-08-16 16:28:29 +08:00
}
2014-08-19 19:09:13 +08:00
g . Windows . clear ( ) ;
2015-03-27 02:51:45 +08:00
g . WindowsSortBuffer . clear ( ) ;
2014-08-19 19:09:13 +08:00
g . CurrentWindowStack . clear ( ) ;
g . FocusedWindow = NULL ;
g . HoveredWindow = NULL ;
2014-12-04 00:54:04 +08:00
g . HoveredRootWindow = NULL ;
2015-07-08 02:39:08 +08:00
for ( int i = 0 ; i < g . Settings . Size ; i + + )
2015-05-15 19:45:35 +08:00
ImGui : : MemFree ( g . Settings [ i ] . Name ) ;
2014-08-19 19:09:13 +08:00
g . Settings . clear ( ) ;
2015-01-07 22:55:35 +08:00
g . ColorModifiers . clear ( ) ;
g . StyleModifiers . clear ( ) ;
2015-01-17 22:13:08 +08:00
g . FontStack . clear ( ) ;
2015-05-11 22:57:02 +08:00
g . OpenedPopupStack . clear ( ) ;
g . CurrentPopupStack . clear ( ) ;
2015-07-08 02:09:25 +08:00
for ( int i = 0 ; i < IM_ARRAYSIZE ( g . RenderDrawLists ) ; i + + )
2015-03-27 02:51:45 +08:00
g . RenderDrawLists [ i ] . clear ( ) ;
2015-07-07 00:52:49 +08:00
g . OverlayDrawList . ClearFreeMemory ( ) ;
2014-08-19 19:09:13 +08:00
g . ColorEditModeStorage . Clear ( ) ;
if ( g . PrivateClipboard )
{
2014-09-25 01:01:07 +08:00
ImGui : : MemFree ( g . PrivateClipboard ) ;
2014-08-19 19:09:13 +08:00
g . PrivateClipboard = NULL ;
}
2015-06-20 08:14:50 +08:00
g . InputTextState . Text . clear ( ) ;
g . InputTextState . InitialText . clear ( ) ;
g . InputTextState . TempTextBuffer . clear ( ) ;
2014-08-14 22:03:10 +08:00
2015-03-21 08:41:34 +08:00
if ( g . LogFile & & g . LogFile ! = stdout )
{
fclose ( g . LogFile ) ;
g . LogFile = NULL ;
}
2014-08-18 19:38:03 +08:00
if ( g . LogClipboard )
{
g . LogClipboard - > ~ ImGuiTextBuffer ( ) ;
2014-09-25 01:01:07 +08:00
ImGui : : MemFree ( g . LogClipboard ) ;
2014-08-18 19:38:03 +08:00
}
2014-08-16 16:28:29 +08:00
2015-05-09 17:27:03 +08:00
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 ( ) ;
2015-03-21 08:41:34 +08:00
2014-08-19 19:09:13 +08:00
g . Initialized = false ;
2014-08-11 04:58:11 +08:00
}
2015-02-09 01:21:51 +08:00
// 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 ;
2015-03-27 01:09:17 +08:00
if ( int d = ( a - > Flags & ImGuiWindowFlags_Popup ) - ( b - > Flags & ImGuiWindowFlags_Popup ) )
return d ;
2015-02-09 01:21:51 +08:00
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 0 ;
}
2015-03-27 02:51:45 +08:00
static void AddWindowToSortedBuffer ( ImVector < ImGuiWindow * > & out_sorted_windows , ImGuiWindow * window )
2014-08-11 04:58:11 +08:00
{
2015-03-27 02:51:45 +08:00
out_sorted_windows . push_back ( window ) ;
2015-05-02 20:20:50 +08:00
if ( window - > Active )
2014-08-19 19:09:13 +08:00
{
2015-07-08 02:39:08 +08:00
int count = window - > DC . ChildWindows . Size ;
2015-02-09 01:21:51 +08:00
if ( count > 1 )
2015-07-08 02:09:25 +08:00
qsort ( window - > DC . ChildWindows . begin ( ) , ( size_t ) count , sizeof ( ImGuiWindow * ) , ChildWindowComparer ) ;
for ( int i = 0 ; i < count ; i + + )
2014-08-19 19:09:13 +08:00
{
ImGuiWindow * child = window - > DC . ChildWindows [ i ] ;
2015-05-02 20:20:50 +08:00
if ( child - > Active )
2015-03-27 02:51:45 +08:00
AddWindowToSortedBuffer ( out_sorted_windows , child ) ;
2014-08-19 19:09:13 +08:00
}
}
2014-08-11 04:58:11 +08:00
}
2015-07-22 07:00:28 +08:00
static void AddDrawListToRenderList ( ImVector < ImDrawList * > & out_render_list , ImDrawList * draw_list )
{
if ( ! draw_list - > CmdBuffer . empty ( ) & & ! draw_list - > VtxBuffer . empty ( ) )
{
if ( draw_list - > CmdBuffer . back ( ) . ElemCount = = 0 )
draw_list - > CmdBuffer . pop_back ( ) ;
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 ( int i = 0 ; i < window - > DC . ChildWindows . Size ; i + + )
{
ImGuiWindow * child = window - > DC . ChildWindows [ i ] ;
if ( ! child - > Active ) // clipped children may have been marked not active
continue ;
if ( ( child - > Flags & ImGuiWindowFlags_Popup ) & & child - > HiddenFrames > 0 )
continue ;
AddWindowToRenderList ( out_render_list , child ) ;
}
}
static void PushClipRect ( const ImVec4 & clip_rect , bool clipped )
2014-08-11 04:58:11 +08:00
{
2015-08-02 11:57:24 +08:00
ImGuiWindow * window = ImGui : : GetCurrentWindow ( ) ;
2014-08-11 04:58:11 +08:00
2014-08-19 19:09:13 +08:00
ImVec4 cr = clip_rect ;
2015-07-07 11:46:12 +08:00
if ( clipped )
2014-08-19 19:09:13 +08:00
{
2015-02-03 03:50:28 +08:00
// Clip with existing clip rect
2015-07-07 11:46:12 +08:00
const ImVec4 cur_cr = window - > ClipRect ;
2014-08-19 19:09:13 +08:00
cr = ImVec4 ( ImMax ( cr . x , cur_cr . x ) , ImMax ( cr . y , cur_cr . y ) , ImMin ( cr . z , cur_cr . z ) , ImMin ( cr . w , cur_cr . w ) ) ;
}
2015-03-27 00:28:41 +08:00
cr . z = ImMax ( cr . x , cr . z ) ;
cr . w = ImMax ( cr . y , cr . w ) ;
2014-08-11 04:58:11 +08:00
2015-03-27 00:03:30 +08:00
IM_ASSERT ( cr . x < = cr . z & & cr . y < = cr . w ) ;
2015-07-07 11:46:12 +08:00
window - > ClipRect = cr ;
2014-08-19 19:09:13 +08:00
window - > DrawList - > PushClipRect ( cr ) ;
2014-08-11 04:58:11 +08:00
}
static void PopClipRect ( )
{
2015-08-02 11:57:24 +08:00
ImGuiWindow * window = ImGui : : GetCurrentWindow ( ) ;
2014-08-19 19:09:13 +08:00
window - > DrawList - > PopClipRect ( ) ;
2015-07-08 10:17:07 +08:00
window - > ClipRect = window - > DrawList - > _ClipRectStack . back ( ) ;
2014-08-11 04:58:11 +08:00
}
2014-11-10 09:29:01 +08:00
void ImGui : : Render ( )
2014-08-11 04:58:11 +08:00
{
2015-02-01 12:28:24 +08:00
ImGuiState & g = * GImGui ;
2015-05-07 18:36:16 +08:00
IM_ASSERT ( g . Initialized ) ; // Forgot to call ImGui::NewFrame()
2014-08-19 19:09:13 +08:00
const bool first_render_of_the_frame = ( g . FrameCountRendered ! = g . FrameCount ) ;
g . FrameCountRendered = g . FrameCount ;
if ( first_render_of_the_frame )
{
2015-05-07 18:36:16 +08:00
// Hide implicit "Debug" window if it hasn't been used
2015-07-08 02:39:08 +08:00
IM_ASSERT ( g . CurrentWindowStack . Size = = 1 ) ; // Mismatched Begin/End
2015-03-20 18:54:21 +08:00
if ( g . CurrentWindow & & ! g . CurrentWindow - > Accessed )
2015-05-02 20:20:50 +08:00
g . CurrentWindow - > Active = false ;
2014-08-19 19:09:13 +08:00
ImGui : : End ( ) ;
2015-05-07 18:36:16 +08:00
// Click to focus window and start moving (after we're done with all our widgets)
2015-07-11 08:54:26 +08:00
if ( ! g . ActiveId )
g . MovedWindow = NULL ;
2015-04-06 18:25:07 +08:00
if ( g . ActiveId = = 0 & & g . HoveredId = = 0 & & g . IO . MouseClicked [ 0 ] )
2015-02-28 18:25:04 +08:00
{
2015-05-28 06:24:39 +08:00
if ( ! ( g . FocusedWindow & & ! g . FocusedWindow - > WasActive & & g . FocusedWindow - > Active ) ) // Unless we just made a popup appear
2015-04-06 18:25:07 +08:00
{
2015-05-28 06:24:39 +08:00
if ( g . HoveredRootWindow ! = NULL )
{
g . MovedWindow = g . HoveredWindow ;
2015-07-22 07:00:28 +08:00
SetActiveID ( g . HoveredRootWindow - > MoveID , g . HoveredRootWindow ) ;
2015-05-28 06:24:39 +08:00
}
2015-06-24 00:02:56 +08:00
else if ( g . FocusedWindow ! = NULL & & GetFrontMostModalRootWindow ( ) = = NULL )
2015-05-28 06:24:39 +08:00
{
// Clicking on void disable focus
FocusWindow ( NULL ) ;
}
2015-04-06 18:25:07 +08:00
}
2015-02-28 18:25:04 +08:00
}
2015-01-16 18:55:49 +08:00
2014-08-19 19:09:13 +08:00
// Sort the window list so that all child windows are after their parent
// We cannot do that on FocusWindow() because childs may not exist yet
2015-03-27 02:01:40 +08:00
g . WindowsSortBuffer . resize ( 0 ) ;
2015-07-08 02:39:08 +08:00
g . WindowsSortBuffer . reserve ( g . Windows . Size ) ;
for ( int i = 0 ; i ! = g . Windows . Size ; i + + )
2014-08-19 19:09:13 +08:00
{
ImGuiWindow * window = g . Windows [ i ] ;
2015-05-02 20:20:50 +08:00
if ( window - > Flags & ImGuiWindowFlags_ChildWindow ) // if a child is active its parent will add it
if ( window - > Active )
2014-08-19 19:09:13 +08:00
continue ;
2015-03-27 02:51:45 +08:00
AddWindowToSortedBuffer ( g . WindowsSortBuffer , window ) ;
2014-08-19 19:09:13 +08:00
}
2015-07-08 02:39:08 +08:00
IM_ASSERT ( g . Windows . Size = = g . WindowsSortBuffer . Size ) ; // we done something wrong
2015-03-27 02:01:40 +08:00
g . Windows . swap ( g . WindowsSortBuffer ) ;
2014-08-19 19:09:13 +08:00
2015-05-07 18:36:16 +08:00
// Clear Input data for next frame
2014-11-30 18:20:25 +08:00
g . IO . MouseWheel = 0.0f ;
2014-08-19 19:09:13 +08:00
memset ( g . IO . InputCharacters , 0 , sizeof ( g . IO . InputCharacters ) ) ;
}
// Skip render altogether if alpha is 0.0
2015-07-06 11:55:24 +08:00
// Note that vertex buffers have been created and are wasted, so it is best practice that you don't create windows in the first place, or respond to Begin() returning false.
2014-08-19 19:09:13 +08:00
if ( g . Style . Alpha > 0.0f )
{
// Render tooltip
if ( g . Tooltip [ 0 ] )
{
ImGui : : BeginTooltip ( ) ;
ImGui : : TextUnformatted ( g . Tooltip ) ;
ImGui : : EndTooltip ( ) ;
}
// Gather windows to render
2015-05-16 00:33:04 +08:00
g . IO . MetricsRenderVertices = g . IO . MetricsRenderIndices = g . IO . MetricsActiveWindows = 0 ;
2015-07-08 02:09:25 +08:00
for ( int i = 0 ; i < IM_ARRAYSIZE ( g . RenderDrawLists ) ; i + + )
2015-03-27 02:51:45 +08:00
g . RenderDrawLists [ i ] . resize ( 0 ) ;
2015-07-08 02:39:08 +08:00
for ( int i = 0 ; i ! = g . Windows . Size ; i + + )
2014-08-19 19:09:13 +08:00
{
ImGuiWindow * window = g . Windows [ i ] ;
2015-05-03 01:22:46 +08:00
if ( window - > Active & & window - > HiddenFrames < = 0 & & ( window - > Flags & ( ImGuiWindowFlags_ChildWindow ) ) = = 0 )
2015-03-27 02:51:45 +08:00
{
2015-07-06 11:55:24 +08:00
// FIXME: Generalize this with a proper layering system so e.g. user can draw in specific layers, below text, ..
2015-05-12 23:08:34 +08:00
g . IO . MetricsActiveWindows + + ;
2015-03-27 02:51:45 +08:00
if ( window - > Flags & ImGuiWindowFlags_Popup )
AddWindowToRenderList ( g . RenderDrawLists [ 1 ] , window ) ;
else if ( window - > Flags & ImGuiWindowFlags_Tooltip )
AddWindowToRenderList ( g . RenderDrawLists [ 2 ] , window ) ;
else
AddWindowToRenderList ( g . RenderDrawLists [ 0 ] , window ) ;
}
2014-08-19 19:09:13 +08:00
}
2015-03-27 02:51:45 +08:00
// Flatten layers
2015-07-08 02:39:08 +08:00
int n = g . RenderDrawLists [ 0 ] . Size ;
2015-07-08 02:09:25 +08:00
int flattened_size = n ;
for ( int i = 1 ; i < IM_ARRAYSIZE ( g . RenderDrawLists ) ; i + + )
2015-07-08 02:39:08 +08:00
flattened_size + = g . RenderDrawLists [ i ] . Size ;
2015-03-27 02:51:45 +08:00
g . RenderDrawLists [ 0 ] . resize ( flattened_size ) ;
2015-07-08 02:09:25 +08:00
for ( int i = 1 ; i < IM_ARRAYSIZE ( g . RenderDrawLists ) ; i + + )
2014-08-19 19:09:13 +08:00
{
2015-03-27 02:51:45 +08:00
ImVector < ImDrawList * > & layer = g . RenderDrawLists [ i ] ;
if ( ! layer . empty ( ) )
{
2015-07-08 02:39:08 +08:00
memcpy ( & g . RenderDrawLists [ 0 ] [ n ] , & layer [ 0 ] , layer . Size * sizeof ( ImDrawList * ) ) ;
n + = layer . Size ;
2015-03-27 02:51:45 +08:00
}
2014-08-19 19:09:13 +08:00
}
2015-02-01 02:55:05 +08:00
if ( g . IO . MouseDrawCursor )
{
2015-03-21 21:59:19 +08:00
const ImGuiMouseCursorData & cursor_data = g . MouseCursorData [ g . MouseCursor ] ;
2015-07-22 07:00:28 +08:00
const ImVec2 pos = g . IO . MousePos - cursor_data . HotOffset ;
2015-03-21 21:59:19 +08:00
const ImVec2 size = cursor_data . Size ;
2015-02-01 02:55:05 +08:00
const ImTextureID tex_id = g . IO . Fonts - > TexID ;
2015-07-07 00:52:49 +08:00
g . OverlayDrawList . PushTextureID ( tex_id ) ;
g . OverlayDrawList . AddImage ( tex_id , pos + ImVec2 ( 1 , 0 ) , pos + ImVec2 ( 1 , 0 ) + size , cursor_data . TexUvMin [ 1 ] , cursor_data . TexUvMax [ 1 ] , 0x30000000 ) ; // Shadow
g . OverlayDrawList . AddImage ( tex_id , pos + ImVec2 ( 2 , 0 ) , pos + ImVec2 ( 2 , 0 ) + size , cursor_data . TexUvMin [ 1 ] , cursor_data . TexUvMax [ 1 ] , 0x30000000 ) ; // Shadow
g . OverlayDrawList . AddImage ( tex_id , pos , pos + size , cursor_data . TexUvMin [ 1 ] , cursor_data . TexUvMax [ 1 ] , 0xFF000000 ) ; // Black border
g . OverlayDrawList . AddImage ( tex_id , pos , pos + size , cursor_data . TexUvMin [ 0 ] , cursor_data . TexUvMax [ 0 ] , 0xFFFFFFFF ) ; // White fill
g . OverlayDrawList . PopTextureID ( ) ;
2015-02-01 02:55:05 +08:00
}
2015-07-08 10:17:07 +08:00
if ( ! g . OverlayDrawList . VtxBuffer . empty ( ) )
2015-07-07 00:52:49 +08:00
AddDrawListToRenderList ( g . RenderDrawLists [ 0 ] , & g . OverlayDrawList ) ;
2015-02-01 02:55:05 +08:00
2014-08-19 19:09:13 +08:00
// Render
2015-03-27 02:51:45 +08:00
if ( ! g . RenderDrawLists [ 0 ] . empty ( ) )
2015-07-06 12:03:46 +08:00
{
ImDrawData data ;
2015-07-08 10:17:07 +08:00
data . CmdLists = & g . RenderDrawLists [ 0 ] [ 0 ] ;
data . CmdListsCount = g . RenderDrawLists [ 0 ] . Size ;
data . TotalVtxCount = g . IO . MetricsRenderVertices ;
data . TotalIdxCount = g . IO . MetricsRenderIndices ;
2015-07-06 12:03:46 +08:00
g . IO . RenderDrawListsFn ( & data ) ;
}
2014-08-19 19:09:13 +08:00
}
2014-08-11 04:58:11 +08:00
}
// Find the optional ## from which we stop displaying text.
2015-07-22 07:00:28 +08:00
static const char * FindTextDisplayEnd ( const char * text , const char * text_end )
2014-08-11 04:58:11 +08:00
{
2014-08-19 19:09:13 +08:00
const char * text_display_end = text ;
2015-03-08 03:36:18 +08:00
if ( ! text_end )
text_end = ( const char * ) - 1 ;
2015-04-07 20:37:30 +08:00
ImGuiState & g = * GImGui ;
if ( g . DisableHideTextAfterDoubleHash > 0 )
{
while ( text_display_end < text_end & & * text_display_end ! = ' \0 ' )
text_display_end + + ;
}
else
{
while ( text_display_end < text_end & & * text_display_end ! = ' \0 ' & & ( text_display_end [ 0 ] ! = ' # ' | | text_display_end [ 1 ] ! = ' # ' ) )
text_display_end + + ;
}
2014-08-19 19:09:13 +08:00
return text_display_end ;
2014-08-11 04:58:11 +08:00
}
2014-12-30 04:18:52 +08:00
// Pass text data straight to log (without being displayed)
void ImGui : : LogText ( const char * fmt , . . . )
{
2015-02-01 12:28:24 +08:00
ImGuiState & g = * GImGui ;
2014-12-30 04:18:52 +08:00
if ( ! g . LogEnabled )
return ;
va_list args ;
va_start ( args , fmt ) ;
if ( g . LogFile )
{
vfprintf ( g . LogFile , fmt , args ) ;
}
else
{
g . LogClipboard - > appendv ( fmt , args ) ;
}
va_end ( args ) ;
}
// Internal version that takes a position to decide on newline placement and pad items according to their depth.
2015-02-08 23:57:06 +08:00
// We split text into individual lines to add current tree level padding
2015-07-31 13:48:59 +08:00
static void LogRenderedText ( const ImVec2 & ref_pos , const char * text , const char * text_end )
2014-08-11 04:58:11 +08:00
{
2015-02-01 12:28:24 +08:00
ImGuiState & g = * GImGui ;
2015-08-02 11:57:24 +08:00
ImGuiWindow * window = ImGui : : GetCurrentWindow ( ) ;
2014-08-19 19:09:13 +08:00
if ( ! text_end )
text_end = FindTextDisplayEnd ( text , text_end ) ;
2015-03-18 00:40:47 +08:00
const bool log_new_line = ref_pos . y > window - > DC . LogLinePosY + 1 ;
window - > DC . LogLinePosY = ref_pos . y ;
2014-08-19 19:09:13 +08:00
const char * text_remaining = text ;
2014-12-30 04:18:52 +08:00
if ( g . LogStartDepth > window - > DC . TreeDepth ) // Re-adjust padding if we have popped out of our starting depth
g . LogStartDepth = window - > DC . TreeDepth ;
const int tree_depth = ( window - > DC . TreeDepth - g . LogStartDepth ) ;
2015-02-02 17:06:51 +08:00
for ( ; ; )
2014-08-19 19:09:13 +08:00
{
2015-01-14 21:04:33 +08:00
// Split the string. Each new line (after a '\n') is followed by spacing corresponding to the current depth of our log entry.
2014-08-19 19:09:13 +08:00
const char * line_end = text_remaining ;
while ( line_end < text_end )
if ( * line_end = = ' \n ' )
break ;
else
line_end + + ;
if ( line_end > = text_end )
line_end = NULL ;
2014-09-02 21:35:06 +08:00
const bool is_first_line = ( text = = text_remaining ) ;
2014-08-19 19:09:13 +08:00
bool is_last_line = false ;
if ( line_end = = NULL )
{
is_last_line = true ;
line_end = text_end ;
}
if ( line_end ! = NULL & & ! ( is_last_line & & ( line_end - text_remaining ) = = 0 ) )
{
const int char_count = ( int ) ( line_end - text_remaining ) ;
2014-12-30 04:18:52 +08:00
if ( log_new_line | | ! is_first_line )
2015-06-30 00:39:17 +08:00
ImGui : : LogText ( IM_NEWLINE " %*s%.*s " , tree_depth * 4 , " " , char_count , text_remaining ) ;
2014-08-19 19:09:13 +08:00
else
2014-12-30 04:18:52 +08:00
ImGui : : LogText ( " %.*s " , char_count , text_remaining ) ;
2014-08-19 19:09:13 +08:00
}
2014-08-11 04:58:11 +08:00
2014-08-19 19:09:13 +08:00
if ( is_last_line )
break ;
text_remaining = line_end + 1 ;
}
2014-08-11 04:58:11 +08:00
}
2015-08-02 11:57:24 +08:00
float ImGui : : CalcWrapWidthForPos ( const ImVec2 & pos , float wrap_pos_x )
2014-11-07 13:45:56 +08:00
{
if ( wrap_pos_x < 0.0f )
return 0.0f ;
2015-08-02 11:57:24 +08:00
ImGuiWindow * window = ImGui : : GetCurrentWindow ( ) ;
2014-11-07 16:22:38 +08:00
if ( wrap_pos_x = = 0.0f )
2014-11-10 09:29:01 +08:00
wrap_pos_x = ImGui : : GetContentRegionMax ( ) . x ;
2014-11-07 13:45:56 +08:00
if ( wrap_pos_x > 0.0f )
wrap_pos_x + = window - > Pos . x ; // wrap_pos_x is provided is window local space
const float wrap_width = wrap_pos_x > 0.0f ? ImMax ( wrap_pos_x - pos . x , 0.00001f ) : 0.0f ;
return wrap_width ;
}
2015-02-08 23:57:06 +08:00
// Internal ImGui functions to render text
// RenderText***() functions calls ImDrawList::AddText() calls ImBitmapFont::RenderText()
2015-08-02 11:57:24 +08:00
void ImGui : : RenderText ( ImVec2 pos , const char * text , const char * text_end , bool hide_text_after_hash )
2014-08-11 04:58:11 +08:00
{
2015-02-01 12:28:24 +08:00
ImGuiState & g = * GImGui ;
2014-08-19 19:09:13 +08:00
ImGuiWindow * window = GetCurrentWindow ( ) ;
// Hide anything after a '##' string
const char * text_display_end ;
if ( hide_text_after_hash )
{
text_display_end = FindTextDisplayEnd ( text , text_end ) ;
}
else
{
if ( ! text_end )
2014-11-17 01:27:26 +08:00
text_end = text + strlen ( text ) ; // FIXME-OPT
2014-08-19 19:09:13 +08:00
text_display_end = text_end ;
}
const int text_len = ( int ) ( text_display_end - text ) ;
if ( text_len > 0 )
{
// Render
2015-03-30 02:45:03 +08:00
window - > DrawList - > AddText ( g . Font , g . FontSize , pos , window - > Color ( ImGuiCol_Text ) , text , text_display_end ) ;
2015-02-08 23:39:40 +08:00
2015-02-08 23:57:06 +08:00
// Log as text
2015-02-08 23:39:40 +08:00
if ( g . LogEnabled )
2015-07-31 13:48:59 +08:00
LogRenderedText ( pos , text , text_display_end ) ;
2015-02-08 23:39:40 +08:00
}
}
2015-08-02 11:57:24 +08:00
void ImGui : : RenderTextWrapped ( ImVec2 pos , const char * text , const char * text_end , float wrap_width )
2015-02-08 23:57:06 +08:00
{
ImGuiState & g = * GImGui ;
ImGuiWindow * window = GetCurrentWindow ( ) ;
if ( ! text_end )
text_end = text + strlen ( text ) ; // FIXME-OPT
const int text_len = ( int ) ( text_end - text ) ;
if ( text_len > 0 )
{
2015-03-30 02:45:03 +08:00
window - > DrawList - > AddText ( g . Font , g . FontSize , pos , window - > Color ( ImGuiCol_Text ) , text , text_end , wrap_width ) ;
2015-02-08 23:57:06 +08:00
if ( g . LogEnabled )
2015-07-31 13:48:59 +08:00
LogRenderedText ( pos , text , text_end ) ;
2015-02-08 23:57:06 +08:00
}
}
2015-08-02 11:57:24 +08:00
void ImGui : : RenderTextClipped ( const ImVec2 & pos_min , const ImVec2 & pos_max , const char * text , const char * text_end , const ImVec2 * text_size_if_known , ImGuiAlign align , const ImVec2 * clip_min , const ImVec2 * clip_max )
2015-02-08 23:39:40 +08:00
{
// Hide anything after a '##' string
const char * text_display_end = FindTextDisplayEnd ( text , text_end ) ;
const int text_len = ( int ) ( text_display_end - text ) ;
2015-05-12 21:02:45 +08:00
if ( text_len = = 0 )
return ;
2015-02-08 23:39:40 +08:00
2015-05-12 21:02:45 +08:00
ImGuiState & g = * GImGui ;
ImGuiWindow * window = GetCurrentWindow ( ) ;
2015-02-08 23:39:40 +08:00
2015-05-12 21:02:45 +08:00
// Perform CPU side clipping for single clipped element to avoid using scissor state
2015-05-27 21:24:52 +08:00
ImVec2 pos = pos_min ;
2015-05-12 21:02:45 +08:00
const ImVec2 text_size = text_size_if_known ? * text_size_if_known : ImGui : : CalcTextSize ( text , text_display_end , false , 0.0f ) ;
2015-06-17 07:21:07 +08:00
if ( ! clip_max ) clip_max = & pos_max ;
bool need_clipping = ( pos . x + text_size . x > = clip_max - > x ) | | ( pos . y + text_size . y > = clip_max - > y ) ;
if ( ! clip_min ) clip_min = & pos_min ; else need_clipping | = ( pos . x < clip_min - > x ) | | ( pos . y < clip_min - > y ) ;
2014-08-19 19:09:13 +08:00
2015-05-12 21:02:45 +08:00
// Align
if ( align & ImGuiAlign_Center ) pos . x = ImMax ( pos . x , ( pos . x + pos_max . x - text_size . x ) * 0.5f ) ;
else if ( align & ImGuiAlign_Right ) pos . x = ImMax ( pos . x , pos_max . x - text_size . x ) ;
2015-05-27 21:20:50 +08:00
if ( align & ImGuiAlign_VCenter ) pos . y = ImMax ( pos . y , ( pos . y + pos_max . y - text_size . y ) * 0.5f ) ;
2015-05-12 21:02:45 +08:00
// Render
2015-06-17 07:21:07 +08:00
if ( need_clipping )
{
ImVec4 fine_clip_rect ( clip_min - > x , clip_min - > y , clip_max - > x , clip_max - > y ) ;
window - > DrawList - > AddText ( g . Font , g . FontSize , pos , window - > Color ( ImGuiCol_Text ) , text , text_display_end , 0.0f , & fine_clip_rect ) ;
}
else
{
window - > DrawList - > AddText ( g . Font , g . FontSize , pos , window - > Color ( ImGuiCol_Text ) , text , text_display_end , 0.0f , NULL ) ;
}
2015-05-12 21:02:45 +08:00
if ( g . LogEnabled )
2015-07-31 13:48:59 +08:00
LogRenderedText ( pos , text , text_display_end ) ;
2014-08-11 04:58:11 +08:00
}
2014-09-02 21:35:06 +08:00
// Render a rectangle shaped with optional rounding and borders
2015-08-02 11:57:24 +08:00
void ImGui : : RenderFrame ( ImVec2 p_min , ImVec2 p_max , ImU32 fill_col , bool border , float rounding )
2014-08-11 04:58:11 +08:00
{
2014-08-19 19:09:13 +08:00
ImGuiWindow * window = GetCurrentWindow ( ) ;
2014-08-11 04:58:11 +08:00
2014-08-19 19:09:13 +08:00
window - > DrawList - > AddRectFilled ( p_min , p_max , fill_col , rounding ) ;
if ( border & & ( window - > Flags & ImGuiWindowFlags_ShowBorders ) )
{
2015-07-06 06:52:06 +08:00
window - > DrawList - > AddRect ( p_min + ImVec2 ( 1 , 1 ) , p_max , window - > Color ( ImGuiCol_BorderShadow ) , rounding ) ;
window - > DrawList - > AddRect ( p_min , p_max - ImVec2 ( 1 , 1 ) , window - > Color ( ImGuiCol_Border ) , rounding ) ;
2014-08-19 19:09:13 +08:00
}
2014-08-11 04:58:11 +08:00
}
2014-09-02 21:35:06 +08:00
// Render a triangle to denote expanded/collapsed state
2015-08-02 11:57:24 +08:00
void ImGui : : RenderCollapseTriangle ( ImVec2 p_min , bool opened , float scale , bool shadow )
2014-08-11 04:58:11 +08:00
{
2015-03-30 02:45:03 +08:00
ImGuiState & g = * GImGui ;
2014-08-19 19:09:13 +08:00
ImGuiWindow * window = GetCurrentWindow ( ) ;
2014-08-11 04:58:11 +08:00
2015-03-30 02:45:03 +08:00
const float h = g . FontSize * 1.00f ;
2014-08-19 19:09:13 +08:00
const float r = h * 0.40f * scale ;
ImVec2 center = p_min + ImVec2 ( h * 0.50f , h * 0.50f * scale ) ;
2014-08-11 04:58:11 +08:00
2014-08-19 19:09:13 +08:00
ImVec2 a , b , c ;
2014-12-22 21:29:39 +08:00
if ( opened )
2014-08-19 19:09:13 +08:00
{
center . y - = r * 0.25f ;
a = center + ImVec2 ( 0 , 1 ) * r ;
b = center + ImVec2 ( - 0.866f , - 0.5f ) * r ;
c = center + ImVec2 ( 0.866f , - 0.5f ) * r ;
}
else
{
a = center + ImVec2 ( 1 , 0 ) * r ;
b = center + ImVec2 ( - 0.500f , 0.866f ) * r ;
c = center + ImVec2 ( - 0.500f , - 0.866f ) * r ;
}
2014-08-28 00:54:11 +08:00
if ( shadow & & ( window - > Flags & ImGuiWindowFlags_ShowBorders ) ! = 0 )
2014-08-19 19:09:13 +08:00
window - > DrawList - > AddTriangleFilled ( a + ImVec2 ( 2 , 2 ) , b + ImVec2 ( 2 , 2 ) , c + ImVec2 ( 2 , 2 ) , window - > Color ( ImGuiCol_BorderShadow ) ) ;
2015-03-07 08:35:47 +08:00
window - > DrawList - > AddTriangleFilled ( a , b , c , window - > Color ( ImGuiCol_Text ) ) ;
2014-08-11 04:58:11 +08:00
}
2015-08-02 11:57:24 +08:00
void ImGui : : RenderCheckMark ( ImVec2 pos , ImU32 col )
2015-05-08 00:06:07 +08:00
{
ImGuiState & g = * GImGui ;
ImGuiWindow * window = GetCurrentWindow ( ) ;
2015-05-08 00:11:32 +08:00
ImVec2 a , b , c ;
float start_x = ( float ) ( int ) ( g . FontSize * 0.307f + 0.5f ) ;
float rem_third = ( float ) ( int ) ( ( g . FontSize - start_x ) / 3.0f ) ;
2015-07-01 04:24:50 +08:00
a . x = pos . x + 0.5f + start_x ;
2015-05-08 00:11:32 +08:00
b . x = a . x + rem_third ;
c . x = a . x + rem_third * 3.0f ;
2015-07-16 02:31:37 +08:00
b . y = pos . y - 1.0f + ( float ) ( int ) ( g . Font - > Ascent * ( g . FontSize / g . Font - > FontSize ) + 0.5f ) + ( float ) ( int ) ( g . Font - > DisplayOffset . y ) ;
2015-05-08 00:11:32 +08:00
a . y = b . y - rem_third ;
c . y = b . y - rem_third * 2.0f ;
2015-07-01 04:24:50 +08:00
window - > DrawList - > PathLineTo ( a ) ;
window - > DrawList - > PathLineTo ( b ) ;
window - > DrawList - > PathLineTo ( c ) ;
2015-07-08 08:34:29 +08:00
window - > DrawList - > PathStroke ( col , false ) ;
2015-05-08 00:06:07 +08:00
}
2014-09-02 21:35:06 +08:00
// Calculate text size. Text can be multi-line. Optionally ignore text after a ## marker.
2015-02-01 12:28:24 +08:00
// CalcTextSize("") should return ImVec2(0.0f, GImGui->FontSize)
2014-11-30 20:21:50 +08:00
ImVec2 ImGui : : CalcTextSize ( const char * text , const char * text_end , bool hide_text_after_double_hash , float wrap_width )
2014-08-11 04:58:11 +08:00
{
2015-03-30 02:45:03 +08:00
ImGuiState & g = * GImGui ;
2014-08-11 04:58:11 +08:00
2014-08-19 19:09:13 +08:00
const char * text_display_end ;
2014-11-30 20:21:50 +08:00
if ( hide_text_after_double_hash )
2014-08-19 19:09:13 +08:00
text_display_end = FindTextDisplayEnd ( text , text_end ) ; // Hide anything after a '##' string
else
text_display_end = text_end ;
2014-08-11 04:58:11 +08:00
2015-03-30 02:45:03 +08:00
ImFont * font = g . Font ;
const float font_size = g . FontSize ;
2015-01-23 07:02:44 +08:00
ImVec2 text_size = font - > CalcTextSizeA ( font_size , FLT_MAX , wrap_width , text , text_display_end , NULL ) ;
// Cancel out character spacing for the last character of a line (it is baked into glyph->XAdvance field)
const float font_scale = font_size / font - > FontSize ;
const float character_spacing_x = 1.0f * font_scale ;
if ( text_size . x > 0.0f )
text_size . x - = character_spacing_x ;
2014-11-07 13:45:56 +08:00
return text_size ;
2014-08-11 04:58:11 +08:00
}
2015-02-09 00:39:34 +08:00
// Helper to calculate coarse clipping of large list of evenly sized items.
2015-05-31 19:33:24 +08:00
// NB: Prefer using the ImGuiListClipper higher-level helper if you can!
2015-02-08 21:44:09 +08:00
// If you are displaying thousands of items and you have a random access to the list, you can perform clipping yourself to save on CPU.
// {
// float item_height = ImGui::GetTextLineHeightWithSpacing();
// int display_start, display_end;
// ImGui::CalcListClipping(count, item_height, &display_start, &display_end); // calculate how many to clip/display
// ImGui::SetCursorPosY(ImGui::GetCursorPosY() + (display_start) * item_height); // advance cursor
// for (int i = display_start; i < display_end; i++) // display only visible items
// // TODO: display visible item
// ImGui::SetCursorPosY(ImGui::GetCursorPosY() + (count - display_end) * item_height); // advance cursor
// }
void ImGui : : CalcListClipping ( int items_count , float items_height , int * out_items_display_start , int * out_items_display_end )
{
ImGuiState & g = * GImGui ;
ImGuiWindow * window = GetCurrentWindow ( ) ;
if ( g . LogEnabled )
{
// If logging is active, do not perform any clipping
* out_items_display_start = 0 ;
* out_items_display_end = items_count ;
return ;
}
const ImVec2 pos = window - > DC . CursorPos ;
2015-07-07 11:46:12 +08:00
const float clip_y1 = window - > ClipRect . y ;
const float clip_y2 = window - > ClipRect . w ;
2015-02-08 21:44:09 +08:00
int start = ( int ) ( ( clip_y1 - pos . y ) / items_height ) ;
int end = ( int ) ( ( clip_y2 - pos . y ) / items_height ) ;
start = ImClamp ( start , 0 , items_count ) ;
end = ImClamp ( end + 1 , start , items_count ) ;
* out_items_display_start = start ;
* out_items_display_end = end ;
}
2014-09-02 21:35:06 +08:00
// Find window given position, search front-to-back
2014-08-11 04:58:11 +08:00
static ImGuiWindow * FindHoveredWindow ( ImVec2 pos , bool excluding_childs )
{
2015-02-01 12:28:24 +08:00
ImGuiState & g = * GImGui ;
2015-07-08 02:39:08 +08:00
for ( int i = g . Windows . Size - 1 ; i > = 0 ; i - - )
2014-08-19 19:09:13 +08:00
{
2015-07-08 02:09:25 +08:00
ImGuiWindow * window = g . Windows [ i ] ;
2015-05-02 20:20:50 +08:00
if ( ! window - > Active )
2014-08-19 19:09:13 +08:00
continue ;
2015-07-18 06:36:04 +08:00
if ( window - > Flags & ImGuiWindowFlags_NoInputs )
continue ;
2014-08-19 19:09:13 +08:00
if ( excluding_childs & & ( window - > Flags & ImGuiWindowFlags_ChildWindow ) ! = 0 )
continue ;
2015-02-22 20:05:38 +08:00
// Using the clipped AABB so a child window will typically be clipped by its parent.
2015-07-07 11:46:12 +08:00
ImRect bb ( window - > ClippedWindowRect . Min - g . Style . TouchExtraPadding , window - > ClippedWindowRect . Max + g . Style . TouchExtraPadding ) ;
2014-08-19 19:09:13 +08:00
if ( bb . Contains ( pos ) )
return window ;
}
return NULL ;
2014-08-11 04:58:11 +08:00
}
2015-03-18 07:35:12 +08:00
// Test if mouse cursor is hovering given rectangle
2015-03-18 07:41:20 +08:00
// NB- Rectangle is clipped by our current clip setting
// NB- Expand the rectangle to be generous on imprecise inputs systems (g.Style.TouchExtraPadding)
2015-07-31 13:48:59 +08:00
bool ImGui : : IsMouseHoveringRect ( const ImVec2 & rect_min , const ImVec2 & rect_max )
2014-08-11 04:58:11 +08:00
{
2015-02-01 12:28:24 +08:00
ImGuiState & g = * GImGui ;
2014-08-19 19:09:13 +08:00
ImGuiWindow * window = GetCurrentWindow ( ) ;
2014-08-11 04:58:11 +08:00
2014-08-19 19:09:13 +08:00
// Clip
2015-07-31 13:48:59 +08:00
ImRect rect_clipped ( rect_min , rect_max ) ;
2015-07-07 11:46:12 +08:00
rect_clipped . Clip ( window - > ClipRect ) ;
2014-08-11 04:58:11 +08:00
2014-08-19 19:09:13 +08:00
// Expand for touch input
2015-03-18 07:41:20 +08:00
const ImRect rect_for_touch ( rect_clipped . Min - g . Style . TouchExtraPadding , rect_clipped . Max + g . Style . TouchExtraPadding ) ;
return rect_for_touch . Contains ( g . IO . MousePos ) ;
2014-08-11 04:58:11 +08:00
}
2014-11-10 09:29:01 +08:00
bool ImGui : : IsMouseHoveringWindow ( )
2014-09-24 22:38:29 +08:00
{
2015-02-01 12:28:24 +08:00
ImGuiState & g = * GImGui ;
2014-09-24 22:38:29 +08:00
ImGuiWindow * window = GetCurrentWindow ( ) ;
2014-09-25 03:39:39 +08:00
return g . HoveredWindow = = window ;
2014-09-24 22:38:29 +08:00
}
2014-11-10 09:29:01 +08:00
bool ImGui : : IsMouseHoveringAnyWindow ( )
2014-09-24 22:38:29 +08:00
{
2015-02-01 12:28:24 +08:00
ImGuiState & g = * GImGui ;
2014-09-25 03:39:39 +08:00
return g . HoveredWindow ! = NULL ;
2014-09-24 22:38:29 +08:00
}
2014-11-10 09:29:01 +08:00
bool ImGui : : IsPosHoveringAnyWindow ( const ImVec2 & pos )
2014-09-24 22:49:15 +08:00
{
2014-11-10 09:29:01 +08:00
return FindHoveredWindow ( pos , false ) ! = NULL ;
2014-09-24 22:49:15 +08:00
}
2014-08-11 04:58:11 +08:00
static bool IsKeyPressedMap ( ImGuiKey key , bool repeat )
{
2015-07-03 11:32:29 +08:00
const int key_index = GImGui - > IO . KeyMap [ key ] ;
2014-11-10 09:29:01 +08:00
return ImGui : : IsKeyPressed ( key_index , repeat ) ;
2014-08-11 04:58:11 +08:00
}
2015-07-03 11:32:29 +08:00
int ImGui : : GetKeyIndex ( ImGuiKey key )
{
IM_ASSERT ( key > = 0 & & key < ImGuiKey_COUNT ) ;
return GImGui - > IO . KeyMap [ key ] ;
}
2015-04-29 00:12:24 +08:00
bool ImGui : : IsKeyDown ( int key_index )
{
2015-07-03 11:32:29 +08:00
if ( key_index < 0 ) return false ;
IM_ASSERT ( key_index > = 0 & & key_index < IM_ARRAYSIZE ( GImGui - > IO . KeysDown ) ) ;
return GImGui - > IO . KeysDown [ key_index ] ;
2015-04-29 00:12:24 +08:00
}
2014-11-10 09:29:01 +08:00
bool ImGui : : IsKeyPressed ( int key_index , bool repeat )
2014-08-11 04:58:11 +08:00
{
2015-02-01 12:28:24 +08:00
ImGuiState & g = * GImGui ;
2015-07-03 11:32:29 +08:00
if ( key_index < 0 ) return false ;
2014-08-19 19:09:13 +08:00
IM_ASSERT ( key_index > = 0 & & key_index < IM_ARRAYSIZE ( g . IO . KeysDown ) ) ;
2015-06-22 09:04:43 +08:00
const float t = g . IO . KeysDownDuration [ key_index ] ;
2014-08-19 19:09:13 +08:00
if ( t = = 0.0f )
return true ;
2014-08-11 04:58:11 +08:00
2015-05-27 22:17:44 +08:00
if ( repeat & & t > g . IO . KeyRepeatDelay )
{
float delay = g . IO . KeyRepeatDelay , rate = g . IO . KeyRepeatRate ;
if ( ( fmodf ( t - delay , rate ) > rate * 0.5f ) ! = ( fmodf ( t - delay - g . IO . DeltaTime , rate ) > rate * 0.5f ) )
2014-08-19 19:09:13 +08:00
return true ;
2015-05-27 22:17:44 +08:00
}
2015-06-22 09:04:43 +08:00
return false ;
}
bool ImGui : : IsKeyReleased ( int key_index )
{
ImGuiState & g = * GImGui ;
2015-07-03 11:32:29 +08:00
if ( key_index < 0 ) return false ;
2015-06-22 09:04:43 +08:00
IM_ASSERT ( key_index > = 0 & & key_index < IM_ARRAYSIZE ( g . IO . KeysDown ) ) ;
if ( g . IO . KeysDownDurationPrev [ key_index ] > = 0.0f & & ! g . IO . KeysDown [ key_index ] )
return true ;
2014-08-19 19:09:13 +08:00
return false ;
2014-08-11 04:58:11 +08:00
}
2015-04-29 00:12:24 +08:00
bool ImGui : : IsMouseDown ( int button )
{
ImGuiState & g = * GImGui ;
IM_ASSERT ( button > = 0 & & button < IM_ARRAYSIZE ( g . IO . MouseDown ) ) ;
return g . IO . MouseDown [ button ] ;
}
2014-11-10 09:29:01 +08:00
bool ImGui : : IsMouseClicked ( int button , bool repeat )
2014-08-11 04:58:11 +08:00
{
2015-02-01 12:28:24 +08:00
ImGuiState & g = * GImGui ;
2014-08-19 19:09:13 +08:00
IM_ASSERT ( button > = 0 & & button < IM_ARRAYSIZE ( g . IO . MouseDown ) ) ;
2015-06-22 08:15:31 +08:00
const float t = g . IO . MouseDownDuration [ button ] ;
2014-08-19 19:09:13 +08:00
if ( t = = 0.0f )
return true ;
2014-08-11 04:58:11 +08:00
2015-05-27 22:17:44 +08:00
if ( repeat & & t > g . IO . KeyRepeatDelay )
{
float delay = g . IO . KeyRepeatDelay , rate = g . IO . KeyRepeatRate ;
if ( ( fmodf ( t - delay , rate ) > rate * 0.5f ) ! = ( fmodf ( t - delay - g . IO . DeltaTime , rate ) > rate * 0.5f ) )
2014-08-19 19:09:13 +08:00
return true ;
2015-05-27 22:17:44 +08:00
}
2014-08-11 04:58:11 +08:00
2014-08-19 19:09:13 +08:00
return false ;
2014-08-11 04:58:11 +08:00
}
2015-06-22 08:15:31 +08:00
bool ImGui : : IsMouseReleased ( int button )
{
ImGuiState & g = * GImGui ;
IM_ASSERT ( button > = 0 & & button < IM_ARRAYSIZE ( g . IO . MouseDown ) ) ;
return g . IO . MouseReleased [ button ] ;
}
2014-11-10 09:29:01 +08:00
bool ImGui : : IsMouseDoubleClicked ( int button )
2014-08-13 02:57:46 +08:00
{
2015-02-01 12:28:24 +08:00
ImGuiState & g = * GImGui ;
2014-08-19 19:09:13 +08:00
IM_ASSERT ( button > = 0 & & button < IM_ARRAYSIZE ( g . IO . MouseDown ) ) ;
return g . IO . MouseDoubleClicked [ button ] ;
2014-08-13 02:57:46 +08:00
}
2015-03-20 19:13:18 +08:00
bool ImGui : : IsMouseDragging ( int button , float lock_threshold )
{
ImGuiState & g = * GImGui ;
IM_ASSERT ( button > = 0 & & button < IM_ARRAYSIZE ( g . IO . MouseDown ) ) ;
2015-07-05 06:46:30 +08:00
if ( ! g . IO . MouseDown [ button ] )
return false ;
2015-03-20 19:13:18 +08:00
if ( lock_threshold < 0.0f )
lock_threshold = g . IO . MouseDragThreshold ;
return g . IO . MouseDragMaxDistanceSqr [ button ] > = lock_threshold * lock_threshold ;
}
2014-11-10 09:29:01 +08:00
ImVec2 ImGui : : GetMousePos ( )
2014-08-11 04:58:11 +08:00
{
2015-02-01 12:28:24 +08:00
return GImGui - > IO . MousePos ;
2014-08-11 04:58:11 +08:00
}
2015-03-20 19:13:18 +08:00
ImVec2 ImGui : : GetMouseDragDelta ( int button , float lock_threshold )
2015-03-20 18:49:17 +08:00
{
ImGuiState & g = * GImGui ;
2015-03-20 19:13:18 +08:00
IM_ASSERT ( button > = 0 & & button < IM_ARRAYSIZE ( g . IO . MouseDown ) ) ;
if ( lock_threshold < 0.0f )
lock_threshold = g . IO . MouseDragThreshold ;
if ( g . IO . MouseDown [ button ] )
if ( g . IO . MouseDragMaxDistanceSqr [ button ] > = lock_threshold * lock_threshold )
return g . IO . MousePos - g . IO . MouseClickedPos [ button ] ; // Assume we can only get active with left-mouse button (at the moment).
2015-03-20 18:49:17 +08:00
return ImVec2 ( 0.0f , 0.0f ) ;
}
2015-04-14 02:54:40 +08:00
void ImGui : : ResetMouseDragDelta ( int button )
{
ImGuiState & g = * GImGui ;
IM_ASSERT ( button > = 0 & & button < IM_ARRAYSIZE ( g . IO . MouseDown ) ) ;
// NB: We don't need to reset g.IO.MouseDragMaxDistanceSqr
g . IO . MouseClickedPos [ button ] = g . IO . MousePos ;
}
2015-03-21 21:59:19 +08:00
ImGuiMouseCursor ImGui : : GetMouseCursor ( )
{
return GImGui - > MouseCursor ;
}
void ImGui : : SetMouseCursor ( ImGuiMouseCursor cursor_type )
{
GImGui - > MouseCursor = cursor_type ;
}
2015-07-02 23:20:15 +08:00
void ImGui : : CaptureKeyboardFromApp ( )
2015-07-02 23:10:31 +08:00
{
2015-07-02 23:20:15 +08:00
GImGui - > CaptureKeyboardNextFrame = true ;
}
void ImGui : : CaptureMouseFromApp ( )
{
GImGui - > CaptureMouseNextFrame = true ;
2015-07-02 23:10:31 +08:00
}
2014-11-10 09:29:01 +08:00
bool ImGui : : IsItemHovered ( )
2014-08-11 04:58:11 +08:00
{
2014-08-19 19:09:13 +08:00
ImGuiWindow * window = GetCurrentWindow ( ) ;
2015-03-18 20:10:26 +08:00
return window - > DC . LastItemHoveredAndUsable ;
}
2015-03-27 00:18:01 +08:00
bool ImGui : : IsItemHoveredRect ( )
2015-03-18 20:10:26 +08:00
{
ImGuiWindow * window = GetCurrentWindow ( ) ;
2015-03-27 00:18:01 +08:00
return window - > DC . LastItemHoveredRect ;
2014-08-11 04:58:11 +08:00
}
2015-01-23 06:54:05 +08:00
bool ImGui : : IsItemActive ( )
2014-11-07 13:45:56 +08:00
{
2015-02-01 12:28:24 +08:00
ImGuiState & g = * GImGui ;
2015-01-23 06:54:05 +08:00
if ( g . ActiveId )
{
ImGuiWindow * window = GetCurrentWindow ( ) ;
return g . ActiveId = = window - > DC . LastItemID ;
}
return false ;
2014-11-07 13:45:56 +08:00
}
2015-05-28 05:02:12 +08:00
bool ImGui : : IsAnyItemHovered ( )
{
return GImGui - > HoveredId ! = 0 | | GImGui - > HoveredIdPreviousFrame ! = 0 ;
}
2015-02-19 01:17:06 +08:00
bool ImGui : : IsAnyItemActive ( )
{
2015-05-28 05:02:12 +08:00
return GImGui - > ActiveId ! = 0 ;
2015-02-19 01:17:06 +08:00
}
2015-04-14 04:52:38 +08:00
bool ImGui : : IsItemVisible ( )
{
ImGuiWindow * window = GetCurrentWindow ( ) ;
2015-07-07 11:46:12 +08:00
ImRect r ( window - > ClipRect ) ;
2015-04-14 04:52:38 +08:00
return r . Overlaps ( window - > DC . LastItemRect ) ;
}
2015-03-18 06:36:54 +08:00
ImVec2 ImGui : : GetItemRectMin ( )
2014-08-14 19:43:30 +08:00
{
2014-08-19 19:09:13 +08:00
ImGuiWindow * window = GetCurrentWindow ( ) ;
2015-03-18 07:35:12 +08:00
return window - > DC . LastItemRect . Min ;
2014-08-14 19:43:30 +08:00
}
2015-03-18 06:36:54 +08:00
ImVec2 ImGui : : GetItemRectMax ( )
2014-08-14 19:43:30 +08:00
{
2014-08-19 19:09:13 +08:00
ImGuiWindow * window = GetCurrentWindow ( ) ;
2015-03-18 07:35:12 +08:00
return window - > DC . LastItemRect . Max ;
2014-08-14 19:43:30 +08:00
}
2015-03-18 06:36:54 +08:00
ImVec2 ImGui : : GetItemRectSize ( )
2014-08-14 19:43:30 +08:00
{
2014-08-19 19:09:13 +08:00
ImGuiWindow * window = GetCurrentWindow ( ) ;
2015-03-18 07:35:12 +08:00
return window - > DC . LastItemRect . GetSize ( ) ;
2014-08-14 19:43:30 +08:00
}
2015-03-18 21:07:37 +08:00
ImVec2 ImGui : : CalcItemRectClosestPoint ( const ImVec2 & pos , bool on_edge , float outward )
2014-08-14 19:43:30 +08:00
{
2014-08-19 19:09:13 +08:00
ImGuiWindow * window = GetCurrentWindow ( ) ;
2015-03-18 21:07:37 +08:00
ImRect rect = window - > DC . LastItemRect ;
rect . Expand ( outward ) ;
return rect . GetClosestPoint ( pos , on_edge ) ;
2014-08-14 19:43:30 +08:00
}
2014-09-25 02:27:29 +08:00
// Tooltip is stored and turned into a BeginTooltip()/EndTooltip() sequence at the end of the frame. Each call override previous value.
2014-11-10 09:29:01 +08:00
void ImGui : : SetTooltipV ( const char * fmt , va_list args )
2014-08-11 04:58:11 +08:00
{
2015-02-01 12:28:24 +08:00
ImGuiState & g = * GImGui ;
2014-09-25 02:27:29 +08:00
ImFormatStringV ( g . Tooltip , IM_ARRAYSIZE ( g . Tooltip ) , fmt , args ) ;
}
2014-11-10 09:29:01 +08:00
void ImGui : : SetTooltip ( const char * fmt , . . . )
2014-09-25 02:27:29 +08:00
{
2014-08-19 19:09:13 +08:00
va_list args ;
va_start ( args , fmt ) ;
2014-09-25 02:27:29 +08:00
SetTooltipV ( fmt , args ) ;
2014-08-19 19:09:13 +08:00
va_end ( args ) ;
2014-08-11 04:58:11 +08:00
}
2014-11-10 09:29:01 +08:00
float ImGui : : GetTime ( )
2014-08-11 04:58:11 +08:00
{
2015-02-01 12:28:24 +08:00
return GImGui - > Time ;
2014-08-11 04:58:11 +08:00
}
2014-11-10 09:29:01 +08:00
int ImGui : : GetFrameCount ( )
2014-08-11 04:58:11 +08:00
{
2015-02-01 12:28:24 +08:00
return GImGui - > FrameCount ;
2014-08-11 04:58:11 +08:00
}
2015-04-26 19:17:56 +08:00
static ImVec4 GetVisibleRect ( )
{
ImGuiState & g = * GImGui ;
if ( g . IO . DisplayVisibleMin . x ! = g . IO . DisplayVisibleMax . x & & g . IO . DisplayVisibleMin . y ! = g . IO . DisplayVisibleMax . y )
return ImVec4 ( g . IO . DisplayVisibleMin . x , g . IO . DisplayVisibleMin . y , g . IO . DisplayVisibleMax . x , g . IO . DisplayVisibleMax . y ) ;
return ImVec4 ( 0.0f , 0.0f , g . IO . DisplaySize . x , g . IO . DisplaySize . y ) ;
}
2014-11-10 09:29:01 +08:00
void ImGui : : BeginTooltip ( )
2014-08-15 23:18:00 +08:00
{
2015-02-01 12:28:24 +08:00
ImGuiState & g = * GImGui ;
2015-03-27 03:18:06 +08:00
ImGuiWindowFlags flags = ImGuiWindowFlags_Tooltip | ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoSavedSettings | ImGuiWindowFlags_AlwaysAutoResize ;
ImGui : : Begin ( " ##Tooltip " , NULL , ImVec2 ( 0 , 0 ) , g . Style . Colors [ ImGuiCol_TooltipBg ] . w , flags ) ;
2014-08-15 23:18:00 +08:00
}
2014-11-10 09:29:01 +08:00
void ImGui : : EndTooltip ( )
2014-08-15 23:18:00 +08:00
{
2014-08-19 19:09:13 +08:00
IM_ASSERT ( GetCurrentWindow ( ) - > Flags & ImGuiWindowFlags_Tooltip ) ;
ImGui : : End ( ) ;
2014-08-15 23:18:00 +08:00
}
2015-05-19 02:12:36 +08:00
static bool IsPopupOpen ( ImGuiID id )
{
ImGuiState & g = * GImGui ;
2015-07-08 02:39:08 +08:00
const bool opened = g . OpenedPopupStack . Size > g . CurrentPopupStack . Size & & g . OpenedPopupStack [ g . CurrentPopupStack . Size ] . PopupID = = id ;
2015-05-19 02:12:36 +08:00
return opened ;
}
// One open popup per level of the popup hierarchy (NB: when assigning we reset the Window member of ImGuiPopupRef to NULL)
2015-05-11 22:57:02 +08:00
void ImGui : : OpenPopup ( const char * str_id )
2015-03-27 01:09:17 +08:00
{
2015-05-08 01:43:08 +08:00
ImGuiState & g = * GImGui ;
2015-05-03 20:04:43 +08:00
ImGuiWindow * window = GetCurrentWindow ( ) ;
2015-07-08 02:09:25 +08:00
ImGuiID id = window - > GetID ( str_id ) ;
2015-07-08 02:39:08 +08:00
int current_stack_size = g . CurrentPopupStack . Size ;
2015-05-29 06:37:11 +08:00
ImGuiPopupRef popup_ref = ImGuiPopupRef ( id , window , window - > GetID ( " ##menus " ) ) ; // Tagged as new ref because constructor sets Window to NULL (we are passing the ParentWindow info here)
2015-07-08 02:39:08 +08:00
if ( g . OpenedPopupStack . Size < current_stack_size + 1 )
2015-05-29 06:37:11 +08:00
g . OpenedPopupStack . push_back ( popup_ref ) ;
else if ( g . OpenedPopupStack [ current_stack_size ] . PopupID ! = id )
2015-05-29 06:47:01 +08:00
{
g . OpenedPopupStack . resize ( current_stack_size + 1 ) ;
2015-05-29 06:37:11 +08:00
g . OpenedPopupStack [ current_stack_size ] = popup_ref ;
2015-05-29 06:47:01 +08:00
}
2015-05-11 22:57:02 +08:00
}
2015-05-29 06:04:29 +08:00
static void CloseInactivePopups ( )
{
ImGuiState & g = * GImGui ;
if ( g . OpenedPopupStack . empty ( ) )
return ;
2015-07-14 23:21:41 +08:00
// When popups are stacked, clicking on a lower level popups puts focus back to it and close popups above it.
2015-05-29 06:04:29 +08:00
// Don't close our own child popup windows
int n = 0 ;
if ( g . FocusedWindow )
{
2015-07-08 02:39:08 +08:00
for ( n = 0 ; n < g . OpenedPopupStack . Size ; n + + )
2015-05-29 06:04:29 +08:00
{
ImGuiPopupRef & popup = g . OpenedPopupStack [ n ] ;
if ( ! popup . Window )
continue ;
IM_ASSERT ( ( popup . Window - > Flags & ImGuiWindowFlags_Popup ) ! = 0 ) ;
if ( popup . Window - > Flags & ImGuiWindowFlags_ChildWindow )
2015-07-14 23:21:41 +08:00
continue ;
bool has_focus = false ;
for ( int m = n ; m < g . OpenedPopupStack . Size & & ! has_focus ; m + + )
has_focus = ( g . OpenedPopupStack [ m ] . Window & & g . OpenedPopupStack [ m ] . Window - > RootWindow = = g . FocusedWindow - > RootWindow ) ;
if ( ! has_focus )
break ;
2015-05-29 06:04:29 +08:00
}
}
2015-07-14 23:21:41 +08:00
if ( n < g . OpenedPopupStack . Size ) // This test is not required but it allows to set a useful breakpoint on the line below
g . OpenedPopupStack . resize ( n ) ;
2015-05-29 06:04:29 +08:00
}
2015-06-24 00:02:56 +08:00
static ImGuiWindow * GetFrontMostModalRootWindow ( )
{
ImGuiState & g = * GImGui ;
if ( ! g . OpenedPopupStack . empty ( ) )
if ( ImGuiWindow * front_most_popup = g . OpenedPopupStack . back ( ) . Window )
if ( front_most_popup - > Flags & ImGuiWindowFlags_Modal )
return front_most_popup ;
return NULL ;
}
2015-05-27 03:33:48 +08:00
static void ClosePopupToLevel ( int remaining )
{
ImGuiState & g = * GImGui ;
if ( remaining > 0 )
2015-08-02 11:57:24 +08:00
ImGui : : FocusWindow ( g . OpenedPopupStack [ remaining - 1 ] . Window ) ;
2015-05-27 03:33:48 +08:00
else
2015-08-02 11:57:24 +08:00
ImGui : : FocusWindow ( g . OpenedPopupStack [ 0 ] . ParentWindow ) ;
2015-05-27 03:33:48 +08:00
g . OpenedPopupStack . resize ( remaining ) ;
}
2015-06-10 02:35:55 +08:00
static void ClosePopup ( ImGuiID id )
{
if ( ! IsPopupOpen ( id ) )
return ;
ImGuiState & g = * GImGui ;
2015-07-08 02:39:08 +08:00
ClosePopupToLevel ( g . OpenedPopupStack . Size - 1 ) ;
2015-06-10 02:35:55 +08:00
}
2015-05-27 03:33:48 +08:00
// Close the popup we have begin-ed into.
2015-05-11 22:57:02 +08:00
void ImGui : : CloseCurrentPopup ( )
{
ImGuiState & g = * GImGui ;
2015-07-08 02:39:08 +08:00
int popup_idx = g . CurrentPopupStack . Size - 1 ;
if ( popup_idx < 0 | | popup_idx > g . OpenedPopupStack . Size | | g . CurrentPopupStack [ popup_idx ] . PopupID ! = g . OpenedPopupStack [ popup_idx ] . PopupID )
2015-05-11 22:57:02 +08:00
return ;
2015-05-27 03:33:48 +08:00
while ( popup_idx > 0 & & g . OpenedPopupStack [ popup_idx ] . Window & & ( g . OpenedPopupStack [ popup_idx ] . Window - > Flags & ImGuiWindowFlags_ChildMenu ) )
popup_idx - - ;
ClosePopupToLevel ( popup_idx ) ;
2015-05-12 02:52:02 +08:00
}
2015-05-25 04:20:10 +08:00
static void ClearSetNextWindowData ( )
{
ImGuiState & g = * GImGui ;
g . SetNextWindowPosCond = g . SetNextWindowSizeCond = g . SetNextWindowCollapsedCond = g . SetNextWindowFocus = 0 ;
}
2015-05-12 02:52:02 +08:00
static bool BeginPopupEx ( const char * str_id , ImGuiWindowFlags extra_flags )
2015-05-11 22:57:02 +08:00
{
ImGuiState & g = * GImGui ;
2015-05-28 05:26:03 +08:00
ImGuiWindow * window = g . CurrentWindow ;
2015-05-11 22:57:02 +08:00
const ImGuiID id = window - > GetID ( str_id ) ;
if ( ! IsPopupOpen ( id ) )
2015-05-25 04:20:10 +08:00
{
ClearSetNextWindowData ( ) ; // We behave like Begin() and need to consume those values
2015-05-11 22:57:02 +08:00
return false ;
2015-05-25 04:20:10 +08:00
}
2015-03-27 03:18:06 +08:00
2015-03-27 01:09:17 +08:00
ImGui : : PushStyleVar ( ImGuiStyleVar_WindowRounding , 0.0f ) ;
2015-06-10 02:35:55 +08:00
ImGuiWindowFlags flags = extra_flags | ImGuiWindowFlags_Popup | ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoSavedSettings | ImGuiWindowFlags_AlwaysAutoResize ;
2015-05-08 01:43:08 +08:00
2015-05-14 22:15:17 +08:00
char name [ 32 ] ;
2015-05-27 05:14:07 +08:00
if ( flags & ImGuiWindowFlags_ChildMenu )
2015-07-08 02:39:08 +08:00
ImFormatString ( name , 20 , " ##menu_%d " , g . CurrentPopupStack . Size ) ; // Recycle windows based on depth
2015-05-27 05:14:07 +08:00
else
ImFormatString ( name , 20 , " ##popup_%08x " , id ) ; // Not recycling, so we can close/open during the same frame
2015-05-11 22:57:02 +08:00
float alpha = 1.0f ;
2015-05-03 20:04:43 +08:00
2015-05-19 02:12:36 +08:00
bool opened = ImGui : : Begin ( name , NULL , ImVec2 ( 0.0f , 0.0f ) , alpha , flags ) ;
2015-05-03 20:04:43 +08:00
if ( ! ( window - > Flags & ImGuiWindowFlags_ShowBorders ) )
2015-07-31 13:48:59 +08:00
g . CurrentWindow - > Flags & = ~ ImGuiWindowFlags_ShowBorders ;
2015-05-18 05:15:40 +08:00
if ( ! opened ) // opened can be 'false' when the popup is completely clipped (e.g. zero size display)
ImGui : : EndPopup ( ) ;
2015-05-11 22:57:02 +08:00
return opened ;
2015-03-27 01:09:17 +08:00
}
2015-05-12 02:52:02 +08:00
bool ImGui : : BeginPopup ( const char * str_id )
{
2015-06-10 02:35:55 +08:00
return BeginPopupEx ( str_id , ImGuiWindowFlags_ShowBorders ) ;
2015-03-27 01:09:17 +08:00
}
2015-06-24 01:08:19 +08:00
bool ImGui : : BeginPopupModal ( const char * name , bool * p_opened , ImGuiWindowFlags extra_flags )
2015-06-24 00:02:56 +08:00
{
ImGuiState & g = * GImGui ;
ImGuiWindow * window = g . CurrentWindow ;
const ImGuiID id = window - > GetID ( name ) ;
if ( ! IsPopupOpen ( id ) )
{
ClearSetNextWindowData ( ) ; // We behave like Begin() and need to consume those values
return false ;
}
2015-06-24 05:17:27 +08:00
ImGuiWindowFlags flags = extra_flags | ImGuiWindowFlags_Popup | ImGuiWindowFlags_Modal | ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoSavedSettings ;
2015-06-24 01:08:19 +08:00
bool opened = ImGui : : Begin ( name , p_opened , ImVec2 ( 0.0f , 0.0f ) , - 1.0f , flags ) ;
if ( ! opened | | ( p_opened & & ! * p_opened ) ) // Opened can be 'false' when the popup is completely clipped (e.g. zero size display)
{
2015-06-24 00:02:56 +08:00
ImGui : : EndPopup ( ) ;
2015-06-24 01:08:19 +08:00
if ( opened )
ClosePopup ( id ) ;
return false ;
}
2015-06-24 00:02:56 +08:00
return opened ;
}
2015-03-27 01:09:17 +08:00
void ImGui : : EndPopup ( )
{
2015-06-24 00:02:56 +08:00
ImGuiWindow * window = GetCurrentWindow ( ) ;
IM_ASSERT ( window - > Flags & ImGuiWindowFlags_Popup ) ;
2015-07-08 02:39:08 +08:00
IM_ASSERT ( GImGui - > CurrentPopupStack . Size > 0 ) ;
2015-03-27 01:09:17 +08:00
ImGui : : End ( ) ;
2015-06-24 00:02:56 +08:00
if ( ! ( window - > Flags & ImGuiWindowFlags_Modal ) )
ImGui : : PopStyleVar ( ) ;
2015-03-27 01:09:17 +08:00
}
2015-05-31 19:55:12 +08:00
bool ImGui : : BeginPopupContextItem ( const char * str_id , int mouse_button )
2015-05-28 05:36:23 +08:00
{
2015-05-31 19:55:12 +08:00
if ( ImGui : : IsItemHovered ( ) & & ImGui : : IsMouseClicked ( mouse_button ) )
2015-05-28 05:36:23 +08:00
ImGui : : OpenPopup ( str_id ) ;
return ImGui : : BeginPopup ( str_id ) ;
}
2015-05-31 19:55:12 +08:00
bool ImGui : : BeginPopupContextWindow ( bool also_over_items , const char * str_id , int mouse_button )
2015-05-28 05:36:23 +08:00
{
2015-05-28 06:28:53 +08:00
if ( ! str_id ) str_id = " window_context_menu " ;
2015-05-31 19:55:12 +08:00
if ( ImGui : : IsMouseHoveringWindow ( ) & & ImGui : : IsMouseClicked ( mouse_button ) )
if ( also_over_items | | ! ImGui : : IsAnyItemHovered ( ) )
2015-05-28 05:36:23 +08:00
ImGui : : OpenPopup ( str_id ) ;
return ImGui : : BeginPopup ( str_id ) ;
}
2015-05-31 19:55:12 +08:00
bool ImGui : : BeginPopupContextVoid ( const char * str_id , int mouse_button )
2015-05-28 06:28:53 +08:00
{
if ( ! str_id ) str_id = " void_context_menu " ;
2015-05-31 19:55:12 +08:00
if ( ! ImGui : : IsMouseHoveringAnyWindow ( ) & & ImGui : : IsMouseClicked ( mouse_button ) )
2015-05-28 06:28:53 +08:00
ImGui : : OpenPopup ( str_id ) ;
return ImGui : : BeginPopup ( str_id ) ;
}
2015-02-22 20:38:46 +08:00
bool ImGui : : BeginChild ( const char * str_id , const ImVec2 & size_arg , bool border , ImGuiWindowFlags extra_flags )
2014-08-11 04:58:11 +08:00
{
2014-08-19 19:09:13 +08:00
ImGuiWindow * window = GetCurrentWindow ( ) ;
2014-12-11 01:13:45 +08:00
ImGuiWindowFlags flags = ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoSavedSettings | ImGuiWindowFlags_ChildWindow ;
2014-08-11 04:58:11 +08:00
2015-08-06 00:43:12 +08:00
const ImVec2 content_avail = ImGui : : GetContentRegionAvail ( ) ;
2015-02-22 20:38:46 +08:00
ImVec2 size = size_arg ;
2014-08-19 19:09:13 +08:00
if ( size . x < = 0.0f )
{
2014-11-07 16:22:38 +08:00
if ( size . x = = 0.0f )
flags | = ImGuiWindowFlags_ChildWindowAutoFitX ;
2015-08-06 00:43:12 +08:00
size . x = ImMax ( content_avail . x , 4.0f ) - fabsf ( size . x ) ; // Arbitrary minimum zero-ish child size of 4.0f (0.0f causing too much issues)
2014-08-19 19:09:13 +08:00
}
if ( size . y < = 0.0f )
{
2014-11-07 16:22:38 +08:00
if ( size . y = = 0.0f )
flags | = ImGuiWindowFlags_ChildWindowAutoFitY ;
2015-08-06 00:43:12 +08:00
size . y = ImMax ( content_avail . y , 4.0f ) - fabsf ( size . y ) ;
2014-08-19 19:09:13 +08:00
}
if ( border )
flags | = ImGuiWindowFlags_ShowBorders ;
flags | = extra_flags ;
2014-08-11 04:58:11 +08:00
2014-08-19 19:09:13 +08:00
char title [ 256 ] ;
ImFormatString ( title , IM_ARRAYSIZE ( title ) , " %s.%s " , window - > Name , str_id ) ;
2014-08-11 04:58:11 +08:00
2015-02-10 21:00:22 +08:00
const float alpha = 1.0f ;
2015-02-21 18:30:05 +08:00
bool ret = ImGui : : Begin ( title , NULL , size , alpha , flags ) ;
2014-08-11 04:58:11 +08:00
2014-08-19 19:09:13 +08:00
if ( ! ( window - > Flags & ImGuiWindowFlags_ShowBorders ) )
2015-05-03 20:04:43 +08:00
GetCurrentWindow ( ) - > Flags & = ~ ImGuiWindowFlags_ShowBorders ;
2015-02-21 18:30:05 +08:00
return ret ;
2014-08-11 04:58:11 +08:00
}
2015-02-22 20:38:46 +08:00
bool ImGui : : BeginChild ( ImGuiID id , const ImVec2 & size , bool border , ImGuiWindowFlags extra_flags )
2015-02-21 18:10:21 +08:00
{
char str_id [ 32 ] ;
2015-05-14 22:15:17 +08:00
ImFormatString ( str_id , IM_ARRAYSIZE ( str_id ) , " child_%08x " , id ) ;
2015-02-21 18:30:05 +08:00
bool ret = ImGui : : BeginChild ( str_id , size , border , extra_flags ) ;
return ret ;
2015-02-21 18:10:21 +08:00
}
2014-11-10 09:29:01 +08:00
void ImGui : : EndChild ( )
2014-08-11 04:58:11 +08:00
{
2014-08-19 19:09:13 +08:00
ImGuiWindow * window = GetCurrentWindow ( ) ;
2014-12-11 01:13:45 +08:00
IM_ASSERT ( window - > Flags & ImGuiWindowFlags_ChildWindow ) ;
2015-05-22 05:21:00 +08:00
if ( ( window - > Flags & ImGuiWindowFlags_ComboBox ) | | window - > BeginCount > 1 )
2014-08-19 19:09:13 +08:00
{
ImGui : : End ( ) ;
}
else
{
2015-03-21 07:34:03 +08:00
// When using auto-filling child window, we don't provide full width/height to ItemSize so that it doesn't feed back into automatic size-fitting.
ImGuiState & g = * GImGui ;
2014-08-19 19:09:13 +08:00
ImVec2 sz = ImGui : : GetWindowSize ( ) ;
2015-05-29 17:24:03 +08:00
if ( window - > Flags & ImGuiWindowFlags_ChildWindowAutoFitX ) // Arbitrary minimum zeroish child size of 4.0f
sz . x = ImMax ( 4.0f , sz . x - g . Style . WindowPadding . x ) ;
2014-08-19 19:09:13 +08:00
if ( window - > Flags & ImGuiWindowFlags_ChildWindowAutoFitY )
2015-05-29 17:24:03 +08:00
sz . y = ImMax ( 4.0f , sz . y - g . Style . WindowPadding . y ) ;
2014-08-19 19:09:13 +08:00
ImGui : : End ( ) ;
2015-03-07 06:05:01 +08:00
window = GetCurrentWindow ( ) ;
2015-03-18 07:35:12 +08:00
ImRect bb ( window - > DC . CursorPos , window - > DC . CursorPos + sz ) ;
2014-11-10 09:29:01 +08:00
ItemSize ( sz ) ;
2015-03-07 06:40:21 +08:00
ItemAdd ( bb , NULL ) ;
2014-08-19 19:09:13 +08:00
}
2014-08-11 04:58:11 +08:00
}
2015-02-11 05:21:23 +08:00
// Helper to create a child window / scrolling region that looks like a normal widget frame.
2015-06-18 22:49:57 +08:00
bool ImGui : : BeginChildFrame ( ImGuiID id , const ImVec2 & size )
2015-02-11 05:21:23 +08:00
{
ImGuiState & g = * GImGui ;
const ImGuiStyle & style = g . Style ;
ImGui : : PushStyleColor ( ImGuiCol_ChildWindowBg , style . Colors [ ImGuiCol_FrameBg ] ) ;
ImGui : : PushStyleVar ( ImGuiStyleVar_ChildWindowRounding , style . FrameRounding ) ;
2015-06-18 22:49:57 +08:00
return ImGui : : BeginChild ( id , size ) ;
2015-02-11 05:21:23 +08:00
}
void ImGui : : EndChildFrame ( )
{
ImGui : : EndChild ( ) ;
ImGui : : PopStyleVar ( ) ;
ImGui : : PopStyleColor ( ) ;
}
2015-05-09 16:59:46 +08:00
// Save and compare stack sizes on Begin()/End() to detect usage errors
static void CheckStacksSize ( ImGuiWindow * window , bool write )
{
2015-05-27 22:35:49 +08:00
// NOT checking: DC.ItemWidth, DC.AllowKeyboardFocus, DC.ButtonRepeat, DC.TextWrapPos (per window) to allow user to conveniently push once and not pop (they are cleared on Begin)
2015-05-09 16:59:46 +08:00
ImGuiState & g = * GImGui ;
int * p_backup = & window - > DC . StackSizesBackup [ 0 ] ;
2015-07-08 02:39:08 +08:00
{ int current = window - > IDStack . Size ; if ( write ) * p_backup = current ; else IM_ASSERT ( * p_backup = = current ) ; p_backup + + ; } // User forgot PopID()
{ int current = window - > DC . GroupStack . Size ; if ( write ) * p_backup = current ; else IM_ASSERT ( * p_backup = = current ) ; p_backup + + ; } // User forgot EndGroup()
{ int current = g . CurrentPopupStack . Size ; if ( write ) * p_backup = current ; else IM_ASSERT ( * p_backup = = current ) ; p_backup + + ; } // User forgot EndPopup()/EndMenu()
{ int current = g . ColorModifiers . Size ; if ( write ) * p_backup = current ; else IM_ASSERT ( * p_backup = = current ) ; p_backup + + ; } // User forgot PopStyleColor()
{ int current = g . StyleModifiers . Size ; if ( write ) * p_backup = current ; else IM_ASSERT ( * p_backup = = current ) ; p_backup + + ; } // User forgot PopStyleVar()
{ int current = g . FontStack . Size ; if ( write ) * p_backup = current ; else IM_ASSERT ( * p_backup = = current ) ; p_backup + + ; } // User forgot PopFont()
2015-05-09 16:59:46 +08:00
IM_ASSERT ( p_backup = = window - > DC . StackSizesBackup + IM_ARRAYSIZE ( window - > DC . StackSizesBackup ) ) ;
}
2015-06-30 05:18:26 +08:00
static ImVec2 FindBestPopupWindowPos ( const ImVec2 & base_pos , const ImVec2 & size , ImGuiWindowFlags flags , int * last_dir , const ImRect & r_inner )
2015-05-02 03:35:44 +08:00
{
const ImGuiStyle & style = GImGui - > Style ;
// Clamp into visible area while not overlapping the cursor
ImRect r_outer ( GetVisibleRect ( ) ) ;
r_outer . Reduce ( style . DisplaySafeAreaPadding ) ;
2015-05-25 06:17:06 +08:00
ImVec2 base_pos_clamped = ImClamp ( base_pos , r_outer . Min , r_outer . Max - size ) ;
2015-05-02 03:35:44 +08:00
2015-05-03 01:22:46 +08:00
for ( int n = ( * last_dir ! = - 1 ) ? - 1 : 0 ; n < 4 ; n + + ) // Right, down, up, left. Favor last used direction.
2015-05-02 03:35:44 +08:00
{
2015-05-03 01:22:46 +08:00
const int dir = ( n = = - 1 ) ? * last_dir : n ;
2015-05-02 03:35:44 +08:00
ImRect rect ( dir = = 0 ? r_inner . Max . x : r_outer . Min . x , dir = = 1 ? r_inner . Max . y : r_outer . Min . y , dir = = 3 ? r_inner . Min . x : r_outer . Max . x , dir = = 2 ? r_inner . Min . y : r_outer . Max . y ) ;
if ( rect . GetWidth ( ) < size . x | | rect . GetHeight ( ) < size . y )
continue ;
2015-05-03 01:22:46 +08:00
* last_dir = dir ;
2015-05-25 06:17:06 +08:00
return ImVec2 ( dir = = 0 ? r_inner . Max . x : dir = = 3 ? r_inner . Min . x - size . x : base_pos_clamped . x , dir = = 1 ? r_inner . Max . y : dir = = 2 ? r_inner . Min . y - size . y : base_pos_clamped . y ) ;
2015-05-02 03:35:44 +08:00
}
// Fallback
2015-05-08 00:06:07 +08:00
* last_dir = - 1 ;
2015-05-25 06:17:06 +08:00
if ( flags & ImGuiWindowFlags_Tooltip ) // For tooltip we prefer avoiding the cursor at all cost even if it means that part of the tooltip won't be visible.
return base_pos + ImVec2 ( 2 , 2 ) ;
// Otherwise try to keep within display
ImVec2 pos = base_pos ;
pos . x = ImMax ( ImMin ( pos . x + size . x , r_outer . Max . x ) - size . x , r_outer . Min . x ) ;
pos . y = ImMax ( ImMin ( pos . y + size . y , r_outer . Max . y ) - size . y , r_outer . Min . y ) ;
return pos ;
2015-05-02 03:35:44 +08:00
}
2014-12-29 01:54:02 +08:00
static ImGuiWindow * FindWindowByName ( const char * name )
{
2015-03-02 18:04:35 +08:00
// FIXME-OPT: Store sorted hashes -> pointers.
2015-02-01 12:28:24 +08:00
ImGuiState & g = * GImGui ;
2015-03-08 07:53:03 +08:00
ImGuiID id = ImHash ( name , 0 ) ;
2015-07-08 02:39:08 +08:00
for ( int i = 0 ; i < g . Windows . Size ; i + + )
2015-03-02 18:04:35 +08:00
if ( g . Windows [ i ] - > ID = = id )
2014-12-29 01:54:02 +08:00
return g . Windows [ i ] ;
return NULL ;
}
static ImGuiWindow * CreateNewWindow ( const char * name , ImVec2 size , ImGuiWindowFlags flags )
{
2015-02-01 12:28:24 +08:00
ImGuiState & g = * GImGui ;
2014-12-29 01:54:02 +08:00
// Create window the first time
ImGuiWindow * window = ( ImGuiWindow * ) ImGui : : MemAlloc ( sizeof ( ImGuiWindow ) ) ;
new ( window ) ImGuiWindow ( name ) ;
window - > Flags = flags ;
if ( flags & ImGuiWindowFlags_NoSavedSettings )
{
2014-12-29 19:23:03 +08:00
// User can disable loading and saving of settings. Tooltip and child windows also don't store settings.
2014-12-29 01:54:02 +08:00
window - > Size = window - > SizeFull = size ;
}
else
{
// Retrieve settings from .ini file
2014-12-29 19:23:03 +08:00
// Use SetWindowPos() or SetNextWindowPos() with the appropriate condition flag to change the initial position of a window.
2014-12-29 01:54:02 +08:00
window - > PosFloat = ImVec2 ( 60 , 60 ) ;
window - > Pos = ImVec2 ( ( float ) ( int ) window - > PosFloat . x , ( float ) ( int ) window - > PosFloat . y ) ;
ImGuiIniData * settings = FindWindowSettings ( name ) ;
if ( ! settings )
{
settings = AddWindowSettings ( name ) ;
}
else
{
2015-02-27 17:51:11 +08:00
window - > SetWindowPosAllowFlags & = ~ ImGuiSetCond_FirstUseEver ;
window - > SetWindowSizeAllowFlags & = ~ ImGuiSetCond_FirstUseEver ;
window - > SetWindowCollapsedAllowFlags & = ~ ImGuiSetCond_FirstUseEver ;
2014-12-29 01:54:02 +08:00
}
if ( settings - > Pos . x ! = FLT_MAX )
{
window - > PosFloat = settings - > Pos ;
window - > Pos = ImVec2 ( ( float ) ( int ) window - > PosFloat . x , ( float ) ( int ) window - > PosFloat . y ) ;
window - > Collapsed = settings - > Collapsed ;
}
2014-12-29 19:23:03 +08:00
if ( ImLengthSqr ( settings - > Size ) > 0.00001f & & ! ( flags & ImGuiWindowFlags_NoResize ) )
2014-12-29 01:54:02 +08:00
size = settings - > Size ;
window - > Size = window - > SizeFull = size ;
}
2015-03-23 01:22:39 +08:00
2015-03-27 23:18:45 +08:00
if ( ( flags & ImGuiWindowFlags_AlwaysAutoResize ) ! = 0 )
{
2015-06-25 10:08:08 +08:00
window - > AutoFitFramesX = window - > AutoFitFramesY = 2 ;
2015-03-27 23:18:45 +08:00
window - > AutoFitOnlyGrows = false ;
}
2015-06-25 10:08:08 +08:00
else
2015-03-23 01:22:39 +08:00
{
2015-06-25 10:08:08 +08:00
if ( window - > Size . x < = 0.0f )
window - > AutoFitFramesX = 2 ;
if ( window - > Size . y < = 0.0f )
window - > AutoFitFramesY = 2 ;
window - > AutoFitOnlyGrows = ( window - > AutoFitFramesX > 0 ) | | ( window - > AutoFitFramesY > 0 ) ;
2015-03-23 01:22:39 +08:00
}
2014-12-29 01:54:02 +08:00
g . Windows . push_back ( window ) ;
return window ;
}
2014-12-11 03:22:30 +08:00
// Push a new ImGui window to add widgets to.
2015-02-22 20:38:46 +08:00
// - A default window called "Debug" is automatically stacked at the beginning of every frame so you can use widgets without explicitly calling a Begin/End pair.
// - Begin/End can be called multiple times during the frame with the same window name to append content.
2015-05-29 06:04:29 +08:00
// - 'size_on_first_use' for a regular window denote the initial size for first-time creation (no saved data) and isn't that useful. Use SetNextWindowSize() prior to calling Begin() for more flexible window manipulation.
2015-03-08 06:01:06 +08:00
// - The window name is used as a unique identifier to preserve window information across frames (and save rudimentary information to the .ini file).
// You can use the "##" or "###" markers to use the same label with different id, or same id with different label. See documentation at the top of this file.
2014-12-22 21:29:39 +08:00
// - Return false when window is collapsed, so you can early out in your code. You always need to call ImGui::End() even if false is returned.
// - Passing 'bool* p_opened' displays a Close button on the upper-right corner of the window, the pointed value will be set to false when the button is pressed.
2015-02-27 17:51:11 +08:00
// - Passing non-zero 'size' is roughly equivalent to calling SetNextWindowSize(size, ImGuiSetCond_FirstUseEver) prior to calling Begin().
2015-03-24 06:50:17 +08:00
bool ImGui : : Begin ( const char * name , bool * p_opened , ImGuiWindowFlags flags )
{
return ImGui : : Begin ( name , p_opened , ImVec2 ( 0.f , 0.f ) , - 1.0f , flags ) ;
}
bool ImGui : : Begin ( const char * name , bool * p_opened , const ImVec2 & size_on_first_use , float bg_alpha , ImGuiWindowFlags flags )
2014-08-11 04:58:11 +08:00
{
2015-02-01 12:28:24 +08:00
ImGuiState & g = * GImGui ;
2014-08-19 19:09:13 +08:00
const ImGuiStyle & style = g . Style ;
2014-11-14 10:17:59 +08:00
IM_ASSERT ( g . Initialized ) ; // Forgot to call ImGui::NewFrame()
2014-12-22 21:29:39 +08:00
IM_ASSERT ( name ! = NULL ) ; // Must pass a name
2014-08-19 19:09:13 +08:00
2015-07-18 06:36:04 +08:00
if ( flags & ImGuiWindowFlags_NoInputs )
flags | = ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoResize ;
2014-12-29 01:54:02 +08:00
// Find or create
2015-03-22 23:58:44 +08:00
bool window_is_new = false ;
2014-12-29 01:54:02 +08:00
ImGuiWindow * window = FindWindowByName ( name ) ;
2014-08-19 19:09:13 +08:00
if ( ! window )
2015-03-22 23:58:44 +08:00
{
2015-03-24 06:50:17 +08:00
window = CreateNewWindow ( name , size_on_first_use , flags ) ;
2015-03-22 23:58:44 +08:00
window_is_new = true ;
}
2014-08-19 19:09:13 +08:00
window - > Flags = ( ImGuiWindowFlags ) flags ;
2015-05-11 22:57:02 +08:00
2014-12-29 01:54:02 +08:00
// Add to stack
2015-05-08 01:43:08 +08:00
ImGuiWindow * parent_window = ! g . CurrentWindowStack . empty ( ) ? g . CurrentWindowStack . back ( ) : NULL ;
2014-08-19 19:09:13 +08:00
g . CurrentWindowStack . push_back ( window ) ;
2015-03-30 02:45:03 +08:00
SetCurrentWindow ( window ) ;
2015-05-09 16:59:46 +08:00
CheckStacksSize ( window , true ) ;
2015-05-29 21:54:33 +08:00
IM_ASSERT ( parent_window ! = NULL | | ! ( flags & ImGuiWindowFlags_ChildWindow ) ) ;
2014-08-19 19:09:13 +08:00
2015-05-11 22:57:02 +08:00
const int current_frame = ImGui : : GetFrameCount ( ) ;
bool window_was_visible = ( window - > LastFrameDrawn = = current_frame - 1 ) ; // Not using !WasActive because the implicit "Debug" window would always toggle off->on
if ( flags & ImGuiWindowFlags_Popup )
{
2015-07-08 02:39:08 +08:00
ImGuiPopupRef & popup_ref = g . OpenedPopupStack [ g . CurrentPopupStack . Size ] ;
2015-05-15 18:30:51 +08:00
window_was_visible & = ( window - > PopupID = = popup_ref . PopupID ) ;
2015-05-16 00:29:42 +08:00
window_was_visible & = ( window = = popup_ref . Window ) ;
2015-05-15 18:30:51 +08:00
popup_ref . Window = window ;
g . CurrentPopupStack . push_back ( popup_ref ) ;
window - > PopupID = popup_ref . PopupID ;
2015-05-11 22:57:02 +08:00
}
2014-08-19 19:09:13 +08:00
2014-12-11 03:22:30 +08:00
// Process SetNextWindow***() calls
2015-06-10 01:35:29 +08:00
bool window_pos_set_by_api = false , window_size_set_by_api = false ;
2014-12-11 03:22:30 +08:00
if ( g . SetNextWindowPosCond )
{
2015-03-23 00:08:42 +08:00
const ImVec2 backup_cursor_pos = window - > DC . CursorPos ; // FIXME: not sure of the exact reason of this anymore :( need to look into that.
2015-05-08 01:39:23 +08:00
if ( ! window_was_visible ) window - > SetWindowPosAllowFlags | = ImGuiSetCond_Appearing ;
window_pos_set_by_api = ( window - > SetWindowPosAllowFlags & g . SetNextWindowPosCond ) ! = 0 ;
2015-06-24 05:34:44 +08:00
if ( window_pos_set_by_api & & ImLengthSqr ( g . SetNextWindowPosVal - ImVec2 ( - FLT_MAX , - FLT_MAX ) ) < 0.001f )
2015-06-24 05:45:34 +08:00
{
window - > SetWindowPosCenterWanted = true ; // May be processed on the next frame if this is our first frame and we are measuring size
window - > SetWindowPosAllowFlags & = ~ ( ImGuiSetCond_Once | ImGuiSetCond_FirstUseEver | ImGuiSetCond_Appearing ) ;
}
2015-06-24 05:34:44 +08:00
else
2015-06-24 05:45:34 +08:00
{
2015-06-24 05:34:44 +08:00
ImGui : : SetWindowPos ( g . SetNextWindowPosVal , g . SetNextWindowPosCond ) ;
2015-06-24 05:45:34 +08:00
}
2014-12-11 03:22:30 +08:00
window - > DC . CursorPos = backup_cursor_pos ;
g . SetNextWindowPosCond = 0 ;
}
if ( g . SetNextWindowSizeCond )
{
2015-05-08 01:39:23 +08:00
if ( ! window_was_visible ) window - > SetWindowSizeAllowFlags | = ImGuiSetCond_Appearing ;
2015-06-10 01:35:29 +08:00
window_size_set_by_api = ( window - > SetWindowSizeAllowFlags & g . SetNextWindowSizeCond ) ! = 0 ;
2014-12-11 03:22:30 +08:00
ImGui : : SetWindowSize ( g . SetNextWindowSizeVal , g . SetNextWindowSizeCond ) ;
g . SetNextWindowSizeCond = 0 ;
}
if ( g . SetNextWindowCollapsedCond )
{
2015-05-08 01:39:23 +08:00
if ( ! window_was_visible ) window - > SetWindowCollapsedAllowFlags | = ImGuiSetCond_Appearing ;
2014-12-11 03:22:30 +08:00
ImGui : : SetWindowCollapsed ( g . SetNextWindowCollapsedVal , g . SetNextWindowCollapsedCond ) ;
g . SetNextWindowCollapsedCond = 0 ;
}
2015-02-27 17:01:12 +08:00
if ( g . SetNextWindowFocus )
{
ImGui : : SetWindowFocus ( ) ;
g . SetNextWindowFocus = false ;
}
2014-12-11 03:22:30 +08:00
2015-02-28 18:29:00 +08:00
// Update known root window (if we are a child window, otherwise window == window->RootWindow)
2015-07-08 02:09:25 +08:00
int root_idx , root_non_popup_idx ;
2015-07-08 02:39:08 +08:00
for ( root_idx = g . CurrentWindowStack . Size - 1 ; root_idx > 0 ; root_idx - - )
2015-07-01 03:52:00 +08:00
if ( ! ( g . CurrentWindowStack [ root_idx ] - > Flags & ImGuiWindowFlags_ChildWindow ) )
break ;
for ( root_non_popup_idx = root_idx ; root_non_popup_idx > 0 ; root_non_popup_idx - - )
if ( ! ( g . CurrentWindowStack [ root_non_popup_idx ] - > Flags & ( ImGuiWindowFlags_ChildWindow | ImGuiWindowFlags_Popup ) ) )
2014-12-04 00:54:04 +08:00
break ;
window - > RootWindow = g . CurrentWindowStack [ root_idx ] ;
2015-07-01 03:52:00 +08:00
window - > RootNonPopupWindow = g . CurrentWindowStack [ root_non_popup_idx ] ; // This is merely for displaying the TitleBgActive color.
2014-12-04 00:54:04 +08:00
2014-08-19 19:09:13 +08:00
// Default alpha
2015-02-22 20:38:46 +08:00
if ( bg_alpha < 0.0f )
bg_alpha = style . WindowFillAlphaDefault ;
2014-08-19 19:09:13 +08:00
// When reusing window again multiple times a frame, just append content (don't need to setup again)
const bool first_begin_of_the_frame = ( window - > LastFrameDrawn ! = current_frame ) ;
if ( first_begin_of_the_frame )
{
2015-05-02 20:20:50 +08:00
window - > Active = true ;
2015-05-22 05:21:00 +08:00
window - > BeginCount = 0 ;
2014-08-19 19:09:13 +08:00
window - > DrawList - > Clear ( ) ;
2015-07-07 11:46:12 +08:00
window - > ClipRect = ImVec4 ( - FLT_MAX , - FLT_MAX , + FLT_MAX , + FLT_MAX ) ;
2015-05-02 17:46:24 +08:00
window - > LastFrameDrawn = current_frame ;
2015-05-03 00:54:25 +08:00
window - > IDStack . resize ( 1 ) ;
2015-05-02 17:32:32 +08:00
2015-05-22 05:45:32 +08:00
// Setup texture, outer clipping rectangle
window - > DrawList - > PushTextureID ( g . Font - > ContainerAtlas - > TexID ) ;
2015-05-22 05:51:21 +08:00
if ( ( flags & ImGuiWindowFlags_ChildWindow ) & & ! ( flags & ( ImGuiWindowFlags_ComboBox | ImGuiWindowFlags_Popup ) ) )
2015-07-07 11:46:12 +08:00
PushClipRect ( parent_window - > ClipRect ) ;
2015-05-22 05:45:32 +08:00
else
PushClipRect ( GetVisibleRect ( ) ) ;
2014-08-19 19:09:13 +08:00
// New windows appears in front
2015-05-07 18:24:51 +08:00
if ( ! window_was_visible )
2015-03-27 02:17:55 +08:00
{
2015-05-03 01:22:46 +08:00
window - > AutoPosLastDirection = - 1 ;
2015-05-12 02:52:02 +08:00
if ( ! ( flags & ( ImGuiWindowFlags_ChildWindow | ImGuiWindowFlags_Tooltip ) ) | | ( flags & ImGuiWindowFlags_Popup ) )
2015-03-13 19:16:11 +08:00
FocusWindow ( window ) ;
2014-08-19 19:09:13 +08:00
2015-05-11 22:57:02 +08:00
// Popup first latch mouse position, will position itself when it appears next frame
if ( ( flags & ImGuiWindowFlags_Popup ) ! = 0 & & ! window_pos_set_by_api )
window - > PosFloat = g . IO . MousePos ;
2015-03-27 02:17:55 +08:00
}
2015-03-27 01:09:17 +08:00
2015-05-03 01:22:46 +08:00
// Collapse window by double-clicking on title bar
// At this point we don't have a clipping rectangle setup yet, so we can use the title bar area for hit detection and drawing
if ( ! ( flags & ImGuiWindowFlags_NoTitleBar ) & & ! ( flags & ImGuiWindowFlags_NoCollapse ) )
{
2015-07-31 13:48:59 +08:00
ImRect title_bar_rect = window - > TitleBarRect ( ) ;
if ( g . HoveredWindow = = window & & IsMouseHoveringRect ( title_bar_rect . Min , title_bar_rect . Max ) & & g . IO . MouseDoubleClicked [ 0 ] )
2015-05-03 01:22:46 +08:00
{
window - > Collapsed = ! window - > Collapsed ;
if ( ! ( flags & ImGuiWindowFlags_NoSavedSettings ) )
MarkSettingsDirty ( ) ;
FocusWindow ( window ) ;
2015-03-27 01:09:17 +08:00
}
2015-03-27 02:17:55 +08:00
}
2015-05-03 01:22:46 +08:00
else
{
window - > Collapsed = false ;
}
2015-03-27 01:09:17 +08:00
2015-05-03 01:22:46 +08:00
const bool window_appearing_after_being_hidden = ( window - > HiddenFrames = = 1 ) ;
if ( window - > HiddenFrames > 0 )
window - > HiddenFrames - - ;
2014-08-19 19:09:13 +08:00
2015-05-03 01:22:46 +08:00
// SIZE
// Save contents size from last frame for auto-fitting
2015-03-22 23:58:44 +08:00
window - > SizeContents = window_is_new ? ImVec2 ( 0.0f , 0.0f ) : window - > DC . CursorMaxPos - window - > Pos ;
2015-03-18 06:03:02 +08:00
window - > SizeContents . y + = window - > ScrollY ;
2015-03-08 20:04:38 +08:00
2015-05-03 01:22:46 +08:00
// Hide popup/tooltip window when first appearing while we measure size (because we recycle them)
2015-05-11 22:57:02 +08:00
if ( ( flags & ( ImGuiWindowFlags_Popup | ImGuiWindowFlags_Tooltip ) ) ! = 0 & & ! window_was_visible )
2015-05-03 01:22:46 +08:00
{
window - > HiddenFrames = 1 ;
2015-06-24 04:28:11 +08:00
if ( flags & ImGuiWindowFlags_AlwaysAutoResize )
{
if ( ! window_size_set_by_api )
window - > Size = window - > SizeFull = ImVec2 ( 0.f , 0.f ) ;
window - > SizeContents = ImVec2 ( 0.f , 0.f ) ;
}
2015-05-03 01:22:46 +08:00
}
2015-07-02 08:55:42 +08:00
// Lock window padding so that altering the ShowBorders flag for childs doesn't have side-effects.
window - > WindowPadding = ( ( flags & ImGuiWindowFlags_ChildWindow ) & & ! ( flags & ( ImGuiWindowFlags_ShowBorders | ImGuiWindowFlags_ComboBox | ImGuiWindowFlags_Popup ) ) ) ? ImVec2 ( 0 , 0 ) : style . WindowPadding ;
2015-05-03 01:22:46 +08:00
// Calculate auto-fit size
ImVec2 size_auto_fit ;
if ( ( flags & ImGuiWindowFlags_Tooltip ) ! = 0 )
{
// Tooltip always resize. We keep the spacing symmetric on both axises for aesthetic purpose.
2015-07-02 08:55:42 +08:00
size_auto_fit = window - > SizeContents + window - > WindowPadding - ImVec2 ( 0.0f , style . ItemSpacing . y ) ;
2015-05-03 01:22:46 +08:00
}
else
{
2015-07-02 08:55:42 +08:00
size_auto_fit = ImClamp ( window - > SizeContents + window - > WindowPadding , style . WindowMinSize , ImMax ( style . WindowMinSize , g . IO . DisplaySize - window - > WindowPadding ) ) ;
2015-05-25 06:25:57 +08:00
if ( size_auto_fit . y < window - > SizeContents . y & & ! ( flags & ImGuiWindowFlags_NoScrollbar ) )
2015-05-03 01:22:46 +08:00
size_auto_fit . x + = style . ScrollbarWidth ;
2015-05-25 04:11:11 +08:00
size_auto_fit . y = ImMax ( size_auto_fit . y - style . ItemSpacing . y , 0.0f ) ;
2015-05-03 01:22:46 +08:00
}
// Handle automatic resize
if ( window - > Collapsed )
{
// We still process initial auto-fit on collapsed windows to get a window width,
// But otherwise we don't honor ImGuiWindowFlags_AlwaysAutoResize when collapsed.
2015-06-25 10:08:08 +08:00
if ( window - > AutoFitFramesX > 0 )
window - > SizeFull . x = window - > AutoFitOnlyGrows ? ImMax ( window - > SizeFull . x , size_auto_fit . x ) : size_auto_fit . x ;
if ( window - > AutoFitFramesY > 0 )
window - > SizeFull . y = window - > AutoFitOnlyGrows ? ImMax ( window - > SizeFull . y , size_auto_fit . y ) : size_auto_fit . y ;
2015-05-03 01:22:46 +08:00
window - > Size = window - > TitleBarRect ( ) . GetSize ( ) ;
}
else
{
2015-06-10 01:35:29 +08:00
if ( ( flags & ImGuiWindowFlags_AlwaysAutoResize ) & & ! window_size_set_by_api )
2015-05-03 01:22:46 +08:00
{
window - > SizeFull = size_auto_fit ;
}
2015-06-25 10:08:08 +08:00
else if ( ( window - > AutoFitFramesX > 0 | | window - > AutoFitFramesY > 0 ) & & ! window_size_set_by_api )
2015-05-03 01:22:46 +08:00
{
// Auto-fit only grows during the first few frames
2015-06-25 10:08:08 +08:00
if ( window - > AutoFitFramesX > 0 )
window - > SizeFull . x = window - > AutoFitOnlyGrows ? ImMax ( window - > SizeFull . x , size_auto_fit . x ) : size_auto_fit . x ;
if ( window - > AutoFitFramesY > 0 )
window - > SizeFull . y = window - > AutoFitOnlyGrows ? ImMax ( window - > SizeFull . y , size_auto_fit . y ) : size_auto_fit . y ;
2015-05-03 01:22:46 +08:00
if ( ! ( flags & ImGuiWindowFlags_NoSavedSettings ) )
MarkSettingsDirty ( ) ;
}
window - > Size = window - > SizeFull ;
}
// Minimum window size
2015-06-25 10:35:40 +08:00
if ( ! ( flags & ( ImGuiWindowFlags_ChildWindow | ImGuiWindowFlags_AlwaysAutoResize ) ) )
2015-05-16 04:40:45 +08:00
{
2015-05-03 01:22:46 +08:00
window - > SizeFull = ImMax ( window - > SizeFull , style . WindowMinSize ) ;
2015-05-16 04:40:45 +08:00
if ( ! window - > Collapsed )
window - > Size = window - > SizeFull ;
}
2015-05-03 01:22:46 +08:00
// POSITION
// Position child window
2014-08-19 19:09:13 +08:00
if ( flags & ImGuiWindowFlags_ChildWindow )
parent_window - > DC . ChildWindows . push_back ( window ) ;
2015-05-12 02:52:02 +08:00
if ( ( flags & ImGuiWindowFlags_ChildWindow ) & & ! ( flags & ImGuiWindowFlags_Popup ) )
{
2014-08-19 19:09:13 +08:00
window - > Pos = window - > PosFloat = parent_window - > DC . CursorPos ;
2015-05-16 04:40:45 +08:00
window - > Size = window - > SizeFull = size_on_first_use ; // NB: argument name 'size_on_first_use' misleading here, it's really just 'size' as provided by user.
2014-08-19 19:09:13 +08:00
}
2015-01-29 21:20:25 +08:00
2015-06-24 05:45:34 +08:00
bool window_pos_center = false ;
window_pos_center | = ( window - > SetWindowPosCenterWanted & & window - > HiddenFrames = = 0 ) ;
window_pos_center | = ( ( flags & ImGuiWindowFlags_Modal ) & & ! window_pos_set_by_api & & window_appearing_after_being_hidden ) ;
2015-06-24 05:34:44 +08:00
if ( window_pos_center )
{
2015-06-24 05:45:34 +08:00
// Center (any sort of window)
2015-06-24 05:34:44 +08:00
ImRect fullscreen_rect ( GetVisibleRect ( ) ) ;
SetWindowPos ( ImMax ( style . DisplaySafeAreaPadding , fullscreen_rect . GetCenter ( ) - window - > SizeFull * 0.5f ) ) ;
}
else if ( flags & ImGuiWindowFlags_ChildMenu )
2015-05-12 02:52:02 +08:00
{
IM_ASSERT ( window_pos_set_by_api ) ;
2015-05-18 00:13:45 +08:00
ImRect rect_to_avoid ;
if ( parent_window - > DC . MenuBarAppending )
rect_to_avoid = ImRect ( - FLT_MAX , parent_window - > Pos . y + parent_window - > TitleBarHeight ( ) , FLT_MAX , parent_window - > Pos . y + parent_window - > TitleBarHeight ( ) + parent_window - > MenuBarHeight ( ) ) ;
else
2015-05-25 06:58:40 +08:00
rect_to_avoid = ImRect ( parent_window - > Pos . x + style . ItemSpacing . x , - FLT_MAX , parent_window - > Pos . x + parent_window - > Size . x - style . ItemSpacing . x - parent_window - > ScrollbarWidth ( ) , FLT_MAX ) ; // We want some overlap to convey the relative depth of each popup (here hard-coded to 4)
2015-06-30 05:18:26 +08:00
window - > PosFloat = FindBestPopupWindowPos ( window - > PosFloat , window - > Size , flags , & window - > AutoPosLastDirection , rect_to_avoid ) ;
2015-05-12 02:52:02 +08:00
}
2015-06-24 04:28:11 +08:00
else if ( ( flags & ImGuiWindowFlags_Popup ) ! = 0 & & ! window_pos_set_by_api & & window_appearing_after_being_hidden )
2015-05-03 01:22:46 +08:00
{
ImRect rect_to_avoid ( window - > PosFloat . x - 1 , window - > PosFloat . y - 1 , window - > PosFloat . x + 1 , window - > PosFloat . y + 1 ) ;
2015-06-30 05:18:26 +08:00
window - > PosFloat = FindBestPopupWindowPos ( window - > PosFloat , window - > Size , flags , & window - > AutoPosLastDirection , rect_to_avoid ) ;
2015-05-03 01:22:46 +08:00
}
2014-08-19 19:09:13 +08:00
2015-05-03 01:22:46 +08:00
// Position tooltip (always follows mouse)
if ( ( flags & ImGuiWindowFlags_Tooltip ) ! = 0 & & ! window_pos_set_by_api )
{
ImRect rect_to_avoid ( g . IO . MousePos . x - 16 , g . IO . MousePos . y - 8 , g . IO . MousePos . x + 24 , g . IO . MousePos . y + 24 ) ; // FIXME: Completely hard-coded. Perhaps center on cursor hit-point instead?
2015-06-30 05:18:26 +08:00
window - > PosFloat = FindBestPopupWindowPos ( g . IO . MousePos , window - > Size , flags , & window - > AutoPosLastDirection , rect_to_avoid ) ;
2015-05-03 01:22:46 +08:00
}
2014-08-19 19:09:13 +08:00
2015-05-03 01:22:46 +08:00
// User moving window (at the beginning of the frame to avoid input lag or sheering). Only valid for root windows.
2015-07-22 07:00:28 +08:00
KeepAliveID ( window - > MoveID ) ;
2015-03-18 20:10:26 +08:00
if ( g . ActiveId = = window - > MoveID )
2014-08-19 19:09:13 +08:00
{
if ( g . IO . MouseDown [ 0 ] )
{
2015-05-03 00:45:54 +08:00
if ( ! ( flags & ImGuiWindowFlags_NoMove ) )
2014-08-19 19:09:13 +08:00
{
window - > PosFloat + = g . IO . MouseDelta ;
2015-05-03 00:45:54 +08:00
if ( ! ( flags & ImGuiWindowFlags_NoSavedSettings ) )
2015-01-05 02:23:30 +08:00
MarkSettingsDirty ( ) ;
2014-08-19 19:09:13 +08:00
}
2015-02-28 18:25:04 +08:00
IM_ASSERT ( g . MovedWindow ! = NULL ) ;
FocusWindow ( g . MovedWindow ) ;
2014-08-19 19:09:13 +08:00
}
else
{
2015-07-22 07:00:28 +08:00
SetActiveID ( 0 ) ;
2015-02-28 18:25:04 +08:00
g . MovedWindow = NULL ; // Not strictly necessary but doing it for sanity.
2014-08-19 19:09:13 +08:00
}
}
2015-05-25 06:17:06 +08:00
// Clamp position so it stays visible
2015-05-03 00:45:54 +08:00
if ( ! ( flags & ImGuiWindowFlags_ChildWindow ) & & ! ( flags & ImGuiWindowFlags_Tooltip ) )
2014-08-19 19:09:13 +08:00
{
2015-06-25 10:08:08 +08:00
if ( ! window_pos_set_by_api & & window - > AutoFitFramesX < = 0 & & window - > AutoFitFramesY < = 0 & & g . IO . DisplaySize . x > 0.0f & & g . IO . DisplaySize . y > 0.0f ) // Ignore zero-sized display explicitly to avoid losing positions if a window manager reports zero-sized window when initializing or minimizing.
2014-12-22 10:14:25 +08:00
{
2015-05-02 03:35:44 +08:00
ImVec2 padding = ImMax ( style . DisplayWindowPadding , style . DisplaySafeAreaPadding ) ;
window - > PosFloat = ImMax ( window - > PosFloat + window - > Size , padding ) - window - > Size ;
window - > PosFloat = ImMin ( window - > PosFloat , g . IO . DisplaySize - padding ) ;
2014-12-22 10:14:25 +08:00
}
2014-08-19 19:09:13 +08:00
}
2014-09-02 21:35:06 +08:00
window - > Pos = ImVec2 ( ( float ) ( int ) window - > PosFloat . x , ( float ) ( int ) window - > PosFloat . y ) ;
2014-08-19 19:09:13 +08:00
2015-01-16 01:47:53 +08:00
// Default item width. Make it proportional to window size if window manually resizes
2015-05-03 00:45:54 +08:00
if ( window - > Size . x > 0.0f & & ! ( flags & ImGuiWindowFlags_Tooltip ) & & ! ( flags & ImGuiWindowFlags_AlwaysAutoResize ) )
2014-08-19 19:09:13 +08:00
window - > ItemWidthDefault = ( float ) ( int ) ( window - > Size . x * 0.65f ) ;
else
2015-05-12 02:47:01 +08:00
window - > ItemWidthDefault = ( float ) ( int ) ( g . FontSize * 16.0f ) ;
2014-08-19 19:09:13 +08:00
2014-09-30 19:31:35 +08:00
// Prepare for focus requests
2015-05-12 01:28:30 +08:00
window - > FocusIdxAllRequestCurrent = ( window - > FocusIdxAllRequestNext = = IM_INT_MAX | | window - > FocusIdxAllCounter = = - 1 ) ? IM_INT_MAX : ( window - > FocusIdxAllRequestNext + ( window - > FocusIdxAllCounter + 1 ) ) % ( window - > FocusIdxAllCounter + 1 ) ;
window - > FocusIdxTabRequestCurrent = ( window - > FocusIdxTabRequestNext = = IM_INT_MAX | | window - > FocusIdxTabCounter = = - 1 ) ? IM_INT_MAX : ( window - > FocusIdxTabRequestNext + ( window - > FocusIdxTabCounter + 1 ) ) % ( window - > FocusIdxTabCounter + 1 ) ;
2014-09-30 19:31:35 +08:00
window - > FocusIdxAllCounter = window - > FocusIdxTabCounter = - 1 ;
2014-09-30 19:22:58 +08:00
window - > FocusIdxAllRequestNext = window - > FocusIdxTabRequestNext = IM_INT_MAX ;
2014-08-19 19:09:13 +08:00
2015-05-02 04:01:54 +08:00
// Apply scrolling
2015-07-03 10:53:45 +08:00
if ( window - > ScrollTargetRelY < FLT_MAX )
2015-06-10 02:49:13 +08:00
{
2015-07-02 07:51:44 +08:00
float center_ratio_y = window - > ScrollTargetCenterRatioY ;
2015-07-02 08:23:11 +08:00
window - > ScrollY = window - > ScrollTargetRelY - ( ( 1.0f - center_ratio_y ) * window - > TitleBarHeight ( ) ) - ( center_ratio_y * window - > SizeFull . y ) ;
2015-07-03 10:53:45 +08:00
window - > ScrollTargetRelY = FLT_MAX ;
2015-06-10 02:49:13 +08:00
}
2014-08-19 19:09:13 +08:00
window - > ScrollY = ImMax ( window - > ScrollY , 0.0f ) ;
if ( ! window - > Collapsed & & ! window - > SkipItems )
2015-03-09 00:24:11 +08:00
window - > ScrollY = ImMin ( window - > ScrollY , ImMax ( 0.0f , window - > SizeContents . y - window - > SizeFull . y ) ) ;
2014-08-19 19:09:13 +08:00
2015-06-24 02:34:15 +08:00
// Modal window darkens what is behind them
if ( ( flags & ImGuiWindowFlags_Modal ) ! = 0 & & window = = GetFrontMostModalRootWindow ( ) )
{
2015-06-24 04:28:11 +08:00
ImRect fullscreen_rect = GetVisibleRect ( ) ;
window - > DrawList - > AddRectFilled ( fullscreen_rect . Min , fullscreen_rect . Max , window - > Color ( ImGuiCol_ModalWindowDarkening , g . ModalWindowDarkeningRatio ) ) ;
2015-06-24 02:34:15 +08:00
}
2015-05-02 19:08:24 +08:00
// Draw window + handle manual resize
ImRect title_bar_rect = window - > TitleBarRect ( ) ;
2015-05-03 00:45:54 +08:00
const float window_rounding = ( flags & ImGuiWindowFlags_ChildWindow ) ? style . ChildWindowRounding : style . WindowRounding ;
2014-08-19 19:09:13 +08:00
if ( window - > Collapsed )
{
2014-09-02 21:35:06 +08:00
// Draw title bar only
2015-03-18 07:37:17 +08:00
window - > DrawList - > AddRectFilled ( title_bar_rect . GetTL ( ) , title_bar_rect . GetBR ( ) , window - > Color ( ImGuiCol_TitleBgCollapsed ) , window_rounding ) ;
2015-05-03 00:45:54 +08:00
if ( flags & ImGuiWindowFlags_ShowBorders )
2014-08-19 19:09:13 +08:00
{
2015-03-18 07:37:17 +08:00
window - > DrawList - > AddRect ( title_bar_rect . GetTL ( ) + ImVec2 ( 1 , 1 ) , title_bar_rect . GetBR ( ) + ImVec2 ( 1 , 1 ) , window - > Color ( ImGuiCol_BorderShadow ) , window_rounding ) ;
window - > DrawList - > AddRect ( title_bar_rect . GetTL ( ) , title_bar_rect . GetBR ( ) , window - > Color ( ImGuiCol_Border ) , window_rounding ) ;
2014-08-19 19:09:13 +08:00
}
}
else
{
ImU32 resize_col = 0 ;
2015-08-02 23:15:36 +08:00
const float resize_corner_size = ImMax ( g . FontSize * 1.35f , window_rounding + 1.0f + g . FontSize * 0.2f ) ;
2015-06-25 10:08:08 +08:00
if ( ! ( flags & ImGuiWindowFlags_AlwaysAutoResize ) & & window - > AutoFitFramesX < = 0 & & window - > AutoFitFramesY < = 0 & & ! ( flags & ImGuiWindowFlags_NoResize ) )
2014-08-19 19:09:13 +08:00
{
2015-05-03 00:54:25 +08:00
// Manual resize
2015-08-02 23:15:36 +08:00
const ImVec2 br = window - > Rect ( ) . GetBR ( ) ;
const ImRect resize_rect ( br - ImVec2 ( resize_corner_size * 0.75f , resize_corner_size * 0.75f ) , br ) ;
2015-05-02 19:08:24 +08:00
const ImGuiID resize_id = window - > GetID ( " #RESIZE " ) ;
bool hovered , held ;
2015-05-18 23:42:55 +08:00
ButtonBehavior ( resize_rect , resize_id , & hovered , & held , true , ImGuiButtonFlags_FlattenChilds ) ;
2015-05-02 19:08:24 +08:00
resize_col = window - > Color ( held ? ImGuiCol_ResizeGripActive : hovered ? ImGuiCol_ResizeGripHovered : ImGuiCol_ResizeGrip ) ;
if ( hovered | | held )
g . MouseCursor = ImGuiMouseCursor_ResizeNWSE ;
if ( g . HoveredWindow = = window & & held & & g . IO . MouseDoubleClicked [ 0 ] )
2014-11-22 00:29:56 +08:00
{
2015-05-02 19:08:24 +08:00
// Manual auto-fit when double-clicking
2014-11-22 00:29:56 +08:00
window - > SizeFull = size_auto_fit ;
2015-05-03 00:45:54 +08:00
if ( ! ( flags & ImGuiWindowFlags_NoSavedSettings ) )
2015-01-05 02:23:30 +08:00
MarkSettingsDirty ( ) ;
2015-07-22 07:00:28 +08:00
SetActiveID ( 0 ) ;
2014-08-19 19:09:13 +08:00
}
2015-05-02 19:08:24 +08:00
else if ( held )
2014-08-19 19:09:13 +08:00
{
2015-05-02 19:35:34 +08:00
window - > SizeFull = ImMax ( window - > SizeFull + g . IO . MouseDelta , style . WindowMinSize ) ;
2015-05-03 00:45:54 +08:00
if ( ! ( flags & ImGuiWindowFlags_NoSavedSettings ) )
2015-05-02 19:08:24 +08:00
MarkSettingsDirty ( ) ;
2014-08-19 19:09:13 +08:00
}
2015-03-19 06:26:55 +08:00
window - > Size = window - > SizeFull ;
2015-03-18 07:37:17 +08:00
title_bar_rect = window - > TitleBarRect ( ) ;
2014-08-19 19:09:13 +08:00
}
2015-02-10 21:00:22 +08:00
// Scrollbar
2015-05-25 04:11:11 +08:00
window - > ScrollbarY = ( window - > SizeContents . y > window - > Size . y + style . ItemSpacing . y ) & & ! ( flags & ImGuiWindowFlags_NoScrollbar ) ;
2015-02-10 21:00:22 +08:00
// Window background
2015-02-22 20:38:46 +08:00
if ( bg_alpha > 0.0f )
2014-08-19 19:09:13 +08:00
{
2015-05-03 00:45:54 +08:00
if ( ( flags & ImGuiWindowFlags_ComboBox ) ! = 0 )
2015-02-22 20:38:46 +08:00
window - > DrawList - > AddRectFilled ( window - > Pos , window - > Pos + window - > Size , window - > Color ( ImGuiCol_ComboBg , bg_alpha ) , window_rounding ) ;
2015-05-03 00:45:54 +08:00
else if ( ( flags & ImGuiWindowFlags_Tooltip ) ! = 0 )
2015-02-22 20:38:46 +08:00
window - > DrawList - > AddRectFilled ( window - > Pos , window - > Pos + window - > Size , window - > Color ( ImGuiCol_TooltipBg , bg_alpha ) , window_rounding ) ;
2015-05-12 02:52:02 +08:00
else if ( ( flags & ImGuiWindowFlags_Popup ) ! = 0 )
window - > DrawList - > AddRectFilled ( window - > Pos , window - > Pos + window - > Size , window - > Color ( ImGuiCol_WindowBg , bg_alpha ) , window_rounding ) ;
2015-05-03 00:45:54 +08:00
else if ( ( flags & ImGuiWindowFlags_ChildWindow ) ! = 0 )
2015-05-25 06:37:47 +08:00
window - > DrawList - > AddRectFilled ( window - > Pos , window - > Pos + window - > Size - ImVec2 ( window - > ScrollbarWidth ( ) , 0.0f ) , window - > Color ( ImGuiCol_ChildWindowBg , bg_alpha ) , window_rounding , window - > ScrollbarY ? ( 1 | 8 ) : ( 0xF ) ) ;
2014-08-19 19:09:13 +08:00
else
2015-02-22 20:38:46 +08:00
window - > DrawList - > AddRectFilled ( window - > Pos , window - > Pos + window - > Size , window - > Color ( ImGuiCol_WindowBg , bg_alpha ) , window_rounding ) ;
2014-08-19 19:09:13 +08:00
}
2015-02-10 21:00:22 +08:00
// Title bar
2015-05-03 00:45:54 +08:00
if ( ! ( flags & ImGuiWindowFlags_NoTitleBar ) )
2015-07-01 03:52:00 +08:00
window - > DrawList - > AddRectFilled ( title_bar_rect . GetTL ( ) , title_bar_rect . GetBR ( ) , window - > Color ( ( g . FocusedWindow & & window - > RootNonPopupWindow = = g . FocusedWindow - > RootNonPopupWindow ) ? ImGuiCol_TitleBgActive : ImGuiCol_TitleBg ) , window_rounding , 1 | 2 ) ;
2014-08-19 19:09:13 +08:00
2015-05-18 00:13:45 +08:00
// Menu bar
if ( flags & ImGuiWindowFlags_MenuBar )
{
ImRect menu_bar_rect = window - > MenuBarRect ( ) ;
window - > DrawList - > AddRectFilled ( menu_bar_rect . GetTL ( ) , menu_bar_rect . GetBR ( ) , window - > Color ( ImGuiCol_MenuBarBg ) , ( flags & ImGuiWindowFlags_NoTitleBar ) ? window_rounding : 0.0f , 1 | 2 ) ;
}
2014-09-25 03:39:39 +08:00
// Borders
2015-05-03 00:45:54 +08:00
if ( flags & ImGuiWindowFlags_ShowBorders )
2014-08-19 19:09:13 +08:00
{
2015-02-10 21:00:22 +08:00
window - > DrawList - > AddRect ( window - > Pos + ImVec2 ( 1 , 1 ) , window - > Pos + window - > Size + ImVec2 ( 1 , 1 ) , window - > Color ( ImGuiCol_BorderShadow ) , window_rounding ) ;
window - > DrawList - > AddRect ( window - > Pos , window - > Pos + window - > Size , window - > Color ( ImGuiCol_Border ) , window_rounding ) ;
2015-05-03 00:45:54 +08:00
if ( ! ( flags & ImGuiWindowFlags_NoTitleBar ) )
2015-07-06 06:24:55 +08:00
window - > DrawList - > AddLine ( title_bar_rect . GetBL ( ) , title_bar_rect . GetBR ( ) , window - > Color ( ImGuiCol_Border ) ) ;
2014-08-19 19:09:13 +08:00
}
// Scrollbar
if ( window - > ScrollbarY )
2015-03-09 00:37:03 +08:00
Scrollbar ( window ) ;
2014-08-19 19:09:13 +08:00
// Render resize grip
// (after the input handling so we don't have a frame of latency)
2015-05-03 00:45:54 +08:00
if ( ! ( flags & ImGuiWindowFlags_NoResize ) )
2014-08-19 19:09:13 +08:00
{
2015-03-18 07:37:17 +08:00
const ImVec2 br = window - > Rect ( ) . GetBR ( ) ;
2015-08-02 23:15:36 +08:00
window - > DrawList - > PathLineTo ( br + ImVec2 ( - resize_corner_size , 0.0f ) ) ;
window - > DrawList - > PathLineTo ( br + ImVec2 ( 0.0f , - resize_corner_size ) ) ;
2015-07-19 08:45:37 +08:00
window - > DrawList - > PathArcToFast ( ImVec2 ( br . x - window_rounding , br . y - window_rounding ) , window_rounding , 0 , 3 ) ;
2015-07-01 02:51:52 +08:00
window - > DrawList - > PathFill ( resize_col ) ;
2014-08-19 19:09:13 +08:00
}
}
// Setup drawing context
2015-07-02 08:55:42 +08:00
window - > DC . ColumnsStartX = window - > WindowPadding . x ;
2014-11-20 19:31:55 +08:00
window - > DC . ColumnsOffsetX = 0.0f ;
2015-07-02 08:55:42 +08:00
window - > DC . CursorStartPos = window - > Pos + ImVec2 ( window - > DC . ColumnsStartX + window - > DC . ColumnsOffsetX , window - > TitleBarHeight ( ) + window - > MenuBarHeight ( ) + window - > WindowPadding . y ) - ImVec2 ( 0.0f , window - > ScrollY ) ;
2014-08-19 19:09:13 +08:00
window - > DC . CursorPos = window - > DC . CursorStartPos ;
window - > DC . CursorPosPrevLine = window - > DC . CursorPos ;
2015-03-18 06:03:02 +08:00
window - > DC . CursorMaxPos = window - > DC . CursorStartPos ;
2014-08-19 19:09:13 +08:00
window - > DC . CurrentLineHeight = window - > DC . PrevLineHeight = 0.0f ;
2015-03-18 04:17:53 +08:00
window - > DC . CurrentLineTextBaseOffset = window - > DC . PrevLineTextBaseOffset = 0.0f ;
2015-05-18 00:13:45 +08:00
window - > DC . MenuBarAppending = false ;
2015-05-25 04:11:11 +08:00
window - > DC . MenuBarOffsetX = ImMax ( window - > DC . ColumnsStartX , style . ItemSpacing . x ) ;
2015-03-18 00:40:47 +08:00
window - > DC . LogLinePosY = window - > DC . CursorPos . y - 9999.0f ;
2014-08-19 19:09:13 +08:00
window - > DC . ChildWindows . resize ( 0 ) ;
2015-05-26 06:08:22 +08:00
window - > DC . LayoutType = ImGuiLayoutType_Vertical ;
2015-05-27 22:35:49 +08:00
window - > DC . ItemWidth = window - > ItemWidthDefault ;
2015-05-27 22:42:29 +08:00
window - > DC . ItemWidthStack . resize ( 0 ) ;
2015-05-27 22:22:09 +08:00
window - > DC . ButtonRepeat = false ;
window - > DC . ButtonRepeatStack . resize ( 0 ) ;
2015-05-27 22:35:49 +08:00
window - > DC . AllowKeyboardFocus = true ;
window - > DC . AllowKeyboardFocusStack . resize ( 0 ) ;
window - > DC . TextWrapPos = - 1.0f ; // disabled
window - > DC . TextWrapPosStack . resize ( 0 ) ;
2014-08-19 19:09:13 +08:00
window - > DC . ColorEditMode = ImGuiColorEditMode_UserSelect ;
2014-11-20 19:31:55 +08:00
window - > DC . ColumnsCurrent = 0 ;
2014-08-19 19:09:13 +08:00
window - > DC . ColumnsCount = 1 ;
2014-11-20 19:31:55 +08:00
window - > DC . ColumnsStartPos = window - > DC . CursorPos ;
window - > DC . ColumnsCellMinY = window - > DC . ColumnsCellMaxY = window - > DC . ColumnsStartPos . y ;
2014-08-19 19:09:13 +08:00
window - > DC . TreeDepth = 0 ;
window - > DC . StateStorage = & window - > StateStorage ;
2015-03-18 07:28:57 +08:00
window - > DC . GroupStack . resize ( 0 ) ;
2015-05-23 20:43:13 +08:00
window - > MenuColumns . Update ( 3 , style . ItemSpacing . x , ! window_was_visible ) ;
2014-08-19 19:09:13 +08:00
2015-06-25 10:08:08 +08:00
if ( window - > AutoFitFramesX > 0 )
window - > AutoFitFramesX - - ;
if ( window - > AutoFitFramesY > 0 )
window - > AutoFitFramesY - - ;
2014-08-19 19:09:13 +08:00
// Title bar
2015-05-03 00:45:54 +08:00
if ( ! ( flags & ImGuiWindowFlags_NoTitleBar ) )
2014-08-19 19:09:13 +08:00
{
2014-12-22 21:29:39 +08:00
if ( p_opened ! = NULL )
CloseWindowButton ( p_opened ) ;
2014-08-19 19:09:13 +08:00
2015-05-12 21:02:45 +08:00
const ImVec2 text_size = CalcTextSize ( name , NULL , true ) ;
2015-05-03 00:45:54 +08:00
if ( ! ( flags & ImGuiWindowFlags_NoCollapse ) )
2015-02-02 17:49:12 +08:00
RenderCollapseTriangle ( window - > Pos + style . FramePadding , ! window - > Collapsed , 1.0f , true ) ;
2015-05-12 21:02:45 +08:00
ImVec2 text_min = window - > Pos + style . FramePadding ;
ImVec2 text_max = window - > Pos + ImVec2 ( window - > Size . x - style . FramePadding . x , style . FramePadding . y * 2 + text_size . y ) ;
ImVec2 clip_max = ImVec2 ( window - > Pos . x + window - > Size . x - ( p_opened ? title_bar_rect . GetHeight ( ) - 3 : style . FramePadding . x ) , text_max . y ) ; // Match the size of CloseWindowButton()
bool pad_left = ( flags & ImGuiWindowFlags_NoCollapse ) = = 0 ;
bool pad_right = ( p_opened ! = NULL ) ;
if ( style . WindowTitleAlign & ImGuiAlign_Center ) pad_right = pad_left ;
if ( pad_left ) text_min . x + = g . FontSize + style . ItemInnerSpacing . x ;
if ( pad_right ) text_max . x - = g . FontSize + style . ItemInnerSpacing . x ;
2015-06-17 07:21:07 +08:00
RenderTextClipped ( text_min , text_max , name , NULL , & text_size , style . WindowTitleAlign , NULL , & clip_max ) ;
2015-03-27 01:09:17 +08:00
}
2015-02-22 20:05:38 +08:00
// Save clipped aabb so we can access it in constant-time in FindHoveredWindow()
2015-07-07 11:46:12 +08:00
window - > ClippedWindowRect = window - > Rect ( ) ;
window - > ClippedWindowRect . Clip ( window - > ClipRect ) ;
2015-03-13 21:04:10 +08:00
// Pressing CTRL+C while holding on a window copy its content to the clipboard
// This works but 1. doesn't handle multiple Begin/End pairs, 2. recursing into another Begin/End pair - so we need to work that out and add better logging scope.
// Maybe we can support CTRL+C on every element?
/*
if ( g . ActiveId = = move_id )
if ( g . IO . KeyCtrl & & IsKeyPressedMap ( ImGuiKey_C ) )
ImGui : : LogToClipboard ( ) ;
*/
2014-08-19 19:09:13 +08:00
}
2015-05-22 05:45:32 +08:00
window - > BeginCount + + ;
2014-08-19 19:09:13 +08:00
// Inner clipping rectangle
// We set this up after processing the resize grip so that our clip rectangle doesn't lag by a frame
2015-03-27 00:03:30 +08:00
// Note that if our window is collapsed we will end up with a null clipping rectangle which is the correct behavior.
2015-03-18 07:37:17 +08:00
const ImRect title_bar_rect = window - > TitleBarRect ( ) ;
2015-07-02 08:55:42 +08:00
ImVec4 clip_rect ( title_bar_rect . Min . x + 0.5f + window - > WindowPadding . x * 0.5f , title_bar_rect . Max . y + window - > MenuBarHeight ( ) + 0.5f , window - > Rect ( ) . Max . x + 0.5f - window - > WindowPadding . x * 0.5f , window - > Rect ( ) . Max . y - 1.5f ) ;
2014-08-19 19:09:13 +08:00
if ( window - > ScrollbarY )
2015-03-08 23:03:55 +08:00
clip_rect . z - = style . ScrollbarWidth ;
2014-11-10 09:29:01 +08:00
PushClipRect ( clip_rect ) ;
2014-08-19 19:09:13 +08:00
2015-03-20 18:54:21 +08:00
// Clear 'accessed' flag last thing
if ( first_begin_of_the_frame )
window - > Accessed = false ;
2014-08-19 19:09:13 +08:00
// Child window can be out of sight and have "negative" clip windows.
// Mark them as collapsed so commands are skipped earlier (we can't manually collapse because they have no title bar).
if ( flags & ImGuiWindowFlags_ChildWindow )
{
IM_ASSERT ( ( flags & ImGuiWindowFlags_NoTitleBar ) ! = 0 ) ;
2015-02-21 18:02:32 +08:00
window - > Collapsed = parent_window & & parent_window - > Collapsed ;
2015-06-25 10:08:08 +08:00
if ( ! ( flags & ImGuiWindowFlags_AlwaysAutoResize ) & & window - > AutoFitFramesX < = 0 & & window - > AutoFitFramesY < = 0 )
2015-05-25 06:56:29 +08:00
{
2015-07-07 11:46:12 +08:00
const ImVec4 clip_rect_t = window - > ClipRect ;
2015-05-25 06:56:29 +08:00
window - > Collapsed | = ( clip_rect_t . x > = clip_rect_t . z | | clip_rect_t . y > = clip_rect_t . w ) ;
}
2014-08-19 19:09:13 +08:00
// We also hide the window from rendering because we've already added its border to the command list.
2014-11-18 15:51:55 +08:00
// (we could perform the check earlier in the function but it is simpler at this point)
2014-08-19 19:09:13 +08:00
if ( window - > Collapsed )
2015-05-02 20:20:50 +08:00
window - > Active = false ;
2014-08-19 19:09:13 +08:00
}
2014-12-30 23:38:28 +08:00
if ( style . Alpha < = 0.0f )
2015-05-02 20:20:50 +08:00
window - > Active = false ;
2014-08-19 19:09:13 +08:00
2014-12-30 23:38:28 +08:00
// Return false if we don't intend to display anything to allow user to perform an early out optimization
2015-06-25 10:08:08 +08:00
window - > SkipItems = ( window - > Collapsed | | ! window - > Active ) & & window - > AutoFitFramesX < = 0 & & window - > AutoFitFramesY < = 0 ;
2014-08-19 19:09:13 +08:00
return ! window - > SkipItems ;
2014-08-11 04:58:11 +08:00
}
2014-11-10 09:29:01 +08:00
void ImGui : : End ( )
2014-08-11 04:58:11 +08:00
{
2015-02-01 12:28:24 +08:00
ImGuiState & g = * GImGui ;
2014-08-19 19:09:13 +08:00
ImGuiWindow * window = g . CurrentWindow ;
ImGui : : Columns ( 1 , " #CloseColumns " ) ;
2014-11-10 09:29:01 +08:00
PopClipRect ( ) ; // inner window clip rectangle
2014-08-19 19:09:13 +08:00
// Stop logging
2014-09-02 21:35:06 +08:00
if ( ! ( window - > Flags & ImGuiWindowFlags_ChildWindow ) ) // FIXME: add more options for scope of logging
2014-12-30 02:13:41 +08:00
ImGui : : LogFinish ( ) ;
2014-08-11 04:58:11 +08:00
2014-08-19 19:09:13 +08:00
// Pop
2015-02-28 18:29:00 +08:00
// NB: we don't clear 'window->RootWindow'. The pointer is allowed to live until the next call to Begin().
2014-08-19 19:09:13 +08:00
g . CurrentWindowStack . pop_back ( ) ;
2015-05-11 22:57:02 +08:00
if ( window - > Flags & ImGuiWindowFlags_Popup )
g . CurrentPopupStack . pop_back ( ) ;
2015-05-12 02:52:02 +08:00
CheckStacksSize ( window , false ) ;
2015-03-30 02:45:03 +08:00
SetCurrentWindow ( g . CurrentWindowStack . empty ( ) ? NULL : g . CurrentWindowStack . back ( ) ) ;
2014-08-11 04:58:11 +08:00
}
2015-03-09 00:37:03 +08:00
// Vertical scrollbar
// The entire piece of code below is rather confusing because:
// - We handle absolute seeking (when first clicking outside the grab) and relative manipulation (afterward or when clicking inside the grab)
// - We store values as ratio and in a form that allows the window content to change while we are holding on a scrollbar
static void Scrollbar ( ImGuiWindow * window )
{
ImGuiState & g = * GImGui ;
const ImGuiStyle & style = g . Style ;
const ImGuiID id = window - > GetID ( " #SCROLLY " ) ;
// Render background
2015-03-18 07:37:17 +08:00
ImRect bb ( window - > Rect ( ) . Max . x - style . ScrollbarWidth , window - > Pos . y + window - > TitleBarHeight ( ) + 1 , window - > Rect ( ) . Max . x , window - > Rect ( ) . Max . y - 1 ) ;
2015-03-09 00:37:03 +08:00
window - > DrawList - > AddRectFilled ( bb . Min , bb . Max , window - > Color ( ImGuiCol_ScrollbarBg ) ) ;
2015-03-18 21:07:37 +08:00
bb . Expand ( - 3.0f ) ;
2015-03-09 00:37:03 +08:00
const float scrollbar_height = bb . GetHeight ( ) ;
// The grabable box size generally represent the amount visible (vs the total scrollable amount)
// But we maintain a minimum size in pixel to allow for the user to still aim inside.
2015-04-03 05:38:37 +08:00
const float grab_h_pixels = ImMin ( ImMax ( scrollbar_height * ImSaturate ( window - > Size . y / ImMax ( window - > SizeContents . y , window - > Size . y ) ) , style . GrabMinSize ) , scrollbar_height ) ;
2015-03-09 00:37:03 +08:00
const float grab_h_norm = grab_h_pixels / scrollbar_height ;
// Handle input right away. None of the code of Begin() is relying on scrolling position before calling Scrollbar().
bool held = false ;
bool hovered = false ;
const bool previously_held = ( g . ActiveId = = id ) ;
2015-08-02 11:57:24 +08:00
ImGui : : ButtonBehavior ( bb , id , & hovered , & held , true ) ;
2015-03-09 00:37:03 +08:00
const float scroll_max = ImMax ( 1.0f , window - > SizeContents . y - window - > Size . y ) ;
float scroll_ratio = ImSaturate ( window - > ScrollY / scroll_max ) ;
float grab_y_norm = scroll_ratio * ( scrollbar_height - grab_h_pixels ) / scrollbar_height ;
if ( held )
{
const float clicked_y_norm = ImSaturate ( ( g . IO . MousePos . y - bb . Min . y ) / scrollbar_height ) ; // Click position in scrollbar space (0.0f->1.0f)
g . HoveredId = id ;
bool seek_absolute = false ;
if ( ! previously_held )
{
// On initial click calculate the distance between mouse and the center of the grab
if ( clicked_y_norm > = grab_y_norm & & clicked_y_norm < = grab_y_norm + grab_h_norm )
{
g . ScrollbarClickDeltaToGrabCenter = clicked_y_norm - grab_y_norm - grab_h_norm * 0.5f ;
}
else
{
seek_absolute = true ;
g . ScrollbarClickDeltaToGrabCenter = 0 ;
}
}
// Apply scroll
2015-06-20 08:01:13 +08:00
// It is ok to modify ScrollY here because we are being called in Begin() after the calculation of SizeContents and before setting up our starting position
2015-03-09 00:37:03 +08:00
const float scroll_y_norm = ImSaturate ( ( clicked_y_norm - g . ScrollbarClickDeltaToGrabCenter - grab_h_norm * 0.5f ) / ( 1.0f - grab_h_norm ) ) ;
window - > ScrollY = ( float ) ( int ) ( 0.5f + scroll_y_norm * ( window - > SizeContents . y - window - > Size . y ) ) ;
// Update values for rendering
scroll_ratio = ImSaturate ( window - > ScrollY / scroll_max ) ;
grab_y_norm = scroll_ratio * ( scrollbar_height - grab_h_pixels ) / scrollbar_height ;
// Update distance to grab now that we have seeked and saturated
if ( seek_absolute )
g . ScrollbarClickDeltaToGrabCenter = clicked_y_norm - grab_y_norm - grab_h_norm * 0.5f ;
}
// Render
const ImU32 grab_col = window - > Color ( held ? ImGuiCol_ScrollbarGrabActive : hovered ? ImGuiCol_ScrollbarGrabHovered : ImGuiCol_ScrollbarGrab ) ;
2015-05-09 17:13:56 +08:00
window - > DrawList - > AddRectFilled ( ImVec2 ( bb . Min . x , ImLerp ( bb . Min . y , bb . Max . y , grab_y_norm ) ) , ImVec2 ( bb . Max . x , ImLerp ( bb . Min . y , bb . Max . y , grab_y_norm ) + grab_h_pixels ) , grab_col , style . ScrollbarRounding ) ;
2015-03-09 00:37:03 +08:00
}
2015-01-05 02:23:30 +08:00
// Moving window to front of display (which happens to be back of our sorted list)
2015-08-02 11:57:24 +08:00
void ImGui : : FocusWindow ( ImGuiWindow * window )
2014-08-11 04:58:11 +08:00
{
2015-02-01 12:28:24 +08:00
ImGuiState & g = * GImGui ;
2015-02-28 17:54:14 +08:00
2015-02-28 18:25:04 +08:00
// Always mark the window we passed as focused. This is used for keyboard interactions such as tabbing.
g . FocusedWindow = window ;
2015-04-06 18:19:31 +08:00
// Passing NULL allow to disable keyboard focus
if ( ! window )
return ;
2015-02-28 18:25:04 +08:00
// And move its root window to the top of the pile
2015-02-28 17:54:14 +08:00
if ( window - > RootWindow )
window = window - > RootWindow ;
2015-05-28 05:58:49 +08:00
// Steal focus on active widgets
if ( window - > Flags & ImGuiWindowFlags_Popup ) // FIXME: This statement should be unnecessary. Need further testing before removing it..
if ( g . ActiveId ! = 0 & & g . ActiveIdWindow & & g . ActiveIdWindow - > RootWindow ! = window )
2015-08-02 11:57:24 +08:00
ImGui : : SetActiveID ( 0 ) ;
2015-05-28 05:58:49 +08:00
2015-01-05 02:23:30 +08:00
if ( g . Windows . back ( ) = = window )
return ;
2015-07-08 02:39:08 +08:00
for ( int i = 0 ; i < g . Windows . Size ; i + + )
2014-08-19 19:09:13 +08:00
if ( g . Windows [ i ] = = window )
{
g . Windows . erase ( g . Windows . begin ( ) + i ) ;
break ;
}
g . Windows . push_back ( window ) ;
2014-08-11 04:58:11 +08:00
}
2014-11-10 09:29:01 +08:00
void ImGui : : PushItemWidth ( float item_width )
2014-08-11 04:58:11 +08:00
{
2014-08-19 19:09:13 +08:00
ImGuiWindow * window = GetCurrentWindow ( ) ;
2015-05-27 22:35:49 +08:00
window - > DC . ItemWidth = ( item_width = = 0.0f ? window - > ItemWidthDefault : item_width ) ;
window - > DC . ItemWidthStack . push_back ( window - > DC . ItemWidth ) ;
2014-08-11 04:58:11 +08:00
}
2015-07-22 07:00:28 +08:00
static void PushMultiItemsWidths ( int components , float w_full )
2015-05-12 04:44:50 +08:00
{
2015-08-02 11:57:24 +08:00
ImGuiWindow * window = ImGui : : GetCurrentWindow ( ) ;
2015-05-12 04:44:50 +08:00
const ImGuiStyle & style = GImGui - > Style ;
if ( w_full < = 0.0f )
w_full = ImGui : : CalcItemWidth ( ) ;
const float w_item_one = ImMax ( 1.0f , ( float ) ( int ) ( ( w_full - ( style . FramePadding . x * 2.0f + style . ItemInnerSpacing . x ) * ( components - 1 ) ) / ( float ) components ) ) ;
const float w_item_last = ImMax ( 1.0f , ( float ) ( int ) ( w_full - ( w_item_one + style . FramePadding . x * 2.0f + style . ItemInnerSpacing . x ) * ( components - 1 ) ) ) ;
2015-05-27 22:35:49 +08:00
window - > DC . ItemWidthStack . push_back ( w_item_last ) ;
2015-05-12 04:44:50 +08:00
for ( int i = 0 ; i < components - 1 ; i + + )
2015-05-27 22:35:49 +08:00
window - > DC . ItemWidthStack . push_back ( w_item_one ) ;
window - > DC . ItemWidth = window - > DC . ItemWidthStack . back ( ) ;
2015-05-12 04:44:50 +08:00
}
2014-11-10 09:29:01 +08:00
void ImGui : : PopItemWidth ( )
2014-08-11 04:58:11 +08:00
{
2014-08-19 19:09:13 +08:00
ImGuiWindow * window = GetCurrentWindow ( ) ;
2015-05-27 22:35:49 +08:00
window - > DC . ItemWidthStack . pop_back ( ) ;
2015-05-27 22:42:29 +08:00
window - > DC . ItemWidth = window - > DC . ItemWidthStack . empty ( ) ? window - > ItemWidthDefault : window - > DC . ItemWidthStack . back ( ) ;
2014-08-11 04:58:11 +08:00
}
2015-02-11 05:07:26 +08:00
float ImGui : : CalcItemWidth ( )
2014-08-12 07:04:13 +08:00
{
2014-08-19 19:09:13 +08:00
ImGuiWindow * window = GetCurrentWindow ( ) ;
2015-05-27 22:35:49 +08:00
float w = window - > DC . ItemWidth ;
2015-02-11 05:07:26 +08:00
if ( w < 0.0f )
{
2015-02-11 05:42:21 +08:00
// Align to a right-side limit. We include 1 frame padding in the calculation because this is how the width is always used (we add 2 frame padding to it), but we could move that responsibility to the widget as well.
ImGuiState & g = * GImGui ;
2015-08-06 00:43:12 +08:00
float width_to_right_edge = ImGui : : GetContentRegionAvail ( ) . x ;
2015-05-12 04:44:50 +08:00
w = ImMax ( 1.0f , width_to_right_edge + w - g . Style . FramePadding . x * 2.0f ) ;
2015-02-11 05:07:26 +08:00
}
2015-02-11 05:42:21 +08:00
w = ( float ) ( int ) w ;
2015-02-11 05:07:26 +08:00
return w ;
2014-08-12 07:04:13 +08:00
}
2015-07-22 07:00:28 +08:00
static void SetCurrentFont ( ImFont * font )
2015-01-17 21:40:56 +08:00
{
2015-02-01 12:28:24 +08:00
ImGuiState & g = * GImGui ;
2015-01-18 06:15:29 +08:00
IM_ASSERT ( font & & font - > IsLoaded ( ) ) ;
2015-01-17 21:40:56 +08:00
IM_ASSERT ( font - > Scale > 0.0f ) ;
g . Font = font ;
2015-03-30 02:45:03 +08:00
g . FontBaseSize = g . IO . FontGlobalScale * g . Font - > FontSize * g . Font - > Scale ;
g . FontSize = g . CurrentWindow ? g . CurrentWindow - > CalcFontSize ( ) : 0.0f ;
2015-01-18 06:15:29 +08:00
g . FontTexUvWhitePixel = g . Font - > ContainerAtlas - > TexUvWhitePixel ;
2015-01-17 21:40:56 +08:00
}
void ImGui : : PushFont ( ImFont * font )
{
2015-02-01 12:28:24 +08:00
ImGuiState & g = * GImGui ;
2015-01-18 06:15:29 +08:00
if ( ! font )
2015-01-18 18:46:49 +08:00
font = g . IO . Fonts - > Fonts [ 0 ] ;
2015-07-22 07:00:28 +08:00
SetCurrentFont ( font ) ;
2015-01-17 22:13:08 +08:00
g . FontStack . push_back ( font ) ;
2015-01-18 06:15:29 +08:00
g . CurrentWindow - > DrawList - > PushTextureID ( font - > ContainerAtlas - > TexID ) ;
2015-01-17 21:40:56 +08:00
}
void ImGui : : PopFont ( )
{
2015-02-01 12:28:24 +08:00
ImGuiState & g = * GImGui ;
2015-01-17 22:13:08 +08:00
g . CurrentWindow - > DrawList - > PopTextureID ( ) ;
2015-01-17 21:40:56 +08:00
g . FontStack . pop_back ( ) ;
2015-07-22 07:00:28 +08:00
SetCurrentFont ( g . FontStack . empty ( ) ? g . IO . Fonts - > Fonts [ 0 ] : g . FontStack . back ( ) ) ;
2015-01-17 21:40:56 +08:00
}
2014-11-10 09:29:01 +08:00
void ImGui : : PushAllowKeyboardFocus ( bool allow_keyboard_focus )
2014-08-11 04:58:11 +08:00
{
2014-08-19 19:09:13 +08:00
ImGuiWindow * window = GetCurrentWindow ( ) ;
2015-05-27 22:35:49 +08:00
window - > DC . AllowKeyboardFocus = allow_keyboard_focus ;
window - > DC . AllowKeyboardFocusStack . push_back ( allow_keyboard_focus ) ;
2014-08-11 04:58:11 +08:00
}
2014-11-10 09:29:01 +08:00
void ImGui : : PopAllowKeyboardFocus ( )
2014-08-11 04:58:11 +08:00
{
2014-08-19 19:09:13 +08:00
ImGuiWindow * window = GetCurrentWindow ( ) ;
2015-05-27 22:35:49 +08:00
window - > DC . AllowKeyboardFocusStack . pop_back ( ) ;
window - > DC . AllowKeyboardFocus = window - > DC . AllowKeyboardFocusStack . empty ( ) ? true : window - > DC . AllowKeyboardFocusStack . back ( ) ;
2014-08-11 04:58:11 +08:00
}
2015-05-27 22:22:09 +08:00
void ImGui : : PushButtonRepeat ( bool repeat )
{
ImGuiWindow * window = GetCurrentWindow ( ) ;
window - > DC . ButtonRepeat = repeat ;
window - > DC . ButtonRepeatStack . push_back ( repeat ) ;
}
void ImGui : : PopButtonRepeat ( )
{
ImGuiWindow * window = GetCurrentWindow ( ) ;
window - > DC . ButtonRepeatStack . pop_back ( ) ;
window - > DC . ButtonRepeat = window - > DC . ButtonRepeatStack . empty ( ) ? false : window - > DC . ButtonRepeatStack . back ( ) ;
2014-08-11 04:58:11 +08:00
}
2015-05-27 22:35:49 +08:00
void ImGui : : PushTextWrapPos ( float wrap_pos_x )
2014-11-07 13:45:56 +08:00
{
ImGuiWindow * window = GetCurrentWindow ( ) ;
2015-05-27 22:35:49 +08:00
window - > DC . TextWrapPos = wrap_pos_x ;
window - > DC . TextWrapPosStack . push_back ( wrap_pos_x ) ;
2014-11-07 13:45:56 +08:00
}
2014-11-10 09:29:01 +08:00
void ImGui : : PopTextWrapPos ( )
2014-11-07 13:45:56 +08:00
{
ImGuiWindow * window = GetCurrentWindow ( ) ;
2015-05-27 22:35:49 +08:00
window - > DC . TextWrapPosStack . pop_back ( ) ;
2015-05-27 22:44:07 +08:00
window - > DC . TextWrapPos = window - > DC . TextWrapPosStack . empty ( ) ? - 1.0f : window - > DC . TextWrapPosStack . back ( ) ;
2014-11-07 13:45:56 +08:00
}
2014-11-10 09:29:01 +08:00
void ImGui : : PushStyleColor ( ImGuiCol idx , const ImVec4 & col )
2014-08-11 04:58:11 +08:00
{
2015-02-01 12:28:24 +08:00
ImGuiState & g = * GImGui ;
2014-08-19 19:09:13 +08:00
ImGuiColMod backup ;
backup . Col = idx ;
backup . PreviousValue = g . Style . Colors [ idx ] ;
2014-11-21 21:23:22 +08:00
g . ColorModifiers . push_back ( backup ) ;
2014-08-19 19:09:13 +08:00
g . Style . Colors [ idx ] = col ;
2014-08-11 04:58:11 +08:00
}
2014-11-21 21:23:22 +08:00
void ImGui : : PopStyleColor ( int count )
2014-08-11 04:58:11 +08:00
{
2015-02-01 12:28:24 +08:00
ImGuiState & g = * GImGui ;
2014-11-21 21:23:22 +08:00
while ( count > 0 )
{
ImGuiColMod & backup = g . ColorModifiers . back ( ) ;
g . Style . Colors [ backup . Col ] = backup . PreviousValue ;
g . ColorModifiers . pop_back ( ) ;
count - - ;
}
2014-08-11 04:58:11 +08:00
}
2014-11-07 16:22:38 +08:00
static float * GetStyleVarFloatAddr ( ImGuiStyleVar idx )
{
2015-02-01 12:28:24 +08:00
ImGuiState & g = * GImGui ;
2014-11-07 16:22:38 +08:00
switch ( idx )
{
case ImGuiStyleVar_Alpha : return & g . Style . Alpha ;
2014-12-19 20:56:00 +08:00
case ImGuiStyleVar_WindowRounding : return & g . Style . WindowRounding ;
2015-02-10 21:00:22 +08:00
case ImGuiStyleVar_ChildWindowRounding : return & g . Style . ChildWindowRounding ;
2015-01-15 17:14:44 +08:00
case ImGuiStyleVar_FrameRounding : return & g . Style . FrameRounding ;
2015-03-15 19:10:48 +08:00
case ImGuiStyleVar_IndentSpacing : return & g . Style . IndentSpacing ;
2015-03-15 09:23:56 +08:00
case ImGuiStyleVar_GrabMinSize : return & g . Style . GrabMinSize ;
2014-11-07 16:22:38 +08:00
}
return NULL ;
}
static ImVec2 * GetStyleVarVec2Addr ( ImGuiStyleVar idx )
{
2015-02-01 12:28:24 +08:00
ImGuiState & g = * GImGui ;
2014-11-07 16:22:38 +08:00
switch ( idx )
{
case ImGuiStyleVar_WindowPadding : return & g . Style . WindowPadding ;
2015-05-23 20:43:45 +08:00
case ImGuiStyleVar_WindowMinSize : return & g . Style . WindowMinSize ;
2014-11-07 16:22:38 +08:00
case ImGuiStyleVar_FramePadding : return & g . Style . FramePadding ;
case ImGuiStyleVar_ItemSpacing : return & g . Style . ItemSpacing ;
case ImGuiStyleVar_ItemInnerSpacing : return & g . Style . ItemInnerSpacing ;
}
return NULL ;
}
2014-11-10 09:29:01 +08:00
void ImGui : : PushStyleVar ( ImGuiStyleVar idx , float val )
2014-11-07 16:22:38 +08:00
{
2015-02-01 12:28:24 +08:00
ImGuiState & g = * GImGui ;
2014-11-07 16:22:38 +08:00
float * pvar = GetStyleVarFloatAddr ( idx ) ;
2015-01-17 20:27:27 +08:00
IM_ASSERT ( pvar ! = NULL ) ; // Called function with wrong-type? Variable is not a float.
2014-11-07 16:22:38 +08:00
ImGuiStyleMod backup ;
backup . Var = idx ;
backup . PreviousValue = ImVec2 ( * pvar , 0.0f ) ;
2014-11-21 21:23:22 +08:00
g . StyleModifiers . push_back ( backup ) ;
2014-11-07 16:22:38 +08:00
* pvar = val ;
}
2014-11-10 09:29:01 +08:00
void ImGui : : PushStyleVar ( ImGuiStyleVar idx , const ImVec2 & val )
2014-11-07 16:22:38 +08:00
{
2015-02-01 12:28:24 +08:00
ImGuiState & g = * GImGui ;
2014-11-07 16:22:38 +08:00
ImVec2 * pvar = GetStyleVarVec2Addr ( idx ) ;
2015-03-07 05:39:50 +08:00
IM_ASSERT ( pvar ! = NULL ) ; // Called function with wrong-type? Variable is not a ImVec2.
2014-11-07 16:22:38 +08:00
ImGuiStyleMod backup ;
backup . Var = idx ;
backup . PreviousValue = * pvar ;
2014-11-21 21:23:22 +08:00
g . StyleModifiers . push_back ( backup ) ;
2014-11-07 16:22:38 +08:00
* pvar = val ;
}
2014-11-21 21:23:22 +08:00
void ImGui : : PopStyleVar ( int count )
2014-11-07 16:22:38 +08:00
{
2015-02-01 12:28:24 +08:00
ImGuiState & g = * GImGui ;
2014-11-21 21:23:22 +08:00
while ( count > 0 )
{
ImGuiStyleMod & backup = g . StyleModifiers . back ( ) ;
if ( float * pvar_f = GetStyleVarFloatAddr ( backup . Var ) )
* pvar_f = backup . PreviousValue . x ;
else if ( ImVec2 * pvar_v = GetStyleVarVec2Addr ( backup . Var ) )
* pvar_v = backup . PreviousValue ;
g . StyleModifiers . pop_back ( ) ;
2014-11-22 00:29:56 +08:00
count - - ;
2014-11-21 21:23:22 +08:00
}
2014-11-07 16:22:38 +08:00
}
2014-12-30 03:56:01 +08:00
const char * ImGui : : GetStyleColName ( ImGuiCol idx )
2014-08-11 04:58:11 +08:00
{
2014-09-02 21:35:06 +08:00
// Create switch-case from enum with regexp: ImGuiCol_{.*}, --> case ImGuiCol_\1: return "\1";
2014-08-19 19:09:13 +08:00
switch ( idx )
{
case ImGuiCol_Text : return " Text " ;
2015-05-12 02:36:32 +08:00
case ImGuiCol_TextDisabled : return " TextDisabled " ;
2014-08-19 19:09:13 +08:00
case ImGuiCol_WindowBg : return " WindowBg " ;
2015-02-10 21:00:22 +08:00
case ImGuiCol_ChildWindowBg : return " ChildWindowBg " ;
2014-08-19 19:09:13 +08:00
case ImGuiCol_Border : return " Border " ;
case ImGuiCol_BorderShadow : return " BorderShadow " ;
case ImGuiCol_FrameBg : return " FrameBg " ;
2015-04-03 21:07:52 +08:00
case ImGuiCol_FrameBgHovered : return " FrameBgHovered " ;
case ImGuiCol_FrameBgActive : return " FrameBgActive " ;
2014-08-19 19:09:13 +08:00
case ImGuiCol_TitleBg : return " TitleBg " ;
case ImGuiCol_TitleBgCollapsed : return " TitleBgCollapsed " ;
2015-07-01 03:40:13 +08:00
case ImGuiCol_TitleBgActive : return " TitleBgActive " ;
2015-05-18 00:13:45 +08:00
case ImGuiCol_MenuBarBg : return " MenuBarBg " ;
2014-08-19 19:09:13 +08:00
case ImGuiCol_ScrollbarBg : return " ScrollbarBg " ;
case ImGuiCol_ScrollbarGrab : return " ScrollbarGrab " ;
case ImGuiCol_ScrollbarGrabHovered : return " ScrollbarGrabHovered " ;
case ImGuiCol_ScrollbarGrabActive : return " ScrollbarGrabActive " ;
case ImGuiCol_ComboBg : return " ComboBg " ;
2015-01-10 01:19:24 +08:00
case ImGuiCol_CheckMark : return " CheckMark " ;
2014-08-19 19:09:13 +08:00
case ImGuiCol_SliderGrab : return " SliderGrab " ;
case ImGuiCol_SliderGrabActive : return " SliderGrabActive " ;
case ImGuiCol_Button : return " Button " ;
case ImGuiCol_ButtonHovered : return " ButtonHovered " ;
case ImGuiCol_ButtonActive : return " ButtonActive " ;
case ImGuiCol_Header : return " Header " ;
case ImGuiCol_HeaderHovered : return " HeaderHovered " ;
case ImGuiCol_HeaderActive : return " HeaderActive " ;
case ImGuiCol_Column : return " Column " ;
case ImGuiCol_ColumnHovered : return " ColumnHovered " ;
case ImGuiCol_ColumnActive : return " ColumnActive " ;
case ImGuiCol_ResizeGrip : return " ResizeGrip " ;
case ImGuiCol_ResizeGripHovered : return " ResizeGripHovered " ;
case ImGuiCol_ResizeGripActive : return " ResizeGripActive " ;
case ImGuiCol_CloseButton : return " CloseButton " ;
case ImGuiCol_CloseButtonHovered : return " CloseButtonHovered " ;
case ImGuiCol_CloseButtonActive : return " CloseButtonActive " ;
case ImGuiCol_PlotLines : return " PlotLines " ;
case ImGuiCol_PlotLinesHovered : return " PlotLinesHovered " ;
case ImGuiCol_PlotHistogram : return " PlotHistogram " ;
2014-08-21 00:42:53 +08:00
case ImGuiCol_PlotHistogramHovered : return " PlotHistogramHovered " ;
2014-08-19 19:09:13 +08:00
case ImGuiCol_TextSelectedBg : return " TextSelectedBg " ;
case ImGuiCol_TooltipBg : return " TooltipBg " ;
2015-06-24 02:34:15 +08:00
case ImGuiCol_ModalWindowDarkening : return " ModalWindowDarkening " ;
2014-08-19 19:09:13 +08:00
}
IM_ASSERT ( 0 ) ;
return " Unknown " ;
2014-08-11 04:58:11 +08:00
}
2015-06-03 03:57:29 +08:00
bool ImGui : : IsWindowHovered ( )
{
ImGuiState & g = * GImGui ;
return g . HoveredWindow = = g . CurrentWindow ;
}
2015-03-13 20:25:43 +08:00
bool ImGui : : IsWindowFocused ( )
2014-08-11 04:58:11 +08:00
{
2015-02-01 12:28:24 +08:00
ImGuiState & g = * GImGui ;
2015-06-03 03:57:29 +08:00
return g . FocusedWindow = = g . CurrentWindow ;
2014-08-11 04:58:11 +08:00
}
2015-03-13 20:25:43 +08:00
bool ImGui : : IsRootWindowFocused ( )
{
ImGuiState & g = * GImGui ;
2015-06-03 03:57:29 +08:00
ImGuiWindow * root_window = g . CurrentWindow - > RootWindow ;
2015-03-13 20:25:43 +08:00
return g . FocusedWindow = = root_window ;
}
bool ImGui : : IsRootWindowOrAnyChildFocused ( )
{
ImGuiState & g = * GImGui ;
2015-06-03 03:57:29 +08:00
ImGuiWindow * root_window = g . CurrentWindow - > RootWindow ;
2015-04-07 21:04:43 +08:00
return g . FocusedWindow & & g . FocusedWindow - > RootWindow = = root_window ;
2015-03-13 20:25:43 +08:00
}
2014-11-10 09:29:01 +08:00
float ImGui : : GetWindowWidth ( )
2014-08-11 04:58:11 +08:00
{
2015-07-03 10:53:45 +08:00
ImGuiWindow * window = GImGui - > CurrentWindow ;
2014-08-19 19:09:13 +08:00
return window - > Size . x ;
2014-08-11 04:58:11 +08:00
}
2014-11-10 09:29:01 +08:00
ImVec2 ImGui : : GetWindowPos ( )
2014-08-11 04:58:11 +08:00
{
2015-06-03 03:57:29 +08:00
ImGuiState & g = * GImGui ;
ImGuiWindow * window = g . CurrentWindow ;
2014-08-19 19:09:13 +08:00
return window - > Pos ;
2014-08-11 04:58:11 +08:00
}
2015-06-20 08:01:13 +08:00
static void SetWindowScrollY ( ImGuiWindow * window , float new_scroll_y )
{
window - > DC . CursorMaxPos . y + = window - > ScrollY ;
window - > ScrollY = new_scroll_y ;
window - > DC . CursorMaxPos . y - = window - > ScrollY ;
}
2015-02-27 17:51:11 +08:00
static void SetWindowPos ( ImGuiWindow * window , const ImVec2 & pos , ImGuiSetCond cond )
2014-08-11 04:58:11 +08:00
{
2014-12-11 03:22:30 +08:00
// Test condition (NB: bit 0 is always true) and clear flags for next time
if ( cond & & ( window - > SetWindowPosAllowFlags & cond ) = = 0 )
return ;
2015-05-08 01:39:23 +08:00
window - > SetWindowPosAllowFlags & = ~ ( ImGuiSetCond_Once | ImGuiSetCond_FirstUseEver | ImGuiSetCond_Appearing ) ;
2015-06-24 05:45:34 +08:00
window - > SetWindowPosCenterWanted = false ;
2014-12-11 03:22:30 +08:00
// Set
2014-08-19 19:09:13 +08:00
const ImVec2 old_pos = window - > Pos ;
window - > PosFloat = pos ;
window - > Pos = ImVec2 ( ( float ) ( int ) window - > PosFloat . x , ( float ) ( int ) window - > PosFloat . y ) ;
2015-03-23 00:08:42 +08:00
window - > DC . CursorPos + = ( window - > Pos - old_pos ) ; // As we happen to move the window while it is being appended to (which is a bad idea - will smear) let's at least offset the cursor
window - > DC . CursorMaxPos + = ( window - > Pos - old_pos ) ; // And more importantly we need to adjust this so size calculation doesn't get affected.
2014-08-11 04:58:11 +08:00
}
2015-02-27 17:51:11 +08:00
void ImGui : : SetWindowPos ( const ImVec2 & pos , ImGuiSetCond cond )
2014-08-11 04:58:11 +08:00
{
2014-08-19 19:09:13 +08:00
ImGuiWindow * window = GetCurrentWindow ( ) ;
2015-02-27 17:07:25 +08:00
SetWindowPos ( window , pos , cond ) ;
2014-08-11 04:58:11 +08:00
}
2015-02-27 17:51:11 +08:00
void ImGui : : SetWindowPos ( const char * name , const ImVec2 & pos , ImGuiSetCond cond )
2015-02-27 17:07:25 +08:00
{
ImGuiWindow * window = FindWindowByName ( name ) ;
if ( window )
SetWindowPos ( window , pos , cond ) ;
}
ImVec2 ImGui : : GetWindowSize ( )
2014-10-27 04:07:15 +08:00
{
ImGuiWindow * window = GetCurrentWindow ( ) ;
2015-02-27 17:07:25 +08:00
return window - > Size ;
}
2014-12-11 03:22:30 +08:00
2015-02-27 17:51:11 +08:00
static void SetWindowSize ( ImGuiWindow * window , const ImVec2 & size , ImGuiSetCond cond )
2015-02-27 17:07:25 +08:00
{
2014-12-11 03:22:30 +08:00
// Test condition (NB: bit 0 is always true) and clear flags for next time
if ( cond & & ( window - > SetWindowSizeAllowFlags & cond ) = = 0 )
return ;
2015-05-08 01:39:23 +08:00
window - > SetWindowSizeAllowFlags & = ~ ( ImGuiSetCond_Once | ImGuiSetCond_FirstUseEver | ImGuiSetCond_Appearing ) ;
2014-12-11 03:22:30 +08:00
// Set
2015-06-25 10:08:08 +08:00
if ( size . x > 0.0f )
2014-11-22 00:29:56 +08:00
{
2015-06-25 10:08:08 +08:00
window - > AutoFitFramesX = 0 ;
window - > SizeFull . x = size . x ;
2014-11-22 00:29:56 +08:00
}
else
{
2015-06-25 10:08:08 +08:00
window - > AutoFitFramesX = 2 ;
window - > AutoFitOnlyGrows = false ;
}
if ( size . y > 0.0f )
2014-11-22 00:29:56 +08:00
{
2015-06-25 10:08:08 +08:00
window - > AutoFitFramesY = 0 ;
window - > SizeFull . y = size . y ;
2014-11-22 00:29:56 +08:00
}
else
{
2015-06-25 10:08:08 +08:00
window - > AutoFitFramesY = 2 ;
2014-12-29 19:23:03 +08:00
window - > AutoFitOnlyGrows = false ;
2014-11-22 00:29:56 +08:00
}
2014-10-27 04:07:15 +08:00
}
2015-02-27 17:51:11 +08:00
void ImGui : : SetWindowSize ( const ImVec2 & size , ImGuiSetCond cond )
2014-12-11 03:22:30 +08:00
{
ImGuiWindow * window = GetCurrentWindow ( ) ;
2015-02-27 17:07:25 +08:00
SetWindowSize ( window , size , cond ) ;
}
2014-12-11 03:22:30 +08:00
2015-02-27 17:51:11 +08:00
void ImGui : : SetWindowSize ( const char * name , const ImVec2 & size , ImGuiSetCond cond )
2015-02-27 17:07:25 +08:00
{
ImGuiWindow * window = FindWindowByName ( name ) ;
if ( window )
SetWindowSize ( window , size , cond ) ;
}
2015-02-27 17:51:11 +08:00
static void SetWindowCollapsed ( ImGuiWindow * window , bool collapsed , ImGuiSetCond cond )
2015-02-27 17:07:25 +08:00
{
2014-12-11 03:22:30 +08:00
// Test condition (NB: bit 0 is always true) and clear flags for next time
if ( cond & & ( window - > SetWindowCollapsedAllowFlags & cond ) = = 0 )
return ;
2015-05-08 01:39:23 +08:00
window - > SetWindowCollapsedAllowFlags & = ~ ( ImGuiSetCond_Once | ImGuiSetCond_FirstUseEver | ImGuiSetCond_Appearing ) ;
2014-12-11 03:22:30 +08:00
// Set
window - > Collapsed = collapsed ;
}
2015-02-27 17:51:11 +08:00
void ImGui : : SetWindowCollapsed ( bool collapsed , ImGuiSetCond cond )
2015-02-27 17:07:25 +08:00
{
ImGuiWindow * window = GetCurrentWindow ( ) ;
2015-02-27 17:13:23 +08:00
SetWindowCollapsed ( window , collapsed , cond ) ;
2015-02-27 17:07:25 +08:00
}
2015-05-31 18:52:57 +08:00
bool ImGui : : IsWindowCollapsed ( )
2015-03-01 04:47:59 +08:00
{
2015-05-31 18:52:57 +08:00
return GImGui - > CurrentWindow - > Collapsed ;
2015-03-01 04:47:59 +08:00
}
2015-02-27 17:51:11 +08:00
void ImGui : : SetWindowCollapsed ( const char * name , bool collapsed , ImGuiSetCond cond )
2015-02-27 17:07:25 +08:00
{
ImGuiWindow * window = FindWindowByName ( name ) ;
if ( window )
2015-02-27 17:13:23 +08:00
SetWindowCollapsed ( window , collapsed , cond ) ;
2015-02-27 17:07:25 +08:00
}
2015-02-27 17:01:12 +08:00
void ImGui : : SetWindowFocus ( )
{
ImGuiWindow * window = GetCurrentWindow ( ) ;
FocusWindow ( window ) ;
}
void ImGui : : SetWindowFocus ( const char * name )
{
2015-04-06 18:19:31 +08:00
if ( name )
{
ImGuiWindow * window = FindWindowByName ( name ) ;
if ( window )
FocusWindow ( window ) ;
}
else
{
FocusWindow ( NULL ) ;
}
2015-02-27 17:01:12 +08:00
}
2015-02-27 17:51:11 +08:00
void ImGui : : SetNextWindowPos ( const ImVec2 & pos , ImGuiSetCond cond )
2014-12-11 03:22:30 +08:00
{
2015-02-01 12:28:24 +08:00
ImGuiState & g = * GImGui ;
2014-12-11 03:22:30 +08:00
g . SetNextWindowPosVal = pos ;
2015-02-27 17:51:11 +08:00
g . SetNextWindowPosCond = cond ? cond : ImGuiSetCond_Always ;
2014-12-11 03:22:30 +08:00
}
2015-06-24 05:34:44 +08:00
void ImGui : : SetNextWindowPosCenter ( ImGuiSetCond cond )
{
ImGuiState & g = * GImGui ;
g . SetNextWindowPosVal = ImVec2 ( - FLT_MAX , - FLT_MAX ) ;
g . SetNextWindowPosCond = cond ? cond : ImGuiSetCond_Always ;
2014-12-11 03:22:30 +08:00
}
2015-02-27 17:51:11 +08:00
void ImGui : : SetNextWindowSize ( const ImVec2 & size , ImGuiSetCond cond )
2014-12-11 03:22:30 +08:00
{
2015-02-01 12:28:24 +08:00
ImGuiState & g = * GImGui ;
2014-12-11 03:22:30 +08:00
g . SetNextWindowSizeVal = size ;
2015-02-27 17:51:11 +08:00
g . SetNextWindowSizeCond = cond ? cond : ImGuiSetCond_Always ;
2014-12-11 03:22:30 +08:00
}
2015-02-27 17:51:11 +08:00
void ImGui : : SetNextWindowCollapsed ( bool collapsed , ImGuiSetCond cond )
2014-12-11 03:22:30 +08:00
{
2015-02-01 12:28:24 +08:00
ImGuiState & g = * GImGui ;
2014-12-11 03:22:30 +08:00
g . SetNextWindowCollapsedVal = collapsed ;
2015-02-27 17:51:11 +08:00
g . SetNextWindowCollapsedCond = cond ? cond : ImGuiSetCond_Always ;
2014-12-11 03:22:30 +08:00
}
2015-02-27 17:01:12 +08:00
void ImGui : : SetNextWindowFocus ( )
{
ImGuiState & g = * GImGui ;
g . SetNextWindowFocus = true ;
}
2015-08-06 00:43:12 +08:00
// In window space (not screen space!)
2014-11-10 09:29:01 +08:00
ImVec2 ImGui : : GetContentRegionMax ( )
2014-11-07 14:17:04 +08:00
{
2014-11-07 16:22:38 +08:00
ImGuiWindow * window = GetCurrentWindow ( ) ;
2015-07-02 08:55:42 +08:00
ImVec2 mx = window - > Size - window - > WindowPadding ;
2014-11-07 16:22:38 +08:00
if ( window - > DC . ColumnsCount ! = 1 )
2015-07-18 05:58:23 +08:00
mx . x = ImGui : : GetColumnOffset ( window - > DC . ColumnsCurrent + 1 ) - window - > WindowPadding . x ;
2014-11-07 16:22:38 +08:00
else
2015-05-25 06:37:47 +08:00
mx . x - = window - > ScrollbarWidth ( ) ;
2015-02-11 05:07:26 +08:00
return mx ;
2014-11-07 14:17:04 +08:00
}
2015-08-06 00:43:12 +08:00
inline ImVec2 ImGui : : GetContentRegionAvail ( )
{
ImGuiWindow * window = GetCurrentWindow ( ) ;
return GetContentRegionMax ( ) - ( window - > DC . CursorPos - window - > Pos ) ;
}
// In window space (not screen space!)
2014-11-10 09:29:01 +08:00
ImVec2 ImGui : : GetWindowContentRegionMin ( )
2014-08-11 04:58:11 +08:00
{
2014-08-19 19:09:13 +08:00
ImGuiWindow * window = GetCurrentWindow ( ) ;
2015-07-02 08:55:42 +08:00
return ImVec2 ( 0 , window - > TitleBarHeight ( ) + window - > MenuBarHeight ( ) ) + window - > WindowPadding ;
2014-08-11 04:58:11 +08:00
}
2014-11-10 09:29:01 +08:00
ImVec2 ImGui : : GetWindowContentRegionMax ( )
2014-08-11 04:58:11 +08:00
{
2014-08-19 19:09:13 +08:00
ImGuiWindow * window = GetCurrentWindow ( ) ;
2015-07-02 08:55:42 +08:00
ImVec2 m = window - > Size - window - > WindowPadding ;
2015-05-25 06:37:47 +08:00
m . x - = window - > ScrollbarWidth ( ) ;
2014-08-19 19:09:13 +08:00
return m ;
2014-08-11 04:58:11 +08:00
}
2014-11-10 09:29:01 +08:00
float ImGui : : GetTextLineHeight ( )
2014-08-11 04:58:11 +08:00
{
2015-03-30 02:45:03 +08:00
ImGuiState & g = * GImGui ;
return g . FontSize ;
2014-08-11 04:58:11 +08:00
}
2015-02-08 21:37:20 +08:00
float ImGui : : GetTextLineHeightWithSpacing ( )
2014-08-11 04:58:11 +08:00
{
2015-02-01 12:28:24 +08:00
ImGuiState & g = * GImGui ;
2015-03-30 02:45:03 +08:00
return g . FontSize + g . Style . ItemSpacing . y ;
2014-08-11 04:58:11 +08:00
}
2015-05-29 16:00:34 +08:00
float ImGui : : GetItemsLineHeightWithSpacing ( )
{
ImGuiState & g = * GImGui ;
return g . FontSize + g . Style . FramePadding . y * 2.0f + g . Style . ItemSpacing . y ;
}
2014-11-10 09:29:01 +08:00
ImDrawList * ImGui : : GetWindowDrawList ( )
2014-08-11 04:58:11 +08:00
{
2014-08-19 19:09:13 +08:00
ImGuiWindow * window = GetCurrentWindow ( ) ;
return window - > DrawList ;
2014-08-11 04:58:11 +08:00
}
2014-11-29 06:38:41 +08:00
ImFont * ImGui : : GetWindowFont ( )
2014-10-25 18:00:24 +08:00
{
2015-03-30 02:45:03 +08:00
ImGuiState & g = * GImGui ;
return g . Font ;
2014-10-25 18:00:24 +08:00
}
2014-11-10 09:29:01 +08:00
float ImGui : : GetWindowFontSize ( )
2014-10-25 18:00:24 +08:00
{
2015-03-30 02:45:03 +08:00
ImGuiState & g = * GImGui ;
return g . FontSize ;
2014-10-25 18:00:24 +08:00
}
2014-11-10 09:29:01 +08:00
void ImGui : : SetWindowFontScale ( float scale )
2014-08-11 04:58:11 +08:00
{
2015-03-30 02:45:03 +08:00
ImGuiState & g = * GImGui ;
2014-08-19 19:09:13 +08:00
ImGuiWindow * window = GetCurrentWindow ( ) ;
2014-09-24 21:03:42 +08:00
window - > FontWindowScale = scale ;
2015-03-30 02:45:03 +08:00
g . FontSize = window - > CalcFontSize ( ) ;
2014-08-11 04:58:11 +08:00
}
2015-07-03 01:26:35 +08:00
// User generally sees positions in window coordinates. Internally we store CursorPos in absolute screen coordinates because it is more convenient.
// Conversion happens as we pass the value to user, but it makes our naming convention confusing because GetCursorPos() == (DC.CursorPos - window.Pos). May want to rename 'DC.CursorPos'.
2014-11-10 09:29:01 +08:00
ImVec2 ImGui : : GetCursorPos ( )
2014-08-11 04:58:11 +08:00
{
2014-08-19 19:09:13 +08:00
ImGuiWindow * window = GetCurrentWindow ( ) ;
return window - > DC . CursorPos - window - > Pos ;
2014-08-11 04:58:11 +08:00
}
2015-02-08 21:37:20 +08:00
float ImGui : : GetCursorPosX ( )
{
return ImGui : : GetCursorPos ( ) . x ;
}
float ImGui : : GetCursorPosY ( )
{
return ImGui : : GetCursorPos ( ) . y ;
}
2014-11-10 09:29:01 +08:00
void ImGui : : SetCursorPos ( const ImVec2 & pos )
2014-08-11 04:58:11 +08:00
{
2014-08-19 19:09:13 +08:00
ImGuiWindow * window = GetCurrentWindow ( ) ;
window - > DC . CursorPos = window - > Pos + pos ;
2015-03-18 06:03:02 +08:00
window - > DC . CursorMaxPos = ImMax ( window - > DC . CursorMaxPos , window - > DC . CursorPos ) ;
2014-08-11 04:58:11 +08:00
}
2014-11-10 09:29:01 +08:00
void ImGui : : SetCursorPosX ( float x )
2014-08-29 20:36:31 +08:00
{
ImGuiWindow * window = GetCurrentWindow ( ) ;
window - > DC . CursorPos . x = window - > Pos . x + x ;
2015-03-18 06:03:02 +08:00
window - > DC . CursorMaxPos . x = ImMax ( window - > DC . CursorMaxPos . x , window - > DC . CursorPos . x ) ;
2014-08-29 20:36:31 +08:00
}
2014-11-10 09:29:01 +08:00
void ImGui : : SetCursorPosY ( float y )
2014-08-29 20:36:31 +08:00
{
ImGuiWindow * window = GetCurrentWindow ( ) ;
window - > DC . CursorPos . y = window - > Pos . y + y ;
2015-03-18 06:03:02 +08:00
window - > DC . CursorMaxPos . y = ImMax ( window - > DC . CursorMaxPos . y , window - > DC . CursorPos . y ) ;
2014-08-29 20:36:31 +08:00
}
2015-07-03 04:46:49 +08:00
ImVec2 ImGui : : GetCursorStartPos ( )
{
ImGuiWindow * window = GetCurrentWindow ( ) ;
return window - > DC . CursorStartPos - window - > Pos ;
}
2014-11-10 09:29:01 +08:00
ImVec2 ImGui : : GetCursorScreenPos ( )
2014-08-19 19:39:30 +08:00
{
2014-08-19 19:45:34 +08:00
ImGuiWindow * window = GetCurrentWindow ( ) ;
return window - > DC . CursorPos ;
2014-08-11 04:58:11 +08:00
}
2015-01-15 22:41:22 +08:00
void ImGui : : SetCursorScreenPos ( const ImVec2 & screen_pos )
{
ImGuiWindow * window = GetCurrentWindow ( ) ;
window - > DC . CursorPos = screen_pos ;
}
2015-07-03 10:47:04 +08:00
float ImGui : : GetScrollY ( )
2015-03-08 20:04:38 +08:00
{
ImGuiWindow * window = GetCurrentWindow ( ) ;
return window - > ScrollY ;
}
float ImGui : : GetScrollMaxY ( )
{
ImGuiWindow * window = GetCurrentWindow ( ) ;
return window - > SizeContents . y - window - > SizeFull . y ;
}
2015-07-03 10:53:45 +08:00
void ImGui : : SetScrollY ( float scroll_y )
{
ImGuiWindow * window = GetCurrentWindow ( ) ;
window - > ScrollTargetRelY = scroll_y + window - > TitleBarHeight ( ) ; // title bar height cancelled out when using ScrollTargetRelY
window - > ScrollTargetCenterRatioY = 0.0f ;
}
void ImGui : : SetScrollFromPosY ( float pos_y , float center_y_ratio )
2014-08-11 04:58:11 +08:00
{
2015-07-02 07:51:44 +08:00
// We store a target position so centering can occur on the next frame when we are guaranteed to have a known window size
2014-08-19 19:09:13 +08:00
ImGuiWindow * window = GetCurrentWindow ( ) ;
2015-07-03 10:53:45 +08:00
IM_ASSERT ( center_y_ratio > = 0.0f & & center_y_ratio < = 1.0f ) ;
window - > ScrollTargetRelY = ( float ) ( int ) ( pos_y + window - > ScrollY ) ;
2015-07-02 09:01:27 +08:00
if ( center_y_ratio < = 0.0f & & window - > ScrollTargetRelY < = window - > WindowPadding . y ) // Minor hack to make "scroll to top" take account of WindowPadding, else it would scroll to (WindowPadding.y - ItemSpacing.y)
window - > ScrollTargetRelY = 0.0f ;
2015-07-02 07:51:44 +08:00
window - > ScrollTargetCenterRatioY = center_y_ratio ;
2014-08-11 04:58:11 +08:00
}
2015-07-03 11:03:15 +08:00
void ImGui : : SetScrollHere ( float center_y_ratio )
2015-07-03 10:53:45 +08:00
{
ImGuiWindow * window = GetCurrentWindow ( ) ;
float target_y = window - > DC . CursorPosPrevLine . y + ( window - > DC . PrevLineHeight * center_y_ratio ) + ( GImGui - > Style . ItemSpacing . y * ( center_y_ratio - 0.5f ) * 2.0f ) ; // Precisely aim above, in the middle or below the last line.
ImGui : : SetScrollFromPosY ( target_y - window - > Pos . y , center_y_ratio ) ;
}
2014-11-10 09:29:01 +08:00
void ImGui : : SetKeyboardFocusHere ( int offset )
2014-09-30 18:57:37 +08:00
{
ImGuiWindow * window = GetCurrentWindow ( ) ;
2014-09-30 19:31:35 +08:00
window - > FocusIdxAllRequestNext = window - > FocusIdxAllCounter + 1 + offset ;
window - > FocusIdxTabRequestNext = IM_INT_MAX ;
2014-09-30 18:57:37 +08:00
}
2014-12-30 02:17:54 +08:00
void ImGui : : SetStateStorage ( ImGuiStorage * tree )
2014-08-11 04:58:11 +08:00
{
2014-08-19 19:09:13 +08:00
ImGuiWindow * window = GetCurrentWindow ( ) ;
window - > DC . StateStorage = tree ? tree : & window - > StateStorage ;
2014-08-11 04:58:11 +08:00
}
2014-12-30 02:17:54 +08:00
ImGuiStorage * ImGui : : GetStateStorage ( )
2014-08-13 02:57:46 +08:00
{
2014-08-19 19:09:13 +08:00
ImGuiWindow * window = GetCurrentWindow ( ) ;
return window - > DC . StateStorage ;
2014-08-13 02:57:46 +08:00
}
2014-11-10 09:29:01 +08:00
void ImGui : : TextV ( const char * fmt , va_list args )
2014-08-11 04:58:11 +08:00
{
2014-08-19 19:09:13 +08:00
ImGuiWindow * window = GetCurrentWindow ( ) ;
if ( window - > SkipItems )
return ;
2014-08-11 04:58:11 +08:00
2015-07-22 10:13:12 +08:00
ImGuiState & g = * GImGui ;
2015-03-16 17:54:28 +08:00
const char * text_end = g . TempBuffer + ImFormatStringV ( g . TempBuffer , IM_ARRAYSIZE ( g . TempBuffer ) , fmt , args ) ;
TextUnformatted ( g . TempBuffer , text_end ) ;
2014-08-11 04:58:11 +08:00
}
2014-11-10 09:29:01 +08:00
void ImGui : : Text ( const char * fmt , . . . )
2014-08-11 04:58:11 +08:00
{
2014-08-19 19:09:13 +08:00
va_list args ;
va_start ( args , fmt ) ;
TextV ( fmt , args ) ;
va_end ( args ) ;
2014-08-11 04:58:11 +08:00
}
2014-11-10 09:29:01 +08:00
void ImGui : : TextColoredV ( const ImVec4 & col , const char * fmt , va_list args )
2014-08-15 19:32:53 +08:00
{
2014-08-19 19:09:13 +08:00
ImGui : : PushStyleColor ( ImGuiCol_Text , col ) ;
2014-09-25 02:27:29 +08:00
TextV ( fmt , args ) ;
ImGui : : PopStyleColor ( ) ;
}
2014-11-10 09:29:01 +08:00
void ImGui : : TextColored ( const ImVec4 & col , const char * fmt , . . . )
2014-09-25 02:27:29 +08:00
{
2014-08-19 19:09:13 +08:00
va_list args ;
va_start ( args , fmt ) ;
2014-09-25 02:27:29 +08:00
TextColoredV ( col , fmt , args ) ;
2014-08-19 19:09:13 +08:00
va_end ( args ) ;
2014-08-15 19:32:53 +08:00
}
2015-05-27 04:10:16 +08:00
void ImGui : : TextDisabledV ( const char * fmt , va_list args )
{
ImGui : : PushStyleColor ( ImGuiCol_Text , GImGui - > Style . Colors [ ImGuiCol_TextDisabled ] ) ;
TextV ( fmt , args ) ;
ImGui : : PopStyleColor ( ) ;
}
void ImGui : : TextDisabled ( const char * fmt , . . . )
{
va_list args ;
va_start ( args , fmt ) ;
TextDisabledV ( fmt , args ) ;
va_end ( args ) ;
}
2014-11-10 09:29:01 +08:00
void ImGui : : TextWrappedV ( const char * fmt , va_list args )
2014-11-07 13:45:56 +08:00
{
ImGui : : PushTextWrapPos ( 0.0f ) ;
TextV ( fmt , args ) ;
ImGui : : PopTextWrapPos ( ) ;
}
2014-11-10 09:29:01 +08:00
void ImGui : : TextWrapped ( const char * fmt , . . . )
2014-11-07 13:45:56 +08:00
{
va_list args ;
va_start ( args , fmt ) ;
TextWrappedV ( fmt , args ) ;
va_end ( args ) ;
}
2014-11-10 09:29:01 +08:00
void ImGui : : TextUnformatted ( const char * text , const char * text_end )
2014-08-11 04:58:11 +08:00
{
2014-08-19 19:09:13 +08:00
ImGuiWindow * window = GetCurrentWindow ( ) ;
if ( window - > SkipItems )
return ;
2015-07-22 10:13:12 +08:00
ImGuiState & g = * GImGui ;
2014-09-25 03:39:39 +08:00
IM_ASSERT ( text ! = NULL ) ;
2014-08-19 19:09:13 +08:00
const char * text_begin = text ;
if ( text_end = = NULL )
2014-11-17 01:27:26 +08:00
text_end = text + strlen ( text ) ; // FIXME-OPT
2014-08-19 19:09:13 +08:00
2015-05-27 22:35:49 +08:00
const float wrap_pos_x = window - > DC . TextWrapPos ;
2014-11-07 13:45:56 +08:00
const bool wrap_enabled = wrap_pos_x > = 0.0f ;
if ( text_end - text > 2000 & & ! wrap_enabled )
2014-08-19 19:09:13 +08:00
{
// Long text!
// Perform manual coarse clipping to optimize for long multi-line text
2015-02-11 06:48:46 +08:00
// From this point we will only compute the width of lines that are visible. Optimization only available when word-wrapping is disabled.
// We also don't vertically center the text within the line full height, which is unlikely to matter because we are likely the biggest and only item on the line.
2014-08-19 19:09:13 +08:00
const char * line = text ;
const float line_height = ImGui : : GetTextLineHeight ( ) ;
2015-03-18 04:17:53 +08:00
const ImVec2 text_pos = window - > DC . CursorPos + ImVec2 ( 0.0f , window - > DC . CurrentLineTextBaseOffset ) ;
2015-07-07 11:46:12 +08:00
const ImVec4 clip_rect = window - > ClipRect ;
2014-08-19 19:09:13 +08:00
ImVec2 text_size ( 0 , 0 ) ;
2015-03-18 04:17:53 +08:00
if ( text_pos . y < = clip_rect . w )
2014-08-19 19:09:13 +08:00
{
2015-03-18 04:17:53 +08:00
ImVec2 pos = text_pos ;
2014-08-19 19:09:13 +08:00
2014-09-02 21:35:06 +08:00
// Lines to skip (can't skip when logging text)
2014-08-19 19:09:13 +08:00
if ( ! g . LogEnabled )
{
2015-07-02 21:42:29 +08:00
int lines_skippable = ( int ) ( ( clip_rect . y - text_pos . y ) / line_height ) ;
2014-08-19 19:09:13 +08:00
if ( lines_skippable > 0 )
{
int lines_skipped = 0 ;
2015-07-02 21:42:29 +08:00
while ( line < text_end & & lines_skipped < lines_skippable )
2014-08-19 19:09:13 +08:00
{
const char * line_end = strchr ( line , ' \n ' ) ;
line = line_end + 1 ;
lines_skipped + + ;
}
pos . y + = lines_skipped * line_height ;
}
}
2014-09-02 21:35:06 +08:00
// Lines to render
2014-08-19 19:09:13 +08:00
if ( line < text_end )
{
2015-03-18 07:41:20 +08:00
ImRect line_rect ( pos , pos + ImVec2 ( ImGui : : GetWindowWidth ( ) , line_height ) ) ;
2014-08-19 19:09:13 +08:00
while ( line < text_end )
{
const char * line_end = strchr ( line , ' \n ' ) ;
2015-04-18 19:11:59 +08:00
if ( IsClippedEx ( line_rect , NULL , false ) )
2014-08-19 19:09:13 +08:00
break ;
const ImVec2 line_size = CalcTextSize ( line , line_end , false ) ;
text_size . x = ImMax ( text_size . x , line_size . x ) ;
RenderText ( pos , line , line_end , false ) ;
if ( ! line_end )
line_end = text_end ;
line = line_end + 1 ;
2015-03-18 07:41:20 +08:00
line_rect . Min . y + = line_height ;
line_rect . Max . y + = line_height ;
2014-08-19 19:09:13 +08:00
pos . y + = line_height ;
}
2014-09-02 21:35:06 +08:00
// Count remaining lines
2014-08-19 19:09:13 +08:00
int lines_skipped = 0 ;
while ( line < text_end )
{
const char * line_end = strchr ( line , ' \n ' ) ;
if ( ! line_end )
line_end = text_end ;
line = line_end + 1 ;
lines_skipped + + ;
}
pos . y + = lines_skipped * line_height ;
}
2015-03-18 04:17:53 +08:00
text_size . y + = ( pos - text_pos ) . y ;
2014-08-19 19:09:13 +08:00
}
2015-03-18 04:17:53 +08:00
2015-03-18 07:35:12 +08:00
ImRect bb ( text_pos , text_pos + text_size ) ;
2014-08-19 19:09:13 +08:00
ItemSize ( bb ) ;
2015-01-23 06:54:05 +08:00
ItemAdd ( bb , NULL ) ;
2014-08-19 19:09:13 +08:00
}
else
{
2014-11-07 13:45:56 +08:00
const float wrap_width = wrap_enabled ? CalcWrapWidthForPos ( window - > DC . CursorPos , wrap_pos_x ) : 0.0f ;
const ImVec2 text_size = CalcTextSize ( text_begin , text_end , false , wrap_width ) ;
2015-03-18 01:17:00 +08:00
2015-03-18 04:17:53 +08:00
// Account of baseline offset
ImVec2 text_pos = window - > DC . CursorPos ;
text_pos . y + = window - > DC . CurrentLineTextBaseOffset ;
2015-03-18 01:17:00 +08:00
2015-03-18 07:35:12 +08:00
ImRect bb ( text_pos , text_pos + text_size ) ;
2015-03-18 01:17:00 +08:00
ItemSize ( bb . GetSize ( ) ) ;
2015-01-23 06:54:05 +08:00
if ( ! ItemAdd ( bb , NULL ) )
2014-08-19 19:09:13 +08:00
return ;
2015-02-08 23:57:06 +08:00
// Render (we don't hide text after ## in this end-user function)
RenderTextWrapped ( bb . Min , text_begin , text_end , wrap_width ) ;
2014-08-19 19:09:13 +08:00
}
2014-08-11 04:58:11 +08:00
}
2014-11-10 09:29:01 +08:00
void ImGui : : AlignFirstTextHeightToWidgets ( )
2014-08-11 04:58:11 +08:00
{
2014-08-19 19:09:13 +08:00
ImGuiWindow * window = GetCurrentWindow ( ) ;
if ( window - > SkipItems )
return ;
2014-08-11 04:58:11 +08:00
2014-08-19 19:09:13 +08:00
// Declare a dummy item size to that upcoming items that are smaller will center-align on the newly expanded line height.
2015-07-22 10:13:12 +08:00
ImGuiState & g = * GImGui ;
2015-03-30 02:45:03 +08:00
ItemSize ( ImVec2 ( 0 , g . FontSize + g . Style . FramePadding . y * 2 ) , g . Style . FramePadding . y ) ;
2014-08-19 19:09:13 +08:00
ImGui : : SameLine ( 0 , 0 ) ;
2014-08-11 04:58:11 +08:00
}
2014-09-02 21:35:06 +08:00
// Add a label+text combo aligned to other label+value widgets
2014-11-10 09:29:01 +08:00
void ImGui : : LabelTextV ( const char * label , const char * fmt , va_list args )
2014-08-11 04:58:11 +08:00
{
2014-08-19 19:09:13 +08:00
ImGuiWindow * window = GetCurrentWindow ( ) ;
if ( window - > SkipItems )
return ;
2015-03-16 08:46:03 +08:00
2015-07-22 10:13:12 +08:00
ImGuiState & g = * GImGui ;
2014-08-19 19:09:13 +08:00
const ImGuiStyle & style = g . Style ;
2015-02-11 05:07:26 +08:00
const float w = ImGui : : CalcItemWidth ( ) ;
2014-08-11 04:58:11 +08:00
2015-03-07 05:58:12 +08:00
const ImVec2 label_size = CalcTextSize ( label , NULL , true ) ;
2015-03-18 07:35:12 +08:00
const ImRect value_bb ( window - > DC . CursorPos , window - > DC . CursorPos + ImVec2 ( w + style . FramePadding . x * 2 , label_size . y + style . FramePadding . y * 2 ) ) ;
2015-03-19 22:48:36 +08:00
const ImRect total_bb ( window - > DC . CursorPos , window - > DC . CursorPos + ImVec2 ( w + style . FramePadding . x * 2 + ( label_size . x > 0.0f ? style . ItemInnerSpacing . x : 0.0f ) , style . FramePadding . y * 2 ) + label_size ) ;
ItemSize ( total_bb , style . FramePadding . y ) ;
if ( ! ItemAdd ( total_bb , NULL ) )
2014-08-19 19:09:13 +08:00
return ;
2014-08-11 04:58:11 +08:00
2014-08-19 19:09:13 +08:00
// Render
2015-05-27 21:39:41 +08:00
const char * value_text_begin = & g . TempBuffer [ 0 ] ;
const char * value_text_end = value_text_begin + ImFormatStringV ( g . TempBuffer , IM_ARRAYSIZE ( g . TempBuffer ) , fmt , args ) ;
2015-06-17 07:21:07 +08:00
RenderTextClipped ( value_bb . Min , value_bb . Max , value_text_begin , value_text_end , NULL , ImGuiAlign_VCenter ) ;
2015-03-18 04:17:53 +08:00
RenderText ( ImVec2 ( value_bb . Max . x + style . ItemInnerSpacing . x , value_bb . Min . y + style . FramePadding . y ) , label ) ;
2014-08-11 04:58:11 +08:00
}
2014-11-10 09:29:01 +08:00
void ImGui : : LabelText ( const char * label , const char * fmt , . . . )
2014-09-25 02:27:29 +08:00
{
va_list args ;
va_start ( args , fmt ) ;
LabelTextV ( label , fmt , args ) ;
va_end ( args ) ;
}
2015-03-27 01:45:11 +08:00
static inline bool IsWindowContentHoverable ( ImGuiWindow * window )
{
2015-04-16 17:54:56 +08:00
// An active popup disable hovering on other windows (apart from its own children)
2015-05-20 17:31:05 +08:00
ImGuiState & g = * GImGui ;
2015-07-06 10:47:25 +08:00
if ( ImGuiWindow * focused_window = g . FocusedWindow )
2015-04-16 17:54:56 +08:00
if ( ImGuiWindow * focused_root_window = focused_window - > RootWindow )
2015-05-02 20:20:50 +08:00
if ( ( focused_root_window - > Flags & ImGuiWindowFlags_Popup ) ! = 0 & & focused_root_window - > WasActive & & focused_root_window ! = window - > RootWindow )
2015-04-16 17:54:56 +08:00
return false ;
2015-03-27 01:45:11 +08:00
return true ;
}
2015-08-02 11:57:24 +08:00
bool ImGui : : IsHovered ( const ImRect & bb , ImGuiID id , bool flatten_childs )
2015-01-05 02:12:30 +08:00
{
2015-02-01 12:28:24 +08:00
ImGuiState & g = * GImGui ;
2015-05-18 00:21:49 +08:00
if ( g . HoveredId = = 0 | | g . HoveredId = = id )
2015-01-05 02:12:30 +08:00
{
ImGuiWindow * window = GetCurrentWindow ( ) ;
2015-05-18 23:42:55 +08:00
if ( g . HoveredWindow = = window | | ( flatten_childs & & g . HoveredRootWindow = = window - > RootWindow ) )
2015-07-31 13:48:59 +08:00
if ( ( g . ActiveId = = 0 | | g . ActiveId = = id | | g . ActiveIdIsFocusedOnly ) & & ImGui : : IsMouseHoveringRect ( bb . Min , bb . Max ) )
2015-03-27 01:45:11 +08:00
if ( IsWindowContentHoverable ( g . HoveredRootWindow ) )
2015-03-27 01:17:42 +08:00
return true ;
2015-01-05 02:12:30 +08:00
}
return false ;
}
2015-08-02 11:57:24 +08:00
bool ImGui : : ButtonBehavior ( const ImRect & bb , ImGuiID id , bool * out_hovered , bool * out_held , bool allow_key_modifiers , ImGuiButtonFlags flags )
2014-08-11 04:58:11 +08:00
{
2015-02-01 12:28:24 +08:00
ImGuiState & g = * GImGui ;
2014-08-19 19:09:13 +08:00
ImGuiWindow * window = GetCurrentWindow ( ) ;
2015-05-23 20:54:57 +08:00
if ( flags & ImGuiButtonFlags_Disabled )
{
if ( out_hovered ) * out_hovered = false ;
if ( out_held ) * out_held = false ;
2015-07-22 07:00:28 +08:00
if ( g . ActiveId = = id ) SetActiveID ( 0 ) ;
2015-05-23 20:54:57 +08:00
return false ;
}
2014-08-19 19:09:13 +08:00
bool pressed = false ;
2015-05-18 23:42:55 +08:00
const bool hovered = IsHovered ( bb , id , ( flags & ImGuiButtonFlags_FlattenChilds ) ! = 0 ) ;
2014-08-19 19:09:13 +08:00
if ( hovered )
{
g . HoveredId = id ;
2015-03-13 17:49:38 +08:00
if ( allow_key_modifiers | | ( ! g . IO . KeyCtrl & & ! g . IO . KeyShift & & ! g . IO . KeyAlt ) )
2014-08-19 19:09:13 +08:00
{
if ( g . IO . MouseClicked [ 0 ] )
{
2015-05-18 23:39:37 +08:00
if ( flags & ImGuiButtonFlags_PressedOnClick )
2015-02-11 01:45:45 +08:00
{
pressed = true ;
2015-07-22 07:00:28 +08:00
SetActiveID ( 0 ) ;
2015-02-11 01:45:45 +08:00
}
else
{
2015-07-22 07:00:28 +08:00
SetActiveID ( id , window ) ;
2015-02-11 01:45:45 +08:00
}
2015-01-05 02:36:33 +08:00
FocusWindow ( window ) ;
2014-08-19 19:09:13 +08:00
}
2015-07-06 10:27:06 +08:00
else if ( g . IO . MouseReleased [ 0 ] & & ( flags & ImGuiButtonFlags_PressedOnRelease ) )
{
pressed = true ;
2015-07-22 07:00:28 +08:00
SetActiveID ( 0 ) ;
2015-07-06 10:27:06 +08:00
}
2015-05-18 23:39:37 +08:00
else if ( ( flags & ImGuiButtonFlags_Repeat ) & & g . ActiveId = = id & & ImGui : : IsMouseClicked ( 0 , true ) )
2014-08-19 19:09:13 +08:00
{
pressed = true ;
}
}
}
bool held = false ;
if ( g . ActiveId = = id )
{
if ( g . IO . MouseDown [ 0 ] )
{
held = true ;
}
else
{
if ( hovered )
pressed = true ;
2015-07-22 07:00:28 +08:00
SetActiveID ( 0 ) ;
2014-08-19 19:09:13 +08:00
}
}
if ( out_hovered ) * out_hovered = hovered ;
if ( out_held ) * out_held = held ;
return pressed ;
2014-08-11 04:58:11 +08:00
}
2015-08-02 11:57:24 +08:00
inline ImVec2 ImGui : : CalcItemSize ( ImVec2 size , float default_x , float default_y )
2015-07-18 05:58:23 +08:00
{
ImGuiState & g = * GImGui ;
ImVec2 content_max ;
if ( size . x < 0.0f | | size . y < 0.0f )
content_max = g . CurrentWindow - > Pos + ImGui : : 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 ;
}
2015-08-02 11:57:24 +08:00
bool ImGui : : ButtonEx ( const char * label , const ImVec2 & size_arg , ImGuiButtonFlags flags )
2014-08-11 04:58:11 +08:00
{
2014-08-19 19:09:13 +08:00
ImGuiWindow * window = GetCurrentWindow ( ) ;
if ( window - > SkipItems )
return false ;
2014-08-11 04:58:11 +08:00
2015-05-27 16:22:30 +08:00
ImGuiState & g = * GImGui ;
2014-08-19 19:09:13 +08:00
const ImGuiStyle & style = g . Style ;
const ImGuiID id = window - > GetID ( label ) ;
2015-05-27 16:22:30 +08:00
const ImVec2 label_size = ImGui : : CalcTextSize ( label , NULL , true ) ;
2014-12-30 23:38:28 +08:00
2015-07-03 02:44:48 +08:00
ImVec2 pos = window - > DC . CursorPos ;
if ( ( flags & ImGuiButtonFlags_AlignTextBaseLine ) & & style . FramePadding . y < window - > DC . CurrentLineTextBaseOffset )
pos . y + = window - > DC . CurrentLineTextBaseOffset - style . FramePadding . y ;
2015-07-18 05:58:23 +08:00
ImVec2 size = CalcItemSize ( size_arg , label_size . x + style . FramePadding . x * 2.0f , label_size . y + style . FramePadding . y * 2.0f ) ;
2015-07-03 02:44:48 +08:00
const ImRect bb ( pos , pos + size ) ;
2015-03-18 04:17:53 +08:00
ItemSize ( bb , style . FramePadding . y ) ;
2015-01-23 06:54:05 +08:00
if ( ! ItemAdd ( bb , & id ) )
2014-08-19 19:09:13 +08:00
return false ;
2014-08-11 04:58:11 +08:00
2015-05-27 22:22:09 +08:00
if ( window - > DC . ButtonRepeat ) flags | = ImGuiButtonFlags_Repeat ;
2014-08-19 19:09:13 +08:00
bool hovered , held ;
2015-05-27 16:22:30 +08:00
bool pressed = ButtonBehavior ( bb , id , & hovered , & held , true , flags ) ;
2014-08-11 04:58:11 +08:00
2014-08-19 19:09:13 +08:00
// Render
const ImU32 col = window - > Color ( ( hovered & & held ) ? ImGuiCol_ButtonActive : hovered ? ImGuiCol_ButtonHovered : ImGuiCol_Button ) ;
2015-01-15 17:14:44 +08:00
RenderFrame ( bb . Min , bb . Max , col , true , style . FrameRounding ) ;
2015-06-17 07:21:07 +08:00
RenderTextClipped ( bb . Min , bb . Max , label , NULL , & label_size , ImGuiAlign_Center | ImGuiAlign_VCenter ) ;
2014-08-11 04:58:11 +08:00
2015-05-27 16:28:44 +08:00
// Automatically close popups
2015-05-27 22:51:50 +08:00
//if (pressed && !(flags & ImGuiButtonFlags_DontClosePopups) && (window->Flags & ImGuiWindowFlags_Popup))
// ImGui::CloseCurrentPopup();
2014-08-11 04:58:11 +08:00
2014-08-19 19:09:13 +08:00
return pressed ;
2014-08-11 04:58:11 +08:00
}
2015-05-27 22:22:09 +08:00
bool ImGui : : Button ( const char * label , const ImVec2 & size_arg )
2015-05-27 16:22:30 +08:00
{
2015-05-27 22:22:09 +08:00
return ButtonEx ( label , size_arg , 0 ) ;
2015-05-27 16:22:30 +08:00
}
// Small buttons fits within text without additional vertical spacing.
2014-11-10 09:29:01 +08:00
bool ImGui : : SmallButton ( const char * label )
2014-08-11 04:58:11 +08:00
{
2015-02-01 12:28:24 +08:00
ImGuiState & g = * GImGui ;
2015-05-27 16:22:30 +08:00
float backup_padding_y = g . Style . FramePadding . y ;
g . Style . FramePadding . y = 0.0f ;
2015-07-03 02:44:48 +08:00
bool pressed = ButtonEx ( label , ImVec2 ( 0 , 0 ) , ImGuiButtonFlags_AlignTextBaseLine ) ;
2015-05-27 16:22:30 +08:00
g . Style . FramePadding . y = backup_padding_y ;
2014-08-19 19:09:13 +08:00
return pressed ;
2014-08-11 04:58:11 +08:00
}
2015-01-22 06:03:25 +08:00
// Tip: use ImGui::PushID()/PopID() to push indices or pointers in the ID stack.
// Then you can keep 'str_id' empty or the same for all your buttons (instead of creating a string based on a non-string id)
bool ImGui : : InvisibleButton ( const char * str_id , const ImVec2 & size )
{
ImGuiWindow * window = GetCurrentWindow ( ) ;
if ( window - > SkipItems )
return false ;
2015-01-23 06:54:05 +08:00
const ImGuiID id = window - > GetID ( str_id ) ;
2015-03-18 07:35:12 +08:00
const ImRect bb ( window - > DC . CursorPos , window - > DC . CursorPos + size ) ;
2015-01-22 06:03:25 +08:00
ItemSize ( bb ) ;
2015-01-23 06:54:05 +08:00
if ( ! ItemAdd ( bb , & id ) )
2015-01-22 06:03:25 +08:00
return false ;
2015-01-29 21:20:25 +08:00
bool hovered , held ;
2015-03-15 09:48:59 +08:00
bool pressed = ButtonBehavior ( bb , id , & hovered , & held , true ) ;
2015-01-22 06:03:25 +08:00
return pressed ;
}
2014-09-02 21:35:06 +08:00
// Upper-right button to close a window.
2014-12-22 21:29:39 +08:00
static bool CloseWindowButton ( bool * p_opened )
2014-08-11 04:58:11 +08:00
{
2015-08-02 11:57:24 +08:00
ImGuiWindow * window = ImGui : : GetCurrentWindow ( ) ;
2014-08-11 04:58:11 +08:00
2015-03-08 06:01:06 +08:00
const ImGuiID id = window - > GetID ( " #CLOSE " ) ;
2014-08-28 05:16:55 +08:00
const float size = window - > TitleBarHeight ( ) - 4.0f ;
2015-07-22 10:13:12 +08:00
const ImRect bb ( window - > Rect ( ) . GetTR ( ) + ImVec2 ( - 2.0f - size , 2.0f ) , window - > Rect ( ) . GetTR ( ) + ImVec2 ( - 2.0f , 2.0f + size ) ) ;
2014-08-11 04:58:11 +08:00
2014-08-19 19:09:13 +08:00
bool hovered , held ;
2015-08-02 11:57:24 +08:00
bool pressed = ImGui : : ButtonBehavior ( bb , id , & hovered , & held , true ) ;
2014-08-11 04:58:11 +08:00
2014-08-19 19:09:13 +08:00
// Render
const ImU32 col = window - > Color ( ( held & & hovered ) ? ImGuiCol_CloseButtonActive : hovered ? ImGuiCol_CloseButtonHovered : ImGuiCol_CloseButton ) ;
2014-08-28 21:52:10 +08:00
const ImVec2 center = bb . GetCenter ( ) ;
window - > DrawList - > AddCircleFilled ( center , ImMax ( 2.0f , size * 0.5f ) , col , 16 ) ;
2014-08-11 04:58:11 +08:00
2014-08-28 21:52:10 +08:00
const float cross_extent = ( size * 0.5f * 0.7071f ) - 1.0f ;
if ( hovered )
2014-08-19 19:09:13 +08:00
{
2014-08-28 21:52:10 +08:00
window - > DrawList - > AddLine ( center + ImVec2 ( + cross_extent , + cross_extent ) , center + ImVec2 ( - cross_extent , - cross_extent ) , window - > Color ( ImGuiCol_Text ) ) ;
window - > DrawList - > AddLine ( center + ImVec2 ( + cross_extent , - cross_extent ) , center + ImVec2 ( - cross_extent , + cross_extent ) , window - > Color ( ImGuiCol_Text ) ) ;
2014-08-19 19:09:13 +08:00
}
2014-08-11 04:58:11 +08:00
2014-12-22 21:29:39 +08:00
if ( p_opened ! = NULL & & pressed )
* p_opened = ! * p_opened ;
2014-08-11 04:58:11 +08:00
2014-08-19 19:09:13 +08:00
return pressed ;
2014-08-11 04:58:11 +08:00
}
2015-02-01 00:44:55 +08:00
void ImGui : : Image ( ImTextureID user_texture_id , const ImVec2 & size , const ImVec2 & uv0 , const ImVec2 & uv1 , const ImVec4 & tint_col , const ImVec4 & border_col )
2015-01-12 00:30:39 +08:00
{
ImGuiWindow * window = GetCurrentWindow ( ) ;
if ( window - > SkipItems )
return ;
2015-03-18 07:35:12 +08:00
ImRect bb ( window - > DC . CursorPos , window - > DC . CursorPos + size ) ;
2015-02-01 00:44:55 +08:00
if ( border_col . w > 0.0f )
2015-01-12 00:30:39 +08:00
bb . Max + = ImVec2 ( 2 , 2 ) ;
2015-02-01 00:21:12 +08:00
ItemSize ( bb ) ;
2015-01-23 06:54:05 +08:00
if ( ! ItemAdd ( bb , NULL ) )
2015-01-12 00:30:39 +08:00
return ;
2015-02-01 00:44:55 +08:00
if ( border_col . w > 0.0f )
2015-01-12 00:30:39 +08:00
{
2015-02-01 00:44:55 +08:00
window - > DrawList - > AddRect ( bb . Min , bb . Max , window - > Color ( border_col ) , 0.0f ) ;
window - > DrawList - > AddImage ( user_texture_id , bb . Min + ImVec2 ( 1 , 1 ) , bb . Max - ImVec2 ( 1 , 1 ) , uv0 , uv1 , window - > Color ( tint_col ) ) ;
2015-01-12 00:30:39 +08:00
}
else
{
2015-02-01 00:44:55 +08:00
window - > DrawList - > AddImage ( user_texture_id , bb . Min , bb . Max , uv0 , uv1 , window - > Color ( tint_col ) ) ;
2015-01-12 00:30:39 +08:00
}
}
2015-02-01 00:21:12 +08:00
// frame_padding < 0: uses FramePadding from style (default)
// frame_padding = 0: no framing
// frame_padding > 0: set framing size
// The color used are the button colors.
2015-02-05 02:19:43 +08:00
bool ImGui : : ImageButton ( ImTextureID user_texture_id , const ImVec2 & size , const ImVec2 & uv0 , const ImVec2 & uv1 , int frame_padding , const ImVec4 & bg_col , const ImVec4 & tint_col )
2015-02-01 00:21:12 +08:00
{
ImGuiWindow * window = GetCurrentWindow ( ) ;
if ( window - > SkipItems )
return false ;
2015-07-22 10:13:12 +08:00
ImGuiState & g = * GImGui ;
2015-02-01 00:21:12 +08:00
const ImGuiStyle & style = g . Style ;
// Default to using texture ID as ID. User can still push string/integer prefixes.
2015-06-15 06:18:22 +08:00
// We could hash the size/uv to create a unique ID but that would prevent the user from animating UV.
2015-02-01 00:21:12 +08:00
ImGui : : PushID ( ( void * ) user_texture_id ) ;
const ImGuiID id = window - > GetID ( " #image " ) ;
ImGui : : PopID ( ) ;
const ImVec2 padding = ( frame_padding > = 0 ) ? ImVec2 ( ( float ) frame_padding , ( float ) frame_padding ) : style . FramePadding ;
2015-03-18 07:35:12 +08:00
const ImRect bb ( window - > DC . CursorPos , window - > DC . CursorPos + size + padding * 2 ) ;
const ImRect image_bb ( window - > DC . CursorPos + padding , window - > DC . CursorPos + padding + size ) ;
2015-02-01 00:21:12 +08:00
ItemSize ( bb ) ;
if ( ! ItemAdd ( bb , & id ) )
return false ;
bool hovered , held ;
2015-03-15 09:48:59 +08:00
bool pressed = ButtonBehavior ( bb , id , & hovered , & held , true ) ;
2015-02-01 00:21:12 +08:00
// Render
const ImU32 col = window - > Color ( ( hovered & & held ) ? ImGuiCol_ButtonActive : hovered ? ImGuiCol_ButtonHovered : ImGuiCol_Button ) ;
2015-06-15 06:18:22 +08:00
RenderFrame ( bb . Min , bb . Max , col ) ;
2015-02-01 00:44:55 +08:00
if ( bg_col . w > 0.0f )
window - > DrawList - > AddRectFilled ( image_bb . Min , image_bb . Max , window - > Color ( bg_col ) ) ;
2015-02-05 02:19:43 +08:00
window - > DrawList - > AddImage ( user_texture_id , image_bb . Min , image_bb . Max , uv0 , uv1 , window - > Color ( tint_col ) ) ;
2015-02-01 00:21:12 +08:00
return pressed ;
}
2014-09-02 21:35:06 +08:00
// Start logging ImGui output to TTY
2014-11-10 09:29:01 +08:00
void ImGui : : LogToTTY ( int max_depth )
2014-08-11 04:58:11 +08:00
{
2015-02-01 12:28:24 +08:00
ImGuiState & g = * GImGui ;
2014-12-30 04:18:52 +08:00
ImGuiWindow * window = GetCurrentWindow ( ) ;
2014-08-19 19:09:13 +08:00
if ( g . LogEnabled )
return ;
2014-12-30 04:18:52 +08:00
2014-08-19 19:09:13 +08:00
g . LogEnabled = true ;
g . LogFile = stdout ;
2014-12-30 04:18:52 +08:00
g . LogStartDepth = window - > DC . TreeDepth ;
2014-08-19 19:09:13 +08:00
if ( max_depth > = 0 )
g . LogAutoExpandMaxDepth = max_depth ;
2014-08-11 04:58:11 +08:00
}
2014-09-02 21:35:06 +08:00
// Start logging ImGui output to given file
2014-11-10 09:29:01 +08:00
void ImGui : : LogToFile ( int max_depth , const char * filename )
2014-08-11 04:58:11 +08:00
{
2015-02-01 12:28:24 +08:00
ImGuiState & g = * GImGui ;
2014-12-30 04:18:52 +08:00
ImGuiWindow * window = GetCurrentWindow ( ) ;
2014-08-19 19:09:13 +08:00
if ( g . LogEnabled )
return ;
if ( ! filename )
2015-03-22 03:30:46 +08:00
{
2014-08-19 19:09:13 +08:00
filename = g . IO . LogFilename ;
2015-03-22 03:30:46 +08:00
if ( ! filename )
return ;
}
2014-12-30 04:18:52 +08:00
2014-12-30 04:41:27 +08:00
g . LogFile = fopen ( filename , " ab " ) ;
2015-01-05 05:37:49 +08:00
if ( ! g . LogFile )
{
IM_ASSERT ( g . LogFile ! = NULL ) ; // Consider this an error
return ;
}
g . LogEnabled = true ;
2014-12-30 04:18:52 +08:00
g . LogStartDepth = window - > DC . TreeDepth ;
2014-08-19 19:09:13 +08:00
if ( max_depth > = 0 )
g . LogAutoExpandMaxDepth = max_depth ;
2014-08-11 04:58:11 +08:00
}
2014-09-02 21:35:06 +08:00
// Start logging ImGui output to clipboard
2014-11-10 09:29:01 +08:00
void ImGui : : LogToClipboard ( int max_depth )
2014-08-11 04:58:11 +08:00
{
2014-12-30 04:18:52 +08:00
ImGuiWindow * window = GetCurrentWindow ( ) ;
2015-02-01 12:28:24 +08:00
ImGuiState & g = * GImGui ;
2014-08-19 19:09:13 +08:00
if ( g . LogEnabled )
return ;
2014-12-30 04:18:52 +08:00
2014-08-19 19:09:13 +08:00
g . LogEnabled = true ;
g . LogFile = NULL ;
2014-12-30 04:18:52 +08:00
g . LogStartDepth = window - > DC . TreeDepth ;
2014-08-19 19:09:13 +08:00
if ( max_depth > = 0 )
g . LogAutoExpandMaxDepth = max_depth ;
2014-08-11 04:58:11 +08:00
}
2014-12-30 02:13:41 +08:00
void ImGui : : LogFinish ( )
{
2015-02-01 12:28:24 +08:00
ImGuiState & g = * GImGui ;
2014-12-30 02:13:41 +08:00
if ( ! g . LogEnabled )
return ;
2014-12-30 04:18:52 +08:00
2015-06-30 00:39:17 +08:00
ImGui : : LogText ( IM_NEWLINE ) ;
2014-12-30 02:13:41 +08:00
g . LogEnabled = false ;
if ( g . LogFile ! = NULL )
{
if ( g . LogFile = = stdout )
fflush ( g . LogFile ) ;
else
fclose ( g . LogFile ) ;
g . LogFile = NULL ;
}
if ( g . LogClipboard - > size ( ) > 1 )
{
if ( g . IO . SetClipboardTextFn )
g . IO . SetClipboardTextFn ( g . LogClipboard - > begin ( ) ) ;
g . LogClipboard - > clear ( ) ;
}
}
2014-09-02 21:35:06 +08:00
// Helper to display logging buttons
2014-11-10 09:29:01 +08:00
void ImGui : : LogButtons ( )
2014-08-11 04:58:11 +08:00
{
2015-02-01 12:28:24 +08:00
ImGuiState & g = * GImGui ;
2014-08-11 04:58:11 +08:00
2014-08-19 19:09:13 +08:00
ImGui : : PushID ( " LogButtons " ) ;
const bool log_to_tty = ImGui : : Button ( " Log To TTY " ) ;
ImGui : : SameLine ( ) ;
const bool log_to_file = ImGui : : Button ( " Log To File " ) ;
ImGui : : SameLine ( ) ;
const bool log_to_clipboard = ImGui : : Button ( " Log To Clipboard " ) ;
ImGui : : SameLine ( ) ;
2014-08-11 04:58:11 +08:00
2014-08-19 19:09:13 +08:00
ImGui : : PushItemWidth ( 80.0f ) ;
ImGui : : PushAllowKeyboardFocus ( false ) ;
ImGui : : SliderInt ( " Depth " , & g . LogAutoExpandMaxDepth , 0 , 9 , NULL ) ;
ImGui : : PopAllowKeyboardFocus ( ) ;
ImGui : : PopItemWidth ( ) ;
ImGui : : PopID ( ) ;
2014-08-11 04:58:11 +08:00
2014-08-19 19:09:13 +08:00
// Start logging at the end of the function so that the buttons don't appear in the log
if ( log_to_tty )
LogToTTY ( g . LogAutoExpandMaxDepth ) ;
if ( log_to_file )
LogToFile ( g . LogAutoExpandMaxDepth , g . IO . LogFilename ) ;
if ( log_to_clipboard )
LogToClipboard ( g . LogAutoExpandMaxDepth ) ;
2014-08-11 04:58:11 +08:00
}
2015-02-27 17:38:22 +08:00
bool ImGui : : CollapsingHeader ( const char * label , const char * str_id , bool display_frame , bool default_open )
2014-08-11 04:58:11 +08:00
{
2014-08-19 19:09:13 +08:00
ImGuiWindow * window = GetCurrentWindow ( ) ;
if ( window - > SkipItems )
return false ;
2015-07-22 10:13:12 +08:00
ImGuiState & g = * GImGui ;
2014-08-19 19:09:13 +08:00
const ImGuiStyle & style = g . Style ;
IM_ASSERT ( str_id ! = NULL | | label ! = NULL ) ;
if ( str_id = = NULL )
str_id = label ;
if ( label = = NULL )
label = str_id ;
const ImGuiID id = window - > GetID ( str_id ) ;
2015-02-27 17:38:22 +08:00
// We only write to the tree storage if the user clicks (or explicitely use SetNextTreeNode*** functions)
2014-12-30 02:17:54 +08:00
ImGuiStorage * storage = window - > DC . StateStorage ;
2014-08-19 19:09:13 +08:00
bool opened ;
2015-02-27 17:38:22 +08:00
if ( g . SetNextTreeNodeOpenedCond ! = 0 )
2014-08-19 19:09:13 +08:00
{
2015-02-27 17:51:11 +08:00
if ( g . SetNextTreeNodeOpenedCond & ImGuiSetCond_Always )
2015-02-27 17:38:22 +08:00
{
opened = g . SetNextTreeNodeOpenedVal ;
storage - > SetInt ( id , opened ) ;
}
else
{
2015-02-27 17:51:11 +08:00
// We treat ImGuiSetCondition_Once and ImGuiSetCondition_FirstUseEver the same because tree node state are not saved persistently.
2015-02-27 17:38:22 +08:00
const int stored_value = storage - > GetInt ( id , - 1 ) ;
if ( stored_value = = - 1 )
{
opened = g . SetNextTreeNodeOpenedVal ;
storage - > SetInt ( id , opened ) ;
}
else
{
opened = stored_value ! = 0 ;
}
}
g . SetNextTreeNodeOpenedCond = 0 ;
2014-08-19 19:09:13 +08:00
}
else
{
2014-12-30 02:17:54 +08:00
opened = storage - > GetInt ( id , default_open ) ! = 0 ;
2014-08-19 19:09:13 +08:00
}
2014-09-25 03:39:39 +08:00
// Framed header expand a little outside the default padding
2015-07-02 08:55:42 +08:00
const ImVec2 window_padding = window - > WindowPadding ;
2015-03-07 05:58:12 +08:00
const ImVec2 label_size = CalcTextSize ( label , NULL , true ) ;
2014-08-19 19:09:13 +08:00
const ImVec2 pos_min = window - > DC . CursorPos ;
2014-11-07 14:17:04 +08:00
const ImVec2 pos_max = window - > Pos + GetContentRegionMax ( ) ;
2015-03-18 07:35:12 +08:00
ImRect bb = ImRect ( pos_min , ImVec2 ( pos_max . x , pos_min . y + label_size . y ) ) ;
2014-08-19 19:09:13 +08:00
if ( display_frame )
{
2015-07-06 06:35:41 +08:00
bb . Min . x - = ( float ) ( int ) ( window_padding . x * 0.5f ) - 1 ;
bb . Max . x + = ( float ) ( int ) ( window_padding . x * 0.5f ) - 1 ;
2014-08-19 19:09:13 +08:00
bb . Max . y + = style . FramePadding . y * 2 ;
}
2015-03-18 04:17:53 +08:00
// FIXME: we don't provide our width so that it doesn't get feed back into AutoFit. Should manage that better so we can still hover without extending ContentsSize
2015-03-30 02:45:03 +08:00
const ImRect text_bb ( bb . Min , bb . Min + ImVec2 ( g . FontSize + style . FramePadding . x * 2 * 2 , 0 ) + label_size ) ;
2015-03-18 04:17:53 +08:00
ItemSize ( ImVec2 ( text_bb . GetSize ( ) . x , bb . GetSize ( ) . y ) , display_frame ? style . FramePadding . y : 0.0f ) ;
2014-08-19 19:09:13 +08:00
2015-03-15 09:48:59 +08:00
// When logging is enabled, if automatically expand tree nodes (but *NOT* collapsing headers.. seems like sensible behavior).
2014-09-02 21:35:06 +08:00
// NB- If we are above max depth we still allow manually opened nodes to be logged.
2015-03-08 05:53:33 +08:00
if ( g . LogEnabled & & ! display_frame & & window - > DC . TreeDepth < g . LogAutoExpandMaxDepth )
opened = true ;
2014-08-19 19:09:13 +08:00
2015-01-23 06:54:05 +08:00
if ( ! ItemAdd ( bb , & id ) )
2014-08-19 19:09:13 +08:00
return opened ;
bool hovered , held ;
2015-03-15 09:48:59 +08:00
bool pressed = ButtonBehavior ( display_frame ? bb : text_bb , id , & hovered , & held , false ) ;
2014-08-19 19:09:13 +08:00
if ( pressed )
{
opened = ! opened ;
2014-12-30 02:17:54 +08:00
storage - > SetInt ( id , opened ) ;
2014-08-19 19:09:13 +08:00
}
// Render
const ImU32 col = window - > Color ( ( held & & hovered ) ? ImGuiCol_HeaderActive : hovered ? ImGuiCol_HeaderHovered : ImGuiCol_Header ) ;
if ( display_frame )
{
2014-09-25 03:39:39 +08:00
// Framed type
2015-01-15 18:29:51 +08:00
RenderFrame ( bb . Min , bb . Max , col , true , style . FrameRounding ) ;
2014-08-19 19:09:13 +08:00
RenderCollapseTriangle ( bb . Min + style . FramePadding , opened , 1.0f , true ) ;
2014-12-30 04:18:52 +08:00
if ( g . LogEnabled )
{
// NB: '##' is normally used to hide text (as a library-wide feature), so we need to specify the text range to make sure the ## aren't stripped out here.
const char log_prefix [ ] = " \n ## " ;
2015-07-31 13:48:59 +08:00
LogRenderedText ( bb . Min + style . FramePadding , log_prefix , log_prefix + 3 ) ;
2014-12-30 04:18:52 +08:00
}
2015-07-06 06:37:49 +08:00
RenderTextClipped ( bb . Min + style . FramePadding + ImVec2 ( g . FontSize + style . FramePadding . x * 2 , 0 ) , bb . Max , label , NULL , & label_size ) ;
2014-12-30 04:18:52 +08:00
if ( g . LogEnabled )
{
const char log_suffix [ ] = " ## " ;
2015-07-31 13:48:59 +08:00
LogRenderedText ( bb . Min + style . FramePadding , log_suffix , log_suffix + 2 ) ;
2014-12-30 04:18:52 +08:00
}
2014-08-19 19:09:13 +08:00
}
else
{
2014-09-25 03:39:39 +08:00
// Unframed typed for tree nodes
2014-08-19 19:09:13 +08:00
if ( ( held & & hovered ) | | hovered )
RenderFrame ( bb . Min , bb . Max , col , false ) ;
2015-03-30 02:45:03 +08:00
RenderCollapseTriangle ( bb . Min + ImVec2 ( style . FramePadding . x , g . FontSize * 0.15f ) , opened , 0.70f , false ) ;
2014-12-30 04:18:52 +08:00
if ( g . LogEnabled )
2015-07-31 13:48:59 +08:00
LogRenderedText ( bb . Min , " > " ) ;
2015-03-30 02:45:03 +08:00
RenderText ( bb . Min + ImVec2 ( g . FontSize + style . FramePadding . x * 2 , 0 ) , label ) ;
2014-08-19 19:09:13 +08:00
}
return opened ;
2014-08-11 04:58:11 +08:00
}
2015-02-24 04:59:22 +08:00
void ImGui : : Bullet ( )
{
ImGuiWindow * window = GetCurrentWindow ( ) ;
if ( window - > SkipItems )
return ;
2015-07-22 10:13:12 +08:00
ImGuiState & g = * GImGui ;
2015-02-24 04:59:22 +08:00
const ImGuiStyle & style = g . Style ;
2015-03-30 02:45:03 +08:00
const float line_height = g . FontSize ;
2015-03-18 07:35:12 +08:00
const ImRect bb ( window - > DC . CursorPos , window - > DC . CursorPos + ImVec2 ( line_height , line_height ) ) ;
2015-02-24 04:59:22 +08:00
ItemSize ( bb ) ;
if ( ! ItemAdd ( bb , NULL ) )
return ;
// Render
const float bullet_size = line_height * 0.15f ;
window - > DrawList - > AddCircleFilled ( bb . Min + ImVec2 ( style . FramePadding . x + line_height * 0.5f , line_height * 0.5f ) , bullet_size , window - > Color ( ImGuiCol_Text ) ) ;
// Stay on same line
ImGui : : SameLine ( 0 , - 1 ) ;
}
2014-09-02 21:35:06 +08:00
// Text with a little bullet aligned to the typical tree node.
2014-11-10 09:29:01 +08:00
void ImGui : : BulletTextV ( const char * fmt , va_list args )
2014-08-11 04:58:11 +08:00
{
2014-08-19 19:09:13 +08:00
ImGuiWindow * window = GetCurrentWindow ( ) ;
if ( window - > SkipItems )
return ;
2014-12-30 23:38:28 +08:00
2015-07-22 10:13:12 +08:00
ImGuiState & g = * GImGui ;
const ImGuiStyle & style = g . Style ;
2015-03-16 17:54:28 +08:00
const char * text_begin = g . TempBuffer ;
const char * text_end = text_begin + ImFormatStringV ( g . TempBuffer , IM_ARRAYSIZE ( g . TempBuffer ) , fmt , args ) ;
2015-03-30 02:45:03 +08:00
const float line_height = g . FontSize ;
2015-03-07 05:58:12 +08:00
const ImVec2 label_size = CalcTextSize ( text_begin , text_end , true ) ;
2015-03-18 07:35:12 +08:00
const ImRect bb ( window - > DC . CursorPos , window - > DC . CursorPos + ImVec2 ( line_height + ( label_size . x > 0.0f ? ( style . FramePadding . x * 2 ) : 0.0f ) , 0 ) + label_size ) ; // Empty text doesn't add padding
2014-08-19 19:09:13 +08:00
ItemSize ( bb ) ;
2015-01-23 06:54:05 +08:00
if ( ! ItemAdd ( bb , NULL ) )
2014-08-19 19:09:13 +08:00
return ;
2014-08-11 04:58:11 +08:00
2014-08-19 19:09:13 +08:00
// Render
const float bullet_size = line_height * 0.15f ;
2014-12-30 23:38:28 +08:00
window - > DrawList - > AddCircleFilled ( bb . Min + ImVec2 ( style . FramePadding . x + line_height * 0.5f , line_height * 0.5f ) , bullet_size , window - > Color ( ImGuiCol_Text ) ) ;
2015-03-30 02:45:03 +08:00
RenderText ( bb . Min + ImVec2 ( g . FontSize + style . FramePadding . x * 2 , 0 ) , text_begin , text_end ) ;
2014-08-11 04:58:11 +08:00
}
2014-11-10 09:29:01 +08:00
void ImGui : : BulletText ( const char * fmt , . . . )
2014-08-11 04:58:11 +08:00
{
2014-08-19 19:09:13 +08:00
va_list args ;
va_start ( args , fmt ) ;
2014-09-25 02:27:29 +08:00
BulletTextV ( fmt , args ) ;
2014-08-19 19:09:13 +08:00
va_end ( args ) ;
2014-09-25 02:27:29 +08:00
}
// If returning 'true' the node is open and the user is responsible for calling TreePop
2014-11-10 09:29:01 +08:00
bool ImGui : : TreeNodeV ( const char * str_id , const char * fmt , va_list args )
2014-09-25 02:27:29 +08:00
{
2015-03-16 08:46:03 +08:00
ImGuiWindow * window = GetCurrentWindow ( ) ;
if ( window - > SkipItems )
return false ;
2015-07-22 10:13:12 +08:00
ImGuiState & g = * GImGui ;
2015-03-16 17:54:28 +08:00
ImFormatStringV ( g . TempBuffer , IM_ARRAYSIZE ( g . TempBuffer ) , fmt , args ) ;
2014-08-19 19:09:13 +08:00
if ( ! str_id | | ! str_id [ 0 ] )
str_id = fmt ;
2014-08-11 04:58:11 +08:00
2014-08-19 19:09:13 +08:00
ImGui : : PushID ( str_id ) ;
2015-03-16 17:54:28 +08:00
const bool opened = ImGui : : CollapsingHeader ( g . TempBuffer , " " , false ) ;
2014-08-19 19:09:13 +08:00
ImGui : : PopID ( ) ;
2014-08-11 04:58:11 +08:00
2014-08-19 19:09:13 +08:00
if ( opened )
ImGui : : TreePush ( str_id ) ;
2014-08-11 04:58:11 +08:00
2014-08-19 19:09:13 +08:00
return opened ;
2014-08-11 04:58:11 +08:00
}
2014-11-10 09:29:01 +08:00
bool ImGui : : TreeNode ( const char * str_id , const char * fmt , . . . )
2014-08-11 04:58:11 +08:00
{
2014-08-19 19:09:13 +08:00
va_list args ;
va_start ( args , fmt ) ;
2014-09-25 02:27:29 +08:00
bool s = TreeNodeV ( str_id , fmt , args ) ;
2014-08-19 19:09:13 +08:00
va_end ( args ) ;
2014-09-25 02:27:29 +08:00
return s ;
}
// If returning 'true' the node is open and the user is responsible for calling TreePop
2014-11-10 09:29:01 +08:00
bool ImGui : : TreeNodeV ( const void * ptr_id , const char * fmt , va_list args )
2014-09-25 02:27:29 +08:00
{
2015-03-16 08:46:03 +08:00
ImGuiWindow * window = GetCurrentWindow ( ) ;
if ( window - > SkipItems )
return false ;
2015-07-22 10:13:12 +08:00
ImGuiState & g = * GImGui ;
2015-03-16 17:54:28 +08:00
ImFormatStringV ( g . TempBuffer , IM_ARRAYSIZE ( g . TempBuffer ) , fmt , args ) ;
2014-08-11 04:58:11 +08:00
2014-08-19 19:09:13 +08:00
if ( ! ptr_id )
ptr_id = fmt ;
2014-08-11 04:58:11 +08:00
2014-08-19 19:09:13 +08:00
ImGui : : PushID ( ptr_id ) ;
2015-03-16 17:54:28 +08:00
const bool opened = ImGui : : CollapsingHeader ( g . TempBuffer , " " , false ) ;
2014-08-19 19:09:13 +08:00
ImGui : : PopID ( ) ;
2014-08-11 04:58:11 +08:00
2014-08-19 19:09:13 +08:00
if ( opened )
ImGui : : TreePush ( ptr_id ) ;
2014-08-11 04:58:11 +08:00
2014-08-19 19:09:13 +08:00
return opened ;
2014-08-11 04:58:11 +08:00
}
2014-11-10 09:29:01 +08:00
bool ImGui : : TreeNode ( const void * ptr_id , const char * fmt , . . . )
2014-09-25 02:27:29 +08:00
{
va_list args ;
va_start ( args , fmt ) ;
bool s = TreeNodeV ( ptr_id , fmt , args ) ;
va_end ( args ) ;
return s ;
}
2014-11-10 09:29:01 +08:00
bool ImGui : : TreeNode ( const char * str_label_id )
2014-08-11 04:58:11 +08:00
{
2014-08-19 19:09:13 +08:00
return TreeNode ( str_label_id , " %s " , str_label_id ) ;
2014-08-11 04:58:11 +08:00
}
2015-02-27 17:51:11 +08:00
void ImGui : : SetNextTreeNodeOpened ( bool opened , ImGuiSetCond cond )
2014-08-11 04:58:11 +08:00
{
2015-02-27 17:38:22 +08:00
ImGuiState & g = * GImGui ;
g . SetNextTreeNodeOpenedVal = opened ;
2015-02-27 17:51:11 +08:00
g . SetNextTreeNodeOpenedCond = cond ? cond : ImGuiSetCond_Always ;
2014-08-11 04:58:11 +08:00
}
2014-11-10 09:29:01 +08:00
void ImGui : : PushID ( const char * str_id )
2014-08-11 04:58:11 +08:00
{
2014-08-19 19:09:13 +08:00
ImGuiWindow * window = GetCurrentWindow ( ) ;
window - > IDStack . push_back ( window - > GetID ( str_id ) ) ;
2014-08-11 04:58:11 +08:00
}
2015-04-13 21:45:27 +08:00
void ImGui : : PushID ( const char * str_id_begin , const char * str_id_end )
{
ImGuiWindow * window = GetCurrentWindow ( ) ;
window - > IDStack . push_back ( window - > GetID ( str_id_begin , str_id_end ) ) ;
}
2014-11-10 09:29:01 +08:00
void ImGui : : PushID ( const void * ptr_id )
2014-08-11 04:58:11 +08:00
{
2014-08-19 19:09:13 +08:00
ImGuiWindow * window = GetCurrentWindow ( ) ;
window - > IDStack . push_back ( window - > GetID ( ptr_id ) ) ;
2014-08-11 04:58:11 +08:00
}
2015-07-09 07:08:37 +08:00
void ImGui : : PushID ( int int_id )
2014-08-11 04:58:11 +08:00
{
2014-08-19 19:09:13 +08:00
const void * ptr_id = ( void * ) ( intptr_t ) int_id ;
ImGuiWindow * window = GetCurrentWindow ( ) ;
window - > IDStack . push_back ( window - > GetID ( ptr_id ) ) ;
2014-08-11 04:58:11 +08:00
}
2014-11-10 09:29:01 +08:00
void ImGui : : PopID ( )
2014-08-11 04:58:11 +08:00
{
2014-08-19 19:09:13 +08:00
ImGuiWindow * window = GetCurrentWindow ( ) ;
window - > IDStack . pop_back ( ) ;
2014-08-11 04:58:11 +08:00
}
2014-12-09 01:14:54 +08:00
ImGuiID ImGui : : GetID ( const char * str_id )
{
2015-05-28 05:26:03 +08:00
return GImGui - > CurrentWindow - > GetID ( str_id ) ;
2014-12-09 01:14:54 +08:00
}
2015-04-13 21:45:27 +08:00
ImGuiID ImGui : : GetID ( const char * str_id_begin , const char * str_id_end )
{
2015-05-28 05:26:03 +08:00
return GImGui - > CurrentWindow - > GetID ( str_id_begin , str_id_end ) ;
2015-04-13 21:45:27 +08:00
}
2014-12-09 01:14:54 +08:00
ImGuiID ImGui : : GetID ( const void * ptr_id )
{
2015-05-28 05:26:03 +08:00
return GImGui - > CurrentWindow - > GetID ( ptr_id ) ;
2014-12-09 01:14:54 +08:00
}
2014-09-02 21:35:06 +08:00
// User can input math operators (e.g. +100) to edit a numerical values.
2015-07-31 13:59:51 +08:00
static void InputTextApplyArithmeticOp ( const char * buf , const char * initial_value_buf , float * v )
2014-08-11 04:58:11 +08:00
{
2015-03-07 01:51:25 +08:00
while ( ImCharIsSpace ( * buf ) )
2014-08-19 19:09:13 +08:00
buf + + ;
// We don't support '-' op because it would conflict with inputing negative value.
// Instead you can use +-100 to subtract from an existing value
char op = buf [ 0 ] ;
if ( op = = ' + ' | | op = = ' * ' | | op = = ' / ' )
{
buf + + ;
2015-03-07 01:51:25 +08:00
while ( ImCharIsSpace ( * buf ) )
2014-08-19 19:09:13 +08:00
buf + + ;
}
else
{
op = 0 ;
}
if ( ! buf [ 0 ] )
return ;
float ref_v = * v ;
if ( op )
2015-07-31 13:59:51 +08:00
if ( sscanf ( initial_value_buf , " %f " , & ref_v ) < 1 )
2014-08-19 19:09:13 +08:00
return ;
float op_v = 0.0f ;
if ( sscanf ( buf , " %f " , & op_v ) < 1 )
return ;
if ( op = = ' + ' )
2015-06-20 08:09:58 +08:00
* v = ref_v + op_v ; // add (uses "+-" to substract)
2014-08-19 19:09:13 +08:00
else if ( op = = ' * ' )
2015-06-20 08:09:58 +08:00
* v = ref_v * op_v ; // multiply
2014-08-19 19:09:13 +08:00
else if ( op = = ' / ' )
{
2015-06-20 08:09:58 +08:00
if ( op_v = = 0.0f ) // divide
2014-08-19 19:09:13 +08:00
return ;
* v = ref_v / op_v ;
}
else
2015-06-20 08:09:58 +08:00
* v = op_v ; // Constant
2014-08-11 04:58:11 +08:00
}
2015-03-15 07:33:12 +08:00
// Create text input in place of a slider (when CTRL+Clicking on slider)
2015-08-02 11:57:24 +08:00
bool ImGui : : InputFloatReplaceWidget ( const ImRect & aabb , const char * label , float * v , ImGuiID id , int decimal_precision )
2014-08-11 04:58:11 +08:00
{
2015-02-01 12:28:24 +08:00
ImGuiState & g = * GImGui ;
2014-08-19 19:09:13 +08:00
ImGuiWindow * window = GetCurrentWindow ( ) ;
2014-08-11 04:58:11 +08:00
2015-03-13 21:23:03 +08:00
char text_buf [ 64 ] ;
ImFormatString ( text_buf , IM_ARRAYSIZE ( text_buf ) , " %.*f " , decimal_precision , * v ) ;
2014-08-11 04:58:11 +08:00
2015-07-22 07:00:28 +08:00
SetActiveID ( g . ScalarAsInputTextId , window ) ;
2015-03-13 21:23:03 +08:00
g . HoveredId = 0 ;
2014-08-11 04:58:11 +08:00
2015-03-13 21:23:03 +08:00
// Our replacement widget will override the focus ID (registered previously to allow for a TAB focus to happen)
2015-07-22 07:00:28 +08:00
FocusableItemUnregister ( window ) ;
2015-03-13 21:23:03 +08:00
2015-07-31 11:51:55 +08:00
bool value_changed = InputTextEx ( label , text_buf , ( int ) IM_ARRAYSIZE ( text_buf ) , aabb . GetSize ( ) - g . Style . FramePadding * 2.0f , ImGuiInputTextFlags_CharsDecimal | ImGuiInputTextFlags_AutoSelectAll ) ;
2015-04-03 04:21:35 +08:00
if ( g . ScalarAsInputTextId = = 0 )
2014-08-19 19:09:13 +08:00
{
2015-03-13 21:23:03 +08:00
// First frame
IM_ASSERT ( g . ActiveId = = id ) ; // InputText ID expected to match the Slider ID (else we'd need to store them both, which is also possible)
2015-04-03 04:21:35 +08:00
g . ScalarAsInputTextId = g . ActiveId ;
2015-03-13 21:23:03 +08:00
g . HoveredId = id ;
}
2015-04-03 04:21:35 +08:00
else if ( g . ActiveId ! = g . ScalarAsInputTextId )
2015-03-13 21:23:03 +08:00
{
// Release
2015-04-03 04:21:35 +08:00
g . ScalarAsInputTextId = 0 ;
2015-03-13 21:23:03 +08:00
}
if ( value_changed )
2015-07-31 13:59:51 +08:00
InputTextApplyArithmeticOp ( text_buf , g . InputTextState . InitialText . Data , v ) ;
2015-03-13 21:23:03 +08:00
return value_changed ;
}
2015-03-14 18:30:07 +08:00
// Parse display precision back from the display format string
2015-08-02 11:57:24 +08:00
inline int ImGui : : ParseFormatPrecision ( const char * fmt , int default_precision )
2015-03-14 18:30:07 +08:00
{
2015-07-31 13:48:59 +08:00
int precision = default_precision ;
2015-03-14 18:30:07 +08:00
while ( ( fmt = strchr ( fmt , ' % ' ) ) ! = NULL )
{
fmt + + ;
if ( fmt [ 0 ] = = ' % ' ) { fmt + + ; continue ; } // Ignore "%%"
while ( * fmt > = ' 0 ' & & * fmt < = ' 9 ' )
fmt + + ;
if ( * fmt = = ' . ' )
2014-08-19 19:09:13 +08:00
{
2015-07-31 13:48:59 +08:00
precision = atoi ( fmt + 1 ) ;
if ( precision < 0 | | precision > 10 )
precision = default_precision ;
2014-08-19 19:09:13 +08:00
}
2015-03-14 18:30:07 +08:00
break ;
2014-08-19 19:09:13 +08:00
}
2015-07-31 13:48:59 +08:00
return precision ;
2015-03-14 18:30:07 +08:00
}
2014-08-14 01:38:24 +08:00
2015-08-02 11:57:24 +08:00
inline float ImGui : : RoundScalar ( float value , int decimal_precision )
2015-04-13 02:15:55 +08:00
{
// Round past decimal precision
// 0: 1, 1: 0.1, 2: 0.01, etc.
// So when our value is 1.99999 with a precision of 0.001 we'll end up rounding to 2.0
// FIXME: Investigate better rounding methods
2015-06-03 22:31:08 +08:00
float min_step = 1.0f / powf ( 10.0f , ( float ) decimal_precision ) ;
bool negative = value < 0.0f ;
value = fabsf ( value ) ;
float remainder = fmodf ( value , min_step ) ;
2015-04-13 02:15:55 +08:00
if ( remainder < = min_step * 0.5f )
value - = remainder ;
else
value + = ( min_step - remainder ) ;
2015-06-03 22:31:08 +08:00
return negative ? - value : value ;
2015-04-13 02:15:55 +08:00
}
2015-08-02 11:57:24 +08:00
bool ImGui : : SliderBehavior ( const ImRect & frame_bb , ImGuiID id , float * v , float v_min , float v_max , float power , int decimal_precision , bool horizontal )
2014-08-11 04:58:11 +08:00
{
2015-02-01 12:28:24 +08:00
ImGuiState & g = * GImGui ;
2014-08-19 19:09:13 +08:00
ImGuiWindow * window = GetCurrentWindow ( ) ;
const ImGuiStyle & style = g . Style ;
2015-03-15 07:55:07 +08:00
// Draw frame
RenderFrame ( frame_bb . Min , frame_bb . Max , window - > Color ( ImGuiCol_FrameBg ) , true , style . FrameRounding ) ;
2014-11-07 13:45:56 +08:00
2015-03-19 17:06:34 +08:00
const bool is_non_linear = fabsf ( power - 1.0f ) > 0.0001f ;
2014-08-19 19:09:13 +08:00
2015-08-06 01:00:00 +08:00
const float grab_padding = 2.0f ;
const float slider_sz = horizontal ? ( frame_bb . GetWidth ( ) - grab_padding * 2.0f ) : ( frame_bb . GetHeight ( ) - grab_padding * 2.0f ) ;
2015-03-15 09:47:12 +08:00
float grab_sz ;
2015-04-03 22:10:07 +08:00
if ( decimal_precision > 0 )
2015-04-03 05:38:37 +08:00
grab_sz = ImMin ( style . GrabMinSize , slider_sz ) ;
2014-08-19 19:09:13 +08:00
else
2015-04-03 05:38:37 +08:00
grab_sz = ImMin ( ImMax ( 1.0f * ( slider_sz / ( v_max - v_min + 1.0f ) ) , style . GrabMinSize ) , slider_sz ) ; // Integer sliders, if possible have the grab size represent 1 unit
2015-03-15 09:47:12 +08:00
const float slider_usable_sz = slider_sz - grab_sz ;
2015-08-06 01:00:00 +08:00
const float slider_usable_pos_min = ( horizontal ? frame_bb . Min . x : frame_bb . Min . y ) + grab_padding + grab_sz * 0.5f ;
const float slider_usable_pos_max = ( horizontal ? frame_bb . Max . x : frame_bb . Max . y ) - grab_padding - grab_sz * 0.5f ;
2014-08-19 19:09:13 +08:00
2015-04-03 22:10:07 +08:00
// For logarithmic sliders that cross over sign boundary we want the exponential increase to be symmetric around 0.0f
float linear_zero_pos = 0.0f ; // 0.0->1.0f
if ( v_min * v_max < 0.0f )
2014-08-19 19:09:13 +08:00
{
2015-04-03 22:10:07 +08:00
// Different sign
const float linear_dist_min_to_0 = powf ( fabsf ( 0.0f - v_min ) , 1.0f / power ) ;
const float linear_dist_max_to_0 = powf ( fabsf ( v_max - 0.0f ) , 1.0f / power ) ;
linear_zero_pos = linear_dist_min_to_0 / ( linear_dist_min_to_0 + linear_dist_max_to_0 ) ;
}
else
{
// Same sign
linear_zero_pos = v_min < 0.0f ? 1.0f : 0.0f ;
}
2014-08-19 19:09:13 +08:00
2015-04-03 22:10:07 +08:00
// Process clicking on the slider
2015-08-06 05:13:13 +08:00
bool value_changed = false ;
2015-04-03 22:10:07 +08:00
if ( g . ActiveId = = id )
{
if ( g . IO . MouseDown [ 0 ] )
2014-08-19 19:09:13 +08:00
{
2015-04-03 22:10:07 +08:00
const float mouse_abs_pos = horizontal ? g . IO . MousePos . x : g . IO . MousePos . y ;
float normalized_pos = ImClamp ( ( mouse_abs_pos - slider_usable_pos_min ) / slider_usable_sz , 0.0f , 1.0f ) ;
if ( ! horizontal )
normalized_pos = 1.0f - normalized_pos ;
2014-08-19 19:09:13 +08:00
2015-04-03 22:10:07 +08:00
float new_value ;
if ( is_non_linear )
{
// Account for logarithmic scale on both sides of the zero
if ( normalized_pos < linear_zero_pos )
2014-08-19 19:09:13 +08:00
{
2015-04-03 22:10:07 +08:00
// Negative: rescale to the negative range before powering
float a = 1.0f - ( normalized_pos / linear_zero_pos ) ;
a = powf ( a , power ) ;
2015-04-19 02:34:05 +08:00
new_value = ImLerp ( ImMin ( v_max , 0.0f ) , v_min , a ) ;
2014-08-19 19:09:13 +08:00
}
else
{
2015-04-03 22:10:07 +08:00
// Positive: rescale to the positive range before powering
float a ;
if ( fabsf ( linear_zero_pos - 1.0f ) > 1.e-6 )
a = ( normalized_pos - linear_zero_pos ) / ( 1.0f - linear_zero_pos ) ;
else
a = normalized_pos ;
a = powf ( a , power ) ;
new_value = ImLerp ( ImMax ( v_min , 0.0f ) , v_max , a ) ;
2014-08-19 19:09:13 +08:00
}
}
2015-03-14 17:36:15 +08:00
else
{
2015-04-03 22:10:07 +08:00
// Linear slider
new_value = ImLerp ( v_min , v_max , normalized_pos ) ;
2014-08-19 19:09:13 +08:00
}
2015-04-03 22:10:07 +08:00
// Round past decimal precision
2015-04-13 02:15:55 +08:00
new_value = RoundScalar ( new_value , decimal_precision ) ;
2015-04-03 22:10:07 +08:00
if ( * v ! = new_value )
2015-03-13 21:28:30 +08:00
{
2015-04-03 22:10:07 +08:00
* v = new_value ;
value_changed = true ;
2015-03-13 21:28:30 +08:00
}
2014-08-19 19:09:13 +08:00
}
else
{
2015-07-22 07:00:28 +08:00
SetActiveID ( 0 ) ;
2014-08-19 19:09:13 +08:00
}
2015-04-03 22:10:07 +08:00
}
2014-08-19 19:09:13 +08:00
2015-04-03 22:10:07 +08:00
// Calculate slider grab positioning
float grab_t ;
if ( is_non_linear )
{
float v_clamped = ImClamp ( * v , v_min , v_max ) ;
if ( v_clamped < 0.0f )
{
const float f = 1.0f - ( v_clamped - v_min ) / ( ImMin ( 0.0f , v_max ) - v_min ) ;
grab_t = ( 1.0f - powf ( f , 1.0f / power ) ) * linear_zero_pos ;
}
2015-03-15 09:47:12 +08:00
else
2015-04-03 22:10:07 +08:00
{
const float f = ( v_clamped - ImMax ( 0.0f , v_min ) ) / ( v_max - ImMax ( 0.0f , v_min ) ) ;
grab_t = linear_zero_pos + powf ( f , 1.0f / power ) * ( 1.0f - linear_zero_pos ) ;
}
2014-08-19 19:09:13 +08:00
}
2015-04-03 22:10:07 +08:00
else
{
// Linear slider
grab_t = ( ImClamp ( * v , v_min , v_max ) - v_min ) / ( v_max - v_min ) ;
}
// Draw
if ( ! horizontal )
grab_t = 1.0f - grab_t ;
const float grab_pos = ImLerp ( slider_usable_pos_min , slider_usable_pos_max , grab_t ) ;
ImRect grab_bb ;
if ( horizontal )
2015-08-06 01:00:00 +08:00
grab_bb = ImRect ( ImVec2 ( grab_pos - grab_sz * 0.5f , frame_bb . Min . y + grab_padding ) , ImVec2 ( grab_pos + grab_sz * 0.5f , frame_bb . Max . y - grab_padding ) ) ;
2015-04-03 22:10:07 +08:00
else
2015-08-06 01:00:00 +08:00
grab_bb = ImRect ( ImVec2 ( frame_bb . Min . x + grab_padding , grab_pos - grab_sz * 0.5f ) , ImVec2 ( frame_bb . Max . x - grab_padding , grab_pos + grab_sz * 0.5f ) ) ;
2015-07-01 05:02:59 +08:00
window - > DrawList - > AddRectFilled ( grab_bb . Min , grab_bb . Max , window - > Color ( g . ActiveId = = id ? ImGuiCol_SliderGrabActive : ImGuiCol_SliderGrab ) , style . GrabRounding ) ;
2014-08-19 19:09:13 +08:00
2015-03-15 07:55:07 +08:00
return value_changed ;
}
// Use power!=1.0 for logarithmic sliders.
// Adjust display_format to decorate the value with a prefix or a suffix.
// "%.3f" 1.234
// "%5.2f secs" 01.23 secs
// "Gold: %.0f" Gold: 1
bool ImGui : : SliderFloat ( const char * label , float * v , float v_min , float v_max , const char * display_format , float power )
{
ImGuiWindow * window = GetCurrentWindow ( ) ;
if ( window - > SkipItems )
return false ;
2015-07-22 10:13:12 +08:00
ImGuiState & g = * GImGui ;
2015-03-15 07:55:07 +08:00
const ImGuiStyle & style = g . Style ;
const ImGuiID id = window - > GetID ( label ) ;
const float w = ImGui : : CalcItemWidth ( ) ;
const ImVec2 label_size = CalcTextSize ( label , NULL , true ) ;
2015-03-18 07:35:12 +08:00
const ImRect frame_bb ( window - > DC . CursorPos , window - > DC . CursorPos + ImVec2 ( w , label_size . y ) + style . FramePadding * 2.0f ) ;
2015-03-19 22:48:36 +08:00
const ImRect total_bb ( frame_bb . Min , frame_bb . Max + ImVec2 ( label_size . x > 0.0f ? style . ItemInnerSpacing . x + label_size . x : 0.0f , 0.0f ) ) ;
2015-03-15 07:55:07 +08:00
2015-04-03 05:38:37 +08:00
// NB- we don't call ItemSize() yet because we may turn into a text edit box below
2015-03-19 22:48:36 +08:00
if ( ! ItemAdd ( total_bb , & id ) )
2015-03-15 07:55:07 +08:00
{
2015-03-19 22:48:36 +08:00
ItemSize ( total_bb , style . FramePadding . y ) ;
2015-03-15 07:55:07 +08:00
return false ;
}
2015-03-15 09:36:29 +08:00
const bool hovered = IsHovered ( frame_bb , id ) ;
2015-03-15 07:55:07 +08:00
if ( hovered )
g . HoveredId = id ;
if ( ! display_format )
display_format = " %.3f " ;
2015-07-31 13:48:59 +08:00
int decimal_precision = ParseFormatPrecision ( display_format , 3 ) ;
2015-03-15 07:55:07 +08:00
2015-04-03 21:07:52 +08:00
// Tabbing or CTRL-clicking on Slider turns it into an input box
2015-03-15 07:55:07 +08:00
bool start_text_input = false ;
2015-07-22 07:00:28 +08:00
const bool tab_focus_requested = FocusableItemRegister ( window , g . ActiveId = = id ) ;
2015-03-15 07:55:07 +08:00
if ( tab_focus_requested | | ( hovered & & g . IO . MouseClicked [ 0 ] ) )
{
2015-07-22 07:00:28 +08:00
SetActiveID ( id , window ) ;
2015-03-15 07:55:07 +08:00
FocusWindow ( window ) ;
2015-07-31 12:49:10 +08:00
if ( tab_focus_requested | | g . IO . KeyCtrl )
2015-03-15 07:55:07 +08:00
{
start_text_input = true ;
2015-04-03 04:21:35 +08:00
g . ScalarAsInputTextId = 0 ;
2015-03-15 07:55:07 +08:00
}
}
2015-04-03 04:21:35 +08:00
if ( start_text_input | | ( g . ActiveId = = id & & g . ScalarAsInputTextId = = id ) )
2015-07-31 12:59:30 +08:00
return InputFloatReplaceWidget ( frame_bb , label , v , id , decimal_precision ) ;
2015-03-15 07:55:07 +08:00
2015-03-19 22:48:36 +08:00
ItemSize ( total_bb , style . FramePadding . y ) ;
2015-03-15 07:55:07 +08:00
// Actual slider behavior + render grab
2015-06-30 03:18:44 +08:00
const bool value_changed = SliderBehavior ( frame_bb , id , v , v_min , v_max , power , decimal_precision , true ) ;
2015-03-15 07:55:07 +08:00
2015-03-14 17:36:15 +08:00
// Display value using user-provided display format so user can add prefix/suffix/decorations to the value.
2014-08-19 19:09:13 +08:00
char value_buf [ 64 ] ;
2015-04-03 05:39:37 +08:00
const char * value_buf_end = value_buf + ImFormatString ( value_buf , IM_ARRAYSIZE ( value_buf ) , display_format , * v ) ;
2015-06-17 07:21:07 +08:00
RenderTextClipped ( frame_bb . Min , frame_bb . Max , value_buf , value_buf_end , NULL , ImGuiAlign_Center | ImGuiAlign_VCenter ) ;
2014-08-19 19:09:13 +08:00
2015-04-03 05:39:37 +08:00
if ( label_size . x > 0.0f )
2015-04-19 02:34:05 +08:00
RenderText ( ImVec2 ( frame_bb . Max . x + style . ItemInnerSpacing . x , frame_bb . Min . y + style . FramePadding . y ) , label ) ;
2014-08-19 19:09:13 +08:00
return value_changed ;
2014-08-11 04:58:11 +08:00
}
2015-03-15 09:58:14 +08:00
bool ImGui : : VSliderFloat ( const char * label , const ImVec2 & size , float * v , float v_min , float v_max , const char * display_format , float power )
{
ImGuiWindow * window = GetCurrentWindow ( ) ;
if ( window - > SkipItems )
return false ;
2015-07-22 10:13:12 +08:00
ImGuiState & g = * GImGui ;
2015-03-15 09:58:14 +08:00
const ImGuiStyle & style = g . Style ;
const ImGuiID id = window - > GetID ( label ) ;
const ImVec2 label_size = CalcTextSize ( label , NULL , true ) ;
2015-03-18 07:35:12 +08:00
const ImRect frame_bb ( window - > DC . CursorPos , window - > DC . CursorPos + size ) ;
const ImRect bb ( frame_bb . Min , frame_bb . Max + ImVec2 ( label_size . x > 0.0f ? style . ItemInnerSpacing . x + label_size . x : 0.0f , 0.0f ) ) ;
2015-03-15 09:58:14 +08:00
2015-03-18 04:17:53 +08:00
ItemSize ( bb , style . FramePadding . y ) ;
2015-03-15 09:58:14 +08:00
if ( ! ItemAdd ( frame_bb , & id ) )
return false ;
const bool hovered = IsHovered ( frame_bb , id ) ;
if ( hovered )
g . HoveredId = id ;
if ( ! display_format )
display_format = " %.3f " ;
2015-07-31 13:48:59 +08:00
int decimal_precision = ParseFormatPrecision ( display_format , 3 ) ;
2015-03-15 09:58:14 +08:00
if ( hovered & & g . IO . MouseClicked [ 0 ] )
{
2015-07-22 07:00:28 +08:00
SetActiveID ( id , window ) ;
2015-03-15 09:58:14 +08:00
FocusWindow ( window ) ;
}
// Actual slider behavior + render grab
2015-06-30 03:18:44 +08:00
bool value_changed = SliderBehavior ( frame_bb , id , v , v_min , v_max , power , decimal_precision , false ) ;
2015-03-15 09:58:14 +08:00
// Display value using user-provided display format so user can add prefix/suffix/decorations to the value.
// For the vertical slider we allow centered text to overlap the frame padding
char value_buf [ 64 ] ;
char * value_buf_end = value_buf + ImFormatString ( value_buf , IM_ARRAYSIZE ( value_buf ) , display_format , * v ) ;
2015-06-17 07:21:07 +08:00
RenderTextClipped ( ImVec2 ( frame_bb . Min . x , frame_bb . Min . y + style . FramePadding . y ) , frame_bb . Max , value_buf , value_buf_end , NULL , ImGuiAlign_Center ) ;
2015-03-15 09:58:14 +08:00
if ( label_size . x > 0.0f )
2015-04-19 02:34:05 +08:00
RenderText ( ImVec2 ( frame_bb . Max . x + style . ItemInnerSpacing . x , frame_bb . Min . y + style . FramePadding . y ) , label ) ;
2015-03-15 09:58:14 +08:00
return value_changed ;
}
2015-04-03 17:22:18 +08:00
bool ImGui : : SliderAngle ( const char * label , float * v_rad , float v_degrees_min , float v_degrees_max )
2014-08-11 04:58:11 +08:00
{
2015-06-30 00:39:17 +08:00
float v_deg = ( * v_rad ) * 360.0f / ( 2 * IM_PI ) ;
2014-11-24 18:02:39 +08:00
bool value_changed = ImGui : : SliderFloat ( label , & v_deg , v_degrees_min , v_degrees_max , " %.0f deg " , 1.0f ) ;
2015-06-30 00:39:17 +08:00
* v_rad = v_deg * ( 2 * IM_PI ) / 360.0f ;
2014-11-24 18:02:39 +08:00
return value_changed ;
2014-08-11 04:58:11 +08:00
}
2014-11-10 09:29:01 +08:00
bool ImGui : : SliderInt ( const char * label , int * v , int v_min , int v_max , const char * display_format )
2014-08-11 04:58:11 +08:00
{
2014-08-19 19:09:13 +08:00
if ( ! display_format )
display_format = " %.0f " ;
float v_f = ( float ) * v ;
2014-11-24 18:02:39 +08:00
bool value_changed = ImGui : : SliderFloat ( label , & v_f , ( float ) v_min , ( float ) v_max , display_format , 1.0f ) ;
2014-08-19 19:09:13 +08:00
* v = ( int ) v_f ;
2014-11-24 18:02:39 +08:00
return value_changed ;
2014-08-11 04:58:11 +08:00
}
2015-03-18 17:39:09 +08:00
bool ImGui : : VSliderInt ( const char * label , const ImVec2 & size , int * v , int v_min , int v_max , const char * display_format )
{
if ( ! display_format )
display_format = " %.0f " ;
float v_f = ( float ) * v ;
bool value_changed = ImGui : : VSliderFloat ( label , size , & v_f , ( float ) v_min , ( float ) v_max , display_format , 1.0f ) ;
* v = ( int ) v_f ;
return value_changed ;
}
2014-11-24 18:02:39 +08:00
// Add multiple sliders on 1 line for compact edition of multiple components
2015-08-02 11:57:24 +08:00
bool ImGui : : SliderFloatN ( const char * label , float * v , int components , float v_min , float v_max , const char * display_format , float power )
2014-08-11 04:58:11 +08:00
{
2014-08-19 19:09:13 +08:00
ImGuiWindow * window = GetCurrentWindow ( ) ;
if ( window - > SkipItems )
return false ;
2015-07-22 10:13:12 +08:00
ImGuiState & g = * GImGui ;
2014-08-19 19:09:13 +08:00
bool value_changed = false ;
2015-03-19 22:48:36 +08:00
ImGui : : BeginGroup ( ) ;
2014-08-19 19:09:13 +08:00
ImGui : : PushID ( label ) ;
2015-05-12 04:44:50 +08:00
PushMultiItemsWidths ( components ) ;
2014-08-19 19:09:13 +08:00
for ( int i = 0 ; i < components ; i + + )
{
ImGui : : PushID ( i ) ;
value_changed | = ImGui : : SliderFloat ( " ##v " , & v [ i ] , v_min , v_max , display_format , power ) ;
2015-07-11 09:36:34 +08:00
ImGui : : SameLine ( 0 , g . Style . ItemInnerSpacing . x ) ;
2014-08-19 19:09:13 +08:00
ImGui : : PopID ( ) ;
2015-05-12 04:44:50 +08:00
ImGui : : PopItemWidth ( ) ;
2014-08-19 19:09:13 +08:00
}
ImGui : : PopID ( ) ;
ImGui : : TextUnformatted ( label , FindTextDisplayEnd ( label ) ) ;
2015-03-19 22:48:36 +08:00
ImGui : : EndGroup ( ) ;
2014-08-19 19:09:13 +08:00
return value_changed ;
2014-08-14 01:38:24 +08:00
}
2014-11-10 09:29:01 +08:00
bool ImGui : : SliderFloat2 ( const char * label , float v [ 2 ] , float v_min , float v_max , const char * display_format , float power )
2014-08-14 01:38:24 +08:00
{
2014-08-19 19:09:13 +08:00
return SliderFloatN ( label , v , 2 , v_min , v_max , display_format , power ) ;
2014-08-17 21:16:10 +08:00
}
2014-08-11 04:58:11 +08:00
2014-11-10 09:29:01 +08:00
bool ImGui : : SliderFloat3 ( const char * label , float v [ 3 ] , float v_min , float v_max , const char * display_format , float power )
2014-08-17 21:16:10 +08:00
{
2014-08-19 19:09:13 +08:00
return SliderFloatN ( label , v , 3 , v_min , v_max , display_format , power ) ;
2014-08-17 21:16:10 +08:00
}
2014-08-11 04:58:11 +08:00
2014-11-10 09:29:01 +08:00
bool ImGui : : SliderFloat4 ( const char * label , float v [ 4 ] , float v_min , float v_max , const char * display_format , float power )
2014-08-17 21:16:10 +08:00
{
2014-08-19 19:09:13 +08:00
return SliderFloatN ( label , v , 4 , v_min , v_max , display_format , power ) ;
2014-08-11 04:58:11 +08:00
}
2015-08-02 11:57:24 +08:00
bool ImGui : : SliderIntN ( const char * label , int * v , int components , int v_min , int v_max , const char * display_format )
2014-12-30 19:54:04 +08:00
{
ImGuiWindow * window = GetCurrentWindow ( ) ;
if ( window - > SkipItems )
return false ;
2015-07-22 10:13:12 +08:00
ImGuiState & g = * GImGui ;
2014-12-30 19:54:04 +08:00
bool value_changed = false ;
2015-03-19 22:48:36 +08:00
ImGui : : BeginGroup ( ) ;
2014-12-30 19:54:04 +08:00
ImGui : : PushID ( label ) ;
2015-05-12 04:44:50 +08:00
PushMultiItemsWidths ( components ) ;
2014-12-30 19:54:04 +08:00
for ( int i = 0 ; i < components ; i + + )
{
ImGui : : PushID ( i ) ;
value_changed | = ImGui : : SliderInt ( " ##v " , & v [ i ] , v_min , v_max , display_format ) ;
2015-07-11 09:36:34 +08:00
ImGui : : SameLine ( 0 , g . Style . ItemInnerSpacing . x ) ;
2014-12-30 19:54:04 +08:00
ImGui : : PopID ( ) ;
2015-05-12 04:44:50 +08:00
ImGui : : PopItemWidth ( ) ;
2014-12-30 19:54:04 +08:00
}
ImGui : : PopID ( ) ;
ImGui : : TextUnformatted ( label , FindTextDisplayEnd ( label ) ) ;
2015-03-19 22:48:36 +08:00
ImGui : : EndGroup ( ) ;
2014-12-30 19:54:04 +08:00
return value_changed ;
}
bool ImGui : : SliderInt2 ( const char * label , int v [ 2 ] , int v_min , int v_max , const char * display_format )
{
return SliderIntN ( label , v , 2 , v_min , v_max , display_format ) ;
}
bool ImGui : : SliderInt3 ( const char * label , int v [ 3 ] , int v_min , int v_max , const char * display_format )
{
return SliderIntN ( label , v , 3 , v_min , v_max , display_format ) ;
}
bool ImGui : : SliderInt4 ( const char * label , int v [ 4 ] , int v_min , int v_max , const char * display_format )
{
return SliderIntN ( label , v , 4 , v_min , v_max , display_format ) ;
}
2015-08-02 11:57:24 +08:00
bool ImGui : : DragBehavior ( const ImRect & frame_bb , ImGuiID id , float * v , float v_speed , float v_min , float v_max , int decimal_precision , float power )
2015-04-03 21:07:52 +08:00
{
ImGuiState & g = * GImGui ;
ImGuiWindow * window = GetCurrentWindow ( ) ;
const ImGuiStyle & style = g . Style ;
// Draw frame
const ImU32 frame_col = window - > Color ( g . ActiveId = = id ? ImGuiCol_FrameBgActive : g . HoveredId = = id ? ImGuiCol_FrameBgHovered : ImGuiCol_FrameBg ) ;
RenderFrame ( frame_bb . Min , frame_bb . Max , frame_col , true , style . FrameRounding ) ;
bool value_changed = false ;
2015-04-20 05:57:39 +08:00
// Process clicking on the drag
2015-04-03 21:07:52 +08:00
if ( g . ActiveId = = id )
{
if ( g . IO . MouseDown [ 0 ] )
{
2015-04-13 02:15:55 +08:00
if ( g . ActiveIdIsJustActivated )
{
// Lock current value on click
g . DragCurrentValue = * v ;
g . DragLastMouseDelta = ImVec2 ( 0.f , 0.f ) ;
}
2015-04-03 21:07:52 +08:00
2015-04-13 02:15:55 +08:00
const ImVec2 mouse_drag_delta = ImGui : : GetMouseDragDelta ( 0 , 1.0f ) ;
2015-04-03 21:07:52 +08:00
if ( fabsf ( mouse_drag_delta . x - g . DragLastMouseDelta . x ) > 0.0f )
{
2015-04-13 07:03:57 +08:00
float speed = v_speed ;
2015-04-17 17:01:39 +08:00
if ( speed = = 0.0f & & ( v_max - v_min ) ! = 0.0f & & ( v_max - v_min ) < FLT_MAX )
speed = ( v_max - v_min ) * g . DragSpeedDefaultRatio ;
2015-04-03 21:07:52 +08:00
if ( g . IO . KeyShift & & g . DragSpeedScaleFast > = 0.0f )
2015-04-17 17:01:39 +08:00
speed = speed * g . DragSpeedScaleFast ;
2015-04-03 21:07:52 +08:00
if ( g . IO . KeyAlt & & g . DragSpeedScaleSlow > = 0.0f )
2015-04-17 17:01:39 +08:00
speed = speed * g . DragSpeedScaleSlow ;
2015-04-03 21:07:52 +08:00
2015-04-13 07:03:57 +08:00
float v_cur = g . DragCurrentValue ;
float delta = ( mouse_drag_delta . x - g . DragLastMouseDelta . x ) * speed ;
if ( fabsf ( power - 1.0f ) > 0.001f )
{
// Logarithmic curve on both side of 0.0
float v0_abs = v_cur > = 0.0f ? v_cur : - v_cur ;
float v0_sign = v_cur > = 0.0f ? 1.0f : - 1.0f ;
float v1 = powf ( v0_abs , 1.0f / power ) + ( delta * v0_sign ) ;
float v1_abs = v1 > = 0.0f ? v1 : - v1 ;
float v1_sign = v1 > = 0.0f ? 1.0f : - 1.0f ; // Crossed sign line
v_cur = powf ( v1_abs , power ) * v0_sign * v1_sign ; // Reapply sign
}
else
{
v_cur + = delta ;
}
2015-04-13 02:15:55 +08:00
g . DragLastMouseDelta . x = mouse_drag_delta . x ;
2015-04-03 22:13:59 +08:00
2015-04-13 07:03:57 +08:00
// Clamp
2015-04-03 22:13:59 +08:00
if ( v_min < v_max )
2015-04-13 07:07:41 +08:00
v_cur = ImClamp ( v_cur , v_min , v_max ) ;
2015-04-13 07:03:57 +08:00
g . DragCurrentValue = v_cur ;
2015-04-03 21:07:52 +08:00
2015-04-13 07:03:57 +08:00
// Round to user desired precision, then apply
v_cur = RoundScalar ( v_cur , decimal_precision ) ;
if ( * v ! = v_cur )
2015-04-13 02:15:55 +08:00
{
2015-04-13 07:03:57 +08:00
* v = v_cur ;
2015-04-13 02:15:55 +08:00
value_changed = true ;
}
2015-04-03 21:07:52 +08:00
}
}
else
{
2015-07-22 07:00:28 +08:00
SetActiveID ( 0 ) ;
2015-04-03 21:07:52 +08:00
}
}
return value_changed ;
}
2015-08-06 05:13:13 +08:00
bool ImGui : : DragFloat ( const char * label , float * v , float v_speed , float v_min , float v_max , const char * display_format , float power )
2015-04-03 21:07:52 +08:00
{
ImGuiWindow * window = GetCurrentWindow ( ) ;
if ( window - > SkipItems )
return false ;
2015-07-22 10:13:12 +08:00
ImGuiState & g = * GImGui ;
2015-04-03 21:07:52 +08:00
const ImGuiStyle & style = g . Style ;
const ImGuiID id = window - > GetID ( label ) ;
const float w = ImGui : : CalcItemWidth ( ) ;
const ImVec2 label_size = CalcTextSize ( label , NULL , true ) ;
const ImRect frame_bb ( window - > DC . CursorPos , window - > DC . CursorPos + ImVec2 ( w , label_size . y ) + style . FramePadding * 2.0f ) ;
const ImRect inner_bb ( frame_bb . Min + style . FramePadding , frame_bb . Max - style . FramePadding ) ;
const ImRect total_bb ( frame_bb . Min , frame_bb . Max + ImVec2 ( label_size . x > 0.0f ? style . ItemInnerSpacing . x + label_size . x : 0.0f , 0.0f ) ) ;
// NB- we don't call ItemSize() yet because we may turn into a text edit box below
if ( ! ItemAdd ( total_bb , & id ) )
{
ItemSize ( total_bb , style . FramePadding . y ) ;
return false ;
}
const bool hovered = IsHovered ( frame_bb , id ) ;
if ( hovered )
g . HoveredId = id ;
if ( ! display_format )
display_format = " %.3f " ;
2015-07-31 13:48:59 +08:00
int decimal_precision = ParseFormatPrecision ( display_format , 3 ) ;
2015-04-03 21:07:52 +08:00
// Tabbing or CTRL-clicking on Drag turns it into an input box
bool start_text_input = false ;
2015-07-22 07:00:28 +08:00
const bool tab_focus_requested = FocusableItemRegister ( window , g . ActiveId = = id ) ;
2015-04-03 22:15:12 +08:00
if ( tab_focus_requested | | ( hovered & & ( g . IO . MouseClicked [ 0 ] | g . IO . MouseDoubleClicked [ 0 ] ) ) )
2015-04-03 21:07:52 +08:00
{
2015-07-22 07:00:28 +08:00
SetActiveID ( id , window ) ;
2015-04-03 21:07:52 +08:00
FocusWindow ( window ) ;
2015-04-03 22:15:12 +08:00
if ( tab_focus_requested | | g . IO . KeyCtrl | | g . IO . MouseDoubleClicked [ 0 ] )
2015-04-03 21:07:52 +08:00
{
start_text_input = true ;
g . ScalarAsInputTextId = 0 ;
}
}
if ( start_text_input | | ( g . ActiveId = = id & & g . ScalarAsInputTextId = = id ) )
2015-07-31 12:59:30 +08:00
return InputFloatReplaceWidget ( frame_bb , label , v , id , decimal_precision ) ;
2015-04-03 21:07:52 +08:00
ItemSize ( total_bb , style . FramePadding . y ) ;
// Actual drag behavior
2015-06-30 03:18:44 +08:00
const bool value_changed = DragBehavior ( frame_bb , id , v , v_speed , v_min , v_max , decimal_precision , power ) ;
2015-04-03 21:07:52 +08:00
// Display value using user-provided display format so user can add prefix/suffix/decorations to the value.
char value_buf [ 64 ] ;
const char * value_buf_end = value_buf + ImFormatString ( value_buf , IM_ARRAYSIZE ( value_buf ) , display_format , * v ) ;
2015-06-17 07:21:07 +08:00
RenderTextClipped ( frame_bb . Min , frame_bb . Max , value_buf , value_buf_end , NULL , ImGuiAlign_Center | ImGuiAlign_VCenter ) ;
2015-04-03 21:07:52 +08:00
if ( label_size . x > 0.0f )
RenderText ( ImVec2 ( frame_bb . Max . x + style . ItemInnerSpacing . x , inner_bb . Min . y ) , label ) ;
return value_changed ;
}
2015-08-02 11:57:24 +08:00
bool ImGui : : DragFloatN ( const char * label , float * v , int components , float v_speed , float v_min , float v_max , const char * display_format , float power )
2015-04-20 06:28:57 +08:00
{
ImGuiWindow * window = GetCurrentWindow ( ) ;
if ( window - > SkipItems )
return false ;
2015-07-22 10:13:12 +08:00
ImGuiState & g = * GImGui ;
2015-04-20 06:28:57 +08:00
bool value_changed = false ;
ImGui : : BeginGroup ( ) ;
ImGui : : PushID ( label ) ;
2015-05-12 04:44:50 +08:00
PushMultiItemsWidths ( components ) ;
2015-04-20 06:28:57 +08:00
for ( int i = 0 ; i < components ; i + + )
{
ImGui : : PushID ( i ) ;
value_changed | = ImGui : : DragFloat ( " ##v " , & v [ i ] , v_speed , v_min , v_max , display_format , power ) ;
2015-07-11 09:36:34 +08:00
ImGui : : SameLine ( 0 , g . Style . ItemInnerSpacing . x ) ;
2015-04-20 06:28:57 +08:00
ImGui : : PopID ( ) ;
2015-05-12 04:44:50 +08:00
ImGui : : PopItemWidth ( ) ;
2015-04-20 06:28:57 +08:00
}
ImGui : : PopID ( ) ;
ImGui : : TextUnformatted ( label , FindTextDisplayEnd ( label ) ) ;
ImGui : : EndGroup ( ) ;
return value_changed ;
}
bool ImGui : : DragFloat2 ( const char * label , float v [ 2 ] , float v_speed , float v_min , float v_max , const char * display_format , float power )
{
return DragFloatN ( label , v , 2 , v_speed , v_min , v_max , display_format , power ) ;
}
2015-07-15 23:36:39 +08:00
bool ImGui : : DragFloat3 ( const char * label , float v [ 3 ] , float v_speed , float v_min , float v_max , const char * display_format , float power )
2015-04-20 06:28:57 +08:00
{
return DragFloatN ( label , v , 3 , v_speed , v_min , v_max , display_format , power ) ;
}
2015-07-15 23:36:39 +08:00
bool ImGui : : DragFloat4 ( const char * label , float v [ 4 ] , float v_speed , float v_min , float v_max , const char * display_format , float power )
2015-04-20 06:28:57 +08:00
{
return DragFloatN ( label , v , 4 , v_speed , v_min , v_max , display_format , power ) ;
}
2015-07-05 02:17:04 +08:00
bool ImGui : : DragFloatRange2 ( const char * label , float * v_current_min , float * v_current_max , float v_speed , float v_min , float v_max , const char * display_format , const char * display_format_max , float power )
{
ImGuiWindow * window = GetCurrentWindow ( ) ;
if ( window - > SkipItems )
return false ;
2015-07-22 10:13:12 +08:00
ImGuiState & g = * GImGui ;
2015-07-05 02:17:04 +08:00
ImGui : : PushID ( label ) ;
ImGui : : BeginGroup ( ) ;
PushMultiItemsWidths ( 2 ) ;
bool value_changed = ImGui : : DragFloat ( " ##min " , v_current_min , v_speed , ( v_min > = v_max ) ? - FLT_MAX : v_min , ( v_min > = v_max ) ? * v_current_max : ImMin ( v_max , * v_current_max ) , display_format , power ) ;
ImGui : : PopItemWidth ( ) ;
2015-07-11 09:36:34 +08:00
ImGui : : SameLine ( 0 , g . Style . ItemInnerSpacing . x ) ;
2015-07-05 02:17:04 +08:00
value_changed | = ImGui : : DragFloat ( " ##max " , v_current_max , v_speed , ( v_min > = v_max ) ? * v_current_min : ImMax ( v_min , * v_current_min ) , ( v_min > = v_max ) ? FLT_MAX : v_max , display_format_max ? display_format_max : display_format , power ) ;
ImGui : : PopItemWidth ( ) ;
2015-07-11 09:36:34 +08:00
ImGui : : SameLine ( 0 , g . Style . ItemInnerSpacing . x ) ;
2015-07-05 02:17:04 +08:00
ImGui : : TextUnformatted ( label , FindTextDisplayEnd ( label ) ) ;
ImGui : : EndGroup ( ) ;
ImGui : : PopID ( ) ;
return value_changed ;
}
2015-04-13 02:15:55 +08:00
// NB: v_speed is float to allow adjusting the drag speed with more precision
bool ImGui : : DragInt ( const char * label , int * v , float v_speed , int v_min , int v_max , const char * display_format )
2015-04-03 21:07:52 +08:00
{
if ( ! display_format )
display_format = " %.0f " ;
float v_f = ( float ) * v ;
2015-04-13 02:15:55 +08:00
bool value_changed = ImGui : : DragFloat ( label , & v_f , v_speed , ( float ) v_min , ( float ) v_max , display_format ) ;
2015-04-03 21:07:52 +08:00
* v = ( int ) v_f ;
return value_changed ;
}
2015-08-02 11:57:24 +08:00
bool ImGui : : DragIntN ( const char * label , int * v , int components , float v_speed , int v_min , int v_max , const char * display_format )
2015-04-20 06:28:57 +08:00
{
ImGuiWindow * window = GetCurrentWindow ( ) ;
if ( window - > SkipItems )
return false ;
2015-07-22 10:13:12 +08:00
ImGuiState & g = * GImGui ;
2015-04-20 06:28:57 +08:00
bool value_changed = false ;
ImGui : : BeginGroup ( ) ;
ImGui : : PushID ( label ) ;
2015-05-12 04:44:50 +08:00
PushMultiItemsWidths ( components ) ;
2015-04-20 06:28:57 +08:00
for ( int i = 0 ; i < components ; i + + )
{
ImGui : : PushID ( i ) ;
value_changed | = ImGui : : DragInt ( " ##v " , & v [ i ] , v_speed , v_min , v_max , display_format ) ;
2015-07-11 09:36:34 +08:00
ImGui : : SameLine ( 0 , g . Style . ItemInnerSpacing . x ) ;
2015-04-20 06:28:57 +08:00
ImGui : : PopID ( ) ;
2015-05-12 04:44:50 +08:00
ImGui : : PopItemWidth ( ) ;
2015-04-20 06:28:57 +08:00
}
ImGui : : PopID ( ) ;
ImGui : : TextUnformatted ( label , FindTextDisplayEnd ( label ) ) ;
ImGui : : EndGroup ( ) ;
return value_changed ;
}
bool ImGui : : DragInt2 ( const char * label , int v [ 2 ] , float v_speed , int v_min , int v_max , const char * display_format )
{
return DragIntN ( label , v , 2 , v_speed , v_min , v_max , display_format ) ;
}
bool ImGui : : DragInt3 ( const char * label , int v [ 3 ] , float v_speed , int v_min , int v_max , const char * display_format )
{
return DragIntN ( label , v , 3 , v_speed , v_min , v_max , display_format ) ;
}
bool ImGui : : DragInt4 ( const char * label , int v [ 4 ] , float v_speed , int v_min , int v_max , const char * display_format )
{
return DragIntN ( label , v , 4 , v_speed , v_min , v_max , display_format ) ;
}
2015-07-05 02:17:04 +08:00
bool ImGui : : DragIntRange2 ( const char * label , int * v_current_min , int * v_current_max , float v_speed , int v_min , int v_max , const char * display_format , const char * display_format_max )
{
ImGuiWindow * window = GetCurrentWindow ( ) ;
if ( window - > SkipItems )
return false ;
2015-07-22 10:13:12 +08:00
ImGuiState & g = * GImGui ;
2015-07-05 02:17:04 +08:00
ImGui : : PushID ( label ) ;
ImGui : : BeginGroup ( ) ;
PushMultiItemsWidths ( 2 ) ;
bool value_changed = ImGui : : DragInt ( " ##min " , v_current_min , v_speed , ( v_min > = v_max ) ? IM_INT_MIN : v_min , ( v_min > = v_max ) ? * v_current_max : ImMin ( v_max , * v_current_max ) , display_format ) ;
ImGui : : PopItemWidth ( ) ;
2015-07-11 09:36:34 +08:00
ImGui : : SameLine ( 0 , g . Style . ItemInnerSpacing . x ) ;
2015-07-05 02:17:04 +08:00
value_changed | = ImGui : : DragInt ( " ##max " , v_current_max , v_speed , ( v_min > = v_max ) ? * v_current_min : ImMax ( v_min , * v_current_min ) , ( v_min > = v_max ) ? IM_INT_MAX : v_max , display_format_max ? display_format_max : display_format ) ;
ImGui : : PopItemWidth ( ) ;
2015-07-11 09:36:34 +08:00
ImGui : : SameLine ( 0 , g . Style . ItemInnerSpacing . x ) ;
2015-07-05 02:17:04 +08:00
ImGui : : TextUnformatted ( label , FindTextDisplayEnd ( label ) ) ;
ImGui : : EndGroup ( ) ;
ImGui : : PopID ( ) ;
return value_changed ;
}
2015-08-02 11:57:24 +08:00
void ImGui : : PlotEx ( ImGuiPlotType plot_type , const char * label , float ( * values_getter ) ( void * data , int idx ) , void * data , int values_count , int values_offset , const char * overlay_text , float scale_min , float scale_max , ImVec2 graph_size )
2014-08-11 04:58:11 +08:00
{
2014-08-19 19:09:13 +08:00
ImGuiWindow * window = GetCurrentWindow ( ) ;
if ( window - > SkipItems )
return ;
2015-07-22 10:13:12 +08:00
ImGuiState & g = * GImGui ;
2014-08-19 19:09:13 +08:00
const ImGuiStyle & style = g . Style ;
2015-03-07 05:58:12 +08:00
const ImVec2 label_size = ImGui : : CalcTextSize ( label , NULL , true ) ;
2014-11-18 17:55:31 +08:00
if ( graph_size . x = = 0.0f )
2015-03-15 09:27:37 +08:00
graph_size . x = ImGui : : CalcItemWidth ( ) + ( style . FramePadding . x * 2 ) ;
2014-11-18 17:55:31 +08:00
if ( graph_size . y = = 0.0f )
2015-03-15 09:27:37 +08:00
graph_size . y = label_size . y + ( style . FramePadding . y * 2 ) ;
2014-08-19 19:09:13 +08:00
2015-03-18 07:35:12 +08:00
const ImRect frame_bb ( window - > DC . CursorPos , window - > DC . CursorPos + ImVec2 ( graph_size . x , graph_size . y ) ) ;
2015-03-19 22:48:36 +08:00
const ImRect inner_bb ( frame_bb . Min + style . FramePadding , frame_bb . Max - style . FramePadding ) ;
const ImRect total_bb ( frame_bb . Min , frame_bb . Max + ImVec2 ( label_size . x > 0.0f ? style . ItemInnerSpacing . x + label_size . x : 0.0f , 0 ) ) ;
ItemSize ( total_bb , style . FramePadding . y ) ;
if ( ! ItemAdd ( total_bb , NULL ) )
2014-08-19 19:09:13 +08:00
return ;
2014-11-24 18:02:39 +08:00
// Determine scale from values if not specified
2014-08-19 19:09:13 +08:00
if ( scale_min = = FLT_MAX | | scale_max = = FLT_MAX )
{
float v_min = FLT_MAX ;
float v_max = - FLT_MAX ;
for ( int i = 0 ; i < values_count ; i + + )
{
2014-10-27 03:25:00 +08:00
const float v = values_getter ( data , i ) ;
2014-08-19 19:09:13 +08:00
v_min = ImMin ( v_min , v ) ;
v_max = ImMax ( v_max , v ) ;
}
if ( scale_min = = FLT_MAX )
scale_min = v_min ;
if ( scale_max = = FLT_MAX )
scale_max = v_max ;
}
2015-01-15 17:14:44 +08:00
RenderFrame ( frame_bb . Min , frame_bb . Max , window - > Color ( ImGuiCol_FrameBg ) , true , style . FrameRounding ) ;
2014-08-19 19:09:13 +08:00
int res_w = ImMin ( ( int ) graph_size . x , values_count ) ;
if ( plot_type = = ImGuiPlotType_Lines )
res_w - = 1 ;
// Tooltip on hover
int v_hovered = - 1 ;
2015-07-06 10:47:25 +08:00
if ( IsHovered ( inner_bb , 0 ) )
2014-08-19 19:09:13 +08:00
{
2015-03-19 22:48:36 +08:00
const float t = ImClamp ( ( g . IO . MousePos . x - inner_bb . Min . x ) / ( inner_bb . Max . x - inner_bb . Min . x ) , 0.0f , 0.9999f ) ;
2014-08-19 19:09:13 +08:00
const int v_idx = ( int ) ( t * ( values_count + ( ( plot_type = = ImGuiPlotType_Lines ) ? - 1 : 0 ) ) ) ;
IM_ASSERT ( v_idx > = 0 & & v_idx < values_count ) ;
2014-10-27 03:25:00 +08:00
const float v0 = values_getter ( data , ( v_idx + values_offset ) % values_count ) ;
const float v1 = values_getter ( data , ( v_idx + 1 + values_offset ) % values_count ) ;
2014-08-19 19:09:13 +08:00
if ( plot_type = = ImGuiPlotType_Lines )
ImGui : : SetTooltip ( " %d: %8.4g \n %d: %8.4g " , v_idx , v0 , v_idx + 1 , v1 ) ;
else if ( plot_type = = ImGuiPlotType_Histogram )
ImGui : : SetTooltip ( " %d: %8.4g " , v_idx , v0 ) ;
v_hovered = v_idx ;
}
const float t_step = 1.0f / ( float ) res_w ;
2014-10-27 03:25:00 +08:00
float v0 = values_getter ( data , ( 0 + values_offset ) % values_count ) ;
2014-08-19 19:09:13 +08:00
float t0 = 0.0f ;
ImVec2 p0 = ImVec2 ( t0 , 1.0f - ImSaturate ( ( v0 - scale_min ) / ( scale_max - scale_min ) ) ) ;
const ImU32 col_base = window - > Color ( ( plot_type = = ImGuiPlotType_Lines ) ? ImGuiCol_PlotLines : ImGuiCol_PlotHistogram ) ;
const ImU32 col_hovered = window - > Color ( ( plot_type = = ImGuiPlotType_Lines ) ? ImGuiCol_PlotLinesHovered : ImGuiCol_PlotHistogramHovered ) ;
for ( int n = 0 ; n < res_w ; n + + )
{
const float t1 = t0 + t_step ;
const int v_idx = ( int ) ( t0 * values_count ) ;
IM_ASSERT ( v_idx > = 0 & & v_idx < values_count ) ;
2014-10-27 03:25:00 +08:00
const float v1 = values_getter ( data , ( v_idx + values_offset + 1 ) % values_count ) ;
2014-08-19 19:09:13 +08:00
const ImVec2 p1 = ImVec2 ( t1 , 1.0f - ImSaturate ( ( v1 - scale_min ) / ( scale_max - scale_min ) ) ) ;
2014-09-02 21:35:06 +08:00
// NB- Draw calls are merged together by the DrawList system.
2014-08-19 19:09:13 +08:00
if ( plot_type = = ImGuiPlotType_Lines )
2015-03-19 22:48:36 +08:00
window - > DrawList - > AddLine ( ImLerp ( inner_bb . Min , inner_bb . Max , p0 ) , ImLerp ( inner_bb . Min , inner_bb . Max , p1 ) , v_hovered = = v_idx ? col_hovered : col_base ) ;
2014-08-19 19:09:13 +08:00
else if ( plot_type = = ImGuiPlotType_Histogram )
2015-03-19 22:48:36 +08:00
window - > DrawList - > AddRectFilled ( ImLerp ( inner_bb . Min , inner_bb . Max , p0 ) , ImLerp ( inner_bb . Min , inner_bb . Max , ImVec2 ( p1 . x , 1.0f ) ) + ImVec2 ( - 1 , 0 ) , v_hovered = = v_idx ? col_hovered : col_base ) ;
2014-08-19 19:09:13 +08:00
t0 = t1 ;
p0 = p1 ;
}
2014-09-02 21:35:06 +08:00
// Text overlay
2014-08-19 19:09:13 +08:00
if ( overlay_text )
2015-06-17 07:21:07 +08:00
RenderTextClipped ( ImVec2 ( frame_bb . Min . x , frame_bb . Min . y + style . FramePadding . y ) , frame_bb . Max , overlay_text , NULL , NULL , ImGuiAlign_Center ) ;
2014-08-19 19:09:13 +08:00
2015-03-19 22:48:36 +08:00
RenderText ( ImVec2 ( frame_bb . Max . x + style . ItemInnerSpacing . x , inner_bb . Min . y ) , label ) ;
2014-08-11 04:58:11 +08:00
}
2014-10-27 03:56:57 +08:00
struct ImGuiPlotArrayGetterData
2014-10-27 03:25:00 +08:00
{
const float * Values ;
2015-07-08 02:09:25 +08:00
int Stride ;
2014-10-27 03:25:00 +08:00
2015-07-08 02:09:25 +08:00
ImGuiPlotArrayGetterData ( const float * values , int stride ) { Values = values ; Stride = stride ; }
2014-10-27 03:25:00 +08:00
} ;
2014-10-27 03:56:57 +08:00
static float Plot_ArrayGetter ( void * data , int idx )
2014-10-27 03:25:00 +08:00
{
2014-10-27 03:56:57 +08:00
ImGuiPlotArrayGetterData * plot_data = ( ImGuiPlotArrayGetterData * ) data ;
2014-11-18 17:55:31 +08:00
const float v = * ( float * ) ( void * ) ( ( unsigned char * ) plot_data - > Values + ( size_t ) idx * plot_data - > Stride ) ;
2014-10-27 03:25:00 +08:00
return v ;
}
2015-07-08 02:09:25 +08:00
void ImGui : : PlotLines ( const char * label , const float * values , int values_count , int values_offset , const char * overlay_text , float scale_min , float scale_max , ImVec2 graph_size , int stride )
2014-08-11 04:58:11 +08:00
{
2014-10-27 03:56:57 +08:00
ImGuiPlotArrayGetterData data ( values , stride ) ;
2015-08-02 11:57:24 +08:00
PlotEx ( ImGuiPlotType_Lines , label , & Plot_ArrayGetter , ( void * ) & data , values_count , values_offset , overlay_text , scale_min , scale_max , graph_size ) ;
2014-10-27 03:25:00 +08:00
}
2014-11-10 09:29:01 +08:00
void ImGui : : PlotLines ( const char * label , float ( * values_getter ) ( void * data , int idx ) , void * data , int values_count , int values_offset , const char * overlay_text , float scale_min , float scale_max , ImVec2 graph_size )
2014-10-27 03:25:00 +08:00
{
2015-08-02 11:57:24 +08:00
PlotEx ( ImGuiPlotType_Lines , label , values_getter , data , values_count , values_offset , overlay_text , scale_min , scale_max , graph_size ) ;
2014-08-11 04:58:11 +08:00
}
2015-07-08 02:09:25 +08:00
void ImGui : : PlotHistogram ( const char * label , const float * values , int values_count , int values_offset , const char * overlay_text , float scale_min , float scale_max , ImVec2 graph_size , int stride )
2014-08-11 04:58:11 +08:00
{
2014-10-27 03:56:57 +08:00
ImGuiPlotArrayGetterData data ( values , stride ) ;
2015-08-02 11:57:24 +08:00
PlotEx ( ImGuiPlotType_Histogram , label , & Plot_ArrayGetter , ( void * ) & data , values_count , values_offset , overlay_text , scale_min , scale_max , graph_size ) ;
2014-08-11 04:58:11 +08:00
}
2014-11-10 09:29:01 +08:00
void ImGui : : PlotHistogram ( const char * label , float ( * values_getter ) ( void * data , int idx ) , void * data , int values_count , int values_offset , const char * overlay_text , float scale_min , float scale_max , ImVec2 graph_size )
2014-10-27 03:25:00 +08:00
{
2015-08-02 11:57:24 +08:00
PlotEx ( ImGuiPlotType_Histogram , label , values_getter , data , values_count , values_offset , overlay_text , scale_min , scale_max , graph_size ) ;
2014-10-27 03:25:00 +08:00
}
2014-11-10 09:29:01 +08:00
bool ImGui : : Checkbox ( const char * label , bool * v )
2014-08-11 04:58:11 +08:00
{
2014-08-19 19:09:13 +08:00
ImGuiWindow * window = GetCurrentWindow ( ) ;
if ( window - > SkipItems )
2014-08-29 00:32:03 +08:00
return false ;
2014-08-11 04:58:11 +08:00
2015-07-22 10:13:12 +08:00
ImGuiState & g = * GImGui ;
2014-08-19 19:09:13 +08:00
const ImGuiStyle & style = g . Style ;
const ImGuiID id = window - > GetID ( label ) ;
2015-03-07 05:58:12 +08:00
const ImVec2 label_size = CalcTextSize ( label , NULL , true ) ;
2014-08-11 04:58:11 +08:00
2015-03-18 07:35:12 +08:00
const ImRect check_bb ( window - > DC . CursorPos , window - > DC . CursorPos + ImVec2 ( label_size . y + style . FramePadding . y * 2 , label_size . y + style . FramePadding . y * 2 ) ) ;
2015-03-18 04:17:53 +08:00
ItemSize ( check_bb , style . FramePadding . y ) ;
2014-08-11 04:58:11 +08:00
2015-03-18 07:35:12 +08:00
ImRect total_bb = check_bb ;
2015-03-07 05:58:12 +08:00
if ( label_size . x > 0 )
2015-07-11 09:36:34 +08:00
SameLine ( 0 , style . ItemInnerSpacing . x ) ;
2015-03-18 07:35:12 +08:00
const ImRect text_bb ( window - > DC . CursorPos + ImVec2 ( 0 , style . FramePadding . y ) , window - > DC . CursorPos + ImVec2 ( 0 , style . FramePadding . y ) + label_size ) ;
2015-03-07 05:58:12 +08:00
if ( label_size . x > 0 )
2014-12-30 19:41:46 +08:00
{
2015-03-18 04:17:53 +08:00
ItemSize ( ImVec2 ( text_bb . GetWidth ( ) , check_bb . GetHeight ( ) ) , style . FramePadding . y ) ;
2015-03-18 07:35:12 +08:00
total_bb = ImRect ( ImMin ( check_bb . Min , text_bb . Min ) , ImMax ( check_bb . Max , text_bb . Max ) ) ;
2014-12-30 19:41:46 +08:00
}
2014-08-11 04:58:11 +08:00
2015-01-23 06:54:05 +08:00
if ( ! ItemAdd ( total_bb , & id ) )
2014-08-29 00:32:03 +08:00
return false ;
2014-08-11 04:58:11 +08:00
2015-01-05 03:23:36 +08:00
bool hovered , held ;
2015-03-15 09:48:59 +08:00
bool pressed = ButtonBehavior ( total_bb , id , & hovered , & held , true ) ;
2014-08-19 19:09:13 +08:00
if ( pressed )
* v = ! ( * v ) ;
2014-08-11 04:58:11 +08:00
2015-04-03 21:07:52 +08:00
RenderFrame ( check_bb . Min , check_bb . Max , window - > Color ( ( held & & hovered ) ? ImGuiCol_FrameBgActive : hovered ? ImGuiCol_FrameBgHovered : ImGuiCol_FrameBg ) , true , style . FrameRounding ) ;
2014-08-19 19:09:13 +08:00
if ( * v )
{
2014-11-29 06:38:41 +08:00
const float check_sz = ImMin ( check_bb . GetWidth ( ) , check_bb . GetHeight ( ) ) ;
2015-08-06 00:50:20 +08:00
const float pad = ImMax ( 1.0f , ( float ) ( int ) ( check_sz / 6.0f ) ) ;
2015-01-15 17:14:44 +08:00
window - > DrawList - > AddRectFilled ( check_bb . Min + ImVec2 ( pad , pad ) , check_bb . Max - ImVec2 ( pad , pad ) , window - > Color ( ImGuiCol_CheckMark ) , style . FrameRounding ) ;
2014-08-19 19:09:13 +08:00
}
2014-08-11 04:58:11 +08:00
2014-08-19 19:09:13 +08:00
if ( g . LogEnabled )
2015-07-31 13:48:59 +08:00
LogRenderedText ( text_bb . GetTL ( ) , * v ? " [x] " : " [ ] " ) ;
2014-08-19 19:09:13 +08:00
RenderText ( text_bb . GetTL ( ) , label ) ;
2014-08-11 04:58:11 +08:00
2014-08-29 00:32:03 +08:00
return pressed ;
2014-08-11 04:58:11 +08:00
}
2014-11-10 09:29:01 +08:00
bool ImGui : : CheckboxFlags ( const char * label , unsigned int * flags , unsigned int flags_value )
2014-08-11 04:58:11 +08:00
{
2014-08-19 19:09:13 +08:00
bool v = ( * flags & flags_value ) ? true : false ;
2014-08-29 00:32:03 +08:00
bool pressed = ImGui : : Checkbox ( label , & v ) ;
2014-08-19 19:09:13 +08:00
if ( v )
* flags | = flags_value ;
else
* flags & = ~ flags_value ;
2014-08-29 00:32:03 +08:00
return pressed ;
2014-08-11 04:58:11 +08:00
}
2014-11-10 09:29:01 +08:00
bool ImGui : : RadioButton ( const char * label , bool active )
2014-08-11 04:58:11 +08:00
{
2014-08-19 19:09:13 +08:00
ImGuiWindow * window = GetCurrentWindow ( ) ;
if ( window - > SkipItems )
return false ;
2014-08-11 04:58:11 +08:00
2015-07-22 10:13:12 +08:00
ImGuiState & g = * GImGui ;
2014-08-19 19:09:13 +08:00
const ImGuiStyle & style = g . Style ;
const ImGuiID id = window - > GetID ( label ) ;
2015-03-07 05:58:12 +08:00
const ImVec2 label_size = CalcTextSize ( label , NULL , true ) ;
2014-08-11 04:58:11 +08:00
2015-03-18 07:35:12 +08:00
const ImRect check_bb ( window - > DC . CursorPos , window - > DC . CursorPos + ImVec2 ( label_size . y + style . FramePadding . y * 2 - 1 , label_size . y + style . FramePadding . y * 2 - 1 ) ) ;
2015-03-18 04:17:53 +08:00
ItemSize ( check_bb , style . FramePadding . y ) ;
2014-08-11 04:58:11 +08:00
2015-03-18 07:35:12 +08:00
ImRect total_bb = check_bb ;
2015-03-07 05:58:12 +08:00
if ( label_size . x > 0 )
2015-07-11 09:36:34 +08:00
SameLine ( 0 , style . ItemInnerSpacing . x ) ;
2015-03-18 07:35:12 +08:00
const ImRect text_bb ( window - > DC . CursorPos + ImVec2 ( 0 , style . FramePadding . y ) , window - > DC . CursorPos + ImVec2 ( 0 , style . FramePadding . y ) + label_size ) ;
2015-03-07 05:58:12 +08:00
if ( label_size . x > 0 )
2014-12-30 19:41:46 +08:00
{
2015-03-18 04:17:53 +08:00
ItemSize ( ImVec2 ( text_bb . GetWidth ( ) , check_bb . GetHeight ( ) ) , style . FramePadding . y ) ;
2014-12-30 19:41:46 +08:00
total_bb . Add ( text_bb ) ;
}
2014-08-11 04:58:11 +08:00
2015-01-23 06:54:05 +08:00
if ( ! ItemAdd ( total_bb , & id ) )
2014-08-19 19:09:13 +08:00
return false ;
2014-08-11 04:58:11 +08:00
2014-08-19 19:09:13 +08:00
ImVec2 center = check_bb . GetCenter ( ) ;
center . x = ( float ) ( int ) center . x + 0.5f ;
center . y = ( float ) ( int ) center . y + 0.5f ;
const float radius = check_bb . GetHeight ( ) * 0.5f ;
2014-08-11 04:58:11 +08:00
2015-01-05 03:23:36 +08:00
bool hovered , held ;
2015-03-15 09:48:59 +08:00
bool pressed = ButtonBehavior ( total_bb , id , & hovered , & held , true ) ;
2014-08-11 04:58:11 +08:00
2015-04-03 21:07:52 +08:00
window - > DrawList - > AddCircleFilled ( center , radius , window - > Color ( ( held & & hovered ) ? ImGuiCol_FrameBgActive : hovered ? ImGuiCol_FrameBgHovered : ImGuiCol_FrameBg ) , 16 ) ;
2014-08-19 19:09:13 +08:00
if ( active )
2014-11-29 06:38:41 +08:00
{
const float check_sz = ImMin ( check_bb . GetWidth ( ) , check_bb . GetHeight ( ) ) ;
2015-08-06 00:50:20 +08:00
const float pad = ImMax ( 1.0f , ( float ) ( int ) ( check_sz / 6.0f ) ) ;
2015-01-05 03:37:24 +08:00
window - > DrawList - > AddCircleFilled ( center , radius - pad , window - > Color ( ImGuiCol_CheckMark ) , 16 ) ;
2014-11-29 06:38:41 +08:00
}
2014-08-11 04:58:11 +08:00
2014-08-19 19:09:13 +08:00
if ( window - > Flags & ImGuiWindowFlags_ShowBorders )
{
window - > DrawList - > AddCircle ( center + ImVec2 ( 1 , 1 ) , radius , window - > Color ( ImGuiCol_BorderShadow ) , 16 ) ;
window - > DrawList - > AddCircle ( center , radius , window - > Color ( ImGuiCol_Border ) , 16 ) ;
}
2014-08-11 04:58:11 +08:00
2014-09-02 21:36:03 +08:00
if ( g . LogEnabled )
2015-07-31 13:48:59 +08:00
LogRenderedText ( text_bb . GetTL ( ) , active ? " (x) " : " ( ) " ) ;
2014-08-19 19:09:13 +08:00
RenderText ( text_bb . GetTL ( ) , label ) ;
2014-08-11 04:58:11 +08:00
2014-08-19 19:09:13 +08:00
return pressed ;
2014-08-11 04:58:11 +08:00
}
2014-11-10 09:29:01 +08:00
bool ImGui : : RadioButton ( const char * label , int * v , int v_button )
2014-08-11 04:58:11 +08:00
{
2014-08-19 19:09:13 +08:00
const bool pressed = ImGui : : RadioButton ( label , * v = = v_button ) ;
if ( pressed )
{
* v = v_button ;
}
return pressed ;
2014-08-11 04:58:11 +08:00
}
2015-06-19 09:09:04 +08:00
static int InputTextCalcTextLenAndLineCount ( const char * text_begin , const char * * out_text_end )
{
int line_count = 0 ;
const char * s = text_begin ;
while ( char c = * s + + ) // We are only matching for \n so we can ignore UTF-8 decoding
if ( c = = ' \n ' )
line_count + + ;
2015-06-19 12:35:08 +08:00
s - - ;
if ( s [ 0 ] ! = ' \n ' & & s [ 0 ] ! = ' \r ' )
2015-06-19 09:09:04 +08:00
line_count + + ;
2015-06-19 12:35:08 +08:00
* out_text_end = s ;
2015-06-19 09:09:04 +08:00
return line_count ;
}
2015-07-22 07:00:28 +08:00
static ImVec2 InputTextCalcTextSizeW ( const ImWchar * text_begin , const ImWchar * text_end , const ImWchar * * remaining , ImVec2 * out_offset , bool stop_on_new_line )
2015-06-17 05:46:45 +08:00
{
2015-06-19 08:31:13 +08:00
ImFont * font = GImGui - > Font ;
const float line_height = GImGui - > FontSize ;
const float scale = line_height / font - > FontSize ;
2015-06-17 05:46:45 +08:00
ImVec2 text_size = ImVec2 ( 0 , 0 ) ;
float line_width = 0.0f ;
const ImWchar * s = text_begin ;
while ( s < text_end )
{
2015-06-19 10:49:20 +08:00
unsigned int c = ( unsigned int ) ( * s + + ) ;
if ( c = = ' \n ' )
2015-06-17 05:46:45 +08:00
{
2015-06-19 10:49:20 +08:00
text_size . x = ImMax ( text_size . x , line_width ) ;
text_size . y + = line_height ;
line_width = 0.0f ;
if ( stop_on_new_line )
break ;
continue ;
2015-06-17 05:46:45 +08:00
}
2015-06-19 10:49:20 +08:00
if ( c = = ' \r ' )
continue ;
2015-06-17 05:46:45 +08:00
2015-06-19 08:31:13 +08:00
const float char_width = font - > GetCharAdvance ( ( unsigned short ) c ) * scale ;
2015-06-17 05:46:45 +08:00
line_width + = char_width ;
}
if ( text_size . x < line_width )
text_size . x = line_width ;
if ( out_offset )
* out_offset = ImVec2 ( line_width , text_size . y + line_height ) ; // offset allow for the possibility of sitting after a trailing \n
if ( line_width > 0 | | text_size . y = = 0.0f ) // whereas size.y will ignore the trailing \n
text_size . y + = line_height ;
if ( remaining )
* remaining = s ;
return text_size ;
}
2014-11-24 07:35:26 +08:00
// Wrapper for stb_textedit.h to edit text (our wrapper is for: statically sized buffer, single-line, wchar characters. InputText converts between UTF-8 and wchar)
2015-07-22 07:00:28 +08:00
namespace ImGuiStb
{
2015-07-08 02:09:25 +08:00
static int STB_TEXTEDIT_STRINGLEN ( const STB_TEXTEDIT_STRING * obj ) { return obj - > CurLenW ; }
2014-11-18 15:51:55 +08:00
static ImWchar STB_TEXTEDIT_GETCHAR ( const STB_TEXTEDIT_STRING * obj , int idx ) { return obj - > Text [ idx ] ; }
2015-06-19 08:31:13 +08:00
static float STB_TEXTEDIT_GETWIDTH ( STB_TEXTEDIT_STRING * obj , int line_start_idx , int char_idx ) { ImWchar c = obj - > Text [ line_start_idx + char_idx ] ; if ( c = = ' \n ' ) return STB_TEXTEDIT_GETWIDTH_NEWLINE ; return GImGui - > Font - > GetCharAdvance ( c ) * ( GImGui - > FontSize / GImGui - > Font - > FontSize ) ; }
2014-11-18 15:51:55 +08:00
static int STB_TEXTEDIT_KEYTOTEXT ( int key ) { return key > = 0x10000 ? 0 : key ; }
2014-11-18 03:35:58 +08:00
static ImWchar STB_TEXTEDIT_NEWLINE = ' \n ' ;
2014-11-18 15:51:55 +08:00
static void STB_TEXTEDIT_LAYOUTROW ( StbTexteditRow * r , STB_TEXTEDIT_STRING * obj , int line_start_idx )
2014-08-11 04:58:11 +08:00
{
2015-07-08 02:39:08 +08:00
const ImWchar * text = obj - > Text . Data ;
2014-09-25 21:51:06 +08:00
const ImWchar * text_remaining = NULL ;
2015-06-19 08:31:13 +08:00
const ImVec2 size = InputTextCalcTextSizeW ( text + line_start_idx , text + obj - > CurLenW , & text_remaining , NULL , true ) ;
2014-08-19 19:09:13 +08:00
r - > x0 = 0.0f ;
r - > x1 = size . x ;
r - > baseline_y_delta = size . y ;
r - > ymin = 0.0f ;
r - > ymax = size . y ;
2015-06-17 11:04:27 +08:00
r - > num_chars = ( int ) ( text_remaining - ( text + line_start_idx ) ) ;
2014-08-11 04:58:11 +08:00
}
2014-11-24 07:35:26 +08:00
static bool is_separator ( unsigned int c ) { return c = = ' , ' | | c = = ' ; ' | | c = = ' ( ' | | c = = ' ) ' | | c = = ' { ' | | c = = ' } ' | | c = = ' [ ' | | c = = ' ] ' | | c = = ' | ' ; }
2015-03-07 01:51:25 +08:00
# define STB_TEXTEDIT_IS_SPACE(CH) ( ImCharIsSpace((unsigned int)CH) || is_separator((unsigned int)CH) )
2015-03-09 04:21:19 +08:00
static void STB_TEXTEDIT_DELETECHARS ( STB_TEXTEDIT_STRING * obj , int pos , int n )
{
2015-07-08 02:39:08 +08:00
ImWchar * dst = obj - > Text . Data + pos ;
2015-03-09 04:21:19 +08:00
// We maintain our buffer length in both UTF-8 and wchar formats
obj - > CurLenA - = ImTextCountUtf8BytesFromStr ( dst , dst + n ) ;
obj - > CurLenW - = n ;
// Offset remaining text
2015-07-08 02:39:08 +08:00
const ImWchar * src = obj - > Text . Data + pos + n ;
2015-03-09 04:21:19 +08:00
while ( ImWchar c = * src + + )
* dst + + = c ;
* dst = ' \0 ' ;
}
2014-11-24 07:35:26 +08:00
static bool STB_TEXTEDIT_INSERTCHARS ( STB_TEXTEDIT_STRING * obj , int pos , const ImWchar * new_text , int new_text_len )
2014-08-11 04:58:11 +08:00
{
2015-07-08 02:09:25 +08:00
const int text_len = obj - > CurLenW ;
2015-07-08 02:39:08 +08:00
if ( new_text_len + text_len + 1 > obj - > Text . Size )
2015-03-09 04:21:19 +08:00
return false ;
const int new_text_len_utf8 = ImTextCountUtf8BytesFromStr ( new_text , new_text + new_text_len ) ;
2015-07-08 02:09:25 +08:00
if ( new_text_len_utf8 + obj - > CurLenA + 1 > obj - > BufSizeA )
2014-08-19 19:09:13 +08:00
return false ;
2014-08-11 04:58:11 +08:00
2015-07-08 02:39:08 +08:00
ImWchar * text = obj - > Text . Data ;
2015-07-08 02:09:25 +08:00
if ( pos ! = text_len )
memmove ( text + pos + new_text_len , text + pos , ( size_t ) ( text_len - pos ) * sizeof ( ImWchar ) ) ;
memcpy ( text + pos , new_text , ( size_t ) new_text_len * sizeof ( ImWchar ) ) ;
2015-03-09 04:21:19 +08:00
obj - > CurLenW + = new_text_len ;
obj - > CurLenA + = new_text_len_utf8 ;
obj - > Text [ obj - > CurLenW ] = ' \0 ' ;
2014-08-11 04:58:11 +08:00
2014-08-19 19:09:13 +08:00
return true ;
2014-08-11 04:58:11 +08:00
}
2015-03-20 22:03:23 +08:00
// We don't use an enum so we can build even with conflicting symbols (if another user of stb_textedit.h leak their STB_TEXTEDIT_K_* symbols)
# define STB_TEXTEDIT_K_LEFT 0x10000 // keyboard input to move cursor left
# define STB_TEXTEDIT_K_RIGHT 0x10001 // keyboard input to move cursor right
# define STB_TEXTEDIT_K_UP 0x10002 // keyboard input to move cursor up
# define STB_TEXTEDIT_K_DOWN 0x10003 // keyboard input to move cursor down
# define STB_TEXTEDIT_K_LINESTART 0x10004 // keyboard input to move cursor to start of line
# define STB_TEXTEDIT_K_LINEEND 0x10005 // keyboard input to move cursor to end of line
# define STB_TEXTEDIT_K_TEXTSTART 0x10006 // keyboard input to move cursor to start of text
# define STB_TEXTEDIT_K_TEXTEND 0x10007 // keyboard input to move cursor to end of text
# define STB_TEXTEDIT_K_DELETE 0x10008 // keyboard input to delete selection or character under cursor
# define STB_TEXTEDIT_K_BACKSPACE 0x10009 // keyboard input to delete selection or character left of cursor
# define STB_TEXTEDIT_K_UNDO 0x1000A // keyboard input to perform undo
# define STB_TEXTEDIT_K_REDO 0x1000B // keyboard input to perform redo
# define STB_TEXTEDIT_K_WORDLEFT 0x1000C // keyboard input to move cursor left one word
# define STB_TEXTEDIT_K_WORDRIGHT 0x1000D // keyboard input to move cursor right one word
# define STB_TEXTEDIT_K_SHIFT 0x20000
2014-08-11 04:58:11 +08:00
# define STB_TEXTEDIT_IMPLEMENTATION
# include "stb_textedit.h"
2015-07-22 07:00:28 +08:00
2015-01-21 18:59:11 +08:00
}
2014-08-11 04:58:11 +08:00
2014-11-24 18:02:39 +08:00
void ImGuiTextEditState : : OnKeyPressed ( int key )
2014-08-11 04:58:11 +08:00
{
2014-08-19 19:09:13 +08:00
stb_textedit_key ( this , & StbState , key ) ;
2015-06-18 07:07:49 +08:00
CursorFollow = true ;
2014-08-19 19:09:13 +08:00
CursorAnimReset ( ) ;
2014-08-11 04:58:11 +08:00
}
2014-11-24 18:02:39 +08:00
// Public API to manipulate UTF-8 text
// We expose UTF-8 to the user (unlike the STB_TEXTEDIT_* functions which are manipulating wchar)
2014-11-30 20:21:50 +08:00
void ImGuiTextEditCallbackData : : DeleteChars ( int pos , int bytes_count )
2014-11-24 07:35:26 +08:00
{
char * dst = Buf + pos ;
const char * src = Buf + pos + bytes_count ;
while ( char c = * src + + )
* dst + + = c ;
* dst = ' \0 ' ;
BufDirty = true ;
2014-11-30 20:21:50 +08:00
if ( CursorPos + bytes_count > = pos )
2014-11-24 07:35:26 +08:00
CursorPos - = bytes_count ;
2014-11-30 20:21:50 +08:00
else if ( CursorPos > = pos )
2014-11-24 07:35:26 +08:00
CursorPos = pos ;
SelectionStart = SelectionEnd = CursorPos ;
}
2014-11-30 20:21:50 +08:00
void ImGuiTextEditCallbackData : : InsertChars ( int pos , const char * new_text , const char * new_text_end )
2014-11-24 07:35:26 +08:00
{
2015-07-08 02:09:25 +08:00
const int text_len = ( int ) strlen ( Buf ) ;
2014-11-24 07:35:26 +08:00
if ( ! new_text_end )
new_text_end = new_text + strlen ( new_text ) ;
2015-07-08 02:09:25 +08:00
const int new_text_len = ( int ) ( new_text_end - new_text ) ;
2014-11-24 07:35:26 +08:00
if ( new_text_len + text_len + 1 > = BufSize )
return ;
2015-07-08 02:09:25 +08:00
if ( text_len ! = pos )
memmove ( Buf + pos + new_text_len , Buf + pos , ( size_t ) ( text_len - pos ) ) ;
memcpy ( Buf + pos , new_text , ( size_t ) new_text_len * sizeof ( char ) ) ;
2014-11-24 07:35:26 +08:00
Buf [ text_len + new_text_len ] = ' \0 ' ;
BufDirty = true ;
2014-11-30 20:21:50 +08:00
if ( CursorPos > = pos )
2015-07-08 02:09:25 +08:00
CursorPos + = new_text_len ;
2014-11-24 07:35:26 +08:00
SelectionStart = SelectionEnd = CursorPos ;
}
2015-02-12 01:39:13 +08:00
// Return false to discard a character.
static bool InputTextFilterCharacter ( unsigned int * p_char , ImGuiInputTextFlags flags , ImGuiTextEditCallback callback , void * user_data )
2015-01-05 02:47:08 +08:00
{
2015-02-12 01:39:13 +08:00
unsigned int c = * p_char ;
2015-01-05 03:23:36 +08:00
if ( c < 128 & & c ! = ' ' & & ! isprint ( ( int ) ( c & 0xFF ) ) )
2015-06-17 05:46:45 +08:00
{
bool pass = false ;
2015-06-20 07:34:19 +08:00
pass | = ( c = = ' \n ' & & ( flags & ImGuiInputTextFlags_Multiline ) ) ;
2015-06-17 05:46:45 +08:00
pass | = ( c = = ' \t ' & & ( flags & ImGuiInputTextFlags_AllowTabInput ) ) ;
if ( ! pass )
return false ;
}
2015-01-05 02:47:08 +08:00
2015-02-01 22:08:37 +08:00
if ( c > = 0xE000 & & c < = 0xF8FF ) // Filter private Unicode range. I don't imagine anybody would want to input them. GLFW on OSX seems to send private characters for special keys like arrow keys.
2015-02-12 01:39:13 +08:00
return false ;
2015-02-01 22:08:37 +08:00
2015-02-12 02:01:32 +08:00
if ( flags & ( ImGuiInputTextFlags_CharsDecimal | ImGuiInputTextFlags_CharsHexadecimal | ImGuiInputTextFlags_CharsUppercase | ImGuiInputTextFlags_CharsNoBlank ) )
{
if ( flags & ImGuiInputTextFlags_CharsDecimal )
if ( ! ( c > = ' 0 ' & & c < = ' 9 ' ) & & ( c ! = ' . ' ) & & ( c ! = ' - ' ) & & ( c ! = ' + ' ) & & ( c ! = ' * ' ) & & ( c ! = ' / ' ) )
return false ;
2015-01-05 02:47:08 +08:00
2015-02-12 02:01:32 +08:00
if ( flags & ImGuiInputTextFlags_CharsHexadecimal )
if ( ! ( c > = ' 0 ' & & c < = ' 9 ' ) & & ! ( c > = ' a ' & & c < = ' f ' ) & & ! ( c > = ' A ' & & c < = ' F ' ) )
return false ;
if ( flags & ImGuiInputTextFlags_CharsUppercase )
if ( c > = ' a ' & & c < = ' z ' )
2015-03-10 02:26:48 +08:00
* p_char = ( c + = ( unsigned int ) ( ' A ' - ' a ' ) ) ;
2015-01-05 02:47:08 +08:00
2015-02-12 02:01:32 +08:00
if ( flags & ImGuiInputTextFlags_CharsNoBlank )
2015-03-07 01:51:25 +08:00
if ( ImCharIsSpace ( c ) )
2015-02-12 02:01:32 +08:00
return false ;
}
2015-02-12 01:46:16 +08:00
2015-02-12 01:39:13 +08:00
if ( flags & ImGuiInputTextFlags_CallbackCharFilter )
{
ImGuiTextEditCallbackData callback_data ;
memset ( & callback_data , 0 , sizeof ( ImGuiTextEditCallbackData ) ) ;
callback_data . EventFlag = ImGuiInputTextFlags_CallbackCharFilter ;
2015-03-10 02:26:48 +08:00
callback_data . EventChar = ( ImWchar ) c ;
2015-02-12 01:39:13 +08:00
callback_data . Flags = flags ;
callback_data . UserData = user_data ;
2015-02-12 01:55:26 +08:00
if ( callback ( & callback_data ) ! = 0 )
return false ;
2015-02-12 01:39:13 +08:00
* p_char = callback_data . EventChar ;
if ( ! callback_data . EventChar )
return false ;
}
return true ;
2015-01-05 02:47:08 +08:00
}
2014-11-24 18:02:39 +08:00
// Edit a string of text
2015-08-02 11:57:24 +08:00
bool ImGui : : InputTextEx ( const char * label , char * buf , int buf_size , const ImVec2 & size_arg , ImGuiInputTextFlags flags , ImGuiTextEditCallback callback , void * user_data )
2014-08-11 04:58:11 +08:00
{
2014-08-19 19:09:13 +08:00
ImGuiWindow * window = GetCurrentWindow ( ) ;
if ( window - > SkipItems )
return false ;
2015-06-17 05:46:45 +08:00
IM_ASSERT ( ! ( ( flags & ImGuiInputTextFlags_CallbackHistory ) & & ( flags & ImGuiInputTextFlags_Multiline ) ) ) ; // Can't use both together (they both use up/down keys)
IM_ASSERT ( ! ( ( flags & ImGuiInputTextFlags_CallbackCompletion ) & & ( flags & ImGuiInputTextFlags_AllowTabInput ) ) ) ; // Can't use both together (they both use tab key)
2015-05-28 05:58:49 +08:00
ImGuiState & g = * GImGui ;
2014-08-19 19:09:13 +08:00
const ImGuiIO & io = g . IO ;
const ImGuiStyle & style = g . Style ;
const ImGuiID id = window - > GetID ( label ) ;
2015-06-17 05:46:45 +08:00
const bool is_multiline = ( flags & ImGuiInputTextFlags_Multiline ) ! = 0 ;
2014-08-19 19:09:13 +08:00
2015-06-17 05:46:45 +08:00
ImVec2 label_size = ImGui : : CalcTextSize ( label , NULL , true ) ;
2015-07-18 06:01:21 +08:00
ImVec2 size = CalcItemSize ( size_arg , ImGui : : CalcItemWidth ( ) , is_multiline ? ImGui : : GetTextLineHeight ( ) * 8.0f : label_size . y ) ; // Arbitrary default of 8 lines high for multi-line
2015-06-17 05:46:45 +08:00
const ImRect frame_bb ( window - > DC . CursorPos , window - > DC . CursorPos + size + style . FramePadding * 2.0f ) ;
2015-03-19 22:48:36 +08:00
const ImRect total_bb ( frame_bb . Min , frame_bb . Max + ImVec2 ( label_size . x > 0.0f ? ( style . ItemInnerSpacing . x + label_size . x ) : 0.0f , 0.0f ) ) ;
2015-06-18 07:07:49 +08:00
2015-06-19 00:40:11 +08:00
ImGuiWindow * draw_window = window ;
2015-06-18 07:07:49 +08:00
if ( is_multiline )
{
ImGui : : BeginGroup ( ) ;
2015-06-18 22:49:57 +08:00
if ( ! ImGui : : BeginChildFrame ( id , frame_bb . GetSize ( ) ) )
{
ImGui : : EndChildFrame ( ) ;
ImGui : : EndGroup ( ) ;
return false ;
}
2015-06-19 00:40:11 +08:00
draw_window = GetCurrentWindow ( ) ;
draw_window - > DC . CursorPos + = style . FramePadding ;
size . x - = draw_window - > ScrollbarWidth ( ) ;
2015-06-18 07:07:49 +08:00
}
else
{
ItemSize ( total_bb , style . FramePadding . y ) ;
if ( ! ItemAdd ( total_bb , & id ) )
return false ;
}
2014-08-19 19:09:13 +08:00
2014-12-30 23:38:28 +08:00
// NB: we are only allowed to access 'edit_state' if we are the active widget.
2014-08-19 19:09:13 +08:00
ImGuiTextEditState & edit_state = g . InputTextState ;
const bool is_ctrl_down = io . KeyCtrl ;
const bool is_shift_down = io . KeyShift ;
2015-05-11 16:26:53 +08:00
const bool is_alt_down = io . KeyAlt ;
2015-07-22 07:00:28 +08:00
const bool focus_requested = FocusableItemRegister ( window , g . ActiveId = = id , ( flags & ( ImGuiInputTextFlags_CallbackCompletion | ImGuiInputTextFlags_AllowTabInput ) ) = = 0 ) ; // Using completion callback disable keyboard tabbing
2015-02-22 00:30:40 +08:00
const bool focus_requested_by_code = focus_requested & & ( window - > FocusIdxAllCounter = = window - > FocusIdxAllRequestCurrent ) ;
const bool focus_requested_by_tab = focus_requested & & ! focus_requested_by_code ;
2014-08-19 19:09:13 +08:00
2015-01-05 02:12:30 +08:00
const bool hovered = IsHovered ( frame_bb , id ) ;
2014-08-19 19:09:13 +08:00
if ( hovered )
2015-03-21 21:59:19 +08:00
{
2014-08-19 19:09:13 +08:00
g . HoveredId = id ;
2015-03-21 21:59:19 +08:00
g . MouseCursor = ImGuiMouseCursor_TextInput ;
}
2015-02-22 00:05:59 +08:00
const bool user_clicked = hovered & & io . MouseClicked [ 0 ] ;
2015-06-19 01:57:43 +08:00
const bool user_scrolled = is_multiline & & g . ActiveId = = 0 & & edit_state . Id = = id & & g . ActiveIdPreviousFrame = = draw_window - > GetID ( " #SCROLLY " ) ;
2014-08-19 19:09:13 +08:00
bool select_all = ( g . ActiveId ! = id ) & & ( flags & ImGuiInputTextFlags_AutoSelectAll ) ! = 0 ;
2015-06-19 01:57:43 +08:00
if ( focus_requested | | user_clicked | | user_scrolled )
2014-08-19 19:09:13 +08:00
{
if ( g . ActiveId ! = id )
{
// Start edition
2014-09-25 21:51:06 +08:00
// Take a copy of the initial buffer value (both in original UTF-8 format and converted to wchar)
2015-03-09 04:21:19 +08:00
// From the moment we focused we are ignoring the content of 'buf'
2015-06-17 11:04:27 +08:00
edit_state . Text . resize ( buf_size ) ; // wchar count <= utf-8 count
edit_state . InitialText . resize ( buf_size ) ; // utf-8
2015-07-08 02:39:08 +08:00
ImFormatString ( edit_state . InitialText . Data , edit_state . InitialText . Size , " %s " , buf ) ;
2015-03-09 04:21:19 +08:00
const char * buf_end = NULL ;
2015-07-08 02:39:08 +08:00
edit_state . CurLenW = ImTextStrFromUtf8 ( edit_state . Text . Data , edit_state . Text . Size , buf , NULL , & buf_end ) ;
2015-07-08 02:09:25 +08:00
edit_state . CurLenA = ( int ) ( buf_end - buf ) ; // We can't get the result from ImFormatString() above because it is not UTF-8 aware. Here we'll cut off malformed UTF-8.
2015-06-17 11:53:04 +08:00
edit_state . InputCursorScreenPos = ImVec2 ( - 1.f , - 1.f ) ;
2015-02-22 00:30:40 +08:00
edit_state . CursorAnimReset ( ) ;
2014-08-19 19:09:13 +08:00
2015-02-22 00:30:40 +08:00
if ( edit_state . Id ! = id )
{
edit_state . Id = id ;
2015-06-18 07:07:49 +08:00
edit_state . ScrollX = 0.f ;
2015-06-17 05:46:45 +08:00
stb_textedit_initialize_state ( & edit_state . StbState , ! is_multiline ) ;
2015-06-17 10:25:08 +08:00
if ( ! is_multiline & & focus_requested_by_code )
2015-02-22 00:30:40 +08:00
select_all = true ;
}
else
{
// Recycle existing cursor/selection/undo stack but clamp position
// Note a single mouse click will override the cursor/position immediately by calling stb_textedit_click handler.
2015-07-08 02:09:25 +08:00
edit_state . StbState . cursor = ImMin ( edit_state . StbState . cursor , edit_state . CurLenW ) ;
edit_state . StbState . select_start = ImMin ( edit_state . StbState . select_start , edit_state . CurLenW ) ;
edit_state . StbState . select_end = ImMin ( edit_state . StbState . select_end , edit_state . CurLenW ) ;
2015-02-22 00:30:40 +08:00
}
2015-07-11 08:47:55 +08:00
if ( flags & ImGuiInputTextFlags_AlwaysInsertMode )
edit_state . StbState . insert_mode = true ;
2015-06-17 10:25:08 +08:00
if ( ! is_multiline & & ( focus_requested_by_tab | | ( user_clicked & & is_ctrl_down ) ) )
2014-08-19 19:09:13 +08:00
select_all = true ;
}
2015-07-22 07:00:28 +08:00
SetActiveID ( id , window ) ;
2015-01-05 02:36:33 +08:00
FocusWindow ( window ) ;
2014-08-19 19:09:13 +08:00
}
else if ( io . MouseClicked [ 0 ] )
{
// Release focus when we click outside
if ( g . ActiveId = = id )
2015-07-22 07:00:28 +08:00
SetActiveID ( 0 ) ;
2014-08-19 19:09:13 +08:00
}
bool value_changed = false ;
bool cancel_edit = false ;
2014-09-08 21:48:39 +08:00
bool enter_pressed = false ;
2015-06-19 02:29:43 +08:00
2014-09-26 08:06:27 +08:00
if ( g . ActiveId = = id )
2014-08-19 19:09:13 +08:00
{
2015-03-09 04:21:19 +08:00
edit_state . BufSizeA = buf_size ;
2015-06-19 02:29:43 +08:00
// Although we are active we don't prevent mouse from hovering other elements unless we are interacting right now with the widget.
// Down the line we should have a cleaner concept of focused vs active in the library.
g . ActiveIdIsFocusedOnly = ! io . MouseDown [ 0 ] ;
2015-03-20 00:45:37 +08:00
// Edit in progress
2015-06-18 07:07:49 +08:00
const float mouse_x = ( g . IO . MousePos . x - frame_bb . Min . x - style . FramePadding . x ) + edit_state . ScrollX ;
2015-06-19 00:40:11 +08:00
const float mouse_y = ( is_multiline ? ( g . IO . MousePos . y - draw_window - > DC . CursorPos . y - style . FramePadding . y ) : ( g . FontSize * 0.5f ) ) ;
2014-08-19 19:09:13 +08:00
if ( select_all | | ( hovered & & io . MouseDoubleClicked [ 0 ] ) )
{
edit_state . SelectAll ( ) ;
edit_state . SelectedAllMouseLock = true ;
}
else if ( io . MouseClicked [ 0 ] & & ! edit_state . SelectedAllMouseLock )
{
2015-06-18 07:07:49 +08:00
stb_textedit_click ( & edit_state , & edit_state . StbState , mouse_x , mouse_y ) ;
2014-08-19 19:09:13 +08:00
edit_state . CursorAnimReset ( ) ;
}
else if ( io . MouseDown [ 0 ] & & ! edit_state . SelectedAllMouseLock )
{
2015-06-18 07:07:49 +08:00
stb_textedit_drag ( & edit_state , & edit_state . StbState , mouse_x , mouse_y ) ;
2014-08-19 19:09:13 +08:00
edit_state . CursorAnimReset ( ) ;
}
if ( edit_state . SelectedAllMouseLock & & ! io . MouseDown [ 0 ] )
2015-03-21 21:59:19 +08:00
edit_state . SelectedAllMouseLock = false ;
2014-08-19 19:09:13 +08:00
2015-02-01 07:51:00 +08:00
if ( g . IO . InputCharacters [ 0 ] )
{
// Process text input (before we check for Return because using some IME will effectively send a Return?)
for ( int n = 0 ; n < IM_ARRAYSIZE ( g . IO . InputCharacters ) & & g . IO . InputCharacters [ n ] ; n + + )
{
2015-06-17 05:46:45 +08:00
if ( unsigned int c = ( unsigned int ) g . IO . InputCharacters [ n ] )
2015-02-01 07:51:00 +08:00
{
// Insert character if they pass filtering
2015-02-12 01:39:13 +08:00
if ( ! InputTextFilterCharacter ( & c , flags , callback , user_data ) )
2015-02-01 07:51:00 +08:00
continue ;
2015-02-12 01:39:13 +08:00
edit_state . OnKeyPressed ( ( int ) c ) ;
2015-02-01 07:51:00 +08:00
}
}
// Consume characters
memset ( g . IO . InputCharacters , 0 , sizeof ( g . IO . InputCharacters ) ) ;
}
2014-08-19 19:09:13 +08:00
const int k_mask = ( is_shift_down ? STB_TEXTEDIT_K_SHIFT : 0 ) ;
2015-05-11 16:29:10 +08:00
const bool is_ctrl_only = is_ctrl_down & & ! is_alt_down & & ! is_shift_down ;
2015-06-17 05:46:45 +08:00
if ( IsKeyPressedMap ( ImGuiKey_LeftArrow ) ) { edit_state . OnKeyPressed ( is_ctrl_down ? STB_TEXTEDIT_K_WORDLEFT | k_mask : STB_TEXTEDIT_K_LEFT | k_mask ) ; }
else if ( IsKeyPressedMap ( ImGuiKey_RightArrow ) ) { edit_state . OnKeyPressed ( is_ctrl_down ? STB_TEXTEDIT_K_WORDRIGHT | k_mask : STB_TEXTEDIT_K_RIGHT | k_mask ) ; }
2015-06-20 08:01:13 +08:00
else if ( is_multiline & & IsKeyPressedMap ( ImGuiKey_UpArrow ) ) { if ( is_ctrl_down ) SetWindowScrollY ( draw_window , draw_window - > ScrollY - g . FontSize ) ; else edit_state . OnKeyPressed ( STB_TEXTEDIT_K_UP | k_mask ) ; }
else if ( is_multiline & & IsKeyPressedMap ( ImGuiKey_DownArrow ) ) { if ( is_ctrl_down ) SetWindowScrollY ( draw_window , draw_window - > ScrollY + g . FontSize ) ; else edit_state . OnKeyPressed ( STB_TEXTEDIT_K_DOWN | k_mask ) ; }
2015-06-17 05:46:45 +08:00
else if ( IsKeyPressedMap ( ImGuiKey_Home ) ) { edit_state . OnKeyPressed ( is_ctrl_down ? STB_TEXTEDIT_K_TEXTSTART | k_mask : STB_TEXTEDIT_K_LINESTART | k_mask ) ; }
else if ( IsKeyPressedMap ( ImGuiKey_End ) ) { edit_state . OnKeyPressed ( is_ctrl_down ? STB_TEXTEDIT_K_TEXTEND | k_mask : STB_TEXTEDIT_K_LINEEND | k_mask ) ; }
else if ( IsKeyPressedMap ( ImGuiKey_Delete ) ) { edit_state . OnKeyPressed ( STB_TEXTEDIT_K_DELETE | k_mask ) ; }
else if ( IsKeyPressedMap ( ImGuiKey_Backspace ) ) { edit_state . OnKeyPressed ( STB_TEXTEDIT_K_BACKSPACE | k_mask ) ; }
else if ( IsKeyPressedMap ( ImGuiKey_Enter ) )
{
bool ctrl_enter_for_new_line = ( flags & ImGuiInputTextFlags_CtrlEnterForNewLine ) ! = 0 ;
if ( ! is_multiline | | ( ctrl_enter_for_new_line & & ! is_ctrl_down ) | | ( ! ctrl_enter_for_new_line & & is_ctrl_down ) )
{
2015-07-22 07:00:28 +08:00
SetActiveID ( 0 ) ;
2015-06-17 05:46:45 +08:00
enter_pressed = true ;
}
else // New line
{
unsigned int c = ' \n ' ;
if ( InputTextFilterCharacter ( & c , flags , callback , user_data ) )
edit_state . OnKeyPressed ( ( int ) c ) ;
}
}
2015-06-17 10:25:08 +08:00
else if ( ( flags & ImGuiInputTextFlags_AllowTabInput ) & & IsKeyPressedMap ( ImGuiKey_Tab ) & & ! is_ctrl_down & & ! is_shift_down & & ! is_alt_down )
2015-06-17 05:46:45 +08:00
{
unsigned int c = ' \t ' ;
if ( InputTextFilterCharacter ( & c , flags , callback , user_data ) )
edit_state . OnKeyPressed ( ( int ) c ) ;
}
2015-07-22 07:00:28 +08:00
else if ( IsKeyPressedMap ( ImGuiKey_Escape ) ) { SetActiveID ( 0 ) ; cancel_edit = true ; }
2015-06-19 02:32:55 +08:00
else if ( is_ctrl_only & & IsKeyPressedMap ( ImGuiKey_Z ) ) { edit_state . OnKeyPressed ( STB_TEXTEDIT_K_UNDO ) ; edit_state . ClearSelection ( ) ; }
else if ( is_ctrl_only & & IsKeyPressedMap ( ImGuiKey_Y ) ) { edit_state . OnKeyPressed ( STB_TEXTEDIT_K_REDO ) ; edit_state . ClearSelection ( ) ; }
2015-06-18 07:07:49 +08:00
else if ( is_ctrl_only & & IsKeyPressedMap ( ImGuiKey_A ) ) { edit_state . SelectAll ( ) ; edit_state . CursorFollow = true ; }
else if ( is_ctrl_only & & ( IsKeyPressedMap ( ImGuiKey_X ) | | IsKeyPressedMap ( ImGuiKey_C ) ) & & ( ! is_multiline | | edit_state . HasSelection ( ) ) )
2014-08-19 19:09:13 +08:00
{
2014-09-25 21:51:06 +08:00
// Cut, Copy
const bool cut = IsKeyPressedMap ( ImGuiKey_X ) ;
if ( cut & & ! edit_state . HasSelection ( ) )
2014-08-19 19:09:13 +08:00
edit_state . SelectAll ( ) ;
if ( g . IO . SetClipboardTextFn )
2014-09-25 21:51:06 +08:00
{
const int ib = edit_state . HasSelection ( ) ? ImMin ( edit_state . StbState . select_start , edit_state . StbState . select_end ) : 0 ;
2015-07-08 02:09:25 +08:00
const int ie = edit_state . HasSelection ( ) ? ImMax ( edit_state . StbState . select_start , edit_state . StbState . select_end ) : edit_state . CurLenW ;
2015-06-17 11:04:27 +08:00
edit_state . TempTextBuffer . resize ( ( ie - ib ) * 4 + 1 ) ;
2015-07-08 02:39:08 +08:00
ImTextStrToUtf8 ( edit_state . TempTextBuffer . Data , edit_state . TempTextBuffer . Size , edit_state . Text . Data + ib , edit_state . Text . Data + ie ) ;
g . IO . SetClipboardTextFn ( edit_state . TempTextBuffer . Data ) ;
2014-09-25 21:51:06 +08:00
}
if ( cut )
2015-06-18 07:07:49 +08:00
{
edit_state . CursorFollow = true ;
2014-09-25 21:51:06 +08:00
stb_textedit_cut ( & edit_state , & edit_state . StbState ) ;
2015-06-18 07:07:49 +08:00
}
2014-08-19 19:09:13 +08:00
}
2015-05-11 16:29:10 +08:00
else if ( is_ctrl_only & & IsKeyPressedMap ( ImGuiKey_V ) )
2014-08-19 19:09:13 +08:00
{
2014-09-25 21:51:06 +08:00
// Paste
2014-08-19 19:09:13 +08:00
if ( g . IO . GetClipboardTextFn )
2014-09-25 18:35:23 +08:00
{
2014-08-19 19:09:13 +08:00
if ( const char * clipboard = g . IO . GetClipboardTextFn ( ) )
{
// Remove new-line from pasted buffer
2015-07-08 02:09:25 +08:00
const int clipboard_len = ( int ) strlen ( clipboard ) ;
2014-09-25 21:51:06 +08:00
ImWchar * clipboard_filtered = ( ImWchar * ) ImGui : : MemAlloc ( ( clipboard_len + 1 ) * sizeof ( ImWchar ) ) ;
2014-08-19 19:09:13 +08:00
int clipboard_filtered_len = 0 ;
2014-09-25 21:51:06 +08:00
for ( const char * s = clipboard ; * s ; )
2014-08-19 19:09:13 +08:00
{
2014-09-25 21:51:06 +08:00
unsigned int c ;
2015-02-11 20:23:38 +08:00
s + = ImTextCharFromUtf8 ( & c , s , NULL ) ;
if ( c = = 0 )
2014-09-25 21:51:06 +08:00
break ;
2015-06-18 07:07:49 +08:00
if ( c > = 0x10000 | | ! InputTextFilterCharacter ( & c , flags , callback , user_data ) )
2015-01-05 03:23:36 +08:00
continue ;
clipboard_filtered [ clipboard_filtered_len + + ] = ( ImWchar ) c ;
2014-08-19 19:09:13 +08:00
}
clipboard_filtered [ clipboard_filtered_len ] = 0 ;
2015-01-05 02:47:08 +08:00
if ( clipboard_filtered_len > 0 ) // If everything was filtered, ignore the pasting operation
2015-06-18 07:07:49 +08:00
{
2015-01-05 03:23:36 +08:00
stb_textedit_paste ( & edit_state , & edit_state . StbState , clipboard_filtered , clipboard_filtered_len ) ;
2015-06-18 07:07:49 +08:00
edit_state . CursorFollow = true ;
}
2014-09-25 01:01:07 +08:00
ImGui : : MemFree ( clipboard_filtered ) ;
2014-08-19 19:09:13 +08:00
}
2014-09-25 18:35:23 +08:00
}
2014-08-19 19:09:13 +08:00
}
if ( cancel_edit )
{
// Restore initial value
2015-07-08 02:39:08 +08:00
ImFormatString ( buf , buf_size , " %s " , edit_state . InitialText . Data ) ;
2014-08-19 19:09:13 +08:00
value_changed = true ;
}
else
{
// Apply new value immediately - copy modified buffer back
2014-09-25 21:51:06 +08:00
// Note that as soon as we can focus into the input box, the in-widget value gets priority over any underlying modification of the input buffer
// FIXME: We actually always render 'buf' in RenderTextScrolledClipped
2014-11-24 07:35:26 +08:00
// FIXME-OPT: CPU waste to do this every time the widget is active, should mark dirty state from the stb_textedit callbacks
2015-07-08 02:39:08 +08:00
edit_state . TempTextBuffer . resize ( edit_state . Text . Size * 4 ) ;
ImTextStrToUtf8 ( edit_state . TempTextBuffer . Data , edit_state . TempTextBuffer . Size , edit_state . Text . Data , NULL ) ;
2014-11-24 07:35:26 +08:00
// User callback
if ( ( flags & ( ImGuiInputTextFlags_CallbackCompletion | ImGuiInputTextFlags_CallbackHistory | ImGuiInputTextFlags_CallbackAlways ) ) ! = 0 )
{
IM_ASSERT ( callback ! = NULL ) ;
// The reason we specify the usage semantic (Completion/History) is that Completion needs to disable keyboard TABBING at the moment.
2015-02-12 01:39:13 +08:00
ImGuiInputTextFlags event_flag = 0 ;
2014-11-24 07:35:26 +08:00
ImGuiKey event_key = ImGuiKey_COUNT ;
if ( ( flags & ImGuiInputTextFlags_CallbackCompletion ) ! = 0 & & IsKeyPressedMap ( ImGuiKey_Tab ) )
2015-02-12 01:39:13 +08:00
{
event_flag = ImGuiInputTextFlags_CallbackCompletion ;
2014-11-24 07:35:26 +08:00
event_key = ImGuiKey_Tab ;
2015-02-12 01:39:13 +08:00
}
2014-11-24 07:35:26 +08:00
else if ( ( flags & ImGuiInputTextFlags_CallbackHistory ) ! = 0 & & IsKeyPressedMap ( ImGuiKey_UpArrow ) )
2015-02-12 01:39:13 +08:00
{
event_flag = ImGuiInputTextFlags_CallbackHistory ;
2014-11-24 07:35:26 +08:00
event_key = ImGuiKey_UpArrow ;
2015-02-12 01:39:13 +08:00
}
2014-11-24 07:35:26 +08:00
else if ( ( flags & ImGuiInputTextFlags_CallbackHistory ) ! = 0 & & IsKeyPressedMap ( ImGuiKey_DownArrow ) )
2015-02-12 01:39:13 +08:00
{
event_flag = ImGuiInputTextFlags_CallbackHistory ;
2014-11-24 07:35:26 +08:00
event_key = ImGuiKey_DownArrow ;
2015-02-12 01:39:13 +08:00
}
2014-11-24 07:35:26 +08:00
if ( event_key ! = ImGuiKey_COUNT | | ( flags & ImGuiInputTextFlags_CallbackAlways ) ! = 0 )
{
ImGuiTextEditCallbackData callback_data ;
2015-02-12 01:39:13 +08:00
callback_data . EventFlag = event_flag ;
2014-11-24 07:35:26 +08:00
callback_data . EventKey = event_key ;
2015-07-08 02:39:08 +08:00
callback_data . Buf = edit_state . TempTextBuffer . Data ;
2015-03-09 04:21:19 +08:00
callback_data . BufSize = edit_state . BufSizeA ;
2014-11-24 07:35:26 +08:00
callback_data . BufDirty = false ;
callback_data . Flags = flags ;
2014-11-27 06:16:38 +08:00
callback_data . UserData = user_data ;
2014-11-24 07:35:26 +08:00
// We have to convert from position from wchar to UTF-8 positions
2015-07-08 02:39:08 +08:00
ImWchar * text = edit_state . Text . Data ;
2015-06-17 11:04:27 +08:00
const int utf8_cursor_pos = callback_data . CursorPos = ImTextCountUtf8BytesFromStr ( text , text + edit_state . StbState . cursor ) ;
const int utf8_selection_start = callback_data . SelectionStart = ImTextCountUtf8BytesFromStr ( text , text + edit_state . StbState . select_start ) ;
const int utf8_selection_end = callback_data . SelectionEnd = ImTextCountUtf8BytesFromStr ( text , text + edit_state . StbState . select_end ) ;
2014-11-24 07:35:26 +08:00
2014-11-27 06:16:38 +08:00
// Call user code
callback ( & callback_data ) ;
2014-11-24 07:35:26 +08:00
// Read back what user may have modified
2015-07-08 02:39:08 +08:00
IM_ASSERT ( callback_data . Buf = = edit_state . TempTextBuffer . Data ) ; // Invalid to modify those fields
2015-03-09 04:21:19 +08:00
IM_ASSERT ( callback_data . BufSize = = edit_state . BufSizeA ) ;
2014-11-24 07:35:26 +08:00
IM_ASSERT ( callback_data . Flags = = flags ) ;
if ( callback_data . CursorPos ! = utf8_cursor_pos ) edit_state . StbState . cursor = ImTextCountCharsFromUtf8 ( callback_data . Buf , callback_data . Buf + callback_data . CursorPos ) ;
if ( callback_data . SelectionStart ! = utf8_selection_start ) edit_state . StbState . select_start = ImTextCountCharsFromUtf8 ( callback_data . Buf , callback_data . Buf + callback_data . SelectionStart ) ;
if ( callback_data . SelectionEnd ! = utf8_selection_end ) edit_state . StbState . select_end = ImTextCountCharsFromUtf8 ( callback_data . Buf , callback_data . Buf + callback_data . SelectionEnd ) ;
if ( callback_data . BufDirty )
{
2015-07-08 02:39:08 +08:00
edit_state . CurLenW = ImTextStrFromUtf8 ( text , edit_state . Text . Size , edit_state . TempTextBuffer . Data , NULL ) ;
edit_state . CurLenA = ( int ) strlen ( edit_state . TempTextBuffer . Data ) ;
2014-11-24 07:35:26 +08:00
edit_state . CursorAnimReset ( ) ;
}
}
}
2015-07-08 02:39:08 +08:00
if ( strcmp ( edit_state . TempTextBuffer . Data , buf ) ! = 0 )
2014-08-19 19:09:13 +08:00
{
2015-07-08 02:39:08 +08:00
ImFormatString ( buf , buf_size , " %s " , edit_state . TempTextBuffer . Data ) ;
2014-08-19 19:09:13 +08:00
value_changed = true ;
}
}
}
2015-06-18 07:07:49 +08:00
if ( ! is_multiline )
RenderFrame ( frame_bb . Min , frame_bb . Max , window - > Color ( ImGuiCol_FrameBg ) , true , style . FrameRounding ) ;
2014-08-19 19:09:13 +08:00
2015-06-19 11:20:15 +08:00
ImVec2 render_pos = is_multiline ? draw_window - > DC . CursorPos : frame_bb . Min + style . FramePadding ;
2014-08-19 19:09:13 +08:00
2015-06-18 07:07:49 +08:00
ImVec4 clip_rect ( frame_bb . Min . x , frame_bb . Min . y , frame_bb . Min . x + size . x + style . FramePadding . x * 2.0f , frame_bb . Min . y + size . y + style . FramePadding . y * 2.0f ) ;
2015-06-19 08:31:13 +08:00
ImVec2 text_size ( 0.f , 0.f ) ;
2015-06-19 02:29:43 +08:00
if ( g . ActiveId = = id | | ( edit_state . Id = = id & & is_multiline & & g . ActiveId = = draw_window - > GetID ( " #SCROLLY " ) ) )
2014-08-19 19:09:13 +08:00
{
2015-06-19 02:29:43 +08:00
edit_state . CursorAnim + = g . IO . DeltaTime ;
2015-06-19 13:23:20 +08:00
// We need to:
// - Display the text (this can be more easily clipped)
// - Handle scrolling, highlight selection, display cursor (those all requires some form of 1d->2d cursor position calculation)
// - Measure text height (for scrollbar)
// We are attempting to do most of that in one main pass to minimize the computation cost (non-negligible for large amount of text) + 2nd pass for selection rendering (we could merge them by an extra refactoring effort)
2015-07-08 02:39:08 +08:00
const ImWchar * text_begin = edit_state . Text . Data ;
2015-06-19 13:23:20 +08:00
const ImWchar * text_end = text_begin + edit_state . CurLenW ;
ImVec2 cursor_offset , select_start_offset ;
2015-06-19 08:31:13 +08:00
2015-06-19 13:23:20 +08:00
{
// Count lines + find lines numbers of cursor and select_start
int matches_remaining = 0 ;
int matches_line_no [ 2 ] = { - 1 , - 999 } ;
2015-07-16 01:56:07 +08:00
const ImWchar * matches_ptr [ 2 ] = { NULL , NULL } ;
2015-06-19 13:23:20 +08:00
matches_ptr [ 0 ] = text_begin + edit_state . StbState . cursor ; matches_remaining + + ;
if ( edit_state . StbState . select_start ! = edit_state . StbState . select_end )
{
matches_ptr [ 1 ] = text_begin + ImMin ( edit_state . StbState . select_start , edit_state . StbState . select_end ) ;
matches_line_no [ 1 ] = - 1 ;
matches_remaining + + ;
}
matches_remaining + = is_multiline ? 1 : 0 ; // So that we never exit the loop until all lines are counted.
int line_count = 0 ;
for ( const ImWchar * s = text_begin ; s < text_end + 1 ; s + + )
if ( ( * s ) = = ' \n ' | | s = = text_end )
{
line_count + + ;
if ( matches_line_no [ 0 ] = = - 1 & & s > = matches_ptr [ 0 ] ) { matches_line_no [ 0 ] = line_count ; if ( - - matches_remaining < = 0 ) break ; }
if ( matches_line_no [ 1 ] = = - 1 & & s > = matches_ptr [ 1 ] ) { matches_line_no [ 1 ] = line_count ; if ( - - matches_remaining < = 0 ) break ; }
}
// Calculate 2d position
IM_ASSERT ( matches_line_no [ 0 ] ! = - 1 ) ;
cursor_offset . x = InputTextCalcTextSizeW ( ImStrbolW ( matches_ptr [ 0 ] , text_begin ) , matches_ptr [ 0 ] ) . x ;
cursor_offset . y = matches_line_no [ 0 ] * g . FontSize ;
if ( matches_line_no [ 1 ] > = 0 )
{
select_start_offset . x = InputTextCalcTextSizeW ( ImStrbolW ( matches_ptr [ 1 ] , text_begin ) , matches_ptr [ 1 ] ) . x ;
select_start_offset . y = matches_line_no [ 1 ] * g . FontSize ;
}
// Calculate text height
if ( is_multiline )
text_size = ImVec2 ( size . x , line_count * g . FontSize ) ;
}
2015-06-19 07:40:51 +08:00
// Scroll
2015-06-18 07:07:49 +08:00
if ( edit_state . CursorFollow )
2014-08-19 19:09:13 +08:00
{
2015-06-18 07:07:49 +08:00
// Horizontal scroll in chunks of quarter width
2015-07-11 08:17:46 +08:00
if ( ! ( flags & ImGuiInputTextFlags_NoHorizontalScroll ) )
{
const float scroll_increment_x = size . x * 0.25f ;
if ( cursor_offset . x < edit_state . ScrollX )
edit_state . ScrollX = ImMax ( 0.0f , cursor_offset . x - scroll_increment_x ) ;
else if ( cursor_offset . x - size . x > = edit_state . ScrollX )
edit_state . ScrollX = cursor_offset . x - size . x + scroll_increment_x ;
}
else
{
edit_state . ScrollX = 0.0f ;
}
2015-06-18 07:07:49 +08:00
// Vertical scroll
if ( is_multiline )
{
2015-06-19 11:20:15 +08:00
float scroll_y = draw_window - > ScrollY ;
if ( cursor_offset . y - g . FontSize < scroll_y )
scroll_y = ImMax ( 0.0f , cursor_offset . y - g . FontSize ) ;
else if ( cursor_offset . y - size . y > = scroll_y )
scroll_y = cursor_offset . y - size . y ;
draw_window - > DC . CursorPos . y + = ( draw_window - > ScrollY - scroll_y ) ; // To avoid a frame of lag
draw_window - > ScrollY = scroll_y ;
render_pos . y = draw_window - > DC . CursorPos . y ;
2015-06-18 07:07:49 +08:00
}
2014-08-19 19:09:13 +08:00
}
2015-06-19 02:29:43 +08:00
edit_state . CursorFollow = false ;
2015-06-19 13:23:20 +08:00
ImVec2 render_scroll = ImVec2 ( edit_state . ScrollX , 0.0f ) ;
2014-08-19 19:09:13 +08:00
// Draw selection
2015-06-19 13:23:20 +08:00
if ( edit_state . StbState . select_start ! = edit_state . StbState . select_end )
2014-08-19 19:09:13 +08:00
{
2015-06-19 13:23:20 +08:00
const ImWchar * text_selected_begin = text_begin + ImMin ( edit_state . StbState . select_start , edit_state . StbState . select_end ) ;
const ImWchar * text_selected_end = text_begin + ImMax ( edit_state . StbState . select_start , edit_state . StbState . select_end ) ;
2014-08-19 19:09:13 +08:00
2015-06-19 11:01:56 +08:00
float bg_offy_up = is_multiline ? 0.0f : - 1.0f ; // FIXME: those offsets should be part of the style? they don't play so well with multi-line selection.
float bg_offy_dn = is_multiline ? 0.0f : 2.0f ;
2015-06-19 01:57:43 +08:00
ImU32 bg_color = draw_window - > Color ( ImGuiCol_TextSelectedBg ) ;
2015-06-19 13:23:20 +08:00
ImVec2 rect_pos = render_pos + select_start_offset - render_scroll ;
2015-06-17 10:14:54 +08:00
for ( const ImWchar * p = text_selected_begin ; p < text_selected_end ; )
{
2015-06-19 13:23:20 +08:00
if ( rect_pos . y > clip_rect . w + g . FontSize )
break ;
if ( rect_pos . y < clip_rect . y )
{
while ( p < text_selected_end )
if ( * p + + = = ' \n ' )
break ;
}
else
{
ImVec2 rect_size = InputTextCalcTextSizeW ( p , text_selected_end , & p , NULL , true ) ;
if ( rect_size . x < = 0.0f ) rect_size . x = ( float ) ( int ) ( g . Font - > GetCharAdvance ( ( unsigned short ) ' ' ) * 0.50f ) ; // So we can see selected empty lines
ImRect rect ( rect_pos + ImVec2 ( 0.0f , bg_offy_up - g . FontSize ) , rect_pos + ImVec2 ( rect_size . x , bg_offy_dn ) ) ;
rect . Clip ( clip_rect ) ;
if ( rect . Overlaps ( clip_rect ) )
draw_window - > DrawList - > AddRectFilled ( rect . Min , rect . Max , bg_color ) ;
}
rect_pos . x = render_pos . x - render_scroll . x ;
2015-06-17 10:14:54 +08:00
rect_pos . y + = g . FontSize ;
}
2014-08-19 19:09:13 +08:00
}
2015-06-17 10:14:54 +08:00
2015-06-19 10:49:20 +08:00
draw_window - > DrawList - > AddText ( g . Font , g . FontSize , render_pos - render_scroll , draw_window - > Color ( ImGuiCol_Text ) , buf , buf + edit_state . CurLenA , 0.0f , is_multiline ? NULL : & clip_rect ) ;
2014-09-25 21:51:06 +08:00
2014-08-19 19:09:13 +08:00
// Draw blinking cursor
2015-06-19 13:23:20 +08:00
ImVec2 cursor_screen_pos = render_pos + cursor_offset - render_scroll ;
2015-07-22 07:00:28 +08:00
bool cursor_is_visible = ( g . InputTextState . CursorAnim < = 0.0f ) | | fmodf ( g . InputTextState . CursorAnim , 1.20f ) < = 0.80f ;
if ( cursor_is_visible )
2015-07-06 06:24:55 +08:00
draw_window - > DrawList - > AddLine ( cursor_screen_pos + ImVec2 ( 0.0f , - g . FontSize + 0.5f ) , cursor_screen_pos + ImVec2 ( 0.0f , - 1.5f ) , window - > Color ( ImGuiCol_Text ) ) ;
2014-09-25 21:51:06 +08:00
2015-02-21 21:36:24 +08:00
// Notify OS of text input position for advanced IME
2015-06-19 07:40:51 +08:00
if ( io . ImeSetInputScreenPosFn & & ImLengthSqr ( edit_state . InputCursorScreenPos - cursor_screen_pos ) > 0.0001f )
io . ImeSetInputScreenPosFn ( ( int ) cursor_screen_pos . x - 1 , ( int ) ( cursor_screen_pos . y - g . FontSize ) ) ; // -1 x offset so that Windows IME can cover our cursor. Bit of an extra nicety.
2014-09-25 21:51:06 +08:00
2015-06-19 07:40:51 +08:00
edit_state . InputCursorScreenPos = cursor_screen_pos ;
2014-08-19 19:09:13 +08:00
}
2015-06-19 01:57:43 +08:00
else
{
// Render text only
2015-06-19 09:09:04 +08:00
const char * buf_end = NULL ;
2015-06-19 08:31:13 +08:00
if ( is_multiline )
2015-06-19 09:09:04 +08:00
text_size = ImVec2 ( size . x , InputTextCalcTextLenAndLineCount ( buf , & buf_end ) * g . FontSize ) ; // We don't need width
draw_window - > DrawList - > AddText ( g . Font , g . FontSize , render_pos , draw_window - > Color ( ImGuiCol_Text ) , buf , buf_end , 0.0f , is_multiline ? NULL : & clip_rect ) ;
2014-08-19 19:09:13 +08:00
}
2015-06-18 07:07:49 +08:00
if ( is_multiline )
{
ImGui : : Dummy ( text_size + ImVec2 ( 0.0f , g . FontSize ) ) ; // Always add room to scroll an extra line
ImGui : : EndChildFrame ( ) ;
ImGui : : EndGroup ( ) ;
}
2015-06-19 01:57:43 +08:00
// Log as text
2015-07-31 13:48:59 +08:00
if ( g . LogEnabled )
LogRenderedText ( render_pos , buf , NULL ) ;
2015-06-19 01:57:43 +08:00
2015-03-07 05:58:12 +08:00
if ( label_size . x > 0 )
2015-02-25 20:28:28 +08:00
RenderText ( ImVec2 ( frame_bb . Max . x + style . ItemInnerSpacing . x , frame_bb . Min . y + style . FramePadding . y ) , label ) ;
2014-08-19 19:09:13 +08:00
2014-09-08 21:48:39 +08:00
if ( ( flags & ImGuiInputTextFlags_EnterReturnsTrue ) ! = 0 )
return enter_pressed ;
else
return value_changed ;
2014-08-11 04:58:11 +08:00
}
2015-06-17 05:46:45 +08:00
bool ImGui : : InputText ( const char * label , char * buf , size_t buf_size , ImGuiInputTextFlags flags , ImGuiTextEditCallback callback , void * user_data )
{
IM_ASSERT ( ! ( flags & ImGuiInputTextFlags_Multiline ) ) ; // call InputTextMultiline()
2015-07-08 02:09:25 +08:00
bool ret = InputTextEx ( label , buf , ( int ) buf_size , ImVec2 ( 0 , 0 ) , flags , callback , user_data ) ;
2015-06-17 05:46:45 +08:00
return ret ;
}
bool ImGui : : InputTextMultiline ( const char * label , char * buf , size_t buf_size , const ImVec2 & size , ImGuiInputTextFlags flags , ImGuiTextEditCallback callback , void * user_data )
{
2015-07-08 02:09:25 +08:00
bool ret = InputTextEx ( label , buf , ( int ) buf_size , size , flags | ImGuiInputTextFlags_Multiline , callback , user_data ) ;
2015-06-17 05:46:45 +08:00
return ret ;
}
2015-06-17 05:57:04 +08:00
bool ImGui : : InputFloat ( const char * label , float * v , float step , float step_fast , int decimal_precision , ImGuiInputTextFlags extra_flags )
{
ImGuiWindow * window = GetCurrentWindow ( ) ;
if ( window - > SkipItems )
return false ;
2015-07-22 10:13:12 +08:00
ImGuiState & g = * GImGui ;
2015-06-17 05:57:04 +08:00
const ImGuiStyle & style = g . Style ;
const float w = ImGui : : CalcItemWidth ( ) ;
const ImVec2 label_size = CalcTextSize ( label , NULL , true ) ;
const ImRect frame_bb ( window - > DC . CursorPos , window - > DC . CursorPos + ImVec2 ( w , label_size . y ) + style . FramePadding * 2.0f ) ;
ImGui : : BeginGroup ( ) ;
ImGui : : PushID ( label ) ;
const ImVec2 button_sz = ImVec2 ( g . FontSize , g . FontSize ) + style . FramePadding * 2 ;
if ( step > 0.0f )
ImGui : : PushItemWidth ( ImMax ( 1.0f , w - ( button_sz . x + style . ItemInnerSpacing . x ) * 2 ) ) ;
char buf [ 64 ] ;
if ( decimal_precision < 0 )
ImFormatString ( buf , IM_ARRAYSIZE ( buf ) , " %f " , * v ) ; // Ideally we'd have a minimum decimal precision of 1 to visually denote that it is a float, while hiding non-significant digits?
else
ImFormatString ( buf , IM_ARRAYSIZE ( buf ) , " %.*f " , decimal_precision , * v ) ;
bool value_changed = false ;
const ImGuiInputTextFlags flags = extra_flags | ( ImGuiInputTextFlags_CharsDecimal | ImGuiInputTextFlags_AutoSelectAll ) ;
if ( ImGui : : InputText ( " " , buf , IM_ARRAYSIZE ( buf ) , flags ) )
{
2015-07-31 13:59:51 +08:00
InputTextApplyArithmeticOp ( buf , g . InputTextState . InitialText . Data , v ) ;
2015-06-17 05:57:04 +08:00
value_changed = true ;
}
// Step buttons
if ( step > 0.0f )
{
ImGui : : PopItemWidth ( ) ;
2015-07-11 09:36:34 +08:00
ImGui : : SameLine ( 0 , style . ItemInnerSpacing . x ) ;
2015-06-17 05:57:04 +08:00
if ( ButtonEx ( " - " , button_sz , ImGuiButtonFlags_Repeat | ImGuiButtonFlags_DontClosePopups ) )
{
* v - = g . IO . KeyCtrl & & step_fast > 0.0f ? step_fast : step ;
value_changed = true ;
}
2015-07-11 09:36:34 +08:00
ImGui : : SameLine ( 0 , style . ItemInnerSpacing . x ) ;
2015-06-17 05:57:04 +08:00
if ( ButtonEx ( " + " , button_sz , ImGuiButtonFlags_Repeat | ImGuiButtonFlags_DontClosePopups ) )
{
* v + = g . IO . KeyCtrl & & step_fast > 0.0f ? step_fast : step ;
value_changed = true ;
}
}
ImGui : : PopID ( ) ;
if ( label_size . x > 0 )
{
2015-07-11 09:36:34 +08:00
ImGui : : SameLine ( 0 , style . ItemInnerSpacing . x ) ;
2015-06-17 05:57:04 +08:00
RenderText ( ImVec2 ( window - > DC . CursorPos . x , window - > DC . CursorPos . y + style . FramePadding . y ) , label ) ;
ItemSize ( label_size , style . FramePadding . y ) ;
}
ImGui : : EndGroup ( ) ;
return value_changed ;
}
bool ImGui : : InputInt ( const char * label , int * v , int step , int step_fast , ImGuiInputTextFlags extra_flags )
{
float f = ( float ) * v ;
const bool value_changed = ImGui : : InputFloat ( label , & f , ( float ) step , ( float ) step_fast , 0 , extra_flags ) ;
if ( value_changed )
* v = ( int ) f ;
return value_changed ;
}
2015-08-02 11:57:24 +08:00
bool ImGui : : InputFloatN ( const char * label , float * v , int components , int decimal_precision , ImGuiInputTextFlags extra_flags )
2014-08-11 04:58:11 +08:00
{
2014-08-19 19:09:13 +08:00
ImGuiWindow * window = GetCurrentWindow ( ) ;
if ( window - > SkipItems )
return false ;
2015-07-22 10:13:12 +08:00
ImGuiState & g = * GImGui ;
2014-08-19 19:09:13 +08:00
bool value_changed = false ;
2015-03-19 22:48:36 +08:00
ImGui : : BeginGroup ( ) ;
2014-08-19 19:09:13 +08:00
ImGui : : PushID ( label ) ;
2015-05-12 04:44:50 +08:00
PushMultiItemsWidths ( components ) ;
2014-08-19 19:09:13 +08:00
for ( int i = 0 ; i < components ; i + + )
{
ImGui : : PushID ( i ) ;
2015-05-11 23:40:08 +08:00
value_changed | = ImGui : : InputFloat ( " ##v " , & v [ i ] , 0 , 0 , decimal_precision , extra_flags ) ;
2015-07-11 09:36:34 +08:00
ImGui : : SameLine ( 0 , g . Style . ItemInnerSpacing . x ) ;
2014-08-19 19:09:13 +08:00
ImGui : : PopID ( ) ;
2015-05-12 04:44:50 +08:00
ImGui : : PopItemWidth ( ) ;
2014-08-19 19:09:13 +08:00
}
ImGui : : PopID ( ) ;
2015-05-12 04:44:50 +08:00
window - > DC . CurrentLineTextBaseOffset = ImMax ( window - > DC . CurrentLineTextBaseOffset , g . Style . FramePadding . y ) ;
2014-08-19 19:09:13 +08:00
ImGui : : TextUnformatted ( label , FindTextDisplayEnd ( label ) ) ;
2015-03-19 22:48:36 +08:00
ImGui : : EndGroup ( ) ;
2014-08-19 19:09:13 +08:00
return value_changed ;
2014-08-14 01:38:24 +08:00
}
2015-05-11 23:40:08 +08:00
bool ImGui : : InputFloat2 ( const char * label , float v [ 2 ] , int decimal_precision , ImGuiInputTextFlags extra_flags )
2014-08-14 01:38:24 +08:00
{
2015-05-11 23:40:08 +08:00
return InputFloatN ( label , v , 2 , decimal_precision , extra_flags ) ;
2014-08-17 21:16:10 +08:00
}
2014-08-11 04:58:11 +08:00
2015-05-11 23:40:08 +08:00
bool ImGui : : InputFloat3 ( const char * label , float v [ 3 ] , int decimal_precision , ImGuiInputTextFlags extra_flags )
2014-08-17 21:16:10 +08:00
{
2015-05-11 23:40:08 +08:00
return InputFloatN ( label , v , 3 , decimal_precision , extra_flags ) ;
2014-08-17 21:16:10 +08:00
}
2014-08-11 04:58:11 +08:00
2015-05-11 23:40:08 +08:00
bool ImGui : : InputFloat4 ( const char * label , float v [ 4 ] , int decimal_precision , ImGuiInputTextFlags extra_flags )
2014-08-17 21:16:10 +08:00
{
2015-05-11 23:40:08 +08:00
return InputFloatN ( label , v , 4 , decimal_precision , extra_flags ) ;
2014-08-11 04:58:11 +08:00
}
2015-08-02 11:57:24 +08:00
bool ImGui : : InputIntN ( const char * label , int * v , int components , ImGuiInputTextFlags extra_flags )
2015-03-15 19:30:00 +08:00
{
ImGuiWindow * window = GetCurrentWindow ( ) ;
if ( window - > SkipItems )
return false ;
2015-07-22 10:13:12 +08:00
ImGuiState & g = * GImGui ;
2015-03-15 19:30:00 +08:00
bool value_changed = false ;
2015-03-19 22:48:36 +08:00
ImGui : : BeginGroup ( ) ;
2015-03-15 19:30:00 +08:00
ImGui : : PushID ( label ) ;
2015-05-12 04:44:50 +08:00
PushMultiItemsWidths ( components ) ;
2015-03-15 19:30:00 +08:00
for ( int i = 0 ; i < components ; i + + )
{
ImGui : : PushID ( i ) ;
2015-05-11 23:40:08 +08:00
value_changed | = ImGui : : InputInt ( " ##v " , & v [ i ] , 0 , 0 , extra_flags ) ;
2015-07-11 09:36:34 +08:00
ImGui : : SameLine ( 0 , g . Style . ItemInnerSpacing . x ) ;
2015-03-15 19:30:00 +08:00
ImGui : : PopID ( ) ;
2015-05-12 04:44:50 +08:00
ImGui : : PopItemWidth ( ) ;
2015-03-15 19:30:00 +08:00
}
ImGui : : PopID ( ) ;
2015-05-12 04:44:50 +08:00
window - > DC . CurrentLineTextBaseOffset = ImMax ( window - > DC . CurrentLineTextBaseOffset , g . Style . FramePadding . y ) ;
2015-03-15 19:30:00 +08:00
ImGui : : TextUnformatted ( label , FindTextDisplayEnd ( label ) ) ;
2015-03-19 22:48:36 +08:00
ImGui : : EndGroup ( ) ;
2015-03-15 19:30:00 +08:00
return value_changed ;
}
2015-05-11 23:40:08 +08:00
bool ImGui : : InputInt2 ( const char * label , int v [ 2 ] , ImGuiInputTextFlags extra_flags )
2015-03-15 19:30:00 +08:00
{
2015-05-11 23:40:08 +08:00
return InputIntN ( label , v , 2 , extra_flags ) ;
2015-03-15 19:30:00 +08:00
}
2015-05-11 23:40:08 +08:00
bool ImGui : : InputInt3 ( const char * label , int v [ 3 ] , ImGuiInputTextFlags extra_flags )
2015-03-15 19:30:00 +08:00
{
2015-05-11 23:40:08 +08:00
return InputIntN ( label , v , 3 , extra_flags ) ;
2015-03-15 19:30:00 +08:00
}
2015-05-11 23:40:08 +08:00
bool ImGui : : InputInt4 ( const char * label , int v [ 4 ] , ImGuiInputTextFlags extra_flags )
2015-03-15 19:30:00 +08:00
{
2015-05-11 23:40:08 +08:00
return InputIntN ( label , v , 4 , extra_flags ) ;
2015-03-15 19:30:00 +08:00
}
2015-02-11 01:45:45 +08:00
static bool Items_ArrayGetter ( void * data , int idx , const char * * out_text )
2014-08-11 04:58:11 +08:00
{
2014-08-19 19:09:13 +08:00
const char * * items = ( const char * * ) data ;
if ( out_text )
* out_text = items [ idx ] ;
return true ;
2014-08-11 04:58:11 +08:00
}
2015-02-11 01:45:45 +08:00
static bool Items_SingleStringGetter ( void * data , int idx , const char * * out_text )
2014-08-11 04:58:11 +08:00
{
2015-02-11 01:45:45 +08:00
// FIXME-OPT: we could pre-compute the indices to fasten this. But only 1 active combo means the waste is limited.
2014-08-19 19:09:13 +08:00
const char * items_separated_by_zeros = ( const char * ) data ;
int items_count = 0 ;
const char * p = items_separated_by_zeros ;
while ( * p )
{
if ( idx = = items_count )
break ;
p + = strlen ( p ) + 1 ;
items_count + + ;
}
if ( ! * p )
return false ;
if ( out_text )
* out_text = p ;
return true ;
2014-08-11 04:58:11 +08:00
}
2015-02-11 01:45:45 +08:00
// Combo box helper allowing to pass an array of strings.
2015-02-11 06:48:46 +08:00
bool ImGui : : Combo ( const char * label , int * current_item , const char * * items , int items_count , int height_in_items )
2015-02-11 01:45:45 +08:00
{
2015-02-11 06:48:46 +08:00
const bool value_changed = Combo ( label , current_item , Items_ArrayGetter , ( void * ) items , items_count , height_in_items ) ;
2015-02-11 01:45:45 +08:00
return value_changed ;
}
2014-09-02 21:35:06 +08:00
// Combo box helper allowing to pass all items in a single string.
2015-02-11 06:48:46 +08:00
bool ImGui : : Combo ( const char * label , int * current_item , const char * items_separated_by_zeros , int height_in_items )
2014-08-11 04:58:11 +08:00
{
2014-08-19 19:09:13 +08:00
int items_count = 0 ;
2015-06-10 02:35:55 +08:00
const char * p = items_separated_by_zeros ; // FIXME-OPT: Avoid computing this, or at least only when combo is open
2014-08-19 19:09:13 +08:00
while ( * p )
{
p + = strlen ( p ) + 1 ;
items_count + + ;
}
2015-02-11 06:48:46 +08:00
bool value_changed = Combo ( label , current_item , Items_SingleStringGetter , ( void * ) items_separated_by_zeros , items_count , height_in_items ) ;
2014-08-19 19:09:13 +08:00
return value_changed ;
2014-08-11 04:58:11 +08:00
}
2014-09-02 21:35:06 +08:00
// Combo box function.
2015-02-11 06:48:46 +08:00
bool ImGui : : Combo ( const char * label , int * current_item , bool ( * items_getter ) ( void * , int , const char * * ) , void * data , int items_count , int height_in_items )
2014-08-11 04:58:11 +08:00
{
2014-08-19 19:09:13 +08:00
ImGuiWindow * window = GetCurrentWindow ( ) ;
if ( window - > SkipItems )
return false ;
2015-07-22 10:13:12 +08:00
ImGuiState & g = * GImGui ;
2014-08-19 19:09:13 +08:00
const ImGuiStyle & style = g . Style ;
const ImGuiID id = window - > GetID ( label ) ;
2015-02-11 05:07:26 +08:00
const float w = ImGui : : CalcItemWidth ( ) ;
2014-08-19 19:09:13 +08:00
2015-03-07 05:58:12 +08:00
const ImVec2 label_size = CalcTextSize ( label , NULL , true ) ;
2015-03-18 07:35:12 +08:00
const ImRect frame_bb ( window - > DC . CursorPos , window - > DC . CursorPos + ImVec2 ( w , label_size . y ) + style . FramePadding * 2.0f ) ;
2015-05-25 06:45:44 +08:00
const ImRect total_bb ( frame_bb . Min , frame_bb . Max + ImVec2 ( label_size . x > 0.0f ? style . ItemInnerSpacing . x + label_size . x : 0.0f , 0.0f ) ) ;
2015-03-19 22:48:36 +08:00
ItemSize ( total_bb , style . FramePadding . y ) ;
if ( ! ItemAdd ( total_bb , & id ) )
2014-08-19 19:09:13 +08:00
return false ;
2015-03-30 02:45:03 +08:00
const float arrow_size = ( g . FontSize + style . FramePadding . x * 2.0f ) ;
2015-01-05 02:32:15 +08:00
const bool hovered = IsHovered ( frame_bb , id ) ;
2014-08-19 19:09:13 +08:00
2015-03-18 07:35:12 +08:00
const ImRect value_bb ( frame_bb . Min , frame_bb . Max - ImVec2 ( arrow_size , 0.0f ) ) ;
2015-01-15 17:14:44 +08:00
RenderFrame ( frame_bb . Min , frame_bb . Max , window - > Color ( ImGuiCol_FrameBg ) , true , style . FrameRounding ) ;
RenderFrame ( ImVec2 ( frame_bb . Max . x - arrow_size , frame_bb . Min . y ) , frame_bb . Max , window - > Color ( hovered ? ImGuiCol_ButtonHovered : ImGuiCol_Button ) , true , style . FrameRounding ) ; // FIXME-ROUNDING
2014-08-19 19:09:13 +08:00
RenderCollapseTriangle ( ImVec2 ( frame_bb . Max . x - arrow_size , frame_bb . Min . y ) + style . FramePadding , true ) ;
if ( * current_item > = 0 & & * current_item < items_count )
{
const char * item_text ;
if ( items_getter ( data , * current_item , & item_text ) )
2015-05-27 21:24:52 +08:00
RenderTextClipped ( frame_bb . Min + style . FramePadding , value_bb . Max , item_text , NULL , NULL ) ;
2014-08-19 19:09:13 +08:00
}
2015-03-07 05:58:12 +08:00
if ( label_size . x > 0 )
2015-02-25 20:28:28 +08:00
RenderText ( ImVec2 ( frame_bb . Max . x + style . ItemInnerSpacing . x , frame_bb . Min . y + style . FramePadding . y ) , label ) ;
2015-04-03 22:10:07 +08:00
2014-08-19 19:09:13 +08:00
bool menu_toggled = false ;
if ( hovered )
{
g . HoveredId = id ;
if ( g . IO . MouseClicked [ 0 ] )
{
2015-07-22 07:00:28 +08:00
SetActiveID ( 0 ) ;
2015-06-10 02:35:55 +08:00
if ( IsPopupOpen ( id ) )
{
ClosePopup ( id ) ;
}
else
{
FocusWindow ( window ) ;
ImGui : : OpenPopup ( label ) ;
menu_toggled = true ;
}
2014-08-19 19:09:13 +08:00
}
}
2015-06-10 02:35:55 +08:00
bool value_changed = false ;
if ( IsPopupOpen ( id ) )
2014-08-19 19:09:13 +08:00
{
2015-02-11 06:48:46 +08:00
// Size default to hold ~7 items
if ( height_in_items < 0 )
height_in_items = 7 ;
2015-07-18 06:14:04 +08:00
float popup_height = ( label_size . y + style . ItemSpacing . y ) * ImMin ( items_count , height_in_items ) + ( style . FramePadding . y * 3 ) ;
ImRect popup_rect ( ImVec2 ( frame_bb . Min . x , frame_bb . Max . y ) , ImVec2 ( frame_bb . Max . x , frame_bb . Max . y + popup_height ) ) ;
popup_rect . Max . y = ImMin ( popup_rect . Max . y , g . IO . DisplaySize . y - style . DisplaySafeAreaPadding . y ) ; // Adhoc height limit for Combo. Ideally should be handled in Begin() along with other popups size, we want to have the possibility of moving the popup above as well.
2015-06-10 02:35:55 +08:00
ImGui : : SetNextWindowPos ( popup_rect . Min ) ;
ImGui : : SetNextWindowSize ( popup_rect . GetSize ( ) ) ;
2015-04-03 21:32:46 +08:00
ImGui : : PushStyleVar ( ImGuiStyleVar_WindowPadding , style . FramePadding ) ;
2014-08-19 19:09:13 +08:00
2015-06-10 02:35:55 +08:00
const ImGuiWindowFlags flags = ImGuiWindowFlags_ComboBox | ( ( window - > Flags & ImGuiWindowFlags_ShowBorders ) ? ImGuiWindowFlags_ShowBorders : 0 ) ;
if ( BeginPopupEx ( label , flags ) )
2014-08-19 19:09:13 +08:00
{
2015-06-10 02:35:55 +08:00
// Display items
ImGui : : Spacing ( ) ;
for ( int i = 0 ; i < items_count ; i + + )
2014-08-19 19:09:13 +08:00
{
2015-06-10 02:35:55 +08:00
ImGui : : PushID ( ( void * ) ( intptr_t ) i ) ;
const bool item_selected = ( i = = * current_item ) ;
const char * item_text ;
if ( ! items_getter ( data , i , & item_text ) )
item_text = " *Unknown item* " ;
if ( ImGui : : Selectable ( item_text , item_selected ) )
{
2015-07-22 07:00:28 +08:00
SetActiveID ( 0 ) ;
2015-06-10 02:35:55 +08:00
value_changed = true ;
* current_item = i ;
}
if ( item_selected & & menu_toggled )
2015-07-03 11:03:15 +08:00
ImGui : : SetScrollHere ( ) ;
2015-06-10 02:35:55 +08:00
ImGui : : PopID ( ) ;
2014-08-19 19:09:13 +08:00
}
2015-06-10 02:35:55 +08:00
ImGui : : EndPopup ( ) ;
2014-08-19 19:09:13 +08:00
}
2015-04-03 21:32:46 +08:00
ImGui : : PopStyleVar ( ) ;
2014-08-19 19:09:13 +08:00
}
return value_changed ;
2014-08-11 04:58:11 +08:00
}
2015-06-15 02:41:40 +08:00
// Tip: pass an empty label (e.g. "##dummy") then you can use the space to draw other text or image.
// But you need to make sure the ID is unique, e.g. enclose calls in PushID/PopID.
2015-06-15 05:19:47 +08:00
bool ImGui : : Selectable ( const char * label , bool selected , ImGuiSelectableFlags flags , const ImVec2 & size_arg )
2015-02-11 03:09:37 +08:00
{
ImGuiWindow * window = GetCurrentWindow ( ) ;
if ( window - > SkipItems )
return false ;
2015-07-22 10:13:12 +08:00
ImGuiState & g = * GImGui ;
const ImGuiStyle & style = g . Style ;
2015-06-15 02:41:40 +08:00
if ( ( flags & ImGuiSelectableFlags_SpanAllColumns ) & & window - > DC . ColumnsCount > 1 )
PopClipRect ( ) ;
2015-05-19 02:41:02 +08:00
ImGuiID id = window - > GetID ( label ) ;
ImVec2 label_size = ImGui : : CalcTextSize ( label , NULL , true ) ;
ImVec2 size ( size_arg . x ! = 0.0f ? size_arg . x : label_size . x , size_arg . y ! = 0.0f ? size_arg . y : label_size . y ) ;
ImVec2 pos = window - > DC . CursorPos ;
2015-05-26 06:03:16 +08:00
pos . y + = window - > DC . CurrentLineTextBaseOffset ;
2015-05-17 20:09:46 +08:00
ImRect bb ( pos , pos + size ) ;
2015-02-11 03:09:37 +08:00
ItemSize ( bb ) ;
2015-05-17 22:48:56 +08:00
// Fill horizontal space.
2015-07-02 08:55:42 +08:00
ImVec2 window_padding = window - > WindowPadding ;
2015-06-15 02:41:40 +08:00
float max_x = ( flags & ImGuiSelectableFlags_SpanAllColumns ) ? ImGui : : GetWindowContentRegionMax ( ) . x : ImGui : : GetContentRegionMax ( ) . x ;
float w_draw = ImMax ( label_size . x , window - > Pos . x + max_x - window_padding . x - window - > DC . CursorPos . x ) ;
2015-06-14 04:20:02 +08:00
ImVec2 size_draw ( ( size_arg . x ! = 0 & & ! ( flags & ImGuiSelectableFlags_DrawFillAvailWidth ) ) ? size_arg . x : w_draw , size_arg . y ! = 0.0f ? size_arg . y : size . y ) ;
2015-05-17 22:48:56 +08:00
ImRect bb_with_spacing ( pos , pos + size_draw ) ;
2015-06-14 04:20:02 +08:00
if ( size_arg . x = = 0.0f | | ( flags & ImGuiSelectableFlags_DrawFillAvailWidth ) )
2015-05-17 20:09:46 +08:00
bb_with_spacing . Max . x + = window_padding . x ;
2015-05-17 22:48:56 +08:00
// Selectables are tightly packed together, we extend the box to cover spacing between selectable.
2015-05-19 02:41:02 +08:00
float spacing_L = ( float ) ( int ) ( style . ItemSpacing . x * 0.5f ) ;
float spacing_U = ( float ) ( int ) ( style . ItemSpacing . y * 0.5f ) ;
float spacing_R = style . ItemSpacing . x - spacing_L ;
float spacing_D = style . ItemSpacing . y - spacing_U ;
2015-02-11 03:09:37 +08:00
bb_with_spacing . Min . x - = spacing_L ;
2015-02-11 06:48:46 +08:00
bb_with_spacing . Min . y - = spacing_U ;
2015-02-11 03:09:37 +08:00
bb_with_spacing . Max . x + = spacing_R ;
2015-02-11 06:48:46 +08:00
bb_with_spacing . Max . y + = spacing_D ;
2015-02-11 03:09:37 +08:00
if ( ! ItemAdd ( bb_with_spacing , & id ) )
2015-06-15 02:41:40 +08:00
{
if ( ( flags & ImGuiSelectableFlags_SpanAllColumns ) & & window - > DC . ColumnsCount > 1 )
PushColumnClipRect ( ) ;
2015-02-11 03:09:37 +08:00
return false ;
2015-06-15 02:41:40 +08:00
}
2015-02-11 03:09:37 +08:00
2015-07-06 10:27:06 +08:00
ImGuiButtonFlags button_flags = 0 ;
if ( flags & ImGuiSelectableFlags_Menu ) button_flags | = ImGuiButtonFlags_PressedOnClick ;
if ( flags & ImGuiSelectableFlags_MenuItem ) button_flags | = ImGuiButtonFlags_PressedOnClick | ImGuiButtonFlags_PressedOnRelease ;
if ( flags & ImGuiSelectableFlags_Disabled ) button_flags | = ImGuiButtonFlags_Disabled ;
2015-02-11 03:09:37 +08:00
bool hovered , held ;
2015-07-06 10:27:06 +08:00
bool pressed = ButtonBehavior ( bb_with_spacing , id , & hovered , & held , true , button_flags ) ;
2015-05-27 01:02:42 +08:00
if ( flags & ImGuiSelectableFlags_Disabled )
selected = false ;
2015-02-11 03:09:37 +08:00
// Render
if ( hovered | | selected )
{
const ImU32 col = window - > Color ( ( held & & hovered ) ? ImGuiCol_HeaderActive : hovered ? ImGuiCol_HeaderHovered : ImGuiCol_Header ) ;
2015-06-10 04:32:37 +08:00
RenderFrame ( bb_with_spacing . Min , bb_with_spacing . Max , col , false , 0.0f ) ;
2015-02-11 03:09:37 +08:00
}
2015-06-15 02:41:40 +08:00
if ( ( flags & ImGuiSelectableFlags_SpanAllColumns ) & & window - > DC . ColumnsCount > 1 )
{
PushColumnClipRect ( ) ;
bb_with_spacing . Max . x - = ( ImGui : : GetContentRegionMax ( ) . x - max_x ) ;
2015-02-11 03:09:37 +08:00
}
2015-06-15 02:41:40 +08:00
2015-05-26 23:12:40 +08:00
if ( flags & ImGuiSelectableFlags_Disabled ) ImGui : : PushStyleColor ( ImGuiCol_Text , g . Style . Colors [ ImGuiCol_TextDisabled ] ) ;
2015-05-27 21:24:52 +08:00
RenderTextClipped ( bb . Min , bb_with_spacing . Max , label , NULL , & label_size ) ;
2015-05-26 23:12:40 +08:00
if ( flags & ImGuiSelectableFlags_Disabled ) ImGui : : PopStyleColor ( ) ;
2015-02-11 03:09:37 +08:00
2015-05-11 22:57:02 +08:00
// Automatically close popups
2015-05-27 03:33:48 +08:00
if ( pressed & & ! ( flags & ImGuiSelectableFlags_DontClosePopups ) & & ( window - > Flags & ImGuiWindowFlags_Popup ) )
2015-05-11 22:57:02 +08:00
ImGui : : CloseCurrentPopup ( ) ;
2015-02-11 03:09:37 +08:00
return pressed ;
}
2015-06-15 05:19:47 +08:00
bool ImGui : : Selectable ( const char * label , bool * p_selected , ImGuiSelectableFlags flags , const ImVec2 & size_arg )
2015-02-11 03:09:37 +08:00
{
2015-06-15 05:19:47 +08:00
if ( ImGui : : Selectable ( label , * p_selected , flags , size_arg ) )
2015-02-11 03:09:37 +08:00
{
* p_selected = ! * p_selected ;
return true ;
}
2015-02-11 03:47:35 +08:00
return false ;
2015-02-11 03:09:37 +08:00
}
2015-02-11 08:21:27 +08:00
// Helper to calculate the size of a listbox and display a label on the right.
// Tip: To have a list filling the entire window width, PushItemWidth(-1) and pass an empty label "##empty"
bool ImGui : : ListBoxHeader ( const char * label , const ImVec2 & size_arg )
{
ImGuiWindow * window = GetCurrentWindow ( ) ;
2015-05-31 18:54:00 +08:00
if ( window - > SkipItems )
return false ;
2015-02-11 08:21:27 +08:00
const ImGuiStyle & style = ImGui : : GetStyle ( ) ;
const ImGuiID id = ImGui : : GetID ( label ) ;
const ImVec2 label_size = ImGui : : CalcTextSize ( label , NULL , true ) ;
// Size default to hold ~7 items. Fractional number of items helps seeing that we can scroll down/up without looking at scrollbar.
2015-07-18 05:58:23 +08:00
ImVec2 size = CalcItemSize ( size_arg , ImGui : : CalcItemWidth ( ) + style . FramePadding . x * 2.0f , ImGui : : GetTextLineHeightWithSpacing ( ) * 7.4f + style . ItemSpacing . y ) ;
2015-05-19 02:41:02 +08:00
ImVec2 frame_size = ImVec2 ( size . x , ImMax ( size . y , label_size . y ) ) ;
ImRect frame_bb ( window - > DC . CursorPos , window - > DC . CursorPos + frame_size ) ;
ImRect bb ( frame_bb . Min , frame_bb . Max + ImVec2 ( label_size . x > 0.0f ? style . ItemInnerSpacing . x + label_size . x : 0.0f , 0.0f ) ) ;
2015-03-18 07:35:12 +08:00
window - > DC . LastItemRect = bb ;
2015-02-11 08:21:27 +08:00
2015-03-19 22:48:36 +08:00
ImGui : : BeginGroup ( ) ;
2015-02-11 08:21:27 +08:00
if ( label_size . x > 0 )
RenderText ( ImVec2 ( frame_bb . Max . x + style . ItemInnerSpacing . x , frame_bb . Min . y + style . FramePadding . y ) , label ) ;
ImGui : : BeginChildFrame ( id , frame_bb . GetSize ( ) ) ;
return true ;
}
bool ImGui : : ListBoxHeader ( const char * label , int items_count , int height_in_items )
{
// Size default to hold ~7 items. Fractional number of items helps seeing that we can scroll down/up without looking at scrollbar.
// However we don't add +0.40f if items_count <= height_in_items. It is slightly dodgy, because it means a dynamic list of items will make the widget resize occasionally when it crosses that size.
// I am expecting that someone will come and complain about this behavior in a remote future, then we can advise on a better solution.
if ( height_in_items < 0 )
height_in_items = ImMin ( items_count , 7 ) ;
float height_in_items_f = height_in_items < items_count ? ( height_in_items + 0.40f ) : ( height_in_items + 0.00f ) ;
// We include ItemSpacing.y so that a list sized for the exact number of items doesn't make a scrollbar appears. We could also enforce that by passing a flag to BeginChild().
ImVec2 size ;
size . x = 0.0f ;
size . y = ImGui : : GetTextLineHeightWithSpacing ( ) * height_in_items_f + ImGui : : GetStyle ( ) . ItemSpacing . y ;
return ImGui : : ListBoxHeader ( label , size ) ;
}
void ImGui : : ListBoxFooter ( )
{
2015-03-07 06:40:21 +08:00
ImGuiWindow * parent_window = GetParentWindow ( ) ;
2015-03-18 07:35:12 +08:00
const ImRect bb = parent_window - > DC . LastItemRect ;
2015-03-18 04:17:53 +08:00
const ImGuiStyle & style = ImGui : : GetStyle ( ) ;
2015-03-07 06:40:21 +08:00
2015-02-11 08:21:27 +08:00
ImGui : : EndChildFrame ( ) ;
2015-03-07 06:40:21 +08:00
2015-03-18 07:37:17 +08:00
// Redeclare item size so that it includes the label (we have stored the full size in LastItemRect)
2015-03-18 07:28:57 +08:00
// We call SameLine() to restore DC.CurrentLine* data
ImGui : : SameLine ( ) ;
2015-03-07 06:40:21 +08:00
parent_window - > DC . CursorPos = bb . Min ;
2015-03-18 04:17:53 +08:00
ItemSize ( bb , style . FramePadding . y ) ;
2015-03-19 22:48:36 +08:00
ImGui : : EndGroup ( ) ;
2015-02-11 08:21:27 +08:00
}
bool ImGui : : ListBox ( const char * label , int * current_item , const char * * items , int items_count , int height_items )
{
const bool value_changed = ListBox ( label , current_item , Items_ArrayGetter , ( void * ) items , items_count , height_items ) ;
return value_changed ;
}
bool ImGui : : ListBox ( const char * label , int * current_item , bool ( * items_getter ) ( void * , int , const char * * ) , void * data , int items_count , int height_in_items )
{
if ( ! ImGui : : ListBoxHeader ( label , items_count , height_in_items ) )
return false ;
2015-05-31 19:35:21 +08:00
// Assume all items have even height (= 1 line of text). If you need items of different or variable sizes you can create a custom version of ListBox() in your code without using the clipper.
2015-02-11 08:21:27 +08:00
bool value_changed = false ;
2015-05-31 19:35:21 +08:00
ImGuiListClipper clipper ( items_count , ImGui : : GetTextLineHeightWithSpacing ( ) ) ;
for ( int i = clipper . DisplayStart ; i < clipper . DisplayEnd ; i + + )
2015-02-11 08:21:27 +08:00
{
const bool item_selected = ( i = = * current_item ) ;
const char * item_text ;
if ( ! items_getter ( data , i , & item_text ) )
item_text = " *Unknown item* " ;
ImGui : : PushID ( i ) ;
if ( ImGui : : Selectable ( item_text , item_selected ) )
{
* current_item = i ;
value_changed = true ;
}
ImGui : : PopID ( ) ;
}
2015-05-31 19:35:21 +08:00
clipper . End ( ) ;
2015-02-11 08:21:27 +08:00
ImGui : : ListBoxFooter ( ) ;
return value_changed ;
}
2015-05-23 20:54:57 +08:00
bool ImGui : : MenuItem ( const char * label , const char * shortcut , bool selected , bool enabled )
2015-05-08 00:21:48 +08:00
{
ImGuiWindow * window = GetCurrentWindow ( ) ;
if ( window - > SkipItems )
return false ;
2015-07-22 10:13:12 +08:00
ImGuiState & g = * GImGui ;
2015-08-06 00:43:12 +08:00
ImVec2 pos = window - > DC . CursorPos ;
2015-05-12 03:58:07 +08:00
ImVec2 label_size = CalcTextSize ( label , NULL , true ) ;
ImVec2 shortcut_size = shortcut ? CalcTextSize ( shortcut , NULL ) : ImVec2 ( 0.0f , 0.0f ) ;
2015-05-17 22:48:56 +08:00
float w = window - > MenuColumns . DeclColumns ( label_size . x , shortcut_size . x , ( float ) ( int ) ( g . FontSize * 1.20f ) ) ; // Feedback for next frame
2015-08-06 00:43:12 +08:00
float extra_w = ImMax ( 0.0f , ImGui : : GetContentRegionAvail ( ) . x - w ) ;
2015-05-08 00:21:48 +08:00
2015-06-15 05:19:47 +08:00
bool pressed = ImGui : : Selectable ( label , false , ImGuiSelectableFlags_MenuItem | ImGuiSelectableFlags_DrawFillAvailWidth | ( ! enabled ? ImGuiSelectableFlags_Disabled : 0 ) , ImVec2 ( w , 0.0f ) ) ;
2015-05-12 02:52:02 +08:00
if ( shortcut_size . x > 0.0f )
2015-05-08 00:21:48 +08:00
{
2015-05-12 02:52:02 +08:00
ImGui : : PushStyleColor ( ImGuiCol_Text , g . Style . Colors [ ImGuiCol_TextDisabled ] ) ;
2015-05-17 22:48:56 +08:00
RenderText ( pos + ImVec2 ( window - > MenuColumns . Pos [ 1 ] + extra_w , 0.0f ) , shortcut , NULL , false ) ;
2015-05-12 02:52:02 +08:00
ImGui : : PopStyleColor ( ) ;
2015-05-08 00:21:48 +08:00
}
2015-05-12 02:52:02 +08:00
if ( selected )
2015-05-17 22:48:56 +08:00
RenderCheckMark ( pos + ImVec2 ( window - > MenuColumns . Pos [ 2 ] + extra_w + g . FontSize * 0.20f , 0.0f ) , window - > Color ( ImGuiCol_Text ) ) ;
2015-05-12 02:52:02 +08:00
2015-05-11 22:57:02 +08:00
return pressed ;
2015-05-08 00:21:48 +08:00
}
2015-05-23 20:54:57 +08:00
bool ImGui : : MenuItem ( const char * label , const char * shortcut , bool * p_selected , bool enabled )
2015-05-08 00:21:48 +08:00
{
2015-05-23 20:54:57 +08:00
if ( ImGui : : MenuItem ( label , shortcut , p_selected ? * p_selected : false , enabled ) )
2015-05-08 00:21:48 +08:00
{
if ( p_selected )
* p_selected = ! * p_selected ;
return true ;
}
return false ;
}
2015-05-23 20:43:45 +08:00
bool ImGui : : BeginMainMenuBar ( )
{
ImGuiState & g = * GImGui ;
ImGui : : SetNextWindowPos ( ImVec2 ( 0.0f , 0.0f ) ) ;
ImGui : : SetNextWindowSize ( ImVec2 ( g . IO . DisplaySize . x , g . FontBaseSize + g . Style . FramePadding . y * 2.0f ) ) ;
ImGui : : PushStyleVar ( ImGuiStyleVar_WindowRounding , 0.0f ) ;
ImGui : : PushStyleVar ( ImGuiStyleVar_WindowMinSize , ImVec2 ( 0 , 0 ) ) ;
if ( ! ImGui : : Begin ( " ##MainMenuBar " , NULL , ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoScrollbar | ImGuiWindowFlags_NoSavedSettings | ImGuiWindowFlags_MenuBar )
| | ! ImGui : : BeginMenuBar ( ) )
{
ImGui : : End ( ) ;
ImGui : : PopStyleVar ( 2 ) ;
return false ;
}
2015-05-25 04:11:11 +08:00
g . CurrentWindow - > DC . MenuBarOffsetX + = g . Style . DisplaySafeAreaPadding . x ;
2015-05-23 20:43:45 +08:00
return true ;
}
void ImGui : : EndMainMenuBar ( )
{
ImGui : : EndMenuBar ( ) ;
ImGui : : End ( ) ;
ImGui : : PopStyleVar ( 2 ) ;
}
2015-05-18 00:13:45 +08:00
bool ImGui : : BeginMenuBar ( )
{
ImGuiWindow * window = GetCurrentWindow ( ) ;
if ( window - > SkipItems )
return false ;
2015-05-18 06:13:53 +08:00
if ( ! ( window - > Flags & ImGuiWindowFlags_MenuBar ) )
return false ;
2015-05-18 00:13:45 +08:00
IM_ASSERT ( ! window - > DC . MenuBarAppending ) ;
2015-05-26 06:08:22 +08:00
ImGui : : BeginGroup ( ) ; // Save position
2015-05-18 00:13:45 +08:00
ImGui : : PushID ( " ##menubar " ) ;
2015-05-18 22:54:56 +08:00
ImRect rect = window - > MenuBarRect ( ) ;
PushClipRect ( ImVec4 ( rect . Min . x + 0.5f , rect . Min . y - 0.5f , rect . Max . x + 0.5f , rect . Max . y - 1.5f ) , false ) ;
2015-05-26 06:08:22 +08:00
window - > DC . CursorPos = ImVec2 ( rect . Min . x + window - > DC . MenuBarOffsetX , rect . Min . y ) ; // + g.Style.FramePadding.y);
window - > DC . LayoutType = ImGuiLayoutType_Horizontal ;
window - > DC . MenuBarAppending = true ;
ImGui : : AlignFirstTextHeightToWidgets ( ) ;
2015-05-18 00:13:45 +08:00
return true ;
}
void ImGui : : EndMenuBar ( )
{
ImGuiWindow * window = GetCurrentWindow ( ) ;
if ( window - > SkipItems )
return ;
IM_ASSERT ( window - > Flags & ImGuiWindowFlags_MenuBar ) ;
IM_ASSERT ( window - > DC . MenuBarAppending ) ;
2015-05-18 22:54:56 +08:00
PopClipRect ( ) ;
2015-05-18 00:13:45 +08:00
ImGui : : PopID ( ) ;
2015-05-26 06:08:22 +08:00
window - > DC . MenuBarOffsetX = window - > DC . CursorPos . x - window - > MenuBarRect ( ) . Min . x ;
window - > DC . GroupStack . back ( ) . AdvanceCursor = false ;
ImGui : : EndGroup ( ) ;
window - > DC . LayoutType = ImGuiLayoutType_Vertical ;
window - > DC . MenuBarAppending = false ;
2015-05-18 00:13:45 +08:00
}
2015-05-27 01:02:42 +08:00
bool ImGui : : BeginMenu ( const char * label , bool enabled )
2015-05-12 02:52:02 +08:00
{
ImGuiWindow * window = GetCurrentWindow ( ) ;
if ( window - > SkipItems )
return false ;
2015-05-26 06:08:22 +08:00
ImGuiState & g = * GImGui ;
2015-05-18 00:13:45 +08:00
const ImGuiStyle & style = g . Style ;
2015-05-12 02:52:02 +08:00
const ImGuiID id = window - > GetID ( label ) ;
2015-05-12 03:58:07 +08:00
ImVec2 label_size = CalcTextSize ( label , NULL , true ) ;
2015-05-19 02:12:36 +08:00
ImGuiWindow * backed_focused_window = g . FocusedWindow ;
2015-05-18 00:13:45 +08:00
2015-05-19 01:44:15 +08:00
bool pressed ;
2015-05-19 02:12:36 +08:00
bool opened = IsPopupOpen ( id ) ;
2015-07-08 02:39:08 +08:00
bool menuset_opened = ! ( window - > Flags & ImGuiWindowFlags_Popup ) & & ( g . OpenedPopupStack . Size > g . CurrentPopupStack . Size & & g . OpenedPopupStack [ g . CurrentPopupStack . Size ] . ParentMenuSet = = window - > GetID ( " ##menus " ) ) ;
2015-05-19 02:12:36 +08:00
if ( menuset_opened )
g . FocusedWindow = window ;
2015-05-19 01:44:15 +08:00
2015-05-26 06:08:22 +08:00
ImVec2 popup_pos , pos = window - > DC . CursorPos ;
if ( window - > DC . LayoutType = = ImGuiLayoutType_Horizontal )
2015-05-18 00:13:45 +08:00
{
2015-07-02 08:55:42 +08:00
popup_pos = ImVec2 ( pos . x - window - > WindowPadding . x , pos . y - style . FramePadding . y + window - > MenuBarHeight ( ) ) ;
2015-05-26 06:15:00 +08:00
window - > DC . CursorPos . x + = ( float ) ( int ) ( style . ItemSpacing . x * 0.5f ) ;
2015-05-18 00:13:45 +08:00
ImGui : : PushStyleVar ( ImGuiStyleVar_ItemSpacing , style . ItemSpacing * 2.0f ) ;
float w = label_size . x ;
2015-07-06 10:27:06 +08:00
pressed = ImGui : : Selectable ( label , opened , ImGuiSelectableFlags_Menu | ImGuiSelectableFlags_DontClosePopups | ( ! enabled ? ImGuiSelectableFlags_Disabled : 0 ) , ImVec2 ( w , 0.0f ) ) ;
2015-05-18 00:13:45 +08:00
ImGui : : PopStyleVar ( ) ;
2015-05-26 06:15:00 +08:00
ImGui : : SameLine ( ) ;
window - > DC . CursorPos . x + = ( float ) ( int ) ( style . ItemSpacing . x * 0.5f ) ;
2015-05-18 00:13:45 +08:00
}
else
{
popup_pos = ImVec2 ( pos . x , pos . y - style . WindowPadding . y ) ;
float w = window - > MenuColumns . DeclColumns ( label_size . x , 0.0f , ( float ) ( int ) ( g . FontSize * 1.20f ) ) ; // Feedback to next frame
2015-08-06 00:43:12 +08:00
float extra_w = ImMax ( 0.0f , ImGui : : GetContentRegionAvail ( ) . x - w ) ;
2015-07-06 10:27:06 +08:00
pressed = ImGui : : Selectable ( label , opened , ImGuiSelectableFlags_Menu | ImGuiSelectableFlags_DontClosePopups | ImGuiSelectableFlags_DrawFillAvailWidth | ( ! enabled ? ImGuiSelectableFlags_Disabled : 0 ) , ImVec2 ( w , 0.0f ) ) ;
2015-05-27 01:02:42 +08:00
if ( ! enabled ) ImGui : : PushStyleColor ( ImGuiCol_Text , g . Style . Colors [ ImGuiCol_TextDisabled ] ) ;
2015-05-18 00:13:45 +08:00
RenderCollapseTriangle ( pos + ImVec2 ( window - > MenuColumns . Pos [ 2 ] + extra_w + g . FontSize * 0.20f , 0.0f ) , false ) ;
2015-05-27 01:02:42 +08:00
if ( ! enabled ) ImGui : : PopStyleColor ( ) ;
2015-05-18 00:13:45 +08:00
}
2015-05-12 02:52:02 +08:00
2015-05-27 01:02:42 +08:00
bool hovered = enabled & & IsHovered ( window - > DC . LastItemRect , id ) ;
2015-05-19 02:12:36 +08:00
if ( menuset_opened )
g . FocusedWindow = backed_focused_window ;
2015-05-19 01:44:15 +08:00
2015-05-29 06:14:25 +08:00
bool want_open = false , want_close = false ;
2015-05-19 01:44:15 +08:00
if ( window - > Flags & ( ImGuiWindowFlags_Popup | ImGuiWindowFlags_ChildMenu ) )
2015-05-26 22:01:04 +08:00
{
2015-05-27 00:09:21 +08:00
// Implement http://bjk5.com/post/44698559168/breaking-down-amazons-mega-dropdown to avoid using timers so menus feel more reactive.
bool moving_within_opened_triangle = false ;
2015-07-08 02:39:08 +08:00
if ( g . HoveredWindow = = window & & g . OpenedPopupStack . Size > g . CurrentPopupStack . Size & & g . OpenedPopupStack [ g . CurrentPopupStack . Size ] . ParentWindow = = window )
2015-05-27 00:09:21 +08:00
{
2015-07-08 02:39:08 +08:00
if ( ImGuiWindow * next_window = g . OpenedPopupStack [ g . CurrentPopupStack . Size ] . Window )
2015-05-27 00:09:21 +08:00
{
ImRect next_window_rect = next_window - > Rect ( ) ;
ImVec2 ta = g . IO . MousePos - g . IO . MouseDelta ;
ImVec2 tb = ( window - > Pos . x < next_window - > Pos . x ) ? next_window_rect . GetTL ( ) : next_window_rect . GetTR ( ) ;
ImVec2 tc = ( window - > Pos . x < next_window - > Pos . x ) ? next_window_rect . GetBL ( ) : next_window_rect . GetBR ( ) ;
2015-05-27 00:27:35 +08:00
float extra = ImClamp ( fabsf ( ta . x - tb . x ) * 0.30f , 5.0f , 30.0f ) ; // add a bit of extra slack.
2015-05-27 00:09:21 +08:00
ta . x + = ( window - > Pos . x < next_window - > Pos . x ) ? - 0.5f : + 0.5f ; // to avoid numerical issues
tb . y = ta . y + ImMax ( ( tb . y - extra ) - ta . y , - 100.0f ) ; // triangle is maximum 200 high to limit the slope and the bias toward large sub-menus
tc . y = ta . y + ImMin ( ( tc . y + extra ) - ta . y , + 100.0f ) ;
moving_within_opened_triangle = ImIsPointInTriangle ( g . IO . MousePos , ta , tb , tc ) ;
//window->DrawList->PushClipRectFullScreen(); window->DrawList->AddTriangleFilled(ta, tb, tc, moving_within_opened_triangle ? 0x80008000 : 0x80000080); window->DrawList->PopClipRect(); // Debug
}
}
2015-05-29 06:14:25 +08:00
want_close = ( opened & & ! hovered & & g . HoveredWindow = = window & & g . HoveredIdPreviousFrame ! = 0 & & g . HoveredIdPreviousFrame ! = id & & ! moving_within_opened_triangle ) ;
2015-05-27 00:09:21 +08:00
want_open = ( ! opened & & hovered & & ! moving_within_opened_triangle ) | | ( ! opened & & hovered & & pressed ) ;
2015-05-26 22:01:04 +08:00
}
else if ( opened & & pressed & & menuset_opened ) // menu-bar: click open menu to close
2015-05-12 02:52:02 +08:00
{
2015-05-29 06:14:25 +08:00
want_close = true ;
2015-05-19 02:41:02 +08:00
want_open = opened = false ;
2015-05-12 02:52:02 +08:00
}
2015-05-26 22:01:04 +08:00
else if ( pressed | | ( hovered & & menuset_opened & & ! opened ) ) // menu-bar: first click to open, then hover to open others
2015-05-19 02:41:02 +08:00
want_open = true ;
2015-05-29 06:14:25 +08:00
if ( want_close & & IsPopupOpen ( id ) )
2015-07-08 02:39:08 +08:00
ClosePopupToLevel ( GImGui - > CurrentPopupStack . Size ) ;
2015-05-29 06:14:25 +08:00
2015-07-08 02:39:08 +08:00
if ( ! opened & & want_open & & g . OpenedPopupStack . Size > g . CurrentPopupStack . Size )
2015-05-19 04:26:46 +08:00
{
// Don't recycle same menu level in the same frame, first close the other menu and yield for a frame.
ImGui : : OpenPopup ( label ) ;
return false ;
}
2015-05-19 02:41:02 +08:00
opened | = want_open ;
if ( want_open )
ImGui : : OpenPopup ( label ) ;
2015-05-19 01:44:15 +08:00
2015-05-12 02:52:02 +08:00
if ( opened )
{
2015-05-18 00:13:45 +08:00
ImGui : : SetNextWindowPos ( popup_pos , ImGuiSetCond_Always ) ;
2015-06-10 02:35:55 +08:00
ImGuiWindowFlags flags = ImGuiWindowFlags_ShowBorders | ( ( window - > Flags & ( ImGuiWindowFlags_Popup | ImGuiWindowFlags_ChildMenu ) ) ? ImGuiWindowFlags_ChildMenu | ImGuiWindowFlags_ChildWindow : ImGuiWindowFlags_ChildMenu ) ;
2015-05-19 01:44:15 +08:00
opened = BeginPopupEx ( label , flags ) ; // opened can be 'false' when the popup is completely clipped (e.g. zero size display)
2015-05-12 02:52:02 +08:00
}
return opened ;
}
void ImGui : : EndMenu ( )
{
ImGui : : EndPopup ( ) ;
}
2014-08-11 04:58:11 +08:00
// A little colored square. Return true when clicked.
2014-11-10 09:29:01 +08:00
bool ImGui : : ColorButton ( const ImVec4 & col , bool small_height , bool outline_border )
2014-08-11 04:58:11 +08:00
{
2014-08-19 19:09:13 +08:00
ImGuiWindow * window = GetCurrentWindow ( ) ;
if ( window - > SkipItems )
return false ;
2014-08-11 04:58:11 +08:00
2015-07-22 10:13:12 +08:00
ImGuiState & g = * GImGui ;
2014-08-19 19:09:13 +08:00
const ImGuiStyle & style = g . Style ;
2015-03-08 06:01:06 +08:00
const ImGuiID id = window - > GetID ( " #colorbutton " ) ;
2015-03-30 02:45:03 +08:00
const float square_size = g . FontSize ;
2015-06-10 04:39:27 +08:00
const ImRect bb ( window - > DC . CursorPos , window - > DC . CursorPos + ImVec2 ( square_size + style . FramePadding . y * 2 , square_size + ( small_height ? 0 : style . FramePadding . y * 2 ) ) ) ;
2015-03-18 04:17:53 +08:00
ItemSize ( bb , small_height ? 0.0f : style . FramePadding . y ) ;
2015-01-23 06:54:05 +08:00
if ( ! ItemAdd ( bb , & id ) )
2014-08-19 19:09:13 +08:00
return false ;
2014-08-11 04:58:11 +08:00
2015-01-15 17:49:12 +08:00
bool hovered , held ;
2015-03-15 09:48:59 +08:00
bool pressed = ButtonBehavior ( bb , id , & hovered , & held , true ) ;
2015-01-15 17:14:44 +08:00
RenderFrame ( bb . Min , bb . Max , window - > Color ( col ) , outline_border , style . FrameRounding ) ;
2014-08-11 04:58:11 +08:00
2014-08-19 19:09:13 +08:00
if ( hovered )
{
int ix = ( int ) ( col . x * 255.0f + 0.5f ) ;
int iy = ( int ) ( col . y * 255.0f + 0.5f ) ;
int iz = ( int ) ( col . z * 255.0f + 0.5f ) ;
int iw = ( int ) ( col . w * 255.0f + 0.5f ) ;
ImGui : : SetTooltip ( " Color: \n (%.2f,%.2f,%.2f,%.2f) \n #%02X%02X%02X%02X " , col . x , col . y , col . z , col . w , ix , iy , iz , iw ) ;
}
2014-08-11 04:58:11 +08:00
2014-08-19 19:09:13 +08:00
return pressed ;
2014-08-11 04:58:11 +08:00
}
2014-11-10 09:29:01 +08:00
bool ImGui : : ColorEdit3 ( const char * label , float col [ 3 ] )
2014-08-11 04:58:11 +08:00
{
2014-08-19 19:09:13 +08:00
float col4 [ 4 ] ;
col4 [ 0 ] = col [ 0 ] ;
col4 [ 1 ] = col [ 1 ] ;
col4 [ 2 ] = col [ 2 ] ;
col4 [ 3 ] = 1.0f ;
2014-09-02 21:35:06 +08:00
const bool value_changed = ImGui : : ColorEdit4 ( label , col4 , false ) ;
2014-08-19 19:09:13 +08:00
col [ 0 ] = col4 [ 0 ] ;
col [ 1 ] = col4 [ 1 ] ;
col [ 2 ] = col4 [ 2 ] ;
return value_changed ;
2014-08-11 04:58:11 +08:00
}
2014-11-24 18:02:39 +08:00
// Edit colors components (each component in 0.0f..1.0f range
2014-08-11 04:58:11 +08:00
// Use CTRL-Click to input value and TAB to go to next item.
2014-11-10 09:29:01 +08:00
bool ImGui : : ColorEdit4 ( const char * label , float col [ 4 ] , bool alpha )
2014-08-11 04:58:11 +08:00
{
2014-08-19 19:09:13 +08:00
ImGuiWindow * window = GetCurrentWindow ( ) ;
if ( window - > SkipItems )
return false ;
2015-07-22 10:13:12 +08:00
ImGuiState & g = * GImGui ;
2014-08-19 19:09:13 +08:00
const ImGuiStyle & style = g . Style ;
const ImGuiID id = window - > GetID ( label ) ;
2015-02-11 05:07:26 +08:00
const float w_full = ImGui : : CalcItemWidth ( ) ;
2015-06-10 04:39:27 +08:00
const float square_sz = ( g . FontSize + style . FramePadding . y * 2.0f ) ;
2014-08-19 19:09:13 +08:00
ImGuiColorEditMode edit_mode = window - > DC . ColorEditMode ;
2015-01-15 17:49:12 +08:00
if ( edit_mode = = ImGuiColorEditMode_UserSelect | | edit_mode = = ImGuiColorEditMode_UserSelectShowButton )
2014-08-19 19:09:13 +08:00
edit_mode = g . ColorEditModeStorage . GetInt ( id , 0 ) % 3 ;
2015-04-03 18:49:02 +08:00
float f [ 4 ] = { col [ 0 ] , col [ 1 ] , col [ 2 ] , col [ 3 ] } ;
2014-08-19 19:09:13 +08:00
if ( edit_mode = = ImGuiColorEditMode_HSV )
2015-04-03 18:49:02 +08:00
ImGui : : ColorConvertRGBtoHSV ( f [ 0 ] , f [ 1 ] , f [ 2 ] , f [ 0 ] , f [ 1 ] , f [ 2 ] ) ;
2014-08-19 19:09:13 +08:00
2015-04-03 18:49:02 +08:00
int i [ 4 ] = { ( int ) ( f [ 0 ] * 255.0f + 0.5f ) , ( int ) ( f [ 1 ] * 255.0f + 0.5f ) , ( int ) ( f [ 2 ] * 255.0f + 0.5f ) , ( int ) ( f [ 3 ] * 255.0f + 0.5f ) } ;
2014-08-19 19:09:13 +08:00
int components = alpha ? 4 : 3 ;
bool value_changed = false ;
2015-03-19 22:48:36 +08:00
ImGui : : BeginGroup ( ) ;
2014-08-19 19:09:13 +08:00
ImGui : : PushID ( label ) ;
2015-04-03 18:49:02 +08:00
const bool hsv = ( edit_mode = = 1 ) ;
2014-08-19 19:09:13 +08:00
switch ( edit_mode )
{
case ImGuiColorEditMode_RGB :
case ImGuiColorEditMode_HSV :
{
2015-04-03 18:49:02 +08:00
// RGB/HSV 0..255 Sliders
2014-08-19 19:09:13 +08:00
const float w_items_all = w_full - ( square_sz + style . ItemInnerSpacing . x ) ;
2014-12-30 19:41:46 +08:00
const float w_item_one = ImMax ( 1.0f , ( float ) ( int ) ( ( w_items_all - ( style . FramePadding . x * 2.0f + style . ItemInnerSpacing . x ) * ( components - 1 ) ) / ( float ) components ) ) ;
const float w_item_last = ImMax ( 1.0f , ( float ) ( int ) ( w_items_all - ( w_item_one + style . FramePadding . x * 2.0f + style . ItemInnerSpacing . x ) * ( components - 1 ) ) ) ;
2014-08-19 19:09:13 +08:00
2015-04-03 18:25:44 +08:00
const bool hide_prefix = ( w_item_one < = CalcTextSize ( " M:999 " ) . x ) ;
2015-04-03 18:49:02 +08:00
const char * ids [ 4 ] = { " ##X " , " ##Y " , " ##Z " , " ##W " } ;
const char * fmt_table [ 3 ] [ 4 ] =
2014-08-19 19:09:13 +08:00
{
2015-04-03 18:49:02 +08:00
{ " %3.0f " , " %3.0f " , " %3.0f " , " %3.0f " } ,
{ " R:%3.0f " , " G:%3.0f " , " B:%3.0f " , " A:%3.0f " } ,
{ " H:%3.0f " , " S:%3.0f " , " V:%3.0f " , " A:%3.0f " }
} ;
const char * * fmt = hide_prefix ? fmt_table [ 0 ] : hsv ? fmt_table [ 2 ] : fmt_table [ 1 ] ;
2015-04-03 18:25:44 +08:00
2014-08-19 19:09:13 +08:00
ImGui : : PushItemWidth ( w_item_one ) ;
2015-04-03 18:49:02 +08:00
for ( int n = 0 ; n < components ; n + + )
2014-08-19 19:09:13 +08:00
{
2015-04-03 18:49:02 +08:00
if ( n > 0 )
2015-07-11 09:36:34 +08:00
ImGui : : SameLine ( 0 , style . ItemInnerSpacing . x ) ;
2015-04-03 18:49:02 +08:00
if ( n + 1 = = components )
ImGui : : PushItemWidth ( w_item_last ) ;
2015-04-13 02:15:55 +08:00
value_changed | = ImGui : : DragInt ( ids [ n ] , & i [ n ] , 1.0f , 0 , 255 , fmt [ n ] ) ;
2014-08-19 19:09:13 +08:00
}
ImGui : : PopItemWidth ( ) ;
ImGui : : PopItemWidth ( ) ;
}
break ;
case ImGuiColorEditMode_HEX :
{
2015-04-03 18:49:02 +08:00
// RGB Hexadecimal Input
2014-08-19 19:09:13 +08:00
const float w_slider_all = w_full - square_sz ;
char buf [ 64 ] ;
if ( alpha )
2015-04-03 18:49:02 +08:00
ImFormatString ( buf , IM_ARRAYSIZE ( buf ) , " #%02X%02X%02X%02X " , i [ 0 ] , i [ 1 ] , i [ 2 ] , i [ 3 ] ) ;
2014-08-19 19:09:13 +08:00
else
2015-04-03 18:49:02 +08:00
ImFormatString ( buf , IM_ARRAYSIZE ( buf ) , " #%02X%02X%02X " , i [ 0 ] , i [ 1 ] , i [ 2 ] ) ;
2014-12-30 23:38:28 +08:00
ImGui : : PushItemWidth ( w_slider_all - style . ItemInnerSpacing . x ) ;
2015-04-03 18:49:02 +08:00
value_changed | = ImGui : : InputText ( " ##Text " , buf , IM_ARRAYSIZE ( buf ) , ImGuiInputTextFlags_CharsHexadecimal | ImGuiInputTextFlags_CharsUppercase ) ;
2014-08-19 19:09:13 +08:00
ImGui : : PopItemWidth ( ) ;
char * p = buf ;
2015-03-07 01:51:25 +08:00
while ( * p = = ' # ' | | ImCharIsSpace ( * p ) )
2014-08-19 19:09:13 +08:00
p + + ;
2014-10-27 02:57:19 +08:00
2014-10-27 04:07:15 +08:00
// Treat at unsigned (%X is unsigned)
2015-04-03 18:49:02 +08:00
i [ 0 ] = i [ 1 ] = i [ 2 ] = i [ 3 ] = 0 ;
2014-08-19 19:09:13 +08:00
if ( alpha )
2015-04-03 18:49:02 +08:00
sscanf ( p , " %02X%02X%02X%02X " , ( unsigned int * ) & i [ 0 ] , ( unsigned int * ) & i [ 1 ] , ( unsigned int * ) & i [ 2 ] , ( unsigned int * ) & i [ 3 ] ) ;
2014-08-19 19:09:13 +08:00
else
2015-04-03 18:49:02 +08:00
sscanf ( p , " %02X%02X%02X " , ( unsigned int * ) & i [ 0 ] , ( unsigned int * ) & i [ 1 ] , ( unsigned int * ) & i [ 2 ] ) ;
2014-08-19 19:09:13 +08:00
}
break ;
}
2015-07-11 09:36:34 +08:00
ImGui : : SameLine ( 0 , style . ItemInnerSpacing . x ) ;
2015-04-03 18:49:02 +08:00
const ImVec4 col_display ( col [ 0 ] , col [ 1 ] , col [ 2 ] , 1.0f ) ;
2015-01-15 17:49:12 +08:00
if ( ImGui : : ColorButton ( col_display ) )
2015-04-03 18:49:02 +08:00
g . ColorEditModeStorage . SetInt ( id , ( edit_mode + 1 ) % 3 ) ; // Don't set local copy of 'edit_mode' right away!
2014-08-19 19:09:13 +08:00
2015-01-15 17:49:12 +08:00
if ( window - > DC . ColorEditMode = = ImGuiColorEditMode_UserSelectShowButton )
2014-08-19 19:09:13 +08:00
{
2015-07-11 09:36:34 +08:00
ImGui : : SameLine ( 0 , style . ItemInnerSpacing . x ) ;
2014-08-19 19:09:13 +08:00
const char * button_titles [ 3 ] = { " RGB " , " HSV " , " HEX " } ;
2015-05-27 16:28:44 +08:00
if ( ButtonEx ( button_titles [ edit_mode ] , ImVec2 ( 0 , 0 ) , ImGuiButtonFlags_DontClosePopups ) )
2015-04-03 18:49:02 +08:00
g . ColorEditModeStorage . SetInt ( id , ( edit_mode + 1 ) % 3 ) ; // Don't set local copy of 'edit_mode' right away!
2015-01-18 01:19:07 +08:00
ImGui : : SameLine ( ) ;
}
else
{
2015-07-11 09:36:34 +08:00
ImGui : : SameLine ( 0 , style . ItemInnerSpacing . x ) ;
2014-08-19 19:09:13 +08:00
}
ImGui : : TextUnformatted ( label , FindTextDisplayEnd ( label ) ) ;
// Convert back
2015-04-03 18:49:02 +08:00
for ( int n = 0 ; n < 4 ; n + + )
f [ n ] = i [ n ] / 255.0f ;
2014-08-19 19:09:13 +08:00
if ( edit_mode = = 1 )
2015-04-03 18:49:02 +08:00
ImGui : : ColorConvertHSVtoRGB ( f [ 0 ] , f [ 1 ] , f [ 2 ] , f [ 0 ] , f [ 1 ] , f [ 2 ] ) ;
2014-08-19 19:09:13 +08:00
if ( value_changed )
{
2015-04-03 18:49:02 +08:00
col [ 0 ] = f [ 0 ] ;
col [ 1 ] = f [ 1 ] ;
col [ 2 ] = f [ 2 ] ;
2014-08-19 19:09:13 +08:00
if ( alpha )
2015-04-03 18:49:02 +08:00
col [ 3 ] = f [ 3 ] ;
2014-08-19 19:09:13 +08:00
}
ImGui : : PopID ( ) ;
2015-03-19 22:48:36 +08:00
ImGui : : EndGroup ( ) ;
2014-08-19 19:09:13 +08:00
return value_changed ;
2014-08-11 04:58:11 +08:00
}
2014-11-10 09:29:01 +08:00
void ImGui : : ColorEditMode ( ImGuiColorEditMode mode )
2014-08-11 04:58:11 +08:00
{
2014-08-19 19:09:13 +08:00
ImGuiWindow * window = GetCurrentWindow ( ) ;
window - > DC . ColorEditMode = mode ;
2014-08-11 04:58:11 +08:00
}
2014-11-24 18:02:39 +08:00
// Horizontal separating line.
2014-11-10 09:29:01 +08:00
void ImGui : : Separator ( )
2014-08-11 04:58:11 +08:00
{
2014-08-19 19:09:13 +08:00
ImGuiWindow * window = GetCurrentWindow ( ) ;
if ( window - > SkipItems )
return ;
2014-08-11 04:58:11 +08:00
2014-08-19 19:09:13 +08:00
if ( window - > DC . ColumnsCount > 1 )
2014-11-10 09:29:01 +08:00
PopClipRect ( ) ;
2014-08-11 04:58:11 +08:00
2015-04-28 04:19:14 +08:00
float x1 = window - > Pos . x ;
float x2 = window - > Pos . x + window - > Size . x ;
if ( ! window - > DC . GroupStack . empty ( ) )
x1 + = window - > DC . ColumnsStartX ;
const ImRect bb ( ImVec2 ( x1 , window - > DC . CursorPos . y ) , ImVec2 ( x2 , window - > DC . CursorPos . y ) ) ;
2014-08-19 19:09:13 +08:00
ItemSize ( ImVec2 ( 0.0f , bb . GetSize ( ) . y ) ) ; // NB: we don't provide our width so that it doesn't get feed back into AutoFit
2015-01-23 06:54:05 +08:00
if ( ! ItemAdd ( bb , NULL ) )
2014-08-19 19:09:13 +08:00
{
if ( window - > DC . ColumnsCount > 1 )
2014-11-10 09:29:01 +08:00
PushColumnClipRect ( ) ;
2014-08-19 19:09:13 +08:00
return ;
}
2014-08-11 04:58:11 +08:00
2015-07-06 06:24:55 +08:00
window - > DrawList - > AddLine ( bb . Min , bb . Max , window - > Color ( ImGuiCol_Border ) ) ;
2014-08-11 04:58:11 +08:00
2015-03-13 20:44:04 +08:00
ImGuiState & g = * GImGui ;
if ( g . LogEnabled )
2015-06-30 00:39:17 +08:00
ImGui : : LogText ( IM_NEWLINE " -------------------------------- " ) ;
2015-03-13 20:44:04 +08:00
2014-08-19 19:09:13 +08:00
if ( window - > DC . ColumnsCount > 1 )
2015-02-22 22:43:52 +08:00
{
2014-11-10 09:29:01 +08:00
PushColumnClipRect ( ) ;
2015-02-22 22:43:52 +08:00
window - > DC . ColumnsCellMinY = window - > DC . CursorPos . y ;
}
2014-08-11 04:58:11 +08:00
}
2014-11-10 09:29:01 +08:00
void ImGui : : Spacing ( )
2014-08-11 04:58:11 +08:00
{
2014-08-19 19:09:13 +08:00
ImGuiWindow * window = GetCurrentWindow ( ) ;
if ( window - > SkipItems )
return ;
ItemSize ( ImVec2 ( 0 , 0 ) ) ;
2014-08-11 04:58:11 +08:00
}
2015-05-25 00:30:59 +08:00
void ImGui : : Dummy ( const ImVec2 & size )
{
ImGuiWindow * window = GetCurrentWindow ( ) ;
if ( window - > SkipItems )
return ;
ItemSize ( size ) ;
}
2015-05-17 19:10:57 +08:00
// Advance cursor given item size for layout.
2015-08-02 11:57:24 +08:00
void ImGui : : ItemSize ( const ImVec2 & size , float text_offset_y )
2014-08-11 04:58:11 +08:00
{
2014-08-19 19:09:13 +08:00
ImGuiWindow * window = GetCurrentWindow ( ) ;
if ( window - > SkipItems )
return ;
2014-08-11 04:58:11 +08:00
2014-08-19 19:09:13 +08:00
// Always align ourselves on pixel boundaries
2015-07-22 10:13:12 +08:00
ImGuiState & g = * GImGui ;
2015-03-18 01:17:00 +08:00
const float line_height = ImMax ( window - > DC . CurrentLineHeight , size . y ) ;
2015-03-18 04:17:53 +08:00
const float text_base_offset = ImMax ( window - > DC . CurrentLineTextBaseOffset , text_offset_y ) ;
2014-08-19 19:09:13 +08:00
window - > DC . CursorPosPrevLine = ImVec2 ( window - > DC . CursorPos . x + size . x , window - > DC . CursorPos . y ) ;
2014-11-20 19:31:55 +08:00
window - > DC . CursorPos = ImVec2 ( ( float ) ( int ) ( window - > Pos . x + window - > DC . ColumnsStartX + window - > DC . ColumnsOffsetX ) , ( float ) ( int ) ( window - > DC . CursorPos . y + line_height + g . Style . ItemSpacing . y ) ) ;
2015-03-18 06:03:02 +08:00
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 ) ;
2014-08-11 04:58:11 +08:00
2015-03-18 06:27:00 +08:00
//window->DrawList->AddCircle(window->DC.CursorMaxPos, 3.0f, 0xFF0000FF, 4); // Debug
2014-08-11 04:58:11 +08:00
2014-08-19 19:09:13 +08:00
window - > DC . PrevLineHeight = line_height ;
2015-03-18 04:17:53 +08:00
window - > DC . PrevLineTextBaseOffset = text_base_offset ;
window - > DC . CurrentLineHeight = window - > DC . CurrentLineTextBaseOffset = 0.0f ;
2014-08-11 04:58:11 +08:00
}
2015-08-02 11:57:24 +08:00
inline void ImGui : : ItemSize ( const ImRect & bb , float text_offset_y )
2014-08-11 04:58:11 +08:00
{
2015-03-18 04:17:53 +08:00
ItemSize ( bb . GetSize ( ) , text_offset_y ) ;
2014-08-11 04:58:11 +08:00
}
2015-08-02 11:57:24 +08:00
bool ImGui : : IsClippedEx ( const ImRect & bb , const ImGuiID * id , bool clip_even_when_logged )
2014-08-11 04:58:11 +08:00
{
2015-02-01 12:28:24 +08:00
ImGuiState & g = * GImGui ;
2014-08-19 19:09:13 +08:00
ImGuiWindow * window = GetCurrentWindow ( ) ;
2014-08-11 04:58:11 +08:00
2015-07-07 11:46:12 +08:00
if ( ! bb . Overlaps ( ImRect ( window - > ClipRect ) ) )
2015-04-14 04:52:38 +08:00
{
2015-04-18 19:11:59 +08:00
if ( ! id | | * id ! = GImGui - > ActiveId )
if ( clip_even_when_logged | | ! g . LogEnabled )
return true ;
2015-04-14 04:52:38 +08:00
}
2014-08-19 19:09:13 +08:00
return false ;
2014-08-11 04:58:11 +08:00
}
2015-05-31 16:12:07 +08:00
bool ImGui : : IsRectVisible ( const ImVec2 & size )
2014-08-11 04:58:11 +08:00
{
2014-08-19 19:09:13 +08:00
ImGuiWindow * window = GetCurrentWindow ( ) ;
2015-07-07 11:46:12 +08:00
ImRect r ( window - > ClipRect ) ;
2015-05-31 16:12:07 +08:00
return r . Overlaps ( ImRect ( window - > DC . CursorPos , window - > DC . CursorPos + size ) ) ;
2014-08-11 04:58:11 +08:00
}
2015-05-17 19:10:57 +08:00
// 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().
2015-08-02 11:57:24 +08:00
bool ImGui : : ItemAdd ( const ImRect & bb , const ImGuiID * id )
2014-08-11 04:58:11 +08:00
{
2014-08-19 19:09:13 +08:00
ImGuiWindow * window = GetCurrentWindow ( ) ;
2015-01-23 06:54:05 +08:00
window - > DC . LastItemID = id ? * id : 0 ;
2015-03-18 07:35:12 +08:00
window - > DC . LastItemRect = bb ;
2015-04-18 19:11:59 +08:00
if ( IsClippedEx ( bb , id , false ) )
2014-08-19 19:09:13 +08:00
{
2015-04-18 19:11:59 +08:00
window - > DC . LastItemHoveredAndUsable = window - > DC . LastItemHoveredRect = false ;
return false ;
2014-08-19 19:09:13 +08:00
}
2015-02-22 00:50:34 +08:00
// This is a sensible default, but widgets are free to override it after calling ItemAdd()
2015-03-18 20:10:26 +08:00
ImGuiState & g = * GImGui ;
2015-07-31 13:48:59 +08:00
if ( ImGui : : IsMouseHoveringRect ( bb . Min , bb . Max ) )
2015-03-18 20:10:26 +08:00
{
// Matching the behavior of IsHovered() but ignore 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()
2015-03-27 00:18:01 +08:00
window - > DC . LastItemHoveredRect = true ;
2015-03-27 01:17:42 +08:00
window - > DC . LastItemHoveredAndUsable = false ;
2015-04-07 21:03:17 +08:00
if ( g . HoveredRootWindow = = window - > RootWindow )
if ( g . ActiveId = = 0 | | ( id & & g . ActiveId = = * id ) | | g . ActiveIdIsFocusedOnly | | ( g . ActiveId = = window - > MoveID ) )
if ( IsWindowContentHoverable ( window ) )
window - > DC . LastItemHoveredAndUsable = true ;
2015-03-18 20:10:26 +08:00
}
else
{
2015-03-27 00:18:01 +08:00
window - > DC . LastItemHoveredAndUsable = window - > DC . LastItemHoveredRect = false ;
2015-03-18 20:10:26 +08:00
}
2015-01-23 06:54:05 +08:00
return true ;
2014-08-11 04:58:11 +08:00
}
2015-03-18 07:28:57 +08:00
void ImGui : : BeginGroup ( )
{
ImGuiWindow * window = GetCurrentWindow ( ) ;
2015-07-08 02:39:08 +08:00
window - > DC . GroupStack . resize ( window - > DC . GroupStack . Size + 1 ) ;
2015-03-18 07:28:57 +08:00
ImGuiGroupData & group_data = window - > DC . GroupStack . back ( ) ;
group_data . BackupCursorPos = window - > DC . CursorPos ;
group_data . BackupCursorMaxPos = window - > DC . CursorMaxPos ;
group_data . BackupColumnsStartX = window - > DC . ColumnsStartX ;
group_data . BackupCurrentLineHeight = window - > DC . CurrentLineHeight ;
group_data . BackupCurrentLineTextBaseOffset = window - > DC . CurrentLineTextBaseOffset ;
group_data . BackupLogLinePosY = window - > DC . LogLinePosY ;
2015-05-26 06:08:22 +08:00
group_data . AdvanceCursor = true ;
2015-03-18 07:28:57 +08:00
window - > DC . ColumnsStartX = window - > DC . CursorPos . x - window - > Pos . x ;
window - > DC . CursorMaxPos = window - > DC . CursorPos ;
window - > DC . CurrentLineHeight = 0.0f ;
window - > DC . LogLinePosY = window - > DC . CursorPos . y - 9999.0f ;
}
void ImGui : : EndGroup ( )
{
ImGuiWindow * window = GetCurrentWindow ( ) ;
ImGuiStyle & style = ImGui : : GetStyle ( ) ;
IM_ASSERT ( ! window - > DC . GroupStack . empty ( ) ) ;
ImGuiGroupData & group_data = window - > DC . GroupStack . back ( ) ;
2015-03-18 07:35:12 +08:00
ImRect group_bb ( group_data . BackupCursorPos , window - > DC . CursorMaxPos ) ;
2015-03-18 21:21:32 +08:00
group_bb . Max . y - = style . ItemSpacing . y ; // Cancel out last vertical spacing because we are adding one ourselves.
group_bb . Max = ImMax ( group_bb . Min , group_bb . Max ) ;
2015-03-18 07:28:57 +08:00
window - > DC . CursorPos = group_data . BackupCursorPos ;
window - > DC . CursorMaxPos = ImMax ( group_data . BackupCursorMaxPos , window - > DC . CursorMaxPos ) ;
window - > DC . CurrentLineHeight = group_data . BackupCurrentLineHeight ;
2015-05-27 05:13:07 +08:00
window - > DC . CurrentLineTextBaseOffset = group_data . BackupCurrentLineTextBaseOffset ;
2015-03-18 07:28:57 +08:00
window - > DC . ColumnsStartX = group_data . BackupColumnsStartX ;
window - > DC . LogLinePosY = window - > DC . CursorPos . y - 9999.0f ;
2015-05-26 06:08:22 +08:00
if ( group_data . AdvanceCursor )
{
2015-05-27 05:13:07 +08:00
window - > DC . CurrentLineTextBaseOffset = ImMax ( window - > DC . PrevLineTextBaseOffset , group_data . BackupCurrentLineTextBaseOffset ) ; // FIXME: Incorrect, we should grab the base offset from the *first line* of the group but it is hard to obtain now.
2015-05-26 06:08:22 +08:00
ItemSize ( group_bb . GetSize ( ) , group_data . BackupCurrentLineTextBaseOffset ) ;
ItemAdd ( group_bb , NULL ) ;
}
2015-03-18 07:28:57 +08:00
window - > DC . GroupStack . pop_back ( ) ;
//window->DrawList->AddRect(group_bb.Min, group_bb.Max, 0xFFFF00FF); // Debug
}
2014-08-11 04:58:11 +08:00
// Gets back to previous line and continue with horizontal layout
2015-07-11 09:36:34 +08:00
// pos_x == 0 : follow on previous item
// pos_x != 0 : align to specified column
2014-08-19 19:09:13 +08:00
// spacing_w < 0 : use default spacing if column_x==0, no spacing if column_x!=0
// spacing_w >= 0 : enforce spacing
2015-07-11 09:36:34 +08:00
void ImGui : : SameLine ( float pos_x , float spacing_w )
2014-08-11 04:58:11 +08:00
{
2014-08-19 19:09:13 +08:00
ImGuiWindow * window = GetCurrentWindow ( ) ;
if ( window - > SkipItems )
return ;
2015-07-22 10:13:12 +08:00
ImGuiState & g = * GImGui ;
2014-08-19 19:09:13 +08:00
float x , y ;
2015-07-11 09:36:34 +08:00
if ( pos_x ! = 0.0f )
2014-08-19 19:09:13 +08:00
{
2015-07-11 09:36:34 +08:00
if ( spacing_w < 0.0f ) spacing_w = 0.0f ;
x = window - > Pos . x + pos_x + spacing_w ;
2014-08-19 19:09:13 +08:00
y = window - > DC . CursorPosPrevLine . y ;
}
else
{
2015-07-11 09:36:34 +08:00
if ( spacing_w < 0.0f ) spacing_w = g . Style . ItemSpacing . x ;
x = window - > DC . CursorPosPrevLine . x + spacing_w ;
2014-08-19 19:09:13 +08:00
y = window - > DC . CursorPosPrevLine . y ;
}
window - > DC . CurrentLineHeight = window - > DC . PrevLineHeight ;
2015-03-18 04:17:53 +08:00
window - > DC . CurrentLineTextBaseOffset = window - > DC . PrevLineTextBaseOffset ;
2014-08-19 19:09:13 +08:00
window - > DC . CursorPos = ImVec2 ( x , y ) ;
2014-08-11 04:58:11 +08:00
}
2015-01-23 06:54:05 +08:00
void ImGui : : NextColumn ( )
{
ImGuiWindow * window = GetCurrentWindow ( ) ;
if ( window - > SkipItems )
return ;
2015-07-22 10:13:12 +08:00
ImGuiState & g = * GImGui ;
2015-01-23 06:54:05 +08:00
if ( window - > DC . ColumnsCount > 1 )
{
ImGui : : PopItemWidth ( ) ;
PopClipRect ( ) ;
window - > DC . ColumnsCellMaxY = ImMax ( window - > DC . ColumnsCellMaxY , window - > DC . CursorPos . y ) ;
if ( + + window - > DC . ColumnsCurrent < window - > DC . ColumnsCount )
{
window - > DC . ColumnsOffsetX = ImGui : : GetColumnOffset ( window - > DC . ColumnsCurrent ) - window - > DC . ColumnsStartX + g . Style . ItemSpacing . x ;
2015-07-08 08:19:01 +08:00
window - > DrawList - > ChannelsSetCurrent ( window - > DC . ColumnsCurrent ) ;
2015-01-23 06:54:05 +08:00
}
else
{
window - > DC . ColumnsCurrent = 0 ;
window - > DC . ColumnsOffsetX = 0.0f ;
window - > DC . ColumnsCellMinY = window - > DC . ColumnsCellMaxY ;
2015-07-08 08:19:01 +08:00
window - > DrawList - > ChannelsSetCurrent ( 0 ) ;
2015-01-23 06:54:05 +08:00
}
window - > DC . CursorPos . x = ( float ) ( int ) ( window - > Pos . x + window - > DC . ColumnsStartX + window - > DC . ColumnsOffsetX ) ;
window - > DC . CursorPos . y = window - > DC . ColumnsCellMinY ;
window - > DC . CurrentLineHeight = 0.0f ;
2015-03-18 04:17:53 +08:00
window - > DC . CurrentLineTextBaseOffset = 0.0f ;
2015-01-23 06:54:05 +08:00
PushColumnClipRect ( ) ;
2015-04-18 20:46:28 +08:00
ImGui : : PushItemWidth ( ImGui : : GetColumnWidth ( ) * 0.65f ) ; // FIXME
2015-01-23 06:54:05 +08:00
}
}
2015-03-07 20:18:01 +08:00
int ImGui : : GetColumnIndex ( )
{
ImGuiWindow * window = GetCurrentWindow ( ) ;
return window - > DC . ColumnsCurrent ;
}
int ImGui : : GetColumnsCount ( )
{
ImGuiWindow * window = GetCurrentWindow ( ) ;
return window - > DC . ColumnsCount ;
}
2015-04-18 20:46:28 +08:00
static float GetDraggedColumnOffset ( int column_index )
{
// Active (dragged) column always follow mouse. The reason we need this is that dragging a column to the right edge of an auto-resizing
// window creates a feedback loop because we store normalized positions/ So while dragging we enforce absolute positioning
ImGuiState & g = * GImGui ;
2015-08-02 11:57:24 +08:00
ImGuiWindow * window = ImGui : : GetCurrentWindow ( ) ;
2015-04-29 00:09:38 +08:00
IM_ASSERT ( column_index > 0 ) ; // We cannot drag column 0. If you get this assert you may have a conflict between the ID of your columns and another widgets.
2015-04-18 20:46:28 +08:00
IM_ASSERT ( g . ActiveId = = window - > DC . ColumnsSetID + ImGuiID ( column_index ) ) ;
2015-07-06 06:32:26 +08:00
float x = g . IO . MousePos . x + g . ActiveClickDeltaToCenter . x - window - > Pos . x ;
2015-04-18 20:46:28 +08:00
x = ImClamp ( x , ImGui : : GetColumnOffset ( column_index - 1 ) + g . Style . ColumnsMinSpacing , ImGui : : GetColumnOffset ( column_index + 1 ) - g . Style . ColumnsMinSpacing ) ;
2015-07-06 06:32:26 +08:00
return ( float ) ( int ) x ;
2015-04-18 20:46:28 +08:00
}
2014-11-10 09:29:01 +08:00
float ImGui : : GetColumnOffset ( int column_index )
2014-08-11 04:58:11 +08:00
{
2015-02-01 12:28:24 +08:00
ImGuiState & g = * GImGui ;
2014-08-19 19:09:13 +08:00
ImGuiWindow * window = GetCurrentWindow ( ) ;
if ( column_index < 0 )
2014-11-20 19:31:55 +08:00
column_index = window - > DC . ColumnsCurrent ;
2014-08-11 04:58:11 +08:00
2015-04-18 20:46:28 +08:00
if ( g . ActiveId )
{
const ImGuiID column_id = window - > DC . ColumnsSetID + ImGuiID ( column_index ) ;
if ( g . ActiveId = = column_id )
return GetDraggedColumnOffset ( column_index ) ;
}
2015-02-11 19:50:24 +08:00
// Read from cache
2015-07-08 02:39:08 +08:00
IM_ASSERT ( column_index < window - > DC . ColumnsOffsetsT . Size ) ;
2015-02-11 19:50:24 +08:00
const float t = window - > DC . ColumnsOffsetsT [ column_index ] ;
2015-02-23 00:27:34 +08:00
const float min_x = window - > DC . ColumnsStartX ;
2015-07-31 02:32:20 +08:00
const float max_x = window - > Size . x - ( ( window - > Flags & ImGuiWindowFlags_NoScrollbar ) ? 0 : g . Style . ScrollbarWidth ) ; // - window->WindowPadding().x;
2015-07-06 06:32:26 +08:00
const float x = min_x + t * ( max_x - min_x ) ;
return ( float ) ( int ) x ;
2014-08-11 04:58:11 +08:00
}
2014-11-10 09:29:01 +08:00
void ImGui : : SetColumnOffset ( int column_index , float offset )
2014-08-11 04:58:11 +08:00
{
2015-02-01 12:28:24 +08:00
ImGuiState & g = * GImGui ;
2014-08-19 19:09:13 +08:00
ImGuiWindow * window = GetCurrentWindow ( ) ;
if ( column_index < 0 )
2014-11-20 19:31:55 +08:00
column_index = window - > DC . ColumnsCurrent ;
2014-08-11 04:58:11 +08:00
2015-07-08 02:39:08 +08:00
IM_ASSERT ( column_index < window - > DC . ColumnsOffsetsT . Size ) ;
2014-08-20 17:40:31 +08:00
const ImGuiID column_id = window - > DC . ColumnsSetID + ImGuiID ( column_index ) ;
2015-02-23 00:27:34 +08:00
const float min_x = window - > DC . ColumnsStartX ;
2015-07-31 02:32:20 +08:00
const float max_x = window - > Size . x - ( ( window - > Flags & ImGuiWindowFlags_NoScrollbar ) ? 0 : g . Style . ScrollbarWidth ) ; // - window->WindowPadding().x;
2015-02-23 00:27:34 +08:00
const float t = ( offset - min_x ) / ( max_x - min_x ) ;
2015-02-25 07:29:15 +08:00
window - > DC . StateStorage - > SetFloat ( column_id , t ) ;
2015-02-11 19:50:24 +08:00
window - > DC . ColumnsOffsetsT [ column_index ] = t ;
2014-08-11 04:58:11 +08:00
}
2014-11-10 09:29:01 +08:00
float ImGui : : GetColumnWidth ( int column_index )
2014-08-11 04:58:11 +08:00
{
2014-08-19 19:09:13 +08:00
ImGuiWindow * window = GetCurrentWindow ( ) ;
if ( column_index < 0 )
2014-11-20 19:31:55 +08:00
column_index = window - > DC . ColumnsCurrent ;
2014-08-11 04:58:11 +08:00
2014-08-19 19:09:13 +08:00
const float w = GetColumnOffset ( column_index + 1 ) - GetColumnOffset ( column_index ) ;
return w ;
2014-08-11 04:58:11 +08:00
}
static void PushColumnClipRect ( int column_index )
{
2015-08-02 11:57:24 +08:00
ImGuiWindow * window = ImGui : : GetCurrentWindow ( ) ;
2014-08-19 19:09:13 +08:00
if ( column_index < 0 )
2014-11-20 19:31:55 +08:00
column_index = window - > DC . ColumnsCurrent ;
2014-08-11 04:58:11 +08:00
2014-08-19 19:09:13 +08:00
const float x1 = window - > Pos . x + ImGui : : GetColumnOffset ( column_index ) - 1 ;
const float x2 = window - > Pos . x + ImGui : : GetColumnOffset ( column_index + 1 ) - 1 ;
2014-11-10 09:29:01 +08:00
PushClipRect ( ImVec4 ( x1 , - FLT_MAX , x2 , + FLT_MAX ) ) ;
2014-08-11 04:58:11 +08:00
}
2014-11-10 09:29:01 +08:00
void ImGui : : Columns ( int columns_count , const char * id , bool border )
2014-08-11 04:58:11 +08:00
{
2015-02-01 12:28:24 +08:00
ImGuiState & g = * GImGui ;
2014-08-19 19:09:13 +08:00
ImGuiWindow * window = GetCurrentWindow ( ) ;
if ( window - > DC . ColumnsCount ! = 1 )
{
2014-11-20 19:31:55 +08:00
if ( window - > DC . ColumnsCurrent ! = 0 )
2014-11-10 09:29:01 +08:00
ItemSize ( ImVec2 ( 0 , 0 ) ) ; // Advance to column 0
2014-08-19 19:09:13 +08:00
ImGui : : PopItemWidth ( ) ;
2014-11-10 09:29:01 +08:00
PopClipRect ( ) ;
2015-07-08 08:19:01 +08:00
window - > DrawList - > ChannelsMerge ( window - > DC . ColumnsCount ) ;
2014-11-20 19:31:55 +08:00
window - > DC . ColumnsCellMaxY = ImMax ( window - > DC . ColumnsCellMaxY , window - > DC . CursorPos . y ) ;
window - > DC . CursorPos . y = window - > DC . ColumnsCellMaxY ;
2014-08-19 19:09:13 +08:00
}
2014-11-20 19:31:55 +08:00
// Draw columns borders and handle resize at the time of "closing" a columns set
2015-02-21 18:27:13 +08:00
if ( window - > DC . ColumnsCount ! = columns_count & & window - > DC . ColumnsCount ! = 1 & & window - > DC . ColumnsShowBorders & & ! window - > SkipItems )
2014-08-19 19:09:13 +08:00
{
2014-11-20 19:31:55 +08:00
const float y1 = window - > DC . ColumnsStartPos . y ;
2014-08-19 19:09:13 +08:00
const float y2 = window - > DC . CursorPos . y ;
for ( int i = 1 ; i < window - > DC . ColumnsCount ; i + + )
{
float x = window - > Pos . x + GetColumnOffset ( i ) ;
2014-08-20 17:40:31 +08:00
const ImGuiID column_id = window - > DC . ColumnsSetID + ImGuiID ( i ) ;
2015-03-18 07:37:17 +08:00
const ImRect column_rect ( ImVec2 ( x - 4 , y1 ) , ImVec2 ( x + 4 , y2 ) ) ;
2015-04-18 19:11:59 +08:00
if ( IsClippedEx ( column_rect , & column_id , false ) )
2014-08-19 19:09:13 +08:00
continue ;
bool hovered , held ;
2015-03-18 07:37:17 +08:00
ButtonBehavior ( column_rect , column_id , & hovered , & held , true ) ;
2015-03-22 01:48:03 +08:00
if ( hovered | | held )
g . MouseCursor = ImGuiMouseCursor_ResizeEW ;
2014-08-19 19:09:13 +08:00
2014-09-02 21:35:06 +08:00
// Draw before resize so our items positioning are in sync with the line being drawn
2014-08-19 19:09:13 +08:00
const ImU32 col = window - > Color ( held ? ImGuiCol_ColumnActive : hovered ? ImGuiCol_ColumnHovered : ImGuiCol_Column ) ;
const float xi = ( float ) ( int ) x ;
2015-07-06 06:24:55 +08:00
window - > DrawList - > AddLine ( ImVec2 ( xi , y1 + 1.0f ) , ImVec2 ( xi , y2 ) , col ) ;
2014-08-19 19:09:13 +08:00
if ( held )
{
2015-04-18 20:46:28 +08:00
if ( g . ActiveIdIsJustActivated )
g . ActiveClickDeltaToCenter . x = x - g . IO . MousePos . x ;
x = GetDraggedColumnOffset ( i ) ;
2014-08-19 19:09:13 +08:00
SetColumnOffset ( i , x ) ;
}
}
}
2014-11-20 19:31:55 +08:00
// Set state for first column
2015-04-29 00:09:38 +08:00
ImGui : : PushID ( 0x11223344 ) ; // Differentiate column ID with an arbitrary/random prefix for cases where users name their columns set the same as another non-scope widget
2014-08-19 19:09:13 +08:00
window - > DC . ColumnsSetID = window - > GetID ( id ? id : " " ) ;
2015-04-29 00:09:38 +08:00
ImGui : : PopID ( ) ;
2014-11-20 19:31:55 +08:00
window - > DC . ColumnsCurrent = 0 ;
2014-08-19 19:09:13 +08:00
window - > DC . ColumnsCount = columns_count ;
window - > DC . ColumnsShowBorders = border ;
2014-11-20 19:31:55 +08:00
window - > DC . ColumnsStartPos = window - > DC . CursorPos ;
window - > DC . ColumnsCellMinY = window - > DC . ColumnsCellMaxY = window - > DC . CursorPos . y ;
window - > DC . ColumnsOffsetX = 0.0f ;
window - > DC . CursorPos . x = ( float ) ( int ) ( window - > Pos . x + window - > DC . ColumnsStartX + window - > DC . ColumnsOffsetX ) ;
2014-08-19 19:09:13 +08:00
if ( window - > DC . ColumnsCount ! = 1 )
{
2015-02-11 19:50:24 +08:00
// Cache column offsets
2015-07-08 02:09:25 +08:00
window - > DC . ColumnsOffsetsT . resize ( columns_count + 1 ) ;
2015-02-11 19:50:24 +08:00
for ( int column_index = 0 ; column_index < columns_count + 1 ; column_index + + )
{
const ImGuiID column_id = window - > DC . ColumnsSetID + ImGuiID ( column_index ) ;
2015-07-22 07:00:28 +08:00
KeepAliveID ( column_id ) ;
2015-02-11 19:50:24 +08:00
const float default_t = column_index / ( float ) window - > DC . ColumnsCount ;
2015-02-25 07:29:15 +08:00
const float t = window - > DC . StateStorage - > GetFloat ( column_id , default_t ) ; // Cheaply store our floating point value inside the integer (could store an union into the map?)
2015-02-11 19:50:24 +08:00
window - > DC . ColumnsOffsetsT [ column_index ] = t ;
}
2015-07-08 08:19:01 +08:00
window - > DrawList - > ChannelsSplit ( window - > DC . ColumnsCount ) ;
2014-11-10 09:29:01 +08:00
PushColumnClipRect ( ) ;
2014-08-19 19:09:13 +08:00
ImGui : : PushItemWidth ( ImGui : : GetColumnWidth ( ) * 0.65f ) ;
}
2015-02-11 19:50:24 +08:00
else
{
window - > DC . ColumnsOffsetsT . resize ( 2 ) ;
window - > DC . ColumnsOffsetsT [ 0 ] = 0.0f ;
window - > DC . ColumnsOffsetsT [ 1 ] = 1.0f ;
}
2014-08-11 04:58:11 +08:00
}
2015-05-07 17:48:05 +08:00
void ImGui : : Indent ( )
2014-08-11 04:58:11 +08:00
{
2015-02-01 12:28:24 +08:00
ImGuiState & g = * GImGui ;
2014-08-19 19:09:13 +08:00
ImGuiWindow * window = GetCurrentWindow ( ) ;
2015-03-15 19:10:48 +08:00
window - > DC . ColumnsStartX + = g . Style . IndentSpacing ;
2014-11-20 19:31:55 +08:00
window - > DC . CursorPos . x = window - > Pos . x + window - > DC . ColumnsStartX + window - > DC . ColumnsOffsetX ;
2015-03-15 19:19:10 +08:00
}
2015-05-07 17:48:05 +08:00
void ImGui : : Unindent ( )
2014-08-11 04:58:11 +08:00
{
2015-02-01 12:28:24 +08:00
ImGuiState & g = * GImGui ;
2014-08-19 19:09:13 +08:00
ImGuiWindow * window = GetCurrentWindow ( ) ;
2015-03-15 19:19:10 +08:00
window - > DC . ColumnsStartX - = g . Style . IndentSpacing ;
2014-11-20 19:31:55 +08:00
window - > DC . CursorPos . x = window - > Pos . x + window - > DC . ColumnsStartX + window - > DC . ColumnsOffsetX ;
2015-03-15 19:19:10 +08:00
}
void ImGui : : TreePush ( const char * str_id )
{
ImGuiWindow * window = GetCurrentWindow ( ) ;
ImGui : : Indent ( ) ;
2014-08-19 19:09:13 +08:00
window - > DC . TreeDepth + + ;
PushID ( str_id ? str_id : " #TreePush " ) ;
2014-08-11 04:58:11 +08:00
}
2014-11-10 09:29:01 +08:00
void ImGui : : TreePush ( const void * ptr_id )
2014-08-11 04:58:11 +08:00
{
2014-08-19 19:09:13 +08:00
ImGuiWindow * window = GetCurrentWindow ( ) ;
2015-03-15 19:19:10 +08:00
ImGui : : Indent ( ) ;
2014-08-19 19:09:13 +08:00
window - > DC . TreeDepth + + ;
PushID ( ptr_id ? ptr_id : ( const void * ) " #TreePush " ) ;
2014-08-11 04:58:11 +08:00
}
2014-11-10 09:29:01 +08:00
void ImGui : : TreePop ( )
2014-08-11 04:58:11 +08:00
{
2014-08-19 19:09:13 +08:00
ImGuiWindow * window = GetCurrentWindow ( ) ;
2015-03-15 19:19:10 +08:00
ImGui : : Unindent ( ) ;
2014-08-19 19:09:13 +08:00
window - > DC . TreeDepth - - ;
PopID ( ) ;
2014-08-11 04:58:11 +08:00
}
2014-11-12 07:36:34 +08:00
void ImGui : : Value ( const char * prefix , bool b )
2014-08-11 04:58:11 +08:00
{
2014-08-19 19:09:13 +08:00
ImGui : : Text ( " %s: %s " , prefix , ( b ? " true " : " false " ) ) ;
2014-08-11 04:58:11 +08:00
}
2014-11-12 07:36:34 +08:00
void ImGui : : Value ( const char * prefix , int v )
2014-08-11 04:58:11 +08:00
{
2014-08-19 19:09:13 +08:00
ImGui : : Text ( " %s: %d " , prefix , v ) ;
2014-08-11 04:58:11 +08:00
}
2014-11-12 07:36:34 +08:00
void ImGui : : Value ( const char * prefix , unsigned int v )
2014-08-11 04:58:11 +08:00
{
2014-08-19 19:09:13 +08:00
ImGui : : Text ( " %s: %d " , prefix , v ) ;
2014-08-11 04:58:11 +08:00
}
2014-11-12 07:36:34 +08:00
void ImGui : : Value ( const char * prefix , float v , const char * float_format )
2014-08-11 04:58:11 +08:00
{
2014-08-19 19:09:13 +08:00
if ( float_format )
{
char fmt [ 64 ] ;
2015-03-22 03:27:29 +08:00
ImFormatString ( fmt , IM_ARRAYSIZE ( fmt ) , " %%s: %s " , float_format ) ;
2014-08-19 19:09:13 +08:00
ImGui : : Text ( fmt , prefix , v ) ;
}
else
{
ImGui : : Text ( " %s: %.3f " , prefix , v ) ;
}
2014-08-11 04:58:11 +08:00
}
2014-11-12 07:36:34 +08:00
void ImGui : : Color ( const char * prefix , const ImVec4 & v )
2014-08-11 04:58:11 +08:00
{
2014-08-19 19:09:13 +08:00
ImGui : : Text ( " %s: (%.2f,%.2f,%.2f,%.2f) " , prefix , v . x , v . y , v . z , v . w ) ;
ImGui : : SameLine ( ) ;
ImGui : : ColorButton ( v , true ) ;
2014-08-11 04:58:11 +08:00
}
2014-11-12 07:36:34 +08:00
void ImGui : : Color ( const char * prefix , unsigned int v )
2014-08-11 04:58:11 +08:00
{
2014-08-19 19:09:13 +08:00
ImGui : : Text ( " %s: %08X " , prefix , v ) ;
ImGui : : SameLine ( ) ;
2014-08-11 04:58:11 +08:00
2014-08-19 19:09:13 +08:00
ImVec4 col ;
col . x = ( float ) ( ( v > > 0 ) & 0xFF ) / 255.0f ;
col . y = ( float ) ( ( v > > 8 ) & 0xFF ) / 255.0f ;
col . z = ( float ) ( ( v > > 16 ) & 0xFF ) / 255.0f ;
col . w = ( float ) ( ( v > > 24 ) & 0xFF ) / 255.0f ;
ImGui : : ColorButton ( col , true ) ;
2014-08-11 04:58:11 +08:00
}
//-----------------------------------------------------------------------------
2015-07-22 07:00:28 +08:00
// PLATFORM DEPENDANT HELPERS
2014-08-11 04:58:11 +08:00
//-----------------------------------------------------------------------------
2015-07-22 07:00:28 +08:00
# if defined(_MSC_VER) && !defined(IMGUI_DISABLE_WIN32_DEFAULT_CLIPBOARD_FUNCS)
2015-01-12 00:30:39 +08:00
2015-07-22 07:00:28 +08:00
# ifndef _WINDOWS_
# define WIN32_LEAN_AND_MEAN
# include <windows.h>
# endif
# pragma comment(lib, "user32")
2014-08-11 04:58:11 +08:00
2015-07-22 07:00:28 +08:00
// Win32 API clipboard implementation
static const char * GetClipboardTextFn_DefaultImpl ( )
2015-03-21 08:41:34 +08:00
{
2015-07-22 07:00:28 +08:00
static char * buf_local = NULL ;
if ( buf_local )
2015-07-08 08:19:01 +08:00
{
2015-07-22 07:00:28 +08:00
ImGui : : MemFree ( buf_local ) ;
buf_local = NULL ;
2015-07-08 08:19:01 +08:00
}
2015-07-22 07:00:28 +08:00
if ( ! OpenClipboard ( NULL ) )
return NULL ;
HANDLE wbuf_handle = GetClipboardData ( CF_UNICODETEXT ) ;
if ( wbuf_handle = = NULL )
return NULL ;
if ( ImWchar * wbuf_global = ( ImWchar * ) GlobalLock ( wbuf_handle ) )
2014-08-19 19:09:13 +08:00
{
2015-07-22 07:00:28 +08:00
int buf_len = ImTextCountUtf8BytesFromStr ( wbuf_global , NULL ) + 1 ;
buf_local = ( char * ) ImGui : : MemAlloc ( buf_len * sizeof ( char ) ) ;
ImTextStrToUtf8 ( buf_local , buf_len , wbuf_global , NULL ) ;
2015-01-31 23:24:55 +08:00
}
2015-07-22 07:00:28 +08:00
GlobalUnlock ( wbuf_handle ) ;
CloseClipboard ( ) ;
return buf_local ;
2015-07-08 08:19:01 +08:00
}
2015-07-22 07:00:28 +08:00
// Win32 API clipboard implementation
static void SetClipboardTextFn_DefaultImpl ( const char * text )
2015-07-08 08:19:01 +08:00
{
2015-07-22 07:00:28 +08:00
if ( ! OpenClipboard ( NULL ) )
2015-07-08 08:19:01 +08:00
return ;
2015-07-22 07:00:28 +08:00
const int wbuf_length = ImTextCountCharsFromUtf8 ( text , NULL ) + 1 ;
HGLOBAL wbuf_handle = GlobalAlloc ( GMEM_MOVEABLE , ( SIZE_T ) wbuf_length * sizeof ( ImWchar ) ) ;
if ( wbuf_handle = = NULL )
2015-01-23 08:38:04 +08:00
return ;
2015-07-22 07:00:28 +08:00
ImWchar * wbuf_global = ( ImWchar * ) GlobalLock ( wbuf_handle ) ;
ImTextStrFromUtf8 ( wbuf_global , wbuf_length , text , NULL ) ;
GlobalUnlock ( wbuf_handle ) ;
EmptyClipboard ( ) ;
SetClipboardData ( CF_UNICODETEXT , wbuf_handle ) ;
CloseClipboard ( ) ;
2015-01-23 08:38:04 +08:00
}
2015-07-22 07:00:28 +08:00
# else
2014-12-31 00:49:39 +08:00
2015-07-22 07:00:28 +08:00
// Local ImGui-only clipboard implementation, if user hasn't defined better clipboard handlers
static const char * GetClipboardTextFn_DefaultImpl ( )
{
return GImGui - > PrivateClipboard ;
}
2014-11-24 07:35:26 +08:00
2015-07-22 07:00:28 +08:00
// Local ImGui-only clipboard implementation, if user hasn't defined better clipboard handlers
static void SetClipboardTextFn_DefaultImpl ( const char * text )
{
ImGuiState & g = * GImGui ;
if ( g . PrivateClipboard )
2014-11-24 07:35:26 +08:00
{
2015-07-22 07:00:28 +08:00
ImGui : : MemFree ( g . PrivateClipboard ) ;
g . PrivateClipboard = NULL ;
2014-12-31 00:49:39 +08:00
}
2015-07-22 07:00:28 +08:00
const char * text_end = text + strlen ( text ) ;
g . PrivateClipboard = ( char * ) ImGui : : MemAlloc ( ( size_t ) ( text_end - text ) + 1 ) ;
memcpy ( g . PrivateClipboard , text , ( size_t ) ( text_end - text ) ) ;
g . PrivateClipboard [ ( int ) ( text_end - text ) ] = 0 ;
}
2014-12-31 00:49:39 +08:00
2015-07-22 07:00:28 +08:00
# endif
2014-12-31 00:49:39 +08:00
2015-07-22 07:00:28 +08:00
# if defined(_MSC_VER) && !defined(IMGUI_DISABLE_WIN32_DEFAULT_IME_FUNCS)
2014-12-31 00:49:39 +08:00
2015-07-22 07:00:28 +08:00
# ifndef _WINDOWS_
# define WIN32_LEAN_AND_MEAN
# include <windows.h>
# endif
# include <Imm.h>
# pragma comment(lib, "imm32")
2014-12-31 00:49:39 +08:00
2015-07-22 07:00:28 +08:00
static void ImeSetInputScreenPosFn_DefaultImpl ( int x , int y )
{
// Notify OS Input Method Editor of text input position
if ( HWND hwnd = ( HWND ) GImGui - > IO . ImeWindowHandle )
if ( HIMC himc = ImmGetContext ( hwnd ) )
2014-12-31 00:49:39 +08:00
{
2015-07-22 07:00:28 +08:00
COMPOSITIONFORM cf ;
cf . ptCurrentPos . x = x ;
cf . ptCurrentPos . y = y ;
cf . dwStyle = CFS_FORCE_POSITION ;
ImmSetCompositionWindow ( himc , & cf ) ;
2014-12-31 00:49:39 +08:00
}
2015-07-22 07:00:28 +08:00
}
2014-12-31 00:49:39 +08:00
2015-07-22 07:00:28 +08:00
# else
2014-12-31 00:49:39 +08:00
2015-07-22 07:00:28 +08:00
static void ImeSetInputScreenPosFn_DefaultImpl ( int , int ) { }
2014-12-31 00:49:39 +08:00
2015-07-22 07:00:28 +08:00
# endif
2014-12-31 00:49:39 +08:00
2015-07-22 07:00:28 +08:00
//-----------------------------------------------------------------------------
// HELP
//-----------------------------------------------------------------------------
2014-12-31 00:49:39 +08:00
2015-07-22 07:00:28 +08:00
void ImGui : : ShowMetricsWindow ( bool * opened )
{
if ( ImGui : : Begin ( " ImGui Metrics " , opened ) )
2014-12-31 00:49:39 +08:00
{
2015-07-22 07:00:28 +08:00
ImGui : : Text ( " ImGui %s " , ImGui : : GetVersion ( ) ) ;
ImGui : : Text ( " Application average %.3f ms/frame (%.1f FPS) " , 1000.0f / ImGui : : GetIO ( ) . Framerate , ImGui : : GetIO ( ) . Framerate ) ;
ImGui : : Text ( " %d vertices, %d triangles " , ImGui : : GetIO ( ) . MetricsRenderVertices , ImGui : : GetIO ( ) . MetricsRenderIndices / 3 ) ;
ImGui : : Text ( " %d allocations " , ImGui : : GetIO ( ) . MetricsAllocs ) ;
static bool show_clip_rects = true ;
ImGui : : Checkbox ( " Show clipping rectangles when hovering ImDrawList " , & show_clip_rects ) ;
ImGui : : Separator ( ) ;
2014-11-24 07:35:26 +08:00
2015-07-22 07:00:28 +08:00
struct Funcs
2014-11-24 07:35:26 +08:00
{
2015-07-22 07:00:28 +08:00
static void NodeDrawList ( ImDrawList * draw_list , const char * label )
2014-11-24 07:35:26 +08:00
{
2015-07-22 07:00:28 +08:00
bool node_opened = ImGui : : TreeNode ( draw_list , " %s: '%s' %d vtx, %d indices, %d cmds " , label , draw_list - > _OwnerName ? draw_list - > _OwnerName : " " , draw_list - > VtxBuffer . Size , draw_list - > IdxBuffer . Size , draw_list - > CmdBuffer . Size ) ;
if ( draw_list = = ImGui : : GetWindowDrawList ( ) )
2014-11-24 07:35:26 +08:00
{
2015-07-22 07:00:28 +08:00
ImGui : : SameLine ( ) ;
ImGui : : TextColored ( ImColor ( 255 , 100 , 100 ) , " CURRENTLY APPENDING " ) ; // Can't display stats for active draw list! (we don't have the data double-buffered)
2014-11-24 07:35:26 +08:00
}
2015-07-22 07:00:28 +08:00
if ( ! node_opened )
return ;
2014-11-24 07:35:26 +08:00
2015-07-22 07:00:28 +08:00
int elem_offset = 0 ;
for ( const ImDrawCmd * pcmd = draw_list - > CmdBuffer . begin ( ) ; pcmd < draw_list - > CmdBuffer . end ( ) ; elem_offset + = pcmd - > ElemCount , pcmd + + )
2014-11-24 07:35:26 +08:00
{
2015-07-22 07:00:28 +08:00
if ( pcmd - > UserCallback )
ImGui : : BulletText ( " Callback %p, user_data %p " , pcmd - > UserCallback , pcmd - > UserCallbackData ) ;
else
2014-11-27 06:16:38 +08:00
{
2015-07-22 07:00:28 +08:00
ImGui : : BulletText ( " Draw %d indexed vtx, tex = %p, clip_rect = (%.0f,%.0f)..(%.0f,%.0f) " , pcmd - > ElemCount , pcmd - > TextureId , pcmd - > ClipRect . x , pcmd - > ClipRect . y , pcmd - > ClipRect . z , pcmd - > ClipRect . w ) ;
if ( show_clip_rects & & ImGui : : IsItemHovered ( ) )
{
ImRect clip_rect = pcmd - > ClipRect ;
ImRect vtxs_rect ;
for ( int i = elem_offset ; i < elem_offset + ( int ) pcmd - > ElemCount ; i + + )
vtxs_rect . Add ( draw_list - > VtxBuffer [ draw_list - > IdxBuffer [ i ] ] . pos ) ;
GImGui - > OverlayDrawList . PushClipRectFullScreen ( ) ;
clip_rect . Round ( ) ; GImGui - > OverlayDrawList . AddRect ( clip_rect . Min , clip_rect . Max , ImColor ( 255 , 255 , 0 ) ) ;
vtxs_rect . Round ( ) ; GImGui - > OverlayDrawList . AddRect ( vtxs_rect . Min , vtxs_rect . Max , ImColor ( 255 , 0 , 255 ) ) ;
GImGui - > OverlayDrawList . PopClipRect ( ) ;
}
2014-11-27 06:16:38 +08:00
}
2014-11-24 07:35:26 +08:00
}
2015-07-22 07:00:28 +08:00
ImGui : : TreePop ( ) ;
2014-11-24 07:35:26 +08:00
}
2014-12-31 00:49:39 +08:00
2015-07-22 07:00:28 +08:00
static void NodeWindows ( ImVector < ImGuiWindow * > & windows , const char * label )
{
if ( ! ImGui : : TreeNode ( label , " %s (%d) " , label , windows . Size ) )
return ;
for ( int i = 0 ; i < windows . Size ; i + + )
Funcs : : NodeWindow ( windows [ i ] , " Window " ) ;
ImGui : : TreePop ( ) ;
2014-12-31 00:49:39 +08:00
}
2014-11-07 16:22:38 +08:00
2015-07-22 07:00:28 +08:00
static void NodeWindow ( ImGuiWindow * window , const char * label )
2015-05-29 17:29:26 +08:00
{
2015-07-22 07:00:28 +08:00
if ( ! ImGui : : TreeNode ( window , " %s '%s', %d @ 0x%p " , label , window - > Name , window - > Active | | window - > WasActive , window ) )
return ;
NodeDrawList ( window - > DrawList , " DrawList " ) ;
if ( window - > RootWindow ! = window ) NodeWindow ( window - > RootWindow , " RootWindow " ) ;
if ( window - > DC . ChildWindows . Size > 0 ) NodeWindows ( window - > DC . ChildWindows , " ChildWindows " ) ;
ImGui : : BulletText ( " Storage: %d bytes " , window - > StateStorage . Data . Size * sizeof ( ImGuiStorage : : Pair ) ) ;
ImGui : : TreePop ( ) ;
2015-05-29 17:29:26 +08:00
}
2015-07-22 07:00:28 +08:00
} ;
2015-05-29 17:29:26 +08:00
2015-07-22 07:00:28 +08:00
ImGuiState & g = * GImGui ; // Access private state
g . DisableHideTextAfterDoubleHash + + ; // Not exposed (yet). Disable processing that hides text after '##' markers.
Funcs : : NodeWindows ( g . Windows , " Windows " ) ;
if ( ImGui : : TreeNode ( " DrawList " , " Active DrawLists (%d) " , g . RenderDrawLists [ 0 ] . Size ) )
2015-05-29 17:29:26 +08:00
{
2015-07-22 07:00:28 +08:00
for ( int i = 0 ; i < g . RenderDrawLists [ 0 ] . Size ; i + + )
Funcs : : NodeDrawList ( g . RenderDrawLists [ 0 ] [ i ] , " DrawList " ) ;
ImGui : : TreePop ( ) ;
2015-01-09 07:35:01 +08:00
}
2015-07-22 07:00:28 +08:00
if ( ImGui : : TreeNode ( " Popups " , " Opened Popups Stack (%d) " , g . OpenedPopupStack . Size ) )
{
for ( int i = 0 ; i < g . OpenedPopupStack . Size ; i + + )
{
ImGuiWindow * window = g . OpenedPopupStack [ i ] . Window ;
ImGui : : BulletText ( " PopupID: %08x, Window: '%s'%s%s " , g . OpenedPopupStack [ i ] . PopupID , window ? window - > Name : " NULL " , window & & ( window - > Flags & ImGuiWindowFlags_ChildWindow ) ? " ChildWindow " : " " , window & & ( window - > Flags & ImGuiWindowFlags_ChildMenu ) ? " ChildMenu " : " " ) ;
2015-01-09 07:35:01 +08:00
}
2015-07-22 07:00:28 +08:00
ImGui : : TreePop ( ) ;
2015-01-09 07:35:01 +08:00
}
2015-07-22 07:00:28 +08:00
g . DisableHideTextAfterDoubleHash - - ;
2015-01-09 07:35:01 +08:00
}
2015-07-22 07:00:28 +08:00
ImGui : : End ( ) ;
2015-01-09 07:35:01 +08:00
}
2014-08-11 04:58:11 +08:00
//-----------------------------------------------------------------------------
2014-09-10 18:22:01 +08:00
2014-12-06 07:09:43 +08:00
//---- Include imgui_user.inl at the end of imgui.cpp
//---- So you can include code that extends ImGui using its private data/functions.
2014-10-02 18:56:40 +08:00
# ifdef IMGUI_INCLUDE_IMGUI_USER_INL
# include "imgui_user.inl"
2014-09-10 18:22:01 +08:00
# endif
//-----------------------------------------------------------------------------