Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Strange problems when using TextBoxComponent #3156

Closed
1 task
DavidChZh opened this issue May 13, 2024 · 6 comments
Closed
1 task

Strange problems when using TextBoxComponent #3156

DavidChZh opened this issue May 13, 2024 · 6 comments
Labels

Comments

@DavidChZh
Copy link

DavidChZh commented May 13, 2024

What happened?

I encountered a strange problem when using the TextBoxComponent component. My game class inherits from Forge2DGame. When debugging the TextBoxComponent, I found that the font was very blurry and there was also a problem with the font size. Later I found that after changing to inherit FlameGame, it became normal.

import 'dart:async';
import 'package:flame/components.dart';
import 'package:flame/game.dart';
import 'package:flame/palette.dart';
import 'package:flame/text.dart';
import 'package:flame_forge2d/forge2d_game.dart';
import 'package:flutter/material.dart';

class TextExample extends FlameGame {
  //Forge2DGame
  static const String description = '''
    In this example we show different ways of rendering text.
  ''';

  @override
  Future<void> onLoad() async {
    world.add(MyTextBox(
      'Let A be a finitely generated torsion-free abelian group. Then '
      'A is free.',
      align: Anchor.center,
      size: Vector2(30, 30),
      timePerChar: 0,
      margins: 10,
    )..position = Vector2.zero());
  }
}

final _regularTextStyle = TextStyle(
  fontSize: 12,
  color: BasicPalette.white.color,
);
final _regular = TextPaint(
  style: _regularTextStyle,
);

final _box = _regular.copyWith(
  (style) => style.copyWith(
    color: Colors.lightGreenAccent,
    fontFamily: 'monospace',
    letterSpacing: 2.0,
  ),
);

class MyTextBox extends TextBoxComponent {
  late Paint paint;
  late Rect bgRect;

  MyTextBox(
    String text, {
    super.align,
    super.size,
    double? timePerChar,
    double? margins,
  }) : super(
          text: text,
          textRenderer: _box,
          anchor: Anchor.center,
          boxConfig: TextBoxConfig(
            maxWidth: 400,
            timePerChar: timePerChar ?? 0.05,
            growingBox: true,
            margins: EdgeInsets.all(margins ?? 25),
          ),
        );

  @override
  Future<void> onLoad() {
    paint = Paint();
    bgRect = Rect.fromLTWH(0, 0, width, height);
    size.addListener(() {
      bgRect = Rect.fromLTWH(0, 0, width, height);
    });

    paint.color = Colors.white10;
    return super.onLoad();
  }

  @override
  void render(Canvas canvas) {
    canvas.drawRect(bgRect, paint);
    super.render(canvas);
  }
}

The above code runs normally, but when I change FlameGame to Forge2DGame, something goes wrong when I rerun it.

What do you expect?

Inheriting FlameGame and Forge2DGame can show the same effect

How can we reproduce this?

No response

What steps should take to fix this?

No response

Do have an example of where the bug occurs?

No response

Relevant log output

No response

Execute in a terminal and put output into the code block below

Output of: flutter doctor -v

Affected platforms

Android

Other information

No response

Are you interested in working on a PR for this?

  • I want to work on this
@DavidChZh DavidChZh added the bug label May 13, 2024
@spydon
Copy link
Member

spydon commented May 22, 2024

Can you please add your code in a code block?

@DavidChZh
Copy link
Author

DavidChZh commented May 22, 2024

Can you please add your code in a code block?

text_example.dart.zip

Sorry, I confirmed that my code is in the code block, but I don't know why the preview turned into normal text. I tried various methods but it didn't work. I uploaded the file.

@ufrshubham
Copy link
Collaborator

image
@DavidChZh you need to wrap the code like this ☝🏼

@DavidChZh
Copy link
Author

image @DavidChZh you need to wrap the code like this ☝🏼

Thanks!

@spydon
Copy link
Member

spydon commented May 24, 2024

Forge2DGame has a default zoom of 10 (so that you don't hit the speed limit), so that's why you are getting different results.

@DavidChZh
Copy link
Author

Forge2DGame has a default zoom of 10 (so that you don't hit the speed limit), so that's why you are getting different results.

I forgot that zoom also affects font components. I subconsciously thought that it would only affect spheres, pictures, etc.
I got it back to normal by setting the scale individually for the font component.
Thank you so much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants