/
BottomLeftImageEmitDetails.cs
119 lines (112 loc) · 5.71 KB
/
BottomLeftImageEmitDetails.cs
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
// <copyright file="BottomLeftImageEmitDetails.cs" company="natsnudasoft">
// Copyright (c) Adrian John Dunstan. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// </copyright>
namespace Natsnudasoft.EgamiFlowScreensaver
{
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Natsnudasoft.EgamiFlowScreensaver.Config;
using Natsnudasoft.NatsnudaLibrary;
/// <summary>
/// Provides a class capable of creating locations at the bottom left of a screen for a
/// <see cref="ScreensaverImageEmitter" />.
/// </summary>
public sealed class BottomLeftImageEmitDetails : ImageEmitDetails
{
/// <summary>
/// Initializes a new instance of the <see cref="BottomLeftImageEmitDetails"/> class.
/// </summary>
/// <param name="screensaverArea">The description of the area of the screensaver.</param>
/// <param name="screensaverConfiguration">The <see cref="ScreensaverConfiguration"/>
/// representing the configured state of the screensaver.</param>
/// <param name="behaviorFactories">A collection of factories that define how to create
/// behaviours that will be attached to any images emitted by a
/// <see cref="ScreensaverImageEmitter"/>.</param>
/// <exception cref="ArgumentNullException"><paramref name="screensaverArea"/>,
/// <paramref name="screensaverConfiguration"/>, or <paramref name="behaviorFactories"/>
/// is <see langword="null"/>.</exception>
public BottomLeftImageEmitDetails(
ScreensaverArea screensaverArea,
ScreensaverConfiguration screensaverConfiguration,
IEnumerable<Func<IScreensaverImageItemBehavior>> behaviorFactories)
: base(screensaverArea, screensaverConfiguration, behaviorFactories)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="BottomLeftImageEmitDetails"/> class.
/// </summary>
/// <param name="screensaverArea">The description of the area of the screensaver.</param>
/// <param name="screensaverConfiguration">The <see cref="ScreensaverConfiguration"/>
/// representing the configured state of the screensaver.</param>
/// <param name="behaviorFactories">A collection of factories that define how to create
/// behaviours that will be attached to any images emitted by a
/// <see cref="ScreensaverImageEmitter"/>.</param>
/// <param name="random">A pseudo-random number generator that can be used to generate
/// randomness in the <see cref="BottomLeftImageEmitDetails"/>.</param>
/// <exception cref="ArgumentNullException"><paramref name="screensaverArea"/>,
/// <paramref name="screensaverConfiguration"/>, <paramref name="behaviorFactories"/>, or
/// <paramref name="random"/> is <see langword="null"/>.</exception>
public BottomLeftImageEmitDetails(
ScreensaverArea screensaverArea,
ScreensaverConfiguration screensaverConfiguration,
IEnumerable<Func<IScreensaverImageItemBehavior>> behaviorFactories,
Random random)
: base(screensaverArea, screensaverConfiguration, behaviorFactories, random)
{
}
/// <inheritdoc/>
/// <exception cref="ArgumentNullException"><paramref name="texture"/> is
/// <see langword="null"/>.</exception>
public override ScreensaverImageItem CreateScreensaverImageItem(Texture2D texture)
{
ParameterValidation.IsNotNull(texture, nameof(texture));
var origin = new Vector2(texture.Width, texture.Height) / 2f;
var bottomLeftPrimary = new Point(
this.ScreensaverArea.PrimaryGameBounds.Left,
this.ScreensaverArea.PrimaryGameBounds.Bottom);
var maxX = bottomLeftPrimary.X - texture.Width + origin.X;
var minX = maxX - TwoPositionDistribution;
var minY = bottomLeftPrimary.Y + origin.Y;
var maxY = minY + TwoPositionDistribution;
var position = new Vector2(
this.Random.NextFloat(minX, maxX),
this.Random.NextFloat(minY, maxY));
return new ScreensaverImageItem(
texture,
this.BehaviorFactories.Select(f => f?.Invoke()))
{
Position = position,
Origin = origin
};
}
/// <inheritdoc/>
protected override Func<IScreensaverImageItemBehavior> CreateDefaultMovingBehaviorFactory()
{
IScreensaverImageItemBehavior CreateDefaultMovingBehavior()
{
const float minSpeed = MovingScreensaverImageItemBehavior.DefaultMinSpeed;
const float maxSpeed = MovingScreensaverImageItemBehavior.DefaultMaxSpeed;
var speed = new Vector2(
this.Random.NextFloat(minSpeed, maxSpeed),
-this.Random.NextFloat(minSpeed, maxSpeed));
return new MovingScreensaverImageItemBehavior(this.ScreensaverArea, speed);
}
return CreateDefaultMovingBehavior;
}
}
}