SpriteManager.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. var sprite = function (
  2. uncheckedImg,
  3. checkedImg,
  4. spriteButtonImage,
  5. spriteButtonText,
  6. execute
  7. ) {
  8. this.UncheckedImg = uncheckedImg;
  9. this.CheckedImg = checkedImg;
  10. this.SpriteButtonImage = spriteButtonImage;
  11. this.SpriteButtonText = spriteButtonText;
  12. this.display = function () {
  13. this.SpriteButtonImage.visible = true;
  14. this.SpriteButtonText.visible = true;
  15. };
  16. this.isVisible = function () {
  17. return this.SpriteButtonImage.visible;
  18. };
  19. this.hide = function () {
  20. this.SpriteButtonImage.visible = false;
  21. this.SpriteButtonText.visible = false;
  22. };
  23. this.checked = function () {
  24. EventCenter.emit("SelectedSpriteChanged", this);
  25. this.SpriteButtonImage.material.map = this.CheckedImg;
  26. execute();
  27. };
  28. this.hightLight = function () {
  29. this.SpriteButtonImage.material.map = this.CheckedImg;
  30. };
  31. this.unchecked = function () {
  32. this.SpriteButtonImage.material.map = this.UncheckedImg;
  33. };
  34. };
  35. var SpriteManager = {
  36. spriteMaxCount: 7,
  37. spriteInterval: 50,
  38. spriteRotateL: undefined,
  39. spriteRotateR: undefined,
  40. spriteQuickClip: undefined,
  41. spriteMove: undefined,
  42. spriteClip: undefined,
  43. isSpriteButtonAdded: false,
  44. spriteAreaWidth: undefined,
  45. spriteWidth: undefined,
  46. spriteHeight: undefined,
  47. clipType: undefined,
  48. allowClip: false,
  49. sprites: [],
  50. hightLightSprite: undefined,
  51. selectedSprite: undefined,
  52. infoHideTask: undefined,
  53. quickClipExecute: false,
  54. init: function () {
  55. this.initSpriteSize();
  56. EventCenter.addEventHandler(
  57. "SelectedSpriteChanged",
  58. this.selectSpriteChanged
  59. );
  60. this.spriteMove = this.createSprite(
  61. "3DWeb/ButtonImage/move.png",
  62. "3DWeb/ButtonImage/moveMouseDown.png",
  63. this.moveClipPlaneChecked,
  64. languageContent.Move
  65. );
  66. this.spriteClip = this.createSprite(
  67. "3DWeb/ButtonImage/clip.png",
  68. "3DWeb/ButtonImage/clipMouseDown.png",
  69. this.drawLineChecked,
  70. languageContent.Clip
  71. );
  72. this.spriteQuickClip = this.createSprite(
  73. "3DWeb/ButtonImage/AI.png",
  74. "3DWeb/ButtonImage/AIDown.png",
  75. this.quickClipChecked,
  76. languageContent.QuickClip
  77. );
  78. this.createSprite(
  79. "3DWeb/ButtonImage/reset.png",
  80. "3DWeb/ButtonImage/resetMouseDown.png",
  81. this.resetChecked,
  82. languageContent.Reset
  83. );
  84. this.createSprite(
  85. "3DWeb/ButtonImage/delete.png",
  86. "3DWeb/ButtonImage/deleteMouseDown.png",
  87. this.deleteChecked,
  88. languageContent.Delete
  89. );
  90. this.spriteRotateR = this.createSprite(
  91. "3DWeb/ButtonImage/rotate-.png",
  92. "3DWeb/ButtonImage/rotate-MouseDown.png",
  93. this.rotateRChecked,
  94. languageContent.RotateR
  95. );
  96. this.spriteRotateL = this.createSprite(
  97. "3DWeb/ButtonImage/rotate+.png",
  98. "3DWeb/ButtonImage/rotate+MouseDown.png",
  99. this.rotateLChecked,
  100. languageContent.RotateL
  101. );
  102. },
  103. initSpriteSize: function () {
  104. let windowWidth =
  105. window.innerWidth < OSHelper.windowWidthDefault
  106. ? OSHelper.windowWidthDefault
  107. : window.innerWidth;
  108. this.spriteInterval = windowWidth * 0.02;
  109. this.spriteAreaWidth = OSHelper.isPC ? windowWidth * 0.4 : windowWidth;
  110. this.spriteWidth =
  111. this.spriteAreaWidth / this.spriteMaxCount - this.spriteInterval;
  112. this.spriteHeight = this.spriteWidth;
  113. },
  114. selectSpriteChanged: function (selectItem) {
  115. if (SpriteManager.selectedSprite != undefined) {
  116. SpriteManager.selectedSprite.unchecked();
  117. }
  118. SpriteManager.selectedSprite = selectItem;
  119. },
  120. createSprite: function (originalImage, clickImage, execute, name) {
  121. let checkedTexture = LoaderUtility.loadTexture(originalImage);
  122. let uncheckedTexture = LoaderUtility.loadTexture(clickImage);
  123. let material = new THREE.SpriteMaterial({ map: checkedTexture });
  124. let spriteImage = new THREE.Sprite(material);
  125. spriteImage.scale.set(this.spriteWidth, this.spriteHeight, 1);
  126. let spriteText = this.createSpriteText(name);
  127. let info = new sprite(
  128. checkedTexture,
  129. uncheckedTexture,
  130. spriteImage,
  131. spriteText,
  132. execute
  133. );
  134. sceneOrtho.add(info.SpriteButtonImage);
  135. sceneOrtho.add(info.SpriteButtonText);
  136. info.hide();
  137. this.sprites.push(info);
  138. return info;
  139. },
  140. createSpriteText: function (text) {
  141. let canvas = document.createElement("canvas");
  142. canvas.width = 512;
  143. canvas.height = 512;
  144. let textCanvas = canvas.getContext("2d");
  145. textCanvas.textAlign = "center";
  146. textCanvas.fillStyle = "#FFF";
  147. textCanvas.font = "800 132px Microsoft YaHei";
  148. textCanvas.lineWidth = 1;
  149. textCanvas.fillText(text, 256, 128);
  150. let texture = new THREE.Texture(canvas);
  151. texture.needsUpdate = true;
  152. let material = new THREE.SpriteMaterial({ map: texture });
  153. let textObj = new THREE.Sprite(material);
  154. let size = this.spriteWidth + this.spriteInterval / 2;
  155. textObj.scale.set(size, size, 1);
  156. return textObj;
  157. },
  158. mdlFileLoaded: function (sourceenum, docarotidaiclip) {
  159. var hasAIClip = sourceenum == "Carotid" && docarotidaiclip == "True";
  160. if (!hasAIClip) {
  161. this.sprites.splice(2, 1);
  162. }
  163. this.resetSpritePosition();
  164. this.sprites.forEach((m) => {
  165. if (m != this.spriteRotateL && m != this.spriteRotateR) {
  166. m.display();
  167. }
  168. });
  169. this.sprites[0].checked();
  170. this.allowClip = true;
  171. //hide loading image
  172. var img = document.getElementById("image");
  173. img.style.display = "none";
  174. },
  175. waitMdlFileLoading: function () {
  176. this.sprites.forEach((m) => {
  177. m.hide();
  178. });
  179. //show loading image
  180. var img = document.getElementById("image");
  181. img.style.display = "block";
  182. },
  183. resetSpritePosition: function () {
  184. let spriteCount = this.sprites.length;
  185. if (!this.spriteRotateR.isVisible()) {
  186. spriteCount -= 2;
  187. }
  188. for (let i = 0; i < spriteCount; i++) {
  189. let positionX =
  190. (0.5 + i - spriteCount / 2) * (this.spriteWidth + this.spriteInterval);
  191. let positionY = window.innerHeight / 2 - 50;
  192. this.sprites[i].SpriteButtonImage.position.set(positionX, positionY, 1);
  193. this.sprites[i].SpriteButtonText.position.set(
  194. positionX,
  195. positionY - this.spriteHeight - 10,
  196. 1
  197. );
  198. }
  199. },
  200. showRotateSprite: function () {
  201. if (!this.spriteRotateR.isVisible()) {
  202. this.spriteRotateR.display();
  203. this.spriteRotateL.display();
  204. this.resetSpritePosition();
  205. }
  206. },
  207. hideRotateSprite: function () {
  208. if (this.spriteRotateR.isVisible()) {
  209. this.spriteRotateR.hide();
  210. this.spriteRotateL.hide();
  211. this.resetSpritePosition();
  212. }
  213. },
  214. mouseDown: function (rayCaster) {
  215. let intersectSprite = this.getIntersectSprite(rayCaster);
  216. if (intersectSprite != undefined) {
  217. intersectSprite.checked();
  218. document.addEventListener(OSHelper.touchEnd, this.mouseUp, false);
  219. return true;
  220. }
  221. return false;
  222. },
  223. mouseUp: function () {
  224. if (SpriteManager.clipType == ClipType.DrawLine) {
  225. SpriteManager.spriteClip.checked();
  226. }
  227. if (SpriteManager.clipType == ClipType.MoveClipPlane) {
  228. SpriteManager.spriteMove.checked();
  229. }
  230. document.removeEventListener(OSHelper.touchEnd, this.mouseUp, false);
  231. document.body.style.cursor = "default";
  232. },
  233. mouseMove: function (rayCaster) {
  234. if (
  235. this.hightLightSprite != undefined &&
  236. this.hightLightSprite != this.selectedSprite
  237. ) {
  238. let intersects = rayCaster.intersectObject(
  239. this.hightLightSprite.SpriteButtonImage
  240. );
  241. if (intersects.length > 0) {
  242. return;
  243. } else {
  244. this.hightLightSprite.unchecked();
  245. document.body.style.cursor = "default";
  246. this.hightLightSprite = undefined;
  247. }
  248. }
  249. let intersectSprite = this.getIntersectSprite(rayCaster);
  250. if (intersectSprite != undefined) {
  251. if (this.hightLightSprite == intersectSprite) return;
  252. this.hightLightSprite = intersectSprite;
  253. if (this.hightLightSprite != this.selectedSprite) {
  254. document.body.style.cursor = "pointer";
  255. intersectSprite.hightLight();
  256. }
  257. }
  258. },
  259. getIntersectSprite: function (rayCaster) {
  260. let intersects;
  261. for (let i = 0; i < this.sprites.length; i++) {
  262. intersects = rayCaster.intersectObject(this.sprites[i].SpriteButtonImage);
  263. if (intersects.length > 0) {
  264. return this.sprites[i];
  265. }
  266. }
  267. return undefined;
  268. },
  269. rotateLChecked: function () {
  270. addRotateTask(-1);
  271. },
  272. rotateRChecked: function () {
  273. addRotateTask(1);
  274. },
  275. resetChecked: function () {
  276. ClipPlaneManager.resetClipInfo();
  277. resetCubePosition();
  278. ClipPlaneManager.meshIsInactive();
  279. },
  280. ///AI 切割
  281. quickClipChecked: async function () {
  282. if (!SpriteManager.quickClipExecute) {
  283. SpriteManager.quickClipExecute = true;
  284. var success = await SpriteManager.executeQuickClip();
  285. if (!success) {
  286. //show info
  287. var info = document.getElementById("info");
  288. info.style.display = "block";
  289. SpriteManager.infoHideTask = setInterval(function () {
  290. var info = document.getElementById("info");
  291. info.style.display = "none";
  292. clearInterval(SpriteManager.infoHideTask);
  293. }, 5000);
  294. // jsObj.AIClipStateCallBack(success);
  295. Dart_aiClipStateCallBack(success);
  296. SpriteManager.quickClipExecute = false;
  297. } else {
  298. ClipPlaneManager.meshIsActive();
  299. Dart_aiClipStateCallBack(success);
  300. }
  301. }
  302. },
  303. executeQuickClip: async function () {
  304. // let json = jsObj.GetVesselClipPlanePoints();
  305. let json = await Dart_getVesselClipPlanePoints("useless arg");
  306. let clipPlaneData = JSON.parse(json);
  307. if (clipPlaneData.ErrorCode != 1000) {
  308. return false;
  309. }
  310. convertBackToWorldPoints(clipPlaneData.WorldPoints);
  311. let verticesPoints = [];
  312. clipPlaneData.WorldPoints.forEach((item) =>
  313. verticesPoints.push(new THREE.Vector3(item.X, item.Y, item.Z))
  314. );
  315. return ClipPlaneManager.generateClipPlaneByPoints(
  316. verticesPoints,
  317. function () {
  318. SpriteManager.quickClipExecute = false;
  319. }
  320. );
  321. },
  322. deleteChecked: function () {
  323. ClipPlaneManager.removeClipPlane();
  324. removeRotateInfo();
  325. LinesDrawingManager.hideClipLine();
  326. LinesDrawingManager.hideAll();
  327. ClipPlaneManager.meshIsInactive();
  328. },
  329. drawLineChecked: function () {
  330. SpriteManager.clipType = ClipType.DrawLine;
  331. },
  332. moveClipPlaneChecked: function () {
  333. SpriteManager.clipType = ClipType.MoveClipPlane;
  334. },
  335. };