|
| 1 | +#include "pasteTool.h" |
| 2 | +#include "backend/backend.h" |
| 3 | + |
| 4 | +void PasteTool::activate() { |
| 5 | + CircuitTool::activate(); |
| 6 | + registerFunction("Tool Primary Activate", std::bind(&PasteTool::place, this, std::placeholders::_1)); |
| 7 | +} |
| 8 | + |
| 9 | +bool PasteTool::validatePlacement() const { |
| 10 | + SharedCopiedBlocks copiedBlocks = circuitView->getBackend()->getClipboard(); |
| 11 | + if (!copiedBlocks) return false; |
| 12 | + |
| 13 | + Vector totalOffset = Vector(lastPointerPosition.x, lastPointerPosition.y) + (Position() - copiedBlocks->getMinPosition()); |
| 14 | + |
| 15 | + for (const CopiedBlocks::CopiedBlockData& block : copiedBlocks->getCopiedBlocks()) { |
| 16 | + Position testPos = block.position + totalOffset; |
| 17 | + if (circuit->getBlockContainer()->checkCollision(testPos)) { |
| 18 | + return false; |
| 19 | + } |
| 20 | + } |
| 21 | + return true; |
| 22 | +} |
| 23 | + |
| 24 | +bool PasteTool::place(const Event* event) { |
| 25 | + if (!validatePlacement()) { return true; } |
| 26 | + |
| 27 | + SharedCopiedBlocks copiedBlocks = circuitView->getBackend()->getClipboard(); |
| 28 | + circuit->tryInsertCopiedBlocks(copiedBlocks, lastPointerPosition); |
| 29 | + |
| 30 | + return true; |
| 31 | +} |
| 32 | + |
| 33 | +// Preview is only shown for the primary parsed circuit, not the dependencies that will be created in a different circuit |
| 34 | +void PasteTool::updateElements() { |
| 35 | + if (!elementCreator.isSetup()) return; |
| 36 | + elementCreator.clear(); |
| 37 | + if (!pointerInView) return; |
| 38 | + |
| 39 | + SharedCopiedBlocks copiedBlocks = circuitView->getBackend()->getClipboard(); |
| 40 | + if (!copiedBlocks) return; |
| 41 | + |
| 42 | + Vector totalOffset = Vector(lastPointerPosition.x, lastPointerPosition.y) + (Position() - copiedBlocks->getMinPosition()); |
| 43 | + |
| 44 | + for (const CopiedBlocks::CopiedBlockData& block : copiedBlocks->getCopiedBlocks()) { |
| 45 | + elementCreator.addBlockPreview(BlockPreview(block.blockType, block.position + totalOffset, block.rotation)); |
| 46 | + } |
| 47 | +} |
0 commit comments