public override void OnGetGraphic(ImagePackage package, FilePath path) { // Already resized? ResizedImage resized = ResizedImages.Get(path.Path); if (resized != null) { // Sure is! package.GotGraphic(resized.Image); return; } if (Callback.WillDelay) { // Buffer this call until later - we're not on the main thread. // Create the callback: ResourcesProtocolCallback callback = new ResourcesProtocolCallback(package, path); // Hook up the protocol handler for speed later: callback.Protocol = this; // Request it to run: callback.Go(); return; } if (path.Filetype == "spa") { // Animated. // Load the binary file - Note: the full file should be called file.spa.bytes for this to work in Unity. byte[] binary = ((TextAsset)Resources.Load(path.Path)).bytes; if (binary != null) { // Apply it now: package.GotGraphic(new SPA(path.Url, binary)); return; } } else { // Image Texture2D image = (Texture2D)Resources.Load(path.Directory + path.Filename); // Resize the image: resized = ResizedImages.Add(path.Path, image); if (image != null) { package.GotGraphic(resized.Image); return; } } package.GotGraphic("Image not found in resources (" + path.Directory + path.Filename + " from URL '" + path.Url + "')."); }
private void OnLoadEvent(Dom.Event e) { BackgroundImage bgImage = RenderData.BGImage; if (bgImage == null) { return; } Image = bgImage.Image; if (Image == null) { return; } float width = (float)Image.Width; float height = (float)Image.Height; // Figure out the aspect ratios: AspectRatio = width / height; InverseAspectRatio = height / width; // Cache w/h: RawWidth = width; RawHeight = height; // Request layout: bgImage.RequestLayout(); }
public override void OnGetGraphic(ImagePackage package) { // Main thread only: Callback.MainThread(delegate(){ string resUrl = package.location.Directory + package.location.Filename; if (resUrl.Length > 0 && resUrl[0] == '/') { resUrl = resUrl.Substring(1); } // Get the image: UnityEngine.Object resource = Resources.Load(resUrl); if (resource == null) { // Note: the full file should be called something.bytes for this to work in Unity. resUrl = package.location.Path; if (resUrl.Length > 0 && resUrl[0] == '/') { resUrl = resUrl.Substring(1); } resource = Resources.Load(resUrl); } // Try loading from the asset: if (package.Contents.LoadFromAsset(resource, package)) { package.Done(); } }); }
public override void OnGetGraphic(ImagePackage package,FilePath path){ // Already resized? ResizedImage resized=ResizedImages.Get(path.Path); if(resized!=null){ // Sure is! package.GotGraphic(resized.Image); return; } if(Callback.WillDelay){ // Buffer this call until later - we're not on the main thread. // Create the callback: ResourcesProtocolCallback callback=new ResourcesProtocolCallback(package,path); // Hook up the protocol handler for speed later: callback.Protocol=this; // Request it to run: callback.Go(); return; } if(path.Filetype=="spa"){ // Animated. // Load the binary file - Note: the full file should be called file.spa.bytes for this to work in Unity. byte[] binary=((TextAsset)Resources.Load(path.Path)).bytes; if(binary!=null){ // Apply it now: package.GotGraphic(new SPA(path.Url,binary)); return; } }else{ // Image Texture2D image=(Texture2D)Resources.Load(path.Directory+path.Filename); // Resize the image: resized=ResizedImages.Add(path.Path,image); if(image!=null){ package.GotGraphic(resized.Image); return; } } package.GotGraphic("Image not found in resources ("+path.Directory+path.Filename+" from URL '"+path.Url+"')."); }
/// <summary>Attempts to get a graphic from the given location using this protocol.</summary> /// <param name="package">The image request. GotGraphic must be called on this when the protocol is done.</param> /// <param name="path">The location of the file to retrieve using this protocol.</param> public override void OnGetGraphic(ImagePackage package) { // Get from cache and apply to package: package.Contents = ImageCache.Get(package.location.Path); // Ok! package.Done(); }
/// <summary>Adds the given result to the cache for future request use.</summary> /// <param name="url">The url that will be requested.</param> /// <param name="result">The previous result to add to the cache.</param> public static void AddToCache(string url, ImagePackage result) { if (result == null) { return; } Cache[url] = result; }
/// <summary>Attempts to get a graphic from the given location using this protocol.</summary> /// <param name="package">The image request. GotGraphic must be called on this when the protocol is done.</param> /// <param name="path">The location of the file to retrieve using this protocol.</param> public override void OnGetGraphic(ImagePackage package,FilePath path){ if(path.Filetype=="spa"){ // Grab a runtime SPA: package.GotGraphic(SPA.Get(path.Path)); return; } package.GotGraphic(ImageCache.Get(path.Path)); }
/// <summary>Attempts to get a graphic from the given location using this protocol.</summary> /// <param name="package">The image request. GotGraphic must be called on this when the protocol is done.</param> /// <param name="path">The location of the file to retrieve using this protocol.</param> public override void OnGetGraphic(ImagePackage package, FilePath path) { // Create it: Texture2D tex = new Texture2D(0, 0); // Load it: tex.LoadImage(System.IO.File.ReadAllBytes(path.Path)); // Let the package know: package.GotGraphic(tex); }
public override void OnGetGraphic(ImagePackage package,FilePath path){ if(Callback.WillDelay){ // Buffer this call until later - we're not on the main thread. // Create the callback: ResourcesProtocolCallback callback=new ResourcesProtocolCallback(package,path); // Hook up the protocol handler for speed later: callback.Protocol=this; // Request it to run: callback.Go(); return; } if(path.Filetype=="spa"){ // Animated. // Load the binary file - Note: the full file should be called file.spa.bytes for this to work in Unity. byte[] binary=((TextAsset)Resources.Load(path.Path)).bytes; if(binary!=null){ // Apply it now: package.GotGraphic(new SPA(path.Url,binary)); return; } #if !MOBILE }else if(ContentType.IsVideo(path.Filetype)){ // Video MovieTexture movie=(MovieTexture)Resources.Load(path.Directory+path.Filename,typeof(MovieTexture)); if(movie!=null){ package.GotGraphic(movie); return; } #endif }else{ // Image Texture2D image=(Texture2D)Resources.Load(path.Directory+path.Filename); if(image!=null){ package.GotGraphic(image); return; } } package.GotGraphic("Image not found in resources ("+path.Directory+path.Filename+" from URL '"+path.Url+"')."); }
/// <summary>Attempts to get a graphic from the given location using this protocol.</summary> /// <param name="package">The image request. GotGraphic must be called on this when the protocol is done.</param> /// <param name="path">The location of the file to retrieve using this protocol.</param> public override void OnGetGraphic(ImagePackage package, FilePath path) { if (path.Filetype == "spa") { // Grab a runtime SPA: package.GotGraphic(SPA.Get(path.Path)); return; } package.GotGraphic(ImageCache.Get(path.Path)); }
/// <summary>Attempts to get a graphic from the given location using this protocol.</summary> /// <param name="package">The image request. GotGraphic must be called on this when the protocol is done.</param> /// <param name="path">The location of the file to retrieve using this protocol.</param> public override void OnGetGraphic(ImagePackage package,FilePath path){ // Create it: Texture2D tex = new Texture2D(0,0); // Load it: tex.LoadImage(System.IO.File.ReadAllBytes(path.Path)); // Let the package know: package.GotGraphic(tex); }
public override bool LoadFromAsset(UnityEngine.Object asset, ImagePackage package) { // Video Video = asset as MovieTexture; if (Video != null) { return(true); } return(base.LoadFromAsset(asset, package)); }
/// <summary>Called if the same resource was requested before so the image can be redisplayed quickly. /// This will always preceed some other result function.</summary> public void GotCached(ImagePackage package) { Error = package.Error; if (package.Animated) { GotGraphic(package.SPAFile); } else if (!package.IsVideo) { GotGraphic(package.Image); } }
public override void OnGetGraphic(ImagePackage package) { // Already resized? ResizedImage resized = ResizedImages.Get(package.location.Path); if (resized != null) { // Sure is! package.GotGraphic(resized.Image); return; } // Main thread only: Callback.MainThread(delegate(){ // Try loading from resources: string resUrl = package.location.Directory + package.location.Filename; if (resUrl.Length > 0 && resUrl[0] == '/') { resUrl = resUrl.Substring(1); } // Get the image: UnityEngine.Object resource = Resources.Load(resUrl); if (resource == null) { // Note: the full file should be called something.bytes for this to work in Unity. resource = Resources.Load(package.location.Path); } if (!package.Contents.LoadFromAsset(resource, package)) { return; } PictureFormat pict = package.Contents as PictureFormat; if (pict != null) { // Resize the image: resized = ResizedImages.Add(package.location.Path, pict.Image as Texture2D); // Apply: pict.Image = resized.Image; } // Great, stop there: package.Done(); }); }
public override bool LoadData(byte[] data, ImagePackage package) { // Create it: Texture2D image = new Texture2D(0, 0); // Load it: image.LoadImage(data); // Apply: Image = image; return(true); }
/// <summary>Attempts to get a graphic from the given location using this protocol.</summary> /// <param name="package">The image request. GotGraphic must be called on this when the protocol is done.</param> /// <param name="path">The location of the file to retrieve using this protocol.</param> public override void OnGetGraphic(ImagePackage package) { LoadBundle(package.location, delegate(AssetBundle bundle){ // Pull an image from the bundle using the hash as our hierarchy: UnityEngine.Object asset = bundle.LoadAsset(package.location.hash); // Try loading from the asset: if (package.Contents.LoadFromAsset(asset, package)) { // Ok! package.Done(); } }); }
public override void OnGetGraphic(ImagePackage package) { // Apply as camera format: string path = package.location.Path; CameraFormat cmf = package.Contents as CameraFormat; if (cmf == null) { cmf = new CameraFormat(); package.Contents = cmf; } // Update path: cmf.SetPath(path); // Ready: package.Done(); }
private void GotGraphicResult(HttpRequest request) { ImagePackage package = (ImagePackage)request.ExtraData; if (request.Errored) { package.GotGraphic(request.Error); } // Cache it: AddToCache(request.Url, package); string url = request.Url.ToLower(); // Split by dot for the type: string[] pieces = url.Split('.'); // Grab the type: string type = pieces[pieces.Length - 1]; if (type == "spa") { // Animation package.GotGraphic(new SPA(request.Url, request.Bytes)); #if !MOBILE } else if (ContentType.IsVideo(type)) { // Video package.GotGraphic(request.Video); #endif } else if (request.Image != null) { // Image package.GotGraphic(request.Image); } else { package.GotGraphic(request.Text); } }
public override void OnLoaded(string type) { ComputedStyle computed = Element.Style.Computed; if (computed.BGImage == null) { return; } Image = computed.BGImage.Image; if (Image == null) { return; } float width = (float)Image.Width(); float height = (float)Image.Height(); // Figure out the aspect ratios: AspectRatio = width / height; InverseAspectRatio = height / width; if (Image.PixelPerfect) { width *= ScreenInfo.ResolutionScale; height *= ScreenInfo.ResolutionScale; } // Compute the tag width: TagWidth = new Css.Value((int)width + "fpx", Css.ValueType.Pixels); // Compute the tag height: TagHeight = new Css.Value((int)height + "fpx", Css.ValueType.Pixels); // Act like we're setting tag properties: // This is so that class/ID/Style can override any of them. computed.ChangeTagProperty("height", TagHeight); computed.ChangeTagProperty("width", TagWidth); computed.ChangeTagProperty("background-repeat", new Css.Value("no-repeat", Css.ValueType.Text)); computed.ChangeTagProperty("background-size", new Css.Value("100% 100%", Css.ValueType.Point)); }
/// <summary>Called when the given character was not found in the font.</summary> /// <param name="glyph">The character to try and find an alternate image for.</param. public ImagePackage Load(Glyph glyph, int charcode) { string path = GetPath(charcode); if (string.IsNullOrEmpty(path)) { return(null); } // Create an image package and request for the graphic. Convert it to lowercase hex to match standard filenames: ImagePackage package = new ImagePackage(path, ""); // Assign the character as some extra data: package.ExtraData = glyph; // Go get it, calling ImageReady when it's been retrieved. package.Get(ImageReady); return(package); }
/// <summary>Called when an image is found for this character. Used by e.g. Emoji.</summary> /// <param name="package">The image that was found.</param> public void SetupImage(ImagePackage package){ if(package==null){ Image=null; return; } Image=package; // Great! We found this character somewhere else as an image: Width=Image.Width(); Height=Image.Height(); // And apply delta width too: AdvanceWidth=Width; // Queue up a global layout. UI.document.Renderer.RequestLayout(); }
/// <summary>Attempt to load the image from a Unity resource.</summary> public virtual bool LoadFromAsset(UnityEngine.Object asset, ImagePackage package) { // Note: the full file should be called something.bytes for this to work in Unity. TextAsset text = asset as TextAsset; if (text == null) { package.Failed(404); return(false); } byte[] binary = text.bytes; package.ReceivedHeaders(binary.Length); // Apply it now: package.ReceivedData(binary, 0, binary.Length); return(true); }
/// <summary>Called when an image has been received.</summary> /// <param name="package">The package holding the image.</param> public void ImageReady(ImagePackage package) { // The package returned! // Was it sucessful? if (package.Ok) { // Grab the character: Glyph glyph = package.ExtraData as Glyph; if (glyph != null) { // Tell it about the success: glyph.SetupImage(package); } } // Otherwise, nope! Silent error as the character might just not be available anyway. }
public override void OnGetGraphic(ImagePackage package,FilePath path){ // Work like a proper browser - let's go get the image from the web. // Second is for video - it's called when the video is ready for playback but // Not necessarily fully downloaded. // First, was this graphic already requested? // If so, we'll provide that immediately. ImagePackage previousRequest=GetFromCache(path.Url); if(previousRequest!=null){ // Great - provide this packages result first. // This prevents any flashing if innerHTML is loaded. package.GotCached(previousRequest); return; } HttpRequest request=new HttpRequest(path.Url,GotGraphicResult); request.OnRequestReady+=GotGraphicResult; request.ExtraData=package; request.Send(); }
/// <summary>Applies the image data so it's ready for rendering.</summary> public void ApplyImageData() { // Grab the canvas: Element element = canvas; // Grab its computed style: ComputedStyle computed = element.style.Computed; if (ImageData == null) { ImageData = new DynamicTexture(); } // Resize the texture: ImageData.Resize(computed.PixelWidth, computed.PixelHeight, false); if (ImageData.Width != 0 && ImageData.Height != 0) { if (Package == null) { //We now need a package to actually display it. // Create the package: Package = new ImagePackage(ImageData); // Apply it to the element: if (computed.BGImage == null) { computed.BGImage = new BackgroundImage(element); } computed.BGImage.SetImage(Package); } // Run the change event: element.Run("onchange"); apply(); } }
public override bool LoadFromAsset(UnityEngine.Object asset, ImagePackage package) { Image = asset as Texture2D; if (Image != null) { return(true); } if (asset != null && !(asset is TextAsset)) { Dom.Log.Add( "That's not an image - Url '" + package.location.absolute + "' is a '" + asset.GetType() + "'. In Unity this happens if you've got more than one resource with the same name (but different file types)." ); return(false); } // Try binary: return(base.LoadFromAsset(asset, package)); }
/// <summary>Called when the given character was not found in the font.</summary> /// <param name="glyph">The character to try and find an alternate image for.</param. public ImagePackage Load(Glyph glyph, int charcode) { string path = GetPath(charcode); if (string.IsNullOrEmpty(path)) { return(null); } // Create an image package and request for the graphic. Convert it to lowercase hex to match standard filenames: ImagePackage package = new ImagePackage(path, null); // Go get it, calling ImageReady when it's been retrieved (we only care about success here). package.onload = delegate(UIEvent e){ // Tell it about the success: glyph.SetupImage(package); }; package.send(); return(package); }
public override void OnGetGraphic(ImagePackage package, FilePath path) { // Work like a proper browser - let's go get the image from the web. // Second is for video - it's called when the video is ready for playback but // Not necessarily fully downloaded. // First, was this graphic already requested? // If so, we'll provide that immediately. ImagePackage previousRequest = GetFromCache(path.Url); if (previousRequest != null) { // Great - provide this packages result first. // This prevents any flashing if innerHTML is loaded. package.GotCached(previousRequest); return; } HttpRequest request = new HttpRequest(path.Url, GotGraphicResult); request.OnRequestReady += GotGraphicResult; request.ExtraData = package; request.Send(); }
/// <summary>Called if the same resource was requested before so the image can be redisplayed quickly. /// This will always preceed some other result function.</summary> public void GotCached(ImagePackage package){ Error=package.Error; if(package.Animated){ GotGraphic(package.SPAFile); }else if(!package.IsVideo){ GotGraphic(package.Image); } }
public override void OnGetGraphic(ImagePackage package, FilePath path) { package.GotGraphic(DynamicTexture.Get(path.Path)); }
/// <summary>Some formats may cache their result internally. This checks and updates if it has.</summary> public virtual bool InternallyCached(Location path, ImagePackage package) { return(false); }
/// <summary>Loads the raw block of data into an object of this format.</summary> public virtual bool LoadData(byte[] data, ImagePackage package) { return(false); }
/// <summary>Caches the image in this HTTP request. Useful for preloading images with ajax.</summary> public void CacheImage(){ // Create a package: ImagePackage package=new ImagePackage(Url,""); // Hook up the image: package.Image=Image; // Cache it: HttpProtocol.AddToCache(Url,package); }
/// <summary>Get the file at the given path as a MovieTexture (Unity Pro only!), Texture2D, /// SPA Animation or DynamicTexture using this protocol. /// Once it's been retrieved, this must call package.GotGraphic(theObject) internally.</summary> public virtual void OnGetGraphic(ImagePackage package,FilePath path){}
public override bool OnAttributeChange(string property){ if(base.OnAttributeChange(property)){ return true; } if(property=="path"){ // Go get the camera now! // Clear any existing one: Camera=null; // Grab the path itself: string path=Element["path"]; // Get it: GameObject gameObject=GameObject.Find(path); if(gameObject!=null){ // Grab the camera: Camera=gameObject.GetComponent<Camera>(); } if(Camera!=null){ // Setup the clear flags: Camera.clearFlags=CameraClearFlags.Depth; } ParentMask(); return true; }else if(property=="mask"){ // We've got a mask! // Grab the file path: string maskFile=Element["mask"]; if(maskFile==null){ SetMask(null); }else{ // Create a package to get the mask: ImagePackage package=new ImagePackage(maskFile,Element.Document.basepath); // Go get it, calling ImageReady when it's been retrieved. package.Get(ImageReady); } return true; } return false; }
/// <summary>Adds the given result to the cache for future request use.</summary> /// <param name="url">The url that will be requested.</param> /// <param name="result">The previous result to add to the cache.</param> public static void AddToCache(string url,ImagePackage result){ if(result==null){ return; } Cache[url]=result; }
public ResourcesProtocolCallback(ImagePackage package,FilePath path){ Images=package; Path=path; }
//--------------------------------------
public ResourcesProtocolCallback(ImagePackage package, FilePath path) { Images = package; Path = path; }
/// <summary>Called when a mask image has been received.</summary> /// <param name="package">The package holding the mask image.</param> public void ImageReady(ImagePackage package){ // Apply the mask: SetMask(package.Image); }
/// <summary>Get the file at the given path as a MovieTexture (Unity Pro only!), Texture2D, /// SPA Animation or DynamicTexture using this protocol. /// Once it's been retrieved, this must call package.GotGraphic(theObject) internally.</summary> public virtual void OnGetGraphic(ImagePackage package) { // Get the data - it'll call package.Done which will handle it for us: OnGetData(package); }
public override bool OnAttributeChange(string property) { if (base.OnAttributeChange(property)) { return(true); } if (property == "path") { // Go get the camera now! // Clear any existing one: Camera = null; Callback.MainThread(delegate(){ // Grab the path itself: string path = this["path"]; // Get it: GameObject gameObject = GameObject.Find(path); if (gameObject != null) { // Grab the camera: Camera = gameObject.GetComponent <Camera>(); } if (Camera != null) { ComputedStyle computed = Style.Computed; // Create RT if one is needed: RenderTexture rt = Camera.targetTexture; if (rt == null) { // Apply: ApplyNewRenderTexture((int)computed.InnerWidth, (int)computed.InnerHeight); } else { // Apply to background: image = rt; } } ParentMask(); }); return(true); } else if (property == "noresize") { // Can't resize if noresize is not null: CanResize = (this["noresize"] == null); } else if (property == "mask") { // We've got a mask! // Grab the file path: string maskFile = this["mask"]; if (maskFile == null) { SetMask(null); } else { // Create a package to get the mask: ImagePackage package = new ImagePackage(maskFile, document.basepath); package.onload = delegate(UIEvent e){ // Apply the mask: PictureFormat pict = package.Contents as PictureFormat; if (pict != null) { SetMask(pict.Image); } }; // Go get it: package.send(); } return(true); } return(false); }
public override void OnLoaded(string type){ ComputedStyle computed=Element.Style.Computed; if(computed.BGImage==null){ return; } Image=computed.BGImage.Image; if(Image==null){ return; } float width=(float)Image.Width(); float height=(float)Image.Height(); // Figure out the aspect ratios: AspectRatio=width/height; InverseAspectRatio=height/width; if(Image.PixelPerfect){ width*=ScreenInfo.ResolutionScale; height*=ScreenInfo.ResolutionScale; } // Compute the tag width: TagWidth=new Css.Value((int)width+"fpx",Css.ValueType.Pixels); // Compute the tag height: TagHeight=new Css.Value((int)height+"fpx",Css.ValueType.Pixels); // Act like we're setting tag properties: // This is so that class/ID/Style can override any of them. computed.ChangeTagProperty("height",TagHeight); computed.ChangeTagProperty("width",TagWidth); computed.ChangeTagProperty("background-repeat",new Css.Value("no-repeat",Css.ValueType.Text)); computed.ChangeTagProperty("background-size",new Css.Value("100% 100%",Css.ValueType.Point)); }
public override bool OnAttributeChange(string property) { if (base.OnAttributeChange(property)) { return(true); } if (property == "path") { // Go get the camera now! // Clear any existing one: Camera = null; // Grab the path itself: string path = Element["path"]; // Get it: GameObject gameObject = GameObject.Find(path); if (gameObject != null) { // Grab the camera: Camera = gameObject.GetComponent <Camera>(); } if (Camera != null) { // Setup the clear flags: Camera.clearFlags = CameraClearFlags.Depth; } ParentMask(); return(true); } else if (property == "mask") { // We've got a mask! // Grab the file path: string maskFile = Element["mask"]; if (maskFile == null) { SetMask(null); } else { // Create a package to get the mask: ImagePackage package = new ImagePackage(maskFile, Element.Document.basepath); // Go get it, calling ImageReady when it's been retrieved. package.Get(ImageReady); } return(true); } return(false); }
public override void OnGetGraphic(ImagePackage package,FilePath path){ package.GotGraphic(DynamicTexture.Get(path.Path)); }