buttonStyle.cpp (3629B)
1 #include "buttonStyle.h" 2 3 #include "button.h" 4 #include "uiObject.h" 5 6 namespace genericUI 7 { 8 void ButtonStyle::apply(Editor& _editor, const UiObject& _object) 9 { 10 UiObjectStyle::apply(_editor, _object); 11 12 m_isToggle = _object.getPropertyInt("isToggle") != 0; 13 m_radioGroupId = _object.getPropertyInt("radioGroupId"); 14 15 if(!m_drawable) 16 return; 17 18 juce::Drawable** imageDrawables[] = { 19 &m_normalImage, 20 &m_overImage, 21 &m_downImage, 22 &m_disabledImage, 23 &m_normalImageOn, 24 &m_overImageOn, 25 &m_downImageOn, 26 &m_disabledImageOn 27 }; 28 29 const char* properties[] = { 30 "normalImage", 31 "overImage", 32 "downImage", 33 "disabledImage", 34 "normalImageOn", 35 "overImageOn", 36 "downImageOn", 37 "disabledImageOn" 38 }; 39 40 static_assert(std::size(imageDrawables) == std::size(properties), "arrays must have same size"); 41 42 const bool isVerticalTiling = m_tileSizeY <= (m_drawable->getHeight()>>1); 43 44 std::map<int, juce::Drawable*> drawables; 45 46 const auto targetW = _object.getPropertyInt("width", 0); 47 const auto targetH = _object.getPropertyInt("height", 0); 48 49 for(size_t i=0; i<std::size(imageDrawables); ++i) 50 { 51 const auto imageIndex = _object.getPropertyInt(properties[i], -1); 52 53 if(imageIndex < 0) 54 continue; 55 56 const auto it = drawables.find(imageIndex); 57 58 if(it != drawables.end()) 59 { 60 *imageDrawables[i] = it->second; 61 } 62 else 63 { 64 const auto needsScale = targetW && targetH && (targetW != m_tileSizeX || targetH != m_tileSizeY); 65 juce::Drawable* d = m_drawable; 66 if(needsScale || imageIndex > 0) 67 { 68 m_createdDrawables.emplace_back(d->createCopy()); 69 d = m_createdDrawables.back().get(); 70 71 juce::AffineTransform t; 72 if(isVerticalTiling) 73 { 74 t = d->getTransform().translated(0, static_cast<float>(-imageIndex * m_tileSizeY)); 75 } 76 else 77 { 78 t = d->getTransform().translated(static_cast<float>(-imageIndex * m_tileSizeX), 0); 79 } 80 d->setTransform(t); 81 } 82 83 *imageDrawables[i] = d; 84 85 drawables.insert(std::make_pair(imageIndex, d)); 86 87 if(needsScale) 88 { 89 auto* d = *imageDrawables[i]; 90 const auto scaleX = static_cast<float>(targetW) / static_cast<float>(m_tileSizeX); 91 const auto scaleY = static_cast<float>(targetH) / static_cast<float>(m_tileSizeY); 92 d->setTransform(d->getTransform().scaled(scaleX, scaleY)); 93 } 94 } 95 } 96 } 97 98 void ButtonStyle::apply(juce::DrawableButton& _button) const 99 { 100 _button.setColour(juce::DrawableButton::ColourIds::backgroundColourId, juce::Colours::transparentBlack); 101 _button.setColour(juce::DrawableButton::ColourIds::backgroundOnColourId, juce::Colours::transparentBlack); 102 103 _button.setClickingTogglesState(m_isToggle); 104 105 if(m_radioGroupId) 106 _button.setRadioGroupId(m_radioGroupId); 107 108 _button.setImages(m_normalImage, m_overImage, m_downImage, m_disabledImage, m_normalImageOn, m_overImageOn, m_downImageOn, m_disabledImageOn); 109 110 auto* button = dynamic_cast<Button<juce::DrawableButton>*>(&_button); 111 112 if(button) 113 { 114 auto hitRect = m_hitAreaOffset; 115 116 if(hitRect.getX() < 0) hitRect.setX(0); 117 if(hitRect.getY() < 0) hitRect.setY(0); 118 if(hitRect.getWidth() > 0) hitRect.setWidth(0); 119 if(hitRect.getHeight() > 0) hitRect.setHeight(0); 120 121 auto newW = button->getWidth(); 122 auto newH = button->getHeight(); 123 124 if(m_hitAreaOffset.getWidth() > 0) newW += m_hitAreaOffset.getWidth(); 125 if(m_hitAreaOffset.getHeight() > 0) newH += m_hitAreaOffset.getHeight(); 126 127 button->setSize(newW, newH); 128 129 hitRect.setWidth(-hitRect.getWidth()); 130 hitRect.setHeight(-hitRect.getHeight()); 131 132 button->setHitAreaOffset(hitRect); 133 } 134 } 135 }