Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Adding ComponentNotifier API #1889

Merged
merged 13 commits into from Nov 26, 2022
Merged

feat: Adding ComponentNotifier API #1889

merged 13 commits into from Nov 26, 2022

Conversation

erickzanardo
Copy link
Member

@erickzanardo erickzanardo commented Sep 5, 2022

Description

This adds a proposal for a new API for flame, the ComponentNotifier.

This API offers the user change notifiers classes that are tied to FlameGame and its components so the user can be notified when a component is added, removed or updated.

This will enable users to:

  • Take the benefit of reactive programming inside the game
  • Have a simple way of watching certain states from the game, on Flutter Widgets

One important note here is that this proposal does not mean to replace integrations like flame_bloc, but rather provider an simple and out of the box solution, without any need of additional packages, since change notifiers are provided by flutter itself.

Opening this as draft for now to get feedback on the implementation, will write tests and docs once we have the final implementation.

Demo of the example:

Screen.Recording.2022-09-05.at.18.21.57.mov

Checklist

  • The title of my PR starts with a Conventional Commit prefix (fix:, feat:, docs: etc).
  • I have read the Contributor Guide and followed the process outlined for submitting PRs.
  • I have updated/added tests for ALL new/updated/fixed functionality.
  • I have updated/added relevant documentation in docs and added dartdoc comments with ///.
  • I have updated/added relevant examples in examples.

Breaking Change

  • Yes, this is a breaking change.
  • No, this is not a breaking change.

Related Issues

Copy link
Member

@spydon spydon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks very useful to me!

packages/flame/lib/src/components/component_notifier.dart Outdated Show resolved Hide resolved
@filiph
Copy link
Contributor

filiph commented Nov 17, 2022

I like this! Does this mean that I can do something like the following?

class Entity extends PositionComponent with Notifiable {

  Color _color = Color(0xffffffff);

  Color get color => _color;

  set color(Color value) {
    _color = value;
    notifyChanges();
  }
}

And then the UI can be like the following?

Widget build(context) {
  // I am probably butchering provider's API here, but hopefully you get the idea.
  final color = context.select<ComponentsNotifier<Entity>, Color>((e) => e.color);

  return Container(
    color: color,
    child: Text(entity.name),
  );
}

@erickzanardo
Copy link
Member Author

I like this! Does this mean that I can do something like the following?

class Entity extends PositionComponent with Notifiable {

  Color _color = Color(0xffffffff);

  Color get color => _color;

  set color(Color value) {
    _color = value;
    notifyChanges();
  }
}

And then the UI can be like the following?

Widget build(context) {
  // I am probably butchering provider's API here, but hopefully you get the idea.
  final color = context.select<ComponentsNotifier<Entity>, Color>((e) => e.color);

  return Container(
    color: color,
    child: Text(entity.name),
  );
}

Yes! Exactly

@erickzanardo erickzanardo marked this pull request as ready for review November 25, 2022 01:15
Copy link
Member

@spydon spydon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lgtm! It looks like a lot of comments, but most actually just regard one thing; that the name Notifiable should be changed (#1889 (comment)). :)

.github/.cspell/flame_dictionary.txt Outdated Show resolved Hide resolved
doc/flame/components.md Outdated Show resolved Hide resolved
doc/flame/components.md Outdated Show resolved Hide resolved
doc/flame/components.md Outdated Show resolved Hide resolved
doc/flame/components.md Outdated Show resolved Hide resolved
packages/flame/lib/src/components/mixins/notifiable.dart Outdated Show resolved Hide resolved
packages/flame/lib/src/components/mixins/notifiable.dart Outdated Show resolved Hide resolved
packages/flame/lib/src/components/mixins/notifiable.dart Outdated Show resolved Hide resolved
packages/flame/lib/src/components/mixins/notifiable.dart Outdated Show resolved Hide resolved
erickzanardo and others added 2 commits November 25, 2022 09:20
Co-authored-by: Lukas Klingsbo <me@lukas.fyi>
doc/flame/components.md Outdated Show resolved Hide resolved
packages/flame/lib/src/game/flame_game.dart Outdated Show resolved Hide resolved
packages/flame/lib/src/game/flame_game.dart Outdated Show resolved Hide resolved
Co-authored-by: Lukas Klingsbo <me@lukas.fyi>
@spydon spydon enabled auto-merge (squash) November 26, 2022 13:37
@spydon spydon merged commit bd7f51f into main Nov 26, 2022
@erickzanardo erickzanardo deleted the erick.component-notifier branch February 21, 2023 23:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants