mirror of
https://github.com/EsotericSoftware/spine-runtimes.git
synced 2026-03-26 22:49:01 +08:00
[flutter] Fix retention of drawable in SpineWidgetState, beginnings of debug renderer.
This commit is contained in:
parent
0b84fdbb32
commit
dbc2e164f0
26
spine-flutter/example/lib/debug_rendering.dart
Normal file
26
spine-flutter/example/lib/debug_rendering.dart
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:esotericsoftware_spine_flutter/spine_flutter.dart';
|
||||||
|
|
||||||
|
class DebugRendering extends StatelessWidget {
|
||||||
|
const DebugRendering({Key? key}) : super(key: key);
|
||||||
|
|
||||||
|
@override
|
||||||
|
Widget build(BuildContext context) {
|
||||||
|
reportLeaks();
|
||||||
|
|
||||||
|
const debugRenderer = DebugRenderer();
|
||||||
|
final controller = SpineWidgetController(
|
||||||
|
onInitialized: (controller) {
|
||||||
|
controller.animationState.setAnimationByName(0, "walk", true);
|
||||||
|
},
|
||||||
|
onAfterPaint: (controller, canvas, commands) {
|
||||||
|
debugRenderer.render(controller.drawable, canvas, commands);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
return Scaffold(
|
||||||
|
appBar: AppBar(title: const Text('Debug Renderer')),
|
||||||
|
body: SpineWidget.asset("assets/spineboy.atlas", "assets/spineboy-pro.skel", controller),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,5 +1,6 @@
|
|||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:esotericsoftware_spine_flutter/spine_flutter.dart';
|
import 'package:esotericsoftware_spine_flutter/spine_flutter.dart';
|
||||||
|
import 'package:spine_flutter_example/debug_rendering.dart';
|
||||||
|
|
||||||
import 'flame_example.dart';
|
import 'flame_example.dart';
|
||||||
import 'simple_animation.dart';
|
import 'simple_animation.dart';
|
||||||
@ -57,6 +58,18 @@ class ExampleSelector extends StatelessWidget {
|
|||||||
},
|
},
|
||||||
),
|
),
|
||||||
spacer,
|
spacer,
|
||||||
|
ElevatedButton(
|
||||||
|
child: const Text('Debug Rendering'),
|
||||||
|
onPressed: () {
|
||||||
|
Navigator.push(
|
||||||
|
context,
|
||||||
|
MaterialPageRoute<void>(
|
||||||
|
builder: (context) => DebugRendering(),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
},
|
||||||
|
),
|
||||||
|
spacer,
|
||||||
ElevatedButton(
|
ElevatedButton(
|
||||||
child: const Text('Skins'),
|
child: const Text('Skins'),
|
||||||
onPressed: () {
|
onPressed: () {
|
||||||
|
|||||||
@ -3,6 +3,7 @@ import 'dart:io';
|
|||||||
import 'dart:typed_data';
|
import 'dart:typed_data';
|
||||||
import 'dart:ui';
|
import 'dart:ui';
|
||||||
|
|
||||||
|
import 'package:flutter/material.dart' as material;
|
||||||
import 'package:flutter/rendering.dart' as rendering;
|
import 'package:flutter/rendering.dart' as rendering;
|
||||||
import 'package:flutter/services.dart';
|
import 'package:flutter/services.dart';
|
||||||
import 'package:http/http.dart' as http;
|
import 'package:http/http.dart' as http;
|
||||||
@ -3242,7 +3243,6 @@ class AnimationState {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// FIXME add callbacks for update, apply and updateWorldTransform. Pass through SpineWidgetController
|
|
||||||
class SkeletonDrawable {
|
class SkeletonDrawable {
|
||||||
final Atlas atlas;
|
final Atlas atlas;
|
||||||
final SkeletonData skeletonData;
|
final SkeletonData skeletonData;
|
||||||
@ -3321,19 +3321,23 @@ class SkeletonDrawable {
|
|||||||
class RenderCommand {
|
class RenderCommand {
|
||||||
late final Vertices vertices;
|
late final Vertices vertices;
|
||||||
late final int atlasPageIndex;
|
late final int atlasPageIndex;
|
||||||
|
late final Float32List positions;
|
||||||
|
late final Float32List uvs;
|
||||||
|
late final Int32List colors;
|
||||||
|
late final Uint16List indices;
|
||||||
|
|
||||||
RenderCommand._(spine_render_command nativeCmd, double pageWidth, double pageHeight) {
|
RenderCommand._(spine_render_command nativeCmd, double pageWidth, double pageHeight) {
|
||||||
atlasPageIndex = _bindings.spine_render_command_get_atlas_page(nativeCmd);
|
atlasPageIndex = _bindings.spine_render_command_get_atlas_page(nativeCmd);
|
||||||
int numVertices = _bindings.spine_render_command_get_num_vertices(nativeCmd);
|
int numVertices = _bindings.spine_render_command_get_num_vertices(nativeCmd);
|
||||||
int numIndices = _bindings.spine_render_command_get_num_indices(nativeCmd);
|
int numIndices = _bindings.spine_render_command_get_num_indices(nativeCmd);
|
||||||
final positions = _bindings.spine_render_command_get_positions(nativeCmd).asTypedList(numVertices * 2);
|
positions = _bindings.spine_render_command_get_positions(nativeCmd).asTypedList(numVertices * 2);
|
||||||
final uvs = _bindings.spine_render_command_get_uvs(nativeCmd).asTypedList(numVertices * 2);
|
uvs = _bindings.spine_render_command_get_uvs(nativeCmd).asTypedList(numVertices * 2);
|
||||||
for (int i = 0; i < numVertices * 2; i += 2) {
|
for (int i = 0; i < numVertices * 2; i += 2) {
|
||||||
uvs[i] *= pageWidth;
|
uvs[i] *= pageWidth;
|
||||||
uvs[i + 1] *= pageHeight;
|
uvs[i + 1] *= pageHeight;
|
||||||
}
|
}
|
||||||
final colors = _bindings.spine_render_command_get_colors(nativeCmd).asTypedList(numVertices);
|
colors = _bindings.spine_render_command_get_colors(nativeCmd).asTypedList(numVertices);
|
||||||
final indices = _bindings.spine_render_command_get_indices(nativeCmd).asTypedList(numIndices);
|
indices = _bindings.spine_render_command_get_indices(nativeCmd).asTypedList(numIndices);
|
||||||
|
|
||||||
if (!kIsWeb) {
|
if (!kIsWeb) {
|
||||||
// We pass the native data as views directly to Vertices.raw. According to the sources, the data
|
// We pass the native data as views directly to Vertices.raw. According to the sources, the data
|
||||||
@ -3357,3 +3361,16 @@ class RenderCommand {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class DebugRenderer {
|
||||||
|
const DebugRenderer();
|
||||||
|
|
||||||
|
void render(SkeletonDrawable drawable, Canvas canvas, List<RenderCommand> commands) {
|
||||||
|
final bonePaint = Paint()
|
||||||
|
..color = material.Colors.blue
|
||||||
|
..style = PaintingStyle.fill;
|
||||||
|
for (final bone in drawable.skeleton.getBones()) {
|
||||||
|
canvas.drawRect(Rect.fromCenter(center: Offset(bone.getWorldX(), bone.getWorldY()), width: 5, height: 5), bonePaint);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -14,7 +14,7 @@ class SpineWidgetController {
|
|||||||
final void Function(SpineWidgetController controller)? onBeforeUpdateWorldTransforms;
|
final void Function(SpineWidgetController controller)? onBeforeUpdateWorldTransforms;
|
||||||
final void Function(SpineWidgetController controller)? onAfterUpdateWorldTransforms;
|
final void Function(SpineWidgetController controller)? onAfterUpdateWorldTransforms;
|
||||||
final void Function(SpineWidgetController controller, Canvas canvas)? onBeforePaint;
|
final void Function(SpineWidgetController controller, Canvas canvas)? onBeforePaint;
|
||||||
final void Function(SpineWidgetController controller, Canvas canvas)? onAfterPaint;
|
final void Function(SpineWidgetController controller, Canvas canvas, List<RenderCommand> commands)? onAfterPaint;
|
||||||
|
|
||||||
SpineWidgetController({this.onInitialized, this.onBeforeUpdateWorldTransforms, this.onAfterUpdateWorldTransforms, this.onBeforePaint, this.onAfterPaint});
|
SpineWidgetController({this.onInitialized, this.onBeforeUpdateWorldTransforms, this.onAfterUpdateWorldTransforms, this.onBeforePaint, this.onAfterPaint});
|
||||||
|
|
||||||
@ -211,6 +211,7 @@ class SpineWidget extends StatefulWidget {
|
|||||||
|
|
||||||
class _SpineWidgetState extends State<SpineWidget> {
|
class _SpineWidgetState extends State<SpineWidget> {
|
||||||
late Bounds _computedBounds;
|
late Bounds _computedBounds;
|
||||||
|
SkeletonDrawable? _drawable;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void initState() {
|
void initState() {
|
||||||
@ -223,6 +224,7 @@ class _SpineWidgetState extends State<SpineWidget> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void loadDrawable(SkeletonDrawable drawable) {
|
void loadDrawable(SkeletonDrawable drawable) {
|
||||||
|
_drawable = drawable;
|
||||||
_computedBounds = widget._boundsProvider.computeBounds(drawable);
|
_computedBounds = widget._boundsProvider.computeBounds(drawable);
|
||||||
widget._controller._initialize(drawable);
|
widget._controller._initialize(drawable);
|
||||||
setState(() {});
|
setState(() {});
|
||||||
@ -246,8 +248,8 @@ class _SpineWidgetState extends State<SpineWidget> {
|
|||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
if (widget._controller._drawable != null) {
|
if (_drawable != null) {
|
||||||
return _SpineRenderObjectWidget(widget._controller._drawable!, widget._controller, widget._fit, widget._alignment, _computedBounds, widget._sizedByBounds);
|
return _SpineRenderObjectWidget(_drawable!, widget._controller, widget._fit, widget._alignment, _computedBounds, widget._sizedByBounds);
|
||||||
} else {
|
} else {
|
||||||
return const SizedBox();
|
return const SizedBox();
|
||||||
}
|
}
|
||||||
@ -461,8 +463,11 @@ class _SpineRenderObject extends RenderBox {
|
|||||||
_setCanvasTransform(canvas, offset);
|
_setCanvasTransform(canvas, offset);
|
||||||
|
|
||||||
_controller.onBeforePaint?.call(_controller, canvas);
|
_controller.onBeforePaint?.call(_controller, canvas);
|
||||||
_skeletonDrawable.renderToCanvas(canvas);
|
var commands = _skeletonDrawable.render();
|
||||||
_controller.onAfterPaint?.call(_controller, canvas);
|
for (final cmd in commands) {
|
||||||
|
canvas.drawVertices(cmd.vertices, rendering.BlendMode.modulate, _skeletonDrawable.atlas.atlasPagePaints[cmd.atlasPageIndex]);
|
||||||
|
}
|
||||||
|
_controller.onAfterPaint?.call(_controller, canvas, commands);
|
||||||
|
|
||||||
canvas.restore();
|
canvas.restore();
|
||||||
SchedulerBinding.instance.scheduleFrameCallback(_beginFrame);
|
SchedulerBinding.instance.scheduleFrameCallback(_beginFrame);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user