Skip to main content

RectInnerPointHandle

rect-inner-point-handle

See the RectInnerPointHandleDemo.cs.

using Gridrand.Contracts;
using Gridrand.RimGui.Manual;
using Gridrand.RimGui.Manual.Utility;

namespace Gridrand.RimGui.Extensions.Manual
{
/// <summary>
/// Demo for handling a point within a rectangle.
/// </summary>
class RectInnerPointHandleDemo : ManualBase, IManual
{
Vector2 position = new(150, 150);
Vector2 min = new(100, 100);
Vector2 max = new(200, 200);


Vector2 value = new Vector2(0.5f, 0.5f);
Vector2 snapValue = new Vector2(0.5f, 0.5f);

public RectInnerPointHandleDemo(ManualBaseResource p) : base(p)
{
}

public void Draw()
{
DrawStandard();
DrawSnap();
DrawCustom();
}

void DrawStandard()
{
Gui.Heading("RectInnerPointHandle");

Gui.Text(value.ToString("F2"));
var rect = Ctx.AllocateRect(height: 100);
Gui.FrameBox(rect);
// Use handle to interactively move the point inside the rect
value = Gui.RectInnerPointHandle(rect, value);
}

void DrawSnap()
{
Gui.Heading("SnapRectInnerPointHandle");

Gui.Text(snapValue.ToString("F2"));
var rect = Ctx.AllocateRect(height: 100);
Gui.FrameBox(rect);
// Use handle to interactively move the point inside the rect
snapValue = Gui.SnapRectInnerPointHandle(rect, snapValue);
}

void DrawCustom()
{
Gui.Heading("Custom");

// UI inputs for adjusting min and max boundaries
Gui.LabelSliderInput("Min", ref min, new Vector2(0f, 0f), new Vector2(200f, 200f));
Gui.LabelSliderInput("Max", ref max, new Vector2(300f, 300f), new Vector2(500f, 500f));

// Clamp position within min and max
position = Vector2.Max(min, position);
position = Vector2.Min(max, position);

var rect = Ctx.AllocateRect(height: 250f).CreateCentered(200f, 200f);

// For input interaction
Gui.NextRect(rect).InteractiveArea();

// If the InvisibleArea was clicked, update position based on normalized pointer offset
if (Ctx.IsLastPressed())
{
var normalizedOffset = (Input.Pointer.Pos - rect.Position) / rect.Size;
position = min + normalizedOffset * (max - min);
position = position.Clamp(min, max);
}

// Draw frame box around the rect
Gui.FrameBox(rect);

// Display current position as text near the handle
var normalized = (position - min) / (max - min);
var screenPos = rect.Position + normalized * rect.Size;
var textRect = RectUtil.CreateFromCenter(new Vector2(screenPos.x, screenPos.y + 15f), new Vector2(100f, 20f));
Gui.NextRect(textRect).Text(position.ToString());

// Use handle to interactively move the point inside the rect
var ratePos = position.ReampTo01(min, max);
ratePos = Gui.RectInnerPointHandle(rect, ratePos);
position = ratePos.Reamp01(min, max);
}
}
}