Browse Source

Allow changing speed

master
Dylan Baker 3 years ago
parent
commit
baa0543774
3 changed files with 69 additions and 7 deletions
  1. 20
    5
      src/Board.tsx
  2. 20
    1
      src/Controls.tsx
  3. 29
    1
      src/index.css

+ 20
- 5
src/Board.tsx View File

@@ -12,6 +12,7 @@ interface Props {
12 12
 interface State {
13 13
   grid: boolean[][];
14 14
   running: boolean;
15
+  speed: number;
15 16
 }
16 17
 
17 18
 class Board extends React.Component<Props, State> {
@@ -19,11 +20,12 @@ class Board extends React.Component<Props, State> {
19 20
 
20 21
   constructor(props: Props) {
21 22
     super(props);
22
-    const height = props.height ?? 5;
23
-    const width = props.height ?? 5;
23
+    const height = props.height ?? 10;
24
+    const width = props.height ?? 20;
24 25
     this.state = {
25 26
       grid: Array(height).fill(Array(width).fill(false)),
26 27
       running: false,
28
+      speed: 500,
27 29
     };
28 30
     this.timer = null;
29 31
   }
@@ -56,7 +58,7 @@ class Board extends React.Component<Props, State> {
56 58
       });
57 59
 
58 60
       this.setState({ grid: newGrid });
59
-    }, 500);
61
+    }, this.state.speed);
60 62
   };
61 63
 
62 64
   stop = () => {
@@ -77,12 +79,25 @@ class Board extends React.Component<Props, State> {
77 79
     this.setState({ running: !this.state.running });
78 80
   };
79 81
 
82
+  updateSpeed = (speed: number) => {
83
+    this.setState({ speed });
84
+    if (this.state.running) {
85
+      this.stop();
86
+      this.start();
87
+    }
88
+  };
89
+
80 90
   render() {
81
-    const { grid, running } = this.state;
91
+    const { grid, running, speed } = this.state;
82 92
 
83 93
     return (
84 94
       <div className="board">
85
-        <Controls running={running} toggle={this.toggleRunning} />
95
+        <Controls
96
+          running={running}
97
+          speed={speed}
98
+          toggle={this.toggleRunning}
99
+          updateSpeed={this.updateSpeed}
100
+        />
86 101
         {grid.map((row, y) => (
87 102
           <div className="row" key={y}>
88 103
             {row.map((cell, x) => (

+ 20
- 1
src/Controls.tsx View File

@@ -2,15 +2,34 @@ import React from 'react';
2 2
 
3 3
 interface Props {
4 4
   running: boolean;
5
+  speed: number;
5 6
   toggle: () => void;
7
+  updateSpeed: (speed: number) => void;
6 8
 }
7 9
 
8 10
 const Controls = (props: Props) => {
9
-  const { running, toggle } = props;
11
+  const { running, speed, toggle, updateSpeed } = props;
12
+
13
+  const goSlower = () => {
14
+    updateSpeed(speed + 100);
15
+  };
16
+
17
+  const goFaster = () => {
18
+    if (speed > 100) {
19
+      updateSpeed(speed - 100);
20
+    }
21
+  };
10 22
 
11 23
   return (
12 24
     <div className="controls">
13 25
       <button onClick={toggle}>{running ? 'Pause' : 'Play'}</button>
26
+      <div className="speed">
27
+        <button onClick={goSlower}>Slower</button>
28
+        <button disabled={speed <= 100} onClick={goFaster}>
29
+          Faster
30
+        </button>
31
+        <span>{speed}ms</span>
32
+      </div>
14 33
     </div>
15 34
   );
16 35
 };

+ 29
- 1
src/index.css View File

@@ -63,13 +63,41 @@ code {
63 63
 .controls button {
64 64
   background: transparent;
65 65
   border: 1px solid black;
66
+  border-right: none;
66 67
   cursor: pointer;
67 68
   flex: 1;
68 69
   font-size: 18px;
69 70
   padding: 0.25em 0;
70 71
 }
71 72
 
72
-.controls button:hover {
73
+.controls button:last-child,
74
+.controls .speed button:last-of-type {
75
+  border-right: 1px solid black;
76
+}
77
+
78
+.controls button:not([disabled]):hover {
73 79
   background: black;
74 80
   color: white;
75 81
 }
82
+
83
+.controls .speed {
84
+  align-items: center;
85
+  display: flex;
86
+  flex: 1;
87
+  justify-content: center;
88
+}
89
+
90
+.controls .speed button,
91
+.controls .speed span {
92
+  flex: 1;
93
+}
94
+
95
+.controls .speed span {
96
+  font-weight: bold;
97
+  text-align: center;
98
+}
99
+
100
+.controls .speed button[disabled] {
101
+  cursor: not-allowed;
102
+  opacity: 0.6;
103
+}

Loading…
Cancel
Save