MCPcopy Create free account
hub / github.com/VCVRack/Rack / unsqueezeModulePos

Method unsqueezeModulePos

src/app/RackWidget.cpp:907–951  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

905}
906
907void RackWidget::unsqueezeModulePos(ModuleWidget* mw) {
908 math::Rect mwBox = mw->getGridBox();
909
910 // Collect modules to the left and right of old pos, including this module.
911 std::set<ModuleWidget*, decltype(compareModuleLeft)*> leftModules(compareModuleLeft);
912 std::set<ModuleWidget*, decltype(compareModuleLeft)*> rightModules(compareModuleLeft);
913 for (widget::Widget* w2 : internal->moduleContainer->children) {
914 ModuleWidget* mw2 = static_cast<ModuleWidget*>(w2);
915 // Skip this module
916 if (mw2 == mw)
917 continue;
918 // Modules must be on the same row as pos
919 if (mw2->getGridBox().getTop() != mwBox.getTop())
920 continue;
921 // Insert into leftModules or rightModules
922 if (mw2->getGridBox().getLeft() >= mwBox.getLeft())
923 rightModules.insert(mw2);
924 else
925 leftModules.insert(mw2);
926 }
927
928 // Immediate right/left modules
929 ModuleWidget* leftModule = leftModules.empty() ? NULL : *leftModules.rbegin();
930 ModuleWidget* rightModule = rightModules.empty() ? NULL : *rightModules.begin();
931
932 // Shove right modules back to empty space left by module.
933 if (leftModule && rightModule && (leftModule != mw) && (rightModule != mw) && leftModule->getGridBox().getRight() >= mwBox.getLeft() && rightModule->getGridBox().getLeft() <= mwBox.getRight()) {
934 float xLeft = mwBox.getLeft();
935 float xRight = mwBox.getRight();
936 for (auto it = rightModules.begin(); it != rightModules.end(); it++) {
937 widget::Widget* w2 = *it;
938 ModuleWidget* mw2 = static_cast<ModuleWidget*>(w2);
939 math::Rect mw2Box = mw2->getGridBox();
940 // Break when module is no longer touching
941 if (xRight < mw2Box.getLeft())
942 break;
943 // Shove module to the left
944 math::Rect newBox = mw2Box;
945 newBox.pos.x = xLeft;
946 mw2->setGridPosition(newBox.pos);
947 xLeft = newBox.getRight();
948 xRight = mw2Box.getRight();
949 }
950 }
951}
952
953void RackWidget::updateModuleOldPositions() {
954 internal->moduleOldPositions.clear();

Callers 1

removeActionMethod · 0.80

Calls 6

getGridBoxMethod · 0.80
getTopMethod · 0.80
getLeftMethod · 0.80
getRightMethod · 0.80
setGridPositionMethod · 0.80
emptyMethod · 0.45

Tested by

no test coverage detected