Files
ueblueprint/js/entity/PinEntity.js
2023-01-07 17:57:06 +01:00

272 lines
9.5 KiB
JavaScript
Executable File

import ByteEntity from "./ByteEntity"
import FunctionReferenceEntity from "./FunctionReferenceEntity"
import GuidEntity from "./GuidEntity"
import IEntity from "./IEntity"
import Integer64Entity from "./Integer64Entity"
import IntegerEntity from "./IntegerEntity"
import LinearColorEntity from "./LinearColorEntity"
import LocalizedTextEntity from "./LocalizedTextEntity"
import ObjectReferenceEntity from "./ObjectReferenceEntity"
import PathSymbolEntity from "./PathSymbolEntity"
import PinReferenceEntity from "./PinReferenceEntity"
import PinTypeEntity from "./PinTypeEntity"
import RotatorEntity from "./RotatorEntity"
import SimpleSerializationRotatorEntity from "./SimpleSerializationRotatorEntity"
import SimpleSerializationVector2DEntity from "./SimpleSerializationVector2DEntity"
import SimpleSerializationVectorEntity from "./SimpleSerializationVectorEntity"
import SubAttributesDeclaration from "./SubObject"
import UnionType from "./UnionType"
import Utility from "../Utility"
import Vector2DEntity from "./Vector2DEntity"
import VectorEntity from "./VectorEntity"
/** @typedef {import("./IEntity").AnyValue} AnyValue */
/** @template {AnyValue} T */
export default class PinEntity extends IEntity {
static #typeEntityMap = {
"/Script/CoreUObject.LinearColor": LinearColorEntity,
"/Script/CoreUObject.Rotator": RotatorEntity,
"/Script/CoreUObject.Vector": VectorEntity,
"/Script/CoreUObject.Vector2D": Vector2DEntity,
"bool": Boolean,
"byte": ByteEntity,
"exec": String,
"int": IntegerEntity,
"int64": Integer64Entity,
"name": String,
"real": Number,
"string": String,
}
static #alternativeTypeEntityMap = {
"/Script/CoreUObject.Vector2D": SimpleSerializationVector2DEntity,
"/Script/CoreUObject.Vector": SimpleSerializationVectorEntity,
"/Script/CoreUObject.Rotator": SimpleSerializationRotatorEntity,
}
static lookbehind = "Pin"
static attributes = {
PinId: {
type: GuidEntity,
},
PinName: "",
PinFriendlyName: {
type: new UnionType(LocalizedTextEntity, String),
showDefault: false,
},
PinToolTip: {
type: String,
showDefault: false,
},
Direction: {
type: String,
showDefault: false,
},
PinType: new SubAttributesDeclaration({
PinCategory: "",
PinSubCategory: "",
PinSubCategoryObject: {
type: ObjectReferenceEntity,
},
PinSubCategoryMemberReference: {
type: FunctionReferenceEntity,
value: null,
},
PinValueType: {
type: PinTypeEntity,
value: null,
},
ContainerType: {
type: PathSymbolEntity,
},
bIsReference: false,
bIsConst: false,
bIsWeakPointer: false,
bIsUObjectWrapper: false,
bSerializeAsSinglePrecisionFloat: false,
}),
LinkedTo: {
type: [PinReferenceEntity],
showDefault: false,
},
DefaultValue: {
/** @param {PinEntity} pinEntity */
type: pinEntity => pinEntity.getEntityType(true) ?? String,
serialized: true,
showDefault: false,
},
AutogeneratedDefaultValue: {
type: String,
showDefault: false,
},
DefaultObject: {
type: ObjectReferenceEntity,
showDefault: false,
value: null,
},
PersistentGuid: {
type: GuidEntity,
},
bHidden: false,
bNotConnectable: false,
bDefaultValueIsReadOnly: false,
bDefaultValueIsIgnored: false,
bAdvancedView: false,
bOrphanedPin: false,
}
static {
this.cleanupAttributes(this.attributes)
}
constructor(values = {}, suppressWarns = false) {
super(values, suppressWarns)
/** @type {GuidEntity} */ this.PinId
/** @type {String} */ this.PinName
/** @type {LocalizedTextEntity | String} */ this.PinFriendlyName
/** @type {String} */ this.PinToolTip
/** @type {String} */ this.Direction
/**
* @type {{
* PinCategory: String,
* PinSubCategory: String,
* PinSubCategoryObject: ObjectReferenceEntity,
* PinSubCategoryMemberReference: FunctionReferenceEntity,
* PinValueType: PinTypeEntity,
* ContainerType: PathSymbolEntity,
* bIsReference: Boolean,
* bIsConst: Boolean,
* bIsWeakPointer: Boolean,
* bIsUObjectWrapper: Boolean,
* bSerializeAsSinglePrecisionFloat: Boolean,
* }}
*/ this.PinType
/** @type {PinReferenceEntity[]} */ this.LinkedTo
/** @type {T} */ this.DefaultValue
/** @type {String} */ this.AutogeneratedDefaultValue
/** @type {ObjectReferenceEntity} */ this.DefaultObject
/** @type {GuidEntity} */ this.PersistentGuid
/** @type {Boolean} */ this.bHidden
/** @type {Boolean} */ this.bNotConnectable
/** @type {Boolean} */ this.bDefaultValueIsReadOnly
/** @type {Boolean} */ this.bDefaultValueIsIgnored
/** @type {Boolean} */ this.bAdvancedView
/** @type {Boolean} */ this.bOrphanedPin
}
getType() {
if (this.PinType.PinCategory == "struct" || this.PinType.PinCategory == "object") {
return this.PinType.PinSubCategoryObject.path
}
return this.PinType.PinCategory
}
getEntityType(alternative = false) {
const typeString = this.getType()
const entity = PinEntity.#typeEntityMap[typeString]
const alternativeEntity = PinEntity.#alternativeTypeEntityMap[typeString]
return alternative && alternativeEntity !== undefined
? alternativeEntity
: entity
}
getDisplayName() {
let matchResult = null
if (
this.PinToolTip
// Match up until the first \n excluded or last character
&& (matchResult = this.PinToolTip.match(/\s*(.+?(?=\n)|.+\S)\s*/))
) {
return Utility.formatStringName(matchResult[1])
}
return Utility.formatStringName(this.PinName)
}
/** @param {PinEntity} other */
copyTypeFrom(other) {
this.PinType.PinCategory = other.PinType.PinCategory
this.PinType.PinSubCategory = other.PinType.PinSubCategory
this.PinType.PinSubCategoryObject = other.PinType.PinSubCategoryObject
this.PinType.PinSubCategoryMemberReference = other.PinType.PinSubCategoryMemberReference
this.PinType.PinValueType = other.PinType.PinValueType
this.PinType.ContainerType = other.PinType.ContainerType
this.PinType.bIsReference = other.PinType.bIsReference
this.PinType.bIsConst = other.PinType.bIsConst
this.PinType.bIsWeakPointer = other.PinType.bIsWeakPointer
this.PinType.bIsUObjectWrapper = other.PinType.bIsUObjectWrapper
this.PinType.bSerializeAsSinglePrecisionFloat = other.PinType.bSerializeAsSinglePrecisionFloat
}
getDefaultValue(maybeCreate = false) {
if (this.DefaultValue === undefined && maybeCreate) {
this.DefaultValue = new (this.getEntityType(true))()
}
return this.DefaultValue
}
isExecution() {
return this.PinType.PinCategory === "exec"
}
isHidden() {
return this.bHidden
}
isInput() {
return !this.bHidden && this.Direction != "EGPD_Output"
}
isOutput() {
return !this.bHidden && this.Direction == "EGPD_Output"
}
isLinked() {
return this.LinkedTo?.length > 0 ?? false
}
/**
* @param {String} targetObjectName
* @param {PinEntity} targetPinEntity
*/
linkTo(targetObjectName, targetPinEntity) {
/** @type {PinReferenceEntity[]} */
this.LinkedTo
const linkFound = this.LinkedTo?.find(pinReferenceEntity => {
return pinReferenceEntity.objectName.toString() == targetObjectName
&& pinReferenceEntity.pinGuid.valueOf() == targetPinEntity.PinId.valueOf()
})
if (!linkFound) {
(this.LinkedTo ?? (this.LinkedTo = [])).push(new PinReferenceEntity({
objectName: targetObjectName,
pinGuid: targetPinEntity.PinId,
}))
return true
}
return false
}
/**
* @param {String} targetObjectName
* @param {PinEntity} targetPinEntity
*/
unlinkFrom(targetObjectName, targetPinEntity) {
const indexElement = this.LinkedTo?.findIndex(pinReferenceEntity => {
return pinReferenceEntity.objectName.toString() == targetObjectName
&& pinReferenceEntity.pinGuid.valueOf() == targetPinEntity.PinId.valueOf()
})
if (indexElement >= 0) {
if (this.LinkedTo.length == 1) {
this.LinkedTo = undefined
} else {
this.LinkedTo.splice(indexElement, 1)
}
return true
}
return false
}
getSubCategory() {
return this.PinType.PinSubCategoryObject.path
}
}