Krotos Modules 3
Loading...
Searching...
No Matches
KwidgetGUI_TriggerButton.cpp
Go to the documentation of this file.
1namespace krotos
2{
3 //======================================= KwidgetGUI_TriggerButton
4 //========================================================================
6 {
8
9 addMouseListener(this, true);
10
12
13 // Add the main triggerButton component
14 addAndMakeVisible(m_onClickButton);
15
16 // Connect triggerButton to the onClick Param
17 auto& onClickParam = *owner.getParameter(Params::OnClick)->getAttachedParameter();
19
20 auto* midiModeParam = owner.getCustomParameter(Parameters::MIDIMode);
21
22 midiModeParam->valueChanged = [&](const var&) {
23 m_onClickButton.setButtonText(owner.getCustomParameter(Parameters::MIDIMode)->getValue().toString());
24 };
25
26 // Initialise button text
27 m_onClickButton.setButtonText(midiModeParam->getValue().toString());
28
29 // Connect the label to the customParam
30 m_editableLabel = std::make_unique<AttachedLabel>(owner.getKwidgetLabel());
31 m_editableLabel->getAttachment() =
33 m_editableLabel->getLabel().setColour(juce::Label::textColourId, juce::Colours::lightgrey);
34 m_editableLabel->getLabel().setJustificationType(Justification::centredBottom);
35 addAndMakeVisible(m_editableLabel.get());
36
37 addAndMakeVisible(m_contextMenu);
38 m_contextMenu.onClick = [&] { showContextMenu(); };
39 }
40
42 {
43 if (auto* midiModeParam = getOwner().getCustomParameter(Parameters::MIDIMode))
44 {
45 midiModeParam->valueChanged = nullptr;
46 }
47 }
48
50 {
51 PopupMenu menu;
53 const auto selectedMode = Kwidget_CoreEngine::MIDIModeNames.indexOf(mode.toString());
54
55 // Populate menu with mode names - skipping All notes
56 for (int i = static_cast<int>(MIDIMode::All) + 1; i < static_cast<int>(MIDIMode::NUM_MODES); ++i)
57 {
58 menu.addItem(i, Kwidget_CoreEngine::MIDIModeNames[i], true, i == selectedMode);
59 }
60
61 // Action callback from the menu to set the mode
62 std::function<void(int)> menuCallback = [&](int itemResultID) {
63 if (itemResultID > 0)
64 {
65 // No need to correct for the menu offset since trigger buttons don't have "All notes" enum
66 getOwner()
68 ->setValue(Kwidget_CoreEngine::MIDIModeNames[itemResultID], true);
69 }
70 };
71
72 menu.showMenuAsync(PopupMenu::Options{}
73 .withTargetComponent(m_contextMenu)
74 .withMinimumNumColumns(1)
75 .withStandardItemHeight(20),
76 menuCallback);
77 }
78
80
82 {
84
85 auto kwidgetBounds = getLocalBounds();
86
87 // TriggerButton Button bounds
88 auto triggerButtonBounds = kwidgetBounds.removeFromTop(static_cast<int>(2.0f * getHeight() / 3.0f));
89 const int contextMenuWidth{20};
90 auto contextButtonRect = triggerButtonBounds.removeFromRight(contextMenuWidth);
91 m_onClickButton.setBounds(triggerButtonBounds);
92
93 // Label Font
94 auto labelFont = Font().withPointHeight(static_cast<float>(constants.FontSize));
95 labelFont.setExtraKerningFactor(constants.KerningFactor);
96 int labelHeight = static_cast<int>(constants.LabelPadding * 0.5f);
97 int labelWidth = static_cast<int>(triggerButtonBounds.getWidth());
98 // Label Bounds
99 auto labelBounds = Rectangle<int>(static_cast<int>(triggerButtonBounds.getWidth()) - labelWidth,
100 static_cast<int>(triggerButtonBounds.getBottom()), labelWidth, labelHeight);
101 m_editableLabel->setBounds(labelBounds);
102
103 m_contextMenu.setVisible(!m_performMode);
104 m_contextMenu.setBounds(contextButtonRect.removeFromTop(contextMenuWidth));
105
106 if (m_performMode == true)
107 {
108 m_editableLabel->getLabel().setFont(labelFont);
109 m_editableLabel->getLabel().setEditable(false);
110 }
111 else // edit Mode
112 {
113 m_editableLabel->getLabel().setFont(labelFont);
114 m_editableLabel->getLabel().setEditable(false, false, true, false);
115 }
116 }
117} // namespace krotos
std::function< void(const var &)> valueChanged
Definition CustomParameter.h:60
const var & getValue() const
Definition CustomParameter.cpp:39
void setValue(const var &newValue, bool selfUpdate=false)
Definition CustomParameter.cpp:46
GenericParameter * getAttachedParameter()
Definition KParameter.cpp:170
static const StringArray MIDIModeNames
Definition Kwidget_CoreEngine.h:80
bool m_performMode
Definition KwidgetGUI_TriggerButton.h:33
struct krotos::KwidgetGUI_TriggerButton::Constants constants
void resized() override
Definition KwidgetGUI_TriggerButton.cpp:81
std::unique_ptr< AttachedLabel > m_editableLabel
Definition KwidgetGUI_TriggerButton.h:42
std::unique_ptr< TriggerButtonParameterAttachment > m_triggerButtonAttachment
Definition KwidgetGUI_TriggerButton.h:36
~KwidgetGUI_TriggerButton()
Definition KwidgetGUI_TriggerButton.cpp:41
KwidgetGUI_TriggerButton(Kwidget &owner)
Definition KwidgetGUI_TriggerButton.cpp:5
TriggerButton m_onClickButton
Definition KwidgetGUI_TriggerButton.h:35
ContextMenuButton m_contextMenu
Definition KwidgetGUI_TriggerButton.h:38
void paint(Graphics &g) override
Definition KwidgetGUI_TriggerButton.cpp:79
void showContextMenu()
Definition KwidgetGUI_TriggerButton.cpp:49
Interface for a UI Component that controls a KwidgetProcessor.
Definition KwidgetGUI.h:24
std::unique_ptr< LabelCustomParameterAttachment > createCustomParameterAttachment(const String &paramID, Label &label)
Definition KwidgetGUI.cpp:100
void paint(Graphics &g) override
Definition KwidgetGUI.cpp:26
Kwidget & getOwner()
Definition KwidgetGUI.cpp:106
void resized() override
Definition KwidgetGUI.cpp:45
void setRenderStyle(RenderStyle newStyle)
Definition KwidgetGUI.cpp:114
Definition Kwidget.h:8
KParameter * getParameter(const String &parameterID) const noexcept
Get a raw pointer to a KParameter belonging to this Kwidget's KwidgetProcessor. Useful for low overhe...
Definition Kwidget.cpp:168
CustomParameter * getCustomParameter(const String &parameterID)
Definition Kwidget.cpp:188
virtual const String & getKwidgetLabel()
Definition Kwidget.cpp:378
A custom TriggerButton attachment class.
Definition TriggerButton.h:60
Definition AirAbsorptionFilter.cpp:2
Definition Kwidget_TriggerButton.h:18
static const String MIDIMode
Definition Kwidget_TriggerButton.h:19
static const String Label
Definition Kwidget_TriggerButton.h:21
const int FontSize
Definition KwidgetGUI_TriggerButton.h:27
const float KerningFactor
Definition KwidgetGUI_TriggerButton.h:28
const int LabelPadding
Definition KwidgetGUI_TriggerButton.h:25