Skip to content

Commit

Permalink
import (slightly outdated) gdext
Browse files Browse the repository at this point in the history
  • Loading branch information
pkdawson committed Jan 19, 2024
1 parent 35968b9 commit 2edc323
Show file tree
Hide file tree
Showing 54 changed files with 4,215 additions and 6 deletions.
20 changes: 20 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,23 @@ msvc.*/
*.pdb
doc/cpp-demo/MyGame/addons
*.old.*
gdext/vcpkg_installed/
.sconsign.dblite
gdext/x64/
*.exp
*.obj
*.lib
*.os
gdext/proj/addons/imgui-godot-native/include/*.h
gdext/gen/
*.o
*.pyc
gdext/proj/doc/
gdext/proj/modules/
gdext/proj/platform/

addons/imgui-godot-native/
gdext/proj/addons/imgui-godot/
gdext/samples/project/addons/
addons/imgui-godot/imgui-godot-native.gdextension
addons/imgui-godot/include/*.h
9 changes: 9 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[submodule "gdext/dear_bindings"]
path = gdext/dear_bindings
url = https://github.com/dearimgui/dear_bindings
[submodule "gdext/imgui"]
path = gdext/imgui
url = https://github.com/ocornut/imgui
[submodule "gdext/godot-cpp"]
path = gdext/godot-cpp
url = https://github.com/godotengine/godot-cpp
2 changes: 2 additions & 0 deletions addons/imgui-godot/ImGuiGodot/ImGuiExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#if GODOT_PC
using Godot;
using ImGuiNET;
using Vector3 = System.Numerics.Vector3;
Expand Down Expand Up @@ -63,3 +64,4 @@ public static void SetIniFilename(this ImGuiIOPtr io, string fileName)
Internal.State.Instance.SetIniFilename(io, fileName);
}
}
#endif
2 changes: 2 additions & 0 deletions addons/imgui-godot/ImGuiGodot/ImGuiGD.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ public static float Scale
}
}

public static bool Visible { get; set; }

static ImGuiGD()
{
_backend = ClassDB.ClassExists("ImGuiGD") ? new Internal.BackendNative() : new Internal.BackendNet();
Expand Down
32 changes: 32 additions & 0 deletions addons/imgui-godot/ImGuiGodot/ImGuiSync.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using Godot;
#if GODOT_PC
using ImGuiNET;
using System.Runtime.InteropServices;
using System;

namespace ImGuiGodot;

public partial class ImGuiSync : GodotObject
{
public static void SyncPtrs()
{
GodotObject gd = Engine.GetSingleton("ImGuiGD");
long[] ptrs = (long[])gd.Call("GetImGuiPtrs",
ImGui.GetVersion(),
Marshal.SizeOf<ImGuiIO>(),
Marshal.SizeOf<ImDrawVert>(),
sizeof(ushort),
sizeof(ushort)
);

if (ptrs.Length != 3)
return;

checked
{
ImGui.SetCurrentContext((IntPtr)ptrs[0]);
ImGui.SetAllocatorFunctions((IntPtr)ptrs[1], (IntPtr)ptrs[2]);
}
}
}
#endif
3 changes: 3 additions & 0 deletions addons/imgui-godot/ImGuiGodot/Internal/BackendNative.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#if GODOT_PC
using Godot;

namespace ImGuiGodot.Internal;
Expand All @@ -6,6 +7,7 @@ internal sealed class BackendNative : IBackend
{
public float JoyAxisDeadZone { get => throw new System.NotImplementedException(); set => throw new System.NotImplementedException(); }
public float Scale { get => throw new System.NotImplementedException(); set => throw new System.NotImplementedException(); }
public bool Visible { get => throw new System.NotImplementedException(); set => throw new System.NotImplementedException(); }

public void AddFont(FontFile fontData, int fontSize, bool merge)
{
Expand Down Expand Up @@ -37,3 +39,4 @@ public bool SubViewportWidget(SubViewport svp)
throw new System.NotImplementedException();
}
}
#endif
9 changes: 9 additions & 0 deletions addons/imgui-godot/ImGuiGodot/Internal/BackendNet.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#if GODOT_PC
using Godot;
using ImGuiNET;
using System;
Expand All @@ -8,12 +9,19 @@ namespace ImGuiGodot.Internal;
internal sealed class BackendNet : IBackend
{
public float JoyAxisDeadZone { get; set; } = 0.15f;

public float Scale
{
get => State.Instance.Scale;
set => State.Instance.Scale = value;
}

public bool Visible
{
get => ImGuiLayer.Instance.Visible;
set => ImGuiLayer.Instance.Visible = value;
}

public void AddFont(FontFile fontData, int fontSize, bool merge)
{
State.Instance.Fonts.AddFont(fontData, fontSize, merge);
Expand Down Expand Up @@ -59,3 +67,4 @@ public bool SubViewportWidget(SubViewport vp)
return false;
}
}
#endif
3 changes: 3 additions & 0 deletions addons/imgui-godot/ImGuiGodot/Internal/IBackend.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#if GODOT_PC
using Godot;

namespace ImGuiGodot.Internal;

internal interface IBackend
{
public bool Visible { get; set; }
public float JoyAxisDeadZone { get; set; }
public float Scale { get; set; }
public void ResetFonts();
Expand All @@ -13,3 +15,4 @@ internal interface IBackend
public void Connect(Callable callable);
public bool SubViewportWidget(SubViewport svp);
}
#endif
2 changes: 2 additions & 0 deletions addons/imgui-godot/ImGuiGodot/Internal/IRenderer.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#if GODOT_PC
using Godot;

namespace ImGuiGodot.Internal;
Expand All @@ -11,3 +12,4 @@ internal interface IRenderer
public void OnHide();
public void Shutdown();
}
#endif
2 changes: 1 addition & 1 deletion addons/imgui-godot/plugin.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
name="imgui-godot"
description="Dear ImGui for Godot"
author="Patrick Dawson"
version="4.1.0"
version="5.0.0"
script="scripts/ImGuiPlugin.gd"
11 changes: 8 additions & 3 deletions addons/imgui-godot/scripts/ImGuiRoot.gd
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,16 @@ const csharp_layer := "res://addons/imgui-godot/ImGuiGodot/ImGuiLayer.cs"
const csharp_sync := "res://addons/imgui-godot/ImGuiGodot/ImGuiSync.cs"

func _enter_tree():
Engine.register_singleton("ImGuiRoot", self)
var features := ProjectSettings.get_setting("application/config/features")
if ClassDB.class_exists("ImGuiLayerNative"):
add_child(ClassDB.instantiate("ImGuiLayerNative"))
if ClassDB.class_exists("ImGuiLayer"):
# native
add_child(ClassDB.instantiate("ImGuiLayer"))
if "C#" in features:
add_child(load(csharp_sync).new())
var obj: Object = load(csharp_sync).new()
obj.SyncPtrs()
obj.free()
else:
# C# only
if "C#" in features:
add_child(load(csharp_layer).new())
14 changes: 14 additions & 0 deletions gdext/.clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
Language: Cpp
BasedOnStyle: Microsoft
AccessModifierOffset: -4
AllowAllArgumentsOnNextLine: false
AlignEscapedNewlines: Left
AlwaysBreakTemplateDeclarations: Yes
BinPackArguments: false
BinPackParameters: true
PointerAlignment: Left

BreakBeforeBraces: Custom
BraceWrapping:
AfterNamespace: false
AfterExternBlock: false
Empty file added gdext/.gdignore
Empty file.
92 changes: 92 additions & 0 deletions gdext/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
cmake_minimum_required(VERSION 3.26)

# using cmake for dev on Windows, scons for production builds

# set up vcpkg
if (WIN32)
set(VCPKG_TARGET_TRIPLET x64-windows-static-md)
endif()
file(TO_CMAKE_PATH "$ENV{VCPKG_ROOT}" VCPKG_ROOT)
set(CMAKE_TOOLCHAIN_FILE "${VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake")

project(imgui-godot-native CXX)

if (NOT TARGET godot-cpp)
include(FetchContent)
FetchContent_Declare(
godot-cpp
GIT_REPOSITORY https://github.com/godotengine/godot-cpp
GIT_TAG 4.2
)
FetchContent_MakeAvailable(godot-cpp)
endif()

find_package(freetype CONFIG REQUIRED)
#find_package(unofficial-lunasvg CONFIG REQUIRED)

add_library(imgui-godot-native SHARED)
target_compile_features(imgui-godot-native PRIVATE cxx_std_20)
target_compile_definitions(imgui-godot-native PUBLIC
IMGUI_USER_CONFIG="imconfig-godot.h"
IMGUI_ENABLE_FREETYPE
# IMGUI_ENABLE_FREETYPE_LUNASVG
IGN_EXPORT
)

if (MSVC)
target_compile_options(godot-cpp PRIVATE "/MP")
target_compile_options(imgui-godot-native PRIVATE "/MP")
endif()

add_subdirectory(src)

target_sources(imgui-godot-native PRIVATE
imgui/imgui.cpp
imgui/imgui_demo.cpp
imgui/imgui_draw.cpp
imgui/imgui_tables.cpp
imgui/imgui_widgets.cpp
imgui/imgui.h
imgui/misc/freetype/imgui_freetype.cpp
imgui/misc/freetype/imgui_freetype.h
include/imconfig-godot.h
include/imgui-godot.h
gen/imgui_bindings.gen.h
gen/cimgui.cpp
gen/cimgui.h
)

target_link_libraries(imgui-godot-native PUBLIC
godot-cpp
freetype
# unofficial::lunasvg::lunasvg
)

target_include_directories(imgui-godot-native PRIVATE src imgui gen PUBLIC include)
set_target_properties(imgui-godot-native PROPERTIES PUBLIC_HEADER "include/imconfig-godot.h;include/imgui-godot.h")

if(WIN32)
set_property(TARGET imgui-godot-native
PROPERTY OUTPUT_NAME "libimgui-godot-native.windows.$<IF:$<CONFIG:Debug>,debug,release>.x86_64")
elseif(APPLE)
set_property(TARGET imgui-godot-native
PROPERTY OUTPUT_NAME "imgui-godot-native.macos.$<IF:$<CONFIG:Debug>,debug,release>")
set_target_properties(imgui-godot-native PROPERTIES SUFFIX "")
endif()

set(CMAKE_INSTALL_PREFIX "${CMAKE_SOURCE_DIR}/../addons/imgui-godot")

if(WIN32)
install(TARGETS imgui-godot-native
RUNTIME DESTINATION bin
PUBLIC_HEADER DESTINATION include
)
elseif(APPLE)
install(TARGETS imgui-godot-native
DESTINATION bin/libimgui-godot-native.macos.$<IF:$<CONFIG:Debug>,debug,release>.framework
PUBLIC_HEADER DESTINATION include
)
endif()
install(FILES "${CMAKE_SOURCE_DIR}/imgui-godot-native.gdextension"
DESTINATION .
)
77 changes: 77 additions & 0 deletions gdext/SConstruct
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#!/usr/bin/env python
from glob import glob
from pathlib import Path

env = SConscript("godot-cpp/SConstruct")
env.Append(CPPDEFINES=['IMGUI_USER_CONFIG="\\"imconfig-godot.h\\""', "IGN_EXPORT"])
env.Append(CPPPATH=["src/", "imgui/", "include/", "gen/"])
env.Replace(CXXFLAGS=str(env["CXXFLAGS"]).replace("c++17", "c++20"))

sources = Glob("src/*.cpp") + Glob("imgui/*.cpp") + Glob("gen/*.cpp")

(extension_path,) = glob("proj/addons/*/*.gdextension")
addon_path = Path(extension_path).parent
project_name = Path(extension_path).stem

config = "release" if env["target"] == "template_release" else "debug"

libpath = env.get("LIBPATH", [])
libs = [env["LIBS"]]

windows = env["platform"] == "windows"
linux = env["platform"] == "linux"

if config == "release":
if windows:
env.Append(CPPDEFINES=["NDEBUG"])

if windows:
triplet = "x64-windows-static"
elif linux:
triplet = "x64-linux"
else:
triplet = "arm64-osx"

env.Append(CPPPATH=["imgui/misc/freetype/"])
env.Append(CPPPATH=[f"vcpkg_installed/{triplet}/include"])
env.Append(CPPDEFINES=["IMGUI_ENABLE_FREETYPE"]) #, "IMGUI_ENABLE_FREETYPE_LUNASVG"])
sources += Glob("imgui/misc/freetype/*.cpp")
libpath += [f"vcpkg_installed/{triplet}/lib"]
libs += [
"freetype",
# "lunasvg",
"bz2",
"libpng16" if windows else "png16",
"zlib" if windows else "z",
"brotlidec",
"brotlicommon",
]

if env["platform"] == "macos":
library = env.SharedLibrary(
"{0}/bin/lib{1}.{2}.{3}.framework/{1}.{2}.{3}".format(
addon_path,
project_name,
env["platform"],
config,
),
source=sources,
LIBPATH=libpath,
LIBS=libs,
)
else:
library = env.SharedLibrary(
"{}/bin/lib{}.{}.{}.{}{}".format(
addon_path,
project_name,
env["platform"],
config,
env["arch"],
env["SHLIBSUFFIX"],
),
source=sources,
LIBPATH=libpath,
LIBS=libs,
)

Default(library)
1 change: 1 addition & 0 deletions gdext/dear_bindings
Submodule dear_bindings added at 0c3357
Loading

0 comments on commit 2edc323

Please sign in to comment.