Skip to content

Commit

Permalink
chore: Added the padding of a cell in the draw badge screen.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jhalakupadhyay committed Nov 23, 2024
1 parent 9f77806 commit aef64e5
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 164 deletions.
53 changes: 0 additions & 53 deletions lib/bademagic_module/utils/byte_array_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -53,56 +53,3 @@ List<List<bool>> hexStringToBool(String hexString) {

return boolArray;
}

List<List<int>> byteArrayToBinaryArray(List<int> byteArray) {
List<List<int>> binaryArray = List.generate(11, (_) => []);

int rowIndex = 0;
for (int byte in byteArray) {
List<int> binaryRepresentation = [];
for (int i = 7; i >= 0; i--) {
binaryRepresentation.add((byte >> i) & 1);
}

binaryArray[rowIndex].addAll(binaryRepresentation);

rowIndex = (rowIndex + 1) % 11;
}

logger.d(
"binaryArray: $binaryArray"); // Use print instead of logger for standalone example
return binaryArray;
}

String hexToBin(String hex) {
// Convert hex to binary string
String binaryString = BigInt.parse(hex, radix: 16).toRadixString(2);

// Pad the binary string with leading zeros if necessary to ensure it's a multiple of 8 bits
int paddingLength = (8 - (binaryString.length % 8)) % 8;
binaryString = binaryString.padLeft(binaryString.length + paddingLength, '0');
logger.d("binaryString: $binaryString");
return binaryString;
}

List<List<int>> binaryStringTo2DList(String binaryString) {
int maxHeight = 11;
List<List<int>> binary2DList = List.generate(maxHeight, (_) => []);

for (int x = 0; x < binaryString.length; x++) {
int a = 0;
for (int y = a; y < 11; y++) {
for (int z = 0; z < 8; z++) {
binary2DList[y].add(int.parse(binaryString[x++]));
if (x >= binaryString.length) {
break;
}
}
if (x >= binaryString.length) {
break;
}
}
}
logger.d("binary2DList: $binary2DList");
return binary2DList;
}
1 change: 0 additions & 1 deletion lib/view/about_us_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ class _AboutUsScreenState extends State<AboutUsScreen> {
@override
void initState() {
_setOrientation();
// TODO: implement initState
super.initState();
}

Expand Down
202 changes: 103 additions & 99 deletions lib/view/draw_badge_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,13 @@ class _DrawBadgeState extends State<DrawBadge> {
return CommonScaffold(
index: 1,
title: 'BadgeMagic',
body: Column(
children: [
Expanded(
child: Container(
margin: EdgeInsets.symmetric(vertical: 20.h, horizontal: 20.w),
padding: EdgeInsets.all(10.dg),
height: 390.h,
body: Padding(
padding: EdgeInsets.only(top: 20.h, right: 20.w, left: 20.w),
child: Column(
children: [
Container(
padding: EdgeInsets.only(top: 4.h, left: 1.w,right: 1.w),
height: 232,
width: 500.w,
decoration: BoxDecoration(
color: Colors.black,
Expand All @@ -76,105 +76,109 @@ class _DrawBadgeState extends State<DrawBadge> {
.toList(),
),
),
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
TextButton(
onPressed: () {
setState(() {
drawToggle.toggleIsDrawing(true);
});
},
child: Column(
children: [
Icon(
Icons.edit,
color:
drawToggle.getIsDrawing() ? Colors.red : Colors.black,
),
Text(
'Draw',
style: TextStyle(
color: drawToggle.isDrawing ? Colors.red : Colors.black,
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
TextButton(
onPressed: () {
setState(() {
drawToggle.toggleIsDrawing(true);
});
},
child: Column(
children: [
Icon(
Icons.edit,
color: drawToggle.getIsDrawing()
? Colors.red
: Colors.black,
),
)
],
Text(
'Draw',
style: TextStyle(
color:
drawToggle.isDrawing ? Colors.red : Colors.black,
),
)
],
),
),
),
TextButton(
onPressed: () {
setState(() {
drawToggle.toggleIsDrawing(false);
});
},
child: Column(
children: [
Icon(
Icons.delete,
color: drawToggle.isDrawing ? Colors.black : Colors.red,
),
Text(
'Erase',
style: TextStyle(
TextButton(
onPressed: () {
setState(() {
drawToggle.toggleIsDrawing(false);
});
},
child: Column(
children: [
Icon(
Icons.delete,
color: drawToggle.isDrawing ? Colors.black : Colors.red,
),
)
],
Text(
'Erase',
style: TextStyle(
color:
drawToggle.isDrawing ? Colors.black : Colors.red,
),
)
],
),
),
),
TextButton(
onPressed: () {
setState(() {
drawToggle.resetDrawViewGrid();
});
},
child: const Column(
children: [
Icon(
Icons.refresh,
color: Colors.black,
),
Text(
'Reset',
style: TextStyle(color: Colors.black),
)
],
TextButton(
onPressed: () {
setState(() {
drawToggle.resetDrawViewGrid();
});
},
child: const Column(
children: [
Icon(
Icons.refresh,
color: Colors.black,
),
Text(
'Reset',
style: TextStyle(color: Colors.black),
)
],
),
),
),
TextButton(
onPressed: () {
List<List<int>> badgeGrid = drawToggle
.getDrawViewGrid()
.map((e) => e.map((e) => e ? 1 : 0).toList())
.toList();
List<String> hexString =
Converters.convertBitmapToLEDHex(badgeGrid, false);
widget.isSavedCard!
? fileHelper.updateBadgeText(
widget.filename!,
hexString,
)
: widget.isSavedClipart!
? fileHelper.updateClipart(
widget.filename!, badgeGrid)
: fileHelper.saveImage(drawToggle.getDrawViewGrid());
fileHelper.generateClipartCache();
ToastUtils().showToast("Clipart Saved Successfully");
},
child: const Column(
children: [
Icon(
Icons.save,
color: Colors.black,
),
Text('Save', style: TextStyle(color: Colors.black))
],
TextButton(
onPressed: () {
List<List<int>> badgeGrid = drawToggle
.getDrawViewGrid()
.map((e) => e.map((e) => e ? 1 : 0).toList())
.toList();
List<String> hexString =
Converters.convertBitmapToLEDHex(badgeGrid, false);
widget.isSavedCard!
? fileHelper.updateBadgeText(
widget.filename!,
hexString,
)
: widget.isSavedClipart!
? fileHelper.updateClipart(
widget.filename!, badgeGrid)
: fileHelper
.saveImage(drawToggle.getDrawViewGrid());
fileHelper.generateClipartCache();
ToastUtils().showToast("Clipart Saved Successfully");
},
child: const Column(
children: [
Icon(
Icons.save,
color: Colors.black,
),
Text('Save', style: TextStyle(color: Colors.black))
],
),
),
),
],
)
],
],
)
],
),
),
key: const Key(drawBadgeScreen),
);
Expand Down
35 changes: 24 additions & 11 deletions lib/virtualbadge/view/draw_badge_paint.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,33 +10,46 @@ class DrawBadgePaint extends CustomPainter {

@override
void paint(Canvas canvas, Size size) {
double cellWidth = size.width / grid[0].length;
double cellHeight = size.height / grid.length;
// Add two rows and columns as padding
final int paddedRows = grid.length + 2; // Top and bottom padding rows
final int paddedCols = grid[0].length + 2; // Left and right padding columns

// Calculate cell size based on the padded dimensions
double cellWidth = size.width / paddedCols;
double cellHeight = size.height / paddedRows;

// Dynamic offsets using ScreenUtil
double pathOffsetX1 = 0.15.w;
double pathOffsetX2 = 0.3.w;
double pathOffsetX3 = 0.15.w;

// Draw the actual grid cells only (exclude the padding cells)
for (int row = 0; row < grid.length; row++) {
for (int col = 0; col < grid[row].length; col++) {
for (int col = 0; col < grid[0].length; col++) {
final Paint paint = Paint()
..color = grid[row][col] ? Colors.red : Colors.grey.shade900
..style = PaintingStyle.fill;

// Calculate positions with an offset for the padding
double startX =
(col + 1) * cellWidth; // Offset by 1 column for left padding
double startY =
(row + 1) * cellHeight; // Offset by 1 row for top padding

// Create a triangular shape for the cell
final Path path = Path()
..moveTo(col * cellWidth, row * cellHeight)
..lineTo(col * cellWidth + cellWidth * pathOffsetX1, row * cellHeight)
..lineTo(col * cellWidth + cellWidth * pathOffsetX2,
row * cellHeight + cellHeight)
..lineTo(col * cellWidth + cellWidth * pathOffsetX3,
row * cellHeight + cellHeight)
..moveTo(startX, startY)
..lineTo(startX + cellWidth * pathOffsetX1, startY)
..lineTo(startX + cellWidth * pathOffsetX2, startY + cellHeight)
..lineTo(startX + cellWidth * pathOffsetX3, startY + cellHeight)
..close();

// Rotate and draw the cell
const double radians = math.pi / 3;
canvas.save();
canvas.translate((col + 0.5) * cellWidth, (row + 0.5) * cellHeight);
canvas.translate(startX + cellWidth / 2, startY + cellHeight / 2);
canvas.rotate(radians);
canvas.translate(-(col + 0.5) * cellWidth, -(row + 0.5) * cellHeight);
canvas.translate(-(startX + cellWidth / 2), -(startY + cellHeight / 2));

canvas.drawPath(path, paint);
canvas.restore();
Expand Down

0 comments on commit aef64e5

Please sign in to comment.