entity list auto scroll

This commit is contained in:
Thijs Wenker 2018-10-05 22:13:45 +02:00
parent daa13b3706
commit 27e0c85b01
4 changed files with 55 additions and 13 deletions

View file

@ -194,7 +194,7 @@ function loaded() {
if (selectedIndex >= 0) {
selection = [];
selection = selection.concat(selectedEntities);
selection.splice(selectedIndex, 1)
selection.splice(selectedIndex, 1);
} else {
selection = selection.concat(selectedEntities);
}
@ -453,7 +453,10 @@ function loaded() {
}
}
function updateSelectedEntities(selectedIDs) {
function updateSelectedEntities(selectedIDs, autoScroll) {
// force autoScroll to be a boolean
autoScroll = !!autoScroll;
let notFound = false;
// reset all currently selected entities and their rows first
@ -482,6 +485,26 @@ function loaded() {
}
});
if (autoScroll && selectedIDs.length > 0) {
let firstItem = Number.MAX_VALUE;
let lastItem = -1;
let itemFound = false;
visibleEntities.forEach(function(entity, index) {
if (selectedIDs.indexOf(entity.id) !== -1) {
if (firstItem > index) {
firstItem = index;
}
if (lastItem < index) {
lastItem = index;
}
itemFound = true;
}
});
if (itemFound) {
entityList.scrollToRow(firstItem, lastItem);
}
}
refreshFooter();
return notFound;
@ -640,8 +663,8 @@ function loaded() {
data = JSON.parse(data);
if (data.type === "clearEntityList") {
clearEntities();
} else if (data.type === "selectionUpdate") {
let notFound = updateSelectedEntities(data.selectedIDs);
} else if (data.type === "selectionUpdate" && data.caller !== "entityList") {
let notFound = updateSelectedEntities(data.selectedIDs, true);
if (notFound) {
refreshEntities();
}
@ -653,13 +676,13 @@ function loaded() {
clearEntities();
} else {
updateEntityData(newEntities);
updateSelectedEntities(data.selectedIDs);
updateSelectedEntities(data.selectedIDs, true);
}
}
});
} else if (data.type === "removeEntities" && data.deletedIDs !== undefined && data.selectedIDs !== undefined) {
removeEntities(data.deletedIDs);
updateSelectedEntities(data.selectedIDs);
updateSelectedEntities(data.selectedIDs, true);
} else if (data.type === "deleted" && data.ids) {
removeEntities(data.ids);
}

View file

@ -152,6 +152,24 @@ ListView.prototype = {
this.refresh();
}
},
scrollToRow(rowIndex, lastRowIndex) {
lastRowIndex = lastRowIndex ? lastRowIndex : rowIndex;
let boundingTop = rowIndex * this.rowHeight;
let boundingBottom = (lastRowIndex * this.rowHeight) + this.rowHeight;
if ((boundingBottom - boundingTop) > this.elTableScroll.clientHeight) {
boundingBottom = boundingTop + this.elTableScroll.clientHeight;
}
let currentVisibleAreaTop = this.elTableScroll.scrollTop;
let currentVisibleAreaBottom = currentVisibleAreaTop + this.elTableScroll.clientHeight;
if (boundingTop < currentVisibleAreaTop) {
this.elTableScroll.scrollTop = boundingTop;
} else if (boundingBottom > currentVisibleAreaBottom) {
this.elTableScroll.scrollTop = boundingBottom - (this.elTableScroll.clientHeight);
}
},
refresh: function() {
// block refreshing before rows are initialized

View file

@ -98,7 +98,7 @@ EntityListTool = function(shouldUseEditTabletApp) {
that.setVisible(!visible);
};
selectionManager.addEventListener(function() {
selectionManager.addEventListener(function(isSelectionUpdate, caller) {
var selectedIDs = [];
for (var i = 0; i < selectionManager.selections.length; i++) {
@ -107,7 +107,8 @@ EntityListTool = function(shouldUseEditTabletApp) {
emitJSONScriptEvent({
type: 'selectionUpdate',
selectedIDs: selectedIDs
selectedIDs: selectedIDs,
caller: caller
});
});
@ -224,7 +225,7 @@ EntityListTool = function(shouldUseEditTabletApp) {
for (var i = 0; i < ids.length; i++) {
entityIDs.push(ids[i]);
}
selectionManager.setSelections(entityIDs);
selectionManager.setSelections(entityIDs, "entityList");
if (data.focus) {
cameraManager.enable();
cameraManager.focus(selectionManager.worldPosition,

View file

@ -136,7 +136,7 @@ SelectionManager = (function() {
return that.selections.length > 0;
};
that.setSelections = function(entityIDs) {
that.setSelections = function(entityIDs, caller) {
that.selections = [];
for (var i = 0; i < entityIDs.length; i++) {
var entityID = entityIDs[i];
@ -144,7 +144,7 @@ SelectionManager = (function() {
Selection.addToSelectedItemsList(HIGHLIGHT_LIST_NAME, "entity", entityID);
}
that._update(true);
that._update(true, caller);
};
that.addEntity = function(entityID, toggleSelection) {
@ -477,7 +477,7 @@ SelectionManager = (function() {
undoHistory.pushCommand(undo, copiedProperties, redo, copiedProperties);
}
that._update = function(selectionUpdated) {
that._update = function(selectionUpdated, caller) {
var properties = null;
if (that.selections.length === 0) {
that.localDimensions = null;
@ -542,7 +542,7 @@ SelectionManager = (function() {
for (var j = 0; j < listeners.length; j++) {
try {
listeners[j](selectionUpdated === true);
listeners[j](selectionUpdated === true, caller);
} catch (e) {
print("ERROR: entitySelectionTool.update got exception: " + JSON.stringify(e));
}