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

feat(sandbox): Added top menu with control buttons #30

Merged
merged 4 commits into from Jan 10, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
47 changes: 45 additions & 2 deletions sandbox/lib/main.dart
Expand Up @@ -4,12 +4,28 @@ import 'presets.dart';
import 'scene.dart';
import 'widgets/left_menu.dart';
import 'widgets/scene_widget.dart';
import 'widgets/top_menu.dart';

void main() {
runApp(
MaterialApp(
title: 'Radiance sandbox',
theme: ThemeData.dark(),
theme: ThemeData(
brightness: Brightness.dark,
textButtonTheme: TextButtonThemeData(
style: ButtonStyle(
minimumSize: MaterialStateProperty.all(const Size(30, 30)),
padding: MaterialStateProperty.all(const EdgeInsets.all(5)),
foregroundColor: MaterialStateProperty.resolveWith<Color>(
(Set<MaterialState> states) =>
states.contains(MaterialState.disabled)
? const Color(0xFF5F5F5F)
: const Color(0xFFBBBBBB),
),
overlayColor: MaterialStateProperty.all(const Color(0xFF555555)),
),
),
),
home: const _MyApp(),
),
);
Expand All @@ -23,8 +39,28 @@ class _MyApp extends StatefulWidget {
}

class SandboxState extends State<_MyApp> {
SandboxState() {
currentScene = kPresets[currentPreset]();
}

int currentPreset = 0;
Scene get currentScene => kPresets[currentPreset];
EngineState engineState = EngineState.running;
late Scene currentScene;

void startEngine() {
setState(() => engineState = EngineState.running);
}

void pauseEngine() {
setState(() => engineState = EngineState.paused);
}

void stopEngine() {
setState(() {
engineState = EngineState.stopped;
currentScene = kPresets[currentPreset]();
});
}

@override
Widget build(BuildContext context) {
Expand All @@ -49,6 +85,7 @@ class SandboxState extends State<_MyApp> {
Container(
constraints: const BoxConstraints.expand(height: 40),
color: Theme.of(context).cardColor,
child: TopMenu(this),
),
// Main canvas area
Expanded(
Expand Down Expand Up @@ -81,3 +118,9 @@ class SandboxState extends State<_MyApp> {
);
}
}

enum EngineState {
running,
paused,
stopped,
}
4 changes: 2 additions & 2 deletions sandbox/lib/presets.dart
Expand Up @@ -5,8 +5,8 @@ import 'entities/max_acceleration_entity.dart';
import 'entities/static_target_entity.dart';
import 'scene.dart';

final kPresets = <Scene>[
_seek(),
final kPresets = [
st-pasha marked this conversation as resolved.
Show resolved Hide resolved
_seek,
];

Scene _seek() {
Expand Down
2 changes: 1 addition & 1 deletion sandbox/lib/widgets/left_menu.dart
Expand Up @@ -74,7 +74,7 @@ class _ListItemState extends State<_ListItem> {
child: Padding(
padding: const EdgeInsets.symmetric(vertical: 2, horizontal: 4),
child: Text(
kPresets[widget.index].name,
widget.app.currentScene.name,
style: isCurrent ? styleWhenCurrent : null,
),
),
Expand Down
14 changes: 14 additions & 0 deletions sandbox/lib/widgets/scene_widget.dart
Expand Up @@ -16,6 +16,7 @@ class SceneWidget extends StatefulWidget {
class _SceneWidgetState extends State<SceneWidget> {
Ticker? ticker;
Duration currentTime = Duration.zero;
EngineState currentState = EngineState.running;
double dt = 0.0;

void _handleTick(Duration time) {
Expand All @@ -26,6 +27,16 @@ class _SceneWidgetState extends State<SceneWidget> {
});
}

void _handleStateChange() {
if (widget.app.engineState == EngineState.running) {
ticker?.start();
} else {
ticker?.stop();
currentTime = Duration.zero;
}
currentState = widget.app.engineState;
}

@override
void initState() {
super.initState();
Expand All @@ -40,6 +51,9 @@ class _SceneWidgetState extends State<SceneWidget> {

@override
Widget build(BuildContext context) {
if (currentState != widget.app.engineState) {
_handleStateChange();
}
return Align(
child: AspectRatio(
aspectRatio: 100 / 80,
Expand Down
34 changes: 34 additions & 0 deletions sandbox/lib/widgets/top_menu.dart
@@ -0,0 +1,34 @@
import 'package:flutter/material.dart';
import '../main.dart';

class TopMenu extends StatelessWidget {
const TopMenu(this.app);

final SandboxState app;

@override
Widget build(BuildContext context) {
final canPause = app.engineState == EngineState.running;
final canStop = app.engineState != EngineState.stopped;
return Padding(
padding: const EdgeInsets.symmetric(vertical: 3, horizontal: 10),
child: Row(
children: [
TextButton(
onPressed: canPause ? _handlePause : _handleStart,
child:
Icon(canPause ? Icons.pause_rounded : Icons.play_arrow_rounded),
),
TextButton(
onPressed: canStop ? _handleStop : null,
child: const Icon(Icons.stop_rounded),
),
],
),
);
}

void _handleStart() => app.startEngine();
void _handlePause() => app.pauseEngine();
void _handleStop() => app.stopEngine();
}