Math Attacks !

Back in 2009, I have been asked by a French Game Studio to develop a small shooter game for a Job Interview. They gave me some specifications and a week end to code it in Flash / Action Script. I had very little knowledge of the language, and here is what I came up with. It was one of the funniest and shortest project I worked on !

Source code and binaries available on codeplex !

Math Attacks

Mongoose Alpha 0.2 is out !

I added some navigation features for this second release.

  • The LegacyTreeViewItem (a WPF TreeView item with the look of the winform TreeViewItem) is now available in the mongoose.windows library
  • Some Surface Move/Scale operations are now animated.
  • Ctrl + Left Click on a surface pad now centers/scales it.
  • Ctrl + Left Click on the surface now centers/scales it to have all pads visible.
  • Performance enhancements.

Available on codeplex !

WPF MDI – Call of the Mongoose

Four months without any update is definitely too long of a time !

So, for this new post, I decided to share the personal project I’ve been working on these last few days. It’s a kind of multiple document interface using WPF. Ideally I’d like it to take the advantages of old school MDI and those of modern UIs using Docking managers.

It’s still fresh, and it’s now available on CodePlex. Don’t hesitate to take a look !

Mongoose project on CodePlex

MongooseSampleApplication

Finding a Visual in the WPF VisualTree

This is a classic : here are the methods I use to find a Visual element in the VisualTree.

public static class Visual_ExtensionMethods
{
    /// <summary>
    /// Retrieves the first Descendant of the currren Visual in the VisualTree matching the given predicate
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="this">The current Visual.</param>
    /// <param name="predicate">An optional predicate that the descendant have to satisfy.</param>
    /// <returns></returns>
    public static T SafeFindDescendant<T>(this Visual @this, Predicate<T> predicate = null) where T : Visual
    {
        T result = null;
        if (@this == null)
        {
            return null;
        }

        // iterate on VisualTree children thanks to VisualTreeHelper
        int childrenCount = VisualTreeHelper.GetChildrenCount(@this);
        for (int i = 0; i < childrenCount; i++)
        {
            var currentChild = VisualTreeHelper.GetChild(@this, i);

            var typedChild = currentChild as T;
            if (typedChild == null)
            {
                // recursive search
                result = ((Visual)currentChild).SafeFindDescendant<T>(predicate);
                if (result != null)
                {
                    break;
                }
            }
            else
            {
                if (predicate == null || predicate(typedChild))
                {
                    result = typedChild;
                    break;
                }
            }
        }

        return result;
    }

    /// <summary>
    /// Retrieves the first Ancestor of the currren Visual in the VisualTree matching the given predicate
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="this">The current Visual.</param>
    /// <param name="predicate">An optional predicate that the ancestor have to satisfy.</param>
    /// <returns></returns>
    public static T SafeFindAncestor<T>(this Visual @this, Predicate<T> predicate = null) where T : Visual
    {
        if (@this == null)
        {
            return null;
        }

        // retrieve Visual Parent thanks to VisualTreeHelper
        var parent = VisualTreeHelper.GetParent(@this);
        var typedParent = parent as T;

        return typedParent == null || (predicate != null && !predicate(typedParent)) ?
            ((Visual)parent).SafeFindAncestor<T>(predicate) : // recursive search
            typedParent; // ancestor found
    }
}

WPF Built-in value converters

I have been asked some time ago what were the buil-in value converters available in .NET 4.0. Here is the list :

IValueConverter

In System.Windows.Controls

In System.Windows.Documents

IMultiValueConverter

In System.Windows.Controls

In Microsoft.Windows.Themes

In System.Windows.Navigation

The WPF Developer Survival Tool Kit

Working with WPF is an everyday adventure, and a proper survival toolkit is required !

  • Visual Studio / Blend : Best .NET / XAML/ WPF editors out there.
  • Snoop : Very usefull to monitor any WPF application Visual Tree. It’s a lot like the visual tree view integrated in Visual Studio but with a few perks. A really appreciable one is its ability to show how Routed Events are processed and handled through the Visual TreeView. It can be very helpful when a bubbled event is handled and you have to know which control handled it
  • ILSpy .NET Reflector : Even if .NET and WPF sometimes looks like Dark Magic, everything can actually be explained if you look in the .NET assemblies code. When having troubles understanding an issue with your application, it’s often a good idea to look inside to see how things work underneath.
  • ShowMeTheTemplates : A very useful tool that exposes all the default WPF controls control templates. As Visual Studio 2012 now have a similar feature (that is working better) it’s however not as usefull as it once was.
  • dotTrace Performance / Memory profilers : Optimizing a WPF application and hunting memory leaks is an essential task in the life of a WPF developer, and the guys at JetBrains know their job : dotTrace is by far the best profiler I had the chance to work with.