Skip to content

Commit

Permalink
fine progress bar
Browse files Browse the repository at this point in the history
  • Loading branch information
khjxiaogu committed Dec 27, 2024
1 parent 9a201a4 commit c193536
Show file tree
Hide file tree
Showing 26 changed files with 368 additions and 17 deletions.
21 changes: 18 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ sourceSets {
"$rootDir/src/main/resources",
]
exclude 'assets/frostedheart/lang/**/'
exclude 'assets/frostedheart/font/unifont/*'
}
}
noDatagen{java{srcDirs=[]} resources{srcDirs=[]}}
Expand Down Expand Up @@ -305,7 +306,7 @@ dependencies {
}
import groovy.json.JsonSlurper
import groovy.json.JsonOutput
tasks.register('joinLangTask') {
tasks.register('joinLang') {
ext {
buildDir=new File(project.buildDir,"jointLang");
}
Expand Down Expand Up @@ -333,10 +334,24 @@ tasks.register('joinLangTask') {
}
}
}
processResources.dependsOn(joinLangTask)
tasks.register('zipUnifont', Zip) {
ext {
buildDir=new File(project.buildDir,"zipUnifont");
}
inputs.dir "$rootDir/src/main/resources/assets/frostedheart/font/unifont"
outputs.dir buildDir
archiveFileName = "default.zip"
destinationDirectory = buildDir

from new File("$rootDir/src/main/resources/assets/frostedheart/font/unifont")
}

processResources.dependsOn(joinLang)
processResources.dependsOn(zipUnifont)
tasks.processResources {
duplicatesStrategy = DuplicatesStrategy.WARN
from( tasks.joinLangTask.buildDir){ into 'assets/frostedheart/lang/'}
from( tasks.joinLang.buildDir){ into 'assets/frostedheart/lang/'}
from( tasks.zipUnifont.buildDir){ into 'assets/frostedheart/font/'}
}
jar {
manifest {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
import com.teammoeg.frostedheart.content.health.capability.NutritionCapability;
import com.teammoeg.frostedheart.infrastructure.config.FHConfig;
import com.teammoeg.frostedheart.infrastructure.data.FHDataManager;
import com.teammoeg.frostedheart.util.MultipleRoundHelper;
import com.teammoeg.frostedheart.util.lang.Components;
import com.teammoeg.frostedheart.util.lang.FHTextIcon;
import com.teammoeg.frostedheart.util.lang.FineProgressBarBuilder;
import com.teammoeg.frostedheart.util.lang.Lang;
import com.teammoeg.frostedheart.util.lang.LangBuilder;
import net.minecraft.ChatFormatting;
Expand Down Expand Up @@ -41,7 +43,11 @@ public void modify(ItemTooltipEvent context) {
tooltip.addAll(desc.getCurrentLines());
}
}

static final int FAT_COLOR=0xFFd41c53;
static final int PROTEIN_COLOR=0xFFd4a31c;
static final int CARBOHYDRATE_COLOR=0xFFd4781c;
static final int VEGETABLE_COLOR=0xFF31d41c;
static final int PROGRESS_LENGTH=100;
public static List<Component> getFoodStats(Item item, ItemStack stack, Player player) {
List<Component> list = new ArrayList<>();

Expand All @@ -52,25 +58,26 @@ public static List<Component> getFoodStats(Item item, ItemStack stack, Player pl
.style(ChatFormatting.GRAY)
.addTo(list);
if (foodNutrition != null&&stack.isEdible()) {
NutritionCapability.Nutrition nutrition = foodNutrition.scale(1/foodNutrition.getNutritionValue());

NutritionCapability.Nutrition nutrition = foodNutrition.scale(1/foodNutrition.getNutritionValue()).scale(0.75f);
FineProgressBarBuilder builder=new FineProgressBarBuilder(PROGRESS_LENGTH);
//list.add(Lang.str("\uF504").withStyle(FHTextIcon.applyFont(Style.EMPTY)));
if(nutrition.fat()>0) {
addLine(list,"fat",nutrition.fat(),0xFFd41c53);
builder.addElement(FAT_COLOR, "\uF504",nutrition.fat());
}
if(nutrition.protein()>0) {
addLine(list,"protein",nutrition.protein(),0xFFd4a31c);
builder.addElement(PROTEIN_COLOR, "\uF505",nutrition.protein());
}
if(nutrition.carbohydrate()>0) {
addLine(list,"carbohydrate",nutrition.carbohydrate(),0xFFd4781c);
builder.addElement(CARBOHYDRATE_COLOR, "\uF502",nutrition.carbohydrate());
}
if(nutrition.vegetable()>0) {
addLine(list,"vegetable",nutrition.vegetable(),0xFF31d41c);
builder.addElement(VEGETABLE_COLOR, "\uF503",nutrition.vegetable());
}

list.addAll(builder.build());
}
return list;
}

private static void addLine(List<Component> list,String suffix,float value,int color) {

int progress = Mth.ceil(Mth.clamp(value * 3, 0, 3));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package com.teammoeg.frostedheart.mixin.client;

import java.io.InputStream;

import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Mutable;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
import org.spongepowered.asm.mixin.injection.callback.LocalCapture;

import com.mojang.blaze3d.font.GlyphInfo;
import com.mojang.blaze3d.font.GlyphProvider;
import com.mojang.blaze3d.platform.NativeImage;
import com.mojang.serialization.DataResult;
import com.teammoeg.frostedheart.util.mixin.ChangeAdvanceGlyph;

import it.unimi.dsi.fastutil.ints.IntAVLTreeSet;
import it.unimi.dsi.fastutil.ints.IntSet;
import net.minecraft.client.gui.font.CodepointMap;
import net.minecraft.client.gui.font.providers.BitmapProvider;
import net.minecraft.client.gui.font.providers.BitmapProvider.Glyph;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.packs.resources.ResourceManager;

@Mixin(net.minecraft.client.gui.font.providers.BitmapProvider.Definition.class)
public class MixinBitmapProviderDefinition {
int advance=-256;
@Shadow
@Mutable
int ascent;
public MixinBitmapProviderDefinition() {

}
@Inject(at=@At("HEAD"),method="validate")
private static void fh$validate(BitmapProvider.Definition p_286662_,CallbackInfoReturnable<DataResult<BitmapProvider.Definition>> res){
if(p_286662_.ascent()>0xFF&&p_286662_.ascent()>p_286662_.height()) {
((MixinBitmapProviderDefinition)(Object)p_286662_).advance=((p_286662_.ascent()>>8)&0xff)-1;
if(((MixinBitmapProviderDefinition)(Object)p_286662_).advance>=128)
((MixinBitmapProviderDefinition)(Object)p_286662_).advance=((MixinBitmapProviderDefinition)(Object)p_286662_).advance-256;
//System.out.println("assum advance "+((MixinBitmapProviderDefinition)(Object)p_286662_).advance);
((MixinBitmapProviderDefinition)(Object)p_286662_).ascent&=0Xff;
}

}
@Inject(at=@At(value="INVOKE",target="Lnet/minecraft/client/gui/font/providers/BitmapProvider;<init>(Lcom/mojang/blaze3d/platform/NativeImage;Lnet/minecraft/client/gui/font/CodepointMap;)V"),method="load",locals=LocalCapture.CAPTURE_FAILSOFT)
private void fh$load(ResourceManager p_286694_, CallbackInfoReturnable<GlyphProvider> cir, ResourceLocation resourcelocation, InputStream inputstream, NativeImage nativeimage, int i, int j, int k, int l, float f, CodepointMap codepointmap) {
if(advance>-256) {
IntSet keys=new IntAVLTreeSet(codepointmap.keySet());
//System.out.println("modifying fonts");
keys.forEach(t->{
BitmapProvider.Glyph gi=(Glyph) codepointmap.get(t);
//System.out.println(t+":"+gi);
if(gi!=null)
codepointmap.put(t, new BitmapProvider.Glyph(gi.scale(),gi.image(), gi.offsetX(), gi.offsetY(), gi.width(), gi.height(), advance, gi.ascent()) );
});
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.teammoeg.frostedheart.util;

public class MultipleRoundHelper {
private float reminder=0;
private int maximum;
private int originMax;
public MultipleRoundHelper(int maximum) {
this.originMax=this.maximum=maximum;
}
public int getRounded(float value) {
value-=reminder;
int retval=Math.round(value);
reminder=retval-value;
if(retval>maximum)
retval=maximum;
maximum-=retval;
return retval;
}
public int getReminder() {
return maximum;
}
public int getPercentRounded(float value) {
return getRounded(value*originMax);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.teammoeg.frostedheart.util.lang;

import java.util.ArrayList;
import java.util.List;

import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.MutableComponent;
import net.minecraft.network.chat.Style;

public class ComponentOptimizer {
StringBuilder sb=new StringBuilder();
Style sty=Style.EMPTY;
List<Component> calculated=new ArrayList<>();
public ComponentOptimizer() {

}
public void appendChar(String ch,Style style) {
if(style==sty) {
sb.append(ch);
}else {
createComponent();
sty=style;
}
}
public void createComponent() {
if(sb.length()!=0) {
calculated.add(Lang.str(sb.toString()).withStyle(sty));
sb=new StringBuilder();
}
sty=Style.EMPTY;

}
public Component build() {
createComponent();
MutableComponent mstr=Lang.str("");
for(Component c:calculated) {
mstr.append(c);
}
return mstr;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.MutableComponent;
import net.minecraft.network.chat.Style;
import net.minecraft.resources.ResourceLocation;

/**
Expand Down Expand Up @@ -54,5 +55,7 @@ public MutableComponent appendAfter(Component which) {

/** thermometer icon . */
public static final TextIconType thermometer=new TextIconType("\uF500");

public static Style applyFont(Style style) {
return style.withFont(iconFont);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package com.teammoeg.frostedheart.util.lang;

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

import com.teammoeg.frostedheart.util.MultipleRoundHelper;

import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.MutableComponent;
import net.minecraft.network.chat.Style;

public class FineProgressBarBuilder {
private static record ProgressElement(int color,int len,String icon){

}
private static final Style def_style=FHTextIcon.applyFont(Style.EMPTY);
List<ProgressElement> elm=new ArrayList<>();
MutableComponent parent=Lang.str("");
MultipleRoundHelper rounder;
int len;
public FineProgressBarBuilder(int length) {
len=length;
rounder=new MultipleRoundHelper(length);
}
public FineProgressBarBuilder addElement(int color,String icon,float percent) {
elm.add(new ProgressElement(color,rounder.getPercentRounded(percent),icon));
return this;
}
public List<Component> build() {
if(elm.isEmpty()) {
return Arrays.asList(Lang.str("\uF510"+"\uF512".repeat(len-2)+"\uF510").withStyle(def_style),Component.empty());
}
List<Component> siblings=new ArrayList<>();
StringBuilder sb=new StringBuilder();
int total=0;
for(ProgressElement pe:elm) {
siblings.add(Lang.str("\uF510".repeat(pe.len())).withStyle(t->t.withColor(pe.color)));
total+=pe.len();
//System.out.println(pe.len);
int prelen=(pe.len()-8)/2;
for(int i=0;i<pe.len();i++) {
if(i==prelen&&pe.icon()!=null) {
sb.append(pe.icon());
i+=8;
}else {
sb.append("\uF511");
}
}
}

int remainLen=len-total;
if(remainLen>0) {
siblings.add(Lang.str("\uF512".repeat(remainLen-1)+"\uF510"));
}
MutableComponent mstr=Lang.str("").withStyle(def_style);
siblings.forEach(mstr::append);
return Arrays.asList(mstr,Lang.str(sb.toString()).withStyle(def_style));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import java.util.List;

import com.teammoeg.frostedheart.FHMain;
import com.teammoeg.frostedheart.util.lang.FHTextIcon.TextIconType;

/**
Expand All @@ -30,7 +31,9 @@ public class LangBuilder {
public LangBuilder(String namespace) {
this.namespace = namespace;
}

public LangBuilder() {
this.namespace = FHMain.MODID;
}
public LangBuilder space() {
return text(" ");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.teammoeg.frostedheart.util.mixin;

import java.util.function.Function;

import com.mojang.blaze3d.font.GlyphInfo;
import com.mojang.blaze3d.font.SheetGlyphInfo;

import net.minecraft.client.gui.font.glyphs.BakedGlyph;

public record ChangeAdvanceGlyph(int advance,GlyphInfo nested) implements GlyphInfo {


@Override
public float getAdvance() {
return advance;
}

@Override
public BakedGlyph bake(Function<SheetGlyphInfo, BakedGlyph> p_231088_) {
return nested.bake(p_231088_);
}

}
4 changes: 3 additions & 1 deletion src/main/resources/META-INF/accesstransformer.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,6 @@ public net.minecraft.world.level.levelgen.structure.templatesystem.StructurePlac
public net.minecraft.world.damagesource.DamageSource f_19330_ # hungerDamage
public net.minecraftforge.common.capabilities.CapabilityProvider serializeCaps
public net.minecraft.client.renderer.GameRenderer m_109141_(Lnet/minecraft/client/Camera;FZ)D # getFov
public net.minecraftforge.common.capabilities.CapabilityManager providers
public net.minecraftforge.common.capabilities.CapabilityManager providers
public net.minecraft.client.gui.font.providers.BitmapProvider$Glyph
public net.minecraft.client.gui.font.providers.BitmapProvider$Glyph <init>(FLcom/mojang/blaze3d/platform/NativeImage;IIIIII)V # <init>
Loading

0 comments on commit c193536

Please sign in to comment.