SebastianLawe

Untitled

Apr 23rd, 2015
270
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.60 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using UnityEngine;
  6.  
  7. public class RelativeRect {
  8.     float _x;
  9.     float _y;
  10.     float _width;
  11.     float _height;
  12.     public enum Pin {
  13.         TopLeft = 0,
  14.         TopRight = 1,
  15.         BottomLeft = 2,
  16.         BottomRight = 3
  17.     }
  18.     public float X {
  19.         get {
  20.             return _x;
  21.         }
  22.         set {
  23.             _x = value;
  24.         }
  25.     }
  26.     public float Y {
  27.         get {
  28.             return _y;
  29.         }
  30.         set {
  31.             _y = value;
  32.         }
  33.     }
  34.     public float Width {
  35.         get {
  36.             return _width;
  37.         }
  38.         set {
  39.             _width = value;
  40.         }
  41.     }
  42.     public float Height {
  43.         get {
  44.             return _height;
  45.         }
  46.         set {
  47.             _height = value;
  48.         }
  49.     }
  50.     public Rect ScreenCoordinates {
  51.         get {
  52.             return GetExact(_x, _y, _width, _height, false);
  53.         }
  54.     }
  55.     public RelativeRect(float x, float y, float width, float height, bool LockRatio = false) {
  56.         if (!LockRatio) {
  57.             _x = x;
  58.             _y = y;
  59.             _width = width;
  60.             _height = height;
  61.         } else {
  62.             Rect rect;
  63.             rect = new Rect((x / 100) * Screen.width,
  64.                 (y / 100) * Screen.height,
  65.                 (width / 100) * Screen.width,
  66.                 (width / height) * ((width / 100) * Screen.width));
  67.             _x = rect.x;
  68.             _y = rect.y;
  69.             _width = rect.width;
  70.             _height = rect.height;
  71.         }
  72.     }
  73.     public RelativeRect(Rect rect, bool LockRatio = false) {
  74.         if (!LockRatio) {
  75.             _x = rect.x;
  76.             _y = rect.y;
  77.             _width = rect.width;
  78.             _height = rect.height;
  79.         } else {
  80.             rect = new Rect((rect.x / 100) * Screen.width,
  81. (rect.y / 100) * Screen.height,
  82. (rect.width / 100) * Screen.width,
  83. (rect.width / rect.height) * ((rect.width / 100) * Screen.width));
  84.             _x = rect.x;
  85.             _y = rect.y;
  86.             _width = rect.width;
  87.             _height = rect.height;
  88.         }
  89.     }
  90.     public static Rect GetExact(float x, float y, float width, float height, bool LockRatio = false, float Offset = 0, bool correctiveMovement = false) {
  91.         Rect rect;
  92.         if (!LockRatio) {
  93.             rect = new Rect((x / 100) * Screen.width,
  94.                 (y / 100) * Screen.height,
  95.                 (width / 100) * Screen.width,
  96.                 (height / 100) * Screen.height);
  97.         } else {
  98.             rect = new Rect((x / 100) * Screen.width,
  99.                 (y / 100) * Screen.height,
  100.                 (width / 100) * Screen.width,
  101.                 (width / height) * ((width / 100) * Screen.width));
  102.             if (correctiveMovement) {
  103.                 if (rect.y + rect.height + Offset > Screen.height) {
  104.                     rect.y -= ((rect.y + rect.height + Offset) - Screen.height);
  105.                 }
  106.             }
  107.         }
  108.         return rect;
  109.     }
  110.     public static Rect GetExact(Rect position, bool LockRatio = false, float Offset = 0, bool correctiveMovement = false) {
  111.         Rect rect;
  112.         if (!LockRatio) {
  113.             rect = new Rect((position.x / 100) * Screen.width,
  114.                 (position.y / 100) * Screen.height,
  115.                 (position.width / 100) * Screen.width,
  116.                 (position.height / 100) * Screen.height);
  117.         } else {
  118.             rect = new Rect((position.x / 100) * Screen.width,
  119.         (position.y / 100) * Screen.height,
  120.         (position.width / 100) * Screen.width,
  121.         (position.width / position.height) * ((position.width / 100) * Screen.width));
  122.             if (correctiveMovement) {
  123.                 if (rect.y + rect.height + Offset > Screen.height) {
  124.                     rect.y -= ((rect.y + rect.height + Offset) - Screen.height);
  125.                 }
  126.             }
  127.         }
  128.         return rect;
  129.     }
  130.     public static Rect GetExact(Rect position, Pin sideToPin, bool LockRatio = false) {
  131.         Rect rect;
  132.         if (!LockRatio) {
  133.             rect = new Rect((position.x / 100) * Screen.width,
  134.                 (position.y / 100) * Screen.height,
  135.                 (position.width / 100) * Screen.width,
  136.                 (position.height / 100) * Screen.height);
  137.         } else {
  138.             rect = new Rect((position.x / 100) * Screen.width,
  139.         (position.y / 100) * Screen.height,
  140.         (position.width / 100) * Screen.width,
  141.         (position.width / position.height) * ((position.width / 100) * Screen.width));
  142.         }
  143.         switch (sideToPin) {
  144.             case Pin.TopLeft:
  145.                 rect.y = 0;
  146.                 rect.x = 0;
  147.                 break;
  148.             case Pin.TopRight:
  149.                 rect.y = 0;
  150.                 rect.x = 0;
  151.                 break;
  152.             case Pin.BottomLeft:
  153.                 rect.y = Screen.height - rect.height;
  154.                 rect.x = 0;
  155.                 break;
  156.             case Pin.BottomRight:
  157.                 rect.y = Screen.height - rect.height;
  158.                 rect.x = Screen.width - rect.width;
  159.                 break;
  160.  
  161.         }
  162.         return rect;
  163.     }
  164.     public static float GetExactWidth(float relativeWidth) {
  165.         return GetExact(0, 0, relativeWidth, 0, false, 0, false).width;
  166.     }
  167.     public static float GetExactHeight(float relativeHeight) {
  168.         return GetExact(0, 0, 0, relativeHeight, false, 0, false).height;
  169.     }
  170.     public static Rect GetExact(Rect rect) {
  171.         rect = new Rect((rect.x / 100) * Screen.width,
  172.             (rect.y / 100) * Screen.height,
  173.             (rect.width / 100) * Screen.width,
  174.             (rect.height / 100) * Screen.height);
  175.         return rect;
  176.     }
  177.     public static float GetRelativeText(float size) {
  178.         return (size / 100f) * Screen.width;
  179.     }
  180. }
Advertisement
Add Comment
Please, Sign In to add comment