Skip to content

Commit

Permalink
feat: quick replace glyph via num key
Browse files Browse the repository at this point in the history
  • Loading branch information
baileyholl committed Sep 17, 2023
1 parent 4d2db3e commit 7f4fbe0
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@
import com.hollingsworth.arsnouveau.common.network.PacketUpdateCaster;
import com.hollingsworth.arsnouveau.common.spell.validation.CombinedSpellValidator;
import com.hollingsworth.arsnouveau.common.spell.validation.GlyphMaxTierValidator;
import com.hollingsworth.arsnouveau.common.util.GuiUtils;
import com.hollingsworth.arsnouveau.setup.ItemsRegistry;
import com.mojang.blaze3d.systems.RenderSystem;
import com.mojang.blaze3d.vertex.*;
import net.minecraft.Util;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.components.AbstractWidget;
import net.minecraft.client.gui.components.Button;
import net.minecraft.client.gui.components.EditBox;
import net.minecraft.client.gui.components.Widget;
Expand Down Expand Up @@ -79,7 +81,7 @@ public class GuiSpellBook extends BaseBook {
public int maxManaCache = 0;
int currentCostCache = 0;
public boolean setFocusOnLoad = true;

public Widget hoveredWidget = null;
@Deprecated(forRemoval = true) // TODO: remove in 1.20
public GuiSpellBook(ItemStack bookStack, int tier, List<AbstractSpellPart> unlockedSpells) {
this(InteractionHand.MAIN_HAND);
Expand Down Expand Up @@ -435,6 +437,25 @@ public void onSlotChange(Button button) {
validate();
}

@Override
public boolean charTyped(char pCodePoint, int pModifiers) {
if(hoveredWidget instanceof GlyphButton glyphButton && glyphButton.validationErrors.isEmpty()){
// check if char is a number
if(pCodePoint >= '0' && pCodePoint <= '9'){
int num = Integer.parseInt(String.valueOf(pCodePoint));
if(num == 0){
num = 10;
}
num -= 1;
this.craftingCells.get(num).abstractSpellPart = glyphButton.abstractSpellPart;
this.craftingCells.get(num).spellTag = glyphButton.abstractSpellPart.getRegistryName();
validate();
return true;
}
}
return super.charTyped(pCodePoint, pModifiers);
}

public void updateCraftingSlots(int bookSlot) {
//Crafting slots
List<AbstractSpellPart> recipe = CasterUtil.getCaster(bookStack).getSpell(bookSlot).recipe;
Expand Down Expand Up @@ -630,16 +651,20 @@ private void validateGlyphButton(List<AbstractSpellPart> recipe, GlyphButton gly
recipe.remove(recipe.size() - 1);
}

/**
* Draws the screen and all the components in it.
*/
@Override
public void render(PoseStack ms, int mouseX, int mouseY, float partialTicks) {
super.render(ms, mouseX, mouseY, partialTicks);
if(this.setFocusOnLoad){
this.setFocusOnLoad = false;
this.setInitialFocus(searchBar);
}
hoveredWidget = null;
for(Widget widget : renderables){
if(widget instanceof AbstractWidget abstractWidget && GuiUtils.isMouseInRelativeRange(mouseX, mouseY, abstractWidget)){
hoveredWidget = widget;
break;
}
}
spell_name.setSuggestion(spell_name.getValue().isEmpty() ? Component.translatable("ars_nouveau.spell_book_gui.spell_name").getString() : "");
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.hollingsworth.arsnouveau.common.util;

import net.minecraft.client.gui.components.AbstractWidget;

public class GuiUtils {

public static boolean isMouseInRelativeRange(int mouseX, int mouseY, AbstractWidget widget){
return isMouseInRelativeRange(mouseX, mouseY, widget.x, widget.y, widget.getWidth(), widget.getHeight());
}

public static boolean isMouseInRelativeRange(int mouseX, int mouseY, int x, int y, int w, int h) {
return mouseX >= x && mouseX <= x + w && mouseY >= y && mouseY <= y + h;
}
}

0 comments on commit 7f4fbe0

Please sign in to comment.