Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Another Screensaver with WPF

, 27 Jan 2012
Lessons learnt from writing a screensaver with WPF
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;

namespace RZWScreenSaver{
    static class IListExtension{
        static public T[] CastToArray<T>(this IList list){
            var result = new T[list.Count];
            for(int i=0; i < result.Length; ++i)
                result[i] = (T) list[i];
            return result;
        }
        static public int IndexOf<T>(this IList list, Func<T,bool> predicate){
            for(var i=0; i < list.Count; ++i){
                if (predicate((T)list[i]))
                    return i;
            }
            return -1;
        }
        static public void Shuffle<T>(this T[] source, int pos1, int pos2, ref T[] target){
            Debug.Assert(source.Length == target.Length);
            var firstCut = Math.Min(pos1, pos2);
            var secondCut = Math.Max(pos1, pos2);
            var firstChunkLength = firstCut;
            var secondChunkLength = secondCut - firstCut;
            var thirdChunkLength = source.Length - secondCut;
            const int targetFirstChunk = 0;
            if (pos1 < pos2){
                // perform shuffle pattern 1: swap chunk #1 and #2.
                var targetSecondChunk = secondChunkLength;
                var targetThirdChunk = secondChunkLength + firstChunkLength;
                Array.Copy(source, 0, target, targetSecondChunk, firstChunkLength);
                Array.Copy(source, firstCut, target, targetFirstChunk, secondChunkLength);
                Array.Copy(source, secondCut, target, targetThirdChunk, thirdChunkLength);
            }else{
                // swap chunk #1 and #3
                var targetSecondChunk = thirdChunkLength;
                var targetThirdChunk = thirdChunkLength + secondChunkLength;
                Array.Copy(source, 0, target, targetThirdChunk, firstChunkLength);
                Array.Copy(source, firstCut, target, targetSecondChunk, secondChunkLength);
                Array.Copy(source, secondCut, target, targetFirstChunk, thirdChunkLength);
            }
        }
        static public void Swap<T>(this IList<T> list, int pos1, int pos2){
            T temp = list[pos1];
            list[pos1] = list[pos2];
            list[pos2] = temp;
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Common Development and Distribution License (CDDL)

Share

About the Author

Ruxo Zheng
Technical Lead
Thailand Thailand
C/C++ and C# programmer.

| Advertise | Privacy | Mobile
Web04 | 2.8.140826.1 | Last Updated 27 Jan 2012
Article Copyright 2009 by Ruxo Zheng
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid