Skip to content

Commit

Permalink
Move mascot context menu to main
Browse files Browse the repository at this point in the history
    - Misc reorganization
    - Added image set settings to UI
  • Loading branch information
LavenderSnek committed Jul 21, 2024
1 parent 2ab4c50 commit cd49c63
Show file tree
Hide file tree
Showing 10 changed files with 218 additions and 104 deletions.
54 changes: 47 additions & 7 deletions src/main/java/com/group_finity/mascot/Mascot.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
import com.group_finity.mascot.imageset.ImageSetStore;
import com.group_finity.mascot.manager.MascotManager;
import com.group_finity.mascot.window.TranslucentWindow;
// todo: not ok
import com.group_finity.mascot.window.TranslucentWindowEvent;
import com.group_finity.mascot.window.TranslucentWindowEventHandler;
import com.group_finity.mascotapp.gui.debug.DebugWindow;
import com.group_finity.mascot.window.contextmenu.TopLevelMenuRep;

import javax.sound.sampled.Clip;
import java.awt.Point;
Expand Down Expand Up @@ -66,24 +66,63 @@ public class Mascot implements ScriptableMascot {

private final MascotPrefProvider prefProvider;
private final ImageSetStore imageSetStore;
private final MascotUiFactory uiFactory;

private record EventHandler(Mascot mascot) implements TranslucentWindowEventHandler {

@Override
public void onDragBegin(TranslucentWindowEvent event) {
if (mascot.getBehavior() != null) {
try {
mascot.getBehavior().mousePressed(event);
} catch (final CantBeAliveException e) {
mascot.dispose();
throw new RuntimeException(e);
}
}
}

@Override
public void onDragEnd(TranslucentWindowEvent event) {
if (mascot.getBehavior() != null) {
try {
mascot.getBehavior().mouseReleased(event);
} catch (final CantBeAliveException e) {
mascot.dispose();
throw new RuntimeException(e);
}
}
}

@Override
public TopLevelMenuRep getContextMenuRep() {
return mascot.uiFactory.createContextMenuFor(mascot);
}

public Mascot(String imageSet, MascotPrefProvider prefProvider, ImageSetStore imageSetStore) {
}

// might need to make this a builder

public Mascot(String imageSet, MascotPrefProvider prefProvider, ImageSetStore imageSetStore, MascotUiFactory uiFactory) {
this.uiFactory = uiFactory;
this.id = lastId.incrementAndGet();
this.imageSet = imageSet;

this.prefProvider = prefProvider;
this.imageSetStore = imageSetStore;

MascotEventHandler eventHandler = new MascotEventHandler(this);
EventHandler eventHandler = new EventHandler(this);
getWindow().setEventHandler(eventHandler);

log.log(Level.INFO, "Created a mascot ({0})", this);
}

public void startDebugUi() {
if (debugUi == null) {
// todo: maybe make this a factory
debugUi = new DebugWindow();
debugUi = uiFactory.createDebugUiFor(this);
}
if (debugUi == null) {
return;
}
// slightly messy
debugUi.setAfterDisposeAction(() -> debugUi = null);
Expand Down Expand Up @@ -233,7 +272,7 @@ public Behavior getBehavior() {
// not part of the API, please don't call/rely on these from scripts, (maybe these should be mangled?)

public static Mascot createBlankFrom(Mascot mascot) {
return new Mascot(mascot.imageSet, mascot.prefProvider, mascot.imageSetStore);
return new Mascot(mascot.imageSet, mascot.prefProvider, mascot.imageSetStore, mascot.uiFactory);
}

public double getScaling() {
Expand All @@ -253,4 +292,5 @@ public ImageSet getImageSetDependency(String name) {
public boolean isTransientBreedingAllowed() { return prefProvider.isTransientBreedingAllowed(getImageSet()); }
public boolean isTransformationAllowed() { return prefProvider.isTransformationAllowed(getImageSet()); }
public boolean isSoundAllowed() { return prefProvider.isSoundAllowed(getImageSet()); }

}
52 changes: 0 additions & 52 deletions src/main/java/com/group_finity/mascot/MascotEventHandler.java

This file was deleted.

11 changes: 11 additions & 0 deletions src/main/java/com/group_finity/mascot/MascotUiFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.group_finity.mascot;

import com.group_finity.mascot.window.contextmenu.TopLevelMenuRep;

public interface MascotUiFactory {

DebugUi createDebugUiFor(Mascot mascot);

TopLevelMenuRep createContextMenuFor(Mascot mascot);

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
package com.group_finity.mascotapp.imageset;

import com.group_finity.mascot.imageset.ImageSet;
package com.group_finity.mascot.imageset;

public interface ImageSetLoadingDelegate {
ImageSet load(String name);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package com.group_finity.mascotapp.imageset;

import com.group_finity.mascot.imageset.ImageSet;
import com.group_finity.mascot.imageset.ImageSetStore;

import java.util.*;
package com.group_finity.mascot.imageset;

import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Random;
import java.util.Set;

public class ImageSetManager implements ImageSetStore {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
package com.group_finity.mascotapp.imageset;

import com.group_finity.mascot.imageset.ImageSet;
package com.group_finity.mascot.imageset;

public interface ImageSetSelectionDelegate {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@ public void disposeIf(Predicate<Mascot> predicate) {
queueTask(() -> mascots.values().stream().filter(predicate).forEach(Mascot::dispose));
}

public void reduceToOne() {
mascots.values().stream().findAny().ifPresent(target -> {
disposeIf(m -> m.id != target.id);
});
}

public void trySetBehaviorAll(String name) {
queueTask(() -> mascots.values().forEach(m -> {
try {
Expand Down
Loading

0 comments on commit cd49c63

Please sign in to comment.