Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Themes support for components #13

Open
wants to merge 30 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
4a5d8a5
Allow ReferenceCountedObject to send around const pointers
kunitoki Aug 1, 2024
0132fa1
Allow fonts to be constructed without a rive::Factory
kunitoki Aug 1, 2024
af88187
Add ApplicationThheme and initial theme (v1)
kunitoki Aug 1, 2024
94b5835
Code formatting
yup-bot Aug 1, 2024
bbd4d38
Fix ide grouping
kunitoki Aug 1, 2024
d58539d
Code formatting
yup-bot Aug 1, 2024
27f9450
Fix formatting
kunitoki Aug 1, 2024
e4822d4
Code formatting
yup-bot Aug 1, 2024
2d7bb67
Fix formatted comment tricking the clang formatter
kunitoki Aug 1, 2024
c06f8c3
Code formatting
yup-bot Aug 1, 2024
929aa2d
Added tests
kunitoki Aug 1, 2024
25e8fc3
Get rid of formatting issues
kunitoki Aug 1, 2024
157d810
Code formatting
yup-bot Aug 1, 2024
24cfc18
Allow using Identifier in std::unordered_map
kunitoki Aug 1, 2024
e34cc87
Allow components to work with colors
kunitoki Aug 1, 2024
0e20445
Code formatting
yup-bot Aug 1, 2024
7ddfdce
Avoid getting crazy with clang format
kunitoki Aug 1, 2024
cfeac25
Code formatting
yup-bot Aug 1, 2024
53a1104
Code formatting
kunitoki Aug 1, 2024
1c29716
Code formatting
yup-bot Aug 1, 2024
3ee53ea
Merge branch 'main' into dev/themes
kunitoki Aug 20, 2024
e449cb0
Code formatting
yup-bot Aug 20, 2024
4bb408e
Merge branch 'main' into dev/themes
kunitoki Oct 30, 2024
24005e2
Code formatting
yup-bot Oct 30, 2024
c7883ea
Merge branch 'main' into dev/themes
kunitoki Oct 30, 2024
c006fca
Code formatting
yup-bot Oct 30, 2024
0a045a9
More themes work
kunitoki Nov 4, 2024
2a5d42f
Code formatting
yup-bot Nov 4, 2024
d494894
Merge branch 'main' into dev/themes
kunitoki Nov 4, 2024
133f1db
Merge branch 'main' into dev/themes
kunitoki Dec 5, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions examples/graphics/source/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,21 +47,21 @@ class CustomWindow
return;
}

/*
#if JUCE_WASM
yup::File dataPath = yup::File ("/data");
#else
yup::File dataPath = yup::File (__FILE__).getParentDirectory().getSiblingFile ("data");
#endif

yup::File fontFilePath = dataPath.getChildFile ("Roboto-Regular.ttf");

if (auto result = font.loadFromFile (fontFilePath, factory); result.failed())
if (auto result = font.loadFromFile (fontFilePath); result.failed())
yup::Logger::outputDebugString (result.getErrorMessage());
*/

setTitle ("main");

for (int i = 0; i < totalRows * totalColumns; ++i)
addAndMakeVisible (sliders.add (std::make_unique<yup::Slider> (yup::String (i), font)));
addAndMakeVisible (sliders.add (std::make_unique<yup::Slider> (yup::String (i))));

//button = std::make_unique<yup::TextButton> ("xyz", font);
//addAndMakeVisible (*button);
Expand Down
18 changes: 9 additions & 9 deletions modules/juce_core/memory/juce_ReferenceCountedObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,15 +88,15 @@ class JUCE_API ReferenceCountedObject
This is done automatically by the smart pointer, but is public just
in case it's needed for nefarious purposes.
*/
void incReferenceCount() noexcept
void incReferenceCount() const noexcept
{
++refCount;
}

/** Decreases the object's reference count.
If the count gets to zero, the object will be deleted.
*/
void decReferenceCount() noexcept
void decReferenceCount() const noexcept
{
jassert (getReferenceCount() > 0);

Expand All @@ -108,7 +108,7 @@ class JUCE_API ReferenceCountedObject
If the count gets to zero, the object will not be deleted, but this method
will return true, allowing the caller to take care of deletion.
*/
bool decReferenceCountWithoutDeleting() noexcept
bool decReferenceCountWithoutDeleting() const noexcept
{
jassert (getReferenceCount() > 0);
return --refCount == 0;
Expand Down Expand Up @@ -144,14 +144,14 @@ class JUCE_API ReferenceCountedObject
/** Resets the reference count to zero without deleting the object.
You should probably never need to use this!
*/
void resetReferenceCount() noexcept
void resetReferenceCount() const noexcept
{
refCount = 0;
}

private:
//==============================================================================
Atomic<int> refCount { 0 };
mutable Atomic<int> refCount { 0 };
friend struct ContainerDeletePolicy<ReferenceCountedObject>;
};

Expand All @@ -177,15 +177,15 @@ class JUCE_API SingleThreadedReferenceCountedObject
This is done automatically by the smart pointer, but is public just
in case it's needed for nefarious purposes.
*/
void incReferenceCount() noexcept
void incReferenceCount() const noexcept
{
++refCount;
}

/** Decreases the object's reference count.
If the count gets to zero, the object will be deleted.
*/
void decReferenceCount() noexcept
void decReferenceCount() const noexcept
{
jassert (getReferenceCount() > 0);

Expand All @@ -197,7 +197,7 @@ class JUCE_API SingleThreadedReferenceCountedObject
If the count gets to zero, the object will not be deleted, but this method
will return true, allowing the caller to take care of deletion.
*/
bool decReferenceCountWithoutDeleting() noexcept
bool decReferenceCountWithoutDeleting() const noexcept
{
jassert (getReferenceCount() > 0);
return --refCount == 0;
Expand Down Expand Up @@ -232,7 +232,7 @@ class JUCE_API SingleThreadedReferenceCountedObject

private:
//==============================================================================
int refCount = 0;
mutable int refCount = 0;
friend struct ContainerDeletePolicy<ReferenceCountedObject>;
};

Expand Down
16 changes: 8 additions & 8 deletions modules/yup_graphics/fonts/yup_Font.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,25 +24,25 @@ namespace yup

//==============================================================================

Font::Font (const MemoryBlock& fontBytes, rive::Factory* factory)
Font::Font (const MemoryBlock& fontBytes)
{
loadFromData (fontBytes, factory);
loadFromData (fontBytes);
}

Font::Font (const File& fontFile, rive::Factory* factory)
Font::Font (const File& fontFile)
{
loadFromFile (fontFile, factory);
loadFromFile (fontFile);
}

//==============================================================================

Result Font::loadFromData (const MemoryBlock& fontBytes, rive::Factory* factory)
Result Font::loadFromData (const MemoryBlock& fontBytes)
{
font = factory->decodeFont (rive::Span<const uint8_t> { static_cast<const uint8_t*> (fontBytes.getData()), fontBytes.getSize() });
font = HBFont::Decode (rive::Span<const uint8_t> { static_cast<const uint8_t*> (fontBytes.getData()), fontBytes.getSize() });
return font ? Result::ok() : Result::fail ("Unable to load font");
}

Result Font::loadFromFile (const File& fontFile, rive::Factory* factory)
Result Font::loadFromFile (const File& fontFile)
{
if (! fontFile.existsAsFile())
return Result::fail ("Unable to load font from non existing file");
Expand All @@ -52,7 +52,7 @@ Result Font::loadFromFile (const File& fontFile, rive::Factory* factory)
yup::MemoryBlock mb;
is->readIntoMemoryBlock (mb);

font = factory->decodeFont (rive::Span<const uint8_t> { static_cast<const uint8_t*> (mb.getData()), mb.getSize() });
font = HBFont::Decode (rive::Span<const uint8_t> { static_cast<const uint8_t*> (mb.getData()), mb.getSize() });
if (! font)
return Result::fail ("Unable to load font");
}
Expand Down
8 changes: 4 additions & 4 deletions modules/yup_graphics/fonts/yup_Font.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ class JUCE_API Font
Font() = default;

//==============================================================================
Font (const MemoryBlock& fontBytes, rive::Factory* factory);
Font (const File& fontFile, rive::Factory* factory);
Font (const MemoryBlock& fontBytes);
Font (const File& fontFile);

//==============================================================================
/** Copy and move constructors and assignment operators. */
Expand All @@ -42,10 +42,10 @@ class JUCE_API Font
Font& operator= (Font&& other) noexcept = default;

//==============================================================================
Result loadFromData (const MemoryBlock& fontBytes, rive::Factory* factory);
Result loadFromData (const MemoryBlock& fontBytes);

//==============================================================================
Result loadFromFile (const File& fontFile, rive::Factory* factory);
Result loadFromFile (const File& fontFile);

//==============================================================================
rive::rcp<rive::Font> getFont() const;
Expand Down
6 changes: 5 additions & 1 deletion modules/yup_graphics/yup_graphics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
*/

#ifdef YUP_GRAPHICS_H_INCLUDED
/* When you add this cpp file to your project, you mustn't include it in a file where you've
/* When you add this cpp file to your project, you mustn't include it in a file where you've
already included any other headers - just put it inside a file on its own, possibly with your config
flags preceding it, but don't include anything else. That also includes avoiding any automatic prefix
header files that the compiler may be using.
Expand All @@ -32,6 +32,10 @@

//==============================================================================

#include <rive/text/font_hb.hpp>

//==============================================================================

#if JUCE_WINDOWS
#include <array>
#include <dxgi1_2.h>
Expand Down
2 changes: 2 additions & 0 deletions modules/yup_gui/native/yup_Windowing_glfw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1332,6 +1332,8 @@ void initialiseYup_Windowing()
glfwWindowHint (GLFW_CONTEXT_VERSION_MINOR, 6);
#endif

ApplicationTheme::setGlobalTheme (createThemeVersion1());

Desktop::getInstance()->updateDisplays();

glfwSetMonitorCallback (+[] (GLFWmonitor* monitor, int event)
Expand Down
1 change: 1 addition & 0 deletions modules/yup_gui/themes/theme_v1/RobotoRegularFont.inc

Large diffs are not rendered by default.

122 changes: 122 additions & 0 deletions modules/yup_gui/themes/theme_v1/yup_ThemeVersion1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
/*
==============================================================================

This file is part of the YUP library.
Copyright (c) 2024 - [email protected]

YUP is an open source library subject to open-source licensing.

The code included in this file is provided under the terms of the ISC license
http://www.isc.org/downloads/software-support-policy/isc-license. Permission
to use, copy, modify, and/or distribute this software for any purpose with or
without fee is hereby granted provided that the above copyright notice and
this permission notice appear in all copies.

YUP IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
DISCLAIMED.

==============================================================================
*/

namespace yup
{

//==============================================================================

extern const uint8_t RobotoRegularFont_data[];
extern const std::size_t RobotoRegularFont_size;

//==============================================================================

ApplicationTheme::Ptr createThemeVersion1()
{
auto theme = new ApplicationTheme;

Font font;
if (auto result = font.loadFromData (MemoryBlock (&RobotoRegularFont_data[0], RobotoRegularFont_size)); result.failed())
yup::Logger::outputDebugString (result.getErrorMessage());

theme->setDefaultFont (std::move (font));

theme->setComponentTheme<Slider::Theme> ({ [theme] (Graphics& g, const Slider& s)
{
auto bounds = s.getLocalBounds().reduced (s.proportionOfWidth (0.1f));
const auto center = bounds.getCenter();
kunitoki marked this conversation as resolved.
Show resolved Hide resolved

constexpr auto fromRadians = degreesToRadians (135.0f);
constexpr auto toRadians = fromRadians + degreesToRadians (270.0f);

Path backgroundPath;
backgroundPath.addEllipse (bounds.reduced (s.proportionOfWidth (0.105f)));

g.setFillColor (Color (0xff3d3d3d));
g.fillPath (backgroundPath);

g.setStrokeColor (Color (0xff2b2b2b));
g.setStrokeWidth (s.proportionOfWidth (0.0175f));
g.strokePath (backgroundPath);

Path backgroundArc;
backgroundArc.addCenteredArc (center,
bounds.getWidth() / 2.0f,
bounds.getHeight() / 2.0f,
0.0f,
fromRadians,
toRadians,
true);

g.setStrokeCap (StrokeCap::Round);
g.setStrokeColor (Color (0xff636363));
g.setStrokeWidth (s.proportionOfWidth (0.075f));
g.strokePath (backgroundArc);

const auto toCurrentRadians = fromRadians + degreesToRadians (270.0f) * s.getValue();

Path foregroundArc;
foregroundArc.addCenteredArc (center,
bounds.getWidth() / 2.0f,
bounds.getHeight() / 2.0f,
0.0f,
fromRadians,
toCurrentRadians,
true);

g.setStrokeCap (StrokeCap::Round);
g.setStrokeColor (s.isMouseOver() ? Color (0xff4ebfff).brighter (0.3f) : Color (0xff4ebfff));
g.setStrokeWidth (s.proportionOfWidth (0.075f));
g.strokePath (foregroundArc);

const auto reducedBounds = bounds.reduced (s.proportionOfWidth (0.175f));
const auto pos = center.getPointOnCircumference (
reducedBounds.getWidth() / 2.0f,
reducedBounds.getHeight() / 2.0f,
toCurrentRadians);

Path foregroundLine;
foregroundLine.addLine (Line<float> (pos, center).keepOnlyStart (0.25f));

g.setStrokeCap (StrokeCap::Round);
g.setStrokeColor (Color (0xffffffff));
g.setStrokeWidth (s.proportionOfWidth (0.03f));
g.strokePath (foregroundLine);

StyledText text;
text.appendText (theme->getDefaultFont(), s.proportionOfHeight (0.1f), s.proportionOfHeight (0.1f), String (s.getValue(), 3).toRawUTF8());
text.layout (s.getLocalBounds().reduced (5).removeFromBottom (s.proportionOfWidth (0.1f)), StyledText::center);

g.setStrokeColor (Color (0xffffffff));
g.strokeFittedText (text, s.getLocalBounds().reduced (5).removeFromBottom (s.proportionOfWidth (0.1f)));

if (s.hasFocus())
{
g.setStrokeColor (Color (0xffff5f2b));
g.setStrokeWidth (s.proportionOfWidth (0.0175f));
g.strokeRect (s.getLocalBounds());
}
} });

return theme;
}

} // namespace yup
29 changes: 29 additions & 0 deletions modules/yup_gui/themes/theme_v1/yup_ThemeVersion1.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
==============================================================================

This file is part of the YUP library.
Copyright (c) 2024 - [email protected]

YUP is an open source library subject to open-source licensing.

The code included in this file is provided under the terms of the ISC license
http://www.isc.org/downloads/software-support-policy/isc-license. Permission
to use, copy, modify, and/or distribute this software for any purpose with or
without fee is hereby granted provided that the above copyright notice and
this permission notice appear in all copies.

YUP IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
DISCLAIMED.

==============================================================================
*/

namespace yup
{

//==============================================================================

ApplicationTheme::Ptr createThemeVersion1();

} // namespace yup
33 changes: 33 additions & 0 deletions modules/yup_gui/themes/theme_v1/yup_ThemeVersion1_Resources.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
==============================================================================

This file is part of the YUP library.
Copyright (c) 2024 - [email protected]

YUP is an open source library subject to open-source licensing.

The code included in this file is provided under the terms of the ISC license
http://www.isc.org/downloads/software-support-policy/isc-license. Permission
to use, copy, modify, and/or distribute this software for any purpose with or
without fee is hereby granted provided that the above copyright notice and
this permission notice appear in all copies.

YUP IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
DISCLAIMED.

==============================================================================
*/

namespace yup
{

//==============================================================================

const uint8_t RobotoRegularFont_data[] = {
#include "RobotoRegularFont.inc"
};

const std::size_t RobotoRegularFont_size = sizeof (RobotoRegularFont_data);

} // namespace yup
Loading