UI_Idioms.ts 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. }
  38. //高亮显示
  39. public light_Show(cube_infor: Cube_Infor) {
  40. const layout = this.getLayout<Layout_Idioms>();
  41. let txt_length = cube_infor.Text.length;
  42. let flag = false;
  43. for (let i = 0; i < this.idioms.length; i++) {
  44. if (this.idioms[i].piece_1_word === cube_infor.Text) {
  45. this.idioms[i].hud_sp.node.active = true;
  46. this.idioms[i].hud_sp.node.getComponent(UITransform).contentSize = new Size(30 * txt_length, 30);
  47. let target = 0;
  48. this.idioms[i].hud_sp.node.position = new Vec3(11 + target * 30, -11.5, 0);
  49. this.scrollToTarget(i);
  50. }
  51. else if (this.idioms[i].piece_2_word === cube_infor.Text) {
  52. this.idioms[i].hud_sp.node.active = true;
  53. this.idioms[i].hud_sp.node.getComponent(UITransform).contentSize = new Size(30 * txt_length, 30);
  54. let target = 4 - txt_length;
  55. this.idioms[i].hud_sp.node.position = new Vec3(11 + target * 30, -11.5, 0);
  56. this.scrollToTarget(i);
  57. }
  58. }
  59. }
  60. //取消高亮
  61. public light_Hide(cube1: Cube_Infor, cube2: Cube_Infor) {
  62. const layout = this.getLayout<Layout_Idioms>();
  63. this.idioms = this.idioms.filter(element => {
  64. let shouldRemove = false;
  65. let hasMatch = false;
  66. // 检查与 cube1 相关的成语
  67. if (cube1.Text === element.piece_1_word) {
  68. for (const cubeInfor of layout.Container.idiom_combine.keys()) {
  69. if (cubeInfor.txt.string === cube1.Text) {
  70. hasMatch = true;
  71. break;
  72. }
  73. }
  74. // 只有当前字没有参与成语时才移除提示
  75. if (!hasMatch) {
  76. element.hud_sp.node.active = false; // 销毁UI中的成语提示
  77. }
  78. }
  79. // 检查与 cube2 相关的成语
  80. if (cube2.Text === element.piece_2_word) {
  81. for (const cubeInfor of layout.Container.idiom_combine.keys()) {
  82. if (cubeInfor.txt.string === cube2.Text) {
  83. hasMatch = true;
  84. break;
  85. }
  86. }
  87. // 只有当前字没有参与成语时才移除提示
  88. if (!hasMatch) {
  89. element.hud_sp.node.active = false; // 销毁UI中的成语提示
  90. }
  91. }
  92. // 检查是否能组合成一个成语
  93. if (cube1.Text + cube2.Text === element.txt.string) {
  94. element.node.destroy(); // 销毁节点
  95. shouldRemove = true; // 如果已组合,移除该成语
  96. }
  97. // 保留未被组合的元素
  98. return !shouldRemove;
  99. });
  100. // 监测所有字是否仍然需要高亮显示
  101. this.idioms.forEach(element => {
  102. // 检查每个字是否仍然在任何成语组合中
  103. let shouldDisplay = false;
  104. // 遍历容器中的所有成语组合,看看当前字是否仍然参与任何组合
  105. for (const cubeInfor of layout.Container.idiom_combine.keys()) {
  106. if (cubeInfor.txt.string === element.piece_1_word || cubeInfor.txt.string === element.piece_2_word) {
  107. shouldDisplay = true;
  108. break;
  109. }
  110. }
  111. // 如果该字还参与成语,则保持其提示显示
  112. if (shouldDisplay) {
  113. element.hud_sp.node.active = true;
  114. }
  115. });
  116. }
  117. public scrollToTarget(index: number) {
  118. const layout = this.getLayout<Layout_Idioms>();
  119. let row = Math.trunc(index / 5);
  120. if (row == 0) {
  121. layout.scrollView.scrollTo(new Vec2(0, 1), 0.2, false);
  122. } else if (row == this.idioms.length / 5) {
  123. layout.scrollView.scrollTo(new Vec2(0, 0), 0.2, false);
  124. } else {
  125. let res = Math.trunc(((row) / (Math.trunc((this.idioms.length / 5)) - 2)) * 10) / 10;
  126. layout.scrollView.scrollTo(new Vec2(0, 1 - res), 0.2, false);
  127. console.log("weizhi:" + (1 - res));
  128. }
  129. }
  130. public all_light_Hide() {
  131. this.idioms.forEach(element => {
  132. element.hud_sp.node.active = false;
  133. });
  134. }
  135. }