| 846 | } |
| 847 | |
| 848 | void RackWidget::squeezeModulePos(ModuleWidget* mw, math::Vec pos) { |
| 849 | math::Rect mwBox; |
| 850 | mwBox.pos = ((pos - RACK_OFFSET) / RACK_GRID_SIZE).round(); |
| 851 | mwBox.size = mw->getGridSize(); |
| 852 | |
| 853 | // Collect modules to the left and right of new pos |
| 854 | std::set<ModuleWidget*, decltype(compareModuleLeft)*> leftModules(compareModuleLeft); |
| 855 | std::set<ModuleWidget*, decltype(compareModuleLeft)*> rightModules(compareModuleLeft); |
| 856 | for (widget::Widget* w2 : internal->moduleContainer->children) { |
| 857 | ModuleWidget* mw2 = static_cast<ModuleWidget*>(w2); |
| 858 | // Skip this module |
| 859 | if (mw2 == mw) |
| 860 | continue; |
| 861 | // Modules must be on the same row as pos |
| 862 | if (mw2->getGridBox().getTop() != mwBox.getTop()) |
| 863 | continue; |
| 864 | // Insert into leftModules or rightModules |
| 865 | if (mw2->getGridBox().getLeft() >= mwBox.getLeft()) |
| 866 | rightModules.insert(mw2); |
| 867 | else |
| 868 | leftModules.insert(mw2); |
| 869 | } |
| 870 | |
| 871 | ModuleWidget* leftModule = leftModules.empty() ? NULL : *leftModules.rbegin(); |
| 872 | ModuleWidget* rightModule = rightModules.empty() ? NULL : *rightModules.begin(); |
| 873 | |
| 874 | // If there isn't enough space between the last leftModule and first rightModule, place module to the right of the leftModule and shove right modules. |
| 875 | if (leftModule && rightModule && leftModule->getGridBox().getRight() + mwBox.getWidth() > rightModule->getGridBox().getLeft()) { |
| 876 | mwBox.pos.x = leftModule->getGridBox().getRight(); |
| 877 | |
| 878 | // Shove right modules |
| 879 | float xRight = mwBox.getRight(); |
| 880 | for (auto it = rightModules.begin(); it != rightModules.end(); it++) { |
| 881 | widget::Widget* w2 = *it; |
| 882 | ModuleWidget* mw2 = static_cast<ModuleWidget*>(w2); |
| 883 | math::Rect mw2Box = mw2->getGridBox(); |
| 884 | // Break when module no longer needs to be shoved |
| 885 | if (mw2Box.getLeft() >= xRight) |
| 886 | break; |
| 887 | // Shove module to the right of the last module |
| 888 | math::Rect newBox = mw2Box; |
| 889 | newBox.pos.x = xRight; |
| 890 | mw2->setGridPosition(newBox.pos); |
| 891 | xRight = newBox.getRight(); |
| 892 | } |
| 893 | } |
| 894 | // Place right of leftModule |
| 895 | else if (leftModule && leftModule->getGridBox().getRight() > mwBox.getLeft()) { |
| 896 | mwBox.pos.x = leftModule->getGridBox().getRight(); |
| 897 | } |
| 898 | // Place left of rightModule |
| 899 | else if (rightModule && rightModule->getGridBox().getLeft() < mwBox.getRight()) { |
| 900 | mwBox.pos.x = rightModule->getGridBox().getLeft() - mwBox.getWidth(); |
| 901 | } |
| 902 | |
| 903 | // Commit new pos |
| 904 | mw->setGridPosition(mwBox.pos); |
| 905 | } |
no test coverage detected