Skip to content

Commit

Permalink
move and hide modules when 2nd button is clicked
Browse files Browse the repository at this point in the history
  • Loading branch information
AdamPorcineFudgepuppy committed Nov 21, 2024
1 parent 98e8eca commit 172725c
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 33 deletions.
9 changes: 4 additions & 5 deletions res/ComputerscareCondensePanel.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
125 changes: 97 additions & 28 deletions src/ComputerscareCondense.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@


struct CondenseModule : ComputerscareMenuParamModule {
enum ParamIds {
enum ParamIds {
ENABLED,
TOGGLE_HIDE,

Expand All @@ -25,21 +25,95 @@ enum LightIds {

int counter = 0;
int maxCounter = 2000;
Vec* widgetPosition = nullptr; // Pointer to the widget's position
Vec* widgetSize = nullptr; // Pointer to the widget's position

std::unordered_map<int, bool> visibilityMap; // Tracks visibility state by module ID
std::unordered_map<int, Vec> originalPositions;
std::unordered_map<int, float> originalPositionsX;
std::unordered_map<int, float> originalPositionsY;

CondenseModule() {
CondenseModule() {

config(NUM_PARAMS, NUM_INPUTS, NUM_OUTPUTS, NUM_LIGHTS);
}


void toggleCollapse() {
bool allAligned = true;
float leftmostX = 0.f;
float upmostY = 0.f;
// Check if the trigger button is pressed
if (params[TOGGLE_HIDE].getValue() > 0.f) {

if (!widgetPosition || !widgetSize) {
DEBUG("Widget position not set");
return;
}

float condenseX = widgetPosition->x + widgetSize->x; // Right edge of the CondenseModule
float condenseY = widgetPosition->y; // Y position of the CondenseModule

//gather info
for (auto widget : APP->scene->rack->getSelected()) {
if (widget) {
int moduleId = widget->module->id;
// Toggle visibility state
bool currentState = visibilityMap[moduleId];


if (originalPositions.find(moduleId) == originalPositions.end()) {
originalPositions[moduleId] = widget->box.pos;
}
allAligned = allAligned && (leftmostX == widget->box.pos.x) && (upmostY == widget->box.pos.y);

leftmostX = std::max(leftmostX, widget->box.pos.x);
upmostY = std::max(upmostY, widget->box.pos.y);


// widget->setVisible(!currentState);
visibilityMap[moduleId] = !currentState;




}
}

// Toggle positions
DEBUG("allAligned: %i",allAligned);
for (auto widget : APP->scene->rack->getSelected()) {
if (widget && widget->module) {
int moduleId = widget->module->id;
widget->box.pos.x = condenseX;
widget->box.pos.y = condenseY;
widget->box.size.x = 2.f;
}
}

// Reset trigger button state
params[1].setValue(0.f);

config(NUM_PARAMS, NUM_INPUTS, NUM_OUTPUTS, NUM_LIGHTS);
}
}


void process(const ProcessArgs& args) override {
// Check if the "Condense" mode is active
// Check if the "Condense" mode is active


if(counter%maxCounter==0) {
bool condenseEnabled =params[ENABLED].getValue();
if (condenseEnabled) {

logSelected();
toggleCollapse();

}
counter++;
}

void logSelected() {
bool condenseEnabled =params[ENABLED].getValue();
if (condenseEnabled) {
condenseEnabled = false; // Prevent constant printing
// Access selected modules
for (auto widget : APP->scene->rack->getSelected()) {
Expand All @@ -50,46 +124,41 @@ void process(const ProcessArgs& args) override {
}
counter = 0;
}

// Check if the trigger button is pressed
if (params[TOGGLE_HIDE].getValue() > 0.f) { // Button is pressed
for (auto widget : APP->scene->rack->getSelected()) {
if (widget) {
int moduleId = widget->module->id;
// Toggle visibility state
bool currentState = visibilityMap[moduleId];
widget->setVisible(!currentState);
visibilityMap[moduleId] = !currentState;

DEBUG("Toggled visibility for Module ID: %d, New State: %s",
moduleId, !currentState ? "Hidden" : "Visible");
}
}
// Reset trigger button state
params[1].setValue(0.f);
}
}
counter++;
}
};

// Custom widget to add buttons
struct CondenseModuleWidget : ModuleWidget {
CondenseModuleWidget(CondenseModule* module) {
setModule(module);
box.size = Vec(3 * 15, 380);
setPanel(createPanel(asset::plugin(pluginInstance, "res/ComputerscareCondensePanel.svg")));

// Add "Condense" toggle button
addParam(createParam<CKSS>(mm2px(Vec(5, 20)), module, CondenseModule::ENABLED));


addParam(createParam<CKSS>(mm2px(Vec(25, 20)), module, 1));
addParam(createParam<CKSS>(mm2px(Vec(45, 20)), module, 2));
addParam(createParam<CKSS>(mm2px(Vec(10, 20)), module, 1));
addParam(createParam<CKSS>(mm2px(Vec(15, 20)), module, 2));

// Add trigger button
//addParam(createParam<LEDButton>(mm2px(Vec(5, 40)), module, 0));
// Optional: Add another toggle for future use if needed
}
void step() override {
ModuleWidget::step(); // Call parent step to ensure proper functionality

// Ensure that the module has access to the widget's position
if (module) {
// Update the module's widget position
CondenseModule* condenseModule = dynamic_cast<CondenseModule*>(module);
if (condenseModule) {
condenseModule->widgetPosition = &box.pos; // Update the widget position
condenseModule->widgetSize = &box.size; // Update the widget position

}
}
}
};

// Register the module
Expand Down

0 comments on commit 172725c

Please sign in to comment.