0
\$\begingroup\$

I have three types of UI buttons that go on a cooldown bar:

  1. A traditional cooldown bar button where once the corresponding key is pressed, a timer shows over the button and dims the texture.
  2. A button that shows a cooldown, doesn't dim and adds stacks counting upwards to a max (say 5) in the upper left
  3. A button that acts like a toggle with no timer, but dims depending on if it's on or off

These all inherit from my AbilityButton class. I'd like to put all three Draw() functions in AbilityButton like:

public virtual DrawNormal() {...stuff...} 
public virtual DrawStackable() {...stuff...}
public virtual DrawToggle() {...stuff...}

... and tell the children to implement the correct one. The problem is that I don't think it's possible to just tell my children to use one of those. From what I've seen, I'd need to make an instance of the parent in each child and call the corresponding method. This just feels weird and wrong - like I'm designing incorrectly to land in this position in the first place.

Is there a better way to do this? Is this better suited for interfaces? One interface per button type? The problem with interfaces is that I want to actually define the method functionality in the parent which interfaces do not allow.

\$\endgroup\$
1
\$\begingroup\$

If you distinguish these button types by making derived classes for each, I would have the AbilityButton class have an abstract Draw() function and each of the derived classes would then implement the draw function their way. Then you would be able to have a list of AbilityButtons and call their Draw() method, not caring if it is one or the other from the calling code.

And yes, this could even be abstracted to an interface Drawable if you have more classes that should be able to be drawn. Then just have AbilityButton "inherit" from it, without implementing it, which would force the subclasses to do so. (Remember to make your AbilityButton abstract if it shouldn't be instansiable in itself)

| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.