CUIRegionManager.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. package com.zjinja.mcmod.zry_client_utils_mod.cui;
  2. import com.mojang.logging.LogUtils;
  3. import com.zjinja.mcmod.zry_client_utils_mod.renderer.RenderContext;
  4. import com.zjinja.mcmod.zry_client_utils_mod.utils.ZLogUtil;
  5. import net.minecraft.client.Minecraft;
  6. import net.minecraft.network.chat.Component;
  7. import net.minecraft.world.phys.AABB;
  8. import java.util.HashMap;
  9. import java.util.Optional;
  10. import java.util.UUID;
  11. public class CUIRegionManager {
  12. private static final CUIRegionManager INSTANCE = new CUIRegionManager();
  13. private IRegion selRegion = new EmptyRegion();
  14. private final HashMap<UUID, IRegion> regions = new HashMap<>();
  15. private IRegion activeRegion = new EmptyRegion();
  16. public enum RegionType {
  17. EMPTY,
  18. CUBOID,
  19. POLYGON,
  20. ELLIPSOID,
  21. CYLINDER,
  22. POLYHEDRON,
  23. }
  24. public static CUIRegionManager getInstance() {
  25. return CUIRegionManager.INSTANCE;
  26. }
  27. public CUIRegionManager() {
  28. }
  29. public void clearSelection(){
  30. this.selRegion = new EmptyRegion();
  31. }
  32. public void clearRegions()
  33. {
  34. this.activeRegion = new EmptyRegion();
  35. this.regions.clear();
  36. }
  37. public void setRegion(UUID id, IRegion region)
  38. {
  39. if (id == null)
  40. {
  41. this.selRegion = region;
  42. return;
  43. }
  44. if (region == null)
  45. {
  46. this.regions.remove(id);
  47. this.activeRegion = new EmptyRegion();
  48. return;
  49. }
  50. this.regions.put(id, region);
  51. this.activeRegion = region;
  52. }
  53. public void update(boolean multi, String type, String[] param) {
  54. switch (type) {
  55. default -> {
  56. ZLogUtil.log(
  57. LogUtils.getLogger(), ZLogUtil.Level.WARN,
  58. "wecui/process-pkg", "unknown msg type '{}'", type
  59. );
  60. }
  61. case "s" -> {
  62. if(!checkArgsCount(param, 1, 2, type)) return;
  63. IRegion r = createRegionByTypeName(param[0]);
  64. UUID id = null;
  65. if (multi)
  66. {
  67. if (r == null && param.length < 2)
  68. {
  69. ZLogUtil.log(
  70. LogUtils.getLogger(), ZLogUtil.Level.DEBUG,
  71. "wecui/process-pkg", "clear selection event."
  72. );
  73. this.clearRegions();
  74. return;
  75. }
  76. if (param.length < 2) {
  77. ZLogUtil.log(
  78. LogUtils.getLogger(), ZLogUtil.Level.WARN,
  79. "wecui/process-pkg", "invalid args for msg type '{}'.", type
  80. );
  81. return;
  82. }
  83. id = UUID.fromString(param[1]);
  84. }
  85. this.setRegion(id, r);
  86. ZLogUtil.log(
  87. LogUtils.getLogger(), ZLogUtil.Level.DEBUG,
  88. "wecui/process-pkg", "new region event: id={}", id
  89. );
  90. }
  91. case "p" -> {
  92. if(!checkArgsCount(param, 5, 6, type)) return;
  93. IRegion r = this.getSelection(multi);
  94. if (r == null)
  95. {
  96. ZLogUtil.log(
  97. LogUtils.getLogger(), ZLogUtil.Level.DEBUG,
  98. "wecui/process-pkg", "for event '{}', no selection exists now.", type
  99. );
  100. return;
  101. }
  102. int id = Integer.parseUnsignedInt(param[0]);
  103. if (
  104. multi &&
  105. param[1].equals("~") &&
  106. param[2].equals("~") &&
  107. param[3].equals("~")
  108. ){
  109. var player = Minecraft.getInstance().player;
  110. if (player != null){
  111. player.sendSystemMessage(Component.translatable("chat.tip.not_support_this_wecui_type"));
  112. player.sendSystemMessage(Component.translatable("chat.tip.not_support_l2"));
  113. }else {
  114. ZLogUtil.log(
  115. LogUtils.getLogger(), ZLogUtil.Level.WARN,
  116. "wecui/process-pkg", "player is null"
  117. );
  118. }
  119. this.clearRegions();
  120. this.clearSelection();
  121. return;
  122. }
  123. double x = Double.parseDouble(param[1]);
  124. double y = Double.parseDouble(param[2]);
  125. double z = Double.parseDouble(param[3]);
  126. r.updatePoint(id, x, y, z);
  127. }
  128. case "p2" -> {
  129. if(!checkArgsCount(param, 4, 5, type)) return;
  130. IRegion r = this.getSelection(multi);
  131. if (r == null)
  132. {
  133. ZLogUtil.log(
  134. LogUtils.getLogger(), ZLogUtil.Level.DEBUG,
  135. "wecui/process-pkg", "for event '{}', no selection exists now.", type
  136. );
  137. return;
  138. }
  139. int id = Integer.parseUnsignedInt(param[0]);
  140. int x = Integer.parseInt(param[1]);
  141. int z = Integer.parseInt(param[2]);
  142. r.updatePolygonPoint(id, x, z);
  143. }
  144. case "e" -> {
  145. if(!checkArgsCount(param, 4, 4, type)) return;
  146. IRegion r = this.getSelection(multi);
  147. if (r == null)
  148. {
  149. ZLogUtil.log(
  150. LogUtils.getLogger(), ZLogUtil.Level.DEBUG,
  151. "wecui/process-pkg", "for event '{}', no selection exists now.", type
  152. );
  153. return;
  154. }
  155. int id = Integer.parseUnsignedInt(param[0]);
  156. switch (id) {
  157. case 0 -> {
  158. int x = Integer.parseInt(param[1]);
  159. int y = Integer.parseInt(param[2]);
  160. int z = Integer.parseInt(param[3]);
  161. r.updateEllipsoidCenter(x, y, z);
  162. }
  163. case 1 -> {
  164. double x = Double.parseDouble(param[1]);
  165. double y = Double.parseDouble(param[2]);
  166. double z = Double.parseDouble(param[3]);
  167. r.updateEllipsoidRadius(x, y, z);
  168. }
  169. default -> {
  170. ZLogUtil.log(
  171. LogUtils.getLogger(), ZLogUtil.Level.DEBUG,
  172. "wecui/process-pkg", "for event '{}', invalid id {}.", type, id
  173. );
  174. }
  175. }
  176. }
  177. case "cyl" -> {
  178. if(!checkArgsCount(param, 5, 5, type)) return;
  179. IRegion r = this.getSelection(multi);
  180. if (r == null)
  181. {
  182. ZLogUtil.log(
  183. LogUtils.getLogger(), ZLogUtil.Level.DEBUG,
  184. "wecui/process-pkg", "for event '{}', no selection exists now.", type
  185. );
  186. return;
  187. }
  188. int x = Integer.parseInt(param[0]);
  189. int y = Integer.parseInt(param[1]);
  190. int z = Integer.parseInt(param[2]);
  191. double rx = Double.parseDouble(param[3]);
  192. double rz = Double.parseDouble(param[4]);
  193. r.updateCylinder(x, y, z, rx, rz);
  194. }
  195. case "mm" -> {
  196. if(!checkArgsCount(param, 2, 2, type)) return;
  197. IRegion r = this.getSelection(multi);
  198. if (r == null)
  199. {
  200. ZLogUtil.log(
  201. LogUtils.getLogger(), ZLogUtil.Level.DEBUG,
  202. "wecui/process-pkg", "for event '{}', no selection exists now.", type
  203. );
  204. return;
  205. }
  206. int min = Integer.parseInt(param[0]);
  207. int max = Integer.parseInt(param[1]);
  208. r.updateMinMax(min, max);
  209. }
  210. case "u" -> {
  211. if(!checkArgsCount(param, 1, 1, type)) return;
  212. }
  213. case "poly" -> {
  214. if(!checkArgsCount(param, 3, 99, type)) return;
  215. IRegion r = this.getSelection(multi);
  216. if (r == null)
  217. {
  218. ZLogUtil.log(
  219. LogUtils.getLogger(), ZLogUtil.Level.DEBUG,
  220. "wecui/process-pkg", "for event '{}', no selection exists now.", type
  221. );
  222. return;
  223. }
  224. final int[] vertexIds = new int[param.length];
  225. for (int i = 0; i < param.length; ++i)
  226. {
  227. vertexIds[i] = Integer.parseInt(param[i]);
  228. }
  229. r.updatePolygon(vertexIds);
  230. }
  231. case "col" -> {
  232. if(!checkArgsCount(param, 4, 4, type)) return;
  233. ZLogUtil.log(
  234. LogUtils.getLogger(), ZLogUtil.Level.DEBUG,
  235. "wecui/process-pkg", "event '{}' not supported yet.", type
  236. );
  237. }
  238. case "grid" -> {
  239. if(!checkArgsCount(param, 1, 2, type)) return;
  240. ZLogUtil.log(
  241. LogUtils.getLogger(), ZLogUtil.Level.DEBUG,
  242. "wecui/process-pkg", "event '{}' not supported yet.", type
  243. );
  244. }
  245. }
  246. }
  247. private IRegion createRegionByTypeName(String typename) {
  248. switch (typename) {
  249. default -> {
  250. ZLogUtil.log(
  251. LogUtils.getLogger(), ZLogUtil.Level.WARN,
  252. "wecui/process-pkg", "unknown region type '{}'", typename
  253. );
  254. return null;
  255. }
  256. case "clear" -> {
  257. return null;
  258. }
  259. case "cuboid" -> {
  260. return new CuboidRegion();
  261. }
  262. case "ellipsoid" -> {
  263. return new EllipsoidRegion();
  264. }
  265. case "cylinder" -> {
  266. return new CylinderRegion();
  267. }
  268. case "polygon2d" -> {
  269. var player = Minecraft.getInstance().player;
  270. if (player != null){
  271. player.sendSystemMessage(Component.translatable("chat.tip.not_support_polygon_yet"));
  272. player.sendSystemMessage(Component.translatable("chat.tip.not_support_l2"));
  273. }else {
  274. ZLogUtil.log(
  275. LogUtils.getLogger(), ZLogUtil.Level.WARN,
  276. "wecui/process-pkg", "player is null"
  277. );
  278. }
  279. return new PolygonRegion();
  280. }
  281. case "polyhedron" -> {
  282. var player = Minecraft.getInstance().player;
  283. if (player != null){
  284. player.sendSystemMessage(Component.translatable("chat.tip.not_support_polyhedron_yet"));
  285. player.sendSystemMessage(Component.translatable("chat.tip.not_support_l2"));
  286. }else {
  287. ZLogUtil.log(
  288. LogUtils.getLogger(), ZLogUtil.Level.WARN,
  289. "wecui/process-pkg", "player is null"
  290. );
  291. }
  292. return new PolygonRegion();
  293. }
  294. }
  295. }
  296. public IRegion getSelection(boolean multi)
  297. {
  298. return multi ? this.activeRegion : this.selRegion;
  299. }
  300. public String describeSelection() {
  301. if(this.selRegion == null) {
  302. return "empty";
  303. }
  304. return this.selRegion.describe();
  305. }
  306. public Optional<AABB> getSelectionAABB() {
  307. IRegion ir = this.selRegion;
  308. if(ir == null){
  309. return Optional.empty();
  310. }
  311. if(ir instanceof CuboidRegion cr) {
  312. return cr.getRegionAABB();
  313. }else{
  314. return Optional.empty();
  315. }
  316. }
  317. private boolean checkArgsCount(String[] params, int min, int max, String mtype) {
  318. if (max == min)
  319. {
  320. if (params.length != max)
  321. {
  322. ZLogUtil.log(
  323. LogUtils.getLogger(), ZLogUtil.Level.WARN,
  324. "wecui/process-pkg", "invalid args count {} of msg type '{}'",
  325. params.length, mtype
  326. );
  327. return false;
  328. }
  329. }
  330. if (params.length > max || params.length < min)
  331. {
  332. ZLogUtil.log(
  333. LogUtils.getLogger(), ZLogUtil.Level.WARN,
  334. "wecui/process-pkg", "invalid args count {} of msg type '{}'",
  335. params.length, mtype
  336. );
  337. return false;
  338. }else{
  339. return true;
  340. }
  341. }
  342. public static void render(RenderContext rctx) {
  343. var inst = getInstance();
  344. if (inst != null) {
  345. if (inst.regions != null) {
  346. for (IRegion ir : inst.regions.values()) {
  347. ir.render(rctx, false);
  348. }
  349. }
  350. if(inst.selRegion != null) {
  351. inst.selRegion.render(rctx, true);
  352. }
  353. }
  354. }
  355. }