Show first N users that can fit

This commit is contained in:
David Rowe 2015-03-25 17:44:14 -07:00
parent 8e66f5998a
commit 567375368e

View file

@ -40,6 +40,7 @@ var usersWindow = (function () {
usersOnline, // Raw users data usersOnline, // Raw users data
linesOfUsers = [], // Array of indexes pointing into usersOnline linesOfUsers = [], // Array of indexes pointing into usersOnline
numUsersToDisplay = 0,
API_URL = "https://metaverse.highfidelity.com/api/v1/users?status=online", API_URL = "https://metaverse.highfidelity.com/api/v1/users?status=online",
HTTP_GET_TIMEOUT = 60000, // ms = 1 minute HTTP_GET_TIMEOUT = 60000, // ms = 1 minute
@ -73,19 +74,23 @@ var usersWindow = (function () {
function calculateWindowHeight() { function calculateWindowHeight() {
var AUDIO_METER_HEIGHT = 52, var AUDIO_METER_HEIGHT = 52,
MIRROR_HEIGHT = 220, MIRROR_HEIGHT = 220,
nonUsersHeight,
maxWindowHeight; maxWindowHeight;
// Reserve 5 lines for window heading plus visibility heading and controls // Reserve 5 lines for window heading plus visibility heading and controls
// Subtract windowLineSpacing for both end of user list and end of controls // Subtract windowLineSpacing for both end of user list and end of controls
windowHeight = (linesOfUsers.length > 0 ? linesOfUsers.length + 5 : 5) * windowLineHeight nonUsersHeight = 5 * windowLineHeight - 2 * windowLineSpacing + VISIBILITY_SPACER_2D + 2 * WINDOW_MARGIN_2D;
- 2 * windowLineSpacing + VISIBILITY_SPACER_2D + 2 * WINDOW_MARGIN_2D;
// Limit to height of window minus VU meter and mirror if displayed // Limit window to height of viewport minus VU meter and mirror if displayed
windowHeight = linesOfUsers.length * windowLineHeight + nonUsersHeight;
maxWindowHeight = viewportHeight - AUDIO_METER_HEIGHT; maxWindowHeight = viewportHeight - AUDIO_METER_HEIGHT;
if (isMirrorDisplay && !isFullscreenMirror) { if (isMirrorDisplay && !isFullscreenMirror) {
maxWindowHeight -= MIRROR_HEIGHT; maxWindowHeight -= MIRROR_HEIGHT;
} }
windowHeight = Math.min(windowHeight, maxWindowHeight); windowHeight = Math.min(windowHeight, maxWindowHeight);
// Corresponding number of users to actually display
numUsersToDisplay = Math.round((windowHeight - nonUsersHeight) / windowLineHeight);
} }
function updateOverlayPositions() { function updateOverlayPositions() {
@ -130,46 +135,39 @@ var usersWindow = (function () {
function updateUsersDisplay() { function updateUsersDisplay() {
var displayText = "", var displayText = "",
myUsername,
user, user,
userText, userText,
textWidth, textWidth,
maxTextWidth, maxTextWidth,
ellipsisWidth,
reducedTextWidth,
i; i;
myUsername = GlobalServices.username; maxTextWidth = WINDOW_WIDTH_2D - SCROLLBAR_BACKGROUND_WIDTH_2D - 2 * WINDOW_MARGIN_2D;
linesOfUsers = []; ellipsisWidth = Overlays.textSize(windowPane2D, "...").width;
for (i = 0; i < usersOnline.length; i += 1) { reducedTextWidth = maxTextWidth - ellipsisWidth;
user = usersOnline[i];
if (user.username !== myUsername && user.online) {
userText = user.username;
if (user.location.root) {
userText += " @ " + user.location.root.name;
}
textWidth = Overlays.textSize(windowPane2D, userText).width;
maxTextWidth = WINDOW_WIDTH_2D - SCROLLBAR_BACKGROUND_WIDTH_2D - 2 * WINDOW_MARGIN_2D; for (i = 0; i < numUsersToDisplay; i += 1) {
if (textWidth > maxTextWidth) { user = usersOnline[linesOfUsers[i]];
// Trim and append "..." to fit window width userText = user.text;
maxTextWidth = maxTextWidth - Overlays.textSize(windowPane2D, "...").width; print(userText);
while (textWidth > maxTextWidth) { textWidth = user.textWidth;
userText = userText.slice(0, -1);
textWidth = Overlays.textSize(windowPane2D, userText).width; if (textWidth > maxTextWidth) {
} // Trim and append "..." to fit window width
userText += "..."; maxTextWidth = maxTextWidth - Overlays.textSize(windowPane2D, "...").width;
while (textWidth > reducedTextWidth) {
userText = userText.slice(0, -1);
textWidth = Overlays.textSize(windowPane2D, userText).width; textWidth = Overlays.textSize(windowPane2D, userText).width;
} }
userText += "...";
usersOnline[i].textWidth = textWidth;
linesOfUsers.push(i);
displayText += "\n" + userText;
} }
displayText += "\n" + userText;
} }
displayText = displayText.slice(1); // Remove leading "\n". displayText = displayText.slice(1); // Remove leading "\n".
calculateWindowHeight();
Overlays.editOverlay(windowPane2D, { Overlays.editOverlay(windowPane2D, {
y: viewportHeight - windowHeight, y: viewportHeight - windowHeight,
height: windowHeight, height: windowHeight,
@ -183,11 +181,11 @@ var usersWindow = (function () {
Overlays.editOverlay(scrollbarBackground2D, { Overlays.editOverlay(scrollbarBackground2D, {
y: viewportHeight - windowHeight + WINDOW_MARGIN_2D + windowTextHeight, y: viewportHeight - windowHeight + WINDOW_MARGIN_2D + windowTextHeight,
height: linesOfUsers.length * windowLineHeight - windowLineSpacing / 2 height: numUsersToDisplay * windowLineHeight - windowLineSpacing / 2
}); });
Overlays.editOverlay(scrollbarBar2D, { Overlays.editOverlay(scrollbarBar2D, {
y: viewportHeight - windowHeight + WINDOW_MARGIN_2D + windowTextHeight + 1, y: viewportHeight - windowHeight + WINDOW_MARGIN_2D + windowTextHeight + 1,
height: linesOfUsers.length * windowLineHeight / 3 // TODO height: numUsersToDisplay * windowLineHeight / 3 // TODO
}); });
updateOverlayPositions(); updateOverlayPositions();
@ -203,7 +201,11 @@ var usersWindow = (function () {
} }
processUsers = function () { processUsers = function () {
var response; var response,
myUsername,
user,
userText,
i;
if (usersRequest.readyState === usersRequest.DONE) { if (usersRequest.readyState === usersRequest.DONE) {
if (usersRequest.status === 200) { if (usersRequest.status === 200) {
@ -220,7 +222,26 @@ var usersWindow = (function () {
} }
usersOnline = response.data.users; usersOnline = response.data.users;
myUsername = GlobalServices.username;
linesOfUsers = [];
for (i = 0; i < usersOnline.length; i += 1) {
user = usersOnline[i];
if (user.username !== myUsername && user.online) {
userText = user.username;
if (user.location.root) {
userText += " @ " + user.location.root.name;
}
usersOnline[i].text = userText;
usersOnline[i].textWidth = Overlays.textSize(windowPane2D, userText).width;
linesOfUsers.push(i);
}
}
calculateWindowHeight();
updateUsersDisplay(); updateUsersDisplay();
} else { } else {
print("Error: Request for users status returned " + usersRequest.status + " " + usersRequest.statusText); print("Error: Request for users status returned " + usersRequest.status + " " + usersRequest.statusText);
usersTimer = Script.setTimeout(pollUsers, HTTP_GET_TIMEOUT); // Try again after a longer delay. usersTimer = Script.setTimeout(pollUsers, HTTP_GET_TIMEOUT); // Try again after a longer delay.
@ -346,6 +367,7 @@ var usersWindow = (function () {
|| isMirrorDisplay !== oldIsMirrorDisplay || isMirrorDisplay !== oldIsMirrorDisplay
|| isFullscreenMirror !== oldIsFullscreenMirror) { || isFullscreenMirror !== oldIsFullscreenMirror) {
calculateWindowHeight(); calculateWindowHeight();
updateUsersDisplay();
updateOverlayPositions(); updateOverlayPositions();
} }
} }
@ -361,10 +383,10 @@ var usersWindow = (function () {
radioButtonDiameter = RADIO_BUTTON_DISPLAY_SCALE * windowTextHeight; radioButtonDiameter = RADIO_BUTTON_DISPLAY_SCALE * windowTextHeight;
Overlays.deleteOverlay(textSizeOverlay); Overlays.deleteOverlay(textSizeOverlay);
calculateWindowHeight();
viewportHeight = Controller.getViewportDimensions().y; viewportHeight = Controller.getViewportDimensions().y;
calculateWindowHeight();
windowPane2D = Overlays.addOverlay("text", { windowPane2D = Overlays.addOverlay("text", {
x: 0, x: 0,
y: viewportHeight, // Start up off-screen y: viewportHeight, // Start up off-screen