widget_item.ts 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744
  1. import { _decorator, assetManager, Color, Component, Director, director, EventTouch, ImageAsset, instantiate, Label, math, Node, NodePool, Prefab, Size, Sprite, SpriteFrame, Texture2D, Toggle, Tween, tween, UITransform, Vec2, Vec3 } from 'cc';
  2. import { att_click_data, att_container, att_count_down, att_drag_data, att_question_select, att_slide_data, att_text_sound_data, att_top_data, attributes_data, widget_item_data } from '../../data/data';
  3. import { ClientEvent } from '../clientEvent';
  4. import { config } from '../config';
  5. import { scroll_scene } from './scroll_scene';
  6. import { slide } from './slide';
  7. import { ui_top } from './uiWidget/ui_top';
  8. import { tools } from '../tools';
  9. import { Text_Sound } from './uiWidget/Text_Sound';
  10. import { question_select } from './uiWidget/question_select';
  11. import { count_down } from './uiWidget/count_down';
  12. const { ccclass, property } = _decorator;
  13. @ccclass('widget_item')
  14. export class widget_item extends Component {
  15. @property(Node) lab_name:Node = null;
  16. @property(Node) spr:Node = null;
  17. @property(Node) slide:Node = null;
  18. @property(Node) drag:Node = null;
  19. @property(Node) lab_drag:Node = null;
  20. @property(Node) img_select:Node = null;
  21. @property(Prefab) ui_prefab:Prefab = null;
  22. @property(Prefab) text_sound_pf:Prefab = null;
  23. @property(Prefab) count_down_pf:Prefab = null;
  24. @property(Prefab) question_select_pf:Prefab = null;
  25. @property(Prefab) self_pf:Prefab = null;
  26. @property(Node) toggle_active:Node = null;
  27. @property(Node) toggle_check:Node = null;
  28. @property(Node) other_drag_content:Node = null;
  29. @property(Node) lab_remark:Node = null;
  30. @property(Node) btn_look_voice_text:Node = null;
  31. @property(Node) btn_delete:Node = null;
  32. @property(Node) container:Node = null;
  33. private m_data:widget_item_data = null;
  34. private m_att:attributes_data = null;
  35. private isMove:boolean = false;
  36. private call_back = null;
  37. private m_ui_widget:Node = null;
  38. private m_text_sound:Node = null;
  39. private m_question_select:Node = null;
  40. private m_count_down:Node = null;
  41. private x_len:number = 0;
  42. private y_len:number = 0;
  43. private m_isShowDragOtherList:boolean = false;
  44. private m_isShowContainerChilden:boolean = true;
  45. private m_delete_cb = null;
  46. protected start(): void {
  47. this.btn_delete.on(Node.EventType.TOUCH_END, ()=>{
  48. this.m_delete_cb && this.m_delete_cb(this)
  49. })
  50. }
  51. public showPageNumberName() {
  52. // console.log('showPageNumberName lab_name=',this.lab_name.getComponent(Label).string)
  53. if(this.m_data.scene_type>0) {
  54. if(this.m_data.is_container_layer) {
  55. let name = `第${this.m_data.scene_page_number}页-容器层-${this.lab_name.getComponent(Label).string}`
  56. this.lab_name.getComponent(Label).string = name
  57. } else {
  58. let name = `第${this.m_data.scene_page_number}页-${this.lab_name.getComponent(Label).string}`
  59. this.lab_name.getComponent(Label).string = name
  60. }
  61. }
  62. }
  63. public initWidgetAddCurEditSecne(timeout:number=0) {
  64. setTimeout(()=>{
  65. this.updateLabNamePosition()
  66. this.lab_name.getComponent(Label).string = this.m_data.name + (this.m_data.att?`id:${this.m_data.att.id}`:"");
  67. },timeout)
  68. }
  69. public initWidgetType() {
  70. this.lab_name.setPosition(0,0,0)
  71. }
  72. public hideContainerChilden(){
  73. this.m_isShowContainerChilden = false
  74. }
  75. public hideDragOtherList() {
  76. // 点击->控件列表,不显示拖拽其他按钮
  77. this.hideContainerChilden()
  78. this.m_isShowDragOtherList = true
  79. }
  80. public initView(data:widget_item_data,call=null,cur_select_id:number=-1){
  81. this.m_data = data;
  82. this.lab_name.getComponent(Label).string = data.name + (this.m_data.att?`id:${this.m_data.att.id}`:"");
  83. this.lab_remark.getComponent(Label).string = ''
  84. if(data.att!=null){
  85. this.lab_remark.getComponent(Label).string = this.m_data.att.remark
  86. if(this.m_data.type==config.Widget_Type_List.TEXT_SOUND) {
  87. this.btn_look_voice_text.active = true
  88. this.btn_look_voice_text.off(Node.EventType.TOUCH_END)
  89. this.btn_look_voice_text.on(Node.EventType.TOUCH_END, ()=> {
  90. tools.show_dialog(this.m_data.att.text_sound_data.text, null)
  91. },this)
  92. } else {
  93. this.btn_look_voice_text.active = false
  94. }
  95. this.call_back = call
  96. if(cur_select_id===null){
  97. this.toggle_active.active = true;
  98. this.toggle_check.getComponent(Toggle).isChecked = this.m_data.att.edit_active
  99. this.toggle_check.on(Node.EventType.TOUCH_END,()=>{
  100. if(this.call_back!=null){
  101. this.m_data.att.edit_active = !this.m_data.att.edit_active;
  102. this.call_back(this.m_data)
  103. }
  104. })
  105. }else{
  106. this.node.on(Node.EventType.TOUCH_END,()=>{
  107. if(this.call_back!=null){
  108. this.call_back(this.m_data)
  109. }
  110. })
  111. if(cur_select_id===this.m_data.att.id){
  112. this.img_select.active = true;
  113. }
  114. }
  115. this.initWidgetHaveAtt(data.att)
  116. this.node.active = true
  117. }else{
  118. this.node.on(Node.EventType.MOUSE_DOWN,this.onDragWidget.bind(this),this)
  119. }
  120. }
  121. public showButtonDelete(is_show:boolean, delete_cb:Function) {
  122. this.btn_delete.active = is_show
  123. if(is_show) {
  124. this.m_delete_cb = delete_cb
  125. }
  126. }
  127. public getShowWidgetData(){
  128. return this.m_data;
  129. }
  130. public setSelectStatus(){
  131. if(this.call_back!=null){
  132. this.m_data.att.edit_active = true;
  133. this.toggle_check.getComponent(Toggle).isChecked = true
  134. this.call_back(this.m_data)
  135. }
  136. }
  137. public setUnSelectStatus(){
  138. if(this.call_back!=null){
  139. this.m_data.att.edit_active = false;
  140. this.toggle_check.getComponent(Toggle).isChecked = false
  141. this.call_back(this.m_data)
  142. }
  143. }
  144. onDragWidget(){
  145. ClientEvent.dispatchEvent(config.Event.DragWidget,this.node)
  146. }
  147. initDrag(){
  148. this.drag.on(Node.EventType.TOUCH_MOVE,(et:EventTouch)=>{
  149. let p = new Vec3(et.getUILocation().x,et.getUILocation().y)
  150. let n_p = this.node.getComponent(UITransform).convertToNodeSpaceAR(p)
  151. this.drag.position = n_p;
  152. this.m_att.drag_data.drag_pos_x = this.drag.position.x;
  153. this.m_att.drag_data.drag_pos_y = this.drag.position.y;
  154. })
  155. if(this.m_att.drag_data.other_drag_list==undefined){
  156. this.m_att.drag_data.other_drag_list = []
  157. }
  158. // 刷新 其他拖拽
  159. this.other_drag_content.removeAllChildren()
  160. for (let index = 0; index < this.m_att.drag_data.other_drag_list.length; index++) {
  161. const element:att_drag_data = this.m_att.drag_data.other_drag_list[index];
  162. let item = instantiate(this.drag)
  163. item.off(Node.EventType.TOUCH_MOVE)
  164. item.parent = this.other_drag_content;
  165. item.position = new Vec3(element.drag_pos_x,element.drag_pos_y)
  166. item.getComponent(UITransform).setContentSize(new Size(element.drag_size_width,element.drag_size_height))
  167. item.on(Node.EventType.TOUCH_MOVE,(et:EventTouch)=>{
  168. let p = new Vec3(et.getUILocation().x,et.getUILocation().y)
  169. let n_p = this.node.getComponent(UITransform).convertToNodeSpaceAR(p)
  170. item.position = n_p;
  171. this.m_att.drag_data.other_drag_list[index].drag_pos_x = item.position.x;
  172. this.m_att.drag_data.other_drag_list[index].drag_pos_y = item.position.y;
  173. })
  174. item.getComponent(Sprite).color = this.getOtherDragListColor(index) //Color.YELLOW
  175. item.getChildByName('lab_drag_remark').getComponent(Label).string = element.remark
  176. if(element.is_show_inTheEditor==undefined) {
  177. element.is_show_inTheEditor = true
  178. }
  179. item.active = element.is_show_inTheEditor
  180. }
  181. this.lab_drag.getComponent(Label).string = `${this.m_data.name}id:${this.m_att.id}`
  182. }
  183. initSlide(){
  184. this.slide.getComponent(slide).updateDistance(this.m_att.slide_data.slide_dir,this.m_att.slide_data.slide_distance)
  185. }
  186. initUiWidget(){
  187. if(this.call_back==null){
  188. let ui = instantiate(this.ui_prefab)
  189. ui.parent = this.node;
  190. ui.getComponent(ui_top).initView(this.m_att.top_data)
  191. this.m_ui_widget = ui;
  192. this.node.getComponent(UITransform).contentSize = new Size(1080,1020)
  193. }
  194. }
  195. initTextSound(){
  196. if(this.call_back==null){
  197. let text_sound = instantiate(this.text_sound_pf)
  198. text_sound.parent = this.node;
  199. text_sound.getComponent(Text_Sound).initView(this.m_att.text_sound_data)
  200. this.m_text_sound = text_sound;
  201. this.node.getComponent(UITransform).contentSize = new Size(1080,1020)
  202. }
  203. }
  204. initQuestionSelect(){
  205. if(this.call_back==null){
  206. let node_question_select = instantiate(this.question_select_pf)
  207. node_question_select.parent = this.node;
  208. node_question_select.getComponent(question_select).initView(this.m_att.question_select)
  209. this.m_question_select = node_question_select;
  210. this.node.getComponent(UITransform).contentSize = new Size(1080,1020)
  211. }
  212. }
  213. initConatiner(){
  214. let container_layer = this.m_att.container_layer
  215. this.container.removeAllChildren()
  216. if(container_layer!=null){
  217. for (let index = 0; index < container_layer.widget_list.length; index++) {
  218. const widget_data = container_layer.widget_list[index];
  219. // console.log("widget_data",widget_data)
  220. let node = instantiate(this.self_pf)
  221. node.getComponent(widget_item).initWidgetByScene(widget_data,widget_data.att)
  222. node.parent = this.container
  223. }
  224. }
  225. }
  226. initCountDown(){
  227. if(this.call_back==null){
  228. let _time_count = instantiate(this.count_down_pf)
  229. _time_count.parent = this.node;
  230. _time_count.getComponent(count_down).initView(this.m_att.count_down)
  231. this.m_count_down = _time_count;
  232. this.node.getComponent(UITransform).contentSize = new Size(1080,1020)
  233. }
  234. }
  235. initWidgetHaveAtt(att:attributes_data){
  236. if(att!=null){
  237. this.lab_name.active = true;
  238. this.m_att = att;
  239. this.initWidgetAtt()
  240. }else{
  241. this.m_att = new attributes_data
  242. this.m_att.id = config.getNewId();
  243. }
  244. if(this.m_data.type===config.Widget_Type_List.CLICK_TYPE){
  245. if(this.m_att.click_data===null){
  246. this.m_att.click_data = new att_click_data;
  247. }
  248. this.lab_name.getComponent(Label).string = config.clcik_type_map.get(this.m_att.click_data.click_type) +`id:${this.m_att.id}`
  249. }else if(this.m_data.type===config.Widget_Type_List.SLIDE_TYPE){
  250. this.slide.active = true;
  251. if(this.m_att.slide_data===null){
  252. this.m_att.slide_data = new att_slide_data;
  253. }
  254. this.initSlide()
  255. this.lab_name.getComponent(Label).string = `滑动id:${this.m_att.id}`
  256. }else if(this.m_data.type===config.Widget_Type_List.DRAG_TYPE){
  257. if(this.m_att.drag_data===null){
  258. this.m_att.drag_data = new att_drag_data;
  259. }
  260. this.drag.active = true;
  261. if(this.m_isShowDragOtherList == false) {
  262. this.initDrag()
  263. }
  264. this.lab_name.getComponent(Label).string = `拖拽id:${this.m_att.id}`
  265. }else if(this.m_data.type===config.Widget_Type_List.UI_TOP){
  266. if(this.m_att.top_data===null){
  267. this.m_att.top_data = new att_top_data;
  268. }
  269. this.initUiWidget()
  270. this.lab_name.getComponent(Label).string = `弹窗:${config.top_view_type_map.get(this.m_att.top_data.top_ui_type)}:id:${this.m_att.id}`
  271. }else if(this.m_data.type==config.Widget_Type_List.IMG_TYPE) {
  272. this.lab_name.getComponent(Label).string = `图片id:${this.m_att.id}`
  273. }else if(this.m_data.type==config.Widget_Type_List.ACTION_TYPE) {
  274. this.lab_name.getComponent(Label).string = `动画id:${this.m_att.id}`
  275. }else if(this.m_data.type===config.Widget_Type_List.TEXT_SOUND){
  276. if(this.m_att.text_sound_data===null){
  277. this.m_att.text_sound_data = new att_text_sound_data;
  278. }
  279. this.lab_name.getComponent(Label).string =`文本和语音id:${this.m_att.id}`
  280. this.initTextSound()
  281. }else if(this.m_data.type===config.Widget_Type_List.QUESTION_SELECT){
  282. if(this.m_att.question_select===null){
  283. this.m_att.question_select = new att_question_select;
  284. }
  285. this.initQuestionSelect()
  286. this.lab_name.getComponent(Label).string =`问题选择id:${this.m_att.id}`
  287. }else if(this.m_data.type===config.Widget_Type_List.COUNT_DOWN){
  288. if(this.m_att.count_down===null){
  289. this.m_att.count_down = new att_count_down;
  290. }
  291. this.initCountDown()
  292. this.lab_name.getComponent(Label).string =`倒计时id:${this.m_att.id}`
  293. }else if(this.m_data.type===config.Widget_Type_List.CONTAINER_LAYER){
  294. if(this.m_att.container_layer===null){
  295. this.m_att.container_layer = new att_container;
  296. }
  297. if(this.m_isShowContainerChilden){
  298. this.initConatiner()
  299. }
  300. this.lab_name.getComponent(Label).string =`容器层id:${this.m_att.id}`
  301. }
  302. this.node.active = this.m_att.edit_active
  303. }
  304. public initWidgetByScene(data:widget_item_data,att:attributes_data=null){
  305. this.m_data = data;
  306. this.initWidgetHaveAtt(att)
  307. if(this.m_data.type===config.Widget_Type_List.UI_TOP){ //弹窗不可以拖动
  308. // MAC电脑浏览器
  309. if(config.is_MAC_edit) {
  310. this.node.on(Node.EventType.TOUCH_END,(et:EventTouch)=>{
  311. ClientEvent.dispatchEvent(config.Event.UpdateAttributes,this.getWidgetAtt())
  312. })
  313. return
  314. }
  315. this.node.on(Node.EventType.MOUSE_DOWN,(et:EventTouch)=>{
  316. ClientEvent.dispatchEvent(config.Event.UpdateAttributes,this.getWidgetAtt())
  317. })
  318. }else if(this.m_data.type===config.Widget_Type_List.TEXT_SOUND){
  319. // MAC电脑浏览器
  320. if(config.is_MAC_edit) {
  321. this.node.on(Node.EventType.TOUCH_END,(et:EventTouch)=>{
  322. ClientEvent.dispatchEvent(config.Event.UpdateAttributes,this.getWidgetAtt())
  323. })
  324. return
  325. }
  326. this.node.on(Node.EventType.MOUSE_DOWN,(et:EventTouch)=>{
  327. ClientEvent.dispatchEvent(config.Event.UpdateAttributes,this.getWidgetAtt())
  328. })
  329. }
  330. else if(this.m_data.type==config.Widget_Type_List.CONTAINER_LAYER){
  331. // MAC电脑浏览器
  332. if(config.is_MAC_edit) {
  333. this.node.on(Node.EventType.TOUCH_END,(et:EventTouch)=>{
  334. ClientEvent.dispatchEvent(config.Event.UpdateAttributes,this.getWidgetAtt())
  335. },this,true)
  336. return
  337. }
  338. this.node.on(Node.EventType.MOUSE_DOWN,(et:EventTouch)=>{
  339. ClientEvent.dispatchEvent(config.Event.UpdateAttributes,this.getWidgetAtt())
  340. },this,true)
  341. }
  342. else{
  343. this.node.on(Node.EventType.TOUCH_MOVE,(et:EventTouch)=>{
  344. if(this.isMove){
  345. let p = new Vec3(et.getUILocation().x,et.getUILocation().y)
  346. let n_p = this.node.parent.getComponent(UITransform).convertToNodeSpaceAR(p)
  347. let offset_x = this.x_len;
  348. let offset_y = this.y_len;
  349. this.node.position = new Vec3(n_p.x-offset_x,n_p.y-offset_y);
  350. if(this.node.parent.name=="content"){
  351. this.node.parent.parent.parent.getComponent(scroll_scene).stopTouch()
  352. }
  353. ClientEvent.dispatchEvent(config.Event.UpdateAttributes,this.getWidgetAtt())
  354. }
  355. })
  356. if(!config.is_MAC_edit) {
  357. this.node.on(Node.EventType.MOUSE_DOWN,(et:EventTouch)=>{
  358. et.propagationStopped = true;
  359. let pos = this.node.parent.getComponent(UITransform).convertToWorldSpaceAR(this.node.position)
  360. if(this.x_len==0){
  361. this.x_len = (et.getUILocation().x - pos.x)*2;
  362. this.y_len = (et.getUILocation().y - pos.y)*2;
  363. let p = new Vec3(et.getUILocation().x,et.getUILocation().y)
  364. let n_p = this.node.parent.getComponent(UITransform).convertToNodeSpaceAR(p)
  365. this.node.position = new Vec3(n_p.x-this.x_len,n_p.y-this.y_len);
  366. // console.log("a_x",this.x_len, this.y_len ,et.getUILocation().x,this.m_att.width)
  367. }
  368. // console.log("this.x_len",this.x_len,this.y_len)
  369. this.isMove = true;
  370. ClientEvent.dispatchEvent(config.Event.UpdateAttributes,this.getWidgetAtt())
  371. })
  372. }
  373. this.node.on(Node.EventType.TOUCH_END,(et:EventTouch)=>{
  374. if(this.node.parent.name=="content"){
  375. this.node.parent.parent.parent.getComponent(scroll_scene).startTouch()
  376. }
  377. this.isMove = false;
  378. this.x_len = 0;
  379. this.y_len = 0;
  380. // MAC电脑浏览器
  381. if(config.is_MAC_edit) {
  382. let pos = this.node.parent.getComponent(UITransform).convertToWorldSpaceAR(this.node.position)
  383. if(this.x_len==0){
  384. this.x_len = (et.getUILocation().x - pos.x)*2;
  385. this.y_len = (et.getUILocation().y - pos.y)*2;
  386. let p = new Vec3(et.getUILocation().x,et.getUILocation().y)
  387. let n_p = this.node.parent.getComponent(UITransform).convertToNodeSpaceAR(p)
  388. this.node.position = new Vec3(n_p.x-this.x_len,n_p.y-this.y_len);
  389. // console.log("a_x",this.x_len, this.y_len ,et.getUILocation().x,this.m_att.width)
  390. }
  391. // console.log("this.x_len",this.x_len,this.y_len)
  392. this.isMove = true;
  393. ClientEvent.dispatchEvent(config.Event.UpdateAttributes,this.getWidgetAtt())
  394. }
  395. })
  396. this.node.on(Node.EventType.MOUSE_LEAVE,()=>{
  397. if(this.node.parent.name=="content"){
  398. this.node.parent.parent.parent.getComponent(scroll_scene).startTouch()
  399. }
  400. this.isMove = false;
  401. this.x_len = 0;
  402. this.y_len = 0;
  403. })
  404. }
  405. ClientEvent.dispatchEvent(config.Event.UpdateAttributes,this.getWidgetAtt())
  406. ClientEvent.on(config.Event.UpdateAttributesToView,this.UpdateAttributesToView,this)
  407. }
  408. protected onDestroy(): void {
  409. ClientEvent.off(config.Event.UpdateAttributesToView,this.UpdateAttributesToView,this)
  410. }
  411. UpdateAttributesToView(data:attributes_data,update_type:string){
  412. if(this.m_att===null){
  413. return;
  414. }
  415. if(data.id===this.m_att.id){
  416. // console.log("this.m_att",this.m_att)
  417. switch(update_type){
  418. case config.attributes_list_type.pos:
  419. this.m_att.x = data.x;
  420. this.m_att.y = data.y;
  421. this.m_att.z = data.z;
  422. this.node.setSiblingIndex(this.m_att.z)
  423. this.node.position = new Vec3(data.x,data.y)
  424. break;
  425. case config.attributes_list_type.animation:
  426. this.m_att.animation_list = data.animation_list;
  427. break;
  428. case config.attributes_list_type.size:
  429. this.m_att.width = data.width;
  430. this.m_att.height = data.height;
  431. this.spr.getComponent(UITransform).contentSize = new Size(data.width,data.height)
  432. this.node.getComponent(UITransform).contentSize = this.spr.getComponent(UITransform).contentSize
  433. this.updateLabNamePosition()
  434. break;
  435. case config.attributes_list_type.anchor:
  436. this.m_att.anchor_x = data.anchor_x;
  437. this.m_att.anchor_y = data.anchor_y;
  438. this.spr.getComponent(UITransform).setAnchorPoint(data.anchor_x, data.anchor_y)
  439. break;
  440. case config.attributes_list_type.rotation:
  441. this.m_att.rotation = data.rotation;
  442. this.spr.angle = data.rotation;
  443. break;
  444. case config.attributes_list_type.url:
  445. this.m_att.src = data.src
  446. this.m_att.src_name = data.src_name
  447. if(this.m_att.src.length<=0){
  448. this.spr.getComponent(Sprite).spriteFrame = null
  449. }else{
  450. assetManager.loadRemote<ImageAsset>(this.m_att.src, (err, imageAsset2)=>{
  451. if (!err && imageAsset2) {
  452. // this.lab_name.active = false;
  453. const texture = new Texture2D();
  454. texture.image = imageAsset2;
  455. let spFrame2 = new SpriteFrame();
  456. spFrame2.texture = texture;
  457. this.spr.getComponent(Sprite).spriteFrame = spFrame2
  458. this.spr.getComponent(UITransform).contentSize = new Size(data.width,data.height)
  459. this.updateLabNamePosition()
  460. director.once(Director.EVENT_AFTER_DRAW,()=>{
  461. this.node.getComponent(UITransform).contentSize = this.spr.getComponent(UITransform).contentSize
  462. })
  463. }
  464. });
  465. }
  466. break;
  467. case config.attributes_list_type.origin:
  468. if(this.m_att.src.length>0){
  469. this.spr.getComponent(UITransform).setContentSize(new Size(this.m_att.width,this.m_att.height))
  470. this.node.getComponent(UITransform).contentSize = this.spr.getComponent(UITransform).contentSize
  471. this.updateLabNamePosition()
  472. ClientEvent.dispatchEvent(config.Event.UpdateAttributes,this.getWidgetAtt())
  473. // this.spr.getComponent(Sprite).sizeMode = Sprite.SizeMode.RAW;
  474. // director.once(Director.EVENT_AFTER_DRAW,()=>{
  475. // console.log("Director.EVENT_AFTER_DRAW")
  476. // this.m_att.width = this.spr.getComponent(UITransform).contentSize.width;
  477. // this.m_att.height = this.spr.getComponent(UITransform).contentSize.height;
  478. // director.once(Director.EVENT_AFTER_DRAW,()=>{
  479. // this.node.getComponent(UITransform).contentSize = this.spr.getComponent(UITransform).contentSize
  480. // })
  481. // ClientEvent.dispatchEvent(config.Event.UpdateAttributes,this.getWidgetAtt())
  482. // })
  483. }
  484. break;
  485. case config.attributes_list_type.delete:
  486. this.node.destroy()
  487. this.node.removeFromParent()
  488. ClientEvent.dispatchEvent(config.Event.UpdateAttributes,null)
  489. ClientEvent.dispatchEvent(config.Event.RemoveWidget,this.node)
  490. break;
  491. case config.attributes_list_type.drag:
  492. this.m_att.drag_data = data.drag_data
  493. if(this.m_data.type===config.Widget_Type_List.DRAG_TYPE){
  494. this.drag.getComponent(UITransform).contentSize = new Size(data.drag_data.drag_size_width,data.drag_data.drag_size_height);
  495. this.drag.position = new Vec3( this.m_att.drag_data.drag_pos_x , this.m_att.drag_data.drag_pos_y)
  496. if(this.m_att.drag_data.other_drag_list==undefined){
  497. this.m_att.drag_data.other_drag_list = []
  498. }
  499. // 更新 其他拖拽
  500. this.other_drag_content.removeAllChildren()
  501. for (let index = 0; index < this.m_att.drag_data.other_drag_list.length; index++) {
  502. const element = this.m_att.drag_data.other_drag_list[index];
  503. let item = instantiate(this.drag)
  504. item.off(Node.EventType.TOUCH_MOVE)
  505. item.parent = this.other_drag_content;
  506. item.position = new Vec3(element.drag_pos_x,element.drag_pos_y)
  507. item.getComponent(UITransform).setContentSize(new Size(element.drag_size_width,element.drag_size_height))
  508. item.on(Node.EventType.TOUCH_MOVE,(et:EventTouch)=>{
  509. let p = new Vec3(et.getUILocation().x,et.getUILocation().y)
  510. let n_p = this.node.getComponent(UITransform).convertToNodeSpaceAR(p)
  511. item.position = n_p;
  512. this.m_att.drag_data.other_drag_list[index].drag_pos_x = item.position.x;
  513. this.m_att.drag_data.other_drag_list[index].drag_pos_y = item.position.y;
  514. })
  515. item.getComponent(Sprite).color = this.getOtherDragListColor(index) //Color.YELLOW
  516. item.getChildByName('lab_drag_remark').getComponent(Label).string = element.remark
  517. item.active = element.is_show_inTheEditor
  518. }
  519. }
  520. break;
  521. case config.attributes_list_type.drag_err_back_status:
  522. if(this.m_data.type===config.Widget_Type_List.DRAG_TYPE){
  523. this.m_att.drag_data.is_err_drag_back = data.drag_data.is_err_drag_back;
  524. }
  525. break;
  526. case config.attributes_list_type.slide:
  527. if(this.m_data.type===config.Widget_Type_List.SLIDE_TYPE){
  528. this.m_att.slide_data.slide_dir = data.slide_data.slide_dir;
  529. this.m_att.slide_data.slide_distance = data.slide_data.slide_distance;
  530. this.m_att.slide_data.slide_num = data.slide_data.slide_num;
  531. this.slide.getComponent(slide).updateDistance(this.m_att.slide_data.slide_dir,this.m_att.slide_data.slide_distance)
  532. }
  533. break;
  534. case config.attributes_list_type.click:
  535. if(this.m_data.type===config.Widget_Type_List.CLICK_TYPE){
  536. this.m_att.click_data = data.click_data;
  537. this.lab_name.getComponent(Label).string = config.clcik_type_map.get(this.m_att.click_data.click_type)
  538. }
  539. case config.attributes_list_type.is_interaction:
  540. this.m_att.is_interaction = data.is_interaction;
  541. break;
  542. case config.attributes_list_type.find_widget_pos:
  543. this.Shiny()
  544. break;
  545. case config.attributes_list_type.active:
  546. this.m_att.edit_active = data.edit_active;
  547. this.node.active = this.m_att.edit_active
  548. this.toggle_check.getComponent(Toggle).isChecked = this.m_att.edit_active
  549. break;
  550. case config.attributes_list_type.top:
  551. this.m_att.top_data = data.top_data;
  552. if(this.m_ui_widget!=null){
  553. this.m_ui_widget.getComponent(ui_top).changeData(this.m_att.top_data)
  554. }
  555. break;
  556. case config.attributes_list_type.text_sound:
  557. this.m_att.text_sound_data = data.text_sound_data;
  558. if(this.m_text_sound!=null){
  559. this.m_text_sound.getComponent(Text_Sound).updateView(this.m_att.text_sound_data)
  560. }
  561. break;
  562. case config.attributes_list_type.question_select:
  563. this.m_att.question_select = data.question_select;
  564. if(this.m_question_select!=null){
  565. this.m_question_select.getComponent(question_select).updateView(this.m_att.question_select)
  566. }
  567. break;
  568. case config.attributes_list_type.count_down:
  569. this.m_att.count_down = data.count_down;
  570. if(this.m_count_down!=null){
  571. this.m_count_down.getComponent(count_down).updateView(this.m_att.count_down)
  572. }
  573. break;
  574. case config.attributes_list_type.zIndex:
  575. this.m_att.zIndex = data.zIndex;
  576. break;
  577. case config.attributes_list_type.dir:
  578. this.m_att.img_dir = data.img_dir;
  579. this.updateDir()
  580. break;
  581. case config.attributes_list_type.show:
  582. this.m_att.is_show = data.is_show;
  583. break;
  584. case config.attributes_list_type.container:
  585. this.m_att.container_layer = data.container_layer
  586. // this.initConatiner()
  587. break;
  588. }
  589. }
  590. director.once(Director.EVENT_AFTER_DRAW,()=>{
  591. ClientEvent.dispatchEvent(config.Event.updateWidgetData)
  592. })
  593. }
  594. updateLabNamePosition() {
  595. let lab_name_y = -(this.spr.getComponent(UITransform).contentSize.height/2+17) //-117
  596. this.lab_name.setPosition(this.lab_name.getPosition().x,lab_name_y,0)
  597. }
  598. updateDir(){
  599. if(this.call_back==null){
  600. switch (this.m_att.img_dir) {
  601. case config.widget_scale_dir.left:
  602. this.spr.scale = new Vec3(-1,1)
  603. break;
  604. case config.widget_scale_dir.up:
  605. this.spr.scale = new Vec3(1,-1)
  606. break;
  607. case config.widget_scale_dir.normal:
  608. this.spr.scale = new Vec3(1,1)
  609. break;
  610. }
  611. }
  612. }
  613. getOtherDragListColor(index:number):Color {
  614. var color_index = index
  615. if(index >= config.COLOR_LIST.length) {
  616. let count = Math.floor(index / config.COLOR_LIST.length)
  617. color_index = index - (config.COLOR_LIST.length * count)
  618. }
  619. return config.COLOR_LIST[color_index]
  620. // return Color.YELLOW
  621. }
  622. Shiny(){
  623. this.img_select.active = true;
  624. this.img_select.getComponent(UITransform).setContentSize(this.node.getComponent(UITransform).contentSize)
  625. Tween.stopAllByTarget(this.img_select)
  626. tween(this.img_select).delay(0.3).hide().delay(0.3).show().delay(0.3).hide().delay(0.3).show().delay(0.3).call(()=>{
  627. this.img_select.active = false;
  628. }).start()
  629. }
  630. public getWidgetAtt(){
  631. this.m_att.name = this.m_data.name!=""?this.m_data.name:"普通控件";
  632. this.m_att.remark = this.m_att.remark;
  633. this.m_att.height = this.spr.getComponent(UITransform).contentSize.height;
  634. this.m_att.width = this.spr.getComponent(UITransform).contentSize.width;
  635. this.m_att.anchor_x = this.m_att.anchor_x==undefined?0.5:this.m_att.anchor_x
  636. this.m_att.anchor_y = this.m_att.anchor_y==undefined?0.5:this.m_att.anchor_y
  637. this.m_att.rotation = this.m_att.rotation;
  638. this.m_att.x = this.node.position.x;
  639. this.m_att.widget_type = this.m_data.type;
  640. this.m_att.y = this.node.position.y;
  641. this.m_att.z = this.node.getSiblingIndex();
  642. this.m_att.type = config.attributes_type.widget;
  643. this.m_att.src_name = this.m_att.src.length>0? this.m_att.src_name:"空";
  644. if(this.m_att.drag_data!=null){
  645. this.m_att.drag_data.drag_pos_x = this.drag.position.x;
  646. this.m_att.drag_data.drag_pos_y = this.drag.position.y;
  647. this.m_att.drag_data.drag_size_width = this.drag.getComponent(UITransform).contentSize.width;
  648. this.m_att.drag_data.drag_size_height = this.drag.getComponent(UITransform).contentSize.height;
  649. }
  650. return this.m_att;
  651. }
  652. initWidgetAtt(){
  653. if(this.call_back==null){
  654. this.node.position = new Vec3(this.m_att.x,this.m_att.y)
  655. this.node.getComponent(UITransform).setContentSize(new Size( this.m_att.width,this.m_att.height))
  656. this.spr.angle = this.m_att.rotation
  657. this.spr.getComponent(UITransform).setAnchorPoint(new Vec2(this.m_att.anchor_x, this.m_att.anchor_y))
  658. if(this.m_att.drag_data!=null){
  659. this.drag.position = new Vec3(this.m_att.drag_data.drag_pos_x,this.m_att.drag_data.drag_pos_y)
  660. this.drag.getComponent(UITransform).setContentSize(new Size(this.m_att.drag_data.drag_size_width ,this.m_att.drag_data.drag_size_height ))
  661. }
  662. this.spr.getComponent(UITransform).setContentSize(this.node.getComponent(UITransform).contentSize)
  663. this.lab_name.getComponent(Label).string = this.m_att.name+"id:"+this.m_att.id;;
  664. }else{
  665. this.lab_name.getComponent(Label).string = this.m_data.name+"id:"+this.m_att.id;
  666. }
  667. if(this.m_att.src!=""&&this.m_att.src.length>0){
  668. assetManager.loadRemote<ImageAsset>(this.m_att.src, (err, imageAsset2)=>{
  669. if (!err && imageAsset2) {
  670. const texture = new Texture2D();
  671. texture.image = imageAsset2;
  672. let spFrame2 = new SpriteFrame();
  673. spFrame2.texture = texture;
  674. this.spr.getComponent(Sprite).spriteFrame = spFrame2
  675. }
  676. });
  677. }
  678. //进行数据差矫正
  679. let t_data = new attributes_data
  680. for (const key in t_data) {
  681. if (Object.prototype.hasOwnProperty.call(t_data ,key)) {
  682. const element = t_data[key];
  683. if(this.m_att[key]===undefined){
  684. this.m_att[key] = element;
  685. }
  686. }
  687. }
  688. this.updateDir()
  689. }
  690. public getData(){
  691. if(this.m_att!=null){
  692. this.getWidgetAtt()
  693. }
  694. this.m_data.att = this.m_att;
  695. return this.m_data;
  696. }
  697. public setData(data){
  698. this.m_data = data;
  699. }
  700. public getContainer(){
  701. return this.container
  702. }
  703. }