Added token category option

This commit is contained in:
Mitchell McCaffrey 2020-08-27 19:09:16 +10:00
parent a912163079
commit 3c27f65d0f
7 changed files with 70 additions and 25 deletions

View File

@ -187,14 +187,29 @@ function Map({
setIsTokenMenuOpen(true); setIsTokenMenuOpen(true);
} }
function getMapTokenCategoryWeight(category) {
switch (category) {
case "character":
return 0;
case "vehicle":
return 1;
case "prop":
return 2;
default:
return 0;
}
}
// Sort so vehicles render below other tokens // Sort so vehicles render below other tokens
function sortMapTokenStates(a, b, draggingTokenOptions) { function sortMapTokenStates(a, b, draggingTokenOptions) {
const tokenA = tokensById[a.tokenId]; const tokenA = tokensById[a.tokenId];
const tokenB = tokensById[b.tokenId]; const tokenB = tokensById[b.tokenId];
if (tokenA && tokenB) { if (tokenA && tokenB) {
// If one token is a vehicle and one isn't sort vehicles below // If categories are different sort in order "prop", "vehicle", "character"
if (tokenB.isVehicle !== tokenA.isVehicle) { if (tokenB.category !== tokenA.category) {
return tokenB.isVehicle - tokenA.isVehicle; const aWeight = getMapTokenCategoryWeight(tokenA.category);
const bWeight = getMapTokenCategoryWeight(tokenB.category);
return bWeight - aWeight;
} else if ( } else if (
draggingTokenOptions && draggingTokenOptions &&
draggingTokenOptions.tokenState.id === a.id draggingTokenOptions.tokenState.id === a.id
@ -206,6 +221,7 @@ function Map({
draggingTokenOptions.tokenState.id === b.id draggingTokenOptions.tokenState.id === b.id
) { ) {
// If dragging token b move above // If dragging token b move above
return -1; return -1;
} else { } else {
// Else sort so last modified is on top // Else sort so last modified is on top

View File

@ -53,10 +53,10 @@ function MapToken({
const tokenGroup = event.target; const tokenGroup = event.target;
const tokenImage = imageRef.current; const tokenImage = imageRef.current;
if (token && token.isVehicle) { if (token && token.category === "vehicle") {
// Find all other tokens on the map // Find all other tokens on the map
const layer = tokenGroup.getLayer(); const layer = tokenGroup.getLayer();
const tokens = layer.find(".token"); const tokens = layer.find(".character");
for (let other of tokens) { for (let other of tokens) {
if (other === tokenGroup) { if (other === tokenGroup) {
continue; continue;
@ -101,9 +101,9 @@ function MapToken({
const tokenGroup = event.target; const tokenGroup = event.target;
const mountChanges = {}; const mountChanges = {};
if (token && token.isVehicle) { if (token && token.category === "vehicle") {
const parent = tokenGroup.getParent(); const parent = tokenGroup.getParent();
const mountedTokens = tokenGroup.find(".token"); const mountedTokens = tokenGroup.find(".character");
for (let mountedToken of mountedTokens) { for (let mountedToken of mountedTokens) {
// Save and restore token position after moving layer // Save and restore token position after moving layer
const position = mountedToken.absolutePosition(); const position = mountedToken.absolutePosition();
@ -227,10 +227,10 @@ function MapToken({
return null; return null;
} }
// Token name is used by on click to find whether a token is a vehicle // Token name is used by on click to find whether a token is a vehicle or prop
let tokenName = ""; let tokenName = "";
if (token) { if (token) {
tokenName = token.isVehicle ? "vehicle" : "token"; tokenName = token.category;
} }
if (tokenState && tokenState.locked) { if (tokenState && tokenState.locked) {
tokenName = tokenName + "-locked"; tokenName = tokenName + "-locked";

View File

@ -66,7 +66,7 @@ function TokenDragOverlay({
useEffect(() => { useEffect(() => {
function handleTokenDragEnd() { function handleTokenDragEnd() {
// Handle other tokens when a vehicle gets deleted // Handle other tokens when a vehicle gets deleted
if (token && token.isVehicle) { if (token && token.category === "vehicle") {
const layer = tokenGroup.getLayer(); const layer = tokenGroup.getLayer();
const mountedTokens = tokenGroup.find(".token"); const mountedTokens = tokenGroup.find(".token");
for (let mountedToken of mountedTokens) { for (let mountedToken of mountedTokens) {

View File

@ -1,9 +1,23 @@
import React from "react"; import React from "react";
import { Flex, Box, Input, IconButton, Label, Checkbox } from "theme-ui"; import {
Flex,
Box,
Input,
IconButton,
Label,
Checkbox,
Select,
} from "theme-ui";
import ExpandMoreIcon from "../../icons/ExpandMoreIcon"; import ExpandMoreIcon from "../../icons/ExpandMoreIcon";
import { isEmpty } from "../../helpers/shared"; import { isEmpty } from "../../helpers/shared";
const categorySettings = [
{ id: "character", name: "Character" },
{ id: "prop", name: "Prop" },
{ id: "vehicle", name: "Vehicle / Mount" },
];
function TokenSettings({ function TokenSettings({
token, token,
onSettingsChange, onSettingsChange,
@ -43,18 +57,21 @@ function TokenSettings({
</Box> </Box>
<Flex my={2}> <Flex my={2}>
<Box sx={{ flexGrow: 1 }}> <Box sx={{ flexGrow: 1 }}>
<Label> <Label>Category</Label>
<Checkbox <Select
checked={token && token.isVehicle} my={1}
disabled={tokenEmpty || token.type === "default"} value={!tokenEmpty && token.category}
onChange={(e) => disabled={tokenEmpty || token.type === "default"}
onSettingsChange("isVehicle", e.target.checked) onChange={(e) => onSettingsChange("category", e.target.value)}
} >
/> {categorySettings.map((category) => (
Vehicle / Mount <option key={category.id} value={category.id}>
</Label> {category.name}
</option>
))}
</Select>
</Box> </Box>
<Box sx={{ flexGrow: 1 }}> <Flex sx={{ flexGrow: 1, alignItems: "center" }} ml={2}>
<Label> <Label>
<Checkbox <Checkbox
checked={token && token.hideInSidebar} checked={token && token.hideInSidebar}
@ -65,7 +82,7 @@ function TokenSettings({
/> />
Hide in Sidebar Hide in Sidebar
</Label> </Label>
</Box> </Flex>
</Flex> </Flex>
</> </>
)} )}

View File

@ -172,6 +172,18 @@ function loadVersions(db) {
} }
}); });
}); });
// v1.5.1 - Added token prop category and remove isVehicle bool
db.version(10)
.stores({})
.upgrade((tx) => {
return tx
.table("tokens")
.toCollection()
.modify((token) => {
token.category = token.isVehicle ? "vehicle" : "character";
delete token.isVehicle;
});
});
} }
// Get the dexie database used in DatabaseContext // Get the dexie database used in DatabaseContext

View File

@ -77,7 +77,7 @@ function SelectTokensModal({ isOpen, onRequestClose }) {
lastModified: Date.now(), lastModified: Date.now(),
owner: userId, owner: userId,
defaultSize: 1, defaultSize: 1,
isVehicle: false, category: "character",
hideInSidebar: false, hideInSidebar: false,
}); });
setImageLoading(false); setImageLoading(false);

View File

@ -85,7 +85,7 @@ export const tokens = Object.keys(tokenSources).map((key) => ({
name: Case.capital(key), name: Case.capital(key),
type: "default", type: "default",
defaultSize: getDefaultTokenSize(key), defaultSize: getDefaultTokenSize(key),
isVehicle: false, category: "character",
hideInSidebar: false, hideInSidebar: false,
})); }));