Skip to content

Commit

Permalink
Merge pull request #12 from nikhil-RGB/nn/feat/includeEncryption
Browse files Browse the repository at this point in the history
Adding Beta Symmetric Key gen feature
  • Loading branch information
nikhil-RGB authored Mar 23, 2023
2 parents 9a8edfd + cdfdb6d commit 888fe31
Show file tree
Hide file tree
Showing 3 changed files with 289 additions and 6 deletions.
17 changes: 11 additions & 6 deletions lib/pages/AutomatonPage.dart
Original file line number Diff line number Diff line change
Expand Up @@ -193,11 +193,13 @@ class _AutomatonPageState extends State<AutomatonPage> {
OutlinedButton(
style: OutlinedButton.styleFrom(
side: BorderSide(
color: (AutomatonPage.automate) ? Colors.grey : Colors.cyan,
color: (AutomatonPage.automate || (!AutomatonPage.running))
? Colors.grey
: Colors.cyan,
width: 2,
)),
onPressed: () {
if (AutomatonPage.automate) {
if (AutomatonPage.automate || (!AutomatonPage.running)) {
return;
}
setState(() {
Expand All @@ -210,17 +212,20 @@ class _AutomatonPageState extends State<AutomatonPage> {
Icon(
Icons.delete_forever_outlined,
color:
(AutomatonPage.automate) ? Colors.grey : Colors.cyan,
(AutomatonPage.automate || (!AutomatonPage.running))
? Colors.grey
: Colors.cyan,
),
SizedBox(
width: MediaQuery.of(context).size.width * 0.01,
),
Text(
"FORCE KILL SYSTEM",
style: TextStyle(
color: (AutomatonPage.automate)
? Colors.grey
: Colors.cyan,
color:
(AutomatonPage.automate || (!AutomatonPage.running))
? Colors.grey
: Colors.cyan,
),
),
],
Expand Down
257 changes: 257 additions & 0 deletions lib/pages/EncrypterPage.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,257 @@
// ignore_for_file: must_be_immutable

import 'package:flutter/material.dart';
import 'package:flutter_automata/CellGrid.dart';

import 'package:google_fonts/google_fonts.dart';

import '../models/Cell.dart';

class EncrypterPage extends StatefulWidget {
@override
State<EncrypterPage> createState() => _EncrypterPageState();
static bool running = true;

bool beautify_mode;
int generationCount = 0;
int ub;
int lb;
int ress;

final List<List<Cell>> grid; //grid which will be used to build the board
EncrypterPage(
{super.key,
required this.grid,
required this.ub,
required this.lb,
// ignore: non_constant_identifier_names
required this.ress,
required this.beautify_mode});
}

class _EncrypterPageState extends State<EncrypterPage> {
TextEditingController binary =
TextEditingController(); //TextEditingController for binary
TextEditingController ascii =
TextEditingController(); //TextEditingController for ascii text area
@override
Widget build(BuildContext context) {
binary.text = extractBinaryFromGrid();
ascii.text = parseUnix(binary.text);
return SafeArea(
child: Scaffold(
backgroundColor: Colors.black,
// appBar: AppBar(
// backgroundColor: Colors.black,
// actions: [
// generationLabel(),
// SizedBox(
// width: MediaQuery.of(context).size.width * 0.06,
// ),
// IconButton(
// color: Colors.cyan,
// onPressed: () {
// Phoenix.rebirth(context);
// },
// icon: const Icon(Icons.restart_alt_rounded))
// ],
// ),
body: Column(
children: [
SizedBox(height: MediaQuery.of(context).size.height * 0.02),
generateToolBar(),
SizedBox(height: MediaQuery.of(context).size.height * 0.01),
infoField(txtc: binary, label: "Binary Text"),
SizedBox(height: MediaQuery.of(context).size.height * 0.02),
infoField(txtc: ascii, label: "ASCII value"),
SizedBox(height: MediaQuery.of(context).size.height * 0.02),
Expanded(
flex: 8,
child: CellGrid(
pretty: widget.beautify_mode,
grid: widget.grid,
initPage: false,
),
),
SizedBox(height: MediaQuery.of(context).size.height * 0.02),
],
),
floatingActionButton: FloatingActionButton(
onPressed: () async {
if (!EncrypterPage.running) {
return;
}
if (EncrypterPage.running) {
setState(() {
EncrypterPage.running = Cell.generationUpdate(
widget.grid, widget.lb, widget.ub, widget.ress);
++widget.generationCount;
});
if (!EncrypterPage.running) {
await openInfoDialog(
context: context,
details:
"The Grid system has either stabilized or been force killed.\nNo further growth possible!",
title: "Automaton Stabilized");
}
} else {
await openInfoDialog(
context: context,
details:
"The Grid system has either stabilized or been force killed.\nNo further growth possible!",
title: "Grid Biome Disabled");
}
},
backgroundColor: (!EncrypterPage.running) ? Colors.grey : Colors.cyan,
child: const Icon(Icons.play_arrow_rounded),
),
),
);
}

Future openInfoDialog(
{String? title,
required String details,
required BuildContext context}) =>
showDialog(
context: context,
builder: (context) {
return AlertDialog(
shape: const RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(6.0))),
title: Text(
title ?? "Info:",
style: GoogleFonts.dmSans(
color: Colors.cyan,
),
),
content: Text(
details,
style: GoogleFonts.dmSans(
color: Colors.cyan,
),
),
actions: [
ElevatedButton(
onPressed: () {
Navigator.pop(context);
},
child: Text("Ok", style: GoogleFonts.dmSans()),
),
],
backgroundColor: const Color(0XFF004246),
);
},
);

Widget generationLabel() {
String lab = "";
int gc = widget.generationCount;
lab = gc.toString();
if (gc < 10) {
lab = "0$lab";
}
return Text(
"Generation Count: ${widget.generationCount}",
style: const TextStyle(color: Colors.cyan),
);
}

Widget generateToolBar() {
return Row(
children: [
Row(
mainAxisSize: MainAxisSize.min,
children: [
IconButton(
onPressed: () {
Navigator.pop(context);
},
icon: const Icon(
Icons.arrow_back,
color: Colors.white,
)),
SizedBox(
width: MediaQuery.of(context).size.width * 0.01,
),
const Text(
"Go Back",
style: TextStyle(color: Colors.white),
)
],
),
SizedBox(
width: MediaQuery.of(context).size.width * 0.23,
),
generationLabel(),
],
);
}

Widget infoField(
{required TextEditingController txtc, required String label}) {
return Center(
child: SizedBox(
width: MediaQuery.of(context).size.width * 0.80,
child: TextField(
minLines: 3,
maxLines: 5,
readOnly: true,
style: GoogleFonts.sourceCodePro(color: Colors.cyan),
controller: txtc,
decoration: InputDecoration(
labelText: label,
alignLabelWithHint: true,
labelStyle: GoogleFonts.sourceCodePro(
fontSize: 15,
color: Colors.cyan,
decoration: TextDecoration.none,
fontWeight: FontWeight.w400),
floatingLabelStyle: GoogleFonts.sourceCodePro(color: Colors.cyan),
hintStyle:
GoogleFonts.sourceCodePro(color: Colors.cyan, fontSize: 14),
focusedBorder: OutlineInputBorder(
borderSide: const BorderSide(width: 3, color: Colors.cyan),
borderRadius: BorderRadius.circular(12.0)),
enabledBorder: OutlineInputBorder(
borderSide: const BorderSide(
width: 3, color: Colors.teal), //<-- SEE HERE
borderRadius: BorderRadius.circular(12.0),
),
)),
),
);
}

//This method extracts a binary number from the current grid by coverting live cells to 1 and dead ones to 0
String extractBinaryFromGrid() {
String binary = "";
for (int i = 0; i < widget.grid.length; ++i) {
for (int j = 0; j < widget.grid[i].length; ++j) {
binary += (widget.grid[i][j].state) ? "1" : "0";
}
}
return binary;
}

String parseUnix(String binary) {
String unix = "";
for (int i = 0; i < binary.length; ++i) {
String sub = "";
if ((i + 12) <= binary.length) {
sub = binary.substring(i, (i = i + 12));
--i;
} else {
sub = binary.substring(i);
i = binary.length;
}
unix += String.fromCharCode(int.parse(sub, radix: 2));
}
return unix;
}
}

class AlwaysDisabledFocusNode extends FocusNode {
@override
bool get hasFocus => false;
}
21 changes: 21 additions & 0 deletions lib/pages/InitializationPage.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'dart:math';
import 'package:flutter/material.dart';
import 'package:flutter_automata/CellGrid.dart';
import 'package:flutter_automata/pages/AutomatonPage.dart';
import 'package:flutter_automata/pages/EncrypterPage.dart';
import 'package:flutter_automata/util/DialogManager.dart';
import 'package:flutter_phoenix/flutter_phoenix.dart';
import 'package:google_fonts/google_fonts.dart';
Expand Down Expand Up @@ -120,6 +121,12 @@ class _InitializationPageState extends State<InitializationPage> {
"Exit Interface",
style: GoogleFonts.sourceCodePro(color: Colors.white),
)),
PopupMenuItem(
value: 4,
child: Text(
"Simulate Encryption Keys[BETA]",
style: GoogleFonts.sourceCodePro(color: Colors.white),
)),
],
onSelected: (value) async {
switch (value) {
Expand Down Expand Up @@ -162,6 +169,20 @@ class _InitializationPageState extends State<InitializationPage> {
Phoenix.rebirth(context);
}
break;
case 4:
{
EncrypterPage.running = true;
Navigator.push(
context,
MaterialPageRoute(
builder: ((context) => EncrypterPage(
beautify_mode: widget.beautify,
grid: Cell.cloneGrid(widget.grid),
ub: widget.ub,
lb: widget.lb,
ress: widget.ress))));
}
break;
}
},
child: const Icon(
Expand Down

0 comments on commit 888fe31

Please sign in to comment.