Skip to content

Interactable: allow specifying arbitrary 2D polygon for mouse interaction#21574

Merged
abcdefg30 merged 2 commits into
OpenRA:bleedfrom
michaeldgg2:feature/interactable-polygon-mouse-bounds
Sep 20, 2024
Merged

Interactable: allow specifying arbitrary 2D polygon for mouse interaction#21574
abcdefg30 merged 2 commits into
OpenRA:bleedfrom
michaeldgg2:feature/interactable-polygon-mouse-bounds

Conversation

@michaeldgg2

@michaeldgg2 michaeldgg2 commented Sep 8, 2024

Copy link
Copy Markdown
Contributor

Background

Interactable trait does not allow specifying non-rectangular region as mouse bounds...

[Desc("Used to enable mouse interaction on actors that are not Selectable.")]
public class InteractableInfo : TraitInfo, IMouseBoundsInfo
{
	[Desc("Defines a custom rectangle for mouse interaction with the actor.",
		"If null, the engine will guess an appropriate size based on the With*Body trait.",
		"The first two numbers define the width and height of the rectangle as a world distance.",
		"The (optional) second two numbers define an x and y offset from the actor center.")]
	public readonly WDist[] Bounds = null;

	// ...

... despite MouseoverBounds() method in IMouseBounds interface returning Polygon:

public interface IMouseBounds { Polygon MouseoverBounds(Actor self, WorldRenderer wr); }

PR description

This PR allows specifying arbitrary 2D polygon for mouse interaction that will be used instead of Bounds (or bounds generated using IAutoMouseBounds trait):

[Desc("Defines a custom 2D polygon for mouse interaction with the actor.",
	"If null, Bounds will be used instead",
	"Each vertex has two components (so two numbers), which define an x and y offset from the actor center.")]
public readonly int2[] Polygon = null;

Example in RA

This is how a (bit contrived) test case could look like in RA:

openra_nonrectangular_mousebounds
(here only Refinery has polygon mouse bounds, the rest of the buildings use normal rectangle)

Selectable:
	# Bounds: 3072, 2133, 0, 170
	DecorationBounds: 3072, 2986, 0, -85
	Polygon: 0,1194, 938,170, 3072,170, 3072,1621, 2560,1621, 1066,2303, 0,2303

I also added a debug trait RenderMouseBounds for use by modders in order to tweak the polygon, since the mouse bounds are not visible unlike the selection box. It's not essential for the PR, so I can remove it, if required.

More background (and a real use case)

In OpenE2140 we have a small issue with the mouse bounds for Refinery and Mine buildings. Both have conveyor belt on one entire tile, which "sticks" out of the main part of each building. The problem is that it's not possible to target the cell right below the conveyor belt:

opene2140_refinery_rectangular_mouse_bounds

With Interactable allowing to define polygon (in addition to normal rectangle), we can solve this issue very elegantly:

opene2140_refinery_non_rectangular_mouse_bounds

Actually this is how the mouse interaction works in the original Earth 2140:

e2140_mine_mousebounds

Comment thread OpenRA.Mods.Common/Traits/Interactable.cs
@michaeldgg2 michaeldgg2 force-pushed the feature/interactable-polygon-mouse-bounds branch from 543ca2d to 58a10f3 Compare September 10, 2024 18:31

@RoosterDragon RoosterDragon left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like a nice addition to me, and RenderMouseBounds is a lightweight addon so seems worth keeping for debugging utility.

@anvilvapre

Copy link
Copy Markdown
Contributor

if i look at the example graphics, it seems to me it migh be nicer to draw 3d bounding box around a building. i.e. letting a selection/bounding box be part of the graphics of a building.

abcdefg30
abcdefg30 previously approved these changes Sep 19, 2024

@abcdefg30 abcdefg30 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lgtm otherwise. Can you remove the testcase?

Comment thread OpenRA.Mods.Common/Traits/Render/RenderMouseBounds.cs Outdated
@michaeldgg2 michaeldgg2 force-pushed the feature/interactable-polygon-mouse-bounds branch from 24d3ccb to 5069f5b Compare September 20, 2024 20:57
@abcdefg30 abcdefg30 merged commit 9524db2 into OpenRA:bleed Sep 20, 2024
@abcdefg30

Copy link
Copy Markdown
Member

Changelog

@michaeldgg2 michaeldgg2 deleted the feature/interactable-polygon-mouse-bounds branch November 8, 2024 16:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants