From a022b2231f0a8cc07fc5e55b85a95c243fd0f939 Mon Sep 17 00:00:00 2001 From: ivan100sic Date: Tue, 20 Oct 2020 15:49:05 +0200 Subject: [PATCH] Visuals are good, functionality - almost there --- .../fancyzones/lib/ZoneWindowDrawing.h | 71 ++++++++++++++++--- 1 file changed, 63 insertions(+), 8 deletions(-) diff --git a/src/modules/fancyzones/lib/ZoneWindowDrawing.h b/src/modules/fancyzones/lib/ZoneWindowDrawing.h index e3d837f5e5..c2eb99fca3 100644 --- a/src/modules/fancyzones/lib/ZoneWindowDrawing.h +++ b/src/modules/fancyzones/lib/ZoneWindowDrawing.h @@ -5,6 +5,7 @@ #include #include #include +#include #include "util.h" #include "Zone.h" @@ -39,6 +40,7 @@ class ZoneWindowDrawing D2D1_RECT_F rect; D2D1_COLOR_F borderColor; D2D1_COLOR_F fillColor; + size_t id; }; HWND m_window; @@ -48,7 +50,6 @@ class ZoneWindowDrawing std::mutex m_sceneMutex; std::vector m_sceneRects; - std::vector m_sceneLabels; void DrawBackdrop() { @@ -57,7 +58,7 @@ class ZoneWindowDrawing static ID2D1Factory* GetD2DFactory() { - static ID2D1Factory* pD2DFactory = 0; + static ID2D1Factory* pD2DFactory = nullptr; if (!pD2DFactory) { D2D1CreateFactory(D2D1_FACTORY_TYPE_MULTI_THREADED, &pD2DFactory); @@ -67,6 +68,18 @@ class ZoneWindowDrawing // TODO: Destroy factory } + static IDWriteFactory* GetWriteFactory() + { + static IUnknown* pDWriteFactory = nullptr; + if (!pDWriteFactory) + { + DWriteCreateFactory(DWRITE_FACTORY_TYPE_SHARED, __uuidof(IDWriteFactory), &pDWriteFactory); + } + return reinterpret_cast(pDWriteFactory); + + // TODO: Destroy factory + } + static D2D1_COLOR_F ConvertColor(COLORREF color) { return D2D1::ColorF(GetRValue(color) / 255.f, @@ -84,9 +97,13 @@ public: ZoneWindowDrawing(HWND window) { m_window = window; + m_renderTarget = nullptr; // Obtain the size of the drawing area. - GetClientRect(window, &m_clientRect); + if (!GetClientRect(window, &m_clientRect)) + { + return; + } // Create a Direct2D render target GetD2DFactory()->CreateHwndRenderTarget( @@ -105,16 +122,24 @@ public: { std::unique_lock lock(m_sceneMutex); + if (!m_renderTarget) + { + return; + } + m_renderTarget->BeginDraw(); DrawBackdrop(); + + for (const auto& drawableRect : m_sceneRects) { - ID2D1SolidColorBrush* borderBrush = NULL; - ID2D1SolidColorBrush* fillBrush = NULL; - // TODO: hresult and null checks + ID2D1SolidColorBrush* borderBrush = nullptr; + ID2D1SolidColorBrush* fillBrush = nullptr; + ID2D1SolidColorBrush* textBrush = nullptr; m_renderTarget->CreateSolidColorBrush(drawableRect.borderColor, &borderBrush); m_renderTarget->CreateSolidColorBrush(drawableRect.fillColor, &fillBrush); + m_renderTarget->CreateSolidColorBrush(D2D1::ColorF(D2D1::ColorF::Black), &textBrush); if (fillBrush) { @@ -127,6 +152,34 @@ public: m_renderTarget->DrawRectangle(drawableRect.rect, borderBrush); borderBrush->Release(); } + + std::wstring idStr = std::to_wstring(drawableRect.id); + + // TODO: mark string as non-localizable + IDWriteTextFormat* textFormat = nullptr; + auto writeFactory = GetWriteFactory(); + + if (writeFactory) + { + writeFactory->CreateTextFormat(L"Segoe ui", nullptr, DWRITE_FONT_WEIGHT_NORMAL, DWRITE_FONT_STYLE_NORMAL, DWRITE_FONT_STRETCH_NORMAL, 80.f, L"en-US", &textFormat); + } + + if (textFormat && textBrush) + { + textFormat->SetTextAlignment(DWRITE_TEXT_ALIGNMENT_CENTER); + textFormat->SetParagraphAlignment(DWRITE_PARAGRAPH_ALIGNMENT_CENTER); + m_renderTarget->DrawTextW(idStr.c_str(), idStr.size(), textFormat, drawableRect.rect, textBrush); + } + + if (textFormat) + { + textFormat->Release(); + } + + if (textBrush) + { + textBrush->Release(); + } } m_renderTarget->EndDraw(); @@ -173,7 +226,8 @@ public: DrawableRect drawableRect{ .rect = ConvertRect(zone->GetZoneRect()), .borderColor = borderColor, - .fillColor = inactiveColor + .fillColor = inactiveColor, + .id = zone->Id() }; m_sceneRects.push_back(drawableRect); @@ -195,7 +249,8 @@ public: DrawableRect drawableRect{ .rect = ConvertRect(zone->GetZoneRect()), .borderColor = borderColor, - .fillColor = highlightColor + .fillColor = highlightColor, + .id = zone->Id() }; m_sceneRects.push_back(drawableRect);