src/editmodes/magicWandMode.cpp

Sun, 04 Oct 2015 14:09:46 +0300

author
Teemu Piippo <crimsondusk64@gmail.com>
date
Sun, 04 Oct 2015 14:09:46 +0300
changeset 1007
998f1a04ef03
parent 996
9ecc878c7dea
permissions
-rw-r--r--

Branch close

/*
 *  LDForge: LDraw parts authoring CAD
 *  Copyright (C) 2013 - 2015 Teemu Piippo
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <QMouseEvent>
#include "magicWandMode.h"
#include "../ldDocument.h"
#include "../mainwindow.h"
#include "../glRenderer.h"

MagicWandMode::MagicWandMode (GLRenderer* renderer) :
	Super (renderer)
{
	// Get vertex<->object data
	for (LDObject* obj : currentDocument()->objects())
	{
		// Note: this deliberately only takes vertex-objects into account.
		// The magic wand does not process subparts.
		if (obj->hasVertices())
		{
			LDVertexObject* vo = static_cast<LDVertexObject*> (obj);
			for (int i = 0; i < vo->numVertices(); ++i)
				m_vertices[vo->vertex (i)] << vo;
		}
	}
}

EditModeType MagicWandMode::type() const
{
	return EditModeType::MagicWand;
}

void MagicWandMode::fillBoundaries (LDVertexObject* obj, Boundaries& boundaries, Candidates& candidates)
{
	// All boundaries obviously share vertices with the object, therefore they're all in the list
	// of candidates.
	for (LDVertexObject* candidate : candidates)
	{
		if (not isOneOf (candidate->type(), OBJ_Line, OBJ_CondLine) or candidate->vertex (0) == candidate->vertex (1))
			continue;

		int matches = 0;

		for (int i = 0; i < obj->numVertices(); ++i)
		{
			if (not isOneOf (obj->vertex (i), candidate->vertex (0), candidate->vertex (1)))
				continue;

			if (++matches == 2)
			{
				// Boundary found. If it's an edgeline, add it to the boundaries list, if a
				// conditional line, select it.
				if (candidate->type() == OBJ_CondLine)
					m_selection << candidate;
				else
					boundaries.append (std::make_tuple (candidate->vertex (0), candidate->vertex (1)));
				break;
			}
		}
	}
}

void MagicWandMode::doMagic (LDObject* input, MagicType type)
{
	if (input == nullptr or not input->hasVertices())
	{
		if (type == Set)
		{
			currentDocument()->clearSelection();
			m_window->buildObjectList();
		}

		return;
	}

	LDVertexObject* obj = static_cast<LDVertexObject*> (input);
	int matchesneeded = (obj->numVertices() == 2) ? 1 : 2;
	Boundaries boundaries;
	LDObjectType objtype = obj->type();

	if (type != InternalRecursion)
	{
		m_selection.clear();
		m_selection.append (obj);
	}

	Candidates candidates;

	// Get the list of objects that touch this object, i.e. share a vertex
	// with this.
	for (int i = 0; i < obj->numVertices(); ++i)
		candidates += m_vertices[obj->vertex (i)];

	removeDuplicates (candidates);

	// If we're dealing with surfaces, get a list of boundaries.
	if (matchesneeded > 1)
		fillBoundaries (obj, boundaries, candidates);

	for (LDVertexObject* candidate : candidates)
	{
		// If we're doing this on lines, we need exact type match. Surface types (quads and
		// triangles) can be mixed. Also don't consider self a candidate, and don't consider
		// objects we have already processed.
		if ((candidate == obj) or
			(candidate->color() != obj->color()) or
			(m_selection.contains (candidate)) or
			(matchesneeded == 1 and (candidate->type() != objtype)) or
			((candidate->numVertices() > 2) ^ (matchesneeded == 2)))
		{
			continue;
		}

		// Now ensure the two objects share enough vertices.
		QSet<Vertex> matches = obj->vertexSet();
		matches.intersect (candidate->vertexSet());

		if (matches.size() < matchesneeded)
			goto skipthis; // Not enough matches.

		// Check if a boundary gets in between the objects.
		for (const Boundary& boundary : boundaries)
		{
			if (matches.contains (std::get<0>(boundary)) and matches.contains (std::get<1>(boundary)))
				goto skipthis;
		}

		m_selection.append (candidate);
		doMagic (candidate, InternalRecursion);
skipthis:
		continue;
	}

	switch (type)
	{
	case Set:
		currentDocument()->clearSelection();
	case Additive:
		for (LDObject* obj : m_selection)
			obj->select();
		break;

	case Subtractive:
		for (LDObject* obj : m_selection)
			obj->deselect();
		break;

	case InternalRecursion:
		break;
	}

	if (type != InternalRecursion)
		m_window->buildObjectList();
}

bool MagicWandMode::mouseReleased (MouseEventData const& data)
{
	if (Super::mouseReleased (data))
		return true;

	if (data.releasedButtons & Qt::LeftButton and not data.mouseMoved)
	{
		MagicType wandtype = MagicWandMode::Set;

		if (data.keymods & Qt::ShiftModifier)
			wandtype = MagicWandMode::Additive;
		else if (data.keymods & Qt::ControlModifier)
			wandtype = MagicWandMode::Subtractive;

		LDObject* obj = renderer()->pickOneObject (data.ev->x(), data.ev->y());
		doMagic (obj, wandtype);
		return true;
	}

	return false;
}

mercurial