commit c95c071916988a87949635d46c265afd2c0c81c4
parent 4df575f1a6e4324edcec82419f3c765f0e53a378
Author: dsp56300 <dsp56300@users.noreply.github.com>
Date: Thu, 1 Aug 2024 14:37:19 +0200
support hit area specification for buttons
Diffstat:
5 files changed, 55 insertions(+), 3 deletions(-)
diff --git a/source/juceUiLib/button.cpp b/source/juceUiLib/button.cpp
@@ -0,0 +1 @@
+#include "button.h"
diff --git a/source/juceUiLib/button.h b/source/juceUiLib/button.h
@@ -63,10 +63,28 @@ namespace genericUI
return m_allowRightClick;
}
+ bool hitTest (const int _x, const int _y) override
+ {
+ if(!T::hitTest(_x,_y))
+ return false;
+
+ if(_x < m_hitAreaOffset.getX() || _y < m_hitAreaOffset.getY())
+ return false;
+ if(_x > T::getWidth() - m_hitAreaOffset.getWidth() || _y > T::getHeight() - m_hitAreaOffset.getHeight())
+ return false;
+ return true;
+ }
+
+ void setHitAreaOffset(const juce::Rectangle<int>& _offset)
+ {
+ m_hitAreaOffset = _offset;
+ }
+
Callback onDown;
Callback onUp;
private:
bool m_allowRightClick = false;
+ juce::Rectangle<int> m_hitAreaOffset{0,0,0,0};
};
}
\ No newline at end of file
diff --git a/source/juceUiLib/buttonStyle.cpp b/source/juceUiLib/buttonStyle.cpp
@@ -1,5 +1,6 @@
#include "buttonStyle.h"
+#include "button.h"
#include "uiObject.h"
namespace genericUI
@@ -98,5 +99,30 @@ namespace genericUI
_button.setRadioGroupId(m_radioGroupId);
_button.setImages(m_normalImage, m_overImage, m_downImage, m_disabledImage, m_normalImageOn, m_overImageOn, m_downImageOn, m_disabledImageOn);
+
+ auto* button = dynamic_cast<Button<juce::DrawableButton>*>(&_button);
+
+ if(button)
+ {
+ auto hitRect = m_hitAreaOffset;
+
+ if(hitRect.getX() < 0) hitRect.setX(0);
+ if(hitRect.getY() < 0) hitRect.setY(0);
+ if(hitRect.getWidth() > 0) hitRect.setWidth(0);
+ if(hitRect.getHeight() > 0) hitRect.setHeight(0);
+
+ auto newW = button->getWidth();
+ auto newH = button->getHeight();
+
+ if(m_hitAreaOffset.getWidth() > 0) newW += m_hitAreaOffset.getWidth();
+ if(m_hitAreaOffset.getHeight() > 0) newH += m_hitAreaOffset.getHeight();
+
+ button->setSize(newW, newH);
+
+ hitRect.setWidth(-hitRect.getWidth());
+ hitRect.setHeight(-hitRect.getHeight());
+
+ button->setHitAreaOffset(hitRect);
+ }
}
}
diff --git a/source/juceUiLib/uiObjectStyle.cpp b/source/juceUiLib/uiObjectStyle.cpp
@@ -57,9 +57,9 @@ namespace genericUI
juce::Justification a = 0;
switch (alignH[0])
{
- case 'L': a = juce::Justification::left; break;
+ case 'L': a = juce::Justification::left; break;
case 'C': a = juce::Justification::horizontallyCentred; break;
- case 'R': a = juce::Justification::right; break;
+ case 'R': a = juce::Justification::right; break;
}
m_align = a;
}
@@ -71,7 +71,7 @@ namespace genericUI
switch (alignV[0])
{
case 'T': a = juce::Justification::top; break;
- case 'C': a = juce::Justification::verticallyCentred; break;
+ case 'C': a = juce::Justification::verticallyCentred; break;
case 'B': a = juce::Justification::bottom; break;
}
m_align = m_align.getFlags() | a.getFlags();
@@ -89,6 +89,11 @@ namespace genericUI
m_offsetT = _object.getPropertyInt("offsetT", 1);
m_offsetR = _object.getPropertyInt("offsetR", -30);
m_offsetB = _object.getPropertyInt("offsetB", -2);
+
+ m_hitAreaOffset.setX(_object.getPropertyInt("hitOffsetL", 0));
+ m_hitAreaOffset.setY(_object.getPropertyInt("hitOffsetT", 0));
+ m_hitAreaOffset.setWidth(_object.getPropertyInt("hitOffsetR", 0));
+ m_hitAreaOffset.setHeight(_object.getPropertyInt("hitOffsetB", 0));
}
std::optional<juce::Font> UiObjectStyle::getFont() const
diff --git a/source/juceUiLib/uiObjectStyle.h b/source/juceUiLib/uiObjectStyle.h
@@ -69,5 +69,7 @@ namespace genericUI
int m_offsetB = 0;
std::string m_url;
+
+ juce::Rectangle<int> m_hitAreaOffset; // only supported for buttons atm
};
}