Example: live sine + demo windows; run example in tools/test.sh; README usage, API differences, status
This commit is contained in:
1 parent
e623a0961b
commit
051c4fec8f
3 files changed
+95
-3
No files matched your search
@@ -5,13 +5,53 @@ Immediate-mode 2D plotting for imgui-luau (Roblox / LÖVE).
|
||||
Luau port of [ImPlot](https://github.com/epezent/implot) 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/implot-luau) / [GitHub](https://github.com/4DBug/implot-luau).
|
||||
|
||||
**Status: work in progress.**
|
||||
**Status:** near-complete port of ImPlot (all plot types, axes/scales, subplots, legends, drag tools, styles/colormaps) and the full ImPlot demo (`ImPlotDemo.lua`).
|
||||
|
||||
## Usage
|
||||
|
||||
```lua
|
||||
local G = require(ReplicatedStorage.ImGui)
|
||||
local ImPlot = require(ReplicatedStorage.ImPlot)(G)
|
||||
require(ReplicatedStorage.ImPlotDemo)(G, ImPlot) -- optional, adds ImPlot.ShowDemoWindow
|
||||
local ImGui = G.ImGui
|
||||
|
||||
ImGui.CreateContext()
|
||||
ImPlot.CreateContext()
|
||||
|
||||
-- each frame, between ImGui.NewFrame() and ImGui.Render():
|
||||
ImGui.Begin("My Window")
|
||||
if ImPlot.BeginPlot("My Plot") then
|
||||
ImPlot.PlotLine("sin", Xs, Ys) -- or PlotLine(label, Values[, Count, XScale, X0])
|
||||
ImPlot.EndPlot()
|
||||
end
|
||||
ImGui.End()
|
||||
ImPlot.ShowDemoWindow(true)
|
||||
```
|
||||
|
||||
Full LocalScript: `examples/roblox/main.client.luau` (live scrolling sine wave + demo windows).
|
||||
|
||||
## API differences from C++
|
||||
|
||||
- **Pointer args become return values.** Values are passed in and the new values returned, e.g.
|
||||
`x, y, modified, clicked, hovered, held = ImPlot.DragPoint(id, x, y, col, radius, flags)`,
|
||||
`value, modified, clicked, hovered, held = ImPlot.DragLineX(id, value, ...)`,
|
||||
`p_open = ImPlot.ShowDemoWindow(p_open)` (same for `ShowMetricsWindow`).
|
||||
- **Overloads are dispatched at runtime** on argument types: `PlotLine(label, xs, ys[, count, spec])` vs
|
||||
`PlotLine(label, values[, count, xscale, x0, spec])`; getter variants keep the `G` suffix (`PlotLineG(label, fn, data, count, spec)`, `fn(idx, data) -> x, y`).
|
||||
- **Arrays are 1-based Luau tables.** `Count` defaults to the table length. `Offset` and `Stride` (set via spec) are
|
||||
counted in **elements**, not bytes; `Offset` is 0-based like C++ (ring-buffer index).
|
||||
- **Specs** may be `nil`, a plain table (`{ LineColor = ..., Offset = ... }`) or an `ImPlotSpec`.
|
||||
- **`SetNextLineStyle` / `SetNextFillStyle` / `SetNextMarkerStyle` / `SetNextErrorBarStyle`** (obsolete upstream) are
|
||||
kept for compatibility and applied on top of the spec of the next plot item.
|
||||
- Enums are tables (`ImPlot.ImAxis.X1`, `G.ImGuiCond.Always`, ...).
|
||||
- Port-specific deviations are tagged `[Port]` in the source.
|
||||
|
||||
## TODO
|
||||
|
||||
- Upstream TODOs carried over as-is (e.g. per-style `AxisBg*`/`AxisTick` colors, half-locked axis ranges, tick-count heuristics).
|
||||
- No LÖVE example yet (only `examples/roblox`).
|
||||
- Performance tuning for very large datasets.
|
||||
|
||||
## Development
|
||||
|
||||
```sh
|
||||
|
||||
@@ -1,15 +1,63 @@
|
||||
-- Live sine wave (scrolling buffer) + ImPlot / ImGui demo windows.
|
||||
local RS = game:GetService("ReplicatedStorage")
|
||||
local RunService = game:GetService("RunService")
|
||||
local G = require(RS:WaitForChild("ImGui"))
|
||||
local ImPlot = require(RS:WaitForChild("ImPlot"))(G)
|
||||
require(RS:WaitForChild("ImPlotDemo"))(G, ImPlot)
|
||||
local ImGui = G.ImGui
|
||||
|
||||
local MaxPoints = 500
|
||||
local Xs, Ys = table.create(MaxPoints, 0), table.create(MaxPoints, 0)
|
||||
local Count, Offset, Time = 0, 0, 0
|
||||
local Frequency = 1
|
||||
local ShowImPlotDemo, ShowImGuiDemo = true, false
|
||||
|
||||
local function AddPoint(X, Y)
|
||||
if Count < MaxPoints then
|
||||
Count += 1
|
||||
Xs[Count], Ys[Count] = X, Y
|
||||
else
|
||||
Xs[Offset + 1], Ys[Offset + 1] = X, Y
|
||||
Offset = (Offset + 1) % MaxPoints
|
||||
end
|
||||
end
|
||||
|
||||
local StartClock = os.clock()
|
||||
|
||||
local function RenderDemo()
|
||||
Time = os.clock() - StartClock
|
||||
AddPoint(Time, math.sin(Time * Frequency * 2 * math.pi))
|
||||
|
||||
ImGui.SetNextWindowSize(G.ImVec2(600, 400), G.ImGuiCond.FirstUseEver)
|
||||
ImGui.Begin("Live Sine")
|
||||
_, ShowImPlotDemo = ImGui.Checkbox("ImPlot Demo", ShowImPlotDemo)
|
||||
ImGui.SameLine()
|
||||
_, ShowImGuiDemo = ImGui.Checkbox("ImGui Demo", ShowImGuiDemo)
|
||||
Frequency = ImGui.SliderFloat("Frequency", Frequency, 0.1, 5)
|
||||
if ImPlot.BeginPlot("##Sine", G.ImVec2(-1, -1)) then
|
||||
ImPlot.SetupAxisLimits(ImPlot.ImAxis.X1, Time - 10, Time, G.ImGuiCond.Always)
|
||||
ImPlot.SetupAxisLimits(ImPlot.ImAxis.Y1, -1.1, 1.1)
|
||||
-- ring buffer: Offset (0-based, in elements) via spec, as in the C++ ScrollingBuffer demo
|
||||
ImPlot.PlotLine("sin", Xs, Ys, Count, { Offset = Offset })
|
||||
ImPlot.EndPlot()
|
||||
end
|
||||
ImGui.End()
|
||||
|
||||
if ShowImPlotDemo then
|
||||
ShowImPlotDemo = ImPlot.ShowDemoWindow(ShowImPlotDemo)
|
||||
end
|
||||
if ShowImGuiDemo then
|
||||
ShowImGuiDemo = ImGui.ShowDemoWindow(ShowImGuiDemo)
|
||||
end
|
||||
end
|
||||
|
||||
ImGui.CreateContext()
|
||||
G.ImGui_ImplRoblox.Init(game:GetService("Players").LocalPlayer:WaitForChild("PlayerGui"))
|
||||
ImPlot.CreateContext()
|
||||
G.ImGui_ImplRoblox.Init() -- defaults to LocalPlayer.PlayerGui
|
||||
RunService.RenderStepped:Connect(function()
|
||||
G.ImGui_ImplRoblox.NewFrame()
|
||||
ImGui.NewFrame()
|
||||
-- ImPlot example goes here
|
||||
RenderDemo()
|
||||
ImGui.Render()
|
||||
G.ImGui_ImplRoblox.RenderDrawData(ImGui.GetDrawData())
|
||||
end)
|
||||
@@ -6,4 +6,8 @@ for t in tests/t_*.luau; do
|
||||
out=$(python3 tools/core/tools/test/run.py --module ImPlot=ImPlot.lua --module ImPlotDemo=ImPlotDemo.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
|
||||
# example LocalScript: 400 frames of random input (mock drives Stepped, not RenderStepped)
|
||||
ex=$(mktemp --suffix=.luau); sed 's/RenderStepped/Stepped/' examples/roblox/main.client.luau > "$ex"
|
||||
out=$(python3 tools/core/tools/test/run.py --module ImPlot=ImPlot.lua --module ImPlotDemo=ImPlotDemo.lua --main "$ex" --roblox 2>&1); rm -f "$ex"
|
||||
if echo "$out" | grep -q "^avg frame"; then echo "PASS examples/roblox/main.client.luau"; else echo "FAIL examples/roblox/main.client.luau"; echo "$out" | tail -20; fail=1; fi
|
||||
exit $fail
|
||||
Reference in new issue
Block a user