1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740
|
/**
bespoke synth, a software modular synthesizer
Copyright (C) 2021 Ryan Challinor (contact: awwbees@gmail.com)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
**/
/*
==============================================================================
ModuleContainer.cpp
Created: 16 Oct 2016 3:47:21pm
Author: Ryan Challinor
==============================================================================
*/
#include "ModuleContainer.h"
#include "ModularSynth.h"
#include "PatchCableSource.h"
#include "TitleBar.h"
#include "PerformanceTimer.h"
#include "SynthGlobals.h"
#include "QuickSpawnMenu.h"
#include "Prefab.h"
#include "juce_core/juce_core.h"
ModuleContainer::ModuleContainer()
{
}
ofVec2f ModuleContainer::GetOwnerPosition() const
{
if (mOwner)
return mOwner->GetPosition();
return ofVec2f();
}
void ModuleContainer::GetAllModules(std::vector<IDrawableModule*>& out)
{
out.insert(out.begin(), mModules.begin(), mModules.end());
for (int i = 0; i < mModules.size(); ++i)
{
if (mModules[i]->GetContainer())
mModules[i]->GetContainer()->GetAllModules(out);
}
}
void ModuleContainer::DrawContents()
{
DrawPatchCables(!K(parentMinimized), !K(inFront));
DrawModules();
DrawPatchCables(!K(parentMinimized), K(inFront));
DrawUnclipped();
}
void ModuleContainer::DrawModules()
{
for (int i = (int)mModules.size() - 1; i >= 0; --i)
{
if (!mModules[i]->AlwaysOnTop())
mModules[i]->Draw();
}
for (int i = (int)mModules.size() - 1; i >= 0; --i)
{
if (mModules[i]->AlwaysOnTop())
mModules[i]->Draw();
}
}
void ModuleContainer::DrawUnclipped()
{
for (int i = (int)mModules.size() - 1; i >= 0; --i)
{
if (!mModules[i]->AlwaysOnTop())
mModules[i]->RenderUnclipped();
}
for (int i = (int)mModules.size() - 1; i >= 0; --i)
{
if (mModules[i]->AlwaysOnTop())
mModules[i]->RenderUnclipped();
}
}
void ModuleContainer::PostRender()
{
for (int i = (int)mModules.size() - 1; i >= 0; --i)
mModules[i]->PostRender();
}
void ModuleContainer::DrawPatchCables(bool parentMinimized, bool inFront)
{
if (mOwner != nullptr && mOwner->Minimized())
parentMinimized = true;
for (int i = (int)mModules.size() - 1; i >= 0; --i)
{
mModules[i]->DrawPatchCables(parentMinimized, inFront);
if (mModules[i]->GetContainer())
mModules[i]->GetContainer()->DrawPatchCables(parentMinimized, inFront);
}
}
void ModuleContainer::Poll()
{
if (mOwner != nullptr)
return;
for (int i = 0; i < mModules.size(); ++i)
{
if (mModules[i]->GetContainer())
mModules[i]->GetContainer()->Poll();
mModules[i]->BasePoll();
}
}
void ModuleContainer::Clear()
{
std::vector<IDrawableModule*> modulesToDelete = mModules;
for (auto* module : modulesToDelete)
{
if (module)
{
if (module->GetContainer())
module->GetContainer()->Clear();
if (module->IsSingleton() == false)
DeleteModule(module);
}
}
mModules.clear();
}
void ModuleContainer::Exit()
{
for (int i = 0; i < mModules.size(); ++i)
{
if (mModules[i]->GetContainer())
mModules[i]->GetContainer()->Exit();
mModules[i]->Exit();
}
}
void ModuleContainer::KeyPressed(int key, bool isRepeat)
{
for (int i = 0; i < mModules.size(); ++i)
{
if (mModules[i]->GetContainer())
mModules[i]->GetContainer()->KeyPressed(key, isRepeat);
mModules[i]->KeyPressed(key, isRepeat);
}
}
void ModuleContainer::KeyReleased(int key)
{
for (int i = 0; i < mModules.size(); ++i)
{
if (mModules[i]->GetContainer())
mModules[i]->GetContainer()->KeyReleased(key);
mModules[i]->KeyReleased(key);
}
}
void ModuleContainer::MouseMoved(float x, float y)
{
if (mOwner != nullptr)
return;
for (int i = (int)mModules.size() - 1; i >= 0; --i) //run this backwards so that we can figure out the top hover control
{
ModuleContainer* subcontainer = mModules[i]->GetContainer();
if (subcontainer)
{
subcontainer->MouseMoved(x - subcontainer->GetOwnerPosition().x, y - subcontainer->GetOwnerPosition().y);
}
mModules[i]->NotifyMouseMoved(x, y);
}
}
void ModuleContainer::MouseReleased()
{
if (mOwner != nullptr)
return;
for (int i = 0; i < mModules.size(); i++)
{
ModuleContainer* subcontainer = mModules[i]->GetContainer();
if (subcontainer)
subcontainer->MouseReleased();
mModules[i]->MouseReleased();
}
}
IDrawableModule* ModuleContainer::GetModuleAt(float x, float y)
{
if (mOwner != nullptr)
{
float ownerX, ownerY;
mOwner->GetPosition(ownerX, ownerY);
x -= ownerX;
y -= ownerY;
}
const auto& modalItems = TheSynth->GetModalFocusItemStack();
for (int i = (int)modalItems.size() - 1; i >= 0; --i)
{
if (modalItems[i]->GetOwningContainer() == this && modalItems[i]->TestClick(x, y, false, true))
return modalItems[i];
}
for (int i = 0; i < mModules.size(); ++i)
{
if (mModules[i]->AlwaysOnTop() && mModules[i]->TestClick(x, y, false, true))
{
ModuleContainer* subcontainer = mModules[i]->GetContainer();
if (subcontainer)
{
IDrawableModule* contained = subcontainer->GetModuleAt(x - subcontainer->GetOwnerPosition().x, y - subcontainer->GetOwnerPosition().y);
if (contained)
{
return contained;
}
}
return mModules[i];
}
}
for (int i = 0; i < mModules.size(); ++i)
{
if (!mModules[i]->AlwaysOnTop() && mModules[i]->TestClick(x, y, false, true))
{
ModuleContainer* subcontainer = mModules[i]->GetContainer();
if (subcontainer)
{
IDrawableModule* contained = subcontainer->GetModuleAt(x, y);
if (contained)
{
return contained;
}
}
return mModules[i];
}
}
return nullptr;
}
ofVec2f ModuleContainer::GetDrawOffset()
{
if (mOwner != nullptr && mOwner->GetOwningContainer() != nullptr)
return mDrawOffset + mOwner->GetOwningContainer()->GetDrawOffset();
return mDrawOffset;
}
float ModuleContainer::GetDrawScale() const
{
if (mOwner != nullptr && mOwner->GetOwningContainer() != nullptr)
return mDrawScale * mOwner->GetOwningContainer()->GetDrawScale();
return mDrawScale;
}
void ModuleContainer::GetModulesWithinRect(ofRectangle rect, std::vector<IDrawableModule*>& output, bool ignorePinned /* = false */)
{
output.clear();
for (int i = 0; i < mModules.size(); ++i)
{
if (mModules[i]->IsWithinRect(rect) && mModules[i] != TheQuickSpawnMenu && mModules[i]->IsShowing() && (ignorePinned && !mModules[i]->Pinned()))
output.push_back(mModules[i]);
}
}
void ModuleContainer::MoveToFront(IDrawableModule* module)
{
if (mOwner && mOwner->GetOwningContainer() != nullptr)
mOwner->GetOwningContainer()->MoveToFront(mOwner);
for (int i = 0; i < mModules.size(); ++i)
{
if (mModules[i] == module)
{
for (int j = i; j > 0; --j)
mModules[j] = mModules[j - 1];
mModules[0] = module;
break;
}
}
}
void ModuleContainer::AddModule(IDrawableModule* module)
{
mModules.push_back(module);
MoveToFront(module);
TheSynth->OnModuleAdded(module);
module->SetOwningContainer(this);
if (mOwner)
mOwner->AddChild(module);
}
void ModuleContainer::TakeModule(IDrawableModule* module)
{
assert(module->GetOwningContainer()); //module must already be in a container
ofVec2f oldOwnerPos = module->GetOwningContainer()->GetOwnerPosition();
if (module->GetOwningContainer()->mOwner)
module->GetOwningContainer()->mOwner->RemoveChild(module);
RemoveFromVector(module, module->GetOwningContainer()->mModules);
std::string newName = GetUniqueName(module->Name(), mModules);
mModules.push_back(module);
MoveToFront(module);
ofVec2f offset = oldOwnerPos - GetOwnerPosition();
module->SetPosition(module->GetPosition(true).x + offset.x,
module->GetPosition(true).y + offset.y);
module->SetOwningContainer(this);
if (mOwner)
mOwner->AddChild(module);
else //root modulecontainer
module->SetName(newName.c_str());
}
void ModuleContainer::DeleteModule(IDrawableModule* module, bool fail /*= true*/)
{
if (!module->CanBeDeleted())
return;
if (module->HasSpecialDelete())
{
module->DoSpecialDelete();
RemoveFromVector(module, mModules, fail);
return;
}
if (module->GetParent())
module->GetParent()->GetModuleParent()->RemoveChild(module);
RemoveFromVector(module, mModules, fail);
for (const auto iter : mModules)
{
if (iter->GetPatchCableSource())
{
std::vector<PatchCable*> cablesToDestroy;
for (auto cable : iter->GetPatchCableSource()->GetPatchCables())
{
if (cable->GetTarget() == module)
cablesToDestroy.push_back(cable);
}
for (const auto cable : cablesToDestroy)
cable->Destroy(false);
}
}
// Remove all cables that targeted controls on this module
IUIControl::DestroyCablesTargetingControls(module->GetUIControls());
for (const auto child : module->GetChildren())
{
child->MarkAsDeleted();
child->SetEnabled(false);
child->Exit();
TheSynth->OnModuleDeleted(child);
}
module->MarkAsDeleted();
module->SetEnabled(false);
module->Exit();
TheSynth->OnModuleDeleted(module);
}
void ModuleContainer::DeleteCablesForControl(const IUIControl* control)
{
// Remove all cables that targetted control on this module
std::vector<IDrawableModule*> modules;
TheSynth->GetAllModules(modules);
std::vector<PatchCable*> cablesToDestroy;
for (const auto module_iter : modules)
{
for (const auto source : module_iter->GetPatchCableSources())
{
for (const auto cable : source->GetPatchCables())
{
if (cable->GetTarget() == control)
{
cablesToDestroy.push_back(cable);
break;
}
}
}
}
for (const auto cable : cablesToDestroy)
cable->Destroy(false);
}
IDrawableModule* ModuleContainer::FindModule(std::string name, bool fail)
{
/*string ownerPath = "";
if (mOwner)
ownerPath = mOwner->Path();
if (strstr(name.c_str(), ownerPath.c_str()) != name.c_str()) //path doesn't start with ownerPath
return nullptr;
name = name.substr(ownerPath.length());*/
if (name == "")
return nullptr;
for (int i = 0; i < mModules.size(); ++i)
{
if (name == mModules[i]->Name())
return mModules[i];
std::vector<std::string> tokens = ofSplitString(name, "~");
if (mModules[i]->GetContainer())
{
if (tokens[0] == mModules[i]->Name())
{
ofStringReplace(name, tokens[0] + "~", "", true);
return mModules[i]->GetContainer()->FindModule(name, fail);
}
}
if (tokens.size() == 2 && tokens[0] == mModules[i]->Name())
{
IDrawableModule* child = nullptr;
try
{
child = mModules[i]->FindChild(tokens[1], fail);
}
catch (UnknownModuleException& e)
{
}
if (child)
return child;
}
}
if (fail)
throw UnknownModuleException(name);
return nullptr;
}
IUIControl* ModuleContainer::FindUIControl(std::string path)
{
/*string ownerPath = "";
if (mOwner)
ownerPath = mOwner->Path();
if (strstr(path.c_str(), ownerPath.c_str()) != path.c_str()) //path doesn't start with ownerPath
return nullptr;
path = path.substr(ownerPath.length());*/
if (path == "")
return nullptr;
std::vector<std::string> tokens = ofSplitString(path, "~");
std::string control = tokens[tokens.size() - 1];
std::string modulePath = path.substr(0, path.length() - (control.length() + 1));
IDrawableModule* module = FindModule(modulePath, false);
if (module)
{
try
{
module->OnUIControlRequested(control.c_str());
return module->FindUIControl(control.c_str());
}
catch (UnknownUIControlException& e)
{
TheSynth->LogEvent("Couldn't find UI control at path \"" + path + "\"", kLogEventType_Error);
return nullptr;
}
}
TheSynth->LogEvent("Couldn't find module at path \"" + modulePath + "\"", kLogEventType_Error);
return nullptr;
}
bool ModuleContainer::IsHigherThan(IDrawableModule* checkFor, IDrawableModule* checkAgainst) const
{
while (checkFor->GetParent())
checkFor = dynamic_cast<IDrawableModule*>(checkFor->GetParent());
for (int i = 0; i < mModules.size(); ++i)
{
if (checkFor == mModules[i])
return true;
if (checkAgainst == mModules[i])
return false;
}
return false;
}
void ModuleContainer::LoadModules(const ofxJSONElement& modules)
{
PerformanceTimer timer;
{
TimerInstance t("load", timer);
//two-pass loading for dependencies
if (mOwner)
IClickable::SetLoadContext(mOwner);
{
TimerInstance t("create", timer);
for (int i = 0; i < modules.size(); ++i)
{
try
{
TimerInstance t("create " + modules[i]["name"].asString(), timer);
IDrawableModule* module = TheSynth->CreateModule(modules[i]);
if (module != nullptr)
{
//ofLog() << "create " << module->Name();
AddModule(module);
}
}
catch (LoadingJSONException& e)
{
TheSynth->LogEvent("Couldn't load json", kLogEventType_Error);
}
}
}
{
TimerInstance t("setup", timer);
for (int i = 0; i < modules.size(); ++i)
{
try
{
TimerInstance t("setup " + modules[i]["name"].asString(), timer);
IDrawableModule* module = FindModule(modules[i]["name"].asString(), true);
if (module != nullptr)
{
//ofLog() << "setup " << module->Name();
TheSynth->SetUpModule(module, modules[i]);
}
}
catch (LoadingJSONException& e)
{
TheSynth->LogEvent("Couldn't set up modules from json", kLogEventType_Error);
}
catch (UnknownModuleException& e)
{
TheSynth->LogEvent("Couldn't find module \"" + e.mSearchName + "\"", kLogEventType_Error);
}
}
}
{
TimerInstance t("init", timer);
for (int i = 0; i < mModules.size(); ++i)
{
TimerInstance t(std::string("init ") + mModules[i]->Name(), timer);
if (mModules[i]->IsSingleton() == false)
mModules[i]->Init();
}
}
IClickable::ClearLoadContext();
}
}
bool ModuleSorter(const IDrawableModule* a, const IDrawableModule* b)
{
return std::string(a->Name()) < std::string(b->Name());
}
ofxJSONElement ModuleContainer::WriteModules()
{
if (mOwner)
IClickable::SetSaveContext(mOwner);
for (auto i = mModules.begin(); i != mModules.end(); ++i)
UpdateTarget(*i);
ofxJSONElement modules;
std::vector<IDrawableModule*> saveModules;
for (int i = 0; i < mModules.size(); ++i)
{
IDrawableModule* module = mModules[i];
if (module->IsSaveable())
saveModules.push_back(module);
}
sort(saveModules.begin(), saveModules.end(), ModuleSorter);
modules.resize((unsigned int)saveModules.size());
for (int i = 0; i < saveModules.size(); ++i)
{
ofxJSONElement moduleInfo;
saveModules[i]->SaveLayoutBase(moduleInfo);
modules[i] = moduleInfo;
}
IClickable::ClearSaveContext();
return modules;
}
void ModuleContainer::SaveState(FileStreamOut& out)
{
out << ModularSynth::kSaveStateRev;
int savedModules = 0;
for (auto* module : mModules)
{
if (module->IsSaveable())
++savedModules;
}
out << savedModules;
if (mOwner)
IClickable::SetSaveContext(mOwner);
for (auto* module : mModules)
{
if (module->IsSaveable())
{
//ofLog() << "Saving " << module->Name();
out << std::string(module->Name());
module->SaveState(out);
for (int i = 0; i < GetModuleSeparatorLength(); ++i)
out << GetModuleSeparator()[i];
}
}
IClickable::ClearSaveContext();
}
void ModuleContainer::LoadState(FileStreamIn& in)
{
Prefab::sLastLoadWasPrefab = Prefab::sLoadingPrefab;
bool wasLoadingState = TheSynth->IsLoadingState();
TheSynth->SetIsLoadingState(true);
static int sModuleContainerLoadStack = 0;
++sModuleContainerLoadStack;
int header;
in >> header;
assert(header <= ModularSynth::kSaveStateRev);
ModularSynth::sLoadingFileSaveStateRev = header;
ModularSynth::sLastLoadedFileSaveStateRev = header;
int savedModules;
in >> savedModules;
if (mOwner)
IClickable::SetLoadContext(mOwner);
for (int i = 0; i < savedModules; ++i)
{
std::string moduleName;
in >> moduleName;
//ofLog() << "Loading " << moduleName;
IDrawableModule* module = FindModule(moduleName, false);
try
{
if (module == nullptr)
throw LoadStateException();
module->LoadState(in, module->LoadModuleSaveStateRev(in));
for (int j = 0; j < GetModuleSeparatorLength(); ++j)
{
char separatorChar;
in >> separatorChar;
if (separatorChar != GetModuleSeparator()[j])
{
ofLog() << "Error loading state for " << module->Name();
//something went wrong, let's print some info to try to figure it out
ofLog() << "Read char " + ofToString(separatorChar) + " but expected " + GetModuleSeparator()[j] + "!";
ofLog() << "Save state file position is " + ofToString(in.GetFilePosition()) + ", EoF is " + (in.Eof() ? "true" : "false");
std::string nextFewChars = "Next 10 characters are:";
for (int c = 0; c < 10; ++c)
{
char ch;
in >> ch;
nextFewChars += ofToString(ch);
}
ofLog() << nextFewChars;
}
assert(separatorChar == GetModuleSeparator()[j]);
}
}
catch (LoadStateException& e)
{
TheSynth->LogEvent("Error loading state for module \"" + moduleName + "\"", kLogEventType_Error);
//read through the rest of the module until we find the spacer, so we can continue loading the next module
int separatorProgress = 0;
juce::uint64 safetyCheck = 0;
while (!in.Eof() && safetyCheck < 1000000)
{
char val;
in >> val;
if (val == GetModuleSeparator()[separatorProgress])
++separatorProgress;
else
separatorProgress = 0;
if (separatorProgress == GetModuleSeparatorLength())
break; //we did it!
++safetyCheck;
}
}
}
for (auto module : mModules)
module->PostLoadState();
IClickable::ClearLoadContext();
TheSynth->SetIsLoadingState(wasLoadingState);
--sModuleContainerLoadStack;
if (sModuleContainerLoadStack <= 0)
ModularSynth::sLoadingFileSaveStateRev = ModularSynth::kSaveStateRev; //reset to current
}
//static
bool ModuleContainer::DoesModuleHaveMoreSaveData(FileStreamIn& in)
{
char test[GetModuleSeparatorLength() + 1];
in.Peek(test, GetModuleSeparatorLength());
test[GetModuleSeparatorLength()] = 0;
if (strcmp(GetModuleSeparator(), test) == 0)
return false;
return true;
}
|