scroll_scene.ts 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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.url:
  39. this.m_att.src = data.src
  40. this.m_att.src_name = data.src_name
  41. if(this.m_att.src.length<=0){
  42. this.getBg().getComponent(Sprite).spriteFrame =null;
  43. }else{
  44. assetManager.loadRemote<ImageAsset>(this.m_att.src, (err, imageAsset2)=>{
  45. if (!err && imageAsset2) {
  46. const texture = new Texture2D();
  47. texture.image = imageAsset2;
  48. let spFrame2 = new SpriteFrame();
  49. spFrame2.texture = texture;
  50. this.getBg().getComponent(UITransform).contentSize = new Size(data.width,data.height)
  51. this.getBg().getComponent(Sprite).spriteFrame = spFrame2
  52. }
  53. });
  54. }
  55. break;
  56. }
  57. }
  58. }
  59. protected onDestroy(): void {
  60. ClientEvent.off(config.Event.UpdateAttributesToView,this.UpdateAttributesToView,this)
  61. }
  62. public setBg(bg:Node){
  63. this._bg = bg;
  64. }
  65. public getBg(){
  66. if(this._bg===null){
  67. this.setBg(this.node)
  68. }
  69. return this._bg;
  70. }
  71. public addWidget(node:Node){
  72. node.parent = this.getBg()
  73. }
  74. public initView(is_mask:boolean,att_data:attributes_data=null){
  75. if(att_data!=null){
  76. this.m_att =att_data;
  77. if(this.m_att.src.length>0){
  78. assetManager.loadRemote<ImageAsset>(this.m_att.src, (err, imageAsset2)=>{
  79. if (!err && imageAsset2) {
  80. const texture = new Texture2D();
  81. texture.image = imageAsset2;
  82. let spFrame2 = new SpriteFrame();
  83. spFrame2.texture = texture;
  84. this.getBg().getComponent(UITransform).contentSize = new Size(this.m_data.att.width,this.m_data.att.height)
  85. this.getBg().getComponent(Sprite).spriteFrame = spFrame2
  86. }
  87. });
  88. }
  89. }else{
  90. this.m_att.id = config.getNewId();
  91. }
  92. if(is_mask){
  93. this.mask_check_node.active = true;
  94. this.mask_check.getComponent(Toggle).isChecked = true;
  95. }else{
  96. this.mask_check.getComponent(Toggle).isChecked = false;
  97. this.mask_check_node.active = false;
  98. }
  99. this.mask_check.on(Node.EventType.TOUCH_START,()=>{
  100. this.mask_check.getComponent(Toggle).isChecked=!this.mask_check.getComponent(Toggle).isChecked
  101. this.updatStatus()
  102. })
  103. this.updatStatus()
  104. this.setBg(this.mask_view_content)
  105. }
  106. public initFullView(att_data:attributes_data=null){
  107. if(att_data!=null){
  108. this.m_att =att_data;
  109. if(this.m_att.src.length>0){
  110. assetManager.loadRemote<ImageAsset>(this.m_att.src, (err, imageAsset2)=>{
  111. if (!err && imageAsset2) {
  112. const texture = new Texture2D();
  113. texture.image = imageAsset2;
  114. let spFrame2 = new SpriteFrame();
  115. spFrame2.texture = texture;
  116. this.getBg().getComponent(UITransform).contentSize = new Size(this.m_data.att.width,this.m_data.att.height)
  117. this.getBg().getComponent(Sprite).spriteFrame = spFrame2
  118. }
  119. });
  120. }
  121. }else{
  122. this.m_att.id = config.getNewId();
  123. }
  124. }
  125. public setData(data:scene_item_data){
  126. this.m_data = data;
  127. this.m_data.att = this.m_att;
  128. }
  129. public getData(){
  130. return this.m_data;
  131. }
  132. public setCallBack(call){
  133. this.m_call_back = call;
  134. this.getBg().on(Node.EventType.TOUCH_START,()=>{
  135. if(this.m_call_back!=null){
  136. this.m_call_back()
  137. }
  138. })
  139. }
  140. public getScenePageAtt(){
  141. this.m_att.name = this.m_data.name!=""?this.m_data.name:"场景";
  142. this.m_att.height = this.getBg().getComponent(UITransform).contentSize.height;
  143. this.m_att.width = this.getBg().getComponent(UITransform).contentSize.width;
  144. this.m_att.type = config.attributes_type.scene;
  145. this.m_att.src_name = this.m_att.src.length>0? this.m_att.src_name:"空";
  146. return this.m_att;
  147. }
  148. updatStatus(){
  149. if(this.mask_check.getComponent(Toggle).isChecked){
  150. this.mask.active = true;
  151. }else{
  152. this.mask.active = false;
  153. }
  154. }
  155. public stopTouch(){
  156. if(this.mask_view!=null){
  157. this.node.getComponent(ScrollView).enabled = false;
  158. }
  159. }
  160. public startTouch(){
  161. if(this.mask_view!=null){
  162. this.node.getComponent(ScrollView).enabled = true;
  163. }
  164. }
  165. public getContent():Node{
  166. return this.node.getComponent(ScrollView).content
  167. }
  168. getAtt(){
  169. return this.m_att;
  170. }
  171. }