Java Code Examples for com.badlogic.gdx.files.FileHandle#isDirectory()

The following examples show how to use com.badlogic.gdx.files.FileHandle#isDirectory() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FilePopupMenu.java    From vis-ui with Apache License 2.0 6 votes vote down vote up
public void build (Array<FileHandle> favorites, FileHandle file) {
	sortingPopupMenu.build();
	this.file = file;

	clearChildren();

	addItem(newDirectory);
	addItem(sortBy);
	addItem(refresh);
	addSeparator();

	if (file.type() == FileType.Absolute || file.type() == FileType.External) addItem(delete);

	if (file.type() == FileType.Absolute) {
		addItem(showInExplorer);

		if (file.isDirectory()) {
			if (favorites.contains(file, false))
				addItem(removeFromFavorites);
			else
				addItem(addToFavorites);
		}
	}
}
 
Example 2
Source File: FileChooser.java    From vis-ui with Apache License 2.0 6 votes vote down vote up
/** List currently set directory with all active filters */
private FileHandle[] listFilteredCurrentDirectory () {
	FileHandle[] files = currentDirectory.list(fileFilter);
	if (fileTypeFilter == null || activeFileTypeRule == null) return files;

	FileHandle[] filtered = new FileHandle[files.length];

	int count = 0;
	for (FileHandle file : files) {
		if (file.isDirectory() == false && activeFileTypeRule.accept(file) == false) continue;
		filtered[count++] = file;
	}

	if (count == 0) return new FileHandle[0];

	FileHandle[] newFiltered = new FileHandle[count];
	System.arraycopy(filtered, 0, newFiltered, 0, count);
	return newFiltered;
}
 
Example 3
Source File: FileUtils.java    From vis-ui with Apache License 2.0 6 votes vote down vote up
/** Shows given directory in system explorer window. */
@SuppressWarnings("unchecked")
public static void showDirInExplorer (FileHandle dir) throws IOException {
	File dirToShow;
	if (dir.isDirectory()) {
		dirToShow = dir.file();
	} else {
		dirToShow = dir.parent().file();
	}

	if (OsUtils.isMac()) {
		FileManager.revealInFinder(dirToShow);
	} else {
		try {
			// Using reflection to avoid importing AWT desktop which would trigger Android Lint errors
			// This is desktop only, rarely called, performance drop is negligible
			// Basically 'Desktop.getDesktop().open(dirToShow);'
			Class desktopClass = Class.forName("java.awt.Desktop");
			Object desktop = desktopClass.getMethod("getDesktop").invoke(null);
			desktopClass.getMethod("open", File.class).invoke(desktop, dirToShow);
		} catch (Exception e) {
			Gdx.app.log("VisUI", "Can't open file " + dirToShow.getPath(), e);
		}
	}
}
 
Example 4
Source File: FileUtils.java    From vis-ui with Apache License 2.0 6 votes vote down vote up
/**
 * Sorts file list, using this rules: directories first, sorted using provided comparator, then files sorted using provided comparator.
 * @param files list to sort
 * @param comparator comparator used to sort files list
 * @param descending if true then sorted list will be in reversed order
 * @return sorted file list
 */
public static Array<FileHandle> sortFiles (FileHandle[] files, Comparator<FileHandle> comparator, boolean descending) {
	Array<FileHandle> directoriesList = new Array<FileHandle>();
	Array<FileHandle> filesList = new Array<FileHandle>();

	for (FileHandle f : files) {
		if (f.isDirectory()) {
			directoriesList.add(f);
		} else {
			filesList.add(f);
		}
	}

	Sort sorter = new Sort();
	sorter.sort(directoriesList, comparator);
	sorter.sort(filesList, comparator);

	if (descending) {
		directoriesList.reverse();
		filesList.reverse();
	}

	directoriesList.addAll(filesList); // combine lists
	return directoriesList;
}
 
Example 5
Source File: LegacyCompareTest.java    From talos with Apache License 2.0 5 votes vote down vote up
private void traverseFolder (FileHandle folder, Array<String> fileList, String extension, int depth) {
	for (FileHandle file : folder.list()) {
		if (file.isDirectory() && depth < 10) {
			traverseFolder(file, fileList, extension, depth + 1);
		}
		if (file.extension().equals(extension)) {
			fileList.add(file.path());
		}
	}
}
 
Example 6
Source File: ModInputStream.java    From Cubes with MIT License 5 votes vote down vote up
@Override
public ModFile getNextModFile() throws IOException {
  FileHandle file = files.pollFirst();
  if (file != null) return new FolderModFile(file);
  FileHandle folder = folders.pollFirst();
  if (folder == null) return null;
  for (FileHandle f : folder.list()) {
    if (f.isDirectory()) {
      folders.add(f);
    } else {
      files.add(f);
    }
  }
  return new FolderModFile(folder);
}
 
Example 7
Source File: FileHandleMetadata.java    From vis-ui with Apache License 2.0 5 votes vote down vote up
private FileHandleMetadata (FileHandle file) {
	this.name = file.name();
	this.directory = file.isDirectory();
	this.lastModified = file.lastModified();
	this.length = file.length();
	this.readableFileSize = FileUtils.readableFileSize(length);
}
 
Example 8
Source File: FormValidator.java    From vis-ui with Apache License 2.0 5 votes vote down vote up
@Override
protected boolean validate (String input) {
	FileHandle file = Gdx.files.absolute(input);
	if (file.exists() == false || file.isDirectory() == false) return false;
	if (mustBeEmpty) {
		return file.list().length == 0;
	} else {
		return file.list().length != 0;
	}
}
 
Example 9
Source File: ClientSaveManager.java    From Cubes with MIT License 5 votes vote down vote up
public static Save[] getSaves() {
  FileHandle clientSavesFolder = getSavesFolder();
  if (!clientSavesFolder.isDirectory()) return new Save[0];
  FileHandle[] list = clientSavesFolder.list();
  Save[] saves = new Save[list.length];
  for (int i = 0; i < list.length; i++) {
    saves[i] = new Save(list[i].name(), list[i]);
  }
  return saves;
}
 
Example 10
Source File: ClassFinder.java    From libgdx-snippets with MIT License 5 votes vote down vote up
private void processDirectory(FileHandle root, FileHandle directory,
							  Predicate<String> filter, Consumer<Class<?>> processor) {

	FileHandle[] files = directory.list();

	for (FileHandle file : files) {

		if (file.isDirectory()) {
			processDirectory(root, new FileHandle(new File(directory.file(), file.name())), filter, processor);
		} else {
			process(root, file, filter, processor);
		}

	}
}
 
Example 11
Source File: Compatibility.java    From Cubes with MIT License 5 votes vote down vote up
public void makeBaseFolder() {
  FileHandle baseFolder = getBaseFolder();
  baseFolder.mkdirs();
  if (!baseFolder.isDirectory()) {
    FileHandle parent = baseFolder.parent();
    if (!parent.isDirectory()) Log.error("Parent directory '" + parent.path() + "' doesn't exist!");
    throw new CubesException("Failed to make Cubes folder! '" + baseFolder.path() + "'");
  }
}
 
Example 12
Source File: InputFile.java    From gdx-texture-packer-gui with Apache License 2.0 5 votes vote down vote up
public InputFile(FileHandle fileHandle, Type type) {
    this.fileHandle = fileHandle;
    this.type = type;

    // Determine if the file is a directory.
    // We cannot use just `fileHandle.isDirectory()`,
    // because in case the file doesn't exist it always returns `false`.
    if (fileHandle.exists()) {
        this.directory = fileHandle.isDirectory();
    } else {
        this.directory = fileHandle.extension().length() == 0;
    }
}
 
Example 13
Source File: SkinEditorGame.java    From gdx-skineditor with Apache License 2.0 5 votes vote down vote up
@Override
public void create() {
	
	opt = new OptionalChecker();
	
	fm = new SystemFonts();
	fm.refreshFonts();
	
	// Create projects folder if not already here
	FileHandle dirProjects = new FileHandle("projects");
	
	if (dirProjects.isDirectory() == false) {
		dirProjects.mkdirs();
	}
	
	// Rebuild from raw resources, kind of overkill, might disable it for production
	TexturePacker.Settings settings = new TexturePacker.Settings();
	settings.combineSubdirectories = true;
	TexturePacker.process(settings, "resources/raw/", ".", "resources/uiskin");

	batch = new SpriteBatch();
	skin = new Skin();
	atlas = new TextureAtlas(Gdx.files.internal("resources/uiskin.atlas"));
	
	
	skin.addRegions(new TextureAtlas(Gdx.files.local("resources/uiskin.atlas")));
	skin.load(Gdx.files.local("resources/uiskin.json"));
	
	screenMain = new MainScreen(this);
	screenWelcome = new WelcomeScreen(this);
	setScreen(screenWelcome);
	
}
 
Example 14
Source File: GameLevels.java    From uracer-kotd with Apache License 2.0 5 votes vote down vote up
public static final boolean init () {

		// setup map loader
		mapLoaderParams.forceTextureFilters = true;
		mapLoaderParams.textureMinFilter = TextureFilter.Linear;
		mapLoaderParams.textureMagFilter = TextureFilter.Linear;
		mapLoaderParams.yUp = false;

		// check invalid
		FileHandle dirLevels = Gdx.files.internal(Storage.Levels);
		if (dirLevels == null || !dirLevels.isDirectory()) {
			throw new URacerRuntimeException("Path not found (" + Storage.Levels + "), cannot check for available game levels.");
		}

		// check for any level
		FileHandle[] tracks = dirLevels.list("tmx");
		if (tracks.length == 0) {
			throw new URacerRuntimeException("Cannot find game levels.");
		}

		// build internal maps
		for (int i = 0; i < tracks.length; i++) {
			GameLevelDescriptor desc = computeDescriptor(tracks[i].name());
			levels.add(desc);

			// build lookup table
			levelIdToDescriptor.put(desc.getId(), desc);

			Gdx.app.log("GameLevels", "Found level \"" + desc.getName() + "\" (" + desc.getId() + ")");
		}

		// sort tracks
		Collections.sort(levels);

		return true;
	}
 
Example 15
Source File: DeckBuilderScreen.java    From Cardshifter with Apache License 2.0 5 votes vote down vote up
private void updateSavedDeckList() {
    java.util.List<String> list = new ArrayList<String>();
    for (FileHandle handle : external.list()) {
        if (!handle.isDirectory()) {
            list.add(handle.nameWithoutExtension());
        }
    }
    savedDecks.setItems(list.toArray(new String[list.size()]));
}
 
Example 16
Source File: ModInputStream.java    From Cubes with MIT License 5 votes vote down vote up
public FolderModInputStream(FileHandle file) {
  root = file;
  for (FileHandle f : file.list()) {
    if (f.isDirectory()) {
      folders.add(f);
    } else {
      files.add(f);
    }
  }
}
 
Example 17
Source File: SinglePlayer.java    From uracer-kotd with Apache License 2.0 4 votes vote down vote up
/** Load from disk all the replays for the specified trackId, pruning while loading respecting the ReplayManager.MaxReplays
 * constant. Any previous Replay will be cleared from the lapManager instance. */
private int loadReplaysFromDiskFor (String trackId) {
	lapManager.removeAllReplays();

	int reloaded = 0;
	for (FileHandle userdir : Gdx.files.external(Storage.ReplaysRoot + gameWorld.getLevelId()).list()) {
		if (userdir.isDirectory()) {
			for (FileHandle userreplay : userdir.list()) {
				Replay replay = Replay.load(userreplay.path());
				if (replay != null) {
					// add replays even if slower
					ReplayResult ri = lapManager.addReplay(replay);
					if (ri.is_accepted) {
						ReplayUtils.pruneReplay(ri.pruned); // prune if needed
						reloaded++;
						Gdx.app.log("SinglePlayer", "Loaded replay #" + ri.accepted.getShortId());
					} else {

						String msg = "";
						switch (ri.reason) {
						case Null:
							msg = "null replay (" + userreplay.path() + ")";
							break;
						case InvalidMinDuration:
							msg = "invalid lap (" + ri.discarded.getSecondsStr() + "s < " + GameplaySettings.ReplayMinDurationSecs
								+ "s) (#" + ri.discarded.getShortId() + ")";
							break;
						case Invalid:
							msg = "the specified replay is not valid. (" + userreplay.path() + ")";
							break;
						case WrongTrack:
							msg = "the specified replay belongs to another game track (#" + ri.discarded.getShortId() + ")";
							break;
						case Slower:
							msg = "too slow! (#" + ri.discarded.getShortId() + ")";
							ReplayUtils.pruneReplay(ri.discarded);
							break;
						case Accepted:
							break;
						}

						Gdx.app.log("SinglePlayer", "Discarded at loading time, " + msg);
					}
				}
			}
		}
	}

	Gdx.app.log("SinglePlayer", "Building opponents list:");

	rebindAllReplays();

	int pos = 1;
	for (Replay r : lapManager.getReplays()) {
		Gdx.app.log("SinglePlayer",
			"#" + pos + ", #" + r.getShortId() + ", secs=" + r.getSecondsStr() + ", ct=" + r.getCreationTimestamp());
		pos++;
	}

	Gdx.app.log("SinglePlayer", "Reloaded " + reloaded + " opponents.");
	return reloaded;
}
 
Example 18
Source File: PerSpineKeeper.java    From AzurLaneSpineCharacterDecoder with MIT License 4 votes vote down vote up
public void setSavePath(FileHandle savePath) {
    if (savePath.isDirectory())
        this.savePath = savePath;
}
 
Example 19
Source File: FileUtils.java    From shattered-pixel-dungeon with GNU General Public License v3.0 4 votes vote down vote up
public static boolean dirExists( String name ){
	FileHandle dir = getFileHandle( name );
	return dir.exists() && dir.isDirectory();
}
 
Example 20
Source File: ClassFinder.java    From libgdx-snippets with MIT License 2 votes vote down vote up
/**
 * Iterates classes of all URLs filtered by a previous call of {@link ClassFinder#filterURLforClass(Class)}.
 */
public ClassFinder process(Predicate<String> filter, Consumer<Class<?>> processor) {

	for (URL url : urls) {

		Path path;

		// required for Windows paths with spaces
		try {
			path = Paths.get(url.toURI());
		} catch (URISyntaxException e) {
			throw new GdxRuntimeException(e);
		}

		FileHandle file = new FileHandle(path.toFile());

		if (file.isDirectory()) {

			processDirectory(file, file, filter, processor);

		} else if (file.extension().equals("jar")) {

			try (JarFile jar = new JarFile(file.file())) {

				Enumeration<JarEntry> entries = jar.entries();

				while (entries.hasMoreElements()) {

					JarEntry entry = entries.nextElement();

					if (entry.isDirectory()) {
						continue;
					}

					FileHandle entryFile = new FileHandle(entry.getName());

					process(null, entryFile, filter, processor);
				}

			} catch (IOException ignored) {

			}
		}

	}

	return this;
}