Skip to content

Codechange: Add and use Rect WithX/WithY methods.#14669

Merged
PeterN merged 2 commits intoOpenTTD:masterfrom
PeterN:rect-setxy
Sep 27, 2025
Merged

Codechange: Add and use Rect WithX/WithY methods.#14669
PeterN merged 2 commits intoOpenTTD:masterfrom
PeterN:rect-setxy

Conversation

@PeterN
Copy link
Member

@PeterN PeterN commented Sep 27, 2025

Motivation / Problem

Often we have a Rect but need to set the left and right, or top and button, coordinates directly. This requires decomposing the Rect into a new one, making sure that parameters are the correct signed type.

Sometimes they are set based on another existing Rect.

Description

Add WithX() and WithY() SetX() and SetY() methods to Rect, in two forms: one takes two coordinates to replace left and right or top and bottom as appropriate, the other takes another Rect, and does the same.

Update some places that deconstruct Rect to use WithX() or WithY() SetX() or SetY(). Unlikely to be exhaustive.

Limitations

Checklist for review

Some things are not automated, and forgotten often. This list is a reminder for the reviewers.

  • The bug fix is important enough to be backported? (label: 'backport requested')
  • This PR touches english.txt or translations? Check the guidelines
  • This PR affects the GS/AI API? (label 'needs review: Script API')
    • ai_changelog.hpp, game_changelog.hpp need updating.
    • The compatibility wrappers (compat_*.nut) need updating.
  • This PR affects the NewGRF API? (label 'needs review: NewGRF')

Copy link
Member

@LordAro LordAro left a comment

Choose a reason for hiding this comment

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

(warning, bike shedding) how about WithX ? Set implies it modifies the original object

@PeterN
Copy link
Member Author

PeterN commented Sep 27, 2025

(warning, bike shedding) how about WithX ? Set implies it modifies the original object

Good call, that's why (some of) the current methods are With...

I did also consider SetLeftRight, so maybe SetX -> WithLeftRight, but also maybe WithX is fine.

This simplifies creating a new Rect with specific horizontal or vertical coordinates.
@PeterN PeterN changed the title Codechange: Add and use Rect SetX/SetY methods. Codechange: Add and use Rect WithX/WithY methods. Sep 27, 2025
@PeterN PeterN merged commit 31eec71 into OpenTTD:master Sep 27, 2025
16 checks passed
@PeterN PeterN deleted the rect-setxy branch September 27, 2025 23:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants