Skip to content
Snippets Groups Projects
Select Git revision
  • master default protected
  • 17-improve-app-metadata
  • Release_0.9.0_28 protected
  • Release_0.8.2_27 protected
  • Release_0.8.1_26 protected
  • Release_0.8.0_25 protected
  • Release_0.7.0_24 protected
  • Release_0.6.0_23 protected
  • Release_0.5.0_22 protected
  • Release_0.4.2_21 protected
  • Release_0.4.1_20 protected
  • Release_0.4.0_19 protected
  • Release_0.3.1_18 protected
  • Release_0.3.0_17 protected
  • Release_0.2.1_16 protected
  • Release_0.2.0_15 protected
  • Release_0.1.1_14 protected
  • Release_0.1.0_13 protected
  • Release_0.0.12_12 protected
  • Release_0.0.11_11 protected
  • Release_0.0.10_10 protected
  • Release_0.0.8_8 protected
22 results

counter.dart

Blame
  • counter.dart 5.58 KiB
    import 'dart:math';
    
    import 'package:flutter/material.dart';
    
    import 'package:tetrisdual/provider/data.dart';
    
    class Counter {
      // Current counter
      bool _match = false; // Does this new tetrimino touch an other tetrimino of same player
      int _lines = 0; // Count lines fully filled by this new tetrimino
      int _holes = 0; // Count non fillable holes caused by this new tetrimino
    
      // Points definitions
      static const int _base = 50;
      static const int _pointsIfMatch = 50;
      static const int _pointsPerLine = 60;
      static const int _pointsPerHole = -10;
    
      static const double iconSize = 30.0;
      static const double fontSize = 50.0;
      static const double spacerHeight = 7;
    
      // Counter categories icons
      static const Color categoryIconColor = Colors.green;
      static const Icon iconTouchingColor = Icon(
        Icons.join_full,
        color: categoryIconColor,
        size: iconSize,
      );
      static const Icon iconRowsCount = Icon(
        Icons.table_rows,
        color: categoryIconColor,
        size: iconSize,
      );
      static const Icon iconHolesCount = Icon(
        Icons.check_box_outline_blank,
        color: categoryIconColor,
        size: iconSize,
      );
    
      // Action buttons icons
      static const Color buttonIconColor = Colors.blue;
      static const Icon iconRemove = Icon(
        Icons.remove,
        color: buttonIconColor,
        size: iconSize,
      );
      static const Icon iconAdd = Icon(
        Icons.add,
        color: buttonIconColor,
        size: iconSize,
      );
    
      Widget buildCounterWidget(Data myProvider) {
        return Table(
          children: [
            buildMatchWidget(myProvider),
            buildSpacerRow(),
            buildLinesWidget(myProvider),
            buildSpacerRow(),
            buildHolesWidget(myProvider),
            buildSpacerRow(),
          ],
        );
      }
    
      TableRow buildSpacerRow() {
        return const TableRow(children: [
          SizedBox(height: spacerHeight),
          SizedBox(height: spacerHeight),
          SizedBox(height: spacerHeight),
          SizedBox(height: spacerHeight)
        ]);
      }
    
      TableRow buildMatchWidget(Data myProvider) {
        return TableRow(
          children: [
            iconTouchingColor,
            IconButton(
              padding: EdgeInsets.zero,
              constraints: const BoxConstraints(),
              style: const ButtonStyle(
                tapTargetSize: MaterialTapTargetSize.shrinkWrap,
              ),
              icon: iconRemove,
              onPressed: () {
                _match = false;
                myProvider.redraw();
              },
            ),
            Center(
              child: Icon(
                _match ? Icons.radio_button_checked : Icons.radio_button_unchecked,
                color: categoryIconColor,
                size: iconSize,
              ),
            ),
            IconButton(
              padding: EdgeInsets.zero,
              constraints: const BoxConstraints(),
              style: const ButtonStyle(
                tapTargetSize: MaterialTapTargetSize.shrinkWrap,
              ),
              icon: iconAdd,
              onPressed: () {
                _match = true;
                myProvider.redraw();
              },
            ),
          ],
        );
      }
    
      TableRow buildLinesWidget(Data myProvider) {
        return TableRow(
          children: [
            iconRowsCount,
            IconButton(
              padding: EdgeInsets.zero,
              constraints: const BoxConstraints(),
              style: const ButtonStyle(
                tapTargetSize: MaterialTapTargetSize.shrinkWrap,
              ),
              icon: iconRemove,
              onPressed: () {
                _lines = max(_lines - 1, 0);
                myProvider.redraw();
              },
            ),
            Center(
              child: Text(
                _lines.toString(),
                style: const TextStyle(
                  fontFamily: 'Blocks',
                  fontSize: fontSize,
                ),
                textHeightBehavior: const TextHeightBehavior(
                  applyHeightToFirstAscent: false,
                  applyHeightToLastDescent: false,
                ),
              ),
            ),
            IconButton(
              padding: EdgeInsets.zero,
              constraints: const BoxConstraints(),
              style: const ButtonStyle(
                tapTargetSize: MaterialTapTargetSize.shrinkWrap,
              ),
              icon: iconAdd,
              onPressed: () {
                _lines = min(_lines + 1, 4);
                myProvider.redraw();
              },
            ),
          ],
        );
      }
    
      TableRow buildHolesWidget(Data myProvider) {
        return TableRow(
          children: [
            iconHolesCount,
            IconButton(
              padding: EdgeInsets.zero,
              constraints: const BoxConstraints(),
              style: const ButtonStyle(
                tapTargetSize: MaterialTapTargetSize.shrinkWrap,
              ),
              icon: iconRemove,
              onPressed: () {
                _holes = max(_holes - 1, 0);
                myProvider.redraw();
              },
            ),
            Center(
              child: Text(
                _holes.toString(),
                style: const TextStyle(
                  fontFamily: 'Blocks',
                  fontSize: fontSize,
                ),
                textHeightBehavior: const TextHeightBehavior(
                  applyHeightToFirstAscent: false,
                  applyHeightToLastDescent: false,
                ),
              ),
            ),
            IconButton(
              padding: EdgeInsets.zero,
              constraints: const BoxConstraints(),
              style: const ButtonStyle(
                tapTargetSize: MaterialTapTargetSize.shrinkWrap,
              ),
              icon: iconAdd,
              onPressed: () {
                _holes = min(_holes + 1, 9);
                myProvider.redraw();
              },
            ),
          ],
        );
      }
    
      int computePoints() {
        return _base +
            (_match ? _pointsIfMatch : 0) +
            _lines * _pointsPerLine +
            _holes * _pointsPerHole;
      }
    
      void reset() {
        _match = false;
        _lines = 0;
        _holes = 0;
      }
    }