unsigned int Editor::addBox(int boxBeginPos, int boxLength, unsigned int motherId) { if (motherId == NO_ID) { if (m_boxIdToContainingCSP.find(ROOT_BOX_ID) == m_boxIdToContainingCSP.end()) { m_mainCSP->addBox(m_nextBoxId, boxBeginPos, boxLength, motherId, boxLength); m_boxIdToContainingCSP[m_nextBoxId] = m_mainCSP; } else { return NO_ID; } } else { ConstrainedBox* motherBox = getBoxById(motherId); if ((boxBeginPos < motherBox->lengthValue()) && (boxBeginPos + boxLength) < motherBox->lengthValue()) { CSP* motherCSP = motherBox->getCSP(); motherCSP->addBox(m_nextBoxId, boxBeginPos, boxLength, motherId, motherBox->lengthValue()); m_boxIdToContainingCSP[m_nextBoxId] = motherCSP; motherCSP->getBoxById(m_nextBoxId)->setMother(motherBox); if (motherId != ROOT_BOX_ID) { if (motherBox->getHierarchyRelationId() == NO_ID) { std::vector<unsigned int> movedBox; unsigned int hierarchyRelation = addAntPostRelation(motherId, BEGIN_CONTROL_POINT_INDEX, motherId, END_CONTROL_POINT_INDEX, ANTPOST_ANTERIORITY, motherBox->lengthValue(), motherBox->lengthValue(), movedBox); motherBox->setHierarchyInformations(hierarchyRelation); } } } else { return NO_ID; } } ++m_nextBoxId; return (m_nextBoxId - 1); }
bool Editor::performMoving(unsigned int boxId, int x, int y, vector<unsigned int>& movedBoxes, int maxModification) { CSP* containingCSP = m_boxIdToContainingCSP[boxId]; ConstrainedBox* currentBox = containingCSP->getBoxById(boxId); ConstrainedBox* motherBox = currentBox->mother(); // if ((x > motherBox->lengthValue()) || (y > motherBox->lengthValue())) { // return false; // } containingCSP->changeAllBoxMaxSceneWidth(motherBox->lengthValue()); CSP* boxCSP = currentBox->getCSP(); int previousX = getBeginValue(boxId); int previousY = getEndValue(boxId); if (boxCSP != NULL) { std::vector<unsigned int> boxesId; boxCSP->getAllBoxesId(boxesId); if (boxesId.size() != 0) { unsigned int maxValue = 0; for (unsigned int i = 0; i < boxesId.size(); ++i) { unsigned int currentChildBoxEnd = boxCSP->getEndValue(boxesId[i]); if(currentChildBoxEnd > maxValue) { maxValue = currentChildBoxEnd; } } if ((unsigned int)(y - x) < maxValue) { return false; } } } int modificationX = abs(previousX - x); int modificationY = abs(previousY - y); if (maxModification < modificationX) { maxModification = modificationX; } if (maxModification < modificationY) { maxModification = modificationY; } unsigned int hierarchyRelation = currentBox->getHierarchyRelationId(); if (hierarchyRelation != NO_ID) { containingCSP->removeTemporalRelation(hierarchyRelation); } std::vector<unsigned int> hierarchyRelationMovedBoxes; if(containingCSP->performMoving(boxId, x, y, movedBoxes, maxModification)) { if (hierarchyRelation != NO_ID) { containingCSP->addAntPostRelation(hierarchyRelation, boxId, BEGIN_CONTROL_POINT_INDEX, boxId, END_CONTROL_POINT_INDEX, ANTPOST_ANTERIORITY, currentBox->lengthValue(), currentBox->lengthValue(), hierarchyRelationMovedBoxes, false); } movedBoxes.clear(); getAllBoxesId(movedBoxes); return true; } else { // put back the CSP into its previous state. containingCSP->performMoving(boxId, previousX, previousY, movedBoxes, maxModification); movedBoxes.clear(); getAllBoxesId(movedBoxes); return false; } }