Skip to content

Commit

Permalink
Update all rail point selection handling
Browse files Browse the repository at this point in the history
  • Loading branch information
KillzXGaming committed Nov 6, 2023
1 parent 517d913 commit 43192b0
Showing 1 changed file with 22 additions and 17 deletions.
39 changes: 22 additions & 17 deletions Fushigi/ui/bgunit/BGUnitRail.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,28 +71,30 @@ public CourseUnit.Rail Save()
return rail;
}

public void DeselectAll()
public void DeselectAll(CourseAreaEditContext ctx)
{
foreach (var point in Points)
point.IsSelected = false;
if (ctx.IsSelected(point))
ctx.Deselect(point);
}

public void SelectAll()
public void SelectAll(CourseAreaEditContext ctx)
{
foreach (var point in Points)
point.IsSelected = true;
ctx.Select(Points);
}

public void InsertPoint(LevelViewport viewport, RailPoint point, int index)
{
this.Points.Insert(index, point);
viewport.mEditContext.AddToUndo(new UnitRailPointAddUndo(this, point, index));
viewport.mEditContext.Select(point);
}

public void AddPoint(LevelViewport viewport, RailPoint point)
{
this.Points.Add(point);
viewport.mEditContext.AddToUndo(new UnitRailPointAddUndo(this, point));
viewport.mEditContext.Select(point);
}

public void RemoveSelected(LevelViewport viewport)
Expand All @@ -117,7 +119,7 @@ public void OnKeyDown(LevelViewport viewport)
if (ImGui.IsKeyPressed(ImGuiKey.Delete))
RemoveSelected(viewport);
if (viewport.mEditContext.IsSelected(this) && ImGui.GetIO().KeyCtrl && ImGui.IsKeyPressed(ImGuiKey.A))
SelectAll();
SelectAll(viewport.mEditContext);
}

public bool HitTest(LevelViewport viewport)
Expand All @@ -128,7 +130,8 @@ public bool HitTest(LevelViewport viewport)

public void OnMouseDown(LevelViewport viewport)
{
bool isSelected = viewport.mEditContext.IsSelected(this);
var ctx = viewport.mEditContext;
bool isSelected = ctx.IsSelected(this);

//Line hit test
if (!isSelected && viewport.HoveredObject==this)
Expand All @@ -154,12 +157,12 @@ public void OnMouseDown(LevelViewport viewport)
MathF.Round(posVec.Y, MidpointRounding.AwayFromZero),
selected[0].Position.Z);

DeselectAll();
DeselectAll(viewport.mEditContext);

if (this.Points.Count - 1 == index) //is last point
InsertPoint(viewport, new RailPoint(pos) { IsSelected = true }, 0);
InsertPoint(viewport, new RailPoint(pos), 0);
else
InsertPoint(viewport, new RailPoint(pos) { IsSelected = true }, index + 1);
InsertPoint(viewport, new RailPoint(pos), index + 1);
}
else if (ImGui.GetIO().KeyAlt && selected.Count == 0) //Add new point from last
{
Expand All @@ -170,14 +173,14 @@ public void OnMouseDown(LevelViewport viewport)
MathF.Round(posVec.Y, MidpointRounding.AwayFromZero),
2);

DeselectAll();
DeselectAll(viewport.mEditContext);

AddPoint(viewport, new RailPoint(pos) { IsSelected = true });
AddPoint(viewport, new RailPoint(pos));
}
else
{
if (!ImGui.GetIO().KeyCtrl && !ImGui.GetIO().KeyShift)
DeselectAll();
DeselectAll(viewport.mEditContext);
}

for (int i = 0; i < Points.Count; i++)
Expand All @@ -189,7 +192,7 @@ public void OnMouseDown(LevelViewport viewport)
bool isHovered = (ImGui.GetMousePos() - pnt).Length() < 6.0f;

if (isHovered)
Points[i].IsSelected = true;
ctx.Select(Points[i]);

Points[i].PreviousPosition = point;
}
Expand Down Expand Up @@ -217,6 +220,8 @@ public void OnSelecting(LevelViewport viewport)
{
if (!mouseDown) return;

var ctx = viewport.mEditContext;

Vector3 posVec = viewport.ScreenToWorld(ImGui.GetMousePos());
Vector3 diff = posVec - mouseDownPos;
if (diff.X != 0 && diff.Y != 0 && !transformStart)
Expand All @@ -231,7 +236,7 @@ public void OnSelecting(LevelViewport viewport)

for (int i = 0; i < Points.Count; i++)
{
if (transformStart && Points[i].IsSelected)
if (transformStart && ctx.IsSelected(Points[i]))
{
diff.X = MathF.Round(diff.X, MidpointRounding.AwayFromZero);
diff.Y = MathF.Round(diff.Y, MidpointRounding.AwayFromZero);
Expand All @@ -246,6 +251,7 @@ public void Render(LevelViewport viewport, ImDrawListPtr mDrawList)
if (!this.Visible)
return;

var ctx = viewport.mEditContext;
bool isSelected = viewport.mEditContext.IsSelected(this);

if (ImGui.IsMouseClicked(0) && ImGui.IsMouseDown(ImGuiMouseButton.Left))
Expand Down Expand Up @@ -322,7 +328,7 @@ public void Render(LevelViewport viewport, ImDrawListPtr mDrawList)

//Display point color
uint color = 0xFFFFFFFF;
if (Points[i].IsHovered || Points[i].IsSelected)
if (Points[i].IsHovered || ctx.IsSelected(Points[i]))
color = ImGui.ColorConvertFloat4ToU32(new(0.84f, .437f, .437f, 1));

mDrawList.AddCircleFilled(pos2D, 6.0f, color);
Expand Down Expand Up @@ -367,7 +373,6 @@ public Vector3 Position
set { Transform.Position = value; }
}

public bool IsSelected { get; set; }
public bool IsHovered { get; set; }

//For transforming
Expand Down

0 comments on commit 43192b0

Please sign in to comment.