Initial commit

This commit is contained in:
alexpete
2021-03-05 11:26:34 -08:00
commit a10351f38d
27091 changed files with 5521199 additions and 0 deletions
@@ -0,0 +1,115 @@
/*
* All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
* its licensors.
*
* For complete copyright and license terms please see the LICENSE at the root of this
* distribution (the "License"). All use of this software is governed by the License,
* or, if provided, by the license below or the license accompanying this file. Do not
* remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*
*/
#include "EditorDefs.h"
#include "ThumbnailsSampleWidget.h"
// Qt
#include <QLabel>
// AzToolsFramework
#include <AzToolsFramework/AssetBrowser/AssetBrowserModel.h>
#include <AzToolsFramework/AssetBrowser/AssetBrowserEntry.h>
#include <AzToolsFramework/Thumbnails/ThumbnailWidget.h>
#include <AzToolsFramework/AssetBrowser/Thumbnails/AssetBrowserProductThumbnail.h>
// Editor
#include "QtViewPaneManager.h" // for RegisterQtViewPane
AZ_PUSH_DISABLE_DLL_EXPORT_MEMBER_WARNING
#include <Thumbnails/Example/ui_ThumbnailsSampleWidget.h>
AZ_POP_DISABLE_DLL_EXPORT_MEMBER_WARNING
const int MAX_PRODUCTS_TO_DISPLAY = 20;
ThumbnailsSampleWidget::ThumbnailsSampleWidget(QWidget* parent)
: QWidget(parent)
, m_ui(new Ui::ThumbnailsSampleWidgetClass())
, m_filterModel(new AzToolsFramework::AssetBrowser::AssetBrowserFilterModel(parent))
{
m_ui->setupUi(this);
m_ui->m_searchWidget->Setup(true, true);
using namespace AzToolsFramework::AssetBrowser;
AssetBrowserComponentRequestBus::BroadcastResult(m_assetBrowserModel, &AssetBrowserComponentRequests::GetAssetBrowserModel);
AZ_Assert(m_assetBrowserModel, "Failed to get filebrowser model");
m_filterModel->setSourceModel(m_assetBrowserModel);
m_filterModel->SetFilter(m_ui->m_searchWidget->GetFilter());
m_ui->m_assetBrowserTreeViewWidget->setModel(m_filterModel.data());
auto layout = static_cast<QVBoxLayout*>(m_ui->m_thumbnailScrollAreaRoot->layout());
layout->addStretch(1);
connect(m_ui->m_assetBrowserTreeViewWidget, &AssetBrowserTreeView::selectionChangedSignal,
this, &ThumbnailsSampleWidget::SelectionChangedSlot);
connect(m_ui->m_searchWidget->GetFilter().data(), &AzToolsFramework::AssetBrowser::AssetBrowserEntryFilter::updatedSignal,
m_filterModel.data(), &AzToolsFramework::AssetBrowser::AssetBrowserFilterModel::filterUpdatedSlot);
}
ThumbnailsSampleWidget::~ThumbnailsSampleWidget() = default;
void ThumbnailsSampleWidget::RegisterViewClass()
{
QtViewOptions options;
options.preferedDockingArea = Qt::NoDockWidgetArea;
options.canHaveMultipleInstances = true;
RegisterQtViewPane<ThumbnailsSampleWidget>(GetIEditor(), "Thumbnails Demo", LyViewPane::CategoryTools, options);
}
void ThumbnailsSampleWidget::SelectionChangedSlot(const QItemSelection& /*selected*/, const QItemSelection& /*deselected*/) const
{
UpdateThumbnail();
}
void ThumbnailsSampleWidget::UpdateThumbnail() const
{
auto layout = static_cast<QVBoxLayout*>(m_ui->m_thumbnailScrollAreaRoot->layout());
// delete any previous thumbnails
qDeleteAll(m_ui->m_thumbnailScrollAreaRoot->findChildren<QWidget*>("", Qt::FindDirectChildrenOnly));
auto selectedAssets = m_ui->m_assetBrowserTreeViewWidget->GetSelectedAssets();
if (selectedAssets.size() > 0)
{
using namespace AzToolsFramework::AssetBrowser;
//get all products from selected entry (it can be a folder, source or product asset and can contain 0 or more products)
AZStd::vector<const ProductAssetBrowserEntry*> products;
selectedAssets.front()->GetChildrenRecursively<ProductAssetBrowserEntry>(products);
// because thumbnails are displayed via individual widgets, limit to 10 otherwise it can take ages
int productsLeft = MAX_PRODUCTS_TO_DISPLAY;
for (const auto* product : products)
{
// create thumbnail widget
auto thumbnailWidget = new AzToolsFramework::Thumbnailer::ThumbnailWidget(m_ui->m_thumbnailScrollArea);
thumbnailWidget->SetThumbnailKey(MAKE_TKEY(AzToolsFramework::AssetBrowser::ProductThumbnailKey, product->GetAssetId()));
thumbnailWidget->setMinimumSize(100, 100);
thumbnailWidget->setMaximumSize(100, 100);
// insert it before space to align on top
layout->insertWidget(layout->count() - 1, thumbnailWidget);
// add label indicating name of the asset
auto label = new QLabel(product->GetName().c_str(), m_ui->m_thumbnailScrollArea);
layout->insertWidget(layout->count() - 1, label);
// do not render more than 10 thumbnails at a time
productsLeft--;
if (productsLeft <= 0)
{
break;
}
}
}
return;
}
#include <Thumbnails/Example/moc_ThumbnailsSampleWidget.cpp>
@@ -0,0 +1,63 @@
/*
* All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
* its licensors.
*
* For complete copyright and license terms please see the LICENSE at the root of this
* distribution (the "License"). All use of this software is governed by the License,
* or, if provided, by the license below or the license accompanying this file. Do not
* remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*
*/
#pragma once
#if !defined(Q_MOC_RUN)
#include <Editor/Util/FileUtil.h>
#include <Editor/Util/Image.h>
#include <AzCore/Memory/SystemAllocator.h>
#include <AzToolsFramework/Thumbnails/Thumbnail.h>
#include <QWidget>
#include <QScopedPointer>
#endif
class QItemSelection;
namespace Ui
{
class ThumbnailsSampleWidgetClass;
}
namespace AzToolsFramework
{
namespace AssetBrowser
{
class ProductAssetBrowserEntry;
class AssetBrowserEntry;
class AssetBrowserFilterModel;
class AssetBrowserModel;
}
}
class ThumbnailsSampleWidget
: public QWidget
{
Q_OBJECT
public:
AZ_CLASS_ALLOCATOR(ThumbnailsSampleWidget, AZ::SystemAllocator, 0);
explicit ThumbnailsSampleWidget(QWidget* parent = nullptr);
~ThumbnailsSampleWidget() override;
static void RegisterViewClass();
private:
QScopedPointer<Ui::ThumbnailsSampleWidgetClass> m_ui;
QScopedPointer<AzToolsFramework::AssetBrowser::AssetBrowserFilterModel> m_filterModel;
AzToolsFramework::AssetBrowser::AssetBrowserModel* m_assetBrowserModel;
void UpdateThumbnail() const;
private Q_SLOTS:
void SelectionChangedSlot(const QItemSelection& selected, const QItemSelection& deselected) const;
};
@@ -0,0 +1,101 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>ThumbnailsSampleWidgetClass</class>
<widget class="QWidget" name="ThumbnailsSampleWidgetClass">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>567</width>
<height>513</height>
</rect>
</property>
<property name="windowTitle">
<string>Thumbnails Sample</string>
</property>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
<widget class="AzToolsFramework::AssetBrowser::SearchWidget" name="m_searchWidget" native="true">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
</widget>
</item>
<item>
<widget class="AzToolsFramework::AssetBrowser::AssetBrowserTreeView" name="m_assetBrowserTreeViewWidget">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Expanding">
<horstretch>1</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="dragDropMode">
<enum>QAbstractItemView::DragOnly</enum>
</property>
</widget>
</item>
</layout>
</item>
<item>
<widget class="Line" name="line">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
</widget>
</item>
<item>
<widget class="QScrollArea" name="m_thumbnailScrollArea">
<property name="minimumSize">
<size>
<width>200</width>
<height>0</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>200</width>
<height>16777215</height>
</size>
</property>
<property name="verticalScrollBarPolicy">
<enum>Qt::ScrollBarAlwaysOn</enum>
</property>
<property name="widgetResizable">
<bool>true</bool>
</property>
<widget class="QWidget" name="m_thumbnailScrollAreaRoot">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>181</width>
<height>493</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout"/>
</widget>
</widget>
</item>
</layout>
</widget>
<customwidgets>
<customwidget>
<class>AzToolsFramework::AssetBrowser::AssetBrowserTreeView</class>
<extends>QTreeView</extends>
<header>AzToolsFramework/AssetBrowser/Views/AssetBrowserTreeView.h</header>
</customwidget>
<customwidget>
<class>AzToolsFramework::AssetBrowser::SearchWidget</class>
<extends>QWidget</extends>
<header>AzToolsFramework/AssetBrowser/Search/SearchWidget.h</header>
<container>1</container>
</customwidget>
</customwidgets>
<resources/>
<connections/>
</ui>
@@ -0,0 +1,128 @@
/*
* All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
* its licensors.
*
* For complete copyright and license terms please see the LICENSE at the root of this
* distribution (the "License"). All use of this software is governed by the License,
* or, if provided, by the license below or the license accompanying this file. Do not
* remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*
*/
#include "EditorDefs.h"
#include "MaterialThumbnailRenderer.h"
// AzCore
#include <AzCore/Asset/AssetManagerBus.h>
#include <AzCore/Asset/AssetTypeInfoBus.h>
// AzToolsFramework
#include <AzToolsFramework/AssetBrowser/EBusFindAssetTypeByName.h>
// CryCommon
#include <CryCommon/IStreamEngine.h>
// Editor
#include "Util/Image.h"
#include "Controls/PreviewModelCtrl.h"
#include "Material/MaterialManager.h"
const char* MATERIAL_PREVIEW_MODEL_FILE = "Editor/Objects/MtlSphere.cgf";
MaterialThumbnailRenderer::MaterialThumbnailRenderer()
{
m_previewControl = AZStd::make_unique<CPreviewModelCtrl>();
m_previewControl->SetGrid(false);
m_previewControl->SetAxis(false);
m_previewControl->SetClearColor(ColorF(0, 0, 0, 0));
EBusFindAssetTypeByName result("Material");
AZ::AssetTypeInfoBus::BroadcastResult(result, &AZ::AssetTypeInfo::GetAssetType);
m_assetType = result.GetAssetType();
AzToolsFramework::Thumbnailer::ThumbnailerRendererRequestBus::Handler::BusConnect(m_assetType);
AZ::SystemTickBus::Handler::BusConnect();
}
MaterialThumbnailRenderer::~MaterialThumbnailRenderer()
{
AzToolsFramework::Thumbnailer::ThumbnailerRendererRequestBus::Handler::BusDisconnect();
AZ::SystemTickBus::Handler::BusDisconnect();
}
void MaterialThumbnailRenderer::OnSystemTick()
{
AzToolsFramework::Thumbnailer::ThumbnailerRendererRequestBus::ExecuteQueuedEvents();
}
void MaterialThumbnailRenderer::RenderThumbnail(AZ::Data::AssetId assetId, int thumbnailSize)
{
m_previewControl->setFixedSize(thumbnailSize, thumbnailSize);
// get asset type name
AZ::Data::AssetInfo info;
AZ::Data::AssetCatalogRequestBus::BroadcastResult(info, &AZ::Data::AssetCatalogRequests::GetAssetInfoById, assetId);
AZ::Data::AssetType assetType = info.m_assetType;
QString assetTypeName;
AZ::AssetTypeInfoBus::EventResult(assetTypeName, assetType, &AZ::AssetTypeInfo::GetAssetTypeDisplayName);
QPixmap thumbnail;
if (Render(thumbnail, assetId, thumbnailSize))
{
AzToolsFramework::Thumbnailer::ThumbnailerRendererNotificationBus::Event(assetId,
&AzToolsFramework::Thumbnailer::ThumbnailerRendererNotifications::ThumbnailRendered, thumbnail);
}
else
{
AzToolsFramework::Thumbnailer::ThumbnailerRendererNotificationBus::Event(assetId,
&AzToolsFramework::Thumbnailer::ThumbnailerRendererNotifications::ThumbnailFailedToRender);
}
}
bool MaterialThumbnailRenderer::Installed() const
{
return true;
}
bool MaterialThumbnailRenderer::Render(QPixmap& thumbnail, AZ::Data::AssetId assetId, int thumbnailSize) const
{
// get filepath
AZStd::string path;
AZ::Data::AssetCatalogRequestBus::BroadcastResult(
path,
&AZ::Data::AssetCatalogRequests::GetAssetPathById,
assetId);
auto material = GetIEditor()->GetMaterialManager()->LoadMaterial(path.c_str(), false);
m_previewControl->LoadFile(MATERIAL_PREVIEW_MODEL_FILE);
m_previewControl->SetMaterial(material);
m_previewControl->FitToScreen();
gEnv->p3DEngine->Update();
gEnv->pSystem->GetStreamEngine()->Update();
m_previewControl->Update(true);
m_previewControl->repaint();
CImageEx img;
m_previewControl->show();
// ensure all the initial (might be first time show) event handling is done for m_previewControl
QCoreApplication::sendPostedEvents(m_previewControl.get());
m_previewControl->GetImageOffscreen(img, QSize(thumbnailSize, thumbnailSize));
m_previewControl->hide();
if (img.IsValid())
{
// this can fail if the request to draw the thumbnail was queued up but then the window
// was hidden or deleted in the interim.
thumbnail = QPixmap::fromImage(QImage(reinterpret_cast<uchar*>(img.GetData()),
img.GetWidth(), img.GetHeight(), QImage::Format_ARGB32)).copy();
img.Release();
return true;
}
return false;
}
@@ -0,0 +1,50 @@
/*
* All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
* its licensors.
*
* For complete copyright and license terms please see the LICENSE at the root of this
* distribution (the "License"). All use of this software is governed by the License,
* or, if provided, by the license below or the license accompanying this file. Do not
* remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*
*/
#pragma once
#include <AzCore/Memory/SystemAllocator.h>
#include <AzCore/std/smart_ptr/unique_ptr.h>
#include <AzCore/Asset/AssetCommon.h>
#include <AzCore/Component/TickBus.h>
#include <AzToolsFramework/Thumbnails/ThumbnailerBus.h>
class CPreviewModelCtrl;
//! Loads thumbnails that require acccess to renderer
class MaterialThumbnailRenderer
: public AzToolsFramework::Thumbnailer::ThumbnailerRendererRequestBus::Handler
, public AZ::SystemTickBus::Handler
{
public:
AZ_CLASS_ALLOCATOR(MaterialThumbnailRenderer, AZ::SystemAllocator, 0)
MaterialThumbnailRenderer();
~MaterialThumbnailRenderer();
//////////////////////////////////////////////////////////////////////////
// TickBus
//////////////////////////////////////////////////////////////////////////
void OnSystemTick() override;
//////////////////////////////////////////////////////////////////////////
// ThumbnailerRendererRequests
//////////////////////////////////////////////////////////////////////////
void RenderThumbnail(AZ::Data::AssetId assetId, int thumbnailSize) override;
bool Installed() const override;
private:
AZ::Data::AssetType m_assetType;
AZStd::unique_ptr<CPreviewModelCtrl> m_previewControl;
bool Render(QPixmap& thumbnail, AZ::Data::AssetId assetId, int thumbnailSize) const;
};
@@ -0,0 +1,122 @@
/*
* All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
* its licensors.
*
* For complete copyright and license terms please see the LICENSE at the root of this
* distribution (the "License"). All use of this software is governed by the License,
* or, if provided, by the license below or the license accompanying this file. Do not
* remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*
*/
#include "EditorDefs.h"
#include "StaticMeshThumbnailRenderer.h"
// AzCore
#include <AzCore/Asset/AssetManagerBus.h>
// AzToolsFramework
#include <AzToolsFramework/AssetBrowser/EBusFindAssetTypeByName.h>
// CryCommon
#include <CryCommon/IStreamEngine.h>
// Editor
#include "Controls/PreviewModelCtrl.h"
#include "Util/Image.h"
StaticMeshThumbnailRenderer::StaticMeshThumbnailRenderer()
{
m_previewControl = AZStd::make_unique<CPreviewModelCtrl>();
m_previewControl->SetGrid(false);
m_previewControl->SetAxis(false);
m_previewControl->SetClearColor(ColorF(0, 0, 0, 0));
EBusFindAssetTypeByName result("Static Mesh");
AZ::AssetTypeInfoBus::BroadcastResult(result, &AZ::AssetTypeInfo::GetAssetType);
m_assetType = result.GetAssetType();
AzToolsFramework::Thumbnailer::ThumbnailerRendererRequestBus::Handler::BusConnect(m_assetType);
AZ::SystemTickBus::Handler::BusConnect();
}
StaticMeshThumbnailRenderer::~StaticMeshThumbnailRenderer()
{
AzToolsFramework::Thumbnailer::ThumbnailerRendererRequestBus::Handler::BusDisconnect();
AZ::SystemTickBus::Handler::BusDisconnect();
}
void StaticMeshThumbnailRenderer::OnSystemTick()
{
AzToolsFramework::Thumbnailer::ThumbnailerRendererRequestBus::ExecuteQueuedEvents();
}
void StaticMeshThumbnailRenderer::RenderThumbnail(AZ::Data::AssetId assetId, int thumbnailSize)
{
m_previewControl->setFixedSize(thumbnailSize, thumbnailSize);
// get asset type name
AZ::Data::AssetInfo info;
AZ::Data::AssetCatalogRequestBus::BroadcastResult(info, &AZ::Data::AssetCatalogRequests::GetAssetInfoById, assetId);
AZ::Data::AssetType assetType = info.m_assetType;
QString assetTypeName;
AZ::AssetTypeInfoBus::EventResult(assetTypeName, assetType, &AZ::AssetTypeInfo::GetAssetTypeDisplayName);
QPixmap thumbnail;
if (Render(thumbnail, assetId, thumbnailSize))
{
AzToolsFramework::Thumbnailer::ThumbnailerRendererNotificationBus::Event(assetId,
&AzToolsFramework::Thumbnailer::ThumbnailerRendererNotifications::ThumbnailRendered, thumbnail);
}
else
{
AzToolsFramework::Thumbnailer::ThumbnailerRendererNotificationBus::Event(assetId,
&AzToolsFramework::Thumbnailer::ThumbnailerRendererNotifications::ThumbnailFailedToRender);
}
}
bool StaticMeshThumbnailRenderer::Installed() const
{
return true;
}
bool StaticMeshThumbnailRenderer::Render(QPixmap& thumbnail, AZ::Data::AssetId assetId, int thumbnailSize) const
{
// get filepath
AZStd::string path;
AZ::Data::AssetCatalogRequestBus::BroadcastResult(
path,
&AZ::Data::AssetCatalogRequests::GetAssetPathById,
assetId);
m_previewControl->SetMaterial(nullptr);
m_previewControl->LoadFile(path.c_str());
m_previewControl->FitToScreen();
gEnv->p3DEngine->Update();
gEnv->pSystem->GetStreamEngine()->Update();
m_previewControl->Update(true);
m_previewControl->repaint();
CImageEx img;
// getimageoffscreen actually requires a real operating system window handle resource, which hiding the window can cause
// to be lost.
m_previewControl->GetImageOffscreen(img, QSize(thumbnailSize, thumbnailSize));
m_previewControl->hide();
if (img.IsValid())
{
// this can fail if the request to draw the thumbnail was queued up but then the window
// was hidden or deleted in the interim.
thumbnail = QPixmap::fromImage(QImage(reinterpret_cast<uchar*>(img.GetData()),
img.GetWidth(), img.GetHeight(), QImage::Format_ARGB32)).copy();
img.Release();
return true;
}
return false;
}
@@ -0,0 +1,50 @@
/*
* All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
* its licensors.
*
* For complete copyright and license terms please see the LICENSE at the root of this
* distribution (the "License"). All use of this software is governed by the License,
* or, if provided, by the license below or the license accompanying this file. Do not
* remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*
*/
#pragma once
#include <AzCore/Memory/SystemAllocator.h>
#include <AzCore/std/smart_ptr/unique_ptr.h>
#include <AzCore/Asset/AssetCommon.h>
#include <AzCore/Component/TickBus.h>
#include <AzToolsFramework/Thumbnails/ThumbnailerBus.h>
class CPreviewModelCtrl;
//! Loads thumbnails that require acccess to renderer
class StaticMeshThumbnailRenderer
: public AzToolsFramework::Thumbnailer::ThumbnailerRendererRequestBus::Handler
, public AZ::SystemTickBus::Handler
{
public:
AZ_CLASS_ALLOCATOR(StaticMeshThumbnailRenderer, AZ::SystemAllocator, 0)
StaticMeshThumbnailRenderer();
~StaticMeshThumbnailRenderer();
//////////////////////////////////////////////////////////////////////////
// TickBus
//////////////////////////////////////////////////////////////////////////
void OnSystemTick() override;
//////////////////////////////////////////////////////////////////////////
// ThumbnailerRendererRequests
//////////////////////////////////////////////////////////////////////////
void RenderThumbnail(AZ::Data::AssetId assetId, int thumbnailSize) override;
bool Installed() const override;
private:
AZ::Data::AssetType m_assetType;
AZStd::unique_ptr<CPreviewModelCtrl> m_previewControl;
bool Render(QPixmap& thumbnail, AZ::Data::AssetId assetId, int thumbnailSize) const;
};
@@ -0,0 +1,103 @@
/*
* All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
* its licensors.
*
* For complete copyright and license terms please see the LICENSE at the root of this
* distribution (the "License"). All use of this software is governed by the License,
* or, if provided, by the license below or the license accompanying this file. Do not
* remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*
*/
#include "EditorDefs.h"
#include "TextureThumbnailRenderer.h"
// AzCore
#include <AzCore/Asset/AssetManagerBus.h>
// AzToolsFramework
#include <AzToolsFramework/AssetBrowser/EBusFindAssetTypeByName.h>
// Editor
#include "Util/Image.h"
#include "Util/ImageUtil.h"
TextureThumbnailRenderer::TextureThumbnailRenderer()
{
EBusFindAssetTypeByName result("Texture");
AZ::AssetTypeInfoBus::BroadcastResult(result, &AZ::AssetTypeInfo::GetAssetType);
m_assetType = result.GetAssetType();
AzToolsFramework::Thumbnailer::ThumbnailerRendererRequestBus::Handler::BusConnect(m_assetType);
AZ::SystemTickBus::Handler::BusConnect();
}
TextureThumbnailRenderer::~TextureThumbnailRenderer()
{
AzToolsFramework::Thumbnailer::ThumbnailerRendererRequestBus::Handler::BusDisconnect();
AZ::SystemTickBus::Handler::BusDisconnect();
}
void TextureThumbnailRenderer::OnSystemTick()
{
AzToolsFramework::Thumbnailer::ThumbnailerRendererRequestBus::ExecuteQueuedEvents();
}
void TextureThumbnailRenderer::RenderThumbnail(AZ::Data::AssetId assetId, int thumbnailSize)
{
QPixmap thumbnail;
if (Render(thumbnail, assetId, thumbnailSize))
{
AzToolsFramework::Thumbnailer::ThumbnailerRendererNotificationBus::Event(assetId,
&AzToolsFramework::Thumbnailer::ThumbnailerRendererNotifications::ThumbnailRendered, thumbnail);
}
else
{
AzToolsFramework::Thumbnailer::ThumbnailerRendererNotificationBus::Event(assetId,
&AzToolsFramework::Thumbnailer::ThumbnailerRendererNotifications::ThumbnailFailedToRender);
}
}
bool TextureThumbnailRenderer::Installed() const
{
return true;
}
bool TextureThumbnailRenderer::Render(QPixmap& thumbnail, AZ::Data::AssetId assetId, int thumbnailSize) const
{
// get filepath
AZStd::string path;
AZ::Data::AssetCatalogRequestBus::BroadcastResult(
path,
&AZ::Data::AssetCatalogRequests::GetAssetPathById,
assetId);
CImageEx img;
if (!CImageUtil::LoadImage(path.c_str(), img))
{
return false;
}
unsigned int* data = img.GetData();
if (!data)
{
img.Release();
return false;
}
if (0 == img.GetWidth() || 0 == img.GetHeight())
{
img.Release();
return false;
}
thumbnail = QPixmap::fromImage(QImage(reinterpret_cast<uchar*>(data), img.GetWidth(), img.GetHeight(), QImage::Format_ARGB32)
.scaled(thumbnailSize, thumbnailSize, Qt::IgnoreAspectRatio)).copy();
return true;
}
@@ -0,0 +1,46 @@
/*
* All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
* its licensors.
*
* For complete copyright and license terms please see the LICENSE at the root of this
* distribution (the "License"). All use of this software is governed by the License,
* or, if provided, by the license below or the license accompanying this file. Do not
* remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*
*/
#pragma once
#include <AzCore/Memory/SystemAllocator.h>
#include <AzCore/Asset/AssetCommon.h>
#include <AzCore/Component/TickBus.h>
#include <AzToolsFramework/Thumbnails/ThumbnailerBus.h>
//! Loads thumbnails that require acccess to renderer
class TextureThumbnailRenderer
: public AzToolsFramework::Thumbnailer::ThumbnailerRendererRequestBus::Handler
, public AZ::SystemTickBus::Handler
{
public:
AZ_CLASS_ALLOCATOR(TextureThumbnailRenderer, AZ::SystemAllocator, 0)
TextureThumbnailRenderer();
~TextureThumbnailRenderer();
//////////////////////////////////////////////////////////////////////////
// TickBus
//////////////////////////////////////////////////////////////////////////
void OnSystemTick() override;
//////////////////////////////////////////////////////////////////////////
// ThumbnailerRendererRequests
//////////////////////////////////////////////////////////////////////////
void RenderThumbnail(AZ::Data::AssetId assetId, int thumbnailSize) override;
bool Installed() const override;
private:
AZ::Data::AssetType m_assetType;
bool Render(QPixmap& thumbnail, AZ::Data::AssetId assetId, int thumbnailSize) const;
};
@@ -0,0 +1,165 @@
/*
* All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
* its licensors.
*
* For complete copyright and license terms please see the LICENSE at the root of this
* distribution (the "License"). All use of this software is governed by the License,
* or, if provided, by the license below or the license accompanying this file. Do not
* remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*
*/
#include "EditorDefs.h"
#include <AzCore/Asset/AssetTypeInfoBus.h>
#include <IStreamEngine.h>
#include <Editor/Thumbnails/ThumbnailRenderer.h>
#include <Editor/Controls/PreviewModelCtrl.h>
#include <Editor/Material/MaterialManager.h>
const char* MATERIAL_PREVIEW_MODEL_FILE = "Editor/Objects/MtlSphere.cgf";
ThumbnailRenderer::ThumbnailRenderer()
{
m_previewControl = std::make_unique<CPreviewModelCtrl>();
m_previewControl->SetGrid(false);
m_previewControl->SetAxis(false);
m_previewControl->SetClearColor(ColorF(0, 0, 0, 0));
AzToolsFramework::Thumbnailer::ThumbnailerRendererRequestsBus::Handler::BusConnect();
AZ::SystemTickBus::Handler::BusConnect();
}
ThumbnailRenderer::~ThumbnailRenderer()
{
AzToolsFramework::Thumbnailer::ThumbnailerRendererRequestsBus::Handler::BusDisconnect();
AZ::SystemTickBus::Handler::BusDisconnect();
}
void ThumbnailRenderer::OnSystemTick()
{
AzToolsFramework::Thumbnailer::ThumbnailerRendererRequestsBus::ExecuteQueuedEvents();
}
void ThumbnailRenderer::RenderThumbnail(AZ::Data::AssetId assetId, int thumbnailSize)
{
m_previewControl->setFixedSize(thumbnailSize, thumbnailSize);
// get asset type name
AZ::Data::AssetInfo info;
AZ::Data::AssetCatalogRequestBus::BroadcastResult(info, &AZ::Data::AssetCatalogRequests::GetAssetInfoById, assetId);
AZ::Data::AssetType assetType = info.m_assetType;
QString assetTypeName;
AZ::AssetTypeInfoBus::EventResult(assetTypeName, assetType, &AZ::AssetTypeInfo::GetAssetTypeDisplayName);
// get filepath
AZStd::string path;
AZ::Data::AssetCatalogRequestBus::BroadcastResult(
path,
&AZ::Data::AssetCatalogRequests::GetAssetPathById,
assetId);
bool success = false;
QPixmap thumbnail;
if (assetTypeName == "Static Mesh")
{
success = RenderMesh(thumbnail, path.c_str(), thumbnailSize);
}
else if (assetTypeName == "Material")
{
success = RenderMaterial(thumbnail, path.c_str(), thumbnailSize);
}
else if (assetTypeName == "Texture")
{
success = RenderTexture(thumbnail, path.c_str(), thumbnailSize);
}
if (success)
{
AzToolsFramework::Thumbnailer::ThumbnailerRendererNotificationsBus::Event(assetId,
&AzToolsFramework::Thumbnailer::ThumbnailerRendererNotifications::ThumbnailRendered, thumbnail);
}
else
{
AzToolsFramework::Thumbnailer::ThumbnailerRendererNotificationsBus::Event(assetId,
&AzToolsFramework::Thumbnailer::ThumbnailerRendererNotifications::ThumbnailFailedToRender);
}
}
bool ThumbnailRenderer::RenderMesh(QPixmap& thumbnail, const char* path, int thumbnailSize) const
{
m_previewControl->SetMaterial(nullptr);
m_previewControl->LoadFile(path);
m_previewControl->FitToScreen();
gEnv->p3DEngine->Update();
gEnv->pSystem->GetStreamEngine()->Update();
m_previewControl->Update(true);
m_previewControl->repaint();
m_previewControl->hide();
CImageEx img;
m_previewControl->GetImageOffscreen(img, QSize(thumbnailSize, thumbnailSize));
thumbnail = QPixmap::fromImage(QImage(reinterpret_cast<uchar*>(img.GetData()),
img.GetWidth(), img.GetHeight(), QImage::Format_ARGB32)).copy();
img.Release();
return true;
}
bool ThumbnailRenderer::RenderMaterial(QPixmap& thumbnail, const char* path, int thumbnailSize) const
{
auto material = GetIEditor()->GetMaterialManager()->LoadMaterial(path, false);
m_previewControl->LoadFile(MATERIAL_PREVIEW_MODEL_FILE);
m_previewControl->SetMaterial(material);
m_previewControl->FitToScreen();
gEnv->p3DEngine->Update();
gEnv->pSystem->GetStreamEngine()->Update();
m_previewControl->Update(true);
m_previewControl->repaint();
m_previewControl->hide();
CImageEx img;
m_previewControl->GetImageOffscreen(img, QSize(thumbnailSize, thumbnailSize));
thumbnail = QPixmap::fromImage(QImage(reinterpret_cast<uchar*>(img.GetData()),
img.GetWidth(), img.GetHeight(), QImage::Format_ARGB32)).copy();
img.Release();
return true;
}
bool ThumbnailRenderer::RenderTexture(QPixmap& thumbnail, const char* path, int thumbnailSize) const
{
CImageEx img;
if (!CImageUtil::LoadImage(path, img))
{
return false;
}
unsigned int* pData = img.GetData();
if (!pData)
{
img.Release();
return false;
}
if (0 == img.GetWidth() || 0 == img.GetHeight())
{
img.Release();
return false;
}
thumbnail = QPixmap::fromImage(QImage(reinterpret_cast<uchar*>(pData), img.GetWidth(), img.GetHeight(), QImage::Format_ARGB32)
.scaled(thumbnailSize, thumbnailSize, Qt::IgnoreAspectRatio)).copy();
return true;
}
@@ -0,0 +1,50 @@
/*
* All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
* its licensors.
*
* For complete copyright and license terms please see the LICENSE at the root of this
* distribution (the "License"). All use of this software is governed by the License,
* or, if provided, by the license below or the license accompanying this file. Do not
* remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*
*/
#pragma once
#include <AzCore/Memory/SystemAllocator.h>
#include <AzCore/std/smart_ptr/unique_ptr.h>
#include <AzCore/Asset/AssetCommon.h>
#include <AzCore/Component/TickBus.h>
#include <AzToolsFramework/Thumbnails/ThumbnailerBus.h>
class CPreviewModelCtrl;
//! Loads thumbnails that require acccess to renderer
class ThumbnailRenderer
: public AzToolsFramework::Thumbnailer::ThumbnailerRendererRequestsBus::Handler
, public AZ::SystemTickBus::Handler
{
public:
AZ_CLASS_ALLOCATOR(ThumbnailRenderer, AZ::SystemAllocator, 0)
ThumbnailRenderer();
~ThumbnailRenderer();
//////////////////////////////////////////////////////////////////////////
// TickBus
//////////////////////////////////////////////////////////////////////////
void OnSystemTick() override;
//////////////////////////////////////////////////////////////////////////
// ThumbnailerRendererRequests
//////////////////////////////////////////////////////////////////////////
void RenderThumbnail(AZ::Data::AssetId assetId, int thumbnailSize) override;
private:
AZStd::unique_ptr<CPreviewModelCtrl> m_previewControl;
bool RenderMesh(QPixmap& thumbnail, const char* path, int thumbnailSize) const;
bool RenderMaterial(QPixmap& thumbnail, const char* path, int thumbnailSize) const;
bool RenderTexture(QPixmap& thumbnail, const char* path, int thumbnailSize) const;
};