Skip to content

Commit

Permalink
Merge pull request #4 from gildurao/feature/turtle-stack
Browse files Browse the repository at this point in the history
feat: save/pop state commands
  • Loading branch information
zonble authored Mar 25, 2024
2 parents 9a30597 + 5288314 commit b67411e
Show file tree
Hide file tree
Showing 17 changed files with 359 additions and 85 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 0.5.0

- Upgrade to SDK 3.3.0
- Add Save/Pop State commands

## 0.4.2

- Upgrade to SDK 2.17.0
Expand Down
Binary file added example/images/13.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 7 additions & 0 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:flutter/material.dart';
import 'package:flutter_turtle_example/pages/lsystem_page.dart';

import 'pages/clock2_page.dart';
import 'pages/clock_page.dart';
Expand Down Expand Up @@ -39,6 +40,7 @@ class MyApp extends StatelessWidget {
'spiral': (_) => const SpiralPage(),
'spiral2': (_) => const Spiral2Page(),
'snowflake': (_) => const SnowflakePage(),
'lsystem_plant': (_) => const LSystemPage(),
},
);
}
Expand Down Expand Up @@ -76,6 +78,11 @@ class _MyHomePageState extends State<MyHomePage> {
title: 'Spiral', image: 'images/11.png', pageName: 'spiral2'),
const MyTile(
title: 'Snowflake', image: 'images/12.png', pageName: 'snowflake'),
const MyTile(
title: 'L-System Plant',
pageName: 'lsystem_plant',
image: 'images/13.png',
),
];
return Scaffold(
appBar: AppBar(title: Text(widget.title)),
Expand Down
138 changes: 138 additions & 0 deletions example/lib/pages/lsystem_page.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
import 'package:flutter/material.dart';
import 'package:flutter_turtle/flutter_turtle.dart';

class Rule {
final String a;
final String b;

const Rule(this.a, this.b);
}

class LSystem {
List<Rule> ruleSet;
List<TurtleCommand> turtleCommands = [];
String axiom;

LSystem(
this.axiom,
this.ruleSet,
) {
turtleCommands.add(
PenDown(),
);
turtleCommands.add(
SetColor(
(_) => Colors.lightGreen,
),
);
turtleCommands.add(
SetStrokeWidth(
(_) => 2,
),
);
}

void resetTurtle() {
turtleCommands.clear();
turtleCommands.add(
PenDown(),
);
turtleCommands.add(
SetColor(
(_) => Colors.green,
),
);
turtleCommands.add(
SetStrokeWidth(
(_) => 2,
),
);
}

void generate(int iterations) {
var sentence = axiom;
while (iterations != 0) {
StringBuffer nextGeneration = StringBuffer();

for (int i = 0; i < sentence.length; i++) {
String current = sentence[i];
String replace = current;
for (int j = 0; j < ruleSet.length; j++) {
String a = ruleSet.elementAt(j).a;
if (a == current) {
replace = ruleSet.elementAt(j).b;
break;
}
}
nextGeneration.write(replace);
}
sentence = nextGeneration.toString();
iterations--;
}
parseFractalPlant(sentence);
}

void parseFractalPlant(String sentence) {
for (int i = 0; i < sentence.length; i++) {
String character = sentence[i];
if (character == 'F') turtleCommands.add(Forward((_) => 1));
if (character == '-') turtleCommands.add(Left((_) => 35.0));
if (character == '+') turtleCommands.add(Right((_) => 25.0));
if (character == '[') turtleCommands.add(SaveState());
if (character == ']') turtleCommands.add(PopState());
}
}
}

class LSystemPage extends StatefulWidget {
const LSystemPage({super.key});

@override
LSystemPageState createState() => LSystemPageState();
}

class LSystemPageState extends State<LSystemPage> {
LSystem fractalPlant = LSystem(
'X',
const [
Rule(
'X',
'F+[[X]-X]-F[-FX]+X',
),
Rule(
'F',
'FF',
),
],
);

@override
Widget build(BuildContext context) {
fractalPlant.generate(8);
return Scaffold(
appBar: AppBar(title: const Text('Logo'), actions: <Widget>[
TextButton(
onPressed: () => setState(() {}),
child: const Text('Run', style: TextStyle(color: Colors.white)),
)
]),
body: Column(
children: [
const SizedBox(
height: 500,
),
Expanded(
child: AnimatedTurtleView(
animationDuration: const Duration(seconds: 5),
commands: fractalPlant.turtleCommands,
child: const SizedBox(
width: double.infinity,
height: 600,
),
),
),
],
),
);
}
}
2 changes: 1 addition & 1 deletion example/lib/pages/tree_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import 'package:flutter_turtle/flutter_turtle.dart';

/// An example from https://www.calormen.com/jslogo/
class TreePage extends StatefulWidget {
const TreePage({Key? key}) : super(key: key);
const TreePage({super.key});

@override
State<TreePage> createState() => _TreePageState();
Expand Down
84 changes: 54 additions & 30 deletions example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,18 @@ packages:
dependency: transitive
description:
name: collection
sha256: "4a07be6cb69c84d677a6c3096fcf960cc3285a8330b4603e0d463d15d9bd934c"
sha256: ee67cb0715911d28db6bf4af1026078bd6f0128b07a5f66fb2ed94ec6783c09a
url: "https://pub.dev"
source: hosted
version: "1.17.1"
version: "1.18.0"
cupertino_icons:
dependency: "direct main"
description:
name: cupertino_icons
sha256: e35129dc44c9118cee2a5603506d823bab99c68393879edb440e0090d07586be
sha256: d57953e10f9f8327ce64a508a355f0b1ec902193f66288e8cb5070e7c47eeb2d
url: "https://pub.dev"
source: hosted
version: "1.0.5"
version: "1.0.6"
fake_async:
dependency: transitive
description:
Expand All @@ -66,10 +66,10 @@ packages:
dependency: "direct dev"
description:
name: flutter_lints
sha256: aeb0b80a8b3709709c9cc496cdc027c5b3216796bc0af0ce1007eaf24464fd4c
sha256: e2a421b7e59244faef694ba7b30562e489c2b489866e505074eb005cd7060db7
url: "https://pub.dev"
source: hosted
version: "2.0.1"
version: "3.0.1"
flutter_test:
dependency: "direct dev"
description: flutter
Expand All @@ -81,55 +81,71 @@ packages:
path: ".."
relative: true
source: path
version: "0.4.2"
js:
version: "0.5.0"
leak_tracker:
dependency: transitive
description:
name: leak_tracker
sha256: "78eb209deea09858f5269f5a5b02be4049535f568c07b275096836f01ea323fa"
url: "https://pub.dev"
source: hosted
version: "10.0.0"
leak_tracker_flutter_testing:
dependency: transitive
description:
name: leak_tracker_flutter_testing
sha256: b46c5e37c19120a8a01918cfaf293547f47269f7cb4b0058f21531c2465d6ef0
url: "https://pub.dev"
source: hosted
version: "2.0.1"
leak_tracker_testing:
dependency: transitive
description:
name: js
sha256: f2c445dce49627136094980615a031419f7f3eb393237e4ecd97ac15dea343f3
name: leak_tracker_testing
sha256: a597f72a664dbd293f3bfc51f9ba69816f84dcd403cdac7066cb3f6003f3ab47
url: "https://pub.dev"
source: hosted
version: "0.6.7"
version: "2.0.1"
lints:
dependency: transitive
description:
name: lints
sha256: "5e4a9cd06d447758280a8ac2405101e0e2094d2a1dbdd3756aec3fe7775ba593"
sha256: cbf8d4b858bb0134ef3ef87841abdf8d63bfc255c266b7bf6b39daa1085c4290
url: "https://pub.dev"
source: hosted
version: "2.0.1"
version: "3.0.0"
matcher:
dependency: transitive
description:
name: matcher
sha256: "6501fbd55da300384b768785b83e5ce66991266cec21af89ab9ae7f5ce1c4cbb"
sha256: d2323aa2060500f906aa31a895b4030b6da3ebdcc5619d14ce1aada65cd161cb
url: "https://pub.dev"
source: hosted
version: "0.12.15"
version: "0.12.16+1"
material_color_utilities:
dependency: transitive
description:
name: material_color_utilities
sha256: d92141dc6fe1dad30722f9aa826c7fbc896d021d792f80678280601aff8cf724
sha256: "0e0a020085b65b6083975e499759762399b4475f766c21668c4ecca34ea74e5a"
url: "https://pub.dev"
source: hosted
version: "0.2.0"
version: "0.8.0"
meta:
dependency: transitive
description:
name: meta
sha256: "3c74dbf8763d36539f114c799d8a2d87343b5067e9d796ca22b5eb8437090ee3"
sha256: d584fa6707a52763a52446f02cc621b077888fb63b93bbcb1143a7be5a0c0c04
url: "https://pub.dev"
source: hosted
version: "1.9.1"
version: "1.11.0"
path:
dependency: transitive
description:
name: path
sha256: "8829d8a55c13fc0e37127c29fedf290c102f4e40ae94ada574091fe0ff96c917"
sha256: "087ce49c3f0dc39180befefc60fdb4acd8f8620e5682fe2476afd0b3688bb4af"
url: "https://pub.dev"
source: hosted
version: "1.8.3"
version: "1.9.0"
sky_engine:
dependency: transitive
description: flutter
Expand All @@ -139,26 +155,26 @@ packages:
dependency: transitive
description:
name: source_span
sha256: dd904f795d4b4f3b870833847c461801f6750a9fa8e61ea5ac53f9422b31f250
sha256: "53e943d4206a5e30df338fd4c6e7a077e02254531b138a15aec3bd143c1a8b3c"
url: "https://pub.dev"
source: hosted
version: "1.9.1"
version: "1.10.0"
stack_trace:
dependency: transitive
description:
name: stack_trace
sha256: c3c7d8edb15bee7f0f74debd4b9c5f3c2ea86766fe4178eb2a18eb30a0bdaed5
sha256: "73713990125a6d93122541237550ee3352a2d84baad52d375a4cad2eb9b7ce0b"
url: "https://pub.dev"
source: hosted
version: "1.11.0"
version: "1.11.1"
stream_channel:
dependency: transitive
description:
name: stream_channel
sha256: "83615bee9045c1d322bbbd1ba209b7a749c2cbcdcb3fdd1df8eb488b3279c1c8"
sha256: ba2aa5d8cc609d96bbb2899c28934f9e1af5cddbd60a827822ea467161eb54e7
url: "https://pub.dev"
source: hosted
version: "2.1.1"
version: "2.1.2"
string_scanner:
dependency: transitive
description:
Expand All @@ -179,10 +195,10 @@ packages:
dependency: transitive
description:
name: test_api
sha256: eb6ac1540b26de412b3403a163d919ba86f6a973fe6cc50ae3541b80092fdcfb
sha256: "5c2f730018264d276c20e4f1503fd1308dfbbae39ec8ee63c5236311ac06954b"
url: "https://pub.dev"
source: hosted
version: "0.5.1"
version: "0.6.1"
vector_math:
dependency: transitive
description:
Expand All @@ -191,5 +207,13 @@ packages:
url: "https://pub.dev"
source: hosted
version: "2.1.4"
vm_service:
dependency: transitive
description:
name: vm_service
sha256: b3d56ff4341b8f182b96aceb2fa20e3dcb336b9f867bc0eafc0de10f1048e957
url: "https://pub.dev"
source: hosted
version: "13.0.0"
sdks:
dart: ">=3.0.0-0 <4.0.0"
dart: ">=3.3.0 <4.0.0"
1 change: 1 addition & 0 deletions example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ flutter:
- images/10.png
- images/11.png
- images/12.png
- images/13.png

# An image asset can refer to one or more resolution-specific "variants", see
# https://flutter.dev/assets-and-images/#resolution-aware.
Expand Down
7 changes: 6 additions & 1 deletion example/windows/flutter/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ include(${EPHEMERAL_DIR}/generated_config.cmake)
# https://github.com/flutter/flutter/issues/57146.
set(WRAPPER_ROOT "${EPHEMERAL_DIR}/cpp_client_wrapper")

# Set fallback configurations for older versions of the flutter tool.
if (NOT DEFINED FLUTTER_TARGET_PLATFORM)
set(FLUTTER_TARGET_PLATFORM "windows-x64")
endif()

# === Flutter Library ===
set(FLUTTER_LIBRARY "${EPHEMERAL_DIR}/flutter_windows.dll")

Expand Down Expand Up @@ -92,7 +97,7 @@ add_custom_command(
COMMAND ${CMAKE_COMMAND} -E env
${FLUTTER_TOOL_ENVIRONMENT}
"${FLUTTER_ROOT}/packages/flutter_tools/bin/tool_backend.bat"
windows-x64 $<CONFIG>
${FLUTTER_TARGET_PLATFORM} $<CONFIG>
VERBATIM
)
add_custom_target(flutter_assemble DEPENDS
Expand Down
Loading

0 comments on commit b67411e

Please sign in to comment.