Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 98 additions & 4 deletions sky/sdk/example/game/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@

import 'package:sky/mojo/asset_bundle.dart';
import 'package:sky/theme/colors.dart' as colors;
import 'package:sky/rendering/object.dart';
import 'package:sky/widgets/basic.dart';
import 'package:sky/widgets/raised_button.dart';
import 'package:sky/widgets/button_base.dart';
import 'package:sky/widgets/navigator.dart';
import 'package:sky/widgets/widget.dart';
import 'package:sky/widgets/task_description.dart';
Expand Down Expand Up @@ -90,13 +91,106 @@ class GameDemoApp extends App {

Widget _buildMainScene(navigator, route) {
return new Center(
child: new RaisedButton(
child: new Text("Play"),
child: new TextureButton(
onPressed: () {
_game = new GameDemoWorld(_app, navigator, _loader, _spriteSheet, _spriteSheetUI);
navigator.pushNamed('/game');
}
},
texture: _spriteSheetUI["btn_play_up.png"],
textureDown: _spriteSheetUI["btn_play_down.png"],
width: 128.0,
height: 128.0
)
);
}
}

class TextureButton extends ButtonBase {
TextureButton({
Key key,
this.onPressed,
this.texture,
this.textureDown,
this.width: 128.0,
this.height: 128.0
}) : super(key: key);

final Function onPressed;
final Texture texture;
final Texture textureDown;
final double width;
final double height;

Widget buildContent() {
return new Listener(
child: new Container(
width: width,
height: height,
child: new CustomPaint(
callback: paintCallback,
token: new _TextureButtonToken(
highlight,
texture,
textureDown,
width,
height
)
)
),
onPointerUp: (_) {
if (onPressed != null) onPressed();
}
);
}

void paintCallback(PaintingCanvas canvas, Size size) {
if (texture == null)
return;

if (highlight && textureDown != null) {
// Draw down state
canvas.scale(size.width / textureDown.size.width, size.height / textureDown.size.height);
textureDown.drawTexture(canvas, Point.origin, new Paint());
} else {
// Draw up state
canvas.scale(size.width / texture.size.width, size.height / texture.size.height);
texture.drawTexture(canvas, Point.origin, new Paint());
}
}
}

class _TextureButtonToken {
_TextureButtonToken(
this._highlight,
this._texture,
this._textureDown,
this._width,
this._height
);

final bool _highlight;
final Texture _texture;
final Texture _textureDown;
final double _width;
final double _height;

bool operator== (other) {
return
other is _TextureButtonToken &&
_highlight == other._highlight &&
_texture == other._texture &&
_textureDown == other._textureDown &&
_width == other._width &&
_height == other._height;
}

int get hashCode {
int value = 373;
value = 37 * value * _highlight.hashCode;
value = 37 * value * _texture.hashCode;
value = 37 * value * _textureDown.hashCode;
value = 37 * value * _width.hashCode;
value = 37 * value * _height.hashCode;
return value;
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you should implement int get hashCode on this class. Look around the codebase, there are examples of how to do it all over the place.