Skip to content

Commit

Permalink
Reorganize UI out of mascot
Browse files Browse the repository at this point in the history
    - Mascot importing debug ui needs to be fixed
LavenderSnek committed Jul 21, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent 3f6e1af commit 33f530c
Showing 24 changed files with 142 additions and 149 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
package com.group_finity.mascot.ui.debug;

import com.group_finity.mascot.Mascot;
package com.group_finity.mascot;

public interface DebugUi {

5 changes: 3 additions & 2 deletions src/main/java/com/group_finity/mascot/Mascot.java
Original file line number Diff line number Diff line change
@@ -7,9 +7,9 @@
import com.group_finity.mascot.image.MascotImage;
import com.group_finity.mascot.imageset.ImageSet;
import com.group_finity.mascot.imageset.ImageSetStore;
import com.group_finity.mascot.ui.debug.DebugUi;
import com.group_finity.mascot.ui.debug.DebugWindow;
import com.group_finity.mascot.window.TranslucentWindow;
// todo: not ok
import com.group_finity.mascotapp.gui.debug.DebugWindow;

import javax.sound.sampled.Clip;
import java.awt.Point;
@@ -80,6 +80,7 @@ public Mascot(final String imageSet, MascotPrefProvider prefProvider, ImageSetSt

void startDebugUi() {
if (debugUi == null) {
// todo: maybe make this a factory
debugUi = new DebugWindow();
}
// slightly messy
6 changes: 3 additions & 3 deletions src/main/java/com/group_finity/mascot/MascotEventHandler.java
Original file line number Diff line number Diff line change
@@ -2,9 +2,9 @@

import com.group_finity.mascot.behavior.Behavior;
import com.group_finity.mascot.exception.CantBeAliveException;
import com.group_finity.mascot.ui.contextmenu.MenuItemRep;
import com.group_finity.mascot.ui.contextmenu.MenuRep;
import com.group_finity.mascot.ui.contextmenu.TopLevelMenuRep;
import com.group_finity.mascot.window.contextmenu.MenuItemRep;
import com.group_finity.mascot.window.contextmenu.MenuRep;
import com.group_finity.mascot.window.contextmenu.TopLevelMenuRep;
import com.group_finity.mascot.window.TranslucentWindowEvent;
import com.group_finity.mascot.window.TranslucentWindowEventHandler;

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.group_finity.mascot.window;

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

public interface TranslucentWindowEventHandler {

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.group_finity.mascot.ui.contextmenu;
package com.group_finity.mascot.window.contextmenu;

public class MenuItemRep {

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.group_finity.mascot.ui.contextmenu;
package com.group_finity.mascot.window.contextmenu;

public class MenuRep extends MenuItemRep {

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.group_finity.mascot.ui.contextmenu;
package com.group_finity.mascot.window.contextmenu;

public class TopLevelMenuRep extends MenuRep {

5 changes: 4 additions & 1 deletion src/main/java/com/group_finity/mascotapp/Manager.java
Original file line number Diff line number Diff line change
@@ -12,6 +12,9 @@

public class Manager implements MascotManager {

// this could technically go in mascot since it does draw the mascots (maybe as DefaultManager?)
// todo: think abt it later

public static final int TICK_INTERVAL_MILLIS = 40;

private final ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
@@ -53,7 +56,7 @@ private void runPendingTasks() {
}

// basically just that linkedHashSet thing from the original Manager
void queueTask(Runnable r) {
public void queueTask(Runnable r) {
tasks.add(r);
}

7 changes: 2 additions & 5 deletions src/main/java/com/group_finity/mascotapp/ShimejiEE.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
package com.group_finity.mascotapp;

import com.group_finity.mascotapp.runners.MinimalRunner;
import picocli.CommandLine;
import picocli.CommandLine.Command;

import javax.swing.*;

import static picocli.CommandLine.*;

@Command(name = "shimeji", version = "2.1.0", mixinStandardHelpOptions = true,
description = "%nShimejiEE desktop pets%nrun jar with no args for default configuration%n"
)
public class ShimejiEE {

public static void main(String[] args) {
var ec = new CommandLine(new ShimejiEE())
.addSubcommand("run", new ShimejiRun())
.addSubcommand("run", new MinimalRunner())
.execute("run");
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.group_finity.mascot.ui;
package com.group_finity.mascotapp.gui;

import java.awt.Color;

Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.group_finity.mascot.ui.imagesets;
package com.group_finity.mascotapp.gui.chooser;

import com.group_finity.mascot.Tr;
import com.group_finity.mascot.imageset.ShimejiProgramFolder;
import com.group_finity.mascot.ui.Theme;
import com.group_finity.mascotapp.gui.Theme;

import javax.swing.BorderFactory;
import javax.swing.DefaultListModel;
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.group_finity.mascot.ui.imagesets;
package com.group_finity.mascotapp.gui.chooser;

import com.group_finity.mascot.ui.Theme;
import com.group_finity.mascotapp.gui.Theme;

import javax.swing.BorderFactory;
import javax.swing.DefaultListModel;
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.group_finity.mascot.ui.imagesets;
package com.group_finity.mascotapp.gui.chooser;

import javax.imageio.ImageIO;
import javax.swing.Box;
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.group_finity.mascot.ui.imagesets;
package com.group_finity.mascotapp.gui.chooser;

import com.group_finity.mascot.imageset.ShimejiProgramFolder;

Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.group_finity.mascot.ui.debug;
package com.group_finity.mascotapp.gui.debug;

import com.group_finity.mascot.DebugUi;
import com.group_finity.mascot.Mascot;
import com.group_finity.mascot.Tr;
import com.group_finity.mascot.environment.Area;
Original file line number Diff line number Diff line change
@@ -28,7 +28,7 @@ public void setSelected(Collection<String> newSelected) {
// get list copy first so we're not deleting while iterating
getLoaded().stream()
.filter(s -> !newSelected.contains(s))
.forEach(this::removeSelected);
.forEach(this::removeImageSet);

for (String s : newSelected) {
if (!loaded.containsKey(s)) {
@@ -43,8 +43,11 @@ public void setSelected(Collection<String> newSelected) {
}
}

private void removeSelected(String name) {
private void removeImageSet(String name) {
var ims = loaded.get(name);
if (ims == null) {
return;
}
selectionDelegate.imageSetWillBeRemoved(name, ims);
selected.remove(name);
loaded.remove(name);
@@ -93,4 +96,11 @@ public Collection<String> getSelected() {
public Collection<String> getLoaded() {
return loaded.keySet().stream().toList();
}

public String getRandomSelection() {
return selected.stream()
.skip(new Random().nextInt(selected.size()))
.findFirst()
.orElse(null);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.group_finity.mascotapp;
package com.group_finity.mascotapp.runners;

import com.group_finity.mascot.Mascot;
import com.group_finity.mascot.NativeFactory;
@@ -12,23 +12,22 @@
import com.group_finity.mascot.imageset.ShimejiImageSet;
import com.group_finity.mascot.imageset.ShimejiProgramFolder;
import com.group_finity.mascot.sound.SoundLoader;
import com.group_finity.mascotapp.Constants;
import com.group_finity.mascotapp.Manager;
import com.group_finity.mascotapp.imageset.ImageSetLoadingDelegate;
import com.group_finity.mascotapp.imageset.ImageSetManager;
import com.group_finity.mascotapp.imageset.ImageSetSelectionDelegate;
import com.group_finity.mascotapp.options.*;
import com.group_finity.mascotapp.options.ImageSetOptions;
import com.group_finity.mascotapp.options.LaunchAppOptions;
import com.group_finity.mascotapp.options.PersistentAppOptions;
import com.group_finity.mascotapp.options.ProgramFolderOptions;

import javax.xml.parsers.DocumentBuilderFactory;
import java.awt.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.awt.Point;
import java.nio.file.Path;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.logging.LogManager;

import static java.util.Map.*;
import static picocli.CommandLine.*;

@Command(
@@ -39,7 +38,7 @@
sortOptions = false,
sortSynopsis = false
)
public class ShimejiRun implements Callable<Integer>, ImageSetLoadingDelegate, ImageSetSelectionDelegate {
public class MinimalRunner implements Runnable, ImageSetLoadingDelegate, ImageSetSelectionDelegate {

// saved
@ArgGroup(validate = false, heading = "%nGeneral:%n") PersistentAppOptions genOpts = new PersistentAppOptions();
@@ -49,17 +48,13 @@ public class ShimejiRun implements Callable<Integer>, ImageSetLoadingDelegate, I
@ArgGroup(validate = false, heading = "%nProgram Folder:%n") ProgramFolderOptions pfOpts = new ProgramFolderOptions();
@ArgGroup(validate = false, heading = "%nLaunch:%n") LaunchAppOptions launchOpts = new LaunchAppOptions();

private Manager manager = new Manager();
private ImageSetManager imageSets = new ImageSetManager(this, this);
private Manager manager;
private ImageSetManager imageSets;

private ShimejiProgramFolder programFolder = null;

@Override
public ImageSet load(String name) {
if (programFolder == null) {
programFolder = pfOpts.toProgramFolder(Constants.JAR_DIR);
}

try {
return loadImageSet(programFolder, name, imgOpts);
} catch (Exception e) {
@@ -88,7 +83,16 @@ public void imageSetHasBeenRemoved(String name, ImageSet imageSet) {

@Override
public void imageSetHasBeenAdded(String name, ImageSet imageSet) {
var m = new Mascot(name, imgOpts, imageSets);
spawnMascot(name);
}

private void spawnMascot(String imageSetName) {
if (imageSets.get(imageSetName) == null) {
System.err.println("Invalid spawn, image set not loaded: " + imageSetName);
return;
}

var m = new Mascot(imageSetName, imgOpts, imageSets);

m.setAnchor(new Point(-4000, -4000));
try {
@@ -101,45 +105,34 @@ public void imageSetHasBeenAdded(String name, ImageSet imageSet) {
}
}

@Override
public Integer call() throws Exception {
// temp until i fix the logging
LogManager.getLogManager().reset();
public void init() {
// setup object w settings
programFolder = pfOpts.toProgramFolder(Constants.JAR_DIR);
imageSets = new ImageSetManager(this, this);
manager = new Manager();

// setup native
NativeFactory.init(launchOpts.nativePkg, Constants.NATIVE_LIB_DIR);
NativeFactory.getInstance().getEnvironment().init();
}

// selection
imageSets.setSelected(List.of("dark-shime", "Shimeji"));

// start manager
var mf = manager.start();
@Override
public void run() {
// temp until i fix the logging
LogManager.getLogManager().reset();

// start "UI" can be swing and/or console
var t = new Thread(() -> {
// can't believe im actually using BufferedReader irl
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
try {
System.out.println("ShimejiEE");
String str;
while (true) {
System.out.print(">>> ");
str = reader.readLine();
if (str == null) {
break;
}
System.out.println(str);
}
} catch (IOException e) {
throw new RuntimeException(e);
}
});
t.start();
// setup
init();

// selection
imageSets.setSelected(genOpts.imageSetSelections);

mf.get();
return 0;
// start manager
try {
manager.start().get();
} catch (InterruptedException | ExecutionException e) {
throw new RuntimeException(e);
}
}

private static ImageSet loadImageSet(ShimejiProgramFolder pf, String name, ImageSetOptions options) throws Exception {
@@ -169,5 +162,4 @@ private static ImageSet loadImageSet(ShimejiProgramFolder pf, String name, Image
return new ShimejiImageSet(config, imgStore, soundStore);
}


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

import com.group_finity.mascot.Mascot;
import com.group_finity.mascot.MascotPrefProvider;
@@ -14,7 +14,7 @@
import com.group_finity.mascot.imageset.ShimejiProgramFolder;
import com.group_finity.mascot.imageset.ShimejiImageSet;
import com.group_finity.mascot.sound.SoundLoader;
import com.group_finity.mascot.ui.imagesets.ImageSetChooserUtils;
import com.group_finity.mascotapp.gui.chooser.ImageSetChooserUtils;
import com.group_finity.mascotapp.Constants;
import org.xml.sax.SAXException;

@@ -70,7 +70,7 @@
*
* @author see readme
*/
public final class Main implements MascotPrefProvider {
public final class Main implements Runnable, MascotPrefProvider {
private static final Logger log = Logger.getLogger(Main.class.getName());

// Action that matches the followCursor action
@@ -80,32 +80,26 @@ public final class Main implements MascotPrefProvider {

private static final String USER_BEHAVIORNAMES_FILE = "user-behaviornames.properties";

public static final Path JAR_PARENT_DIR;
private static final Path SETTINGS_PATH;

static {

System.setProperty("java.util.PropertyResourceBundle.encoding", "UTF-8");

Path folder = null;
try {
folder = Path.of(Main.class.getProtectionDomain().getCodeSource().getLocation().toURI()).getParent();
} catch (Exception e) {
showError("Unable to find path to jar");
System.exit(1);
}
JAR_PARENT_DIR = folder;

try (var ins = new FileInputStream(JAR_PARENT_DIR.resolve(Path.of("conf","logging.properties")).toFile())) {
final var logPropsPath = Constants.JAR_DIR.resolve(Path.of("conf","logging.properties"));
try (var ins = new FileInputStream(logPropsPath.toFile())) {
LogManager.getLogManager().readConfiguration(ins);
} catch (final SecurityException | IOException e) {
e.printStackTrace();
}

String settingsPathProp = System.getProperty(SP_PREFIX + "SettingsPath");
SETTINGS_PATH = settingsPathProp != null
? Path.of(settingsPathProp)
: Constants.JAR_DIR.resolve(Path.of("conf","settings.properties"));
}

//--------//

private ShimejiProgramFolder programFolder = ShimejiProgramFolder.fromFolder(JAR_PARENT_DIR);
private ShimejiProgramFolder programFolder = ShimejiProgramFolder.fromFolder(Constants.JAR_DIR);

private final ConcurrentMap<String, ImageSet> loadedImageSets = new ConcurrentHashMap<>();
private final List<String> activeImageSets = new ArrayList<>();
@@ -214,59 +208,56 @@ private void setScaling(double scaling) {

//-------------------------------------//

/**
* Program entry point
*/
public static void main(final String[] args) {
getInstance().run();
}

@Override
public void run() {
try {
getInstance().run();
// init settings
loadAllSettings(SETTINGS_PATH);
Tr.loadLanguage(locale);
Tr.setCustomBehaviorTranslations(loadProperties(
Constants.JAR_DIR.resolve(Path.of("conf", USER_BEHAVIORNAMES_FILE))
));
Runtime.getRuntime().addShutdownHook(new Thread(() -> writeAllSettings(SETTINGS_PATH)));

// tray icon (optional)
createTrayIcon();

// init native
final String nativeProp = System.getProperty("com.group_finity.mascotnative", Constants.NATIVE_PKG_DEFAULT);
NativeFactory.init(nativeProp, Constants.NATIVE_LIB_DIR);
NativeFactory.getInstance().getEnvironment().init();

// set exit to false because the chooser is async
boolean isExit = manager.isExitOnLastRemoved();
manager.setExitOnLastRemoved(false);

Set<String> selections = getActiveImageSets();

// get selections (show chooser if needed)
if (selections.isEmpty() || shouldShowChooserAtStart()) {
ImageSetChooserUtils.askUserForSelection(c -> {
setActiveImageSets(c);
manager.setExitOnLastRemoved(isExit);
}, getActiveImageSets(), programFolder);
} else {
selections.forEach(this::addActiveImageSet);
manager.setExitOnLastRemoved(isExit);
}

// start
manager.start();

} catch (Exception | Error error) {
log.log(Level.SEVERE, error.getMessage(), error);
Main.showError(error.getMessage());
System.exit(0);
}
}

private void run() {
String settingsPathProp = System.getProperty(SP_PREFIX + "SettingsPath");
final Path SETTINGS_PATH = settingsPathProp != null
? Path.of(settingsPathProp)
: JAR_PARENT_DIR.resolve(Path.of("conf","settings.properties"));

loadAllSettings(SETTINGS_PATH);
Tr.loadLanguage(locale);
Tr.setCustomBehaviorTranslations(loadProperties(
JAR_PARENT_DIR.resolve(Path.of("conf", USER_BEHAVIORNAMES_FILE))
));

// optional
createTrayIcon();

// init environment
NativeFactory.init(Constants.NATIVE_PKG_DEFAULT, Constants.NATIVE_LIB_DIR);
NativeFactory.getInstance().getEnvironment().init();

//because the chooser is async
boolean isExit = manager.isExitOnLastRemoved();
manager.setExitOnLastRemoved(false);

Set<String> selections = getActiveImageSets();

if (selections.isEmpty() || shouldShowChooserAtStart()) {
ImageSetChooserUtils.askUserForSelection(c -> {
setActiveImageSets(c);
manager.setExitOnLastRemoved(isExit);
}, getActiveImageSets(), programFolder);
} else {
selections.forEach(this::addActiveImageSet);
manager.setExitOnLastRemoved(isExit);
}

manager.start();

Runtime.getRuntime().addShutdownHook(new Thread(() -> writeAllSettings(SETTINGS_PATH)));
}

private static final JFrame frame = new JFrame();

static void showError(String message) {
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.group_finity.mascotapp.old;
package com.group_finity.mascotapp.runners.old;

import com.group_finity.mascot.Mascot;
import com.group_finity.mascot.MascotManager;
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.group_finity.mascotnative.macjni;

import com.group_finity.mascot.image.NativeImage;
import com.group_finity.mascot.ui.contextmenu.TopLevelMenuRep;
import com.group_finity.mascot.window.contextmenu.TopLevelMenuRep;
import com.group_finity.mascot.window.TranslucentWindow;
import com.group_finity.mascot.window.TranslucentWindowEvent;
import com.group_finity.mascot.window.TranslucentWindowEventHandler;
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.group_finity.mascotnative.macjni.menu;

import com.group_finity.mascot.ui.contextmenu.MenuItemRep;
import com.group_finity.mascot.ui.contextmenu.MenuRep;
import com.group_finity.mascot.ui.contextmenu.TopLevelMenuRep;
import com.group_finity.mascot.window.contextmenu.MenuItemRep;
import com.group_finity.mascot.window.contextmenu.MenuRep;
import com.group_finity.mascot.window.contextmenu.TopLevelMenuRep;

public class MacJniPopupUtil {

Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.group_finity.mascotnative.shared;

import com.group_finity.mascot.image.NativeImage;
import com.group_finity.mascot.ui.contextmenu.TopLevelMenuRep;
import com.group_finity.mascot.window.contextmenu.TopLevelMenuRep;
import com.group_finity.mascot.window.TranslucentWindow;
import com.group_finity.mascot.window.TranslucentWindowEvent;
import com.group_finity.mascot.window.TranslucentWindowEventHandler;
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.group_finity.mascotnative.shared;

import com.group_finity.mascot.ui.contextmenu.MenuItemRep;
import com.group_finity.mascot.ui.contextmenu.MenuRep;
import com.group_finity.mascot.ui.contextmenu.TopLevelMenuRep;
import com.group_finity.mascot.window.contextmenu.MenuItemRep;
import com.group_finity.mascot.window.contextmenu.MenuRep;
import com.group_finity.mascot.window.contextmenu.TopLevelMenuRep;

import javax.swing.JMenu;
import javax.swing.JMenuItem;
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.group_finity.mascotnative.virtualdesktop;

import com.group_finity.mascot.image.NativeImage;
import com.group_finity.mascot.ui.contextmenu.TopLevelMenuRep;
import com.group_finity.mascot.window.contextmenu.TopLevelMenuRep;
import com.group_finity.mascot.window.TranslucentWindow;
import com.group_finity.mascot.window.TranslucentWindowEvent;
import com.group_finity.mascot.window.TranslucentWindowEventHandler;

0 comments on commit 33f530c

Please sign in to comment.