Skip to content

Commit

Permalink
add support for minecraft's color codes
Browse files Browse the repository at this point in the history
  • Loading branch information
moehreag committed Nov 25, 2023
1 parent 5440e3c commit 2aacec0
Show file tree
Hide file tree
Showing 21 changed files with 366 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public static void setContext(long context) {
public static NVGFont getFont() {
if (font == null) {
try {
font = NVGMC.createFont(NVGHolder.class.getResourceAsStream("/Inter-Regular.ttf"));
font = NVGMC.createFont("/Inter-Regular.ttf", "/Inter-Italic.ttf", "/Inter-Bold.ttf", "/Inter-BoldItalic.ttf");
} catch (IOException ignored) {

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,27 @@ public void onInitializeClient() {
subCategory.add(new BooleanOption("some option", true));
example.add(subCategory);
OptionCategory sub2 = OptionCategory.create("sub2");
sub2.add(new BooleanOption("§0colored 1", false));
sub2.add(new BooleanOption("§1colored 2", false));
sub2.add(new BooleanOption("§2colored 3", false));
sub2.add(new BooleanOption("§3colored 4", false));
sub2.add(new BooleanOption("§4colored 5", false));
sub2.add(new BooleanOption("§5colored 6", false));
sub2.add(new BooleanOption("§6colored 7", false));
sub2.add(new BooleanOption("§7colored 8", false));
sub2.add(new BooleanOption("§8colored 9", false));
sub2.add(new BooleanOption("§9colored 10", false));
sub2.add(new BooleanOption("§acolored 11", false));
sub2.add(new BooleanOption("§bcolored 12", false));
sub2.add(new BooleanOption("§ccolored 13", false));
sub2.add(new BooleanOption("§dcolored 14", false));
sub2.add(new BooleanOption("§ecolored 15", false));
sub2.add(new BooleanOption("§fcolored 16", false));
sub2.add(new BooleanOption("§mstrikethrough", false));
sub2.add(new BooleanOption("§lbold", false));
sub2.add(new BooleanOption("§nunderlined", false));
sub2.add(new BooleanOption("§oitalic", false));
sub2.add(new BooleanOption("§rreset", false));
example.add(sub2);
example.add(OptionCategory.create("sub3"));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public static void setContext(long context) {
public static NVGFont getFont() {
if (font == null) {
try {
font = NVGMC.createFont(NVGHolder.class.getResourceAsStream("/Inter-Regular.ttf"));
font = NVGMC.createFont("/Inter-Regular.ttf", "/Inter-Italic.ttf", "/Inter-Bold.ttf", "/Inter-BoldItalic.ttf");
} catch (IOException ignored) {

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,30 @@ public void onInitializeClient(ModContainer mod) {
OptionCategory subCategory = OptionCategory.create("sub-category");
subCategory.add(new BooleanOption("some option", true));
example.add(subCategory);
OptionCategory sub2 = OptionCategory.create("sub2");
sub2.add(new BooleanOption("§0colored 1", false));
sub2.add(new BooleanOption("§1colored 2", false));
sub2.add(new BooleanOption("§2colored 3", false));
sub2.add(new BooleanOption("§3colored 4", false));
sub2.add(new BooleanOption("§4colored 5", false));
sub2.add(new BooleanOption("§5colored 6", false));
sub2.add(new BooleanOption("§6colored 7", false));
sub2.add(new BooleanOption("§7colored 8", false));
sub2.add(new BooleanOption("§8colored 9", false));
sub2.add(new BooleanOption("§9colored 10", false));
sub2.add(new BooleanOption("§acolored 11", false));
sub2.add(new BooleanOption("§bcolored 12", false));
sub2.add(new BooleanOption("§ccolored 13", false));
sub2.add(new BooleanOption("§dcolored 14", false));
sub2.add(new BooleanOption("§ecolored 15", false));
sub2.add(new BooleanOption("§fcolored 16", false));
sub2.add(new BooleanOption("§mstrikethrough", false));
sub2.add(new BooleanOption("§lbold", false));
sub2.add(new BooleanOption("§nunderlined", false));
sub2.add(new BooleanOption("§oitalic", false));
sub2.add(new BooleanOption("§rreset", false));
example.add(sub2);
example.add(OptionCategory.create("sub3"));

example.add(new IntegerOption("integer", 5, 0, 8));
example.add(new FloatOption("float", 3f, 1f, 5f));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public static void setContext(long context) {
public static NVGFont getFont() {
if (font == null) {
try {
font = NVGMC.createFont(NVGHolder.class.getResourceAsStream("/Inter-Regular.ttf"));
font = NVGMC.createFont("/Inter-Regular.ttf", "/Inter-Italic.ttf", "/Inter-Bold.ttf", "/Inter-BoldItalic.ttf");
} catch (IOException ignored) {

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,30 @@ public void onInitializeClient(ModContainer mod) {
OptionCategory subCategory = OptionCategory.create("sub-category");
subCategory.add(new BooleanOption("some option", true));
example.add(subCategory);
OptionCategory sub2 = OptionCategory.create("sub2");
sub2.add(new BooleanOption("§0colored 1", false));
sub2.add(new BooleanOption("§1colored 2", false));
sub2.add(new BooleanOption("§2colored 3", false));
sub2.add(new BooleanOption("§3colored 4", false));
sub2.add(new BooleanOption("§4colored 5", false));
sub2.add(new BooleanOption("§5colored 6", false));
sub2.add(new BooleanOption("§6colored 7", false));
sub2.add(new BooleanOption("§7colored 8", false));
sub2.add(new BooleanOption("§8colored 9", false));
sub2.add(new BooleanOption("§9colored 10", false));
sub2.add(new BooleanOption("§acolored 11", false));
sub2.add(new BooleanOption("§bcolored 12", false));
sub2.add(new BooleanOption("§ccolored 13", false));
sub2.add(new BooleanOption("§dcolored 14", false));
sub2.add(new BooleanOption("§ecolored 15", false));
sub2.add(new BooleanOption("§fcolored 16", false));
sub2.add(new BooleanOption("§mstrikethrough", false));
sub2.add(new BooleanOption("§lbold", false));
sub2.add(new BooleanOption("§nunderlined", false));
sub2.add(new BooleanOption("§oitalic", false));
sub2.add(new BooleanOption("§rreset", false));
example.add(sub2);
example.add(OptionCategory.create("sub3"));

example.add(new IntegerOption("integer", 5, 0, 8));
example.add(new FloatOption("float", 3f, 1f, 5f));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public static void setContext(long context) {
public static NVGFont getFont() {
if (font == null) {
try {
font = NVGMC.createFont(NVGHolder.class.getResourceAsStream("/Inter-Regular.ttf"));
font = NVGMC.createFont("/Inter-Regular.ttf", "/Inter-Italic.ttf", "/Inter-Bold.ttf", "/Inter-BoldItalic.ttf");
} catch (IOException ignored) {

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,30 @@ public void onInitializeClient(ModContainer mod) {
OptionCategory subCategory = OptionCategory.create("sub-category");
subCategory.add(new BooleanOption("some option", true));
example.add(subCategory);
OptionCategory sub2 = OptionCategory.create("sub2");
sub2.add(new BooleanOption("§0colored 1", false));
sub2.add(new BooleanOption("§1colored 2", false));
sub2.add(new BooleanOption("§2colored 3", false));
sub2.add(new BooleanOption("§3colored 4", false));
sub2.add(new BooleanOption("§4colored 5", false));
sub2.add(new BooleanOption("§5colored 6", false));
sub2.add(new BooleanOption("§6colored 7", false));
sub2.add(new BooleanOption("§7colored 8", false));
sub2.add(new BooleanOption("§8colored 9", false));
sub2.add(new BooleanOption("§9colored 10", false));
sub2.add(new BooleanOption("§acolored 11", false));
sub2.add(new BooleanOption("§bcolored 12", false));
sub2.add(new BooleanOption("§ccolored 13", false));
sub2.add(new BooleanOption("§dcolored 14", false));
sub2.add(new BooleanOption("§ecolored 15", false));
sub2.add(new BooleanOption("§fcolored 16", false));
sub2.add(new BooleanOption("§mstrikethrough", false));
sub2.add(new BooleanOption("§lbold", false));
sub2.add(new BooleanOption("§nunderlined", false));
sub2.add(new BooleanOption("§oitalic", false));
sub2.add(new BooleanOption("§rreset", false));
example.add(sub2);
example.add(OptionCategory.create("sub3"));

example.add(new IntegerOption("integer", 5, 0, 8));
example.add(new FloatOption("float", 3f, 1f, 5f));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public static void setContext(long context) {
public static NVGFont getFont() {
if (font == null) {
try {
font = NVGMC.createFont(NVGHolder.class.getResourceAsStream("/Inter-Regular.ttf"));
font = NVGMC.createFont("/Inter-Regular.ttf", "/Inter-Italic.ttf", "/Inter-Bold.ttf", "/Inter-BoldItalic.ttf");
} catch (IOException ignored) {

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,27 @@ public void onInitializeClient(ModContainer mod) {
subCategory.add(new BooleanOption("some option", true));
example.add(subCategory);
OptionCategory sub2 = OptionCategory.create("sub2");
sub2.add(new BooleanOption("§0colored 1", false));
sub2.add(new BooleanOption("§1colored 2", false));
sub2.add(new BooleanOption("§2colored 3", false));
sub2.add(new BooleanOption("§3colored 4", false));
sub2.add(new BooleanOption("§4colored 5", false));
sub2.add(new BooleanOption("§5colored 6", false));
sub2.add(new BooleanOption("§6colored 7", false));
sub2.add(new BooleanOption("§7colored 8", false));
sub2.add(new BooleanOption("§8colored 9", false));
sub2.add(new BooleanOption("§9colored 10", false));
sub2.add(new BooleanOption("§acolored 11", false));
sub2.add(new BooleanOption("§bcolored 12", false));
sub2.add(new BooleanOption("§ccolored 13", false));
sub2.add(new BooleanOption("§dcolored 14", false));
sub2.add(new BooleanOption("§ecolored 15", false));
sub2.add(new BooleanOption("§fcolored 16", false));
sub2.add(new BooleanOption("§mstrikethrough", false));
sub2.add(new BooleanOption("§lbold", false));
sub2.add(new BooleanOption("§nunderlined", false));
sub2.add(new BooleanOption("§oitalic", false));
sub2.add(new BooleanOption("§rreset", false));
example.add(sub2);
example.add(OptionCategory.create("sub3"));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public static void setContext(long context) {
public static NVGFont getFont() {
if (font == null) {
try {
font = NVGMC.createFont(NVGHolder.class.getResourceAsStream("/Inter-Regular.ttf"));
font = NVGMC.createFont("/Inter-Regular.ttf", "/Inter-Italic.ttf", "/Inter-Bold.ttf", "/Inter-BoldItalic.ttf");
} catch (IOException ignored) {

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,27 @@ public void initClient() {
subCategory.add(new BooleanOption("some option", true));
example.add(subCategory);
OptionCategory sub2 = OptionCategory.create("sub2");
sub2.add(new BooleanOption("§0colored 1", false));
sub2.add(new BooleanOption("§1colored 2", false));
sub2.add(new BooleanOption("§2colored 3", false));
sub2.add(new BooleanOption("§3colored 4", false));
sub2.add(new BooleanOption("§4colored 5", false));
sub2.add(new BooleanOption("§5colored 6", false));
sub2.add(new BooleanOption("§6colored 7", false));
sub2.add(new BooleanOption("§7colored 8", false));
sub2.add(new BooleanOption("§8colored 9", false));
sub2.add(new BooleanOption("§9colored 10", false));
sub2.add(new BooleanOption("§acolored 11", false));
sub2.add(new BooleanOption("§bcolored 12", false));
sub2.add(new BooleanOption("§ccolored 13", false));
sub2.add(new BooleanOption("§dcolored 14", false));
sub2.add(new BooleanOption("§ecolored 15", false));
sub2.add(new BooleanOption("§fcolored 16", false));
sub2.add(new BooleanOption("§mstrikethrough", false));
sub2.add(new BooleanOption("§lbold", false));
sub2.add(new BooleanOption("§nunderlined", false));
sub2.add(new BooleanOption("§oitalic", false));
sub2.add(new BooleanOption("§rreset", false));
example.add(sub2);
example.add(OptionCategory.create("sub3"));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@ default void fillCircle(long ctx, float centerX, float centerY, Color color, flo
nvgBeginPath(ctx);
nvgFillColor(ctx, color.toNVG());
nvgArc(ctx, centerX, centerY, radius, nvgDegToRad(startDeg), nvgDegToRad(endDeg), NVG_CW);
//nvgLineTo(ctx, centerX, centerY);
//nvgCircle(ctx, centerX, centerY, radius);
nvgFill(ctx);
}

Expand All @@ -56,7 +54,6 @@ default void outline(long ctx, float x, float y, float width, float height, Colo
default void outlineRoundedRect(long ctx, float x, float y, float width, float height, Color color, float radius, float lineWidth) {
nvgBeginPath(ctx);
nvgStrokeColor(ctx, color.toNVG());
//nvgPathWinding(ctx, NVG_HOLE);
nvgRoundedRect(ctx, x, y, width, height, radius);
nvgStrokeWidth(ctx, lineWidth);
nvgStroke(ctx);
Expand All @@ -66,8 +63,6 @@ default void outlineCircle(long ctx, float centerX, float centerY, Color color,
startDeg -= 90;
endDeg -= 90;
nvgBeginPath(ctx);
//nvgPathWinding(ctx, NVG_HOLE);
//nvgCircle(ctx, centerX, centerY, radius);
nvgArc(ctx, centerX, centerY, radius, nvgDegToRad(startDeg), nvgDegToRad(endDeg), NVG_CW);
nvgStrokeColor(ctx, color.toNVG());
nvgStrokeWidth(ctx, lineWidth);
Expand All @@ -83,9 +78,14 @@ default NVGColor toNVGColor(int alpha, int red, int green, int blue) {
}

default float drawString(long ctx, NVGFont font, String text, float x, float y, Color color) {
nvgFillColor(ctx, color.toNVG());
/*nvgFillColor(ctx, color.toNVG());
font.bind();
return font.renderString(text, x, y);
return font.renderString(text, x, y);*/
return drawStringWithFormatting(ctx, font, text, x, y, color);
}

default float drawStringWithFormatting(long ctx, NVGFont font, String text, float x, float y, Color color){
return FormattingUtil.getInstance().drawStringWithFormatting(ctx, font, text, x, y, color);
}

default float drawCenteredString(long ctx, NVGFont font, String text, float centerX, float y, Color color) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package io.github.axolotlclient.AxolotlClientConfig.impl.ui;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import io.github.axolotlclient.AxolotlClientConfig.api.util.Color;
import org.lwjgl.nanovg.NVGColor;
import org.lwjgl.nanovg.NanoVG;

public class FormattingUtil implements DrawingUtil {
private static final FormattingUtil INSTANCE = new FormattingUtil();
private static final Pattern FORMATTING_CODE_PATTERN = Pattern.compile("(?i)§[0-9A-FK-OR]");
private static final Pattern PARAGRAPH_PATTERN = Pattern.compile("§");
private final Map<Character, NVGColor> MINECRAFT_COLOR_CODES;

static FormattingUtil getInstance() {
return INSTANCE;
}

private FormattingUtil() {
Map<Character, NVGColor> map = new HashMap<>();
map.put('0', toNVGColor(0)); // black
map.put('1', toNVGColor(179)); // dark blue
map.put('2', toNVGColor(43520)); // dark green
map.put('3', toNVGColor(43690)); // dark aqua
map.put('4', toNVGColor(11141120)); // dark red
map.put('5', toNVGColor(11141290)); // dark purple
map.put('6', toNVGColor(16755200)); // gold
map.put('7', toNVGColor(11184810)); // gray
map.put('8', toNVGColor(5592405)); // dark gray
map.put('9', toNVGColor(5592575)); // blue
map.put('a', toNVGColor(5635925)); // green
map.put('b', toNVGColor(5636095)); // aqua
map.put('c', toNVGColor(16733525)); // red
map.put('d', toNVGColor(16733695)); // light_purple
map.put('e', toNVGColor(16777045)); // yellow
map.put('f', toNVGColor(16777215)); // white
MINECRAFT_COLOR_CODES = map;
MINECRAFT_COLOR_CODES.values().forEach(c -> c.a(1));
}


public float drawStringWithFormatting(long ctx, NVGFont font, String text, float x, float y, Color color) {
if (text.contains("§")) { // work out Minecraft's formatting codes
Matcher matcher = FORMATTING_CODE_PATTERN.matcher(text);
if (matcher.find()) {
boolean boldItalicSupported = font instanceof VariantFont;
float lineHeight = font.getLineHeight();
String[] parts = PARAGRAPH_PATTERN.split(text);
NVGColor textColor = color.toNVG();
float partX = x;
float lastPartX;
boolean strikethrough = false, italic = false, bold = false, underlined = false;
for (String part : parts) {
if (part.isEmpty()) {
continue;
}
char first = part.charAt(0);
switch (first) {
case 'm':
strikethrough = true;
break;
case 'k': // obfuscated
break;
case 'l':
bold = boldItalicSupported;
break;
case 'n':
underlined = true;
break;
case 'o':
italic = boldItalicSupported;
break;
case 'r':
strikethrough = italic = bold = underlined = false;
break;
default:
textColor = MINECRAFT_COLOR_CODES.getOrDefault(first, color.toNVG());
}
NanoVG.nvgFillColor(ctx, textColor);
if (bold || italic) { // bold and italic actually are separate fonts
partX = ((VariantFont) font).renderString(part.substring(1), lastPartX = partX, y, italic, bold);
} else {
partX = font.renderString(part.substring(1), lastPartX = partX, y);
}
if (underlined || strikethrough) {
NanoVG.nvgBeginPath(ctx);
if (underlined) {
NanoVG.nvgMoveTo(ctx, lastPartX, y + lineHeight + 1);
NanoVG.nvgLineTo(ctx, partX, y + lineHeight + 1);
}
if (strikethrough) {
NanoVG.nvgMoveTo(ctx, lastPartX, y + lineHeight / 2 + 1);
NanoVG.nvgLineTo(ctx, partX, y + lineHeight / 2 + 1);
}
NanoVG.nvgFill(ctx);
}
}
return partX;
}
}
NanoVG.nvgFillColor(ctx, color.toNVG());
font.bind();
return font.renderString(text, x, y);
}
}
Loading

0 comments on commit 2aacec0

Please sign in to comment.