SelectTool.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*
  2. * SelectTool.js
  3. *
  4. * @author realor
  5. */
  6. import { Tool } from './Tool.js'
  7. import { Controls } from '../ui/Controls.js'
  8. import { Application } from '../ui/Application.js'
  9. import { I18N } from '../i18n/I18N.js'
  10. class SelectTool extends Tool {
  11. constructor(application, options) {
  12. super(application)
  13. this.name = 'select'
  14. this.label = 'tool.select.label'
  15. this.help = 'tool.select.help'
  16. this.className = 'select'
  17. this.setOptions(options)
  18. this._onPointerDown = this.onPointerDown.bind(this)
  19. this.createPanel()
  20. }
  21. createPanel() {
  22. this.panel = this.application.createPanel(this.label, 'left')
  23. this.panel.preferredHeight = 140
  24. const helpElem = document.createElement('div')
  25. I18N.set(helpElem, 'innerHTML', this.help)
  26. helpElem.style.marginBottom = '4px'
  27. this.panel.bodyElem.appendChild(helpElem)
  28. this.selectModeElem = Controls.addRadioButtons(
  29. this.panel.bodyElem,
  30. 'selection_mode',
  31. 'label.selection_mode',
  32. [
  33. [Application.SET_SELECTION_MODE, 'label.set_selection_mode'],
  34. [Application.ADD_SELECTION_MODE, 'label.add_selection_mode'],
  35. [Application.REMOVE_SELECTION_MODE, 'label.remove_selection_mode']
  36. ],
  37. Application.SET_SELECTION_MODE,
  38. 'selection_mode',
  39. () => {
  40. this.application.selectionMode = this.selectModeElem.value
  41. }
  42. )
  43. this.posElem = document.createElement('div')
  44. this.posElem.style.textAlign = 'center'
  45. this.posElem.style.padding = '4px'
  46. this.panel.bodyElem.appendChild(this.posElem)
  47. }
  48. activate() {
  49. this.panel.visible = true
  50. var container = this.application.container
  51. container.addEventListener('pointerdown', this._onPointerDown, false)
  52. }
  53. deactivate() {
  54. this.panel.visible = false
  55. var container = this.application.container
  56. container.removeEventListener('pointerdown', this._onPointerDown, false)
  57. }
  58. onPointerDown(event) {
  59. if (!this.isCanvasEvent(event)) return
  60. const application = this.application
  61. const scene = application.scene
  62. const selection = application.selection
  63. var pointerPosition = this.getEventPosition(event)
  64. var intersect = this.intersect(pointerPosition, scene, true)
  65. if (intersect) {
  66. var point = intersect.point
  67. var xpos = Math.round(point.x * 1000) / 1000
  68. var ypos = Math.round(point.y * 1000) / 1000
  69. var zpos = Math.round(point.z * 1000) / 1000
  70. this.posElem.innerHTML = '(x, y ,z) = (' + xpos + ', ' + ypos + ', ' + zpos + ')'
  71. var object = intersect.object
  72. var parent = object
  73. while (parent && !parent.userData.selection) {
  74. parent = parent.parent
  75. }
  76. if (parent && parent.userData.selection.group) {
  77. object = parent
  78. }
  79. application.userSelectObjects([object], event)
  80. } else {
  81. selection.clear()
  82. }
  83. }
  84. }
  85. export { SelectTool }