GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — plugdata/Source/Object.cpp at develop · plugdata-team/plugdata Expand file tree/ Copy pathObject.cppMore file actions 1509 lines (1217 loc) · 53 KB / Copy pathObject.cppFile metadata and controls1509 lines (1217 loc) · 53 KB 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 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 /* // Copyright (c) 2021-2025 Timothy Schoen. // For information on usage and redistribution, and for a DISCLAIMER OF ALL // WARRANTIES, see the file, "LICENSE.txt," in this distribution. */ #include <juce_gui_basics/juce_gui_basics.h> #include "Utility/Config.h" #include "Utility/Fonts.h" #include "Utility/CachedStringWidth.h" #include "Object.h" #include "Canvas.h" #include "Components/SuggestionComponent.h" #include "Connection.h" #include "Iolet.h" #include "Constants.h" #include "PluginEditor.h" #include "PluginProcessor.h" #include "Sidebar/Sidebar.h" #include "ObjectGrid.h" #include "Objects/ObjectBase.h" #include "Objects/TextObject.h" #include "Dialogs/Dialogs.h" #include "Heavy/CompatibleObjects.h" #include "Pd/Patch.h" extern "C" { #include <m_pd.h> } Object::Object(Canvas* parent, String const& name, Point<int> const position) : NVGComponent(this) , cnv(parent) , editor(parent->editor) , ds(parent->dragState) { setTopLeftPosition(position - Point<int>(margin, margin)); initialise(); // Open editor for undefined objects // Delay the setting of the type to prevent creating an invalid object first if (name.isEmpty()) { setSize(58, height); } else { setType(name); } if (!getValue<bool>(locked)) { showEditor(); } updater.addAnimator(activityStateFade); } Object::Object(pd::WeakReference object, Canvas* parent) : NVGComponent(this) , cnv(parent) , editor(parent->editor) , ds(parent->dragState) { initialise(); setType("", object); updater.addAnimator(activityStateFade); } Object::~Object() { hideEditor(); // Make sure the editor is not still open, that could lead to issues with listeners attached to the editor (i.e. suggestioncomponent) cnv->selectedComponents.removeChangeListener(this); } Rectangle<int> Object::getObjectBounds() const { return getBounds().reduced(margin) - cnv->canvasOrigin; } Rectangle<int> Object::getSelectableBounds() const { if (gui) { return gui->getSelectableBounds() + cnv->canvasOrigin; } return getBounds().reduced(margin); } void Object::setObjectBounds(Rectangle<int> const bounds) { setBounds(bounds.expanded(margin) + cnv->canvasOrigin); } void Object::initialise() { setCachedComponentImage(new NVGSurface::InvalidationChecker([this](){ commandBufferDirty = true; })); cnv->objectLayer.addAndMakeVisible(this); cnv->selectedComponents.addChangeListener(this); // Updates lock/unlock mode locked.referTo(cnv->locked); commandLocked.referTo(cnv->pd->commandLocked); presentationMode.referTo(cnv->presentationMode); hvccMode.referTo(SettingsFile::getInstance()->getPropertyAsValue("hvcc_mode")); patchDownwardsOnly.referTo(SettingsFile::getInstance()->getPropertyAsValue("patch_downwards_only")); presentationMode.addListener(this); locked.addListener(this); commandLocked.addListener(this); originalBounds.setBounds(0, 0, 0, 0); setAccessible(false); } void Object::changeListenerCallback(ChangeBroadcaster* source) { if (auto const selectedItems = dynamic_cast<SelectedItemSet<WeakReference<Component>>*>(source)) setSelected(selectedItems->isSelected(this)); } void Object::setSelected(bool const shouldBeSelected) { if (selectedFlag != shouldBeSelected) { selectedFlag = shouldBeSelected; repaint(); } } bool Object::isSelected() const { return selectedFlag; } void Object::settingsChanged(String const& name, var const& value) { if (name == "hvcc_mode") { if (gui) { if (value) isHvccCompatible = gui->checkHvccCompatibility(); else isHvccCompatible = true; } repaint(); } if (name == "touch_mode") { updateIoletGeometry(); repaint(); } if (name == "overlays") { repaint(); } } void Object::valueChanged(Value& v) { if (v.refersToSameSourceAs(cnv->presentationMode)) { // else it was a lock/unlock/presentation mode action // Hide certain objects in GOP setVisible(!((cnv->isGraph || cnv->presentationMode == var(true)) && gui && gui->hideInGraph())); } else if (v.refersToSameSourceAs(cnv->locked) || v.refersToSameSourceAs(cnv->commandLocked)) { if (gui) { gui->lock(cnv->isGraph || locked == var(true) || commandLocked == var(true)); } } } bool Object::hitTest(int const x, int const y) { if (::getValue<bool>(presentationMode)) { if (cnv->isPointOutsidePluginArea(cnv->getLocalPoint(this, Point<int>(x, y)))) return false; } if (cnv->panningModifierDown()) return false; // If the hit-test get's to here, and any of these are still true // return! Otherwise it will test non-existent iolets and return true! bool const blockIolets = presentationMode.getValue() || locked.getValue() || commandLocked.getValue(); // Mouse over iolets for (auto const* iolet : iolets) { if (!blockIolets && iolet->getBounds().contains(x, y)) return true; } if (selectedFlag) { for (auto& corner : getCorners()) { if (corner.contains(x, y)) return true; } return getLocalBounds().reduced(margin).contains(x, y); } if (gui && !gui->canReceiveMouseEvent(x, y)) { return false; } // Mouse over object if (getLocalBounds().reduced(margin).contains(x, y)) { return true; } return false; } // To make iolets show/hide void Object::mouseEnter(MouseEvent const& e) { drawIoletExpanded = true; if (!getValue<bool>(locked)) { repaint(); } } void Object::mouseExit(MouseEvent const& e) { // we need to reset the resizeZone & validResizeZone, // otherwise it can have an old zone already selected on re-entry resizeZone = ResizableBorderComponent::Zone(ResizableBorderComponent::Zone::centre); validResizeZone = false; drawIoletExpanded = false; if (!getValue<bool>(locked)) { repaint(); } } void Object::mouseMove(MouseEvent const& e) { using Zone = ResizableBorderComponent::Zone; using Direction = ObjectBase::ResizeDirection; if (!selectedFlag || locked == var(true) || commandLocked == var(true)) { setMouseCursor(MouseCursor::NormalCursor); updateMouseCursor(); return; } int zone = 0; auto const b = getLocalBounds().toFloat().reduced(margin - 2); if (b.contains(e.position) && !b.reduced(7).contains(e.position)) { auto corners = getCorners(); auto const minW = jmax(b.getWidth() / 10.0f, jmin(10.0f, b.getWidth() / 3.0f)); auto const minH = jmax(b.getHeight() / 10.0f, jmin(10.0f, b.getHeight() / 3.0f)); if (corners[0].contains(e.position) || corners[1].contains(e.position) || (e.position.x < jmax(7.0f, minW) && b.getX() > 0.0f)) zone |= Zone::left; else if (corners[2].contains(e.position) || corners[3].contains(e.position) || e.position.x >= b.getWidth() - jmax(7.0f, minW)) zone |= Zone::right; if (corners[0].contains(e.position) || corners[3].contains(e.position) || e.position.y < jmax(7.0f, minH)) zone |= Zone::top; else if (corners[1].contains(e.position) || corners[2].contains(e.position) || e.position.y >= b.getHeight() - jmax(7.0f, minH)) zone |= Zone::bottom; } auto allowedDirection = gui ? gui->getAllowedResizeDirections() : Direction::None; switch (allowedDirection) { case Direction::None: { zone = 0; break; } case Direction::HorizontalOnly: { zone &= (Zone::left | Zone::right); break; } case Direction::VerticalOnly: { zone &= (Zone::top | Zone::bottom); break; } case Direction::DiagonalOnly: { bool const hasHorizontal = zone & (Zone::left | Zone::right); bool const hasVertical = zone & (Zone::top | Zone::bottom); if (!(hasHorizontal && hasVertical)) zone = 0; break; } default: break; // Allow any direction } resizeZone = static_cast<Zone>(zone); validResizeZone = resizeZone.getZoneFlags() != ResizableBorderComponent::Zone::centre && e.originalComponent == this && !(gui && gui->isEditorShown()) && !newObjectEditor; setMouseCursor(validResizeZone ? resizeZone.getMouseCursor() : MouseCursor::NormalCursor); updateMouseCursor(); } void Object::applyBounds() { UnorderedMap<Object*, Rectangle<int>> newObjectSizes; for (auto* obj : cnv->getSelectionOfType<Object>()) newObjectSizes[obj] = obj->getObjectBounds(); auto const positionOffset = gui ? getBounds().reduced(margin).getPosition() - cnv->canvasOrigin - gui->getPdBounds().getPosition() : Point<int>(0, 0); auto* patch = &cnv->patch; cnv->pd->lockAudioThread(); if (ds.wasResized || ds.wasDragDuplicated) { patch->startUndoSequence("Resize"); for (auto& [object, bounds] : newObjectSizes) { if (object->gui) object->gui->setPdBounds(bounds); } if (auto* patch = cnv->patch.getRawPointer()) { canvas_dirty(patch, 1); } patch->endUndoSequence("Resize"); } else if (ds.didStartDragging) { patch->startUndoSequence("Move"); SmallArray<t_gobj*> objects; for (auto const* obj : cnv->getSelectionOfType<Object>()) { if (auto* ptr = obj->getPointer()) { objects.add(ptr); } } cnv->patch.moveObjects(objects, positionOffset.x, positionOffset.y); patch->endUndoSequence("Move"); } cnv->pd->unlockAudioThread(); MessageManager::callAsync([editor = SafePointer(this->editor)] { if (editor) editor->updateCommandStatus(); }); } void Object::updateBounds() { if (cnv->isGraph && gui && gui->hideInGraph()) return; // only update if we have a gui and the object isn't been moved by the user // otherwise PD hasn't been informed of the new position 'while' we are dragging // so we don't need to update the bounds when an object is being interacted with if (gui && !isObjectMouseActive) { // Get the bounds of the object in Pd auto const newBounds = gui->getPdBounds(); // Objects may return empty bounds if they are not a real object (like scalars) if (!newBounds.isEmpty()) setObjectBounds(newBounds); else setTopLeftPosition(newBounds.getX() + margin + cnv->canvasOrigin.x, newBounds.getY() + margin + cnv->canvasOrigin.y); } if (newObjectEditor) { textEditorTextChanged(*newObjectEditor); } } void Object::setType(String const& newType, pd::WeakReference existingObject) { // Change object type String type = newType.upToFirstOccurrenceOf(" ", false, false); pd::WeakReference objectPtr = nullptr; // "exists" indicates that this object already exists in pd // When setting exists to true, the gui needs to be assigned already if (!existingObject.isValid()) { auto* patch = &cnv->patch; if (gui) { // Clear connections to this object // They will be remade by the synchronise call later for (auto const* connection : getConnections()) cnv->connections.remove_one(connection); if (auto* checkedObject = pd::Interface::checkObject(getPointer())) { auto const renamedObject = patch->renameObject(checkedObject, newType); objectPtr = pd::WeakReference(renamedObject, cnv->pd); } // Synchronise to make sure connections are preserved correctly cnv->synchronise(); } else { auto const rect = getObjectBounds(); auto* newObject = patch->createObject(rect.getX(), rect.getY(), newType); objectPtr = pd::WeakReference(newObject, cnv->pd); } } else { objectPtr = existingObject; } if (!objectPtr.getRaw<t_gobj>()) { jassertfalse; return; } // Create gui for the object gui.reset(ObjectBase::createGui(objectPtr, this)); isGemObject = is_gem_object(gui->getText().toString().toRawUTF8()); if (gui) { gui->initialise(); gui->lock(cnv->isGraph || locked == var(true) || commandLocked == var(true)); gui->addMouseListener(this, true); addAndMakeVisible(gui.get()); if (getValue<bool>(hvccMode)) isHvccCompatible = gui->checkHvccCompatibility(); else isHvccCompatible = true; } // Update inlets/outlets updateIolets(); updateBounds(); resized(); // If bounds haven't changed, we'll still want to update gui and iolets bounds // Auto patching if (getValue<bool>(editor->autoconnect) && numInputs && cnv->lastSelectedObject && cnv->lastSelectedObject != this && cnv->lastSelectedObject->numOutputs) { auto* outlet = cnv->lastSelectedObject->iolets[cnv->lastSelectedObject->numInputs]; auto* inlet = iolets[0]; if (outlet->isSignal() == inlet->isSignal()) { // Call async to make sure the object is created before the connection MessageManager::callAsync([_this = SafePointer(this), outlet = SafePointer(outlet), inlet = SafePointer(inlet)] { if (!_this || !outlet || !inlet) return; _this->cnv->connections.add(_this->cnv, outlet.getComponent(), inlet.getComponent(), nullptr); }); } } if (cnv->lastSelectedConnection && numInputs && numOutputs) { // if 1 connection is selected, connect the new object in middle of connection auto const outobj = cnv->lastSelectedConnection->outobj; auto const inobj = cnv->lastSelectedConnection->inobj; auto* outlet = outobj->iolets[outobj->numInputs + cnv->lastSelectedConnection->outIdx]; auto* inlet = inobj->iolets[cnv->lastSelectedConnection->inIdx]; auto* checkedOut = pd::Interface::checkObject(outobj->getPointer()); auto* checkedIn = pd::Interface::checkObject(inobj->getPointer()); if (checkedOut && checkedIn && outlet->isSignal() == iolets[0]->isSignal() && inlet->isSignal() == iolets[this->numInputs]->isSignal()) { // Call async to make sure the object is created before the connection MessageManager::callAsync([_this = SafePointer(this), this, outlet = SafePointer(outlet), inlet = SafePointer(inlet)] { if (!_this || !outlet || !inlet || !numInputs || iolets.size() <= numInputs) return; cnv->connections.add(cnv, outlet.getComponent(), iolets[0], nullptr); cnv->connections.add(cnv, iolets[this->numInputs], inlet.getComponent(), nullptr); }); // remove the previous connection cnv->patch.removeConnection(checkedOut, cnv->lastSelectedConnection->outIdx, checkedIn, cnv->lastSelectedConnection->inIdx, cnv->lastSelectedConnection->getPathState()); cnv->connections.remove_one(cnv->lastSelectedConnection); } } cnv->lastSelectedObject = nullptr; cnv->lastSelectedConnection = nullptr; editor->updateCommandStatus(); cnv->synchroniseSplitCanvas(); cnv->pd->updateObjectImplementations(); } StackArray<Rectangle<float>, 4> Object::getCorners() const { auto const rect = getLocalBounds().reduced(margin); constexpr float offset = 2.0f; StackArray<Rectangle<float>, 4> corners = { Rectangle<float>(9.0f, 9.0f).withCentre(rect.getTopLeft().toFloat()).translated(offset, offset), Rectangle<float>(9.0f, 9.0f).withCentre(rect.getBottomLeft().toFloat()).translated(offset, -offset), Rectangle<float>(9.0f, 9.0f).withCentre(rect.getBottomRight().toFloat()).translated(-offset, -offset), Rectangle<float>(9.0f, 9.0f).withCentre(rect.getTopRight().toFloat()).translated(-offset, offset) }; return corners; } SmallString Object::getType(bool const withOriginPrefix) const { if (gui && withOriginPrefix) return gui->getTypeWithOriginPrefix(); if (gui) return gui->getType(); return SmallString(); } void Object::triggerOverlayActiveState() { if (rateReducer.tooFast()) return; if (!cnv->shouldShowObjectActivity()) return; activeStateAlpha = 1.0f; activityStateFade.start(); } void Object::lookAndFeelChanged() { if (gui) gui->updateLabel(); } void Object::moved() { if (cnv->suggestor && cnv->suggestor->isVisible()) { cnv->suggestor->updateBounds(); } } void Object::resized() { setVisible(!((cnv->isGraph || cnv->presentationMode == var(true)) && gui && gui->hideInGraph())); if (gui) { gui->setBounds(getLocalBounds().reduced(margin)); } if (newObjectEditor) { newObjectEditor->setBounds(getLocalBounds().reduced(margin)); } updateIoletGeometry(); } void Object::updateIoletGeometry() { int maxIoletWidth = std::min((getWidth() - doubleMargin) / std::max<int>(numInputs, 1) - 4, (getWidth() - doubleMargin) / std::max<int>(numOutputs, 1) - 4); int maxIoletHeight = getHeight() / 2.0f - 2; int ioletSize = getPlugDataLook(*this).getIoletSize(); ioletSize = std::max(std::min({ ioletSize, maxIoletWidth, maxIoletHeight }), 10); int const borderWidth = jmap<float>(ioletSize, 10, 13, 7, 12); // IOLET layout for vanilla style (iolets in corners of objects) if (getPlugDataLook(*this).getUseIoletSpacingEdge()) { auto vanillaIoletBounds = getLocalBounds(); auto const marginOffset = (getPlugDataLook(*this).getObjectCornerRadius() == 0.0f) * 2; vanillaIoletBounds.removeFromLeft(margin - marginOffset); vanillaIoletBounds.removeFromRight(margin - marginOffset); auto objectWidth = vanillaIoletBounds.getWidth() + 0.5f; // FIXME: the right most iolet looks not right otherwise int inletIndex = 0; int outletIndex = 0; for (auto const& iolet : iolets) { bool const isInlet = iolet->isInlet(); float const yPosition = (isInlet ? margin + 1 : getHeight() - margin) - ioletSize / 2.0f; auto distributeIolets = [ioletSize, objectWidth, vanillaIoletBounds, yPosition](Iolet* iolet, int const ioletIndex, int const totalIolets) { auto const allOutLetWidth = totalIolets * ioletSize; auto const spacing = ioletIndex != 0 ? (objectWidth - allOutLetWidth) / static_cast<float>(totalIolets - 1) : 0; auto const ioletOffset = ioletIndex != 0 ? ioletSize * ioletIndex : 0; iolet->setBounds(vanillaIoletBounds.getX() + spacing * ioletIndex + ioletOffset, yPosition, ioletSize, ioletSize); }; if (isInlet) { distributeIolets(iolet, inletIndex, numInputs); inletIndex++; } else { distributeIolets(iolet, outletIndex, numOutputs); outletIndex++; } } // DEFAULT iolet position style } else { constexpr int ioletHitBox = 6; auto inletBounds = getLocalBounds(); if (auto const spaceToRemove = jlimit<int>(0, borderWidth, inletBounds.getWidth() - ioletHitBox * numInputs - borderWidth)) { inletBounds.removeFromLeft(spaceToRemove); inletBounds.removeFromRight(spaceToRemove); } auto outletBounds = getLocalBounds(); if (auto const spaceToRemove = jlimit<int>(0, borderWidth, outletBounds.getWidth() - ioletHitBox * numOutputs - borderWidth)) { outletBounds.removeFromLeft(spaceToRemove); outletBounds.removeFromRight(spaceToRemove); } int index = 0; for (auto const& iolet : iolets) { bool const isInlet = iolet->isInlet(); int const position = index < numInputs ? index : index - numInputs; int const total = isInlet ? numInputs : numOutputs; float const yPosition = (isInlet ? margin + 1 : getHeight() - margin) - ioletSize / 2.0f; auto const bounds = isInlet ? inletBounds : outletBounds; if (total == 1 && position == 0) { iolet->setBounds(getWidth() < 25 + ioletSize ? getLocalBounds().getCentreX() - (ioletSize - 1) / 2.0f : bounds.getX(), yPosition, ioletSize, ioletSize); } else if (total > 1) { float const ratio = (bounds.getWidth() - ioletSize) / static_cast<float>(total - 1); iolet->setBounds(bounds.getX() + ratio * position, yPosition, ioletSize, ioletSize); } index++; } } } void Object::updateTooltips() { if (!gui || cnv->isGraph) return; auto const& objectInfo = cnv->pd->objectLibrary->getObjectInfo(gui->getTypeWithOriginPrefix()); StackArray<StringArray, 2> ioletTooltips; gui->setTooltip(objectInfo.description); ioletTooltips = cnv->pd->objectLibrary->parseIoletTooltips(objectInfo.inlets, objectInfo.outlets, gui->getText(), numInputs, numOutputs); // First clear all tooltips, so we can see later if it has already been set or not for (auto const iolet : iolets) { iolet->setTooltip(""); } // Load tooltips from documentation files, these have priority for (int i = 0; i < iolets.size(); i++) { auto* iolet = iolets[i]; auto& tooltip = ioletTooltips[!iolet->isInlet()][iolet->isInlet() ? i : i - numInputs]; if (tooltip.startsWith("(gemlist)")) { iolet->setType(Iolet::GemState); iolet->setTooltip("(gemlist)"); } else { if (tooltip.isNotEmpty()) { // Don't overwrite custom documentation if there is no md documentation iolet->setTooltip(tooltip); } } } SmallArray<std::pair<int, SmallString>, 16> inletMessages; SmallArray<std::pair<int, SmallString>, 16> outletMessages; if (auto const subpatch = gui->getPatch()) { cnv->pd->lockAudioThread(); auto* subpatchPtr = subpatch->getPointer().get(); // Check child objects of subpatch for inlet/outlet messages for (auto obj : subpatch->getObjects()) { if (!obj.isValid()) continue; auto const name = String::fromUTF8((*obj.getRaw<t_pd>())->c_name->s_name); auto const* checkedObject = pd::Interface::checkObject(obj.getRaw<t_pd>()); if (name == "inlet" || name == "inlet~") { auto text = pd::Interface::getObjectText(checkedObject); int x, y, w, h; pd::Interface::getObjectBounds(subpatchPtr, obj.getRaw<t_gobj>(), &x, &y, &w, &h); // Anything after the first space will be the comment inletMessages.emplace_back(x, text.fromFirstOccurrenceOf(" ")); } else if (name == "outlet" || name == "outlet~") { auto text = pd::Interface::getObjectText(checkedObject); int x, y, w, h; pd::Interface::getObjectBounds(subpatchPtr, obj.getRaw<t_gobj>(), &x, &y, &w, &h); outletMessages.emplace_back(x, text.fromFirstOccurrenceOf(" ")); } } cnv->pd->unlockAudioThread(); } if (inletMessages.empty() && outletMessages.empty()) return; auto sortFunc = [](auto const& a, auto const& b) { return a.first < b.first; }; inletMessages.sort(sortFunc); outletMessages.sort(sortFunc); int numIn = 0; int numOut = 0; for (auto const iolet : iolets) { if (iolet->getTooltip().isNotEmpty()) continue; if ((iolet->isInlet() && numIn >= inletMessages.size()) || (!iolet->isInlet() && numOut >= outletMessages.size())) continue; auto& [x, message] = iolet->isInlet() ? inletMessages[numIn++] : outletMessages[numOut++]; iolet->setTooltip(message.toString()); if (message.startsWith("(gemlist)")) iolet->setType(Iolet::GemState); } } void Object::updateIolets() { if (!getPointer()) return; // update inlets and outlets int oldNumInputs = 0; int oldNumOutputs = 0; for (auto const* iolet : iolets) { iolet->isInlet() ? oldNumInputs++ : oldNumOutputs++; } numInputs = 0; numOutputs = 0; if (auto ptr = gui->ptr.get<t_pd>()) { if(auto* checkedObject = pd::Interface::checkObject(ptr.get())) { numInputs = pd::Interface::numInlets(checkedObject); numOutputs = pd::Interface::numOutlets(checkedObject); } } // Looking up tooltips takes a bit of time, so we make sure we're not constantly updating them for no reason bool const tooltipsNeedUpdate = gui->getPatch() != nullptr || numInputs != oldNumInputs || numOutputs != oldNumOutputs || isGemObject; for (auto* iolet : iolets) { if (gui && !iolet->isInlet()) { iolet->setHidden(gui->hideOutlet()); } else if (gui && iolet->isInlet()) { iolet->setHidden(gui->hideInlet()); } } while (numInputs < oldNumInputs) iolets.remove_at(--oldNumInputs); while (numInputs > oldNumInputs) { iolets.insert(oldNumInputs, this, true, oldNumInputs); ++oldNumInputs; } while (numOutputs < oldNumOutputs) iolets.remove_at(numInputs + --oldNumOutputs); while (numOutputs > oldNumOutputs) { iolets.insert(numInputs + oldNumOutputs, this, false, oldNumOutputs); ++oldNumOutputs; } for (int i = 0; i < numInputs + numOutputs; i++) { auto* iolet = iolets[i]; bool isSignal = false; if (auto ptr = gui->ptr.get<t_pd>()) { auto* checkedObject = pd::Interface::checkObject(ptr.get()); if (checkedObject && i < numInputs) { isSignal = pd::Interface::isSignalInlet(checkedObject, i); } else if(checkedObject) { isSignal = pd::Interface::isSignalOutlet(checkedObject, i - numInputs); } } iolet->setType(isSignal ? Iolet::Signal : Iolet::Data); iolet->repaint(); } if (tooltipsNeedUpdate) updateTooltips(); resized(); } void Object::mouseDown(MouseEvent const& e) { // Only show right-click menu in locked mode if the object can be opened // We don't allow alt+click for popupmenus here, as that will conflict with some object behaviour, like for [range.hsl] if (e.mods.isRightButtonDown() && !cnv->isGraph && !(gui && gui->isEditorShown())) { PopupMenu::dismissAllActiveMenus(); if (!getValue<bool>(locked)) { if (!selectedFlag && !e.mods.isAnyModifierKeyDown()) cnv->deselectAll(); cnv->setSelected(this, true); } Dialogs::showCanvasRightClickMenu(cnv, this, e.getScreenPosition()); return; } if (cnv->isGraph || getValue<bool>(presentationMode) || getValue<bool>(locked) || getValue<bool>(commandLocked)) { wasLockedOnMouseDown = true; return; } wasLockedOnMouseDown = false; if (e.mods.isShiftDown()) { // select multiple objects ds.wasSelectedOnMouseDown = selectedFlag; ds.duplicateOffset = { 0, 0 }; ds.lastDuplicateOffset = { 0, 0 }; ds.wasDuplicated = false; } else if (!selectedFlag) { cnv->deselectAll(); ds.duplicateOffset = { 0, 0 }; ds.lastDuplicateOffset = { 0, 0 }; ds.wasDuplicated = false; } cnv->setSelected(this, true); ds.componentBeingDragged = this; for (auto* object : cnv->getSelectionOfType<Object>()) { object->originalBounds = object->getBounds(); } bool const overEdgeNotCorner = resizeZone.isDraggingTopEdge() + resizeZone.isDraggingLeftEdge() + resizeZone.isDraggingBottomEdge() + resizeZone.isDraggingRightEdge() == 1; for (auto toResize = cnv->getSelectionOfType<Object>(); auto const* obj : toResize) { if (!obj->gui) continue; bool isDraggingOneEdge = false; if (obj->gui->canEdgeOverrideAspectRatio() && overEdgeNotCorner) { isDraggingOneEdge = true; } if (auto* constrainer = obj->getConstrainer()) { if (gui && gui->canEdgeOverrideAspectRatio()) { if (isDraggingOneEdge) constrainer->setFixedAspectRatio(0); else constrainer->setFixedAspectRatio(static_cast<float>(obj->getObjectBounds().getWidth()) / obj->getObjectBounds().getHeight()); } } } repaint(); ds.canvasDragStartPosition = -cnv->viewport->getViewPositionUnscaled().roundToInt(); if (!selectedFlag) { selectionStateChanged = true; } cnv->updateSidebarSelection(); } void Object::mouseUp(MouseEvent const& e) { if (wasLockedOnMouseDown || (gui && gui->isEditorShown())) return; if (!ds.didStartDragging && !getValue<bool>(locked) && e.mods.isAltDown()) { // Show help file on alt+mouseup if object were not draged openHelpPatch(); } auto objects = cnv->getSelectionOfType<Object>(); for (auto* obj : objects) obj->isObjectMouseActive = false; if (ds.wasResized) { cnv->objectGrid.clearIndicators(false); for (auto* connection : getConnections()) connection->pushPathState(true); applyBounds(); cnv->patch.endUndoSequence("Resize"); ds.wasResized = false; originalBounds.setBounds(0, 0, 0, 0); } else { if (cnv->isGraph) { return; } if (e.mods.isShiftDown() && ds.wasSelectedOnMouseDown && !ds.didStartDragging) { // Unselect object if selected cnv->setSelected(this, false); repaint(); } else if (!e.mods.isShiftDown() && !e.mods.isAltDown() && selectedFlag && !ds.didStartDragging && !e.mods.isRightButtonDown()) { // Don't run normal deselectAll, that would clear the sidebar inspector as well // We'll update sidebar selection later in this function cnv->selectedComponents.deselectAll(); cnv->setSelected(this, true); } if (ds.didStartDragging) { for (auto* connection : getConnections()) connection->pushPathState(true); applyBounds(); cnv->patch.endUndoSequence("Drag"); cnv->objectGrid.clearIndicators(false); ds.didStartDragging = false; } cnv->updateSidebarSelection(); if (ds.objectSnappingInbetween) { auto const* c = ds.connectionToSnapInbetween.getComponent(); cnv->patch.startUndoSequence("Snap inbetween"); auto* checkedOut = pd::Interface::checkObject(c->outobj->getPointer()); auto* checkedIn = pd::Interface::checkObject(c->inobj->getPointer()); auto* checkedSnapped = pd::Interface::checkObject(ds.objectSnappingInbetween->getPointer()); if (checkedOut && checkedIn && checkedSnapped) { cnv->patch.removeConnection(checkedOut, c->outIdx, checkedIn, c->inIdx, c->getPathState()); cnv->patch.createConnection(checkedOut, c->outIdx, checkedSnapped, 0); cnv->patch.createConnection(checkedSnapped, 0, checkedIn, c->inIdx); } cnv->patch.endUndoSequence("Snap inbetween"); ds.objectSnappingInbetween->iolets[0]->setTargeted(false); ds.objectSnappingInbetween->iolets[ds.objectSnappingInbetween->numInputs]->setTargeted(false); ds.objectSnappingInbetween = nullptr; cnv->synchronise(); } for (auto* object : cnv->getSelectionOfType<Object>()) { object->repaint(); } ds.componentBeingDragged = nullptr; repaint(); } if (ds.wasDragDuplicated) { cnv->patch.endUndoSequence("Duplicate"); ds.wasDragDuplicated = false; } if (gui && selectedFlag && !selectionStateChanged && !e.mouseWasDraggedSinceMouseDown() && !e.mods.isRightButtonDown() && !e.mods.isAnyModifierKeyDown()) { gui->showEditor(); } selectionStateChanged = false; cnv->needsSearchUpdate = true; } void Object::mouseDrag(MouseEvent const& e) { if (wasLockedOnMouseDown || (gui && gui->isEditorShown())) return; if (cnv->objectRateReducer.tooFast()) return; #if JUCE_MAC || JUCE_WINDOWS beginDragAutoRepeat(25); // Doing this leads to terrible performance on Linux, unfortunately #endif cnv->cancelConnectionCreation(); if (e.mods.isMiddleButtonDown()) return; if (validResizeZone && !originalBounds.isEmpty()) { auto const draggedBounds = resizeZone.resizeRectangleBy(originalBounds, e.getOffsetFromDragStart()); auto const dragDistance = cnv->objectGrid.performResize(this, e.getOffsetFromDragStart(), draggedBounds); auto toResize = cnv->getSelectionOfType<Object>(); if (!ds.wasResized) { cnv->patch.startUndoSequence("Resize"); } for (auto* obj : toResize) { if (!obj->gui) continue; obj->isObjectMouseActive = true; // Create undo step when we start resizing if (!ds.wasResized) { auto* objPtr = obj->getPointer(); auto const* cnv = obj->cnv; if (auto patchPtr = cnv->patch.getPointer()) { pd::Interface::undoApply(patchPtr.get(), objPtr); } } auto const newBounds = resizeZone.resizeRectangleBy(obj->originalBounds, dragDistance); if (auto* constrainer = obj->getConstrainer()) { constrainer->setBoundsForComponent(obj, newBounds, resizeZone.isDraggingTopEdge(), resizeZone.isDraggingLeftEdge(), resizeZone.isDraggingBottomEdge(), resizeZone.isDraggingRightEdge()); } } ds.wasResized = true; } else if (!cnv->isGraph) { int constexpr minimumMovementToStartDrag = 5; // Ensure tiny movements don't start a drag. if (!ds.didStartDragging && e.getDistanceFromDragStart() < minimumMovementToStartDrag) return; |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |