暂无描述
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

SpriteFrameModuleBase.cs 22KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638
  1. using System;
  2. using System.Collections;
  3. using System.Linq;
  4. using System.Collections.Generic;
  5. using UnityEngine;
  6. using UnityEditorInternal;
  7. namespace UnityEditor.U2D.Sprites
  8. {
  9. internal class SpriteRectModel : ScriptableObject, ISerializationCallbackReceiver
  10. {
  11. [Serializable]
  12. struct StringGUID
  13. {
  14. [SerializeField]
  15. string m_StringGUID;
  16. public StringGUID(GUID guid)
  17. {
  18. m_StringGUID = guid.ToString();
  19. }
  20. public static implicit operator GUID(StringGUID d) => new GUID(d.m_StringGUID);
  21. public static implicit operator StringGUID(GUID d) => new StringGUID(d);
  22. }
  23. [Serializable]
  24. class StringGUIDList : List<StringGUID>, IReadOnlyList<GUID>
  25. {
  26. GUID IReadOnlyList<GUID>.this[int index]
  27. {
  28. get => this[index];
  29. }
  30. IEnumerator<GUID> IEnumerable<GUID>.GetEnumerator()
  31. {
  32. // Not used for now
  33. throw new NotImplementedException();
  34. }
  35. }
  36. /// <summary>
  37. /// List of all SpriteRects
  38. /// </summary>
  39. [SerializeField] private List<SpriteRect> m_SpriteRects;
  40. /// <summary>
  41. /// List of all names in the Name-FileId Table
  42. /// </summary>
  43. [SerializeField] private List<string> m_SpriteNames;
  44. /// <summary>
  45. /// List of all FileIds in the Name-FileId Table
  46. /// </summary>
  47. [SerializeField] private StringGUIDList m_SpriteFileIds;
  48. /// <summary>
  49. /// HashSet of all names currently in use by SpriteRects
  50. /// </summary>
  51. private HashSet<string> m_NamesInUse;
  52. private HashSet<GUID> m_InternalIdsInUse;
  53. public IReadOnlyList<SpriteRect> spriteRects => m_SpriteRects;
  54. public IReadOnlyList<string> spriteNames => m_SpriteNames;
  55. public IReadOnlyList<GUID> spriteFileIds => m_SpriteFileIds;
  56. private SpriteRectModel()
  57. {
  58. m_SpriteNames = new List<string>();
  59. m_SpriteFileIds = new StringGUIDList();
  60. Clear();
  61. }
  62. public void SetSpriteRects(List<SpriteRect> newSpriteRects)
  63. {
  64. m_SpriteRects = newSpriteRects;
  65. m_NamesInUse = new HashSet<string>();
  66. m_InternalIdsInUse = new HashSet<GUID>();
  67. for (var i = 0; i < m_SpriteRects.Count; ++i)
  68. {
  69. m_NamesInUse.Add(m_SpriteRects[i].name);
  70. m_InternalIdsInUse.Add(m_SpriteRects[i].spriteID);
  71. }
  72. }
  73. public void SetNameFileIdPairs(IEnumerable<SpriteNameFileIdPair> pairs)
  74. {
  75. m_SpriteNames.Clear();
  76. m_SpriteFileIds.Clear();
  77. foreach (var pair in pairs)
  78. AddNameFileIdPair(pair.name, pair.GetFileGUID());
  79. }
  80. public int FindIndex(Predicate<SpriteRect> match)
  81. {
  82. int i = 0;
  83. foreach (var spriteRect in m_SpriteRects)
  84. {
  85. if (match.Invoke(spriteRect))
  86. return i;
  87. i++;
  88. }
  89. return -1;
  90. }
  91. public void Clear()
  92. {
  93. m_SpriteRects = new List<SpriteRect>();
  94. m_NamesInUse = new HashSet<string>();
  95. m_InternalIdsInUse = new HashSet<GUID>();
  96. }
  97. public bool Add(SpriteRect spriteRect, bool shouldReplaceInTable = false)
  98. {
  99. if (spriteRect.spriteID.Empty())
  100. {
  101. spriteRect.spriteID = GUID.Generate();
  102. }
  103. else
  104. {
  105. if (IsInternalIdInUsed(spriteRect.spriteID))
  106. return false;
  107. }
  108. if (shouldReplaceInTable)
  109. {
  110. // replace id from sprite to file id table
  111. if (!UpdateIdInNameIdPair(spriteRect.name, spriteRect.spriteID))
  112. {
  113. // add it into file id table if update wasn't successful i.e. it doesn't exist yet
  114. AddNameFileIdPair(spriteRect.name, spriteRect.spriteID);
  115. }
  116. }
  117. else
  118. {
  119. // Since we are not replacing the file id table,
  120. // look for any existing id and set it to the SpriteRect
  121. var index = m_SpriteNames.FindIndex(x => x == spriteRect.name);
  122. if (index >= 0)
  123. {
  124. if (IsInternalIdInUsed(m_SpriteFileIds[index]))
  125. return false;
  126. spriteRect.spriteID = m_SpriteFileIds[index];
  127. }
  128. else
  129. AddNameFileIdPair(spriteRect.name, spriteRect.spriteID);
  130. }
  131. m_SpriteRects.Add(spriteRect);
  132. m_NamesInUse.Add(spriteRect.name);
  133. m_InternalIdsInUse.Add(spriteRect.spriteID);
  134. return true;
  135. }
  136. public void Remove(SpriteRect spriteRect)
  137. {
  138. m_SpriteRects.Remove(spriteRect);
  139. m_NamesInUse.Remove(spriteRect.name);
  140. m_InternalIdsInUse.Remove(spriteRect.spriteID);
  141. }
  142. /// <summary>
  143. /// Checks whether or not the name is currently in use by any of the SpriteRects in the texture.
  144. /// </summary>
  145. /// <param name="rectName">The name to check for</param>
  146. /// <returns>True if the name is currently in use</returns>
  147. public bool IsNameUsed(string rectName)
  148. {
  149. return m_NamesInUse.Contains(rectName);
  150. }
  151. /// <summary>
  152. /// Checks whether or not the id is currently in use by any of the SpriteRects in the texture.
  153. /// </summary>
  154. /// <param name="rectName">The id to check for</param>
  155. /// <returns>True if the name is currently in use</returns>
  156. public bool IsInternalIdInUsed(GUID internalId)
  157. {
  158. return m_InternalIdsInUse.Contains(internalId);
  159. }
  160. public List<SpriteRect> GetSpriteRects()
  161. {
  162. return m_SpriteRects;
  163. }
  164. public bool Rename(string oldName, string newName, GUID fileId)
  165. {
  166. if (!IsNameUsed(oldName))
  167. return false;
  168. if (IsNameUsed(newName))
  169. return false;
  170. var index = m_SpriteNames.FindIndex(x => x == oldName);
  171. if (index >= 0)
  172. {
  173. m_SpriteNames.RemoveAt(index);
  174. m_SpriteFileIds.RemoveAt(index);
  175. }
  176. index = m_SpriteNames.FindIndex(x => x == newName);
  177. if (index >= 0)
  178. m_SpriteFileIds[index] = fileId;
  179. else
  180. AddNameFileIdPair(newName, fileId);
  181. m_NamesInUse.Remove(oldName);
  182. m_NamesInUse.Add(newName);
  183. return true;
  184. }
  185. void AddNameFileIdPair(string spriteName, GUID fileId)
  186. {
  187. m_SpriteNames.Add(spriteName);
  188. m_SpriteFileIds.Add(fileId);
  189. }
  190. bool UpdateIdInNameIdPair(string spriteName, GUID newFileId)
  191. {
  192. var index = m_SpriteNames.FindIndex(x => x == spriteName);
  193. if (index >= 0)
  194. {
  195. m_SpriteFileIds[index] = newFileId;
  196. return true;
  197. }
  198. return false;
  199. }
  200. public void ClearUnusedFileID()
  201. {
  202. m_SpriteNames.Clear();
  203. m_SpriteFileIds.Clear();
  204. foreach (var sprite in m_SpriteRects)
  205. {
  206. m_SpriteNames.Add(sprite.name);
  207. m_SpriteFileIds.Add(sprite.spriteID);
  208. }
  209. }
  210. void ISerializationCallbackReceiver.OnBeforeSerialize()
  211. {}
  212. void ISerializationCallbackReceiver.OnAfterDeserialize()
  213. {
  214. SetSpriteRects(m_SpriteRects);
  215. }
  216. }
  217. internal class OutlineSpriteRect : SpriteRect
  218. {
  219. public List<Vector2[]> outlines;
  220. public OutlineSpriteRect(SpriteRect rect)
  221. {
  222. this.name = rect.name;
  223. this.originalName = rect.originalName;
  224. this.pivot = rect.pivot;
  225. this.alignment = rect.alignment;
  226. this.border = rect.border;
  227. this.rect = rect.rect;
  228. this.spriteID = rect.spriteID;
  229. outlines = new List<Vector2[]>();
  230. }
  231. }
  232. internal abstract partial class SpriteFrameModuleBase : SpriteEditorModuleBase
  233. {
  234. protected SpriteRectModel m_RectsCache;
  235. protected ITextureDataProvider m_TextureDataProvider;
  236. protected ISpriteEditorDataProvider m_SpriteDataProvider;
  237. protected ISpriteNameFileIdDataProvider m_NameFileIdDataProvider;
  238. string m_ModuleName;
  239. internal enum PivotUnitMode
  240. {
  241. Normalized,
  242. Pixels
  243. }
  244. private PivotUnitMode m_PivotUnitMode = PivotUnitMode.Normalized;
  245. protected SpriteFrameModuleBase(string name, ISpriteEditor sw, IEventSystem es, IUndoSystem us, IAssetDatabase ad)
  246. {
  247. spriteEditor = sw;
  248. eventSystem = es;
  249. undoSystem = us;
  250. assetDatabase = ad;
  251. m_ModuleName = name;
  252. }
  253. // implements ISpriteEditorModule
  254. public override void OnModuleActivate()
  255. {
  256. spriteImportMode = SpriteFrameModule.GetSpriteImportMode(spriteEditor.GetDataProvider<ISpriteEditorDataProvider>());
  257. m_TextureDataProvider = spriteEditor.GetDataProvider<ITextureDataProvider>();
  258. m_NameFileIdDataProvider = spriteEditor.GetDataProvider<ISpriteNameFileIdDataProvider>();
  259. m_SpriteDataProvider = spriteEditor.GetDataProvider<ISpriteEditorDataProvider>();
  260. int width, height;
  261. m_TextureDataProvider.GetTextureActualWidthAndHeight(out width, out height);
  262. textureActualWidth = width;
  263. textureActualHeight = height;
  264. m_RectsCache = ScriptableObject.CreateInstance<SpriteRectModel>();
  265. m_RectsCache.hideFlags = HideFlags.HideAndDontSave;
  266. var spriteList = m_SpriteDataProvider.GetSpriteRects().ToList();
  267. m_RectsCache.SetSpriteRects(spriteList);
  268. spriteEditor.spriteRects = spriteList;
  269. if (m_NameFileIdDataProvider == null)
  270. m_NameFileIdDataProvider = new DefaultSpriteNameFileIdDataProvider(spriteList);
  271. var nameFileIdPairs = m_NameFileIdDataProvider.GetNameFileIdPairs();
  272. m_RectsCache.SetNameFileIdPairs(nameFileIdPairs);
  273. if (spriteEditor.selectedSpriteRect != null)
  274. spriteEditor.selectedSpriteRect = m_RectsCache.spriteRects.FirstOrDefault(x => x.spriteID == spriteEditor.selectedSpriteRect.spriteID);
  275. AddMainUI(spriteEditor.GetMainVisualContainer());
  276. undoSystem.RegisterUndoCallback(UndoCallback);
  277. }
  278. public override void OnModuleDeactivate()
  279. {
  280. if (m_RectsCache != null)
  281. {
  282. undoSystem.ClearUndo(m_RectsCache);
  283. ScriptableObject.DestroyImmediate(m_RectsCache);
  284. m_RectsCache = null;
  285. }
  286. undoSystem.UnregisterUndoCallback(UndoCallback);
  287. RemoveMainUI(spriteEditor.GetMainVisualContainer());
  288. }
  289. public override bool ApplyRevert(bool apply)
  290. {
  291. if (apply)
  292. {
  293. var array = m_RectsCache != null ? m_RectsCache.spriteRects.ToArray() : null;
  294. m_SpriteDataProvider.SetSpriteRects(array);
  295. var spriteNames = m_RectsCache?.spriteNames;
  296. var spriteFileIds = m_RectsCache?.spriteFileIds;
  297. if (spriteNames != null && spriteFileIds != null)
  298. {
  299. var pairList = new List<SpriteNameFileIdPair>(spriteNames.Count);
  300. for (var i = 0; i < spriteNames.Count; ++i)
  301. pairList.Add(new SpriteNameFileIdPair(spriteNames[i], spriteFileIds[i]));
  302. m_NameFileIdDataProvider.SetNameFileIdPairs(pairList.ToArray());
  303. }
  304. var outlineDataProvider = m_SpriteDataProvider.GetDataProvider<ISpriteOutlineDataProvider>();
  305. var physicsDataProvider = m_SpriteDataProvider.GetDataProvider<ISpritePhysicsOutlineDataProvider>();
  306. foreach (var rect in array)
  307. {
  308. if (rect is OutlineSpriteRect outlineRect)
  309. {
  310. if (outlineRect.outlines.Count > 0)
  311. {
  312. outlineDataProvider.SetOutlines(outlineRect.spriteID, outlineRect.outlines);
  313. physicsDataProvider.SetOutlines(outlineRect.spriteID, outlineRect.outlines);
  314. }
  315. }
  316. }
  317. if (m_RectsCache != null)
  318. undoSystem.ClearUndo(m_RectsCache);
  319. }
  320. else
  321. {
  322. if (m_RectsCache != null)
  323. {
  324. undoSystem.ClearUndo(m_RectsCache);
  325. var spriteList = m_SpriteDataProvider.GetSpriteRects().ToList();
  326. m_RectsCache.SetSpriteRects(spriteList);
  327. var nameFileIdPairs = m_NameFileIdDataProvider.GetNameFileIdPairs();
  328. m_RectsCache.SetNameFileIdPairs(nameFileIdPairs);
  329. spriteEditor.spriteRects = spriteList;
  330. if (spriteEditor.selectedSpriteRect != null)
  331. spriteEditor.selectedSpriteRect = m_RectsCache.spriteRects.FirstOrDefault(x => x.spriteID == spriteEditor.selectedSpriteRect.spriteID);
  332. }
  333. }
  334. return true;
  335. }
  336. public override string moduleName
  337. {
  338. get { return m_ModuleName; }
  339. }
  340. // injected interfaces
  341. protected IEventSystem eventSystem
  342. {
  343. get;
  344. private set;
  345. }
  346. protected IUndoSystem undoSystem
  347. {
  348. get;
  349. private set;
  350. }
  351. protected IAssetDatabase assetDatabase
  352. {
  353. get;
  354. private set;
  355. }
  356. protected SpriteRect selected
  357. {
  358. get { return spriteEditor.selectedSpriteRect; }
  359. set { spriteEditor.selectedSpriteRect = value; }
  360. }
  361. protected SpriteImportMode spriteImportMode
  362. {
  363. get; private set;
  364. }
  365. protected string spriteAssetPath
  366. {
  367. get { return assetDatabase.GetAssetPath(m_TextureDataProvider.texture); }
  368. }
  369. public bool hasSelected
  370. {
  371. get { return spriteEditor.selectedSpriteRect != null; }
  372. }
  373. public SpriteAlignment selectedSpriteAlignment
  374. {
  375. get { return selected.alignment; }
  376. }
  377. public Vector2 selectedSpritePivot
  378. {
  379. get { return selected.pivot; }
  380. }
  381. private Vector2 selectedSpritePivotInCurUnitMode
  382. {
  383. get
  384. {
  385. return m_PivotUnitMode == PivotUnitMode.Pixels
  386. ? ConvertFromNormalizedToRectSpace(selectedSpritePivot, selectedSpriteRect)
  387. : selectedSpritePivot;
  388. }
  389. }
  390. public int CurrentSelectedSpriteIndex()
  391. {
  392. if (m_RectsCache != null && selected != null)
  393. return m_RectsCache.FindIndex(x => x.spriteID == selected.spriteID);
  394. return -1;
  395. }
  396. public Vector4 selectedSpriteBorder
  397. {
  398. get { return ClampSpriteBorderToRect(selected.border, selected.rect); }
  399. set
  400. {
  401. undoSystem.RegisterCompleteObjectUndo(m_RectsCache, "Change Sprite Border");
  402. spriteEditor.SetDataModified();
  403. selected.border = ClampSpriteBorderToRect(value, selected.rect);
  404. }
  405. }
  406. public Rect selectedSpriteRect
  407. {
  408. get { return selected.rect; }
  409. set
  410. {
  411. undoSystem.RegisterCompleteObjectUndo(m_RectsCache, "Change Sprite rect");
  412. spriteEditor.SetDataModified();
  413. selected.rect = ClampSpriteRect(value, textureActualWidth, textureActualHeight);
  414. }
  415. }
  416. public string selectedSpriteName
  417. {
  418. get { return selected.name; }
  419. set
  420. {
  421. if (selected.name == value)
  422. return;
  423. if (m_RectsCache.IsNameUsed(value))
  424. return;
  425. undoSystem.RegisterCompleteObjectUndo(m_RectsCache, "Change Sprite Name");
  426. spriteEditor.SetDataModified();
  427. string oldName = selected.name;
  428. string newName = InternalEditorUtility.RemoveInvalidCharsFromFileName(value, true);
  429. // These can only be changed in sprite multiple mode
  430. if (string.IsNullOrEmpty(selected.originalName) && (newName != oldName))
  431. selected.originalName = oldName;
  432. // Is the name empty?
  433. if (string.IsNullOrEmpty(newName))
  434. newName = oldName;
  435. // Did the rename succeed?
  436. if (m_RectsCache.Rename(oldName, newName, selected.spriteID))
  437. selected.name = newName;
  438. }
  439. }
  440. public int spriteCount
  441. {
  442. get { return m_RectsCache.spriteRects.Count; }
  443. }
  444. public Vector4 GetSpriteBorderAt(int i)
  445. {
  446. return m_RectsCache.spriteRects[i].border;
  447. }
  448. public Rect GetSpriteRectAt(int i)
  449. {
  450. return m_RectsCache.spriteRects[i].rect;
  451. }
  452. public int textureActualWidth { get; private set; }
  453. public int textureActualHeight { get; private set; }
  454. public void SetSpritePivotAndAlignment(Vector2 pivot, SpriteAlignment alignment)
  455. {
  456. undoSystem.RegisterCompleteObjectUndo(m_RectsCache, "Change Sprite Pivot");
  457. spriteEditor.SetDataModified();
  458. selected.alignment = alignment;
  459. selected.pivot = SpriteEditorUtility.GetPivotValue(alignment, pivot);
  460. }
  461. public bool containsMultipleSprites
  462. {
  463. get { return spriteImportMode == SpriteImportMode.Multiple; }
  464. }
  465. protected void SnapPivotToSnapPoints(Vector2 pivot, out Vector2 outPivot, out SpriteAlignment outAlignment)
  466. {
  467. Rect rect = selectedSpriteRect;
  468. // Convert from normalized space to texture space
  469. Vector2 texturePos = new Vector2(rect.xMin + rect.width * pivot.x, rect.yMin + rect.height * pivot.y);
  470. Vector2[] snapPoints = GetSnapPointsArray(rect);
  471. // Snapping is now a firm action, it will always snap to one of the snapping points.
  472. SpriteAlignment snappedAlignment = SpriteAlignment.Custom;
  473. float nearestDistance = float.MaxValue;
  474. for (int alignment = 0; alignment < snapPoints.Length; alignment++)
  475. {
  476. float distance = (texturePos - snapPoints[alignment]).magnitude * m_Zoom;
  477. if (distance < nearestDistance)
  478. {
  479. snappedAlignment = (SpriteAlignment)alignment;
  480. nearestDistance = distance;
  481. }
  482. }
  483. outAlignment = snappedAlignment;
  484. outPivot = ConvertFromTextureToNormalizedSpace(snapPoints[(int)snappedAlignment], rect);
  485. }
  486. protected void SnapPivotToPixels(Vector2 pivot, out Vector2 outPivot, out SpriteAlignment outAlignment)
  487. {
  488. outAlignment = SpriteAlignment.Custom;
  489. Rect rect = selectedSpriteRect;
  490. float unitsPerPixelX = 1.0f / rect.width;
  491. float unitsPerPixelY = 1.0f / rect.height;
  492. outPivot.x = Mathf.Round(pivot.x / unitsPerPixelX) * unitsPerPixelX;
  493. outPivot.y = Mathf.Round(pivot.y / unitsPerPixelY) * unitsPerPixelY;
  494. }
  495. private void UndoCallback()
  496. {
  497. UIUndoCallback();
  498. }
  499. protected static Rect ClampSpriteRect(Rect rect, float maxX, float maxY)
  500. {
  501. // Clamp rect to width height
  502. rect = FlipNegativeRect(rect);
  503. Rect newRect = new Rect();
  504. newRect.xMin = Mathf.Clamp(rect.xMin, 0, maxX - 1);
  505. newRect.yMin = Mathf.Clamp(rect.yMin, 0, maxY - 1);
  506. newRect.xMax = Mathf.Clamp(rect.xMax, 1, maxX);
  507. newRect.yMax = Mathf.Clamp(rect.yMax, 1, maxY);
  508. // Prevent width and height to be 0 value after clamping.
  509. if (Mathf.RoundToInt(newRect.width) == 0)
  510. newRect.width = 1;
  511. if (Mathf.RoundToInt(newRect.height) == 0)
  512. newRect.height = 1;
  513. return SpriteEditorUtility.RoundedRect(newRect);
  514. }
  515. protected static Rect FlipNegativeRect(Rect rect)
  516. {
  517. Rect newRect = new Rect();
  518. newRect.xMin = Mathf.Min(rect.xMin, rect.xMax);
  519. newRect.yMin = Mathf.Min(rect.yMin, rect.yMax);
  520. newRect.xMax = Mathf.Max(rect.xMin, rect.xMax);
  521. newRect.yMax = Mathf.Max(rect.yMin, rect.yMax);
  522. return newRect;
  523. }
  524. protected static Vector4 ClampSpriteBorderToRect(Vector4 border, Rect rect)
  525. {
  526. Rect flipRect = FlipNegativeRect(rect);
  527. float w = flipRect.width;
  528. float h = flipRect.height;
  529. Vector4 newBorder = new Vector4();
  530. // Make sure borders are within the width/height and left < right and top < bottom
  531. newBorder.x = Mathf.RoundToInt(Mathf.Clamp(border.x, 0, Mathf.Min(Mathf.Abs(w - border.z), w))); // Left
  532. newBorder.z = Mathf.RoundToInt(Mathf.Clamp(border.z, 0, Mathf.Min(Mathf.Abs(w - newBorder.x), w))); // Right
  533. newBorder.y = Mathf.RoundToInt(Mathf.Clamp(border.y, 0, Mathf.Min(Mathf.Abs(h - border.w), h))); // Bottom
  534. newBorder.w = Mathf.RoundToInt(Mathf.Clamp(border.w, 0, Mathf.Min(Mathf.Abs(h - newBorder.y), h))); // Top
  535. return newBorder;
  536. }
  537. }
  538. }