Small fixes

This commit is contained in:
barsdeveloper
2022-01-08 18:57:37 +01:00
parent a6ff4161e8
commit 41b741e8b8
12 changed files with 175 additions and 116 deletions

View File

@@ -22,8 +22,6 @@ export default class Blueprint extends GraphElement {
this.template
/** @type {number} */
this.gridSize = Configuration.gridSize
/** @type {number} */
this.gridSnap = Configuration.gridSnap
/** @type {GraphNode[]}" */
this.nodes = []
/** @type {GraphLink[]}" */
@@ -216,10 +214,7 @@ export default class Blueprint extends GraphElement {
}
snapToGrid(location) {
return [
this.gridSnap * Math.round(location[0] / this.gridSnap),
this.gridSnap * Math.round(location[1] / this.gridSnap)
]
return Utility.snapToGrid(location, this.gridSize)
}
/**
@@ -357,7 +352,7 @@ export default class Blueprint extends GraphElement {
setFocused(value = true) {
if (this.focused == value) {
return;
return
}
let event = new CustomEvent(value ? "blueprint-focus" : "blueprint-unfocus")
this.focused = value

View File

@@ -3,5 +3,4 @@ export default class Configuration {
static deleteNodesKeyboardKey = "Delete"
static expandGridSize = 400
static gridSize = 16
static gridSnap = 16
}

View File

@@ -1,3 +1,4 @@
import Configuration from "./Configuration"
import TypeInitialization from "./entity/TypeInitialization"
export default class Utility {
@@ -77,4 +78,19 @@ export default class Utility {
return constructor
}
}
/**
*
* @param {Number[]} location
* @param {Number} gridSize
*/
static snapToGrid(location, gridSize = Configuration.gridSize) {
if (gridSize === 1) {
return location
}
return [
gridSize * Math.round(location[0] / gridSize),
gridSize * Math.round(location[1] / gridSize)
]
}
}

View File

@@ -2,6 +2,7 @@ import Entity from "./Entity"
export default class LocalizedTextEntity extends Entity {
static lookbehind = "NSLOCTEXT"
static attributes = {
namespace: String,
key: String,

View File

@@ -1,6 +1,5 @@
import GraphElement from "./GraphElement"
import LinkTemplate from "../template/LinkTemplate"
import GraphPin from "./GraphPin"
/**

View File

@@ -61,4 +61,11 @@ export default class SelectableDraggable extends GraphElement {
})
this.blueprint.dispatchEvent(dragEvent)
}
snapToGrid() {
let snappedLocation = this.blueprint.snapToGrid(this.location)
if (this.location[0] != snappedLocation[0] || this.location[1] != snappedLocation[1]) {
this.setLocation(snappedLocation)
}
}
}

View File

@@ -45,6 +45,7 @@ export default class Paste extends Context {
]
node.addLocation(this.blueprint.compensateTranslation(locationOffset))
node.setSelected(true)
node.snapToGrid()
})
}
}

View File

@@ -1,39 +1,50 @@
import MouseClickDrag from "./MouseClickDrag"
import Utility from "../../Utility"
/**
* @typedef {import("../../graph/SelectableDraggable").default} SelectableDraggable
*/
export default class MouseMoveNodes extends MouseClickDrag {
/**
*
* @param {SelectableDraggable} target
* @param {*} blueprint
* @param {*} options
*/
constructor(target, blueprint, options) {
super(target, blueprint, options)
this.stepSize = parseInt(options?.stepSize)
this.mousePosition = [0, 0]
}
snapToGrid(location) {
return [
this.stepSize * Math.round(location[0] / this.stepSize),
this.stepSize * Math.round(location[1] / this.stepSize)
]
this.stepSize = parseInt(options?.stepSize ?? this.blueprint.gridSize)
this.mouseLocation = [0, 0]
/** @type {SelectableDraggable} */
this.target
}
startDrag() {
if (isNaN(this.stepSize) || this.stepSize <= 0) {
this.stepSize = this.blueprint.gridSnap
}
// Get the current mouse position
this.mousePosition = this.stepSize != 1 ? this.snapToGrid(this.clickedPosition) : this.clickedPosition
this.mouseLocation = Utility.snapToGrid(this.clickedPosition, this.stepSize)
}
dragTo(location, movement) {
const mousePosition = this.stepSize != 1 ? this.snapToGrid(location) : location
const d = [mousePosition[0] - this.mousePosition[0], mousePosition[1] - this.mousePosition[1]]
const [mouseLocation, targetLocation] = this.stepSize > 1
? [Utility.snapToGrid(location, this.stepSize), Utility.snapToGrid(this.target.location, this.stepSize)]
: [location, this.target.location]
const d = [
mouseLocation[0] - this.mouseLocation[0],
mouseLocation[1] - this.mouseLocation[1]
]
if (d[0] == 0 && d[1] == 0) {
return
}
// Make sure it snaps on the grid
d[0] += targetLocation[0] - this.target.location[0]
d[1] += targetLocation[1] - this.target.location[1]
this.target.dispatchDragEvent(d)
// Reassign the position of mouse
this.mousePosition = mousePosition
this.mouseLocation = mouseLocation
}
}

View File

@@ -4,8 +4,9 @@ import Serializer from "./Serializer"
export default class GeneralSerializer extends Serializer {
constructor(wrap, entityType, prefix, separator, trailingSeparator, attributeValueConjunctionSign, attributeKeyPrinter) {
wrap = wrap ?? (v => `(${v})`)
super(entityType, prefix, separator, trailingSeparator, attributeValueConjunctionSign, attributeKeyPrinter)
this.wrap = wrap ?? (v => `(${v})`)
this.wrap = wrap
}
read(value) {

View File

@@ -48,7 +48,7 @@ export default class Grammar {
AttributeName = r => r.Word.sepBy1(P.string(".")).tieWith(".").desc('words separated by ""')
AttributeAnyValue = r => P.alt(r.Null, r.None, r.Boolean, r.Number, r.Integer, r.String, r.Guid, r.Reference, r.LocalizedText)
LocalizedText = r => P.seqMap(
P.string("NSLOCTEXT").skip(P.optWhitespace).skip(P.string("(")),
P.string(LocalizedTextEntity.lookbehind).skip(P.optWhitespace).skip(P.string("(")),
r.String.trim(P.optWhitespace), // namespace
P.string(","),
r.String.trim(P.optWhitespace), // key

View File

@@ -20,7 +20,7 @@ export default function initializeSerializerFactory() {
)
SerializerFactory.registerSerializer(
PinEntity,
new GeneralSerializer(v => `Pin (${v})`, PinEntity, "", ",", true)
new GeneralSerializer(v => `${PinEntity.lookbehind} (${v})`, PinEntity, "", ",", true)
)
SerializerFactory.registerSerializer(
FunctionReferenceEntity,
@@ -28,7 +28,7 @@ export default function initializeSerializerFactory() {
)
SerializerFactory.registerSerializer(
LocalizedTextEntity,
new GeneralSerializer(v => `NSLOCTEXT(${v})`, LocalizedTextEntity, "", ",", false, "", _ => "")
new GeneralSerializer(v => `${LocalizedTextEntity.lookbehind}(${v})`, LocalizedTextEntity, "", ",", false, "", _ => "")
)
SerializerFactory.registerSerializer(
PinReferenceEntity,