@ -30,11 +30,10 @@ namespace ImGuiNET
public unsafe SampleWindow ( )
{
_ nativeWindow = new NativeWindow ( 9 6 0 , 5 4 0 , "ImGui.NET" , GameWindowFlags . Default , OpenTK . Graphics . GraphicsMode . Default , DisplayDevice . Default ) ;
_ nativeWindow . WindowBorder = WindowBorder . Resizable ;
GraphicsContextFlags flags = GraphicsContextFlags . Default ;
_ graphicsContext = new GraphicsContext ( GraphicsMode . Default , _ nativeWindow . WindowInfo , 3 , 0 , flags ) ;
_ graphicsContext . MakeCurrent ( _ nativeWindow . WindowInfo ) ;
_ graphicsContext . LoadAll ( ) ; // wtf is this?
( ( IGraphicsContextInternal ) _ graphicsContext ) . LoadAll ( ) ; // wtf is this?
GL . ClearColor ( Color . Black ) ;
_ nativeWindow . Visible = true ;
@ -42,10 +41,9 @@ namespace ImGuiNET
_ nativeWindow . KeyUp + = OnKeyUp ;
_ nativeWindow . KeyPress + = OnKeyPress ;
IO * io = ImGuiNative . igGetIO ( ) ;
ImGuiNative . ImFontAtlas_AddFontDefault ( io - > FontAtlas ) ;
ImGui . LoadDefaultFont ( ) ;
SetOpenTKKeyMappings ( io ) ;
SetOpenTKKeyMappings ( ) ;
_ textInputBufferLength = 1 0 2 4 ;
_ textInputBuffer = Marshal . AllocHGlobal ( _ textInputBufferLength ) ;
@ -60,85 +58,94 @@ namespace ImGuiNET
private void OnKeyPress ( object sender , KeyPressEventArgs e )
{
ImGuiNative . ImGuiIO_ AddInputCharacter( e . KeyChar ) ;
ImGui . AddInputCharacter ( e . KeyChar ) ;
}
private static unsafe void SetOpenTKKeyMappings ( IO * io )
private static unsafe void SetOpenTKKeyMappings ( )
{
io - > KeyMap [ ( int ) GuiKey . Tab ] = ( int ) Key . Tab ;
io - > KeyMap [ ( int ) GuiKey . LeftArrow ] = ( int ) Key . Left ;
io - > KeyMap [ ( int ) GuiKey . RightArrow ] = ( int ) Key . Right ;
io - > KeyMap [ ( int ) GuiKey . UpArrow ] = ( int ) Key . Up ;
io - > KeyMap [ ( int ) GuiKey . DownArrow ] = ( int ) Key . Down ;
io - > KeyMap [ ( int ) GuiKey . PageUp ] = ( int ) Key . PageUp ;
io - > KeyMap [ ( int ) GuiKey . PageDown ] = ( int ) Key . PageDown ;
io - > KeyMap [ ( int ) GuiKey . Home ] = ( int ) Key . Home ;
io - > KeyMap [ ( int ) GuiKey . End ] = ( int ) Key . End ;
io - > KeyMap [ ( int ) GuiKey . Delete ] = ( int ) Key . Delete ;
io - > KeyMap [ ( int ) GuiKey . Backspace ] = ( int ) Key . BackSpace ;
io - > KeyMap [ ( int ) GuiKey . Enter ] = ( int ) Key . Enter ;
io - > KeyMap [ ( int ) GuiKey . Escape ] = ( int ) Key . Escape ;
io - > KeyMap [ ( int ) GuiKey . A ] = ( int ) Key . A ;
io - > KeyMap [ ( int ) GuiKey . C ] = ( int ) Key . C ;
io - > KeyMap [ ( int ) GuiKey . V ] = ( int ) Key . V ;
io - > KeyMap [ ( int ) GuiKey . X ] = ( int ) Key . X ;
io - > KeyMap [ ( int ) GuiKey . Y ] = ( int ) Key . Y ;
io - > KeyMap [ ( int ) GuiKey . Z ] = ( int ) Key . Z ;
IO io = ImGui . GetIO ( ) ;
io . KeyMap [ GuiKey . Tab ] = ( int ) Key . Tab ;
io . KeyMap [ GuiKey . LeftArrow ] = ( int ) Key . Left ;
io . KeyMap [ GuiKey . RightArrow ] = ( int ) Key . Right ;
io . KeyMap [ GuiKey . UpArrow ] = ( int ) Key . Up ;
io . KeyMap [ GuiKey . DownArrow ] = ( int ) Key . Down ;
io . KeyMap [ GuiKey . PageUp ] = ( int ) Key . PageUp ;
io . KeyMap [ GuiKey . PageDown ] = ( int ) Key . PageDown ;
io . KeyMap [ GuiKey . Home ] = ( int ) Key . Home ;
io . KeyMap [ GuiKey . End ] = ( int ) Key . End ;
io . KeyMap [ GuiKey . Delete ] = ( int ) Key . Delete ;
io . KeyMap [ GuiKey . Backspace ] = ( int ) Key . BackSpace ;
io . KeyMap [ GuiKey . Enter ] = ( int ) Key . Enter ;
io . KeyMap [ GuiKey . Escape ] = ( int ) Key . Escape ;
io . KeyMap [ GuiKey . A ] = ( int ) Key . A ;
io . KeyMap [ GuiKey . C ] = ( int ) Key . C ;
io . KeyMap [ GuiKey . V ] = ( int ) Key . V ;
io . KeyMap [ GuiKey . X ] = ( int ) Key . X ;
io . KeyMap [ GuiKey . Y ] = ( int ) Key . Y ;
io . KeyMap [ GuiKey . Z ] = ( int ) Key . Z ;
}
private unsafe void OnKeyDown ( object sender , KeyboardKeyEventArgs e )
{
var ptr = ImGuiNative . igGetIO ( ) ;
ptr - > KeysDown [ ( int ) e . Key ] = 1 ;
UpdateModifiers ( e , ptr ) ;
ImGui . GetIO ( ) . KeysDown [ ( int ) e . Key ] = true ;
UpdateModifiers ( e ) ;
}
private unsafe void OnKeyUp ( object sender , KeyboardKeyEventArgs e )
{
var ptr = ImGuiNative . igGetIO ( ) ;
ptr - > KeysDown [ ( int ) e . Key ] = 0 ;
UpdateModifiers ( e , ptr ) ;
ImGui . GetIO ( ) . KeysDown [ ( int ) e . Key ] = false ;
UpdateModifiers ( e ) ;
}
private static unsafe void UpdateModifiers ( KeyboardKeyEventArgs e , IO * ptr )
private static unsafe void UpdateModifiers ( KeyboardKeyEventArgs e )
{
ptr - > KeyAlt = e . Alt ? ( byte ) 1 : ( byte ) 0 ;
ptr - > KeyCtrl = e . Control ? ( byte ) 1 : ( byte ) 0 ;
ptr - > KeyShift = e . Shift ? ( byte ) 1 : ( byte ) 0 ;
IO io = ImGui . GetIO ( ) ;
io . AltPressed = e . Alt ;
io . CtrlPressed = e . Control ;
io . ShiftPressed = e . Shift ;
}
private unsafe void CreateDeviceObjects ( )
{
IO * io = ImGuiNative . ig GetIO( ) ;
IO io = ImGui . GetIO ( ) ;
// Build texture atlas
byte * pixels ;
int width , height ;
ImGuiNative . ImFontAtlas_GetTexDataAsAlpha8 ( io - > FontAtlas , & pixels , & width , & height , null ) ;
Alpha8TexData texData = io . FontAtlas . GetTexDataAsAlpha8 ( ) ;
// Create OpenGL texture
s_fontTexture = GL . GenTexture ( ) ;
GL . BindTexture ( TextureTarget . Texture2D , s_fontTexture ) ;
GL . TexParameter ( TextureTarget . Texture2D , TextureParameterName . TextureMinFilter , ( int ) All . Linear ) ;
GL . TexParameter ( TextureTarget . Texture2D , TextureParameterName . TextureMagFilter , ( int ) All . Linear ) ;
GL . TexImage2D ( TextureTarget . Texture2D , 0 , PixelInternalFormat . Alpha , width , height , 0 , PixelFormat . Alpha , PixelType . UnsignedByte , new IntPtr ( pixels ) ) ;
GL . TexImage2D (
TextureTarget . Texture2D ,
0 ,
PixelInternalFormat . Alpha ,
texData . Width ,
texData . Height ,
0 ,
PixelFormat . Alpha ,
PixelType . UnsignedByte ,
new IntPtr ( texData . Pixels ) ) ;
// Store the texture identifier in the ImFontAtlas substructure.
io - > FontAtlas - > TexID = new IntPtr ( s_fontTexture ) . ToPointer ( ) ;
io . FontAtlas . SetTexID ( s_fontTexture ) ;
// Cleanup (don't clear the input data if you want to append new fonts later)
//io.Fonts->ClearInputData();
ImGuiNative . ImFontAtlas_ClearTexData ( io - > FontAtlas ) ;
io . FontAtlas . ClearTexData ( ) ;
GL . BindTexture ( TextureTarget . Texture2D , 0 ) ;
}
public void RunWindowLoop ( )
{
_ nativeWindow . Visible = true ;
while ( _ nativeWindow . Visible )
{
_ previousFrameStartTime = DateTime . UtcNow ;
RenderFrame ( ) ;
_ nativeWindow . ProcessEvents ( ) ;
DateTime afterFrameTime = DateTime . UtcNow ;
@ -151,79 +158,78 @@ namespace ImGuiNET
{
Thread . Sleep ( 0 ) ;
}
Thread . Sleep ( ( int ) ( sleepTime * 1 0 0 0 ) ) ;
}
}
}
private unsafe void RenderFrame ( )
{
IO * io = ImGuiNative . ig GetIO( ) ;
io - > DisplaySize = new System . Numerics . Vector2 ( _ nativeWindow . Width , _ nativeWindow . Height ) ;
io - > DisplayFramebufferScale = new System . Numerics . Vector2 ( 1 , 1 ) ;
io - > DeltaTime = ( 1f / 6 0f ) ;
IO io = ImGui . GetIO ( ) ;
io . DisplaySize = new System . Numerics . Vector2 ( _ nativeWindow . Width , _ nativeWindow . Height ) ;
io . DisplayFramebufferScale = new System . Numerics . Vector2 ( 1 , 1 ) ;
io . DeltaTime = ( 1f / 6 0f ) ;
UpdateImGuiInput ( io ) ;
ImGuiNative . ig NewFrame( ) ;
ImGui . NewFrame ( ) ;
SubmitImGuiSti ff ( ) ;
SubmitImGuiStu ff ( ) ;
ImGuiNative . ig Render( ) ;
ImGui . Render ( ) ;
DrawData * data = ImGuiNative . ig GetDrawData( ) ;
DrawData * data = ImGui . GetDrawData ( ) ;
RenderImDrawData ( data ) ;
}
private unsafe void SubmitImGuiSti ff ( )
private unsafe void SubmitImGuiStu ff ( )
{
ImGuiNative . ig GetStyle( ) - > WindowRounding = 0 ;
ImGui . GetStyle ( ) . WindowRounding = 0 ;
ImGuiNative . ig SetNextWindowSize( new System . Numerics . Vector2 ( _ nativeWindow . Width - 1 0 , _ nativeWindow . Height - 2 0 ) , SetCondition . Always ) ;
ImGuiNative . ig SetNextWindowPosCenter( SetCondition . Always ) ;
ImGuiNative . ig Begin( "ImGUI.NET Sample Program" , ref _ mainWindowOpened , WindowFlags . NoResize | WindowFlags . NoTitleBar | WindowFlags . NoMove ) ;
ImGui . SetNextWindowSize ( new System . Numerics . Vector2 ( _ nativeWindow . Width - 1 0 , _ nativeWindow . Height - 2 0 ) , SetCondition . Always ) ;
ImGui . SetNextWindowPosCenter ( SetCondition . Always ) ;
ImGui . BeginWindow ( "ImGUI.NET Sample Program" , ref _ mainWindowOpened , WindowFlags . NoResize | WindowFlags . NoTitleBar | WindowFlags . NoMove ) ;
ImGuiNative . ig BeginMainMenuBar( ) ;
if ( ImGuiNative . ig BeginMenu( "Help" ) )
ImGui . BeginMainMenuBar ( ) ;
if ( ImGui . BeginMenu ( "Help" ) )
{
if ( ImGuiNative . ig MenuItem( "About" , "Ctrl-Alt-A" , false , true ) )
if ( ImGui . MenuItem ( "About" , "Ctrl-Alt-A" , false , true ) )
{
}
ImGuiNative . ig EndMenu( ) ;
ImGui . EndMenu ( ) ;
}
ImGuiNative . ig EndMainMenuBar( ) ;
ImGui . EndMainMenuBar ( ) ;
ImGuiNative . ig Text( "Hello," ) ;
ImGuiNative . ig Text( "World!" ) ;
ImGuiNative . ig Text( "From ImGui.NET. ...Did that work?" ) ;
var pos = ImGuiNative . ig GetIO( ) - > MousePos ;
bool leftPressed = ImGuiNative . ig GetIO( ) - > MouseDown [ 0 ] = = 1 ;
ImGuiNative . ig Text( "Current mouse position: " + pos + ". Pressed=" + leftPressed ) ;
ImGui . Text ( "Hello," ) ;
ImGui . Text ( "World!" ) ;
ImGui . Text ( "From ImGui.NET. ...Did that work?" ) ;
var pos = ImGui . GetIO ( ) . MousePosition ;
bool leftPressed = ImGui . GetIO ( ) . MouseDown [ 0 ] ;
ImGui . Text ( "Current mouse position: " + pos + ". Pressed=" + leftPressed ) ;
if ( ImGuiNative . ig Button( "Press me!" , new System . Numerics . Vector2 ( 1 2 0 , 3 0 ) ) )
if ( ImGui . Button ( "Press me!" , new System . Numerics . Vector2 ( 1 2 0 , 3 0 ) ) )
{
_ pressCount + = 1 ;
}
ImGuiNative . ig TextColored( new System . Numerics . Vector4 ( 0 , 1 , 1 , 1 ) , $"Button pressed {_pressCount} times." ) ;
ImGui . TextColored ( new System . Numerics . Vector4 ( 0 , 1 , 1 , 1 ) , $"Button pressed {_pressCount} times." ) ;
ImGuiNative . ig InputTextMultiline( "Input some numbers:" ,
ImGui . InputTextMultiline ( "Input some numbers:" ,
_ textInputBuffer , ( uint ) _ textInputBufferLength ,
new System . Numerics . Vector2 ( 3 6 0 , 2 4 0 ) ,
InputTextFlags . CharsDecimal ,
OnTextEdited , null ) ;
OnTextEdited ) ;
ImGuiNative . ig SliderFloat( "SlidableValue" , ref _ sliderVal , - 5 0f , 1 0 0f , $"{_sliderVal.ToString(" # # 0.00 ")}" , 1 ) ;
ImGui . SliderFloat ( "SlidableValue" , ref _ sliderVal , - 5 0f , 1 0 0f , $"{_sliderVal.ToString(" # # 0.00 ")}" , 1.0f ) ;
if ( ImGuiNative . ig TreeNode( "First Item" ) )
if ( ImGui . TreeNode ( "First Item" ) )
{
ImGuiNative . ig Text( "Word!" ) ;
ImGuiNative . ig TreePop( ) ;
ImGui . Text ( "Word!" ) ;
ImGui . TreePop ( ) ;
}
if ( ImGuiNative . ig TreeNode( "Second Item" ) )
if ( ImGui . TreeNode ( "Second Item" ) )
{
ImGuiNative . ig ColorButton( _ buttonColor , false , true ) ;
ImGui . ColorButton ( _ buttonColor , false , true ) ;
if ( ImGui . Button ( "Push me to change color" , new System . Numerics . Vector2 ( 1 2 0 , 3 0 ) ) )
{
_ buttonColor = new System . Numerics . Vector4 ( _ buttonColor . Y + . 2 5f , _ buttonColor . Z , _ buttonColor . X , _ buttonColor . W ) ;
@ -233,20 +239,24 @@ namespace ImGuiNET
}
}
ImGuiNative . ig TreePop( ) ;
ImGui . TreePop ( ) ;
}
ImGuiNative . igEnd ( ) ;
ImGui . EndWindow ( ) ;
if ( ImGui . GetIO ( ) . AltPressed & & ImGui . GetIO ( ) . KeysDown [ ( int ) Key . F4 ] )
{
_ nativeWindow . Close ( ) ;
}
}
private unsafe int OnTextEdited ( TextEditCallbackData * data )
{
char currentEventChar = ( char ) data - > EventChar ;
Console . WriteLine ( "Event char: " + currentEventChar ) ;
return 0 ;
}
private unsafe void UpdateImGuiInput ( IO * io )
private unsafe void UpdateImGuiInput ( IO io )
{
MouseState cursorState = Mouse . GetCursorState ( ) ;
MouseState mouseState = Mouse . GetState ( ) ;
@ -254,21 +264,21 @@ namespace ImGuiNET
if ( _ nativeWindow . Bounds . Contains ( cursorState . X , cursorState . Y ) )
{
Point windowPoint = _ nativeWindow . PointToClient ( new Point ( cursorState . X , cursorState . Y ) ) ;
io - > MousePos = new System . Numerics . Vector2 ( windowPoint . X , windowPoint . Y ) ;
io . MousePosition = new System . Numerics . Vector2 ( windowPoint . X , windowPoint . Y ) ;
}
else
{
io - > MousePos = new System . Numerics . Vector2 ( - 1f , - 1f ) ;
io . MousePosition = new System . Numerics . Vector2 ( - 1f , - 1f ) ;
}
io - > MouseDown [ 0 ] = ( mouseState . LeftButton = = ButtonState . Pressed ) ? ( byte ) 2 5 5 : ( byte ) 0 ; // Left
io - > MouseDown [ 1 ] = ( mouseState . RightButton = = ButtonState . Pressed ) ? ( byte ) 2 5 5 : ( byte ) 0 ; // Right
io - > MouseDown [ 2 ] = ( mouseState . MiddleButton = = ButtonState . Pressed ) ? ( byte ) 2 5 5 : ( byte ) 0 ; // Middle
io . MouseDown [ 0 ] = mouseState . LeftButton = = ButtonState . Pressed ;
io . MouseDown [ 1 ] = mouseState . RightButton = = ButtonState . Pressed ;
io . MouseDown [ 2 ] = mouseState . MiddleButton = = ButtonState . Pressed ;
float newWheelPos = mouseState . WheelPrecise ;
float delta = newWheelPos - _ wheelPosition ;
_ wheelPosition = newWheelPos ;
io - > MouseWheel = delta ;
io . MouseWheel = delta ;
}
private unsafe void RenderImDrawData ( DrawData * draw_data )
@ -301,15 +311,15 @@ namespace ImGuiNET
GL . UseProgram ( 0 ) ;
// Handle cases of screen coordinates != from framebuffer coordinates (e.g. retina displays)
IO * io = ImGuiNative . ig GetIO( ) ;
float fb_height = io - > DisplaySize . Y * io - > DisplayFramebufferScale . Y ;
ImGui . ScaleClipRects ( draw_data , io - > DisplayFramebufferScale ) ;
IO io = ImGui . GetIO ( ) ;
float fb_height = io . DisplaySize . Y * io . DisplayFramebufferScale . Y ;
ImGui . ScaleClipRects ( draw_data , io . DisplayFramebufferScale ) ;
// Setup orthographic projection matrix
GL . MatrixMode ( MatrixMode . Projection ) ;
GL . PushMatrix ( ) ;
GL . LoadIdentity ( ) ;
GL . Ortho ( 0.0f , io - > DisplaySize . X , io - > DisplaySize . Y , 0.0f , - 1.0f , 1.0f ) ;
GL . Ortho ( 0.0f , io . DisplaySize . X , io . DisplaySize . Y , 0.0f , - 1.0f , 1.0f ) ;
GL . MatrixMode ( MatrixMode . Modelview ) ;
GL . PushMatrix ( ) ;
GL . LoadIdentity ( ) ;