Sample Code: Custom operator – Pre-highlight Area Operator

Language: C++

This operator is based on Highlight Area Operator, but it is extended to pre-highlights the selected objects by a rectangle which is shown by the mouse drag.
The operator consists of three operators, Highlight Area Operator, Select Area Operator, and Construct Rectangle Operator. The original operator codes are found in the package, \samples\operators.

Pre-highlight Area Operator uses pre_highlight_style addition to highlight_style for pre highlighting.

MyHighlightAreaOperator::MyHighlightAreaOperator(Canvas canvas, MouseButtons in_mouse_trigger, ModifierKeys in_modifier_trigger)
	: MySelectAreaOperator(in_mouse_trigger, in_modifier_trigger), highlight_options(HighlightOptionsKit::GetDefault())
	, operator_active(false)
{
	_canvas = canvas;
	highlight_options.SetStyleName("highlight_style");
	highlight_options.SetOverlay(HPS::Drawing::Overlay::InPlace);

	HPS::PortfolioKey myPortfolio = HPS::Database::CreatePortfolio();
	_canvas.GetFrontView().GetSegmentKey().GetPortfolioControl().Push(myPortfolio);

	HPS::NamedStyleDefinition myHighlightStyle = myPortfolio.DefineNamedStyle("pre_highlight_style", HPS::Database::CreateRootSegment());
	myHighlightStyle.GetSource().GetMaterialMappingControl().SetFaceColor(HPS::RGBAColor(1.0f, 0.0f, 1.0f))
		.SetLineColor(HPS::RGBAColor(1.0f, 0.0f, 1.0f));

	prehighlight_options.SetStyleName("pre_highlight_style");
	prehighlight_options.SetOverlay(HPS::Drawing::Overlay::InPlace);
}

During mouse moves, PreHighlightCommon is called for pre-highlighting with the style.

bool MyHighlightAreaOperator::OnMouseMove(MouseState const& in_state)
{
	if (!MySelectAreaOperator::OnMouseMove(in_state))
		return false;

	HPS::WindowKey window = in_state.GetEventSource();
	return PreHighlightCommon(window, in_state.GetModifierKeys());
}

bool MyHighlightAreaOperator::PreHighlightCommon(WindowKey& in_window, ModifierKeys in_modifiers)
{
	HPS::SelectionResults results = GetActiveSelection();
	size_t selected = results.GetCount();

	if (selected > 0)
	{
		in_window.GetHighlightControl().Highlight(results, prehighlight_options);
	}
	else
	{
		in_window.GetHighlightControl().Unhighlight(prehighlight_options);
	}

	GetAttachedView().Update();

	return true;
}

Select objects using a rectangle.

bool MySelectAreaOperator::OnMouseMove(MouseState const& in_state)
{
	if (!MyConstructRectangleOperator::OnMouseMove(in_state))
		return false;

	HPS::WindowKey window = in_state.GetEventSource();
	return PreSelectCommon(window, in_state.GetModifierKeys(), in_state);
}

bool MySelectAreaOperator::PreSelectCommon(HPS::WindowKey& in_window, HPS::ModifierKeys in_modifiers, MouseState const& in_state)
{
	try
	{
		HPS::SelectionResults new_selection;
		size_t selected = in_window.GetSelectionControl().SelectByArea(GetWindowRectangle(), selection_options, new_selection);
	  
		active_selection = new_selection;
	}
	catch (HPS::InvalidObjectException const&)
	{
		//do nothing
	}
	GetAttachedView().Update();
	return true;
}

Create a rectangle for selection on mouse move

bool MyConstructRectangleOperator::OnMouseMove(MouseState const& in_state)
{
	if (operator_active && IsMouseTriggered(in_state))
	{
		HPS::KeyPath event_path(in_state.GetEventPath());
		HPS::Point inner_window_point;
		event_path.ConvertCoordinate(HPS::Coordinate::Space::Window, in_state.GetLocation(), HPS::Coordinate::Space::InnerWindow, inner_window_point);
				
		bool res =  ConstructRectCommon(inner_window_point);

		HPS::Point new_window_point(in_state.GetLocation());
		HPS::Point window_points[2] = { new_window_point, window_start_point };
		window_rect = HPS::Rectangle(2, window_points);

		HPS::Point inner_window_points[2] = { inner_window_point, inner_window_start_point };
		inner_window_rect = HPS::Rectangle(2, inner_window_points);

		return res;
	}
	return false;
}

09C4CA03-8F9A-469E-A135-140764AEF519

Pre-highlight Area Operator.zip (5.9 KB)