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

Core - Rework style / theme #174

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
25 changes: 25 additions & 0 deletions demo/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ int main(int argc, char* argv[])
brls::Application::registerXMLView("RecyclingListTab", RecyclingListTab::create);
brls::Application::registerXMLView("ComponentsTab", ComponentsTab::create);

/*
// Add custom values to the theme
brls::getLightTheme().addColor("captioned_image/caption", nvgRGB(2, 176, 183));
brls::getDarkTheme().addColor("captioned_image/caption", nvgRGB(51, 186, 227));
Expand All @@ -69,6 +70,30 @@ int main(int argc, char* argv[])
brls::getStyle().addMetric("about/padding_top_bottom", 50);
brls::getStyle().addMetric("about/padding_sides", 75);
brls::getStyle().addMetric("about/description_margin", 50);
*/

const std::string captionedImageThemeXML = R"xml(
<brls:Stylesheet theme="brls/default" prefix="captioned_image">
<brls:ThemeVariant name="light">
<brls:Color name="caption" value="rgb(2,176,183)"/>
</brls:ThemeVariant>

<brls:ThemeVariant name="dark">
<brls:Color name="caption" value="rgb(51,186,227)"/>
</brls:ThemeVariant>
</brls:Stylesheet>
)xml";

const std::string aboutThemeXML = R"xml(
<brls:Stylesheet theme="brls/default" prefix="about">
<brls:Metric name="padding_top_bottom" value="50.0"/>
<brls:Metric name="padding_sides" value="75.0"/>
<brls:Metric name="description_margin" value="50.0"/>
</brls:Stylesheet>
)xml";

brls::Application::getTheme().inflateFromXMLString(captionedImageThemeXML);
brls::Application::getTheme().inflateFromXMLString(aboutThemeXML);

// Create and push the main activity to the stack
brls::Application::pushActivity(new MainActivity());
Expand Down
6 changes: 4 additions & 2 deletions library/include/borealis/core/application.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,10 @@ class Application

inline static Style getStyle()
{
return brls::getStyle();
return *style;
}

static Theme getTheme();
static Theme &getTheme();
static ThemeVariant getThemeVariant();

/**
Expand Down Expand Up @@ -195,6 +195,8 @@ class Application
inline static bool quitRequested = false;

inline static Platform* platform = nullptr;
inline static Theme* theme = nullptr;
inline static Style* style = nullptr;

inline static std::string title;

Expand Down
2 changes: 1 addition & 1 deletion library/include/borealis/core/frame_context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class FrameContext
NVGcontext* vg = nullptr;
float pixelRatio = 0.0;
FontStash* fontStash = nullptr;
Theme theme = nullptr;
Theme theme;
};

} // namespace brls
34 changes: 11 additions & 23 deletions library/include/borealis/core/style.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Copyright 2019-2020 natinusala
Copyright 2019 WerWolv
Copyright 2019 p-sam
Copyright 2021 EmreTech

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand All @@ -25,32 +26,19 @@
namespace brls
{

class StyleValues
{
public:
StyleValues(std::initializer_list<std::pair<std::string, float>> list);

void addMetric(std::string name, float value);
float getMetric(std::string name);

private:
std::unordered_map<std::string, float> values;
};
struct Theme;

// Simple wrapper around StyleValues for the array operator
class Style
// Wrapper to grab metrics from a theme
// KEPT FOR BACKWARDS COMPATIBILITY
struct Style
{
public:
Style(StyleValues* values);
float operator[](std::string name);

void addMetric(std::string name, float value);
float getMetric(std::string name);
Style(Theme &theme);

// Shortcut for Theme.getMetric(name)
float operator[](const std::string name);

private:
StyleValues* values;
private:
Theme &parentTheme;
};

Style getStyle();

} // namespace brls
46 changes: 24 additions & 22 deletions library/include/borealis/core/theme.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/*
Copyright 2019-2020 natinusala
Copyright 2019 p-sam
Copyright 2021 EmreTech

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand All @@ -18,8 +19,8 @@
#pragma once

#include <nanovg.h>
#include <tinyxml2.h>

#include <initializer_list>
#include <string>
#include <unordered_map>

Expand All @@ -32,33 +33,34 @@ enum class ThemeVariant
DARK
};

class ThemeValues
struct Theme
{
public:
ThemeValues(std::initializer_list<std::pair<std::string, NVGcolor>> list);
Theme(std::string name);
Theme() = default;

void addColor(std::string name, NVGcolor color);
NVGcolor getColor(std::string name);
void inflateFromXMLElement(tinyxml2::XMLElement *element);
void inflateFromXMLString(const std::string xml);
void inflateFromXMLFile(const std::string path);

private:
std::unordered_map<std::string, NVGcolor> values;
};
//float getMetric(const std::string path, ThemeVariant variant);

// Simple wrapper around ThemeValues for the array operator
class Theme
{
public:
Theme(ThemeValues* values);
NVGcolor operator[](std::string name);
NVGcolor getColor(const std::string path, ThemeVariant variant);
float getMetric(const std::string path);
NVGcolor getColor(const std::string path);

void addColor(std::string name, NVGcolor color);
NVGcolor getColor(std::string name);
// Shortcut for getColor(name)
NVGcolor operator[](const std::string name);

private:
ThemeValues* values;
};
void getAllMetricKeys(const std::string prefix);

Theme getLightTheme();
Theme getDarkTheme();
private:
// Each color/metric has a key of their theme variant + prefix
// An example: "dark/brls/sidebar/background"

std::unordered_map<std::string, NVGcolor> colors;
std::unordered_map<std::string, float> metrics;

std::string name;
};

} // namespace brls
81 changes: 74 additions & 7 deletions library/lib/core/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,80 @@ constexpr uint32_t ORIGINAL_WINDOW_HEIGHT = 720;
namespace brls
{

const std::string generalThemeXML = R"xml(
<brls:Stylesheet theme="brls/default" prefix="brls">
<brls:ThemeVariant name="light">
<brls:Color name="background" value="rgb(235,235,235)"/>
<brls:Color name="text" value="rgb(45,45,45)"/>
<brls:Color name="backdrop" value="rgba(0,0,0,178)"/>
<brls:Color name="click_pulse" value="rgba(13,182,213,38)"/>
</brls:ThemeVariant>

<brls:ThemeVariant name="dark">
<brls:Color name="background" value="rgb(45,45,45)"/>
<brls:Color name="text" value="rgb(255,255,255)"/>
<brls:Color name="backdrop" value="rgba(0,0,0,178)"/>
<brls:Color name="click_pulse" value="rgba(25,138,198,38)"/>
</brls:ThemeVariant>
</brls:Stylesheet>
)xml";

const std::string highlightThemeXML = R"xml(
<brls:Stylesheet theme="brls/default" prefix="brls/highlight">
<brls:ThemeVariant name="light">
<brls:Color name="background" value="rgb(252,255,248)"/>
<brls:Color name="color1" value="rgb(13,182,213)"/>
<brls:Color name="color2" value="rgb(80,239,217)"/>
</brls:ThemeVariant>

<brls:ThemeVariant name="dark">
<brls:Color name="background" value="rgb(31,34,39)"/>
<brls:Color name="color1" value="rgb(25,138,198)"/>
<brls:Color name="color2" value="rgb(137,241,242)"/>
</brls:ThemeVariant>

<brls:Metric name="stroke_width" value="5.0"/>
<brls:Metric name="corner_radius" value="0.5"/>
<brls:Metric name="shadow_width" value="2.0"/>
<brls:Metric name="shadow_offset" value="10.0"/>
<brls:Metric name="shadow_feather" value="10.0"/>
<brls:Metric name="shadow_opacity" value="128.0"/>
</brls:Stylesheet>
)xml";

const std::string animationThemeXML = R"xml(
<brls:Stylesheet theme="brls/default" prefix="brls/animations">
<brls:Metric name="show" value="250.0"/>
<brls:Metric name="show_slide" value="125.0"/>

<brls:Metric name="highlight" value="100.0"/>
<brls:Metric name="highlight_shake" value="15.0"/>

<brls:Metric name="label_scrolling_timer" value="1500.0"/>
<brls:Metric name="label_scrolling_speed" value="0.05"/>
</brls:Stylesheet>
)xml";

const std::string shadowThemeXML = R"xml(
<brls:Stylesheet theme="brls/default" prefix="brls/shadow">
<brls:Metric name="width" value="2.0"/>
<brls:Metric name="feather" value="10.0"/>
<brls:Metric name="opacity" value="63.75"/>
<brls:Metric name="offset" value="10.0"/>
</brls:Stylesheet>
)xml";

bool Application::init()
{
// Init platform
Application::platform = Platform::createPlatform();
Application::theme = new Theme("brls/default");
Application::style = new Style(*Application::theme);

Application::theme->inflateFromXMLString(generalThemeXML);
Application::theme->inflateFromXMLString(highlightThemeXML);
Application::theme->inflateFromXMLString(animationThemeXML);
Application::theme->inflateFromXMLString(shadowThemeXML);

if (!Application::platform)
{
Expand Down Expand Up @@ -375,10 +445,10 @@ void Application::frame()
frameContext.pixelRatio = (float)Application::windowWidth / (float)Application::windowHeight;
frameContext.vg = Application::getNVGContext();
frameContext.fontStash = &Application::fontStash;
frameContext.theme = Application::getTheme();
frameContext.theme = *Application::theme;

// Begin frame and clear
NVGcolor backgroundColor = frameContext.theme["brls/background"];
NVGcolor backgroundColor = frameContext.theme.getColor("brls/background");
videoContext->beginFrame();
videoContext->clear(backgroundColor);

Expand Down Expand Up @@ -632,12 +702,9 @@ void Application::clear()
Application::activitiesStack.clear();
}

Theme Application::getTheme()
Theme &Application::getTheme()
{
if (Application::getThemeVariant() == ThemeVariant::LIGHT)
return getLightTheme();
else
return getDarkTheme();
return *theme;
}

ThemeVariant Application::getThemeVariant()
Expand Down
Loading