Skip to content

Commit 2175f24

Browse files
committed
Adding the colour options to the maps
1 parent 378422b commit 2175f24

6 files changed

Lines changed: 165 additions & 0 deletions

File tree

assets/fonts/PixelCode-Regular.otf

67.1 KB
Binary file not shown.

lib/project_view/configs/views/base.dart

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import "dart:math" as math;
44
import "package:file_picker/file_picker.dart";
55
import "package:flutter/material.dart";
66
import "package:flutter/services.dart";
7+
import "package:flutter_colorpicker/flutter_colorpicker.dart";
78

89
import "../../../main_menu/settings/setting_heading.dart";
910
import "../../../utils.dart";
@@ -107,11 +108,13 @@ class _Option extends StatelessWidget {
107108
final String title;
108109
final List<SettingsBodyBase> descriptionList;
109110
final Widget? subtitle;
111+
final Widget? button;
110112

111113
const _Option({
112114
required this.title,
113115
required this.descriptionList,
114116
this.subtitle,
117+
this.button,
115118
});
116119

117120
@override
@@ -124,6 +127,7 @@ class _Option extends StatelessWidget {
124127
descriptionList,
125128
),
126129
subtitle: subtitle,
130+
trailing: button,
127131
);
128132
}
129133
}
@@ -298,3 +302,106 @@ class ToggleOption extends StatelessWidget {
298302
);
299303
}
300304
}
305+
306+
class ColourOption extends StatelessWidget {
307+
final String title;
308+
final List<SettingsBodyBase> descriptionList;
309+
final Color colour;
310+
final String label;
311+
final void Function(Color colour, String hex) onPicked;
312+
313+
ColourOption({
314+
required this.title,
315+
required String description,
316+
required this.colour,
317+
required this.label,
318+
required this.onPicked,
319+
super.key,
320+
}) : descriptionList = [SettingsBodyText(description)];
321+
322+
const ColourOption.customDescription({
323+
required this.title,
324+
required this.descriptionList,
325+
required this.colour,
326+
required this.label,
327+
required this.onPicked,
328+
super.key,
329+
});
330+
331+
@override
332+
Widget build(BuildContext context) {
333+
final textColour = getTextColourForBackground(colour);
334+
return _Option(
335+
title: title,
336+
descriptionList: descriptionList,
337+
button: ElevatedButton.icon(
338+
style: ElevatedButton.styleFrom(backgroundColor: colour),
339+
icon: Icon(Icons.color_lens, color: textColour),
340+
label: Text(
341+
label,
342+
style: pixelCode.copyWith(color: textColour, fontWeight: .w400),
343+
),
344+
onPressed: () async {
345+
final pickedColour = await showDialog<Color>(
346+
context: context,
347+
builder: (BuildContext context) {
348+
Color pickingColour = colour;
349+
final controller = TextEditingController(
350+
text: label.replaceFirst("#", ""),
351+
);
352+
return AlertDialog(
353+
title: const Text("Pick a colour"),
354+
scrollable: true,
355+
content: Column(
356+
children: [
357+
ColorPicker(
358+
pickerColor: colour,
359+
onColorChanged: (Color value) => pickingColour = value,
360+
displayThumbColor: true,
361+
paletteType: PaletteType.hsv,
362+
labelTypes: const [],
363+
hexInputController: controller,
364+
enableAlpha: false,
365+
portraitOnly: true,
366+
),
367+
TextFormField(
368+
controller: controller,
369+
decoration: const InputDecoration(
370+
labelText: "Colour (in hex)",
371+
prefixText: "#",
372+
),
373+
autofocus: true,
374+
maxLength: 6,
375+
inputFormatters: [
376+
UpperCaseTextFormatter(),
377+
FilteringTextInputFormatter.allow(RegExp(kValidHexPattern)),
378+
],
379+
onEditingComplete: () => Navigator.of(context).pop(pickingColour),
380+
),
381+
],
382+
),
383+
actions: [
384+
TextButton(
385+
child: const Text("Cancel"),
386+
onPressed: () => Navigator.of(context).pop(),
387+
),
388+
TextButton(
389+
child: const Text("Confirm"),
390+
onPressed: () => Navigator.of(context).pop(pickingColour),
391+
),
392+
],
393+
);
394+
},
395+
);
396+
if (pickedColour == null) return; //Dialog was dismissed
397+
final String hex = colorToHex(
398+
pickedColour,
399+
includeHashSign: true,
400+
enableAlpha: false,
401+
);
402+
onPicked(pickedColour, hex);
403+
},
404+
),
405+
);
406+
}
407+
}

lib/project_view/configs/views/map.dart

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,32 @@ class _MapConfigViewState extends ConsumerState<MapConfigView> {
138138
onChanged: null,
139139
onEditingComplete: () => setState(validateAndSaveOptionsThatCannotBeBlank),
140140
),
141+
ColourOption(
142+
title: "Sky Colour",
143+
description: "The colour of the sky.",
144+
colour: colorFromHexColour(model.skyColor),
145+
label: model.skyColor,
146+
onPicked: (Color colour, String hex) => setState(() {
147+
model = model.copyWith(skyColor: hex);
148+
configFile!.changeValueInFile(
149+
MapConfigKeys.skyColor,
150+
jsonEncode(model.skyColor),
151+
);
152+
}),
153+
),
154+
ColourOption(
155+
title: "Void Colour",
156+
description: "The colour of the void.",
157+
colour: colorFromHexColour(model.voidColor),
158+
label: model.voidColor,
159+
onPicked: (Color colour, String hex) => setState(() {
160+
model = model.copyWith(voidColor: hex);
161+
configFile!.changeValueInFile(
162+
MapConfigKeys.voidColor,
163+
jsonEncode(model.voidColor),
164+
);
165+
}),
166+
),
141167
_DangerZone(configFile!),
142168
],
143169
);

lib/utils.dart

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,28 @@ const TextStyle pixelCode = TextStyle(
3535
fontFamily: "PixelCode",
3636
fontSize: 14,
3737
height: 1.2,
38+
fontWeight: .w200,
3839
);
3940

41+
Color getTextColourForBackground(Color backgroundColour) {
42+
return ThemeData.estimateBrightnessForColor(backgroundColour) == Brightness.dark
43+
? Colors.white
44+
: Colors.black;
45+
}
46+
47+
Color colorFromHexColour(String hexString) {
48+
final int ox = int.parse(hexString.replaceFirst("#", "0x")); //heehee
49+
50+
if (hexString.length == 1 + 6) {
51+
final int b = ox & 255;
52+
final int g = (ox >> 8) & 255;
53+
final int r = (ox >> 16) & 255;
54+
return Color.fromARGB(255, r, g, b);
55+
}
56+
57+
throw Exception("Failed to parse colour code: $hexString");
58+
}
59+
4060
/// Checks if the given file has the same SHA256 hash as the given hash.
4161
/// Returns true if the hashes match, false otherwise.
4262
Future<bool> checkHash(File file, String validHash) async {

pubspec.lock

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,14 @@ packages:
318318
description: flutter
319319
source: sdk
320320
version: "0.0.0"
321+
flutter_colorpicker:
322+
dependency: "direct main"
323+
description:
324+
name: flutter_colorpicker
325+
sha256: "969de5f6f9e2a570ac660fb7b501551451ea2a1ab9e2097e89475f60e07816ea"
326+
url: "https://pub.dev"
327+
source: hosted
328+
version: "1.1.0"
321329
flutter_lints:
322330
dependency: "direct dev"
323331
description:

pubspec.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ dependencies:
3535
file_picker: ^10.3.10
3636
flutter:
3737
sdk: flutter
38+
flutter_colorpicker: ^1.1.0
3839
flutter_riverpod: ^3.1.0
3940
freezed_annotation: ^3.1.0
4041
json_annotation: ^4.9.0
@@ -96,6 +97,9 @@ flutter:
9697
- family: PixelCode
9798
fonts:
9899
- asset: assets/fonts/PixelCode-ExtraLight.otf
100+
weight: 200
101+
- asset: assets/fonts/PixelCode-Regular.otf
102+
weight: 400
99103

100104
#
101105
# For details regarding fonts from package dependencies,

0 commit comments

Comments
 (0)