Skip to content
This repository was archived by the owner on Jun 24, 2024. It is now read-only.

Some minor but required fixes #12

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions example/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,29 @@ 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();
}
}

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'),
);
}
}
Expand All @@ -38,10 +38,10 @@ class _MyHomePageState extends State<MyHomePage>
@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();
Expand All @@ -52,11 +52,11 @@ class _MyHomePageState extends State<MyHomePage>

@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),
),
);
}
Expand Down
4 changes: 2 additions & 2 deletions example/sketch.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
4 changes: 1 addition & 3 deletions lib/PApplet.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
16 changes: 6 additions & 10 deletions lib/PVector.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
}
Expand All @@ -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
Expand Down Expand Up @@ -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);
}
Expand Down
65 changes: 32 additions & 33 deletions lib/p5.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -108,7 +108,7 @@ class PPainter extends ChangeNotifier implements CustomPainter {
bool useStroke = true;

var vertices = <Offset>[];
Path path = new Path();
Path path = Path();
var shapeMode = PConstants.POLYGON;

PPainter() {
Expand All @@ -117,6 +117,7 @@ class PPainter extends ChangeNotifier implements CustomPainter {
redraw();
}

@override
bool? hitTest(Offset position) => null;

@override
Expand All @@ -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,
),
Expand Down Expand Up @@ -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);
}
Expand All @@ -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);
}
}
Expand All @@ -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);
}
Expand All @@ -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 = <double>[];
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 = <double>[];
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);
}
}
}
Expand Down