UI_Idioms.ts 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. import { _decorator, Component, DynamicAtlasManager, find, instantiate, Label, macro, Node, Prefab, ScrollView, Size, UITransform, Vec2, Vec3 } from 'cc';
  2. import { UI_Idiom } from './UI_Idiom';
  3. import { Layout_Idioms } from './Layout_Idioms';
  4. import { GameUILayers } from '../../../core/ui/ui';
  5. import ui_base from '../../../core/ui/ui_base';
  6. import { ModuleDef } from '../../../Scripts/ModuleDef';
  7. import { Container_Manager } from '../../game/Container_Manager';
  8. import { Cube_Infor } from '../../game/Cube_Infor';
  9. const { ccclass, property } = _decorator;
  10. @ccclass('UI_Idioms')
  11. export class UI_Idioms extends ui_base {
  12. idioms: UI_Idiom[] = [];
  13. constructor() {
  14. super(ModuleDef.GAME, 'ui/UI_Idioms/ScrollView', GameUILayers.HUD, Layout_Idioms);
  15. }
  16. protected async onCreated() {
  17. const layout = this.getLayout<Layout_Idioms>();
  18. layout.Container = find('Container').getComponent(Container_Manager);
  19. this.init();
  20. }
  21. public init() {
  22. const layout = this.getLayout<Layout_Idioms>();
  23. console.log("init");
  24. for (let i = 0; i < this.idioms.length; i++) {
  25. this.idioms[i].node.destroy();
  26. }
  27. this.idioms = [];
  28. for (let i = 0; i < layout.Container.idioms.length; i++) {
  29. let node = instantiate(layout.idiom_prefab);
  30. node.parent = layout.content;
  31. console.log("label" + layout.Container.idioms[i].idiom);
  32. node.getComponent(UI_Idiom).txt.string = layout.Container.idioms[i].idiom;
  33. node.getComponent(UI_Idiom).piece_1_word = layout.Container.idioms[i].piece_1_word;
  34. node.getComponent(UI_Idiom).piece_2_word = layout.Container.idioms[i].piece_2_word;
  35. this.idioms.push(node.getComponent(UI_Idiom));
  36. }
  37. layout.scrollView.scrollToTop();
  38. }
  39. //高亮显示
  40. public light_Show(cube_infor: Cube_Infor) {
  41. const layout = this.getLayout<Layout_Idioms>();
  42. let txt_length = cube_infor.Text.length;
  43. let flag = false;
  44. for (let i = 0; i < this.idioms.length; i++) {
  45. if (this.idioms[i].piece_1_word === cube_infor.Text) {
  46. this.idioms[i].hud_sp.node.active = true;
  47. this.idioms[i].hud_sp.node.getComponent(UITransform).contentSize = new Size(30 * txt_length, 30);
  48. let target = 0;
  49. this.idioms[i].hud_sp.node.position = new Vec3(11 + target * 30, -11.5, 0);
  50. this.scrollToTarget(i);
  51. }
  52. else if (this.idioms[i].piece_2_word === cube_infor.Text) {
  53. this.idioms[i].hud_sp.node.active = true;
  54. this.idioms[i].hud_sp.node.getComponent(UITransform).contentSize = new Size(30 * txt_length, 30);
  55. let target = 4 - txt_length;
  56. this.idioms[i].hud_sp.node.position = new Vec3(11 + target * 30, -11.5, 0);
  57. this.scrollToTarget(i);
  58. }
  59. }
  60. }
  61. //取消高亮
  62. public light_Hide(cube1: Cube_Infor, cube2: Cube_Infor) {
  63. const layout = this.getLayout<Layout_Idioms>();
  64. console.log("开始执行light——hide");
  65. const checkAndRemoveHint = (cube: Cube_Infor, piece_1: string, piece_2: string) => {
  66. console.log("检测并移除");
  67. let hasMatch = false;
  68. // 检查 cube 是否与成语的某个词匹配
  69. if (cube.Text === piece_1 || cube.Text === piece_2) {
  70. for (const cubeInfor of layout.Container.idiom_combine.keys()) {
  71. if (cubeInfor.txt.string === cube.Text) {
  72. hasMatch = true;
  73. break;
  74. }
  75. }
  76. // 如果当前字没有参与成语,则移除提示
  77. if (!hasMatch) {
  78. console.log("移除提示");
  79. return false;
  80. }
  81. }
  82. return true;
  83. };
  84. // 过滤掉已匹配的成语,并销毁提示
  85. this.idioms = this.idioms.filter(element => {
  86. let shouldRemove = false;
  87. // 检查与 cube1 和 cube2 相关的成语
  88. const cube1Active = checkAndRemoveHint(cube1, element.piece_1_word, element.piece_2_word);
  89. const cube2Active = checkAndRemoveHint(cube2, element.piece_2_word, element.piece_1_word);
  90. if (!cube1Active || !cube2Active) {
  91. element.hud_sp.node.active = false; // 销毁提示
  92. }
  93. // 如果 cube1 和 cube2 组合成一个成语,销毁该成语节点
  94. if (cube1.Text + cube2.Text === element.txt.string) {
  95. element.node.destroy();
  96. shouldRemove = true;
  97. }
  98. return !shouldRemove;
  99. });
  100. // 监测每个字是否仍然在某个成语中,如果是,则高亮显示
  101. const str=[...layout.Container.idiom_combine.keys()];
  102. for(const element of this.idioms){
  103. if(str.some(cubeInfor =>
  104. (cubeInfor.Text == element.piece_1_word) || (cubeInfor.Text == element.piece_2_word)
  105. )){
  106. element.hud_sp.node.active = true;
  107. }else{
  108. element.hud_sp.node.active = false;
  109. }
  110. }
  111. }
  112. public scrollToTarget(index: number) {
  113. const layout = this.getLayout<Layout_Idioms>();
  114. let row = Math.trunc(index / 5);
  115. if (row == 0) {
  116. layout.scrollView.scrollTo(new Vec2(0, 1), 0.2, false);
  117. } else if (row == this.idioms.length / 5) {
  118. layout.scrollView.scrollTo(new Vec2(0, 0), 0.2, false);
  119. } else {
  120. let res = Math.trunc(((row) / (Math.trunc((this.idioms.length / 5)) - 2)) * 10) / 10;
  121. layout.scrollView.scrollTo(new Vec2(0, 1 - res), 0.2, false);
  122. console.log("weizhi:" + (1 - res));
  123. }
  124. }
  125. public all_light_Hide() {
  126. this.idioms.forEach(element => {
  127. element.hud_sp.node.active = false;
  128. });
  129. }
  130. }