This is part 4 of a multi-part series. You can find the previous posts here: part1, part2, part3.
Now that we have created our guessing game designer, it is time to use it to build applications.
Instead of opening our GuessingGame1 solution in Visual Studio 2005 and then selecting run without debugging, we can now open our new solution directly. From the Start Menu, run "Visual Studio 2005 Experimental". From the start page, select the GuessingGame1Debugging project. This is the Visual Studio solution, created in our previous steps, which developers will use to create guessing games. To recap, we have written a tool which created a new tool. This new tool is the graphical designer associated with the .gg1 file type. The GuessingGame1Debugging project already has everything mapped correctly so that, when we open it in Visual Studio 2005 Experimental, we have access to our new designer.
A couple of templates have already been created for us. In order to keep things simple, go ahead and delete them. They are:
CREATING A TEMPLATE
To create a template, we need to add a new file to the project. Right click on the GuessingGame1Debugging project and select "Add New Item". Choose text file and set the name to GuessingGame. This will create a file called GuessingGame.txt in your project. Copy and paste the code from here into the text file.
In a future post, I will go over the structure of the template file in more detail. For now, we will focus on the extension and fileName attributes. The extension attributes indicates that when this template is transformed, the output file name will have .txt replaced with .cs. The fileName attribute tells where the input for this template will come from. In this case, it is Game.gg1.
In order for Visual Studio to know that this is a template, and not just a regular text file, we need to set the custom tool property on GuessingGame.txt. Set it to "TextTemplatingFileGenerator".
DESIGNING A GAME
To design a new game, we need to add a new .gg1 file. Right click on the GuessingGame1Debugging project and select "Add New Item". Along with all of the usual choices, you will notice a new item type of "GuessingGame1". Choose this type and set the name of the file to Game. This will add a file named Game.gg1 to your project. When you open this file, you should see our shapes and connectors in the toolbox. Drag a SetUp shape and a couple of Hint or Guess shapes onto the design surface. Also, connect SetUp to one of the Hint or Guess shapes using the FirstMove connector.
RUNNNG THE CODE
The final step is to change the properties on the project itself. We need to set Output Type (in the Applicaton Tab) to Console Application. This is because the .cs file which will be generated from our template is, in fact, a console application.
As with the DSL tools themselves, building our application is a two step process. First, we press the transform all templates button to create GuessingGame.cs from GuessingGame.txt. Then we build the solution. Once the build has completed successfully, we can run our simple application. You should see the name of the first move in the game written out to the console. Press any key to terminate the application.
Try changing the desing of Game.gg1 and rebuilding the application to make sure it does what you expect.