Added Jason Kemp's DataBound ItemsControl Refactoring http://www.ageektrapped.com/blog/babysmash-iv-itemscontrol-and-data-bound-uis/
This commit is contained in:
Родитель
8ef33ba990
Коммит
c1b7d5dee7
|
@ -22,7 +22,7 @@ namespace BabySmash
|
|||
_hookID = InterceptKeys.SetHook(_proc);
|
||||
this.ShutdownMode = ShutdownMode.OnLastWindowClose; //TODO: Should this be OnMainWindowClose?
|
||||
mainWindow = new MainWindow();
|
||||
MainWindow.WindowState = WindowState.Maximized; //Do it here, rather than in XAML otherwise multimon won't work.
|
||||
//MainWindow.WindowState = WindowState.Maximized; //Do it here, rather than in XAML otherwise multimon won't work.
|
||||
mainWindow.Show();
|
||||
}
|
||||
catch (Exception e)
|
||||
|
|
|
@ -110,6 +110,8 @@
|
|||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="Audio.cs" />
|
||||
<Compile Include="Figure.cs" />
|
||||
<Compile Include="FigureGenerator.cs" />
|
||||
<Compile Include="KeyboardHook.cs" />
|
||||
<Compile Include="MainWindow.xaml.cs">
|
||||
<DependentUpon>MainWindow.xaml</DependentUpon>
|
||||
|
|
|
@ -0,0 +1,167 @@
|
|||
using System;
|
||||
using System.Windows;
|
||||
using System.Windows.Media;
|
||||
using System.Windows.Shapes;
|
||||
|
||||
namespace BabySmash
|
||||
{
|
||||
public abstract class Figure
|
||||
{
|
||||
private UIElement shape;
|
||||
private readonly string name;
|
||||
|
||||
protected Figure(Brush fill, string name)
|
||||
{
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public UIElement Shape
|
||||
{
|
||||
get { return shape; }
|
||||
protected set { shape = value; }
|
||||
}
|
||||
|
||||
public string Name
|
||||
{
|
||||
get { return name; }
|
||||
}
|
||||
}
|
||||
|
||||
public class LetterFigure : Figure
|
||||
{
|
||||
public LetterFigure(Brush fill, string name)
|
||||
: base(fill, name)
|
||||
{
|
||||
string nameToDisplay;
|
||||
if (Properties.Settings.Default.ForceUppercase)
|
||||
{
|
||||
nameToDisplay = name;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (Utils.GetRandomBoolean())
|
||||
nameToDisplay = name;
|
||||
else
|
||||
nameToDisplay = name.ToLowerInvariant();
|
||||
}
|
||||
Shape = Utils.DrawCharacter(400, nameToDisplay, fill);
|
||||
}
|
||||
}
|
||||
|
||||
public class SquareFigure : Figure
|
||||
{
|
||||
public SquareFigure(Brush fill)
|
||||
: base(fill, "square")
|
||||
{
|
||||
Shape = new Rectangle()
|
||||
{
|
||||
Fill = fill,
|
||||
Height = 380,
|
||||
Width = 380,
|
||||
StrokeThickness = 5,
|
||||
Stroke = Brushes.Black,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
public class RectangleFigure : Figure
|
||||
{
|
||||
public RectangleFigure(Brush fill)
|
||||
: base(fill, "rectangle")
|
||||
{
|
||||
Shape = new Rectangle()
|
||||
{
|
||||
Fill = fill,
|
||||
Height = 160,
|
||||
Width = 380,
|
||||
StrokeThickness = 5,
|
||||
Stroke = Brushes.Black,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
public class CircleFigure : Figure
|
||||
{
|
||||
public CircleFigure(Brush fill)
|
||||
: base(fill, "circle")
|
||||
{
|
||||
Shape = new Ellipse()
|
||||
{
|
||||
Fill = fill,
|
||||
Height = 400,
|
||||
Width = 400,
|
||||
StrokeThickness = 5,
|
||||
Stroke = Brushes.Black,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
public class TriangleFigure : Figure
|
||||
{
|
||||
public TriangleFigure(Brush fill)
|
||||
: base(fill, "triangle")
|
||||
{
|
||||
Shape = new Polygon()
|
||||
{
|
||||
Points = new PointCollection(new Point[]{
|
||||
new Point(200,50),
|
||||
new Point(400,400),
|
||||
new Point(0,400),
|
||||
new Point(200,50)}),
|
||||
Height = 400,
|
||||
Width = 400,
|
||||
Fill = fill,
|
||||
StrokeThickness = 5,
|
||||
Stroke = Brushes.Black,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
public class StarFigure : Figure
|
||||
{
|
||||
public StarFigure(Brush fill)
|
||||
: base(fill, "star")
|
||||
{
|
||||
Shape = new Star()
|
||||
{
|
||||
NumberOfPoints = 5,
|
||||
Height = 400,
|
||||
Width = 400,
|
||||
Fill = fill,
|
||||
StrokeThickness = 5,
|
||||
Stroke = Brushes.Black,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
public class TrapezoidFigure : Figure
|
||||
{
|
||||
public TrapezoidFigure(Brush fill)
|
||||
: base(fill, "trapezoid")
|
||||
{
|
||||
Shape = new Path()
|
||||
{
|
||||
Data = Geometry.Parse("F1 M 257.147,126.953L 543.657,126.953L 640.333,448.287L 160.333,448.287L 257.147,126.953 Z "),
|
||||
Fill = fill,
|
||||
Stroke = Brushes.Black,
|
||||
StrokeThickness = 5,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
public class HeartFigure : Figure
|
||||
{
|
||||
public HeartFigure(Brush fill)
|
||||
: base(fill, "heart")
|
||||
{
|
||||
Shape = new Path()
|
||||
{
|
||||
Data = Geometry.Parse("F1 M 429,161.333C 506.333,88.6667 609,142.122 609,225.333C 609,308.544 429,462.667 429,462.667C 429,462.667 257,306.544 257,223.333C 257,140.123 356.138,88.4713 429,161.333 Z "),
|
||||
Fill = fill,
|
||||
Stroke = Brushes.Black,
|
||||
StrokeThickness = 5,
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
}
|
|
@ -0,0 +1,92 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Windows;
|
||||
using System.Windows.Shapes;
|
||||
using System.Windows.Media;
|
||||
using System.Windows.Media.Animation;
|
||||
|
||||
namespace BabySmash
|
||||
{
|
||||
public class FigureGenerator
|
||||
{
|
||||
private int clearAfter;
|
||||
private readonly ObservableCollection<Figure> figures = new ObservableCollection<Figure>();
|
||||
|
||||
public int ClearAfter
|
||||
{
|
||||
get { return clearAfter; }
|
||||
set { clearAfter = value; }
|
||||
}
|
||||
|
||||
public ObservableCollection<Figure> Figures
|
||||
{
|
||||
get { return figures; }
|
||||
}
|
||||
|
||||
public void Generate(FrameworkElement container, string letter)
|
||||
{
|
||||
if (figures.Count == clearAfter)
|
||||
figures.Clear();
|
||||
Figure f = GenerateFigure(letter);
|
||||
Storyboard s = CreateStoryboardAnimation(container, f.Shape, Shape.OpacityProperty);
|
||||
figures.Add(f);
|
||||
if (Properties.Settings.Default.FadeAway) s.Begin(container);
|
||||
}
|
||||
|
||||
private Storyboard CreateStoryboardAnimation(FrameworkElement container, UIElement shape, DependencyProperty dp)
|
||||
{
|
||||
Storyboard st = new Storyboard();
|
||||
NameScope.SetNameScope(container, new NameScope());
|
||||
container.RegisterName("shape", shape);
|
||||
|
||||
DoubleAnimation d = new DoubleAnimation();
|
||||
d.From = 1.0;
|
||||
d.To = 0.0;
|
||||
d.Duration = new Duration(TimeSpan.FromSeconds(5));
|
||||
d.AutoReverse = false;
|
||||
|
||||
st.Children.Add(d);
|
||||
Storyboard.SetTargetName(d, "shape");
|
||||
Storyboard.SetTargetProperty(d, new PropertyPath(dp));
|
||||
return st;
|
||||
}
|
||||
|
||||
private Figure GenerateFigure(string letter)
|
||||
{
|
||||
//TODO: Should this be in XAML? Would that make it better?
|
||||
Brush fill = Utils.GetRandomColoredBrush();
|
||||
if (letter.Length == 1 && Char.IsLetterOrDigit(letter[0]))
|
||||
{
|
||||
return new LetterFigure(fill, letter);
|
||||
}
|
||||
else
|
||||
{
|
||||
int shape = Utils.RandomBetweenTwoNumbers(0, 6);
|
||||
//TODO: Should I change the height, width and stroke to be relative to the screen size?
|
||||
//TODO: I think I need a shapefactory?
|
||||
switch (shape)
|
||||
{
|
||||
case 0:
|
||||
return new SquareFigure(fill);
|
||||
case 1:
|
||||
return new CircleFigure(fill);
|
||||
case 2:
|
||||
return new TriangleFigure(fill);
|
||||
case 3:
|
||||
return new StarFigure(fill);
|
||||
case 4:
|
||||
return new HeartFigure(fill);
|
||||
case 5:
|
||||
return new TrapezoidFigure(fill);
|
||||
case 6:
|
||||
return new RectangleFigure(fill);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
|
@ -1,11 +1,10 @@
|
|||
using System.Globalization;
|
||||
using System.Windows;
|
||||
using System.Windows.Media;
|
||||
using System.Windows.Shapes;
|
||||
|
||||
namespace BabySmash
|
||||
{
|
||||
class HiResTextBlock : Shape
|
||||
class HiResTextBlock : FrameworkElement
|
||||
{
|
||||
public HiResTextBlock()
|
||||
: base()
|
||||
|
@ -18,10 +17,6 @@ namespace BabySmash
|
|||
static Pen m_pen;
|
||||
#endregion
|
||||
|
||||
protected override Geometry DefiningGeometry
|
||||
{
|
||||
get { return m_textg ?? Geometry.Empty; }
|
||||
}
|
||||
#region methods
|
||||
protected override void OnRender(DrawingContext drawingContext)
|
||||
{
|
||||
|
@ -60,7 +55,56 @@ namespace BabySmash
|
|||
#endregion
|
||||
|
||||
#region DPs
|
||||
|
||||
public Brush Stroke
|
||||
{
|
||||
get
|
||||
{
|
||||
return (Brush)GetValue(StrokeProperty);
|
||||
}
|
||||
|
||||
set
|
||||
{
|
||||
SetValue(StrokeProperty, value);
|
||||
}
|
||||
}
|
||||
|
||||
public static readonly DependencyProperty StrokeProperty = DependencyProperty.Register(
|
||||
"Stroke",
|
||||
typeof(Brush),
|
||||
typeof(HiResTextBlock),
|
||||
new FrameworkPropertyMetadata(
|
||||
new SolidColorBrush(Colors.Black),
|
||||
FrameworkPropertyMetadataOptions.AffectsRender,
|
||||
new PropertyChangedCallback(OnTextInvalidated),
|
||||
null
|
||||
)
|
||||
);
|
||||
|
||||
public ushort StrokeThickness
|
||||
{
|
||||
get
|
||||
{
|
||||
return (ushort)GetValue(StrokeThicknessProperty);
|
||||
}
|
||||
|
||||
set
|
||||
{
|
||||
SetValue(StrokeThicknessProperty, value);
|
||||
}
|
||||
}
|
||||
|
||||
public static readonly DependencyProperty StrokeThicknessProperty = DependencyProperty.Register(
|
||||
"StrokeThickness",
|
||||
typeof(ushort),
|
||||
typeof(HiResTextBlock),
|
||||
new FrameworkPropertyMetadata(
|
||||
(ushort)1,
|
||||
FrameworkPropertyMetadataOptions.AffectsRender,
|
||||
new PropertyChangedCallback(OnTextInvalidated),
|
||||
null
|
||||
)
|
||||
);
|
||||
|
||||
public string Text
|
||||
{
|
||||
get
|
||||
|
@ -111,6 +155,31 @@ namespace BabySmash
|
|||
)
|
||||
);
|
||||
|
||||
public Brush Fill
|
||||
{
|
||||
get
|
||||
{
|
||||
return (Brush)GetValue(FillProperty);
|
||||
}
|
||||
|
||||
set
|
||||
{
|
||||
SetValue(FillProperty, value);
|
||||
}
|
||||
}
|
||||
|
||||
public static readonly DependencyProperty FillProperty = DependencyProperty.Register(
|
||||
"Fill",
|
||||
typeof(Brush),
|
||||
typeof(HiResTextBlock),
|
||||
new FrameworkPropertyMetadata(
|
||||
new SolidColorBrush(Colors.White),
|
||||
FrameworkPropertyMetadataOptions.AffectsRender,
|
||||
new PropertyChangedCallback(OnTextInvalidated),
|
||||
null
|
||||
)
|
||||
);
|
||||
|
||||
public FontFamily Font
|
||||
{
|
||||
get
|
||||
|
|
14
Utils.cs
14
Utils.cs
|
@ -51,6 +51,20 @@ namespace BabySmash
|
|||
return lRandom.Next(min, max + 1);
|
||||
}
|
||||
|
||||
public static UIElement DrawCharacter(double fontSize, string textString, Brush brush)
|
||||
{
|
||||
HiResTextBlock textBlock = new HiResTextBlock()
|
||||
{
|
||||
//TextWrapping = TextWrapping.Wrap,
|
||||
//FontFamily = new FontFamily("Rockwell Extra Bold"),
|
||||
//Foreground = brush, //randomize
|
||||
FontSize = fontSize, //pick better size
|
||||
Fill = brush,
|
||||
Text = textString,
|
||||
StrokeThickness = 5,
|
||||
};
|
||||
return textBlock;
|
||||
}
|
||||
public static UIElement DrawCharacter(double fontSize, string textString, Brush brush, double left, double top)
|
||||
{
|
||||
HiResTextBlock textBlock = new HiResTextBlock()
|
||||
|
|
Загрузка…
Ссылка в новой задаче