Skip to content

Commit

Permalink
Merge pull request #816 from jpcima/theme-selector.2
Browse files Browse the repository at this point in the history
UI: Theme selector
  • Loading branch information
jpcima authored Apr 12, 2021
2 parents 54442fa + e6bb118 commit a838dc3
Show file tree
Hide file tree
Showing 9 changed files with 367 additions and 58 deletions.
6 changes: 5 additions & 1 deletion plugins/editor/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ set(EDITOR_RESOURCES
Fonts/sfizz-fluentui-system-f20.ttf
Fonts/sfizz-misc-icons.ttf
Fonts/Roboto-Regular.ttf
Themes/Default/theme.xml
PARENT_SCOPE)

function(copy_editor_resources SOURCE_DIR DESTINATION_DIR)
Expand Down Expand Up @@ -50,6 +51,8 @@ add_library(sfizz_editor STATIC EXCLUDE_FROM_ALL
src/editor/GUIPiano.cpp
src/editor/DlgAbout.h
src/editor/DlgAbout.cpp
src/editor/Theme.h
src/editor/Theme.cpp
src/editor/ColorHelpers.h
src/editor/ColorHelpers.cpp
src/editor/ImageHelpers.h
Expand Down Expand Up @@ -104,7 +107,8 @@ else()
target_include_directories(sfizz_editor PRIVATE ${sfizz-gio_INCLUDE_DIRS})
target_link_libraries(sfizz_editor PRIVATE ${sfizz-gio_LIBRARIES})
endif()
target_link_libraries(sfizz_editor PRIVATE sfizz::colorspaces sfizz::stb_image sfizz::bit_array sfizz::filesystem)
target_link_libraries(sfizz_editor PRIVATE sfizz::colorspaces sfizz::stb_image
sfizz::bit_array sfizz::filesystem sfizz::pugixml)

# layout tool
if(NOT CMAKE_CROSSCOMPILING)
Expand Down
11 changes: 11 additions & 0 deletions plugins/editor/layout/main.fl
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,17 @@ widget_class mainView {open
class ValueMenu
}
}
Fl_Group {} {
label Theme open
xywh {40 270 105 100} box ROUNDED_BOX labelsize 12 align 17 hide
class TitleGroup
} {
Fl_Spinner themeMenu_ {
comment {tag=kTagThemeMenu}
xywh {60 330 65 25} labelsize 12 textsize 12
class OptionMenu
}
}
}
Fl_Box piano_ {
xywh {5 400 790 70} labelsize 12
Expand Down
34 changes: 34 additions & 0 deletions plugins/editor/resources/Themes/Default/theme.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?xml version="1.0"?>
<sfizz-theme>
<color name="frameBackground">#d3d7cf</color>
<palette name="normal">
<color name="boxBackground">#babdb6</color>
<color name="text">#000000</color>
<color name="inactiveText">#b2b2b2</color>
<color name="highlightedText">#fd9800</color>
<color name="titleBoxText">#ffffff</color>
<color name="titleBoxBackground">#2e3436</color>
<color name="icon">#000000</color>
<color name="iconHighlight">#fd9800</color>
<color name="valueText">#ffffff</color>
<color name="valueBackground">#2e3436</color>
<color name="knobActiveTrack">#00b62a</color>
<color name="knobInactiveTrack">#303030</color>
<color name="knobLineIndicator">#000000</color>
</palette>
<palette name="inverted">
<color name="boxBackground">#2e3436</color>
<color name="text">#ffffff</color>
<color name="inactiveText">#b2b2b2</color>
<color name="highlightedText">#fd9800</color>
<color name="titleBoxText">#000000</color>
<color name="titleBoxBackground">#babdb6</color>
<color name="icon">#b2b2b2</color>
<color name="iconHighlight">#fd9800</color>
<color name="valueText">#000000</color>
<color name="valueBackground">#9a9a9a</color>
<color name="knobActiveTrack">#00b62a</color>
<color name="knobInactiveTrack">#606060</color>
<color name="knobLineIndicator">#ffffff</color>
</palette>
</sfizz-theme>
35 changes: 35 additions & 0 deletions plugins/editor/src/editor/ColorHelpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,38 @@ SColorHCY::SColorHCY(const SColorRGB &rgb)
y = vhcy[2];
a = rgb.a;
}

static int hexDigitFromChar(char c)
{
return (c >= '0' && c <= '9') ? (c - '0') :
(c >= 'a' && c <= 'z') ? (c - 'a' + 10) :
(c >= 'A' && c <= 'Z') ? (c - 'A' + 10) : -1;
}

bool colorFromHex(absl::string_view hex, CColor& color)
{
if (hex.empty() || hex[0] != '#')
return false;

hex = hex.substr(1, hex.size());
size_t length = hex.size();
uint32_t rgba = 0;
if (length == 6 || length == 8) {
for (size_t i = 0; i < length; ++i) {
int d = hexDigitFromChar(hex[i]);
if (d == -1)
return false;

rgba = (rgba << 4) | d;
}
}
if (length == 6)
rgba = (rgba << 8) | 0xff;

color.red = rgba >> 24;
color.green = (rgba >> 16) & 0xff;
color.blue = (rgba >> 8) & 0xff;
color.alpha = rgba & 0xff;

return true;
}
3 changes: 3 additions & 0 deletions plugins/editor/src/editor/ColorHelpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "utility/vstgui_before.h"
#include "vstgui/lib/ccolor.h"
#include "utility/vstgui_after.h"
#include <absl/strings/string_view.h>

using namespace VSTGUI;

Expand All @@ -33,3 +34,5 @@ struct SColorHCY {

float h {}, c {}, y {}, a { 1.0 };
};

bool colorFromHex(absl::string_view hex, CColor& color);
109 changes: 55 additions & 54 deletions plugins/editor/src/editor/Editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "NativeHelpers.h"
#include "VSTGUIHelpers.h"
#include "BitArray.h"
#include "Theme.h"
#include "plugin/MessageUtils.h"
#include <absl/strings/string_view.h>
#include <absl/strings/match.h>
Expand All @@ -32,6 +33,7 @@
#include <cstdarg>
#include <cstdio>
#include <cstring>
#include <memory>

#include "utility/vstgui_before.h"
#include "vstgui/vstgui.h"
Expand All @@ -50,6 +52,7 @@ struct Editor::Impl : EditorController::Receiver, IControlListener {

std::string currentSfzFile_;
std::string currentScalaFile_;
std::string currentThemeName_;
std::string userFilesDir_;
std::string fallbackFilesDir_;

Expand Down Expand Up @@ -91,6 +94,7 @@ struct Editor::Impl : EditorController::Receiver, IControlListener {
kTagSetCCPan,
kTagChooseUserFilesDir,
kTagAbout,
kTagThemeMenu,
kTagFirstChangePanel,
kTagLastChangePanel = kTagFirstChangePanel + kNumPanels - 1,
};
Expand Down Expand Up @@ -119,6 +123,8 @@ struct Editor::Impl : EditorController::Receiver, IControlListener {
CTextLabel* keyswitchLabel_ = nullptr;
CTextLabel* keyswitchInactiveLabel_ = nullptr;
CTextLabel* keyswitchBadge_ = nullptr;
COptionMenu* themeMenu_ = nullptr;
std::unique_ptr<Theme> theme_;

STitleContainer* userFilesGroup_ = nullptr;
STextButton* userFilesDirButton_ = nullptr;
Expand Down Expand Up @@ -590,54 +596,14 @@ void Editor::Impl::createFrameContents()
backgroundBitmap_ = background;

{
const CColor frameBackground = { 0xd3, 0xd7, 0xcf };

struct Palette {
CColor boxBackground;
CColor text;
CColor inactiveText;
CColor highlightedText;
CColor titleBoxText;
CColor titleBoxBackground;
CColor icon;
CColor iconHighlight;
CColor valueText;
CColor valueBackground;
CColor knobActiveTrackColor;
CColor knobInactiveTrackColor;
CColor knobLineIndicatorColor;
};

Palette normalPalette;
normalPalette.boxBackground = { 0xba, 0xbd, 0xb6 };
normalPalette.text = { 0x00, 0x00, 0x00 };
normalPalette.inactiveText = { 0xb2, 0xb2, 0xb2 };
normalPalette.highlightedText = { 0xfd, 0x98, 0x00 };
normalPalette.titleBoxText = { 0xff, 0xff, 0xff };
normalPalette.titleBoxBackground = { 0x2e, 0x34, 0x36 };
normalPalette.icon = normalPalette.text;
normalPalette.iconHighlight = { 0xfd, 0x98, 0x00 };
normalPalette.valueText = { 0xff, 0xff, 0xff };
normalPalette.valueBackground = { 0x2e, 0x34, 0x36 };
normalPalette.knobActiveTrackColor = { 0x00, 0xb6, 0x2a };
normalPalette.knobInactiveTrackColor = { 0x30, 0x30, 0x30 };
normalPalette.knobLineIndicatorColor = { 0x00, 0x00, 0x00 };
Palette invertedPalette;
invertedPalette.boxBackground = { 0x2e, 0x34, 0x36 };
invertedPalette.text = { 0xff, 0xff, 0xff };
invertedPalette.inactiveText = { 0xb2, 0xb2, 0xb2 };
invertedPalette.highlightedText = { 0xfd, 0x98, 0x00 };
invertedPalette.titleBoxText = { 0x00, 0x00, 0x00 };
invertedPalette.titleBoxBackground = { 0xba, 0xbd, 0xb6 };
invertedPalette.icon = { 0xb2, 0xb2, 0xb2 };
invertedPalette.iconHighlight = { 0xfd, 0x98, 0x00 };
invertedPalette.valueText = { 0x00, 0x00, 0x00 };
invertedPalette.valueBackground = { 0x9a, 0x9a, 0x9a };
invertedPalette.knobActiveTrackColor = { 0x00, 0xb6, 0x2a };
invertedPalette.knobInactiveTrackColor = { 0x60, 0x60, 0x60 };
invertedPalette.knobLineIndicatorColor = { 0xff, 0xff, 0xff };
Palette& defaultPalette = normalPalette;

// Try to load the Default theme from disk or hardcoded one as fallback
Theme* theme = new Theme;
theme_.reset(theme);
currentThemeName_ = theme->loadCurrentName();
theme->load(currentThemeName_);

Palette& invertedPalette = theme->invertedPalette;
Palette& defaultPalette = theme->normalPalette;
Palette* palette = &defaultPalette;
auto enterPalette = [&palette](Palette& p) { palette = &p; };

Expand Down Expand Up @@ -697,9 +663,9 @@ void Editor::Impl::createFrameContents()
};
auto createStyledKnob = [this, &palette](const CRect& bounds, int tag, const char*, CHoriTxtAlign, int) {
SStyledKnob* knob = new SStyledKnob(bounds, this, tag);
knob->setActiveTrackColor(palette->knobActiveTrackColor);
knob->setInactiveTrackColor(palette->knobInactiveTrackColor);
knob->setLineIndicatorColor(palette->knobLineIndicatorColor);
knob->setActiveTrackColor(palette->knobActiveTrack);
knob->setInactiveTrackColor(palette->knobInactiveTrack);
knob->setLineIndicatorColor(palette->knobLineIndicator);
return knob;
};
auto createValueLabel = [&palette](const CRect& bounds, int, const char* label, CHoriTxtAlign align, int fontsize) {
Expand Down Expand Up @@ -781,6 +747,18 @@ void Editor::Impl::createFrameContents()
vm->setRoundRectRadius(5.0);
return vm;
};
auto createOptionMenu = [this, &palette](const CRect& bounds, int tag, const char*, CHoriTxtAlign align, int fontsize) {
auto* cb = new COptionMenu(bounds, this, tag);
cb->setHoriAlign(align);
auto font = makeOwned<CFontDesc>("Roboto", fontsize);
cb->setFont(font);
cb->setFontColor(palette->valueText);
cb->setBackColor(palette->valueBackground);
cb->setFrameColor(CColor(0x00, 0x00, 0x00, 0x00));
cb->setStyle(CParamDisplay::kRoundRectStyle);
cb->setRoundRectRadius(5.0);
return cb;
};
auto createGlyphButton = [this, &palette](UTF8StringPtr glyph, const CRect& bounds, int tag, int fontsize) {
STextButton* btn = new STextButton(bounds, this, tag, glyph);
btn->setFont(makeOwned<CFontDesc>("Sfizz Fluent System F20", fontsize));
Expand Down Expand Up @@ -821,10 +799,12 @@ void Editor::Impl::createFrameContents()
btn->setFont(makeOwned<CFontDesc>("Sfizz Fluent System F20", fontsize));
return btn;
};
auto createPiano = [](const CRect& bounds, int, const char*, CHoriTxtAlign, int fontsize) {
auto createPiano = [&palette](const CRect& bounds, int, const char*, CHoriTxtAlign, int fontsize) {
SPiano* piano = new SPiano(bounds);
auto font = makeOwned<CFontDesc>("Roboto", fontsize);
piano->setFont(font);
piano->setFontColor(palette->text);
piano->setBackColor(palette->boxBackground);
return piano;
};
auto createChevronDropDown = [this, &palette](const CRect& bounds, int, const char*, CHoriTxtAlign, int fontsize) {
Expand Down Expand Up @@ -858,7 +838,7 @@ void Editor::Impl::createFrameContents()
box->setNameLabelFontColor(palette->text);
box->setKnobFont(font);
box->setKnobFontColor(palette->text);
box->setKnobLineIndicatorColor(palette->knobLineIndicatorColor);
box->setKnobLineIndicatorColor(palette->knobLineIndicator);
box->setValueToStringFunction([](float value, std::string& text) -> bool {
text = std::to_string(std::lround(value * 127));
return true;
Expand All @@ -877,7 +857,7 @@ void Editor::Impl::createFrameContents()

#include "layout/main.hpp"

mainView->setBackgroundColor(frameBackground);
mainView->setBackgroundColor(theme->frameBackground);

#if LINUX
if (!isZenityAvailable()) {
Expand Down Expand Up @@ -1111,6 +1091,19 @@ void Editor::Impl::createFrameContents()
}

applyBackgroundForCurrentPanel();

if (COptionMenu* menu = themeMenu_) {
const std::vector<std::string>& names = Theme::getAvailableNames();
size_t index = ~size_t(0);
for (size_t i = 0, n = names.size(); i < n; ++i) {
const std::string& name = names[i];
menu->addEntry(UTF8String(name));
if (name == currentThemeName_)
index = i;
}
if (index != ~size_t(0))
menu->setCurrent(index);
}
}

void Editor::Impl::chooseSfzFile()
Expand Down Expand Up @@ -1847,6 +1840,14 @@ void Editor::Impl::valueChanged(CControl* ctl)
Call::later([this]() { aboutDialog_->setVisible(true); });
break;

case kTagThemeMenu:
{
currentThemeName_ = Theme::getAvailableNames()[int(value)];
Theme::storeCurrentName(currentThemeName_);
// TODO: live reload theme
}
break;

default:
if (tag >= kTagFirstChangePanel && tag <= kTagLastChangePanel) {
int panelId = tag - kTagFirstChangePanel;
Expand Down
Loading

0 comments on commit a838dc3

Please sign in to comment.