better default agent props & speed & position numeric, not strings
This commit is contained in:
@@ -47,7 +47,13 @@ app.helpers.formBuilder = {
|
||||
}
|
||||
if(component){
|
||||
component.classList.add('formbuilder-field')
|
||||
component.value = this.getPathInObj(fieldsValues, propName) || fieldsObj[propName].default
|
||||
const rawValue = this.getPathInObj(fieldsValues, propName) ?? fieldsObj[propName].default
|
||||
if(fieldsObj[propName].type === 'number') {
|
||||
const n = Number(rawValue)
|
||||
component.value = Number.isFinite(n) ? n : 0
|
||||
} else {
|
||||
component.value = rawValue
|
||||
}
|
||||
fieldRow.append(component)
|
||||
allFields.push(fieldRow)
|
||||
}
|
||||
@@ -77,6 +83,14 @@ app.helpers.formBuilder = {
|
||||
return(target[parts[parts.length - 1]] )
|
||||
},
|
||||
|
||||
#fieldValue(el) {
|
||||
if(el?.type === 'number') {
|
||||
const n = Number(el.value)
|
||||
return(Number.isFinite(n) ? n : el.value)
|
||||
}
|
||||
return(el.value)
|
||||
},
|
||||
|
||||
getFieldsValues(rootSel){
|
||||
const result = {}
|
||||
document.querySelectorAll(`${rootSel} .formbuilder-field`).forEach(el => {
|
||||
@@ -89,14 +103,14 @@ app.helpers.formBuilder = {
|
||||
}
|
||||
target = target[key]
|
||||
}
|
||||
target[path[path.length - 1]] = el.value
|
||||
target[path[path.length - 1]] = this.#fieldValue(el)
|
||||
})
|
||||
return(result)
|
||||
},
|
||||
|
||||
getFieldValue(rootSel, name){
|
||||
const comp = document.querySelector(`${rootSel} .formbuilder-field[name="${name}"]`)
|
||||
if(comp) return(comp.value)
|
||||
if(comp) return(this.#fieldValue(comp))
|
||||
else return(null)
|
||||
},
|
||||
|
||||
|
||||
@@ -43,7 +43,15 @@ class AgentsModel extends WindozModel {
|
||||
async getDefaultProps(id){
|
||||
const aprops = await this.getProperties(id)
|
||||
const defaults={ position: { x:0, y:0, z:0 }, speed: { x:0, y:0, z:0 }}
|
||||
for(const p in aprops) defaults[p] = aprops[p].default
|
||||
for(const p in aprops) {
|
||||
const prop = aprops[p]
|
||||
if(prop?.type === 'number') {
|
||||
const n = Number(prop.default)
|
||||
defaults[p] = Number.isFinite(n) ? n : 0
|
||||
} else {
|
||||
defaults[p] = prop.default
|
||||
}
|
||||
}
|
||||
return(defaults)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -38,14 +38,31 @@ class KeyframesModel extends WindozModel {
|
||||
)
|
||||
}
|
||||
|
||||
#normalizeAxisVector(vec) {
|
||||
if(!vec || typeof(vec) !== 'object') return(null)
|
||||
const axes = ['x', 'y', 'z']
|
||||
const out = {}
|
||||
for(const axis of axes) {
|
||||
const n = Number(vec[axis])
|
||||
if(!Number.isFinite(n)) return(null)
|
||||
out[axis] = n
|
||||
}
|
||||
return(out)
|
||||
}
|
||||
|
||||
async save(kfId, data) {
|
||||
const kfData = Object.keys(data).map(aid => {
|
||||
const { position, speed, ...storeValues} = data[aid].values
|
||||
const gpsPosition = this.#normalizeAxisVector(position)
|
||||
const gpsSpeed = this.#normalizeAxisVector(speed)
|
||||
if(!gpsPosition || !gpsSpeed) {
|
||||
throw(new Error(`Agent ${aid}: position and speed must be numeric vectors`))
|
||||
}
|
||||
return({
|
||||
aid: aid,
|
||||
type: data[aid].type,
|
||||
storeValues: storeValues,
|
||||
gpsValues: { position: data[aid].values.position, speed: data[aid].values.speed }
|
||||
gpsValues: { position: gpsPosition, speed: gpsSpeed },
|
||||
})
|
||||
})
|
||||
|
||||
|
||||
@@ -207,14 +207,14 @@ class KeyframeView extends WindozDomContent {
|
||||
if(this.currentlySelectedAid && this.kfArena.agents[this.currentlySelectedAid]){
|
||||
const AgentValues = this.getFieldsValues('div[data-output="agentProperties"]')
|
||||
this.kfArena.agents[this.currentlySelectedAid].values = AgentValues
|
||||
const val = Number.parseInt(comp.value, 10)
|
||||
if((comp.name.startsWith('position.')) && (!Number.isNaN(val))){
|
||||
const val = Number(comp.value)
|
||||
if((comp.name.startsWith('position.')) && (Number.isFinite(val))){
|
||||
this.kfArena.moveAgent(this.currentlySelectedAid, {
|
||||
x: this.getFieldValue('div[data-output="agentProperties"]', 'position.x'),
|
||||
y: this.getFieldValue('div[data-output="agentProperties"]', 'position.y'),
|
||||
z: this.getFieldValue('div[data-output="agentProperties"]', 'position.z'),
|
||||
})
|
||||
} else if((comp.name.startsWith('speed.')) && (!Number.isNaN(val))){
|
||||
} else if((comp.name.startsWith('speed.')) && (Number.isFinite(val))){
|
||||
this.kfArena.changeAgentSpeed(this.currentlySelectedAid, {
|
||||
x: this.getFieldValue('div[data-output="agentProperties"]', 'speed.x'),
|
||||
y: this.getFieldValue('div[data-output="agentProperties"]', 'speed.y'),
|
||||
|
||||
Reference in New Issue
Block a user