scroll_scene.ts 7.7 KB

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