Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make calculate(Horizontal/Vertical)ContentSize in Component #276

Draft
wants to merge 4 commits into
base: 1.21
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 19 additions & 4 deletions src/main/java/io/wispforest/owo/ui/base/BaseComponent.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,16 +61,31 @@ protected BaseComponent() {

/**
* @return The horizontal size this component needs to fit its contents
* @throws UnsupportedOperationException if this component doesn't support horizontal content sizing
* @deprecated Override {@link Component#calculateHorizontalContentSize(Sizing)}
*/
protected int determineHorizontalContentSize(Sizing sizing) {
throw new UnsupportedOperationException(this.getClass().getSimpleName() + " does not support Sizing.content() on the horizontal axis");
return Component.super.calculateHorizontalContentSize(sizing);
}

/**
* @return The vertical size this component needs to fit its contents
* @throws UnsupportedOperationException if this component doesn't support vertical content sizing
* @deprecated Override {@link Component#calculateVerticalContentSize(Sizing)}
*/
@Deprecated
protected int determineVerticalContentSize(Sizing sizing) {
throw new UnsupportedOperationException(this.getClass().getSimpleName() + " does not support Sizing.content() on the vertical axis");
return Component.super.calculateVerticalContentSize(sizing);
}

@Override
public int calculateHorizontalContentSize(Sizing sizing) {
return this.determineHorizontalContentSize(sizing);
}

@Override
public int calculateVerticalContentSize(Sizing sizing) {
return this.determineVerticalContentSize(sizing);
}

@Override
Expand All @@ -90,8 +105,8 @@ protected void applySizing() {

final var margins = this.margins.get();

this.width = horizontalSizing.inflate(this.space.width() - margins.horizontal(), this::determineHorizontalContentSize);
this.height = verticalSizing.inflate(this.space.height() - margins.vertical(), this::determineVerticalContentSize);
this.width = horizontalSizing.inflate(this.space.width() - margins.horizontal(), this::calculateHorizontalContentSize);
this.height = verticalSizing.inflate(this.space.height() - margins.vertical(), this::calculateVerticalContentSize);
}

protected void notifyParentIfMounted() {
Expand Down
16 changes: 16 additions & 0 deletions src/main/java/io/wispforest/owo/ui/core/Component.java
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,22 @@ default Component sizing(Sizing sizing) {
@Contract(pure = true)
AnimatableProperty<Sizing> verticalSizing();

/**
* @return The horizontal size this component needs to fit its contents
* @throws UnsupportedOperationException if this component doesn't support horizontal content sizing
*/
default int calculateHorizontalContentSize(Sizing sizing) {
throw new UnsupportedOperationException(this.getClass().getSimpleName() + " does not support Sizing.content() on the horizontal axis");
}

/**
* @return The vertical size this component needs to fit its contents
* @throws UnsupportedOperationException if this component doesn't support vertical content sizing
*/
default int calculateVerticalContentSize(Sizing sizing) {
throw new UnsupportedOperationException(this.getClass().getSimpleName() + " does not support Sizing.content() on the vertical axis");
}

/**
* Set the id of this component. If this is not unique across the hierarchy,
* calls to {@link ParentComponent#childById(Class, String)} may not be deterministic
Expand Down