b6f88988de790295fe3287962ea60c0330f940f1.svn-base 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. import { Marker, Coordinate } from 'maptalks'
  2. import { mapGoto } from './statusManagement'
  3. import { threeLayer, vector } from './initScene3D'
  4. function structText(args) {
  5. var point = new Marker(args.point,
  6. {
  7. properties: {
  8. altitude: args.top
  9. },
  10. visible: true,
  11. editable: true,
  12. cursor: 'pointer',
  13. shadowBlur: 0,
  14. shadowColor: 'black',
  15. draggable: false,
  16. dragShadow: false, // display a shadow during dragging
  17. drawOnAxis: null, // force dragging stick on a axis, can be: x, y
  18. symbol: {
  19. 'textFaceName': 'sans-serif',
  20. 'textName': args.name,
  21. 'textFill': '#000000',
  22. 'textHaloFill': '#fff',
  23. 'textHaloRadius': 2,
  24. 'textHorizontalAlignment': 'center',
  25. 'textVerticalAlignment': 'top',
  26. 'textSize': 12
  27. }
  28. }
  29. );
  30. point.on('click', () => {
  31. mapGoto({ center: args.point, zoom: 19 })
  32. })
  33. point.__id = args.id
  34. return point
  35. }
  36. function addToAttr(args) {
  37. var point = new Coordinate(args.point),
  38. containerPoint = map.coordinateToContainerPoint(point).round();
  39. args.position = [
  40. Number(containerPoint.x),
  41. Number(containerPoint.y)
  42. ]
  43. }
  44. function checkIsAdd(texts, args) {
  45. addToAttr(args)
  46. let tMinX = args.position[0] - args.width / 2
  47. let tMaxX = args.position[0] + args.width / 2
  48. let tMinY = args.position[1]
  49. let tMaxY = args.position[1] + args.height
  50. let index = texts.findIndex(text => {
  51. let minX = text._struct_attribute.position[0] - text._struct_attribute.width / 2
  52. let minY = text._struct_attribute.position[1]
  53. let maxX = text._struct_attribute.position[0] + text._struct_attribute.width / 2
  54. let maxY = text._struct_attribute.position[1] + text._struct_attribute.height
  55. return !(
  56. (tMinX < minX && tMaxX < minX) ||
  57. (tMinX > maxX && tMaxX > maxX) ||
  58. (tMinY < minY && tMaxY < minY) ||
  59. (tMinY > maxY && tMaxY > maxY)
  60. )
  61. })
  62. return !~index
  63. }
  64. function addText(mesh, textjsons) {
  65. if (textjsons.length === 0) return;
  66. let children = threeLayer.getScene().children
  67. let texts = []
  68. let newTexts = []
  69. children.forEach(mesh => {
  70. mesh.texts && texts.push(...mesh.texts)
  71. })
  72. textjsons.forEach(item => {
  73. if (checkIsAdd(texts, item)) {
  74. let text = structText(item)
  75. newTexts.push(text)
  76. text._struct_attribute = item
  77. texts.push(text)
  78. }
  79. })
  80. vector.addGeometry(newTexts)
  81. mesh.texts = newTexts
  82. mesh.textjsons = textjsons
  83. }
  84. function referText() {
  85. let children = threeLayer.getScene().children
  86. let checkTexts = []
  87. children.forEach(mesh => {
  88. if (mesh.texts) {
  89. for (let i = 0; i < mesh.texts.length; i++) {
  90. let text = mesh.texts[i]
  91. if (checkIsAdd(checkTexts, text._struct_attribute)) {
  92. checkTexts.push(text)
  93. } else {
  94. vector.removeGeometry(text)
  95. mesh.texts.splice(i, 1)
  96. --i
  97. }
  98. }
  99. }
  100. if (mesh.textjsons) {
  101. mesh.textjsons.map(item => {
  102. let index = mesh.texts.findIndex(text => text.__id === item.id)
  103. if (!~index && checkIsAdd(checkTexts, item)) {
  104. let text = structText(item)
  105. checkTexts.push(text)
  106. text._struct_attribute = item
  107. vector.addGeometry(text)
  108. mesh.texts.push(text)
  109. }
  110. })
  111. }
  112. })
  113. }
  114. export { referText }
  115. export default addText