mirror of
https://github.com/lubosz/overte.git
synced 2025-04-07 05:42:08 +02:00
adding support for filters to get old properties
This commit is contained in:
parent
cf3e9f78c9
commit
5a843b1342
4 changed files with 47 additions and 4 deletions
|
@ -42,7 +42,7 @@ QList<EntityItemID> EntityEditFilters::getZonesByPosition(glm::vec3& position) {
|
|||
}
|
||||
|
||||
bool EntityEditFilters::filter(glm::vec3& position, EntityItemProperties& propertiesIn, EntityItemProperties& propertiesOut, bool& wasChanged,
|
||||
EntityTree::FilterType filterType, EntityItemID& itemID) {
|
||||
EntityTree::FilterType filterType, EntityItemID& itemID, EntityItemPointer& existingEntity) {
|
||||
|
||||
// get the ids of all the zones (plus the global entity edit filter) that the position
|
||||
// lies within
|
||||
|
@ -68,9 +68,15 @@ bool EntityEditFilters::filter(glm::vec3& position, EntityItemProperties& proper
|
|||
propertiesIn.setDesiredProperties(oldProperties);
|
||||
|
||||
auto in = QJsonValue::fromVariant(inputValues.toVariant()); // grab json copy now, because the inputValues might be side effected by the filter.
|
||||
|
||||
// get the current properties for then entity and include them for the filter call
|
||||
auto currentProperties = existingEntity ? existingEntity->getProperties() : EntityItemProperties();
|
||||
QScriptValue currentValues = currentProperties.copyToScriptValue(filterData.engine, false, true, true);
|
||||
|
||||
QScriptValueList args;
|
||||
args << inputValues;
|
||||
args << filterType;
|
||||
args << currentValues;
|
||||
|
||||
QScriptValue result = filterData.filterFn.call(_nullObjectForFilter, args);
|
||||
if (filterData.uncaughtExceptions()) {
|
||||
|
@ -182,8 +188,8 @@ static bool hadUncaughtExceptions(QScriptEngine& engine, const QString& fileName
|
|||
void EntityEditFilters::scriptRequestFinished(EntityItemID entityID) {
|
||||
qDebug() << "script request completed for entity " << entityID;
|
||||
auto scriptRequest = qobject_cast<ResourceRequest*>(sender());
|
||||
const QString urlString = scriptRequest->getUrl().toString();
|
||||
if (scriptRequest && scriptRequest->getResult() == ResourceRequest::Success) {
|
||||
const QString urlString = scriptRequest->getUrl().toString();
|
||||
auto scriptContents = scriptRequest->getData();
|
||||
qInfo() << "Downloaded script:" << scriptContents;
|
||||
QScriptProgram program(scriptContents, urlString);
|
||||
|
@ -227,6 +233,7 @@ void EntityEditFilters::scriptRequestFinished(EntityItemID entityID) {
|
|||
}
|
||||
}
|
||||
} else if (scriptRequest) {
|
||||
const QString urlString = scriptRequest->getUrl().toString();
|
||||
qCritical() << "Failed to download script at" << urlString;
|
||||
// See HTTPResourceRequest::onRequestFinished for interpretation of codes. For example, a 404 is code 6 and 403 is 3. A timeout is 2. Go figure.
|
||||
qCritical() << "ResourceRequest error was" << scriptRequest->getResult();
|
||||
|
|
|
@ -43,7 +43,7 @@ public:
|
|||
void removeFilter(EntityItemID entityID);
|
||||
|
||||
bool filter(glm::vec3& position, EntityItemProperties& propertiesIn, EntityItemProperties& propertiesOut, bool& wasChanged,
|
||||
EntityTree::FilterType filterType, EntityItemID& entityID);
|
||||
EntityTree::FilterType filterType, EntityItemID& entityID, EntityItemPointer& existingEntity);
|
||||
|
||||
signals:
|
||||
void filterAdded(EntityItemID id, bool success);
|
||||
|
|
|
@ -1099,7 +1099,7 @@ bool EntityTree::filterProperties(EntityItemPointer& existingEntity, EntityItemP
|
|||
if (entityEditFilters) {
|
||||
auto position = existingEntity ? existingEntity->getWorldPosition() : propertiesIn.getPosition();
|
||||
auto entityID = existingEntity ? existingEntity->getEntityItemID() : EntityItemID();
|
||||
accepted = entityEditFilters->filter(position, propertiesIn, propertiesOut, wasChanged, filterType, entityID);
|
||||
accepted = entityEditFilters->filter(position, propertiesIn, propertiesOut, wasChanged, filterType, entityID, existingEntity);
|
||||
}
|
||||
|
||||
return accepted;
|
||||
|
|
36
scripts/tutorials/entity_edit_filters/postition-example.js
Normal file
36
scripts/tutorials/entity_edit_filters/postition-example.js
Normal file
|
@ -0,0 +1,36 @@
|
|||
function filter(properties, type, originalProperties) {
|
||||
|
||||
/* Clamp position changes.*/
|
||||
var maxChange = 5;
|
||||
function sign(val) {
|
||||
if (val > 0) {
|
||||
return 1;
|
||||
} else if (val < 0) {
|
||||
return -1;
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
function clamp(val, min, max) {
|
||||
if (val > max) {
|
||||
val = max;
|
||||
} else if (val < min) {
|
||||
val = min;
|
||||
}
|
||||
return val;
|
||||
}
|
||||
|
||||
|
||||
if (properties.position) {
|
||||
/* Random near-zero value used as "zero" to prevent two sequential updates from being
|
||||
exactly the same (which would cause them to be ignored) */
|
||||
var nearZero = 0.0001 * Math.random() + 0.001;
|
||||
var maxFudgeChange = (maxChange + nearZero);
|
||||
properties.position.x = clamp(properties.position.x, originalProperties.position.x - maxFudgeChange, originalProperties.position.x + maxFudgeChange);
|
||||
properties.position.y = clamp(properties.position.y, originalProperties.position.y - maxFudgeChange, originalProperties.position.y + maxFudgeChange);
|
||||
properties.position.z = clamp(properties.position.z, originalProperties.position.z - maxFudgeChange, originalProperties.position.z + maxFudgeChange);
|
||||
}
|
||||
|
||||
return properties;
|
||||
}
|
Loading…
Reference in a new issue