1
0
forked from Clones/Controlify

Add radial icons to the public-facing API

This commit is contained in:
isXander
2023-08-11 23:13:02 +01:00
parent e4b651f9e0
commit e3f50ea552
5 changed files with 40 additions and 17 deletions

View File

@ -55,6 +55,15 @@ public interface ControlifyBindingsApi {
*/ */
void excludeVanillaBind(KeyMapping... keyMapping); void excludeVanillaBind(KeyMapping... keyMapping);
/**
* Registers a radial icon to be used in the radial menu.
* The identifier should be passed to {@link ControllerBindingBuilder#radialCandidate(ResourceLocation)}.
*
* @param id the identifier for the icon, the namespace should be your modid.
* @param icon the renderer for the icon.
*/
void registerRadialIcon(ResourceLocation id, RadialIcon icon);
static ControlifyBindingsApi get() { static ControlifyBindingsApi get() {
return ControllerBindings.Api.INSTANCE; return ControllerBindings.Api.INSTANCE;
} }

View File

@ -0,0 +1,8 @@
package dev.isxander.controlify.api.bind;
import net.minecraft.client.gui.GuiGraphics;
@FunctionalInterface
public interface RadialIcon {
void draw(GuiGraphics graphics, int x, int y, float tickDelta);
}

View File

@ -5,6 +5,7 @@ import dev.isxander.controlify.Controlify;
import dev.isxander.controlify.api.bind.ControlifyBindingsApi; import dev.isxander.controlify.api.bind.ControlifyBindingsApi;
import dev.isxander.controlify.api.bind.ControllerBinding; import dev.isxander.controlify.api.bind.ControllerBinding;
import dev.isxander.controlify.api.bind.ControllerBindingBuilder; import dev.isxander.controlify.api.bind.ControllerBindingBuilder;
import dev.isxander.controlify.api.bind.RadialIcon;
import dev.isxander.controlify.controller.Controller; import dev.isxander.controlify.controller.Controller;
import dev.isxander.controlify.controller.ControllerState; import dev.isxander.controlify.controller.ControllerState;
import dev.isxander.controlify.api.event.ControlifyEvents; import dev.isxander.controlify.api.event.ControlifyEvents;
@ -616,5 +617,10 @@ public class ControllerBindings<T extends ControllerState> {
public void excludeVanillaBind(KeyMapping... keyMappings) { public void excludeVanillaBind(KeyMapping... keyMappings) {
EXCLUDED_VANILLA_BINDS.addAll(Arrays.asList(keyMappings)); EXCLUDED_VANILLA_BINDS.addAll(Arrays.asList(keyMappings));
} }
@Override
public void registerRadialIcon(ResourceLocation id, RadialIcon icon) {
RadialIcons.registerIcon(id, icon);
}
} }
} }

View File

@ -1,8 +1,8 @@
package dev.isxander.controlify.bindings; package dev.isxander.controlify.bindings;
import dev.isxander.controlify.api.bind.RadialIcon;
import net.minecraft.Util; import net.minecraft.Util;
import net.minecraft.client.Minecraft; import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.renderer.texture.TextureAtlasSprite; import net.minecraft.client.renderer.texture.TextureAtlasSprite;
import net.minecraft.client.resources.MobEffectTextureManager; import net.minecraft.client.resources.MobEffectTextureManager;
import net.minecraft.core.registries.BuiltInRegistries; import net.minecraft.core.registries.BuiltInRegistries;
@ -21,11 +21,11 @@ public final class RadialIcons {
public static final ResourceLocation EMPTY = new ResourceLocation("controlify", "empty"); public static final ResourceLocation EMPTY = new ResourceLocation("controlify", "empty");
public static final ResourceLocation FABRIC_ICON = new ResourceLocation("fabric-resource-loader-v0", "icon.png"); public static final ResourceLocation FABRIC_ICON = new ResourceLocation("fabric-resource-loader-v0", "icon.png");
private static final Map<ResourceLocation, Icon> icons = Util.make(() -> { private static final Map<ResourceLocation, RadialIcon> icons = Util.make(() -> {
Map<ResourceLocation, Icon> map = new HashMap<>(); Map<ResourceLocation, RadialIcon> map = new HashMap<>();
map.put(EMPTY, (graphics, x, y) -> {}); map.put(EMPTY, (graphics, x, y, tickDelta) -> {});
map.put(FABRIC_ICON, (graphics, x, y) -> { map.put(FABRIC_ICON, (graphics, x, y, tickDelta) -> {
graphics.pose().pushPose(); graphics.pose().pushPose();
graphics.pose().translate(x, y, 0); graphics.pose().translate(x, y, 0);
graphics.pose().scale(0.5f, 0.5f, 1f); graphics.pose().scale(0.5f, 0.5f, 1f);
@ -38,10 +38,14 @@ public final class RadialIcons {
return map; return map;
}); });
public static Map<ResourceLocation, Icon> getIcons() { public static Map<ResourceLocation, RadialIcon> getIcons() {
return icons; return icons;
} }
public static void registerIcon(ResourceLocation location, RadialIcon icon) {
icons.put(location, icon);
}
public static ResourceLocation getItem(Item item) { public static ResourceLocation getItem(Item item) {
return prefixLocation("item", BuiltInRegistries.ITEM.getKey(item)); return prefixLocation("item", BuiltInRegistries.ITEM.getKey(item));
} }
@ -50,18 +54,18 @@ public final class RadialIcons {
return prefixLocation("effect", BuiltInRegistries.MOB_EFFECT.getKey(effect)); return prefixLocation("effect", BuiltInRegistries.MOB_EFFECT.getKey(effect));
} }
private static void addItems(Map<ResourceLocation, Icon> map) { private static void addItems(Map<ResourceLocation, RadialIcon> map) {
BuiltInRegistries.ITEM.entrySet().forEach(entry -> { BuiltInRegistries.ITEM.entrySet().forEach(entry -> {
ResourceKey<Item> key = entry.getKey(); ResourceKey<Item> key = entry.getKey();
ItemStack stack = entry.getValue().getDefaultInstance(); ItemStack stack = entry.getValue().getDefaultInstance();
map.put(prefixLocation("item", key.location()), (graphics, x, y) -> { map.put(prefixLocation("item", key.location()), (graphics, x, y, tickDelta) -> {
graphics.renderItem(stack, x, y); graphics.renderItem(stack, x, y);
}); });
}); });
} }
private static void addPotionEffects(Map<ResourceLocation, Icon> map) { private static void addPotionEffects(Map<ResourceLocation, RadialIcon> map) {
MobEffectTextureManager mobEffectTextureManager = minecraft.getMobEffectTextures(); MobEffectTextureManager mobEffectTextureManager = minecraft.getMobEffectTextures();
BuiltInRegistries.MOB_EFFECT.entrySet().forEach(entry -> { BuiltInRegistries.MOB_EFFECT.entrySet().forEach(entry -> {
@ -69,7 +73,7 @@ public final class RadialIcons {
MobEffect effect = entry.getValue(); MobEffect effect = entry.getValue();
TextureAtlasSprite sprite = mobEffectTextureManager.get(effect); TextureAtlasSprite sprite = mobEffectTextureManager.get(effect);
map.put(prefixLocation("effect", key.location()), (graphics, x, y) -> { map.put(prefixLocation("effect", key.location()), (graphics, x, y, tickDelta) -> {
graphics.pose().pushPose(); graphics.pose().pushPose();
graphics.pose().translate(x, y, 0); graphics.pose().translate(x, y, 0);
graphics.pose().scale(0.88f, 0.88f, 1f); graphics.pose().scale(0.88f, 0.88f, 1f);
@ -84,9 +88,4 @@ public final class RadialIcons {
private static ResourceLocation prefixLocation(String prefix, ResourceLocation location) { private static ResourceLocation prefixLocation(String prefix, ResourceLocation location) {
return new ResourceLocation(location.getNamespace(), prefix + "/" + location.getPath()); return new ResourceLocation(location.getNamespace(), prefix + "/" + location.getPath());
} }
@FunctionalInterface
public interface Icon {
void draw(GuiGraphics graphics, int x, int y);
}
} }

View File

@ -3,6 +3,7 @@ package dev.isxander.controlify.gui.screen;
import dev.isxander.controlify.Controlify; import dev.isxander.controlify.Controlify;
import dev.isxander.controlify.api.bind.BindRenderer; import dev.isxander.controlify.api.bind.BindRenderer;
import dev.isxander.controlify.api.bind.ControllerBinding; import dev.isxander.controlify.api.bind.ControllerBinding;
import dev.isxander.controlify.api.bind.RadialIcon;
import dev.isxander.controlify.bindings.RadialIcons; import dev.isxander.controlify.bindings.RadialIcons;
import dev.isxander.controlify.controller.Controller; import dev.isxander.controlify.controller.Controller;
import dev.isxander.controlify.gui.guide.GuideAction; import dev.isxander.controlify.gui.guide.GuideAction;
@ -213,7 +214,7 @@ public class RadialMenuScreen extends Screen implements ScreenControllerEventLis
private boolean focused; private boolean focused;
private ControllerBinding binding; private ControllerBinding binding;
private MultiLineLabel name; private MultiLineLabel name;
private RadialIcons.Icon icon; private RadialIcon icon;
private RadialButton(int index, float x, float y) { private RadialButton(int index, float x, float y) {
this.setX(x); this.setX(x);
@ -236,7 +237,7 @@ public class RadialMenuScreen extends Screen implements ScreenControllerEventLis
graphics.pose().pushPose(); graphics.pose().pushPose();
graphics.pose().translate(4, 4, 0); graphics.pose().translate(4, 4, 0);
graphics.pose().scale(1.5f, 1.5f, 1); graphics.pose().scale(1.5f, 1.5f, 1);
this.icon.draw(graphics, 0, 0); this.icon.draw(graphics, 0, 0, delta);
graphics.pose().popPose(); graphics.pose().popPose();
} else { } else {
BindRenderer renderer = controller.bindings().GUI_PRESS.renderer(); BindRenderer renderer = controller.bindings().GUI_PRESS.renderer();