Correctly implement the ScreenUtils class

This commit is contained in:
Menotdan
2023-05-10 13:34:28 -04:00
parent 1855ff133e
commit 2ce727d994
6 changed files with 73 additions and 20 deletions

View File

@@ -112,9 +112,6 @@ namespace FlaxEditor.GUI.Dialogs
_onChanged = colorChanged;
_onClosed = pickerClosed;
// Register the event for eyedropper pixels being read.
Screenshot.PixelReadDelegate += PixelDataRead;
// Selector
_cSelector = new ColorSelectorWithSliders(180, 18)
{
@@ -204,7 +201,7 @@ namespace FlaxEditor.GUI.Dialogs
{
Parent = this,
};
_cEyedropper.Clicked += OnEyedropColor;
_cEyedropper.Clicked += OnEyedropStart;
_cEyedropper.Height = (_cValue.Bottom - _cEyedropper.Y) * 0.5f;
_cEyedropper.Width = _cEyedropper.Height;
_cEyedropper.X -= _cEyedropper.Width;
@@ -214,18 +211,21 @@ namespace FlaxEditor.GUI.Dialogs
SelectedColor = initialValue;
}
private void PixelDataRead(Color32 pixel_color)
private Color32 GetEyedropColor()
{
Color color = pixel_color;
Editor.Log(string.Format("Color: {0} {1} {2}", color.R, color.G, color.B));
Int2 mousePosition = ScreenUtils.GetScreenCursorPosition();
Color32 pixelColor = ScreenUtils.GetPixelAt(mousePosition.X, mousePosition.Y);
return pixelColor;
}
private void OnEyedropColor()
private void OnEyedropStart()
{
Float2 mousePosition = FlaxEngine.Input.MouseScreenPosition;
Screenshot.GetPixelAt(Mathf.FloorToInt(mousePosition.X), Mathf.FloorToInt(mousePosition.Y));
Color32 pixelColor = GetEyedropColor();
Editor.Log(string.Format("Pixel Color: ({0}, {1}, {2})", pixelColor.R, pixelColor.G, pixelColor.B));
}
private void OnRGBAChanged()
{
if (_disableEvents)
@@ -251,6 +251,14 @@ namespace FlaxEditor.GUI.Dialogs
SelectedColor = color;
}
/// <inheritdoc />
public override void Update(float deltaTime)
{
base.Update(deltaTime);
}
/// <inheritdoc />
public override void Draw()
{

View File

@@ -0,0 +1,11 @@
#include "ScreenUtilsBase.h"
#include "Engine/Core/Math/Color32.h"
#include "Engine/Core/Math/Vector2.h"
Color32 ScreenUtilsBase::GetPixelAt(int32 x, int32 y) {
return Color32::Black;
}
Int2 ScreenUtilsBase::GetScreenCursorPosition() {
return { 0, 0 };
}

View File

@@ -0,0 +1,29 @@
#pragma once
#include "Engine/Core/Types/BaseTypes.h"
API_INJECT_CODE(cpp, "#include \"Engine/Platform/ScreenUtils.h\"");
/// <summary>
/// Platform-dependent screen utilties.
/// </summary>
API_CLASS(Static, Name = "ScreenUtils", Tag = "NativeInvokeUseName")
class FLAXENGINE_API ScreenUtilsBase
{
public:
static struct FLAXENGINE_API ScriptingTypeInitializer TypeInitializer;
/// <summary>
/// Gets the pixel color at the specified coordinates.
/// </summary>
/// <param name="x">X Coordinate to read.</param>
/// <param name="y">Y Coordinate to read.</param>
/// <returns>Pixel color at the specified coordinates.</returns>
API_FUNCTION() static Color32 GetPixelAt(int32 x, int32 y);
/// <summary>
/// Gets the cursor position, in screen cooridnates.
/// </summary>
/// <returns>Cursor position, in screen coordinates.</returns>
API_FUNCTION() static Int2 GetScreenCursorPosition();
};

View File

@@ -9,9 +9,3 @@
#endif
#include "Types.h"
#include "Engine/Scripting/ScriptingType.h"
API_CLASS(Static) class FLAXENGINE_API ScreenUtils
{
DECLARE_SCRIPTING_TYPE_NO_SPAWN(ScreenUtils);
};

View File

@@ -4,7 +4,11 @@
#include <Windows.h>
Color32 PlatformScreenUtils::GetPixelAt(int32 x, int32 y) {
#if PLATFORM_WINDOWS
#pragma comment(lib, "Gdi32.lib")
#endif
Color32 ScreenUtils::GetPixelAt(int32 x, int32 y) {
HDC deviceContext = GetDC(NULL);
COLORREF color = GetPixel(deviceContext, x, y);
ReleaseDC(NULL, deviceContext);
@@ -13,7 +17,7 @@ Color32 PlatformScreenUtils::GetPixelAt(int32 x, int32 y) {
return returnColor;
}
Int2 PlatformScreenUtils::GetScreenCursorPosition() {
Int2 ScreenUtils::GetScreenCursorPosition() {
POINT cursorPos;
GetCursorPos(&cursorPos);

View File

@@ -1,9 +1,16 @@
#pragma once
#include "Engine/Core/Math/Color32.h"
class PlatformScreenUtils {
#if PLATFORM_WINDOWS
#include "Engine/Core/Math/Color32.h"
#include "Engine/Platform/Base/ScreenUtilsBase.h"
class FLAXENGINE_API ScreenUtils : public ScreenUtilsBase {
public:
// [ScreenUtilsBase]
static Color32 GetPixelAt(int32 x, int32 y);
static Int2 GetScreenCursorPosition();
};
#endif