C# cross-thread SendToBack/BringToFront operations on controls - c#

I am developing a card game. I have represented cards with PictureBox class. I have a game where players put cards one on top of another, one by one. Z-index of all these cards are different and when card is put on top i give it top z-index with this code:
PictureBox cardPictureBox = move.Card.CardPictureBox;
if (cardPictureBox.InvokeRequired)
cardPictureBox.Invoke(new MethodInvoker(cardPictureBox.BringToFront));
This code works fine and all cards are brought to front when they supposed to. I have a problem when this game ends. That is when I need to put PictureBox objects back to their original place. I put them back but they are now not ordered as I ordered them in Designer. I have to rearrange their z-indexes. I do this with the same code, but in a loop where I do the same thing for every card.
Program doesn't throw an exception but just freezes and that's it!?!?!
Did someone encounter this problem, and does it has an answer?
How can I make a safe cross-thread change of PictureBox z-index without my program freezing?
Thanks in advance

Simplest way would be to skip the invoke when you're doing them all in a loop at the end:
foreach (CardPictureBox cardPB in Collection)
{
cardPB.BringToFront();
}

Related

Tangram puzzle application

I am trying to create a WPF application using C# to run on Pixelsense that is basic version of the tangram puzzle. I am able to draw my 7 shapes and translate and rotate them all around the screen.
Could anyone give me advise regarding how I should go about saving the pattern (with shapes in specific positions and orientations) so that when a user creates the pattern next time, the application can match it to the saved one and tell the user if it's correct.
It's a pattern matching and recognition problem that I am trying to solve.
I have been stuck on this for a while now :(
Define the solution as a collection of objects with shapeType, position, and orientation properties. Have the solution include one shape at position 0, 0 and an orientation of 0. Now loop over all the shapes the user has actually placed to find the ones with a shapeType that matches the shape your solution has at 0,0,0. Calculate the position and orientation of every other shape relative to where the user put this one. Compare those values to the rest of your solution. You'll need to experiment with how much tolerance to allow because this stuff is not precise - to make the game fun, err on the side of having high tolerances. If needed, you can follow this up with some performance optimizations to only re-evaluate pieces that moved.
Hopefully you are using physical shape prices with tags on them instead of this purely a virtual game. I always wanted to build this when I was on the Surface team but it never happened. One challenge you will run into is defining how the tag's position/orientation relates to the actual shape. If you'll be putting tag stickers on multiple tangram sets, you almost certainly won't get the on precisely the same each time so you may need to add a "calibration" mode to your app (have the user place each piece in a specific spot and then push a button so you can record where the tag is relative to those spots). The TagVisualizer WPF control should help a lot for building your UI - definitely look into using it (this scenario was top of mind when we designed that API). The default behavior of that control (if you tell it the ID of a tag to look for but not how to visualize it) is a "crosshair" that can help you find tune your offset values.
Good luck! If you wouldn't mind recording a YouTube video when you are done and posting a comment here linking to it, I'd really appreciate that
You can use ObservableCollection or List of a custom class. That class can consist of various values such as position, orientation etc as properties.
When a new pattern is drawn or when the pattern change its position you can update that particular object stored in the collection. As you have all the details of the pattern(positions and orientation) you can iterate the for loop and check the position of the new pattern when added.

How do I add a new image to the window of an already running game in XNA C# using an if statement in the Update method?

I am pretty sure something like this has been asked before, but I haven't been able to find it. Anyway, I am making a simple game animation with two moving objects (halloween themed). The objects bounce off the walls when they hit them, but I also need an image to be displayed when the two objects hit eachother. I have tried multiple ways, but none of them work. They either have no effect or raise an error. Anyway, here is the last thing I tried:
public PumpkinCheckCollisionPumpkin(GameTime gameTime)
{
if (pumpkin1.BoundingBox.Intersects(pumpkin2.BoundingBox))
{
pumpkinCollide = True;
Draw(gameTime);
}
I then tried passing the that bool (which I set to false earlier) to the draw section, but it did not work. The above method is called within the Update method.
I tried having the draw method invoked in the Update (GameTime gameTime) part, but that didn't work either. How do I trigger another image to be displayed in addition to what is already is displayed when my two objects collide? (also, that public method was originally private but made it public so another tactic I tried might work(didn't work)).
The answer is pretty simple, never directly call the Draw function.
In this case, I would set a class level state variable that holds the fact that a collision graphic should be drawn on the next draw call, which can then happen.
You erase every time you draw anyways, so even if the call did work correctly, the next Draw call (which will happen really soon) would erase it. For the same reason, don't unset the flag after the Draw call happens, because it will get erased faster than your players can see it. You need to keep it on the screen for some time.
You could do this by setting a variable that holds the time when the flag was set, then checking it against the current time until enough "visible" time has passed. This would then clear the "show collision" flag, and the graphic would no longer draw.
Just let the framework handle the timing of that call, and let the state you set drive what is drawn.

WPF / C#: async load/buffer next movie clip

I need a main 'home' video (full screen HD) running in a loop, and then at some point (say the user presses a key) another short movie plays (one of twelve selected at random), and then back to the home movie loop.
I'm using the mediaElement in WPF from my C# code - Is there a way to load the next movie into memory so that it's ready to play instantly? It currently takes about a second or so...
(I'm actually using two media elements with the triggered movie on top - I was doing a cross fade but have taken that out now until I can buffer the next clip. Not sure if this is the best way of doing this, any ideas? Many Thanks.
I figured out a way in case anyone is interested.
You can just specify a bunch of mediaElements on top of each other (can have different z index but not essential). Then did
video.Opacity = 0.0;
video.Play();
video.Pause();
for all of them, then when you want to play one you just set the opacity to 1 and play it and it'll play instantly.
Don't know if that's the best way, but works well. Now going to implement a cross fade to finish up. Happy days.

Drawing a map based on an array, and applying run time changes

I am writing a program to control a mobile robot. One of the things it has to do is to draw a map of the "world" as the robot senses it and apply changes in the map as it senses them.
It also has to highlight in some way the location of the robot and (ideally) the direction it points to.
I currently have an auto-updating array (100 x150) representing the map, using the following representations: 0 represents a clear path, 1 represents an obstacle.
I also have a variable containing the robot's location and next location.
What I need is to visualize it. I thought about using labels, but it is way too tedious using them, and the end result is not so pretty. My other possibility is to write all that data into an Excel spreadsheet, but then I will be back to square one: visualizing the data in an attractive way. Is there a drawing package of some sort that can do the job?
To sum it up:
Using:
- int[] MapArray //100 x 150 array representing the robot's world, and the data there is changing.
- Point[] Locations //Locations[0] is the current location, Locations[1] is the next step.
And I want to draw a map on a Windows Forms application that updates itself in a nice visual manner.
I hope my question is clear enough, don't hesitate to contact me if not!
Try Code: Drawing a Filled Rectangle on a Form (Visual C#) or something similar.
Graphics Programming Example Topics
Just write a couple of cycles which accesses every cell of an array and draws a rectangle on a form according to coords - that's the simplest way.
for(int i=0; i<100; i++)
{
for(int j=0;j<150;j++)
{
<access[i,j] and draw a rectangle with color accordingly to your contents.>
}
}
The same for the drawing location.
If you don't find any nice controls, you can always use a DataGridView with the column width = row height so that it always displays square cells. After that, just change the background color of the obstacles.
You could also look into observable collections for the map array so that the grid updates based on events rather than on timers. Make sure that you overwrite the observable collection to send the CollectionChanged event even when a cell changes its value, not only when adding/removing cells.

Designing a CAD application

I am designing a CAD application using a variation of MVC architecture. My model and view are independent of each other. They communicate through the controller. My problem is if I need to draw an object (say line or polyline) I need a number of input points. What would be the best way to get the points? All the events from the view are subscribed by the controller and controller has to keep the points, then generate the line or polyline and finally add this line to view. But I dont know how capturing the mouse points be done efficiently, because each object will have different number of inputs and different algorithms of input validations.
Any help would be highly appreciated.
I was working in a CAD application 3 years ago, and these are some tips I remembered we have done (BTW: the application is free, you can download it, register your copy, and make use of the features in the Truss Editor):
1- You may add buttons for shape drawing, example: a button for a line, a button for a polyline, a rectangle, ...etc.
2- Create a variable that holds the current state your application (may be an enum): ready, drawing point, drawing line, drawing polyline, drawing circle, ...etc.
3- Wherever the user clicks a drawing button, the system enters a relevant state from those mentioned above.
4- The system returns to the "ready mode" when finished drawing, which can be detected automatically by the expected number of points (1 for point, 2 for line, 3 for ellipse, ...etc) or when the user presses Esc or right-clicked the drawing area (if the expected number of points is unknown, example: polyline). You may also end polyline drawing if the user re-clicked the first point and he has drawn 3+ points.
5- The system may cancel current drawing operation if the user ends the operation before completing the number of expected points.
...
when designing a CAD software, you must not only think of flexibility and dynamic, but also of speed. You should use some kind of wrapper class that works as a very thin layer between you and the hardware driver, it should return stuff like the pixel array of the screen, the current bpp, etc... This is how I would do it (and did actually). Now in C#, seeing as it is a .NET language, I'm not sure you can go that below, but you can still have someking of handler between the controller and your pen object, can't you?

Categories

Resources