feat: implemented custom colors for GlyphComponentBuilder + fixed DialogueRenderer
This commit is contained in:
parent
4877ea4547
commit
36eb128e85
@ -337,17 +337,17 @@ public class DialogueRenderer extends Inventory {
|
|||||||
boolean endWithDots = lineIdx + 1 == GlyphPositions.answerText.maxLines() && lineIdx + 1 != lines.size();
|
boolean endWithDots = lineIdx + 1 == GlyphPositions.answerText.maxLines() && lineIdx + 1 != lines.size();
|
||||||
var line = lines.get(lineIdx);
|
var line = lines.get(lineIdx);
|
||||||
|
|
||||||
builder.append(
|
builder.setColorTo(switch (number) {
|
||||||
number.getIndex() % 2 == 0
|
|
||||||
? GlyphPositions.answerText.leftLineX()
|
|
||||||
: GlyphPositions.answerText.rightLineX(),
|
|
||||||
line.toGlyphList(line.textureProperties(), 0, endWithDots, switch (number) {
|
|
||||||
case BUTTON_1 -> theme.getColorAnswerText1();
|
case BUTTON_1 -> theme.getColorAnswerText1();
|
||||||
case BUTTON_2 -> theme.getColorAnswerText2();
|
case BUTTON_2 -> theme.getColorAnswerText2();
|
||||||
case BUTTON_3 -> theme.getColorAnswerText3();
|
case BUTTON_3 -> theme.getColorAnswerText3();
|
||||||
case BUTTON_4 -> theme.getColorAnswerText4();
|
case BUTTON_4 -> theme.getColorAnswerText4();
|
||||||
})
|
}).append(
|
||||||
);
|
number.getIndex() % 2 == 0
|
||||||
|
? GlyphPositions.answerText.leftLineX()
|
||||||
|
: GlyphPositions.answerText.rightLineX(),
|
||||||
|
line.toGlyphList(line.textureProperties(), 0, endWithDots)
|
||||||
|
).resetColor();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -400,12 +400,12 @@ public class DialogueRenderer extends Inventory {
|
|||||||
|
|
||||||
for (int lineIdx = shift; (lineIdx - shift) < Math.min(textLines.size() - shift, GlyphPositions.phraseText.maxLines()); lineIdx++) {
|
for (int lineIdx = shift; (lineIdx - shift) < Math.min(textLines.size() - shift, GlyphPositions.phraseText.maxLines()); lineIdx++) {
|
||||||
var line = textLines.get(lineIdx);
|
var line = textLines.get(lineIdx);
|
||||||
builder.append(GlyphPositions.phraseText.lineX(), line.toGlyphList(
|
builder.setColorTo(getTheme().getColorText())
|
||||||
|
.append(GlyphPositions.phraseText.lineX(), line.toGlyphList(
|
||||||
line.textureProperties(),
|
line.textureProperties(),
|
||||||
shift,
|
shift,
|
||||||
textLines.size() - lineIdx > 0 && lineIdx - shift == GlyphPositions.phraseText.maxLines() - 1,
|
textLines.size() - lineIdx > 0 && lineIdx - shift == GlyphPositions.phraseText.maxLines() - 1
|
||||||
getTheme().getColorText()
|
)).resetColor();
|
||||||
));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -440,7 +440,7 @@ public class DialogueRenderer extends Inventory {
|
|||||||
|
|
||||||
private record TextLine(DialogueTheme theme, String text, TextureProperties textureProperties) {
|
private record TextLine(DialogueTheme theme, String text, TextureProperties textureProperties) {
|
||||||
|
|
||||||
public GlyphComponent toGlyphList(TextureProperties parentProperties, int lineShift, boolean cutEnding, TextColor color) {
|
public GlyphComponent toGlyphList(TextureProperties parentProperties, int lineShift, boolean cutEnding) {
|
||||||
var properties = new TextureProperties(
|
var properties = new TextureProperties(
|
||||||
parentProperties.height(),
|
parentProperties.height(),
|
||||||
parentProperties.ascent() + lineShift * (textureProperties().height() + 1)
|
parentProperties.ascent() + lineShift * (textureProperties().height() + 1)
|
||||||
|
|||||||
@ -1,8 +1,10 @@
|
|||||||
package ru.dragonestia.msb3.resource.glyph;
|
package ru.dragonestia.msb3.resource.glyph;
|
||||||
|
|
||||||
import net.kyori.adventure.text.Component;
|
import net.kyori.adventure.text.Component;
|
||||||
|
import net.kyori.adventure.text.TextComponent;
|
||||||
import net.kyori.adventure.text.format.NamedTextColor;
|
import net.kyori.adventure.text.format.NamedTextColor;
|
||||||
import net.kyori.adventure.text.format.Style;
|
import net.kyori.adventure.text.format.Style;
|
||||||
|
import net.kyori.adventure.text.format.TextColor;
|
||||||
import net.kyori.adventure.text.format.TextDecoration;
|
import net.kyori.adventure.text.format.TextDecoration;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
@ -16,9 +18,9 @@ import java.util.List;
|
|||||||
*/
|
*/
|
||||||
public class GlyphComponentBuilder {
|
public class GlyphComponentBuilder {
|
||||||
|
|
||||||
private static final Entry NEW_LINE = new Entry(Position.ABSOLUTE, 0, new Text("\n", 0));
|
private static final Container NEW_LINE = new Container(EntryType.COMPONENT, new EntryComponent(Position.ABSOLUTE, 0, new Text("\n", 0)));
|
||||||
|
|
||||||
private final List<Entry> entries = new ArrayList<>();
|
private final List<Container> entries = new ArrayList<>();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Добавить компонент после предыдущего компонента
|
* Добавить компонент после предыдущего компонента
|
||||||
@ -47,7 +49,7 @@ public class GlyphComponentBuilder {
|
|||||||
* @return Текущий объект билдера
|
* @return Текущий объект билдера
|
||||||
*/
|
*/
|
||||||
public GlyphComponentBuilder append(Position position, int offset, GlyphComponent component) {
|
public GlyphComponentBuilder append(Position position, int offset, GlyphComponent component) {
|
||||||
entries.add(new Entry(position, offset, component));
|
entries.add(new Container(EntryType.COMPONENT, new EntryComponent(position, offset, component)));
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -63,11 +65,35 @@ public class GlyphComponentBuilder {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Установить новый цвет для последующих глифов. Не окрашивает старые компоненты глифов
|
||||||
|
*
|
||||||
|
* <p> Применяется также для цветных изображений. Во избежание неправильного отображения цвета у глифа
|
||||||
|
* советуется сбрасывать цвет
|
||||||
|
*
|
||||||
|
* @param color Цвет
|
||||||
|
* @return Текущий объект билдера
|
||||||
|
*/
|
||||||
|
public GlyphComponentBuilder setColorTo(TextColor color) {
|
||||||
|
entries.add(new Container(EntryType.COLOR, new EntryColor(color)));
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Сбросить цветовой код глифов.
|
||||||
|
*
|
||||||
|
* <p> В основном используется, для того чтобы избежать проблемы с отображением цветных глифов
|
||||||
|
* @return Текущий объект билдера
|
||||||
|
*/
|
||||||
|
public GlyphComponentBuilder resetColor() {
|
||||||
|
return setColorTo(NamedTextColor.WHITE);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Собрать все компоненты глифов в один текстовый компонент
|
* Собрать все компоненты глифов в один текстовый компонент
|
||||||
* @return Текстовый компонент
|
* @return Текстовый компонент
|
||||||
*/
|
*/
|
||||||
public Component build() {
|
public TextComponent build() {
|
||||||
return build(false);
|
return build(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -76,15 +102,7 @@ public class GlyphComponentBuilder {
|
|||||||
* @param resetPosition Возвращать ли смещение на начало?
|
* @param resetPosition Возвращать ли смещение на начало?
|
||||||
* @return Текстовый компонент
|
* @return Текстовый компонент
|
||||||
*/
|
*/
|
||||||
public Component build(boolean resetPosition) {
|
public TextComponent build(boolean resetPosition) {
|
||||||
var sb = new StringBuilder();
|
|
||||||
var text = buildAsGlyphComponent();
|
|
||||||
|
|
||||||
sb.append(text.content());
|
|
||||||
if (resetPosition) {
|
|
||||||
sb.append(Spacing.getAsString(-text.width()));
|
|
||||||
}
|
|
||||||
|
|
||||||
var style = Style.style()
|
var style = Style.style()
|
||||||
.color(NamedTextColor.WHITE)
|
.color(NamedTextColor.WHITE)
|
||||||
.decoration(TextDecoration.ITALIC, false)
|
.decoration(TextDecoration.ITALIC, false)
|
||||||
@ -93,44 +111,67 @@ public class GlyphComponentBuilder {
|
|||||||
.decoration(TextDecoration.STRIKETHROUGH, false)
|
.decoration(TextDecoration.STRIKETHROUGH, false)
|
||||||
.decoration(TextDecoration.OBFUSCATED, false)
|
.decoration(TextDecoration.OBFUSCATED, false)
|
||||||
.build();
|
.build();
|
||||||
|
var builder = Component.text().style(style);
|
||||||
return Component.text(sb.toString(), style);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Собрать все компоненты глифов в один компонент глифа
|
|
||||||
* @return Компонент глифа
|
|
||||||
*/
|
|
||||||
public GlyphComponent buildAsGlyphComponent() {
|
|
||||||
var builder = new StringBuilder();
|
|
||||||
|
|
||||||
int offset = 0;
|
int offset = 0;
|
||||||
|
TextColor lastColor = NamedTextColor.WHITE;
|
||||||
|
|
||||||
for (var entry: entries) {
|
for (var entry: entries) {
|
||||||
var component = entry.glyphComponent;
|
switch (entry.type()) {
|
||||||
var val = entry.offset;
|
case COMPONENT -> {
|
||||||
switch (entry.position) {
|
var obj = entry.asComponent();
|
||||||
|
var component = obj.glyphComponent;
|
||||||
|
var val = obj.offset;
|
||||||
|
switch (obj.position) {
|
||||||
case ABSOLUTE -> {
|
case ABSOLUTE -> {
|
||||||
builder.append(Spacing.getAsString(val - offset));
|
builder.append(Spacing.get(val - offset));
|
||||||
offset = val;
|
offset = val;
|
||||||
}
|
}
|
||||||
|
|
||||||
case RELATIVE -> {
|
case RELATIVE -> {
|
||||||
if (val == 0) {
|
if (val == 0) {
|
||||||
builder.append(Spacing.getAsString(val));
|
builder.append(Spacing.get(val));
|
||||||
}
|
}
|
||||||
offset += val;
|
offset += val;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
offset += entry.glyphComponent.width() + 1;
|
offset += obj.glyphComponent.width() + 1;
|
||||||
builder.append(component.content());
|
builder.append(Component.text(component.content(), lastColor));
|
||||||
}
|
}
|
||||||
|
|
||||||
return new Text(builder.toString(), offset);
|
case COLOR -> lastColor = entry.asColor().color();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private record Entry(Position position, int offset, GlyphComponent glyphComponent) {}
|
if (resetPosition) {
|
||||||
|
builder.append(Spacing.get(-offset));
|
||||||
|
}
|
||||||
|
|
||||||
|
return builder.build();
|
||||||
|
}
|
||||||
|
|
||||||
|
private enum EntryType {
|
||||||
|
COMPONENT,
|
||||||
|
COLOR,
|
||||||
|
}
|
||||||
|
|
||||||
|
private interface Entry {}
|
||||||
|
|
||||||
|
private record EntryComponent(Position position, int offset, GlyphComponent glyphComponent) implements Entry {}
|
||||||
|
|
||||||
|
private record EntryColor(TextColor color) implements Entry {}
|
||||||
|
|
||||||
|
private record Container(EntryType type, Entry obj) {
|
||||||
|
|
||||||
|
EntryComponent asComponent() {
|
||||||
|
return (EntryComponent) obj;
|
||||||
|
}
|
||||||
|
|
||||||
|
EntryColor asColor() {
|
||||||
|
return (EntryColor) obj;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private static class Text implements GlyphComponent {
|
private static class Text implements GlyphComponent {
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user