uiObjectStyle.cpp (5806B)
1 #include "uiObjectStyle.h" 2 3 #include "editor.h" 4 #include "uiObject.h" 5 6 namespace genericUI 7 { 8 UiObjectStyle::UiObjectStyle(Editor& _editor) : m_editor(_editor) {} 9 10 void UiObjectStyle::setTileSize(int _w, int _h) 11 { 12 m_tileSizeX = _w; 13 m_tileSizeY = _h; 14 } 15 16 void UiObjectStyle::setDrawable(juce::Drawable* _drawable) 17 { 18 m_drawable = _drawable; 19 } 20 21 void UiObjectStyle::setTextHeight(int _height) 22 { 23 m_textHeight = _height; 24 } 25 26 void UiObjectStyle::apply(Editor& _editor, const UiObject& _object) 27 { 28 const auto x = _object.getPropertyInt("tileSizeX"); 29 const auto y = _object.getPropertyInt("tileSizeY"); 30 31 setTileSize(x,y); 32 33 const auto tex = _object.getProperty("texture"); 34 if(!tex.empty()) 35 setDrawable(_editor.getImageDrawable(tex)); 36 37 setTextHeight(_object.getPropertyInt("textHeight")); 38 39 m_text = _object.getProperty("text"); 40 const auto color = _object.getProperty("color"); 41 42 auto parseColor = [&_object](juce::Colour& _target, const std::string& _prop) 43 { 44 const auto color = _object.getProperty(_prop); 45 UiObjectStyle::parseColor(_target, color); 46 }; 47 48 parseColor(m_color, "color"); 49 parseColor(m_bgColor, "backgroundColor"); 50 parseColor(m_linesColor, "linesColor"); 51 parseColor(m_selectedItemBgColor, "selectedItemBackgroundColor"); 52 parseColor(m_outlineColor, "outlineColor"); 53 54 const auto alignH = _object.getProperty("alignH"); 55 if(!alignH.empty()) 56 { 57 juce::Justification a = 0; 58 switch (alignH[0]) 59 { 60 case 'L': a = juce::Justification::left; break; 61 case 'C': a = juce::Justification::horizontallyCentred; break; 62 case 'R': a = juce::Justification::right; break; 63 } 64 m_align = a; 65 } 66 67 const auto alignV = _object.getProperty("alignV"); 68 if(!alignV.empty()) 69 { 70 juce::Justification a = 0; 71 switch (alignV[0]) 72 { 73 case 'T': a = juce::Justification::top; break; 74 case 'C': a = juce::Justification::verticallyCentred; break; 75 case 'B': a = juce::Justification::bottom; break; 76 } 77 m_align = m_align.getFlags() | a.getFlags(); 78 } 79 80 m_fontFile = _object.getProperty("fontFile"); 81 m_fontName = _object.getProperty("fontName"); 82 83 m_bold = _object.getPropertyInt("bold") != 0; 84 m_italic = _object.getPropertyInt("italic") != 0; 85 m_antialiasing = _object.getPropertyInt("antialiasing", 1) != 0; 86 87 m_url = _object.getProperty("url"); 88 89 m_offsetL = _object.getPropertyInt("offsetL", 1); 90 m_offsetT = _object.getPropertyInt("offsetT", 1); 91 m_offsetR = _object.getPropertyInt("offsetR", -30); 92 m_offsetB = _object.getPropertyInt("offsetB", -2); 93 94 m_hitAreaOffset.setX(_object.getPropertyInt("hitOffsetL", 0)); 95 m_hitAreaOffset.setY(_object.getPropertyInt("hitOffsetT", 0)); 96 m_hitAreaOffset.setWidth(_object.getPropertyInt("hitOffsetR", 0)); 97 m_hitAreaOffset.setHeight(_object.getPropertyInt("hitOffsetB", 0)); 98 } 99 100 std::optional<juce::Font> UiObjectStyle::getFont() const 101 { 102 if (m_fontFile.empty()) 103 return {}; 104 105 auto font = juce::Font(m_editor.getFont(m_fontFile).getTypefacePtr()); 106 applyFontProperties(font); 107 return font; 108 } 109 110 bool UiObjectStyle::parseColor(juce::Colour& _color, const std::string& _colorString) 111 { 112 uint32_t r,g,b,a; 113 114 if(_colorString.size() == 8) 115 { 116 sscanf(_colorString.c_str(), "%02x%02x%02x%02x", &r, &g, &b, &a); 117 } 118 else if(_colorString.size() == 6) 119 { 120 sscanf(_colorString.c_str(), "%02x%02x%02x", &r, &g, &b); 121 a = 255; 122 } 123 else 124 { 125 return false; 126 } 127 128 _color = juce::Colour(static_cast<uint8_t>(r), static_cast<uint8_t>(g), static_cast<uint8_t>(b), static_cast<uint8_t>(a)); 129 return true; 130 } 131 132 void UiObjectStyle::drawLabel(juce::Graphics& _graphics, juce::Label& _label) 133 { 134 _graphics.setImageResamplingQuality(m_antialiasing ? juce::Graphics::highResamplingQuality : juce::Graphics::lowResamplingQuality); 135 LookAndFeel_V4::drawLabel(_graphics, _label); 136 } 137 138 void UiObjectStyle::drawButtonText(juce::Graphics& _graphics, juce::TextButton& _textButton, bool shouldDrawButtonAsHighlighted, bool shouldDrawButtonAsDown) 139 { 140 _graphics.setImageResamplingQuality(m_antialiasing ? juce::Graphics::highResamplingQuality : juce::Graphics::lowResamplingQuality); 141 LookAndFeel_V4::drawButtonText(_graphics, _textButton, shouldDrawButtonAsHighlighted, shouldDrawButtonAsDown); 142 } 143 144 juce::Font UiObjectStyle::getComboBoxFont(juce::ComboBox& _comboBox) 145 { 146 if (const auto f = getFont()) 147 return *f; 148 149 auto font = LookAndFeel_V4::getComboBoxFont(_comboBox); 150 applyFontProperties(font); 151 return font; 152 } 153 154 juce::Font UiObjectStyle::getLabelFont(juce::Label& _label) 155 { 156 if (const auto f = getFont()) 157 return *f; 158 159 auto font = LookAndFeel_V4::getLabelFont(_label); 160 applyFontProperties(font); 161 return font; 162 } 163 164 juce::Font UiObjectStyle::getPopupMenuFont() 165 { 166 auto f = LookAndFeel_V4::getPopupMenuFont(); 167 f.setHeight(f.getHeight() * 2.0f); 168 return f; 169 } 170 171 juce::Font UiObjectStyle::getTextButtonFont(juce::TextButton& _textButton, int buttonHeight) 172 { 173 if (const auto f = getFont()) 174 return *f; 175 auto font = LookAndFeel_V4::getTextButtonFont(_textButton, buttonHeight); 176 applyFontProperties(font); 177 return font; 178 } 179 180 bool UiObjectStyle::shouldPopupMenuScaleWithTargetComponent(const juce::PopupMenu::Options& _options) 181 { 182 return true; 183 } 184 185 juce::PopupMenu::Options UiObjectStyle::getOptionsForComboBoxPopupMenu(juce::ComboBox& _comboBox, juce::Label& _label) 186 { 187 return LookAndFeel_V4::getOptionsForComboBoxPopupMenu(_comboBox, _label).withStandardItemHeight(0); 188 } 189 190 void UiObjectStyle::applyFontProperties(juce::Font& _font) const 191 { 192 if(m_textHeight) 193 _font.setHeight(static_cast<float>(m_textHeight)); 194 _font.setBold(m_bold); 195 _font.setItalic(m_italic); 196 } 197 198 void UiObjectStyle::applyColorIfNotZero(juce::Component& _target, int _id, const juce::Colour& _col) 199 { 200 if(_col.getARGB()) 201 _target.setColour(_id, _col); 202 } 203 }