Click here to Skip to main content
15,886,026 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

Last year I created a framework for developing chess/checker-like games in WinForms, in MVC style. I am now trying to move this framework to UWP/XAML (close to WPF). I have some experience with XAML, but wonder what the best way to create the UI would be.

In my WinForms project I had a 2D array of PictureBoxes, which the player could move around using the built-in drag'n'drop functionality.

In XAML there are no PictureBoxes so I need to come up with another approach. One idea is to have a Grid with a number of cells/panels/similiar. Each "cell" should ideally have a background color (ie black or white) and then possibly an image of the piece in question (eg king or pawn). The images/cells/panels/... then need to be draggable, so that the pieces can be moved over the checkboard. Note that the checkboard can have different lenghts, such as 4*9 squares, and that the lengths are set in a Constants class. In other words, the Grid/similar must have its number of rows/columns set in codebehind.

Any ideas of how I should do this? I don't need the math, just what controls to use and perhaps how to create them dynamically in codebehind and to be able to perform drag'n'drop.

Many thanks!

Petter
Posted
Updated 28-Dec-15 21:51pm
v2
Comments
Sergey Alexandrovich Kryukov 28-Dec-15 20:44pm    
UWP is closer to WPF. WPF-like development platform with XAML which can be considered as one of the interfaces to UWP.
There is no need to sorry about the lack of PictureBox. This control is totally redundant even in System.Windows.Forms. You could always develop same thing by yourself, unlike many other controls, which are based on Windows legacy controls and GDI+. There is nothing like that in WPF and in UWP.
Actually, the structures like a chess board/game can be easier represented in WOF or UWP than in in Forms. A lot more things is already done for you. Actually, a subset of XAML is ready-to-use full-fledged vector graphics. With Forms, you need to develop a lot more extra, to get to the same level, and Forms would achive poorer performance...
—SA
petter2012 29-Dec-15 3:48am    
Thanks, I agree and it was a bit o a pian working so graphically with WinForms anyway. So I agree with you but I still need hints on what to use. I rephrase the heading of my question to make it more attractive. :)
Sergey Alexandrovich Kryukov 29-Dec-15 12:15pm    
Very good. My answer is ready. I tried to describe 100% of what you need to create all the graphics and even suggested how to avoid drawing anything programmatically. You just put all elements and move them relative to the board element. Overall, it's fairly simple.
—SA
kendokumar 31-Dec-15 8:06am    
I have an doubt what is that SA , did you mean South Africa

1 solution

I can answer in detail just on WPF. I hope you can use these ideas for Universal Application as well.

For the game field control, use Canvas. This control allows to position UI elements at arbitrary positions, so they can overlap. You can simply put 8x8 elements of the type Rectangle to depict a board.

But I would suggest somewhat better alternative. The elements you put on Canvas themselves could be of the elements of the type Canvas, which allows for complex vector-graphics structure. So, you can draw the board with different decorative patterns, several alternative graphical designs for boards and figures, to give the users the design choice, and a lot more.

Draw these elements in some vector graphical editors. Not every editor will be useful, but only the one which can save graphics as XAML. I never saw anything better than open-source InkScape:
Inkscape - Wikipedia, the free encyclopedia[^],
Home | Inkscape[^].

With InkScape, you can create your graphical designs for the board and figures in SVG. Store it all in your Revision Control Database as SVG, because this is the most universal and compact data format. For importing this data to your project, export it to XAML. You will need separate files for the board and each chess figure. Besides, some implementations use highlight of the field of the board, in one-three designs showing different phases. If you plan to do it, two, also picture those separate fields, for "white" and "black" board fields.

After exporting to XAML, you will need to do a little extra work: remove each name attribute from all elements. One way to do so is using any text editor with Regular Expression replacement. Then you can copy all the XAML graphic files into one or more dictionaries, ResourceDictionary in your project. They you would need to give each design element (top-level Canvas). To allow each such Canvas element to work in ResourceDictionary, you would need to add the unique attribute Key to each (with default namespace prefix, it would be x:Key, where the namespace prefix "x" comes from the namespace definition xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml", which is generated automatically each time you add XAML from the item template in Visual Studio).

You need to take care of consistent scaling for all elements. Each figure should be of the size of the whole board field, with consistent padding between the figure and the field square boundaries. Use the same relative size for all figures and additional board fields. You don't need to care about the size of the whole board: vector graphic is arbitrarily scaled, and you need only the coordinates of all figures relative to the whole board when you position the figure (and those extra fields for highlighting). The position of each would be easily calculated as X * W/8, Y * H/8 relative to the board (I hope my notation is self-explained).

So, all the graphics of your game will be reduced to putting elements on top of the board Canvas at different positions, which is itself is put on the main instance of Canvas. Don't insert one canvas as the child of another one. All your XAML Canvas elements, including the image of the board, should be children on only one (transparent) top-level Canvas element.

One more step: make that top-level Canvas element a children of another element of the type Viewbox:
Viewbox Class (System.Windows.Controls)[^].

Make the whole application resizeable. Viewbox will automatically scale the whole graphics to the size of the outer container element.

The elements, children of Canvas, are moved relative to its parent using the functions Canvas.SetLeft and Canvas.SetTop:
Canvas.SetLeft Method (UIElement, Double) (System.Windows.Controls)[^],
Canvas.SetTop Method (UIElement, Double) (System.Windows.Controls)[^].

Also, you may need to learn WPF drag-n-drop. However, I would suggest to avoid it. Instead, the move could be done in two more logical phases. First, the player clicks on the figure. If it's a valid figure, it or its field (or both) is highlighted. (That's why I suggested highlighted elements; you may also need highlighted variants for all figures.) The the player clicks on the field to move. If this field is valid for a move, you move the figure and finalize the move. The move can be cancelled after figure selection, but not after the final field click. (I hope you are going to implement optional redo for the whole all moves of the game.)

But if you still want to implement drag-n-drop, you have to learn it, too: Drag and Drop Overview[^].

That's all I described nearly 100% of the graphics work for the game. I hope I can leave the game logic for your home exercise. :-)

[EDIT]

I almost forgot: a complete code sample for using SVG to XAML vector graphics in WPF application is demonstrated in my article Practical Sound Recorder with Sound Activation.

See "WindowAbout.xaml" in source code and the directory "Resource". Of course, resource dictionary can in in a separate file, and the graphic can be scale the way I explained above.

—SA
 
Share this answer
 
v2
Comments
petter2012 30-Dec-15 4:16am    
Wow - thank you so much for this thorough explanation! Yes, the game logic is already in place so this is exactly what I need. I actually use InkScape plus SVG export already for other vector-based stuff, but I hadn't realized it was this easy to do the game vector-based, so thanks a lot for this suggestion.

I am also used to Canvas from developing games in Silverlight using Andy Bealieu's Physics Helper (plus Farseer Physics) but have not used ViewBox, so thaht will be a new experience.

When it comes to drag'n'drop vs two-clicks, perhaps I can try to implement them both and then let the programmer decide what works best for the game in question (remember that I am creating a framework, not a game in itself).

Thanks again!

Petter
Sergey Alexandrovich Kryukov 30-Dec-15 11:37am    
Great. I see you are pretty close.
You are very welcome.
Good luck, best wishes in New Year, call again.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900