mirror of
https://github.com/flame-engine/flame.git
synced 2025-11-02 11:43:19 +08:00
Add hoverables (#797)
This commit is contained in:
@ -4,6 +4,7 @@ import 'package:flame/game.dart';
|
||||
import '../../commons/commons.dart';
|
||||
import 'advanced_joystick.dart';
|
||||
import 'draggables.dart';
|
||||
import 'hoverables.dart';
|
||||
import 'joystick.dart';
|
||||
import 'keyboard.dart';
|
||||
import 'mouse_movement.dart';
|
||||
@ -55,16 +56,18 @@ void addControlsStories(Dashbook dashbook) {
|
||||
(context) {
|
||||
return GameWidget(
|
||||
game: DraggablesGame(
|
||||
zoom: context.listProperty(
|
||||
'zoom',
|
||||
1,
|
||||
[0.5, 1, 1.5],
|
||||
),
|
||||
zoom: context.listProperty('zoom', 1, [0.5, 1, 1.5]),
|
||||
),
|
||||
);
|
||||
},
|
||||
codeLink: baseLink('controls/draggables.dart'),
|
||||
)
|
||||
..add(
|
||||
'Hoverables',
|
||||
(_) => GameWidget(game: HoverablesGame()),
|
||||
codeLink: baseLink('controls/hoverables.dart'),
|
||||
info: 'Add more squares by clicking. Hover squares to change colors.',
|
||||
)
|
||||
..add(
|
||||
'Joystick',
|
||||
(_) => GameWidget(game: JoystickGame()),
|
||||
|
||||
34
examples/lib/stories/controls/hoverables.dart
Normal file
34
examples/lib/stories/controls/hoverables.dart
Normal file
@ -0,0 +1,34 @@
|
||||
import 'package:flame/components.dart';
|
||||
import 'package:flame/gestures.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flame/game.dart';
|
||||
import 'package:flame/extensions.dart';
|
||||
|
||||
class HoverableSquare extends PositionComponent with Hoverable {
|
||||
static final Paint _white = Paint()..color = const Color(0xFFFFFFFF);
|
||||
static final Paint _grey = Paint()..color = const Color(0xFFA5A5A5);
|
||||
|
||||
HoverableSquare(Vector2 position)
|
||||
: super(position: position, size: Vector2.all(100)) {
|
||||
anchor = Anchor.center;
|
||||
}
|
||||
|
||||
@override
|
||||
void render(Canvas canvas) {
|
||||
super.render(canvas);
|
||||
canvas.drawRect(size.toRect(), isHovered ? _grey : _white);
|
||||
}
|
||||
}
|
||||
|
||||
class HoverablesGame extends BaseGame with HasHoverableComponents, TapDetector {
|
||||
@override
|
||||
Future<void> onLoad() async {
|
||||
add(HoverableSquare(Vector2(200, 500)));
|
||||
add(HoverableSquare(Vector2(700, 300)));
|
||||
}
|
||||
|
||||
@override
|
||||
void onTapDown(TapDownInfo event) {
|
||||
add(HoverableSquare(event.eventPosition.game));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user