widget_item.ts 36 KB

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