def onMouseEvent(self, kwargs):
		uiEvent = kwargs['ui_event']
		reason = uiEvent.reason()

		tool = self.node.parm('tool').eval()

		self.updateCursor(uiEvent)

		if reason == hou.uiEventReason.Picked or reason == hou.uiEventReason.Start:

			if tool == State.TOOL_ADD:

				rotate = hou.Vector3(self.node.parmTuple('r').eval())
				scale = hou.Vector3(self.node.parmTuple('s').eval())

				xform = buildXformByTRS(self.cursor.position, rotate, scale)
				instanceIndex = self.node.parm('instanceindex').eval()

				with hou.undos.group('Add an instance'):
					primnum = self.elements.add(xform, instanceIndex)
					self.updateSelection([primnum])

		
		if reason == hou.uiEventReason.Located:

			if tool == State.TOOL_ADD and self.cursor.normal != None:
				rotate = computeDirectionRotates(self.cursor.normal)

				with hou.undos.disabler():
					self.movePivotWithoutTransform(rotate=rotate)


			# Deselect when selection has cleared
			sel = self.sceneViewer.currentGeometrySelection()
			if sel and len(sel.selections()) == 0 and len(self.state('selected_prims')) > 0:
				with hou.undos.group('Clear selection'):
					self.updateSelection([])
	def onMenuAction(self, kwargs):
		item = kwargs['menu_item']

		if item == 'edit_mode':
			parm = self.node.parm('tool')
			isEdit = kwargs['edit_mode'] == State.TOOL_EDIT
			parm.set(State.TOOL_ADD if isEdit else State.TOOL_EDIT)

		elif item == 'delete':
			with hou.undos.group('Delete Primitives'):
				selection = self.state('selected_prims')
				self.elements.delete(selection)
				self.node.parm('selection').set('')

		elif item == 'duplicate':
			with hou.undos.group('Duplicate Primitives'):
				for primnum in self.state('selected_prims'):
					xform = self.elements.transform(primnum)
					instanceIndex = self.elements.instanceIndex(primnum)
					self.elements.add(xform, instanceIndex)

		elif item == 'move_pivot':
			with hou.undos.group('Move Pivot'):
				self.movePivotWithoutTransform(translate=self.cursor.position)
				self.updatePivotCache()

		elif item == 'orient_pivot':
			with hou.undos.group('Orient Pivot'):
				origin = hou.Vector3(self.node.parmTuple('t').eval())
				target = self.cursor.position

				direction = (target - origin).normalized()  # Direction
				rotate = computeDirectionRotates(direction)

				self.movePivotWithoutTransform(rotate=rotate)
				self.updatePivotCache()