This commit is contained in:
46
src/client/java/com/aranroig/client/CodecraftClient.java
Normal file
46
src/client/java/com/aranroig/client/CodecraftClient.java
Normal file
@@ -0,0 +1,46 @@
|
||||
package com.aranroig.client;
|
||||
|
||||
import com.aranroig.Codecraft;
|
||||
import com.aranroig.client.editor.EditorManager;
|
||||
import com.mojang.blaze3d.platform.InputConstants;
|
||||
import net.fabricmc.api.ClientModInitializer;
|
||||
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
|
||||
import net.minecraft.client.KeyMapping;
|
||||
import net.minecraft.resources.Identifier;
|
||||
import org.lwjgl.glfw.GLFW;
|
||||
import net.fabricmc.fabric.api.client.keymapping.v1.KeyMappingHelper;
|
||||
|
||||
|
||||
import javax.swing.text.JTextComponent;
|
||||
|
||||
public class CodecraftClient implements ClientModInitializer {
|
||||
|
||||
public static JTextComponent.KeyBinding OPEN_EDITOR;
|
||||
|
||||
@Override
|
||||
public void onInitializeClient() {
|
||||
KeyMapping.Category CATEGORY = KeyMapping.Category.register(
|
||||
Identifier.fromNamespaceAndPath(Codecraft.MOD_ID, "codecraft")
|
||||
);
|
||||
|
||||
|
||||
KeyMapping openEditor = KeyMappingHelper.registerKeyMapping(
|
||||
new KeyMapping(
|
||||
"key.codecraft.open_editor", // The translation key for the key mapping.
|
||||
InputConstants.Type.KEYSYM, // The type of the keybinding; KEYSYM for keyboard, MOUSE for mouse.
|
||||
GLFW.GLFW_KEY_J, // The GLFW keycode of the key.
|
||||
CATEGORY // The category of the mapping.
|
||||
)
|
||||
);
|
||||
|
||||
EditorManager editorOverlay = new EditorManager();
|
||||
|
||||
ClientTickEvents.END_CLIENT_TICK.register(client -> {
|
||||
while(openEditor.consumeClick()){
|
||||
if(client.player != null) {
|
||||
editorOverlay.toggleVisible();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.aranroig.client;
|
||||
|
||||
import com.aranroig.client.translations.CodecraftEnglishLangProvider;
|
||||
import net.fabricmc.fabric.api.datagen.v1.DataGeneratorEntrypoint;
|
||||
import net.fabricmc.fabric.api.datagen.v1.FabricDataGenerator;
|
||||
|
||||
public class CodecraftDataGenerator implements DataGeneratorEntrypoint {
|
||||
@Override
|
||||
public void onInitializeDataGenerator(FabricDataGenerator fabricDataGenerator) {
|
||||
FabricDataGenerator.Pack pack = fabricDataGenerator.createPack();
|
||||
|
||||
pack.addProvider(CodecraftEnglishLangProvider::new);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
package com.aranroig.client.editor;
|
||||
|
||||
import net.minecraft.client.gui.Font;
|
||||
import net.minecraft.client.gui.GuiGraphicsExtractor;
|
||||
import net.minecraft.client.input.CharacterEvent;
|
||||
import net.minecraft.client.input.KeyEvent;
|
||||
import net.minecraft.client.input.MouseButtonEvent;
|
||||
import org.jspecify.annotations.NonNull;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class EditorFileTree extends Widget {
|
||||
|
||||
private static final int COLOR_TEXT = 0xFFFFFFFF;
|
||||
private static final int COLOR_BACKGROUND = 0xFF202020;
|
||||
|
||||
public EditorFileTree(EditorManager editorManager) {
|
||||
super(editorManager);
|
||||
}
|
||||
|
||||
public void draw(@NonNull GuiGraphicsExtractor graphics, int mouseX, int mouseY, double delta) {
|
||||
graphics.fill(getStartX(), getStartY(), getEndX(), getEndY(), COLOR_BACKGROUND);
|
||||
|
||||
// Line numbers + text
|
||||
/*
|
||||
for (int i = firstLine; i <= lastLine; i++) {
|
||||
int screenY = start_y + i * lineH - scrollY;
|
||||
|
||||
// Line number — right-aligned, not scrolled horizontally
|
||||
String lineNum = String.valueOf(i + 1);
|
||||
int numW = font.width(lineNum);
|
||||
graphics.text(font, lineNum,
|
||||
gutterDivX() - 4 - numW,
|
||||
screenY, COLOR_GUTTER_TEXT, false);
|
||||
|
||||
// Code text — offset by scrollX
|
||||
graphics.text(font, lines[i], textAreaX() - scrollX, screenY, COLOR_TEXT, true);
|
||||
}
|
||||
*/
|
||||
Font font = editorScreen.getFont();
|
||||
int lineH = font.lineHeight;
|
||||
List<com.aranroig.editor.EditorFile> files = editorManager.getFiles();
|
||||
for(int i = 0; i < files.size(); i++) {
|
||||
graphics.text(font, files.get(i).getName(), getStartX(), getStartY() + i * lineH, COLOR_TEXT, true);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean handleKeyPressed(KeyEvent event) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean handleCharTyped(CharacterEvent event) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean handleMouseClicked(MouseButtonEvent event) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean handleMouseDragged(MouseButtonEvent event) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean handleMouseReleased(MouseButtonEvent event) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean handleMouseScrolled(double scrollY) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
478
src/client/java/com/aranroig/client/editor/EditorFileView.java
Normal file
478
src/client/java/com/aranroig/client/editor/EditorFileView.java
Normal file
@@ -0,0 +1,478 @@
|
||||
package com.aranroig.client.editor;
|
||||
|
||||
import com.aranroig.editor.EditorFile;
|
||||
import com.aranroig.payloads.ServerboundSaveCodePayload;
|
||||
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking;
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraft.client.gui.Font;
|
||||
import net.minecraft.client.gui.GuiGraphicsExtractor;
|
||||
import net.minecraft.client.input.CharacterEvent;
|
||||
import net.minecraft.client.input.KeyEvent;
|
||||
import net.minecraft.client.input.MouseButtonEvent;
|
||||
import org.jspecify.annotations.NonNull;
|
||||
|
||||
public class EditorFileView extends Widget {
|
||||
|
||||
private StringBuilder text = new StringBuilder();
|
||||
|
||||
private EditorFile currentEditorFile;
|
||||
|
||||
private int selectionAnchor = -1;
|
||||
private int cursorPos = 0;
|
||||
|
||||
private Font font;
|
||||
|
||||
public EditorFileView(EditorManager editorManager) {
|
||||
super(editorManager);
|
||||
}
|
||||
|
||||
// ── Scroll state ──────────────────────────────────────────────────────
|
||||
private int scrollX = 0; // horizontal scroll in pixels
|
||||
private int scrollY = 0; // vertical scroll in pixels
|
||||
|
||||
/** How many pixels to scroll per mouse-wheel notch (vertically). */
|
||||
private static final int SCROLL_SPEED = 3; // lines per notch
|
||||
|
||||
/** Width reserved for line numbers + a small right-padding. */
|
||||
private static final int GUTTER_WIDTH = 30;
|
||||
private static final int GUTTER_MARGIN = 5;
|
||||
|
||||
/** Where actual text content starts (after the gutter). */
|
||||
private int gutterDivX() { return getStartX() + GUTTER_WIDTH; }
|
||||
private int textAreaX() { return getStartX() + GUTTER_WIDTH + GUTTER_MARGIN; }
|
||||
|
||||
/** Visible width available for text (excludes gutter). */
|
||||
private int textAreaWidth() { return getEndX() - textAreaX(); }
|
||||
/** Visible height available for text. */
|
||||
private int textAreaHeight() { return getEndY() - getStartY(); }
|
||||
|
||||
// ── Colors ────────────────────────────────────────────────────────────
|
||||
private static final int COLOR_TEXT = 0xFFFFFFFF;
|
||||
private static final int COLOR_CURSOR = 0xFFFFFFFF;
|
||||
private static final int COLOR_SELECTION = 0x664466FF;
|
||||
private static final int COLOR_GUTTER_BG = 0xFF252525;
|
||||
private static final int COLOR_GUTTER_TEXT = 0xFF888888;
|
||||
private static final int COLOR_GUTTER_DIV = 0xFF444444;
|
||||
|
||||
// ─────────────────────────────────────────────────────────────────────
|
||||
|
||||
private String[] getLines() {
|
||||
return text.toString().split("\n", -1);
|
||||
}
|
||||
|
||||
private boolean hasSelection() {
|
||||
return selectionAnchor >= 0 && selectionAnchor != cursorPos;
|
||||
}
|
||||
|
||||
/** Replaces \t with 4 spaces so font.width() measures correctly. */
|
||||
private String sanitize(String input) {
|
||||
return input.replace("\t", " ");
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the rendered pixel width of a prefix of a raw line up to
|
||||
* character index {@code charCount}, expanding tabs properly.
|
||||
*/
|
||||
private int widthUpTo(String rawLine, int charCount) {
|
||||
return font.width(sanitize(rawLine.substring(0, charCount)));
|
||||
}
|
||||
|
||||
// ── Scroll helpers ────────────────────────────────────────────────────
|
||||
|
||||
/** Maximum horizontal scroll so the widest line end is just visible. */
|
||||
private int maxScrollX() {
|
||||
if (font == null) return 0;
|
||||
int maxW = 0;
|
||||
for (String line : getLines()) maxW = Math.max(maxW, font.width(sanitize(line)));
|
||||
return Math.max(0, maxW - textAreaWidth());
|
||||
}
|
||||
|
||||
/** Maximum vertical scroll so the last line is just visible. */
|
||||
private int maxScrollY() {
|
||||
if (font == null) return 0;
|
||||
int totalH = getLines().length * font.lineHeight;
|
||||
return Math.max(0, totalH - textAreaHeight());
|
||||
}
|
||||
|
||||
private void clampScroll() {
|
||||
scrollX = Math.max(0, Math.min(scrollX, maxScrollX()));
|
||||
scrollY = Math.max(0, Math.min(scrollY, maxScrollY()));
|
||||
}
|
||||
|
||||
/**
|
||||
* Scrolls the view so the cursor is always visible.
|
||||
* Call after any cursor movement.
|
||||
*/
|
||||
private void scrollToCursor() {
|
||||
if (font == null) return;
|
||||
int[] lc = offsetToLineCol(cursorPos);
|
||||
String[] lines = getLines();
|
||||
String lineText = (lc[0] < lines.length) ? lines[lc[0]] : "";
|
||||
|
||||
// Vertical
|
||||
int cursorTop = lc[0] * font.lineHeight;
|
||||
int cursorBottom = cursorTop + font.lineHeight;
|
||||
if (cursorTop < scrollY)
|
||||
scrollY = cursorTop;
|
||||
else if (cursorBottom > scrollY + textAreaHeight())
|
||||
scrollY = cursorBottom - textAreaHeight();
|
||||
|
||||
// Horizontal — measure sanitized prefix so tabs expand correctly
|
||||
int cursorPx = widthUpTo(lineText, Math.min(lc[1], lineText.length()));
|
||||
if (cursorPx < scrollX)
|
||||
scrollX = cursorPx;
|
||||
else if (cursorPx + 2 > scrollX + textAreaWidth())
|
||||
scrollX = cursorPx + 2 - textAreaWidth();
|
||||
|
||||
clampScroll();
|
||||
}
|
||||
|
||||
// ── Coordinate mapping ────────────────────────────────────────────────
|
||||
|
||||
private int[] offsetToLineCol(int offset) {
|
||||
String content = text.toString();
|
||||
int line = 0, col = 0;
|
||||
for (int i = 0; i < offset && i < content.length(); i++) {
|
||||
if (content.charAt(i) == '\n') { line++; col = 0; }
|
||||
else { col++; }
|
||||
}
|
||||
return new int[]{line, col};
|
||||
}
|
||||
|
||||
private int lineColToOffset(int targetLine, int targetCol) {
|
||||
String[] lines = getLines();
|
||||
targetLine = Math.max(0, Math.min(targetLine, lines.length - 1));
|
||||
targetCol = Math.max(0, Math.min(targetCol, lines[targetLine].length()));
|
||||
int offset = 0;
|
||||
for (int i = 0; i < targetLine; i++) offset += lines[i].length() + 1;
|
||||
return offset + targetCol;
|
||||
}
|
||||
|
||||
/**
|
||||
* Maps a pixel coordinate to a text offset.
|
||||
* Accounts for the gutter AND current scroll position.
|
||||
* Uses sanitized widths so tab expansion is reflected in click targets.
|
||||
*/
|
||||
private int pixelToOffset(double mouseX, double mouseY) {
|
||||
String[] lines = getLines();
|
||||
int lineH = font.lineHeight;
|
||||
|
||||
// Apply scroll to convert screen coords → content coords
|
||||
double contentY = mouseY - getStartY() + scrollY;
|
||||
double contentX = mouseX - textAreaX() + scrollX;
|
||||
|
||||
int clickedLine = (int) Math.floor(contentY / lineH);
|
||||
clickedLine = Math.max(0, Math.min(clickedLine, lines.length - 1));
|
||||
|
||||
String lineText = lines[clickedLine];
|
||||
String sanitized = sanitize(lineText);
|
||||
int col = 0;
|
||||
for (int i = 0; i < lineText.length(); i++) {
|
||||
// Measure using sanitized prefix widths so tabs count as 4 spaces
|
||||
float charW = font.width(sanitized.substring(i, i + 1));
|
||||
float midPoint = font.width(sanitized.substring(0, i)) + charW / 2f;
|
||||
if (contentX < midPoint) { col = i; break; }
|
||||
col = i + 1;
|
||||
}
|
||||
return lineColToOffset(clickedLine, col);
|
||||
}
|
||||
|
||||
private void deleteSelection() {
|
||||
if (selectionAnchor < 0) return;
|
||||
int lo = Math.min(selectionAnchor, cursorPos);
|
||||
int hi = Math.max(selectionAnchor, cursorPos);
|
||||
text.delete(lo, hi);
|
||||
cursorPos = lo;
|
||||
selectionAnchor = -1;
|
||||
}
|
||||
|
||||
// ── Draw ──────────────────────────────────────────────────────────────
|
||||
|
||||
public void draw(@NonNull GuiGraphicsExtractor graphics, int mouseX, int mouseY, double delta) {
|
||||
String[] lines = getLines();
|
||||
font = editorScreen.getFont();
|
||||
|
||||
int lineH = font.lineHeight;
|
||||
|
||||
int start_x = getStartX();
|
||||
int start_y = getStartY();
|
||||
int end_x = getEndX();
|
||||
int end_y = getEndY();
|
||||
|
||||
graphics.enableScissor(start_x, start_y, end_x, end_y);
|
||||
|
||||
// Background
|
||||
graphics.fill(start_x, start_y, end_x, end_y, 0xFF303030);
|
||||
|
||||
// Gutter background + divider (not scrolled — always fixed on left)
|
||||
graphics.fill(start_x, start_y, gutterDivX() - 1, end_y, COLOR_GUTTER_BG);
|
||||
graphics.fill(gutterDivX() - 1, start_y, gutterDivX(), end_y, COLOR_GUTTER_DIV);
|
||||
|
||||
// Only render lines that are at least partially visible
|
||||
int firstLine = Math.max(0, scrollY / lineH);
|
||||
int lastLine = Math.min(lines.length - 1, (scrollY + textAreaHeight()) / lineH + 1);
|
||||
|
||||
// Selection highlight
|
||||
if (hasSelection()) {
|
||||
int lo = Math.min(selectionAnchor, cursorPos);
|
||||
int hi = Math.max(selectionAnchor, cursorPos);
|
||||
|
||||
int[] loLC = offsetToLineCol(lo);
|
||||
int[] hiLC = offsetToLineCol(hi);
|
||||
|
||||
for (int ln = Math.max(loLC[0], firstLine); ln <= Math.min(hiLC[0], lastLine); ln++) {
|
||||
String lineText = lines[ln];
|
||||
int colStart = (ln == loLC[0]) ? loLC[1] : 0;
|
||||
int colEnd = (ln == hiLC[0]) ? hiLC[1] : lineText.length();
|
||||
|
||||
// Use sanitized widths so tabs expand correctly in the highlight
|
||||
int x0 = textAreaX() + widthUpTo(lineText, colStart) - scrollX;
|
||||
int x1 = textAreaX() + widthUpTo(lineText, colEnd) - scrollX;
|
||||
if (x1 == x0) x1 = x0 + 2;
|
||||
|
||||
// Clamp to text area so highlight doesn't bleed into the gutter
|
||||
x0 = Math.max(x0, textAreaX());
|
||||
x1 = Math.min(x1, end_x);
|
||||
|
||||
int screenY = start_y + ln * lineH - scrollY;
|
||||
graphics.fill(x0, screenY, x1, screenY + lineH, COLOR_SELECTION);
|
||||
}
|
||||
}
|
||||
|
||||
// Line numbers + text
|
||||
for (int i = firstLine; i <= lastLine; i++) {
|
||||
int screenY = start_y + i * lineH - scrollY;
|
||||
|
||||
// Line number — right-aligned, not scrolled horizontally
|
||||
String lineNum = String.valueOf(i + 1);
|
||||
int numW = font.width(lineNum);
|
||||
graphics.text(font, lineNum,
|
||||
gutterDivX() - 4 - numW,
|
||||
screenY, COLOR_GUTTER_TEXT, false);
|
||||
|
||||
// Code text — sanitized so \t renders as 4 spaces, offset by scrollX
|
||||
graphics.text(font, sanitize(lines[i]), textAreaX() - scrollX, screenY, COLOR_TEXT, true);
|
||||
}
|
||||
|
||||
// Blinking cursor
|
||||
boolean showCursor = (System.currentTimeMillis() / 500) % 2 == 0;
|
||||
if (showCursor) {
|
||||
int[] lc = offsetToLineCol(cursorPos);
|
||||
int ln = lc[0];
|
||||
int col = lc[1];
|
||||
|
||||
// Sanitize before measuring so the tab expansion is accounted for
|
||||
int cursorX = textAreaX() + widthUpTo(
|
||||
(ln < lines.length) ? lines[ln] : "",
|
||||
(ln < lines.length) ? Math.min(col, lines[ln].length()) : 0
|
||||
) - scrollX;
|
||||
int cursorY = start_y + ln * lineH - scrollY;
|
||||
|
||||
// Only draw cursor if it's within the visible text area
|
||||
if (cursorX >= textAreaX() && cursorX <= end_x) {
|
||||
graphics.fill(cursorX, cursorY, cursorX + 1, cursorY + lineH, COLOR_CURSOR);
|
||||
}
|
||||
}
|
||||
|
||||
graphics.disableScissor();
|
||||
}
|
||||
|
||||
// ── Input handlers ────────────────────────────────────────────────────
|
||||
|
||||
public boolean handleMouseClicked(MouseButtonEvent event) {
|
||||
int clicked = pixelToOffset(event.x(), event.y());
|
||||
cursorPos = clicked;
|
||||
selectionAnchor = clicked;
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean handleMouseDragged(MouseButtonEvent event) {
|
||||
cursorPos = pixelToOffset(event.x(), event.y());
|
||||
|
||||
// Auto-scroll when dragging outside the editor bounds
|
||||
if (font != null) {
|
||||
int lineH = font.lineHeight;
|
||||
double my = event.y();
|
||||
double mx = event.x();
|
||||
|
||||
if (my < getStartY()) scrollY = Math.max(0, scrollY - lineH);
|
||||
else if (my > getEndY()) scrollY = Math.min(maxScrollY(), scrollY + lineH);
|
||||
|
||||
if (mx < textAreaX()) scrollX = Math.max(0, scrollX - 10);
|
||||
else if (mx > getEndX()) scrollX = Math.min(maxScrollX(), scrollX + 10);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean handleMouseReleased(MouseButtonEvent event) {
|
||||
if (cursorPos == selectionAnchor) selectionAnchor = -1;
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Call this from your Screen's mouseScrolled override whenever inside() is true.
|
||||
*
|
||||
* @param delta positive = scroll up (content moves down), negative = scroll down
|
||||
*/
|
||||
public boolean handleMouseScrolled(double delta) {
|
||||
if (font == null) return true;
|
||||
int amount = (int) (delta * SCROLL_SPEED * font.lineHeight);
|
||||
|
||||
scrollY = Math.max(0, Math.min(scrollY - amount, maxScrollY()));
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean handleCharTyped(CharacterEvent event) {
|
||||
int codePoint = event.codepoint();
|
||||
if (codePoint >= 32 && codePoint != 127) {
|
||||
if (hasSelection()) deleteSelection();
|
||||
text.insert(cursorPos, Character.toString(codePoint));
|
||||
cursorPos += Character.charCount(codePoint);
|
||||
selectionAnchor = -1;
|
||||
scrollToCursor();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean handleKeyPressed(KeyEvent event) {
|
||||
int key = event.key();
|
||||
boolean shift = event.hasShiftDown();
|
||||
boolean ctrl = event.hasControlDown();
|
||||
|
||||
Minecraft minecraft = Minecraft.getInstance();
|
||||
|
||||
switch (key) {
|
||||
case 257 -> { // Enter
|
||||
if (hasSelection()) deleteSelection();
|
||||
text.insert(cursorPos, '\n');
|
||||
cursorPos++;
|
||||
selectionAnchor = -1;
|
||||
scrollToCursor();
|
||||
return true;
|
||||
}
|
||||
case 258 -> { // Tab
|
||||
if (hasSelection()) deleteSelection();
|
||||
text.insert(cursorPos, '\t');
|
||||
cursorPos++;
|
||||
selectionAnchor = -1;
|
||||
scrollToCursor();
|
||||
return true;
|
||||
}
|
||||
case 259 -> { // Backspace
|
||||
if (hasSelection()) { deleteSelection(); }
|
||||
else if (cursorPos > 0) { text.deleteCharAt(--cursorPos); }
|
||||
selectionAnchor = -1;
|
||||
scrollToCursor();
|
||||
return true;
|
||||
}
|
||||
case 261 -> { // Delete
|
||||
if (hasSelection()) { deleteSelection(); }
|
||||
else if (cursorPos < text.length()) { text.deleteCharAt(cursorPos); }
|
||||
selectionAnchor = -1;
|
||||
scrollToCursor();
|
||||
return true;
|
||||
}
|
||||
case 263 -> { // Left
|
||||
if (shift) {
|
||||
if (selectionAnchor < 0) selectionAnchor = cursorPos;
|
||||
if (cursorPos > 0) cursorPos--;
|
||||
} else {
|
||||
if (hasSelection()) cursorPos = Math.min(selectionAnchor, cursorPos);
|
||||
else if (cursorPos > 0) cursorPos--;
|
||||
selectionAnchor = -1;
|
||||
}
|
||||
scrollToCursor();
|
||||
return true;
|
||||
}
|
||||
case 262 -> { // Right
|
||||
if (shift) {
|
||||
if (selectionAnchor < 0) selectionAnchor = cursorPos;
|
||||
if (cursorPos < text.length()) cursorPos++;
|
||||
} else {
|
||||
if (hasSelection()) cursorPos = Math.max(selectionAnchor, cursorPos);
|
||||
else if (cursorPos < text.length()) cursorPos++;
|
||||
selectionAnchor = -1;
|
||||
}
|
||||
scrollToCursor();
|
||||
return true;
|
||||
}
|
||||
case 265 -> { // Up
|
||||
if (shift && selectionAnchor < 0) selectionAnchor = cursorPos;
|
||||
int[] lc = offsetToLineCol(cursorPos);
|
||||
cursorPos = (lc[0] - 1 >= 0) ? lineColToOffset(lc[0] - 1, lc[1]) : 0;
|
||||
if (!shift) selectionAnchor = -1;
|
||||
scrollToCursor();
|
||||
return true;
|
||||
}
|
||||
case 264 -> { // Down
|
||||
if (shift && selectionAnchor < 0) selectionAnchor = cursorPos;
|
||||
int[] lc2 = offsetToLineCol(cursorPos);
|
||||
String[] lines = getLines();
|
||||
cursorPos = (lc2[0] + 1 < lines.length)
|
||||
? lineColToOffset(lc2[0] + 1, lc2[1])
|
||||
: text.length();
|
||||
if (!shift) selectionAnchor = -1;
|
||||
scrollToCursor();
|
||||
return true;
|
||||
}
|
||||
case 268 -> { // Home
|
||||
if (shift && selectionAnchor < 0) selectionAnchor = cursorPos;
|
||||
cursorPos = lineColToOffset(offsetToLineCol(cursorPos)[0], 0);
|
||||
if (!shift) selectionAnchor = -1;
|
||||
scrollToCursor();
|
||||
return true;
|
||||
}
|
||||
case 269 -> { // End
|
||||
if (shift && selectionAnchor < 0) selectionAnchor = cursorPos;
|
||||
int[] lc4 = offsetToLineCol(cursorPos);
|
||||
cursorPos = lineColToOffset(lc4[0], getLines()[lc4[0]].length());
|
||||
if (!shift) selectionAnchor = -1;
|
||||
scrollToCursor();
|
||||
return true;
|
||||
}
|
||||
case 65 -> { // Ctrl+A
|
||||
if (ctrl) { selectionAnchor = 0; cursorPos = text.length(); scrollToCursor(); return true; }
|
||||
}
|
||||
case 67, 88 -> { // Ctrl+C / Ctrl+X
|
||||
if (ctrl && hasSelection()) {
|
||||
int lo = Math.min(selectionAnchor, cursorPos);
|
||||
int hi = Math.max(selectionAnchor, cursorPos);
|
||||
minecraft.keyboardHandler.setClipboard(text.substring(lo, hi));
|
||||
if (key == 88) { deleteSelection(); scrollToCursor(); }
|
||||
return true;
|
||||
}
|
||||
}
|
||||
case 86 -> { // Ctrl+V
|
||||
if (ctrl) {
|
||||
if (hasSelection()) deleteSelection();
|
||||
String clip = minecraft.keyboardHandler.getClipboard();
|
||||
text.insert(cursorPos, clip);
|
||||
cursorPos += clip.length();
|
||||
selectionAnchor = -1;
|
||||
scrollToCursor();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
case 83 -> { // Ctrl+S
|
||||
saveEditorFile();
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public void setEditorFile(EditorFile currentEditorFile) {
|
||||
this.currentEditorFile = currentEditorFile;
|
||||
text = new StringBuilder(currentEditorFile.getText());
|
||||
}
|
||||
|
||||
public void saveEditorFile() {
|
||||
if(this.currentEditorFile == null) return;
|
||||
this.currentEditorFile.setText(text.toString());
|
||||
|
||||
// Send save
|
||||
ServerboundSaveCodePayload payload = new ServerboundSaveCodePayload(this.currentEditorFile);
|
||||
ClientPlayNetworking.send(payload);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
package com.aranroig.client.editor;
|
||||
|
||||
import com.aranroig.client.wrapper.Logger;
|
||||
import net.minecraft.client.Minecraft;
|
||||
import party.iroiro.luajava.Lua;
|
||||
import party.iroiro.luajava.luaj.LuaJ;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class EditorManager {
|
||||
|
||||
Minecraft client;
|
||||
|
||||
EditorFileTree editorFileTree;
|
||||
EditorTopBar editorTopBar;
|
||||
EditorFileView editorFileView;
|
||||
|
||||
List<com.aranroig.editor.EditorFile> files;
|
||||
|
||||
public EditorManager() {
|
||||
client = Minecraft.getInstance();
|
||||
|
||||
editorFileTree = new EditorFileTree(this);
|
||||
editorTopBar = new EditorTopBar(this);
|
||||
editorFileView = new EditorFileView(this);
|
||||
|
||||
files = new ArrayList<>();
|
||||
|
||||
// Add example file
|
||||
com.aranroig.editor.EditorFile editorFile = new com.aranroig.editor.EditorFile("test.lua", "");
|
||||
files.add(editorFile);
|
||||
}
|
||||
|
||||
public List<com.aranroig.editor.EditorFile> getFiles() {
|
||||
return files;
|
||||
}
|
||||
|
||||
private final int FILE_RENDER_WIDTH_SIZE = 150;
|
||||
private final int TOP_BAR_HEIGHT = 12;
|
||||
|
||||
public void toggleVisible() {
|
||||
TextEditorScreen textEditorScreen = new TextEditorScreen();
|
||||
|
||||
// Populate screen with widgets
|
||||
editorFileTree.setSize(0, TOP_BAR_HEIGHT, FILE_RENDER_WIDTH_SIZE, -1);
|
||||
editorFileTree.addToEditorScreen(textEditorScreen);
|
||||
|
||||
editorTopBar.setSize(0, 0, -1, TOP_BAR_HEIGHT);
|
||||
editorTopBar.setZIndex(1);
|
||||
editorTopBar.addToEditorScreen(textEditorScreen);
|
||||
|
||||
editorFileView.setSize(FILE_RENDER_WIDTH_SIZE, TOP_BAR_HEIGHT, -1, -1);
|
||||
editorFileView.setEditorFile(files.getFirst());
|
||||
editorFileView.addToEditorScreen(textEditorScreen);
|
||||
|
||||
client.setScreenAndShow(textEditorScreen);
|
||||
}
|
||||
|
||||
public void newFile() {
|
||||
}
|
||||
|
||||
public void save() {
|
||||
}
|
||||
|
||||
public void undo() {
|
||||
}
|
||||
|
||||
public void run() {
|
||||
String code = files.getFirst().getText();
|
||||
System.out.println(code);
|
||||
|
||||
Lua l = new LuaJ();
|
||||
l.register("print", Logger.print());
|
||||
l.run(code);
|
||||
}
|
||||
|
||||
public void redo() {
|
||||
}
|
||||
|
||||
public void stop() {
|
||||
}
|
||||
}
|
||||
113
src/client/java/com/aranroig/client/editor/EditorScreen.java
Normal file
113
src/client/java/com/aranroig/client/editor/EditorScreen.java
Normal file
@@ -0,0 +1,113 @@
|
||||
package com.aranroig.client.editor;
|
||||
|
||||
import net.minecraft.client.gui.screens.Screen;
|
||||
import net.minecraft.client.input.CharacterEvent;
|
||||
import net.minecraft.client.input.KeyEvent;
|
||||
import net.minecraft.client.input.MouseButtonEvent;
|
||||
import net.minecraft.network.chat.Component;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public abstract class EditorScreen extends Screen {
|
||||
|
||||
private boolean isDragging = false;
|
||||
|
||||
protected EditorScreen(Component title) {
|
||||
super(title);
|
||||
}
|
||||
|
||||
protected List<Widget> widgets = new ArrayList<>();
|
||||
|
||||
protected Widget selectedWidget;
|
||||
|
||||
public void addWidget(Widget widget){
|
||||
widgets.add(widget);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean keyPressed(KeyEvent event) {
|
||||
if(selectedWidget != null){
|
||||
if(selectedWidget.handleKeyPressed(event)) return true;
|
||||
}
|
||||
|
||||
return super.keyPressed(event);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean charTyped(CharacterEvent event) {
|
||||
if(selectedWidget != null){
|
||||
if(selectedWidget.handleCharTyped(event))
|
||||
return true;
|
||||
}
|
||||
|
||||
return super.charTyped(event);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean keyReleased(KeyEvent event) {
|
||||
return super.keyReleased(event);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean mouseClicked(final MouseButtonEvent event, final boolean doubleClick) {
|
||||
selectedWidget = raycastWidget(event);
|
||||
if(selectedWidget != null)
|
||||
if (event.button() == 0) { // left button
|
||||
isDragging = true;
|
||||
if(selectedWidget.handleMouseClicked(event))
|
||||
return true;
|
||||
}
|
||||
return super.mouseClicked(event, doubleClick);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean mouseDragged(final MouseButtonEvent event, final double dx, final double dy) {
|
||||
if(selectedWidget != null)
|
||||
if (event.button() == 0 && isDragging) {
|
||||
if(selectedWidget.handleMouseDragged(event))
|
||||
return true;
|
||||
}
|
||||
return super.mouseDragged(event, dx, dy);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean mouseReleased(final MouseButtonEvent event) {
|
||||
if(selectedWidget != null)
|
||||
if (event.button() == 0) {
|
||||
isDragging = false;
|
||||
if(selectedWidget.handleMouseReleased(event))
|
||||
return true;
|
||||
}
|
||||
return super.mouseReleased(event);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean mouseScrolled(final double x, final double y, final double scrollX, final double scrollY) {
|
||||
if (selectedWidget != null){
|
||||
if (selectedWidget.inside(x, y)) {
|
||||
// scrollY from GLFW: positive = up, which in our model = scroll content upward
|
||||
if(selectedWidget.handleMouseScrolled(scrollY))
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return super.mouseScrolled(x, y, scrollX, scrollY);
|
||||
}
|
||||
|
||||
private Widget raycastWidget(MouseButtonEvent event) {
|
||||
Widget bestWidget = null;
|
||||
for(Widget widget : widgets){
|
||||
if(widget.inside(event.x(), event.y())){
|
||||
if(bestWidget == null) bestWidget = widget;
|
||||
else {
|
||||
if(bestWidget.getZIndex() < widget.getZIndex()) {
|
||||
bestWidget = widget;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return bestWidget;
|
||||
}
|
||||
|
||||
}
|
||||
314
src/client/java/com/aranroig/client/editor/EditorTopBar.java
Normal file
314
src/client/java/com/aranroig/client/editor/EditorTopBar.java
Normal file
@@ -0,0 +1,314 @@
|
||||
package com.aranroig.client.editor;
|
||||
|
||||
import net.minecraft.client.gui.Font;
|
||||
import net.minecraft.client.gui.GuiGraphicsExtractor;
|
||||
import net.minecraft.client.input.CharacterEvent;
|
||||
import net.minecraft.client.input.KeyEvent;
|
||||
import net.minecraft.client.input.MouseButtonEvent;
|
||||
import org.jspecify.annotations.NonNull;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class EditorTopBar extends Widget {
|
||||
public EditorTopBar(EditorManager editorManager) {
|
||||
super(editorManager);
|
||||
}
|
||||
|
||||
private static final int BAR_HEIGHT = 12;
|
||||
private static final int TAB_PADDING_X = 6;
|
||||
private static final int TAB_PADDING_Y = 2;
|
||||
|
||||
// ── Colors ────────────────────────────────────────────────────────────
|
||||
private static final int COLOR_TEXT = 0xFFFFFFFF;
|
||||
private static final int COLOR_BACKGROUND = 0xFF1C1C1C;
|
||||
private static final int COLOR_TAB_HOVER = 0xFF2D2D2D;
|
||||
private static final int COLOR_TAB_OPEN = 0xFF3A3A3A;
|
||||
private static final int COLOR_DROPDOWN_BG = 0xFF252525;
|
||||
private static final int COLOR_ITEM_HOVER = 0xFF0055AA;
|
||||
private static final int COLOR_SEPARATOR = 0xFF444444;
|
||||
|
||||
|
||||
|
||||
// ── Menu definitions ──────────────────────────────────────────────────
|
||||
private record MenuItem(String label, boolean isSeparator) {
|
||||
static MenuItem item(String label) { return new MenuItem(label, false); }
|
||||
static MenuItem separator() { return new MenuItem("", true); }
|
||||
}
|
||||
|
||||
private static final String[] TAB_LABELS = { "File", "Edit", "Run" };
|
||||
|
||||
private static final List<List<MenuItem>> TAB_ITEMS = List.of(
|
||||
// File
|
||||
List.of(
|
||||
MenuItem.item("New File"),
|
||||
MenuItem.item("Open..."),
|
||||
MenuItem.item("Save"),
|
||||
MenuItem.item("Save As..."),
|
||||
MenuItem.separator(),
|
||||
MenuItem.item("Close")
|
||||
),
|
||||
// Edit
|
||||
List.of(
|
||||
MenuItem.item("Undo"),
|
||||
MenuItem.item("Redo"),
|
||||
MenuItem.separator(),
|
||||
MenuItem.item("Cut"),
|
||||
MenuItem.item("Copy"),
|
||||
MenuItem.item("Paste"),
|
||||
MenuItem.separator(),
|
||||
MenuItem.item("Find..."),
|
||||
MenuItem.item("Replace...")
|
||||
),
|
||||
// Run
|
||||
List.of(
|
||||
MenuItem.item("Run Script"),
|
||||
MenuItem.item("Stop"),
|
||||
MenuItem.separator(),
|
||||
MenuItem.item("Build Project")
|
||||
)
|
||||
);
|
||||
|
||||
// ── State ─────────────────────────────────────────────────────────────
|
||||
/** Index of the currently open dropdown (-1 = none). */
|
||||
private int openTab = -1;
|
||||
/** Mouse position, updated each frame from the screen. */
|
||||
private int mouseX, mouseY;
|
||||
|
||||
// ── Drawing ───────────────────────────────────────────────────────────
|
||||
|
||||
public void draw(@NonNull GuiGraphicsExtractor graphics, int mouseX, int mouseY, double delta) {
|
||||
// Bar background
|
||||
Font font = editorScreen.getFont();
|
||||
cachedFont = font;
|
||||
graphics.fill(getStartX(), getStartY(), getEndX(), getStartY() + BAR_HEIGHT, COLOR_BACKGROUND);
|
||||
this.mouseX = mouseX;
|
||||
this.mouseY = mouseY;
|
||||
|
||||
int tabX = getStartX();
|
||||
|
||||
for (int i = 0; i < TAB_LABELS.length; i++) {
|
||||
String label = TAB_LABELS[i];
|
||||
int labelW = font.width(label);
|
||||
int tabWidth = labelW + TAB_PADDING_X * 2;
|
||||
int tabEndX = tabX + tabWidth;
|
||||
int tabEndY = getStartY() + BAR_HEIGHT;
|
||||
|
||||
boolean hovered = mouseX >= tabX && mouseX < tabEndX
|
||||
&& mouseY >= getStartY() && mouseY < tabEndY;
|
||||
boolean isOpen = openTab == i;
|
||||
|
||||
// Tab background
|
||||
int bg = isOpen ? COLOR_TAB_OPEN : (hovered ? COLOR_TAB_HOVER : COLOR_BACKGROUND);
|
||||
graphics.fill(tabX, getStartY(), tabEndX, tabEndY, bg);
|
||||
|
||||
// Tab label
|
||||
graphics.text(font, label,
|
||||
tabX + TAB_PADDING_X,
|
||||
getStartY() + TAB_PADDING_Y,
|
||||
COLOR_TEXT, false);
|
||||
|
||||
// Dropdown
|
||||
if (isOpen) {
|
||||
drawDropdown(graphics, font, i, tabX, tabEndY);
|
||||
}
|
||||
|
||||
tabX = tabEndX;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean handleKeyPressed(KeyEvent event) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean handleCharTyped(CharacterEvent event) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean handleMouseClicked(MouseButtonEvent event) {
|
||||
int button = event.button();
|
||||
double mx = event.x();
|
||||
double my = event.y();
|
||||
|
||||
if (button != 0) return false;
|
||||
|
||||
int imx = (int) mx;
|
||||
int imy = (int) my;
|
||||
|
||||
// Click on a tab label?
|
||||
int tabX = getStartX();
|
||||
for (int i = 0; i < TAB_LABELS.length; i++) {
|
||||
int tabW = font().width(TAB_LABELS[i]) + TAB_PADDING_X * 2;
|
||||
int tabEndX = tabX + tabW;
|
||||
|
||||
if (imx >= tabX && imx < tabEndX
|
||||
&& imy >= getStartY() && imy < getStartY() + BAR_HEIGHT) {
|
||||
openTab = (openTab == i) ? -1 : i;
|
||||
return true;
|
||||
}
|
||||
tabX = tabEndX;
|
||||
}
|
||||
|
||||
// Click on a dropdown item?
|
||||
if (openTab >= 0) {
|
||||
int result = getClickedItemIndex(imx, imy, openTab);
|
||||
if (result >= 0) {
|
||||
onItemClicked(openTab, result);
|
||||
openTab = -1;
|
||||
return true;
|
||||
}
|
||||
// Click outside → close
|
||||
openTab = -1;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean handleMouseDragged(MouseButtonEvent event) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean handleMouseReleased(MouseButtonEvent event) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean handleMouseScrolled(double scrollY) {
|
||||
return false;
|
||||
}
|
||||
|
||||
private void drawDropdown(@NonNull GuiGraphicsExtractor graphics, Font font,
|
||||
int tabIndex, int tabX, int topY) {
|
||||
List<MenuItem> items = TAB_ITEMS.get(tabIndex);
|
||||
int dropW = dropdownWidth(font, tabIndex);
|
||||
int dropX2 = tabX + dropW;
|
||||
int itemH = font.lineHeight + TAB_PADDING_Y * 2;
|
||||
|
||||
// Calculate total height
|
||||
int totalH = 0;
|
||||
for (MenuItem m : items) totalH += m.isSeparator() ? 5 : itemH;
|
||||
|
||||
// Background + border
|
||||
graphics.fill(tabX, topY, dropX2, topY + totalH, COLOR_DROPDOWN_BG);
|
||||
graphics.fill(tabX, topY, dropX2, topY + 1, COLOR_SEPARATOR); // top
|
||||
graphics.fill(tabX, topY, tabX + 1, topY + totalH, COLOR_SEPARATOR); // left
|
||||
graphics.fill(dropX2-1, topY, dropX2, topY + totalH, COLOR_SEPARATOR); // right
|
||||
graphics.fill(tabX, topY+totalH-1, dropX2, topY + totalH, COLOR_SEPARATOR); // bottom
|
||||
|
||||
int curY = topY;
|
||||
for (MenuItem item : items) {
|
||||
if (item.isSeparator()) {
|
||||
graphics.fill(tabX + 4, curY + 2, dropX2 - 4, curY + 3, COLOR_SEPARATOR);
|
||||
curY += 5;
|
||||
} else {
|
||||
boolean hovered = mouseX >= tabX && mouseX < dropX2
|
||||
&& mouseY >= curY && mouseY < curY + itemH;
|
||||
|
||||
if (hovered) {
|
||||
graphics.fill(tabX + 1, curY, dropX2 - 1, curY + itemH, COLOR_ITEM_HOVER);
|
||||
}
|
||||
|
||||
graphics.text(font, item.label(),
|
||||
tabX + TAB_PADDING_X,
|
||||
curY + TAB_PADDING_Y,
|
||||
COLOR_TEXT, false);
|
||||
|
||||
curY += itemH;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ── Item action dispatch ──────────────────────────────────────────────
|
||||
|
||||
private void onItemClicked(int tabIndex, int itemIndex) {
|
||||
MenuItem item = TAB_ITEMS.get(tabIndex).get(itemIndex);
|
||||
if (item.isSeparator()) return;
|
||||
|
||||
// TODO: wire these to your EditorManager
|
||||
switch (item.label()) {
|
||||
case "New File" -> editorManager.newFile();
|
||||
case "Save" -> editorManager.save();
|
||||
case "Undo" -> editorManager.undo();
|
||||
case "Redo" -> editorManager.redo();
|
||||
case "Run Script" -> editorManager.run();
|
||||
case "Stop" -> editorManager.stop();
|
||||
// Add remaining cases as needed
|
||||
default -> System.out.println("[TopBar] Clicked: " + item.label());
|
||||
}
|
||||
}
|
||||
|
||||
// ── Helpers ───────────────────────────────────────────────────────────
|
||||
|
||||
private int dropdownWidth(Font font, int tabIndex) {
|
||||
int maxW = 80; // minimum dropdown width
|
||||
for (MenuItem item : TAB_ITEMS.get(tabIndex)) {
|
||||
if (!item.isSeparator()) {
|
||||
maxW = Math.max(maxW, font.width(item.label()) + TAB_PADDING_X * 2 + 8);
|
||||
}
|
||||
}
|
||||
return maxW;
|
||||
}
|
||||
|
||||
private int getClickedItemIndex(int mx, int my, int tabIndex) {
|
||||
int tabX = getStartX();
|
||||
for (int i = 0; i < tabIndex; i++) {
|
||||
tabX += font().width(TAB_LABELS[i]) + TAB_PADDING_X * 2;
|
||||
}
|
||||
|
||||
int dropW = dropdownWidth(font(), tabIndex);
|
||||
int topY = getStartY() + BAR_HEIGHT;
|
||||
int itemH = font().lineHeight + TAB_PADDING_Y * 2;
|
||||
int curY = topY;
|
||||
|
||||
List<MenuItem> items = TAB_ITEMS.get(tabIndex);
|
||||
for (int i = 0; i < items.size(); i++) {
|
||||
MenuItem item = items.get(i);
|
||||
int h = item.isSeparator() ? 5 : itemH;
|
||||
if (!item.isSeparator()
|
||||
&& mx >= tabX && mx < tabX + dropW
|
||||
&& my >= curY && my < curY + h) {
|
||||
return i;
|
||||
}
|
||||
curY += h;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean inside(double x, double y) {
|
||||
if (getStartX() <= x && x <= getEndX() && getStartY() <= y && y <= getEndY()) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (openTab >= 0 && cachedFont != null) {
|
||||
// Find the X origin of the open tab
|
||||
int tabX = getStartX();
|
||||
for (int i = 0; i < openTab; i++) {
|
||||
tabX += cachedFont.width(TAB_LABELS[i]) + TAB_PADDING_X * 2;
|
||||
}
|
||||
|
||||
int dropW = dropdownWidth(cachedFont, openTab);
|
||||
int topY = getStartY() + BAR_HEIGHT;
|
||||
int itemH = cachedFont.lineHeight + TAB_PADDING_Y * 2;
|
||||
|
||||
int totalH = 0;
|
||||
for (MenuItem m : TAB_ITEMS.get(openTab)) {
|
||||
totalH += m.isSeparator() ? 5 : itemH;
|
||||
}
|
||||
|
||||
if (tabX <= x && x <= tabX + dropW && topY <= y && y <= topY + totalH) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/** Lazy accessor — store the font reference after the first draw() call if preferred. */
|
||||
private Font cachedFont;
|
||||
private Font font() { return cachedFont; }
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package com.aranroig.client.editor;
|
||||
|
||||
import net.minecraft.client.gui.GuiGraphicsExtractor;
|
||||
import net.minecraft.network.chat.Component;
|
||||
import org.jspecify.annotations.NonNull;
|
||||
|
||||
public class TextEditorScreen extends EditorScreen {
|
||||
|
||||
/** True while the left mouse button is held down during a drag-select. */
|
||||
|
||||
public TextEditorScreen() {
|
||||
super(Component.literal("Codecraft Editor"));
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void extractRenderState(
|
||||
@NonNull GuiGraphicsExtractor graphics,
|
||||
int mouseX,
|
||||
int mouseY,
|
||||
float delta
|
||||
) {
|
||||
super.extractRenderState(graphics, mouseX, mouseY, delta);
|
||||
|
||||
for (Widget widget : widgets) {
|
||||
widget.draw(
|
||||
graphics,
|
||||
mouseX,
|
||||
mouseY,
|
||||
delta
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
84
src/client/java/com/aranroig/client/editor/Widget.java
Normal file
84
src/client/java/com/aranroig/client/editor/Widget.java
Normal file
@@ -0,0 +1,84 @@
|
||||
package com.aranroig.client.editor;
|
||||
|
||||
import net.minecraft.client.gui.Font;
|
||||
import net.minecraft.client.gui.GuiGraphicsExtractor;
|
||||
import net.minecraft.client.gui.screens.Screen;
|
||||
import net.minecraft.client.input.CharacterEvent;
|
||||
import net.minecraft.client.input.KeyEvent;
|
||||
import net.minecraft.client.input.MouseButtonEvent;
|
||||
import org.jspecify.annotations.NonNull;
|
||||
|
||||
public abstract class Widget {
|
||||
|
||||
private int start_x = 0;
|
||||
private int start_y = 0;
|
||||
private int end_x = 0;
|
||||
private int end_y = 0;
|
||||
|
||||
private int z_index = 0;
|
||||
|
||||
private Screen s;
|
||||
|
||||
// Maybe moure aixo per més genèric?
|
||||
public EditorManager editorManager;
|
||||
|
||||
public Widget(EditorManager editorManager) {
|
||||
this.editorManager = editorManager;
|
||||
}
|
||||
|
||||
public boolean inside(double x, double y) {
|
||||
return getStartX() <= x && x <= getEndX() && getStartY() <= y && y <= getEndY();
|
||||
}
|
||||
|
||||
public void setSize(int start_x, int start_y, int end_x, int end_y) {
|
||||
this.start_x = start_x;
|
||||
this.start_y = start_y;
|
||||
this.end_x = end_x;
|
||||
this.end_y = end_y;
|
||||
}
|
||||
|
||||
public abstract void draw(@NonNull GuiGraphicsExtractor graphics, int mouseX, int mouseY, double delta);
|
||||
|
||||
public int getStartX() {
|
||||
return normalize(start_x, editorScreen.width);
|
||||
}
|
||||
|
||||
public int getStartY() {
|
||||
return normalize(start_y, editorScreen.height);
|
||||
}
|
||||
|
||||
public int getEndX() {
|
||||
return normalize(end_x, editorScreen.width);
|
||||
}
|
||||
|
||||
public int getEndY() {
|
||||
return normalize(end_y, editorScreen.height);
|
||||
}
|
||||
|
||||
private int normalize(int start, int total) {
|
||||
if(start < 0) return total + start + 1;
|
||||
return start;
|
||||
}
|
||||
|
||||
protected EditorScreen editorScreen;
|
||||
|
||||
public void addToEditorScreen(EditorScreen editorScreen){
|
||||
this.editorScreen = editorScreen;
|
||||
editorScreen.addWidget(this);
|
||||
}
|
||||
|
||||
public abstract boolean handleKeyPressed(KeyEvent event);
|
||||
public abstract boolean handleCharTyped(CharacterEvent event);
|
||||
public abstract boolean handleMouseClicked(MouseButtonEvent event);
|
||||
public abstract boolean handleMouseDragged(MouseButtonEvent event);
|
||||
public abstract boolean handleMouseReleased(MouseButtonEvent event);
|
||||
public abstract boolean handleMouseScrolled(double scrollY);
|
||||
|
||||
public int getZIndex() {
|
||||
return z_index;
|
||||
}
|
||||
|
||||
public void setZIndex(int z_index) {
|
||||
this.z_index = z_index;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package com.aranroig.client.mixin;
|
||||
|
||||
import net.minecraft.client.Minecraft;
|
||||
import org.spongepowered.asm.mixin.Mixin;
|
||||
import org.spongepowered.asm.mixin.injection.At;
|
||||
import org.spongepowered.asm.mixin.injection.Inject;
|
||||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
|
||||
|
||||
@Mixin(Minecraft.class)
|
||||
public class ExampleClientMixin {
|
||||
@Inject(at = @At("HEAD"), method = "run")
|
||||
private void init(CallbackInfo info) {
|
||||
// This code is injected into the start of Minecraft.run()V
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package com.aranroig.client.translations;
|
||||
|
||||
import net.fabricmc.fabric.api.datagen.v1.FabricPackOutput;
|
||||
import net.fabricmc.fabric.api.datagen.v1.provider.FabricLanguageProvider;
|
||||
import net.minecraft.core.HolderLookup;
|
||||
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
public class CodecraftEnglishLangProvider extends FabricLanguageProvider {
|
||||
public CodecraftEnglishLangProvider(FabricPackOutput dataOutput, CompletableFuture<HolderLookup.Provider> registryLookup) {
|
||||
super(dataOutput, "en_us", registryLookup);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void generateTranslations(HolderLookup.Provider registryLookup, TranslationBuilder translationBuilder) {
|
||||
translationBuilder.add("key.codecraft.open_editor", "Open Editor");
|
||||
translationBuilder.add("key.category.codecraft.codecraft", "Codecraft");
|
||||
}
|
||||
}
|
||||
24
src/client/java/com/aranroig/client/wrapper/Logger.java
Normal file
24
src/client/java/com/aranroig/client/wrapper/Logger.java
Normal file
@@ -0,0 +1,24 @@
|
||||
package com.aranroig.client.wrapper;
|
||||
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraft.network.chat.Component;
|
||||
import party.iroiro.luajava.value.LuaFunction;
|
||||
|
||||
public class Logger {
|
||||
public static LuaFunction print(){
|
||||
return (lua, args) -> {
|
||||
Minecraft client = Minecraft.getInstance();
|
||||
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
for (int i = 0; i < args.length; i++) {
|
||||
if (i > 0) sb.append('\t');
|
||||
sb.append(args[i]);
|
||||
}
|
||||
|
||||
client.player.sendSystemMessage(Component.literal(sb.toString()));
|
||||
|
||||
return null;
|
||||
};
|
||||
}
|
||||
}
|
||||
14
src/client/resources/codecraft.client.mixins.json
Normal file
14
src/client/resources/codecraft.client.mixins.json
Normal file
@@ -0,0 +1,14 @@
|
||||
{
|
||||
"required": true,
|
||||
"package": "com.aranroig.client.mixin",
|
||||
"compatibilityLevel": "JAVA_25",
|
||||
"client": [
|
||||
"ExampleClientMixin"
|
||||
],
|
||||
"injectors": {
|
||||
"defaultRequire": 1
|
||||
},
|
||||
"overwrites": {
|
||||
"requireAnnotations": true
|
||||
}
|
||||
}
|
||||
4
src/main/generated/assets/codecraft/lang/en_us.json
Normal file
4
src/main/generated/assets/codecraft/lang/en_us.json
Normal file
@@ -0,0 +1,4 @@
|
||||
{
|
||||
"key.category.codecraft.codecraft": "Codecraft",
|
||||
"key.codecraft.open_editor": "Open Editor"
|
||||
}
|
||||
39
src/main/java/com/aranroig/Codecraft.java
Normal file
39
src/main/java/com/aranroig/Codecraft.java
Normal file
@@ -0,0 +1,39 @@
|
||||
package com.aranroig;
|
||||
|
||||
import com.aranroig.payloads.ServerboundSaveCodePayload;
|
||||
import net.fabricmc.api.ModInitializer;
|
||||
|
||||
import net.fabricmc.fabric.api.networking.v1.PayloadTypeRegistry;
|
||||
import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking;
|
||||
import net.minecraft.resources.Identifier;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class Codecraft implements ModInitializer {
|
||||
public static final String MOD_ID = "codecraft";
|
||||
|
||||
// This logger is used to write text to the console and the log file.
|
||||
// It is considered best practice to use your mod id as the logger's name.
|
||||
// That way, it's clear which mod wrote info, warnings, and errors.
|
||||
public static final Logger LOGGER = LoggerFactory.getLogger(MOD_ID);
|
||||
|
||||
@Override
|
||||
public void onInitialize() {
|
||||
// This code runs as soon as Minecraft is in a mod-load-ready state.
|
||||
// However, some things (like resources) may still be uninitialized.
|
||||
// Proceed with mild caution.
|
||||
LOGGER.info("Hello Fabric world!");
|
||||
|
||||
PayloadTypeRegistry.serverboundPlay().register(ServerboundSaveCodePayload.TYPE, ServerboundSaveCodePayload.CODEC);
|
||||
|
||||
ServerPlayNetworking.registerGlobalReceiver(ServerboundSaveCodePayload.TYPE, (payload, context) -> {
|
||||
System.out.println(payload.data().getName());
|
||||
System.out.println(payload.data().getText());
|
||||
});
|
||||
}
|
||||
|
||||
public static Identifier id(String path) {
|
||||
return Identifier.fromNamespaceAndPath(MOD_ID, path);
|
||||
}
|
||||
}
|
||||
40
src/main/java/com/aranroig/editor/EditorFile.java
Normal file
40
src/main/java/com/aranroig/editor/EditorFile.java
Normal file
@@ -0,0 +1,40 @@
|
||||
package com.aranroig.editor;
|
||||
|
||||
import net.minecraft.network.RegistryFriendlyByteBuf;
|
||||
import net.minecraft.network.codec.ByteBufCodecs;
|
||||
import net.minecraft.network.codec.StreamCodec;
|
||||
|
||||
public class EditorFile {
|
||||
public static final StreamCodec<RegistryFriendlyByteBuf, EditorFile> CODEC =
|
||||
StreamCodec.composite(
|
||||
ByteBufCodecs.STRING_UTF8,
|
||||
EditorFile::getName,
|
||||
|
||||
ByteBufCodecs.STRING_UTF8,
|
||||
EditorFile::getText,
|
||||
|
||||
EditorFile::new
|
||||
);
|
||||
|
||||
// Class with file info
|
||||
private String name;
|
||||
private String text;
|
||||
|
||||
public EditorFile(String name, String text) {
|
||||
this.name = name;
|
||||
this.text = text;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public String getText() {
|
||||
return text;
|
||||
}
|
||||
|
||||
public void setText(String text) {
|
||||
this.text = text;
|
||||
}
|
||||
|
||||
}
|
||||
15
src/main/java/com/aranroig/mixin/ExampleMixin.java
Normal file
15
src/main/java/com/aranroig/mixin/ExampleMixin.java
Normal file
@@ -0,0 +1,15 @@
|
||||
package com.aranroig.mixin;
|
||||
|
||||
import net.minecraft.server.MinecraftServer;
|
||||
import org.spongepowered.asm.mixin.Mixin;
|
||||
import org.spongepowered.asm.mixin.injection.At;
|
||||
import org.spongepowered.asm.mixin.injection.Inject;
|
||||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
|
||||
|
||||
@Mixin(MinecraftServer.class)
|
||||
public class ExampleMixin {
|
||||
@Inject(at = @At("HEAD"), method = "loadLevel")
|
||||
private void init(CallbackInfo info) {
|
||||
// This code is injected into the start of MinecraftServer.loadLevel()V
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package com.aranroig.payloads;
|
||||
|
||||
import com.aranroig.Codecraft;
|
||||
import com.aranroig.editor.EditorFile;
|
||||
import net.minecraft.network.RegistryFriendlyByteBuf;
|
||||
import net.minecraft.network.codec.StreamCodec;
|
||||
import net.minecraft.network.protocol.common.custom.CustomPacketPayload;
|
||||
import net.minecraft.resources.Identifier;
|
||||
|
||||
public record ClientboundSaveCodePayload(EditorFile data) implements CustomPacketPayload {
|
||||
|
||||
public static final Identifier SAVE_CODE_PAYLOAD_ID = Identifier.fromNamespaceAndPath(Codecraft.MOD_ID, "save_code");
|
||||
|
||||
public static final CustomPacketPayload.Type<ClientboundSaveCodePayload> TYPE = new CustomPacketPayload.Type<>(SAVE_CODE_PAYLOAD_ID);
|
||||
|
||||
public static final StreamCodec<RegistryFriendlyByteBuf, ClientboundSaveCodePayload> CODEC =
|
||||
StreamCodec.composite(EditorFile.CODEC, ClientboundSaveCodePayload::data, ClientboundSaveCodePayload::new);
|
||||
|
||||
@Override
|
||||
public Type<? extends CustomPacketPayload> type() {
|
||||
return TYPE;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package com.aranroig.payloads;
|
||||
|
||||
import com.aranroig.Codecraft;
|
||||
import com.aranroig.editor.EditorFile;
|
||||
import net.minecraft.network.RegistryFriendlyByteBuf;
|
||||
import net.minecraft.network.codec.StreamCodec;
|
||||
import net.minecraft.network.protocol.common.custom.CustomPacketPayload;
|
||||
import net.minecraft.resources.Identifier;
|
||||
|
||||
public record ServerboundSaveCodePayload(EditorFile data) implements CustomPacketPayload {
|
||||
public static final Identifier SAVE_CODE_PAYLOAD_ID = Identifier.fromNamespaceAndPath(Codecraft.MOD_ID, "save_code");
|
||||
|
||||
public static final CustomPacketPayload.Type<ServerboundSaveCodePayload> TYPE = new CustomPacketPayload.Type<>(SAVE_CODE_PAYLOAD_ID);
|
||||
|
||||
public static final StreamCodec<RegistryFriendlyByteBuf, ServerboundSaveCodePayload> CODEC =
|
||||
StreamCodec.composite(EditorFile.CODEC, ServerboundSaveCodePayload::data, ServerboundSaveCodePayload::new);
|
||||
|
||||
@Override
|
||||
public CustomPacketPayload.Type<? extends CustomPacketPayload> type() {
|
||||
return TYPE;
|
||||
}
|
||||
}
|
||||
BIN
src/main/resources/assets/codecraft/icon.png
Normal file
BIN
src/main/resources/assets/codecraft/icon.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 1.9 KiB |
14
src/main/resources/codecraft.mixins.json
Normal file
14
src/main/resources/codecraft.mixins.json
Normal file
@@ -0,0 +1,14 @@
|
||||
{
|
||||
"required": true,
|
||||
"package": "com.aranroig.mixin",
|
||||
"compatibilityLevel": "JAVA_25",
|
||||
"mixins": [
|
||||
"ExampleMixin"
|
||||
],
|
||||
"injectors": {
|
||||
"defaultRequire": 1
|
||||
},
|
||||
"overwrites": {
|
||||
"requireAnnotations": true
|
||||
}
|
||||
}
|
||||
41
src/main/resources/fabric.mod.json
Normal file
41
src/main/resources/fabric.mod.json
Normal file
@@ -0,0 +1,41 @@
|
||||
{
|
||||
"schemaVersion": 1,
|
||||
"id": "codecraft",
|
||||
"version": "${version}",
|
||||
"name": "codecraft",
|
||||
"description": "Codecraft Mod",
|
||||
"authors": [
|
||||
"Aran Roig"
|
||||
],
|
||||
"contact": {
|
||||
"homepage": "https://aranroig.com/",
|
||||
"sources": "https://github.com/FabricMC/fabric-example-mod"
|
||||
},
|
||||
"license": "CC0-1.0",
|
||||
"icon": "assets/codecraft/icon.png",
|
||||
"environment": "*",
|
||||
"entrypoints": {
|
||||
"main": [
|
||||
"com.aranroig.Codecraft"
|
||||
],
|
||||
"client": [
|
||||
"com.aranroig.client.CodecraftClient"
|
||||
],
|
||||
"fabric-datagen": [
|
||||
"com.aranroig.client.CodecraftDataGenerator"
|
||||
]
|
||||
},
|
||||
"mixins": [
|
||||
"codecraft.mixins.json",
|
||||
{
|
||||
"config": "codecraft.client.mixins.json",
|
||||
"environment": "client"
|
||||
}
|
||||
],
|
||||
"depends": {
|
||||
"fabricloader": ">=0.19.3",
|
||||
"minecraft": "~26.2",
|
||||
"java": ">=25",
|
||||
"fabric-api": "*"
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user