Skip to content

Commit

Permalink
apply animate module to minecraft-gui-button and minecraft-gui-mask
Browse files Browse the repository at this point in the history
  • Loading branch information
HSGamer committed Jun 20, 2024
1 parent 5553743 commit c7ab62e
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 112 deletions.
5 changes: 5 additions & 0 deletions minecraft/minecraft-gui-button/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,10 @@
<artifactId>hscore-minecraft-gui</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>me.hsgamer</groupId>
<artifactId>hscore-animate</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package me.hsgamer.hscore.minecraft.gui.button.impl;

import me.hsgamer.hscore.animate.Animation;
import me.hsgamer.hscore.minecraft.gui.GUIProperties;
import me.hsgamer.hscore.minecraft.gui.button.Button;
import me.hsgamer.hscore.minecraft.gui.button.DisplayButton;
import me.hsgamer.hscore.ui.property.IdentifiedUpdatable;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;

Expand All @@ -13,10 +13,9 @@
/**
* The animated button with child buttons as frames
*/
public class AnimatedButton implements Button, IdentifiedUpdatable {
public class AnimatedButton implements Button {
private final List<Button> buttons = new ArrayList<>();
private final Map<UUID, Integer> currentIndexMap = new ConcurrentHashMap<>();
private final Map<UUID, Long> lastUpdateMap = new ConcurrentHashMap<>();
private final Map<UUID, Animation<Button>> animationMap = new ConcurrentHashMap<>();
private long periodMillis = 50L;

/**
Expand Down Expand Up @@ -82,14 +81,13 @@ public List<Button> getButtons() {
return buttons;
}

private int getCurrentIndex(UUID uuid) {
return currentIndexMap.getOrDefault(uuid, 0);
private Animation<Button> getAnimation(UUID uuid) {
return animationMap.computeIfAbsent(uuid, key -> new Animation<>(buttons, periodMillis));
}

@Override
public DisplayButton display(@NotNull UUID uuid) {
update(uuid);
return buttons.get(getCurrentIndex(uuid)).display(uuid);
return getAnimation(uuid).getCurrentFrame().display(uuid);
}

@Override
Expand All @@ -104,17 +102,4 @@ public void init() {
public void stop() {
this.buttons.forEach(Button::stop);
}

@Override
public void update(@NotNull UUID uuid) {
long currentTimeMillis = System.currentTimeMillis();
long lastUpdate = lastUpdateMap.computeIfAbsent(uuid, k -> currentTimeMillis);
if (currentTimeMillis - lastUpdate < periodMillis) return;
long diff = currentTimeMillis - lastUpdate;
long remainder = diff % periodMillis;
int skip = (int) (diff / periodMillis);
int currentIndex = getCurrentIndex(uuid);
currentIndexMap.put(uuid, (currentIndex + skip) % buttons.size());
lastUpdateMap.put(uuid, currentTimeMillis - remainder);
}
}
5 changes: 5 additions & 0 deletions minecraft/minecraft-gui-mask/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,10 @@
<artifactId>hscore-minecraft-gui</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>me.hsgamer</groupId>
<artifactId>hscore-animate</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package me.hsgamer.hscore.minecraft.gui.mask.impl;

import me.hsgamer.hscore.animate.Animation;
import me.hsgamer.hscore.minecraft.gui.GUIProperties;
import me.hsgamer.hscore.minecraft.gui.button.Button;
import me.hsgamer.hscore.minecraft.gui.mask.BaseMask;
import me.hsgamer.hscore.minecraft.gui.mask.Mask;
import me.hsgamer.hscore.minecraft.gui.object.InventorySize;
import me.hsgamer.hscore.ui.property.IdentifiedUpdatable;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;

Expand All @@ -15,10 +15,9 @@
/**
* The animated mask with child masks as frames
*/
public class AnimatedMask extends BaseMask implements IdentifiedUpdatable {
public class AnimatedMask extends BaseMask {
private final List<Mask> masks = new ArrayList<>();
private final Map<UUID, Integer> currentIndexMap = new ConcurrentHashMap<>();
private final Map<UUID, Long> lastUpdateMap = new ConcurrentHashMap<>();
private final Map<UUID, Animation<Mask>> animationMap = new ConcurrentHashMap<>();
private long periodMillis = 50;

/**
Expand Down Expand Up @@ -94,14 +93,13 @@ public List<Mask> getMasks() {
return masks;
}

private int getCurrentIndex(@NotNull UUID uuid) {
return currentIndexMap.getOrDefault(uuid, 0);
private Animation<Mask> getAnimation(@NotNull UUID uuid) {
return animationMap.computeIfAbsent(uuid, k -> new Animation<>(masks, periodMillis));
}

@Override
public Optional<Map<Integer, Button>> generateButtons(@NotNull UUID uuid, @NotNull InventorySize inventorySize) {
update(uuid);
return masks.get(getCurrentIndex(uuid)).generateButtons(uuid, inventorySize);
return getAnimation(uuid).getCurrentFrame().generateButtons(uuid, inventorySize);
}

@Override
Expand All @@ -116,17 +114,4 @@ public void init() {
public void stop() {
this.masks.forEach(Mask::stop);
}

@Override
public void update(@NotNull UUID uuid) {
long currentTimeMillis = System.currentTimeMillis();
long lastUpdate = lastUpdateMap.computeIfAbsent(uuid, k -> currentTimeMillis);
if (currentTimeMillis - lastUpdate < periodMillis) return;
long diff = currentTimeMillis - lastUpdate;
long remainder = diff % periodMillis;
int skip = (int) (diff / periodMillis);
int currentIndex = getCurrentIndex(uuid);
currentIndexMap.put(uuid, (currentIndex + skip) % masks.size());
lastUpdateMap.put(uuid, currentTimeMillis - remainder);
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package me.hsgamer.hscore.minecraft.gui.mask.impl;

import me.hsgamer.hscore.animate.Animation;
import me.hsgamer.hscore.minecraft.gui.GUIProperties;
import me.hsgamer.hscore.minecraft.gui.button.Button;
import me.hsgamer.hscore.minecraft.gui.mask.BaseMask;
import me.hsgamer.hscore.minecraft.gui.mask.Mask;
import me.hsgamer.hscore.minecraft.gui.object.InventorySize;
import me.hsgamer.hscore.ui.property.IdentifiedUpdatable;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;

Expand All @@ -15,9 +15,9 @@
/**
* The animated mask with child masks as frames, but only run once
*/
public class OneTimeAnimatedMask extends BaseMask implements IdentifiedUpdatable {
public class OneTimeAnimatedMask extends BaseMask {
private final List<Mask> masks = new ArrayList<>();
private final Map<UUID, SequenceRunner> runnerMap = new ConcurrentHashMap<>();
private final Map<UUID, Animation<Mask>> animationMap = new ConcurrentHashMap<>();
private boolean viewLast = false;
private long periodMillis = 50L;

Expand Down Expand Up @@ -111,29 +111,24 @@ public List<Mask> getMasks() {
* @param uuid the unique id
*/
public void reset(@NotNull UUID uuid) {
getRunner(uuid).reset();
getAnimation(uuid).reset();
}

private SequenceRunner getRunner(@NotNull UUID uuid) {
return runnerMap.computeIfAbsent(uuid, k -> new SequenceRunner());
private Animation<Mask> getAnimation(@NotNull UUID uuid) {
return animationMap.computeIfAbsent(uuid, key -> new Animation<>(masks, periodMillis));
}

@Override
public Optional<Map<@NotNull Integer, @NotNull Button>> generateButtons(@NotNull UUID uuid, @NotNull InventorySize inventorySize) {
update(uuid);
SequenceRunner runner = getRunner(uuid);
if (runner.maxed && !viewLast) {
Animation<Mask> animation = getAnimation(uuid);
long currentMillis = System.currentTimeMillis();
if (animation.isFirstRun()) {
return animation.getCurrentFrame(currentMillis).generateButtons(uuid, inventorySize);
} else if (viewLast) {
return masks.get(masks.size() - 1).generateButtons(uuid, inventorySize);
} else {
return Optional.empty();
}
return masks.get(runner.index).generateButtons(uuid, inventorySize);
}

@Override
public void update(@NotNull UUID uuid) {
SequenceRunner runner = getRunner(uuid);
if (!runner.maxed) {
runner.updateIndex();
}
}

@Override
Expand All @@ -146,40 +141,8 @@ public void init() {

@Override
public void stop() {
runnerMap.clear();
animationMap.clear();
masks.forEach(Mask::stop);
masks.clear();
}

private class SequenceRunner {
private int index = 0;
private long lastTickMillis = System.currentTimeMillis();
private boolean maxed = false;

private void updateIndex() {
long currentTick = System.currentTimeMillis();
long diff = currentTick - lastTickMillis;
if (diff < periodMillis) {
return;
}

int passed = (int) (diff / periodMillis);
long remainder = diff % periodMillis;
lastTickMillis = currentTick - remainder;

int max = masks.size();
if (index + passed >= max) {
index = max - 1;
maxed = true;
} else {
index += passed;
}
}

private void reset() {
index = 0;
lastTickMillis = System.currentTimeMillis();
maxed = false;
}
}
}

This file was deleted.

0 comments on commit c7ab62e

Please sign in to comment.