Skip to content

Commit

Permalink
Improved some code
Browse files Browse the repository at this point in the history
  • Loading branch information
RaydanOMGr committed Oct 24, 2024
1 parent 0dc7526 commit 4d5130c
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 95 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import net.minecraft.client.gui.components.ImageButton;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.player.Player;
import org.lwjgl.opengl.GL;
import org.lwjgl.opengl.GL11;
Expand All @@ -23,33 +24,35 @@
import java.util.List;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;

public class EyesEditorScreen extends Screen {
public EyesEditorScreen() {
super(Component.empty());
}
private int guiLeft, guiTop;

protected int xSize = 256;
protected int ySize = 222;

HashMap<Point, Color> pixels = new HashMap<>();
List<Button> modeButtons = new ArrayList<>();
private int guiLeft, guiTop,
headX, headY,
endHeadX, endHeadY;
private boolean displaySecondLayer = false;
private int xSize = 256;
private int ySize = 222;

Map<Point, Color> pixels = new HashMap<>();
Map<Mode, Button> modeButtons = new HashMap<>();
Color headBackgroundColor = new Color(160, 160, 160, 255);
Mode mode = Mode.BRUSH;

@Override
protected void init() {
super.init();
this.guiLeft = (this.width - this.xSize) / 2;
this.guiTop = (this.height - this.ySize) / 2;

AtomicBoolean saved = new AtomicBoolean(false);
Player player = Minecraft.getInstance().player;
if(player != null) {
pixels = GlowingEyesComponent.getGlowingEyesMap(player);
saved.set(true);
}
if(!saved.get()) {
} else {
LogUtils.getLogger().error("Could not load glowing eyes map from player capability");
}

Expand All @@ -70,57 +73,18 @@ protected void init() {
0, 0, 20,
TextureLocations.PRESET_MENU_BUTTON,
64, 64,
button -> {
Minecraft.getInstance().setScreen(new PresetsScreen(this));
}
button -> Minecraft.getInstance().setScreen(new PresetsScreen(this))
));

modeButtons.clear();
modeButtons.add(new ImageButton(
this.guiLeft + 8, this.guiTop + 70,
20, 20,
0, 0, 20,
TextureLocations.BRUSH_BUTTON,
64, 64,
button -> {
mode = Mode.BRUSH;
modeButtons.forEach(b -> b.active = true);
if(mode == Mode.BRUSH) button.active = false;
}
));
modeButtons.add(new ImageButton(
this.guiLeft + 8, this.guiTop + 95,
20, 20,
0, 0, 20,
TextureLocations.ERASER_BUTTON,
64, 64,
button -> {
mode = Mode.ERASER;
modeButtons.forEach(b -> b.active = true);
if(mode == Mode.ERASER) button.active = false;
}
));
modeButtons.add(new ImageButton(
this.guiLeft + 8, this.guiTop + 120,
20, 20,
0, 0, 20,
TextureLocations.PIPETTE_BUTTON,
64, 64,
button -> {
mode = Mode.PICKER;
modeButtons.forEach(b -> b.active = true);
if(mode == Mode.PICKER) button.active = false;
}
));

modeButtons.get(0).onPress();
modeButtons.forEach(this::addRenderableWidget);
}
this.modeButtons.clear();

int headX, headY;
int endHeadX, endHeadY;
this.createModeButton(8, 70, TextureLocations.BRUSH_BUTTON, Mode.BRUSH);
this.createModeButton(8, 95, TextureLocations.ERASER_BUTTON, Mode.ERASER);
this.createModeButton(8, 120, TextureLocations.PIPETTE_BUTTON, Mode.PICKER);

boolean displaySecondLayer = false;
this.modeButtons.get(Mode.BRUSH).onPress();
this.modeButtons.forEach((mode, button) -> this.addRenderableWidget(button));
}

/**
* The method that renders the screen
Expand All @@ -131,7 +95,7 @@ protected void init() {
*/
@Override
public void render(PoseStack poseStack, int mouseX, int mouseY, float deltaTime) {
renderBackground(poseStack);
this.renderBackground(poseStack);

GuiUtil.drawBackground(poseStack,
TextureLocations.UI_BACKGROUND_BROAD, this.guiLeft, this.guiTop, this.xSize, this.ySize);
Expand All @@ -140,16 +104,13 @@ public void render(PoseStack poseStack, int mouseX, int mouseY, float deltaTime)
int pixelSize = 16;
final int headSize = 8;

headX = this.guiLeft + (this.xSize - (headSize * pixelSize + (headSize - 1) * spaceBetweenPixels)) / 2;
headY = this.guiTop + (this.ySize - (headSize * pixelSize + (headSize - 1) * spaceBetweenPixels)) / 2;
endHeadX = headX + headSize * pixelSize + (headSize - 1) * spaceBetweenPixels;
endHeadY = headY + headSize * pixelSize + (headSize - 1) * spaceBetweenPixels;
this.calculateHeadSize(headSize, pixelSize, spaceBetweenPixels);

fill(
Gui.fill(
poseStack,
headX - spaceBetweenPixels, headY - spaceBetweenPixels,
endHeadX + spaceBetweenPixels, endHeadY + spaceBetweenPixels,
new Color(160, 160, 160, 255).getRGB()
headBackgroundColor.getRGB()
);

RenderSystem.setShaderTexture(0, Minecraft.getInstance().player.getSkinTextureLocation());
Expand All @@ -169,7 +130,7 @@ public void render(PoseStack poseStack, int mouseX, int mouseY, float deltaTime)
);

if(pixels.containsKey(point)) {
fill(
Gui.fill(
poseStack,
headX + x * pixelSize + x * spaceBetweenPixels,
headY + y * pixelSize + y * spaceBetweenPixels,
Expand All @@ -184,8 +145,7 @@ public void render(PoseStack poseStack, int mouseX, int mouseY, float deltaTime)
poseStack,
headX + x * pixelSize + x * spaceBetweenPixels,
headY + y * pixelSize + y * spaceBetweenPixels,
pixelSize,
pixelSize,
pixelSize, pixelSize,
40f + x, 8f + y,
1, 1,
64, 64
Expand Down Expand Up @@ -238,7 +198,7 @@ public boolean mouseClicked(double mouseX, double mouseY, int button) {
System.out.println("Color: " + ColorUtil.intToHex(color.getRGB()));
ColorPickerScreen.setSelectedColor(color);

modeButtons.get(0).onPress();
modeButtons.get(Mode.BRUSH).onPress();
}
}

Expand All @@ -249,17 +209,14 @@ public boolean mouseClicked(double mouseX, double mouseY, int button) {
public boolean mouseDragged(double mouseX, double mouseY, int button, double dragX, double dragY) {
return this.mouseClicked(mouseX, mouseY, button);
}

@Override
public void onClose() {
AtomicBoolean saved = new AtomicBoolean(false);
Player player = Minecraft.getInstance().player;
if(player != null) {
GlowingEyesComponent.setGlowingEyesMap(player, pixels);
saved.set(true);

ClientGlowingEyesComponent.sendUpdate();
}
if(!saved.get()) {
} else {
LogUtils.getLogger().error("Could not save glowing eyes map to player capability");
}
super.onClose();
Expand All @@ -277,7 +234,31 @@ public void openAsParent() {
Minecraft.getInstance().setScreen(this);
}

Mode mode = Mode.BRUSH;
private void calculateHeadSize(int headSize, int pixelSize, int spaceBetweenPixels) {
int head = headSize * pixelSize + (headSize - 1) * spaceBetweenPixels;
headX = this.guiLeft + (this.xSize - head) / 2;
headY = this.guiTop + (this.ySize - head) / 2;
endHeadX = headX + head;
endHeadY = headY + head;
}

private ImageButton createModeButton(int x, int y, ResourceLocation texture, Mode buttonMode) {
ImageButton imageButton = new ImageButton(
this.guiLeft + x, this.guiTop + y,
20, 20,
0, 0, 20,
texture,
64, 64,
button -> {
mode = buttonMode;
modeButtons.forEach((m, b) -> b.active = true);
button.active = false;
}
);
modeButtons.put(buttonMode, imageButton);
return imageButton;
}

enum Mode {
BRUSH,
ERASER,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import net.minecraft.world.entity.player.Player;

import java.awt.*;
import java.util.HashMap;
import java.util.Map;

public class GlowingEyesComponent {
private static IGlowingEyesComponent instance;
Expand All @@ -14,7 +14,7 @@ public class GlowingEyesComponent {
* @param player the player to get the glowing eyes map from
* @return the glowing eyes map (Point, Color)
*/
public static HashMap<Point, Color> getGlowingEyesMap(Player player) {
public static Map<Point, Color> getGlowingEyesMap(Player player) {
return instance.getGlowingEyesMap(player);
}

Expand All @@ -24,7 +24,7 @@ public static HashMap<Point, Color> getGlowingEyesMap(Player player) {
* @param player the player to set the glowing eyes map for
* @param glowingEyesMap the glowing eyes map to set
*/
public static void setGlowingEyesMap(Player player, HashMap<Point, Color> glowingEyesMap) {
public static void setGlowingEyesMap(Player player, Map<Point, Color> glowingEyesMap) {
instance.setGlowingEyesMap(player, glowingEyesMap);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,23 @@
import net.minecraft.world.entity.player.Player;

import java.awt.*;
import java.util.HashMap;
import java.util.Map;

public interface IGlowingEyesComponent {
/**
* Gets the glowing eyes map from a player
* @param player the player to get the glowing eyes map from
* @return the glowing eyes map (Point, Color)
*/
HashMap<Point, Color> getGlowingEyesMap(Player player);
Map<Point, Color> getGlowingEyesMap(Player player);

/**
* Sets the glowing eyes map (should only be using when completely overwriting the map)
* Otherwise, get the glowing eyes map and modify it
* @param player the player to set the glowing eyes map for
* @param glowingEyesMap the glowing eyes map to set
*/
void setGlowingEyesMap(Player player, HashMap<Point, Color> glowingEyesMap);
void setGlowingEyesMap(Player player, Map<Point, Color> glowingEyesMap);

/**
* Gets whether the glowing eyes are toggled on for a player
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@

import me.andreasmelone.glowingeyes.common.component.eyes.IGlowingEyesComponent;
import me.andreasmelone.glowingeyes.fabric.common.component.ComponentHandler;
import me.andreasmelone.glowingeyes.fabric.common.packet.ComponentUpdatePacket;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.entity.player.Player;

import java.awt.*;
import java.util.HashMap;
import java.util.Map;

public class GlowingEyesComponentImpl implements IGlowingEyesComponent {
private final IGlowingEyes localComponent = new GlowingEyesImpl();
Expand All @@ -18,12 +17,12 @@ public IGlowingEyes getComponent(Player player) {
}

@Override
public HashMap<Point, Color> getGlowingEyesMap(Player player) {
public Map<Point, Color> getGlowingEyesMap(Player player) {
return getComponent(player).getGlowingEyesMap();
}

@Override
public void setGlowingEyesMap(Player player, HashMap<Point, Color> glowingEyesMap) {
public void setGlowingEyesMap(Player player, Map<Point, Color> glowingEyesMap) {
getComponent(player).setGlowingEyesMap(glowingEyesMap);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

import java.awt.Color;
import java.awt.Point;
import java.util.HashMap;
import java.util.Map;

public interface IGlowingEyes extends Component {
HashMap<Point, Color> getGlowingEyesMap();
void setGlowingEyesMap(HashMap<Point, Color> glowingEyesMap);
Map<Point, Color> getGlowingEyesMap();
void setGlowingEyesMap(Map<Point, Color> glowingEyesMap);
boolean isToggledOn();
void setToggledOn(boolean toggledOn);
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import net.minecraftforge.network.PacketDistributor;

import java.awt.*;
import java.util.HashMap;
import java.util.Map;

public class GlowingEyesComponentImpl implements IGlowingEyesComponent {
protected static final Capability<IGlowingEyes> INSTANCE = CapabilityManager.get(new CapabilityToken<>() {});
Expand All @@ -35,12 +35,12 @@ public IGlowingEyes getComponent(Player player) {
}

@Override
public HashMap<Point, Color> getGlowingEyesMap(Player player) {
public Map<Point, Color> getGlowingEyesMap(Player player) {
return getComponent(player).getGlowingEyesMap();
}

@Override
public void setGlowingEyesMap(Player player, HashMap<Point, Color> glowingEyesMap) {
public void setGlowingEyesMap(Player player, Map<Point, Color> glowingEyesMap) {
getComponent(player).setGlowingEyesMap(glowingEyesMap);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

import java.awt.*;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

public interface IGlowingEyes extends Serializable {
HashMap<Point, Color> getGlowingEyesMap();
void setGlowingEyesMap(HashMap<Point, Color> glowingEyesMap);
Map<Point, Color> getGlowingEyesMap();
void setGlowingEyesMap(Map<Point, Color> glowingEyesMap);
boolean isToggledOn();
void setToggledOn(boolean toggledOn);
}
1 change: 0 additions & 1 deletion settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,4 @@ pluginManagement {

rootProject.name = 'glowingeyes'
include("Common", "Fabric", "Forge")
include 'Server'

0 comments on commit 4d5130c

Please sign in to comment.