forked from CipherYuvraj/Algorithm-Visualiser-Platform
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathControlPanel.jsx
More file actions
251 lines (236 loc) · 9.94 KB
/
ControlPanel.jsx
File metadata and controls
251 lines (236 loc) · 9.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
import React, { useCallback, useEffect } from 'react';
import { Play, Pause, RotateCcw, SkipBack, SkipForward, Settings, Volume2, VolumeX, RefreshCw, SplitSquareHorizontal } from 'lucide-react';
import { useAlgorithmSettings } from '../../contexts/AlgorithmSettingsContext';
const ControlPanel = ({
isPlaying,
onPlayPause,
onReset,
onSpeedChange,
speed,
currentStep,
totalSteps,
isLoading,
onStepForward,
onStepBackward,
onCompareMode,
onThemeChange,
}) => {
const { settings, updateSettings, resetSettings } = useAlgorithmSettings();
const getSpeedLabel = (speed) => {
if (speed < 300) return 'Very Fast';
if (speed < 500) return 'Fast';
if (speed < 700) return 'Medium';
if (speed < 900) return 'Slow';
return 'Very Slow';
};
// Fixed speed calculation - now higher values = faster
const speedPercentage = Math.round(((1000 - speed) / 800) * 100);
return (
<div className="space-y-4">
{/* Main Controls */}
<div className="flex flex-wrap gap-2">
<div className="flex space-x-2 flex-1">
<button
onClick={onStepBackward}
disabled={currentStep === 0 || isPlaying}
className={`px-3 py-2 ${settings.theme === 'neon' ? 'bg-gray-800 text-purple-400 hover:bg-gray-700' : 'bg-gray-100 text-gray-700 hover:bg-gray-200'} rounded-lg disabled:opacity-50 disabled:cursor-not-allowed transition-all`}
>
<SkipBack className="h-4 w-4" />
</button>
<button
onClick={onPlayPause}
disabled={isLoading}
className={`flex-1 flex items-center justify-center px-4 py-3 bg-gradient-to-r ${
settings.theme === 'neon'
? 'from-purple-500 to-pink-500 hover:from-purple-600 hover:to-pink-600'
: settings.theme === 'minimal'
? 'from-gray-700 to-gray-800 hover:from-gray-800 hover:to-gray-900'
: 'from-blue-500 to-blue-600 hover:from-blue-600 hover:to-blue-700'
} text-white font-semibold rounded-lg disabled:opacity-50 transition-all shadow-md`}
>
{isLoading ? (
<div className="animate-spin rounded-full h-4 w-4 border-2 border-white border-t-transparent" />
) : isPlaying ? (
<Pause className="h-4 w-4 mr-2" />
) : (
<Play className="h-4 w-4 mr-2" />
)}
{isLoading ? 'Loading...' : isPlaying ? 'Pause' : 'Play'}
</button>
<button
onClick={onStepForward}
disabled={currentStep >= totalSteps - 1 || isPlaying}
className={`px-3 py-2 ${settings.theme === 'neon' ? 'bg-gray-800 text-purple-400 hover:bg-gray-700' : 'bg-gray-100 text-gray-700 hover:bg-gray-200'} rounded-lg disabled:opacity-50 disabled:cursor-not-allowed transition-all`}
>
<SkipForward className="h-4 w-4" />
</button>
<button
onClick={onReset}
className={`px-3 py-2 ${settings.theme === 'neon' ? 'bg-gray-800 text-purple-400 hover:bg-gray-700' : 'bg-gray-100 text-gray-700 hover:bg-gray-200'} rounded-lg transition-all`}
>
<RotateCcw className="h-4 w-4" />
</button>
</div>
<div className="flex space-x-2">
<button
onClick={() => updateSettings({ soundEffects: !settings.soundEffects })}
className={`px-3 py-2 ${settings.theme === 'neon' ? 'bg-gray-800 text-purple-400 hover:bg-gray-700' : 'bg-gray-100 text-gray-700 hover:bg-gray-200'} rounded-lg transition-all`}
title={settings.soundEffects ? 'Disable Sound Effects' : 'Enable Sound Effects'}
>
{settings.soundEffects ? <Volume2 className="h-4 w-4" /> : <VolumeX className="h-4 w-4" />}
</button>
<button
onClick={() => updateSettings({ compareMode: !settings.compareMode })}
className={`px-3 py-2 ${
settings.compareMode
? settings.theme === 'neon'
? 'bg-purple-600 text-white'
: 'bg-blue-500 text-white'
: settings.theme === 'neon'
? 'bg-gray-800 text-purple-400 hover:bg-gray-700'
: 'bg-gray-100 text-gray-700 hover:bg-gray-200'
} rounded-lg transition-all`}
title="Toggle Comparison Mode"
>
<SplitSquareHorizontal className="h-4 w-4" />
</button>
<button
onClick={resetSettings}
className={`px-3 py-2 ${settings.theme === 'neon' ? 'bg-gray-800 text-purple-400 hover:bg-gray-700' : 'bg-gray-100 text-gray-700 hover:bg-gray-200'} rounded-lg transition-all`}
title="Reset Settings"
>
<RefreshCw className="h-4 w-4" />
</button>
</div>
</div>
{/* Settings Panel */}
<div className="grid grid-cols-1 md:grid-cols-2 gap-4 mt-4">
{/* Speed Control */}
<div>
<div className="flex justify-between items-center mb-2">
<label className={`block text-sm font-medium ${settings.theme === 'neon' ? 'text-purple-200' : 'text-gray-700'}`}>
Speed: {speed}x
</label>
<div className="flex space-x-2">
{[0.1, 0.5, 1, 2, 5, 10].map((preset) => (
<button
key={preset}
onClick={() => onSpeedChange(preset)}
className={`px-2 py-1 text-xs rounded ${
speed === preset
? settings.theme === 'neon'
? 'bg-purple-600 text-white'
: settings.theme === 'minimal'
? 'bg-gray-700 text-white'
: 'bg-blue-500 text-white'
: settings.theme === 'neon'
? 'bg-gray-800 text-purple-400 hover:bg-gray-700'
: 'bg-gray-100 text-gray-700 hover:bg-gray-200'
}`}
>
{preset}x
</button>
))}
</div>
</div>
<input
type="range"
min="0.1"
max="10"
step="0.1"
value={speed}
onChange={(e) => onSpeedChange(parseFloat(e.target.value))}
className={`w-full h-3 rounded-lg appearance-none cursor-pointer ${
settings.theme === 'neon' ? 'bg-gray-800' : 'bg-gray-200'
}`}
style={{
background: `linear-gradient(to right, ${
settings.theme === 'neon'
? '#a855f7'
: settings.theme === 'minimal'
? '#374151'
: '#3b82f6'
} 0%, ${
settings.theme === 'neon'
? '#a855f7'
: settings.theme === 'minimal'
? '#374151'
: '#3b82f6'
} ${(speed / 10) * 100}%, ${
settings.theme === 'neon' ? '#1f2937' : '#e5e7eb'
} ${(speed / 10) * 100}%, ${
settings.theme === 'neon' ? '#1f2937' : '#e5e7eb'
} 100%)`
}}
/>
</div>
{/* Theme Selection */}
<div>
<label className={`block text-sm font-medium mb-2 ${
settings.theme === 'neon' ? 'text-purple-200' : 'text-gray-700'
}`}>
Theme
</label>
<div className="grid grid-cols-3 gap-2">
{['classic', 'neon', 'minimal'].map((theme) => (
<button
key={theme}
onClick={() => updateSettings({ theme })}
className={`px-3 py-2 text-sm rounded-lg capitalize ${
settings.theme === theme
? theme === 'neon'
? 'bg-purple-600 text-white'
: theme === 'minimal'
? 'bg-gray-700 text-white'
: 'bg-blue-500 text-white'
: theme === 'neon'
? 'bg-gray-800 text-purple-400 hover:bg-gray-700'
: 'bg-gray-100 text-gray-700 hover:bg-gray-200'
}`}
>
{theme}
</button>
))}
</div>
</div>
</div>
{/* Enhanced Progress Bar */}
<div>
<div className="flex justify-between items-center mb-2">
<span className="text-sm font-medium text-gray-700">Progress</span>
<span className="text-sm text-gray-600 font-mono">
{currentStep} / {totalSteps || 1}
</span>
</div>
<div className="w-full bg-gray-200 rounded-full h-3 overflow-hidden">
<div
className="bg-gradient-to-r from-blue-500 to-blue-600 h-3 rounded-full transition-all duration-300 ease-out shadow-sm"
style={{
width: `${totalSteps > 0 ? (currentStep / (totalSteps - 1)) * 100 : 0}%`
}}
/>
</div>
</div>
{/* Enhanced Stats */}
<div className="bg-gray-50 rounded-lg p-3">
<div className="text-sm text-gray-700 space-y-2">
<div className="flex justify-between">
<span className="text-gray-600">Current Step:</span>
<span className="font-semibold text-blue-600">{currentStep + 1}</span>
</div>
<div className="flex justify-between">
<span className="text-gray-600">Total Steps:</span>
<span className="font-semibold text-green-600">{totalSteps}</span>
</div>
<div className="flex justify-between">
<span className="text-gray-600">Completion:</span>
<span className="font-semibold text-purple-600">
{totalSteps > 0 ? Math.round((currentStep / (totalSteps - 1)) * 100) : 0}%
</span>
</div>
</div>
</div>
</div>
);
};
export default ControlPanel;