Skip to content

Commit

Permalink
Add linter
Browse files Browse the repository at this point in the history
  • Loading branch information
arnovanliere committed Mar 14, 2024
1 parent d59f468 commit 31918c3
Show file tree
Hide file tree
Showing 5 changed files with 148 additions and 47 deletions.
67 changes: 67 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
name: Flutter CI

on:
push:
branches:
- master
pull_request:
branches:
- master

jobs:
lint:
name: Lint
runs-on: ubuntu-latest
container:
image: cirruslabs/flutter:stable

steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Flutter pub get
run: flutter pub get

- name: Activate dart_code_metrics
run: flutter pub global activate dart_code_metrics

- name: Run code metrics
run: metrics lib -r codeclimate > gh-code-quality-report.json

- name: Upload code quality report
uses: actions/upload-artifact@v2
with:
name: code-quality-report
path: gh-code-quality-report.json

analyze:
name: Analyze
runs-on: ubuntu-latest
container:
image: cirruslabs/flutter:stable

steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Flutter pub get
run: flutter pub get

- name: Run Dart analyze
run: dart analyze --fatal-infos

format:
name: Format
runs-on: ubuntu-latest
container:
image: cirruslabs/flutter:stable

steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Flutter pub get
run: flutter pub get

- name: Run Dart format
run: dart format . -o none --set-exit-if-changed
15 changes: 15 additions & 0 deletions analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
include: package:flutter_lints/flutter.yaml

analyzer:
exclude:
# workaround for https://github.com/dart-lang/sdk/issues/42910
- 'example/**'

linter:
# https://dart-lang.github.io/linter/lints/index.html.
rules:
prefer_single_quotes: true
require_trailing_commas: true
prefer_final_locals: true
prefer_final_in_for_each: true
use_build_context_synchronously: true
27 changes: 16 additions & 11 deletions example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,18 @@ packages:
dependency: transitive
description:
name: characters
url: "https://pub.dartlang.org"
sha256: "04a925763edad70e8443c99234dc3328f442e811f1d8fd1a72f1c8ad0f69a605"
url: "https://pub.dev"
source: hosted
version: "1.2.1"
version: "1.3.0"
collection:
dependency: transitive
description:
name: collection
url: "https://pub.dartlang.org"
sha256: ee67cb0715911d28db6bf4af1026078bd6f0128b07a5f66fb2ed94ec6783c09a
url: "https://pub.dev"
source: hosted
version: "1.16.0"
version: "1.18.0"
flutter:
dependency: "direct main"
description: flutter
Expand All @@ -24,16 +26,18 @@ packages:
dependency: transitive
description:
name: material_color_utilities
url: "https://pub.dartlang.org"
sha256: "0e0a020085b65b6083975e499759762399b4475f766c21668c4ecca34ea74e5a"
url: "https://pub.dev"
source: hosted
version: "0.1.5"
version: "0.8.0"
meta:
dependency: transitive
description:
name: meta
url: "https://pub.dartlang.org"
sha256: d584fa6707a52763a52446f02cc621b077888fb63b93bbcb1143a7be5a0c0c04
url: "https://pub.dev"
source: hosted
version: "1.8.0"
version: "1.11.0"
object_3d:
dependency: "direct main"
description:
Expand All @@ -50,9 +54,10 @@ packages:
dependency: transitive
description:
name: vector_math
url: "https://pub.dartlang.org"
sha256: "80b3257d1492ce4d091729e3a67a60407d227c27241d6927be0130c98e741803"
url: "https://pub.dev"
source: hosted
version: "2.1.2"
version: "2.1.4"
sdks:
dart: ">=2.18.2 <3.0.0"
dart: ">=3.2.0-0 <4.0.0"
flutter: ">=1.17.0"
83 changes: 47 additions & 36 deletions lib/object_3d.dart
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
library object_3d;

import 'dart:async';
import 'dart:math' as math;
import 'dart:ui';
import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:vector_math/vector_math.dart' show Vector3;
import 'package:vector_math/vector_math.dart' as v;
import 'package:vector_math/vector_math.dart' show Vector3;

class Object3D extends StatefulWidget {
const Object3D({
Expand All @@ -21,10 +21,14 @@ class Object3D extends StatefulWidget {
this.maxSpeed = 10.0,
this.reversePitch = true,
this.reverseYaw = false,
}) : assert(object != null || path != null,
'You must provide an object or a path'),
assert(object == null || path == null,
'You must provide an object or a path, not both');
}) : assert(
object != null || path != null,
'You must provide an object or a path',
),
assert(
object == null || path == null,
'You must provide an object or a path, not both',
);

final Size size;
final String? path;
Expand Down Expand Up @@ -58,14 +62,20 @@ class _Object3DState extends State<Object3D> {
_parseObj(widget.object!);
}

assert(widget.swipeCoef > 0,
"Parameter swipeCoef must be a positive, non-zero real number.");
assert(widget.dampCoef >= 0.001 && widget.dampCoef <= 0.999,
"Parameter dampCoef must be in the range [0.001, 0.999].");
assert(widget.maxSpeed > 0,
"Parameter maxSpeed must be positive, non-zero real number.");
assert(
widget.swipeCoef > 0,
'Parameter swipeCoef must be a positive, non-zero real number.',
);
assert(
widget.dampCoef >= 0.001 && widget.dampCoef <= 0.999,
'Parameter dampCoef must be in the range [0.001, 0.999].',
);
assert(
widget.maxSpeed > 0,
'Parameter maxSpeed must be positive, non-zero real number.',
);

_updateTimer = Timer.periodic(Duration(milliseconds: 16), (_) {
_updateTimer = Timer.periodic(const Duration(milliseconds: 16), (_) {
if (!mounted) return;
setState(() {
final adx = _deltaX.abs();
Expand All @@ -92,31 +102,31 @@ class _Object3DState extends State<Object3D> {

/// Parse the object file.
void _parseObj(String obj) {
List<Vector3> vertices = [];
List<List<int>> faces = [];
final lines = obj.split("\n");
final List<Vector3> vertices = [];
final List<List<int>> faces = [];
final lines = obj.split('\n');
for (String line in lines) {
const space = " ";
line = line.replaceAll(RegExp(r"\s+"), space);
const space = ' ';
line = line.replaceAll(RegExp(r'\s+'), space);

// Split into tokens and drop empty tokens
final List<String> chars =
line.split(space).where((v) => v.isNotEmpty).toList(growable: false);

if (chars.isEmpty) continue;

if (chars[0] == "v") {
if (chars[0] == 'v') {
vertices.add(
Vector3(
double.parse(chars[1]),
double.parse(chars[2]),
double.parse(chars[3]),
),
);
} else if (chars[0] == "f") {
List<int> face = [];
} else if (chars[0] == 'f') {
final List<int> face = [];
for (int i = 1; i < chars.length; i++) {
face.add(int.parse(chars[i].split("/")[0]));
face.add(int.parse(chars[i].split('/')[0]));
}
faces.add(face);
}
Expand Down Expand Up @@ -195,12 +205,13 @@ class _ObjectPainter extends CustomPainter {

/// Calculate the normal vector of a face.
Vector3 _normalVector3(Vector3 first, Vector3 second, Vector3 third) {
Vector3 secondFirst = Vector3.copy(second)..sub(first);
Vector3 secondThird = Vector3.copy(second)..sub(third);
final Vector3 secondFirst = Vector3.copy(second)..sub(first);
final Vector3 secondThird = Vector3.copy(second)..sub(third);
return Vector3(
(secondFirst.y * secondThird.z) - (secondFirst.z * secondThird.y),
(secondFirst.z * secondThird.x) - (secondFirst.x * secondThird.z),
(secondFirst.x * secondThird.y) - (secondFirst.y * secondThird.x));
(secondFirst.y * secondThird.z) - (secondFirst.z * secondThird.y),
(secondFirst.z * secondThird.x) - (secondFirst.x * secondThird.z),
(secondFirst.x * secondThird.y) - (secondFirst.y * secondThird.x),
);
}

/// Multiply two vectors.
Expand All @@ -226,7 +237,7 @@ class _ObjectPainter extends CustomPainter {

/// Calculate the 2D-positions of a vertex in the 3D space.
List<Offset> _drawFace(List<Vector3> vertices, List<int> face) {
List<Offset> coordinates = [];
final List<Offset> coordinates = [];
for (int i = 0; i < face.length; i++) {
double x, y;
if (i < face.length - 1) {
Expand Down Expand Up @@ -254,9 +265,9 @@ class _ObjectPainter extends CustomPainter {
/// Calculate the color of a vertex based on the
/// position of the vertex and the light.
List<Color> _calcColor(Color color, Vector3 normalVector) {
double s = _scalarMultiplication(normalVector, light);
double coefficient = math.max(0, s);
Color c = Color.fromRGBO(
final double s = _scalarMultiplication(normalVector, light);
final double coefficient = math.max(0, s);
final Color c = Color.fromRGBO(
(color.red * coefficient).round(),
(color.green * coefficient).round(),
(color.blue * coefficient).round(),
Expand All @@ -283,7 +294,7 @@ class _ObjectPainter extends CustomPainter {
@override
void paint(Canvas canvas, Size size) {
// Calculate the position of the vertices in the 3D space.
List<Vector3> verticesToDraw = [];
final List<Vector3> verticesToDraw = [];
for (final vertex in vertices) {
final defV = _calcVertex(Vector3.copy(vertex));
verticesToDraw.add(defV);
Expand All @@ -293,17 +304,17 @@ class _ObjectPainter extends CustomPainter {

// Calculate the position of the vertices in the 2D space
// and calculate the colors of the vertices.
List<Offset> offsets = [];
List<Color> colors = [];
final List<Offset> offsets = [];
final List<Color> colors = [];
for (int i = 0; i < faces.length; i++) {
List<int> face = faces[avgOfZ[i].index];
final List<int> face = faces[avgOfZ[i].index];
final n = _normalVector(verticesToDraw, face);
colors.addAll(_calcColor(color, n));
offsets.addAll(_drawFace(verticesToDraw, face));
}

// Draw the vertices.
Paint paint = Paint();
final Paint paint = Paint();
paint.style = PaintingStyle.fill;
paint.color = color;
final v = Vertices(VertexMode.triangles, offsets, colors: colors);
Expand Down
3 changes: 3 additions & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,6 @@ dependencies:
flutter:
sdk: flutter
vector_math: ^2.1.2

dev_dependencies:
flutter_lints: ^3.0.1

0 comments on commit 31918c3

Please sign in to comment.