Skip to content

Commit

Permalink
Added listener for texture download
Browse files Browse the repository at this point in the history
  • Loading branch information
thothbot committed Feb 8, 2016
1 parent 0a05cd0 commit 82cdcfe
Show file tree
Hide file tree
Showing 14 changed files with 120 additions and 96 deletions.
12 changes: 0 additions & 12 deletions parallax/src/org/parallax3d/parallax/Files.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,33 +32,21 @@ public interface Files {
* @see FileType */
FileHandle getFileHandle(String path, FileType type);

FileHandle getFileHandle(String path, FileType type, FileListener<?> listener);

/** Convenience method that returns a {@link FileType#Classpath} file handle. */
FileHandle classpath(String path);

FileHandle classpath(String path, FileListener<?> listener);

/** Convenience method that returns a {@link FileType#Internal} file handle. */
FileHandle internal(String path);

FileHandle internal(String path, FileListener<?> listener);

/** Convenience method that returns a {@link FileType#External} file handle. */
FileHandle external(String path);

FileHandle external(String path, FileListener<?> listener);

/** Convenience method that returns a {@link FileType#Absolute} file handle. */
FileHandle absolute(String path);

FileHandle absolute(String path, FileListener<?> listener);

/** Convenience method that returns a {@link FileType#Local} file handle. */
FileHandle local(String path);

FileHandle local(String path, FileListener<?> listener);

/** Returns the external storage path directory. This is the SD card on Android and the home directory of the current user on
* the desktop. */
String getExternalStoragePath();
Expand Down
42 changes: 21 additions & 21 deletions parallax/src/org/parallax3d/parallax/files/FileHandle.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
/*******************************************************************************
* Copyright 2011 See AUTHORS file.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
******************************************************************************/
/*
* Copyright 2012 Alex Usachev, thothbot@gmail.com
* Base on libgdx FileHandle.java file
*
* This file is part of Parallax project.
*
* Parallax is free software: you can redistribute it and/or modify it
* under the terms of the Creative Commons Attribution 3.0 Unported License.
*
* Parallax 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 Creative Commons Attribution
* 3.0 Unported License. for more details.
*
* You should have received a copy of the the Creative Commons Attribution
* 3.0 Unported License along with Parallax.
* If not, see http://creativecommons.org/licenses/by/3.0/.
*/

package org.parallax3d.parallax.files;

Expand Down Expand Up @@ -43,21 +46,18 @@ protected FileHandle () {
* Do not use this constructor in case you write something cross-platforms. Use the {@link Files} interface instead.
* @param fileName the filename. */
public FileHandle (String fileName) {
this.file = new File(fileName);
this.type = FileType.Absolute;
this(new File(fileName), FileType.Absolute);
}

/** Creates a new absolute FileHandle for the {@link File}. Use this for tools on the desktop that don't need any of the
* platforms. Do not use this constructor in case you write something cross-platforms. Use the {@link Files} interface instead.
* @param file the file. */
public FileHandle (File file) {
this.file = file;
this.type = FileType.Absolute;
this(file, FileType.Absolute);
}

protected FileHandle (String fileName, FileType type) {
this.type = type;
file = new File(fileName);
this(new File(fileName), type);
}

protected FileHandle (File file, FileType type) {
Expand Down
3 changes: 1 addition & 2 deletions parallax/src/org/parallax3d/parallax/files/FileListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,10 @@

package org.parallax3d.parallax.files;

public interface FileListener<T> {
public interface FileListener<T> extends FileListenerSuccess<T> {

void onProgress(double amount);

void onFailure();

void onSuccess(T result);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright 2012 Alex Usachev, thothbot@gmail.com
*
* This file is part of Parallax project.
*
* Parallax is free software: you can redistribute it and/or modify it
* under the terms of the Creative Commons Attribution 3.0 Unported License.
*
* Parallax 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 Creative Commons Attribution
* 3.0 Unported License. for more details.
*
* You should have received a copy of the the Creative Commons Attribution
* 3.0 Unported License along with Parallax.
* If not, see http://creativecommons.org/licenses/by/3.0/.
*/

package org.parallax3d.parallax.files;

public interface FileListenerSuccess<T> {

void onSuccess(T result);
}
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ private static List<PixmapTextureData> getImagesFromUrl(String url)
for(String part: parts)
{
FileHandle file = App.files.internal(urlStart + part + urlEnd);
images.add(new PixmapTextureData(file));
images.add(new PixmapTextureData(file, null));
}

return images;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@
package org.parallax3d.parallax.graphics.textures;

import org.parallax3d.parallax.files.FileHandle;
import org.parallax3d.parallax.files.FileListenerSuccess;
import org.parallax3d.parallax.system.gl.GL20;

public class PixmapTextureData implements TextureData {

public PixmapTextureData (FileHandle file) {
public PixmapTextureData (FileHandle file, FileListenerSuccess<?> listener) {
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

import org.parallax3d.parallax.App;
import org.parallax3d.parallax.files.FileHandle;
import org.parallax3d.parallax.files.FileListener;
import org.parallax3d.parallax.files.FileListenerSuccess;
import org.parallax3d.parallax.system.ThreejsObject;
import org.parallax3d.parallax.graphics.renderers.GLRenderer;
import org.parallax3d.parallax.math.Vector2;
Expand Down Expand Up @@ -103,12 +105,16 @@ public Texture()
}

public Texture (String internalPath) {
this( App.files.internal(internalPath) );
this( App.files.internal(internalPath), null );
}

public Texture (FileHandle file)
public Texture (String internalPath, final FileListenerSuccess<?> listener) {
this( App.files.internal(internalPath), listener );
}

public Texture (FileHandle file, final FileListenerSuccess<?> listener)
{
this( new PixmapTextureData(file));
this( new PixmapTextureData(file, listener));
}

/**
Expand Down Expand Up @@ -161,6 +167,8 @@ public Texture(TextureData image, MAPPING_MODE mapping, TextureWrapMode wrapS,
this.id = Texture.TextureCount++;
this.offset = new Vector2(0, 0);
this.repeat = new Vector2(1, 1);

this.anisotropy = anisotropy;
}

/**
Expand All @@ -181,7 +189,6 @@ public Texture.MAPPING_MODE getMapping() {
* Sets the @{link Texture.MAPPING_MODE} value.
*/
public Texture setMapping(Texture.MAPPING_MODE mapping) {

this.mapping = mapping;
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,19 @@ public GwtAppConfiguration getConfig () {
public void onModuleLoad () {

App.app = GwtApp.this;

// Preload info about all assets
App.files = new GwtFiles( new Preloader(GWT.getHostPageBaseURL() + "assets/", "assets.txt", new Preloader.PreloaderCallback() {
@Override
public void ready(boolean success) {
onInit();
}
}) );

GwtApp.agentInfo = computeAgentInfo();
this.config = getConfig();

addEventListeners();

// Preload info about all assets
App.files = new GwtFiles( new Preloader( GWT.getHostPageBaseURL() + "assets/", "assets.txt" ) );

onInit();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,6 @@ public class GwtFileHandle extends FileHandle {
private final FileType type;

public GwtFileHandle(Preloader preloader, String fileName, FileType type) {
this(preloader, fileName, type, null);
}

public GwtFileHandle(Preloader preloader, String fileName, FileType type, FileListener<?> listener) {
if (type != FileType.Internal && type != FileType.Classpath)
throw new ParallaxRuntimeException("FileType '" + type + "' Not supported in GWT backend");
this.preloader = preloader;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,63 +37,33 @@ public GwtFiles (Preloader preloader) {
}

@Override
public FileHandle getFileHandle (String path, FileType type) {
return getFileHandle(path, type, null);
}

@Override
public FileHandle getFileHandle(String path, FileType type, FileListener<?> listener) {
public FileHandle getFileHandle( String path, FileType type ) {
if (type != FileType.Internal) throw new ParallaxRuntimeException("FileType '" + type + "' not supported in GWT backend");
return new GwtFileHandle(preloader, path, type, listener);
return new GwtFileHandle( preloader, path, type );
}

@Override
public FileHandle classpath (String path) {
return classpath(path, null);
}

@Override
public FileHandle classpath(String path, FileListener<?> listener) {
public FileHandle classpath( String path ) {
return new GwtFileHandle(preloader, path, FileType.Classpath);
}

@Override
public FileHandle internal (String path) {
return internal(path, null);
}

@Override
public FileHandle internal(String path, FileListener<?> listener) {
public FileHandle internal( String path ) {
return new GwtFileHandle(preloader, path, FileType.Internal);
}

@Override
public FileHandle external (String path) {
return external(path, null);
}

@Override
public FileHandle external(String path, FileListener<?> listener) {
public FileHandle external( String path ) {
throw new ParallaxRuntimeException("Not supported in GWT backend");
}

@Override
public FileHandle absolute (String path) {
return absolute(path, null);
}

@Override
public FileHandle absolute(String path, FileListener<?> listener) {
public FileHandle absolute( String path ) {
throw new ParallaxRuntimeException("Not supported in GWT backend");
}

@Override
public FileHandle local (String path) {
return local(path, null);
}

@Override
public FileHandle local(String path, FileListener<?> listener) {
public FileHandle local( String path ) {
throw new ParallaxRuntimeException("Not supported in GWT backend");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.io.*;

public class FileHandle {

public String path () {
throw new ParallaxRuntimeException("Stub");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,11 @@
import com.google.gwt.user.client.ui.Image;
import org.parallax3d.parallax.Log;
import org.parallax3d.parallax.files.FileHandle;
import org.parallax3d.parallax.graphics.textures.TextureData;
import org.parallax3d.parallax.files.FileListener;
import org.parallax3d.parallax.files.FileListenerSuccess;
import org.parallax3d.parallax.platforms.gwt.GwtFileHandle;
import org.parallax3d.parallax.platforms.gwt.GwtGL20;
import org.parallax3d.parallax.platforms.gwt.preloader.AssetDownloader;
import org.parallax3d.parallax.system.gl.GL20;
import org.parallax3d.parallax.system.gl.enums.PixelFormat;
import org.parallax3d.parallax.system.gl.enums.PixelType;
Expand All @@ -33,8 +36,28 @@ public class PixmapTextureData implements TextureData {

private Element image;

public PixmapTextureData(FileHandle file) {
public PixmapTextureData(final FileHandle file, final FileListenerSuccess listener) {

image = new Image(file.path()).getElement();

final AssetDownloader loader = new AssetDownloader();
loader.loadImage((GwtFileHandle) file, new FileListener<ImageElement>() {
@Override
public void onSuccess(ImageElement result) {
Log.info("Loaded texture: " + file.path());
listener.onSuccess(result);
}

@Override
public void onProgress(double amount) {

}

@Override
public void onFailure() {
Log.error("An error occurred while loading texture: " + file.path());
}
});
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@
import com.google.gwt.xhr.client.ReadyStateChangeHandler;
import com.google.gwt.xhr.client.XMLHttpRequest;
import com.google.gwt.xhr.client.XMLHttpRequest.ResponseType;
import org.parallax3d.parallax.Log;
import org.parallax3d.parallax.files.AssetFilter;
import org.parallax3d.parallax.files.FileListener;
import org.parallax3d.parallax.platforms.gwt.GwtFileHandle;
import org.parallax3d.parallax.system.ParallaxRuntimeException;

public class AssetDownloader {
Expand Down Expand Up @@ -128,6 +130,12 @@ public void onSuccess (Blob result) {
}
}

public void loadImage (GwtFileHandle file, final FileListener<ImageElement> listener) {
// Log.error(file.path());
// Log.error( file.preloader.images);
// loadImage(file.path(), file.preloader.images.get(file.path()).mimeType, listener);
}

public void loadImage (final String url, final String mimeType, final FileListener<ImageElement> listener) {
if (useBrowserCache || useInlineBase64) {
loadBinary(url, new FileListener<Blob>() {
Expand Down
Loading

0 comments on commit 82cdcfe

Please sign in to comment.