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

Individual pet sizes - for #412 #419

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 5 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,11 @@
"command": "vscode-pets.roll-call",
"title": "Roll-call",
"category": "Pet Coding"
},
{
"command": "vscode-pets.change-pet-size",
"title": "Change pet size",
"category": "Pet Coding"
}
],
"configuration": [
Expand Down Expand Up @@ -200,17 +205,6 @@
"description": "Pet type",
"scope": "window"
},
"vscode-pets.petSize": {
"type": "string",
"enum": [
"nano",
"small",
"medium",
"large"
],
"default": "nano",
"description": "Pet size"
},
"vscode-pets.position": {
"type": "string",
"enum": [
Expand Down
2 changes: 1 addition & 1 deletion src/common/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export const enum PetSpeed {
veryFast = 5,
}

export const enum PetSize {
export enum PetSize {
nano = 'nano',
small = 'small',
medium = 'medium',
Expand Down
100 changes: 87 additions & 13 deletions src/extension/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
const EXTRA_PETS_KEY_TYPES = EXTRA_PETS_KEY + '.types';
const EXTRA_PETS_KEY_COLORS = EXTRA_PETS_KEY + '.colors';
const EXTRA_PETS_KEY_NAMES = EXTRA_PETS_KEY + '.names';
const DEFAULT_PET_SCALE = PetSize.nano;
const DEFAULT_PET_SCALE = PetSize.medium;
const DEFAULT_COLOR = PetColor.brown;
const DEFAULT_PET_TYPE = PetType.cat;
const DEFAULT_POSITION = ExtPosition.panel;
Expand Down Expand Up @@ -49,12 +49,12 @@

let webviewViewProvider: PetWebviewViewProvider;

function getConfiguredSize(): PetSize {
function getConfiguredSize(petSize?: PetSize | any): PetSize {
var size = vscode.workspace
.getConfiguration('vscode-pets')
.get<PetSize>('petSize', DEFAULT_PET_SCALE);
.get<PetSize>('petSize', petSize);
if (ALL_SCALES.lastIndexOf(size) === -1) {
size = DEFAULT_PET_SCALE;
size = petSize;
}
return size;
}
Expand Down Expand Up @@ -127,11 +127,15 @@
var type = vscode.workspace
.getConfiguration('vscode-pets')
.get<PetType>('petType', DEFAULT_PET_TYPE);

var size = vscode.workspace
.getConfiguration('vscode-pets')
.get<PetSize>('petSize');
if (ALL_PETS.lastIndexOf(type) === -1) {
type = DEFAULT_PET_TYPE;
}

return new PetSpecification(color, type, getConfiguredSize());
return new PetSpecification(color, type, getConfiguredSize(size));
}

static collectionFromMemento(
Expand Down Expand Up @@ -192,6 +196,7 @@
interface IPetInfo {
type: PetType;
name: string;
size: PetSize;
color: PetColor;
}

Expand All @@ -210,6 +215,7 @@
petList.push({
type: parts[0] as PetType,
name: parts[1],
size: parts[3] as PetSize,
color: parts[2] as PetColor,
});
});
Expand Down Expand Up @@ -305,7 +311,7 @@
if (PetPanel.currentPanel) {
var collection = PetSpecification.collectionFromMemento(
context,
getConfiguredSize(),
getConfiguredSize(DEFAULT_PET_SCALE),
);
collection.forEach((item) => {
PetPanel.currentPanel?.spawnPet(item);
Expand Down Expand Up @@ -399,7 +405,7 @@
async () => {
const pets = PetSpecification.collectionFromMemento(
context,
getConfiguredSize(),
getConfiguredSize(DEFAULT_PET_SCALE),
);
const petJson = JSON.stringify(pets, null, 2);
const fileName = `pets-${Date.now()}.json`;
Expand Down Expand Up @@ -466,7 +472,7 @@
// load the pets into the collection
var collection = PetSpecification.collectionFromMemento(
context,
getConfiguredSize(),
getConfiguredSize(DEFAULT_PET_SCALE),
);
// fetch just the pet types
const panel = getPetPanel();
Expand Down Expand Up @@ -549,6 +555,24 @@
return;
}

const selectedSize = await vscode.window.showQuickPick(
Object.values(PetSize).map((value) => ({
label: value,
value: value,
})),
{
placeHolder: 'Select a size',
},
);
if (selectedSize === undefined) {
return;
}

// store the selectedSize in the vscode.workspace
vscode.workspace

Check warning on line 572 in src/extension/extension.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

Promises must be awaited, end with a call to .catch, end with a call to .then with a rejection handler or be explicitly marked as ignored with the `void` operator

Check warning on line 572 in src/extension/extension.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

Promises must be awaited, end with a call to .catch, end with a call to .then with a rejection handler or be explicitly marked as ignored with the `void` operator
.getConfiguration('vscode-pets')
.update('petSize', selectedSize.value);

const name = await vscode.window.showInputBox({
placeHolder: vscode.l10n.t('Leave blank for a random name'),
prompt: vscode.l10n.t('Name your pet'),
Expand All @@ -557,7 +581,7 @@
const spec = new PetSpecification(
petColor,
selectedPetType.value,
getConfiguredSize(),
getConfiguredSize(selectedSize.value),
name,
);
if (!spec.type || !spec.color || !spec.size) {
Expand All @@ -569,7 +593,7 @@
}
var collection = PetSpecification.collectionFromMemento(
context,
getConfiguredSize(),
getConfiguredSize(selectedSize.value),
);
collection.push(spec);
await storeCollectionAsMemento(context, collection);
Expand Down Expand Up @@ -604,14 +628,64 @@
),
);

context.subscriptions.push(
vscode.commands.registerCommand(
'vscode-pets.change-pet-size',
async () => {
const panel = getPetPanel();
if (panel !== undefined) {
// show quick pick of all the pets
const petList = PetSpecification.collectionFromMemento(
context,
getConfiguredSize(DEFAULT_PET_SCALE),
);
const selectedPet = await vscode.window.showQuickPick(
petList.map((pet) => ({
label: pet.name,
description: `${pet.color} ${pet.type}`,
value: pet,
})),
{
placeHolder: vscode.l10n.t('Select a pet'),
},
);
if (selectedPet === undefined) {
return;
}
const selectedSize = await vscode.window.showQuickPick(
Object.values(PetSize).map((value) => ({
label: value,
value: value,
})),
{
placeHolder: 'Select a size',
},
);
if (selectedSize === undefined) {
return;
}
selectedPet.value.size = selectedSize.value;
panel.spawnPet(selectedPet.value);
await storeCollectionAsMemento(context, petList);
} else {
await createPetPlayground(context);
await vscode.window.showInformationMessage(
vscode.l10n.t(
"A Pet Playground has been created. You can now use the 'Change Pet Size' Command to change the size of the pets.",
),
);
}
},
),
);

// Listening to configuration changes
context.subscriptions.push(
vscode.workspace.onDidChangeConfiguration(
(e: vscode.ConfigurationChangeEvent): void => {
if (
e.affectsConfiguration('vscode-pets.petColor') ||
e.affectsConfiguration('vscode-pets.petType') ||
e.affectsConfiguration('vscode-pets.petSize') ||
e.affectsConfiguration('vscode-pets.theme') ||
e.affectsConfiguration('workbench.colorTheme')
) {
Expand Down Expand Up @@ -1152,7 +1226,7 @@
if (PetPanel.currentPanel) {
var collection = PetSpecification.collectionFromMemento(
context,
getConfiguredSize(),
getConfiguredSize(DEFAULT_PET_SCALE),
);
collection.forEach((item) => {
PetPanel.currentPanel?.spawnPet(item);
Expand All @@ -1161,7 +1235,7 @@
} else {
var collection = PetSpecification.collectionFromMemento(
context,
getConfiguredSize(),
getConfiguredSize(DEFAULT_PET_SCALE),
);
collection.push(spec);
await storeCollectionAsMemento(context, collection);
Expand Down
Loading