/******************************************************************************* The block below describes the properties of this PIP. A PIP is a short snippet of code that can be read by the Projucer and used to generate a JUCE project. BEGIN_JUCE_PIP_METADATA name: Drag dependencies: juce_core, juce_data_structures, juce_events, juce_graphics, juce_gui_basics exporters: xcode_mac moduleFlags: JUCE_STRICT_REFCOUNTEDPOINTER=1 type: Component mainClass: Drag END_JUCE_PIP_METADATA *******************************************************************************/ #pragma once //________________________________________________________________________ class Constrainer : public ComponentBoundsConstrainer { public: void setBounds(Rectangle bounds) { this->bounds = bounds; } void checkBounds(juce::Rectangle ¤t, const juce::Rectangle& prev, const juce::Rectangle& /**/, bool /**/, bool /**/, bool /**/, bool /**/) override { auto constrained = bounds.getConstrainedPoint(current.getCentre()); current.setCentre(constrained); } private: Rectangle bounds; }; //________________________________________________________________________ class Draggable : public Component { public: void paint(Graphics& g) override { g.fillAll(Colours::white); } void mouseDown(const MouseEvent &e) { dragger.startDraggingComponent(this, e); } void mouseDrag(const MouseEvent &e) { dragger.dragComponent(this, e, &constrainer); } void setDragConstraints(Rectangle constraints) { constrainer.setBounds(constraints); } private: ComponentDragger dragger; Constrainer constrainer; }; //________________________________________________________________________ class Drag : public Component { public: Drag() { draggable.setSize(50, 50); addAndMakeVisible(draggable); setSize (600, 400); } ~Drag() { } void paint (Graphics& g) override { g.fillAll(Colours::black); } void resized() { draggable.setDragConstraints(getLocalBounds()); } private: Draggable draggable; JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (Drag) };