refactor options
This commit is contained in:
102
src/yugecin/opsudance/skinning/SkinService.java
Normal file
102
src/yugecin/opsudance/skinning/SkinService.java
Normal file
@@ -0,0 +1,102 @@
|
||||
/*
|
||||
* opsu!dance - fork of opsu! with cursordance auto
|
||||
* Copyright (C) 2017 yugecin
|
||||
*
|
||||
* opsu!dance is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* opsu!dance is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with opsu!dance. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package yugecin.opsudance.skinning;
|
||||
|
||||
import itdelatrisu.opsu.audio.SoundController;
|
||||
import itdelatrisu.opsu.skins.Skin;
|
||||
import itdelatrisu.opsu.skins.SkinLoader;
|
||||
import org.newdawn.slick.util.ClasspathLocation;
|
||||
import org.newdawn.slick.util.FileSystemLocation;
|
||||
import org.newdawn.slick.util.ResourceLoader;
|
||||
import yugecin.opsudance.core.events.EventBus;
|
||||
import yugecin.opsudance.core.inject.Inject;
|
||||
import yugecin.opsudance.events.ResolutionOrSkinChangedEvent;
|
||||
import yugecin.opsudance.options.Configuration;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
/**
|
||||
* @author itdelatrisu (https://github.com/itdelatrisu) most functions are copied from itdelatrisu.opsu.Options.java
|
||||
*/
|
||||
public class SkinService {
|
||||
|
||||
@Inject
|
||||
private Configuration config;
|
||||
|
||||
public String[] availableSkinDirectories;
|
||||
public String usedSkinName = "Default";
|
||||
public static Skin skin;
|
||||
|
||||
@Inject
|
||||
public SkinService() {
|
||||
}
|
||||
|
||||
public void reloadSkin() {
|
||||
loadSkin();
|
||||
SoundController.init();
|
||||
EventBus.post(new ResolutionOrSkinChangedEvent(usedSkinName, -1, -1));
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads the skin given by the current skin directory.
|
||||
* If the directory is invalid, the default skin will be loaded.
|
||||
*/
|
||||
public void loadSkin() {
|
||||
File skinDir = getCurrentSkinDirectory();
|
||||
if (skinDir == null) {
|
||||
// invalid skin name
|
||||
usedSkinName = Skin.DEFAULT_SKIN_NAME;
|
||||
}
|
||||
|
||||
// create available skins list
|
||||
File[] dirs = SkinLoader.getSkinDirectories(config.skinRootDir);
|
||||
availableSkinDirectories = new String[dirs.length + 1];
|
||||
availableSkinDirectories[0] = Skin.DEFAULT_SKIN_NAME;
|
||||
for (int i = 0; i < dirs.length; i++) {
|
||||
availableSkinDirectories[i + 1] = dirs[i].getName();
|
||||
}
|
||||
|
||||
// set skin and modify resource locations
|
||||
ResourceLoader.removeAllResourceLocations();
|
||||
if (skinDir == null) {
|
||||
skin = new Skin(null);
|
||||
} else {
|
||||
// load the skin
|
||||
skin = SkinLoader.loadSkin(skinDir);
|
||||
ResourceLoader.addResourceLocation(new FileSystemLocation(skinDir));
|
||||
}
|
||||
ResourceLoader.addResourceLocation(new ClasspathLocation());
|
||||
ResourceLoader.addResourceLocation(new FileSystemLocation(new File(".")));
|
||||
ResourceLoader.addResourceLocation(new FileSystemLocation(new File("./res/")));
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the current skin directory.
|
||||
* <p>
|
||||
* NOTE: This directory will differ from that of the currently loaded skin
|
||||
* if {@link #loadSkin()} has not been called after a directory change.
|
||||
* Use {@link Skin#getDirectory()} to get the directory of the currently
|
||||
* loaded skin.
|
||||
* @return the skin directory, or null for the default skin
|
||||
*/
|
||||
public File getCurrentSkinDirectory() {
|
||||
File dir = new File(config.skinRootDir, usedSkinName);
|
||||
return (dir.isDirectory()) ? dir : null;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user