Krotos Modules 3
Loading...
Searching...
No Matches
DraggableLabel.cpp
Go to the documentation of this file.
1#include "DraggableLabel.h"
2
3namespace krotos
4{
5 const String DraggableLabel::DraggableLabelID{"DraggableLabel"};
6
7 DraggableLabel::DraggableLabel(const String& name, const String& labelText) : Label(name, labelText)
8 {
9 setAlpha(m_alphas.defaultAlpha);
10
11 // Set this property so DropComponent will be interested in us
12 getProperties().set(DropComponent::DragSource, DraggableLabelID);
13 }
14
15 void DraggableLabel::mouseDown(const MouseEvent&)
16 {
17 DragAndDropContainer* parentContainer = DragAndDropContainer::findParentDragContainerFor(this);
18
19 if (parentContainer && m_dragEnabled)
20 // If container parentDragContainer cannot be found make sure your editor inherits from the DragAndDropContainer
21 // class
22 {
23 // Take snapshot of DragComponent
24 DragComponent dragComponent(getText());
25 Image dragImage = dragComponent.createComponentSnapshot(dragComponent.getLocalBounds());
26
27 // Calculate the offset so the cursor is at the bottom-left of the image
28 Point<int> cursorOffset(0, -dragComponent.getHeight());
29
30 parentContainer->startDragging(m_dragAndDropDescription, this, dragImage, false, &cursorOffset);
31 m_isDragging = true;
32 // Set label alpha to 100% when clicked and dragging
33 setAlpha(m_alphas.hoverGrabAlpha);
34 // Set MouseCursor to grab hand
35 setMouseCursor(m_grabHandImage.isValid() ? MouseCursor(m_grabHandImage, m_grabHandImage.getWidth() / 2,
36 m_grabHandImage.getHeight() / 2)
37 : MouseCursor::DraggingHandCursor);
38 }
39 }
40
41 void DraggableLabel::setDragAndDropDescription(const var& newDescription)
42 {
43 m_dragAndDropDescription = newDescription;
44 }
45
46 void DraggableLabel::setEnableDrag(bool dragEnabled) { m_dragEnabled = dragEnabled; }
47
48 void DraggableLabel::mouseMove(const MouseEvent& event)
49 {
50 if (!m_dragEnabled)
51 {
52 return;
53 }
54
55 Image cursorImage;
56
57 if (isMouseOver())
58 {
59 // Choose the Image
61
62 // Set label alpha to max
63 setAlpha(m_alphas.hoverGrabAlpha);
64 }
65 else if (!m_isDragging)
66 {
67 // Set label alpha to default if not dragging
68 setAlpha(m_alphas.defaultAlpha);
69 }
70
71 // Set the cursor
72 setMouseCursor(cursorImage.isValid()
73 ? MouseCursor(cursorImage, cursorImage.getWidth() / 2, cursorImage.getHeight() / 2)
74 : MouseCursor::DraggingHandCursor);
75 }
76
77 void DraggableLabel::mouseExit(const MouseEvent& event)
78 {
79 if (!m_isDragging)
80 {
81 setMouseCursor(MouseCursor::NormalCursor);
82 setAlpha(m_alphas.defaultAlpha);
83 }
84 }
85
86 void DraggableLabel::mouseUp(const MouseEvent&)
87 {
88 m_isDragging = false;
89 // Set label alpha to default when mouse is released
90 if (!isMouseOver())
91 {
92 setAlpha(m_alphas.defaultAlpha);
93 }
94 }
95
96 void DraggableLabel::setHoverHandImage(const Image& image) { m_hoverHandImage = image; }
97
98 void DraggableLabel::setGrabHandImage(const Image& image) { m_grabHandImage = image; }
99} // namespace krotos
A component designed to represent a draggable element with a visual representation.
Definition DragComponent.h:12
void mouseExit(const MouseEvent &) override
Definition DraggableLabel.cpp:77
void setEnableDrag(bool dragEnabled)
Definition DraggableLabel.cpp:46
void setHoverHandImage(const Image &image)
Definition DraggableLabel.cpp:96
void setGrabHandImage(const Image &image)
Definition DraggableLabel.cpp:98
var m_dragAndDropDescription
Definition DraggableLabel.h:39
DraggableLabel(const String &name=String(), const String &labelText=String())
Definition DraggableLabel.cpp:7
bool m_isDragging
Definition DraggableLabel.h:44
void setDragAndDropDescription(const var &newDescription)
Definition DraggableLabel.cpp:41
struct krotos::DraggableLabel::AlphaValues m_alphas
bool m_dragEnabled
Definition DraggableLabel.h:38
Image m_hoverHandImage
Definition DraggableLabel.h:41
static const String DraggableLabelID
The ID of this drag source.
Definition DraggableLabel.h:29
void mouseUp(const MouseEvent &) override
Definition DraggableLabel.cpp:86
Image m_grabHandImage
Definition DraggableLabel.h:42
void mouseMove(const MouseEvent &) override
Definition DraggableLabel.cpp:48
void mouseDown(const MouseEvent &) override
Definition DraggableLabel.cpp:15
static const Identifier DragSource
Set this identifier in your drag source component properties so DropComponent will be interested in i...
Definition DropComponent.h:103
Definition AirAbsorptionFilter.cpp:2
float defaultAlpha
Definition DraggableLabel.h:34
float hoverGrabAlpha
Definition DraggableLabel.h:35