flame icon indicating copy to clipboard operation
flame copied to clipboard

Add Game.backgroundDecoration property

Open st-pasha opened this issue 3 years ago • 7 comments

Based on discussion in #1448, we would like to have the backgroundColor property of a Game to be dynamically changeable. One problem with that, however, is that right now backgroundColor is a method, not a property, and I don't think there is a way to convert a method into a property without making a breaking change.

So, instead of changing backgroundColor, I suggest to add a coexisting property backgroundDecoration, while deprecating backgroundColor method. The backgroundDecoration property will work like this:

  • by default it will return null;
  • if it returns an object, that object will be used as the decoration: property of the widget;
  • if it returns null, then we will call backgroundColor() and construct a decoration box out of that color;
  • the backgroundColor() method will be deprecated.

Note that backgroundDecoration allows more complicated backgrounds than simple color. For example, you can implement gradient colors through it.

st-pasha avatar Mar 14 '22 16:03 st-pasha

How can it be dynamically changeable? Since both color and decoration are set on the widget and we can't refresh the widget? I think if you'd want to change it mid-game it is better that the user does it at a widget level with state management than from inside of the game.

spydon avatar Mar 14 '22 16:03 spydon

You can refresh the widget, since it's stateful.

I mean, the game has the ability to make the widget refresh itself.

st-pasha avatar Mar 14 '22 17:03 st-pasha

You can refresh the widget, since it's stateful.

I mean, the game has the ability to make the widget refresh itself.

I don't think we want to do that though, the state of the widget should lay on the Flutter side, not inside of the game. And if you initialize your game in the widget then your game will restart when you change the background color.

spydon avatar Mar 14 '22 17:03 spydon

I don't think we want to do that though, the state of the widget should lay on the Flutter side, not inside of the game.

The state of the widget belongs to the GameWidget class, and Game has a means of communicating with its widget. There is nothing new here, we're already doing that.

And if you initialize your game in the widget then your game will restart when you change the background color.

The game is a final property inside the GameWidget, which means it is impossible to "restart" it from within the GameWidget. Not that we wanted to.

st-pasha avatar Mar 14 '22 21:03 st-pasha

Weird idea, but can we not just do a canvas.drawRect(Vector2.zero() & size, Paint()..color = backgroundColor()) (with some caching etc) at the start of our render pipeline? I mean even Flutter eventually uses some form of canvas drawing to draw the background color of a widget, why should we let a widget do that for us if we can provide it "easily" ourselves?

wolfenrain avatar Mar 14 '22 21:03 wolfenrain

The state of the widget belongs to the GameWidget class, and Game has a means of communicating with its widget. There is nothing new here, we're already doing that.

You're right, saw that we are handling the mouseCursor in the same way as this would be done. Then I think this is a better idea (if we'd want this feature) than to draw a rectangle on the canvas manually.

spydon avatar Mar 14 '22 23:03 spydon

I mean even Flutter eventually uses some form of canvas drawing to draw the background color of a widget, why should we let a widget do that for us if we can provide it "easily" ourselves?

I think Flutter would cache the rendered background so that it wouldn't need to be re-drawn on every tick.

st-pasha avatar Mar 15 '22 20:03 st-pasha