Scaffold: upstream @ 105e4fd, core submodule, test runner, example
This commit is contained in:
commit
83426a3cbf
27 files changed
+8149
No files matched your search
@@ -0,0 +1,2 @@
|
||||
.gitattributes export-ignore
|
||||
tools/upstream/** linguist-vendored=true
|
||||
@@ -0,0 +1,3 @@
|
||||
[submodule "tools/core"]
|
||||
path = tools/core
|
||||
url = https://git.bug.tools/bug/imgui-luau.git
|
||||
@@ -0,0 +1,8 @@
|
||||
-- ImGuizmo for imgui-luau. Port of https://github.com/CedricGuillemet/ImGuizmo (105e4fde45e4e74aae8973d98750820ef7aca7e2).
|
||||
-- Usage: local ImGuizmo = require(path.to.ImGuizmo)(require(path.to.ImGui))
|
||||
return function(G)
|
||||
local ImGui, ImVec2, ImVec4 = G.ImGui, G.ImVec2, G.ImVec4
|
||||
local ImGuizmo = {}
|
||||
-- [Port] TODO: port from tools/upstream
|
||||
return ImGuizmo
|
||||
end
|
||||
@@ -0,0 +1,23 @@
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2016 Cedric Guillemet
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
|
||||
Luau port: Copyright (c) 2026 4DBug, same MIT terms.
|
||||
@@ -0,0 +1,26 @@
|
||||
# imguizmo-luau
|
||||
|
||||
3D gizmo (translate/rotate/scale) for imgui-luau, with Roblox Camera/CFrame helpers.
|
||||
|
||||
Luau port of [ImGuizmo](https://github.com/CedricGuillemet/ImGuizmo) for [imgui-luau](https://git.bug.tools/bug/imgui-luau) ([GitHub](https://github.com/4DBug/imgui-luau)).
|
||||
Mirrors: [git.bug.tools](https://git.bug.tools/bug/imguizmo-luau) / [GitHub](https://github.com/4DBug/imguizmo-luau).
|
||||
|
||||
**Status: work in progress.**
|
||||
|
||||
```lua
|
||||
local G = require(ReplicatedStorage.ImGui)
|
||||
local ImGuizmo = require(ReplicatedStorage.ImGuizmo)(G)
|
||||
```
|
||||
|
||||
## Development
|
||||
|
||||
```sh
|
||||
git clone --recursive https://git.bug.tools/bug/imguizmo-luau.git
|
||||
bash tools/test.sh # tests/t_*.luau in the core's Roblox mock
|
||||
```
|
||||
|
||||
Upstream source (pinned) is in `tools/upstream/`; port-specific comments are tagged `[Port]`.
|
||||
|
||||
## License
|
||||
|
||||
MIT, see LICENSE (upstream copyright kept).
|
||||
@@ -0,0 +1,11 @@
|
||||
{
|
||||
"name": "imguizmo-luau",
|
||||
"tree": {
|
||||
"$className": "DataModel",
|
||||
"ReplicatedStorage": {
|
||||
"ImGui": { "$path": "tools/core/build/ImGuiTyped.luau", "Impl": { "$path": "tools/core/build/ImGui.luau" } },
|
||||
"ImGuizmo": { "$path": "ImGuizmo.lua" }
|
||||
},
|
||||
"StarterPlayer": { "StarterPlayerScripts": { "ImGuizmoExample": { "$path": "examples/roblox/main.client.luau" } } }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
local RS = game:GetService("ReplicatedStorage")
|
||||
local RunService = game:GetService("RunService")
|
||||
local G = require(RS:WaitForChild("ImGui"))
|
||||
local ImGuizmo = require(RS:WaitForChild("ImGuizmo"))(G)
|
||||
local ImGui = G.ImGui
|
||||
|
||||
ImGui.CreateContext()
|
||||
G.ImGui_ImplRoblox.Init(game:GetService("Players").LocalPlayer:WaitForChild("PlayerGui"))
|
||||
RunService.RenderStepped:Connect(function()
|
||||
G.ImGui_ImplRoblox.NewFrame()
|
||||
ImGui.NewFrame()
|
||||
-- ImGuizmo example goes here
|
||||
ImGui.Render()
|
||||
G.ImGui_ImplRoblox.RenderDrawData(ImGui.GetDrawData())
|
||||
end)
|
||||
@@ -0,0 +1,5 @@
|
||||
local RS = game:GetService("ReplicatedStorage")
|
||||
local G = require(RS:WaitForChild("ImGui"))
|
||||
local ImGuizmo = require(RS:WaitForChild("ImGuizmo"))(G)
|
||||
assert(type(ImGuizmo) == "table", "module did not return a table")
|
||||
print("OK")
|
||||
Submodule
+1
Submodule tools/core added at 7cd2ee010a.
Executable
+9
@@ -0,0 +1,9 @@
|
||||
#!/usr/bin/env bash
|
||||
# Runs every tests/t_*.luau against the core (tools/core submodule) in the Roblox mock.
|
||||
cd "$(dirname "$0")/.."
|
||||
fail=0
|
||||
for t in tests/t_*.luau; do
|
||||
out=$(python3 tools/core/tools/test/run.py --module ImGuizmo=ImGuizmo.lua --main "$t" --quiet --roblox 2>&1)
|
||||
if echo "$out" | grep -q "^OK"; then echo "PASS $t"; else echo "FAIL $t"; echo "$out" | tail -20; fail=1; fi
|
||||
done
|
||||
exit $fail
|
||||
Vendored
+21
@@ -0,0 +1,21 @@
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2016 Cedric Guillemet
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
Vendored
+1
@@ -0,0 +1 @@
|
||||
https://github.com/CedricGuillemet/ImGuizmo @ 105e4fde45e4e74aae8973d98750820ef7aca7e2
|
||||
Vendored
+1128
File diff suppressed because it is too large.
Load diff
Vendored
+151
@@ -0,0 +1,151 @@
|
||||
// https://github.com/CedricGuillemet/ImGuizmo
|
||||
// v1.92.5 WIP
|
||||
//
|
||||
// The MIT License(MIT)
|
||||
//
|
||||
// Copyright(c) 2016-2026 Cedric Guillemet and contributors
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files(the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions :
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in all
|
||||
// copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
//
|
||||
#pragma once
|
||||
|
||||
#include <vector>
|
||||
#include <stdint.h>
|
||||
#include <string>
|
||||
#include "imgui.h"
|
||||
#include "imgui_internal.h"
|
||||
|
||||
namespace GraphEditor {
|
||||
|
||||
typedef size_t NodeIndex;
|
||||
typedef size_t SlotIndex;
|
||||
typedef size_t LinkIndex;
|
||||
typedef size_t TemplateIndex;
|
||||
|
||||
// Force the view to be respositionned and zoom to fit nodes with Show function.
|
||||
// Parameter value will be changed to Fit_None by the function.
|
||||
enum FitOnScreen
|
||||
{
|
||||
Fit_None,
|
||||
Fit_AllNodes,
|
||||
Fit_SelectedNodes
|
||||
};
|
||||
|
||||
// Display options and colors
|
||||
struct Options
|
||||
{
|
||||
ImRect mMinimap{{0.75f, 0.8f, 0.99f, 0.99f}}; // rectangle coordinates of minimap
|
||||
ImU32 mBackgroundColor{ IM_COL32(40, 40, 40, 255) }; // full background color
|
||||
ImU32 mGridColor{ IM_COL32(0, 0, 0, 60) }; // grid lines color
|
||||
ImU32 mGridColor2{ IM_COL32(0, 0, 0, 160) }; // grid lines color every 10th
|
||||
ImU32 mSelectedNodeBorderColor{ IM_COL32(255, 130, 30, 255) }; // node border color when it's selected
|
||||
ImU32 mNodeBorderColor{ IM_COL32(100, 100, 100, 0) }; // node border color when it's not selected
|
||||
ImU32 mQuadSelection{ IM_COL32(255, 32, 32, 64) }; // quad selection inside color
|
||||
ImU32 mQuadSelectionBorder{ IM_COL32(255, 32, 32, 255) }; // quad selection border color
|
||||
ImU32 mDefaultSlotColor{ IM_COL32(128, 128, 128, 255) }; // when no color is provided in node template, use this value
|
||||
ImU32 mFrameFocus{ IM_COL32(64, 128, 255, 255) }; // rectangle border when graph editor has focus
|
||||
float mLineThickness{ 5 }; // links width in pixels when zoom value is 1
|
||||
float mGridSize{ 64.f }; // background grid size in pixels when zoom value is 1
|
||||
float mRounding{ 3.f }; // rounding at node corners
|
||||
float mZoomRatio{ 0.1f }; // factor per mouse wheel delta
|
||||
float mZoomLerpFactor{ 0.25f }; // the smaller, the smoother
|
||||
float mBorderSelectionThickness{ 6.f }; // thickness of selection border around nodes
|
||||
float mBorderThickness{ 6.f }; // thickness of selection border around nodes
|
||||
float mNodeSlotRadius{ 8.f }; // circle radius for inputs and outputs
|
||||
float mNodeSlotHoverFactor{ 1.2f }; // increase size when hovering
|
||||
float mMinZoom{ 0.2f }, mMaxZoom { 1.1f };
|
||||
float mSnap{ 5.f };
|
||||
bool mDisplayLinksAsCurves{ true }; // false is straight and 45deg lines
|
||||
bool mAllowQuadSelection{ true }; // multiple selection using drag and drop
|
||||
bool mRenderGrid{ true }; // grid or nothing
|
||||
bool mDrawIONameOnHover{ true }; // only draw node input/output when hovering
|
||||
};
|
||||
|
||||
// View state: scroll position and zoom factor
|
||||
struct ViewState
|
||||
{
|
||||
ImVec2 mPosition{0.0f, 0.0f}; // scroll position
|
||||
float mFactor{ 1.0f }; // current zoom factor
|
||||
float mFactorTarget{ 1.0f }; // targeted zoom factor interpolated using Options.mZoomLerpFactor
|
||||
};
|
||||
|
||||
struct Template
|
||||
{
|
||||
ImU32 mHeaderColor;
|
||||
ImU32 mBackgroundColor;
|
||||
ImU32 mBackgroundColorOver;
|
||||
ImU8 mInputCount;
|
||||
const char** mInputNames; // can be nullptr. No text displayed.
|
||||
ImU32* mInputColors; // can be nullptr, default slot color will be used.
|
||||
ImU8 mOutputCount;
|
||||
const char** mOutputNames; // can be nullptr. No text displayed.
|
||||
ImU32* mOutputColors; // can be nullptr, default slot color will be used.
|
||||
};
|
||||
|
||||
struct Node
|
||||
{
|
||||
const char* mName;
|
||||
TemplateIndex mTemplateIndex;
|
||||
ImRect mRect;
|
||||
bool mSelected{ false };
|
||||
};
|
||||
|
||||
struct Link
|
||||
{
|
||||
NodeIndex mInputNodeIndex;
|
||||
SlotIndex mInputSlotIndex;
|
||||
NodeIndex mOutputNodeIndex;
|
||||
SlotIndex mOutputSlotIndex;
|
||||
};
|
||||
|
||||
struct Delegate
|
||||
{
|
||||
virtual bool AllowedLink(NodeIndex from, NodeIndex to) = 0;
|
||||
|
||||
virtual void SelectNode(NodeIndex nodeIndex, bool selected) = 0;
|
||||
virtual void MoveSelectedNodes(const ImVec2 delta) = 0;
|
||||
|
||||
virtual void AddLink(NodeIndex inputNodeIndex, SlotIndex inputSlotIndex, NodeIndex outputNodeIndex, SlotIndex outputSlotIndex) = 0;
|
||||
virtual void DelLink(LinkIndex linkIndex) = 0;
|
||||
|
||||
// user is responsible for clipping
|
||||
virtual void CustomDraw(ImDrawList* drawList, ImRect rectangle, NodeIndex nodeIndex) = 0;
|
||||
|
||||
// use mouse position to open context menu
|
||||
// if nodeIndex != -1, right click happens on the specified node
|
||||
virtual void RightClick(NodeIndex nodeIndex, SlotIndex slotIndexInput, SlotIndex slotIndexOutput) = 0;
|
||||
|
||||
virtual const size_t GetTemplateCount() = 0;
|
||||
virtual const Template GetTemplate(TemplateIndex index) = 0;
|
||||
|
||||
virtual const size_t GetNodeCount() = 0;
|
||||
virtual const Node GetNode(NodeIndex index) = 0;
|
||||
|
||||
virtual const size_t GetLinkCount() = 0;
|
||||
virtual const Link GetLink(LinkIndex index) = 0;
|
||||
|
||||
virtual ~Delegate() = default;
|
||||
};
|
||||
|
||||
void Show(Delegate& delegate, const Options& options, ViewState& viewState, bool enabled, FitOnScreen* fit = nullptr);
|
||||
void GraphEditorClear();
|
||||
|
||||
bool EditOptions(Options& options);
|
||||
|
||||
} // namespace
|
||||
Vendored
+466
@@ -0,0 +1,466 @@
|
||||
// https://github.com/CedricGuillemet/ImGuizmo
|
||||
// v1.92.5 WIP
|
||||
//
|
||||
// The MIT License(MIT)
|
||||
//
|
||||
// Copyright(c) 2016-2026 Cedric Guillemet and contributors
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files(the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions :
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in all
|
||||
// copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
//
|
||||
#include "ImCurveEdit.h"
|
||||
#include "imgui.h"
|
||||
#include "imgui_internal.h"
|
||||
#include <stdint.h>
|
||||
#include <set>
|
||||
#include <vector>
|
||||
|
||||
#if defined(_MSC_VER) || defined(__MINGW32__)
|
||||
#include <malloc.h>
|
||||
#endif
|
||||
#if !defined(_MSC_VER) && !defined(__MINGW64_VERSION_MAJOR)
|
||||
#define _malloca(x) alloca(x)
|
||||
#define _freea(x)
|
||||
#endif
|
||||
|
||||
namespace ImCurveEdit
|
||||
{
|
||||
|
||||
#ifndef IMGUI_DEFINE_MATH_OPERATORS
|
||||
static ImVec2 operator+(const ImVec2& a, const ImVec2& b) {
|
||||
return ImVec2(a.x + b.x, a.y + b.y);
|
||||
}
|
||||
|
||||
static ImVec2 operator-(const ImVec2& a, const ImVec2& b) {
|
||||
return ImVec2(a.x - b.x, a.y - b.y);
|
||||
}
|
||||
|
||||
static ImVec2 operator*(const ImVec2& a, const ImVec2& b) {
|
||||
return ImVec2(a.x * b.x, a.y * b.y);
|
||||
}
|
||||
|
||||
static ImVec2 operator/(const ImVec2& a, const ImVec2& b) {
|
||||
return ImVec2(a.x / b.x, a.y / b.y);
|
||||
}
|
||||
|
||||
static ImVec2 operator*(const ImVec2& a, const float b) {
|
||||
return ImVec2(a.x * b, a.y * b);
|
||||
}
|
||||
#endif
|
||||
|
||||
static float smoothstep(float edge0, float edge1, float x)
|
||||
{
|
||||
x = ImClamp((x - edge0) / (edge1 - edge0), 0.0f, 1.0f);
|
||||
return x * x * (3 - 2 * x);
|
||||
}
|
||||
|
||||
static float distance(float x, float y, float x1, float y1, float x2, float y2)
|
||||
{
|
||||
float A = x - x1;
|
||||
float B = y - y1;
|
||||
float C = x2 - x1;
|
||||
float D = y2 - y1;
|
||||
|
||||
float dot = A * C + B * D;
|
||||
float len_sq = C * C + D * D;
|
||||
float param = -1.f;
|
||||
if (len_sq > FLT_EPSILON)
|
||||
param = dot / len_sq;
|
||||
|
||||
float xx, yy;
|
||||
|
||||
if (param < 0.f) {
|
||||
xx = x1;
|
||||
yy = y1;
|
||||
}
|
||||
else if (param > 1.f) {
|
||||
xx = x2;
|
||||
yy = y2;
|
||||
}
|
||||
else {
|
||||
xx = x1 + param * C;
|
||||
yy = y1 + param * D;
|
||||
}
|
||||
|
||||
float dx = x - xx;
|
||||
float dy = y - yy;
|
||||
return sqrtf(dx * dx + dy * dy);
|
||||
}
|
||||
|
||||
static int DrawPoint(ImDrawList* draw_list, ImVec2 pos, const ImVec2 size, const ImVec2 offset, bool edited)
|
||||
{
|
||||
int ret = 0;
|
||||
ImGuiIO& io = ImGui::GetIO();
|
||||
|
||||
static const ImVec2 localOffsets[4] = { ImVec2(1,0), ImVec2(0,1), ImVec2(-1,0), ImVec2(0,-1) };
|
||||
ImVec2 offsets[4];
|
||||
for (int i = 0; i < 4; i++)
|
||||
{
|
||||
offsets[i] = pos * size + localOffsets[i] * 4.5f + offset;
|
||||
}
|
||||
|
||||
const ImVec2 center = pos * size + offset;
|
||||
const ImRect anchor(center - ImVec2(5, 5), center + ImVec2(5, 5));
|
||||
draw_list->AddConvexPolyFilled(offsets, 4, 0xFF000000);
|
||||
if (anchor.Contains(io.MousePos))
|
||||
{
|
||||
ret = 1;
|
||||
if (io.MouseDown[0])
|
||||
ret = 2;
|
||||
}
|
||||
#if IMGUI_VERSION_NUM < 19276
|
||||
if (edited)
|
||||
draw_list->AddPolyline(offsets, 4, 0xFFFFFFFF, true, 3.0f);
|
||||
else if (ret)
|
||||
draw_list->AddPolyline(offsets, 4, 0xFF80B0FF, true, 2.0f);
|
||||
else
|
||||
draw_list->AddPolyline(offsets, 4, 0xFF0080FF, true, 2.0f);
|
||||
#else
|
||||
if (edited)
|
||||
draw_list->AddPolyline(offsets, 4, 0xFFFFFFFF, 3.0f);
|
||||
else if (ret)
|
||||
draw_list->AddPolyline(offsets, 4, 0xFF80B0FF, 2.0f);
|
||||
else
|
||||
draw_list->AddPolyline(offsets, 4, 0xFF0080FF, 2.0f);
|
||||
#endif
|
||||
return ret;
|
||||
}
|
||||
|
||||
int Edit(Delegate& delegate, const ImVec2& size, unsigned int id, const ImRect* clippingRect, ImVector<EditPoint>* selectedPoints)
|
||||
{
|
||||
static bool selectingQuad = false;
|
||||
static ImVec2 quadSelection;
|
||||
static int overCurve = -1;
|
||||
static int movingCurve = -1;
|
||||
static bool scrollingV = false;
|
||||
static std::set<EditPoint> selection;
|
||||
static bool overSelectedPoint = false;
|
||||
|
||||
int ret = 0;
|
||||
|
||||
ImGuiIO& io = ImGui::GetIO();
|
||||
ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2(0, 0));
|
||||
ImGui::PushStyleColor(ImGuiCol_Border, 0);
|
||||
ImGui::BeginChild(id, size, ImGuiChildFlags_FrameStyle);
|
||||
delegate.focused = ImGui::IsWindowFocused();
|
||||
ImDrawList* draw_list = ImGui::GetWindowDrawList();
|
||||
if (clippingRect)
|
||||
draw_list->PushClipRect(clippingRect->Min, clippingRect->Max, true);
|
||||
|
||||
const ImVec2 offset = ImGui::GetCursorScreenPos() + ImVec2(0.f, size.y);
|
||||
const ImVec2 ssize(size.x, -size.y);
|
||||
const ImRect container(offset + ImVec2(0.f, ssize.y), offset + ImVec2(ssize.x, 0.f));
|
||||
ImVec2& min = delegate.GetMin();
|
||||
ImVec2& max = delegate.GetMax();
|
||||
|
||||
// handle zoom and VScroll
|
||||
if (container.Contains(io.MousePos))
|
||||
{
|
||||
if (fabsf(io.MouseWheel) > FLT_EPSILON)
|
||||
{
|
||||
const float r = (io.MousePos.y - offset.y) / ssize.y;
|
||||
float ratioY = ImLerp(min.y, max.y, r);
|
||||
auto scaleValue = [&](float v) {
|
||||
v -= ratioY;
|
||||
v *= (1.f - io.MouseWheel * 0.05f);
|
||||
v += ratioY;
|
||||
return v;
|
||||
};
|
||||
min.y = scaleValue(min.y);
|
||||
max.y = scaleValue(max.y);
|
||||
}
|
||||
if (!scrollingV && ImGui::IsMouseDown(2))
|
||||
{
|
||||
scrollingV = true;
|
||||
}
|
||||
}
|
||||
ImVec2 range = max - min + ImVec2(1.f, 0.f); // +1 because of inclusive last frame
|
||||
|
||||
const ImVec2 viewSize(size.x, -size.y);
|
||||
const ImVec2 sizeOfPixel = ImVec2(1.f, 1.f) / viewSize;
|
||||
const size_t curveCount = delegate.GetCurveCount();
|
||||
|
||||
if (scrollingV)
|
||||
{
|
||||
float deltaH = io.MouseDelta.y * range.y * sizeOfPixel.y;
|
||||
min.y -= deltaH;
|
||||
max.y -= deltaH;
|
||||
if (!ImGui::IsMouseDown(2))
|
||||
scrollingV = false;
|
||||
}
|
||||
|
||||
draw_list->AddRectFilled(offset, offset + ssize, delegate.GetBackgroundColor());
|
||||
|
||||
auto pointToRange = [&](ImVec2 pt) { return (pt - min) / range; };
|
||||
auto rangeToPoint = [&](ImVec2 pt) { return (pt * range) + min; };
|
||||
|
||||
draw_list->AddLine(ImVec2(-1.f, -min.y / range.y) * viewSize + offset, ImVec2(1.f, -min.y / range.y) * viewSize + offset, 0xFF000000, 1.5f);
|
||||
bool overCurveOrPoint = false;
|
||||
|
||||
int localOverCurve = -1;
|
||||
// make sure highlighted curve is rendered last
|
||||
int* curvesIndex = (int*)_malloca(sizeof(int) * curveCount);
|
||||
for (size_t c = 0; c < curveCount; c++)
|
||||
curvesIndex[c] = int(c);
|
||||
int highLightedCurveIndex = -1;
|
||||
if (overCurve != -1 && curveCount)
|
||||
{
|
||||
ImSwap(curvesIndex[overCurve], curvesIndex[curveCount - 1]);
|
||||
highLightedCurveIndex = overCurve;
|
||||
}
|
||||
|
||||
for (size_t cur = 0; cur < curveCount; cur++)
|
||||
{
|
||||
int c = curvesIndex[cur];
|
||||
if (!delegate.IsVisible(c))
|
||||
continue;
|
||||
const size_t ptCount = delegate.GetPointCount(c);
|
||||
if (ptCount < 1)
|
||||
continue;
|
||||
CurveType curveType = delegate.GetCurveType(c);
|
||||
if (curveType == CurveNone)
|
||||
continue;
|
||||
const ImVec2* pts = delegate.GetPoints(c);
|
||||
uint32_t curveColor = delegate.GetCurveColor(c);
|
||||
if ((c == highLightedCurveIndex && selection.empty() && !selectingQuad) || movingCurve == c)
|
||||
curveColor = 0xFFFFFFFF;
|
||||
|
||||
for (size_t p = 0; p < ptCount - 1; p++)
|
||||
{
|
||||
const ImVec2 p1 = pointToRange(pts[p]);
|
||||
const ImVec2 p2 = pointToRange(pts[p + 1]);
|
||||
|
||||
if (curveType == CurveSmooth || curveType == CurveLinear)
|
||||
{
|
||||
size_t subStepCount = (curveType == CurveSmooth) ? 20 : 2;
|
||||
float step = 1.f / float(subStepCount - 1);
|
||||
for (size_t substep = 0; substep < subStepCount - 1; substep++)
|
||||
{
|
||||
float t = float(substep) * step;
|
||||
|
||||
const ImVec2 sp1 = ImLerp(p1, p2, t);
|
||||
const ImVec2 sp2 = ImLerp(p1, p2, t + step);
|
||||
|
||||
const float rt1 = smoothstep(p1.x, p2.x, sp1.x);
|
||||
const float rt2 = smoothstep(p1.x, p2.x, sp2.x);
|
||||
|
||||
const ImVec2 pos1 = ImVec2(sp1.x, ImLerp(p1.y, p2.y, rt1)) * viewSize + offset;
|
||||
const ImVec2 pos2 = ImVec2(sp2.x, ImLerp(p1.y, p2.y, rt2)) * viewSize + offset;
|
||||
|
||||
if (distance(io.MousePos.x, io.MousePos.y, pos1.x, pos1.y, pos2.x, pos2.y) < 8.f && !scrollingV)
|
||||
{
|
||||
localOverCurve = int(c);
|
||||
overCurve = int(c);
|
||||
overCurveOrPoint = true;
|
||||
}
|
||||
|
||||
draw_list->AddLine(pos1, pos2, curveColor, 1.3f);
|
||||
} // substep
|
||||
}
|
||||
else if (curveType == CurveDiscrete)
|
||||
{
|
||||
ImVec2 dp1 = p1 * viewSize + offset;
|
||||
ImVec2 dp2 = ImVec2(p2.x, p1.y) * viewSize + offset;
|
||||
ImVec2 dp3 = p2 * viewSize + offset;
|
||||
draw_list->AddLine(dp1, dp2, curveColor, 1.3f);
|
||||
draw_list->AddLine(dp2, dp3, curveColor, 1.3f);
|
||||
|
||||
if ((distance(io.MousePos.x, io.MousePos.y, dp1.x, dp1.y, dp3.x, dp1.y) < 8.f ||
|
||||
distance(io.MousePos.x, io.MousePos.y, dp3.x, dp1.y, dp3.x, dp3.y) < 8.f)
|
||||
/*&& localOverCurve == -1*/)
|
||||
{
|
||||
localOverCurve = int(c);
|
||||
overCurve = int(c);
|
||||
overCurveOrPoint = true;
|
||||
}
|
||||
}
|
||||
} // point loop
|
||||
|
||||
for (size_t p = 0; p < ptCount; p++)
|
||||
{
|
||||
const int drawState = DrawPoint(draw_list, pointToRange(pts[p]), viewSize, offset, (selection.find({ int(c), int(p) }) != selection.end() && movingCurve == -1 && !scrollingV));
|
||||
if (drawState && movingCurve == -1 && !selectingQuad)
|
||||
{
|
||||
overCurveOrPoint = true;
|
||||
overSelectedPoint = true;
|
||||
overCurve = -1;
|
||||
if (drawState == 2)
|
||||
{
|
||||
if (!io.KeyShift && selection.find({ int(c), int(p) }) == selection.end())
|
||||
selection.clear();
|
||||
selection.insert({ int(c), int(p) });
|
||||
}
|
||||
}
|
||||
}
|
||||
} // curves loop
|
||||
|
||||
if (localOverCurve == -1)
|
||||
overCurve = -1;
|
||||
|
||||
// move selection
|
||||
static bool pointsMoved = false;
|
||||
static ImVec2 mousePosOrigin;
|
||||
static std::vector<ImVec2> originalPoints;
|
||||
if (overSelectedPoint && io.MouseDown[0])
|
||||
{
|
||||
if ((fabsf(io.MouseDelta.x) > 0.f || fabsf(io.MouseDelta.y) > 0.f) && !selection.empty())
|
||||
{
|
||||
if (!pointsMoved)
|
||||
{
|
||||
delegate.BeginEdit(0);
|
||||
mousePosOrigin = io.MousePos;
|
||||
originalPoints.resize(selection.size());
|
||||
int index = 0;
|
||||
for (auto& sel : selection)
|
||||
{
|
||||
const ImVec2* pts = delegate.GetPoints(sel.curveIndex);
|
||||
originalPoints[index++] = pts[sel.pointIndex];
|
||||
}
|
||||
}
|
||||
pointsMoved = true;
|
||||
ret = 1;
|
||||
auto prevSelection = selection;
|
||||
int originalIndex = 0;
|
||||
for (auto& sel : prevSelection)
|
||||
{
|
||||
const ImVec2 p = rangeToPoint(pointToRange(originalPoints[originalIndex]) + (io.MousePos - mousePosOrigin) * sizeOfPixel);
|
||||
const int newIndex = delegate.EditPoint(sel.curveIndex, sel.pointIndex, p);
|
||||
if (newIndex != sel.pointIndex)
|
||||
{
|
||||
selection.erase(sel);
|
||||
selection.insert({ sel.curveIndex, newIndex });
|
||||
}
|
||||
originalIndex++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (overSelectedPoint && !io.MouseDown[0])
|
||||
{
|
||||
overSelectedPoint = false;
|
||||
if (pointsMoved)
|
||||
{
|
||||
pointsMoved = false;
|
||||
delegate.EndEdit();
|
||||
}
|
||||
}
|
||||
|
||||
// add point
|
||||
if (overCurve != -1 && io.MouseDoubleClicked[0])
|
||||
{
|
||||
const ImVec2 np = rangeToPoint((io.MousePos - offset) / viewSize);
|
||||
delegate.BeginEdit(overCurve);
|
||||
delegate.AddPoint(overCurve, np);
|
||||
delegate.EndEdit();
|
||||
ret = 1;
|
||||
}
|
||||
|
||||
// move curve
|
||||
|
||||
if (movingCurve != -1)
|
||||
{
|
||||
const size_t ptCount = delegate.GetPointCount(movingCurve);
|
||||
const ImVec2* pts = delegate.GetPoints(movingCurve);
|
||||
if (!pointsMoved)
|
||||
{
|
||||
mousePosOrigin = io.MousePos;
|
||||
pointsMoved = true;
|
||||
originalPoints.resize(ptCount);
|
||||
for (size_t index = 0; index < ptCount; index++)
|
||||
{
|
||||
originalPoints[index] = pts[index];
|
||||
}
|
||||
}
|
||||
if (ptCount >= 1)
|
||||
{
|
||||
for (size_t p = 0; p < ptCount; p++)
|
||||
{
|
||||
delegate.EditPoint(movingCurve, int(p), rangeToPoint(pointToRange(originalPoints[p]) + (io.MousePos - mousePosOrigin) * sizeOfPixel));
|
||||
}
|
||||
ret = 1;
|
||||
}
|
||||
if (!io.MouseDown[0])
|
||||
{
|
||||
movingCurve = -1;
|
||||
pointsMoved = false;
|
||||
delegate.EndEdit();
|
||||
}
|
||||
}
|
||||
if (movingCurve == -1 && overCurve != -1 && ImGui::IsMouseClicked(0) && selection.empty() && !selectingQuad)
|
||||
{
|
||||
movingCurve = overCurve;
|
||||
delegate.BeginEdit(overCurve);
|
||||
}
|
||||
|
||||
// quad selection
|
||||
if (selectingQuad)
|
||||
{
|
||||
const ImVec2 bmin = ImMin(quadSelection, io.MousePos);
|
||||
const ImVec2 bmax = ImMax(quadSelection, io.MousePos);
|
||||
draw_list->AddRectFilled(bmin, bmax, 0x40FF0000, 1.f);
|
||||
draw_list->AddRect(bmin, bmax, 0xFFFF0000, 1.f);
|
||||
const ImRect selectionQuad(bmin, bmax);
|
||||
if (!io.MouseDown[0])
|
||||
{
|
||||
if (!io.KeyShift)
|
||||
selection.clear();
|
||||
// select everythnig is quad
|
||||
for (size_t c = 0; c < curveCount; c++)
|
||||
{
|
||||
if (!delegate.IsVisible(c))
|
||||
continue;
|
||||
|
||||
const size_t ptCount = delegate.GetPointCount(c);
|
||||
if (ptCount < 1)
|
||||
continue;
|
||||
|
||||
const ImVec2* pts = delegate.GetPoints(c);
|
||||
for (size_t p = 0; p < ptCount; p++)
|
||||
{
|
||||
const ImVec2 center = pointToRange(pts[p]) * viewSize + offset;
|
||||
if (selectionQuad.Contains(center))
|
||||
selection.insert({ int(c), int(p) });
|
||||
}
|
||||
}
|
||||
// done
|
||||
selectingQuad = false;
|
||||
}
|
||||
}
|
||||
if (!overCurveOrPoint && ImGui::IsMouseClicked(0) && !selectingQuad && movingCurve == -1 && !overSelectedPoint && container.Contains(io.MousePos))
|
||||
{
|
||||
selectingQuad = true;
|
||||
quadSelection = io.MousePos;
|
||||
}
|
||||
if (clippingRect)
|
||||
draw_list->PopClipRect();
|
||||
|
||||
ImGui::EndChild();
|
||||
ImGui::PopStyleVar();
|
||||
ImGui::PopStyleColor(1);
|
||||
|
||||
if (selectedPoints)
|
||||
{
|
||||
selectedPoints->resize(int(selection.size()));
|
||||
int index = 0;
|
||||
for (auto& point : selection)
|
||||
(*selectedPoints)[index++] = point;
|
||||
}
|
||||
_freea(curvesIndex);
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
Vendored
+82
@@ -0,0 +1,82 @@
|
||||
// https://github.com/CedricGuillemet/ImGuizmo
|
||||
// v1.92.5 WIP
|
||||
//
|
||||
// The MIT License(MIT)
|
||||
//
|
||||
// Copyright(c) 2016-2026 Cedric Guillemet and contributors
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files(the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions :
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in all
|
||||
// copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
//
|
||||
#pragma once
|
||||
#include <stdint.h>
|
||||
#include "imgui.h"
|
||||
|
||||
struct ImRect;
|
||||
|
||||
namespace ImCurveEdit
|
||||
{
|
||||
enum CurveType
|
||||
{
|
||||
CurveNone,
|
||||
CurveDiscrete,
|
||||
CurveLinear,
|
||||
CurveSmooth,
|
||||
CurveBezier,
|
||||
};
|
||||
|
||||
struct EditPoint
|
||||
{
|
||||
int curveIndex;
|
||||
int pointIndex;
|
||||
bool operator <(const EditPoint& other) const
|
||||
{
|
||||
if (curveIndex < other.curveIndex)
|
||||
return true;
|
||||
if (curveIndex > other.curveIndex)
|
||||
return false;
|
||||
|
||||
if (pointIndex < other.pointIndex)
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
struct Delegate
|
||||
{
|
||||
bool focused = false;
|
||||
virtual size_t GetCurveCount() = 0;
|
||||
virtual bool IsVisible(size_t /*curveIndex*/) { return true; }
|
||||
virtual CurveType GetCurveType(size_t /*curveIndex*/) const { return CurveLinear; }
|
||||
virtual ImVec2& GetMin() = 0;
|
||||
virtual ImVec2& GetMax() = 0;
|
||||
virtual size_t GetPointCount(size_t curveIndex) = 0;
|
||||
virtual uint32_t GetCurveColor(size_t curveIndex) = 0;
|
||||
virtual ImVec2* GetPoints(size_t curveIndex) = 0;
|
||||
virtual int EditPoint(size_t curveIndex, int pointIndex, ImVec2 value) = 0;
|
||||
virtual void AddPoint(size_t curveIndex, ImVec2 value) = 0;
|
||||
virtual unsigned int GetBackgroundColor() { return 0xFF202020; }
|
||||
// handle undo/redo thru this functions
|
||||
virtual void BeginEdit(int /*index*/) {}
|
||||
virtual void EndEdit() {}
|
||||
|
||||
virtual ~Delegate() = default;
|
||||
};
|
||||
|
||||
int Edit(Delegate& delegate, const ImVec2& size, unsigned int id, const ImRect* clippingRect = NULL, ImVector<EditPoint>* selectedPoints = NULL);
|
||||
}
|
||||
Vendored
+123
@@ -0,0 +1,123 @@
|
||||
// https://github.com/CedricGuillemet/ImGuizmo
|
||||
// v1.92.5 WIP
|
||||
//
|
||||
// The MIT License(MIT)
|
||||
//
|
||||
// Copyright(c) 2016-2026 Cedric Guillemet and contributors
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files(the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions :
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in all
|
||||
// copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
//
|
||||
#include "ImGradient.h"
|
||||
#include "imgui.h"
|
||||
#include "imgui_internal.h"
|
||||
|
||||
namespace ImGradient
|
||||
{
|
||||
#ifndef IMGUI_DEFINE_MATH_OPERATORS
|
||||
static inline ImVec2 operator*(const ImVec2& lhs, const float rhs) { return ImVec2(lhs.x * rhs, lhs.y * rhs); }
|
||||
static inline ImVec2 operator/(const ImVec2& lhs, const float rhs) { return ImVec2(lhs.x / rhs, lhs.y / rhs); }
|
||||
static inline ImVec2 operator+(const ImVec2& lhs, const ImVec2& rhs) { return ImVec2(lhs.x + rhs.x, lhs.y + rhs.y); }
|
||||
static inline ImVec2 operator-(const ImVec2& lhs, const ImVec2& rhs) { return ImVec2(lhs.x - rhs.x, lhs.y - rhs.y); }
|
||||
static inline ImVec2 operator*(const ImVec2& lhs, const ImVec2& rhs) { return ImVec2(lhs.x * rhs.x, lhs.y * rhs.y); }
|
||||
static inline ImVec2 operator/(const ImVec2& lhs, const ImVec2& rhs) { return ImVec2(lhs.x / rhs.x, lhs.y / rhs.y); }
|
||||
#endif
|
||||
|
||||
static int DrawPoint(ImDrawList* draw_list, ImVec4 color, const ImVec2 size, bool editing, ImVec2 pos)
|
||||
{
|
||||
ImGuiIO& io = ImGui::GetIO();
|
||||
|
||||
ImVec2 p1 = ImLerp(pos, ImVec2(pos + ImVec2(size.x - size.y, 0.f)), color.w) + ImVec2(3, 3);
|
||||
ImVec2 p2 = ImLerp(pos + ImVec2(size.y, size.y), ImVec2(pos + size), color.w) - ImVec2(3, 3);
|
||||
ImRect rc(p1, p2);
|
||||
|
||||
color.w = 1.f;
|
||||
draw_list->AddRectFilled(p1, p2, ImColor(color));
|
||||
#if IMGUI_VERSION_NUM < 19276
|
||||
if (editing)
|
||||
draw_list->AddRect(p1, p2, 0xFFFFFFFF, 2.f, 15, 2.5f);
|
||||
else
|
||||
draw_list->AddRect(p1, p2, 0x80FFFFFF, 2.f, 15, 1.25f);
|
||||
#else
|
||||
if (editing)
|
||||
draw_list->AddRect(p1, p2, 0xFFFFFFFF, 2.f, 2.5f);
|
||||
else
|
||||
draw_list->AddRect(p1, p2, 0x80FFFFFF, 2.f, 1.25f);
|
||||
#endif
|
||||
|
||||
if (rc.Contains(io.MousePos))
|
||||
{
|
||||
if (io.MouseClicked[0])
|
||||
return 2;
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
bool Edit(Delegate& delegate, const ImVec2& size, int& selection)
|
||||
{
|
||||
bool ret = false;
|
||||
ImGuiIO& io = ImGui::GetIO();
|
||||
ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2(0, 0));
|
||||
ImGui::BeginChild(137, size, ImGuiChildFlags_FrameStyle);
|
||||
|
||||
ImDrawList* draw_list = ImGui::GetWindowDrawList();
|
||||
const ImVec2 offset = ImGui::GetCursorScreenPos();
|
||||
|
||||
const ImVec4* pts = delegate.GetPoints();
|
||||
static int currentSelection = -1;
|
||||
static int movingPt = -1;
|
||||
if (currentSelection >= int(delegate.GetPointCount()))
|
||||
currentSelection = -1;
|
||||
if (movingPt != -1)
|
||||
{
|
||||
ImVec4 current = pts[movingPt];
|
||||
current.w += io.MouseDelta.x / size.x;
|
||||
current.w = ImClamp(current.w, 0.f, 1.f);
|
||||
delegate.EditPoint(movingPt, current);
|
||||
ret = true;
|
||||
if (!io.MouseDown[0])
|
||||
movingPt = -1;
|
||||
}
|
||||
for (size_t i = 0; i < delegate.GetPointCount(); i++)
|
||||
{
|
||||
int ptSel = DrawPoint(draw_list, pts[i], size, i == currentSelection, offset);
|
||||
if (ptSel == 2)
|
||||
{
|
||||
currentSelection = int(i);
|
||||
ret = true;
|
||||
}
|
||||
if (ptSel == 1 && io.MouseDown[0] && movingPt == -1)
|
||||
{
|
||||
movingPt = int(i);
|
||||
}
|
||||
}
|
||||
ImRect rc(offset, offset + size);
|
||||
if (rc.Contains(io.MousePos) && io.MouseDoubleClicked[0])
|
||||
{
|
||||
float t = (io.MousePos.x - offset.x) / size.x;
|
||||
delegate.AddPoint(delegate.GetPoint(t));
|
||||
ret = true;
|
||||
}
|
||||
ImGui::EndChild();
|
||||
ImGui::PopStyleVar();
|
||||
|
||||
selection = currentSelection;
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
Vendored
+45
@@ -0,0 +1,45 @@
|
||||
// https://github.com/CedricGuillemet/ImGuizmo
|
||||
// v1.92.5 WIP
|
||||
//
|
||||
// The MIT License(MIT)
|
||||
//
|
||||
// Copyright(c) 2016-2026 Cedric Guillemet and contributors
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files(the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions :
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in all
|
||||
// copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
//
|
||||
#pragma once
|
||||
#include <cstddef>
|
||||
|
||||
struct ImVec4;
|
||||
struct ImVec2;
|
||||
|
||||
namespace ImGradient
|
||||
{
|
||||
struct Delegate
|
||||
{
|
||||
virtual size_t GetPointCount() = 0;
|
||||
virtual ImVec4* GetPoints() = 0;
|
||||
virtual int EditPoint(int pointIndex, ImVec4 value) = 0;
|
||||
virtual ImVec4 GetPoint(float t) = 0;
|
||||
virtual void AddPoint(ImVec4 value) = 0;
|
||||
virtual ~Delegate() = default;
|
||||
};
|
||||
|
||||
bool Edit(Delegate& delegate, const ImVec2& size, int& selection);
|
||||
}
|
||||
+3537
File diff suppressed because it is too large.
Load diff
Vendored
+348
@@ -0,0 +1,348 @@
|
||||
// https://github.com/CedricGuillemet/ImGuizmo
|
||||
// v1.92.5 WIP
|
||||
//
|
||||
// The MIT License(MIT)
|
||||
//
|
||||
// Copyright(c) 2016-2026 Cedric Guillemet and contributors
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files(the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions :
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in all
|
||||
// copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
//
|
||||
// -------------------------------------------------------------------------------------------
|
||||
// History :
|
||||
// 2026/05/03 v1.9 release. CMake build system, vcpkg support, many fixes and new features from contributors.
|
||||
// 2019/11/03 View gizmo
|
||||
// 2016/09/11 Behind camera culling. Scaling Delta matrix not multiplied by source matrix scales. local/world rotation and translation fixed. Display message is incorrect (X: ... Y:...) in local mode.
|
||||
// 2016/09/09 Hatched negative axis. Snapping. Documentation update.
|
||||
// 2016/09/04 Axis switch and translation plan autohiding. Scale transform stability improved
|
||||
// 2016/09/01 Mogwai changed to Manipulate. Draw debug cube. Fixed inverted scale. Mixing scale and translation/rotation gives bad results.
|
||||
// 2016/08/31 First version
|
||||
//
|
||||
// -------------------------------------------------------------------------------------------
|
||||
// Future (no order):
|
||||
//
|
||||
// - Multi view
|
||||
// - display rotation/translation/scale infos in local/world space and not only local
|
||||
// - finish local/world matrix application
|
||||
// - OPERATION as bitmask
|
||||
//
|
||||
// -------------------------------------------------------------------------------------------
|
||||
// Example
|
||||
#if 0
|
||||
void EditTransform(const Camera& camera, matrix_t& matrix)
|
||||
{
|
||||
static ImGuizmo::OPERATION mCurrentGizmoOperation(ImGuizmo::ROTATE);
|
||||
static ImGuizmo::MODE mCurrentGizmoMode(ImGuizmo::WORLD);
|
||||
if (ImGui::IsKeyPressed(90))
|
||||
mCurrentGizmoOperation = ImGuizmo::TRANSLATE;
|
||||
if (ImGui::IsKeyPressed(69))
|
||||
mCurrentGizmoOperation = ImGuizmo::ROTATE;
|
||||
if (ImGui::IsKeyPressed(82)) // r Key
|
||||
mCurrentGizmoOperation = ImGuizmo::SCALE;
|
||||
if (ImGui::RadioButton("Translate", mCurrentGizmoOperation == ImGuizmo::TRANSLATE))
|
||||
mCurrentGizmoOperation = ImGuizmo::TRANSLATE;
|
||||
ImGui::SameLine();
|
||||
if (ImGui::RadioButton("Rotate", mCurrentGizmoOperation == ImGuizmo::ROTATE))
|
||||
mCurrentGizmoOperation = ImGuizmo::ROTATE;
|
||||
ImGui::SameLine();
|
||||
if (ImGui::RadioButton("Scale", mCurrentGizmoOperation == ImGuizmo::SCALE))
|
||||
mCurrentGizmoOperation = ImGuizmo::SCALE;
|
||||
float matrixTranslation[3], matrixRotation[3], matrixScale[3];
|
||||
ImGuizmo::DecomposeMatrixToComponents(matrix.m16, matrixTranslation, matrixRotation, matrixScale);
|
||||
ImGui::InputFloat3("Tr", matrixTranslation, 3);
|
||||
ImGui::InputFloat3("Rt", matrixRotation, 3);
|
||||
ImGui::InputFloat3("Sc", matrixScale, 3);
|
||||
ImGuizmo::RecomposeMatrixFromComponents(matrixTranslation, matrixRotation, matrixScale, matrix.m16);
|
||||
|
||||
if (mCurrentGizmoOperation != ImGuizmo::SCALE)
|
||||
{
|
||||
if (ImGui::RadioButton("Local", mCurrentGizmoMode == ImGuizmo::LOCAL))
|
||||
mCurrentGizmoMode = ImGuizmo::LOCAL;
|
||||
ImGui::SameLine();
|
||||
if (ImGui::RadioButton("World", mCurrentGizmoMode == ImGuizmo::WORLD))
|
||||
mCurrentGizmoMode = ImGuizmo::WORLD;
|
||||
}
|
||||
static bool useSnap(false);
|
||||
if (ImGui::IsKeyPressed(83))
|
||||
useSnap = !useSnap;
|
||||
ImGui::Checkbox("", &useSnap);
|
||||
ImGui::SameLine();
|
||||
vec_t snap;
|
||||
switch (mCurrentGizmoOperation)
|
||||
{
|
||||
case ImGuizmo::TRANSLATE:
|
||||
snap = config.mSnapTranslation;
|
||||
ImGui::InputFloat3("Snap", &snap.x);
|
||||
break;
|
||||
case ImGuizmo::ROTATE:
|
||||
snap = config.mSnapRotation;
|
||||
ImGui::InputFloat("Angle Snap", &snap.x);
|
||||
break;
|
||||
case ImGuizmo::SCALE:
|
||||
snap = config.mSnapScale;
|
||||
ImGui::InputFloat("Scale Snap", &snap.x);
|
||||
break;
|
||||
}
|
||||
ImGuiIO& io = ImGui::GetIO();
|
||||
ImGuizmo::SetRect(0, 0, io.DisplaySize.x, io.DisplaySize.y);
|
||||
ImGuizmo::Manipulate(camera.mView.m16, camera.mProjection.m16, mCurrentGizmoOperation, mCurrentGizmoMode, matrix.m16, NULL, useSnap ? &snap.x : NULL);
|
||||
}
|
||||
#endif
|
||||
#pragma once
|
||||
|
||||
#ifdef USE_IMGUI_API
|
||||
#include "imconfig.h"
|
||||
#endif
|
||||
#ifndef IMGUI_API
|
||||
#define IMGUI_API
|
||||
#endif
|
||||
|
||||
#ifndef IMGUIZMO_NAMESPACE
|
||||
#define IMGUIZMO_NAMESPACE ImGuizmo
|
||||
#endif
|
||||
|
||||
struct ImGuiWindow;
|
||||
|
||||
namespace IMGUIZMO_NAMESPACE
|
||||
{
|
||||
// call inside your own window and before Manipulate() in order to draw gizmo to that window.
|
||||
// Or pass a specific ImDrawList to draw to (e.g. ImGui::GetForegroundDrawList()).
|
||||
IMGUI_API void SetDrawlist(ImDrawList* drawlist = nullptr);
|
||||
|
||||
// call BeginFrame right after ImGui_XXXX_NewFrame();
|
||||
IMGUI_API void BeginFrame();
|
||||
|
||||
// this is necessary because when imguizmo is compiled into a dll, and imgui into another
|
||||
// globals are not shared between them.
|
||||
// More details at https://stackoverflow.com/questions/19373061/what-happens-to-global-and-static-variables-in-a-shared-library-when-it-is-dynam
|
||||
// expose method to set imgui context
|
||||
IMGUI_API void SetImGuiContext(ImGuiContext* ctx);
|
||||
|
||||
// return true if mouse cursor is over any gizmo control (axis, plan or screen component)
|
||||
IMGUI_API bool IsOver();
|
||||
|
||||
// return true if mouse IsOver or if the gizmo is in moving state
|
||||
IMGUI_API bool IsUsing();
|
||||
|
||||
// return true if the view gizmo is in moving state
|
||||
IMGUI_API bool IsUsingViewManipulate();
|
||||
// only check if your mouse is over the view manipulator - no matter whether it's active or not
|
||||
IMGUI_API bool IsViewManipulateHovered();
|
||||
|
||||
// return true if any gizmo is in moving state
|
||||
IMGUI_API bool IsUsingAny();
|
||||
|
||||
// enable/disable the gizmo. Stay in the state until next call to Enable.
|
||||
// gizmo is rendered with gray half transparent color when disabled
|
||||
IMGUI_API void Enable(bool enable);
|
||||
|
||||
// helper functions for manualy editing translation/rotation/scale with an input float
|
||||
// translation, rotation and scale float points to 3 floats each
|
||||
// Angles are in degrees (more suitable for human editing)
|
||||
// example:
|
||||
// float matrixTranslation[3], matrixRotation[3], matrixScale[3];
|
||||
// ImGuizmo::DecomposeMatrixToComponents(gizmoMatrix.m16, matrixTranslation, matrixRotation, matrixScale);
|
||||
// ImGui::InputFloat3("Tr", matrixTranslation, 3);
|
||||
// ImGui::InputFloat3("Rt", matrixRotation, 3);
|
||||
// ImGui::InputFloat3("Sc", matrixScale, 3);
|
||||
// ImGuizmo::RecomposeMatrixFromComponents(matrixTranslation, matrixRotation, matrixScale, gizmoMatrix.m16);
|
||||
//
|
||||
// These functions have some numerical stability issues for now. Use with caution.
|
||||
IMGUI_API void DecomposeMatrixToComponents(const float* matrix, float* translation, float* rotation, float* scale);
|
||||
IMGUI_API void RecomposeMatrixFromComponents(const float* translation, const float* rotation, const float* scale, float* matrix);
|
||||
|
||||
IMGUI_API void SetRect(float x, float y, float width, float height);
|
||||
// default is false
|
||||
IMGUI_API void SetOrthographic(bool isOrthographic);
|
||||
|
||||
// Render coordinate system axes (red X, green Y and blue Z). Usefull for debug/tests
|
||||
IMGUI_API void DrawAxes(const float* view, const float* projection, const float* matrices, int matrixCount);
|
||||
// Render a cube with face color corresponding to face normal. Usefull for debug/tests
|
||||
IMGUI_API void DrawCubes(const float* view, const float* projection, const float* matrices, int matrixCount);
|
||||
IMGUI_API void DrawGrid(const float* view, const float* projection, const float* matrix, const float gridSize);
|
||||
// Render grid with customizable major line step and amount of segments between major lines.
|
||||
// NOTE(m.wlasiuk) : calling this function with majorStep = 1.0f and subdivision = 1 is equivalent to DrawGrid in terms of the end result but performs more calculations
|
||||
IMGUI_API void DrawGridCustom(const float* view, const float* projection, const float* matrix, const float gridSize, const float majorStep, const unsigned int subdivision);
|
||||
// Render grid with customizable major line step and amount of segments between major lines and with possibility to set custom colors for major, minor and center lines
|
||||
IMGUI_API void DrawGridCustomColor(const float* view, const float* projection, const float* matrix, const float gridSize, const float majorStep, const unsigned int subdivision, const ImU32 majorCol, const ImU32 minorCol, const ImU32 centerCol);
|
||||
|
||||
// call it when you want a gizmo
|
||||
// Needs view and projection matrices.
|
||||
// matrix parameter is the source matrix (where will be gizmo be drawn) and might be transformed by the function. Return deltaMatrix is optional
|
||||
// translation is applied in world space
|
||||
enum OPERATION
|
||||
{
|
||||
TRANSLATE_X = (1u << 0),
|
||||
TRANSLATE_Y = (1u << 1),
|
||||
TRANSLATE_Z = (1u << 2),
|
||||
ROTATE_X = (1u << 3),
|
||||
ROTATE_Y = (1u << 4),
|
||||
ROTATE_Z = (1u << 5),
|
||||
ROTATE_SCREEN = (1u << 6),
|
||||
SCALE_X = (1u << 7),
|
||||
SCALE_Y = (1u << 8),
|
||||
SCALE_Z = (1u << 9),
|
||||
BOUNDS = (1u << 10),
|
||||
SCALE_XU = (1u << 11),
|
||||
SCALE_YU = (1u << 12),
|
||||
SCALE_ZU = (1u << 13),
|
||||
|
||||
TRANSLATE = TRANSLATE_X | TRANSLATE_Y | TRANSLATE_Z,
|
||||
ROTATE = ROTATE_X | ROTATE_Y | ROTATE_Z | ROTATE_SCREEN,
|
||||
SCALE = SCALE_X | SCALE_Y | SCALE_Z,
|
||||
SCALEU = SCALE_XU | SCALE_YU | SCALE_ZU, // universal
|
||||
UNIVERSAL = TRANSLATE | ROTATE | SCALEU
|
||||
};
|
||||
|
||||
inline OPERATION operator|(OPERATION lhs, OPERATION rhs)
|
||||
{
|
||||
return static_cast<OPERATION>(static_cast<int>(lhs) | static_cast<int>(rhs));
|
||||
}
|
||||
|
||||
enum MODE
|
||||
{
|
||||
LOCAL,
|
||||
WORLD
|
||||
};
|
||||
|
||||
IMGUI_API bool Manipulate(const float* view, const float* projection, OPERATION operation, MODE mode, float* matrix, float* deltaMatrix = NULL, const float* snap = NULL, const float* localBounds = NULL, const float* boundsSnap = NULL);
|
||||
//
|
||||
// Please note that this cubeview is patented by Autodesk : https://patents.google.com/patent/US7782319B2/en
|
||||
// It seems to be a defensive patent in the US. I don't think it will bring troubles using it as
|
||||
// other software are using the same mechanics. But just in case, you are now warned!
|
||||
//
|
||||
IMGUI_API void ViewManipulate(float* view, float length, ImVec2 position, ImVec2 size, ImU32 backgroundColor);
|
||||
|
||||
// use this version if you did not call Manipulate before and you are just using ViewManipulate
|
||||
IMGUI_API void ViewManipulate(float* view, const float* projection, OPERATION operation, MODE mode, float* matrix, float length, ImVec2 position, ImVec2 size, ImU32 backgroundColor);
|
||||
|
||||
IMGUI_API void SetAlternativeWindow(ImGuiWindow* window);
|
||||
|
||||
[[deprecated("Use PushID/PopID instead.")]]
|
||||
IMGUI_API void SetID(int id);
|
||||
|
||||
// ID stack/scopes
|
||||
// Read the FAQ (docs/FAQ.md or http://dearimgui.org/faq) for more details about how ID are handled in dear imgui.
|
||||
// - Those questions are answered and impacted by understanding of the ID stack system:
|
||||
// - "Q: Why is my widget not reacting when I click on it?"
|
||||
// - "Q: How can I have widgets with an empty label?"
|
||||
// - "Q: How can I have multiple widgets with the same label?"
|
||||
// - Short version: ID are hashes of the entire ID stack. If you are creating widgets in a loop you most likely
|
||||
// want to push a unique identifier (e.g. object pointer, loop index) to uniquely differentiate them.
|
||||
// - You can also use the "Label##foobar" syntax within widget label to distinguish them from each others.
|
||||
// - In this header file we use the "label"/"name" terminology to denote a string that will be displayed + used as an ID,
|
||||
// whereas "str_id" denote a string that is only used as an ID and not normally displayed.
|
||||
IMGUI_API void PushID(const char* str_id); // push string into the ID stack (will hash string).
|
||||
IMGUI_API void PushID(const char* str_id_begin, const char* str_id_end); // push string into the ID stack (will hash string).
|
||||
IMGUI_API void PushID(const void* ptr_id); // push pointer into the ID stack (will hash pointer).
|
||||
IMGUI_API void PushID(int int_id); // push integer into the ID stack (will hash integer).
|
||||
IMGUI_API void PopID(); // pop from the ID stack.
|
||||
IMGUI_API ImGuiID GetID(const char* str_id); // calculate unique ID (hash of whole ID stack + given parameter). e.g. if you want to query into ImGuiStorage yourself
|
||||
IMGUI_API ImGuiID GetID(const char* str_id_begin, const char* str_id_end);
|
||||
IMGUI_API ImGuiID GetID(const void* ptr_id);
|
||||
|
||||
// return true if the cursor is over the operation's gizmo
|
||||
IMGUI_API bool IsOver(OPERATION op);
|
||||
IMGUI_API void SetGizmoSizeClipSpace(float value);
|
||||
|
||||
// Handle type used by the translate/rotate/scale gizmos.
|
||||
enum MOVETYPE
|
||||
{
|
||||
MT_NONE,
|
||||
MT_MOVE_X,
|
||||
MT_MOVE_Y,
|
||||
MT_MOVE_Z,
|
||||
MT_MOVE_YZ,
|
||||
MT_MOVE_ZX,
|
||||
MT_MOVE_XY,
|
||||
MT_MOVE_SCREEN,
|
||||
MT_ROTATE_X,
|
||||
MT_ROTATE_Y,
|
||||
MT_ROTATE_Z,
|
||||
MT_ROTATE_SCREEN,
|
||||
MT_SCALE_X,
|
||||
MT_SCALE_Y,
|
||||
MT_SCALE_Z,
|
||||
MT_SCALE_XYZ
|
||||
};
|
||||
|
||||
// Returns which handle is actively being dragged, or MT_NONE.
|
||||
IMGUI_API MOVETYPE GetActiveHandleType();
|
||||
// Returns which handle is currently hovered, or MT_NONE.
|
||||
IMGUI_API MOVETYPE GetHoveredHandleType();
|
||||
// Aliases matching the MOVETYPE enum name.
|
||||
IMGUI_API MOVETYPE GetActiveMoveType();
|
||||
IMGUI_API MOVETYPE GetHoveredMoveType();
|
||||
|
||||
// Allow axis to flip
|
||||
// When true (default), the guizmo axis flip for better visibility
|
||||
// When false, they always stay along the positive world/local axis
|
||||
IMGUI_API void AllowAxisFlip(bool value);
|
||||
|
||||
// Configure the limit where axis are hidden
|
||||
IMGUI_API void SetAxisLimit(float value);
|
||||
// Set an axis mask to permanently hide a given axis (true -> hidden, false -> shown)
|
||||
IMGUI_API void SetAxisMask(bool x, bool y, bool z);
|
||||
// Configure the limit where planes are hiden
|
||||
IMGUI_API void SetPlaneLimit(float value);
|
||||
// from a x,y,z point in space and using Manipulation view/projection matrix, check if mouse is in pixel radius distance of that projected point
|
||||
IMGUI_API bool IsOver(float* position, float pixelRadius);
|
||||
|
||||
// Compute the world-space mouse picking ray from explicit inputs, without reading ImGui IO.
|
||||
// Useful for tests/headless usage. view and projection are column-major float[16] (same
|
||||
// layout as Manipulate). rayOrigin and rayDirection receive a float[3] each.
|
||||
IMGUI_API void ComputeMouseRay(const float* view, const float* projection, const ImVec2& mousePosition, const ImVec2& rectPosition, const ImVec2& rectSize, float* rayOrigin, float* rayDirection);
|
||||
|
||||
enum COLOR
|
||||
{
|
||||
DIRECTION_X, // directionColor[0]
|
||||
DIRECTION_Y, // directionColor[1]
|
||||
DIRECTION_Z, // directionColor[2]
|
||||
PLANE_X, // planeColor[0]
|
||||
PLANE_Y, // planeColor[1]
|
||||
PLANE_Z, // planeColor[2]
|
||||
SELECTION, // selectionColor
|
||||
INACTIVE, // inactiveColor
|
||||
TRANSLATION_LINE, // translationLineColor
|
||||
SCALE_LINE,
|
||||
ROTATION_USING_BORDER,
|
||||
ROTATION_USING_FILL,
|
||||
HATCHED_AXIS_LINES,
|
||||
TEXT,
|
||||
TEXT_SHADOW,
|
||||
COUNT
|
||||
};
|
||||
|
||||
struct Style
|
||||
{
|
||||
IMGUI_API Style();
|
||||
|
||||
float TranslationLineThickness; // Thickness of lines for translation gizmo
|
||||
float TranslationLineArrowSize; // Size of arrow at the end of lines for translation gizmo
|
||||
float RotationLineThickness; // Thickness of lines for rotation gizmo
|
||||
float RotationOuterLineThickness; // Thickness of line surrounding the rotation gizmo
|
||||
float ScaleLineThickness; // Thickness of lines for scale gizmo
|
||||
float ScaleLineCircleSize; // Size of circle at the end of lines for scale gizmo
|
||||
float HatchedAxisLineThickness; // Thickness of hatched axis lines
|
||||
float CenterCircleSize; // Size of circle at the center of the translate/scale gizmo
|
||||
|
||||
ImVec4 Colors[COLOR::COUNT];
|
||||
};
|
||||
|
||||
IMGUI_API Style& GetStyle();
|
||||
}
|
||||
Vendored
+94
@@ -0,0 +1,94 @@
|
||||
#pragma once
|
||||
#include "imgui.h"
|
||||
|
||||
namespace ImLightRig
|
||||
{
|
||||
struct Light
|
||||
{
|
||||
float r, g, b;
|
||||
float intensity;
|
||||
float x, y;
|
||||
};
|
||||
|
||||
inline float Distance(ImVec2 a, ImVec2 b)
|
||||
{
|
||||
ImVec2 dif = b - a;
|
||||
return sqrtf(dif.x * dif.x + dif.y * dif.y);
|
||||
}
|
||||
|
||||
inline int Edit(Light* lights, int lightCount, int selected = -1, ImVec2 size = ImVec2(0,0))
|
||||
{
|
||||
ImDrawList* draw_list = ImGui::GetWindowDrawList();
|
||||
ImGuiIO& io = ImGui::GetIO();
|
||||
const ImVec2 canvas_pos = ImGui::GetCursorScreenPos(); // ImDrawList API uses screen coordinates!
|
||||
const ImVec2 canvas_size = ImGui::GetContentRegionAvail(); // Resize canvas to what's available
|
||||
|
||||
const float sz = (canvas_size.x < canvas_size.y) ? canvas_size.x : canvas_size.y;
|
||||
ImVec2 frameSize(
|
||||
(size.x < 0.001f) ? sz : size.x,
|
||||
(size.y < 0.001f) ? sz : size.y);
|
||||
|
||||
ImGui::InvisibleButton("ImLightRigCanvas", frameSize);
|
||||
draw_list->PushClipRect(canvas_pos, canvas_pos + frameSize);
|
||||
const float controlWidth = frameSize.x;
|
||||
|
||||
draw_list->AddRectFilled(canvas_pos, canvas_pos + frameSize, 0xFF404040);
|
||||
draw_list->AddLine(ImVec2(canvas_pos.x + controlWidth * 0.5f, canvas_pos.y), ImVec2(canvas_pos.x + controlWidth * 0.5f, canvas_pos.y + controlWidth), 0xFFAAAAAA);
|
||||
draw_list->AddLine(ImVec2(canvas_pos.x, canvas_pos.y + controlWidth * 0.5f), ImVec2(canvas_pos.x + controlWidth, canvas_pos.y + controlWidth * 0.5f), 0xFFAAAAAA);
|
||||
draw_list->AddCircle(ImVec2(canvas_pos.x + controlWidth * 0.5f, canvas_pos.y + controlWidth * 0.5f), controlWidth * 0.5f, 0xFFAAAAAA);
|
||||
|
||||
const ImVec2 middle(canvas_pos.x + controlWidth * 0.5f, canvas_pos.y + controlWidth * 0.5f);
|
||||
int res = selected;
|
||||
static int movingLight = -1;
|
||||
for (int i = 0; i < lightCount; i++)
|
||||
{
|
||||
auto& light = lights[i];
|
||||
const auto center(ImVec2(canvas_pos.x + controlWidth * 0.5f + light.x * controlWidth * 0.5f, canvas_pos.y + controlWidth * 0.5f + light.y * controlWidth * 0.5f));
|
||||
ImColor color(light.r, light.g, light.b, 1.f);
|
||||
ImColor intensity(light.intensity, light.intensity, light.intensity, 1.f);
|
||||
const float radius = controlWidth * 0.05f;
|
||||
|
||||
draw_list->AddLine(center, middle, intensity);
|
||||
draw_list->AddCircle(center, radius, intensity, 0, 3.f);
|
||||
draw_list->AddCircleFilled(center, radius, color);
|
||||
|
||||
if (selected == i)
|
||||
{
|
||||
draw_list->AddCircle(center, radius * 1.5f, intensity, 0, 2.f);
|
||||
}
|
||||
|
||||
const float distance = Distance(io.MousePos, center);
|
||||
const bool isOver = distance < radius && movingLight == -1;
|
||||
if (isOver)
|
||||
{
|
||||
draw_list->AddCircle(center, radius * 1.5f, intensity, 0, 4.f);
|
||||
if (io.MouseClicked[0])
|
||||
{
|
||||
res = i;
|
||||
movingLight = i;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!io.MouseDown[0])
|
||||
{
|
||||
movingLight = -1;
|
||||
}
|
||||
if (movingLight >= 0 && movingLight < lightCount && io.MouseDown[0])
|
||||
{
|
||||
auto& light = lights[movingLight];
|
||||
light.x += (io.MouseDelta.x / controlWidth) * 2.f;
|
||||
light.y += (io.MouseDelta.y / controlWidth) * 2.f;
|
||||
|
||||
ImVec2 dir(light.x, light.y);
|
||||
float len = Distance(ImVec2(0.f, 0.f), dir);
|
||||
if (len > 1.f)
|
||||
{
|
||||
dir *= 1.f / len;
|
||||
light.x = dir.x;
|
||||
light.y = dir.y;
|
||||
}
|
||||
}
|
||||
draw_list->PopClipRect();
|
||||
return res;
|
||||
}
|
||||
}
|
||||
Vendored
+695
@@ -0,0 +1,695 @@
|
||||
// https://github.com/CedricGuillemet/ImGuizmo
|
||||
// v1.92.5 WIP
|
||||
//
|
||||
// The MIT License(MIT)
|
||||
//
|
||||
// Copyright(c) 2016-2026 Cedric Guillemet and contributors
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files(the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions :
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in all
|
||||
// copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
//
|
||||
#include "ImSequencer.h"
|
||||
#include "imgui.h"
|
||||
#include "imgui_internal.h"
|
||||
#include <cstdlib>
|
||||
|
||||
namespace ImSequencer
|
||||
{
|
||||
#ifndef IMGUI_DEFINE_MATH_OPERATORS
|
||||
static ImVec2 operator+(const ImVec2& a, const ImVec2& b) {
|
||||
return ImVec2(a.x + b.x, a.y + b.y);
|
||||
}
|
||||
#endif
|
||||
static bool SequencerAddDelButton(ImDrawList* draw_list, ImVec2 pos, bool add = true)
|
||||
{
|
||||
ImGuiIO& io = ImGui::GetIO();
|
||||
ImRect btnRect(pos, ImVec2(pos.x + 16, pos.y + 16));
|
||||
bool overBtn = btnRect.Contains(io.MousePos);
|
||||
bool containedClick = overBtn && btnRect.Contains(io.MouseClickedPos[0]);
|
||||
bool clickedBtn = containedClick && io.MouseReleased[0];
|
||||
int btnColor = overBtn ? 0xAAEAFFAA : 0x77A3B2AA;
|
||||
if (containedClick && io.MouseDownDuration[0] > 0)
|
||||
btnRect.Expand(2.0f);
|
||||
|
||||
float midy = pos.y + 16 / 2 - 0.5f;
|
||||
float midx = pos.x + 16 / 2 - 0.5f;
|
||||
draw_list->AddRect(btnRect.Min, btnRect.Max, btnColor, 4);
|
||||
draw_list->AddLine(ImVec2(btnRect.Min.x + 3, midy), ImVec2(btnRect.Max.x - 3, midy), btnColor, 2);
|
||||
if (add)
|
||||
draw_list->AddLine(ImVec2(midx, btnRect.Min.y + 3), ImVec2(midx, btnRect.Max.y - 3), btnColor, 2);
|
||||
return clickedBtn;
|
||||
}
|
||||
|
||||
bool Sequencer(SequenceInterface* sequence, int* currentFrame, bool* expanded, int* selectedEntry, int* firstFrame, int sequenceOptions)
|
||||
{
|
||||
bool ret = false;
|
||||
ImGuiIO& io = ImGui::GetIO();
|
||||
int cx = (int)(io.MousePos.x);
|
||||
int cy = (int)(io.MousePos.y);
|
||||
static float framePixelWidth = 10.f;
|
||||
static float framePixelWidthTarget = 10.f;
|
||||
int legendWidth = 200;
|
||||
|
||||
static int movingEntry = -1;
|
||||
static int movingPos = -1;
|
||||
static int movingPart = -1;
|
||||
int delEntry = -1;
|
||||
int dupEntry = -1;
|
||||
int ItemHeight = 20;
|
||||
|
||||
bool popupOpened = false;
|
||||
int sequenceCount = sequence->GetItemCount();
|
||||
if (!sequenceCount)
|
||||
return false;
|
||||
ImGui::BeginGroup();
|
||||
|
||||
ImDrawList* draw_list = ImGui::GetWindowDrawList();
|
||||
ImVec2 canvas_pos = ImGui::GetCursorScreenPos(); // ImDrawList API uses screen coordinates!
|
||||
ImVec2 canvas_size = ImGui::GetContentRegionAvail(); // Resize canvas to what's available
|
||||
int firstFrameUsed = firstFrame ? *firstFrame : 0;
|
||||
|
||||
|
||||
int controlHeight = sequenceCount * ItemHeight;
|
||||
for (int i = 0; i < sequenceCount; i++)
|
||||
controlHeight += int(sequence->GetCustomHeight(i));
|
||||
int frameCount = ImMax(sequence->GetFrameMax() - sequence->GetFrameMin(), 1);
|
||||
|
||||
static bool MovingScrollBar = false;
|
||||
static bool MovingCurrentFrame = false;
|
||||
struct CustomDraw
|
||||
{
|
||||
int index;
|
||||
ImRect customRect;
|
||||
ImRect legendRect;
|
||||
ImRect clippingRect;
|
||||
ImRect legendClippingRect;
|
||||
};
|
||||
ImVector<CustomDraw> customDraws;
|
||||
ImVector<CustomDraw> compactCustomDraws;
|
||||
// zoom in/out
|
||||
const int visibleFrameCount = (int)floorf((canvas_size.x - legendWidth) / framePixelWidth);
|
||||
const float barWidthRatio = ImMin(visibleFrameCount / (float)frameCount, 1.f);
|
||||
const float barWidthInPixels = barWidthRatio * (canvas_size.x - legendWidth);
|
||||
|
||||
ImRect regionRect(canvas_pos, canvas_pos + canvas_size);
|
||||
|
||||
static bool panningView = false;
|
||||
static ImVec2 panningViewSource;
|
||||
static int panningViewFrame;
|
||||
if (ImGui::IsWindowFocused() && io.KeyAlt && io.MouseDown[2])
|
||||
{
|
||||
if (!panningView)
|
||||
{
|
||||
panningViewSource = io.MousePos;
|
||||
panningView = true;
|
||||
panningViewFrame = *firstFrame;
|
||||
}
|
||||
*firstFrame = panningViewFrame - int((io.MousePos.x - panningViewSource.x) / framePixelWidth);
|
||||
*firstFrame = ImClamp(*firstFrame, sequence->GetFrameMin(), sequence->GetFrameMax() - visibleFrameCount);
|
||||
}
|
||||
if (panningView && !io.MouseDown[2])
|
||||
{
|
||||
panningView = false;
|
||||
}
|
||||
framePixelWidthTarget = ImClamp(framePixelWidthTarget, 0.1f, 50.f);
|
||||
|
||||
framePixelWidth = ImLerp(framePixelWidth, framePixelWidthTarget, 0.33f);
|
||||
|
||||
frameCount = sequence->GetFrameMax() - sequence->GetFrameMin();
|
||||
if (visibleFrameCount >= frameCount && firstFrame)
|
||||
*firstFrame = sequence->GetFrameMin();
|
||||
|
||||
|
||||
// --
|
||||
if (expanded && !*expanded)
|
||||
{
|
||||
ImGui::InvisibleButton("canvas", ImVec2(canvas_size.x - canvas_pos.x, (float)ItemHeight));
|
||||
draw_list->AddRectFilled(canvas_pos, ImVec2(canvas_size.x + canvas_pos.x, canvas_pos.y + ItemHeight), 0xFF3D3837, 0);
|
||||
char tmps[512];
|
||||
ImFormatString(tmps, IM_ARRAYSIZE(tmps), sequence->GetCollapseFmt(), frameCount, sequenceCount);
|
||||
draw_list->AddText(ImVec2(canvas_pos.x + 26, canvas_pos.y + 2), 0xFFFFFFFF, tmps);
|
||||
}
|
||||
else
|
||||
{
|
||||
bool hasScrollBar(true);
|
||||
/*
|
||||
int framesPixelWidth = int(frameCount * framePixelWidth);
|
||||
if ((framesPixelWidth + legendWidth) >= canvas_size.x)
|
||||
{
|
||||
hasScrollBar = true;
|
||||
}
|
||||
*/
|
||||
// test scroll area
|
||||
ImVec2 headerSize(canvas_size.x, (float)ItemHeight);
|
||||
ImVec2 scrollBarSize(canvas_size.x, 14.f);
|
||||
ImGui::InvisibleButton("topBar", headerSize);
|
||||
draw_list->AddRectFilled(canvas_pos, canvas_pos + headerSize, 0xFFFF0000, 0);
|
||||
ImVec2 childFramePos = ImGui::GetCursorScreenPos();
|
||||
ImVec2 childFrameSize(canvas_size.x, canvas_size.y - 8.f - headerSize.y - (hasScrollBar ? scrollBarSize.y : 0));
|
||||
ImGui::PushStyleColor(ImGuiCol_FrameBg, 0);
|
||||
ImGui::BeginChild(889, childFrameSize, ImGuiChildFlags_FrameStyle);
|
||||
sequence->focused = ImGui::IsWindowFocused();
|
||||
ImGui::InvisibleButton("contentBar", ImVec2(canvas_size.x, float(controlHeight)));
|
||||
const ImVec2 contentMin = ImGui::GetItemRectMin();
|
||||
const ImVec2 contentMax = ImGui::GetItemRectMax();
|
||||
const ImRect contentRect(contentMin, contentMax);
|
||||
const float contentHeight = contentMax.y - contentMin.y;
|
||||
|
||||
// full background
|
||||
draw_list->AddRectFilled(canvas_pos, canvas_pos + canvas_size, 0xFF242424, 0);
|
||||
|
||||
// current frame top
|
||||
ImRect topRect(ImVec2(canvas_pos.x + legendWidth, canvas_pos.y), ImVec2(canvas_pos.x + canvas_size.x, canvas_pos.y + ItemHeight));
|
||||
|
||||
if (!MovingCurrentFrame && !MovingScrollBar && movingEntry == -1 && sequenceOptions & SEQUENCER_CHANGE_FRAME && currentFrame && *currentFrame >= 0 && topRect.Contains(io.MousePos) && io.MouseDown[0])
|
||||
{
|
||||
MovingCurrentFrame = true;
|
||||
}
|
||||
if (MovingCurrentFrame)
|
||||
{
|
||||
if (frameCount)
|
||||
{
|
||||
*currentFrame = (int)((io.MousePos.x - topRect.Min.x) / framePixelWidth) + firstFrameUsed;
|
||||
if (*currentFrame < sequence->GetFrameMin())
|
||||
*currentFrame = sequence->GetFrameMin();
|
||||
if (*currentFrame >= sequence->GetFrameMax())
|
||||
*currentFrame = sequence->GetFrameMax();
|
||||
}
|
||||
if (!io.MouseDown[0])
|
||||
MovingCurrentFrame = false;
|
||||
}
|
||||
|
||||
//header
|
||||
draw_list->AddRectFilled(canvas_pos, ImVec2(canvas_size.x + canvas_pos.x, canvas_pos.y + ItemHeight), 0xFF3D3837, 0);
|
||||
if (sequenceOptions & SEQUENCER_ADD)
|
||||
{
|
||||
if (SequencerAddDelButton(draw_list, ImVec2(canvas_pos.x + legendWidth - ItemHeight, canvas_pos.y + 2), true))
|
||||
ImGui::OpenPopup("addEntry");
|
||||
|
||||
if (ImGui::BeginPopup("addEntry"))
|
||||
{
|
||||
for (int i = 0; i < sequence->GetItemTypeCount(); i++)
|
||||
if (ImGui::Selectable(sequence->GetItemTypeName(i)))
|
||||
{
|
||||
sequence->Add(i);
|
||||
*selectedEntry = sequence->GetItemCount() - 1;
|
||||
}
|
||||
|
||||
ImGui::EndPopup();
|
||||
popupOpened = true;
|
||||
}
|
||||
}
|
||||
|
||||
//header frame number and lines
|
||||
int modFrameCount = 10;
|
||||
int frameStep = 1;
|
||||
while ((modFrameCount * framePixelWidth) < 150)
|
||||
{
|
||||
modFrameCount *= 2;
|
||||
frameStep *= 2;
|
||||
};
|
||||
int halfModFrameCount = modFrameCount / 2;
|
||||
|
||||
auto drawLine = [&](int i, int regionHeight) {
|
||||
bool baseIndex = ((i % modFrameCount) == 0) || (i == sequence->GetFrameMax() || i == sequence->GetFrameMin());
|
||||
bool halfIndex = (i % halfModFrameCount) == 0;
|
||||
int px = (int)canvas_pos.x + int(i * framePixelWidth) + legendWidth - int(firstFrameUsed * framePixelWidth);
|
||||
int tiretStart = baseIndex ? 4 : (halfIndex ? 10 : 14);
|
||||
int tiretEnd = baseIndex ? regionHeight : ItemHeight;
|
||||
|
||||
if (px <= (canvas_size.x + canvas_pos.x) && px >= (canvas_pos.x + legendWidth))
|
||||
{
|
||||
draw_list->AddLine(ImVec2((float)px, canvas_pos.y + (float)tiretStart), ImVec2((float)px, canvas_pos.y + (float)tiretEnd - 1), 0xFF606060, 1);
|
||||
|
||||
draw_list->AddLine(ImVec2((float)px, canvas_pos.y + (float)ItemHeight), ImVec2((float)px, canvas_pos.y + (float)regionHeight - 1), 0x30606060, 1);
|
||||
}
|
||||
|
||||
if (baseIndex && px > (canvas_pos.x + legendWidth))
|
||||
{
|
||||
char tmps[512];
|
||||
ImFormatString(tmps, IM_ARRAYSIZE(tmps), "%d", i);
|
||||
draw_list->AddText(ImVec2((float)px + 3.f, canvas_pos.y), 0xFFBBBBBB, tmps);
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
auto drawLineContent = [&](int i, int /*regionHeight*/) {
|
||||
int px = (int)canvas_pos.x + int(i * framePixelWidth) + legendWidth - int(firstFrameUsed * framePixelWidth);
|
||||
int tiretStart = int(contentMin.y);
|
||||
int tiretEnd = int(contentMax.y);
|
||||
|
||||
if (px <= (canvas_size.x + canvas_pos.x) && px >= (canvas_pos.x + legendWidth))
|
||||
{
|
||||
//draw_list->AddLine(ImVec2((float)px, canvas_pos.y + (float)tiretStart), ImVec2((float)px, canvas_pos.y + (float)tiretEnd - 1), 0xFF606060, 1);
|
||||
|
||||
draw_list->AddLine(ImVec2(float(px), float(tiretStart)), ImVec2(float(px), float(tiretEnd)), 0x30606060, 1);
|
||||
}
|
||||
};
|
||||
for (int i = sequence->GetFrameMin(); i <= sequence->GetFrameMax(); i += frameStep)
|
||||
{
|
||||
drawLine(i, ItemHeight);
|
||||
}
|
||||
drawLine(sequence->GetFrameMin(), ItemHeight);
|
||||
drawLine(sequence->GetFrameMax(), ItemHeight);
|
||||
/*
|
||||
draw_list->AddLine(canvas_pos, ImVec2(canvas_pos.x, canvas_pos.y + controlHeight), 0xFF000000, 1);
|
||||
draw_list->AddLine(ImVec2(canvas_pos.x, canvas_pos.y + ItemHeight), ImVec2(canvas_size.x, canvas_pos.y + ItemHeight), 0xFF000000, 1);
|
||||
*/
|
||||
// clip content
|
||||
|
||||
draw_list->PushClipRect(childFramePos, childFramePos + childFrameSize, true);
|
||||
|
||||
// draw item names in the legend rect on the left
|
||||
size_t customHeight = 0;
|
||||
for (int i = 0; i < sequenceCount; i++)
|
||||
{
|
||||
int type;
|
||||
sequence->Get(i, NULL, NULL, &type, NULL);
|
||||
ImVec2 tpos(contentMin.x + 3, contentMin.y + i * ItemHeight + 2 + customHeight);
|
||||
draw_list->AddText(tpos, 0xFFFFFFFF, sequence->GetItemLabel(i));
|
||||
|
||||
if (sequenceOptions & SEQUENCER_DEL)
|
||||
{
|
||||
if (SequencerAddDelButton(draw_list, ImVec2(contentMin.x + legendWidth - ItemHeight + 2 - 10, tpos.y + 2), false))
|
||||
delEntry = i;
|
||||
|
||||
if (SequencerAddDelButton(draw_list, ImVec2(contentMin.x + legendWidth - ItemHeight - ItemHeight + 2 - 10, tpos.y + 2), true))
|
||||
dupEntry = i;
|
||||
}
|
||||
customHeight += sequence->GetCustomHeight(i);
|
||||
}
|
||||
|
||||
// slots background
|
||||
customHeight = 0;
|
||||
for (int i = 0; i < sequenceCount; i++)
|
||||
{
|
||||
unsigned int col = (i & 1) ? 0xFF3A3636 : 0xFF413D3D;
|
||||
|
||||
size_t localCustomHeight = sequence->GetCustomHeight(i);
|
||||
ImVec2 pos = ImVec2(contentMin.x + legendWidth, contentMin.y + ItemHeight * i + 1 + customHeight);
|
||||
ImVec2 sz = ImVec2(canvas_size.x + canvas_pos.x, pos.y + ItemHeight - 1 + localCustomHeight);
|
||||
if (!popupOpened && cy >= pos.y && cy < pos.y + (ItemHeight + localCustomHeight) && movingEntry == -1 && cx>contentMin.x && cx < contentMin.x + canvas_size.x)
|
||||
{
|
||||
col += 0x80201008;
|
||||
pos.x -= legendWidth;
|
||||
}
|
||||
draw_list->AddRectFilled(pos, sz, col, 0);
|
||||
customHeight += localCustomHeight;
|
||||
}
|
||||
|
||||
draw_list->PushClipRect(childFramePos + ImVec2(float(legendWidth), 0.f), childFramePos + childFrameSize, true);
|
||||
|
||||
// vertical frame lines in content area
|
||||
for (int i = sequence->GetFrameMin(); i <= sequence->GetFrameMax(); i += frameStep)
|
||||
{
|
||||
drawLineContent(i, int(contentHeight));
|
||||
}
|
||||
drawLineContent(sequence->GetFrameMin(), int(contentHeight));
|
||||
drawLineContent(sequence->GetFrameMax(), int(contentHeight));
|
||||
|
||||
// selection
|
||||
bool selected = selectedEntry && (*selectedEntry >= 0);
|
||||
if (selected)
|
||||
{
|
||||
customHeight = 0;
|
||||
for (int i = 0; i < *selectedEntry; i++)
|
||||
customHeight += sequence->GetCustomHeight(i);
|
||||
draw_list->AddRectFilled(ImVec2(contentMin.x, contentMin.y + ItemHeight * *selectedEntry + customHeight), ImVec2(contentMin.x + canvas_size.x, contentMin.y + ItemHeight * (*selectedEntry + 1) + customHeight), 0x801080FF, 1.f);
|
||||
}
|
||||
|
||||
// slots
|
||||
customHeight = 0;
|
||||
for (int i = 0; i < sequenceCount; i++)
|
||||
{
|
||||
int* start, * end;
|
||||
unsigned int color;
|
||||
sequence->Get(i, &start, &end, NULL, &color);
|
||||
size_t localCustomHeight = sequence->GetCustomHeight(i);
|
||||
|
||||
ImVec2 pos = ImVec2(contentMin.x + legendWidth - firstFrameUsed * framePixelWidth, contentMin.y + ItemHeight * i + 1 + customHeight);
|
||||
ImVec2 slotP1(pos.x + *start * framePixelWidth, pos.y + 2);
|
||||
ImVec2 slotP2(pos.x + *end * framePixelWidth + framePixelWidth, pos.y + ItemHeight - 2);
|
||||
ImVec2 slotP3(pos.x + *end * framePixelWidth + framePixelWidth, pos.y + ItemHeight - 2 + localCustomHeight);
|
||||
unsigned int slotColor = color | 0xFF000000;
|
||||
unsigned int slotColorHalf = (color & 0xFFFFFF) | 0x40000000;
|
||||
|
||||
if (slotP1.x <= (canvas_size.x + contentMin.x) && slotP2.x >= (contentMin.x + legendWidth))
|
||||
{
|
||||
draw_list->AddRectFilled(slotP1, slotP3, slotColorHalf, 2);
|
||||
draw_list->AddRectFilled(slotP1, slotP2, slotColor, 2);
|
||||
}
|
||||
if (ImRect(slotP1, slotP2).Contains(io.MousePos) && io.MouseDoubleClicked[0])
|
||||
{
|
||||
sequence->DoubleClick(i);
|
||||
}
|
||||
// Ensure grabbable handles
|
||||
const float max_handle_width = slotP2.x - slotP1.x / 3.0f;
|
||||
const float min_handle_width = ImMin(10.0f, max_handle_width);
|
||||
const float handle_width = ImClamp(framePixelWidth / 2.0f, min_handle_width, max_handle_width);
|
||||
ImRect rects[3] = { ImRect(slotP1, ImVec2(slotP1.x + handle_width, slotP2.y))
|
||||
, ImRect(ImVec2(slotP2.x - handle_width, slotP1.y), slotP2)
|
||||
, ImRect(slotP1, slotP2) };
|
||||
|
||||
const unsigned int quadColor[] = { 0xFFFFFFFF, 0xFFFFFFFF, slotColor + (selected ? 0 : 0x202020) };
|
||||
if (movingEntry == -1 && (sequenceOptions & SEQUENCER_EDIT_STARTEND))// TODOFOCUS && backgroundRect.Contains(io.MousePos))
|
||||
{
|
||||
for (int j = 2; j >= 0; j--)
|
||||
{
|
||||
ImRect& rc = rects[j];
|
||||
if (!rc.Contains(io.MousePos))
|
||||
continue;
|
||||
draw_list->AddRectFilled(rc.Min, rc.Max, quadColor[j], 2);
|
||||
}
|
||||
|
||||
for (int j = 0; j < 3; j++)
|
||||
{
|
||||
ImRect& rc = rects[j];
|
||||
if (!rc.Contains(io.MousePos))
|
||||
continue;
|
||||
if (!ImRect(childFramePos, childFramePos + childFrameSize).Contains(io.MousePos))
|
||||
continue;
|
||||
if (ImGui::IsMouseClicked(0) && !MovingScrollBar && !MovingCurrentFrame)
|
||||
{
|
||||
movingEntry = i;
|
||||
movingPos = cx;
|
||||
movingPart = j + 1;
|
||||
sequence->BeginEdit(movingEntry);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// custom draw
|
||||
if (localCustomHeight > 0)
|
||||
{
|
||||
ImVec2 rp(canvas_pos.x, contentMin.y + ItemHeight * i + 1 + customHeight);
|
||||
ImRect customRect(rp + ImVec2(legendWidth - (firstFrameUsed - sequence->GetFrameMin() - 0.5f) * framePixelWidth, float(ItemHeight)),
|
||||
rp + ImVec2(legendWidth + (sequence->GetFrameMax() - firstFrameUsed - 0.5f + 2.f) * framePixelWidth, float(localCustomHeight + ItemHeight)));
|
||||
ImRect clippingRect(rp + ImVec2(float(legendWidth), float(ItemHeight)), rp + ImVec2(canvas_size.x, float(localCustomHeight + ItemHeight)));
|
||||
|
||||
ImRect legendRect(rp + ImVec2(0.f, float(ItemHeight)), rp + ImVec2(float(legendWidth), float(localCustomHeight)));
|
||||
ImRect legendClippingRect(canvas_pos + ImVec2(0.f, float(ItemHeight)), canvas_pos + ImVec2(float(legendWidth), float(localCustomHeight + ItemHeight)));
|
||||
customDraws.push_back({ i, customRect, legendRect, clippingRect, legendClippingRect });
|
||||
}
|
||||
else
|
||||
{
|
||||
ImVec2 rp(canvas_pos.x, contentMin.y + ItemHeight * i + customHeight);
|
||||
ImRect customRect(rp + ImVec2(legendWidth - (firstFrameUsed - sequence->GetFrameMin() - 0.5f) * framePixelWidth, float(0.f)),
|
||||
rp + ImVec2(legendWidth + (sequence->GetFrameMax() - firstFrameUsed - 0.5f + 2.f) * framePixelWidth, float(ItemHeight)));
|
||||
ImRect clippingRect(rp + ImVec2(float(legendWidth), float(0.f)), rp + ImVec2(canvas_size.x, float(ItemHeight)));
|
||||
|
||||
compactCustomDraws.push_back({ i, customRect, ImRect(), clippingRect, ImRect() });
|
||||
}
|
||||
customHeight += localCustomHeight;
|
||||
}
|
||||
|
||||
|
||||
// moving
|
||||
if (/*backgroundRect.Contains(io.MousePos) && */movingEntry >= 0)
|
||||
{
|
||||
#if IMGUI_VERSION_NUM >= 18723
|
||||
ImGui::SetNextFrameWantCaptureMouse(true);
|
||||
#else
|
||||
ImGui::CaptureMouseFromApp();
|
||||
#endif
|
||||
int diffFrame = int((cx - movingPos) / framePixelWidth);
|
||||
if (std::abs(diffFrame) > 0)
|
||||
{
|
||||
int* start, * end;
|
||||
sequence->Get(movingEntry, &start, &end, NULL, NULL);
|
||||
if (selectedEntry)
|
||||
*selectedEntry = movingEntry;
|
||||
int& l = *start;
|
||||
int& r = *end;
|
||||
if (movingPart & 1)
|
||||
l += diffFrame;
|
||||
if (movingPart & 2)
|
||||
r += diffFrame;
|
||||
if (l < 0)
|
||||
{
|
||||
if (movingPart & 2)
|
||||
r -= l;
|
||||
l = 0;
|
||||
}
|
||||
if (movingPart & 1 && l > r)
|
||||
l = r;
|
||||
if (movingPart & 2 && r < l)
|
||||
r = l;
|
||||
movingPos += int(diffFrame * framePixelWidth);
|
||||
}
|
||||
if (!io.MouseDown[0])
|
||||
{
|
||||
// single select
|
||||
if (!diffFrame && movingPart && selectedEntry)
|
||||
{
|
||||
*selectedEntry = movingEntry;
|
||||
ret = true;
|
||||
}
|
||||
|
||||
movingEntry = -1;
|
||||
sequence->EndEdit();
|
||||
}
|
||||
}
|
||||
|
||||
// cursor
|
||||
if (currentFrame && firstFrame && *currentFrame >= *firstFrame && *currentFrame <= sequence->GetFrameMax())
|
||||
{
|
||||
static const float cursorWidth = 8.f;
|
||||
float cursorOffset = contentMin.x + legendWidth + (*currentFrame - firstFrameUsed) * framePixelWidth + framePixelWidth / 2 - cursorWidth * 0.5f;
|
||||
draw_list->AddLine(ImVec2(cursorOffset, canvas_pos.y), ImVec2(cursorOffset, contentMax.y), 0xA02A2AFF, cursorWidth);
|
||||
char tmps[512];
|
||||
ImFormatString(tmps, IM_ARRAYSIZE(tmps), "%d", *currentFrame);
|
||||
draw_list->AddText(ImVec2(cursorOffset + 10, canvas_pos.y + 2), 0xFF2A2AFF, tmps);
|
||||
}
|
||||
|
||||
draw_list->PopClipRect();
|
||||
draw_list->PopClipRect();
|
||||
|
||||
for (auto& customDraw : customDraws)
|
||||
sequence->CustomDraw(customDraw.index, draw_list, customDraw.customRect, customDraw.legendRect, customDraw.clippingRect, customDraw.legendClippingRect);
|
||||
for (auto& customDraw : compactCustomDraws)
|
||||
sequence->CustomDrawCompact(customDraw.index, draw_list, customDraw.customRect, customDraw.clippingRect);
|
||||
|
||||
// copy paste
|
||||
if (sequenceOptions & SEQUENCER_COPYPASTE)
|
||||
{
|
||||
ImRect rectCopy(ImVec2(contentMin.x + 100, canvas_pos.y + 2)
|
||||
, ImVec2(contentMin.x + 100 + 30, canvas_pos.y + ItemHeight - 2));
|
||||
bool inRectCopy = rectCopy.Contains(io.MousePos);
|
||||
unsigned int copyColor = inRectCopy ? 0xFF1080FF : 0xFF000000;
|
||||
draw_list->AddText(rectCopy.Min, copyColor, "Copy");
|
||||
|
||||
ImRect rectPaste(ImVec2(contentMin.x + 140, canvas_pos.y + 2)
|
||||
, ImVec2(contentMin.x + 140 + 30, canvas_pos.y + ItemHeight - 2));
|
||||
bool inRectPaste = rectPaste.Contains(io.MousePos);
|
||||
unsigned int pasteColor = inRectPaste ? 0xFF1080FF : 0xFF000000;
|
||||
draw_list->AddText(rectPaste.Min, pasteColor, "Paste");
|
||||
|
||||
if (inRectCopy && io.MouseReleased[0])
|
||||
{
|
||||
sequence->Copy();
|
||||
}
|
||||
if (inRectPaste && io.MouseReleased[0])
|
||||
{
|
||||
sequence->Paste();
|
||||
}
|
||||
}
|
||||
//
|
||||
|
||||
ImGui::EndChild();
|
||||
ImGui::PopStyleColor();
|
||||
if (hasScrollBar)
|
||||
{
|
||||
ImGui::InvisibleButton("scrollBar", scrollBarSize);
|
||||
ImVec2 scrollBarMin = ImGui::GetItemRectMin();
|
||||
ImVec2 scrollBarMax = ImGui::GetItemRectMax();
|
||||
|
||||
// ratio = number of frames visible in control / number to total frames
|
||||
|
||||
float startFrameOffset = ((float)(firstFrameUsed - sequence->GetFrameMin()) / (float)frameCount) * (canvas_size.x - legendWidth);
|
||||
ImVec2 scrollBarA(scrollBarMin.x + legendWidth, scrollBarMin.y - 2);
|
||||
ImVec2 scrollBarB(scrollBarMin.x + canvas_size.x, scrollBarMax.y - 1);
|
||||
draw_list->AddRectFilled(scrollBarA, scrollBarB, 0xFF222222, 0);
|
||||
|
||||
ImRect scrollBarRect(scrollBarA, scrollBarB);
|
||||
bool inScrollBar = scrollBarRect.Contains(io.MousePos);
|
||||
|
||||
draw_list->AddRectFilled(scrollBarA, scrollBarB, 0xFF101010, 8);
|
||||
|
||||
|
||||
ImVec2 scrollBarC(scrollBarMin.x + legendWidth + startFrameOffset, scrollBarMin.y);
|
||||
ImVec2 scrollBarD(scrollBarMin.x + legendWidth + barWidthInPixels + startFrameOffset, scrollBarMax.y - 2);
|
||||
draw_list->AddRectFilled(scrollBarC, scrollBarD, (inScrollBar || MovingScrollBar) ? 0xFF606060 : 0xFF505050, 6);
|
||||
|
||||
ImRect barHandleLeft(scrollBarC, ImVec2(scrollBarC.x + 14, scrollBarD.y));
|
||||
ImRect barHandleRight(ImVec2(scrollBarD.x - 14, scrollBarC.y), scrollBarD);
|
||||
|
||||
bool onLeft = barHandleLeft.Contains(io.MousePos);
|
||||
bool onRight = barHandleRight.Contains(io.MousePos);
|
||||
|
||||
static bool sizingRBar = false;
|
||||
static bool sizingLBar = false;
|
||||
|
||||
draw_list->AddRectFilled(barHandleLeft.Min, barHandleLeft.Max, (onLeft || sizingLBar) ? 0xFFAAAAAA : 0xFF666666, 6);
|
||||
draw_list->AddRectFilled(barHandleRight.Min, barHandleRight.Max, (onRight || sizingRBar) ? 0xFFAAAAAA : 0xFF666666, 6);
|
||||
|
||||
ImRect scrollBarThumb(scrollBarC, scrollBarD);
|
||||
static const float MinBarWidth = 44.f;
|
||||
if (sizingRBar)
|
||||
{
|
||||
if (!io.MouseDown[0])
|
||||
{
|
||||
sizingRBar = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
float barNewWidth = ImMax(barWidthInPixels + io.MouseDelta.x, MinBarWidth);
|
||||
float barRatio = barNewWidth / barWidthInPixels;
|
||||
framePixelWidthTarget = framePixelWidth = framePixelWidth / barRatio;
|
||||
int newVisibleFrameCount = int((canvas_size.x - legendWidth) / framePixelWidthTarget);
|
||||
int lastFrame = *firstFrame + newVisibleFrameCount;
|
||||
if (lastFrame > sequence->GetFrameMax())
|
||||
{
|
||||
framePixelWidthTarget = framePixelWidth = (canvas_size.x - legendWidth) / float(sequence->GetFrameMax() - *firstFrame);
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (sizingLBar)
|
||||
{
|
||||
if (!io.MouseDown[0])
|
||||
{
|
||||
sizingLBar = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (fabsf(io.MouseDelta.x) > FLT_EPSILON)
|
||||
{
|
||||
float barNewWidth = ImMax(barWidthInPixels - io.MouseDelta.x, MinBarWidth);
|
||||
float barRatio = barNewWidth / barWidthInPixels;
|
||||
float previousFramePixelWidthTarget = framePixelWidthTarget;
|
||||
framePixelWidthTarget = framePixelWidth = framePixelWidth / barRatio;
|
||||
int newVisibleFrameCount = int(visibleFrameCount / barRatio);
|
||||
int newFirstFrame = *firstFrame + newVisibleFrameCount - visibleFrameCount;
|
||||
newFirstFrame = ImClamp(newFirstFrame, sequence->GetFrameMin(), ImMax(sequence->GetFrameMax() - visibleFrameCount, sequence->GetFrameMin()));
|
||||
if (newFirstFrame == *firstFrame)
|
||||
{
|
||||
framePixelWidth = framePixelWidthTarget = previousFramePixelWidthTarget;
|
||||
}
|
||||
else
|
||||
{
|
||||
*firstFrame = newFirstFrame;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (MovingScrollBar)
|
||||
{
|
||||
if (!io.MouseDown[0])
|
||||
{
|
||||
MovingScrollBar = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
float framesPerPixelInBar = barWidthInPixels / (float)visibleFrameCount;
|
||||
*firstFrame = int((io.MousePos.x - panningViewSource.x) / framesPerPixelInBar) - panningViewFrame;
|
||||
*firstFrame = ImClamp(*firstFrame, sequence->GetFrameMin(), ImMax(sequence->GetFrameMax() - visibleFrameCount, sequence->GetFrameMin()));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (scrollBarThumb.Contains(io.MousePos) && ImGui::IsMouseClicked(0) && firstFrame && !MovingCurrentFrame && movingEntry == -1)
|
||||
{
|
||||
MovingScrollBar = true;
|
||||
panningViewSource = io.MousePos;
|
||||
panningViewFrame = -*firstFrame;
|
||||
}
|
||||
if (!sizingRBar && onRight && ImGui::IsMouseClicked(0))
|
||||
sizingRBar = true;
|
||||
if (!sizingLBar && onLeft && ImGui::IsMouseClicked(0))
|
||||
sizingLBar = true;
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ImGui::EndGroup();
|
||||
|
||||
if (regionRect.Contains(io.MousePos))
|
||||
{
|
||||
bool overCustomDraw = false;
|
||||
for (auto& custom : customDraws)
|
||||
{
|
||||
if (custom.customRect.Contains(io.MousePos))
|
||||
{
|
||||
overCustomDraw = true;
|
||||
}
|
||||
}
|
||||
if (overCustomDraw)
|
||||
{
|
||||
}
|
||||
else
|
||||
{
|
||||
#if 0
|
||||
frameOverCursor = *firstFrame + (int)(visibleFrameCount * ((io.MousePos.x - (float)legendWidth - canvas_pos.x) / (canvas_size.x - legendWidth)));
|
||||
//frameOverCursor = max(min(*firstFrame - visibleFrameCount / 2, frameCount - visibleFrameCount), 0);
|
||||
|
||||
/**firstFrame -= frameOverCursor;
|
||||
*firstFrame *= framePixelWidthTarget / framePixelWidth;
|
||||
*firstFrame += frameOverCursor;*/
|
||||
if (io.MouseWheel < -FLT_EPSILON)
|
||||
{
|
||||
*firstFrame -= frameOverCursor;
|
||||
*firstFrame = int(*firstFrame * 1.1f);
|
||||
framePixelWidthTarget *= 0.9f;
|
||||
*firstFrame += frameOverCursor;
|
||||
}
|
||||
|
||||
if (io.MouseWheel > FLT_EPSILON)
|
||||
{
|
||||
*firstFrame -= frameOverCursor;
|
||||
*firstFrame = int(*firstFrame * 0.9f);
|
||||
framePixelWidthTarget *= 1.1f;
|
||||
*firstFrame += frameOverCursor;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
if (expanded)
|
||||
{
|
||||
if (SequencerAddDelButton(draw_list, ImVec2(canvas_pos.x + 2, canvas_pos.y + 2), !*expanded))
|
||||
*expanded = !*expanded;
|
||||
}
|
||||
|
||||
if (delEntry != -1)
|
||||
{
|
||||
sequence->Del(delEntry);
|
||||
if (selectedEntry && (*selectedEntry == delEntry || *selectedEntry >= sequence->GetItemCount()))
|
||||
*selectedEntry = -1;
|
||||
}
|
||||
|
||||
if (dupEntry != -1)
|
||||
{
|
||||
sequence->Duplicate(dupEntry);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
Vendored
+79
@@ -0,0 +1,79 @@
|
||||
// https://github.com/CedricGuillemet/ImGuizmo
|
||||
// v1.92.5 WIP
|
||||
//
|
||||
// The MIT License(MIT)
|
||||
//
|
||||
// Copyright(c) 2016-2026 Cedric Guillemet and contributors
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files(the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions :
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in all
|
||||
// copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
//
|
||||
#pragma once
|
||||
|
||||
#include <cstddef>
|
||||
|
||||
struct ImDrawList;
|
||||
struct ImRect;
|
||||
namespace ImSequencer
|
||||
{
|
||||
enum SEQUENCER_OPTIONS
|
||||
{
|
||||
SEQUENCER_EDIT_NONE = 0,
|
||||
SEQUENCER_EDIT_STARTEND = 1 << 1,
|
||||
SEQUENCER_CHANGE_FRAME = 1 << 3,
|
||||
SEQUENCER_ADD = 1 << 4,
|
||||
SEQUENCER_DEL = 1 << 5,
|
||||
SEQUENCER_COPYPASTE = 1 << 6,
|
||||
SEQUENCER_EDIT_ALL = SEQUENCER_EDIT_STARTEND | SEQUENCER_CHANGE_FRAME
|
||||
};
|
||||
|
||||
struct SequenceInterface
|
||||
{
|
||||
bool focused = false;
|
||||
virtual int GetFrameMin() const = 0;
|
||||
virtual int GetFrameMax() const = 0;
|
||||
virtual int GetItemCount() const = 0;
|
||||
|
||||
virtual void BeginEdit(int /*index*/) {}
|
||||
virtual void EndEdit() {}
|
||||
virtual int GetItemTypeCount() const { return 0; }
|
||||
virtual const char* GetItemTypeName(int /*typeIndex*/) const { return ""; }
|
||||
virtual const char* GetItemLabel(int /*index*/) const { return ""; }
|
||||
virtual const char* GetCollapseFmt() const { return "%d Frames / %d entries"; }
|
||||
|
||||
virtual void Get(int index, int** start, int** end, int* type, unsigned int* color) = 0;
|
||||
virtual void Add(int /*type*/) {}
|
||||
virtual void Del(int /*index*/) {}
|
||||
virtual void Duplicate(int /*index*/) {}
|
||||
|
||||
virtual void Copy() {}
|
||||
virtual void Paste() {}
|
||||
|
||||
virtual size_t GetCustomHeight(int /*index*/) { return 0; }
|
||||
virtual void DoubleClick(int /*index*/) {}
|
||||
virtual void CustomDraw(int /*index*/, ImDrawList* /*draw_list*/, const ImRect& /*rc*/, const ImRect& /*legendRect*/, const ImRect& /*clippingRect*/, const ImRect& /*legendClippingRect*/) {}
|
||||
virtual void CustomDrawCompact(int /*index*/, ImDrawList* /*draw_list*/, const ImRect& /*rc*/, const ImRect& /*clippingRect*/) {}
|
||||
|
||||
virtual ~SequenceInterface() = default;
|
||||
};
|
||||
|
||||
|
||||
// return true if selection is made
|
||||
bool Sequencer(SequenceInterface* sequence, int* currentFrame, bool* expanded, int* selectedEntry, int* firstFrame, int sequenceOptions);
|
||||
|
||||
}
|
||||
+815
@@ -0,0 +1,815 @@
|
||||
#include "ImVectorEditor.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
|
||||
namespace ImVectorEditor {
|
||||
namespace {
|
||||
|
||||
static constexpr float kMinZoom = 0.0001f;
|
||||
|
||||
static ImVec2 add(const ImVec2& a, const ImVec2& b) {
|
||||
return ImVec2(a.x + b.x, a.y + b.y);
|
||||
}
|
||||
|
||||
static ImVec2 sub(const ImVec2& a, const ImVec2& b) {
|
||||
return ImVec2(a.x - b.x, a.y - b.y);
|
||||
}
|
||||
|
||||
static ImVec2 mul(const ImVec2& a, float s) {
|
||||
return ImVec2(a.x * s, a.y * s);
|
||||
}
|
||||
|
||||
static ImVec2 mul(const ImVec2& a, const ImVec2& b) {
|
||||
return ImVec2(a.x * b.x, a.y * b.y);
|
||||
}
|
||||
|
||||
static float lengthSq(const ImVec2& v) {
|
||||
return v.x * v.x + v.y * v.y;
|
||||
}
|
||||
|
||||
static float length(const ImVec2& v) {
|
||||
return std::sqrt(lengthSq(v));
|
||||
}
|
||||
|
||||
static ImVec2 rotate(const ImVec2& p, float radians) {
|
||||
const float c = std::cos(radians);
|
||||
const float s = std::sin(radians);
|
||||
return ImVec2(p.x * c - p.y * s, p.x * s + p.y * c);
|
||||
}
|
||||
|
||||
static ImVec2 snapToAngleIncrement(const ImVec2& v, float incrementRadians) {
|
||||
const float len = length(v);
|
||||
if (len <= kMinZoom) {
|
||||
return v;
|
||||
}
|
||||
|
||||
const float angle = std::atan2(v.y, v.x);
|
||||
const float snappedAngle = std::round(angle / incrementRadians) * incrementRadians;
|
||||
return ImVec2(std::cos(snappedAngle) * len, std::sin(snappedAngle) * len);
|
||||
}
|
||||
|
||||
static bool isShiftDown() {
|
||||
#if IMGUI_VERSION_NUM >= 18700
|
||||
return ImGui::IsKeyDown(ImGuiMod_Shift) || ImGui::GetIO().KeyShift;
|
||||
#else
|
||||
return ImGui::GetIO().KeyShift;
|
||||
#endif
|
||||
}
|
||||
|
||||
static bool alignHandles(Anchor& anchor) {
|
||||
if (!anchor.hasHandleIn && !anchor.hasHandleOut) {
|
||||
return AddHandles(anchor);
|
||||
}
|
||||
|
||||
if (!anchor.hasHandleIn) {
|
||||
anchor.handleIn = mul(anchor.handleOut, -1.0f);
|
||||
anchor.hasHandleIn = true;
|
||||
anchor.handleMode = HandleMode::Aligned;
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!anchor.hasHandleOut) {
|
||||
anchor.handleOut = mul(anchor.handleIn, -1.0f);
|
||||
anchor.hasHandleOut = true;
|
||||
anchor.handleMode = HandleMode::Aligned;
|
||||
return true;
|
||||
}
|
||||
|
||||
const float inLength = length(anchor.handleIn);
|
||||
const float outLength = length(anchor.handleOut);
|
||||
ImVec2 direction = outLength >= inLength ? anchor.handleOut : mul(anchor.handleIn, -1.0f);
|
||||
float directionLength = length(direction);
|
||||
if (directionLength <= kMinZoom) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const ImVec2 unit = mul(direction, 1.0f / directionLength);
|
||||
anchor.handleOut = mul(unit, outLength);
|
||||
anchor.handleIn = mul(unit, -inLength);
|
||||
anchor.handleMode = HandleMode::Aligned;
|
||||
return true;
|
||||
}
|
||||
|
||||
static ImVec2 alignedOppositeHandle(const ImVec2& handle, float oppositeLength) {
|
||||
const float handleLength = length(handle);
|
||||
if (handleLength <= kMinZoom) {
|
||||
return ImVec2(0.0f, 0.0f);
|
||||
}
|
||||
|
||||
return mul(handle, -oppositeLength / handleLength);
|
||||
}
|
||||
|
||||
static bool hasCurveBetween(const Anchor& a, const Anchor& b) {
|
||||
return a.hasHandleOut || b.hasHandleIn;
|
||||
}
|
||||
|
||||
static void drawControlPoint(ImDrawList* drawList, const ImVec2& center, float radius,
|
||||
ControlPointShape shape, ImU32 color) {
|
||||
switch (shape) {
|
||||
case ControlPointShape::Circle:
|
||||
drawList->AddCircleFilled(center, radius, color);
|
||||
break;
|
||||
case ControlPointShape::Square:
|
||||
drawList->AddRectFilled(sub(center, ImVec2(radius, radius)),
|
||||
add(center, ImVec2(radius, radius)), color);
|
||||
break;
|
||||
case ControlPointShape::Diamond:
|
||||
drawList->AddQuadFilled(ImVec2(center.x, center.y - radius),
|
||||
ImVec2(center.x + radius, center.y),
|
||||
ImVec2(center.x, center.y + radius),
|
||||
ImVec2(center.x - radius, center.y), color);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
static void drawSegment(ImDrawList* drawList, const Config& config,
|
||||
const ImVec2& origin, const Anchor& a, const Anchor& b,
|
||||
ImU32 color, float thickness) {
|
||||
const ImVec2 p0 = add(origin, config.transform.LocalToCanvas(a.position));
|
||||
const ImVec2 p3 = add(origin, config.transform.LocalToCanvas(b.position));
|
||||
|
||||
if (!hasCurveBetween(a, b)) {
|
||||
drawList->AddLine(p0, p3, color, thickness);
|
||||
return;
|
||||
}
|
||||
|
||||
const ImVec2 cp1Local = add(a.position, a.hasHandleOut ? a.handleOut : ImVec2(0.0f, 0.0f));
|
||||
const ImVec2 cp2Local = add(b.position, b.hasHandleIn ? b.handleIn : ImVec2(0.0f, 0.0f));
|
||||
const ImVec2 cp1 = add(origin, config.transform.LocalToCanvas(cp1Local));
|
||||
const ImVec2 cp2 = add(origin, config.transform.LocalToCanvas(cp2Local));
|
||||
drawList->AddBezierCubic(p0, cp1, cp2, p3, color, thickness, 32);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
bool MakeCorner(Anchor& anchor) {
|
||||
const bool changed = DeleteHandles(anchor) || anchor.handleMode != HandleMode::Corner;
|
||||
anchor.handleMode = HandleMode::Corner;
|
||||
return changed;
|
||||
}
|
||||
|
||||
bool MakeAligned(Anchor& anchor) {
|
||||
return alignHandles(anchor);
|
||||
}
|
||||
|
||||
bool MakeMirrored(Anchor& anchor) {
|
||||
if (!anchor.hasHandleIn && !anchor.hasHandleOut) {
|
||||
const bool changed = AddHandles(anchor);
|
||||
anchor.handleMode = HandleMode::Mirrored;
|
||||
return changed;
|
||||
}
|
||||
|
||||
ImVec2 direction = anchor.hasHandleOut ? anchor.handleOut : mul(anchor.handleIn, -1.0f);
|
||||
float directionLength = length(direction);
|
||||
if (directionLength <= kMinZoom) {
|
||||
direction = anchor.hasHandleIn ? mul(anchor.handleIn, -1.0f) : anchor.handleOut;
|
||||
directionLength = length(direction);
|
||||
}
|
||||
if (directionLength <= kMinZoom) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const float inLength = anchor.hasHandleIn ? length(anchor.handleIn) : 0.0f;
|
||||
const float outLength = anchor.hasHandleOut ? length(anchor.handleOut) : 0.0f;
|
||||
const float mirroredLength = std::max(inLength, outLength);
|
||||
const ImVec2 unit = mul(direction, 1.0f / directionLength);
|
||||
anchor.handleOut = mul(unit, mirroredLength);
|
||||
anchor.handleIn = mul(unit, -mirroredLength);
|
||||
anchor.hasHandleIn = true;
|
||||
anchor.hasHandleOut = true;
|
||||
anchor.handleMode = HandleMode::Mirrored;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool AddHandles(Anchor& anchor, float handleLength) {
|
||||
if (anchor.hasHandleIn && anchor.hasHandleOut) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (anchor.hasHandleIn) {
|
||||
const float inLength = std::max(length(anchor.handleIn), handleLength);
|
||||
const ImVec2 out = alignedOppositeHandle(anchor.handleIn, inLength);
|
||||
anchor.handleOut = lengthSq(out) > 0.0f ? out : ImVec2(handleLength, 0.0f);
|
||||
} else if (anchor.hasHandleOut) {
|
||||
const float outLength = std::max(length(anchor.handleOut), handleLength);
|
||||
const ImVec2 in = alignedOppositeHandle(anchor.handleOut, outLength);
|
||||
anchor.handleIn = lengthSq(in) > 0.0f ? in : ImVec2(-handleLength, 0.0f);
|
||||
} else {
|
||||
anchor.handleIn = ImVec2(-handleLength, 0.0f);
|
||||
anchor.handleOut = ImVec2(handleLength, 0.0f);
|
||||
}
|
||||
|
||||
anchor.hasHandleIn = true;
|
||||
anchor.hasHandleOut = true;
|
||||
anchor.handleMode = HandleMode::Aligned;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool DeleteHandles(Anchor& anchor) {
|
||||
if (!anchor.hasHandleIn && !anchor.hasHandleOut &&
|
||||
anchor.handleMode == HandleMode::Corner) {
|
||||
return false;
|
||||
}
|
||||
|
||||
anchor.handleIn = ImVec2(0.0f, 0.0f);
|
||||
anchor.handleOut = ImVec2(0.0f, 0.0f);
|
||||
anchor.hasHandleIn = false;
|
||||
anchor.hasHandleOut = false;
|
||||
anchor.handleMode = HandleMode::Corner;
|
||||
return true;
|
||||
}
|
||||
|
||||
void ReversePath(Path& path) {
|
||||
std::reverse(path.anchors.begin(), path.anchors.end());
|
||||
for (Anchor& anchor : path.anchors) {
|
||||
std::swap(anchor.handleIn, anchor.handleOut);
|
||||
std::swap(anchor.hasHandleIn, anchor.hasHandleOut);
|
||||
}
|
||||
}
|
||||
|
||||
ImVec2 Transform::LocalToCanvas(const ImVec2& local) const {
|
||||
const float safeZoom = std::max(zoom, kMinZoom);
|
||||
ImVec2 p = sub(local, objectPivot);
|
||||
p = mul(p, objectScale);
|
||||
p = rotate(p, objectRotationRadians);
|
||||
p = add(p, objectPivot);
|
||||
p = add(p, objectTranslation);
|
||||
p = mul(p, safeZoom);
|
||||
return add(p, pan);
|
||||
}
|
||||
|
||||
ImVec2 Transform::CanvasToLocal(const ImVec2& canvas) const {
|
||||
const float safeZoom = std::max(zoom, kMinZoom);
|
||||
ImVec2 p = sub(canvas, pan);
|
||||
p = mul(p, 1.0f / safeZoom);
|
||||
p = sub(p, objectTranslation);
|
||||
p = sub(p, objectPivot);
|
||||
p = rotate(p, -objectRotationRadians);
|
||||
const float sx = std::abs(objectScale.x) < kMinZoom ? kMinZoom : objectScale.x;
|
||||
const float sy = std::abs(objectScale.y) < kMinZoom ? kMinZoom : objectScale.y;
|
||||
return add(ImVec2(p.x / sx, p.y / sy), objectPivot);
|
||||
}
|
||||
|
||||
Result Editor::Draw(const char* id, Path& path, const Config& config) {
|
||||
Result result;
|
||||
|
||||
ImVec2 canvasSize = config.canvasSize;
|
||||
if (canvasSize.x <= 0.0f) {
|
||||
canvasSize.x = ImGui::GetContentRegionAvail().x;
|
||||
}
|
||||
if (canvasSize.y <= 0.0f) {
|
||||
canvasSize.y = ImGui::GetContentRegionAvail().y;
|
||||
}
|
||||
canvasSize.x = std::max(canvasSize.x, 64.0f);
|
||||
canvasSize.y = std::max(canvasSize.y, 64.0f);
|
||||
|
||||
const ImVec2 canvasOrigin = ImGui::GetCursorScreenPos();
|
||||
ImGui::InvisibleButton(id, canvasSize,
|
||||
ImGuiButtonFlags_MouseButtonLeft |
|
||||
ImGuiButtonFlags_MouseButtonMiddle |
|
||||
ImGuiButtonFlags_MouseButtonRight);
|
||||
|
||||
hovered_ = ImGui::IsItemHovered();
|
||||
const ImVec2 mouseScreen = ImGui::GetIO().MousePos;
|
||||
const Hit inputHit = HitTest(path, config, canvasOrigin, mouseScreen);
|
||||
|
||||
if (!config.readOnly) {
|
||||
HandleKeyboard(path, config, result);
|
||||
HandleMouse(path, config, canvasOrigin, inputHit, result);
|
||||
}
|
||||
|
||||
const Hit hoveredHit = HitTest(path, config, canvasOrigin, mouseScreen);
|
||||
const bool viewPanning = ImGui::IsMouseDragging(ImGuiMouseButton_Middle, 0.0f) && hovered_;
|
||||
active_ = ImGui::IsItemActive() || dragging_ || boxSelecting_ || viewPanning || penActive_;
|
||||
result.hovered = hovered_;
|
||||
result.active = active_;
|
||||
|
||||
ImDrawList* drawList = ImGui::GetWindowDrawList();
|
||||
const ImVec2 canvasEnd = add(canvasOrigin, canvasSize);
|
||||
drawList->AddRectFilled(canvasOrigin, canvasEnd,
|
||||
config.style.backgroundColor, 3.0f);
|
||||
|
||||
drawList->PushClipRect(canvasOrigin, canvasEnd, true);
|
||||
if (config.showGrid) {
|
||||
DrawGrid(drawList, canvasOrigin, canvasSize, config);
|
||||
}
|
||||
DrawPath(drawList, path, config, canvasOrigin);
|
||||
DrawControls(drawList, path, config, canvasOrigin, hoveredHit);
|
||||
if (boxSelecting_) {
|
||||
drawList->AddRectFilled(boxSelectStartScreen_, boxSelectCurrentScreen_,
|
||||
IM_COL32(112, 174, 255, 35));
|
||||
drawList->AddRect(boxSelectStartScreen_, boxSelectCurrentScreen_,
|
||||
IM_COL32(112, 174, 255, 170));
|
||||
}
|
||||
drawList->PopClipRect();
|
||||
|
||||
drawList->AddRect(canvasOrigin, canvasEnd, IM_COL32(255, 255, 255, hovered_ ? 85 : 35), 3.0f);
|
||||
|
||||
wantsMouseCapture_ = hovered_ || dragging_ || boxSelecting_ || viewPanning || penActive_;
|
||||
wantsKeyboardCapture_ = penActive_ || selectedAnchor_ >= 0 || !selectedAnchors_.empty();
|
||||
result.wantsMouseCapture = wantsMouseCapture_;
|
||||
result.wantsKeyboardCapture = wantsKeyboardCapture_;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
void Editor::ClearSelection() {
|
||||
selectedAnchor_ = -1;
|
||||
selectedPart_ = HitPart::None;
|
||||
selectedAnchors_.clear();
|
||||
dragging_ = false;
|
||||
newAnchorDrag_ = false;
|
||||
boxSelecting_ = false;
|
||||
dragEditStarted_ = false;
|
||||
dragStartAnchorPositions_.clear();
|
||||
}
|
||||
|
||||
bool Editor::IsAnchorSelected(int anchor) const {
|
||||
return std::find(selectedAnchors_.begin(), selectedAnchors_.end(), anchor) != selectedAnchors_.end();
|
||||
}
|
||||
|
||||
void Editor::SelectAnchor(int anchor, HitPart part, bool addToSelection) {
|
||||
if (anchor < 0 || part == HitPart::None) {
|
||||
if (!addToSelection) {
|
||||
ClearSelection();
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
selectedAnchor_ = anchor;
|
||||
selectedPart_ = part;
|
||||
|
||||
if (!addToSelection || part != HitPart::Anchor) {
|
||||
selectedAnchors_.clear();
|
||||
selectedAnchors_.push_back(anchor);
|
||||
return;
|
||||
}
|
||||
|
||||
const auto existing = std::find(selectedAnchors_.begin(), selectedAnchors_.end(), anchor);
|
||||
if (existing == selectedAnchors_.end()) {
|
||||
selectedAnchors_.push_back(anchor);
|
||||
} else {
|
||||
selectedAnchors_.erase(existing);
|
||||
if (selectedAnchors_.empty()) {
|
||||
selectedAnchor_ = -1;
|
||||
selectedPart_ = HitPart::None;
|
||||
} else {
|
||||
selectedAnchor_ = selectedAnchors_.back();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Editor::RemoveSelectedAnchors(Path& path) {
|
||||
if (selectedAnchors_.empty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
std::sort(selectedAnchors_.begin(), selectedAnchors_.end());
|
||||
selectedAnchors_.erase(std::unique(selectedAnchors_.begin(), selectedAnchors_.end()),
|
||||
selectedAnchors_.end());
|
||||
for (auto it = selectedAnchors_.rbegin(); it != selectedAnchors_.rend(); ++it) {
|
||||
if (*it >= 0 && *it < static_cast<int>(path.anchors.size())) {
|
||||
path.anchors.erase(path.anchors.begin() + *it);
|
||||
}
|
||||
}
|
||||
if (path.anchors.size() < 3) {
|
||||
path.closed = false;
|
||||
}
|
||||
ClearSelection();
|
||||
}
|
||||
|
||||
void Editor::BeginAnchorDrag(const Path& path, const ImVec2& mouseLocal) {
|
||||
dragStartLocal_ = mouseLocal;
|
||||
dragStartAnchorPositions_.clear();
|
||||
for (int anchorIndex : selectedAnchors_) {
|
||||
if (anchorIndex >= 0 && anchorIndex < static_cast<int>(path.anchors.size())) {
|
||||
dragStartAnchorPositions_.push_back(path.anchors[anchorIndex].position);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Editor::UpdateBoxSelection(Path& path, const Config& config, const ImVec2& canvasOrigin,
|
||||
bool addToSelection) {
|
||||
const float minX = std::min(boxSelectStartScreen_.x, boxSelectCurrentScreen_.x);
|
||||
const float maxX = std::max(boxSelectStartScreen_.x, boxSelectCurrentScreen_.x);
|
||||
const float minY = std::min(boxSelectStartScreen_.y, boxSelectCurrentScreen_.y);
|
||||
const float maxY = std::max(boxSelectStartScreen_.y, boxSelectCurrentScreen_.y);
|
||||
|
||||
if (!addToSelection) {
|
||||
selectedAnchors_.clear();
|
||||
}
|
||||
for (int i = 0; i < static_cast<int>(path.anchors.size()); ++i) {
|
||||
const ImVec2 anchorScreen = LocalToScreen(config, canvasOrigin, path.anchors[i].position);
|
||||
if (anchorScreen.x >= minX && anchorScreen.x <= maxX &&
|
||||
anchorScreen.y >= minY && anchorScreen.y <= maxY &&
|
||||
!IsAnchorSelected(i)) {
|
||||
selectedAnchors_.push_back(i);
|
||||
}
|
||||
}
|
||||
|
||||
selectedAnchor_ = selectedAnchors_.empty() ? -1 : selectedAnchors_.back();
|
||||
selectedPart_ = selectedAnchor_ >= 0 ? HitPart::Anchor : HitPart::None;
|
||||
}
|
||||
|
||||
void Editor::BeginEdit(const Config& config, EditKind kind, int anchorIndex) {
|
||||
if (editActive_) {
|
||||
return;
|
||||
}
|
||||
|
||||
editActive_ = true;
|
||||
if (config.delegate) {
|
||||
config.delegate->BeginEdit(kind, anchorIndex);
|
||||
}
|
||||
}
|
||||
|
||||
void Editor::EndEdit(const Config& config) {
|
||||
if (!editActive_) {
|
||||
return;
|
||||
}
|
||||
|
||||
editActive_ = false;
|
||||
if (config.delegate) {
|
||||
config.delegate->EndEdit();
|
||||
}
|
||||
}
|
||||
|
||||
Editor::Hit Editor::HitTest(const Path& path, const Config& config,
|
||||
const ImVec2& canvasOrigin,
|
||||
const ImVec2& mouseScreen) const {
|
||||
Hit best;
|
||||
best.distance = config.style.hitRadius;
|
||||
|
||||
for (int i = 0; i < static_cast<int>(path.anchors.size()); ++i) {
|
||||
const Anchor& anchor = path.anchors[i];
|
||||
const ImVec2 anchorScreen = LocalToScreen(config, canvasOrigin, anchor.position);
|
||||
const float anchorDistance = length(sub(mouseScreen, anchorScreen));
|
||||
if (anchorDistance <= best.distance) {
|
||||
best = {i, HitPart::Anchor, anchorDistance};
|
||||
}
|
||||
|
||||
if (anchor.hasHandleIn) {
|
||||
const ImVec2 handleScreen = LocalToScreen(config, canvasOrigin,
|
||||
add(anchor.position, anchor.handleIn));
|
||||
const float handleDistance = length(sub(mouseScreen, handleScreen));
|
||||
if (handleDistance <= best.distance) {
|
||||
best = {i, HitPart::HandleIn, handleDistance};
|
||||
}
|
||||
}
|
||||
|
||||
if (anchor.hasHandleOut) {
|
||||
const ImVec2 handleScreen = LocalToScreen(config, canvasOrigin,
|
||||
add(anchor.position, anchor.handleOut));
|
||||
const float handleDistance = length(sub(mouseScreen, handleScreen));
|
||||
if (handleDistance <= best.distance) {
|
||||
best = {i, HitPart::HandleOut, handleDistance};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return best;
|
||||
}
|
||||
|
||||
ImVec2 Editor::LocalToScreen(const Config& config, const ImVec2& canvasOrigin,
|
||||
const ImVec2& local) const {
|
||||
return add(canvasOrigin, config.transform.LocalToCanvas(local));
|
||||
}
|
||||
|
||||
ImVec2 Editor::ScreenToLocal(const Config& config, const ImVec2& canvasOrigin,
|
||||
const ImVec2& screen) const {
|
||||
return config.transform.CanvasToLocal(sub(screen, canvasOrigin));
|
||||
}
|
||||
|
||||
void Editor::DrawGrid(ImDrawList* drawList, const ImVec2& origin, const ImVec2& size,
|
||||
const Config& config) const {
|
||||
const float step = std::max(4.0f, config.style.gridStep * config.transform.zoom);
|
||||
const ImVec2 end = add(origin, size);
|
||||
const float startX = origin.x + std::fmod(config.transform.pan.x, step);
|
||||
const float startY = origin.y + std::fmod(config.transform.pan.y, step);
|
||||
|
||||
for (float x = startX; x < end.x; x += step) {
|
||||
drawList->AddLine(ImVec2(x, origin.y), ImVec2(x, end.y), config.style.gridColor);
|
||||
}
|
||||
for (float y = startY; y < end.y; y += step) {
|
||||
drawList->AddLine(ImVec2(origin.x, y), ImVec2(end.x, y), config.style.gridColor);
|
||||
}
|
||||
}
|
||||
|
||||
void Editor::DrawPath(ImDrawList* drawList, const Path& path, const Config& config,
|
||||
const ImVec2& canvasOrigin) const {
|
||||
if (path.anchors.empty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (int i = 1; i < static_cast<int>(path.anchors.size()); ++i) {
|
||||
const bool activeClickDragSegment =
|
||||
dragging_ && newAnchorDrag_ && selectedAnchor_ == i;
|
||||
drawSegment(drawList, config, canvasOrigin, path.anchors[i - 1],
|
||||
path.anchors[i],
|
||||
activeClickDragSegment ? config.style.previewColor : config.style.pathColor,
|
||||
config.style.pathThickness);
|
||||
}
|
||||
|
||||
if (path.closed && path.anchors.size() > 2) {
|
||||
drawSegment(drawList, config, canvasOrigin, path.anchors.back(),
|
||||
path.anchors.front(), config.style.pathColor,
|
||||
config.style.pathThickness);
|
||||
}
|
||||
}
|
||||
|
||||
void Editor::DrawControls(ImDrawList* drawList, const Path& path, const Config& config,
|
||||
const ImVec2& canvasOrigin, const Hit& hoveredHit) const {
|
||||
for (int i = 0; i < static_cast<int>(path.anchors.size()); ++i) {
|
||||
const Anchor& anchor = path.anchors[i];
|
||||
const bool selected = IsAnchorSelected(i);
|
||||
const bool closeTarget = i == 0 &&
|
||||
hoveredHit.anchor == 0 &&
|
||||
hoveredHit.part == HitPart::Anchor &&
|
||||
path.anchors.size() >= 3 &&
|
||||
!path.closed;
|
||||
const ImVec2 anchorScreen = LocalToScreen(config, canvasOrigin, anchor.position);
|
||||
|
||||
if (anchor.hasHandleIn) {
|
||||
const ImVec2 handle = LocalToScreen(config, canvasOrigin,
|
||||
add(anchor.position, anchor.handleIn));
|
||||
drawList->AddLine(anchorScreen, handle, config.style.handleLineColor,
|
||||
config.style.handleLineThickness);
|
||||
drawControlPoint(drawList, handle, config.style.handleRadius,
|
||||
config.style.handleShape,
|
||||
selected && selectedPart_ == HitPart::HandleIn
|
||||
? config.style.anchorSelectedColor
|
||||
: config.style.handleColor);
|
||||
}
|
||||
|
||||
if (anchor.hasHandleOut) {
|
||||
const ImVec2 handle = LocalToScreen(config, canvasOrigin,
|
||||
add(anchor.position, anchor.handleOut));
|
||||
drawList->AddLine(anchorScreen, handle, config.style.handleLineColor,
|
||||
config.style.handleLineThickness);
|
||||
drawControlPoint(drawList, handle, config.style.handleRadius,
|
||||
config.style.handleShape,
|
||||
selected && selectedPart_ == HitPart::HandleOut
|
||||
? config.style.anchorSelectedColor
|
||||
: config.style.handleColor);
|
||||
}
|
||||
|
||||
ImU32 anchorColor = config.style.anchorColor;
|
||||
if (selected && selectedPart_ == HitPart::Anchor) {
|
||||
anchorColor = config.style.anchorSelectedColor;
|
||||
} else if (closeTarget) {
|
||||
anchorColor = config.style.closeTargetColor;
|
||||
}
|
||||
|
||||
drawControlPoint(drawList, anchorScreen, config.style.anchorRadius + 1.5f,
|
||||
config.style.anchorShape, IM_COL32(0, 0, 0, 180));
|
||||
drawControlPoint(drawList, anchorScreen, config.style.anchorRadius,
|
||||
config.style.anchorShape, anchorColor);
|
||||
}
|
||||
}
|
||||
|
||||
bool Editor::HandleKeyboard(Path& path, const Config& config, Result& result) {
|
||||
if (!config.allowKeyboardShortcuts || !wantsKeyboardCapture_) {
|
||||
return false;
|
||||
}
|
||||
|
||||
bool changed = false;
|
||||
if (ImGui::IsKeyPressed(ImGuiKey_Escape)) {
|
||||
if (penActive_) {
|
||||
BeginEdit(config, EditKind::PathOperation, -1);
|
||||
path.clear();
|
||||
penActive_ = false;
|
||||
result.canceled = true;
|
||||
changed = true;
|
||||
EndEdit(config);
|
||||
}
|
||||
ClearSelection();
|
||||
} else if (ImGui::IsKeyPressed(ImGuiKey_Enter)) {
|
||||
if (penActive_ && path.anchors.size() >= 2) {
|
||||
penActive_ = false;
|
||||
result.committed = true;
|
||||
}
|
||||
} else if (ImGui::IsKeyPressed(ImGuiKey_Backspace) ||
|
||||
ImGui::IsKeyPressed(ImGuiKey_Delete)) {
|
||||
if (penActive_ && !path.anchors.empty()) {
|
||||
BeginEdit(config, EditKind::DeleteAnchor,
|
||||
static_cast<int>(path.anchors.size()) - 1);
|
||||
path.anchors.pop_back();
|
||||
selectedAnchor_ = path.anchors.empty()
|
||||
? -1
|
||||
: static_cast<int>(path.anchors.size()) - 1;
|
||||
selectedPart_ = selectedAnchor_ >= 0 ? HitPart::Anchor : HitPart::None;
|
||||
if (path.anchors.empty()) {
|
||||
penActive_ = false;
|
||||
}
|
||||
changed = true;
|
||||
EndEdit(config);
|
||||
} else if (!selectedAnchors_.empty()) {
|
||||
BeginEdit(config, EditKind::DeleteAnchor, selectedAnchor_);
|
||||
RemoveSelectedAnchors(path);
|
||||
changed = true;
|
||||
EndEdit(config);
|
||||
} else if (selectedAnchor_ >= 0 &&
|
||||
selectedAnchor_ < static_cast<int>(path.anchors.size())) {
|
||||
BeginEdit(config, EditKind::DeleteAnchor, selectedAnchor_);
|
||||
path.anchors.erase(path.anchors.begin() + selectedAnchor_);
|
||||
selectedAnchor_ = std::min(selectedAnchor_,
|
||||
static_cast<int>(path.anchors.size()) - 1);
|
||||
selectedPart_ = selectedAnchor_ >= 0 ? HitPart::Anchor : HitPart::None;
|
||||
changed = true;
|
||||
EndEdit(config);
|
||||
}
|
||||
}
|
||||
|
||||
result.changed = result.changed || changed;
|
||||
return changed;
|
||||
}
|
||||
|
||||
bool Editor::HandleMouse(Path& path, const Config& config, const ImVec2& canvasOrigin,
|
||||
const Hit& hoveredHit, Result& result) {
|
||||
bool changed = false;
|
||||
const ImGuiIO& io = ImGui::GetIO();
|
||||
const ImVec2 mouseLocal = ScreenToLocal(config, canvasOrigin, io.MousePos);
|
||||
|
||||
if (hovered_ && io.MouseWheel != 0.0f) {
|
||||
result.viewZoomFactor = std::pow(1.1f, io.MouseWheel);
|
||||
result.viewZoomCenterCanvas = sub(io.MousePos, canvasOrigin);
|
||||
}
|
||||
|
||||
if ((hovered_ || active_) && ImGui::IsMouseDragging(ImGuiMouseButton_Middle, 0.0f)) {
|
||||
result.viewPanDelta = io.MouseDelta;
|
||||
return changed;
|
||||
}
|
||||
|
||||
if ((hovered_ || active_) &&
|
||||
ImGui::IsMouseDoubleClicked(ImGuiMouseButton_Left) &&
|
||||
hoveredHit.anchor >= 0 &&
|
||||
hoveredHit.anchor < static_cast<int>(path.anchors.size()) &&
|
||||
hoveredHit.part == HitPart::Anchor) {
|
||||
SelectAnchor(hoveredHit.anchor, HitPart::Anchor, false);
|
||||
dragging_ = false;
|
||||
newAnchorDrag_ = false;
|
||||
const Anchor& beforeAnchor = path.anchors[hoveredHit.anchor];
|
||||
BeginEdit(config,
|
||||
(!beforeAnchor.hasHandleIn && !beforeAnchor.hasHandleOut)
|
||||
? EditKind::AddHandle
|
||||
: EditKind::ChangePointMode,
|
||||
hoveredHit.anchor);
|
||||
changed = MakeAligned(path.anchors[hoveredHit.anchor]) || changed;
|
||||
EndEdit(config);
|
||||
result.changed = result.changed || changed;
|
||||
return changed;
|
||||
}
|
||||
|
||||
if (hovered_ && ImGui::IsMouseClicked(ImGuiMouseButton_Left)) {
|
||||
if (config.tool == Tool::Pen) {
|
||||
if (penActive_ &&
|
||||
hoveredHit.anchor == 0 && hoveredHit.part == HitPart::Anchor &&
|
||||
path.anchors.size() >= 3) {
|
||||
BeginEdit(config, EditKind::PathOperation, 0);
|
||||
path.closed = true;
|
||||
penActive_ = false;
|
||||
result.pathClosed = true;
|
||||
result.committed = true;
|
||||
changed = true;
|
||||
EndEdit(config);
|
||||
} else {
|
||||
BeginEdit(config, EditKind::AddAnchor, static_cast<int>(path.anchors.size()));
|
||||
if (!penActive_ && path.closed) {
|
||||
path.clear();
|
||||
ClearSelection();
|
||||
}
|
||||
Anchor anchor;
|
||||
anchor.position = mouseLocal;
|
||||
path.anchors.push_back(anchor);
|
||||
SelectAnchor(static_cast<int>(path.anchors.size()) - 1, HitPart::Anchor, false);
|
||||
penActive_ = true;
|
||||
dragging_ = true;
|
||||
newAnchorDrag_ = true;
|
||||
dragEditStarted_ = true;
|
||||
dragStartLocal_ = mouseLocal;
|
||||
changed = true;
|
||||
}
|
||||
} else {
|
||||
const bool additiveSelection = isShiftDown() && hoveredHit.part == HitPart::Anchor;
|
||||
if (hoveredHit.part != HitPart::None) {
|
||||
SelectAnchor(hoveredHit.anchor, hoveredHit.part, additiveSelection);
|
||||
dragging_ = selectedPart_ != HitPart::None;
|
||||
dragEditStarted_ = false;
|
||||
if (dragging_ && selectedPart_ == HitPart::Anchor) {
|
||||
BeginAnchorDrag(path, mouseLocal);
|
||||
}
|
||||
} else {
|
||||
if (!isShiftDown()) {
|
||||
ClearSelection();
|
||||
}
|
||||
boxSelecting_ = true;
|
||||
boxSelectStartScreen_ = io.MousePos;
|
||||
boxSelectCurrentScreen_ = io.MousePos;
|
||||
dragging_ = false;
|
||||
}
|
||||
newAnchorDrag_ = false;
|
||||
}
|
||||
}
|
||||
|
||||
if (boxSelecting_) {
|
||||
boxSelectCurrentScreen_ = io.MousePos;
|
||||
if (ImGui::IsMouseDragging(ImGuiMouseButton_Left, 0.0f)) {
|
||||
UpdateBoxSelection(path, config, canvasOrigin, isShiftDown());
|
||||
}
|
||||
if (ImGui::IsMouseReleased(ImGuiMouseButton_Left)) {
|
||||
UpdateBoxSelection(path, config, canvasOrigin, isShiftDown());
|
||||
boxSelecting_ = false;
|
||||
}
|
||||
}
|
||||
|
||||
if (dragging_ && ImGui::IsMouseDragging(ImGuiMouseButton_Left, 0.0f) &&
|
||||
selectedAnchor_ >= 0 && selectedAnchor_ < static_cast<int>(path.anchors.size())) {
|
||||
Anchor& anchor = path.anchors[selectedAnchor_];
|
||||
if (selectedPart_ == HitPart::Anchor) {
|
||||
if (newAnchorDrag_) {
|
||||
ImVec2 delta = sub(mouseLocal, dragStartLocal_);
|
||||
if (isShiftDown()) {
|
||||
delta = snapToAngleIncrement(delta, 3.14159265358979323846f / 4.0f);
|
||||
}
|
||||
if (lengthSq(delta) > 9.0f) {
|
||||
anchor.handleOut = delta;
|
||||
anchor.handleIn = mul(delta, -1.0f);
|
||||
anchor.hasHandleOut = true;
|
||||
anchor.hasHandleIn = true;
|
||||
anchor.handleMode = HandleMode::Mirrored;
|
||||
changed = true;
|
||||
}
|
||||
} else if (IsAnchorSelected(selectedAnchor_) &&
|
||||
dragStartAnchorPositions_.size() == selectedAnchors_.size()) {
|
||||
if (!dragEditStarted_) {
|
||||
BeginEdit(config, EditKind::MoveAnchor, selectedAnchor_);
|
||||
dragEditStarted_ = true;
|
||||
}
|
||||
const ImVec2 delta = sub(mouseLocal, dragStartLocal_);
|
||||
for (int i = 0; i < static_cast<int>(selectedAnchors_.size()); ++i) {
|
||||
const int anchorIndex = selectedAnchors_[i];
|
||||
if (anchorIndex >= 0 && anchorIndex < static_cast<int>(path.anchors.size())) {
|
||||
path.anchors[anchorIndex].position = add(dragStartAnchorPositions_[i], delta);
|
||||
}
|
||||
}
|
||||
changed = true;
|
||||
} else {
|
||||
if (!dragEditStarted_) {
|
||||
BeginEdit(config, EditKind::MoveAnchor, selectedAnchor_);
|
||||
dragEditStarted_ = true;
|
||||
}
|
||||
anchor.position = mouseLocal;
|
||||
changed = true;
|
||||
}
|
||||
} else if (selectedPart_ == HitPart::HandleIn) {
|
||||
if (!dragEditStarted_) {
|
||||
BeginEdit(config, EditKind::MoveHandle, selectedAnchor_);
|
||||
dragEditStarted_ = true;
|
||||
}
|
||||
anchor.handleIn = sub(mouseLocal, anchor.position);
|
||||
anchor.hasHandleIn = true;
|
||||
if (anchor.handleMode == HandleMode::Mirrored) {
|
||||
anchor.handleOut = mul(anchor.handleIn, -1.0f);
|
||||
anchor.hasHandleOut = true;
|
||||
} else if (anchor.handleMode == HandleMode::Aligned) {
|
||||
const float outLength = anchor.hasHandleOut ? length(anchor.handleOut) : length(anchor.handleIn);
|
||||
anchor.handleOut = alignedOppositeHandle(anchor.handleIn, outLength);
|
||||
anchor.hasHandleOut = true;
|
||||
} else {
|
||||
anchor.handleMode = HandleMode::Free;
|
||||
}
|
||||
changed = true;
|
||||
} else if (selectedPart_ == HitPart::HandleOut) {
|
||||
if (!dragEditStarted_) {
|
||||
BeginEdit(config, EditKind::MoveHandle, selectedAnchor_);
|
||||
dragEditStarted_ = true;
|
||||
}
|
||||
anchor.handleOut = sub(mouseLocal, anchor.position);
|
||||
anchor.hasHandleOut = true;
|
||||
if (anchor.handleMode == HandleMode::Mirrored) {
|
||||
anchor.handleIn = mul(anchor.handleOut, -1.0f);
|
||||
anchor.hasHandleIn = true;
|
||||
} else if (anchor.handleMode == HandleMode::Aligned) {
|
||||
const float inLength = anchor.hasHandleIn ? length(anchor.handleIn) : length(anchor.handleOut);
|
||||
anchor.handleIn = alignedOppositeHandle(anchor.handleOut, inLength);
|
||||
anchor.hasHandleIn = true;
|
||||
} else {
|
||||
anchor.handleMode = HandleMode::Free;
|
||||
}
|
||||
changed = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (dragging_ && ImGui::IsMouseReleased(ImGuiMouseButton_Left)) {
|
||||
dragging_ = false;
|
||||
newAnchorDrag_ = false;
|
||||
dragEditStarted_ = false;
|
||||
dragStartAnchorPositions_.clear();
|
||||
EndEdit(config);
|
||||
}
|
||||
|
||||
result.changed = result.changed || changed;
|
||||
return changed;
|
||||
}
|
||||
|
||||
} // namespace ImVectorEditor
|
||||
Vendored
+205
@@ -0,0 +1,205 @@
|
||||
#pragma once
|
||||
|
||||
#include "imgui.h"
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace ImVectorEditor {
|
||||
|
||||
enum class Tool {
|
||||
Select,
|
||||
Pen,
|
||||
};
|
||||
|
||||
enum class HitPart {
|
||||
None,
|
||||
Anchor,
|
||||
HandleIn,
|
||||
HandleOut,
|
||||
};
|
||||
|
||||
enum class HandleMode {
|
||||
Corner,
|
||||
Mirrored,
|
||||
Aligned,
|
||||
Free,
|
||||
};
|
||||
|
||||
enum class ControlPointShape {
|
||||
Circle,
|
||||
Square,
|
||||
Diamond,
|
||||
};
|
||||
|
||||
enum class EditKind {
|
||||
MoveAnchor,
|
||||
MoveHandle,
|
||||
AddAnchor,
|
||||
DeleteAnchor,
|
||||
AddHandle,
|
||||
DeleteHandle,
|
||||
ChangePointMode,
|
||||
PathOperation,
|
||||
};
|
||||
|
||||
struct Anchor {
|
||||
ImVec2 position = ImVec2(0.0f, 0.0f);
|
||||
ImVec2 handleIn = ImVec2(0.0f, 0.0f);
|
||||
ImVec2 handleOut = ImVec2(0.0f, 0.0f);
|
||||
bool hasHandleIn = false;
|
||||
bool hasHandleOut = false;
|
||||
HandleMode handleMode = HandleMode::Corner;
|
||||
};
|
||||
|
||||
struct Path {
|
||||
std::vector<Anchor> anchors;
|
||||
bool closed = false;
|
||||
|
||||
bool empty() const { return anchors.empty(); }
|
||||
void clear() {
|
||||
anchors.clear();
|
||||
closed = false;
|
||||
}
|
||||
};
|
||||
|
||||
bool MakeCorner(Anchor& anchor);
|
||||
bool MakeAligned(Anchor& anchor);
|
||||
bool MakeMirrored(Anchor& anchor);
|
||||
bool AddHandles(Anchor& anchor, float length = 50.0f);
|
||||
bool DeleteHandles(Anchor& anchor);
|
||||
void ReversePath(Path& path);
|
||||
|
||||
struct Delegate {
|
||||
virtual void BeginEdit(EditKind /*kind*/, int /*anchorIndex*/) {}
|
||||
virtual void EndEdit() {}
|
||||
virtual ~Delegate() = default;
|
||||
};
|
||||
|
||||
// Host-provided transform state. Points are stored in path-local coordinates.
|
||||
// The editor composes object transform first, then canvas pan/zoom.
|
||||
struct Transform {
|
||||
ImVec2 pan = ImVec2(0.0f, 0.0f);
|
||||
float zoom = 1.0f;
|
||||
ImVec2 objectPivot = ImVec2(0.0f, 0.0f);
|
||||
ImVec2 objectTranslation = ImVec2(0.0f, 0.0f);
|
||||
float objectRotationRadians = 0.0f;
|
||||
ImVec2 objectScale = ImVec2(1.0f, 1.0f);
|
||||
|
||||
ImVec2 LocalToCanvas(const ImVec2& local) const;
|
||||
ImVec2 CanvasToLocal(const ImVec2& canvas) const;
|
||||
};
|
||||
|
||||
struct Style {
|
||||
ImU32 backgroundColor = IM_COL32(31, 34, 42, 255);
|
||||
ImU32 gridColor = IM_COL32(255, 255, 255, 22);
|
||||
ImU32 pathColor = IM_COL32(245, 245, 245, 255);
|
||||
ImU32 previewColor = IM_COL32(245, 245, 245, 120);
|
||||
ImU32 anchorColor = IM_COL32(255, 255, 255, 255);
|
||||
ImU32 anchorSelectedColor = IM_COL32(255, 190, 84, 255);
|
||||
ImU32 handleColor = IM_COL32(112, 174, 255, 255);
|
||||
ImU32 handleLineColor = IM_COL32(112, 174, 255, 130);
|
||||
ImU32 closeTargetColor = IM_COL32(120, 240, 160, 255);
|
||||
float pathThickness = 2.0f;
|
||||
float handleLineThickness = 1.0f;
|
||||
float anchorRadius = 5.0f;
|
||||
float handleRadius = 3.5f;
|
||||
ControlPointShape anchorShape = ControlPointShape::Square;
|
||||
ControlPointShape handleShape = ControlPointShape::Circle;
|
||||
float hitRadius = 9.0f;
|
||||
float gridStep = 32.0f;
|
||||
};
|
||||
|
||||
struct Config {
|
||||
ImVec2 canvasSize = ImVec2(0.0f, 360.0f);
|
||||
Tool tool = Tool::Pen;
|
||||
Transform transform;
|
||||
Style style;
|
||||
Delegate* delegate = nullptr;
|
||||
bool showGrid = true;
|
||||
bool allowKeyboardShortcuts = true;
|
||||
bool readOnly = false;
|
||||
};
|
||||
|
||||
struct Result {
|
||||
bool changed = false;
|
||||
bool committed = false;
|
||||
bool canceled = false;
|
||||
bool pathClosed = false;
|
||||
bool hovered = false;
|
||||
bool active = false;
|
||||
bool wantsMouseCapture = false;
|
||||
bool wantsKeyboardCapture = false;
|
||||
ImVec2 viewPanDelta = ImVec2(0.0f, 0.0f);
|
||||
ImVec2 viewZoomCenterCanvas = ImVec2(0.0f, 0.0f);
|
||||
float viewZoomFactor = 1.0f;
|
||||
};
|
||||
|
||||
class Editor {
|
||||
public:
|
||||
Result Draw(const char* id, Path& path, const Config& config = Config());
|
||||
|
||||
bool IsHovered() const { return hovered_; }
|
||||
bool IsActive() const { return active_; }
|
||||
bool WantsMouseCapture() const { return wantsMouseCapture_; }
|
||||
bool WantsKeyboardCapture() const { return wantsKeyboardCapture_; }
|
||||
|
||||
void ClearSelection();
|
||||
int GetSelectedAnchor() const { return selectedAnchor_; }
|
||||
int GetSelectedAnchorCount() const { return static_cast<int>(selectedAnchors_.size()); }
|
||||
HitPart GetSelectedPart() const { return selectedPart_; }
|
||||
|
||||
private:
|
||||
struct Hit {
|
||||
int anchor = -1;
|
||||
HitPart part = HitPart::None;
|
||||
float distance = 0.0f;
|
||||
};
|
||||
|
||||
Hit HitTest(const Path& path, const Config& config, const ImVec2& canvasOrigin,
|
||||
const ImVec2& mouseScreen) const;
|
||||
ImVec2 LocalToScreen(const Config& config, const ImVec2& canvasOrigin,
|
||||
const ImVec2& local) const;
|
||||
ImVec2 ScreenToLocal(const Config& config, const ImVec2& canvasOrigin,
|
||||
const ImVec2& screen) const;
|
||||
|
||||
void DrawGrid(ImDrawList* drawList, const ImVec2& origin, const ImVec2& size,
|
||||
const Config& config) const;
|
||||
void DrawPath(ImDrawList* drawList, const Path& path, const Config& config,
|
||||
const ImVec2& canvasOrigin) const;
|
||||
void DrawControls(ImDrawList* drawList, const Path& path, const Config& config,
|
||||
const ImVec2& canvasOrigin, const Hit& hoveredHit) const;
|
||||
|
||||
bool HandleKeyboard(Path& path, const Config& config, Result& result);
|
||||
bool HandleMouse(Path& path, const Config& config, const ImVec2& canvasOrigin,
|
||||
const Hit& hoveredHit, Result& result);
|
||||
bool IsAnchorSelected(int anchor) const;
|
||||
void SelectAnchor(int anchor, HitPart part, bool addToSelection);
|
||||
void RemoveSelectedAnchors(Path& path);
|
||||
void BeginAnchorDrag(const Path& path, const ImVec2& mouseLocal);
|
||||
void UpdateBoxSelection(Path& path, const Config& config, const ImVec2& canvasOrigin,
|
||||
bool addToSelection);
|
||||
void BeginEdit(const Config& config, EditKind kind, int anchorIndex);
|
||||
void EndEdit(const Config& config);
|
||||
|
||||
int selectedAnchor_ = -1;
|
||||
HitPart selectedPart_ = HitPart::None;
|
||||
std::vector<int> selectedAnchors_;
|
||||
bool dragging_ = false;
|
||||
bool penActive_ = false;
|
||||
bool newAnchorDrag_ = false;
|
||||
bool boxSelecting_ = false;
|
||||
bool editActive_ = false;
|
||||
bool dragEditStarted_ = false;
|
||||
ImVec2 dragStartLocal_ = ImVec2(0.0f, 0.0f);
|
||||
ImVec2 boxSelectStartScreen_ = ImVec2(0.0f, 0.0f);
|
||||
ImVec2 boxSelectCurrentScreen_ = ImVec2(0.0f, 0.0f);
|
||||
std::vector<ImVec2> dragStartAnchorPositions_;
|
||||
|
||||
bool hovered_ = false;
|
||||
bool active_ = false;
|
||||
bool wantsMouseCapture_ = false;
|
||||
bool wantsKeyboardCapture_ = false;
|
||||
};
|
||||
|
||||
} // namespace ImVectorEditor
|
||||
Vendored
+245
@@ -0,0 +1,245 @@
|
||||
// https://github.com/CedricGuillemet/ImGuizmo
|
||||
// v1.92.5 WIP
|
||||
//
|
||||
// The MIT License(MIT)
|
||||
//
|
||||
// Copyright(c) 2016-2026 Cedric Guillemet and contributors
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files(the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions :
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in all
|
||||
// copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
//
|
||||
#pragma once
|
||||
|
||||
namespace ImZoomSlider
|
||||
{
|
||||
typedef int ImGuiZoomSliderFlags;
|
||||
enum ImGuiPopupFlags_
|
||||
{
|
||||
ImGuiZoomSliderFlags_None = 0,
|
||||
ImGuiZoomSliderFlags_Vertical = 1,
|
||||
ImGuiZoomSliderFlags_NoAnchors = 2,
|
||||
ImGuiZoomSliderFlags_NoMiddleCarets = 4,
|
||||
ImGuiZoomSliderFlags_NoWheel = 8,
|
||||
};
|
||||
|
||||
template<typename T> bool ImZoomSlider(const T lower, const T higher, T& viewLower, T& viewHigher, float wheelRatio = 0.01f, ImGuiZoomSliderFlags flags = ImGuiZoomSliderFlags_None)
|
||||
{
|
||||
bool interacted = false;
|
||||
ImGuiIO& io = ImGui::GetIO();
|
||||
ImDrawList* draw_list = ImGui::GetWindowDrawList();
|
||||
|
||||
static const float handleSize = 12;
|
||||
static const float roundRadius = 3.f;
|
||||
static const char* controlName = "ImZoomSlider";
|
||||
|
||||
static bool movingScrollBarSvg = false;
|
||||
static bool sizingRBarSvg = false;
|
||||
static bool sizingLBarSvg = false;
|
||||
static ImGuiID editingId = (ImGuiID)-1;
|
||||
static float scrollingSource = 0.f;
|
||||
static float saveViewLower;
|
||||
static float saveViewHigher;
|
||||
|
||||
const bool isVertical = flags & ImGuiZoomSliderFlags_Vertical;
|
||||
const ImVec2 canvasPos = ImGui::GetCursorScreenPos();
|
||||
const ImVec2 canvasSize = ImGui::GetContentRegionAvail();
|
||||
const float canvasSizeLength = isVertical ? ImGui::GetItemRectSize().y : canvasSize.x;
|
||||
const ImVec2 scrollBarSize = isVertical ? ImVec2(14.f, canvasSizeLength) : ImVec2(canvasSizeLength, 14.f);
|
||||
|
||||
ImGui::InvisibleButton(controlName, scrollBarSize);
|
||||
const ImGuiID currentId = ImGui::GetID(controlName);
|
||||
|
||||
const bool usingEditingId = currentId == editingId;
|
||||
const bool canUseControl = usingEditingId || editingId == -1;
|
||||
const bool movingScrollBar = usingEditingId ? movingScrollBarSvg : false;
|
||||
const bool sizingRBar = usingEditingId ? sizingRBarSvg : false;
|
||||
const bool sizingLBar = usingEditingId ? sizingLBarSvg : false;
|
||||
const int componentIndex = isVertical ? 1 : 0;
|
||||
const ImVec2 scrollBarMin = ImGui::GetItemRectMin();
|
||||
const ImVec2 scrollBarMax = ImGui::GetItemRectMax();
|
||||
const ImVec2 scrollBarA = ImVec2(scrollBarMin.x, scrollBarMin.y) - (isVertical ? ImVec2(2,0) : ImVec2(0,2));
|
||||
const ImVec2 scrollBarB = isVertical ? ImVec2(scrollBarMax.x - 1.f, scrollBarMin.y + canvasSizeLength) : ImVec2(scrollBarMin.x + canvasSizeLength, scrollBarMax.y - 1.f);
|
||||
const float scrollStart = ((viewLower - lower) / (higher - lower)) * canvasSizeLength + scrollBarMin[componentIndex];
|
||||
const float scrollEnd = ((viewHigher - lower) / (higher - lower)) * canvasSizeLength + scrollBarMin[componentIndex];
|
||||
const float screenSize = scrollEnd - scrollStart;
|
||||
const ImVec2 scrollTopLeft = isVertical ? ImVec2(scrollBarMin.x, scrollStart) : ImVec2(scrollStart, scrollBarMin.y);
|
||||
const ImVec2 scrollBottomRight = isVertical ? ImVec2(scrollBarMax.x - 2.f, scrollEnd) : ImVec2(scrollEnd, scrollBarMax.y - 2.f);
|
||||
const bool inScrollBar = canUseControl && ImRect(scrollTopLeft, scrollBottomRight).Contains(io.MousePos);
|
||||
const ImRect scrollBarRect(scrollBarA, scrollBarB);
|
||||
const float deltaScreen = io.MousePos[componentIndex] - scrollingSource;
|
||||
const float deltaView = ((higher - lower) / canvasSizeLength) * deltaScreen;
|
||||
const ImU32 barColor = ImGui::GetColorU32((inScrollBar || movingScrollBar) ? ImGuiCol_FrameBgHovered : ImGuiCol_FrameBg);
|
||||
const float middleCoord = (scrollStart + scrollEnd) * 0.5f;
|
||||
const bool insideControl = canUseControl && ImRect(scrollBarMin, scrollBarMax).Contains(io.MousePos);
|
||||
const bool hasAnchors = !(flags & ImGuiZoomSliderFlags_NoAnchors);
|
||||
const float viewMinSize = ((3.f * handleSize) / canvasSizeLength) * (higher - lower);
|
||||
const auto ClipView = [lower, higher, &viewLower, &viewHigher]() {
|
||||
if (viewLower < lower)
|
||||
{
|
||||
const float deltaClip = lower - viewLower;
|
||||
viewLower += deltaClip;
|
||||
viewHigher += deltaClip;
|
||||
}
|
||||
if (viewHigher > higher)
|
||||
{
|
||||
const float deltaClip = viewHigher - higher;
|
||||
viewLower -= deltaClip;
|
||||
viewHigher -= deltaClip;
|
||||
}
|
||||
};
|
||||
|
||||
bool onLeft = false;
|
||||
bool onRight = false;
|
||||
|
||||
draw_list->AddRectFilled(scrollBarA, scrollBarB, 0xFF101010, roundRadius);
|
||||
draw_list->AddRectFilled(scrollBarA, scrollBarB, 0xFF222222, 0);
|
||||
draw_list->AddRectFilled(scrollTopLeft, scrollBottomRight, barColor, roundRadius);
|
||||
|
||||
if (!(flags & ImGuiZoomSliderFlags_NoMiddleCarets))
|
||||
{
|
||||
for (float i = 0.5f; i < 3.f; i += 1.f)
|
||||
{
|
||||
const float coordA = middleCoord - handleSize * 0.5f;
|
||||
const float coordB = middleCoord + handleSize * 0.5f;
|
||||
ImVec2 base = scrollBarMin;
|
||||
base.x += scrollBarSize.x * 0.25f * i;
|
||||
base.y += scrollBarSize.y * 0.25f * i;
|
||||
|
||||
if (isVertical)
|
||||
{
|
||||
draw_list->AddLine(ImVec2(base.x, coordA), ImVec2(base.x, coordB), ImGui::GetColorU32(ImGuiCol_SliderGrab));
|
||||
}
|
||||
else
|
||||
{
|
||||
draw_list->AddLine(ImVec2(coordA, base.y), ImVec2(coordB, base.y), ImGui::GetColorU32(ImGuiCol_SliderGrab));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Mouse wheel
|
||||
if (io.MouseClicked[0] && insideControl && !inScrollBar)
|
||||
{
|
||||
const float ratio = (io.MousePos[componentIndex] - scrollBarMin[componentIndex]) / (scrollBarMax[componentIndex] - scrollBarMin[componentIndex]);
|
||||
const float size = (higher - lower);
|
||||
const float halfViewSize = (viewHigher - viewLower) * 0.5f;
|
||||
const float middle = ratio * size + lower;
|
||||
viewLower = middle - halfViewSize;
|
||||
viewHigher = middle + halfViewSize;
|
||||
ClipView();
|
||||
interacted = true;
|
||||
}
|
||||
|
||||
if (!(flags & ImGuiZoomSliderFlags_NoWheel) && inScrollBar && fabsf(io.MouseWheel) > 0.f)
|
||||
{
|
||||
const float ratio = (io.MousePos[componentIndex] - scrollStart) / (scrollEnd - scrollStart);
|
||||
const float amount = io.MouseWheel * wheelRatio * (viewHigher - viewLower);
|
||||
|
||||
viewLower -= ratio * amount;
|
||||
viewHigher += (1.f - ratio) * amount;
|
||||
ClipView();
|
||||
interacted = true;
|
||||
}
|
||||
|
||||
if (screenSize > handleSize * 2.f && hasAnchors)
|
||||
{
|
||||
const ImRect barHandleLeft(scrollTopLeft, isVertical ? ImVec2(scrollBottomRight.x, scrollTopLeft.y + handleSize) : ImVec2(scrollTopLeft.x + handleSize, scrollBottomRight.y));
|
||||
const ImRect barHandleRight(isVertical ? ImVec2(scrollTopLeft.x, scrollBottomRight.y - handleSize) : ImVec2(scrollBottomRight.x - handleSize, scrollTopLeft.y), scrollBottomRight);
|
||||
|
||||
onLeft = barHandleLeft.Contains(io.MousePos);
|
||||
onRight = barHandleRight.Contains(io.MousePos);
|
||||
|
||||
draw_list->AddRectFilled(barHandleLeft.Min, barHandleLeft.Max, ImGui::GetColorU32((onLeft || sizingLBar) ? ImGuiCol_SliderGrabActive : ImGuiCol_SliderGrab), roundRadius);
|
||||
draw_list->AddRectFilled(barHandleRight.Min, barHandleRight.Max, ImGui::GetColorU32((onRight || sizingRBar) ? ImGuiCol_SliderGrabActive : ImGuiCol_SliderGrab), roundRadius);
|
||||
}
|
||||
|
||||
if (sizingRBar)
|
||||
{
|
||||
if (!io.MouseDown[0])
|
||||
{
|
||||
sizingRBarSvg = false;
|
||||
editingId = (ImGuiID)-1;
|
||||
}
|
||||
else
|
||||
{
|
||||
viewHigher = ImMin(saveViewHigher + deltaView, higher);
|
||||
}
|
||||
}
|
||||
else if (sizingLBar)
|
||||
{
|
||||
if (!io.MouseDown[0])
|
||||
{
|
||||
sizingLBarSvg = false;
|
||||
editingId = (ImGuiID)-1;
|
||||
}
|
||||
else
|
||||
{
|
||||
viewLower = ImMax(saveViewLower + deltaView, lower);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (movingScrollBar)
|
||||
{
|
||||
if (!io.MouseDown[0])
|
||||
{
|
||||
movingScrollBarSvg = false;
|
||||
editingId = (ImGuiID)-1;
|
||||
}
|
||||
else
|
||||
{
|
||||
viewLower = saveViewLower + deltaView;
|
||||
viewHigher = saveViewHigher + deltaView;
|
||||
ClipView();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (inScrollBar && ImGui::IsMouseClicked(0))
|
||||
{
|
||||
movingScrollBarSvg = true;
|
||||
scrollingSource = io.MousePos[componentIndex];
|
||||
saveViewLower = viewLower;
|
||||
saveViewHigher = viewHigher;
|
||||
editingId = currentId;
|
||||
}
|
||||
if (!sizingRBar && onRight && ImGui::IsMouseClicked(0) && hasAnchors)
|
||||
{
|
||||
sizingRBarSvg = true;
|
||||
editingId = currentId;
|
||||
}
|
||||
if (!sizingLBar && onLeft && ImGui::IsMouseClicked(0) && hasAnchors)
|
||||
{
|
||||
sizingLBarSvg = true;
|
||||
editingId = currentId;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// minimal size check
|
||||
if ((viewHigher - viewLower) < viewMinSize)
|
||||
{
|
||||
const float middle = (viewLower + viewHigher) * 0.5f;
|
||||
viewLower = middle - viewMinSize * 0.5f;
|
||||
viewHigher = middle + viewMinSize * 0.5f;
|
||||
ClipView();
|
||||
}
|
||||
|
||||
return movingScrollBar || sizingRBar || sizingLBar || interacted;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
[package]
|
||||
name = "4dbug/imguizmo-luau"
|
||||
version = "0.1.0"
|
||||
registry = "https://github.com/UpliftGames/wally-index"
|
||||
realm = "shared"
|
||||
license = "MIT"
|
||||
description = "3D gizmo (translate/rotate/scale) for imgui-luau, with Roblox Camera/CFrame helpers."
|
||||
include = ["ImGuizmo.lua", "LICENSE", "README.md", "wally.toml", "default.project.json"]
|
||||
|
||||
[peer-dependencies]
|
||||
ImGui = "4dbug/imgui-luau@^0.4.0"
|
||||
Reference in new issue
Block a user