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

feat: shadow colors #1124

Merged
merged 15 commits into from
Dec 15, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import net.kyori.adventure.key.Key;
import net.kyori.adventure.text.event.ClickEvent;
import net.kyori.adventure.text.event.HoverEventSource;
import net.kyori.adventure.text.format.ShadowColor;
import net.kyori.adventure.text.format.Style;
import net.kyori.adventure.text.format.TextColor;
import net.kyori.adventure.text.format.TextDecoration;
Expand Down Expand Up @@ -236,6 +237,20 @@ private void prepareChildren() {
return (B) this;
}

@Override
@SuppressWarnings("unchecked")
public @NotNull B shadowColor(final @Nullable ShadowColor color) {
this.styleBuilder().shadowColor(color);
return (B) this;
}

@Override
@SuppressWarnings("unchecked")
public @NotNull B shadowColorIfAbsent(final @Nullable ShadowColor color) {
this.styleBuilder().shadowColorIfAbsent(color);
return (B) this;
}

@Override
@SuppressWarnings("unchecked")
public @NotNull B decoration(final @NotNull TextDecoration decoration, final TextDecoration.@NotNull State state) {
Expand Down
33 changes: 33 additions & 0 deletions api/src/main/java/net/kyori/adventure/text/Component.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import net.kyori.adventure.text.event.ClickEvent;
import net.kyori.adventure.text.event.HoverEvent;
import net.kyori.adventure.text.event.HoverEventSource;
import net.kyori.adventure.text.format.ShadowColor;
import net.kyori.adventure.text.format.Style;
import net.kyori.adventure.text.format.StyleBuilderApplicable;
import net.kyori.adventure.text.format.StyleGetter;
Expand Down Expand Up @@ -2189,6 +2190,11 @@ default void detectCycle(final @NotNull Component that) {
return this.style().color();
}

@Override
default @Nullable ShadowColor shadowColor() {
return this.style().shadowColor();
}

/**
* Sets the color of this component.
*
Expand Down Expand Up @@ -2216,6 +2222,33 @@ default void detectCycle(final @NotNull Component that) {
return this;
}

/**
* Sets the shadow color of this component.
*
* @param color the color
* @return a component
* @since 4.18.0
*/
@Contract(pure = true)
@Override
default @NotNull Component shadowColor(final @Nullable ShadowColor color) {
return this.style(this.style().shadowColor(color));
}

/**
* Sets the shadow color if there isn't one set already.
*
* @param color the color
* @return a component
* @since 4.18.0
*/
@Contract(pure = true)
@Override
default @NotNull Component shadowColorIfAbsent(final @Nullable ShadowColor color) {
if (this.shadowColor() == null) return this.shadowColorIfAbsent(color);
return this;
}

/**
* Tests if this component has a decoration.
*
Expand Down
13 changes: 13 additions & 0 deletions api/src/main/java/net/kyori/adventure/text/ScopedComponent.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.util.function.Consumer;
import net.kyori.adventure.text.event.ClickEvent;
import net.kyori.adventure.text.event.HoverEventSource;
import net.kyori.adventure.text.format.ShadowColor;
import net.kyori.adventure.text.format.Style;
import net.kyori.adventure.text.format.TextColor;
import net.kyori.adventure.text.format.TextDecoration;
Expand Down Expand Up @@ -107,6 +108,18 @@ public interface ScopedComponent<C extends Component> extends Component {
return (C) Component.super.colorIfAbsent(color);
}

@Override
@SuppressWarnings("unchecked")
default @NotNull C shadowColor(final @Nullable ShadowColor color) {
return (C) Component.super.shadowColor(color);
}

@Override
@SuppressWarnings("unchecked")
default @NotNull C shadowColorIfAbsent(final @Nullable ShadowColor color) {
return (C) Component.super.shadowColorIfAbsent(color);
}

@Override
@SuppressWarnings("unchecked")
default @NotNull C decorate(final @NotNull TextDecoration decoration) {
Expand Down
70 changes: 70 additions & 0 deletions api/src/main/java/net/kyori/adventure/text/format/ShadowColor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* This file is part of adventure, licensed under the MIT License.
*
* Copyright (c) 2017-2023 KyoriPowered
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package net.kyori.adventure.text.format;

import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;

public interface ShadowColor extends StyleBuilderApplicable {
@Contract(pure = true)
static ShadowColor shadowColor(final int value) {
zml2008 marked this conversation as resolved.
Show resolved Hide resolved
return new ShadowColorImpl(value);
}

@Contract(pure = true)
static ShadowColor fromHexString(String value) {
if (value.startsWith("#")) {
value = value.substring(1);
}
if (value.length() != 8) {
return null;
}
try {
int r = Integer.parseInt(value.substring(0, 2), 16);
int g = Integer.parseInt(value.substring(2, 4), 16);
int b = Integer.parseInt(value.substring(4, 6), 16);
int a = Integer.parseInt(value.substring(6, 8), 16);
return new ShadowColorImpl((a << 24) | (r << 16) | (g << 8) | b);
} catch (NumberFormatException ignored) {
return null;
}
}

@NotNull
default String asHexString() {
int argb = value();
int a = (argb >> 24) & 0xFF;
int r = (argb >> 16) & 0xFF;
int g = (argb >> 8) & 0xFF;
int b = argb & 0xFF;
return String.format("#%02X%02X%02X%02X", r, g, b, a);
}

int value();

@Override
default void styleApply(final Style.@NotNull Builder style) {
style.shadowColor(this);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* This file is part of adventure, licensed under the MIT License.
*
* Copyright (c) 2017-2023 KyoriPowered
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package net.kyori.adventure.text.format;

final class ShadowColorImpl implements ShadowColor {
private final int value;

ShadowColorImpl(final int value) {
this.value = value;
}

@Override
public int value() {
return this.value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,14 @@ public interface StyleGetter {
*/
@Nullable TextColor color();

/**
* Gets the shadow color.
*
* @return the shadow color
* @since 4.18.0
*/
@Nullable ShadowColor shadowColor();

/**
* Tests if this stylable has a decoration.
*
Expand Down
57 changes: 47 additions & 10 deletions api/src/main/java/net/kyori/adventure/text/format/StyleImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,11 @@
import static java.util.Objects.requireNonNull;

final class StyleImpl implements Style {
static final StyleImpl EMPTY = new StyleImpl(null, null, DecorationMap.EMPTY, null, null, null);
static final StyleImpl EMPTY = new StyleImpl(null, null, null, DecorationMap.EMPTY, null, null, null);
// visible to avoid generating accessors when creating a builder
final @Nullable Key font;
final @Nullable TextColor color;
final @Nullable ShadowColor shadowColor;
final @NotNull DecorationMap decorations;
final @Nullable ClickEvent clickEvent;
final @Nullable HoverEvent<?> hoverEvent;
Expand All @@ -52,13 +53,15 @@ final class StyleImpl implements Style {
StyleImpl(
final @Nullable Key font,
final @Nullable TextColor color,
final @Nullable ShadowColor shadowColor,
final @NotNull Map<TextDecoration, TextDecoration.State> decorations,
final @Nullable ClickEvent clickEvent,
final @Nullable HoverEvent<?> hoverEvent,
final @Nullable String insertion
) {
this.font = font;
this.color = color;
this.shadowColor = shadowColor;
this.decorations = DecorationMap.fromMap(decorations);
this.clickEvent = clickEvent;
this.hoverEvent = hoverEvent;
Expand All @@ -73,7 +76,7 @@ final class StyleImpl implements Style {
@Override
public @NotNull Style font(final @Nullable Key font) {
if (Objects.equals(this.font, font)) return this;
return new StyleImpl(font, this.color, this.decorations, this.clickEvent, this.hoverEvent, this.insertion);
return new StyleImpl(font, this.color, this.shadowColor, this.decorations, this.clickEvent, this.hoverEvent, this.insertion);
}

@Override
Expand All @@ -84,7 +87,7 @@ final class StyleImpl implements Style {
@Override
public @NotNull Style color(final @Nullable TextColor color) {
if (Objects.equals(this.color, color)) return this;
return new StyleImpl(this.font, color, this.decorations, this.clickEvent, this.hoverEvent, this.insertion);
return new StyleImpl(this.font, color, this.shadowColor, this.decorations, this.clickEvent, this.hoverEvent, this.insertion);
}

@Override
Expand All @@ -95,6 +98,25 @@ final class StyleImpl implements Style {
return this;
}

@Override
public @Nullable ShadowColor shadowColor() {
return this.shadowColor;
}

@Override
public @NotNull Style shadowColor(final @Nullable ShadowColor color) {
if (Objects.equals(this.shadowColor, color)) return this;
return new StyleImpl(this.font, this.color, color, this.decorations, this.clickEvent, this.hoverEvent, this.insertion);
}

@Override
public @NotNull Style shadowColorIfAbsent(final @Nullable ShadowColor color) {
if (this.shadowColor == null) {
return this.shadowColor(color);
}
return this;
}

@Override
public TextDecoration.@NotNull State decoration(final @NotNull TextDecoration decoration) {
// null -> null
Expand All @@ -109,15 +131,15 @@ final class StyleImpl implements Style {
public @NotNull Style decoration(final @NotNull TextDecoration decoration, final TextDecoration.@NotNull State state) {
requireNonNull(state, "state");
if (this.decoration(decoration) == state) return this;
return new StyleImpl(this.font, this.color, this.decorations.with(decoration, state), this.clickEvent, this.hoverEvent, this.insertion);
return new StyleImpl(this.font, this.color, this.shadowColor, this.decorations.with(decoration, state), this.clickEvent, this.hoverEvent, this.insertion);
}

@Override
public @NotNull Style decorationIfAbsent(final @NotNull TextDecoration decoration, final TextDecoration.@NotNull State state) {
requireNonNull(state, "state");
final TextDecoration.@Nullable State oldState = this.decorations.get(decoration);
if (oldState == TextDecoration.State.NOT_SET) {
return new StyleImpl(this.font, this.color, this.decorations.with(decoration, state), this.clickEvent, this.hoverEvent, this.insertion);
return new StyleImpl(this.font, this.color, this.shadowColor, this.decorations.with(decoration, state), this.clickEvent, this.hoverEvent, this.insertion);
}
if (oldState != null) {
return this;
Expand All @@ -132,7 +154,7 @@ final class StyleImpl implements Style {

@Override
public @NotNull Style decorations(final @NotNull Map<TextDecoration, TextDecoration.State> decorations) {
return new StyleImpl(this.font, this.color, DecorationMap.merge(decorations, this.decorations), this.clickEvent, this.hoverEvent, this.insertion);
return new StyleImpl(this.font, this.color, this.shadowColor, DecorationMap.merge(decorations, this.decorations), this.clickEvent, this.hoverEvent, this.insertion);
}

@Override
Expand All @@ -142,7 +164,7 @@ final class StyleImpl implements Style {

@Override
public @NotNull Style clickEvent(final @Nullable ClickEvent event) {
return new StyleImpl(this.font, this.color, this.decorations, event, this.hoverEvent, this.insertion);
return new StyleImpl(this.font, this.color, this.shadowColor, this.decorations, event, this.hoverEvent, this.insertion);
}

@Override
Expand All @@ -152,7 +174,7 @@ final class StyleImpl implements Style {

@Override
public @NotNull Style hoverEvent(final @Nullable HoverEventSource<?> source) {
return new StyleImpl(this.font, this.color, this.decorations, this.clickEvent, HoverEventSource.unbox(source), this.insertion);
return new StyleImpl(this.font, this.color, this.shadowColor, this.decorations, this.clickEvent, HoverEventSource.unbox(source), this.insertion);
}

@Override
Expand All @@ -163,7 +185,7 @@ final class StyleImpl implements Style {
@Override
public @NotNull Style insertion(final @Nullable String insertion) {
if (Objects.equals(this.insertion, insertion)) return this;
return new StyleImpl(this.font, this.color, this.decorations, this.clickEvent, this.hoverEvent, insertion);
return new StyleImpl(this.font, this.color, this.shadowColor, this.decorations, this.clickEvent, this.hoverEvent, insertion);
}

@Override
Expand Down Expand Up @@ -286,6 +308,7 @@ public int hashCode() {
static final class BuilderImpl implements Builder {
@Nullable Key font;
@Nullable TextColor color;
@Nullable ShadowColor shadowColor;
final Map<TextDecoration, TextDecoration.State> decorations;
@Nullable ClickEvent clickEvent;
@Nullable HoverEvent<?> hoverEvent;
Expand Down Expand Up @@ -324,6 +347,20 @@ static final class BuilderImpl implements Builder {
return this;
}

@Override
public @NotNull Builder shadowColor(final @Nullable ShadowColor color) {
this.shadowColor = color;
return this;
}

@Override
public @NotNull Builder shadowColorIfAbsent(final @Nullable ShadowColor color) {
if (this.shadowColor == null) {
this.shadowColor = color;
}
return this;
}

@Override
public @NotNull Builder decoration(final @NotNull TextDecoration decoration, final TextDecoration.@NotNull State state) {
requireNonNull(state, "state");
Expand Down Expand Up @@ -438,7 +475,7 @@ static final class BuilderImpl implements Builder {
if (this.isEmpty()) {
return EMPTY;
}
return new StyleImpl(this.font, this.color, this.decorations, this.clickEvent, this.hoverEvent, this.insertion);
return new StyleImpl(this.font, this.color, this.shadowColor, this.decorations, this.clickEvent, this.hoverEvent, this.insertion);
}

private boolean isEmpty() {
Expand Down
Loading
Loading