0

I have declared an ISpaceObject interface in my 4X game.

I also have declared the abstract Vehicle and StellarObject classes that implement that interface.

Each of those abstract classes has subclasses inheriting from it (e.g. Ship, Base, Fighter for Vehicl and Planet, Star, Storm for StellarObject).

I want to be able to display icon and portrait pictures for each type of object.

I have implemented graphics code (e.g. an Icon property) in the individual classes, but it's specific to WinForms and I want to write a WPF port of my app.

I don't want to have a WinFormsIcon, WpfIcon, etc. property in each of my classes; that would be kind of silly, and I'm sure putting the WinForms code in there in the first place was a mistake.

How do I do parallel behaviors like this without resorting to code like the following:

if (o is Ship)
    // get ship graphic
else if (o is Base)
    // get base graphic
else if (o is Fighter)
    // get fighter graphic

etc?

umlcat
  • 4,091
  • 3
  • 19
  • 29
ekolis
  • 6,270
  • 12
  • 50
  • 101
  • Is the Icon property part of the ISpaceObject interface? – Yacoub Massad Sep 08 '15 at 19:52
  • 2
    Can you please provide some code for your types? – Yacoub Massad Sep 08 '15 at 19:57
  • Sorry but I didn't get what you want. You don't want to port your code to wpf, you want to keep things as they are in Windows forms? – Thiago Custodio Sep 08 '15 at 20:00
  • I think the usual pattern for this sort of problem is Traits http://stackoverflow.com/questions/10729230 – dtanders Sep 08 '15 at 20:06
  • @ekolis Can you show how you are managing the WinFormsIcon so far in any of the class? That would help understanding the current situation and providing you the solution. – vendettamit Sep 08 '15 at 20:14
  • I second the need for type defs to be posted. – Enigmativity Sep 09 '15 at 03:06
  • @Yacoub Massad The Icon property is part of an interface which ISpaceObject inherits from. – ekolis Sep 09 '15 at 12:31
  • @Thiago Custudio The WinForms icons are implemented as properties; they're computed in a variety of ways depending on the type of object. For instance, for a ship, it depends on the ship's hull and the empire which designed it, while for a planet it depends on the planet's size, atmosphere, and surface. – ekolis Sep 09 '15 at 12:34
  • @dtanders: Hmm, traits? So I'd need a WpfShip class inheriting from Ship which adds on the extra functionality? That seems a bit weird... I suppose I could also put the icons in a view model, and have a view model for each class that implements ISpaceObject, but that seems like it would be a pain... – ekolis Sep 09 '15 at 12:40
  • One thing I've been wanting to do, by the way, is to simplify some of these hierarchies. I'd like to be able to let game modders create custom vehicle types by setting flags and abilities, like "can warp between star systems" and "can go into other vehicles' cargo". So instead of having a bunch of hardcoded vehicle types, I'd like to have those defined in a data file, and fortunately that would make this a lot simpler! – ekolis Sep 09 '15 at 12:44
  • @ekolis yeah, the language isn't really set up to allow for it easily. If you look at the SO question I linked to, you'd "just" need to add interfaces and remember to use extension methods all over the place. I suppose you could also get fancy with Roslyn extensions, but I haven't looked into it. There's also https://remix.codeplex.com/ (mixins and traits are sort-of synonyms). Basically, there are options that don't involve making inheritance spaghetti. – dtanders Sep 09 '15 at 18:08

1 Answers1

0

You could change the property to a string IconName instead.

This name could point to a embedded resource in a resource assembly.

Or you could convert the Bitmap (which I assume is what you have in your WinForms project) to an ImageSource using System.Windows.Interop.Imaging.

Troels Larsen
  • 4,462
  • 2
  • 34
  • 54