diff --git a/src/main/java/kamkeel/npcs/command/profile/CommandHelpProfile.java b/src/main/java/kamkeel/npcs/command/profile/CommandHelpProfile.java index bbbfcc066..41e4da887 100644 --- a/src/main/java/kamkeel/npcs/command/profile/CommandHelpProfile.java +++ b/src/main/java/kamkeel/npcs/command/profile/CommandHelpProfile.java @@ -3,7 +3,6 @@ package kamkeel.npcs.command.profile; import net.minecraft.command.CommandException; import net.minecraft.command.ICommandSender; import net.minecraft.entity.player.EntityPlayer; -import net.minecraft.util.ChatComponentTranslation; import noppes.npcs.CustomNpcsPermissions; import java.lang.reflect.Method; diff --git a/src/main/java/noppes/npcs/client/ClientProxy.java b/src/main/java/noppes/npcs/client/ClientProxy.java index ffd0e64f2..7241efc64 100644 --- a/src/main/java/noppes/npcs/client/ClientProxy.java +++ b/src/main/java/noppes/npcs/client/ClientProxy.java @@ -292,6 +292,7 @@ public class ClientProxy extends CommonProxy { animNpc.display.texture = "customnpcs:textures/entity/humanmale/AnimationBody.png"; save = false; } + animNpc.display.setName("Animation Dude"); return new GuiNPCManageAnimations(animNpc, save); } else if (gui == EnumGuiType.ManageLinked) return new GuiNPCManageLinked(npc); diff --git a/src/main/java/noppes/npcs/client/gui/global/GuiNPCEditAnimation.java b/src/main/java/noppes/npcs/client/gui/global/GuiNPCEditAnimation.java index ea7343682..372efbaeb 100644 --- a/src/main/java/noppes/npcs/client/gui/global/GuiNPCEditAnimation.java +++ b/src/main/java/noppes/npcs/client/gui/global/GuiNPCEditAnimation.java @@ -8,7 +8,9 @@ import noppes.npcs.client.gui.SubGuiAnimationFrame; import noppes.npcs.client.gui.SubGuiAnimationOptions; import noppes.npcs.client.gui.SubGuiColorSelector; import noppes.npcs.client.gui.util.*; +import noppes.npcs.client.gui.util.animation.AnimationGraphEditor; import noppes.npcs.constants.EnumAnimationPart; +import noppes.npcs.constants.animation.EnumFrameType; import noppes.npcs.controllers.data.Animation; import noppes.npcs.controllers.data.AnimationData; import noppes.npcs.controllers.data.Frame; @@ -34,6 +36,7 @@ public class GuiNPCEditAnimation extends GuiModelInterface implements ITextfield private final GuiScreen parent; private long prevTick; + private final GuiNpcSlider[] rotationSliders = new GuiNpcSlider[3]; private final GuiNpcSlider[] pivotSliders = new GuiNpcSlider[3]; @@ -42,12 +45,18 @@ public class GuiNPCEditAnimation extends GuiModelInterface implements ITextfield private final int visibleFrames = 25; private boolean overrideFrame = false; + public AnimationGraphEditor graphEditor; + public GuiNpcButton toggleGraph; + private boolean toggleGraphEditor = true; + public GuiNPCEditAnimation(GuiScreen parent, Animation animation, EntityNPCInterface npc) { super((EntityCustomNpc) npc); this.parent = parent; this.followMouse = false; xOffset = 0; yOffset = -21; + maxZoom = 120; + drawRenderButtons = false; this.animation = animation; AnimationData data = npc.display.animationData; @@ -61,6 +70,7 @@ public class GuiNPCEditAnimation extends GuiModelInterface implements ITextfield this.pivotSliders[i] = new GuiNpcSlider(this, 95 + i, guiLeft + bodyPartX, guiTop + bodyPartY + 115 + (20 * i), 0.5F); } + graphEditor = new AnimationGraphEditor(this); this.frameSlider = new GuiNpcSlider(this, 350, 0, 0, 0.0F); } @@ -68,7 +78,17 @@ public class GuiNPCEditAnimation extends GuiModelInterface implements ITextfield public void initGui() { super.initGui(); - if (animation == null) return; + if (animation == null) + return; + yOffset = -21; + + addButton(toggleGraph = new GuiNpcButton(777, guiLeft - 80, guiTop - 2 + 84, 70, 20, "Toggle Graph")); + if (toggleGraphEditor) { + yOffset = -80; + graphEditor.initGui(guiLeft - 10, guiTop + 135, guiLeft - 10 + xSize, guiTop + 135 + 120); + addGraphEditor(0, graphEditor); + + } frameIndex = !animation.frames.isEmpty() ? frameIndex % animation.frames.size() : 0; this.updateSliders(); @@ -98,20 +118,18 @@ public class GuiNPCEditAnimation extends GuiModelInterface implements ITextfield data.animation.smooth = animation.smooth; data.animation.loop = 0; if (editingFrame != null) { - data.animation.frames.add(editingFrame); + data.animation.addFrame(editingFrame); } } for (Frame frame : animation.frames) { - frame.parent = animation; if (!frame.isCustomized()) { frame.speed = animation.speed; frame.smooth = animation.smooth; } for (Map.Entry entry : frame.frameParts.entrySet()) { FramePart part = entry.getValue(); - part.parent = animation; if (!part.isCustomized()) { part.speed = frame.speed; part.smooth = frame.smooth; @@ -129,37 +147,6 @@ public class GuiNPCEditAnimation extends GuiModelInterface implements ITextfield overrideFrame = false; } - this.addLabel(new GuiNpcLabel(10, "animation.frames", guiLeft + 40, guiTop + 176 - 10, 0xFFFFFF)); - this.addButton(new GuiNpcButton(11, guiLeft - 10, guiTop + 189 - 10, 45, 20, "gui.add")); - if (!animation.frames.isEmpty()) { - this.addButton(new GuiNpcButton(12, guiLeft + 35, guiTop + 189 - 10, 45, 20, "gui.remove")); - this.addButton(new GuiNpcButton(13, guiLeft + 80, guiTop + 189 - 10, 45, 20, "gui.copy")); - this.addButton(new GuiNpcButton(14, guiLeft - 10, guiTop + 210 - 3, 20, 20, "<")); - this.addTextField(new GuiNpcTextField(15, this, guiLeft + 15, guiTop + 212 - 3, 20, 17, frameIndex + "")); - this.getTextField(15).integersOnly = true; - this.getTextField(15).setMinMaxDefault(0, animation.frames.size() - 1, frameIndex); - this.addButton(new GuiNpcButton(16, guiLeft + 40, guiTop + 210 - 3, 20, 20, ">")); - } - - int playPauseX = 330; - int playPauseY = 10; - String animTexture = "customnpcs:textures/gui/animation.png"; - if (data.animation != null && data.animation.frames.size() > 0) { - if (!this.playingAnimation || data.animation.paused) {//Play - this.addLabel(new GuiNpcLabel(210, data.animation.paused ? "animation.paused" : "animation.stopped", guiLeft + playPauseX - 15, guiTop + playPauseY + 203, 0xFFFFFF)); - if (data.animation.paused) { - this.addLabel(new GuiNpcLabel(211, "", guiLeft + playPauseX + 21, guiTop + playPauseY + 203, 0xFFFFFF)); - } - this.addButton(new GuiTexturedButton(200, "", guiLeft + playPauseX + 35, guiTop + playPauseY + 197, 11, 20, animTexture, 18, 71)); - } else {//Pause - this.addLabel(new GuiNpcLabel(212, "animation.playing", guiLeft + playPauseX - 15, guiTop + playPauseY + 203, 0xFFFFFF)); - this.addLabel(new GuiNpcLabel(213, "", guiLeft + playPauseX + 20, guiTop + playPauseY + 203, 0xFFFFFF)); - this.addButton(new GuiTexturedButton(201, "", guiLeft + playPauseX + 35, guiTop + playPauseY + 197, 14, 20, animTexture, 0, 71)); - } - if (this.playingAnimation) {//Stop - this.addButton(new GuiTexturedButton(202, "", guiLeft + playPauseX + 55, guiTop + playPauseY + 197, 14, 20, animTexture, 33, 71)); - } - } //--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- @@ -194,174 +181,211 @@ public class GuiNPCEditAnimation extends GuiModelInterface implements ITextfield //--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- - if (editingFrame != null) { - int frameX = animationX; - int frameY = playPauseY + 100; + if (!toggleGraphEditor) { + int guiLeft = this.guiLeft; + this.addLabel(new GuiNpcLabel(10, "animation.frames", guiLeft + 40, guiTop + 176 - 10, 0xFFFFFF)); + this.addButton(new GuiNpcButton(11, guiLeft - 10, guiTop + 189 - 10, 45, 20, "gui.add")); + if (!animation.frames.isEmpty()) { + this.addButton(new GuiNpcButton(12, guiLeft + 35, guiTop + 189 - 10, 45, 20, "gui.remove")); + this.addButton(new GuiNpcButton(13, guiLeft + 80, guiTop + 189 - 10, 45, 20, "gui.copy")); + this.addButton(new GuiNpcButton(14, guiLeft - 10, guiTop + 210 - 3, 20, 20, "<")); + this.addTextField(new GuiNpcTextField(15, this, guiLeft + 15, guiTop + 212 - 3, 20, 17, frameIndex + "")); + this.getTextField(15).integersOnly = true; + this.getTextField(15).setMinMaxDefault(0, animation.frames.size() - 1, frameIndex); + this.addButton(new GuiNpcButton(16, guiLeft + 40, guiTop + 210 - 3, 20, 20, ">")); + } - this.addLabel(new GuiNpcLabel(50, "animation.frame", guiLeft + frameX + 50, guiTop + frameY - 3, 0xFFFFFF)); - // - //duration - textfield - this.addLabel(new GuiNpcLabel(51, "animation.duration", guiLeft + frameX, guiTop + frameY + 15, 0xFFFFFF)); - this.addTextField(new GuiNpcTextField(51, this, guiLeft + frameX + 88, guiTop + frameY + 11, 30, 15, editingFrame.duration + "")); - this.getTextField(51).integersOnly = true; - this.getTextField(51).setMinMaxDefaultFloat(0, Integer.MAX_VALUE, 10); + int playPauseX = 330; + int playPauseY = 10; - this.addButton(new GuiNpcButton(52, guiLeft + frameX, guiTop + frameY + 31, 80, 20, "animation.frameOptions")); - this.addButton(new GuiNpcButton(53, guiLeft + frameX + getButton(52).width + 5, guiTop + frameY + 31, 35, 20, "gui.color")); - - int bodyPartX = 280; - int bodyPartY = -5; - - // - //6 body part textured buttons - //Add/remove part button - // 6 custom GUI textured buttons for each body part determine "this.currentPart (enum)". - // If the current frame doesn't have that part, the button will say "Add part" - // otherwise, the button will say "Remove", and more options for editing the part become available: - // - - //Head - this.addButton(new GuiTexturedButton(60, "", guiLeft + bodyPartX, guiTop + bodyPartY, 22, 23, animTexture, 0, 0)); - //Body - this.addButton(new GuiTexturedButton(61, "", guiLeft + bodyPartX, guiTop + bodyPartY + 23, 22, 23, animTexture, 24, 0)); - //Right Arm - this.addButton(new GuiTexturedButton(62, "", guiLeft + bodyPartX - 9, guiTop + bodyPartY + 23, 8, 23, animTexture, 48, 0)); - //Left Arm - this.addButton(new GuiTexturedButton(63, "", guiLeft + bodyPartX + 26, guiTop + bodyPartY + 23, 8, 23, animTexture, 48, 0)); - //Right Leg - this.addButton(new GuiTexturedButton(64, "", guiLeft + bodyPartX + 1, guiTop + bodyPartY + 48, 10, 23, animTexture, 58, 0)); - //Left Leg - this.addButton(new GuiTexturedButton(65, "", guiLeft + bodyPartX + 12, guiTop + bodyPartY + 48, 10, 23, animTexture, 58, 0)); - //Full Body - this.addButton(new GuiTexturedButton(66, "", guiLeft + bodyPartX + 40, guiTop + bodyPartY + 2, 17, 23, animTexture, 70, 0)); - - for (int i = 0; i < 7; i++) { - if (this.buttons.containsKey(60 + i)) { - ((GuiTexturedButton) this.getButton(60 + i)).scale = 1.2F; - if (!editingFrame.frameParts.containsKey(EnumAnimationPart.values()[i])) { - ((GuiTexturedButton) this.getButton(60 + i)).textureX += 96; + String animTexture = "customnpcs:textures/gui/animation.png"; + if (data.animation != null && data.animation.frames.size() > 0) { + if (!this.playingAnimation || data.animation.paused) {//Play + this.addLabel(new GuiNpcLabel(210, data.animation.paused ? "animation.paused" : "animation.stopped", guiLeft + playPauseX - 15, guiTop + playPauseY + 203, 0xFFFFFF)); + if (data.animation.paused) { + this.addLabel(new GuiNpcLabel(211, "", guiLeft + playPauseX + 21, guiTop + playPauseY + 203, 0xFFFFFF)); } + this.addButton(new GuiTexturedButton(200, "", guiLeft + playPauseX + 35, guiTop + playPauseY + 197, 11, 20, animTexture, 18, 71)); + } else {//Pause + this.addLabel(new GuiNpcLabel(212, "animation.playing", guiLeft + playPauseX - 15, guiTop + playPauseY + 203, 0xFFFFFF)); + this.addLabel(new GuiNpcLabel(213, "", guiLeft + playPauseX + 20, guiTop + playPauseY + 203, 0xFFFFFF)); + this.addButton(new GuiTexturedButton(201, "", guiLeft + playPauseX + 35, guiTop + playPauseY + 197, 14, 20, animTexture, 0, 71)); + } + if (this.playingAnimation) {//Stop + this.addButton(new GuiTexturedButton(202, "", guiLeft + playPauseX + 55, guiTop + playPauseY + 197, 14, 20, animTexture, 33, 71)); } } - //--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- - //--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- - //--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- + if (editingFrame != null) { + int frameX = animationX; + int frameY = playPauseY + 100; - if (editingPart != null) { - //remove part button - this.addLabel(new GuiNpcLabel(67, editingPart.part.name(), guiLeft + bodyPartX + 65, guiTop + bodyPartY + 20, 0xFFFFFF)); - this.addButton(new GuiNpcButton(67, guiLeft + bodyPartX + 45, guiTop + bodyPartY + 35, 60, 20, "gui.remove")); + this.addLabel(new GuiNpcLabel(50, "animation.frame", guiLeft + frameX + 50, guiTop + frameY - 3, 0xFFFFFF)); + // + //duration - textfield + this.addLabel(new GuiNpcLabel(51, "animation.duration", guiLeft + frameX, guiTop + frameY + 15, 0xFFFFFF)); + this.addTextField(new GuiNpcTextField(51, this, guiLeft + frameX + 88, guiTop + frameY + 11, 30, 15, editingFrame.duration + "")); + this.getTextField(51).integersOnly = true; + this.getTextField(51).setMinMaxDefaultFloat(0, Integer.MAX_VALUE, 10); - this.addButton(new GuiNpcButton(68, guiLeft + bodyPartX + 45, guiTop + bodyPartY + 57, 60, 20, new String[]{"model.sliders", "model.manual"}, partEditMode)); + this.addButton(new GuiNpcButton(52, guiLeft + frameX, guiTop + frameY + 31, 80, 20, "animation.frameOptions")); + this.addButton(new GuiNpcButton(53, guiLeft + frameX + getButton(52).width + 5, guiTop + frameY + 31, 35, 20, "gui.color")); - if (partEditMode == 0) { - this.addButton(new GuiNpcButton(69, guiLeft + bodyPartX, guiTop + bodyPartY + 80, 60, 20, "model.rotate")); - this.addButton(new GuiNpcButton(70, guiLeft + bodyPartX + 62, guiTop + bodyPartY + 80, 60, 20, "model.pivot")); + int bodyPartX = 280; + int bodyPartY = -5; - this.getButton(69).setEnabled(sliderSelection == 1); - this.getButton(70).setEnabled(sliderSelection == 0); + // + //6 body part textured buttons + //Add/remove part button + // 6 custom GUI textured buttons for each body part determine "this.currentPart (enum)". + // If the current frame doesn't have that part, the button will say "Add part" + // otherwise, the button will say "Remove", and more options for editing the part become available: + // - if (sliderSelection == 0) { - for (int i = 0; i < 3; i++) { - this.rotationSliders[i].width = 122; - this.rotationSliders[i].height = 20; - this.rotationSliders[i].xPosition = guiLeft + bodyPartX; + //Head + this.addButton(new GuiTexturedButton(60, "", guiLeft + bodyPartX, guiTop + bodyPartY, 22, 23, animTexture, 0, 0)); + //Body + this.addButton(new GuiTexturedButton(61, "", guiLeft + bodyPartX, guiTop + bodyPartY + 23, 22, 23, animTexture, 24, 0)); + //Right Arm + this.addButton(new GuiTexturedButton(62, "", guiLeft + bodyPartX - 9, guiTop + bodyPartY + 23, 8, 23, animTexture, 48, 0)); + //Left Arm + this.addButton(new GuiTexturedButton(63, "", guiLeft + bodyPartX + 26, guiTop + bodyPartY + 23, 8, 23, animTexture, 48, 0)); + //Right Leg + this.addButton(new GuiTexturedButton(64, "", guiLeft + bodyPartX + 1, guiTop + bodyPartY + 48, 10, 23, animTexture, 58, 0)); + //Left Leg + this.addButton(new GuiTexturedButton(65, "", guiLeft + bodyPartX + 12, guiTop + bodyPartY + 48, 10, 23, animTexture, 58, 0)); + //Full Body + this.addButton(new GuiTexturedButton(66, "", guiLeft + bodyPartX + 40, guiTop + bodyPartY + 2, 17, 23, animTexture, 70, 0)); - int yOffset = 20; - if (i != 0) - yOffset += 3; - yOffset *= i; - this.rotationSliders[i].yPosition = guiTop + bodyPartY + 105 + yOffset; - - this.addSlider(this.rotationSliders[i]); + for (int i = 0; i < 7; i++) { + if (this.buttons.containsKey(60 + i)) { + ((GuiTexturedButton) this.getButton(60 + i)).scale = 1.2F; + if (!editingFrame.frameParts.containsKey(EnumAnimationPart.values()[i])) { + ((GuiTexturedButton) this.getButton(60 + i)).textureX += 96; } + } + } - this.addLabel(new GuiNpcLabel(90, "X", guiLeft + bodyPartX - 10, guiTop + bodyPartY + 112, 0xFFFFFF)); - this.addLabel(new GuiNpcLabel(91, "Y", guiLeft + bodyPartX - 10, guiTop + bodyPartY + 135, 0xFFFFFF)); - this.addLabel(new GuiNpcLabel(92, "Z", guiLeft + bodyPartX - 10, guiTop + bodyPartY + 157, 0xFFFFFF)); + //--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- + //--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- + //--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- + + if (editingPart != null) { + //remove part button + this.addLabel(new GuiNpcLabel(67, editingPart.part.name(), guiLeft + bodyPartX + 65, guiTop + bodyPartY + 20, 0xFFFFFF)); + this.addButton(new GuiNpcButton(67, guiLeft + bodyPartX + 45, guiTop + bodyPartY + 35, 60, 20, "gui.remove")); + + this.addButton(new GuiNpcButton(68, guiLeft + bodyPartX + 45, guiTop + bodyPartY + 57, 60, 20, new String[]{"model.sliders", "model.manual"}, partEditMode)); + + if (partEditMode == 0) { + this.addButton(new GuiNpcButton(69, guiLeft + bodyPartX, guiTop + bodyPartY + 80, 60, 20, "model.rotate")); + this.addButton(new GuiNpcButton(70, guiLeft + bodyPartX + 62, guiTop + bodyPartY + 80, 60, 20, "model.pivot")); + + this.getButton(69).setEnabled(sliderSelection == 1); + this.getButton(70).setEnabled(sliderSelection == 0); + + if (sliderSelection == 0) { + for (int i = 0; i < 3; i++) { + this.rotationSliders[i].width = 122; + this.rotationSliders[i].height = 20; + this.rotationSliders[i].xPosition = guiLeft + bodyPartX; + + int yOffset = 20; + if (i != 0) + yOffset += 3; + yOffset *= i; + this.rotationSliders[i].yPosition = guiTop + bodyPartY + 105 + yOffset; + + this.addSlider(this.rotationSliders[i]); + } + + this.addLabel(new GuiNpcLabel(90, "X", guiLeft + bodyPartX - 10, guiTop + bodyPartY + 112, 0xFFFFFF)); + this.addLabel(new GuiNpcLabel(91, "Y", guiLeft + bodyPartX - 10, guiTop + bodyPartY + 135, 0xFFFFFF)); + this.addLabel(new GuiNpcLabel(92, "Z", guiLeft + bodyPartX - 10, guiTop + bodyPartY + 157, 0xFFFFFF)); + } else { + for (int i = 0; i < 3; i++) { + this.pivotSliders[i].width = 122; + this.pivotSliders[i].height = 20; + this.pivotSliders[i].xPosition = guiLeft + bodyPartX; + + int yOffset = 20; + if (i != 0) + yOffset += 3; + yOffset *= i; + this.pivotSliders[i].yPosition = guiTop + bodyPartY + 105 + yOffset; + this.addSlider(this.pivotSliders[i]); + } + + this.addLabel(new GuiNpcLabel(95, "X", guiLeft + bodyPartX - 10, guiTop + bodyPartY + 112, 0xFFFFFF)); + this.addLabel(new GuiNpcLabel(96, "Y", guiLeft + bodyPartX - 10, guiTop + bodyPartY + 135, 0xFFFFFF)); + this.addLabel(new GuiNpcLabel(97, "Z", guiLeft + bodyPartX - 10, guiTop + bodyPartY + 157, 0xFFFFFF)); + } } else { - for (int i = 0; i < 3; i++) { - this.pivotSliders[i].width = 122; - this.pivotSliders[i].height = 20; - this.pivotSliders[i].xPosition = guiLeft + bodyPartX; - - int yOffset = 20; - if (i != 0) - yOffset += 3; - yOffset *= i; - this.pivotSliders[i].yPosition = guiTop + bodyPartY + 105 + yOffset; - this.addSlider(this.pivotSliders[i]); + // + //rotation - 3 textfields + this.addLabel(new GuiNpcLabel(70, "animation.rotations", guiLeft + bodyPartX, guiTop + bodyPartY + 85, 0xFFFFFF)); + this.addTextField(new GuiNpcTextField(70, this, guiLeft + bodyPartX, guiTop + bodyPartY + 97, 35, 15, editingPart.rotation[0] + "")); + this.getTextField(70).floatsOnly = true; + this.getTextField(70).setMinMaxDefaultFloat(-Float.MAX_VALUE, Float.MAX_VALUE, 0); + this.addTextField(new GuiNpcTextField(71, this, guiLeft + bodyPartX + 40, guiTop + bodyPartY + 97, 35, 15, editingPart.rotation[1] + "")); + this.getTextField(71).floatsOnly = true; + this.getTextField(71).setMinMaxDefaultFloat(-Float.MAX_VALUE, Float.MAX_VALUE, 0); + this.addTextField(new GuiNpcTextField(72, this, guiLeft + bodyPartX + 80, guiTop + bodyPartY + 97, 35, 15, editingPart.rotation[2] + "")); + this.getTextField(72).floatsOnly = true; + this.getTextField(72).setMinMaxDefaultFloat(-Float.MAX_VALUE, Float.MAX_VALUE, 0); + // + //pivot - 3 textfields + this.addLabel(new GuiNpcLabel(80, "animation.pivots", guiLeft + bodyPartX, guiTop + bodyPartY + 117, 0xFFFFFF)); + this.addTextField(new GuiNpcTextField(80, this, guiLeft + bodyPartX, guiTop + bodyPartY + 129, 35, 15, editingPart.pivot[0] + "")); + this.getTextField(80).floatsOnly = true; + this.getTextField(80).setMinMaxDefaultFloat(-Float.MAX_VALUE, Float.MAX_VALUE, 0); + this.addTextField(new GuiNpcTextField(81, this, guiLeft + bodyPartX + 40, guiTop + bodyPartY + 129, 35, 15, editingPart.pivot[1] + "")); + this.getTextField(81).floatsOnly = true; + this.getTextField(81).setMinMaxDefaultFloat(-Float.MAX_VALUE, Float.MAX_VALUE, 0); + this.addTextField(new GuiNpcTextField(82, this, guiLeft + bodyPartX + 80, guiTop + bodyPartY + 129, 35, 15, editingPart.pivot[2] + "")); + this.getTextField(82).floatsOnly = true; + this.getTextField(82).setMinMaxDefaultFloat(-Float.MAX_VALUE, Float.MAX_VALUE, 0); + // + //customized - button, enables all the following options. + this.addLabel(new GuiNpcLabel(83, "animation.customized", guiLeft + bodyPartX, guiTop + bodyPartY + 154, 0xFFFFFF)); + this.addButton(new GuiNpcButton(83, guiLeft + bodyPartX + 55, guiTop + bodyPartY + 148, 30, 20, new String[]{"gui.yes", "gui.no"}, editingPart.isCustomized() ? 0 : 1)); + if (editingPart.isCustomized()) { + // + //speed - textfield + this.addLabel(new GuiNpcLabel(84, "stats.speed", guiLeft + bodyPartX, guiTop + bodyPartY + 174, 0xFFFFFF)); + this.addTextField(new GuiNpcTextField(84, this, guiLeft + bodyPartX + 60, guiTop + bodyPartY + 170, 30, 15, editingPart.speed + "")); + this.getTextField(84).floatsOnly = true; + this.getTextField(84).setMinMaxDefaultFloat(0, Float.MAX_VALUE, 1.0F); + // + //smooth - button + this.addLabel(new GuiNpcLabel(85, "animation.smoothing", guiLeft + bodyPartX, guiTop + bodyPartY + 194, 0xFFFFFF)); + this.addButton(new GuiNpcButton(85, guiLeft + bodyPartX + 55, guiTop + bodyPartY + 190, 60, 20, new String[]{"animation.smooth", "animation.linear", "gui.none"}, editingPart.smooth)); } - - this.addLabel(new GuiNpcLabel(95, "X", guiLeft + bodyPartX - 10, guiTop + bodyPartY + 112, 0xFFFFFF)); - this.addLabel(new GuiNpcLabel(96, "Y", guiLeft + bodyPartX - 10, guiTop + bodyPartY + 135, 0xFFFFFF)); - this.addLabel(new GuiNpcLabel(97, "Z", guiLeft + bodyPartX - 10, guiTop + bodyPartY + 157, 0xFFFFFF)); - } - } else { - // - //rotation - 3 textfields - this.addLabel(new GuiNpcLabel(70, "animation.rotations", guiLeft + bodyPartX, guiTop + bodyPartY + 85, 0xFFFFFF)); - this.addTextField(new GuiNpcTextField(70, this, guiLeft + bodyPartX, guiTop + bodyPartY + 97, 35, 15, editingPart.rotation[0] + "")); - this.getTextField(70).floatsOnly = true; - this.getTextField(70).setMinMaxDefaultFloat(-Float.MAX_VALUE, Float.MAX_VALUE, 0); - this.addTextField(new GuiNpcTextField(71, this, guiLeft + bodyPartX + 40, guiTop + bodyPartY + 97, 35, 15, editingPart.rotation[1] + "")); - this.getTextField(71).floatsOnly = true; - this.getTextField(71).setMinMaxDefaultFloat(-Float.MAX_VALUE, Float.MAX_VALUE, 0); - this.addTextField(new GuiNpcTextField(72, this, guiLeft + bodyPartX + 80, guiTop + bodyPartY + 97, 35, 15, editingPart.rotation[2] + "")); - this.getTextField(72).floatsOnly = true; - this.getTextField(72).setMinMaxDefaultFloat(-Float.MAX_VALUE, Float.MAX_VALUE, 0); - // - //pivot - 3 textfields - this.addLabel(new GuiNpcLabel(80, "animation.pivots", guiLeft + bodyPartX, guiTop + bodyPartY + 117, 0xFFFFFF)); - this.addTextField(new GuiNpcTextField(80, this, guiLeft + bodyPartX, guiTop + bodyPartY + 129, 35, 15, editingPart.pivot[0] + "")); - this.getTextField(80).floatsOnly = true; - this.getTextField(80).setMinMaxDefaultFloat(-Float.MAX_VALUE, Float.MAX_VALUE, 0); - this.addTextField(new GuiNpcTextField(81, this, guiLeft + bodyPartX + 40, guiTop + bodyPartY + 129, 35, 15, editingPart.pivot[1] + "")); - this.getTextField(81).floatsOnly = true; - this.getTextField(81).setMinMaxDefaultFloat(-Float.MAX_VALUE, Float.MAX_VALUE, 0); - this.addTextField(new GuiNpcTextField(82, this, guiLeft + bodyPartX + 80, guiTop + bodyPartY + 129, 35, 15, editingPart.pivot[2] + "")); - this.getTextField(82).floatsOnly = true; - this.getTextField(82).setMinMaxDefaultFloat(-Float.MAX_VALUE, Float.MAX_VALUE, 0); - // - //customized - button, enables all the following options. - this.addLabel(new GuiNpcLabel(83, "animation.customized", guiLeft + bodyPartX, guiTop + bodyPartY + 154, 0xFFFFFF)); - this.addButton(new GuiNpcButton(83, guiLeft + bodyPartX + 55, guiTop + bodyPartY + 148, 30, 20, new String[]{"gui.yes", "gui.no"}, editingPart.isCustomized() ? 0 : 1)); - if (editingPart.isCustomized()) { - // - //speed - textfield - this.addLabel(new GuiNpcLabel(84, "stats.speed", guiLeft + bodyPartX, guiTop + bodyPartY + 174, 0xFFFFFF)); - this.addTextField(new GuiNpcTextField(84, this, guiLeft + bodyPartX + 60, guiTop + bodyPartY + 170, 30, 15, editingPart.speed + "")); - this.getTextField(84).floatsOnly = true; - this.getTextField(84).setMinMaxDefaultFloat(0, Float.MAX_VALUE, 1.0F); - // - //smooth - button - this.addLabel(new GuiNpcLabel(85, "animation.smoothing", guiLeft + bodyPartX, guiTop + bodyPartY + 194, 0xFFFFFF)); - this.addButton(new GuiNpcButton(85, guiLeft + bodyPartX + 55, guiTop + bodyPartY + 190, 60, 20, new String[]{"animation.smooth", "animation.linear", "gui.none"}, editingPart.smooth)); } } + + //--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- + //--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- + //--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- + } else { + this.addLabel(new GuiNpcLabel(50, "animation.addFrame", guiLeft + 270, guiTop + 100, 0xFFFFFF)); } - //--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- - //--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- - //--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- - } else { - this.addLabel(new GuiNpcLabel(50, "animation.addFrame", guiLeft + 270, guiTop + 100, 0xFFFFFF)); - } + this.addSlider(this.frameSlider); + this.frameSlider.xPosition = guiLeft + 62; + this.frameSlider.yPosition = guiTop + playPauseY + 197; + this.frameSlider.width = 63; - this.addSlider(this.frameSlider); - this.frameSlider.xPosition = guiLeft + 62; - this.frameSlider.yPosition = guiTop + playPauseY + 197; - this.frameSlider.width = 63; - - for (int i = 0; i < this.visibleFrames; i++) { - this.addButton(new GuiTexturedButton(300 + i, "", guiLeft + 130 + i * 7, guiTop + 210, 6, 20, animTexture, 0, 71)); + for (int i = 0; i < this.visibleFrames; i++) { + this.addButton(new GuiTexturedButton(300 + i, "", guiLeft + 130 + i * 7, guiTop + 210, 6, 20, animTexture, 0, 71)); + } } } private void updateSliders() { FramePart part = this.editingPart(); - if (part == null) return; + if (part == null) + return; float[] rotations = part.getRotations(); float[] pivots = part.getPivots(); @@ -391,6 +415,22 @@ public class GuiNPCEditAnimation extends GuiModelInterface implements ITextfield return editingFrame.frameParts.get(this.editingPart); } + public boolean isMouseOverRenderer(int x, int y) { + if (!allowRotate) { + return false; + } + // Center of the entity rendering + int centerX = guiLeft + 190 + xOffset; // Matches l in drawScreen() + int centerY = guiTop + 80 + yOffset; // Matches i1 in drawScreen() + + // Define separate buffers for X and Y axes + int xBuffer = 75; // Horizontal buffer + int yBuffer = 120; // Vertical buffer + + // Check if the mouse is within the buffer area + return mouseX >= centerX - xBuffer && mouseX <= centerX + xBuffer && mouseY >= centerY - yBuffer && mouseY <= centerY + yBuffer; + } + @Override protected void actionPerformed(GuiButton guibutton) { super.actionPerformed(guibutton); @@ -406,9 +446,9 @@ public class GuiNPCEditAnimation extends GuiModelInterface implements ITextfield updateFrameSlider(); } else if (guibutton.id == 13 && editingFrame != null) { if (frameIndex < animation.frames.size() - 1) { - animation.frames.add(frameIndex + 1, editingFrame.copy()); + animation.addFrame(frameIndex + 1, editingFrame.copy()); } else { - animation.frames.add(editingFrame.copy()); + animation.addFrame(editingFrame.copy()); } this.frameIndex = frameIndex + 1; updateFrameSlider(); @@ -491,6 +531,10 @@ public class GuiNPCEditAnimation extends GuiModelInterface implements ITextfield } } + if (guibutton == toggleGraph) { + toggleGraphEditor = !toggleGraphEditor; + } + initGui(); } @@ -500,9 +544,9 @@ public class GuiNPCEditAnimation extends GuiModelInterface implements ITextfield private void addFrame(Frame frame) { if (frameIndex < animation.frames.size() - 1) { - animation.frames.add(frameIndex + 1, frame); + animation.addFrame(frameIndex + 1, frame); } else { - animation.frames.add(frame); + animation.addFrame(frame); } this.frameIndex = frameIndex + 1; updateFrameSlider(); @@ -510,7 +554,6 @@ public class GuiNPCEditAnimation extends GuiModelInterface implements ITextfield @Override public void drawScreen(int par1, int par2, float par3) { - super.drawScreen(par1, par2, par3); AnimationData data = npc.display.animationData; if (!data.isActive() && this.playingAnimation) { this.playingAnimation = false; @@ -518,6 +561,7 @@ public class GuiNPCEditAnimation extends GuiModelInterface implements ITextfield } else if (data.isActive()) { long time = mc.theWorld.getTotalWorldTime(); if (time != prevTick) { + npc.display.animationData.increaseTime(); GuiNpcLabel label = this.getLabel(213); if (label != null) { label.label += "."; @@ -529,6 +573,8 @@ public class GuiNPCEditAnimation extends GuiModelInterface implements ITextfield prevTick = time; } + super.drawScreen(par1, par2, par3); + for (int i = 0; i < this.visibleFrames; i++) { if (getButton(300 + i) != null) { GuiTexturedButton button = (GuiTexturedButton) getButton(300 + i); @@ -564,7 +610,7 @@ public class GuiNPCEditAnimation extends GuiModelInterface implements ITextfield animation.name = text.replaceAll("[^a-zA-Z0-9_-]", "_"); } else if (textfield.id == 15 && animation != null && animation.frames.size() > 0) { animation.frames.remove(frameIndex); - animation.frames.add(textfield.getInteger(), frame); + animation.addFrame(textfield.getInteger(), frame); frameIndex = textfield.getInteger(); overrideFrame = true; initGui(); @@ -575,14 +621,25 @@ public class GuiNPCEditAnimation extends GuiModelInterface implements ITextfield } else if (textfield.id == 51 && frame != null) { frame.duration = textfield.getInteger(); } else if (textfield.id >= 70 && textfield.id <= 73 && part != null) { - part.rotation[textfield.id - 70] = textfield.getFloat(); + int index = textfield.id - 70; + float value = textfield.getFloat(); + part.rotation[index] = value; + part.addValue(EnumFrameType.values()[index], value); this.updateSliders(); } else if (textfield.id >= 80 && textfield.id <= 83 && part != null) { - part.pivot[textfield.id - 80] = textfield.getFloat(); + int index = textfield.id - 80; + float value = textfield.getFloat(); + part.pivot[index] = value; + part.addValue(EnumFrameType.values()[3 + index], value); this.updateSliders(); } } + public void onGuiClosed() { + super.onGuiClosed(); + graphEditor.close(); + } + @Override public void close() { if (!this.hasSubGui()) { @@ -618,13 +675,17 @@ public class GuiNPCEditAnimation extends GuiModelInterface implements ITextfield } if (id >= 90 && id < 93) { int value = (int) (360.0F * 2.0F * (guiNpcSlider.sliderValue - 0.5F)); - part.rotation[id - 90] = value; + int index = id - 90; + part.rotation[index] = value; + part.addValue(EnumFrameType.values()[index], value); guiNpcSlider.setString(value + ""); } if (id >= 95 && id < 98) { int value = (int) (100.0F * (guiNpcSlider.sliderValue - 0.5F)); guiNpcSlider.setString(value + ""); - part.pivot[id - 95] = value; + int index = id - 95; + part.pivot[index] = value; + part.addValue(EnumFrameType.values()[3 + index], value); } } @@ -648,7 +709,12 @@ public class GuiNPCEditAnimation extends GuiModelInterface implements ITextfield @Override public void keyTyped(char par1, int par2) { + boolean old = closeOnEsc; + if (graphEditor.presetOverlay.showOverlay) + closeOnEsc = false; + super.keyTyped(par1, par2); + closeOnEsc = old; if (GuiScreen.isCtrlKeyDown() && par2 != 29 && !GuiNpcTextField.isFieldActive()) { switch (par2) { @@ -660,7 +726,6 @@ public class GuiNPCEditAnimation extends GuiModelInterface implements ITextfield case 47: if (copiedFrame != null) { Frame frame = new Frame(10); - frame.parent = this.animation; frame.readFromNBT(copiedFrame.writeToNBT()); this.addFrame(frame); this.initGui(); diff --git a/src/main/java/noppes/npcs/client/gui/global/GuiNPCManageAnimations.java b/src/main/java/noppes/npcs/client/gui/global/GuiNPCManageAnimations.java index 381c12f8a..46aa61511 100644 --- a/src/main/java/noppes/npcs/client/gui/global/GuiNPCManageAnimations.java +++ b/src/main/java/noppes/npcs/client/gui/global/GuiNPCManageAnimations.java @@ -38,6 +38,7 @@ public class GuiNPCManageAnimations extends GuiModelInterface2 implements IScrol this.setSave(save); this.xOffset = -148 + 70; this.yOffset = -170 + 137; + maxZoom = 120; PacketClient.sendClient(new AnimationsGetPacket()); AnimationData data = npc.display.animationData; @@ -73,9 +74,8 @@ public class GuiNPCManageAnimations extends GuiModelInterface2 implements IScrol if (this.animation.frames.size() > 0) { Frame firstFrame = new Frame(); - firstFrame.parent = data.animation; - firstFrame.readFromNBT(this.animation.frames.get(0).writeToNBT()); data.animation.addFrame(firstFrame); + firstFrame.readFromNBT(this.animation.frames.get(0).writeToNBT()); } } @@ -104,8 +104,7 @@ public class GuiNPCManageAnimations extends GuiModelInterface2 implements IScrol } @Override - public void drawScreen(int par1, int par2, float par3) { - super.drawScreen(par1, par2, par3); + public void drawScreen(int par1, int par2, float partialTicks) { AnimationData data = npc.display.animationData; if (!data.isActive() && this.playingAnimation) { this.playingAnimation = false; @@ -113,6 +112,7 @@ public class GuiNPCManageAnimations extends GuiModelInterface2 implements IScrol } else if (data.isActive()) { long time = mc.theWorld.getTotalWorldTime(); if (time != prevTick) { + npc.display.animationData.increaseTime(); GuiNpcLabel label = this.getLabel(94); if (label != null) { label.label += "."; @@ -123,6 +123,7 @@ public class GuiNPCManageAnimations extends GuiModelInterface2 implements IScrol } prevTick = time; } + super.drawScreen(par1, par2, partialTicks); } @Override @@ -276,4 +277,20 @@ public class GuiNPCManageAnimations extends GuiModelInterface2 implements IScrol } } } + + public boolean isMouseOverRenderer(int x, int y) { + if (!allowRotate) { + return false; + } + // Center of the entity rendering + int centerX = guiLeft + 190 + xOffset; // Matches l in drawScreen() + int centerY = guiTop + 130 + yOffset; // Matches i1 in drawScreen() + + // Define separate buffers for X and Y axes + int xBuffer = 100; // Horizontal buffer + int yBuffer = 90; // Vertical buffer + + // Check if the mouse is within the buffer area + return mouseX >= centerX - xBuffer && mouseX <= centerX + xBuffer && mouseY >= centerY - yBuffer && mouseY <= centerY + yBuffer; + } } diff --git a/src/main/java/noppes/npcs/client/gui/script/GuiScriptLinkedItem.java b/src/main/java/noppes/npcs/client/gui/script/GuiScriptLinkedItem.java index dfb1b2900..5e99de749 100644 --- a/src/main/java/noppes/npcs/client/gui/script/GuiScriptLinkedItem.java +++ b/src/main/java/noppes/npcs/client/gui/script/GuiScriptLinkedItem.java @@ -10,12 +10,10 @@ import net.minecraft.nbt.NBTTagList; import net.minecraft.util.StatCollector; import noppes.npcs.NBTTags; import noppes.npcs.NoppesStringUtils; -import noppes.npcs.client.NoppesUtil; import noppes.npcs.client.gui.global.GuiNPCManageLinked; import noppes.npcs.client.gui.util.*; import noppes.npcs.constants.EnumScriptType; import noppes.npcs.controllers.ScriptContainer; -import noppes.npcs.controllers.ScriptController; import noppes.npcs.controllers.data.LinkedItem; import noppes.npcs.controllers.data.LinkedItemScript; diff --git a/src/main/java/noppes/npcs/client/gui/util/GuiModelInterface.java b/src/main/java/noppes/npcs/client/gui/util/GuiModelInterface.java index e7a32e4b4..9540d8d04 100644 --- a/src/main/java/noppes/npcs/client/gui/util/GuiModelInterface.java +++ b/src/main/java/noppes/npcs/client/gui/util/GuiModelInterface.java @@ -21,7 +21,7 @@ public class GuiModelInterface extends GuiNPCInterface { private GuiNpcButton left, right, zoom, unzoom; private static float zoomed = 60; - public float minSize = 10, maxSize = 100; + public float minZoom = 10, maxZoom = 100; public int xOffset = 0, xOffsetButton = 0; public int yOffset = 0, yOffsetButton = 0; @@ -98,16 +98,16 @@ public class GuiModelInterface extends GuiNPCInterface { } @Override - public void drawScreen(int par1, int par2, float par3) { - if (Mouse.isButtonDown(0)) { + public void drawScreen(int par1, int par2, float partialTicks) { + if (Mouse.isButtonDown(0) && drawRenderButtons) { if (this.left.mousePressed(this.mc, par1, par2)) { - rotation += par3 * 1.5F; + rotation += partialTicks * 1.5F; } else if (this.right.mousePressed(this.mc, par1, par2)) { - rotation -= par3 * 1.5F; - } else if (this.zoom.mousePressed(this.mc, par1, par2) && zoomed < maxSize) { - zoomed += par3 * 1.0F; - } else if (this.unzoom.mousePressed(this.mc, par1, par2) && zoomed > minSize) { - zoomed -= par3 * 1.0F; + rotation -= partialTicks * 1.5F; + } else if (this.zoom.mousePressed(this.mc, par1, par2) && zoomed < maxZoom) { + zoomed += partialTicks * 1.0F; + } else if (this.unzoom.mousePressed(this.mc, par1, par2) && zoomed > minZoom) { + zoomed -= partialTicks * 1.0F; } } @@ -118,10 +118,10 @@ public class GuiModelInterface extends GuiNPCInterface { } } - if (zoomed > maxSize) - zoomed = maxSize; - if (zoomed < minSize) - zoomed = minSize; + if (zoomed > maxZoom) + zoomed = maxZoom; + if (zoomed < minZoom) + zoomed = minZoom; if (hasSubGui() && !drawNPConSub) return; @@ -156,13 +156,13 @@ public class GuiModelInterface extends GuiNPCInterface { GL11.glRotatef(-(float) Math.atan(f6 / 800F) * 20F, 1.0F, 0.0F, 0.0F); entity.prevRenderYawOffset = entity.renderYawOffset = rotation; entity.prevRotationYaw = entity.rotationYaw = (float) Math.atan(f5 / 80F) * 40F + rotation; - entity.rotationPitch = followMouse ? -(float) Math.atan(f6 / 40F) * 20F : 0; + entity.rotationPitch = entity.prevRotationPitch = followMouse ? -(float) Math.atan(f6 / 40F) * 20F : 0; entity.prevRotationYawHead = entity.rotationYawHead = followMouse ? entity.rotationYaw : rotation; GL11.glTranslatef(0.0F, entity.yOffset, 1F); RenderManager.instance.playerViewY = 180F; try { - RenderManager.instance.renderEntityWithPosYaw(entity, 0.0D, 0.0D, 0.0D, 0.0F, 1.0F); + RenderManager.instance.renderEntityWithPosYaw(entity, 0.0D, 0.0D, 0.0D, 0.0F, partialTicks); } catch (Exception e) { playerdata.setEntityClass(null); } @@ -178,7 +178,7 @@ public class GuiModelInterface extends GuiNPCInterface { OpenGlHelper.setActiveTexture(OpenGlHelper.defaultTexUnit); GL11.glPushMatrix(); GL11.glTranslatef(0.0F, 0f, 500.065F); - super.drawScreen(par1, par2, par3); + super.drawScreen(par1, par2, partialTicks); GL11.glPopMatrix(); postRender(entity); diff --git a/src/main/java/noppes/npcs/client/gui/util/GuiModelInterface2.java b/src/main/java/noppes/npcs/client/gui/util/GuiModelInterface2.java index 16d3af9d7..a32f1af27 100644 --- a/src/main/java/noppes/npcs/client/gui/util/GuiModelInterface2.java +++ b/src/main/java/noppes/npcs/client/gui/util/GuiModelInterface2.java @@ -22,6 +22,8 @@ public class GuiModelInterface2 extends GuiNPCInterface2 { private GuiNpcButton left, right, zoom, unzoom; private static float zoomed = 60; + public float minZoom = 10, maxZoom = 100; + public boolean allowRotate = true; public int xOffset = 0; public int yOffset = 0; @@ -60,21 +62,50 @@ public class GuiModelInterface2 extends GuiNPCInterface2 { private long start = -1; + public boolean isMouseOverRenderer(int x, int y) { + if (!allowRotate) { + return false; + } + // Center of the entity rendering + int centerX = guiLeft + 190 + xOffset; // Matches l in drawScreen() + int centerY = guiTop + 180 + yOffset; // Matches i1 in drawScreen() + + // Define separate buffers for X and Y axes + int xBuffer = 100; // Horizontal buffer + int yBuffer = 150; // Vertical buffer + + // Check if the mouse is within the buffer area + return mouseX >= centerX - xBuffer && mouseX <= centerX + xBuffer && mouseY >= centerY - yBuffer && mouseY <= centerY + yBuffer; + } + + @Override - public void drawScreen(int par1, int par2, float par3) { + public void drawScreen(int par1, int par2, float partialTicks) { if (Mouse.isButtonDown(0)) { if (left.mousePressed(mc, par1, par2)) - rotation += par3 * 2; + rotation += partialTicks * 2; else if (right.mousePressed(mc, par1, par2)) - rotation -= par3 * 2; + rotation -= partialTicks * 2; else if (zoom.mousePressed(mc, par1, par2)) - zoomed += par3 * 2; + zoomed += partialTicks * 2; else if (unzoom.mousePressed(mc, par1, par2) && zoomed > 10) - zoomed -= par3 * 2; + zoomed -= partialTicks * 2; } - super.drawScreen(par1, par2, par3); + if (isMouseOverRenderer(par1, par2)) { + zoomed += Mouse.getDWheel() * 0.035f; + if (Mouse.isButtonDown(0) || Mouse.isButtonDown(1)) { + rotation -= Mouse.getDX() * 0.75f; + } + } + + if (zoomed > maxZoom) + zoomed = maxZoom; + if (zoomed < minZoom) + zoomed = minZoom; + + super.drawScreen(par1, par2, partialTicks); GL11.glColor4f(1, 1, 1, 1); @@ -105,13 +136,13 @@ public class GuiModelInterface2 extends GuiNPCInterface2 { GL11.glRotatef(-(float) Math.atan(f6 / 80F) * 20F, 1.0F, 0.0F, 0.0F); entity.prevRenderYawOffset = entity.renderYawOffset = rotation; entity.prevRotationYaw = entity.rotationYaw = (float) Math.atan(f5 / 80F) * 40F + rotation; - entity.rotationPitch = -(float) Math.atan(f6 / 80F) * 20F; + entity.rotationPitch = entity.prevRotationPitch = -(float) Math.atan(f6 / 80F) * 20F; entity.prevRotationYawHead = entity.rotationYawHead = entity.rotationYaw; GL11.glTranslatef(0.0F, entity.yOffset, 0.0F); RenderManager.instance.playerViewY = 180F; try { - RenderManager.instance.renderEntityWithPosYaw(entity, 0.0D, 0.0D, 0.0D, 0.0F, 1.0F); + RenderManager.instance.renderEntityWithPosYaw(entity, 0.0D, 0.0D, 0.0D, 0.0F, partialTicks); } catch (Exception e) { playerdata.setEntityClass(null); } diff --git a/src/main/java/noppes/npcs/client/gui/util/GuiNPCInterface.java b/src/main/java/noppes/npcs/client/gui/util/GuiNPCInterface.java index 7ceedfca2..650a34c47 100644 --- a/src/main/java/noppes/npcs/client/gui/util/GuiNPCInterface.java +++ b/src/main/java/noppes/npcs/client/gui/util/GuiNPCInterface.java @@ -13,6 +13,7 @@ import net.minecraft.util.ResourceLocation; import net.minecraft.util.StatCollector; import noppes.npcs.client.CustomNpcResourceListener; import noppes.npcs.client.TextBlockClient; +import noppes.npcs.client.gui.util.animation.ViewportGraphEditor; import noppes.npcs.entity.EntityNPCInterface; import org.lwjgl.input.Keyboard; import org.lwjgl.input.Mouse; @@ -37,6 +38,7 @@ public abstract class GuiNPCInterface extends GuiScreen { protected HashMap sliders = new HashMap(); protected HashMap extra = new HashMap(); protected HashMap scrollWindows = new HashMap<>(); + protected HashMap graphEditors = new HashMap<>(); protected HashMap diagrams = new HashMap<>(); public static boolean resizingActive = false; @@ -92,6 +94,7 @@ public abstract class GuiNPCInterface extends GuiScreen { scrolls.clear(); sliders.clear(); scrollWindows.clear(); + graphEditors.clear(); diagrams.clear(); Keyboard.enableRepeatEvents(true); } @@ -106,6 +109,13 @@ public abstract class GuiNPCInterface extends GuiScreen { tf.updateCursorCounter(); } super.updateScreen(); + + for (GuiScrollWindow guiScrollableComponent : scrollWindows.values()) + guiScrollableComponent.updateScreen(); + + for (ViewportGraphEditor graphEditor : graphEditors.values()) + graphEditor.updateScreen(); + } } @@ -120,6 +130,10 @@ public abstract class GuiNPCInterface extends GuiScreen { scrollWindows.put(id, gui); } + public void addGraphEditor(int id, ViewportGraphEditor graph) { + graphEditors.put(id, graph); + } + /** * Adds a GuiDiagram to the interface. * The diagram is initialized with the current resolution and cached. @@ -145,6 +159,9 @@ public abstract class GuiNPCInterface extends GuiScreen { guiScrollableComponent.mouseClicked(i, j, k); } + for (ViewportGraphEditor graphEditor : graphEditors.values()) + graphEditor.mouseClicked(i, j, k); + if (k == 0) { for (GuiCustomScroll scroll : new ArrayList(scrolls.values())) { scroll.mouseClicked(i, j, k); @@ -168,6 +185,10 @@ public abstract class GuiNPCInterface extends GuiScreen { subgui.mouseClickMove(mouseX, mouseY, clickedMouseButton, timeSinceLastClick); return; } + + for (GuiScrollWindow window : scrollWindows.values()) + window.mouseClickMove(mouseX, mouseY, clickedMouseButton, timeSinceLastClick); + super.mouseClickMove(mouseX, mouseY, clickedMouseButton, timeSinceLastClick); } @@ -202,9 +223,11 @@ public abstract class GuiNPCInterface extends GuiScreen { for (GuiNpcTextField tf : textfields.values()) tf.textboxKeyTyped(c, i); - for (GuiScrollWindow guiScrollableComponent : scrollWindows.values()) { + for (GuiScrollWindow guiScrollableComponent : scrollWindows.values()) guiScrollableComponent.keyTyped(c, i); - } + + for (ViewportGraphEditor graphEditor : graphEditors.values()) + graphEditor.keyTyped(c, i); // Fixes closing sub with escape closes all of its parents boolean enoughTimeSinceSubClosed = Minecraft.getSystemTime() - timeClosedSubGui > 50; @@ -310,16 +333,22 @@ public abstract class GuiNPCInterface extends GuiScreen { for (GuiNpcTextField tf : textfields.values()) { tf.drawTextBox(i, j); } + int wheel = Mouse.getDWheel(); for (GuiCustomScroll scroll : scrolls.values()) { scroll.updateSubGUI(subGui); - scroll.drawScreen(i, j, f, !subGui && scroll.isMouseOver(i, j) ? Mouse.getDWheel() : 0); + scroll.drawScreen(i, j, f, !subGui && scroll.isMouseOver(i, j) ? wheel : 0); } for (GuiScreen gui : extra.values()) gui.drawScreen(i, j, f); + // Draw scrollable windows. for (GuiScrollWindow guiScrollableComponent : scrollWindows.values()) { - guiScrollableComponent.drawScreen(i, j, f, !subGui && guiScrollableComponent.isMouseOver(i, j) ? Mouse.getDWheel() : 0); + guiScrollableComponent.drawScreen(i, j, f, !subGui && guiScrollableComponent.isMouseOver(i, j) ? wheel : 0); } + + for (ViewportGraphEditor graphEditor : graphEditors.values()) + graphEditor.drawScreen(i, j, f, wheel); + for (GuiDiagram diagram : diagrams.values()) { diagram.drawDiagram(i, j, subGui); } @@ -442,7 +471,7 @@ public abstract class GuiNPCInterface extends GuiScreen { float f4 = entity.rotationPitch; float f7 = entity.rotationYawHead; float f5 = (float) (guiLeft + x) - mouseX; - float f6 = (float) ((guiTop + y) - 50 * scale * zoomed) - mouseY; + float f6 = (guiTop + y) - 50 * scale * zoomed - mouseY; int orientation = 0; if (npc != null) { orientation = npc.ais.orientation; @@ -484,8 +513,8 @@ public abstract class GuiNPCInterface extends GuiScreen { public void openLink(String link) { try { Class oclass = Class.forName("java.awt.Desktop"); - Object object = oclass.getMethod("getDesktop", new Class[0]).invoke((Object) null, new Object[0]); - oclass.getMethod("browse", new Class[]{URI.class}).invoke(object, new Object[]{new URI(link)}); + Object object = oclass.getMethod("getDesktop", new Class[0]).invoke(null); + oclass.getMethod("browse", new Class[]{URI.class}).invoke(object, new URI(link)); } catch (Throwable throwable) { } } diff --git a/src/main/java/noppes/npcs/client/gui/util/GuiNPCInterface2.java b/src/main/java/noppes/npcs/client/gui/util/GuiNPCInterface2.java index 7c9f1d43e..048fbfc4e 100644 --- a/src/main/java/noppes/npcs/client/gui/util/GuiNPCInterface2.java +++ b/src/main/java/noppes/npcs/client/gui/util/GuiNPCInterface2.java @@ -5,8 +5,8 @@ import noppes.npcs.entity.EntityNPCInterface; import org.lwjgl.opengl.GL11; public abstract class GuiNPCInterface2 extends GuiNPCInterface { - private ResourceLocation background = new ResourceLocation("customnpcs:textures/gui/menubg.png"); - private GuiNpcMenu menu; + private final ResourceLocation background = new ResourceLocation("customnpcs:textures/gui/menubg.png"); + private final GuiNpcMenu menu; public GuiNPCInterface2(EntityNPCInterface npc) { this(npc, -1); diff --git a/src/main/java/noppes/npcs/client/gui/util/GuiScrollWindow.java b/src/main/java/noppes/npcs/client/gui/util/GuiScrollWindow.java index 6c81a0b86..159778bce 100644 --- a/src/main/java/noppes/npcs/client/gui/util/GuiScrollWindow.java +++ b/src/main/java/noppes/npcs/client/gui/util/GuiScrollWindow.java @@ -113,6 +113,9 @@ public class GuiScrollWindow extends GuiScreen implements ITextfieldListener, IC public void mouseEvent(int i, int j, int k) { } + public void mouseClickMove(int mouseX, int mouseY, int clickedMouseButton, long timeSinceLastClick) { + + } @Override protected void actionPerformed(GuiButton guibutton) { @@ -266,7 +269,7 @@ public class GuiScrollWindow extends GuiScreen implements ITextfieldListener, IC super.drawDefaultBackground(); } - protected void setClip(int x, int y, int width, int height) { + public void setClip(int x, int y, int width, int height) { if (scaledResolution == null) return; int scaleFactor = scaledResolution.getScaleFactor(); diff --git a/src/main/java/noppes/npcs/client/gui/util/GuiUtil.java b/src/main/java/noppes/npcs/client/gui/util/GuiUtil.java new file mode 100644 index 000000000..fda94e343 --- /dev/null +++ b/src/main/java/noppes/npcs/client/gui/util/GuiUtil.java @@ -0,0 +1,164 @@ +package noppes.npcs.client.gui.util; + +import net.minecraft.client.Minecraft; +import net.minecraft.client.gui.ScaledResolution; +import net.minecraft.client.renderer.OpenGlHelper; +import net.minecraft.client.renderer.Tessellator; +import org.lwjgl.input.Mouse; +import org.lwjgl.opengl.GL11; + +public class GuiUtil { + + public static void setScissorClip(int x, int y, int width, int height) { + Minecraft mc = Minecraft.getMinecraft(); + ScaledResolution res = new ScaledResolution(mc, mc.displayWidth, mc.displayHeight); + int scale = res.getScaleFactor(); + + + // Adjust position to Top-Left origin (OpenGL window/screen space uses bottom-left origin) + y = mc.displayHeight - y * scale; + height *= scale; + + // Set clip + GL11.glScissor(x * scale, y - height, width * scale, height); + } + + public static void drawHorizontalLine(double y, double startX, double endX, int color) { + if (endX < startX) { + double i1 = startX; + startX = endX; + endX = i1; + } + + drawRectD(startX, y, endX + 1, y + 1, color); + } + + public static void drawVerticalLine(double x, double startY, double endY, int color) { + if (endY < startY) { + double i1 = startY; + startY = endY; + endY = i1; + } + + drawRectD(x, startY + 1, x + 1, endY, color); + } + + public static void drawRectD(double left, double top, double right, double bottom, int color) { + double j1; + + if (left < right) { + j1 = left; + left = right; + right = j1; + } + + if (top < bottom) { + j1 = top; + top = bottom; + bottom = j1; + } + + float f3 = (float) (color >> 24 & 255) / 255.0F; + float f = (float) (color >> 16 & 255) / 255.0F; + float f1 = (float) (color >> 8 & 255) / 255.0F; + float f2 = (float) (color & 255) / 255.0F; + Tessellator tessellator = Tessellator.instance; + GL11.glEnable(GL11.GL_BLEND); + GL11.glDisable(GL11.GL_TEXTURE_2D); + OpenGlHelper.glBlendFunc(770, 771, 1, 0); + GL11.glColor4f(f, f1, f2, 1); + tessellator.startDrawingQuads(); + tessellator.addVertex(left, bottom, 0.0D); + tessellator.addVertex(right, bottom, 0.0D); + tessellator.addVertex(right, top, 0.0D); + tessellator.addVertex(left, top, 0.0D); + tessellator.draw(); + GL11.glEnable(GL11.GL_TEXTURE_2D); + GL11.glDisable(GL11.GL_BLEND); + } + + public static void drawGradientRect(int left, int top, int right, int bottom, int startColor, int endColor) { + double zLevel = 0; + + float f = (float) (startColor >> 24 & 255) / 255.0F; + float f1 = (float) (startColor >> 16 & 255) / 255.0F; + float f2 = (float) (startColor >> 8 & 255) / 255.0F; + float f3 = (float) (startColor & 255) / 255.0F; + float f4 = (float) (endColor >> 24 & 255) / 255.0F; + float f5 = (float) (endColor >> 16 & 255) / 255.0F; + float f6 = (float) (endColor >> 8 & 255) / 255.0F; + float f7 = (float) (endColor & 255) / 255.0F; + GL11.glDisable(GL11.GL_TEXTURE_2D); + GL11.glEnable(GL11.GL_BLEND); + GL11.glDisable(GL11.GL_ALPHA_TEST); + OpenGlHelper.glBlendFunc(770, 771, 1, 0); + GL11.glShadeModel(GL11.GL_SMOOTH); + Tessellator tessellator = Tessellator.instance; + tessellator.startDrawingQuads(); + tessellator.setColorRGBA_F(f1, f2, f3, f); + tessellator.addVertex(right, top, zLevel); + tessellator.addVertex(left, top, zLevel); + tessellator.setColorRGBA_F(f5, f6, f7, f4); + tessellator.addVertex(left, bottom, zLevel); + tessellator.addVertex(right, bottom, zLevel); + tessellator.draw(); + GL11.glShadeModel(GL11.GL_FLAT); + GL11.glDisable(GL11.GL_BLEND); + GL11.glEnable(GL11.GL_ALPHA_TEST); + GL11.glEnable(GL11.GL_TEXTURE_2D); + } + + public static void drawTexturedModalRect(double x, double y, double width, double height, int textureX, int textureY) { + double zLevel = 0; + float f = 1 / 256f; + float f1 = 1 / 256f; + Tessellator tessellator = Tessellator.instance; + tessellator.startDrawingQuads(); + tessellator.addVertexWithUV(x + 0, y + height, zLevel, (float) (textureX) * f, (float) (textureY + height) * f1); + tessellator.addVertexWithUV(x + width, y + height, zLevel, (float) (textureX + width) * f, (float) (textureY + height) * f1); + tessellator.addVertexWithUV(x + width, y + 0, zLevel, (float) (textureX + width) * f, (float) (textureY) * f1); + tessellator.addVertexWithUV(x + 0, y + 0, zLevel, (float) (textureX) * f, (float) (textureY) * f1); + tessellator.draw(); + } + + public static void setMouse(int guiX, int guiY) { + Minecraft mc = Minecraft.getMinecraft(); + ScaledResolution scaledResolution = new ScaledResolution(mc, mc.displayWidth, mc.displayHeight); + + int factor = scaledResolution.getScaleFactor(); + + Mouse.setCursorPosition(guiX * factor, mc.displayHeight - (guiY * factor)); + } + + public static void setMouseX(int guiX) { + Minecraft mc = Minecraft.getMinecraft(); + ScaledResolution scaledResolution = new ScaledResolution(mc, mc.displayWidth, mc.displayHeight); + + int factor = scaledResolution.getScaleFactor(); + Mouse.setCursorPosition(guiX * factor, Mouse.getY()); + } + + public static void setMouseY(int guiY) { + Minecraft mc = Minecraft.getMinecraft(); + ScaledResolution scaledResolution = new ScaledResolution(mc, mc.displayWidth, mc.displayHeight); + + int factor = scaledResolution.getScaleFactor(); + Mouse.setCursorPosition(Mouse.getX(), mc.displayHeight - (guiY * factor)); + } + + public static double preciseMouseX() { + Minecraft mc = Minecraft.getMinecraft(); + ScaledResolution scaledResolution = new ScaledResolution(mc, mc.displayWidth, mc.displayHeight); + + double width = scaledResolution.getScaledWidth(); + return Mouse.getX() * width / mc.displayWidth; + } + + public static double preciseMouseY() { + Minecraft mc = Minecraft.getMinecraft(); + ScaledResolution scaledResolution = new ScaledResolution(mc, mc.displayWidth, mc.displayHeight); + + double height = scaledResolution.getScaledHeight(); + return height - Mouse.getY() * height / mc.displayHeight - 1; + } +} diff --git a/src/main/java/noppes/npcs/client/gui/util/animation/AnimationGraphEditor.java b/src/main/java/noppes/npcs/client/gui/util/animation/AnimationGraphEditor.java new file mode 100644 index 000000000..808fab0d4 --- /dev/null +++ b/src/main/java/noppes/npcs/client/gui/util/animation/AnimationGraphEditor.java @@ -0,0 +1,26 @@ +package noppes.npcs.client.gui.util.animation; + +import noppes.npcs.client.gui.util.GuiNPCInterface; +import noppes.npcs.client.gui.util.GuiUtil; +import noppes.npcs.client.gui.util.animation.panel.PanelFrameType; + +public class AnimationGraphEditor extends ViewportGraphEditor { + public PanelFrameType frameTypePanel = new PanelFrameType(this); + + public AnimationGraphEditor(GuiNPCInterface parent) { + super(parent); + } + + public void initGui(int startX, int startY, int endX, int endY) { + super.initGui(startX, startY, endX, endY); + frameTypePanel.initGui(startX - 60, startY, startX, endY); + } + + public void drawScreen(int mouseX, int mouseY, float partialTicks, int wheel) { + GuiUtil.drawGradientRect(startX, startY, endX, endY, 0xFF303030, 0xFF303030); + + frameTypePanel.draw(); + grid.draw(mouseX, mouseY, partialTicks, wheel); + presetOverlay.draw(mouseX, mouseY, wheel); + } +} diff --git a/src/main/java/noppes/npcs/client/gui/util/animation/Cursors.java b/src/main/java/noppes/npcs/client/gui/util/animation/Cursors.java new file mode 100644 index 000000000..9bb46c59c --- /dev/null +++ b/src/main/java/noppes/npcs/client/gui/util/animation/Cursors.java @@ -0,0 +1,62 @@ +package noppes.npcs.client.gui.util.animation; + +import net.minecraft.client.Minecraft; +import net.minecraft.util.ResourceLocation; +import noppes.npcs.client.gui.util.GuiUtil; +import org.lwjgl.BufferUtils; +import org.lwjgl.LWJGLException; +import org.lwjgl.input.Cursor; +import org.lwjgl.input.Mouse; +import org.lwjgl.opengl.GL11; + +public enum Cursors { + + MOVE(0, 0, 0, 0, 19, 19); + + + public int offsetX, offsetY, textureX, textureY, width, height; + + Cursors(int offsetX, int offsetY, int textureX, int textureY, int width, int height) { + + this.textureX = textureX; + this.textureY = textureY; + this.width = width; + this.height = height; + this.offsetX = -width / 2; + this.offsetY = offsetY; + } + + public void draw(double mouseX, double mouseY) { + offsetX = width / 2; + offsetY = height / 2; + float scale = 0.5f; + double screenX = (mouseX / scale - offsetX); + double screenY = (mouseY / scale - offsetY); + GL11.glColor4f(1, 1, 1, 1); + GL11.glPushMatrix(); + GL11.glScalef(scale, scale, 1); + Minecraft.getMinecraft().getTextureManager().bindTexture(TEXTURE); + GuiUtil.drawTexturedModalRect(screenX, screenY, width, height, textureX, textureY); + GL11.glPopMatrix(); + } + + public static Cursors currentCursor; + + public static void setCursor(Cursors cursor) { + try { + Cursor c = null; + if (cursor != null) + c = new Cursor(1, 1, 0, 0, 1, BufferUtils.createIntBuffer(1), null); + Mouse.setNativeCursor(c); + } catch (LWJGLException e) { + } + currentCursor = cursor; + } + + public static void reset() { + setCursor(null); + } + + private static ResourceLocation TEXTURE = new ResourceLocation("customnpcs:textures/gui/cursors.png"); +} + diff --git a/src/main/java/noppes/npcs/client/gui/util/animation/Grid.java b/src/main/java/noppes/npcs/client/gui/util/animation/Grid.java new file mode 100644 index 000000000..277c3a2eb --- /dev/null +++ b/src/main/java/noppes/npcs/client/gui/util/animation/Grid.java @@ -0,0 +1,388 @@ +package noppes.npcs.client.gui.util.animation; + +import net.minecraft.client.gui.Gui; +import noppes.npcs.client.gui.util.GuiUtil; +import noppes.npcs.client.gui.util.animation.keys.GraphEditorKeyPresets; +import noppes.npcs.client.gui.util.animation.keys.KeyPreset; +import noppes.npcs.util.ValueUtil; +import org.lwjgl.opengl.GL11; + +import static org.lwjgl.opengl.GL11.*; + +public class Grid { + ViewportGraphEditor parent; + public int startX, startY; // Positioning + public int endX, endY; // Positioning + public int yAxisHeight = 12; + + public int mouseX, mouseY; + + public float zoomX = 20f, targetZoomX = 20f, zoomY = 1f, targetZoomY = 1f; //Zoom smoothness + public float panX = 0, panY = 0, targetPanX, targetPanY; // pan offsets + public int startPanX, startPanY; + public boolean smoothenPanX, smoothenPanY; + public int subDivisionX = 1, subDivisionY = 2; + + public boolean isDragging, isResetting; + public boolean centerAroundMouse = true; + + public GridPointManager manager; + + public Grid(ViewportGraphEditor parent) { + this.parent = parent; + manager = new GridPointManager(this); + keys(); + } + + public void keys() { + GraphEditorKeyPresets keys = parent.keys; + + keys.RESET_GRID.setTask((pressType) -> { + if (pressType == KeyPreset.PRESS) { + if (!isResetting) { + if (xDown()) { + targetPanX = 0; + targetZoomX = 20; + smoothenPanX = true; + } else if (yDown()) { + targetPanY = -((parent.height) * subDivisionY / 2); + targetZoomY = 1.0f; + smoothenPanY = true; + } else { + targetPanY = -((parent.height) * subDivisionY / 2); + targetPanX = 0; + targetZoomX = 20; + targetZoomY = 1.0f; + smoothenPanX = smoothenPanY = true; + } + isResetting = true; + centerAroundMouse = false; + } + } + }); + + keys.PAN_GRID.setTask((pressType) -> { + if (pressType == KeyPreset.PRESS) { + isDragging = true; + startPanX = mouseX; + startPanY = mouseY; + } else if (pressType == KeyPreset.RELEASE) + isDragging = false; + }); + } + + public void init(int startX, int startY, int endX, int endY) { + this.startX = startX; + this.startY = startY; + this.endX = endX; + this.endY = endY; + + panY = -(parent.height * subDivisionY / 2); + } + + public void draw(int mouseX, int mouseY, float partialTicks, int wheel) { + this.mouseX = mouseX; + this.mouseY = mouseY; + + ////////////////////////////////////////////////////////////// + ////////////////////////////////////////////////////////////// + // Panning + + if (isDragging && parent.isWithin(mouseX, mouseY) && !isResetting) { + if (xDown()) + panX -= (mouseX - startPanX) / zoomX; + else if (yDown()) + panY -= (mouseY - startPanY) / zoomY; + else { + panX -= (mouseX - startPanX) / zoomX; + panY -= (mouseY - startPanY) / zoomY * subDivisionY; + } + startPanX = mouseX; + startPanY = mouseY; + } + + ////////////////////////////////////////////////////////////// + ////////////////////////////////////////////////////////////// + // Zoom logic + if (wheel != 0 && !parent.presetOverlay.showOverlay) { + float xRate = zoomX < 15 ? 1.1f : 1.25f; + float yRate = zoomY < 15 ? 1.1f : 1.25f; + + if (xDown()) + targetZoomX = ValueUtil.clamp(wheel > 0 ? targetZoomX * xRate : targetZoomX / xRate, 0.025f, 100); + else if (yDown()) + targetZoomY = ValueUtil.clamp(wheel > 0 ? targetZoomY * yRate : targetZoomY / yRate, 0.025f, 100); + else { + targetZoomX = ValueUtil.clamp(wheel > 0 ? targetZoomX * xRate : targetZoomX / xRate, 0.025f, 100); + targetZoomY = ValueUtil.clamp(wheel > 0 ? targetZoomY * yRate : targetZoomY / yRate, 0.025f, 100); + } + } + + if (zoomX != targetZoomX || zoomY != targetZoomY) + zoom(mouseX, mouseY, wheel); + + ////////////////////////////////////////////////////////////// + ////////////////////////////////////////////////////////////// + // Reset logic + if (isResetting) { + if (zoomX == targetZoomX && zoomY == targetZoomY && (smoothenPanX ? panX == targetPanX : smoothenPanY ? panY == targetPanY : true)) { + isResetting = smoothenPanX = smoothenPanY = false; + centerAroundMouse = true; + } + + float rate = 0.1f; + if (smoothenPanX && panX != targetPanX) { + panX = ValueUtil.lerp(panX, targetPanX, rate); + if (Math.abs(panX - targetPanX) < 0.1) + panX = targetPanX; + } + + if (smoothenPanY && panY != targetPanY) { + panY = ValueUtil.lerp(panY, targetPanY, rate); + if (Math.abs(panY - targetPanY) < 0.1) + panY = targetPanY; + } + } + + ////////////////////////////////////////////////////////////// + ////////////////////////////////////////////////////////////// + Gui.drawRect(startX, startY - yAxisHeight, endX, startY, 0xFF161616); //background + GL11.glEnable(GL11.GL_SCISSOR_TEST); + GuiUtil.setScissorClip(startX, startY, parent.width, parent.height); + + GL11.glDepthMask(false); + drawLines(mouseX, mouseY); + GL11.glDepthMask(true); + + manager.draw(mouseX, mouseY, partialTicks); + GL11.glDisable(GL11.GL_SCISSOR_TEST); + } + + public void zoom(int mouseX, int mouseY, int wheel) { + /////////////////////////////////////////////////////// + /////////////////////////////////////////////////////// + //Zooms in on mouse point + double beforeWorldX = worldX(mouseX - startX); + double beforeWorldY = worldY(mouseY - startY); + + float rate = isResetting ? 0.1f : 0.05f; + zoomX = ValueUtil.lerp(zoomX, targetZoomX, rate); + zoomY = ValueUtil.lerp(zoomY, targetZoomY, rate); + + // Snap to exact target value + if (Math.abs(zoomX - targetZoomX) < 0.001) + zoomX = targetZoomX; + if (Math.abs(zoomY - targetZoomY) < 0.001) + zoomY = targetZoomY; + + double afterWorldX = worldX(mouseX - startX); + double afterWorldY = worldY(mouseY - startY); + + if (centerAroundMouse) { + if (xDown()) + panX += beforeWorldX - afterWorldX; + else if (yDown()) + panY += beforeWorldY - afterWorldY; + else { + panX += beforeWorldX - afterWorldX; + panY += beforeWorldY - afterWorldY; + } + } + } + + public void drawLines(int mouseX, int mouseY) { + int darkGray = 0xFF1a1a1a; // Major grid + int lightGray = 0xFF2a2a2a; // Minor grid + + //////////////////////////////////////////////////////////// + //////////////////////////////////////////////////////////// + // Vertical + double stepX = getStepX(); + double firstGridX = Math.floor((panX) / stepX) * stepX; + for (double x = firstGridX, i = 0; x <= worldX(endX); x += stepX / 5, i++) { + boolean isMajor = i % 5 == 0; + double screenX = screenX(x); + + glPushMatrix(); + glTranslatef(startX, 0, 0); + GuiUtil.drawVerticalLine(screenX, startY, endY, isMajor ? darkGray : lightGray); + glPopMatrix(); + } + + //Expand boundaries of scissor clip + GuiUtil.setScissorClip(startX, startY - yAxisHeight, parent.width, parent.height + yAxisHeight); + for (double x = firstGridX; x <= worldX(endX); x += stepX) + drawXValue((int) screenX(x), (int) x); + + //Reset clip + GuiUtil.setScissorClip(startX, startY, parent.width, parent.height); + //////////////////////////////////////////////////////////// + //////////////////////////////////////////////////////////// + // Horizontal + double stepY = getStepY(); + double firstGridY = Math.floor((panY) / stepY) * stepY; + for (double y = firstGridY, i = 0; y <= worldY(endY); y += stepY / 5, i++) { + boolean isMajor = i % 5 == 0; + double screenY = screenY(y); + + glPushMatrix(); + glTranslatef(0, startY, 0); + GuiUtil.drawHorizontalLine(screenY, startX, endX, isMajor ? darkGray : lightGray); + glPopMatrix(); + } + + for (double y = firstGridY; y <= worldY(endY); y += stepY) + drawYValue((int) screenY(y), (int) -y); + + //////////////////////////////////////////////////////////// + //////////////////////////////////////////////////////////// + } + + public double getStepX() { + double stepSize = 0; + if (zoomX < 0.1) // 0-1000 + stepSize = 1000; + else if (zoomX < 0.2) // 0-500 + stepSize = 500; + else if (zoomX < 0.5) // 0-200 + stepSize = 200; + else if (zoomX <= 1) // 0-100 + stepSize = 100; + else if (zoomX < 1.5) //0-50 + stepSize = 50; + else if (zoomX < 3) //0-20 + stepSize = 20; + else if (zoomX < 4.5) //0-10 + stepSize = 10; + else if (zoomX < 10) //0-5 + stepSize = 5; + else if (zoomX < 18) //0-2 For Default reset + stepSize = 2; + else if (zoomX <= 100) //0-1 + stepSize = 1; + return stepSize; + } + + public double getStepY() { + double stepSize = 0; + if (zoomY < 0.1) // 0-1000 + stepSize = 1000; + else if (zoomY < 0.2) // 0-500 + stepSize = 500; + else if (zoomY < 0.5) // 0-200 + stepSize = 200; + else if (zoomY < 0.9) // 0-100 For Default reset + stepSize = 100; + else if (zoomY < 1.5) //0-50 + stepSize = 50; + else if (zoomY < 3) //0-20 + stepSize = 20; + else if (zoomY < 4.5) //0-10 + stepSize = 10; + else if (zoomY < 10) //0-5 + stepSize = 5; + else if (zoomY < 20) //0-2 + stepSize = 2; + else if (zoomY <= 100) //0-1 + stepSize = 1; + return stepSize; + } + + public void drawXValue(int screenX, int value) { + GL11.glPushMatrix(); + float scale = 0.75f; + int adjustedX = (int) ((screenX + 1) / scale); // Offset to the left of grid line + int adjustedY = (int) ((startY - yAxisHeight + 4) / scale); // Adjust Y position + + GL11.glTranslatef(startX, 0, 1f); + GL11.glScalef(scale, scale, 1f); + parent.drawString(String.valueOf(value), adjustedX, adjustedY, 0xFFA0A0A0); + GL11.glPopMatrix(); + } + + public void drawYValue(int screenY, int value) { + GL11.glPushMatrix(); + float scale = 0.5f; + int adjustedX = (int) ((startX + 10) / scale); // Offset to the left of grid line + int adjustedY = (int) ((screenY - 1) / scale); // Adjust Y position + + GL11.glTranslatef(0, startY, 1f); + GL11.glScalef(scale, scale, 1f); + parent.drawString(String.valueOf(value), adjustedX, adjustedY, 0xFFFFFF); + GL11.glPopMatrix(); + } + + public void mouseClicked(int mouseX, int mouseY, int button) { + manager.mouseClicked(mouseX, mouseY, button); + + } + + public void keyTyped(char c, int i) { + manager.keyTyped(c, i); + } + + public boolean xDown() { + return manager.keys.LOCK_X.isDown; + } + + public boolean yDown() { + return manager.keys.LOCK_Y.isDown; + } + + //////////////////////////////////////////////////////////// + //////////////////////////////////////////////////////////// + // Double + public double worldX(double screenX) { + return screenX * subDivisionX / zoomX + panX; + } + + public double worldY(double screenY) { + return screenY * subDivisionY / zoomY + panY; + } + + public double screenX(double worldX) { + return (worldX - panX) * zoomX / subDivisionX; + } + + public double screenY(double worldY) { + return (worldY - panY) * zoomY / subDivisionY; + } + + //////////////////////////////////////////////////////////// + //////////////////////////////////////////////////////////// + // Float + public double worldX(float screenX) { + return screenX * subDivisionX / zoomX + panX; + } + + public double worldY(float screenY) { + return screenY * subDivisionY / zoomY + panY; + } + + public double screenX(float worldX) { + return (worldX - panX) * zoomX / subDivisionX; + } + + public double screenY(float worldY) { + return (worldY - panY) * zoomY / subDivisionY; + } + + //////////////////////////////////////////////////////////// + //////////////////////////////////////////////////////////// + // Int + public double worldX(int screenX) { + return screenX * subDivisionX / zoomX + panX; + } + + public double worldY(int screenY) { + return screenY * subDivisionY / zoomY + panY; + } + + public double screenX(int worldX) { + return (worldX - panX) * zoomX / subDivisionX; + } + + public double screenY(int worldY) { + return (worldY - panY) * zoomY / subDivisionY; + } +} diff --git a/src/main/java/noppes/npcs/client/gui/util/animation/GridPointManager.java b/src/main/java/noppes/npcs/client/gui/util/animation/GridPointManager.java new file mode 100644 index 000000000..17119c127 --- /dev/null +++ b/src/main/java/noppes/npcs/client/gui/util/animation/GridPointManager.java @@ -0,0 +1,464 @@ +package noppes.npcs.client.gui.util.animation; + +import net.minecraft.client.gui.FontRenderer; +import net.minecraft.client.renderer.Tessellator; +import net.minecraft.util.ResourceLocation; +import noppes.npcs.client.gui.util.GuiUtil; +import noppes.npcs.client.gui.util.animation.keys.GraphEditorKeyPresets; +import noppes.npcs.client.gui.util.animation.keys.KeyPreset; +import noppes.npcs.client.utils.Color; +import noppes.npcs.constants.animation.EnumFrameType; +import noppes.npcs.util.Ease; +import noppes.npcs.util.ValueUtil; +import org.lwjgl.input.Mouse; +import org.lwjgl.opengl.GL11; + +import java.util.*; +import java.util.function.BiConsumer; + +public class GridPointManager { + public Grid grid; + public GraphEditorKeyPresets keys; + public HashMap> typePoints = new HashMap<>(); + public List highlightedTypes = new ArrayList<>(); + public List highlightedPoints = new ArrayList<>(); + public Point selectedPoint; + public Playhead playhead = new Playhead(0); + + public boolean isFreeTransforming; + public double ftGrabX, ftGrabY; //where point is grabbed on free transforming + + public GridPointManager(Grid grid) { + this.grid = grid; + keys = grid.parent.keys; + keys(); + } + + public void keys() { + EnumFrameType type = EnumFrameType.ROTATION_X; + + keys.SELECT_POINT.setTask((pressType) -> { + if (pressType == KeyPreset.PRESS) { + forEachActive((pointType, point) -> { + if (point.isMouseAbove(grid.mouseX, grid.mouseY)) + setSelectedPoint(point); + }); + } + }); + + keys.ADD_POINT.setTask((pressType) -> { + if (pressType == KeyPreset.PRESS) { + if (!highlightedTypes.contains(type)) + highlightedTypes.add(type); + + TreeMap points = pointsOf(type); + Point point = points != null ? points.get((double) playhead.worldX) : null; //check if it exists + if (point == null) + point = addPoint(type, playhead.worldX, 0); // worldX(mouseX - startX), worldY(mouseY - startY) + + setSelectedPoint(point); + } + }); + + keys.DELETE_POINT.setTask((pressType) -> { + if ((pressType == KeyPreset.PRESS) && selectedPoint != null && !grid.isDragging) { + deletePoint(type, selectedPoint.worldX); + } + }); + + keys.FREE_TRANSFORM.setTask((pressType) -> { + if (pressType == KeyPreset.PRESS && selectedPoint != null) { + if (isFreeTransforming) { + selectedPoint.set(ftGrabX, ftGrabY); + ftGrabX = ftGrabY = 0; + Cursors.reset(); + } else { + ftGrabX = selectedPoint.worldX; + ftGrabY = selectedPoint.worldY; + Cursors.setCursor(Cursors.MOVE); + } + + isFreeTransforming = !isFreeTransforming; + } + }); + + } + + public void mouseClicked(int mouseX, int mouseY, int button) { + if (button == 0 && selectedPoint != null && !isFreeTransforming) { + if (grid.parent.isWithin(mouseX, mouseY) && !selectedPoint.isMouseAbove(mouseX, mouseY)) + setSelectedPoint(null); + } + + if (button == 0 && isFreeTransforming) { + selectedPoint.updateKey(); + Cursors.reset(); + isFreeTransforming = false; + } + + playhead.mouseClicked(mouseX, mouseY, button); + forEachActive((type, point) -> { + point.mouseClicked(mouseX, mouseY, button); + }); + } + + public Point addPoint(EnumFrameType type, double x, double y) { + return addPoint(type, new Point(type, x, y)); + } + + public Point addPoint(EnumFrameType type, Point point) { + TreeMap points = pointsOf(type); + if (points == null) { + points = new TreeMap<>(); + typePoints.put(type, points); + } + + points.put(point.worldX, point); + return point; + } + + public void deletePoint(EnumFrameType type, double x) { + TreeMap points = pointsOf(type); + if (points == null) + return; + + Point point = points.remove(x); + if (point == selectedPoint) + setSelectedPoint(null); + } + + public Point getPoint(EnumFrameType type, double x) { + TreeMap points = pointsOf(type); + if (points == null) + return null; + + return points.get(x); + } + + public TreeMap pointsOf(EnumFrameType type) { + return typePoints.get(type); + } + + public void forEachActive(BiConsumer consumer) { + for (Map.Entry> entry : typePoints.entrySet()) { + EnumFrameType type = entry.getKey(); + + if (!highlightedTypes.contains(type)) + continue; + + for (Map.Entry points : entry.getValue().entrySet()) { + Point point = points.getValue(); + consumer.accept(type, point); + } + } + } + + public void setSelectedPoint(Point point) { + if (selectedPoint != null) + selectedPoint.highlighted = false; + + this.selectedPoint = point; + + if (point != null) + point.highlighted = true; + } + + public Point getNext(Point point) { + TreeMap points = pointsOf(point.type); + if (points == null) + return null; + + Map.Entry entry = points.higherEntry(point.worldX); + if (entry != null) + return entry.getValue(); + + return null; + } + + public void drawEasedCurve(Point from, Point to) { + if (from == null || to == null) + return; + + GL11.glDisable(GL11.GL_TEXTURE_2D); + GL11.glEnable(GL11.GL_BLEND); + GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA); + GL11.glEnable(GL11.GL_LINE_SMOOTH); + GL11.glHint(GL11.GL_LINE_SMOOTH_HINT, GL11.GL_NICEST); + GL11.glLineWidth(2F); + + GL11.glPushMatrix(); + GL11.glTranslatef(grid.startX, grid.startY, 0); + GL11.glColor4f(1, 1, 1, 1); + + Tessellator tessellator = Tessellator.instance; + tessellator.startDrawing(GL11.GL_LINE_STRIP); + + + for (float t = 0; t <= 1; t += 0.01) { + float easedT = Ease.INOUTQUINT.apply(t); + double xt = ValueUtil.lerp(from.screenX() + 0.5, to.screenX() + 0.5, t); + double yt = ValueUtil.lerp(from.screenY() + 0.5, to.screenY() + 0.5, easedT); + + tessellator.addVertex(xt, yt, 0); + } + + tessellator.draw(); + GL11.glPopMatrix(); + + GL11.glLineWidth(1F); + GL11.glDisable(GL11.GL_LINE_SMOOTH); + GL11.glDisable(GL11.GL_BLEND); + GL11.glEnable(GL11.GL_TEXTURE_2D); + } + + public void draw(int mouseX, int mouseY, float partialTicks) { + + + //////////////////////////////////////////////////////////////////////// + //////////////////////////////////////////////////////////////////////// + // Free transform logic + if (isFreeTransforming && selectedPoint != null) { + if (grid.xDown()) + selectedPoint.setX(Math.round(grid.worldX(GuiUtil.preciseMouseX() - grid.startX))); + else if (grid.yDown()) + selectedPoint.setY(grid.worldY(GuiUtil.preciseMouseY() - grid.startY)); + else + selectedPoint.set(Math.round(grid.worldX(GuiUtil.preciseMouseX() - grid.startX)), grid.worldY(GuiUtil.preciseMouseY() - grid.startY)); + } + + //////////////////////////////////////////////////////////////////////// + //////////////////////////////////////////////////////////////////////// + playhead.draw(mouseX, mouseY, partialTicks); + + forEachActive((type, point) -> { + drawEasedCurve(point, getNext(point)); + point.draw(mouseX, mouseY, partialTicks); + }); + + if (Cursors.currentCursor != null) + Cursors.currentCursor.draw(mouseX, mouseY); + + //////////////////////////////////////////////////////////////////////// + //////////////////////////////////////////////////////////////////////// + // Pans grid while free transforming + // MUST BE DONE AT THE VERY END -> CHANGES CURSOR POSITION WITHOUT + // CHANGING GuiScreen.drawScreen(mouseX,mouseY) VALUES + if (isFreeTransforming) { + if (mouseX < grid.startX) { + int deltaX = mouseX - grid.startX; + grid.panX += deltaX / grid.zoomX; + GuiUtil.setMouseX(grid.startX - 1); + } + if (mouseX > grid.endX) { + int deltaX = mouseX - grid.endX; + grid.panX += (deltaX / grid.zoomX); + GuiUtil.setMouseX(grid.endX + 1); + } + if (mouseY < grid.startY) { + int deltaY = mouseY - grid.startY; + grid.panY += deltaY / grid.zoomY; + GuiUtil.setMouseY(grid.startY); + } + if (mouseY > grid.endY) { + int deltaY = mouseY - grid.endY; + grid.panY += deltaY / grid.zoomY; + GuiUtil.setMouseY(grid.endY + 2); + } + } + + //////////////////////////////////////////////////////////////////////// + //////////////////////////////////////////////////////////////////////// + } + + public void keyTyped(char c, int key) { + } + + private static ResourceLocation TEXTURE = new ResourceLocation("customnpcs:textures/gui/animation.png"); + + public class Point { + public EnumFrameType type; + public double worldX, worldY; + public boolean highlighted; + + public double previousX; + + public Point(EnumFrameType type, double worldX, double worldY) { + this.type = type; + this.worldX = previousX = worldX; + this.worldY = worldY; + } + + public void setX(double x) { + worldX = (int) x; + } + + public void setY(double y) { + worldY = y; + } + + public void set(double x, double y) { + setX(x); + setY(y); + } + + public void updateKey() { + TreeMap points = pointsOf(type); + if (points == null) + return; + + points.remove(previousX); + points.put(worldX, this); + previousX = worldX; + } + + public void draw(int mouseX, int mouseY, float partialTicks) { + float scale = 0.1f; + int textureWidth = 32, textureHeight = 32; + float offsetX = (textureHeight / 2) * scale - 0.5f, offsetY = (textureHeight / 2) * scale - 0.5f; + double screenX = screenX() / scale, screenY = screenY() / scale; + + grid.parent.mc.getTextureManager().bindTexture(TEXTURE); + GL11.glEnable(GL11.GL_BLEND); + GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA); + new Color(highlighted ? 0xffbe33 : 0xbfbfbf, 1).glColor(); + + GL11.glPushMatrix(); + GL11.glTranslatef(grid.startX - offsetX, grid.startY - offsetY, 0); + GL11.glScalef(scale, scale, 1); + GuiUtil.drawTexturedModalRect(screenX, screenY, textureWidth, textureHeight, 256 - textureWidth, 256 - textureHeight); + + GL11.glPopMatrix(); + GL11.glDisable(GL11.GL_BLEND); + } + + public boolean isMouseAbove(int mouseX, int mouseY) { + int rMX = mouseX - grid.startX, rMY = mouseY - grid.startY; //relative to grid + float scale = 0.1f; + float textureWidth = (32), textureHeight = (32); + float offsetX = (textureWidth / 2) * scale - 0.5f, offsetY = (textureHeight / 2) * scale - 0.5f; + + int screenX = (int) (screenX() - offsetX); + int screenY = (int) ((screenY()) - offsetY); + + return rMX >= screenX && rMX < screenX + textureWidth * scale && rMY >= screenY && rMY < screenY + textureHeight * scale; + } + + public double screenX() { + + return grid.screenX(worldX); + } + + public double screenY() { + return grid.screenY(worldY); + } + + public void mouseClicked(int mouseX, int mouseY, int button) { + } + } + + public class Playhead { + public int worldX; + public boolean isDragging; + + public Playhead(int currentX) { + this.worldX = currentX; + } + + public void draw(int mouseX, int mouseY, float partialTicks) { + // Dragging + if (isDragging && !Mouse.isButtonDown(0)) + isDragging = false; + + if (isDragging) + worldX = (int) Math.round(grid.worldX(GuiUtil.preciseMouseX() - grid.startX)); + + //Expand clip boundary + GuiUtil.setScissorClip(grid.startX, grid.startY - grid.yAxisHeight, grid.parent.width, grid.parent.height + grid.yAxisHeight); + + //////////////////////////////////////////////////////////// + //////////////////////////////////////////////////////////// + // Head lines + double screenX = screenX(); + float height = grid.endY - grid.startY; + + GL11.glPushMatrix(); + GL11.glTranslated(grid.startX + screenX, grid.startY, 0); + GL11.glDepthMask(false); + GuiUtil.drawVerticalLine(-1, 0, height, 0xFF212121); + GuiUtil.drawVerticalLine(0, -5, height, 0xFF4772b3); + GuiUtil.drawVerticalLine(1, 0, height, 0xFF212121); + GL11.glPopMatrix(); + + //////////////////////////////////////////////////////////// + //////////////////////////////////////////////////////////// + // Head texture + FontRenderer fontRenderer = grid.parent.getFontRenderer(); + float textureScaleX = 0.5f, textureScaleY = 0.5f; + + // Texture width dynamically scales with string width + int defaultWidth = 6, width = fontRenderer.getStringWidth(worldX + ""); + if (width / defaultWidth > 1) + textureScaleX += width / defaultWidth * 0.125; + + int textureWidth = 25, textureHeight = 17; + float offset = (textureWidth / 2) * textureScaleX; + float screenY = grid.startY - grid.yAxisHeight; + + grid.parent.mc.getTextureManager().bindTexture(TEXTURE); + GL11.glEnable(GL11.GL_BLEND); + GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA); + new Color(0x426dae, 1).glColor(); + + GL11.glPushMatrix(); + GL11.glTranslatef(grid.startX, 0, 0); + + GL11.glPushMatrix(); + GL11.glTranslatef(-offset, 0, 0); + GL11.glScalef(textureScaleX, textureScaleY, 1); + + GuiUtil.drawTexturedModalRect(screenX / textureScaleX, (screenY + 2) / textureScaleY, textureWidth, textureHeight, 228, 199); + + GL11.glPopMatrix(); + GL11.glDisable(GL11.GL_BLEND); + + //////////////////////////////////////////////////////////// + //////////////////////////////////////////////////////////// + // Head value + float vScale = 0.75f; + double valueScreenX = (screenX + 1) / vScale; + double valueScreenY = (screenY + 4) / vScale; + GL11.glScalef(vScale, vScale, 1); + grid.parent.drawString(String.valueOf(worldX), (int) valueScreenX, (int) valueScreenY, 0xFFccd7e9); + GL11.glPopMatrix(); + + //////////////////////////////////////////////////////////// + //////////////////////////////////////////////////////////// + //Reset clip + GuiUtil.setScissorClip(grid.startX, grid.startY, grid.parent.width, grid.parent.height); + } + + public boolean isMouseAbove(int mouseX, int mouseY) { + int rMX = mouseX - grid.startX, rMY = mouseY - grid.startY; //relative to grid + float scale = 0.5f; + float textureWidth = (25 * scale), textureHeight = (17 * scale); + float offsetX = (textureWidth / 2) * scale; + + int screenX = (int) Math.round(screenX() - (offsetX / scale)); + int screenY = -grid.yAxisHeight + 2; + + return rMX >= screenX && rMX <= screenX + textureWidth && rMY >= screenY && rMY < screenY + textureHeight; + } + + public void mouseClicked(int mouseX, int mouseY, int button) { + + if (button == 0 && mouseX >= grid.startX && mouseX <= grid.endX && mouseY >= grid.startY - grid.yAxisHeight && mouseY <= grid.startY) { + worldX = (int) Math.round(grid.worldX(GuiUtil.preciseMouseX() - grid.startX)); + isDragging = true; + } + } + + public double screenX() { + return grid.screenX(worldX); + } + } +} diff --git a/src/main/java/noppes/npcs/client/gui/util/animation/OverlayKeyPresetViewer.java b/src/main/java/noppes/npcs/client/gui/util/animation/OverlayKeyPresetViewer.java new file mode 100644 index 000000000..8376042da --- /dev/null +++ b/src/main/java/noppes/npcs/client/gui/util/animation/OverlayKeyPresetViewer.java @@ -0,0 +1,419 @@ +package noppes.npcs.client.gui.util.animation; + +import net.minecraft.client.Minecraft; +import net.minecraft.client.gui.FontRenderer; +import net.minecraft.util.ResourceLocation; +import noppes.npcs.client.gui.util.GuiUtil; +import noppes.npcs.client.gui.util.animation.keys.KeyPreset; +import noppes.npcs.client.gui.util.animation.keys.KeyPresetManager; +import noppes.npcs.client.utils.Color; +import noppes.npcs.util.ValueUtil; +import org.lwjgl.input.Mouse; +import org.lwjgl.opengl.GL11; + +import java.util.LinkedList; +import java.util.List; + +public class OverlayKeyPresetViewer { + private static final ResourceLocation TEXTURE = new ResourceLocation("customnpcs:textures/gui/keypreset_highres.png"); + + public int startX, startY, endX, endY, width, height; + public int mouseX, mouseY; + public boolean showOverlay; + + public int elementSpacing = 5, yStartSpacing = 5; + public float scale = 0.75f; + public Scrollable scroll = new Scrollable(); + + public KeyPresetManager manager; + public OverlayButton viewButton = new OverlayButton(); + public LinkedList list = new LinkedList<>(); + + private final FontRenderer font = Minecraft.getMinecraft().fontRenderer; + + public OverlayKeyPresetViewer(KeyPresetManager manager) { + this.manager = manager; + + manager.keys.forEach((key) -> { + PresetElement element = new PresetElement(key); + list.add(element); + }); + } + + public void initGui(int startX, int startY, int endX, int endY) { + this.startX = startX; + this.startY = startY; + this.endX = endX; + this.endY = endY; + this.width = endX - startX; + this.height = endY - startY; + scroll.init(); + } + + public void draw(int mouseX, int mouseY, int wheel) { + this.mouseX = mouseX; + this.mouseY = mouseY; + boolean aboveButton = viewButton.isMouseAbove(mouseX, mouseY); + boolean aboveOverlay = isMouseAbove(mouseX, mouseY); + + if (aboveButton) + showOverlay = true; + + if (showOverlay) { + drawOverlay(wheel); + + if (!aboveOverlay && !aboveButton) + showOverlay = false; + } + + viewButton.drawButton(); + } + + public void drawOverlay(int wheel) { + scroll.update(wheel); + + GL11.glEnable(GL11.GL_SCISSOR_TEST); + GuiUtil.drawGradientRect(startX, startY, endX, endY, 0x88000000, 0x88303030); + if (scroll.maxScroll > 0) + scroll.drawBar(); + + GuiUtil.setScissorClip(startX, startY, scroll.maxScroll > 0 ? width - scroll.barWidth : width, height); + GL11.glPushMatrix(); + GL11.glTranslatef((startX + 2), startY + yStartSpacing - scroll.scrollY, 0); + + for (int i = 0, height = 0; i < list.size(); i++) { + PresetElement element = list.get(i); + GL11.glPushMatrix(); + GL11.glScalef(scale, scale, 1); + GL11.glTranslatef(0, Math.round(height / scale), 0); + element.drawDescription(); + GL11.glPopMatrix(); + + element.drawBox(height); + height += element.getHeight(); + } + + GL11.glPopMatrix(); + GL11.glDisable(GL11.GL_SCISSOR_TEST); + } + + public void keyTyped(char c, int i) { + if (!showOverlay) + return; + + list.forEach((element) -> { + if (element.isEditing) + element.keyTyped(i); + }); + } + + public void mouseClicked(int mouseX, int mouseY, int button) { + if (!showOverlay) + return; + + list.forEach((element) -> { + if (!element.isMouseAboveBox(mouseX, mouseY)) { + if (element.isEditing) + element.cancelEdit(); + } else + element.boxClicked(button); + + if (element.isMouseAboveReset(mouseX, mouseY) && !element.key.isDefault()) { + element.key.defaultState.writeTo(element.key.currentState); + manager.save(); + } + + }); + } + + public boolean isMouseAbove(int mouseX, int mouseY) { + if (scroll.isMouseDragging) + return true; + + return mouseX >= startX && mouseX < endX && mouseY >= startY && mouseY < endY; + } + + public int getWidth() { + return (int) (width / scale); + } + + public class Scrollable { + private float scrollY, targetScrollY, maxScroll; + private int totalHeight, scrollbarHeight, barWidth = 2; + private float heightFactor, scrollFactor; + + private boolean isMouseDragging; + private int startDragY; + + public Scrollable() { + } + + public void init() { + totalHeight = 0; + for (PresetElement element : list) + totalHeight += element.getHeight(); + + totalHeight -= yStartSpacing; + heightFactor = height / totalHeight; + scrollbarHeight = Math.max((int) (height * heightFactor), 20); + scrollFactor = (height - yStartSpacing - scrollbarHeight) / maxScroll; + + scroll.maxScroll = Math.max(0, totalHeight - height); + } + + public void update(int wheel) { + if (wheel != 0 && isMouseAbove(mouseX, mouseY)) + targetScrollY = ValueUtil.clamp(targetScrollY - wheel / 15, 0, maxScroll); + + + if (Mouse.isButtonDown(0)) { + if (!isMouseDragging && isMouseAboveBar(mouseX, mouseY)) { + isMouseDragging = true; + startDragY = (int) (mouseY - scrollY * scrollFactor); + } + } else + isMouseDragging = false; + + if (isMouseDragging) + scrollY = targetScrollY = ValueUtil.clamp((mouseY - startDragY) / scrollFactor, 0, maxScroll); + + if (scrollY != targetScrollY) { + scrollY = ValueUtil.lerp(scrollY, targetScrollY, 0.1f); + + if (Math.abs(scrollY - targetScrollY) < 0.001) // Snap to exact target value + scrollY = targetScrollY; + } + + scrollY = ValueUtil.clamp(scrollY, 0, maxScroll); + } + + public void drawBar() { + GL11.glPushMatrix(); + GL11.glTranslatef(endX - barWidth - 2, startY + yStartSpacing + scrollY * scrollFactor, 0); + GuiUtil.drawRectD(0, 0, barWidth, scrollbarHeight, (isMouseAboveBar(mouseX, mouseY) || isMouseDragging) ? 0xffbababa : 0xff767676); + GL11.glPopMatrix(); + } + + public boolean isMouseAboveBar(int mouseX, int mouseY) { + int barX = endX - barWidth - 2; + int barY = (int) (startY + yStartSpacing + scrollY * scrollFactor); + + return mouseX >= barX && mouseX < barX + barWidth && mouseY >= barY && mouseY < barY + scrollbarHeight; + } + } + + public class PresetElement { + public KeyPreset key; + public boolean isEditing; + public float boxScreenX, boxScreenY; + + public KeyPreset.KeyState newState = new KeyPreset.KeyState(); + + public PresetElement(KeyPreset key) { + this.key = key; + } + + public void drawDescription() { + String name = String.format("> %s", key.name); + font.drawString(name, 0, 0, 0xffffff); + + if (!key.shouldConflict) + font.drawString("(Conflict-free)", font.getStringWidth(name) + 5, 0, 0xface40); + + String description = String.format("- %s", key.description); + font.drawSplitString(description, 9, font.FONT_HEIGHT, getMaxStringWidth() - 9, 0x888888); + } + + public void drawBox(int offsetY) { + //////////////////////////////////////////// + //////////////////////////////////////////// + //Box texture + Minecraft.getMinecraft().getTextureManager().bindTexture(TEXTURE); + GL11.glPushMatrix(); + GL11.glColor4f(1, 1, 1, 1); + float boxScaleX = 1.75f; + float boxWidth = 32 * boxScaleX; + boxScreenX = getMaxStringWidth() * scale + 8; //remove scaling from maxStringWidth + GL11.glScalef(boxScaleX, 1, 0); + + if (isEditing) + new Color(0x4772b3, 1).glColor(); + else if (isMouseAboveBox(mouseX, mouseY)) + new Color(0x656565, 1).glColor(); + else + new Color(0x545454, 1).glColor(); + GuiUtil.drawTexturedModalRect(boxScreenX / boxScaleX, boxScreenY = offsetY - 5, 32, 20, 0, 492); + GL11.glPopMatrix(); + + //Reset box + boolean isDefault = key.isDefault(); + float resetWidth = 10; + float resetScreenX = boxScreenX + boxWidth + 2; + + if (isMouseAboveReset(mouseX, mouseY) && !isDefault) + new Color(0x656565, 1).glColor(); + else + new Color(0x545454, 1).glColor(); + GuiUtil.drawTexturedModalRect(resetScreenX, boxScreenY, 10, 20, 33, 492); + + //////////////////////////////////////////// + //////////////////////////////////////////// + //Key name + String name = getBoxKeyName(); + float nameWidth = font.getStringWidth(name); + float nameScale = scale; + float nameBoxRatio = nameWidth * scale / boxWidth; + + GL11.glPushMatrix(); + if (nameBoxRatio > 0.9) { + nameScale /= nameWidth / boxWidth / 1.2f; + GL11.glTranslatef(0, nameBoxRatio * 0.5f, 0); + } + + float nameX = boxScreenX + boxWidth / 2 - (nameWidth / 2 * nameScale); + float nameY = offsetY + 11.5f - (font.FONT_HEIGHT / 2); + GL11.glScalef(nameScale, nameScale, 1); + GL11.glTranslatef(nameX / nameScale, nameY / nameScale, 1); + font.drawStringWithShadow(name, 0, 0, conflicts() ? 0xff5555 : 0xffffffff); + GL11.glPopMatrix(); + + //Reset letter + char letter = 'X'; + float letterX = resetScreenX + (resetWidth / 2) - (font.getCharWidth(letter) / 2 * scale) + 0.25f; + GL11.glPushMatrix(); + GL11.glScalef(scale, scale, 1); + GL11.glTranslatef(letterX / scale, nameY / scale, 1); + font.drawStringWithShadow(String.valueOf(letter), 0, 0, isDefault ? 0xff8a8a8a : 0xffffffff); + GL11.glPopMatrix(); + + //////////////////////////////////////////// + //////////////////////////////////////////// + } + + public void boxClicked(int button) { + if (isEditing) + setKey(-100 + button); + else if (button == 0) + isEditing = true; + } + + public void keyTyped(int typedKey) { + if (typedKey == 1) { + setKey(0); + return; + } + + newState.setState(newState.keyCode, KeyPreset.isCtrlKeyDown(), KeyPreset.isAltKeyDown(), KeyPreset.isShiftKeyDown()); + if (KeyPreset.isNotCtrlAltShift(typedKey)) + setKey(typedKey); + } + + public void setKey(int keyCode) { + if (keyCode == 0) + newState.clear(); + + newState.keyCode = keyCode; + newState.writeTo(key.currentState); + cancelEdit(); + manager.save(); + } + + public void cancelEdit() { + newState.clear(); + isEditing = false; + } + + public boolean conflicts() { + return list.stream().anyMatch(element -> key.shouldConflict && element.key.shouldConflict && element.key != this.key && element.key.equals(this.key)); + } + + public String getBoxKeyName() { + if (isEditing) { + String modifiers = ""; + if (KeyPreset.isCtrlKeyDown()) + modifiers += "CTRL "; + if (KeyPreset.isAltKeyDown()) + modifiers += "ALT "; + if (KeyPreset.isShiftKeyDown()) + modifiers += "SHIFT "; + + return !modifiers.isEmpty() ? modifiers : "Press a key"; + } + + return key.currentState.getName(); + } + + public boolean isMouseAboveBox(int mouseX, int mouseY) { + mouseX -= startX + 2; + mouseY -= startY + 5 - scroll.scrollY; + + float boxScaleX = 1.75f; + float screenY = boxScreenY + 10; + + return mouseX >= boxScreenX && mouseX < boxScreenX + 32 * boxScaleX && mouseY >= screenY && mouseY < screenY + 10; + } + + public boolean isMouseAboveReset(int mouseX, int mouseY) { + mouseX -= startX + 2; + mouseY -= startY + 5 - scroll.scrollY; + + float boxScaleX = 1.75f; + float resetWidth = 10, boxWidth = 32 * boxScaleX; + float screenX = boxScreenX + boxWidth + 2; + float screenY = boxScreenY + 10; + + return mouseX >= screenX && mouseX < screenX + resetWidth && mouseY >= screenY && mouseY < screenY + 10; + } + + public int getHeight() { + List wrappedDescription = font.listFormattedStringToWidth(String.format("- %s", key.description), getMaxStringWidth()); + + int nameHeight = font.FONT_HEIGHT; + int translations = font.FONT_HEIGHT; + int descriptionHeight = wrappedDescription.size() * (font.FONT_HEIGHT); + + return Math.round((nameHeight + descriptionHeight + translations) * scale) + elementSpacing; + } + + public int getMaxStringWidth() { + return (int) (getWidth() * 2.5f / 4); + } + } + + public class OverlayButton { + public int startX, startY, endX, width, height; + public int textureWidth = 252, textureHeight = 109; + public float scale = 0.25f; + + public OverlayButton() { + } + + public void initGui(int endX, int startY) { + this.endX = endX; + this.startY = startY; + + startX = (int) (endX - textureWidth * scale / 2); + } + + public void drawButton() { + Minecraft.getMinecraft().getTextureManager().bindTexture(TEXTURE); + + double screenX = startX / scale, screenY = startY / scale; + int oldMinFilter = GL11.glGetTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER); + GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR); + + GL11.glPushMatrix(); + float color = 0.55f; + GL11.glColor4f(color, color, color, 1); + GL11.glScalef(scale, scale, 1); + GuiUtil.drawTexturedModalRect(screenX, screenY, textureWidth, textureHeight, 0, 0); + GL11.glPopMatrix(); + + GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, oldMinFilter); + } + + public boolean isMouseAbove(int mouseX, int mouseY) { + return mouseX >= startX && mouseX < startX + textureWidth * scale / 2 && mouseY >= startY - 4 && mouseY < startY + textureHeight * scale / 2; + } + } +} diff --git a/src/main/java/noppes/npcs/client/gui/util/animation/ViewportGraphEditor.java b/src/main/java/noppes/npcs/client/gui/util/animation/ViewportGraphEditor.java new file mode 100644 index 000000000..252331674 --- /dev/null +++ b/src/main/java/noppes/npcs/client/gui/util/animation/ViewportGraphEditor.java @@ -0,0 +1,76 @@ +package noppes.npcs.client.gui.util.animation; + +import net.minecraft.client.Minecraft; +import net.minecraft.client.gui.FontRenderer; +import noppes.npcs.client.gui.util.GuiNPCInterface; +import noppes.npcs.client.gui.util.GuiUtil; +import noppes.npcs.client.gui.util.animation.keys.GraphEditorKeyPresets; + +public class ViewportGraphEditor { + public Minecraft mc = Minecraft.getMinecraft(); + public GuiNPCInterface parent; + + public int startX, startY, endX, endY, width, height; + + public GraphEditorKeyPresets keys = new GraphEditorKeyPresets(); + public OverlayKeyPresetViewer presetOverlay = new OverlayKeyPresetViewer(keys); + + public Grid grid = new Grid(this); + + public ViewportGraphEditor(GuiNPCInterface parent) { + this.parent = parent; + } + + public void initGui(int startX, int startY, int endX, int endY) { + this.startX = startX; + this.startY = startY; + this.endX = endX; + this.endY = endY; + this.width = endX - this.startX; + this.height = endY - this.startY; + + grid.init(this.startX, this.startY, endX, endY); + presetOverlay.initGui(endX - (width / 2) - 37, startY + (height / 4), endX, endY); + presetOverlay.viewButton.initGui(endX + 1, endY + 2); + } + + public void updateScreen() { + keys.tick(); + } + + public void drawScreen(int mouseX, int mouseY, float partialTicks, int wheel) { + GuiUtil.drawGradientRect(startX, startY, endX, endY, 0xFF303030, 0xFF303030); + grid.draw(mouseX, mouseY, partialTicks, wheel); + presetOverlay.draw(mouseX, mouseY, wheel); + } + + public void mouseClicked(int mouseX, int mouseY, int button) { + grid.mouseClicked(mouseX, mouseY, button); + presetOverlay.mouseClicked(mouseX, mouseY, button); + } + + public void keyTyped(char c, int i) { + grid.keyTyped(c, i); + presetOverlay.keyTyped(c, i); + } + + public boolean isWithin(int mouseX, int mouseY) { + if (parent.hasSubGui()) + return false; + + return mouseX >= startX && mouseX <= startX + width && mouseY >= startY && mouseY <= startY + height; + } + + public void drawString(String text, int x, int y, int color) { + parent.drawCenteredString(getFontRenderer(), text, x, y, color); + } + + public FontRenderer getFontRenderer() { + return parent.getFontRenderer(); + } + + public void close() { + if (Cursors.currentCursor != null) + Cursors.reset(); + } +} diff --git a/src/main/java/noppes/npcs/client/gui/util/animation/keys/GraphEditorKeyPresets.java b/src/main/java/noppes/npcs/client/gui/util/animation/keys/GraphEditorKeyPresets.java new file mode 100644 index 000000000..aa06d7da3 --- /dev/null +++ b/src/main/java/noppes/npcs/client/gui/util/animation/keys/GraphEditorKeyPresets.java @@ -0,0 +1,25 @@ +package noppes.npcs.client.gui.util.animation.keys; + +import org.lwjgl.input.Keyboard; + +public class GraphEditorKeyPresets extends KeyPresetManager { + + //////////////////// + //Grid controls + public final KeyPreset PAN_GRID = add("Pan Grid").setDefaultState(KeyPreset.RIGHT_MOUSE, false, false, false).setDescription("Holding pans the grid with mouse cursor"); + public final KeyPreset LOCK_X = add("Lock X Axis").setDefaultState(Keyboard.KEY_X, false, false, false).setDescription("Holding locks the X axis while zooming or panning").shouldConflict(false); + public final KeyPreset LOCK_Y = add("Lock Y Axis").setDefaultState(Keyboard.KEY_Y, false, false, false).setDescription("Holding locks the Y axis while zooming or panning").shouldConflict(false); + public final KeyPreset RESET_GRID = add("Reset Grid").setDefaultState(Keyboard.KEY_R, false, false, false).setDescription("Resets the grid's XY scale and translation to the origins"); + + + //////////////////// + //Grid Point controls + public final KeyPreset SELECT_POINT = add("Select Point").setDefaultState(KeyPreset.LEFT_MOUSE, false, false, false).setDescription("Select point under cursor"); + public final KeyPreset ADD_POINT = add("Add Point").setDefaultState(Keyboard.KEY_Z, false, false, false).setDescription("Add a new point at the play-head's X"); + public final KeyPreset DELETE_POINT = add("Delete Point").setDefaultState(Keyboard.KEY_X, true, false, false).setDescription("Delete selected point"); + public final KeyPreset FREE_TRANSFORM = add("Free Transform").setDefaultState(Keyboard.KEY_G, false, false, false).setDescription("Freely transform the selected point with the mouse").markDone(this); + + public GraphEditorKeyPresets() { + super("graph_editor"); + } +} diff --git a/src/main/java/noppes/npcs/client/gui/util/animation/keys/KeyPreset.java b/src/main/java/noppes/npcs/client/gui/util/animation/keys/KeyPreset.java new file mode 100644 index 000000000..805a22d4a --- /dev/null +++ b/src/main/java/noppes/npcs/client/gui/util/animation/keys/KeyPreset.java @@ -0,0 +1,249 @@ +package noppes.npcs.client.gui.util.animation.keys; + +import net.minecraft.client.Minecraft; +import net.minecraft.client.settings.GameSettings; +import net.minecraft.nbt.NBTTagCompound; +import org.lwjgl.input.Keyboard; +import org.lwjgl.input.Mouse; + +import java.util.function.Consumer; + +public class KeyPreset { + /** + * @field SINGLE_PRESS => on releasing before 5 ticks press time + * @field PRESS_RELEASE => on pressing and releasing + */ + public static final int PRESS = 0, HOLD = 1, RELEASE = 2, SINGLE_PRESS = 3, PRESS_RELEASE = 4; + + public KeyState defaultState = new KeyState(); + public KeyState currentState = new KeyState(); + + public String name, description; + public boolean shouldConflict = true; + public Consumer task; + + public int pressTime; + public boolean isDown; + + + public KeyPreset(String name) { + this.name = name; + } + + public KeyPreset setDefaultState(int keyCode, boolean hasCtrl, boolean hasAlt, boolean hasShift) { + defaultState.setState(keyCode, hasCtrl, hasAlt, hasShift); + currentState.setState(keyCode, hasCtrl, hasAlt, hasShift); + return this; + } + + public KeyPreset setDescription(String description) { + this.description = description; + return this; + } + + public KeyPreset setTask(Consumer task) { + this.task = task; + return this; + } + + public KeyPreset shouldConflict(boolean shouldConflict) { + this.shouldConflict = shouldConflict; + return this; + } + + public void tick() { + int keyCode = keyCode(); + if (keyCode == -1 || keyCode == 0) + return; + + boolean isDown = isMouseKey() ? Mouse.isButtonDown(keyCode + 100) : Keyboard.isKeyDown(keyCode); + isDown = isDown && (hasCtrl() ? isCtrlKeyDown() : true) && (hasAlt() ? isAltKeyDown() : true && (hasShift() ? isShiftKeyDown() : true)); + + setDown(isDown); + } + + public void setDown(boolean down) { + if (down && !isDown) + onAction(PRESS, PRESS_RELEASE); + + if (!down && isDown) { + if (pressTime <= 5) + onAction(SINGLE_PRESS); + + onAction(RELEASE, PRESS_RELEASE); + pressTime = 0; + } + + if (isDown || down) //to fire in both press & release + onAction(HOLD); + + if (isDown) + pressTime++; + + this.isDown = down; + } + + public KeyPreset onAction(int... pressTypes) { + if (task != null) { + for (int pressType : pressTypes) + this.task.accept(pressType); + } + return this; + } + + public boolean isDefault() { + return currentState.equals(defaultState); + } + + public boolean isMouseKey() { + return keyCode() < -1; + } + + public int keyCode() { + return currentState.keyCode; + } + + public boolean hasCtrl() { + return currentState.hasCtrl; + } + + public boolean hasAlt() { + return currentState.hasAlt; + } + + public boolean hasShift() { + return currentState.hasShift; + } + + public String getKeyName() { + return currentState.getName(); + } + + public void clear() { + currentState.clear(); + } + + //call on the last key added, to load saved presets + public KeyPreset markDone(KeyPresetManager manager) { + manager.load(); + return this; + } + + public void writeToNbt(NBTTagCompound c) { + c.setTag(name, currentState.writeToNbt()); + } + + public void readFromNbt(NBTTagCompound compound) { + currentState.readFromNbt(compound.getCompoundTag(name)); + } + + public boolean equals(Object preset) { + if (preset == this) + return true; + + if (preset instanceof KeyPreset) { + KeyPreset key = (KeyPreset) preset; + return key.currentState.equals(currentState); + } + + return false; + } + + + public static class KeyState { + public int keyCode = -1; + public boolean hasCtrl, hasAlt, hasShift; + + public void setState(int keyCode, boolean hasCtrl, boolean hasAlt, boolean hasShift) { + this.keyCode = keyCode; + this.hasCtrl = hasCtrl; + this.hasAlt = hasAlt; + this.hasShift = hasShift; + } + + public void readFrom(KeyState state) { + keyCode = state.keyCode; + hasCtrl = state.hasCtrl; + hasShift = state.hasShift; + hasAlt = state.hasAlt; + } + + public void writeTo(KeyState state) { + state.setState(keyCode, hasCtrl, hasAlt, hasShift); + } + + public boolean hasState() { + return keyCode != -1; + } + + public void clear() { + this.keyCode = -1; + hasCtrl = hasAlt = hasShift = false; + } + + public boolean equals(Object preset) { + if (preset == this) + return true; + + if (preset instanceof KeyState) { + KeyState state = (KeyState) preset; + return state.keyCode == keyCode && state.hasCtrl == hasCtrl && state.hasAlt == hasAlt && state.hasShift == hasShift; + } + + return false; + } + + public NBTTagCompound writeToNbt() { + NBTTagCompound compound = new NBTTagCompound(); + compound.setInteger("keyCode", keyCode); + compound.setBoolean("hasCtrl", hasCtrl); + compound.setBoolean("hasShift", hasShift); + compound.setBoolean("hasAlt", hasAlt); + return compound; + } + + public void readFromNbt(NBTTagCompound compound) { + this.keyCode = compound.getInteger("keyCode"); + this.hasCtrl = compound.getBoolean("hasCtrl"); + this.hasShift = compound.getBoolean("hasShift"); + this.hasAlt = compound.getBoolean("hasAlt"); + } + + public String getName() { + int code = keyCode; + String name = ""; + + if (code == LEFT_MOUSE) + name = "Left Mouse"; + else if (code == RIGHT_MOUSE) + name = "Right Mouse"; + else if (code == MIDDLE_MOUSE) + name = "Middle Mouse"; + else { + name = code == -1 ? "" : GameSettings.getKeyDisplayString(code); + if (name.contains("Button")) + name = name.replace("Button", "Mouse"); + } + + return (hasCtrl ? "CTRL " : "") + (hasAlt ? "ALT " : "") + (hasShift ? "SHIFT " : "") + name; + } + } + + public static final int LEFT_MOUSE = -100, RIGHT_MOUSE = -99, MIDDLE_MOUSE = -98, MOUSE_4 = -97, MOUSE_5 = -96; + + public static boolean isCtrlKeyDown() { + return Minecraft.isRunningOnMac ? Keyboard.isKeyDown(219) || Keyboard.isKeyDown(220) : Keyboard.isKeyDown(29) || Keyboard.isKeyDown(157); + } + + public static boolean isAltKeyDown() { + return Keyboard.isKeyDown(56) || Keyboard.isKeyDown(184); + } + + public static boolean isShiftKeyDown() { + return Keyboard.isKeyDown(42) || Keyboard.isKeyDown(54); + } + + public static boolean isNotCtrlAltShift(int key) { + return key != 219 && key != 220 && key != 29 && key != 157 && key != 42 && key != 54 && key != 56 && key != 184; + } +} diff --git a/src/main/java/noppes/npcs/client/gui/util/animation/keys/KeyPresetManager.java b/src/main/java/noppes/npcs/client/gui/util/animation/keys/KeyPresetManager.java new file mode 100644 index 000000000..7ffebfba4 --- /dev/null +++ b/src/main/java/noppes/npcs/client/gui/util/animation/keys/KeyPresetManager.java @@ -0,0 +1,77 @@ +package noppes.npcs.client.gui.util.animation.keys; + +import net.minecraft.nbt.NBTTagCompound; +import noppes.npcs.CustomNpcs; +import noppes.npcs.LogWriter; +import noppes.npcs.util.NBTJsonUtil; + +import java.io.File; +import java.util.ArrayList; +import java.util.List; + +public class KeyPresetManager { + public List keys = new ArrayList<>(); + public String fileName; + + public KeyPresetManager(String fileName) { + this.fileName = fileName; + } + + public KeyPreset add(String name) { + KeyPreset preset = new KeyPreset(name); + keys.add(preset); + return preset; + } + + public void tick() { + for (KeyPreset key : keys) + key.tick(); + } + + + public void load() { + try { + File dir = getDir(); + File file = new File(dir, fileName + ".json"); + if (!file.exists()) + return; + + NBTTagCompound compound = NBTJsonUtil.LoadFile(file); + for (KeyPreset key : keys) + key.readFromNbt(compound); + + } catch (Exception e) { + LogWriter.except(e); + } + } + + public void save() { + try { + File dir = getDir(); + String filename = fileName + ".json"; + + File newFile = new File(dir, filename + "_new"); + File originalFile = new File(dir, filename); + + NBTTagCompound compound = new NBTTagCompound(); + for (KeyPreset key : keys) + key.writeToNbt(compound); + + NBTJsonUtil.SaveFile(newFile, compound); + + if (originalFile.exists()) + originalFile.delete(); + + newFile.renameTo(originalFile); + } catch (Exception e) { + LogWriter.except(e); + } + } + + public File getDir() { + File dir = new File(CustomNpcs.Dir, "keypresets"); + if (!dir.exists()) + dir.mkdir(); + return dir; + } +} diff --git a/src/main/java/noppes/npcs/client/gui/util/animation/panel/PanelFrameType.java b/src/main/java/noppes/npcs/client/gui/util/animation/panel/PanelFrameType.java new file mode 100644 index 000000000..53437dd3b --- /dev/null +++ b/src/main/java/noppes/npcs/client/gui/util/animation/panel/PanelFrameType.java @@ -0,0 +1,92 @@ +package noppes.npcs.client.gui.util.animation.panel; + +import noppes.npcs.client.gui.util.GuiUtil; +import noppes.npcs.client.gui.util.animation.AnimationGraphEditor; +import noppes.npcs.constants.animation.EnumFrameType; +import org.lwjgl.opengl.GL11; + +import java.util.LinkedList; + +public class PanelFrameType { + public AnimationGraphEditor graph; + + public int startX, startY, endX, endY, width, height; + public float scale = 0.66f; + public int elementHeight = 5, elementSpacing = 7; + + public LinkedList list = new LinkedList<>(); + + public PanelFrameType(AnimationGraphEditor graph) { + this.graph = graph; + + for (EnumFrameType type : EnumFrameType.values()) + list.add(new Element(type)); + } + + public void initGui(int startX, int startY, int endX, int endY) { + this.startX = startX; + this.startY = startY; + this.endX = endX; + this.endY = endY; + this.width = endX - startX; + this.height = endY - startY; + } + + public void draw() { + // GuiUtil.drawRectD(startX,startY,endX,endY, 0xffffffff); + GL11.glPushMatrix(); + GL11.glTranslatef((startX + 2), startY, 0); + + + for (int i = 0; i < list.size(); i++) { + Element element = list.get(i); + element.draw(i); + } + + GL11.glPopMatrix(); + } + + public void keyTyped(char c, int i) { + list.forEach((element) -> { + }); + } + + public void mouseClicked(int mouseX, int mouseY, int button) { + list.forEach((element) -> { + }); + } + + public boolean isMouseAbove(int mouseX, int mouseY) { + return mouseX >= startX && mouseX < endX && mouseY >= startY && mouseY < endY; + } + + public int getWidth() { + return (int) (width / scale); + } + + public class Element { + public EnumFrameType type; + + public Element(EnumFrameType key) { + this.type = key; + } + + public void draw(int index) { + + + // GuiUtil.drawRectD(100,100,endX,endY, type.color); + float offsetY = index * (elementHeight + elementSpacing + 2) / scale; + GL11.glPushMatrix(); + GL11.glScalef(scale, scale, 1); + GL11.glTranslatef(0, offsetY, 0); + GuiUtil.drawRectD(0, 0, width / scale, (elementHeight + elementSpacing) / scale, 0x0); + GuiUtil.drawRectD(0, 0, 3, (elementHeight + elementSpacing) / scale, type.color); + + GL11.glTranslatef(0, ((elementHeight + elementSpacing) / 2), 0); + String name = String.format("%s", type.toString()); + graph.getFontRenderer().drawString(name, 6, 0, 0xffffff); + + GL11.glPopMatrix(); + } + } +} diff --git a/src/main/java/noppes/npcs/client/utils/Color.java b/src/main/java/noppes/npcs/client/utils/Color.java new file mode 100644 index 000000000..85e63b30d --- /dev/null +++ b/src/main/java/noppes/npcs/client/utils/Color.java @@ -0,0 +1,107 @@ +package noppes.npcs.client.utils; + +import cpw.mods.fml.relauncher.Side; +import cpw.mods.fml.relauncher.SideOnly; +import net.minecraft.nbt.NBTTagCompound; +import org.lwjgl.opengl.GL11; + +public class Color { + public int color; + public float alpha; + + public Color(int color, float alpha) { + setColor(color, alpha); + } + + public Color(int red, int green, int blue, float alpha) { + setColor(red, green, blue, alpha); + } + + public void setColor(int color, float alpha) { + this.color = color; + this.alpha = alpha; + } + + public void setColor(int red, int green, int blue, float alpha) { + this.color = (red << 16) + (green << 8) + blue; + this.alpha = alpha; + } + + public static Color lerpRGBA(Color color1, Color color2, float fraction) { + fraction = Math.min(fraction, 1f); + int red = (int) (color1.getRed() + ((color2.getRed() - color1.getRed()) * fraction)); + int green = (int) (color1.getGreen() + ((color2.getGreen() - color1.getGreen()) * fraction)); + int blue = (int) (color1.getBlue() + ((color2.getBlue() - color1.getBlue()) * fraction)); + float newAlpha = color1.alpha + ((color2.alpha - color1.alpha) * fraction); + int newColor = (red << 16) + (green << 8) + blue; + return new Color(newColor, newAlpha); + } + + public Color lerpRGBA(Color color2, float fraction) { + return Color.lerpRGBA(this, color2, fraction); + } + + public Color multiply(float multi) { + int r = (int) ((getRed() * multi)); + int g = (int) ((getGreen() * multi)); + int b = (int) ((getBlue() * multi)); + float a = alpha * multi; + return new Color((r << 16) + (g << 8) + b, a); + } + + @SideOnly(Side.CLIENT) + public void glColor() { + GL11.glColor4f(getRedF(), getGreenF(), getBlueF(), alpha); + } + + public NBTTagCompound writeToNBT(NBTTagCompound compound, String name) { + compound.setInteger(name + "Color", color); + compound.setFloat(name + "Alpha", alpha); + + return compound; + } + + public void readFromNBT(NBTTagCompound compound, String name) { + setColor(compound.getInteger(name + "Color"), compound.getFloat(name + "Alpha")); + } + + public int getRed() { + return (color >> 16 & 0xFF); + } + + public float getRedF() { + return (float) (color >> 16 & 0xFF) / 255f; + } + + public int getGreen() { + return (color >> 8 & 0xFF); + } + + public float getGreenF() { + return (float) (color >> 8 & 0xFF) / 255f; + } + + public int getBlue() { + return (color & 0xFF); + } + + public float getBlueF() { + return (float) (color & 0xFF) / 255f; + } + + public static String getColor(int color) { + String str; + for (str = Integer.toHexString(color); str.length() < 6; str = "0" + str) { + } + return str; + } + + public static String getColor(int color, float alpha) { + String str = getColor(color); + return str + " | " + (int) (alpha * 255); + } + + public Color clone() { + return new Color(color, alpha); + } +} diff --git a/src/main/java/noppes/npcs/constants/animation/EnumFrameType.java b/src/main/java/noppes/npcs/constants/animation/EnumFrameType.java new file mode 100644 index 000000000..43873aa6f --- /dev/null +++ b/src/main/java/noppes/npcs/constants/animation/EnumFrameType.java @@ -0,0 +1,17 @@ +package noppes.npcs.constants.animation; + +import noppes.npcs.client.utils.Color; + +public enum EnumFrameType { + ROTATION_X(0xff3352), ROTATION_Y(0x8bdc00), ROTATION_Z(0x2890ff), PIVOT_X(0xff3352), PIVOT_Y(0x8bdc00), PIVOT_Z(0x2890ff); + + public int color; + + EnumFrameType(int color) { + this.color = color; + } + + public Color getColor() { + return new Color(color, 1); + } +} diff --git a/src/main/java/noppes/npcs/controllers/data/Animation.java b/src/main/java/noppes/npcs/controllers/data/Animation.java index b60f9ad4d..24abfaa8d 100644 --- a/src/main/java/noppes/npcs/controllers/data/Animation.java +++ b/src/main/java/noppes/npcs/controllers/data/Animation.java @@ -54,6 +54,14 @@ public class Animation implements IAnimation { return this.parent; } + public boolean hasFrame(int index) { + return index >= 0 && index < frames.size(); + } + + public IFrame getFrame(int index) { + return hasFrame(index) ? frames.get(index) : null; + } + public IFrame currentFrame() { return currentFrame < frames.size() ? frames.get(currentFrame) : null; } @@ -76,12 +84,22 @@ public class Animation implements IAnimation { } public IAnimation addFrame(IFrame frame) { - this.frames.add((Frame) frame); - return this; + return addFrame((Frame) frame); } public IAnimation addFrame(int index, IFrame frame) { - this.frames.add(index, (Frame) frame); + return addFrame(index, (Frame) frame); + } + + public IAnimation addFrame(Frame frame) { + frame.setParent(this); + this.frames.add(frame); + return this; + } + + public IAnimation addFrame(int index, Frame frame) { + frame.setParent(this); + this.frames.add(index, frame); return this; } @@ -192,7 +210,7 @@ public class Animation implements IAnimation { for (int i = 0; i < list.tagCount(); i++) { NBTTagCompound item = list.getCompoundTagAt(i); Frame frame = new Frame(); - frame.parent = this; + frame.setParent(this); frame.readFromNBT(item); frames.add(frame); } @@ -230,6 +248,18 @@ public class Animation implements IAnimation { return compound; } + public int getCurrentTick() { + int time = 0; + for (int i = 0; i < this.frames.size(); i++) { + if (i >= this.currentFrame) { + break; + } + time += this.frames.get(i).getDuration(); + } + time += this.currentFrameTime; + return time; + } + public boolean increaseTime() { if (paused) return false; diff --git a/src/main/java/noppes/npcs/controllers/data/Frame.java b/src/main/java/noppes/npcs/controllers/data/Frame.java index 4294b9cf3..5a8edf554 100644 --- a/src/main/java/noppes/npcs/controllers/data/Frame.java +++ b/src/main/java/noppes/npcs/controllers/data/Frame.java @@ -9,9 +9,8 @@ import noppes.npcs.constants.EnumAnimationPart; import java.util.HashMap; import java.util.Map; - public class Frame implements IFrame { - public Animation parent; + private Animation parent; public HashMap frameParts = new HashMap<>(); public int duration = 0; @@ -36,16 +35,42 @@ public class Frame implements IFrame { this.customized = true; } + public int getStartTick() { + int time = 0; + for (int i = 0; i < parent.frames.size(); i++) { + Frame f = parent.frames.get(i); + if (f == this) + break; + + time += f.getDuration(); + } + return time; + } + + public void setParent(Animation parent) { + this.parent = parent; + } + + public Animation getParent() { + return parent; + } + public HashMap getFrameMap() { return frameParts; } + public void addPart(EnumAnimationPart key, FramePart frame) { + frame.setParent(parent); + frameParts.put(key, frame); + } + public IFramePart[] getParts() { return frameParts.values().toArray(new IFramePart[0]); } public IFrame addPart(IFramePart partConfig) { - this.frameParts.put(((FramePart) partConfig).getPart(), (FramePart) partConfig); + ((FramePart) partConfig).setParent(parent); + addPart(((FramePart) partConfig).getPart(), (FramePart) partConfig); return this; } @@ -150,6 +175,7 @@ public class Frame implements IFrame { for (int i = 0; i < list.tagCount(); i++) { NBTTagCompound item = list.getCompoundTagAt(i); FramePart framePart = new FramePart(); + framePart.setParent(parent); framePart.readFromNBT(item); if (!framePart.customized) { framePart.smooth = this.smooth; @@ -184,7 +210,7 @@ public class Frame implements IFrame { Frame frame = new Frame(this.duration); HashMap frameParts = this.frameParts; for (Map.Entry entry : frameParts.entrySet()) { - frame.frameParts.put(entry.getKey(), entry.getValue().copy()); + frame.addPart(entry.getKey(), entry.getValue().copy()); } frame.parent = this.parent; frame.duration = this.duration; diff --git a/src/main/java/noppes/npcs/controllers/data/FramePart.java b/src/main/java/noppes/npcs/controllers/data/FramePart.java index 15490dd7e..5d2e43bb7 100644 --- a/src/main/java/noppes/npcs/controllers/data/FramePart.java +++ b/src/main/java/noppes/npcs/controllers/data/FramePart.java @@ -6,13 +6,22 @@ import net.minecraft.nbt.NBTTagCompound; import noppes.npcs.api.handler.data.IFramePart; import noppes.npcs.client.ClientEventHandler; import noppes.npcs.constants.EnumAnimationPart; +import noppes.npcs.constants.animation.EnumFrameType; +import noppes.npcs.util.Ease; +import noppes.npcs.util.ValueUtil; + +import java.util.HashMap; +import java.util.Map; public class FramePart implements IFramePart { + public Animation parent; public EnumAnimationPart part; public float[] rotation = {0, 0, 0}; public float[] pivot = {0, 0, 0}; + public HashMap values = new HashMap<>(); + boolean customized = false; public float speed = 1.0F; @@ -32,6 +41,14 @@ public class FramePart implements IFramePart { this.part = part; } + public void setParent(Animation parent) { + this.parent = parent; + } + + public Animation getParent() { + return parent; + } + public EnumAnimationPart getPart() { return part; } @@ -114,12 +131,28 @@ public class FramePart implements IFramePart { public void readFromNBT(NBTTagCompound compound) { part = EnumAnimationPart.valueOf(compound.getString("Part")); for (int i = 0; i < 3; i++) { - rotation[i] = compound.getFloat("Rotation" + i); + if (compound.hasKey("Rotation" + i)) { + rotation[i] = compound.getFloat("Rotation" + i); + addValue(EnumFrameType.values()[i], rotation[i]); + } } for (int i = 0; i < 3; i++) { - pivot[i] = compound.getFloat("Pivot" + i); + if (compound.hasKey("Pivot" + i)) { + pivot[i] = compound.getFloat("Pivot" + i); + addValue(EnumFrameType.values()[3 + i], rotation[i]); + } } + if (compound.hasKey("Rotation0")) { + for (int i = 0; i < 3; i++) { + rotation[i] = compound.getFloat("Rotation" + i); + addValue(EnumFrameType.values()[i], rotation[i]); + pivot[i] = compound.getFloat("Pivot" + i); + addValue(EnumFrameType.values()[3 + i], rotation[i]); + } + } + + // Customized = TRUE if Speed or Smooth Exist if (compound.hasKey("Speed")) { customized = true; @@ -129,6 +162,15 @@ public class FramePart implements IFramePart { customized = true; smooth = compound.getByte("Smooth"); } + + for (EnumFrameType type : EnumFrameType.values()) { + String name = type.toString().toLowerCase(); + if (compound.hasKey(name)) { + NBTTagCompound cmpd = compound.getCompoundTag(name); + Ease easing = Ease.valueOf(cmpd.getString("easing")); + addValue(type, cmpd.getFloat("value"), easing); + } + } } public NBTTagCompound writeToNBT() { @@ -141,6 +183,11 @@ public class FramePart implements IFramePart { compound.setFloat("Pivot" + i, pivot[i]); } + for (Map.Entry entry : values.entrySet()) { + EnumFrameType type = entry.getKey(); + compound.setTag(type.toString().toLowerCase(), entry.getValue().writeToNBT()); + } + if (customized) { compound.setFloat("Speed", speed); compound.setByte("Smooth", smooth); @@ -165,33 +212,75 @@ public class FramePart implements IFramePart { return; float pi = (float) Math.PI / 180; - if (this.smooth == 2) { - this.prevRotations[0] = this.rotation[0] * pi; - this.prevRotations[1] = this.rotation[1] * pi; - this.prevRotations[2] = this.rotation[2] * pi; - } else if (this.partialRotationTick != ClientEventHandler.partialRenderTick) { + if (this.partialRotationTick != ClientEventHandler.partialRenderTick) { this.partialRotationTick = ClientEventHandler.partialRenderTick; - if (this.smooth == 0) { - this.prevRotations[0] = (this.rotation[0] * pi - this.prevRotations[0]) * Math.abs(this.speed) / 10f + this.prevRotations[0]; - this.prevRotations[1] = (this.rotation[1] * pi - this.prevRotations[1]) * Math.abs(this.speed) / 10f + this.prevRotations[1]; - this.prevRotations[2] = (this.rotation[2] * pi - this.prevRotations[2]) * Math.abs(this.speed) / 10f + this.prevRotations[2]; - } else { - int directionX = Float.compare(this.rotation[0] * pi, this.prevRotations[0]); - this.prevRotations[0] += directionX * this.speed / 10f; - this.prevRotations[0] = directionX == 1 ? - Math.min(this.rotation[0] * pi, this.prevRotations[0]) : Math.max(this.rotation[0] * pi, this.prevRotations[0]); - int directionY = Float.compare(this.rotation[1] * pi, this.prevRotations[1]); - this.prevRotations[1] += directionY * this.speed / 10f; - this.prevRotations[1] = directionY == 1 ? - Math.min(this.rotation[1] * pi, this.prevRotations[1]) : Math.max(this.rotation[1] * pi, this.prevRotations[1]); - int directionZ = Float.compare(this.rotation[2] * pi, this.prevRotations[2]); - this.prevRotations[2] += directionZ * this.speed / 10f; - this.prevRotations[2] = directionZ == 1 ? - Math.min(this.rotation[2] * pi, this.prevRotations[2]) : Math.max(this.rotation[2] * pi, this.prevRotations[2]); + boolean newLogic = true; + if (newLogic && parent != null) { + Frame next = (Frame) parent.getFrame(parent.currentFrame + 1); + if (next != null) { + FramePart nextPart = next.frameParts.get(part); + if (nextPart != null) { + float value = Ease.OUTBOUNCE.apply(getInterpolationValue()); + this.prevRotations[0] = ValueUtil.lerp(values.get(EnumFrameType.ROTATION_X).value * pi, nextPart.values.get(EnumFrameType.ROTATION_X).value * pi, value); + this.prevRotations[1] = ValueUtil.lerp(values.get(EnumFrameType.ROTATION_Y).value * pi, nextPart.values.get(EnumFrameType.ROTATION_Y).value * pi, value); + this.prevRotations[2] = ValueUtil.lerp(values.get(EnumFrameType.ROTATION_Z).value * pi, nextPart.values.get(EnumFrameType.ROTATION_Z).value * pi, value); + + this.prevRotations[0] = ValueUtil.lerp(this.rotation[0] * pi, nextPart.rotation[0] * pi, value); + this.prevRotations[1] = ValueUtil.lerp(this.rotation[1] * pi, nextPart.rotation[1] * pi, value); + this.prevRotations[2] = ValueUtil.lerp(this.rotation[2] * pi, nextPart.rotation[2] * pi, value); + } + } + } else { /** + * Smoothing 0 - Interpolated => equivalent to {@link Ease#INSINE} + */ + float value = Math.abs(speed) / 20f; + this.prevRotations[0] = ValueUtil.lerp(this.prevRotations[0], this.rotation[0] * pi, value); + this.prevRotations[1] = ValueUtil.lerp(this.prevRotations[1], this.rotation[1] * pi, value); + this.prevRotations[2] = ValueUtil.lerp(this.prevRotations[2], this.rotation[2] * pi, value); } } } + public float getInterpolationValue() { // a 0-1 that lerps between currentFrame startTick and nextFrame startTick + float currentTick = parent.getCurrentTick(); + if (currentTick == 0) + return 0; + + Frame current = parent.frames.get(parent.currentFrame); + float partialTicks = parent.paused ? 0 : partialRotationTick; + currentTick += partialTicks; + float ratio = (currentTick - current.getStartTick()) / current.duration; // + + System.out.println("currentTick " + currentTick + ", interp_value " + ratio); + return ratio; + } + + /** + * Smoothing 2 - None => equivalent to {@link Ease#CONSTANT} + */ + public void constant() { + float pi = (float) Math.PI / 180; + this.prevRotations[0] = this.rotation[0] * pi; + this.prevRotations[1] = this.rotation[1] * pi; + this.prevRotations[2] = this.rotation[2] * pi; + } + + /** + * Smoothing 1 - Linear => equivalent to {@link Ease#LINEAR} + */ + public void linear() { + float pi = (float) Math.PI / 180; + int directionX = Float.compare(this.rotation[0] * pi, this.prevRotations[0]); + this.prevRotations[0] += directionX * this.speed / 10f; + this.prevRotations[0] = directionX == 1 ? Math.min(this.rotation[0] * pi, this.prevRotations[0]) : Math.max(this.rotation[0] * pi, this.prevRotations[0]); + int directionY = Float.compare(this.rotation[1] * pi, this.prevRotations[1]); + this.prevRotations[1] += directionY * this.speed / 10f; + this.prevRotations[1] = directionY == 1 ? Math.min(this.rotation[1] * pi, this.prevRotations[1]) : Math.max(this.rotation[1] * pi, this.prevRotations[1]); + int directionZ = Float.compare(this.rotation[2] * pi, this.prevRotations[2]); + this.prevRotations[2] += directionZ * this.speed / 10f; + this.prevRotations[2] = directionZ == 1 ? Math.min(this.rotation[2] * pi, this.prevRotations[2]) : Math.max(this.rotation[2] * pi, this.prevRotations[2]); + } + @SideOnly(Side.CLIENT) public void interpolateOffset() { if (parent != null && parent.paused) @@ -210,16 +299,13 @@ public class FramePart implements IFramePart { } else { int directionX = Float.compare(this.pivot[0], this.prevPivots[0]); this.prevPivots[0] += directionX * this.speed / 10f; - this.prevPivots[0] = directionX == 1 ? - Math.min(this.pivot[0], this.prevPivots[0]) : Math.max(this.pivot[0], this.prevPivots[0]); + this.prevPivots[0] = directionX == 1 ? Math.min(this.pivot[0], this.prevPivots[0]) : Math.max(this.pivot[0], this.prevPivots[0]); int directionY = Float.compare(this.pivot[1], this.prevPivots[1]); this.prevPivots[1] += directionY * this.speed / 10f; - this.prevPivots[1] = directionY == 1 ? - Math.min(this.pivot[1], this.prevPivots[1]) : Math.max(this.pivot[1], this.prevPivots[1]); + this.prevPivots[1] = directionY == 1 ? Math.min(this.pivot[1], this.prevPivots[1]) : Math.max(this.pivot[1], this.prevPivots[1]); int directionZ = Float.compare(this.pivot[2], this.prevPivots[2]); this.prevPivots[2] += directionZ * this.speed / 10f; - this.prevPivots[2] = directionZ == 1 ? - Math.min(this.pivot[2], this.prevPivots[2]) : Math.max(this.pivot[2], this.prevPivots[2]); + this.prevPivots[2] = directionZ == 1 ? Math.min(this.pivot[2], this.prevPivots[2]) : Math.max(this.pivot[2], this.prevPivots[2]); } } } @@ -238,4 +324,53 @@ public class FramePart implements IFramePart { this.prevRotations[1] = this.rotation[1] * pi; this.prevRotations[2] = this.rotation[2] * pi; } + + public IFramePart addValue(String type, float value) { + addValue(EnumFrameType.valueOf(type.toUpperCase()), value, Ease.INSINE); + return this; + } + + public IFramePart addValue(String type, float value, String easing) { + addValue(EnumFrameType.valueOf(type.toUpperCase()), value, Ease.valueOf(easing.toUpperCase())); + return this; + } + + public void addValue(EnumFrameType type, float value) { + addValue(type, value, Ease.INSINE); + } + + public void addValue(EnumFrameType type, float value, Ease ease) { + Value val = new Value(value, ease); + + if (!values.containsKey(type)) + values.put(type, val); + else + values.replace(type, val); + } + + public void setEase(EnumFrameType type, Ease ease) { + if (values.containsKey(type)) + values.get(type).easing = ease; + } + + public class Value { + public float value; + public Ease easing; + + public Value(float value, Ease ease) { + this.value = value; + this.easing = ease; + } + + public Value(float value) { + this(value, Ease.INOUTSINE); + } + + public NBTTagCompound writeToNBT() { + NBTTagCompound cmpd = new NBTTagCompound(); + cmpd.setFloat("value", value); + cmpd.setString("easing", easing.toString()); + return cmpd; + } + } } diff --git a/src/main/java/noppes/npcs/controllers/data/Magic.java b/src/main/java/noppes/npcs/controllers/data/Magic.java index fda3a4edc..ffba31540 100644 --- a/src/main/java/noppes/npcs/controllers/data/Magic.java +++ b/src/main/java/noppes/npcs/controllers/data/Magic.java @@ -173,7 +173,7 @@ public class Magic implements IMagic { @Override public float getInteraction(int magicID, float value) { - if(hasInteraction(magicID)) + if (hasInteraction(magicID)) return interactions.get(magicID); return 0.0f; } diff --git a/src/main/java/noppes/npcs/controllers/data/MagicCycle.java b/src/main/java/noppes/npcs/controllers/data/MagicCycle.java index e1074891c..23bffc97d 100644 --- a/src/main/java/noppes/npcs/controllers/data/MagicCycle.java +++ b/src/main/java/noppes/npcs/controllers/data/MagicCycle.java @@ -18,7 +18,8 @@ public class MagicCycle implements IMagicCycle { public EnumDiagramLayout layout = EnumDiagramLayout.CIRCULAR; public HashMap associations = new HashMap<>(); - public MagicCycle(){} + public MagicCycle() { + } public void readNBT(NBTTagCompound compound) { id = compound.getInteger("ID"); @@ -93,7 +94,7 @@ public class MagicCycle implements IMagicCycle { @Override public void setLayoutType(int layout) { - if(layout < 0 || layout > EnumDiagramLayout.values().length - 1) + if (layout < 0 || layout > EnumDiagramLayout.values().length - 1) return; this.layout = EnumDiagramLayout.values()[layout]; } diff --git a/src/main/java/noppes/npcs/util/Ease.java b/src/main/java/noppes/npcs/util/Ease.java new file mode 100644 index 000000000..3b1889993 --- /dev/null +++ b/src/main/java/noppes/npcs/util/Ease.java @@ -0,0 +1,61 @@ +package noppes.npcs.util; + +public enum Ease { + LINEAR(0, f -> f, "ease."), CONSTANT(1, f -> 0f, "ease."), + INSINE(2, Easing::inSine, "ease."), OUTSINE(3, Easing::outSine, "ease."), INOUTSINE(4, Easing::inOutSine, "ease."), + INCUBIC(5, Easing::inCubic, "ease."), OUTCUBIC(6, Easing::outCubic, "ease."), INOUTCUBIC(7, Easing::inOutCubic, "ease."), + INQUAD(8, Easing::inQuad, "ease."), OUTQUAD(9, Easing::outQuad, "ease."), INOUTQUAD(10, Easing::inOutQuad, "ease."), + INQUART(11, Easing::inQuart, "ease."), OUTQUART(12, Easing::outQuart, "ease."), INOUTQUART(13, Easing::inOutQuart, "ease."), + INQUINT(14, Easing::inQuint, "ease."), OUTQUINT(15, Easing::outQuint, "ease."), INOUTQUINT(16, Easing::inOutQuint, "ease."), + INEXPO(17, Easing::inExpo, "ease."), OUTEXPO(18, Easing::outExpo, "ease."), INOUTEXPO(19, Easing::inOutExpo, "ease."), + INCIRC(20, Easing::inCirc, "ease."), OUTCIRC(21, Easing::outCirc, "ease."), INOUTCIRC(22, Easing::inOutCirc, "ease."), + INBACK(23, Easing::inBack, "ease."), OUTBACK(24, Easing::outBack, "ease."), INOUTBACK(25, Easing::inOutBack, "ease."), + INELASTIC(26, Easing::inElastic, "ease."), OUTELASTIC(27, Easing::outElastic, "ease."), INOUTELASTIC(28, Easing::inOutElastic, "ease."), + INBOUNCE(29, Easing::inBounce, "ease."), OUTBOUNCE(30, Easing::outBack, "ease."), INOUTBOUNCE(31, Easing::inOutBounce, "ease."); + + final byte id; + private final EasingFunction impl; + public String lang; + + Ease(byte id, EasingFunction impl, String lang) { + this.id = id; + this.impl = impl; + } + + Ease(int id, EasingFunction impl, String lang) { + this((byte) id, impl, "ease."); + } + + /** + * Apply the easing + * + * @param f float between 0 and 1 + * @return ease(f) + */ + public float apply(float f) { + return impl.invoke(f); + } + + //To be able to send these as bytes instead of String names. + public static Ease getEase(byte b) { + for (Ease ease : Ease.values()) { + if (ease.id == b) return ease; + } + return LINEAR; + } + + public static Ease getFromOld(byte smoothing) { + switch (smoothing) { + case 1: + return LINEAR; + case 2: + return CONSTANT; + default: + return INSINE; + } + } + + private interface EasingFunction { + float invoke(float f); + } +} diff --git a/src/main/java/noppes/npcs/util/Easing.java b/src/main/java/noppes/npcs/util/Easing.java new file mode 100644 index 000000000..a48495ee8 --- /dev/null +++ b/src/main/java/noppes/npcs/util/Easing.java @@ -0,0 +1,168 @@ +package noppes.npcs.util; + +public class Easing { + + /** + * Easing functions from easings.net + * All function have a string codename + * EasingFromString + *

+ * All function needs an input between 0 and 1 + *

+ * Just use {@link Ease#apply(float)} + */ + public static float easingFromEnum(Ease type, float f) { + return type != null ? type.apply(f) : f; + } + + public static Ease easeFromString(String string) { + try { + if (string.equals("step")) + return Ease.CONSTANT; + if (string.substring(0, 4).equalsIgnoreCase("EASE")) { + string = string.substring(4); + } + return Ease.valueOf(string.toUpperCase()); + } catch (Exception exception) { + //Main.log(Level.ERROR, "Ease name unknown: \"" + string + "\" using linear", true); + //Main.log(Level.WARN, exception.toString()); + return Ease.LINEAR; + } + } + + private static final float c1 = 1.70158f; + private static final float c2 = c1 * 1.525f; + private static final float c3 = c1 + 1; + private static final float c4 = (float) ((2 * Math.PI) / 3); + private static final float c5 = (float) ((2 * Math.PI) / 4.5); + private static final float n1 = 7.5625f; + private static final float d1 = 2.75f; + + public static float inSine(float f) { + return (float) (1 - Math.cos((f * Math.PI) / 2)); + } + + public static float outSine(float f) { + return (float) (Math.sin((f * Math.PI) / 2)); + } + + public static float inOutSine(float f) { + return (float) (-(Math.cos(Math.PI * f) - 1) / 2); + } + + public static float inCubic(float f) { + return f * f * f; + } + + public static float outCubic(float f) { + return (float) (1 - Math.pow(1 - f, 3)); + } + + public static float inOutCubic(float x) { + return (float) ((x < 0.5) ? 4 * x * x * x : 1 - Math.pow(-2 * x + 2, 3) / 2); + } + + public static float inQuad(float x) { + return (x * x); + } + + public static float outQuad(float x) { + return (1 - (1 - x) * (1 - x)); + } + + public static float inOutQuad(float x) { + return (float) ((x < 0.5) ? 2 * x * x : 1 - Math.pow(-2 * x + 2, 2) / 2); + } + + public static float inQuart(float x) { + return (x * x * x * x); + } + + public static float outQuart(float x) { + return (float) (1 - Math.pow(1 - x, 4)); + } + + public static float inOutQuart(float x) { + return (float) (x < 0.5 ? 8 * x * x * x * x : 1 - Math.pow(-2 * x + 2, 4) / 2); + } + + public static float inQuint(float x) { + return (x * x * x * x * x); + } + + public static float outQuint(float x) { + return (float) (1 - Math.pow(1 - x, 5)); + } + + public static float inOutQuint(float x) { + return (float) (x < 0.5 ? 16 * x * x * x * x * x : 1 - Math.pow(-2 * x + 2, 5) / 2); + } + + public static float inExpo(float x) { + return (float) (x == 0 ? 0 : Math.pow(2, 10 * x - 10)); + } + + public static float outExpo(float x) { + return (float) (x == 1 ? 1 : 1 - Math.pow(2, -10 * x)); + } + + public static float inOutExpo(float x) { + return (float) (x == 0 ? 0 : x == 1 ? 1 : x < 0.5 ? Math.pow(2, 20 * x - 10) / 2 : (2 - Math.pow(2, -20 * x + 10)) / 2); + } + + public static float inCirc(float x) { + return (float) (1 - Math.sqrt(1 - Math.pow(x, 2))); + } + + public static float outCirc(float x) { + return (float) (Math.sqrt(1 - Math.pow(x - 1, 2))); + } + + public static float inOutCirc(float x) { + return (float) (x < 0.5 ? (1 - Math.sqrt(1 - Math.pow(2 * x, 2))) / 2 : (Math.sqrt(1 - Math.pow(-2 * x + 2, 2)) + 1) / 2); + } + + public static float inBack(float x) { + return c3 * x * x * x - c1 * x * x; + } + + public static float outBack(float x) { + return (float) (1 + c3 * Math.pow(x - 1, 3) + c1 * Math.pow(x - 1, 2)); + } + + public static float inOutBack(float x) { + return (float) (x < 0.5 ? (Math.pow(2 * x, 2) * ((c2 + 1) * 2 * x - c2)) / 2 : (Math.pow(2 * x - 2, 2) * ((c2 + 1) * (x * 2 - 2) + c2) + 2) / 2); + } + + public static float inElastic(float x) { + return (float) (x == 0 ? 0 : x == 1 ? 1 : -Math.pow(2, 10 * x - 10) * Math.sin((x * 10 - 10.75) * c4)); + } + + public static float outElastic(float x) { + return (float) (x == 0 ? 0 : x == 1 ? 1 : Math.pow(2, -10 * x) * Math.sin((x * 10 - 0.75) * c4) + 1); + } + + public static float inOutElastic(float x) { + return (float) (x == 0 ? 0 : x == 1 ? 1 : x < 0.5 ? -(Math.pow(2, 20 * x - 10) * Math.sin((20 * x - 11.125) * c5)) / 2 : (Math.pow(2, -20 * x + 10) * Math.sin((20 * x - 11.125) * c5)) / 2 + 1); + } + + public static float inBounce(float x) { + return 1 - outBounce(1 - x); + } + + public static float outBounce(float x) { + if (x < 1 / d1) { + return n1 * x * x; + } else if (x < 2 / d1) { + return (float) (n1 * (x -= 1.5 / d1) * x + 0.75); + } else if (x < 2.5 / d1) { + return (float) (n1 * (x -= 2.25 / d1) * x + 0.9375); + } else { + return (float) (n1 * (x -= 2.625 / d1) * x + 0.984375); + } + } + + public static float inOutBounce(float x) { + return x < 0.5 ? (1 - outBounce(1 - 2 * x)) / 2 : (1 + outBounce(2 * x - 1)) / 2; + } +} diff --git a/src/main/java/noppes/npcs/util/ValueUtil.java b/src/main/java/noppes/npcs/util/ValueUtil.java index 48cd0b1a9..2c6500b8f 100644 --- a/src/main/java/noppes/npcs/util/ValueUtil.java +++ b/src/main/java/noppes/npcs/util/ValueUtil.java @@ -1,6 +1,14 @@ package noppes.npcs.util; public class ValueUtil { + public static float lerp(float start, float end, float delta) { + return start + delta * (end - start); + } + + public static double lerp(double start, double end, double delta) { + return start + delta * (end - start); + } + public static float clamp(float given, float min, float max) { if (given < min) return min;