Skip to content

Commit

Permalink
multi value gradient slider
Browse files Browse the repository at this point in the history
  • Loading branch information
Goby56 committed Oct 16, 2024
1 parent 923a8e1 commit b2d5a80
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 31 deletions.
14 changes: 11 additions & 3 deletions src/main/java/com/goby56/wakes/config/WakesConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,19 @@
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;

public class WakesConfig {

public ArrayList<Float> wakeGradientRanges = new ArrayList<>(List.of(-40f, -10f, 10f, 40f));
public ArrayList<WakeColor> wakeColors = new ArrayList<>(List.of(
new WakeColor(255, 0, 0, 255),
new WakeColor(255, 255, 0, 255),
new WakeColor(255, 255, 255, 255),
new WakeColor(0, 255, 255, 255),
new WakeColor(0, 0, 255, 255)
));

// Spawning
public Map<String, EffectSpawningRule> effectSpawningRules = new HashMap<>(Map.of(
"boat", EffectSpawningRule.SIMULATION_AND_PLANES,
Expand Down
34 changes: 34 additions & 0 deletions src/main/java/com/goby56/wakes/config/WakesConfigScreen.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.goby56.wakes.config;

import com.goby56.wakes.WakesClient;
import com.goby56.wakes.config.gui.GradientSlider;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.gui.widget.ButtonWidget;
import net.minecraft.client.gui.widget.LayoutWidgets;
import net.minecraft.client.gui.widget.SliderWidget;
import net.minecraft.client.option.SimpleOption;
import net.minecraft.text.Text;

public class WakesConfigScreen extends Screen {

public WakesConfigScreen() {
super(Text.literal("Wakes Config"));
}

@Override
protected void init() {
this.addDrawableChild(new GradientSlider((int) (width / 2 - width * 0.8f / 2), 10, (int) (width * 0.8f), 40, Text.literal("Gradient slider"), WakesClient.CONFIG_INSTANCE.wakeGradientRanges));
}

@Override
protected void applyBlur(float delta) {
// No Song 2
}

@Override
public void render(DrawContext context, int mouseX, int mouseY, float delta) {
super.render(context, mouseX, mouseY, delta);
context.drawCenteredTextWithShadow(textRenderer, Text.literal("You must see me"), width / 2, height / 2, 0xffffff);
}
}
57 changes: 57 additions & 0 deletions src/main/java/com/goby56/wakes/config/gui/GradientSlider.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.goby56.wakes.config.gui;

import com.goby56.wakes.WakesClient;
import com.mojang.blaze3d.systems.RenderSystem;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.gui.widget.SliderWidget;
import net.minecraft.text.Text;
import net.minecraft.util.math.MathHelper;

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

public class GradientSlider extends SliderWidget {
private ArrayList<Float> values;

public GradientSlider(int x, int y, int width, int height, Text text, ArrayList<Float> values) {
super(x, y, width, height, text, 0f);
this.values = values;
}

@Override
public void renderWidget(DrawContext context, int mouseX, int mouseY, float delta) {
MinecraftClient minecraftClient = MinecraftClient.getInstance();
RenderSystem.enableBlend();
RenderSystem.defaultBlendFunc();
RenderSystem.enableDepthTest();

context.drawGuiTexture(this.getTexture(), this.getX(), this.getY(), this.getWidth(), this.getHeight());
context.setShaderColor(1.0F, 1.0F, 1.0F, 0.3f);

values = new ArrayList<>(List.of(0f, 0.1f, 0.5f, 0.7f, 1f));
for (int i = 1; i < values.size(); i++) {
float value = values.get(i - 1);
float diff = values.get(i) - value;
int x = this.getX() + (int)(value * (double)(this.width - 8));
int y = this.getY();
context.fill(x, y + 1, (int) (x + (diff * (this.width - 8))) + 8, y + this.height - 1, WakesClient.CONFIG_INSTANCE.wakeColors.get(i - 1).argb);
context.drawGuiTexture(this.getHandleTexture(), x, y, 8, this.getHeight());
}
context.drawGuiTexture(this.getHandleTexture(), this.getX() + (int)(this.value * (double)(this.width - 8)), this.getY(), 8, this.getHeight());
context.setShaderColor(1.0F, 1.0F, 1.0F, 1.0F);
int i = this.active ? 16777215 : 10526880;
this.drawScrollableText(context, minecraftClient.textRenderer, 2, i | MathHelper.ceil(this.alpha * 255.0F) << 24);
}

@Override
protected void updateMessage() {
this.setMessage(Text.of(String.valueOf(this.value)));
}

@Override
protected void applyValue() {

}
}
50 changes: 23 additions & 27 deletions src/main/java/com/goby56/wakes/debug/DebugCommand.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.goby56.wakes.debug;

import com.goby56.wakes.particle.ModParticles;
import com.goby56.wakes.event.WakeWorldTicker;
import com.goby56.wakes.simulation.WakeHandler;
import com.goby56.wakes.simulation.WakeNode;
import com.mojang.brigadier.CommandDispatcher;
Expand All @@ -12,43 +12,45 @@
import net.minecraft.client.color.world.BiomeColors;
import net.minecraft.client.render.WorldRenderer;
import net.minecraft.command.CommandRegistryAccess;
import net.minecraft.registry.tag.FluidTags;
import net.minecraft.text.Text;
import net.minecraft.util.hit.HitResult;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.ColorHelper;
import net.minecraft.util.math.Vec3d;
import net.minecraft.world.World;

import java.util.Optional;

public class DebugCommand {
public static void register(CommandDispatcher<FabricClientCommandSource> dispatcher, CommandRegistryAccess registryAccess) {
dispatcher.register(ClientCommandManager.literal("wakes_debug")
.then(ClientCommandManager.literal("light")
.executes(DebugCommand::lightCoordinate))
.then(ClientCommandManager.literal("color")
.executes(DebugCommand::waterColor))
.then(ClientCommandManager.literal("spawn")
.then(ClientCommandManager.literal("node")
.then(ClientCommandManager.argument("flood_level", IntegerArgumentType.integer(0, 5))
.executes(DebugCommand::spawnWakeNode)))));
//.then(ClientCommandManager.literal("splash_cloud_particle")
//.executes(DebugCommand::spawnSplashCloudParticle)))));
dispatcher.register(ClientCommandManager.literal("wakes")
.then(ClientCommandManager.literal("debug")
.then(ClientCommandManager.literal("light")
.executes(DebugCommand::lightCoordinate))
.then(ClientCommandManager.literal("color")
.executes(DebugCommand::waterColor))
.then(ClientCommandManager.literal("spawn")
.then(ClientCommandManager.literal("node")
.then(ClientCommandManager.argument("flood_level", IntegerArgumentType.integer(0, 5))
.executes(DebugCommand::spawnWakeNode)))))
.then(ClientCommandManager.literal("config")
.executes(DebugCommand::openConfig)));
}

public static int openConfig(CommandContext<FabricClientCommandSource> cmdCtx) throws CommandSyntaxException {
WakeWorldTicker.openConfigScreenNextTick = true;
return 1;
}

public static int spawnWakeNode(CommandContext<FabricClientCommandSource> cmdCtx) throws CommandSyntaxException {
WakeHandler wakeHandler = WakeHandler.getInstance().orElse(null);
if (wakeHandler == null) return 0;
HitResult result = cmdCtx.getSource().getPlayer().raycast(10, 0, true);
Vec3d pos = result.getPos();
if (!result.getType().equals(HitResult.Type.BLOCK)) return 0;
if (!cmdCtx.getSource().getWorld().getFluidState(new BlockPos((int) pos.x, (int) Math.floor(pos.y), (int) pos.z)).isIn(FluidTags.WATER)) return 0;
WakeNode node = new WakeNode(result.getPos(), 100);
node.floodLevel = cmdCtx.getArgument("flood_level", Integer.class);
wakeHandler.insert(node);
Optional<WakeHandler> wh = WakeHandler.getInstance();
wh.ifPresent(wakeHandler -> wakeHandler.insert(node));
return 1;
}


public static int lightCoordinate(CommandContext<FabricClientCommandSource> cmdCtx) throws CommandSyntaxException {
World world = cmdCtx.getSource().getWorld();
BlockPos blockPos = cmdCtx.getSource().getPlayer().getBlockPos();
Expand All @@ -68,10 +70,4 @@ public static int waterColor(CommandContext<FabricClientCommandSource> cmdCtx) t
ColorHelper.Argb.getBlue(col))));
return 1;
}

public static int spawnSplashCloudParticle(CommandContext<FabricClientCommandSource> cmdCtx) throws CommandSyntaxException {
Vec3d pos = cmdCtx.getSource().getPosition();
cmdCtx.getSource().getWorld().addParticle(ModParticles.SPLASH_CLOUD, pos.x, pos.y, pos.z, 0, 0, 0);
return 1;
}
}
}
8 changes: 8 additions & 0 deletions src/main/java/com/goby56/wakes/event/WakeWorldTicker.java
Original file line number Diff line number Diff line change
@@ -1,22 +1,30 @@
package com.goby56.wakes.event;

import com.goby56.wakes.WakesClient;
import com.goby56.wakes.config.WakesConfigScreen;
import com.goby56.wakes.render.SplashPlaneRenderer;
import com.goby56.wakes.simulation.WakeHandler;
import com.goby56.wakes.debug.WakesDebugInfo;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
import net.fabricmc.fabric.api.entity.event.v1.ServerEntityWorldChangeEvents;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.world.ClientWorld;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.server.world.ServerWorld;

public class WakeWorldTicker implements ClientTickEvents.EndWorldTick, ServerEntityWorldChangeEvents.AfterPlayerChange {
public static boolean openConfigScreenNextTick = false;

@Override
public void onEndTick(ClientWorld world) {
WakesClient.areShadersEnabled = WakesClient.areShadersEnabled();
WakesDebugInfo.reset();
SplashPlaneRenderer.tick();
WakeHandler.getInstance().ifPresent(WakeHandler::tick);
if (openConfigScreenNextTick) {
MinecraftClient.getInstance().setScreenAndRender(new WakesConfigScreen());
openConfigScreenNextTick = false;
}
}

@Override
Expand Down
5 changes: 4 additions & 1 deletion src/main/resources/wakes.accesswidener
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,7 @@ accessible field net/minecraft/entity/vehicle/BoatEntity NEXT_PADDLE_PHASE F

accessible field net/minecraft/client/model/ModelPart$Cuboid sides [Lnet/minecraft/client/model/ModelPart$Quad;

accessible field net/minecraft/client/render/LightmapTextureManager image Lnet/minecraft/client/texture/NativeImage;
accessible field net/minecraft/client/render/LightmapTextureManager image Lnet/minecraft/client/texture/NativeImage;

accessible method net/minecraft/client/gui/widget/SliderWidget getHandleTexture ()Lnet/minecraft/util/Identifier;
accessible method net/minecraft/client/gui/widget/SliderWidget getTexture ()Lnet/minecraft/util/Identifier;

0 comments on commit b2d5a80

Please sign in to comment.