Inyectando TypeEditors dinámicamente

Muchas aplicaciones de edición todavía utilizan Windows Forms. Y muchas de ellas (sobre todo si son prototipos rápidos o herramientas internas) utilizan el PropertyGrid como método rápido y eficiente para editar propiedades de objetos.

Una de las funcionalidades más utiles de los PropertyGrids, es la posibilidad de definir TypeEditors para las propiedades de un objeto, de forma que el sistema escogerá automáticamente un interfaz de usuario específico para editar su valor. .Net incluye por defecto algunos de ellos, como el ColorPicker o el DateTimePicker:

Cuando los TypeEditors por defecto no son suficiente, es una gran idea desarrollar tus propios editores, para hacer tus editores lo más eficientes posible. Por ejemplo, el selector de colores por defecto de .Net no permite escoger valores para el canal Alpha (transparencia). La solución es sencilla y fácil: podemos implementar nuestro propio editor que sí lo permita.

Ahora bien, ¿qué ocurre si queremos aplicar ese TypeEditor a todas las propiedades de un tipo definido en otra DLL?

Es un caso bastante frecuente. En mi entorno, por ejemplo, tengo mi propia clase para almacenar colores, llamada Color4, pero está definida en una DLL específica para operaciones matemáticas. Es una DLL muy básica que quiero mantener con el menor número de referencias posible, para evitar dependencias todo lo que pueda. Por este motivo, es imposible definir el TypeEditor en dicha DLL, ya que eso implicaría referenciar System.Windows.Forms, System.Drawing, y unas cuantas cosas más que no tienen que estar ahí. A fin de cuentas, es mi editor visual el que debe depender de Windows Forms, y no mi DLL de operaciones matemáticas. ¿La solución?

Inyección dinámica de editores de tipo

La solución es asignar el TypeEditor dinámicamente, programaticamente, o como queráis decirlo. En lugar de incluirlo en tiempo de compilación, con clásico código…

 [EditorAttribute("Color4Editor", typeof(System.Drawing.Design.UITypeEditor))]

…lo añadiremos en tiempo de ejecución, algo posible gracias a la clase TypeDescriptor y su método AddAttributes.

Basta con invocar algo como lo siguiente el el inicio del programa:

TypeDescriptor.AddAttributes(typeof(Color4), new EditorAttribute(typeof(Color4Editor), typeof(UITypeEditor)));

De esta forma, mantenemos las DLLs limpias de referencias innecesarias, y solo dependeremos de Windows Forms y similares donde realmente se necesita: en el editor.

Listo ! Smile

NSU / Nokia Software Updater for Retail doesn’t detect the device

Today, I was updating my phone to Lumia Cyan, and for a while I had an issue: the NSU For Retail didn’t detect my device. No matter how many times I tried to connect in different USB ports and using different cables, the NSU was always like this:

image

If it’s your case, go to Control Panel->Device Manager, and under “Universal Serial Bus Devices”, you’ll find two entries:

image

To fix the issue, just:

  • 1 - Unplug your phone
  • 2 – Right-click on both devices and select “Uninstall”.
  • 3 – Replug your device again

NSU should detect the device again.

Hope it helps !!

Mesa redonda–Intrograph II (Jornadas Nacionales de Informática Gráfica)

Se acaba de publicar un vídeo grabado por uno de los asistentes a Intrograph II, las Jornadas Nacionales de Informática Gráfica celebradas en Mayo en la Universidad Rey Juan Carlos.

Participé como ponente y también en la mesa redonda que clausuraba el evento. El vídeo se corresponde con ésta última sesión:

Un pequeño índice dentro del vídeo, con los temas sobre los que los ponentes respondíamos preguntas o debatíamos:

2:16 ¿Indie o triple A?
20:31 ¿PC o Consola?
49:00 Videojuegos ¿Son mas fáciles ahora?
59:55 Fecha optima del lanzamiento de videojuegos
1:13:39 Desarrollo de videjuegos ¿Por donde ir?

Massive hard drive activity when opening the Visual Studio 2013 XAML designer

This has been happening to me since last Wednesday. Honestly, I have no idea why wasn't happening before and why it started to happen now...

I have a pretty big C# Visual Studio solution, with references to native DLLs. Part of the UI is WPF, so it involves using XAML user controls. Now, when I open one of those XAML files and the designer kicks in (you can see a new process in the task manager called XDesProc.exe), my PC almost freezes.

After checking for a while, I realized that the VStudio's designer is filling the Shadow Cache folder like crazy (in my PC it's C:\Users\XXX\AppData\Local\Microsoft\VisualStudio\12.0\Designer\ShadowCache). And when I say "crazy", believe me... I mean it... It filled like 8 GB of data in a few moments...

There are many people out there with similar problems (some say to have cleaned from that folder more than 50 GB of data). You can find more examples here, here and here

If any of you have any clue about why this is happening, please let me know. By now, I managed to workaround it by completely disabling the XAML visual editor by:

1.- In Visual Studio solution explorer, right-click in a xaml file and select "Open With"...
2.- Choose XML (Text) Editor
3.- Click on "Set as default".
4.- Click Ok.


Please note: I found several websites suggesting a similar change, but saying the the Source Code (Text) Editor would work as well. It didn't work in my case. The designer didn't open, but the disk activity was surprisingly the same. The only choice that worked for me was XML (Text) Editor.

Next time you open an xaml file, the visual editor won't kick in, and you won't have the problem. However, it'd good to find the cause for this... Maybe I´ll try to re-install VStudio 2013 when I have a minute...

Hope it helps !! :)

Evolution of visuals in racing games 1992-2014

22 years. That's the time it takes to move from barely recognizable locations to blazing reality.

I still remember when I first saw Geoff Crammond - MicroProse's Formula One Grand Prix. It was mindblowing!! 3D graphics, amazing gameplay... Best racing experience I had so far...

One of my best friends had a 386 PC that met the ultra-high-end requirements the game needed (DOS, 1 MB of Ram and VGA Graphics Card). Every time I had the chance to drop by his house, first thing we did was to play this game.


But things have changed quite a lot in this time. Comparing the Monaco environment from that game with the one from CodeMasters F1 2013 makes a hell of a difference...

[Best viewed in FullHD 1080p]

And things still keep moving forward. This year, we will have a new F1 2014 game, and some others that keep pushing the limits of visual realism in videogames: [All videos best viewed in FullHD 1080p]

Diveclub:


Project Cars Trailer:


Project Cars vs Real Life:


Awesome !!!

Cómo tomar una muestra de color en cualquier cosa mostrada en tu pantalla, con Photoshop

Esta es una funcionalidad de Photoshop que no conocía. Si alguna vez te has preguntado qué color exactamente utiliza un icono, un botón de una aplicación, o cualquier otra cosa que se muestre en tu PC, puedes averiguarlo con Photoshop siguiendo estos sencillos pasos:

1.- Abre una imagen cualquiera, en Photoshop

2.- Selecciona el “EyeDropper” tool, para tomar muestras de color (normalmente dentro de la imagen en la que estás trabajando).

3.- Haz click en cualquier punto de tu imagen y, SIN SOLTAR EL BOTÓN, arrastra el puntero del mouse a cualquier otro punto de tu escritorio, a cualquier otra aplicación, icono o ventana.

Photoshop irá mostrando constantemente el color debajo del cursor del ratón, sin importar si todavía estás en tu imagen o siquiera dentro de Photoshop. Cuando sueltes el botón, ese color es seleccionado como color de trabajo en Photoshop, por lo que ya puedes utilizarlo en tus imágenes o acceder a sus componentes RGB.

Muy útil!!!


J

How to install Windows Phone 8.1, even with no developer account

If you can't wait to have Windows Phone 8.1 and Cortana, you can have it right now.


As you might have read, all you need to do is search in the Store for the application: Preview for Developers, install and open it. It opens the door to the set of updates that will trigger the installation of Windows Phone 8.1.

The thing is that one of the steps when opening it require you to enter your Live Account credentials, and in theory you must be registered as a Developer in the Windows Phone Dev Center.

Sincerely, you should consider register as so if you plan to do any development... It's only $14 yearly... But if you don't want to pay, there is a way:

1.- Go to http://appstudio.windowsphone.com
2.- Log in with your non-developer Live Account ID, and accept all the terms and conditions
3.- Click in "Start New Project"
4.- Select whichever project template you prefer (empty app will do the trick), and complete the process.

Now, you should be able to run the Preview for Developers thing, and it should accept your credentials as a registered developer.

Ta dá !!!

Disclaimer: Do this at your own risk. Windows Phone 8.1 is currently a developer preview and might contain bugs. I take no responsibility for any damage done to your phone or to your data, so better know what you are doing... :)





Fast casting of C# Structs with no unsafe code (but still kind of "unsafe")

C++ allows us to perform any casting between memory pointers. It's basically up to you to ensure the correct types are casted to prevent memory problems.

C# however doesn't allow to do this out of the box, unless you go into using unsafe code and perform the pointer conversion yourself, pretty much like in C++.

Problem is that unsafe code is not supported in all platforms, and generally it's a good idea to avoid using it as long as you can.

So, imagine we have two structs like this:


    public struct STA
    {
        public int CustomerID;
        public float CustomerRate;
    }
    public struct STB
    {
        public int CustomerID;
        public float CustomerRate;
    }

One of them is yours, and the other one comes from an APIs or legacy software you don't have access to. Now, imagine you need to convert one into another. How would you face that?

Obviously, if you try to simply assign them, it just won't work:



Of course, the most evident (ans safest) solution is to create a new struct of the type STB and copy the contents from STA to STB:

struct_a = new STA(struct_b.CustomerID, struct_b.CustomerRate);

The drawback is that this approach is slow and implies a memory overhead, what might not be an option sometimes.

If performance is a critical issue, you are sure that both structs are 100% compatible and share the exact same memory layout, and that both come from compatible platforms... Why not fooling the compiler and make it just assume that they are compatible types? 

As we mentioned, in unsafe C# code this can be simply achieved by casting pointers, just like in C++. But if you mark your C# code as unsafe, it can be rejected in some platforms. Is there a way to do that without using unsafe code? Yes, there is.

C# StructLayout to the rescue

Perfectly safe C# code allows you to explicitly define the offset of struct members, using attributes from System.Runtime.InteropServices, just like this:


    [StructLayout(LayoutKind.Explicit)]
    public struct STA
    {
        [FieldOffset(0)]
        public int CustomerID;
        [FieldOffset(4)]
        public float CustomerRate;
    }

This allows you to do tricky things like settings two different members of the struct at the same offset, creating something similar to C++ Unions:


    [StructLayout(LayoutKind.Explicit)]
    public struct Union
    {
        [FieldOffset(0)]
        public STA StructA;
        [FieldOffset(0)]
        public STB StructB;
    }

Note that both StructA and StructB are at the same field offset, and therefore will occupy the exact same location in memory. As both share the same memory layout, the result is that you have ONE single object in memory, and two different references (kind of pointers) to them, each one using a different type. 

Now, we can do the following:


            STA struct_a;
            STB struct_b;
            ...
            Union stu = new Union();
            stu.StructB = struct_b;
            struct_a = stu.StructA;

As you can see, no new STA has been created in memory, and we have saved all the process of copying data from one struct to another.

However, please be aware that this is kind of cheating... You are fooling the compiler to accept that, but in practice you are performing a classical pointer conversion, even if you are using purely safe code.

PLEASE BE AWARE that this approach doesn't take into account endianness. Different platforms, with different byte endianness, may store bytes in the opposite way. For example, if STA comes from a big-endian platform, and STB works in a little-endian platform (or just the opposite), bytes will be reversed when doing this operation. It doesn't take into account differences in data types either, so you must be very careful to ensure that all types have the same size in one struct and the other.

So, remember:
if(same endiannes & same data types) 
                              you are good to go !

Functional improvements

The Union struct we have created can be made much more comfortable to use if you add operators to it.

For example, comparison operators like this:

 public static bool operator ==(STA left, Union right)
        {
            return left == right.StructA;
        }
        public static bool operator ==(STB left, Union right)
        {
            return left == right.StructB;
        }

Will allow you to simply compare Unions with the original types:

if(union == struct_a)

And even more comfortable, adding implicit operators like this:

        public static implicit operator Union(STA value)
        {
            Union ret = new Union();
            ret.StructA = value;
            return ret;
        }

Will allow you to simply assign one type to the other like this:

            STA struct_a;
            ...
            Union union = struct_a;

Memory footprint improvements

One small drawback of this approach is the need to create structs of the type Union, each time you want to perform a conversion of this kind. A simple solution is to perform the operation in a static Union object. It's a bit messy, but it works. For instance, if you declare the class like this:

    [StructLayout(LayoutKind.Explicit)]
    public struct Union
    {
        [FieldOffset(0)]
        public STA StructA;
        [FieldOffset(0)]
        public STB StructB;

        public static Union StaticRef = new Union();

        public static STA ToSTA(STB pStructB)
        {
            StaticRef.StructB = pStructB;
            return StaticRef.StructA;
        }
        public static STB ToSTB(STA pStructA)
        {
            StaticRef.StructA = pStructA;
            return StaticRef.StructB;
        }
    }

You can now re-use the same static object over and over again, doing things like:

            STA struct_a;
            STB struct_b;
            ...
            struct_a = Union.ToSTA(struct_b);

Hope it helps!! Cheers...

HLSL code editing in Notepad ++

There are several HLSL syntax highlight add-ins for Visual Studio out there, but if you prefer to use the great NotePad++ to author or edit your shaders, my fellow DirectX MVP Matt Pettineo has written a Notepad++ add-in to allow doing this.

You just need to download the HLSL.xml file from his GoogleDrive account, and in Notepad ++ click Language->Define Your Language->Import and then select the downloaded file. After restarting notepad++, you´ll find a new entry in the Language menu item like this:


By clicking in that new item when you load an HLSL file, you´ll get the following result:


According to him, it supports even SM 5.0 profiles.

Great job Matt !!! :)