-
-
Notifications
You must be signed in to change notification settings - Fork 949
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Component tree for the devtools extension tab (#3094)
This PR creates a devtools extension tab for Flame where you can: * Turn on `debugMode` for a chosen component or the whole tree * See the component tree * Play, pause and step the game loop. [Screencast from 2024-03-24 22-09-55.webm](https://github.com/flame-engine/flame/assets/744771/82217afb-e096-4375-a892-b59e1be6d7ec)
- Loading branch information
Showing
14 changed files
with
407 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
!extension/**/* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export 'src/devtools/connectors/component_tree_connector.dart' | ||
show ComponentTreeNode; |
63 changes: 63 additions & 0 deletions
63
packages/flame/lib/src/devtools/connectors/component_tree_connector.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import 'dart:convert'; | ||
import 'dart:developer'; | ||
|
||
import 'package:flame/components.dart'; | ||
import 'package:flame/src/devtools/dev_tools_connector.dart'; | ||
|
||
/// The [ComponentTreeConnector] is responsible for reporting the component | ||
/// tree of the game to the devtools extension. | ||
class ComponentTreeConnector extends DevToolsConnector { | ||
@override | ||
void init() { | ||
// Get the component tree of the game. | ||
registerExtension( | ||
'ext.flame_devtools.getComponentTree', | ||
(method, parameters) async { | ||
final componentTree = ComponentTreeNode.fromComponent(game); | ||
|
||
return ServiceExtensionResponse.result( | ||
json.encode({ | ||
'component_tree': componentTree.toJson(), | ||
}), | ||
); | ||
}, | ||
); | ||
} | ||
} | ||
|
||
/// This should only be used internally by the devtools extension. | ||
class ComponentTreeNode { | ||
final int id; | ||
final String name; | ||
final String toStringText; | ||
final List<ComponentTreeNode> children; | ||
|
||
ComponentTreeNode(this.id, this.name, this.toStringText, this.children); | ||
|
||
ComponentTreeNode.fromComponent(Component component) | ||
: this( | ||
component.hashCode, | ||
component.runtimeType.toString(), | ||
component.toString(), | ||
component.children.map(ComponentTreeNode.fromComponent).toList(), | ||
); | ||
|
||
ComponentTreeNode.fromJson(Map<String, dynamic> json) | ||
: this( | ||
json['id'] as int, | ||
json['name'] as String, | ||
json['toString'] as String, | ||
(json['children'] as List) | ||
.map((e) => ComponentTreeNode.fromJson(e as Map<String, dynamic>)) | ||
.toList(), | ||
); | ||
|
||
Map<String, dynamic> toJson() { | ||
return { | ||
'id': id, | ||
'name': name, | ||
'toString': toStringText, | ||
'children': children.map((e) => e.toJson()).toList(), | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.