/// <summary>Sets how many pixels of space this renderer has. The actual world space size is dictated by this and /// <see cref="PowerUI.WorldUI.SetResolution"/>. The amount of pixels and pixels per world unit (resolution).</summary> /// <param name="widthPX">The width in pixels.</param> /// <param name="heightPX">The height in pixels.</param> public virtual bool SetDimensions(int widthPX, int heightPX) { if (widthPX == pixelWidth && heightPX == pixelHeight) { return(false); } // Inform media rules if we have any too: var media = document.MediaIfExists; // Resize: if (widthPX != pixelWidth) { pixelWidth = widthPX; document.Viewport.Width = widthPX; if (media != null) { // Nudge it! media.Width = widthPX; } } if (heightPX != pixelHeight) { pixelHeight = heightPX; PixelHeightF = (float)pixelHeight; document.Viewport.Height = heightPX; if (media != null) { // Nudge it! media.Height = heightPX; } } // Update ratio: Ratio = (float)pixelWidth / PixelHeightF; // Reset the origin position: SetOrigin(OriginLocation.x, OriginLocation.y); // Fire the resize event: if (document.body != null) { Dom.Event e = new Dom.Event("resize"); e.SetTrusted(); document.dispatchEvent(e); } // Request a layout too: document.RequestLayout(); return(true); }
/// <summary>Called when this element got added to the DOM.</summary> internal override void AddedToDOM() { HtmlDocument doc = htmlDocument; if (doc != null) { if (doc.AttributeIndex != null) { // Index element if needed: AddToAttributeLookups(); } } // Update its css by telling it the parent changed. // This affects inherit, height/width etc. Style.Computed.ParentChanged(); if (doc != null) { // Request a layout: doc.RequestLayout(); } }
/// <summary>Updates the zoom:auto value.</summary> public void UpdateZoom(bool requestReflow) { // Zoom the lengths: float zoom = LengthScale; #if UNITY_EDITOR _LengthScale = LengthScale; #endif if (AutomaticallyHandleDpi) { // Using the web API to get the pixel ratio: zoom *= Document.window.devicePixelRatio; } // Apply zoom (base for zoom:auto; override by setting some other zoom value): Document.Zoom = zoom; if (requestReflow) { Document.RequestLayout(); } }
/// <summary> Checks if the screen size changed and repaints if it did. /// Called by <see cref="UI.Update"/>.</summary> public static void Update() { if (UI.GUICamera == null) { // PowerUI is offline. return; } bool changedX = false; bool changedY = false; if (UnityEngine.Screen.width != ScreenX) { ScreenX = UnityEngine.Screen.width; ScreenXFloat = (float)ScreenX; changedX = true; } if (UnityEngine.Screen.height != ScreenY) { ScreenY = UnityEngine.Screen.height; ScreenYFloat = (float)ScreenY; changedY = true; } HtmlDocument document = UI.document; // Device orientation changed? bool landscape = IsLandscape(); Css.MediaType media; if (PreviousOrientation == DeviceOrientation.Unknown) { // First time. Straight set it: PreviousOrientation = landscape?DeviceOrientation.LandscapeLeft : DeviceOrientation.Portrait; } else { // Changed? if (landscape && PreviousOrientation != DeviceOrientation.LandscapeLeft) { // Orientation changed! Update previous: PreviousOrientation = landscape?DeviceOrientation.LandscapeLeft : DeviceOrientation.Portrait; // Inform main UI media rules: media = document.MediaIfExists; if (media != null) { // Nudge it! media.Landscape = landscape; } // Fire the rotation event now (on the window): // We're using absolute here because 'deviceorientation' is the actual angle of the device. DeviceOrientationEvent e = new DeviceOrientationEvent("deviceorientationabsolute"); e.absolute = true; e.SetTrusted(); document.window.dispatchEvent(e); } } if (!changedX && !changedY) { return; } // Nudge the matrices: UI.GUICamera.ResetWorldToCameraMatrix(); UI.GUICamera.ResetProjectionMatrix(); UI.GUICamera.ResetAspect(); // Firstly, find the bottom left and top right corners at UI.CameraDistance z units away (zero z-index): Vector3 bottomLeft = UI.GUICamera.ScreenToWorldPoint(new Vector3(0f, 0f, UI.CameraDistance)); Vector3 topRight = UI.GUICamera.ScreenToWorldPoint(new Vector3(ScreenX, ScreenY, UI.CameraDistance)); // With those, we can now find the size of the screen in world units: WorldSize.x = topRight.x - bottomLeft.x; WorldSize.y = topRight.y - bottomLeft.y; // Finally, calculate WorldPerPixel at zero depth: // Mapping PX to world units: WorldPerPixel.x = WorldSize.x / (float)ScreenX; WorldPerPixel.y = WorldSize.y / (float)ScreenY; // Set where the origin is. All rendering occurs relative to this point. // It's offset by 0.2 pixels to target a little closer to the middle of each pixel. This helps Pixel filtering look nice and clear. WorldScreenOrigin.y = bottomLeft.y + (0.4f * WorldPerPixel.y); WorldScreenOrigin.x = bottomLeft.x - (0.4f * WorldPerPixel.x); // Update main document's viewport: document.Viewport.Update(ScreenXFloat, ScreenYFloat); // Fire the resize event (doesn't bubble): Dom.Event resize = new Dom.Event("resize"); resize.SetTrusted(false); document.window.dispatchEvent(resize); // Inform main UI media rules: media = document.MediaIfExists; // Resize: if (changedX) { int w = (int)ScreenXFloat; document.RequestLayout(); if (media != null) { // Nudge it! media.Width = w; } } if (changedY) { int h = (int)ScreenYFloat; document.RequestLayout(); if (media != null) { // Nudge it! media.Height = h; } } }