diff --git a/Source/Editor/GUI/Dialogs/ColorPickerDialog.cs b/Source/Editor/GUI/Dialogs/ColorPickerDialog.cs index daf5cfa05..ab3a3fa17 100644 --- a/Source/Editor/GUI/Dialogs/ColorPickerDialog.cs +++ b/Source/Editor/GUI/Dialogs/ColorPickerDialog.cs @@ -212,7 +212,7 @@ namespace FlaxEditor.GUI.Dialogs private void OnEyedropColor() { - throw new NotImplementedException(); + Editor.Log("Color: " + Screenshot.GetPixelAt(7, 6).ToString()); } private void OnRGBAChanged() diff --git a/Source/Engine/Utilities/Screenshot.cpp b/Source/Engine/Utilities/Screenshot.cpp index d67f8d123..553801e5b 100644 --- a/Source/Engine/Utilities/Screenshot.cpp +++ b/Source/Engine/Utilities/Screenshot.cpp @@ -3,6 +3,7 @@ #include "Screenshot.h" #include "Engine/Core/Log.h" #include "Engine/Core/Math/Math.h" +#include "Engine/Core/Math/Color32.h" #include "Engine/Graphics/RenderTask.h" #include "Engine/Platform/FileSystem.h" #include "Engine/Graphics/Textures/TextureData.h" @@ -129,6 +130,66 @@ void CaptureScreenshot::OnFail() ThreadPoolTask::OnFail(); } + + +/// +/// Capture screenshot helper +/// +/// +class GetPixelData : public ThreadPoolTask +{ + friend Screenshot; +private: + TextureData _data; + Color32 _color; + +public: + int32 x; + int32 y; + +public: + /// + /// Gets the texture data container. + /// + /// Texture data + FORCE_INLINE TextureData& GetData() + { + return _data; + } + + FORCE_INLINE Color32 GetColor() + { + return _color; + } + +protected: + // [ThreadPoolTask] + bool Run() override; + void OnFail() override; +}; + +bool GetPixelData::Run() +{ + LOG(Warning, "REAL"); + TextureMipData *mipData = _data.GetData(0, 0); + Array pixels; + mipData->GetPixels(pixels, _data.Width, _data.Height, _data.Format); + + _color = pixels[(y * _data.Width) + x]; + LOG(Warning, "really real"); + LOG(Warning, "Color: {0} {1} {2}", _color.R, _color.B, _color.G); + return false; +} + +void GetPixelData::OnFail() +{ + LOG(Warning, "Cannot get pixel data."); + + // Base + ThreadPoolTask::OnFail(); +} + + void Screenshot::Capture(GPUTexture* target, const StringView& path) { // Validate @@ -242,3 +303,18 @@ void Screenshot::Capture(const StringView& path) Capture(mainTask, path); } + +Color32 Screenshot::GetPixelAt(int32 x, int32 y) { + GPUSwapChain* swapChain = Engine::MainWindow->GetSwapChain(); + + auto getPixelTask = New(); + getPixelTask->x = x; + getPixelTask->y = y; + + Task* downloadTask = swapChain->DownloadDataAsync(getPixelTask->GetData()); + downloadTask->ContinueWith(getPixelTask); + downloadTask->Start(); + + //downloadTask->Wait(750); + return getPixelTask->GetColor(); +} diff --git a/Source/Engine/Utilities/Screenshot.h b/Source/Engine/Utilities/Screenshot.h index c51c34199..e2b95e748 100644 --- a/Source/Engine/Utilities/Screenshot.h +++ b/Source/Engine/Utilities/Screenshot.h @@ -39,4 +39,12 @@ API_CLASS(Static) class FLAXENGINE_API Screenshot /// /// The custom file location. Use null or empty to use default one. API_FUNCTION() static void Capture(const StringView& path = StringView::Empty); + + /// + /// Get the pixel at specified coordinates. + /// + /// The x coordinate to read. + /// The y coordinate to read. + /// The color + API_FUNCTION() static Color32 GetPixelAt(int32 x, int32 y); };