Port legend rendering (CalcLegendSize, ClampLegendRect, ShowLegendEntries, RenderLegend with toggle/scroll) in EndPlot; add t_line
This commit is contained in:
1 parent
37177de028
commit
1dd3aeb8ee
2 files changed
+220
-1
No files matched your search
+143
-1
@@ -3465,6 +3465,147 @@ return function(G)
|
||||
end
|
||||
end
|
||||
|
||||
--------------------------------------------------------------------------
|
||||
-- [SECTION] Legend Utils
|
||||
--------------------------------------------------------------------------
|
||||
do
|
||||
local ImHasFlag, IG = internal.ImHasFlag, internal.ItemGroup
|
||||
local LF = ImPlot3D.ImPlot3DLegendFlags
|
||||
local band, bnot, lshift = bit32.band, bit32.bnot, bit32.lshift
|
||||
local ImRect = G.ImRect
|
||||
|
||||
local function ImAlphaU32(col, alpha) return band(col, bnot(lshift(math.floor((1 - alpha) * 255), 24))) end
|
||||
internal.ImAlphaU32 = ImAlphaU32
|
||||
|
||||
function internal.CalcLegendSize(items, pad, spacing, vertical)
|
||||
local n = IG.GetLegendCount(items)
|
||||
local txt_ht = ImGui.GetTextLineHeight()
|
||||
local icon_size = txt_ht
|
||||
local max_w, sum_w = 0, 0
|
||||
for i = 0, n - 1 do
|
||||
local w = ImGui.CalcTextSize(IG.GetLegendLabel(items, i), nil, true).x
|
||||
max_w = math.max(max_w, w)
|
||||
sum_w += w
|
||||
end
|
||||
if vertical then
|
||||
return ImVec2(pad.x * 2 + icon_size + max_w, pad.y * 2 + n * txt_ht + (n - 1) * spacing.y)
|
||||
end
|
||||
return ImVec2(pad.x * 2 + icon_size * n + sum_w + (n - 1) * spacing.x, pad.y * 2 + txt_ht)
|
||||
end
|
||||
|
||||
-- [Port] modifies legend_rect in place, returns clamped
|
||||
function internal.ClampLegendRect(r, outer, pad)
|
||||
local clamped = false
|
||||
local minx, miny, maxx, maxy = outer.Min.x + pad.x, outer.Min.y + pad.y, outer.Max.x - pad.x, outer.Max.y - pad.y
|
||||
if r.Min.x < minx then r.Min.x = minx; clamped = true end
|
||||
if r.Min.y < miny then r.Min.y = miny; clamped = true end
|
||||
if r.Max.x > maxx then r.Max.x = maxx; clamped = true end
|
||||
if r.Max.y > maxy then r.Max.y = maxy; clamped = true end
|
||||
return clamped
|
||||
end
|
||||
|
||||
function internal.ShowLegendEntries(items, legend_bb, pad, spacing, vertical, draw_list)
|
||||
local txt_ht = ImGui.GetTextLineHeight()
|
||||
local icon_size, icon_shrink = txt_ht, 2
|
||||
local col_txt = ImPlot3D.GetStyleColorU32(ImPlot3D.ImPlot3DCol.LegendText)
|
||||
local col_txt_dis = ImAlphaU32(col_txt, 0.25)
|
||||
local sum_label_width = 0
|
||||
local n = IG.GetLegendCount(items)
|
||||
for i = 0, n - 1 do
|
||||
local item = IG.GetLegendItem(items, i)
|
||||
local label = IG.GetLegendLabel(items, i)
|
||||
local label_width = ImGui.CalcTextSize(label, nil, true).x
|
||||
local top_left = if vertical then legend_bb.Min + pad + ImVec2(0, i * (txt_ht + spacing.y))
|
||||
else legend_bb.Min + pad + ImVec2(i * (icon_size + spacing.x) + sum_label_width, 0)
|
||||
sum_label_width += label_width
|
||||
local icon_min = top_left + ImVec2(icon_shrink, icon_shrink)
|
||||
local icon_max = top_left + ImVec2(icon_size - icon_shrink, icon_size - icon_shrink)
|
||||
local label_max = top_left + ImVec2(label_width + icon_size, icon_size)
|
||||
local col_item = ImAlphaU32(item.Color, 1)
|
||||
local button_bb = ImRect(icon_min, label_max)
|
||||
ImGui.KeepAliveID(item.ID)
|
||||
local item_clk, item_hov, item_hld = false, false, false
|
||||
if not ImHasFlag(items.Legend.Flags, LF.NoButtons) then
|
||||
item_clk, item_hov, item_hld = ImGui.ButtonBehavior(button_bb, item.ID)
|
||||
end
|
||||
if item_clk then item.Show = not item.Show end
|
||||
local col_txt_hl
|
||||
if item_hov and not ImHasFlag(items.Legend.Flags, LF.NoHighlightItem) then
|
||||
item.LegendHovered = true
|
||||
col_txt_hl = internal.ImMixU32(col_txt, col_item, 64)
|
||||
else
|
||||
item.LegendHovered = false
|
||||
col_txt_hl = ImGui.GetColorU32(col_txt, nil, true)
|
||||
end
|
||||
local col_icon
|
||||
if item_hld then
|
||||
col_icon = if item.Show then ImAlphaU32(col_item, 0.5) else ImGui.GetColorU32(G.ImGuiCol.TextDisabled, 0.5)
|
||||
elseif item_hov then
|
||||
col_icon = if item.Show then ImAlphaU32(col_item, 0.75) else ImGui.GetColorU32(G.ImGuiCol.TextDisabled, 0.75)
|
||||
else
|
||||
col_icon = if item.Show then col_item else col_txt_dis
|
||||
end
|
||||
draw_list:AddRectFilled(icon_min, icon_max, col_icon)
|
||||
local text_end = ImGui.FindRenderedTextEnd(label)
|
||||
if text_end > 1 then
|
||||
draw_list:AddText(top_left + ImVec2(icon_size, 0), if item.Show then col_txt_hl else col_txt_dis, label, text_end)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function internal.RenderLegend()
|
||||
local gp = GImPlot3D
|
||||
local plot = gp.CurrentPlot
|
||||
if ImHasFlag(plot.Flags, ImPlot3D.ImPlot3DFlags.NoLegend) or IG.GetLegendCount(plot.Items) == 0 then return end
|
||||
local draw_list = ImGui.GetWindowDrawList()
|
||||
local IO = ImGui.GetIO()
|
||||
local style = gp.Style
|
||||
local legend = plot.Items.Legend
|
||||
local legend_horz = ImHasFlag(legend.Flags, LF.Horizontal)
|
||||
local legend_size = internal.CalcLegendSize(plot.Items, style.LegendInnerPadding, style.LegendSpacing, not legend_horz)
|
||||
local legend_pos = internal.GetLocationPos(plot.PlotRect, legend_size, legend.Location, style.LegendPadding)
|
||||
legend.Rect = ImRect(legend_pos, legend_pos + legend_size)
|
||||
legend.RectClamped = ImRect(legend.Rect.Min, legend.Rect.Max)
|
||||
local legend_scrollable = internal.ClampLegendRect(legend.RectClamped, plot.PlotRect, style.LegendPadding)
|
||||
-- Test hover (using the clamped rect so off-screen overflow does not register)
|
||||
local BF = G.ImGuiButtonFlags
|
||||
local flags = bit32.bor(BF.AllowOverlap, BF.PressedOnClick, BF.PressedOnDoubleClick, BF.MouseButtonLeft,
|
||||
BF.MouseButtonRight, BF.MouseButtonMiddle, BF.FlattenChildren)
|
||||
ImGui.KeepAliveID(plot.Items.ID)
|
||||
local _
|
||||
_, legend.Hovered, legend.Held = ImGui.ButtonBehavior(legend.RectClamped, plot.Items.ID, flags)
|
||||
legend.Hovered = legend.Hovered or legend.RectClamped:Contains(IO.MousePos)
|
||||
-- Handle legend scrolling
|
||||
if legend_scrollable then
|
||||
if legend.Hovered then
|
||||
ImGui.SetKeyOwner(G.ImGuiKey.MouseWheelY, plot.Items.ID)
|
||||
if IO.MouseWheel ~= 0.0 then
|
||||
local max_step = legend.Rect:GetSize() * 0.67
|
||||
local step = math.floor(math.min(2 * ImGui.GetFontSize(), max_step.x))
|
||||
legend.Scroll.x += step * IO.MouseWheel
|
||||
legend.Scroll.y += step * IO.MouseWheel
|
||||
end
|
||||
end
|
||||
local min_off = legend.RectClamped:GetSize() - legend.Rect:GetSize()
|
||||
legend.Scroll.x = math.clamp(legend.Scroll.x, math.min(min_off.x, 0), 0)
|
||||
legend.Scroll.y = math.clamp(legend.Scroll.y, math.min(min_off.y, 0), 0)
|
||||
local scroll = if legend_horz then ImVec2(legend.Scroll.x, 0) else ImVec2(0, legend.Scroll.y)
|
||||
local off = legend.RectClamped.Min - legend.Rect.Min + scroll
|
||||
legend.Rect = ImRect(legend.Rect.Min + off, legend.Rect.Max + off)
|
||||
else
|
||||
legend.Scroll = ImVec2(0, 0)
|
||||
end
|
||||
-- Render background and entries (clipped to the clamped rect)
|
||||
local col_bg = ImPlot3D.GetStyleColorU32(ImPlot3D.ImPlot3DCol.LegendBg)
|
||||
local col_bd = ImPlot3D.GetStyleColorU32(ImPlot3D.ImPlot3DCol.LegendBorder)
|
||||
ImGui.PushClipRect(legend.RectClamped.Min, legend.RectClamped.Max, true)
|
||||
draw_list:AddRectFilled(legend.RectClamped.Min, legend.RectClamped.Max, col_bg)
|
||||
internal.ShowLegendEntries(plot.Items, legend.Rect, style.LegendInnerPadding, style.LegendSpacing, not legend_horz, draw_list)
|
||||
draw_list:AddRect(legend.RectClamped.Min, legend.RectClamped.Max, col_bd)
|
||||
ImGui.PopClipRect()
|
||||
end
|
||||
end
|
||||
|
||||
--------------------------------------------------------------------------
|
||||
-- [SECTION] ImDrawList3D
|
||||
-- [Port] VtxBuffer is an ImDrawVtxBuffer (same 36-byte vertex layout as ImDrawList), IdxBuffer/ZBuffer are
|
||||
@@ -3665,7 +3806,8 @@ return function(G)
|
||||
ImGui.PopClipRect()
|
||||
-- Reset legend hover
|
||||
plot.Items.Legend.Hovered = false
|
||||
-- [Port] TODO: RenderLegend (added with items)
|
||||
-- Render legend
|
||||
internal.RenderLegend()
|
||||
-- Mouse position text and context menus
|
||||
internal.ShowPlotPopups(plot)
|
||||
-- Pop frame rect clipping
|
||||
|
||||
@@ -0,0 +1,77 @@
|
||||
-- PlotLine/PlotScatter of 500 points add vertices; auto-fit limits match data; clicking a legend entry hides the item
|
||||
local RS = game:GetService("ReplicatedStorage")
|
||||
local G = require(RS:WaitForChild("ImGui"))
|
||||
local ImPlot3D = require(RS:WaitForChild("ImPlot3D"))(G)
|
||||
local ImGui, ImVec2 = G.ImGui, G.ImVec2
|
||||
ImGui.CreateContext()
|
||||
ImGui.GetIO().Fonts:AddFontDefault()
|
||||
G.ImGui_ImplRoblox.Init()
|
||||
ImPlot3D.CreateContext()
|
||||
local io = ImGui.GetIO()
|
||||
local internal, IG = ImPlot3D.internal, ImPlot3D.internal.ItemGroup
|
||||
|
||||
local N = 500
|
||||
local xs, ys, zs = {}, {}, {}
|
||||
for i = 1, N do
|
||||
local t = (i - 1) / (N - 1)
|
||||
xs[i], ys[i], zs[i] = math.cos(t * 20) * t * 3, math.sin(t * 20) * t * 2, t * 5 - 1
|
||||
end
|
||||
local function minmax(a) local lo, hi = math.huge, -math.huge for _, v in a do lo = math.min(lo, v) hi = math.max(hi, v) end return lo, hi end
|
||||
local spec = ImPlot3D.ImPlot3DSpec(ImPlot3D.ImPlot3DProp.Marker, ImPlot3D.ImPlot3DMarker.Square)
|
||||
|
||||
local f, done, entry, hidden_at = 0, false, nil, nil
|
||||
local vtx_line, vtx_scatter
|
||||
game:GetService("RunService").Stepped:Connect(function()
|
||||
if done then return end
|
||||
f += 1
|
||||
G.ImGui_ImplRoblox.NewFrame()
|
||||
if entry then
|
||||
if f == 10 then io:AddMousePosEvent(entry.x, entry.y) end
|
||||
if f == 11 then io:AddMouseButtonEvent(0, true) end
|
||||
if f == 12 then io:AddMouseButtonEvent(0, false) end
|
||||
end
|
||||
ImGui.NewFrame()
|
||||
ImGui.SetNextWindowPos(ImVec2(0, 0))
|
||||
ImGui.SetNextWindowSize(ImVec2(600, 600))
|
||||
ImGui.Begin("Plot window")
|
||||
local wdl = ImGui.GetWindowDrawList()
|
||||
assert(ImPlot3D.BeginPlot("Lines", ImVec2(-1, 500)), "BeginPlot")
|
||||
local plot = internal.GetCurrentPlot()
|
||||
local dl3 = plot.DrawList
|
||||
local v0 = dl3.VtxBuffer.Size
|
||||
ImPlot3D.PlotLine("Line", xs, ys, zs, N)
|
||||
local v1 = dl3.VtxBuffer.Size
|
||||
ImPlot3D.PlotScatter("Scatter", xs, ys, zs, spec)
|
||||
local v2 = dl3.VtxBuffer.Size
|
||||
assert(dl3.ZSize * 3 == dl3.IdxSize, "one depth per triangle")
|
||||
local before = wdl.VtxBuffer.Size
|
||||
ImPlot3D.EndPlot()
|
||||
assert(wdl.VtxBuffer.Size >= before + v2, "vertices moved to window draw list")
|
||||
assert(dl3.VtxBuffer.Size == 0, "3D draw list reset")
|
||||
local line = IG.GetItem(plot.Items, "Line")
|
||||
if f == 5 then
|
||||
vtx_line, vtx_scatter = v1 - v0, v2 - v1
|
||||
assert(vtx_line >= (N - 1) * 4, "PlotLine adds vertices: " .. vtx_line)
|
||||
assert(vtx_scatter >= N * 4, "PlotScatter adds vertices: " .. vtx_scatter)
|
||||
for i, a in { xs, ys, zs } do
|
||||
local lo, hi = minmax(a)
|
||||
local r = plot.Axes[i - 1].Range
|
||||
assert(math.abs(r.Min - lo) < 1e-9 and math.abs(r.Max - hi) < 1e-9, ("auto-fit axis %d: [%g, %g] vs [%g, %g]"):format(i - 1, r.Min, r.Max, lo, hi))
|
||||
end
|
||||
assert(IG.GetLegendCount(plot.Items) == 2, "two legend entries")
|
||||
local legend = plot.Items.Legend
|
||||
local h = ImGui.GetTextLineHeight()
|
||||
local pad = ImPlot3D.GetStyle().LegendInnerPadding
|
||||
entry = ImVec2(legend.Rect.Min.x + pad.x + h * 0.5, legend.Rect.Min.y + pad.y + h * 0.5) -- first entry icon
|
||||
assert(line.Show, "visible before click")
|
||||
end
|
||||
if f > 13 and not hidden_at then
|
||||
assert(not line.Show, "clicking legend entry hides item")
|
||||
assert(v1 == v0, "hidden line adds no vertices")
|
||||
assert(IG.GetItem(plot.Items, "Scatter").Show, "other item still shown")
|
||||
hidden_at = f
|
||||
end
|
||||
ImGui.End()
|
||||
ImGui.Render(); G.ImGui_ImplRoblox.RenderDrawData(ImGui.GetDrawData())
|
||||
if f == 20 then assert(hidden_at, "hidden"); done = true; print("OK") end
|
||||
end)
|
||||
Reference in new issue
Block a user