AdapterSprite.ts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. import { Enum, Sprite, UITransform, v3, Widget, _decorator } from "cc";
  2. import { EDITOR } from "cc/env";
  3. import { Adapter } from "./Adapter";
  4. const { ccclass, property , executeInEditMode,menu} = _decorator;
  5. /**
  6. * 缩放方式
  7. */
  8. export enum SpriteScaleType {
  9. /**
  10. * 缩放到填满父节点(如果父节点有裁剪,图像可能会被裁剪,节点可能会超出父节点)
  11. */
  12. FILL,
  13. /**
  14. * 缩放到刚好在父节点内部最大化显示(图像会完整显示,但父节点上下或者左右可能会留空)
  15. */
  16. SUIT,
  17. }
  18. /**
  19. * 对齐方式
  20. */
  21. export enum SpriteAlignType {
  22. /**
  23. * 缩放后靠左对齐
  24. */
  25. LEFT,
  26. /**
  27. * 缩放后靠上对齐
  28. */
  29. TOP,
  30. /**
  31. * 缩放后靠右对齐
  32. */
  33. RIGHT,
  34. /**
  35. * 缩放后靠下对齐
  36. */
  37. BOTTOM,
  38. /**
  39. * 缩放后居中对齐
  40. */
  41. CENTER,
  42. }
  43. /**
  44. * Sprite 适配组件
  45. *
  46. * @author caizhitao
  47. * @created 2020-12-27 21:22:43
  48. */
  49. @ccclass("AdapterSprite")
  50. @executeInEditMode(true)
  51. @menu("Quick适配组件/AdapterSprite")
  52. export default class AdapterSprite extends Adapter {
  53. @property({
  54. type: Enum(SpriteScaleType),
  55. tooltip: `缩放类型:
  56. -FILL: 缩放到填满父节点(如果父节点有裁剪,图像可能会被裁剪,节点可能会超出父节点)
  57. -SUIT: 缩放到刚好在父节点内部最大化显示(图像会完整显示,但父节点上下或者左右可能会留空)`,
  58. })
  59. get scaleType(){
  60. return this._scaleType;
  61. }
  62. set scaleType(value){
  63. this._scaleType = value;
  64. if ( EDITOR ){
  65. this.updateSprite(this._scaleType,this.alignType);
  66. }
  67. }
  68. private _scaleType: SpriteScaleType = SpriteScaleType.SUIT;
  69. @property({
  70. type: Enum(SpriteAlignType),
  71. tooltip: `齐方式类型:
  72. -LEFT: 缩放后靠左对齐
  73. -TOP: 缩放后靠上对齐
  74. -RIGHT: 缩放后靠右对齐
  75. -BOTTOM: 缩放后靠下对齐
  76. -CENTER: 缩放后居中对齐`,
  77. })
  78. get alignType(){
  79. return this._alignType;
  80. }
  81. set alignType(value){
  82. this._alignType = value;
  83. if ( EDITOR ){
  84. this.updateSprite(this._scaleType,this._alignType);
  85. }
  86. }
  87. private _alignType: SpriteAlignType = SpriteAlignType.CENTER;
  88. private _sprite: Sprite = null!;
  89. onLoad() {
  90. this._sprite = this.node.getComponent(Sprite) as Sprite;
  91. }
  92. start() {
  93. this.updateSprite(this.scaleType, this.alignType);
  94. }
  95. protected onChangeSize() {
  96. this.updateSprite(this.scaleType, this.alignType);
  97. }
  98. updateSprite(scaleType: SpriteScaleType, alignType: SpriteAlignType) {
  99. if (!this._sprite || !this._sprite.enabled || !this._sprite.spriteFrame) {
  100. return;
  101. }
  102. let widget = this.node.parent?.getComponent(Widget);
  103. if (widget) {
  104. widget.updateAlignment();
  105. }
  106. this.width = this._sprite.spriteFrame.rect.width;
  107. this.height = this._sprite.spriteFrame.rect.height;
  108. let trans = this.parentTrans;
  109. if (this.width / this.height > trans.width / trans.height) {
  110. // 设计分辨率宽高比大于显示分辨率
  111. if (scaleType == SpriteScaleType.SUIT) {
  112. let scale = trans.width / this.width;
  113. this.node.scale = v3(scale,scale);
  114. } else if (scaleType == SpriteScaleType.FILL) {
  115. let scale = trans.height / this.height;
  116. this.node.scale = v3(scale,scale);
  117. }
  118. } else {
  119. // 设计分辨率宽高比小于显示分辨率
  120. if (scaleType == SpriteScaleType.SUIT) {
  121. let scale = trans.height / this.height;
  122. this.node.scale = v3(scale,scale);
  123. } else if (scaleType == SpriteScaleType.FILL) {
  124. let scale = trans.width / this.width;
  125. this.node.scale = v3(scale,scale);
  126. }
  127. }
  128. switch (alignType) {
  129. case SpriteAlignType.CENTER:
  130. this.node.setPosition(v3());
  131. break;
  132. case SpriteAlignType.LEFT:
  133. this.node.setPosition(v3(-0.5 * (trans.width - this.width * this.node.scale.x), 0));
  134. break;
  135. case SpriteAlignType.RIGHT:
  136. this.node.setPosition(v3(0.5 * (trans.width - this.width * this.node.scale.x), 0));
  137. break;
  138. case SpriteAlignType.TOP:
  139. this.node.setPosition(v3(0, 0.5 * (trans.height - this.height * this.node.scale.x)));
  140. break;
  141. case SpriteAlignType.BOTTOM:
  142. this.node.setPosition(v3(0, -0.5 * (trans.height - this.height * this.node.scale.x)));
  143. break;
  144. }
  145. this.doOnAdapterComplete();
  146. }
  147. private get parentTrans(){
  148. return this.node.parent?.getComponent(UITransform) as UITransform
  149. }
  150. }