-
-
Save sudara/eed6b8bb3b960b4c2156a0883913ea15 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#include "juce_gui_basics/juce_gui_basics.h" | |
/* | |
* A couple basic helper functions to help components | |
* deal with concepts like drop shadows | |
* | |
* The main point is to allow "bleed" around the normal content bounds | |
* | |
* getLocalBounds() | |
* ----------------------------------- | |
* | paddingY | | |
* | --------------------------- | | |
* | | | | | |
* | | getContentBounds() | <--- paddingX | |
* | | | | | |
* | --------------------------- | | |
* | | | |
* |---------------------------------- | |
* | |
*/ | |
class PaddedComponent : public juce::Component | |
{ | |
public: | |
explicit PaddedComponent (juce::String userFacingName = juce::String()) | |
{ | |
// may as well, makes it visible in the inspector | |
setName (userFacingName); | |
}; | |
// Achtung: JUCE is NOT like CSS padding, goes left, top, right, bottom | |
void setBoundsAndPadding (juce::Rectangle<int> bounds, int l, int t, int r, int b) | |
{ | |
getProperties().set ("paddingLeft", l); | |
getProperties().set ("paddingRight", r); | |
getProperties().set ("paddingTop", t); | |
getProperties().set ("paddingBottom", b); | |
setBounds (bounds.getX() - l, bounds.getY() - t, bounds.getWidth() + l + r, bounds.getHeight() + t + b); | |
} | |
void setBoundsAndPadding (juce::Rectangle<int> newBounds, int newPaddingX, int newPaddingY) | |
{ | |
setBoundsAndPadding (newBounds, newPaddingX, newPaddingY, newPaddingX, newPaddingY); | |
} | |
void setBoundsAndPadding (juce::Rectangle<int> newBounds, int newPadding) | |
{ | |
setBoundsAndPadding (newBounds, newPadding, newPadding); | |
} | |
/* | |
* This is when you are passing bounds that include the padding | |
* getLocalBounds is the bounds passed in | |
* getContentBounds is reduced by the specified padding | |
*/ | |
// Achtung: This is NOT like CSS padding... | |
void setBoundsReducedByPadding (juce::Rectangle<int> newBounds, int l, int t, int r, int b) | |
{ | |
getProperties().set ("paddingLeft", l); | |
getProperties().set ("paddingRight", r); | |
getProperties().set ("paddingTop", t); | |
getProperties().set ("paddingBottom", b); | |
setBounds (newBounds); | |
} | |
void setBoundsReducedByPadding (juce::Rectangle<int> newBounds, int newPaddingX, int newPaddingY) | |
{ | |
setBoundsReducedByPadding (newBounds, newPaddingX, newPaddingY, newPaddingX, newPaddingY); | |
} | |
void setBoundsReducedByPadding (juce::Rectangle<int> newBounds, int newPadding) | |
{ | |
setBoundsReducedByPadding (newBounds, newPadding, newPadding); | |
} | |
int getContentWidth() | |
{ | |
return getWidth() - (int) getProperties().getWithDefault ("paddingLeft", 0) - (int) getProperties().getWithDefault ("paddingRight", 0); | |
} | |
juce::Rectangle<int> getContentBounds() | |
{ | |
auto bounds = getLocalBounds(); | |
auto newWidth = bounds.getWidth() - paddingLeft() - paddingRight(); | |
auto newHeight = bounds.getHeight() - paddingTop() - paddingBottom(); | |
return { bounds.getX() + paddingLeft(), bounds.getY() + paddingTop(), newWidth, newHeight }; | |
} | |
// gives the parent's coordinate for the content bounds (not the component bounds) | |
juce::Rectangle<int> getContentBoundsInParent() | |
{ | |
auto p = getBoundsInParent(); | |
return { p.getX() + paddingLeft(), p.getY() + paddingTop(), p.getWidth() - paddingLeft() - paddingRight(), p.getHeight() - paddingTop() - paddingBottom() }; | |
} | |
inline int paddingLeft() { return (int) getProperties().getWithDefault ("paddingLeft", 0); } | |
inline int paddingTop() { return (int) getProperties().getWithDefault ("paddingTop", 0); } | |
inline int paddingRight() { return (int) getProperties().getWithDefault ("paddingRight", 0); } | |
inline int paddingBottom() { return (int) getProperties().getWithDefault ("paddingBottom", 0); } | |
private: | |
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (PaddedComponent) | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment