diff --git a/example/main.dart b/example/main.dart index a6bfc6b..92d96c7 100644 --- a/example/main.dart +++ b/example/main.dart @@ -3,15 +3,15 @@ import "package:p5/p5.dart"; import "sketch.dart"; void main() { - runApp(new MyApp()); + runApp(MyApp()); } class MyHomePage extends StatefulWidget { - MyHomePage({Key? key, this.title}) : super(key: key); + const MyHomePage({Key? key, this.title}) : super(key: key); final String? title; @override _MyHomePageState createState() { - return new _MyHomePageState(); + return _MyHomePageState(); } } @@ -19,13 +19,13 @@ class MyApp extends StatelessWidget { // This widget is the root of your application. @override Widget build(BuildContext context) { - return new MaterialApp( + return MaterialApp( title: 'P5 Demo', - theme: new ThemeData( + theme: ThemeData( // This is the theme of your application. primarySwatch: Colors.blue, ), - home: new MyHomePage(title: 'P5 Demo Home Page'), + home: const MyHomePage(title: 'P5 Demo Home Page'), ); } } @@ -38,10 +38,10 @@ class _MyHomePageState extends State @override void initState() { super.initState(); - sketch = new MySketch(); + sketch = MySketch(); // Need an animator to call the draw() method in the sketch continuously, // otherwise it will be called only when touch events are detected. - animator = new PAnimator(this); + animator = PAnimator(this); animator.addListener(() { setState(() { sketch!.redraw(); @@ -52,11 +52,11 @@ class _MyHomePageState extends State @override Widget build(BuildContext context) { - return new Scaffold( - appBar: new AppBar(title: new Text("P5 Draw!")), + return Scaffold( + appBar: AppBar(title: const Text("P5 Draw!")), backgroundColor: const Color.fromRGBO(200, 200, 200, 1.0), - body: new Center( - child: new PWidget(sketch), + body: Center( + child: PWidget(sketch), ), ); } diff --git a/example/sketch.dart b/example/sketch.dart index d7a7e4d..7eb4cb1 100644 --- a/example/sketch.dart +++ b/example/sketch.dart @@ -25,11 +25,11 @@ class MySketch extends PPainter { } void mousePressed() { - strokes.add([new PVector(mouseX, mouseY)]); + strokes.add([PVector(mouseX, mouseY)]); } void mouseDragged() { var stroke = strokes.last; - stroke.add(new PVector(mouseX, mouseY)); + stroke.add(PVector(mouseX, mouseY)); } } diff --git a/lib/PApplet.dart b/lib/PApplet.dart index 4b636f6..6d9017e 100644 --- a/lib/PApplet.dart +++ b/lib/PApplet.dart @@ -16,9 +16,7 @@ abstract class PApplet { return 0; } - if (internalRandom == null) { - internalRandom = math.Random(); - } + internalRandom ??= math.Random(); // for some reason (rounding error?) Math.random() * 3 // can sometimes return '3' (once in ~30 million tries) diff --git a/lib/PVector.dart b/lib/PVector.dart index a33206e..05a91c3 100644 --- a/lib/PVector.dart +++ b/lib/PVector.dart @@ -14,17 +14,13 @@ class PVector { return PVector(0, 0, 0); } - PVector(double x, double y, [double z = 0.0]) { - this.x = x; - this.y = y; - this.z = z; - } + PVector(this.x, this.y, [this.z = 0.0]); /// Subtract one vector from another and store in another vector /// @param target PVector in which to store the result static PVector? sub3(PVector v1, PVector v2, PVector? target) { if (target == null) { - target = new PVector(v1.x - v2.x, v1.y - v2.y, v1.z - v2.z); + target = PVector(v1.x - v2.x, v1.y - v2.y, v1.z - v2.z); } else { target.set(v1.x - v2.x, v1.y - v2.y, v1.z - v2.z); } @@ -91,7 +87,7 @@ class PVector { /// @param target the target vector (if null, a new vector will be created) static PVector add3(PVector v1, PVector v2, PVector? target) { if (target == null) { - target = new PVector(v1.x + v2.x, v1.y + v2.y, v1.z + v2.z); + target = PVector(v1.x + v2.x, v1.y + v2.y, v1.z + v2.z); } else { target.set(v1.x + v2.x, v1.y + v2.y, v1.z + v2.z); } @@ -224,7 +220,7 @@ class PVector { /// @param target PVector in which to store the result static PVector? div3(PVector v, double n, PVector? target) { if (target == null) { - target = new PVector(v.x / n, v.y / n, v.z / n); + target = PVector(v.x / n, v.y / n, v.z / n); } else { target.set(v.x / n, v.y / n, v.z / n); } @@ -248,7 +244,7 @@ class PVector { /// @usage web_application /// @brief Get a copy of the vector PVector copy() { - return new PVector(x, y, z); + return PVector(x, y, z); } /// @Deprecated use copy @@ -353,7 +349,7 @@ class PVector { /// @return the PVector static PVector fromAngle_v2(double angle, PVector? target) { if (target == null) { - target = new PVector(math.cos(angle), math.sin(angle)); + target = PVector(math.cos(angle), math.sin(angle)); } else { target.set(math.cos(angle), math.sin(angle), 0); } diff --git a/lib/p5.dart b/lib/p5.dart index 527978d..5ff0999 100644 --- a/lib/p5.dart +++ b/lib/p5.dart @@ -21,16 +21,16 @@ class PWidget extends StatelessWidget { // print("BUILDING WIDGET..."); // print(painter); - return new Container( + return Container( width: painter!.fillParent ? null : painter!.width.toDouble(), height: painter!.fillParent ? null : painter!.height.toDouble(), - constraints: painter!.fillParent ? BoxConstraints.expand() : null, + constraints: painter!.fillParent ? const BoxConstraints.expand() : null, //new margin: const EdgeInsets.all(0.0), - child: new ClipRect( - child: new CustomPaint( + child: ClipRect( + child: CustomPaint( painter: painter, - child: new GestureDetector( + child: GestureDetector( // The gesture detector needs to be declared here so it can // access the context from the CustomPaint, which allows to // transforms global positions into local positions relative @@ -108,7 +108,7 @@ class PPainter extends ChangeNotifier implements CustomPainter { bool useStroke = true; var vertices = []; - Path path = new Path(); + Path path = Path(); var shapeMode = PConstants.POLYGON; PPainter() { @@ -117,6 +117,7 @@ class PPainter extends ChangeNotifier implements CustomPainter { redraw(); } + @override bool? hitTest(Offset position) => null; @override @@ -136,9 +137,9 @@ class PPainter extends ChangeNotifier implements CustomPainter { var rect = Offset.zero & size; rect = const Alignment(0.0, 0.0).inscribe(size, rect); return [ - new CustomPainterSemantics( + CustomPainterSemantics( rect: rect, - properties: new SemanticsProperties( + properties: const SemanticsProperties( label: 'P5 Sketch', textDirection: TextDirection.ltr, ), @@ -315,7 +316,7 @@ class PPainter extends ChangeNotifier implements CustomPainter { } void ellipse(double x, double y, double w, double h) { - final rect = new Offset(x - w / 2, y - h / 2) & new Size(w, h); + final rect = Offset(x - w / 2, y - h / 2) & Size(w, h); if (useFill) { paintCanvas.drawOval(rect, fillPaint); } @@ -326,13 +327,13 @@ class PPainter extends ChangeNotifier implements CustomPainter { void line(double x1, double y1, double x2, double y2) { if (useStroke) { - paintCanvas.drawLine(new Offset(x1, y1), new Offset(x2, y2), strokePaint); + paintCanvas.drawLine(Offset(x1, y1), Offset(x2, y2), strokePaint); } } void point(num x, num y) { if (useStroke) { - var points = [new Offset(x as double, y as double)]; + var points = [Offset(x as double, y as double)]; paintCanvas.drawPoints(PointMode.points, points, strokePaint); } } @@ -347,7 +348,7 @@ class PPainter extends ChangeNotifier implements CustomPainter { } void rect(double x, double y, double w, double h) { - final rect = new Offset(x, y) & new Size(w, h); + final rect = Offset(x, y) & Size(w, h); if (useFill) { paintCanvas.drawRect(rect, fillPaint); } @@ -374,28 +375,26 @@ class PPainter extends ChangeNotifier implements CustomPainter { } void endShape([int mode = 0]) { - if (0 < vertices.length) { - if (shapeMode == PConstants.POINTS || shapeMode == PConstants.LINES) { - var vlist = []; - for (var v in vertices) { - vlist.add(v.dx); - vlist.add(v.dy); - } - var raw = Float32List.fromList(vlist); - if (shapeMode == PConstants.POINTS) { - paintCanvas.drawRawPoints(PointMode.points, raw, strokePaint); - } else { - paintCanvas.drawRawPoints(PointMode.lines, raw, strokePaint); - } + if (shapeMode == PConstants.POINTS || shapeMode == PConstants.LINES) { + var vlist = []; + for (var v in vertices) { + vlist.add(v.dx); + vlist.add(v.dy); + } + var raw = Float32List.fromList(vlist); + if (shapeMode == PConstants.POINTS) { + paintCanvas.drawRawPoints(PointMode.points, raw, strokePaint); } else { - path.reset(); - path.addPolygon(vertices, mode == PConstants.CLOSE); - if (useFill) { - paintCanvas.drawPath(path, fillPaint); - } - if (useStroke) { - paintCanvas.drawPath(path, strokePaint); - } + paintCanvas.drawRawPoints(PointMode.lines, raw, strokePaint); + } + } else { + path.reset(); + path.addPolygon(vertices, mode == PConstants.CLOSE); + if (useFill) { + paintCanvas.drawPath(path, fillPaint); + } + if (useStroke) { + paintCanvas.drawPath(path, strokePaint); } } }