Tweak a few classes, add the Base classes.

This commit is contained in:
Menotdan
2023-05-10 12:46:54 -04:00
parent 51004bbd9d
commit 1855ff133e
8 changed files with 45 additions and 91 deletions

View File

View File

@@ -0,0 +1,17 @@
#if PLATFORM_WINDOWS
#include "Windows/WindowsScreenUtils.h"
#elif PLATFORM_LINUX
#include "Linux/LinuxScreenUtils.h"
#elif PLATFORM_MAC
#include "Mac/MacScreenUtils.h"
#else
#error No Screen Utils For This Platform! Please ensure you are not targetting an Editor build.
#endif
#include "Types.h"
#include "Engine/Scripting/ScriptingType.h"
API_CLASS(Static) class FLAXENGINE_API ScreenUtils
{
DECLARE_SCRIPTING_TYPE_NO_SPAWN(ScreenUtils);
};

View File

@@ -0,0 +1,22 @@
#include "WindowsScreenUtils.h"
#include "Engine/Core/Math/Color32.h"
#include "Engine/Core/Math/Vector2.h"
#include <Windows.h>
Color32 PlatformScreenUtils::GetPixelAt(int32 x, int32 y) {
HDC deviceContext = GetDC(NULL);
COLORREF color = GetPixel(deviceContext, x, y);
ReleaseDC(NULL, deviceContext);
Color32 returnColor = { GetRValue(color), GetGValue(color), GetBValue(color), 255 };
return returnColor;
}
Int2 PlatformScreenUtils::GetScreenCursorPosition() {
POINT cursorPos;
GetCursorPos(&cursorPos);
Int2 returnCursorPos = { cursorPos.x, cursorPos.y };
return returnCursorPos;
}

View File

@@ -1,5 +1,9 @@
#pragma once
class WindowsScreenUtils
{
#include "Engine/Core/Math/Color32.h"
class PlatformScreenUtils {
public:
static Color32 GetPixelAt(int32 x, int32 y);
static Int2 GetScreenCursorPosition();
};

View File

@@ -130,70 +130,6 @@ void CaptureScreenshot::OnFail()
ThreadPoolTask::OnFail();
}
Delegate<Color32> Screenshot::PixelReadDelegate;
/// <summary>
/// Capture screenshot helper
/// </summary>
/// <seealso cref="ThreadPoolTask" />
class GetPixelData : public ThreadPoolTask
{
friend Screenshot;
private:
TextureData _data;
Color32 _color;
public:
int32 x;
int32 y;
public:
/// <summary>
/// Gets the texture data container.
/// </summary>
/// <returns>Texture data</returns>
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<Color32> pixels;
mipData->GetPixels(pixels, _data.Width, _data.Height, _data.Format);
LOG(Warning, "{0}, {1} ({2} at {3})", x, y, pixels.Count(), (y * _data.Width) + x);
_color = pixels[(y * _data.Width) + x];
LOG(Warning, "really real");
LOG(Warning, "Color: R: {0}, G: {1}, B: {2}", _color.R, _color.G, _color.B);
LOG(Warning, "Bound functions: {0}", Screenshot::PixelReadDelegate.Count());
Screenshot::PixelReadDelegate(_color);
return false;
}
void GetPixelData::OnFail()
{
LOG(Warning, "Cannot get pixel data.");
// Base
ThreadPoolTask::OnFail();
}
void Screenshot::Capture(GPUTexture* target, const StringView& path)
{
// Validate
@@ -307,18 +243,3 @@ void Screenshot::Capture(const StringView& path)
Capture(mainTask, path);
}
Color32 Screenshot::GetPixelAt(int32 x, int32 y) {
GPUSwapChain* swapChain = Engine::MainWindow->GetSwapChain();
auto getPixelTask = New<GetPixelData>();
getPixelTask->x = x;
getPixelTask->y = y;
Task* downloadTask = swapChain->DownloadDataAsync(getPixelTask->GetData());
downloadTask->ContinueWith(getPixelTask);
LOG(Warning, "Started download task. real");
downloadTask->Start();
return getPixelTask->GetColor();
}

View File

@@ -39,14 +39,4 @@ API_CLASS(Static) class FLAXENGINE_API Screenshot
/// </summary>
/// <param name="path">The custom file location. Use null or empty to use default one.</param>
API_FUNCTION() static void Capture(const StringView& path = StringView::Empty);
/// <summary>
/// Get the pixel at specified coordinates.
/// </summary>
/// <param name="x">The x coordinate to read.</param>
/// <param name="y">The y coordinate to read.</param>
/// <returns>The color </returns>
API_FUNCTION() static Color32 GetPixelAt(int32 x, int32 y);
API_EVENT() static Delegate<Color32> PixelReadDelegate;
};