Skip to content

Commit

Permalink
Merge pull request #3 from nutyworks/auto-complete-enhance
Browse files Browse the repository at this point in the history
Auto complete enhance
  • Loading branch information
nutyworks authored Mar 19, 2024
2 parents 568713e + aea7a6a commit 5e07712
Show file tree
Hide file tree
Showing 7 changed files with 134 additions and 544 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package works.nuty.calcite.mixin.client;

import com.mojang.brigadier.ParseResults;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.client.gui.screen.ChatInputSuggestor;
import net.minecraft.command.CommandSource;
import net.minecraft.text.OrderedText;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.gen.Accessor;

import java.util.List;

@Environment(EnvType.CLIENT)
@Mixin(ChatInputSuggestor.class)
public interface ChatInputSuggestorFields {
@Accessor
ChatInputSuggestor.SuggestionWindow getWindow();

@Accessor
int getMaxSuggestionSize();

@Accessor
ParseResults<CommandSource> getParse();

@Accessor
List<OrderedText> getMessages();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package works.nuty.calcite.mixin.client;

import com.mojang.brigadier.suggestion.Suggestion;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.client.gui.screen.ChatInputSuggestor;
import net.minecraft.client.util.math.Rect2i;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.gen.Accessor;

import java.util.List;

@Environment(EnvType.CLIENT)
@Mixin(ChatInputSuggestor.SuggestionWindow.class)
public interface SuggestionWindowFields {
@Accessor
Rect2i getArea();

@Accessor
List<Suggestion> getSuggestions();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package works.nuty.calcite.mixin.client;

import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.client.gui.screen.ChatInputSuggestor;
import org.lwjgl.glfw.GLFW;
import org.spongepowered.asm.mixin.Mixin;
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;

@Environment(EnvType.CLIENT)
@Mixin(ChatInputSuggestor.SuggestionWindow.class)
public abstract class SuggestionWindowMixin {
@Shadow
boolean completed;

@Shadow
public abstract void scroll(int offset);

@Shadow
public abstract void complete();

@Inject(at = @At("HEAD"), method = "keyPressed", cancellable = true)
public void keyPressed(int keyCode, int scanCode, int modifiers, CallbackInfoReturnable<Boolean> cir) {
if ((modifiers & GLFW.GLFW_MOD_CONTROL) > 0 && keyCode == GLFW.GLFW_KEY_N) {
this.scroll(1);
this.completed = false;
cir.setReturnValue(true);
cir.cancel();
}
if ((modifiers & GLFW.GLFW_MOD_CONTROL) > 0 && keyCode == GLFW.GLFW_KEY_P) {
this.scroll(-1);
this.completed = false;
cir.setReturnValue(true);
cir.cancel();
}
if (keyCode == GLFW.GLFW_KEY_ENTER || keyCode == GLFW.GLFW_KEY_KP_ENTER) {
this.complete();
cir.setReturnValue(true);
cir.cancel();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.gui.Element;
import net.minecraft.client.gui.Selectable;
import net.minecraft.client.gui.screen.ChatInputSuggestor;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.gui.widget.ButtonWidget;
import net.minecraft.client.gui.widget.ClickableWidget;
Expand All @@ -25,6 +26,8 @@
import net.minecraft.world.World;
import org.jetbrains.annotations.Nullable;
import org.lwjgl.glfw.GLFW;
import works.nuty.calcite.mixin.client.ChatInputSuggestorFields;
import works.nuty.calcite.mixin.client.SuggestionWindowFields;
import works.nuty.calcite.widget.AutoActivateButtonWidget;
import works.nuty.calcite.widget.CalciteTextFieldWidget;
import works.nuty.calcite.widget.ModeButtonWidget;
Expand Down Expand Up @@ -347,7 +350,7 @@ public void scrollToFocused() {

@Environment(EnvType.CLIENT)
public class CommandWidget extends AbstractCommandWidget {
protected final CalciteInputSuggestor commandSuggestor;
protected final ChatInputSuggestor commandSuggestor;
protected final List<ClickableWidget> children;
private final CommandBlockBlockEntity blockEntity;
private final CalciteTextFieldWidget commandEdit;
Expand All @@ -368,10 +371,11 @@ public CommandWidget(CommandBlockBlockEntity blockEntity) {
this.commandEdit.setMaxLength(32500);
this.commandEdit.setChangedListener(this::onCommandChanged);

this.commandSuggestor = new CalciteInputSuggestor(CalciteCommandScreen.this.client, CalciteCommandScreen.this, this.commandEdit, CalciteCommandScreen.this.textRenderer, true, true, 0, 7, Integer.MIN_VALUE);
this.commandSuggestor = new ChatInputSuggestor(CalciteCommandScreen.this.client, CalciteCommandScreen.this, this.commandEdit, CalciteCommandScreen.this.textRenderer, true, true, 0, 7, false, Integer.MIN_VALUE);
this.commandSuggestor.setWindowActive(true);
this.commandSuggestor.refresh();

this.commandEdit.suggestor = this.commandSuggestor;
this.commandEdit.setSuggestion(null);

this.children = List.of(this.commandEdit);
Expand Down Expand Up @@ -503,11 +507,14 @@ public void render(DrawContext context, int index, int y, int x, int entryWidth,
CalciteCommandScreen.this.commandSuggestorRenderer = () -> {
context.getMatrices().push();
context.getMatrices().translate(0, 0, 1);
CalciteInputSuggestor.SuggestionWindow window = this.commandSuggestor.window;
ChatInputSuggestor.SuggestionWindow window = ((ChatInputSuggestorFields) this.commandSuggestor).getWindow();
if (window != null) {
window.area.setY(window.calculateY(y));
((SuggestionWindowFields) window).getArea().setY(calculateSuggestionY(y));
}
if (!this.commandSuggestor.tryRenderWindow(context, mouseX, mouseY)) {
context.getMatrices().translate(0, calculateMessageY(y), 0);
this.commandSuggestor.renderMessages(context);
}
this.commandSuggestor.render(context, mouseX, mouseY, y);
context.getMatrices().pop();
};
}
Expand All @@ -531,5 +538,17 @@ protected void syncSettingsToServer() {
CommandBlockExecutor commandExecutor = blockEntity.getCommandExecutor();
CalciteCommandScreen.this.client.getNetworkHandler().sendPacket(new UpdateCommandBlockC2SPacket(BlockPos.ofFloored(commandExecutor.getPos()), this.commandEdit.getText(), this.mode, commandExecutor.isTrackingOutput(), this.conditional, this.autoActivate));
}

private int calculateSuggestionY(int y) {
return height / 2 - 6 < y
? y - 3 - Math.min(((SuggestionWindowFields) ((ChatInputSuggestorFields) this.commandSuggestor).getWindow()).getSuggestions().size(), ((ChatInputSuggestorFields) this.commandSuggestor).getMaxSuggestionSize()) * 12
: (y + 24) - (this.commandEdit.drawsBackground() ? 1 : 0);
}

private int calculateMessageY(int y) {
return (height / 2 - 6 < y
? y - 3 - ((ChatInputSuggestorFields) this.commandSuggestor).getMessages().size() * 12
: (y + 24) - (this.commandEdit.drawsBackground() ? 1 : 0)) - 72;
}
}
}
Loading

0 comments on commit 5e07712

Please sign in to comment.