-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #26313 from OliBomby/grids-4
Add grid placement tool
- Loading branch information
Showing
8 changed files
with
210 additions
and
52 deletions.
There are no files selected for viewing
126 changes: 126 additions & 0 deletions
126
osu.Game.Rulesets.Osu/Edit/Blueprints/GridPlacementBlueprint.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
// Copyright (c) ppy Pty Ltd <[email protected]>. Licensed under the MIT Licence. | ||
// See the LICENCE file in the repository root for full licence text. | ||
|
||
using osu.Framework.Allocation; | ||
using osu.Framework.Input.Events; | ||
using osu.Game.Rulesets.Edit; | ||
using osuTK; | ||
using osuTK.Input; | ||
|
||
namespace osu.Game.Rulesets.Osu.Edit.Blueprints | ||
{ | ||
public partial class GridPlacementBlueprint : PlacementBlueprint | ||
{ | ||
[Resolved] | ||
private HitObjectComposer? hitObjectComposer { get; set; } | ||
|
||
private OsuGridToolboxGroup gridToolboxGroup = null!; | ||
private Vector2 originalOrigin; | ||
private float originalSpacing; | ||
private float originalRotation; | ||
|
||
[BackgroundDependencyLoader] | ||
private void load(OsuGridToolboxGroup gridToolboxGroup) | ||
{ | ||
this.gridToolboxGroup = gridToolboxGroup; | ||
originalOrigin = gridToolboxGroup.StartPosition.Value; | ||
originalSpacing = gridToolboxGroup.Spacing.Value; | ||
originalRotation = gridToolboxGroup.GridLinesRotation.Value; | ||
} | ||
|
||
public override void EndPlacement(bool commit) | ||
{ | ||
if (!commit && PlacementActive != PlacementState.Finished) | ||
{ | ||
gridToolboxGroup.StartPosition.Value = originalOrigin; | ||
gridToolboxGroup.Spacing.Value = originalSpacing; | ||
if (!gridToolboxGroup.GridLinesRotation.Disabled) | ||
gridToolboxGroup.GridLinesRotation.Value = originalRotation; | ||
} | ||
|
||
base.EndPlacement(commit); | ||
|
||
// You typically only place the grid once, so we switch back to the last tool after placement. | ||
if (commit && hitObjectComposer is OsuHitObjectComposer osuHitObjectComposer) | ||
osuHitObjectComposer.SetLastTool(); | ||
} | ||
|
||
protected override bool OnClick(ClickEvent e) | ||
{ | ||
if (e.Button == MouseButton.Left) | ||
{ | ||
switch (PlacementActive) | ||
{ | ||
case PlacementState.Waiting: | ||
BeginPlacement(true); | ||
return true; | ||
|
||
case PlacementState.Active: | ||
EndPlacement(true); | ||
return true; | ||
} | ||
} | ||
|
||
return base.OnClick(e); | ||
} | ||
|
||
protected override bool OnMouseDown(MouseDownEvent e) | ||
{ | ||
if (e.Button == MouseButton.Right) | ||
{ | ||
// Reset the grid to the default values. | ||
gridToolboxGroup.StartPosition.Value = gridToolboxGroup.StartPosition.Default; | ||
gridToolboxGroup.Spacing.Value = gridToolboxGroup.Spacing.Default; | ||
if (!gridToolboxGroup.GridLinesRotation.Disabled) | ||
gridToolboxGroup.GridLinesRotation.Value = gridToolboxGroup.GridLinesRotation.Default; | ||
EndPlacement(true); | ||
return true; | ||
} | ||
|
||
return base.OnMouseDown(e); | ||
} | ||
|
||
protected override bool OnDragStart(DragStartEvent e) | ||
{ | ||
if (e.Button == MouseButton.Left) | ||
{ | ||
BeginPlacement(true); | ||
return true; | ||
} | ||
|
||
return base.OnDragStart(e); | ||
} | ||
|
||
protected override void OnDragEnd(DragEndEvent e) | ||
{ | ||
if (PlacementActive == PlacementState.Active) | ||
EndPlacement(true); | ||
|
||
base.OnDragEnd(e); | ||
} | ||
|
||
public override SnapType SnapType => ~SnapType.GlobalGrids; | ||
|
||
public override void UpdateTimeAndPosition(SnapResult result) | ||
{ | ||
var pos = ToLocalSpace(result.ScreenSpacePosition); | ||
|
||
if (PlacementActive != PlacementState.Active) | ||
gridToolboxGroup.StartPosition.Value = pos; | ||
else | ||
{ | ||
// Default to the original spacing and rotation if the distance is too small. | ||
if (Vector2.Distance(gridToolboxGroup.StartPosition.Value, pos) < 2) | ||
{ | ||
gridToolboxGroup.Spacing.Value = originalSpacing; | ||
if (!gridToolboxGroup.GridLinesRotation.Disabled) | ||
gridToolboxGroup.GridLinesRotation.Value = originalRotation; | ||
} | ||
else | ||
{ | ||
gridToolboxGroup.SetGridFromPoints(gridToolboxGroup.StartPosition.Value, pos); | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// Copyright (c) ppy Pty Ltd <[email protected]>. Licensed under the MIT Licence. | ||
// See the LICENCE file in the repository root for full licence text. | ||
|
||
using osu.Framework.Graphics; | ||
using osu.Framework.Graphics.Sprites; | ||
using osu.Game.Rulesets.Edit; | ||
using osu.Game.Rulesets.Edit.Tools; | ||
using osu.Game.Rulesets.Osu.Edit.Blueprints; | ||
|
||
namespace osu.Game.Rulesets.Osu.Edit | ||
{ | ||
public partial class GridFromPointsTool : CompositionTool | ||
{ | ||
public GridFromPointsTool() | ||
: base("Grid") | ||
{ | ||
TooltipText = """ | ||
Left click to set the origin. | ||
Left click again to set the spacing and rotation. | ||
Right click to reset to default. | ||
Click and drag to set the origin, spacing and rotation. | ||
"""; | ||
} | ||
|
||
public override Drawable CreateIcon() => new SpriteIcon { Icon = FontAwesome.Solid.DraftingCompass }; | ||
|
||
public override PlacementBlueprint CreatePlacementBlueprint() => new GridPlacementBlueprint(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.