Passing the current mouse position to a ViewModel? - c#

In my MVVM application, I have a Direct3d render window that shows a bunch of 3d meshes in a scene. In that render window, I want to be able to click on one of those 3d meshes in the scene and move it around, having it follow the mouse cursor. This is typical 3d editor stuff, moving a mesh along in screen space. So I need to be able to get the current mouse position, preferably relative to that Direct3d render window.
What's a method to do that?
Thanks!
Edit: Changing the wording since it was too generic and led to confusion.

InputManager.Current.PrimaryMouseDevice is probably your best bet.
Wrap it up in an interface that exposes the info you need and inject it using your favorite IoC framework.

I think this is the wrong way to think about the MVVM pattern. Let me illustrate this by changing your question slightly.
If you have some custom text editing control, what would be the best way to pass keystroke events to the ViewModel?
Wouldn't it be better to bind the the data to the control and then update the data through the binding as the control is manipulated?
So, you have a list of objects you want to show in a 3D view? Pass the objects as a they are, and use template binding to bind each object type to a DataTemplate describing the 3D object, which includes bindings to the X,Y,Z items in the object.

I would declare a delegate on the viewmodel and make the view register to it. this way the viewModel doesn't need to know about the view and is easy to unit test.

Related

How to make custom shaped panel in WPF?

I am facing a problem that have kind of already been asked here before, as far as I have seen.
I read most of these but didn't find the right answer.
This is what I need:
Complex shaped zone (i.e: countries, states..)
Having possibility to get events on it (especially mouse's)
Possibility to place some conrols in it (i.e: images, buttons)
I saw that some uses usercontrols, controltemplates.. But how should I do for that kind of shape ? Most of questions was for some basic mix of standard shapes.
(I saw with Blend that we can make path object with a pen, is there a way to use this to define the shape of a zone ?)
Thanks.
Create your own control and have its main container be a Canvas. Have the canvas background be transparent and then you can place all the controls you want on it. You can use a Polygon as your main shape inside the Canvas and place all the controls you want on the Canvas (but I guess for your purpose, you would want to make sure to not place them outside of the area covered by the polygon.
Let each object you use handle the mousedown event then you can individually drag them. When mousedown occurs, use the CaptureMouse() method on the object you clicked so that all dragging (on or off of the main window) will still be captured by the object you clicked. Make sure to call ReleaseMouse() in the mouseup event.
You can create as many instances of your control as you need. All with different shapes defined by the polygon each one contains. You can slap all of these objects on a single grid or canvas, and you're good to go.
You can use the PathPanel class that is provided in the Expression Blend SDK.
More information here

A tree-like, manipulable control for manifold hierarchical data

I am new to C# and WPF and I need to create a diagramming control similar to MS Visio but without direct user interaction. I have tree-like hierarchical data whose nodes can be of the same type. Quite like graphs whose vertices themselves contain graphs (i.e. vertices are subgraphs). I cannot use the Graph# library for several reasons.
The main requirements for my (customized) control are:
free moveable/draggable items (which represent hierarchical data)
items should have a box layout
items can be connected by arrows
Unfortunately, I don't know how to start. I tried several approaches like nesting ListBoxes but each approach had disadvantages. What do you think about nesting TreeViews and templating them? I need something like a Canvas container to freely position my items.
Do you have any suggestions? Thank you very much in advance.
If you need free movable/draggable items you probably need a degree of flexibility that built-in controls don't really provide. You're better off constructing your own controls, perhaps deriving from existing controls that provide close-enough functionality.
So for starters you'd have a control for a box item and a control for an arrow. The controls need to be movable and draggable inside a container, so you'll also need a container, probably derived from Canvas.
What I'd really recommend is to have a look at Model-View-ViewModel techniques; they can be daunting for a beginner but the benefits are immense. In this case, you'd have a collection of objects that represent your structure; all the objects will have, for starters, X and Y properties that define their coordinates. The objects are represented on the page by the previously mentioned controls, with bindings from their relevant properties to the properties of the objects. The objects are the ViewModel, and the controls represent the View.
Then, when you move the items with the mouse (the code for that is pretty easy), the properties of both the controls and the objects are automatically updated. More properties for the objects can include size, labels, and a collection of snap points that represent where arrows can connect to the object.
Arrows can be represented with the same concepts, except that instead of explicit X and Y coordinates they would have associated objects and snap points; then their coordinates would be automatically calculated based on the positions of the snap points.
Based on these structures, you can do actions exclusively from the ViewModel, like adding new items automatically connected to their parents, and have the view update based on that.
This is quite a broad topic so I really should stop now; I don't even what articles to recommend to get you started. Probably anything by Josh Smith on the topic of MVVM :P

How can I design a curve editor that works well with the WPF model?

I have a data model that represents segments of a curve that looks like this:
Envelope curve http://img710.imageshack.us/img710/9059/envelope.png
The fields are x and y coordinates of the anchor points (red), and a slope value represented by the control points (green). For the curve to be editable by the user, I'm thinking of using Thumb objects for both anchor and control points.
Since I also would like to be able to present the user with an interface where it is possible to enter coordinates into text boxes, I'm thinking of implementing this curve editor as a lookless control. The problem I face is to find the best way to organise the elements. Some thoughts on what I could do:
Add the data model view objects to an ItemsContainer with a custom ItemsPanel and a DataTemplate that creates one anchor point and one control point for each data model object. This causes two and two points to be wrapped in a ContentPresenter which makes the interaction between the custom panel and the thumb points difficult.
Use two different data model view objects, one for anchor points and one for control points, and a DataTemplateSelector to select the right DataTemplate. While there still is a ContentPresenter between the item and the panel (is it possible to avoid this?), interaction may be easier when there is only one item in the ContentPresenter.
Don't use ItemsContainer at all, instead add the Thumb objects directly to the visual and logical trees of the custom panel. Of course I loose a lot of flexibility when it comes to the look and feel of the Thumb objects when they are not specified by templates. Maybe it's possible to duplicate this functionality from ItemsContainer so my control can have separate DataTemplates for the anchor and control points. The Thumb objects could then be added directly to the panel without ContentPresenters.
What's the most reasonable way to do this? Are there other alternatives than the ones I've thought of?

c#: Choosing or modifying a control to create draggable graphics on a canvas

I'm attempting to write a small app that will do something similar to what is shown here:
http://www.soccertutor.com/tour/MASTER_SKIN_team.swf
However I've not used WPF/Win Forms very much at all and can't seem to find a control that will be most applicable to the situation. I want to have some sort of grid for the pitch so I can have players, cones, other items snap to certain parts of the grid and I would like the players to be draggable onto the various grid squares.
Thanks very much, I look forward to hearing your recommendations/suggestions!
You don't use controls for something like this. You use objects in your code that represent a visible item on the screen. Periodically you render an update to the screen. You draw the background, then each object. You add mouse hit testing to make the scene interactive to the user. The main game loop calculates new object positions, implements object collision detection and game rules.

Building a MVVM 3D Editor Application -> Getting Mouse Position?

In my 3d editor application, I want to be able to click on and move a scene's entities (meshes, lights, cameras, etc). To do that, I imagine that I need to get the current mouse position relative to my Direct3d view and somehow give it to my ViewModel. How would I go about doing that, considering that my app is built with the MVVM pattern?
Additional Info: The Direct3d renderer is from a C++ DLL and we're using an HwndHost to place it inside our WPF app. It has a WndProc which I imagine I could use to handle mouse messages there but I'd rather avoid it if at all possible.
Thanks!
The best solution I found is to use an attached behavior. In the control that serves as the placeholder to the HwndHost I set an attached property, like
b:MouseBehavior.LeftClick="{Binding DoSomeActionCommand}"
where MouseBehavior receives the control and sets mouse events on it.
It works well for me.
InputManager.Current.PrimaryMouseDevice is probably your best bet.
you can get the mouse position with
Mouse.Position( RelativeToObject ); // get the mouse pos relative to an object
Mouse.Position( null ); // get the mouse pos relative to the whole screen
if thats not good enough, you can use Win32 calls.

Categories

Resources