Skip to content

Commit

Permalink
Traktor: Style sheet now contain a palette of colors by which element…
Browse files Browse the repository at this point in the history
… reference.
  • Loading branch information
apistol78 committed Jun 3, 2024
1 parent bae0bf3 commit 4f8330c
Show file tree
Hide file tree
Showing 8 changed files with 1,256 additions and 955 deletions.
57 changes: 42 additions & 15 deletions code/Ui/StyleSheet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class MemberEntity : public MemberComplex
virtual void serialize(ISerializer& s) const override final
{
s >> Member< std::wstring >(L"typeName", m_ref.typeName);
s >> MemberSmallMap< std::wstring, Color4ub >(L"colors", m_ref.colors);
s >> MemberSmallMap< std::wstring, int32_t >(L"colors", m_ref.colors);
}

private:
Expand All @@ -67,7 +67,7 @@ class MemberEntity : public MemberComplex

}

T_IMPLEMENT_RTTI_FACTORY_CLASS(L"traktor.ui.StyleSheet", 2, StyleSheet, ISerializable)
T_IMPLEMENT_RTTI_FACTORY_CLASS(L"traktor.ui.StyleSheet", 3, StyleSheet, ISerializable)

StyleSheet::Entity* StyleSheet::findEntity(const std::wstring& typeName)
{
Expand All @@ -84,34 +84,57 @@ StyleSheet::Entity* StyleSheet::addEntity(const std::wstring& typeName)
return &entity;
}

void StyleSheet::setColor(const std::wstring& typeName, const std::wstring& element, const Color4ub& color)
int32_t StyleSheet::findColor(const Color4ub& color) const
{
auto it = std::find_if(m_entities.begin(), m_entities.end(), [&](const Entity& entity) {
const auto it = std::find_if(m_palette.begin(), m_palette.end(), [&](const Color4ub& palette) {
return palette == color;
});
return it != m_palette.end() ? (int32_t)std::distance(m_palette.begin(), it) : -1;
}

int32_t StyleSheet::addColor(const Color4ub& color)
{
m_palette.push_back(color);
return (int32_t)m_palette.size() - 1;
}

const Color4ub& StyleSheet::getColor(int32_t index) const
{
return m_palette[index];
}

void StyleSheet::setColor(const std::wstring& typeName, const std::wstring_view& element, const Color4ub& color)
{
int32_t index = findColor(color);
if (index < 0)
index = addColor(color);

const auto it = std::find_if(m_entities.begin(), m_entities.end(), [&](const Entity& entity) {
return entity.typeName == typeName;
});
if (it != m_entities.end())
it->colors[element] = color;
it->colors[element] = index;
else
{
auto& entity = m_entities.push_back();
entity.typeName = typeName;
entity.colors[element] = color;
entity.colors[element] = index;
}
}

Color4ub StyleSheet::getColor(const std::wstring& typeName, const std::wstring_view& element) const
{
auto it = std::find_if(m_entities.begin(), m_entities.end(), [&](const Entity& entity) {
const auto it = std::find_if(m_entities.begin(), m_entities.end(), [&](const Entity& entity) {
return entity.typeName == typeName;
});
if (it == m_entities.end())
return Color4ub(255, 255, 255);

auto it2 = it->colors.find(element);
const auto it2 = it->colors.find(element);
if (it2 == it->colors.end())
return Color4ub(255, 255, 255);

return it2->second;
return m_palette[it2->second];
}

Color4ub StyleSheet::getColor(const Object* widget, const std::wstring_view& element) const
Expand All @@ -126,7 +149,7 @@ Color4ub StyleSheet::getColor(const Object* widget, const std::wstring_view& ele
{
auto it2 = it->colors.find(element);
if (it2 != it->colors.end())
return it2->second;
return m_palette[it2->second];
}
widgetType = widgetType->getSuper();
}
Expand All @@ -140,7 +163,7 @@ void StyleSheet::setValue(const std::wstring& name, const std::wstring_view& val

std::wstring StyleSheet::getValueRaw(const std::wstring_view& name) const
{
auto it = m_values.find(name);
const auto it = m_values.find(name);
return it != m_values.end() ? it->second : L"";
}

Expand Down Expand Up @@ -181,22 +204,26 @@ Ref< StyleSheet > StyleSheet::merge(const StyleSheet* right) const
ss->m_entities = m_entities;
for (const auto& entity : right->m_entities)
{
for (auto it : entity.colors)
ss->setColor(entity.typeName, it.first, it.second);
for (const auto& it : entity.colors)
{
const Color4ub& rclr = right->m_palette[it.second];
ss->setColor(entity.typeName, it.first, rclr);
}
}

ss->m_values = m_values;
for (auto it : right->m_values)
for (const auto& it : right->m_values)
ss->setValue(it.first, it.second);

return ss;
}

void StyleSheet::serialize(ISerializer& s)
{
T_FATAL_ASSERT(s.getVersion< StyleSheet >() >= 2);
T_FATAL_ASSERT(s.getVersion< StyleSheet >() >= 3);
s >> MemberAlignedVector< std::wstring >(L"include", m_include);
s >> MemberAlignedVector< Entity, MemberEntity >(L"entities", m_entities);
s >> MemberAlignedVector< Color4ub >(L"palette", m_palette);
s >> MemberSmallMap< std::wstring, std::wstring >(L"values", m_values);
}

Expand Down
39 changes: 31 additions & 8 deletions code/Ui/StyleSheet.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* TRAKTOR
* Copyright (c) 2022 Anders Pistol.
* Copyright (c) 2022-2024 Anders Pistol.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
Expand All @@ -22,10 +22,8 @@
# define T_DLLCLASS T_DLLIMPORT
#endif

namespace traktor
namespace traktor::ui
{
namespace ui
{

class StyleBitmap;

Expand All @@ -40,25 +38,51 @@ class T_DLLCLASS StyleSheet : public ISerializable
struct Entity
{
std::wstring typeName;
SmallMap< std::wstring, Color4ub > colors;
SmallMap< std::wstring, int32_t > colors;
};

//@name Entity
//@{

Entity* findEntity(const std::wstring& typeName);

Entity* addEntity(const std::wstring& typeName);

void setColor(const std::wstring& typeName, const std::wstring& element, const Color4ub& color);
//@}

//@name Palette
//@{

int32_t findColor(const Color4ub& color) const;

int32_t addColor(const Color4ub& color);

const Color4ub& getColor(int32_t index) const;

//@}

//@name Entity element colors.
//@{

void setColor(const std::wstring& typeName, const std::wstring_view& element, const Color4ub& color);

Color4ub getColor(const std::wstring& typeName, const std::wstring_view& element) const;

Color4ub getColor(const Object* widget, const std::wstring_view& element) const;

//@}

//@name Values
//@{

void setValue(const std::wstring& name, const std::wstring_view& value);

std::wstring getValueRaw(const std::wstring_view& name) const;

std::wstring getValue(const std::wstring_view& name) const;

//@}

/*! Merge this style sheet with another.
*
* Styles defined in right override existing styles.
Expand All @@ -80,9 +104,8 @@ class T_DLLCLASS StyleSheet : public ISerializable
private:
AlignedVector< std::wstring > m_include;
AlignedVector< Entity > m_entities;
AlignedVector< Color4ub > m_palette;
SmallMap< std::wstring, std::wstring > m_values;
};

}
}

8 changes: 5 additions & 3 deletions code/Ui/Theme/App/ThemeForm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -282,8 +282,10 @@ void ThemeForm::updateTree()

for (const auto& it : entity.colors)
{
const Color4ub& color = m_styleSheet->getColor(it.second);

Ref< drawing::Image > imageColor = new drawing::Image(drawing::PixelFormat::getR8G8B8A8(), 16, 16);
imageColor->clear(Color4f::fromColor4ub(it.second).rgb1());
imageColor->clear(Color4f::fromColor4ub(color).rgb1());

const int32_t imageIndex = m_treeTheme->addImage(new Bitmap(imageColor));

Expand All @@ -307,7 +309,7 @@ void ThemeForm::updatePalette()
for (const auto& entity : m_styleSheet->getEntities())
{
for (auto it : entity.colors)
palette.insert(it.second);
palette.insert(m_styleSheet->getColor(it.second));
}

for (const auto& color : palette)
Expand Down Expand Up @@ -676,7 +678,7 @@ void ThemeForm::eventTreeChange(TreeViewContentChangeEvent* event)

auto it = entity->colors.find(event->getOriginalText());
T_ASSERT(it != entity->colors.end());
Color4ub color = it->second;
Color4ub color = m_styleSheet->getColor(it->second);
entity->colors.erase(it);

if (!modifiedItem->getText().empty())
Expand Down
Loading

0 comments on commit 4f8330c

Please sign in to comment.