This repository was archived by the owner on Jul 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCanvasEffect.hpp
162 lines (152 loc) · 4.66 KB
/
CanvasEffect.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#pragma once
#include "framework.h"
#include "winrt.h"
#include "wil.h"
namespace AcrylicEverywhere
{
struct NamedProperty
{
LPCWSTR Name; // Compile-time constant
UINT Index; // Property index
using GRAPHICS_EFFECT_PROPERTY_MAPPING = ABI::Windows::Graphics::Effects::GRAPHICS_EFFECT_PROPERTY_MAPPING;
GRAPHICS_EFFECT_PROPERTY_MAPPING Mapping;
};
class CanvasEffect : public winrt::implements<CanvasEffect, winrt::Windows::Graphics::Effects::IGraphicsEffect, winrt::Windows::Graphics::Effects::IGraphicsEffectSource, ABI::Windows::Graphics::Effects::IGraphicsEffectD2D1Interop>
{
CLSID m_effectId{};
winrt::hstring m_name{};
std::unordered_map<size_t, winrt::Windows::Foundation::IPropertyValue> m_properties{};
std::unordered_map<size_t, winrt::Windows::Graphics::Effects::IGraphicsEffectSource> m_effectSources{};
protected:
std::vector<NamedProperty> m_namedProperties{};
public:
CanvasEffect(REFCLSID effectId) : m_effectId{ effectId } {}
virtual ~CanvasEffect() = default;
winrt::hstring Name()
{
return m_name;
}
void Name(const winrt::hstring& string)
{
m_name = string;
}
IFACEMETHOD(GetEffectId)(CLSID* id) override
{
if (id)
{
*id = m_effectId;
return S_OK;
}
return E_POINTER;
}
IFACEMETHOD(GetSourceCount)(UINT* count) override
{
if (count)
{
*count = static_cast<UINT>(m_effectSources.size());
return S_OK;
}
return E_POINTER;
}
IFACEMETHOD(GetSource)(UINT index, ABI::Windows::Graphics::Effects::IGraphicsEffectSource** source)
{
if (!source)
{
return E_POINTER;
}
m_effectSources.at(index).as<ABI::Windows::Graphics::Effects::IGraphicsEffectSource>().copy_to(source);
return S_OK;
}
IFACEMETHOD(GetPropertyCount)(UINT* count) override
{
if (count)
{
*count = static_cast<UINT>(m_effectSources.size());
return S_OK;
}
return E_POINTER;
}
IFACEMETHOD(GetProperty)(UINT index, ABI::Windows::Foundation::IPropertyValue** value) override
{
if (!value)
{
return E_POINTER;
}
*value = m_properties.at(index).as<ABI::Windows::Foundation::IPropertyValue>().detach();
return S_OK;
}
IFACEMETHOD(GetNamedPropertyMapping)(LPCWSTR name, UINT* index, NamedProperty::GRAPHICS_EFFECT_PROPERTY_MAPPING* mapping) override
{
for (UINT i = 0; i < m_namedProperties.size(); ++i)
{
const auto& prop = m_namedProperties[i];
if (!_wcsicmp(name, prop.Name))
{
*index = prop.Index;
*mapping = prop.Mapping;
return S_OK;
}
}
return E_INVALIDARG;
}
public:
template <typename T>
static winrt::com_ptr<T> construct_from_abi(T* from)
{
winrt::com_ptr<T> to{ nullptr };
to.copy_from(from);
return to;
};
void SetName(const winrt::hstring& name) { Name(name); }
void SetInput(UINT index, const winrt::Windows::Graphics::Effects::IGraphicsEffectSource& source)
{
m_effectSources.insert_or_assign(index, source);
}
void SetInput(const winrt::Windows::Graphics::Effects::IGraphicsEffectSource& source)
{
SetInput(0, source);
}
void SetInput(const winrt::Windows::UI::Composition::CompositionEffectSourceParameter& source)
{
SetInput(0, source.as<winrt::Windows::Graphics::Effects::IGraphicsEffectSource>());
}
void SetInput(const std::vector<winrt::Windows::Graphics::Effects::IGraphicsEffectSource>& effectSourceList)
{
for (UINT i = 0; i < effectSourceList.size(); i++)
{
SetInput(i, effectSourceList[i]);
}
}
protected:
void SetProperty(UINT index, const winrt::Windows::Foundation::IPropertyValue& value)
{
m_properties.insert_or_assign(index, value);
}
template <typename T>
auto BoxValue(T value)
{
return winrt::Windows::Foundation::PropertyValue::CreateUInt32(value).as<winrt::Windows::Foundation::IPropertyValue>();
}
auto BoxValue(bool value)
{
return winrt::Windows::Foundation::PropertyValue::CreateBoolean(value).as<winrt::Windows::Foundation::IPropertyValue>();
}
auto BoxValue(float value)
{
return winrt::Windows::Foundation::PropertyValue::CreateSingle(value).as<winrt::Windows::Foundation::IPropertyValue>();
}
auto BoxValue(UINT32 value)
{
return winrt::Windows::Foundation::PropertyValue::CreateUInt32(value).as<winrt::Windows::Foundation::IPropertyValue>();
}
auto BoxValue(const D2D1_MATRIX_5X4_F& value)
{
return winrt::Windows::Foundation::PropertyValue::CreateSingleArray({ reinterpret_cast<const float*>(&value), sizeof(value) / sizeof(float) }).as<winrt::Windows::Foundation::IPropertyValue>();
}
template <size_t size>
auto BoxValue(float(&value)[size])
{
return winrt::Windows::Foundation::PropertyValue::CreateSingleArray(value).as<winrt::Windows::Foundation::IPropertyValue>();
}
};
}