Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Limit strcpy_s call to Windows platform #64

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ImGui.uplugin
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"Modules": [
{
"Name": "ImGui",
"Type": "Developer",
"Type": "DeveloperTool",
"LoadingPhase": "PreDefault"
}
]
Expand Down
365 changes: 127 additions & 238 deletions README.md

Large diffs are not rendered by default.

9 changes: 7 additions & 2 deletions Source/ImGui/ImGui.Build.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ public ImGui(TargetInfo Target)

PublicIncludePaths.AddRange(
new string[] {
Path.Combine(ModuleDirectory, "../ThirdParty/ImGuiLibrary/Include")
Path.Combine(ModuleDirectory, "../ThirdParty/ImGuiLibrary/Include"),
Path.Combine(ModuleDirectory, "../ThirdParty/ImPlotLibrary/Public")
// ... add public include paths required here ...
}
);
Expand All @@ -42,7 +43,8 @@ public ImGui(TargetInfo Target)
PrivateIncludePaths.AddRange(
new string[] {
"ImGui/Private",
"ThirdParty/ImGuiLibrary/Private"
"ThirdParty/ImGuiLibrary/Private",
"ThirdParty/ImPlotLibrary/Private"
// ... add other private include paths required here ...
}
);
Expand Down Expand Up @@ -97,5 +99,8 @@ public ImGui(TargetInfo Target)
#endif

PrivateDefinitions.Add(string.Format("RUNTIME_LOADER_ENABLED={0}", bEnableRuntimeLoader ? 1 : 0));

// Force ImPlot to export its methods in this module DLL so we can import them in our main project
PrivateDefinitions.Add(string.Format("IMPLOT_API=DLLEXPORT"));
}
}
21 changes: 19 additions & 2 deletions Source/ImGui/Private/ImGuiContextManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "ImGuiDelegatesContainer.h"
#include "ImGuiImplementation.h"
#include "ImGuiModuleSettings.h"
#include "ImGuiModule.h"
#include "Utilities/WorldContext.h"
#include "Utilities/WorldContextIndex.h"

Expand Down Expand Up @@ -254,14 +255,30 @@ void FImGuiContextManager::SetDPIScale(const FImGuiDPIScaleInfo& ScaleInfo)
}
}

void FImGuiContextManager::BuildFontAtlas()
void FImGuiContextManager::BuildFontAtlas(const TMap<FName, TSharedPtr<ImFontConfig>>& CustomFontConfigs)
{
if (!FontAtlas.IsBuilt())
{
ImFontConfig FontConfig = {};
FontConfig.SizePixels = FMath::RoundFromZero(13.f * DPIScale);
FontAtlas.AddFontDefault(&FontConfig);

#if PLATFORM_WINDOWS
// Build custom fonts
for (const TPair<FName, TSharedPtr<ImFontConfig>>& CustomFontPair : CustomFontConfigs)
{
FName CustomFontName = CustomFontPair.Key;
TSharedPtr<ImFontConfig> CustomFontConfig = CustomFontPair.Value;

// Set font name for debugging
if (CustomFontConfig.IsValid())
{
strcpy_s(CustomFontConfig->Name, 40, TCHAR_TO_ANSI(*CustomFontName.ToString()));
}

FontAtlas.AddFont(CustomFontConfig.Get());
}
#endif
unsigned char* Pixels;
int Width, Height, Bpp;
FontAtlas.GetTexDataAsRGBA32(&Pixels, &Width, &Height, &Bpp);
Expand All @@ -283,5 +300,5 @@ void FImGuiContextManager::RebuildFontAtlas()
FontResourcesReleaseCountdown = 3;
}

BuildFontAtlas();
BuildFontAtlas(FImGuiModule::Get().GetProperties().GetCustomFonts());
}
5 changes: 3 additions & 2 deletions Source/ImGui/Private/ImGuiContextManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ class FImGuiContextManager

void Tick(float DeltaSeconds);

void RebuildFontAtlas();

private:

struct FContextData
Expand Down Expand Up @@ -102,8 +104,7 @@ class FImGuiContextManager
FContextData& GetWorldContextData(const UWorld& World, int32* OutContextIndex = nullptr);

void SetDPIScale(const FImGuiDPIScaleInfo& ScaleInfo);
void BuildFontAtlas();
void RebuildFontAtlas();
void BuildFontAtlas(const TMap<FName, TSharedPtr<ImFontConfig>>& CustomFontConfigs = {});

TMap<int32, FContextData> Contexts;

Expand Down
13 changes: 11 additions & 2 deletions Source/ImGui/Private/ImGuiContextProxy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
#include "Utilities/Arrays.h"
#include "VersionCompatibility.h"

// Include ImPlot here so we can call `ImPlot::CreateContext`
#include <implot.h>

#include <GenericPlatform/GenericPlatformFile.h>
#include <Misc/Paths.h>

Expand Down Expand Up @@ -81,6 +84,9 @@ FImGuiContextProxy::FImGuiContextProxy(const FString& InName, int32 InContextInd
// Create context.
Context = ImGui::CreateContext(InFontAtlas);

// Create ImPlot context
ImPlot::CreateContext();

// Set this context in ImGui for initialization (any allocations will be tracked in this context).
SetAsCurrent();

Expand All @@ -92,7 +98,7 @@ FImGuiContextProxy::FImGuiContextProxy(const FString& InName, int32 InContextInd

// Start with the default canvas size.
ResetDisplaySize();
IO.DisplaySize = { DisplaySize.X, DisplaySize.Y };
IO.DisplaySize = { (float)DisplaySize.X,(float)DisplaySize.Y };

// Set the initial DPI scale.
SetDPIScale(InDPIScale);
Expand All @@ -115,6 +121,9 @@ FImGuiContextProxy::~FImGuiContextProxy()

// Save context data and destroy.
ImGui::DestroyContext(Context);

// Destroy ImPlot context
ImPlot::DestroyContext();
}
}

Expand Down Expand Up @@ -211,7 +220,7 @@ void FImGuiContextProxy::BeginFrame(float DeltaTime)
ImGuiInterops::CopyInput(IO, InputState);
InputState.ClearUpdateState();

IO.DisplaySize = { DisplaySize.X, DisplaySize.Y };
IO.DisplaySize = { (float)DisplaySize.X, (float)DisplaySize.Y };

ImGui::NewFrame();

Expand Down
6 changes: 1 addition & 5 deletions Source/ImGui/Private/ImGuiDrawData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ void FImGuiDrawList::CopyVertexData(TArray<FSlateVertex>& OutVertexBuffer, const
SlateVertex.Position[1] = VertexPosition.Y;
SlateVertex.ClipRect = VertexClippingRect;
#else
SlateVertex.Position = Transform.TransformPoint(ImGuiInterops::ToVector2D(ImGuiVertex.pos));
SlateVertex.Position = (FVector2f)Transform.TransformPoint(ImGuiInterops::ToVector2D(ImGuiVertex.pos));
#endif // ENGINE_COMPATIBILITY_LEGACY_CLIPPING_API

// Unpack ImU32 color.
Expand All @@ -56,8 +56,4 @@ void FImGuiDrawList::TransferDrawData(ImDrawList& Src)
Src.CmdBuffer.swap(ImGuiCommandBuffer);
Src.IdxBuffer.swap(ImGuiIndexBuffer);
Src.VtxBuffer.swap(ImGuiVertexBuffer);

// ImGui seems to clear draw lists in every frame, but since source list can contain pointers to buffers that
// we just swapped, it is better to clear explicitly here.
Src.Clear();
}
5 changes: 5 additions & 0 deletions Source/ImGui/Private/ImGuiImplementation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ static FImGuiContextHandle ImGuiContextPtrHandle(ImGuiContextPtr);
#include "imgui_draw.cpp"
#include "imgui_widgets.cpp"

#include "imgui_tables.cpp"
#include "implot.cpp"
#include "implot_items.cpp"
#include "implot_demo.cpp"

#if PLATFORM_WINDOWS
#include <Windows/HideWindowsPlatformTypes.h>
#endif // PLATFORM_WINDOWS
Expand Down
4 changes: 4 additions & 0 deletions Source/ImGui/Private/ImGuiInputHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,16 @@ FReply UImGuiInputHandler::OnKeyDown(const FKeyEvent& KeyEvent)
InputState->SetKeyDown(KeyEvent, true);
CopyModifierKeys(KeyEvent);

InputState->KeyDownEvents.Add(KeyEvent.GetKeyCode(), KeyEvent);

return ToReply(bConsume);
}
}

FReply UImGuiInputHandler::OnKeyUp(const FKeyEvent& KeyEvent)
{
InputState->KeyUpEvents.Add(KeyEvent.GetKeyCode(), KeyEvent);

if (KeyEvent.GetKey().IsGamepadKey())
{
bool bConsume = false;
Expand Down
2 changes: 1 addition & 1 deletion Source/ImGui/Private/ImGuiInputHandlerFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include <InputCoreTypes.h>


UImGuiInputHandler* FImGuiInputHandlerFactory::NewHandler(const FStringClassReference& HandlerClassReference, FImGuiModuleManager* ModuleManager, UGameViewportClient* GameViewport, int32 ContextIndex)
UImGuiInputHandler* FImGuiInputHandlerFactory::NewHandler(const FSoftClassPath& HandlerClassReference, FImGuiModuleManager* ModuleManager, UGameViewportClient* GameViewport, int32 ContextIndex)
{
UClass* HandlerClass = nullptr;
if (HandlerClassReference.IsValid())
Expand Down
2 changes: 1 addition & 1 deletion Source/ImGui/Private/ImGuiInputHandlerFactory.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class FImGuiInputHandlerFactory
{
public:

static UImGuiInputHandler* NewHandler(const FStringClassReference& HandlerClassReference, FImGuiModuleManager* ModuleManager, UGameViewportClient* GameViewport, int32 ContextIndex);
static UImGuiInputHandler* NewHandler(const FSoftClassPath& HandlerClassReference, FImGuiModuleManager* ModuleManager, UGameViewportClient* GameViewport, int32 ContextIndex);

static void ReleaseHandler(UImGuiInputHandler* Handler);
};
3 changes: 3 additions & 0 deletions Source/ImGui/Private/ImGuiInputState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ void FImGuiInputState::ClearUpdateState()
{
ClearCharacters();

KeyDownEvents.Reset();
KeyUpEvents.Reset();

KeysUpdateRange.SetEmpty();
MouseButtonsUpdateRange.SetEmpty();

Expand Down
3 changes: 3 additions & 0 deletions Source/ImGui/Private/ImGuiInputState.h
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,9 @@ class FImGuiInputState
// and information about dirty parts of keys or mouse buttons arrays.
void ClearUpdateState();

TMap<uint32, FKeyEvent> KeyDownEvents;
TMap<uint32, FKeyEvent> KeyUpEvents;

private:

void SetKeyDown(uint32 KeyIndex, bool bIsDown);
Expand Down
Loading