ability to mute all notifications or just certain types

This commit is contained in:
Thijs Wenker 2015-02-23 21:00:26 +01:00
parent d650a092c2
commit 793116b90c

View file

@ -38,11 +38,13 @@
// function onIncomingMessage(user, message) { // function onIncomingMessage(user, message) {
// //do stuff here; // //do stuff here;
// var text = "This is a notification"; // var text = "This is a notification";
// wordWrap(text); // var wrappedText = wordWrap(text);
// createNotification(wrappedText, NotificationType.SNAPSHOT);
// } // }
// //
// This new function must call wordWrap(text) if the length of message is longer than 42 chars or unknown. // This new function must call wordWrap(text) if the length of message is longer than 42 chars or unknown.
// wordWrap() will format the text to fit the notifications overlay and send it to createNotification(text). // wordWrap() will format the text to fit the notifications overlay and return it
// after that we will send it to createNotification(text).
// If the message is 42 chars or less you should bypass wordWrap() and call createNotification() directly. // If the message is 42 chars or less you should bypass wordWrap() and call createNotification() directly.
@ -50,12 +52,12 @@
// //
// 1. Add a key to the keyPressEvent(key). // 1. Add a key to the keyPressEvent(key).
// 2. Declare a text string. // 2. Declare a text string.
// 3. Call createNotifications(text) parsing the text. // 3. Call createNotifications(text, NotificationType) parsing the text.
// example: // example:
// var welcome; // var welcome;
// if (key.text == "q") { //queries number of users online // if (key.text == "q") { //queries number of users online
// var welcome = "There are " + GlobalServices.onlineUsers.length + " users online now."; // var welcome = "There are " + GlobalServices.onlineUsers.length + " users online now.";
// createNotification(welcome); // createNotification(welcome, NotificationType.USERS_ONLINE);
// } // }
Script.include("./libraries/globals.js"); Script.include("./libraries/globals.js");
Script.include("./libraries/soundArray.js"); Script.include("./libraries/soundArray.js");
@ -83,6 +85,46 @@ var last_users = GlobalServices.onlineUsers;
var users = []; var users = [];
var ctrlIsPressed = false; var ctrlIsPressed = false;
var ready = true; var ready = true;
var MENU_NAME = 'Tools > Notifications';
var PLAY_NOTIFICATION_SOUNDS_MENU_ITEM = "Play Notification Sounds";
var NOTIFICATION_MENU_ITEM_POST = " Notifications";
var PLAY_NOTIFICATION_SOUNDS_SETTING = "play_notification_sounds";
var PLAY_NOTIFICATION_SOUNDS_TYPE_SETTING_PRE = "play_notification_sounds_type_";
var NotificationType = {
UNKNOWN: 0,
USER_JOINS: 1,
USER_LEAVES: 2,
MUTE_TOGGLE: 3,
CHAT_MENTION: 4,
USERS_ONLINE: 5,
SNAPSHOT: 6,
WINDOW_RESIZE: 7,
properties: [
{ text: "User Join" },
{ text: "User Leave" },
{ text: "Mute Toggle" },
{ text: "Chat Mention" },
{ text: "Users Online" },
{ text: "Snapshot" },
{ text: "Window Resize" }
],
getTypeFromMenuItem: function(menuItemName) {
if (menuItemName.substr(menuItemName.length - NOTIFICATION_MENU_ITEM_POST.length) !== NOTIFICATION_MENU_ITEM_POST) {
return NotificationType.UNKNOWN;
}
var preMenuItemName = menuItemName.substr(0, menuItemName.length - NOTIFICATION_MENU_ITEM_POST.length);
for (type in this.properties) {
if (this.properties[type].text === preMenuItemName) {
return parseInt(type) + 1;
}
}
return NotificationType.UNKNOWN;
},
getMenuString: function(type) {
return this.properties[type - 1].text + NOTIFICATION_MENU_ITEM_POST;
}
};
var randomSounds = new SoundArray({ localOnly: true }, true); var randomSounds = new SoundArray({ localOnly: true }, true);
var numberOfSounds = 2; var numberOfSounds = 2;
@ -90,15 +132,6 @@ for (var i = 1; i <= numberOfSounds; i++) {
randomSounds.addSound(HIFI_PUBLIC_BUCKET + "sounds/UI/notification-general" + i + ".raw"); randomSounds.addSound(HIFI_PUBLIC_BUCKET + "sounds/UI/notification-general" + i + ".raw");
} }
// When our script shuts down, we should clean up all of our overlays
function scriptEnding() {
for (i = 0; i < notifications.length; i++) {
Overlays.deleteOverlay(notifications[i]);
Overlays.deleteOverlay(buttons[i]);
}
}
Script.scriptEnding.connect(scriptEnding);
var notifications = []; var notifications = [];
var buttons = []; var buttons = [];
var times = []; var times = [];
@ -211,8 +244,6 @@ function notify(notice, button, height) {
positions, positions,
last; last;
randomSounds.playRandom();
if (isOnHMD) { if (isOnHMD) {
// Calculate 3D values from 2D overlay properties. // Calculate 3D values from 2D overlay properties.
@ -257,7 +288,7 @@ function notify(notice, button, height) {
} }
// This function creates and sizes the overlays // This function creates and sizes the overlays
function createNotification(text) { function createNotification(text, notificationType) {
var count = (text.match(/\n/g) || []).length, var count = (text.match(/\n/g) || []).length,
breakPoint = 43.0, // length when new line is added breakPoint = 43.0, // length when new line is added
extraLine = 0, extraLine = 0,
@ -307,6 +338,12 @@ function createNotification(text) {
alpha: backgroundAlpha alpha: backgroundAlpha
}; };
if (Menu.isOptionChecked(PLAY_NOTIFICATION_SOUNDS_MENU_ITEM) &&
Menu.isOptionChecked(NotificationType.getMenuString(notificationType)))
{
randomSounds.playRandom();
}
notify(noticeProperties, buttonProperties, height); notify(noticeProperties, buttonProperties, height);
} }
@ -345,8 +382,7 @@ function stringDivider(str, slotWidth, spaceReplacer) {
// formats string to add newline every 43 chars // formats string to add newline every 43 chars
function wordWrap(str) { function wordWrap(str) {
var result = stringDivider(str, 43.0, "\n"); return stringDivider(str, 43.0, "\n");
createNotification(result);
} }
// This fires a notification on window resize // This fires a notification on window resize
@ -358,7 +394,7 @@ function checkSize() {
windowDimensions = Controller.getViewportDimensions(); windowDimensions = Controller.getViewportDimensions();
overlayLocationX = (windowDimensions.x - (width + 60.0)); overlayLocationX = (windowDimensions.x - (width + 60.0));
buttonLocationX = overlayLocationX + (width - 35.0); buttonLocationX = overlayLocationX + (width - 35.0);
createNotification(windowResize); createNotification(windowResize, NotificationType.WINDOW_RESIZE);
} }
} }
@ -442,10 +478,7 @@ var STARTUP_TIMEOUT = 500, // ms
// This reports the number of users online at startup // This reports the number of users online at startup
function reportUsers() { function reportUsers() {
var welcome; createNotification("Welcome! There are " + GlobalServices.onlineUsers.length + " users online now.", NotificationType.USERS_ONLINE);
welcome = "Welcome! There are " + GlobalServices.onlineUsers.length + " users online now.";
createNotification(welcome);
} }
function finishStartup() { function finishStartup() {
@ -472,13 +505,13 @@ function onOnlineUsersChanged(users) {
if (!isStartingUp()) { // Skip user notifications at startup. if (!isStartingUp()) { // Skip user notifications at startup.
for (i = 0; i < users.length; i += 1) { for (i = 0; i < users.length; i += 1) {
if (last_users.indexOf(users[i]) === -1.0) { if (last_users.indexOf(users[i]) === -1.0) {
createNotification(users[i] + " has joined"); createNotification(users[i] + " has joined", NotificationType.USER_JOINS);
} }
} }
for (i = 0; i < last_users.length; i += 1) { for (i = 0; i < last_users.length; i += 1) {
if (users.indexOf(last_users[i]) === -1.0) { if (users.indexOf(last_users[i]) === -1.0) {
createNotification(last_users[i] + " has left"); createNotification(last_users[i] + " has left", NotificationType.USER_LEAVES);
} }
} }
} }
@ -497,7 +530,7 @@ function onIncomingMessage(user, message) {
thisAlert = user + ": " + myMessage; thisAlert = user + ": " + myMessage;
if (myMessage.indexOf(alertMe) > -1.0) { if (myMessage.indexOf(alertMe) > -1.0) {
wordWrap(thisAlert); CreateNotification(wordWrap(thisAlert), NotificationType.CHAT_MENTION);
} }
} }
@ -506,9 +539,9 @@ function onMuteStateChanged() {
var muteState, var muteState,
muteString; muteString;
muteState = AudioDevice.getMuted() ? "muted" : "unmuted"; muteState = AudioDevice.getMuted() ? "muted" : "unmuted";
muteString = "Microphone is now " + muteState; muteString = "Microphone is now " + muteState;
createNotification(muteString); createNotification(muteString, NotificationType.MUTE_TOGGLE);
} }
// handles mouse clicks on buttons // handles mouse clicks on buttons
@ -551,25 +584,58 @@ function keyPressEvent(key) {
if (key.text === "q") { //queries number of users online if (key.text === "q") { //queries number of users online
numUsers = GlobalServices.onlineUsers.length; numUsers = GlobalServices.onlineUsers.length;
welcome = "There are " + numUsers + " users online now."; welcome = "There are " + numUsers + " users online now.";
createNotification(welcome); createNotification(welcome, NotificationType.USERS_ONLINE);
} }
if (key.text === "s") { if (key.text === "s") {
if (ctrlIsPressed === true) { if (ctrlIsPressed === true) {
noteString = "Snapshot taken."; noteString = "Snapshot taken.";
createNotification(noteString); createNotification(noteString, NotificationType.SNAPSHOT);
} }
} }
} }
function setup() {
Menu.addMenu(MENU_NAME);
var checked = Settings.getValue(PLAY_NOTIFICATION_SOUNDS_SETTING);
checked = checked === '' ? true : checked;
Menu.addMenuItem({
menuName: MENU_NAME,
menuItemName: PLAY_NOTIFICATION_SOUNDS_MENU_ITEM,
isCheckable: true,
isChecked: Settings.getValue(PLAY_NOTIFICATION_SOUNDS_SETTING)
});
Menu.addSeparator(MENU_NAME, "Play sounds for:");
for (type in NotificationType.properties) {
checked = Settings.getValue(PLAY_NOTIFICATION_SOUNDS_TYPE_SETTING_PRE + (parseInt(type) + 1));
checked = checked === '' ? true : checked;
Menu.addMenuItem({
menuName: MENU_NAME,
menuItemName: NotificationType.properties[type].text + NOTIFICATION_MENU_ITEM_POST,
isCheckable: true,
isChecked: checked
});
}
}
// When our script shuts down, we should clean up all of our overlays // When our script shuts down, we should clean up all of our overlays
function scriptEnding() { function scriptEnding() {
var i; for (var i = 0; i < notifications.length; i++) {
for (i = 0; i < notifications.length; i += 1) {
Overlays.deleteOverlay(notifications[i]); Overlays.deleteOverlay(notifications[i]);
Overlays.deleteOverlay(buttons[i]); Overlays.deleteOverlay(buttons[i]);
} }
Menu.removeMenu(MENU_NAME);
}
function menuItemEvent(menuItem) {
if (menuItem === PLAY_NOTIFICATION_SOUNDS_MENU_ITEM) {
Settings.setValue(PLAY_NOTIFICATION_SOUNDS_SETTING, Menu.isOptionChecked(PLAY_NOTIFICATION_SOUNDS_MENU_ITEM));
return;
}
var notificationType = NotificationType.getTypeFromMenuItem(menuItem);
if (notificationType !== notificationType.UNKNOWN) {
Settings.setValue(PLAY_NOTIFICATION_SOUNDS_TYPE_SETTING_PRE + notificationType, Menu.isOptionChecked(menuItem));
}
} }
AudioDevice.muteToggled.connect(onMuteStateChanged); AudioDevice.muteToggled.connect(onMuteStateChanged);
@ -580,3 +646,6 @@ GlobalServices.incomingMessage.connect(onIncomingMessage);
Controller.keyReleaseEvent.connect(keyReleaseEvent); Controller.keyReleaseEvent.connect(keyReleaseEvent);
Script.update.connect(update); Script.update.connect(update);
Script.scriptEnding.connect(scriptEnding); Script.scriptEnding.connect(scriptEnding);
Menu.menuItemEvent.connect(menuItemEvent);
setup();