scroll_scene.ts 7.8 KB

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