67 lines
2.2 KiB
Java
Executable File
67 lines
2.2 KiB
Java
Executable File
package zero1hd.polyjet.util;
|
|
|
|
import com.badlogic.gdx.Gdx;
|
|
import com.badlogic.gdx.assets.loaders.FileHandleResolver;
|
|
import com.badlogic.gdx.assets.loaders.resolvers.ResolutionFileResolver.Resolution;
|
|
import com.badlogic.gdx.files.FileHandle;
|
|
|
|
public class RoundingResolutionHandler implements FileHandleResolver {
|
|
private final Resolution[] descriptors;
|
|
private final FileHandleResolver resolver;
|
|
|
|
public RoundingResolutionHandler(FileHandleResolver fileResolver, Resolution... descriptors) {
|
|
if (descriptors.length == 0) throw new IllegalArgumentException("At least one Resolution needs to be supplied.");
|
|
this.descriptors = descriptors;
|
|
this.resolver = fileResolver;
|
|
}
|
|
|
|
|
|
public Resolution chooseRounded(Resolution... descriptors) {
|
|
Resolution best = descriptors[0];
|
|
|
|
int leastDifference = -1;
|
|
|
|
int w = Gdx.graphics.getWidth(), h = Gdx.graphics.getHeight();
|
|
|
|
if (w > h) {
|
|
for (int i = 0; i < descriptors.length; i++) {
|
|
int currentDiff = h - descriptors[i].portraitHeight;
|
|
|
|
if (currentDiff < 0) {
|
|
currentDiff = currentDiff*-1;
|
|
}
|
|
|
|
if ((currentDiff < leastDifference) || leastDifference == -1) {
|
|
best = descriptors[i];
|
|
leastDifference = currentDiff;
|
|
}
|
|
}
|
|
} else {
|
|
for (int i = 0; i < descriptors.length; i++) {
|
|
int currentDiff = w - descriptors[i].portraitWidth;
|
|
|
|
if (currentDiff < 0) {
|
|
currentDiff = currentDiff*-1;
|
|
}
|
|
|
|
if (currentDiff < leastDifference || leastDifference == -1) {
|
|
best = descriptors[i];
|
|
leastDifference = currentDiff;
|
|
}
|
|
}
|
|
}
|
|
return best;
|
|
}
|
|
|
|
@Override
|
|
public FileHandle resolve(String fileName) {
|
|
Gdx.app.debug("RResolution Handler", "Finding best match for resolution: " + Gdx.graphics.getWidth() + "x" + Gdx.graphics.getHeight() + " for file: " + fileName);
|
|
Resolution bestRes = chooseRounded(descriptors);
|
|
FileHandle initialHandle = new FileHandle(fileName);
|
|
Gdx.app.debug("RResolution Handler", "Selected folder: " + bestRes.folder);
|
|
FileHandle resSpecificFile = resolver.resolve(bestRes.folder + "/" + initialHandle.name());
|
|
if (!resSpecificFile.exists()) resSpecificFile = resolver.resolve(fileName);
|
|
return resSpecificFile;
|
|
}
|
|
}
|