Skip to content

Commit

Permalink
Add CommentToolScreen UI
Browse files Browse the repository at this point in the history
  • Loading branch information
zbx1425 committed Jul 24, 2023
1 parent e153f79 commit 97c97a7
Show file tree
Hide file tree
Showing 12 changed files with 203 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ protected void init() {

radioButtons.clear();
for (int r = 0; r < 2; r++) {
addRenderableWidget(new WidgetLabel(
SQ_SIZE, baseY,
SQ_SIZE * 4, Component.translatable("gui.worldcomment.comment_type.r" + (r + 1))
addRenderableWidget(new WidgetFlagLabel(
SQ_SIZE - 4, baseY, CommentTypeButton.BTN_WIDTH * 4 + 10, SQ_SIZE / 2,
0xFF2196F3, Component.translatable("gui.worldcomment.comment_type.r" + (r + 1))
));
for (int c = 0; c < 4; c++) {
CommentTypeButton selectBtn = new CommentTypeButton(
Expand All @@ -86,11 +86,11 @@ protected void init() {
}
baseY += CommentTypeButton.BTN_HEIGHT + SQ_SIZE / 2;
}
baseY += SQ_SIZE / 2;
// baseY += SQ_SIZE / 2;

addRenderableWidget(new WidgetLabel(
SQ_SIZE, baseY,
CommentTypeButton.BTN_WIDTH * 4, Component.translatable("gui.worldcomment.message")
addRenderableWidget(new WidgetFlagLabel(
SQ_SIZE - 4, baseY, CommentTypeButton.BTN_WIDTH * 4 + 10, SQ_SIZE / 2,
0xFF00BCD4, Component.translatable("gui.worldcomment.message")
));
baseY += SQ_SIZE / 2;
textBoxMessage = new MultiLineEditBox(
Expand All @@ -103,8 +103,11 @@ protected void init() {
baseY += textBoxMessage.getHeight();
baseY += SQ_SIZE / 2;

btnSendFeedback = Button.builder(Component.translatable("gui.worldcomment.submit"),
sender -> sendReport()).pos(SQ_SIZE, baseY).width(CommentTypeButton.BTN_WIDTH * 4).build();
btnSendFeedback = new WidgetColorButton(
SQ_SIZE, baseY, CommentTypeButton.BTN_WIDTH * 4, SQ_SIZE,
Component.translatable("gui.worldcomment.submit"), 0xFFC5E1A5,
sender -> sendReport()
);
btnSendFeedback.active = selectedCommentType != 0;
addRenderableWidget(btnSendFeedback);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,57 @@
package cn.zbx1425.worldcomment.gui;

import cn.zbx1425.worldcomment.Main;
import com.mojang.blaze3d.systems.RenderSystem;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.components.Button;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.util.Mth;

import java.util.function.Supplier;

public class CommentTypeButton extends Button {

private static final ResourceLocation ATLAS_LOCATION = new ResourceLocation(Main.MOD_ID, "textures/gui/comment-tool.png");

private static final int[] COMMENT_TYPE_COLOR = {
0xFF8BC34A, 0xFFCDDC39, 0xFFFFEB3B, 0xFFFF9800,
0xFF607D8B, 0xFFFFC107, 0xFF03A9F4, 0xFF009888
};

public int commentType;
public int topColor;

public static int BTN_WIDTH = 40;
public static int BTN_HEIGHT = 40;

protected CommentTypeButton(int x, int y, int type, OnPress onPress) {
super(x, y, BTN_WIDTH, 20,
public CommentTypeButton(int x, int y, int type, OnPress onPress) {
super(x, y, BTN_WIDTH, BTN_HEIGHT,
Component.translatable("gui.worldcomment.comment_type." + type),
onPress, Supplier::get);
this.commentType = type;
this.topColor = COMMENT_TYPE_COLOR[type - 1];
}

@Override
protected void renderWidget(GuiGraphics guiGraphics, int mouseX, int mouseY, float partialTick) {
Minecraft minecraft = Minecraft.getInstance();
RenderSystem.enableBlend();
guiGraphics.setColor(((topColor >> 16) & 0xFF) / 255f, ((topColor >> 8) & 0xFF) / 255f,
(topColor & 0xFF) / 255f, 1);
guiGraphics.blit(ATLAS_LOCATION, getX(), getY(), getWidth(), 12,
active ? 0 : 40, 58, 40, 12, 256, 256);
guiGraphics.setColor(1, 1, 1, 1);
guiGraphics.blit(ATLAS_LOCATION, getX(), getY() + 12, getWidth(), 28,
active ? 0 : 40, 70, 40, 28, 256, 256);
if (isHovered && active) {
guiGraphics.blit(ATLAS_LOCATION, getX(), getY(), getWidth(), 40,
80, 58, 40, 40, 256, 256);
}
guiGraphics.blit(ATLAS_LOCATION, getX() + 8 - 1, getY() + 14 - 1, 24, 24,
((commentType - 1) % 4) * 64, (int)((commentType - 1) / 4) * 64 + 128, 64, 64, 256, 256);
renderScrollingString(guiGraphics, minecraft.font, getMessage(),
getX(), getY(), getX() + getWidth(), getY() + 12, active ? 0xFFFFFFFF : 0xFFA0A0A0);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package cn.zbx1425.worldcomment.gui;

import com.mojang.blaze3d.systems.RenderSystem;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.components.Button;
import net.minecraft.network.chat.Component;
import net.minecraft.util.Mth;

import java.util.function.Supplier;

public class WidgetColorButton extends Button {

int color;

public WidgetColorButton(int i, int j, int k, int l, Component component, int color, OnPress onPress) {
super(i, j, k, l, component, onPress, Supplier::get);
this.color = color;
}

@Override
protected void renderWidget(GuiGraphics guiGraphics, int mouseX, int mouseY, float partialTick) {
Minecraft minecraft = Minecraft.getInstance();
if (this.active) {
guiGraphics.setColor(((color >> 16) & 0xFF) / 255f, ((color >> 8) & 0xFF) / 255f,
(color & 0xFF) / 255f, this.alpha);
} else {
guiGraphics.setColor(1.0f, 1.0f, 1.0f, this.alpha);
}
RenderSystem.enableBlend();
RenderSystem.enableDepthTest();
guiGraphics.blitNineSliced(WIDGETS_LOCATION, this.getX(), this.getY(), this.getWidth(), this.getHeight(), 20, 4, 200, 20, 0, this.getTextureY());
guiGraphics.setColor(1.0f, 1.0f, 1.0f, 1.0f);
int i = this.active ? 0xFFFFFF : 0xA0A0A0;
this.renderString(guiGraphics, minecraft.font, i | Mth.ceil(this.alpha * 255.0f) << 24);
}

private int getTextureY() {
int i = 1;
if (!this.active) {
i = 0;
} else if (this.isHoveredOrFocused()) {
i = 2;
}
return 46 + i * 20;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package cn.zbx1425.worldcomment.gui;

import cn.zbx1425.worldcomment.data.CommentEntry;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.components.AbstractWidget;
import net.minecraft.client.gui.narration.NarrationElementOutput;
import net.minecraft.network.chat.Component;

public class WidgetCommentEntry extends AbstractWidget {

private final CommentEntry comment;

public static final int TOP_SINK = 10;

public WidgetCommentEntry(int x, int y, int width, CommentEntry comment) {
super(x, y, width, 0, Component.literal(comment.message));
this.comment = comment;
}

@Override
protected void renderWidget(GuiGraphics guiGraphics, int mouseX, int mouseY, float partialTick) {

}

@Override
protected void updateWidgetNarration(NarrationElementOutput narrationElementOutput) {

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package cn.zbx1425.worldcomment.gui;

import cn.zbx1425.worldcomment.Main;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;

public class WidgetFlagLabel extends WidgetLabel {

private static final ResourceLocation ATLAS_LOCATION = new ResourceLocation(Main.MOD_ID, "textures/gui/comment-tool.png");

public int color;

public WidgetFlagLabel(int x, int y, int width, int height, int color, Component text) {
super(x, y, width, height, text);
this.color = color;
this.padding = 6;
}

@Override
public void renderWidget(GuiGraphics guiGraphics, int mouseX, int mouseY, float delta) {
guiGraphics.setColor(((color >> 16) & 0xFF) / 255f, ((color >> 8) & 0xFF) / 255f,
(color & 0xFF) / 255f, 1);
guiGraphics.blit(ATLAS_LOCATION, getX(), getY(), 10, getHeight(),
0, 48, 20, 10, 256, 256);
guiGraphics.blit(ATLAS_LOCATION, getX() + 10, getY(), getWidth() - 20, getHeight(),
10, 48, 108, 10, 256, 256);
guiGraphics.blit(ATLAS_LOCATION, getX() + getWidth() - 10, getY(), 10, getHeight(),
118, 48, 10, 10, 256, 256);
guiGraphics.setColor(1, 1, 1, 1);
super.renderWidget(guiGraphics, mouseX, mouseY, delta);
}
}
34 changes: 23 additions & 11 deletions common/src/main/java/cn/zbx1425/worldcomment/gui/WidgetLabel.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,17 @@ public class WidgetLabel extends AbstractWidget {

public boolean alignR = false;

public int padding = 0;

private final Runnable onClick;

public WidgetLabel(int x, int y, int width, Component text) {
super(x, y, width, 10, text);
public WidgetLabel(int x, int y, int width, int height, Component text) {
super(x, y, width, height, text);
this.onClick = null;
}

public WidgetLabel(int x, int y, int width, Component text, Runnable onClick) {
super(x, y, width, 10, text);
public WidgetLabel(int x, int y, int width, int height, Component text, Runnable onClick) {
super(x, y, width, height, text);
this.onClick = onClick;
}

Expand All @@ -41,19 +43,20 @@ public void render(PoseStack matrices, int mouseX, int mouseY, float delta) {
if (!visible) return;
String[] lines = this.getMessage().getString().split("\n");
this.height = lines.length * 10;
int textStart = Math.max(getY(), getY() + (getHeight() - 10 * lines.length) / 2);
for (int i = 0; i < lines.length; ++i) {
int textWidth = Minecraft.getInstance().font.width(lines[i]);
#if MC_VERSION >= "11903"
int x = alignR ? this.getX() + this.getWidth() - textWidth : this.getX();
int y = this.getY() + 10 * i;
int x = alignR ? this.padX() + this.padWidth() - textWidth : this.padX();
int y = textStart + 10 * i;
#else
int x = alignR ? this.getX() + this.width - textWidth : this.getX();
int y = this.getY() + 10 * i;
int x = alignR ? this.padX() + this.width - textWidth : this.padX();
int y = textStart + 10 * i;
#endif
if (textWidth > this.width) {
if (textWidth > this.padWidth()) {
int offset = (int)(System.currentTimeMillis() / 25 % (textWidth + 40));
#if MC_VERSION >= "12000"
guiGraphics.enableScissor(this.getX(), this.getY(), this.getX() + this.width, this.getY() + this.height);
guiGraphics.enableScissor(this.padX(), this.getY(), this.padX() + this.padWidth(), this.getY() + this.height);
guiGraphics.drawString(Minecraft.getInstance().font, lines[i], x - offset, y, -1);
guiGraphics.drawString(Minecraft.getInstance().font, lines[i], x + textWidth + 40 - offset, y, -1);
guiGraphics.disableScissor();
Expand Down Expand Up @@ -102,4 +105,13 @@ protected int getY() {
return y;
}
#endif
}


private int padX() {
return this.getX() + padding;
}

private int padWidth() {
return this.getWidth() - padding * 2;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package cn.zbx1425.worldcomment.mixin;

import net.minecraft.client.gui.Gui;
import net.minecraft.client.gui.GuiGraphics;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.Slice;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

@Mixin(Gui.class)
public class InGameHudMixin {

@Inject(method = "render", at = @At(value = "TAIL"), slice = @Slice(from = @At(value = "INVOKE", target = "Lnet/minecraft/client/gui/components/PlayerTabOverlay;render(Lnet/minecraft/client/gui/GuiGraphics;ILnet/minecraft/world/scores/Scoreboard;Lnet/minecraft/world/scores/Objective;)V")))
public void render(GuiGraphics guiGraphics, float partialTick, CallbackInfo callbackInfo) {

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,13 @@
import cn.zbx1425.worldcomment.Main;
import cn.zbx1425.worldcomment.data.CommentEntry;
import io.netty.buffer.Unpooled;
import it.unimi.dsi.fastutil.objects.Object2ObjectArrayMap;
import net.minecraft.Util;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.level.ChunkPos;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.UUID;

public class PacketSubmitCommentC2S {

Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"package": "cn.zbx1425.worldcomment.mixin",
"compatibilityLevel": "JAVA_8",
"client": [

"InGameHudMixin"
],
"injectors": {
"defaultRequire": 1
Expand Down
2 changes: 1 addition & 1 deletion fabric/src/main/resources/fabric.mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
]
},
"mixins": [
"worldcomment.mixins.json"
"worldcomment.mixins.json"
],
"depends": {
"fabric": "*",
Expand Down
12 changes: 0 additions & 12 deletions forge/src/main/resources/worldcomment.mixins.json

This file was deleted.

0 comments on commit 97c97a7

Please sign in to comment.