scroll_scene.ts 8.4 KB

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