views:

87

answers:

4

The application that I'm working on is going to be used to create charts of data contained in a database. Right now objects on the chart are manipulated using a "control panel" - essentially a list of objects and a PropertyGrid to edit values. The users would also like to be able to interact with the objects using mouse interactions - things like grabbing the corner of the chart and dragging to expand/contract it, clicking on a number and getting a text box to edit it, or right clicking on something to get a menu of possible interactions. The chart is being drawn with GDI+ on a metafile (a requirement) which is then drawn on a user-drawn form.

I'm not really sure how to implement this. I've had a couple ideas:

  • Create some custom controls that get overlaid on the chart graphic. Each control could be associated with a specific object or property of an object on a chart, and would update those values depending on how the user interacted with it.
  • Just keep track of where objects are located, and when a user does something with the mouse, run through the list and figure out which object is supposed to be at the mouse location, and go from there.

I'm interested in how you guys would implement this and would really appreciate some suggestions. Thanks!

+2  A: 

The latter - for instance, if it's a pie chart, you're going to have to do most the hit testing work anyway to deal with irregular shaped controls.

Pete Kirkham
+1 for doing hit testing yourself, this is definitely the way to go. But cemkalyoncu is right that if you kept a bounding box in addition to the actual coordinates of the object, then you could quickly reject objects and narrow your search scope.
Daniel Pryden
A: 

Easiest: have a list of objects and their bounding boxes. When mouse event is made check the list for which object is clicked.

Cem Kalyoncu
+1  A: 

A good method for hit detection: have another off-screen image. draw every clickable object on this image with a unique color. You have to disable anti-aliasing. When user clicks get the color at that point from the off-screen image and determine the object. If you have a list of objects you can use object index as color. This method will handle hit detection of irregular shaped objects but will be a bit slower.

PS. Using controls will be slower than this.

Cem Kalyoncu
Interesting. This will be more memory intensive than recalculating the shape if you have only a few large shapes, but if you have many irregularly-shaped small objects, it may just prove to be a win.
Daniel Pryden
Thinking of memory you can surely use palleted image to reduce memory usage to a factor of 4, unless you have more than 256 objects in the workspace.
Cem Kalyoncu
This is pretty well how object selection is done in OpenGL
Pete Kirkham
Yep, adapted from there :)
Cem Kalyoncu
A: 

The former - if you have it overlaid on a form, then all the hit testing will be done for you by the forms framework. You simply need to create some controls, and then implement event handlers for them.

The above also describes any windows forms (or probably MPF for that matter) program :)

Creating your own list of objects etc. is tantamount to re-implementing the windows form framework, or at least a substantial part of it. You don't want to reinvent the wheel, especially since you have wheels already in your application.

Larry Watanabe