Skip to content

Commit b44e8fe

Browse files
committed
TODO: Chat emoji
1 parent 469a44c commit b44e8fe

File tree

3 files changed

+60
-4
lines changed

3 files changed

+60
-4
lines changed

common/src/main/java/com/lambda/mixin/render/ChatScreenMixin.java

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package com.lambda.mixin.render;
22

3+
import com.lambda.Lambda;
34
import com.lambda.command.CommandManager;
45
import com.lambda.graphics.renderer.gui.font.FontRenderer;
56
import com.lambda.graphics.renderer.gui.font.LambdaEmoji;
67
import com.lambda.graphics.renderer.gui.font.glyph.GlyphInfo;
8+
import com.lambda.module.modules.client.LambdaMoji;
9+
import com.lambda.util.math.Vec2d;
710
import kotlin.Pair;
811
import kotlin.ranges.IntRange;
912
import net.minecraft.client.gui.screen.ChatScreen;
@@ -13,16 +16,22 @@
1316
import org.spongepowered.asm.mixin.injection.ModifyArg;
1417
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
1518

19+
import java.util.ArrayList;
1620
import java.util.Collections;
1721
import java.util.List;
1822

1923
@Mixin(ChatScreen.class)
2024
public abstract class ChatScreenMixin {
2125
@ModifyArg(method = "sendMessage", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/network/ClientPlayNetworkHandler;sendChatMessage(Ljava/lang/String;)V"), index = 0)
2226
private String modifyChatText(String chatText) {
27+
if (LambdaMoji.INSTANCE.isDisabled()) return chatText;
28+
2329
List<Pair<GlyphInfo, IntRange>> emojis = FontRenderer.Companion.parseEmojis(chatText, LambdaEmoji.Twemoji);
2430
Collections.reverse(emojis);
2531

32+
List<String> pushEmojis = new ArrayList<>();
33+
List<Vec2d> pushPositions = new ArrayList<>();
34+
2635
for (Pair<GlyphInfo, IntRange> emoji : emojis) {
2736
String emojiString = chatText.substring(emoji.getSecond().getStart() + 1, emoji.getSecond().getEndInclusive());
2837
if (LambdaEmoji.Twemoji.get(emojiString) == null)
@@ -33,10 +42,20 @@ private String modifyChatText(String chatText) {
3342
// and render it after the text
3443
chatText = chatText.substring(0, emoji.getSecond().getStart()) + " " + chatText.substring(emoji.getSecond().getEndInclusive() + 1);
3544

36-
// TODO: Build a renderer for the emojis
37-
// TODO: Render the emojis at their correct position
45+
// We cannot retain the position in the future, but we can
46+
// assume that every time you send a message the height of
47+
// the position will change by the height of the glyph
48+
// The positions are from the top left corner of the screen
49+
int x = Lambda.getMc().textRenderer.getWidth(chatText.substring(0, emoji.getSecond().getStart()));
50+
int y = Lambda.getMc().textRenderer.fontHeight;
51+
52+
pushEmojis.add(String.format(":%s:", emojiString));
53+
pushPositions.add(new Vec2d(x, y));
3854
}
3955

56+
// Not optimal because it has to parse the emoji again but who cares
57+
LambdaMoji.INSTANCE.add(pushEmojis, pushPositions);
58+
4059
return chatText;
4160
}
4261

common/src/main/kotlin/com/lambda/graphics/renderer/gui/font/FontRenderer.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ class FontRenderer(
5656
* The values are hardcoded
5757
* We do not need to ask the emoji font since the height is smaller
5858
*/
59-
private fun getHeight(scale: Double = 1.0) = font.glyphs.fontHeight * getScaleFactor(scale) * 0.7
59+
fun getHeight(scale: Double = 1.0) = font.glyphs.fontHeight * getScaleFactor(scale) * 0.7
6060

6161
/**
6262
* Iterates over each character and emoji in the text.
Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,48 @@
11
package com.lambda.module.modules.client
22

3+
import com.lambda.event.events.RenderEvent
4+
import com.lambda.event.events.TickEvent
5+
import com.lambda.event.listener.SafeListener.Companion.listener
6+
import com.lambda.gui.api.RenderLayer
37
import com.lambda.module.Module
48
import com.lambda.module.tag.ModuleTag
9+
import com.lambda.util.math.Vec2d
510

611
object LambdaMoji : Module(
712
name = "LambdaMoji",
813
description = "",
914
defaultTags = setOf(ModuleTag.CLIENT, ModuleTag.RENDER),
1015
enabledByDefault = true,
11-
)
16+
) {
17+
private val scale by setting("Emoji Scale", 1.0, 0.5..2.0, 0.1)
18+
19+
private val renderer = RenderLayer()
20+
private val renderQueue = hashMapOf<List<String>, List<Vec2d>>()
21+
22+
init {
23+
listener<TickEvent.Pre> {
24+
var index = 0
25+
renderQueue.forEach { (emojis, positions) ->
26+
emojis.forEachIndexed { emojiIndex, emoji ->
27+
val pos = positions[emojiIndex]
28+
29+
renderer.font.build(
30+
text = emoji,
31+
position = Vec2d(pos.x, pos.y * (index.toDouble() + 1)),
32+
scale = scale,
33+
)
34+
}
35+
36+
index++
37+
}
38+
}
39+
40+
listener<RenderEvent.GUI.Fixed> {
41+
renderer.render()
42+
}
43+
}
44+
45+
fun add(emojis: List<String>, positions: List<Vec2d>) {
46+
renderQueue[emojis] = positions
47+
}
48+
}

0 commit comments

Comments
 (0)