瀏覽代碼

Redesign controls

master
Dylan Baker 4 年之前
父節點
當前提交
cd05055086
共有 3 個文件被更改,包括 85 次插入41 次删除
  1. 10
    1
      src/Board.tsx
  2. 22
    8
      src/Controls.tsx
  3. 53
    32
      src/index.css

+ 10
- 1
src/Board.tsx 查看文件

7
 const DEFAULT_HEIGHT = 10;
7
 const DEFAULT_HEIGHT = 10;
8
 const DEFAULT_SPEED = 500;
8
 const DEFAULT_SPEED = 500;
9
 
9
 
10
+const INITIAL_GRID = Array(DEFAULT_HEIGHT).fill(
11
+  Array(DEFAULT_WIDTH).fill(false),
12
+);
13
+
10
 interface State {
14
 interface State {
11
   grid: boolean[][];
15
   grid: boolean[][];
12
   running: boolean;
16
   running: boolean;
21
   constructor(props: {}) {
25
   constructor(props: {}) {
22
     super(props);
26
     super(props);
23
     this.state = {
27
     this.state = {
24
-      grid: Array(DEFAULT_HEIGHT).fill(Array(DEFAULT_WIDTH).fill(false)),
28
+      grid: [...INITIAL_GRID],
25
       running: false,
29
       running: false,
26
       speed: DEFAULT_SPEED,
30
       speed: DEFAULT_SPEED,
27
       width: DEFAULT_WIDTH,
31
       width: DEFAULT_WIDTH,
41
     if (this.timer) clearInterval(this.timer);
45
     if (this.timer) clearInterval(this.timer);
42
   };
46
   };
43
 
47
 
48
+  reset = () => {
49
+    this.setState({ grid: [...INITIAL_GRID] });
50
+  };
51
+
44
   toggleCell = (x: number, y: number) => {
52
   toggleCell = (x: number, y: number) => {
45
     const newGrid = [...this.state.grid];
53
     const newGrid = [...this.state.grid];
46
     const newRow = [...newGrid[y]];
54
     const newRow = [...newGrid[y]];
104
           height={height}
112
           height={height}
105
           updateDimensions={this.updateDimensions}
113
           updateDimensions={this.updateDimensions}
106
           updateSpeed={this.updateSpeed}
114
           updateSpeed={this.updateSpeed}
115
+          reset={this.reset}
107
         />
116
         />
108
         {grid.map((row, y) => (
117
         {grid.map((row, y) => (
109
           <div className="row" key={y}>
118
           <div className="row" key={y}>

+ 22
- 8
src/Controls.tsx 查看文件

5
   speed: number;
5
   speed: number;
6
   width: number;
6
   width: number;
7
   height: number;
7
   height: number;
8
+  reset: () => void;
8
   toggle: () => void;
9
   toggle: () => void;
9
   updateSpeed: (speed: number) => void;
10
   updateSpeed: (speed: number) => void;
10
   updateDimensions: (dimensions: { width?: number; height?: number }) => void;
11
   updateDimensions: (dimensions: { width?: number; height?: number }) => void;
18
     updateSpeed,
19
     updateSpeed,
19
     width,
20
     width,
20
     height,
21
     height,
22
+    reset,
21
     updateDimensions,
23
     updateDimensions,
22
   } = props;
24
   } = props;
23
 
25
 
33
 
35
 
34
   return (
36
   return (
35
     <div className="controls">
37
     <div className="controls">
36
-      <div className="controls__row --width">
38
+      <div className="controls__section dimensions">
37
         <label>
39
         <label>
38
           Width{' '}
40
           Width{' '}
39
           <input
41
           <input
42
+            className="number-field"
40
             type="number"
43
             type="number"
41
             value={width}
44
             value={width}
42
             onChange={(e) =>
45
             onChange={(e) =>
47
         <label>
50
         <label>
48
           Height{' '}
51
           Height{' '}
49
           <input
52
           <input
53
+            className="number-field"
50
             type="number"
54
             type="number"
51
             value={height}
55
             value={height}
52
             onChange={(e) =>
56
             onChange={(e) =>
55
           />
59
           />
56
         </label>
60
         </label>
57
       </div>
61
       </div>
58
-      <div className="controls__row">
59
-        <button onClick={toggle}>{running ? 'Pause' : 'Play'}</button>
60
-        <div className="speed">
61
-          <button onClick={goSlower}>Slower</button>
62
-          <button disabled={speed <= 100} onClick={goFaster}>
63
-            Faster
62
+      <div className="controls__section speed">
63
+        <button className="button" onClick={goSlower}>
64
+          Slower
65
+        </button>
66
+        <button className="button" disabled={speed <= 100} onClick={goFaster}>
67
+          Faster
68
+        </button>
69
+      </div>
70
+      <div className="controls__section play-reset">
71
+        <div className="reset">
72
+          <button className="button" onClick={reset}>
73
+            Reset
74
+          </button>
75
+        </div>
76
+        <div className="play">
77
+          <button className="button" onClick={toggle}>
78
+            {running ? 'Pause' : 'Play'}
64
           </button>
79
           </button>
65
-          <span>{speed}ms</span>
66
         </div>
80
         </div>
67
       </div>
81
       </div>
68
     </div>
82
     </div>

+ 53
- 32
src/index.css 查看文件

55
 }
55
 }
56
 
56
 
57
 .controls {
57
 .controls {
58
+  background: black;
59
+  color: white;
58
   display: flex;
60
   display: flex;
59
-  flex-direction: column;
61
+  left: 0;
62
+  padding: 1em;
63
+  position: absolute;
64
+  top: 0;
60
   width: 100%;
65
   width: 100%;
61
 }
66
 }
62
 
67
 
63
-.controls button {
64
-  background: transparent;
65
-  border: 1px solid black;
66
-  border-right: none;
68
+.button {
69
+  background: black;
70
+  border: 1px solid white;
71
+  color: white;
67
   cursor: pointer;
72
   cursor: pointer;
68
-  flex: 1;
69
   font-size: 18px;
73
   font-size: 18px;
70
-  padding: 0.25em 0;
71
-}
72
-
73
-.controls button:last-child,
74
-.controls .speed button:last-of-type {
75
-  border-right: 1px solid black;
74
+  margin-right: 1em;
75
+  padding: 0.25em 0.5em;
76
 }
76
 }
77
 
77
 
78
-.controls button:not([disabled]):hover {
79
-  background: black;
80
-  color: white;
78
+.button:not([disabled]):hover {
79
+  background: white;
80
+  color: black;
81
 }
81
 }
82
 
82
 
83
-.controls .controls__row {
83
+.controls__section {
84
+  align-items: center;
84
   display: flex;
85
   display: flex;
85
-  margin: 1em 0;
86
+  margin: 0.5em 0;
86
 }
87
 }
87
 
88
 
88
-.controls .speed {
89
+.speed {
89
   align-items: center;
90
   align-items: center;
90
   display: flex;
91
   display: flex;
91
   flex: 1;
92
   flex: 1;
92
-  justify-content: center;
93
+  justify-content: flex-end;
93
 }
94
 }
94
 
95
 
95
-.controls .speed button,
96
-.controls .speed span {
97
-  flex: 1;
96
+.button[disabled] {
97
+  cursor: not-allowed;
98
+  opacity: 0.6;
98
 }
99
 }
99
 
100
 
100
-.controls .speed span {
101
-  font-weight: bold;
102
-  text-align: center;
101
+.controls .play {
102
+  text-align: right;
103
 }
103
 }
104
 
104
 
105
-.controls .speed button[disabled] {
106
-  cursor: not-allowed;
107
-  opacity: 0.6;
105
+.play .button {
106
+  background: #347b20;
108
 }
107
 }
109
 
108
 
110
-.--width label {
111
-  flex: 1;
109
+.play .button:hover {
110
+  background: #4c9c36;
111
+  color: white;
112
 }
112
 }
113
 
113
 
114
-.--width input {
115
-  border: 1px solid black;
114
+.reset .button {
115
+  background: #7b2020;
116
+}
117
+
118
+.reset .button:hover {
119
+  background: #9c3636;
120
+  color: white;
121
+}
122
+
123
+.number-field {
116
   font-size: 16px;
124
   font-size: 16px;
125
+  display: inline-block;
126
+  margin-left: 0.25em;
127
+  text-align: center;
128
+  width: 50px;
129
+}
130
+
131
+.dimensions label {
132
+  margin-right: 1em;
133
+}
134
+
135
+.play-reset {
136
+  flex: 1;
137
+  justify-content: flex-end;
117
 }
138
 }

Loading…
取消
儲存