scroll_scene.ts 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. import { _decorator, assetManager, Component, ImageAsset, Node, ScrollView, Size, Sprite, SpriteFrame, Texture2D, Toggle, UITransform } from 'cc';
  2. import { attributes_data, scene_item_data } from '../../data/data';
  3. import { config } from '../config';
  4. import { ClientEvent } from '../clientEvent';
  5. import { tools } from '../tools';
  6. const { ccclass, property } = _decorator;
  7. @ccclass('scroll_scene')
  8. export class scroll_scene extends Component {
  9. @property(Node) mask_view:Node = null;
  10. @property(Node) mask_view_content:Node = null;
  11. @property(Node) mask_check_node:Node = null;
  12. @property(Node) mask_check:Node = null;
  13. @property(Node) mask:Node = null;
  14. @property(SpriteFrame) mask_sf:SpriteFrame = null;
  15. private m_data:scene_item_data = null;
  16. private m_att:attributes_data = null;
  17. private _bg:Node = null;
  18. private m_call_back = null;
  19. protected start(): void {
  20. if(this._bg===null){
  21. this.setBg(this.node)
  22. }
  23. ClientEvent.on(config.Event.UpdateAttributesToView,this.UpdateAttributesToView,this)
  24. }
  25. UpdateAttributesToView(data:attributes_data,update_type:string){
  26. if(data.id===this.m_att.id){
  27. switch(update_type){
  28. case config.attributes_list_type.pos:
  29. break;
  30. case config.attributes_list_type.animation:
  31. this.m_att.animation_list = data.animation_list;
  32. break;
  33. case config.attributes_list_type.size:
  34. this.m_att.width = data.width;
  35. this.m_att.height = data.height;
  36. this.getBg().getComponent(UITransform).contentSize = new Size(data.width,data.height)
  37. break;
  38. case config.attributes_list_type.rotation:
  39. this.m_att.rotation = data.rotation;
  40. this.getBg().angle = data.rotation;
  41. break;
  42. case config.attributes_list_type.url:
  43. this.m_att.src = data.src
  44. this.m_att.src_name = data.src_name
  45. if(this.m_att.src.length<=0){
  46. this.getBg().getComponent(Sprite).spriteFrame =null;
  47. }else{
  48. assetManager.loadRemote<ImageAsset>(this.m_att.src, (err, imageAsset2)=>{
  49. if (!err && imageAsset2) {
  50. const texture = new Texture2D();
  51. texture.image = imageAsset2;
  52. let spFrame2 = new SpriteFrame();
  53. spFrame2.texture = texture;
  54. this.getBg().getComponent(UITransform).contentSize = new Size(data.width,data.height)
  55. this.getBg().getComponent(Sprite).spriteFrame = spFrame2
  56. }
  57. });
  58. }
  59. break;
  60. }
  61. }
  62. }
  63. protected onDestroy(): void {
  64. ClientEvent.off(config.Event.UpdateAttributesToView,this.UpdateAttributesToView,this)
  65. }
  66. public setBg(bg:Node){
  67. this._bg = bg;
  68. }
  69. public getBg(){
  70. if(this._bg===null){
  71. this.setBg(this.node)
  72. }
  73. return this._bg;
  74. }
  75. public addWidget(node:Node){
  76. node.parent = this.getBg()
  77. }
  78. public initView(is_mask:boolean,att_data:attributes_data=null){
  79. if(att_data!=null){
  80. this.m_att =att_data;
  81. if(this.m_att.src.length>0){
  82. assetManager.loadRemote<ImageAsset>(this.m_att.src, (err, imageAsset2)=>{
  83. if (!err && imageAsset2) {
  84. const texture = new Texture2D();
  85. texture.image = imageAsset2;
  86. let spFrame2 = new SpriteFrame();
  87. spFrame2.texture = texture;
  88. this.getBg().getComponent(UITransform).contentSize = new Size(this.m_data.att.width,this.m_data.att.height)
  89. this.getBg().getComponent(Sprite).spriteFrame = spFrame2
  90. }
  91. });
  92. }
  93. }else{
  94. this.m_att.id = config.getNewId();
  95. }
  96. if(is_mask){
  97. this.mask_check_node.active = true;
  98. this.mask_check.getComponent(Toggle).isChecked = true;
  99. }else{
  100. this.mask_check.getComponent(Toggle).isChecked = false;
  101. this.mask_check_node.active = false;
  102. }
  103. this.mask_check.on(Node.EventType.TOUCH_START,()=>{
  104. this.mask_check.getComponent(Toggle).isChecked=!this.mask_check.getComponent(Toggle).isChecked
  105. this.updatStatus()
  106. })
  107. this.updatStatus()
  108. this.setBg(this.mask_view_content)
  109. }
  110. public initFullView(att_data:attributes_data=null){
  111. if(att_data!=null){
  112. this.m_att =att_data;
  113. if(this.m_att.src.length>0){
  114. assetManager.loadRemote<ImageAsset>(this.m_att.src, (err, imageAsset2)=>{
  115. if (!err && imageAsset2) {
  116. const texture = new Texture2D();
  117. texture.image = imageAsset2;
  118. let spFrame2 = new SpriteFrame();
  119. spFrame2.texture = texture;
  120. this.getBg().getComponent(UITransform).contentSize = new Size(this.m_data.att.width,this.m_data.att.height)
  121. this.getBg().getComponent(Sprite).spriteFrame = spFrame2
  122. }
  123. });
  124. }
  125. }else{
  126. this.m_att.id = config.getNewId();
  127. }
  128. }
  129. public setData(data:scene_item_data){
  130. this.m_data = data;
  131. this.m_data.att = this.m_att;
  132. }
  133. public getData(){
  134. return this.m_data;
  135. }
  136. public setCallBack(call){
  137. this.m_call_back = call;
  138. this.getBg().on(Node.EventType.TOUCH_START,()=>{
  139. if(this.m_call_back!=null){
  140. this.m_call_back()
  141. }
  142. })
  143. }
  144. public getScenePageAtt(){
  145. this.m_att.name = this.m_data.name!=""?this.m_data.name:"场景";
  146. this.m_att.height = this.getBg().getComponent(UITransform).contentSize.height;
  147. this.m_att.width = this.getBg().getComponent(UITransform).contentSize.width;
  148. this.m_att.rotation = this.getBg().angle;
  149. this.m_att.type = config.attributes_type.scene;
  150. this.m_att.src_name = this.m_att.src.length>0? this.m_att.src_name:"空";
  151. return this.m_att;
  152. }
  153. updatStatus(){
  154. if(this.mask_check.getComponent(Toggle).isChecked){
  155. this.mask.active = true;
  156. }else{
  157. this.mask.active = false;
  158. }
  159. }
  160. public stopTouch(){
  161. if(this.mask_view!=null){
  162. this.node.getComponent(ScrollView).enabled = false;
  163. }
  164. }
  165. public startTouch(){
  166. if(this.mask_view!=null){
  167. this.node.getComponent(ScrollView).enabled = true;
  168. }
  169. }
  170. public getContent():Node{
  171. return this.node.getComponent(ScrollView).content
  172. }
  173. getAtt(){
  174. return this.m_att;
  175. }
  176. }