-
Notifications
You must be signed in to change notification settings - Fork 8k
/
VisualProperties.cpp
343 lines (295 loc) · 9.82 KB
/
VisualProperties.cpp
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
#include "pch.h"
#include "VisualProperties.h"
#include "windowsnumerics.h"
using namespace Windows::ApplicationModel::Core;
using namespace Windows::UI::Core;
using namespace Windows::Foundation;
using namespace Windows::UI::Composition;
using namespace Windows::Foundation::Numerics;
using namespace Windows::UI;
//------------------------------------------------------------------------------
//
// main
//
//------------------------------------------------------------------------------
// The main function is only used to initialize our IFrameworkView class.
[Platform::MTAThread]
int main(Platform::Array<Platform::String^>^)
{
auto visualPropertiesFactory = ref new VisualPropertiesFactory();
CoreApplication::Run(visualPropertiesFactory);
return 0;
}
VisualProperties::VisualProperties()
{
}
//------------------------------------------------------------------------------
//
// VisualProperties.Initialize
//
// This method is called during startup to associate the IFrameworkView with the
// CoreApplicationView.
//
//------------------------------------------------------------------------------
void VisualProperties::Initialize(CoreApplicationView^ view)
{
_view = view;
}
//------------------------------------------------------------------------------
//
// VisualProperties.SetWindow
//
// This method is called when the CoreApplication has created a new CoreWindow,
// allowing the application to configure the window and start producing content
// to display.
//
//------------------------------------------------------------------------------
void VisualProperties::SetWindow(Windows::UI::Core::CoreWindow^ window)
{
_window = window;
InitNewComposition();
_window->PointerPressed += ref new TypedEventHandler<CoreWindow^, PointerEventArgs^>(this, &VisualProperties::OnPointerPressed);
_window->PointerMoved += ref new TypedEventHandler<CoreWindow^, PointerEventArgs^>(this, &VisualProperties::OnPointerMoved);
_window->PointerReleased += ref new TypedEventHandler<CoreWindow^, PointerEventArgs^>(this, &VisualProperties::OnPointerReleased);
}
//------------------------------------------------------------------------------
//
// VisualProperties.OnPointerPressed
//
// This method is called when the user touches the screen, taps it with a stylus
// or clicks the mouse.
//
//------------------------------------------------------------------------------
void VisualProperties::OnPointerPressed(Windows::UI::Core::CoreWindow^ window, Windows::UI::Core::PointerEventArgs^ args)
{
auto position = args->CurrentPoint->Position;
//
// Walk our list of visuals to determine who, if anybody, was selected
//
for (Visual^ child : _root->Children)
{
//
// Did we hit this child?
//
float3 offset = child->Offset;
float2 size = child->Size;
if ((position.X >= offset.x) &&
(position.X < offset.x + size.x) &&
(position.Y >= offset.y) &&
(position.Y < offset.y + size.y))
{
//
// This child was hit. Since the children are stored back to front,
// the last one hit is the front-most one so it wins
//
_currentVisual = dynamic_cast<ContainerVisual^>(child);
_offsetBias = new float2((float)(offset.x - position.X),
(float)(offset.y - position.Y));
}
}
//
// If a visual was hit, bring it to the front of the Z order
//
if (_currentVisual != nullptr)
{
ContainerVisual^ parent = dynamic_cast<ContainerVisual^>(_currentVisual->Parent);
parent->Children->Remove(_currentVisual);
parent->Children->InsertAtTop(_currentVisual);
}
}
//------------------------------------------------------------------------------
//
// VisualProperties.OnPointerMoved
//
// This method is called when the user moves their finger, stylus or mouse with
// a button pressed over the screen.
//
//------------------------------------------------------------------------------
void VisualProperties::OnPointerMoved(Windows::UI::Core::CoreWindow^ window, Windows::UI::Core::PointerEventArgs^ args)
{
//
// If a visual is selected, drag it with the pointer position and
// make it opaque while we drag it
//
if (_currentVisual != nullptr)
{
//
// Set up the properties of the visual the first time it is
// dragged. This will last for the duration of the drag
//
if (!_dragging)
{
_currentVisual->Opacity = 1.0f;
//
// Transform the first child of the current visual so that
// the image is rotated
//
for (auto child : _currentVisual->Children)
{
child->RotationAngleInDegrees = 45.0f;
child->CenterPoint = float3(_currentVisual->Size.x / 2, _currentVisual->Size.y / 2, 0);
break;
}
//
// Clip the visual to its original layout rect by using an inset
// clip with a one-pixel margin all around
//
auto clip = _compositor->CreateInsetClip();
clip->LeftInset = 1.0f;
clip->RightInset = 1.0f;
clip->TopInset = 1.0f;
clip->BottomInset = 1.0f;
_currentVisual->Clip = clip;
_dragging = true;
}
Point position = args->CurrentPoint->Position;
_currentVisual->Offset = float3((float)(position.X + _offsetBias->x),
(float)(position.Y + _offsetBias->y),
0.0f);
}
}
//------------------------------------------------------------------------------
//
// VisualProperties.OnPointerReleased
//
// This method is called when the user lifts their finger or stylus from the
// screen, or lifts the mouse button.
//
//------------------------------------------------------------------------------
void VisualProperties::OnPointerReleased(Windows::UI::Core::CoreWindow^ window, Windows::UI::Core::PointerEventArgs^ args)
{
//
// If a visual was selected, make it transparent again when it is
// released and restore the transform and clip
//
if (_currentVisual != nullptr)
{
if (_dragging)
{
//
// Remove the transform from the first child
//
for (auto child : _currentVisual->Children)
{
child->RotationAngleInDegrees = 0;
child->CenterPoint = float3(0, 0, 0);
break;
}
_currentVisual->Opacity = 0.8f;
_currentVisual->Clip = nullptr;
_dragging = false;
}
_currentVisual = nullptr;
}
}
//------------------------------------------------------------------------------
//
// VisualProperties.InitNewComposition
//
// This method is called by SetWindow(), where we initialize Composition after
// the CoreWindow has been created.
//
//------------------------------------------------------------------------------
void VisualProperties::InitNewComposition()
{
//
// Set up Windows.UI.Composition Compositor, root ContainerVisual, and associate with
// the CoreWindow.
//
_compositor = ref new Compositor();
_root = _compositor->CreateContainerVisual();
_compositionTarget = _compositor->CreateTargetForCurrentView();
_compositionTarget->Root = _root;
//
// Create a few visuals for our window
//
for (int index = 0; index < 20; index++)
{
_root->Children->InsertAtTop(CreateChildElement());
}
}
//------------------------------------------------------------------------------
//
// VisualProperties.CreateChildElement
//
// Creates a small sub-tree to represent a visible element in our application.
//
//------------------------------------------------------------------------------
Visual^ VisualProperties::CreateChildElement()
{
//
// Each element consists of three visuals, which produce the appearance
// of a framed rectangle
//
auto element = _compositor->CreateContainerVisual();
element->Size = float2(100.0f, 100.0f);
//
// Position this visual randomly within our window
//
element->Offset = float3(((float)rand() / RAND_MAX) * 400, ((float)rand() / RAND_MAX) * 400, 0.0f);
//
// The outer rectangle is always white
//
auto visual = _compositor->CreateSpriteVisual();
element->Children->InsertAtTop(visual);
visual->Brush = _compositor->CreateColorBrush(ColorHelper::FromArgb(0xFF, 0xFF, 0xFF, 0xFF));
visual->Size = float2(100.0f, 100.0f);
//
// The inner rectangle is inset from the outer by three pixels all around
//
auto child = _compositor->CreateSpriteVisual();
visual->Children->InsertAtTop(child);
child->Offset = float3(3.0f, 3.0f, 0.0f);
child->Size = float2(94.0f, 94.0f);
//
// Pick a random color for every rectangle
//
byte red = (byte)(0xFF * (0.2f + (((float)rand() / RAND_MAX) / 0.8f)));
byte green = (byte)(0xFF * (0.2f + (((float)rand() / RAND_MAX) / 0.8f)));
byte blue = (byte)(0xFF * (0.2f + (((float)rand() / RAND_MAX) / 0.8f)));
child->Brush = _compositor->CreateColorBrush(ColorHelper::FromArgb(0xFF, red, green, blue));
//
// Make the subtree root visual partially transparent. This will cause each visual in the subtree
// to render partially transparent, since a visual's opacity is multiplied with its parent's
// opacity
//
element->Opacity = 0.8f;
return element;
}
//------------------------------------------------------------------------------
//
// VisualProperties.Load
//
// This method is called when a specific page is being loaded in the
// application. It is not used for this application.
//
//------------------------------------------------------------------------------
void VisualProperties::Load(Platform::String ^ unused)
{
}
//------------------------------------------------------------------------------
//
// VisualProperties.Run
//
// This method is called by CoreApplication.Run() to actually run the
// dispatcher's message pump.
//
//------------------------------------------------------------------------------
void VisualProperties::Run()
{
_window->Activate();
_window->Dispatcher->ProcessEvents(CoreProcessEventsOption::ProcessUntilQuit);
}
//------------------------------------------------------------------------------
//
// VisualProperties.Uninitialize
//
// This method is called during shutdown to disconnect the CoreApplicationView,
// and CoreWindow from the IFrameworkView.
//
//------------------------------------------------------------------------------
void VisualProperties::Uninitialize()
{
_window = nullptr;
_view = nullptr;
}