문제

After spending a fair amount of time with Direct2D (1_1), I am looking for information on creating standalone controls with Direct2D, primarily with hit-testing and abstracting layout.

Creating an entire scene is simple enough (special thanks to Kenny Kerr), however I have not found a good strategy for separating code into reusable UI components.

Ultimately I would also like to provide wrappers to these controls that would also be able to be used in WPF. Thanks for your help.

[EDIT Feb 19 - 7AM]

Consider this very simple example: The Render (or Draw) function renders a few rounded rect geometries, some of which overlap. Now let's treat each of these rounded rects as an independent ui control. So we abstract that rounded rect into its own class, provide a render function which has a parameter of a render target and a point at which to draw. But there is a good deal more to consider. The control shouldn't necessarily know where it is located. And a generic hit testing strategy that our system can continue to build on would be important.

As @CodeAngry pointed out, properties like visible, enabled, zOrder, width, height, etc should be implemented. But the layout system should be our starting point.

None of this is incredibly difficult, however I know this stuff is done everyday, and I was hoping to find some pattern or strategy to consider. Thanks again.

도움이 되었습니까?

해결책

The generic hit testing can be done by doing something like this:

  • each control should have a geometry of it's layout. Either a basic rectangle, ellipse, irregular shape. Then, the control manager does hit testing against them. Then orders possible matches by zorder
  • just hit-tests against them in zorder to prevent the sorting step.

You should store control hierarchy zordered anyways, and this, by itself, skips the sorting.

Controls don't need to know about their location but NEED to know their sizes (if rectangular) and/or shapes. And the manager knows the location and a bounding rectangle. Combine these elements and you can the hit-test. First make sure the point falls in the bounding rectangle and only then hit-test against the control's geometry. Before hit-testing, update the HWND client point to the control client point by subtracting the control client offsets known by the control manager.

That's how I'm (planning to) doing it. But I'm making it windowsless. If each control is in a child HWND of its own, life is so much easier!

PS: You can mail me if you want to exchange ideas. You can find my email if you want to. ;)

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top