Skip to content
Snippets Groups Projects
Select Git revision
  • 4622aca01bcafb8dff037f8adf39e917f6585705
  • master default protected
  • 32-upgrade-framework-and-dependencies
  • 15-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.9_9 protected
24 results

home.dart

Blame
  • parameter_painter.dart 9.90 KiB
    import 'dart:math';
    import 'dart:ui' as ui;
    
    import 'package:flutter/material.dart';
    import 'package:flutter_custom_toolbox/flutter_toolbox.dart';
    
    import 'package:jeweled/config/color_theme.dart';
    import 'package:jeweled/config/default_game_settings.dart';
    import 'package:jeweled/config/default_global_settings.dart';
    import 'package:jeweled/models/settings/settings_game.dart';
    import 'package:jeweled/models/settings/settings_global.dart';
    
    class ParameterPainter extends CustomPainter {
      const ParameterPainter({
        required this.code,
        required this.value,
        required this.gameSettings,
        required this.globalSettings,
      });
    
      final String code;
      final String value;
      final GameSettings gameSettings;
      final GlobalSettings globalSettings;
    
      @override
      void paint(Canvas canvas, Size size) {
        // force square
        final double canvasSize = min(size.width, size.height);
    
        // content
        switch (code) {
          case DefaultGameSettings.parameterCodeColorsCount:
            paintColorsCountParameterItem(canvas, canvasSize);
            break;
          case DefaultGameSettings.parameterCodeBoardSize:
            paintBoardSizeParameterItem(canvas, canvasSize);
            break;
          case DefaultGlobalSettings.parameterCodeColorsTheme:
            paintColorsThemeParameterItem(canvas, canvasSize);
            break;
          case DefaultGlobalSettings.parameterCodeGraphicsTheme:
            paintGraphicThemeParameterItem(canvas, canvasSize);
            break;
          default:
            printlog('Unknown parameter: $code/$value');
            paintUnknownParameterItem(canvas, canvasSize);
        }
      }
    
      @override
      bool shouldRepaint(CustomPainter oldDelegate) {
        return false;
      }
    
      // "unknown" parameter -> simple block with text
      void paintUnknownParameterItem(
        final Canvas canvas,
        final double size,
      ) {
        final paint = Paint();
        paint.strokeJoin = StrokeJoin.round;
        paint.strokeWidth = 3;
    
        final textSpan = TextSpan(
          text: '$code\n$value',
          style: const TextStyle(
            color: Colors.black,
            fontSize: 18,
            fontWeight: FontWeight.bold,
          ),
        );
        final textPainter = TextPainter(
          text: textSpan,
          textDirection: TextDirection.ltr,
          textAlign: TextAlign.center,
        );
        textPainter.layout();
        textPainter.paint(
          canvas,
          Offset(
            (size - textPainter.width) * 0.5,
            (size - textPainter.height) * 0.5,
          ),
        );
      }
    
      void paintBoardSizeParameterItem(
        final Canvas canvas,
        final double size,
      ) {
        int gridWidth = 1;
    
        switch (value) {
          case DefaultGameSettings.boardSizeValueSmall:
            gridWidth = 2;
            break;
          case DefaultGameSettings.boardSizeValueMedium:
            gridWidth = 3;
            break;
          case DefaultGameSettings.boardSizeValueLarge:
            gridWidth = 4;
            break;
          case DefaultGameSettings.boardSizeValueExtraLarge:
            gridWidth = 5;
            break;
          default:
            printlog('Wrong value for boardSize parameter value: $value');
        }
    
        final paint = Paint();
        paint.strokeJoin = StrokeJoin.round;
        paint.strokeWidth = 3 / 100 * size;
    
        // Mini grid
        final borderColor = Colors.grey.shade800;
    
        final double cellSize = size / 7;
        final double origin = (size - gridWidth * cellSize) / 2;
    
        for (int row = 0; row < gridWidth; row++) {
          for (int col = 0; col < gridWidth; col++) {
            final Offset topLeft = Offset(origin + col * cellSize, origin + row * cellSize);
            final Offset bottomRight = topLeft + Offset(cellSize, cellSize);
    
            final squareColor =
                Color(ColorTheme.getColorCode(col + row * gridWidth, globalSettings.colorsTheme));
    
            paint.color = squareColor;
            paint.style = PaintingStyle.fill;
            canvas.drawRect(Rect.fromPoints(topLeft, bottomRight), paint);
    
            paint.color = borderColor;
            paint.style = PaintingStyle.stroke;
            canvas.drawRect(Rect.fromPoints(topLeft, bottomRight), paint);
          }
        }
      }
    
      void paintColorsCountParameterItem(
        final Canvas canvas,
        final double size,
      ) {
        final paint = Paint();
        paint.strokeJoin = StrokeJoin.round;
        paint.strokeWidth = 3;
    
        // Colors preview
        const List<Offset> positions = [
          Offset(0, 0),
          Offset(1, 0),
          Offset(2, 0),
          Offset(2, 1),
          Offset(2, 2),
          Offset(1, 2),
          Offset(0, 2),
          Offset(0, 1),
        ];
    
        final double padding = 4 / 100 * size;
        final double margin = 3 / 100 * size;
        final double width = ((size - 2 * padding) / 3) - 2 * margin;
    
        final colorsCount = int.parse(value);
    
        for (int colorIndex = 0; colorIndex < colorsCount; colorIndex++) {
          final Offset position = positions[colorIndex];
    
          final Offset topLeft = Offset(padding + margin + position.dx * (width + 2 * margin),
              padding + margin + position.dy * (width + 2 * margin));
    
          final Offset bottomRight = topLeft + Offset(width, width);
    
          final squareColor =
              Color(ColorTheme.getColorCode(colorIndex, globalSettings.colorsTheme));
          paint.color = squareColor;
          paint.style = PaintingStyle.fill;
          canvas.drawRect(Rect.fromPoints(topLeft, bottomRight), paint);
    
          final borderColor = squareColor.darken(20);
          paint.color = borderColor;
          paint.style = PaintingStyle.stroke;
          canvas.drawRect(Rect.fromPoints(topLeft, bottomRight), paint);
        }
    
        // centered text value
        final textSpan = TextSpan(
          text: value.toString(),
          style: TextStyle(
            color: Colors.black,
            fontSize: size / 4,
            fontWeight: FontWeight.bold,
          ),
        );
        final textPainter = TextPainter(
          text: textSpan,
          textDirection: TextDirection.ltr,
          textAlign: TextAlign.center,
        );
        textPainter.layout();
        textPainter.paint(
          canvas,
          Offset(
            (size - textPainter.width) * 0.5,
            (size - textPainter.height) * 0.5,
          ),
        );
      }
    
      void paintColorsThemeParameterItem(
        final Canvas canvas,
        final double size,
      ) {
        const int gridWidth = 4;
    
        final paint = Paint();
        paint.strokeJoin = StrokeJoin.round;
        paint.strokeWidth = 3 / 100 * size;
    
        // Mini grid
        final borderColor = Colors.grey.shade800;
    
        final double cellSize = size / gridWidth;
        final double origin = (size - gridWidth * cellSize) / 2;
    
        for (int row = 0; row < gridWidth; row++) {
          for (int col = 0; col < gridWidth; col++) {
            final Offset topLeft = Offset(origin + col * cellSize, origin + row * cellSize);
            final Offset bottomRight = topLeft + Offset(cellSize, cellSize);
    
            final squareColor = Color(ColorTheme.getColorCode(col + row * gridWidth, value));
    
            paint.color = squareColor;
            paint.style = PaintingStyle.fill;
            canvas.drawRect(Rect.fromPoints(topLeft, bottomRight), paint);
    
            paint.color = borderColor;
            paint.style = PaintingStyle.stroke;
            canvas.drawRect(Rect.fromPoints(topLeft, bottomRight), paint);
          }
        }
      }
    
      void paintGraphicThemeParameterItem(
        final Canvas canvas,
        final double size,
      ) {
        final paint = Paint();
        paint.strokeJoin = StrokeJoin.round;
        paint.strokeWidth = 3;
    
        // cells preview
        const List<Offset> positions = [
          Offset(0, 0),
          Offset(1, 0),
          Offset(2, 0),
          Offset(2, 1),
          Offset(2, 2),
          Offset(1, 2),
          Offset(0, 2),
          Offset(0, 1),
        ];
    
        final double padding = 5 / 100 * size;
        final double width = (size - 2 * padding) / 3;
    
        bool drawBorder = false;
        bool gradientColor = false;
        List<String> contentStrings = [];
    
        switch (value) {
          case DefaultGlobalSettings.graphicThemeSolidBackground:
            break;
          case DefaultGlobalSettings.graphicThemeGradientAndBorder:
            drawBorder = true;
            gradientColor = true;
            break;
          case DefaultGlobalSettings.graphicThemeEmojis:
            contentStrings = DefaultGlobalSettings.graphicThemeContentEmojiStrings;
            break;
          case DefaultGlobalSettings.graphicThemePatterns:
            contentStrings = DefaultGlobalSettings.graphicThemeContentPatternStrings;
            break;
          default:
            printlog('Wrong value for colorsCount parameter value: $value');
        }
    
        for (int itemValue = 0; itemValue < positions.length; itemValue++) {
          final Offset position = positions[itemValue];
    
          final Offset topLeft =
              Offset(padding + position.dx * width, padding + position.dy * width);
          final Offset bottomRight = topLeft + Offset(width, width);
    
          final Rect itemBox = Rect.fromPoints(topLeft, bottomRight);
          final itemColor = ColorTheme.getColor(itemValue, globalSettings.colorsTheme);
    
          paint.color = itemColor;
          paint.style = PaintingStyle.fill;
          canvas.drawRect(itemBox, paint);
    
          // gradient background
          if (gradientColor) {
            paint.shader = ui.Gradient.linear(itemBox.topLeft, itemBox.bottomCenter, [
              itemColor.lighten(10),
              itemColor.darken(10),
            ]);
            paint.style = PaintingStyle.fill;
            canvas.drawRect(itemBox, paint);
          }
    
          // cell border
          if (drawBorder) {
            final paintBorder = Paint();
            paintBorder.color = itemColor.darken(20);
            paintBorder.style = PaintingStyle.stroke;
            paintBorder.strokeWidth = 2;
    
            canvas.drawRect(itemBox, paintBorder);
          }
    
          // centered text value
          if (contentStrings.isNotEmpty) {
            final textSpan = TextSpan(
              text: contentStrings[itemValue],
              style: TextStyle(
                color: Colors.black,
                fontSize: width / 2,
                fontWeight: FontWeight.bold,
              ),
            );
            final textPainter = TextPainter(
              text: textSpan,
              textDirection: TextDirection.ltr,
              textAlign: TextAlign.center,
            );
            textPainter.layout();
            textPainter.paint(
              canvas,
              Offset(
                topLeft.dx + (width - textPainter.width) * 0.5,
                topLeft.dy + (width - textPainter.height) * 0.5,
              ),
            );
          }
        }
      }
    }