Skip to content

Commit

Permalink
Traktor: Implemented RGBA channel selector in texture asset editor.
Browse files Browse the repository at this point in the history
  • Loading branch information
apistol78 committed Jun 17, 2024
1 parent 4f82e83 commit a9b91b6
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 8 deletions.
24 changes: 18 additions & 6 deletions code/Render/Editor/Texture/TextureAssetEditorPage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include "Ui/StatusBar/StatusBar.h"
#include "Ui/ToolBar/ToolBar.h"
#include "Ui/ToolBar/ToolBarButton.h"
#include "Ui/ToolBar/ToolBarButtonClickEvent.h"

namespace traktor::render
{
Expand Down Expand Up @@ -60,10 +61,15 @@ bool TextureAssetEditorPage::create(ui::Container* parent)
toolBar->addImage(new ui::StyleBitmap(L"Texture.RGBA", 1));
toolBar->addImage(new ui::StyleBitmap(L"Texture.RGBA", 2));
toolBar->addImage(new ui::StyleBitmap(L"Texture.RGBA", 3));
toolBar->addItem(new ui::ToolBarButton(L"R", 0, ui::Command(L"Render.Texture.Editor.ToggleR"), ui::ToolBarButton::BsDefaultToggled));
toolBar->addItem(new ui::ToolBarButton(L"G", 1, ui::Command(L"Render.Texture.Editor.ToggleG"), ui::ToolBarButton::BsDefaultToggled));
toolBar->addItem(new ui::ToolBarButton(L"B", 2, ui::Command(L"Render.Texture.Editor.ToggleB"), ui::ToolBarButton::BsDefaultToggled));
toolBar->addItem(new ui::ToolBarButton(L"A", 3, ui::Command(L"Render.Texture.Editor.ToggleA"), ui::ToolBarButton::BsDefaultToggled));
m_toolToggleR = new ui::ToolBarButton(L"R", 0, ui::Command(L"Render.Texture.Editor.ToggleR"), ui::ToolBarButton::BsDefaultToggled);
m_toolToggleG = new ui::ToolBarButton(L"G", 1, ui::Command(L"Render.Texture.Editor.ToggleG"), ui::ToolBarButton::BsDefaultToggled);
m_toolToggleB = new ui::ToolBarButton(L"B", 2, ui::Command(L"Render.Texture.Editor.ToggleB"), ui::ToolBarButton::BsDefaultToggled);
m_toolToggleA = new ui::ToolBarButton(L"A", 3, ui::Command(L"Render.Texture.Editor.ToggleA"), ui::ToolBarButton::BsDefaultToggled);
toolBar->addItem(m_toolToggleR);
toolBar->addItem(m_toolToggleG);
toolBar->addItem(m_toolToggleB);
toolBar->addItem(m_toolToggleA);
toolBar->addEventHandler< ui::ToolBarButtonClickEvent >([=, this](ui::ToolBarButtonClickEvent* event) { updatePreview(); });

Ref< ui::Container > imageContainer = new ui::Container();
imageContainer->create(container, ui::WsNone, new ui::TableLayout(L"100%", L"100%,*", 0_ut, 0_ut));
Expand Down Expand Up @@ -127,7 +133,13 @@ void TextureAssetEditorPage::updatePreview()
Ref< drawing::Image > image = drawing::Image::load(fileName);
if (image)
{
m_textureControl->setImage(image, m_asset->m_output);
const uint32_t channels =
(m_toolToggleR->isToggled() ? 1 : 0) |
(m_toolToggleG->isToggled() ? 2 : 0) |
(m_toolToggleB->isToggled() ? 4 : 0) |
(m_toolToggleA->isToggled() ? 8 : 0);

m_textureControl->setImage(image, m_asset->m_output, channels);

StringOutputStream ss;
ss << image->getWidth() << L" * " << image->getHeight();
Expand All @@ -148,7 +160,7 @@ void TextureAssetEditorPage::updatePreview()
}
else
{
m_textureControl->setImage(nullptr, m_asset->m_output);
m_textureControl->setImage(nullptr, m_asset->m_output, ~0U);
m_statusBar->setText(0, L"");
}
}
Expand Down
5 changes: 5 additions & 0 deletions code/Render/Editor/Texture/TextureAssetEditorPage.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ namespace traktor::ui

class ContentChangeEvent;
class MouseMoveEvent;
class ToolBarButton;
class StatusBar;

}
Expand Down Expand Up @@ -65,6 +66,10 @@ class TextureAssetEditorPage : public editor::IEditorPage
editor::IEditorPageSite* m_site;
editor::IDocument* m_document;
Ref< TextureAsset > m_asset;
Ref< ui::ToolBarButton > m_toolToggleR;
Ref< ui::ToolBarButton > m_toolToggleG;
Ref< ui::ToolBarButton > m_toolToggleB;
Ref< ui::ToolBarButton > m_toolToggleA;
Ref< TextureControl > m_textureControl;
Ref< ui::StatusBar > m_statusBar;
Ref< editor::PropertiesView > m_propertiesView;
Expand Down
26 changes: 25 additions & 1 deletion code/Render/Editor/Texture/TextureControl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ ui::Size TextureControl::getPreferredSize(const ui::Size& hint) const
return m_bitmapSource ? m_bitmapSource->getSize(this) : ui::Size(0, 0);
}

bool TextureControl::setImage(drawing::Image* image, const TextureOutput& output)
bool TextureControl::setImage(drawing::Image* image, const TextureOutput& output, uint32_t channels)
{
m_imageSource = nullptr;
m_imageOutput = nullptr;
Expand All @@ -86,6 +86,8 @@ bool TextureControl::setImage(drawing::Image* image, const TextureOutput& output

// Create source image.
m_imageSource = image->clone();

// Adjust gamma.
if (!sRGB)
{
// Ensure image is in sRGB since that's what UI expects.
Expand Down Expand Up @@ -191,6 +193,28 @@ bool TextureControl::setImage(drawing::Image* image, const TextureOutput& output
m_imageOutput->apply(&normalizeFilter);
}

// Filter channels.
if ((channels & (1 | 2 | 4 | 8)) != (1 | 2 | 4 | 8))
{
std::wstring swizzle = L"0001";

if (channels & 1)
swizzle[0] = L'R';
if (channels & 2)
swizzle[1] = L'G';
if (channels & 4)
swizzle[2] = L'B';
if (channels & 8)
swizzle[3] = L'A';

// Special case if only alpha is visible.
if (channels == 8)
swizzle = L"AAA1";

const drawing::SwizzleFilter swizzleFilter(swizzle);
m_imageOutput->apply(&swizzleFilter);
}

// Convert to sRGB last since that's what UI expect.
{
const drawing::GammaFilter gammaFilter(1.0f, 2.2f);
Expand Down
2 changes: 1 addition & 1 deletion code/Render/Editor/Texture/TextureControl.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class T_DLLCLASS TextureControl : public ui::Widget

virtual ui::Size getPreferredSize(const ui::Size& hint) const override;

bool setImage(drawing::Image* image, const TextureOutput& output);
bool setImage(drawing::Image* image, const TextureOutput& output, uint32_t channels);

bool getPixel(const ui::Point& position, Color4f& outColor) const;

Expand Down

0 comments on commit a9b91b6

Please sign in to comment.