Skip to content

Commit

Permalink
Update Textbox to align itself, and use that for PushableTextLines
Browse files Browse the repository at this point in the history
Signed-off-by: Rina Mary Shaw <[email protected]>
  • Loading branch information
Pixaurora committed Nov 30, 2024
1 parent d6e68e7 commit 9cbddbf
Show file tree
Hide file tree
Showing 21 changed files with 237 additions and 177 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package net.pixaurora.kitten_cube.impl.math;

public interface Size extends Vec2Int {
public static Size ZERO = Size.of(0, 0);

public static Size of(int width, int height) {
return new SizeImpl(width, height);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import net.pixaurora.kitten_cube.impl.text.Color;
import net.pixaurora.kitten_cube.impl.text.Component;
import net.pixaurora.kitten_cube.impl.ui.screen.align.Alignment;
import net.pixaurora.kitten_cube.impl.ui.widget.text.TextBox;

public class AlignedGuiDisplay extends WrappedGuiDisplay {
private final Alignment alignment;
Expand Down Expand Up @@ -41,4 +42,8 @@ public void drawTexture(ResourcePath path, int width, int height, int x, int y)
super.drawTexture(path, width, height, this.alignX(x, y), this.alignY(x, y));
}

@Override
public void drawTextBox(TextBox box) {
super.drawTextBox(box, this.alignment, this.window);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import net.pixaurora.kitten_cube.impl.math.Size;
import net.pixaurora.kitten_cube.impl.text.Color;
import net.pixaurora.kitten_cube.impl.text.Component;
import net.pixaurora.kitten_cube.impl.ui.screen.align.Alignment;
import net.pixaurora.kitten_cube.impl.ui.texture.GuiTexture;
import net.pixaurora.kitten_cube.impl.ui.texture.Texture;
import net.pixaurora.kitten_cube.impl.ui.widget.text.TextBox;
Expand All @@ -17,7 +18,11 @@ public void drawGuiTextureSubsection(ResourcePath path, int width, int height, i

public void drawText(Component text, Color color, int x, int y, boolean shadowed);

public void drawTextBox(TextBox box);
public void drawTextBox(TextBox box, Alignment alignment, Size window);

public default void drawTextBox(TextBox box) {
this.drawTextBox(box, Alignment.TOP_LEFT, Size.ZERO);
}

public default void drawTexture(ResourcePath path, Size size, Point pos) {
this.drawTexture(path, size.width(), size.height(), pos.x(), pos.y());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package net.pixaurora.kitten_cube.impl.ui.display;

import net.pixaurora.kit_tunes.api.resource.ResourcePath;
import net.pixaurora.kitten_cube.impl.math.Size;
import net.pixaurora.kitten_cube.impl.text.Color;
import net.pixaurora.kitten_cube.impl.text.Component;
import net.pixaurora.kitten_cube.impl.ui.screen.align.Alignment;
import net.pixaurora.kitten_cube.impl.ui.widget.text.TextBox;

public class WrappedGuiDisplay implements GuiDisplay {
Expand Down Expand Up @@ -34,7 +36,7 @@ public void drawText(Component text, Color color, int x, int y, boolean shadowed
}

@Override
public void drawTextBox(TextBox box) {
this.parent.drawTextBox(box);
public void drawTextBox(TextBox box, Alignment alignment, Size window) {
this.parent.drawTextBox(box, alignment, window);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ public Point pos() {
return this.pos;
}

public Size size() {
return this.widget.size();
}

private Optional<Alignment> customizedAlignment() {
if (this.baseAlignment.isPresent()) {
return this.baseAlignment;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,23 @@ public interface Alignment {

public int inverseAlignY(int x, int y, Size window);

public default Point inverseAlign(Point pos, Size window) {
int x = pos.x();
int y = pos.y();
public default int alignX(Point pos, Size window) {
return this.alignX(pos.x(), pos.y(), window);
}

return Point.of(this.inverseAlignX(x, y, window), this.inverseAlignY(x, y, window));
public default int alignY(Point pos, Size window) {
return this.alignY(pos.x(), pos.y(), window);
}

public default int inverseAlignX(Point pos, Size window) {
return this.inverseAlignX(pos.x(), pos.y(), window);
}

public default int inverseAlignY(Point pos, Size window) {
return this.inverseAlignY(pos.x(), pos.y(), window);
}

public default Point inverseAlign(Point pos, Size window) {
return Point.of(this.inverseAlignX(pos, window), this.inverseAlignY(pos, window));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,12 @@ public int anchorY(Widget widget) {
public static class MiddleLeft implements WidgetAnchor {
@Override
public int anchorX(Widget widget) {
return widget.size().width();
return 0;
}

@Override
public int anchorY(Widget widget) {
return 0;
return widget.size().height() / 2;
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package net.pixaurora.kitten_cube.impl.ui.widget.text;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Optional;

import net.pixaurora.kitten_cube.impl.MinecraftClient;
import net.pixaurora.kitten_cube.impl.math.Point;
import net.pixaurora.kitten_cube.impl.math.Size;
import net.pixaurora.kitten_cube.impl.text.Color;
Expand All @@ -15,61 +13,60 @@
import net.pixaurora.kitten_cube.impl.ui.texture.GuiTexture;
import net.pixaurora.kitten_cube.impl.ui.tile.InnerTileGrid;
import net.pixaurora.kitten_cube.impl.ui.tile.PositionedInnerTile;
import net.pixaurora.kitten_cube.impl.ui.widget.IncorporealWidget;
import net.pixaurora.kitten_cube.impl.ui.widget.Widget;
import net.pixaurora.kitten_heart.impl.KitTunes;
import net.pixaurora.kitten_heart.impl.util.Pair;

public class PushableTextLines implements IncorporealWidget {
// TODO: render this from (0, 0) like everything else

public class PushableTextLines implements Widget {
private static final TextLinesBackground REGULAR_BACKGROUND = new TextLinesBackground(Point.of(7, 5),
new InnerTileGrid(
GuiTexture.of(KitTunes.resource("textures/gui/sprites/widget/textbox.png"), Size.of(20, 17)),
Point.of(6, 4), Size.of(6, 9)),
Size.of(6, 2));

private final Optional<TextLinesBackground> backgroundType;
private final List<Component> lines;
private Color color;

private final List<PositionedText> lines;
private final List<PositionedInnerTile> backgroundTiles;
private ImmutableTextDisplay display;

public PushableTextLines(Optional<TextLinesBackground> backgroundType) {
this.backgroundType = backgroundType;
this.lines = new ArrayList<>();
this.backgroundTiles = new ArrayList<>();
this.color = Color.WHITE;
this.resetDisplay();
}

public static PushableTextLines regular() {
return new PushableTextLines(Optional.of(REGULAR_BACKGROUND));
}

private int height() {
return this.lines.size() * MinecraftClient.textHeight();
}

public Point endPos() {
return Point.ZERO.offset(0, this.height());
}

public void clear() {
this.lines.clear();
}

public void push(Component text, Color color) {
Point newLinePos = MinecraftClient.textSize(text).centerHorizontally(Point.ZERO).offset(0, this.height());
this.lines.add(new PositionedText(text, color, newLinePos));
public void setColor(Color color) {
this.color = color;
}

public void push(Component text) {
this.lines.add(text);

this.updateBackground();
this.resetDisplay();
}

@Override
public void draw(GuiDisplay gui, Point mousePos) {
for (PositionedInnerTile tile : this.backgroundTiles) {
for (PositionedInnerTile tile : this.display.tiles) {
tile.draw(gui);
}

for (PositionedText line : this.lines) {
gui.drawText(line.text(), line.color(), line.pos());
}
gui.drawTextBox(this.display.textBox);
}

@Override
public Size size() {
return this.display.size;
}

@Override
Expand All @@ -81,36 +78,43 @@ public boolean isWithinBounds(Point mousePos) {
return false;
}

private void updateBackground() {
this.backgroundTiles.clear();
private void resetDisplay() {
Point textStart;
if (this.backgroundType.isPresent()) {
textStart = this.backgroundType.get().textStart();
} else {
textStart = Point.ZERO;
}

TextLinesBackground background = this.backgroundType.get();
TextBox textBox = TextBox.of(this.lines, color, textStart);
Size displaySize = textBox.size().offset(textStart);

InnerTileGrid grid = background.grid();
Point startPos = this.topLeftLinePoint().offset(background.textStart().scaledBy(-1));
Size totalSize = this.linesSize().offset(background.textStart()).offset(background.bottomRightPadding());
List<PositionedInnerTile> tiles;
if (this.backgroundType.isPresent()) {
TextLinesBackground background = this.backgroundType.get();

this.backgroundTiles.addAll(grid.tilesAndSize(startPos, totalSize).first());
}
Pair<List<PositionedInnerTile>, Size> tilesAndSize = background
.grid()
.tilesAndSize(Point.ZERO, displaySize.offset(background.bottomRightPadding()));

private Size linesSize() {
return MinecraftClient
.textSize(this.lines.stream().map(PositionedText::text).toArray(size -> new Component[size]));
}

private Point topLeftLinePoint() {
Iterator<PositionedText> lines = this.lines.iterator();
tiles = tilesAndSize.first();
displaySize = tilesAndSize.second();
} else {
tiles = new ArrayList<>();
}

PositionedText line = lines.next();
Point topLeftPoint = line.pos();
this.display = new ImmutableTextDisplay(textBox, tiles, displaySize);
}

while (lines.hasNext()) {
line = lines.next();
private static class ImmutableTextDisplay {
private final TextBox textBox;
private final List<PositionedInnerTile> tiles;
private final Size size;

Point linePos = line.pos();
topLeftPoint = Point.of(Math.min(topLeftPoint.x(), linePos.x()), Math.min(topLeftPoint.y(), linePos.y()));
ImmutableTextDisplay(TextBox textBox, List<PositionedInnerTile> tiles, Size size) {
this.textBox = textBox;
this.tiles = tiles;
this.size = size;
}

return topLeftPoint;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,9 @@ public static TextBox of(List<Component> lines, Color color, int maxLineLength,
return KitTunes.UI_LAYER.createTextbox(lines, color, maxLineLength, pos);
}

public static TextBox of(List<Component> lines, Color color, Point pos) {
return of(lines, color, 256, pos);
}

public Size size();
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import net.pixaurora.kit_tunes.api.resource.ResourcePath;
import net.pixaurora.kitten_cube.impl.math.Point;
import net.pixaurora.kitten_cube.impl.math.Size;
import net.pixaurora.kitten_cube.impl.text.Color;
import net.pixaurora.kitten_cube.impl.text.Component;
import net.pixaurora.kitten_cube.impl.ui.screen.Screen;
import net.pixaurora.kitten_cube.impl.ui.screen.WidgetContainer;
Expand Down Expand Up @@ -104,8 +103,8 @@ public DisplayMode createMusicDisplay(PlayingSong song) {
.anchor(WidgetAnchor.TOP_MIDDLE);

WidgetContainer<Timer> timer = this.addWidget(new Timer(song))
.align(Alignment.CENTER_BOTTOM)
.at(Point.of(0, -13));
.align(progressBar.relativeTo(WidgetAnchor.BOTTOM_MIDDLE))
.anchor(WidgetAnchor.TOP_MIDDLE);

Optional<Album> album = song.track().flatMap(Track::album);

Expand All @@ -116,20 +115,23 @@ public DisplayMode createMusicDisplay(PlayingSong song) {
WidgetContainer<StaticTexture> albumArt = this
.addWidget(
new StaticTexture(Texture.of(albumArtTexture, iconSize)))
.at(iconSize.centerOn(Point.of(-70, 0)));
.anchor(WidgetAnchor.MIDDLE_RIGHT)
.at(Point.of(-10, 0));

WidgetContainer<PushableTextLines> songInfo = this.addWidget(PushableTextLines.regular()).at(Point.of(70, -8));
WidgetContainer<PushableTextLines> songInfo = this.addWidget(PushableTextLines.regular())
.anchor(WidgetAnchor.MIDDLE_LEFT)
.at(Point.of(10, 0));

if (song.track().isPresent()) {
Track track = song.track().get();

songInfo.get().push(asComponent(track), Color.WHITE);
songInfo.get().push(asComponent(track.artist()), Color.WHITE);
songInfo.get().push(asComponent(track));
songInfo.get().push(asComponent(track.artist()));
if (album.isPresent()) {
songInfo.get().push(asComponent(track.album().get()), Color.WHITE);
songInfo.get().push(asComponent(track.album().get()));
}
} else {
songInfo.get().push(Component.literal("No track found :("), Color.RED);
songInfo.get().push(Component.literal("No track found :("));
}

WidgetContainer<PauseButton> pauseButton = this
Expand Down
Loading

0 comments on commit 9cbddbf

Please sign in to comment.