Move edit.js to new toolbar API

This commit is contained in:
Brad Davis 2016-07-14 15:50:40 -07:00
parent fdcbf71623
commit 1adf96c8df

View file

@ -16,7 +16,6 @@ HIFI_PUBLIC_BUCKET = "http://s3.amazonaws.com/hifi-public/";
Script.include([ Script.include([
"libraries/stringHelpers.js", "libraries/stringHelpers.js",
"libraries/dataViewHelpers.js", "libraries/dataViewHelpers.js",
"libraries/toolBars.js",
"libraries/progressDialog.js", "libraries/progressDialog.js",
"libraries/entitySelectionTool.js", "libraries/entitySelectionTool.js",
@ -50,11 +49,6 @@ selectionManager.addEventListener(function() {
lightOverlayManager.updatePositions(); lightOverlayManager.updatePositions();
}); });
var toolIconUrl = Script.resolvePath("assets/images/tools/");
var toolHeight = 50;
var toolWidth = 50;
var TOOLBAR_MARGIN_Y = 0;
var DEGREES_TO_RADIANS = Math.PI / 180.0; var DEGREES_TO_RADIANS = Math.PI / 180.0;
var RADIANS_TO_DEGREES = 180.0 / Math.PI; var RADIANS_TO_DEGREES = 180.0 / Math.PI;
var epsilon = 0.001; var epsilon = 0.001;
@ -104,6 +98,7 @@ IMPORTING_SVO_OVERLAY_WIDTH = 144;
IMPORTING_SVO_OVERLAY_HEIGHT = 30; IMPORTING_SVO_OVERLAY_HEIGHT = 30;
IMPORTING_SVO_OVERLAY_MARGIN = 5; IMPORTING_SVO_OVERLAY_MARGIN = 5;
IMPORTING_SVO_OVERLAY_LEFT_MARGIN = 34; IMPORTING_SVO_OVERLAY_LEFT_MARGIN = 34;
var importingSVOImageOverlay = Overlays.addOverlay("image", { var importingSVOImageOverlay = Overlays.addOverlay("image", {
imageURL: Script.resolvePath("assets") + "/images/hourglass.svg", imageURL: Script.resolvePath("assets") + "/images/hourglass.svg",
width: 20, width: 20,
@ -168,161 +163,215 @@ function toggleMarketplace() {
} }
var toolBar = (function() { var toolBar = (function() {
var EDIT_SETTING = "io.highfidelity.isEditting"; // for communication with other scripts
var TOOL_ICON_URL = Script.resolvePath("assets/images/tools/");
var that = {}, var that = {},
toolBar, toolBar,
activeButton, systemToolbar,
newModelButton, activeButton;
newCubeButton,
newSphereButton, function createNewEntity(properties, dragOnCreate) {
newLightButton, Settings.setValue(EDIT_SETTING, false);
newTextButton,
newWebButton, // Default to true if not passed in
newZoneButton, // dragOnCreate = dragOnCreate == undefined ? true : dragOnCreate;
newParticleButton dragOnCreate = false;
var dimensions = properties.dimensions ? properties.dimensions : DEFAULT_DIMENSIONS;
var position = getPositionToCreateEntity();
var entityID = null;
if (position != null) {
position = grid.snapToSurface(grid.snapToGrid(position, false, dimensions), dimensions),
properties.position = position;
entityID = Entities.addEntity(properties);
if (dragOnCreate) {
placingEntityID = entityID;
}
} else {
Window.alert("Can't create " + properties.type + ": " + properties.type + " would be out of bounds.");
}
selectionManager.clearSelections();
entityListTool.sendUpdate();
selectionManager.setSelections([entityID]);
return entityID;
}
function cleanup() {
that.setActive(false);
}
function addButton(name, image, handler) {
var imageUrl = TOOL_ICON_URL + image;
var button = toolBar.addButton({
objectName: name,
imageURL: imageUrl,
buttonState: 1,
alpha: 0.9,
visible: true,
});
if (handler) {
button.clicked.connect(function(){
Script.setTimeout(handler, 100);
});
}
return button;
}
function initialize() { function initialize() {
toolBar = new ToolBar(0, 0, ToolBar.HORIZONTAL, "highfidelity.edit.toolbar", function(windowDimensions, toolbar) { print("QQQ creating edit toolbar");
return { Script.scriptEnding.connect(cleanup);
x: (windowDimensions.x / 2) + (Tool.IMAGE_WIDTH * 2),
y: windowDimensions.y Window.domainChanged.connect(function() {
}; that.setActive(false);
}, { that.clearEntityList();
x: toolWidth,
y: -TOOLBAR_MARGIN_Y - toolHeight
}); });
activeButton = toolBar.addTool({ Entities.canAdjustLocksChanged.connect(function(canAdjustLocks) {
imageURL: toolIconUrl + "edit.svg", if (isActive && !canAdjustLocks) {
subImage: { that.setActive(false);
x: 0, }
y: Tool.IMAGE_WIDTH,
width: Tool.IMAGE_WIDTH,
height: Tool.IMAGE_HEIGHT
},
width: toolWidth,
height: toolHeight,
alpha: 0.9,
visible: true
}, true, false);
newModelButton = toolBar.addTool({
imageURL: toolIconUrl + "model-01.svg",
subImage: {
x: 0,
y: Tool.IMAGE_WIDTH,
width: Tool.IMAGE_WIDTH,
height: Tool.IMAGE_HEIGHT
},
width: toolWidth,
height: toolHeight,
alpha: 0.9,
showButtonDown: true,
visible: false
}); });
newCubeButton = toolBar.addTool({ systemToolbar = Toolbars.getToolbar("com.highfidelity.interface.toolbar.system");
imageURL: toolIconUrl + "cube-01.svg", activeButton = systemToolbar.addButton({
subImage: { objectName: "activeButton",
x: 0, imageURL: TOOL_ICON_URL + "edit.svg",
y: Tool.IMAGE_WIDTH, visible: true,
width: Tool.IMAGE_WIDTH, buttonState: 1,
height: Tool.IMAGE_HEIGHT
},
width: toolWidth,
height: toolHeight,
alpha: 0.9, alpha: 0.9,
showButtonDown: true, });
visible: false activeButton.clicked.connect(function(){
that.setActive(!isActive);
activeButton.writeProperty("buttonState", isActive ? 0 : 1);
}); });
newSphereButton = toolBar.addTool({ toolBar = Toolbars.getToolbar("com.highfidelity.interface.toolbar.edit");
imageURL: toolIconUrl + "sphere-01.svg", toolBar.writeProperty("shown", false);
subImage: {
x: 0, addButton("newModelButton", "model-01.svg", function(){
y: Tool.IMAGE_WIDTH, var url = Window.prompt("Model URL");
width: Tool.IMAGE_WIDTH, if (url !== null && url !== "") {
height: Tool.IMAGE_HEIGHT createNewEntity({
}, type: "Model",
width: toolWidth, modelURL: url
height: toolHeight, }, false);
alpha: 0.9, }
showButtonDown: true,
visible: false
}); });
newLightButton = toolBar.addTool({ addButton("newCubeButton", "cube-01.svg", function(){
imageURL: toolIconUrl + "light-01.svg", createNewEntity({
subImage: { type: "Box",
x: 0, dimensions: DEFAULT_DIMENSIONS,
y: Tool.IMAGE_WIDTH, color: {
width: Tool.IMAGE_WIDTH, red: 255,
height: Tool.IMAGE_HEIGHT green: 0,
}, blue: 0
width: toolWidth, }
height: toolHeight, });
alpha: 0.9,
showButtonDown: true,
visible: false
}); });
newTextButton = toolBar.addTool({ addButton("newSphereButton", "sphere-01.svg", function(){
imageURL: toolIconUrl + "text-01.svg", createNewEntity({
subImage: { type: "Sphere",
x: 0, dimensions: DEFAULT_DIMENSIONS,
y: Tool.IMAGE_WIDTH, color: {
width: Tool.IMAGE_WIDTH, red: 255,
height: Tool.IMAGE_HEIGHT green: 0,
}, blue: 0
width: toolWidth, }
height: toolHeight, });
alpha: 0.9,
showButtonDown: true,
visible: false
}); });
newWebButton = toolBar.addTool({ addButton("newLightButton", "light-01.svg", function(){
imageURL: toolIconUrl + "web-01.svg", createNewEntity({
subImage: { type: "Light",
x: 0, dimensions: DEFAULT_LIGHT_DIMENSIONS,
y: Tool.IMAGE_WIDTH, isSpotlight: false,
width: Tool.IMAGE_WIDTH, color: {
height: Tool.IMAGE_HEIGHT red: 150,
green: 150,
blue: 150
}, },
width: toolWidth,
height: toolHeight, constantAttenuation: 1,
alpha: 0.9, linearAttenuation: 0,
showButtonDown: true, quadraticAttenuation: 0,
visible: false exponent: 0,
cutoff: 180, // in degrees
});
}); });
newZoneButton = toolBar.addTool({ addButton("newTextButton", "text-01.svg", function(){
imageURL: toolIconUrl + "zone-01.svg", createNewEntity({
subImage: { type: "Text",
x: 0, dimensions: {
y: Tool.IMAGE_WIDTH, x: 0.65,
width: Tool.IMAGE_WIDTH, y: 0.3,
height: Tool.IMAGE_HEIGHT z: 0.01
}, },
width: toolWidth, backgroundColor: {
height: toolHeight, red: 64,
alpha: 0.9, green: 64,
showButtonDown: true, blue: 64
visible: false },
textColor: {
red: 255,
green: 255,
blue: 255
},
text: "some text",
lineHeight: 0.06
});
}); });
newParticleButton = toolBar.addTool({ addButton("newWebButton", "web-01.svg", function(){
imageURL: toolIconUrl + "particle-01.svg", createNewEntity({
subImage: { type: "Web",
x: 0, dimensions: {
y: Tool.IMAGE_WIDTH, x: 1.6,
width: Tool.IMAGE_WIDTH, y: 0.9,
height: Tool.IMAGE_HEIGHT z: 0.01
}, },
width: toolWidth, sourceUrl: "https://highfidelity.com/",
height: toolHeight, });
alpha: 0.9, });
showButtonDown: true,
visible: false addButton("newZoneButton", "zone-01.svg", function(){
createNewEntity({
type: "Zone",
dimensions: {
x: 10,
y: 10,
z: 10
},
});
});
addButton("newParticleButton", "particle-01.svg", function(){
createNewEntity({
type: "ParticleEffect",
isEmitting: true,
particleRadius: 0.1,
emitAcceleration: {
x: 0,
y: -1,
z: 0
},
accelerationSpread: {
x: 5,
y: 0,
z: 5
},
emitSpeed: 1,
lifespan: 1,
particleRadius: 0.025,
alphaFinish: 0,
emitRate: 100,
textures: "https://hifi-public.s3.amazonaws.com/alan/Particles/Particle-Sprite-Smoke-1.png",
});
}); });
that.setActive(false); that.setActive(false);
@ -332,13 +381,14 @@ var toolBar = (function() {
entityListTool.clearEntityList(); entityListTool.clearEntityList();
}; };
var EDIT_SETTING = "io.highfidelity.isEditting"; // for communication with other scripts
Settings.setValue(EDIT_SETTING, false);
that.setActive = function(active) { that.setActive = function(active) {
if (active != isActive) { if (active == isActive) {
return;
}
if (active && !Entities.canRez() && !Entities.canRezTmp()) { if (active && !Entities.canRez() && !Entities.canRezTmp()) {
Window.alert(INSUFFICIENT_PERMISSIONS_ERROR_MSG); Window.alert(INSUFFICIENT_PERMISSIONS_ERROR_MSG);
} else { return;
}
Messages.sendLocalMessage("edit-events", JSON.stringify({ Messages.sendLocalMessage("edit-events", JSON.stringify({
enabled: active enabled: active
})); }));
@ -364,246 +414,23 @@ var toolBar = (function() {
// everybody else to think that Interface has lost focus overall. fogbugzid:558 // everybody else to think that Interface has lost focus overall. fogbugzid:558
// Window.setFocus(); // Window.setFocus();
} }
that.showTools(isActive); // Sets visibility of tool buttons, excluding the power button
toolBar.writeProperty("shown", active);
var visible = toolBar.readProperty("visible");
if (active && !visible) {
toolBar.writeProperty("visible", false);
toolBar.writeProperty("shown", false);
toolBar.writeProperty("visible", true);
toolBar.writeProperty("shown", true);
} }
} //toolBar.selectTool(activeButton, isActive);
toolBar.selectTool(activeButton, isActive);
lightOverlayManager.setVisible(isActive && Menu.isOptionChecked(MENU_SHOW_LIGHTS_IN_EDIT_MODE)); lightOverlayManager.setVisible(isActive && Menu.isOptionChecked(MENU_SHOW_LIGHTS_IN_EDIT_MODE));
Entities.setDrawZoneBoundaries(isActive && Menu.isOptionChecked(MENU_SHOW_ZONES_IN_EDIT_MODE)); Entities.setDrawZoneBoundaries(isActive && Menu.isOptionChecked(MENU_SHOW_ZONES_IN_EDIT_MODE));
}; };
// Sets visibility of tool buttons, excluding the power button
that.showTools = function(doShow) {
toolBar.showTool(newModelButton, doShow);
toolBar.showTool(newCubeButton, doShow);
toolBar.showTool(newSphereButton, doShow);
toolBar.showTool(newLightButton, doShow);
toolBar.showTool(newTextButton, doShow);
toolBar.showTool(newWebButton, doShow);
toolBar.showTool(newZoneButton, doShow);
toolBar.showTool(newParticleButton, doShow);
};
var RESIZE_INTERVAL = 50;
var RESIZE_TIMEOUT = 120000; // 2 minutes
var RESIZE_MAX_CHECKS = RESIZE_TIMEOUT / RESIZE_INTERVAL;
function addModel(url) {
var entityID = createNewEntity({
type: "Model",
modelURL: url
}, false);
if (entityID) {
print("Model added: " + url);
selectionManager.setSelections([entityID]);
}
}
function createNewEntity(properties, dragOnCreate) {
// Default to true if not passed in
dragOnCreate = dragOnCreate == undefined ? true : dragOnCreate;
var dimensions = properties.dimensions ? properties.dimensions : DEFAULT_DIMENSIONS;
var position = getPositionToCreateEntity();
var entityID = null;
if (position != null) {
position = grid.snapToSurface(grid.snapToGrid(position, false, dimensions), dimensions),
properties.position = position;
entityID = Entities.addEntity(properties);
if (dragOnCreate) {
placingEntityID = entityID;
}
} else {
Window.alert("Can't create " + properties.type + ": " + properties.type + " would be out of bounds.");
}
selectionManager.clearSelections();
entityListTool.sendUpdate();
selectionManager.setSelections([entityID]);
return entityID;
}
that.mousePressEvent = function(event) {
var clickedOverlay,
url,
file;
if (!event.isLeftButton) {
// if another mouse button than left is pressed ignore it
return false;
}
clickedOverlay = Overlays.getOverlayAtPoint({
x: event.x,
y: event.y
});
if (activeButton === toolBar.clicked(clickedOverlay)) {
that.setActive(!isActive);
return true;
}
if (newModelButton === toolBar.clicked(clickedOverlay)) {
url = Window.prompt("Model URL");
if (url !== null && url !== "") {
addModel(url);
}
return true;
}
if (newCubeButton === toolBar.clicked(clickedOverlay)) {
createNewEntity({
type: "Box",
dimensions: DEFAULT_DIMENSIONS,
color: {
red: 255,
green: 0,
blue: 0
}
});
return true;
}
if (newSphereButton === toolBar.clicked(clickedOverlay)) {
createNewEntity({
type: "Sphere",
dimensions: DEFAULT_DIMENSIONS,
color: {
red: 255,
green: 0,
blue: 0
}
});
return true;
}
if (newLightButton === toolBar.clicked(clickedOverlay)) {
createNewEntity({
type: "Light",
dimensions: DEFAULT_LIGHT_DIMENSIONS,
isSpotlight: false,
color: {
red: 150,
green: 150,
blue: 150
},
constantAttenuation: 1,
linearAttenuation: 0,
quadraticAttenuation: 0,
exponent: 0,
cutoff: 180, // in degrees
});
return true;
}
if (newTextButton === toolBar.clicked(clickedOverlay)) {
createNewEntity({
type: "Text",
dimensions: {
x: 0.65,
y: 0.3,
z: 0.01
},
backgroundColor: {
red: 64,
green: 64,
blue: 64
},
textColor: {
red: 255,
green: 255,
blue: 255
},
text: "some text",
lineHeight: 0.06
});
return true;
}
if (newWebButton === toolBar.clicked(clickedOverlay)) {
createNewEntity({
type: "Web",
dimensions: {
x: 1.6,
y: 0.9,
z: 0.01
},
sourceUrl: "https://highfidelity.com/",
});
return true;
}
if (newZoneButton === toolBar.clicked(clickedOverlay)) {
createNewEntity({
type: "Zone",
dimensions: {
x: 10,
y: 10,
z: 10
},
});
return true;
}
if (newParticleButton === toolBar.clicked(clickedOverlay)) {
createNewEntity({
type: "ParticleEffect",
isEmitting: true,
particleRadius: 0.1,
emitAcceleration: {
x: 0,
y: -1,
z: 0
},
accelerationSpread: {
x: 5,
y: 0,
z: 5
},
emitSpeed: 1,
lifespan: 1,
particleRadius: 0.025,
alphaFinish: 0,
emitRate: 100,
textures: "https://hifi-public.s3.amazonaws.com/alan/Particles/Particle-Sprite-Smoke-1.png",
});
}
return false;
};
that.mouseReleaseEvent = function(event) {
return false;
}
Window.domainChanged.connect(function() {
that.setActive(false);
that.clearEntityList();
});
Entities.canAdjustLocksChanged.connect(function(canAdjustLocks) {
if (isActive && !canAdjustLocks) {
that.setActive(false);
}
});
that.cleanup = function() {
toolBar.cleanup();
};
initialize(); initialize();
return that; return that;
}()); })();
function isLocked(properties) { function isLocked(properties) {
@ -712,7 +539,7 @@ function mousePressEvent(event) {
mouseHasMovedSincePress = false; mouseHasMovedSincePress = false;
mouseCapturedByTool = false; mouseCapturedByTool = false;
if (propertyMenu.mousePressEvent(event) || toolBar.mousePressEvent(event) || progressDialog.mousePressEvent(event)) { if (propertyMenu.mousePressEvent(event) || progressDialog.mousePressEvent(event)) {
mouseCapturedByTool = true; mouseCapturedByTool = true;
return; return;
} }
@ -796,8 +623,7 @@ function mouseReleaseEvent(event) {
mouseMove(lastMouseMoveEvent); mouseMove(lastMouseMoveEvent);
lastMouseMoveEvent = null; lastMouseMoveEvent = null;
} }
if (propertyMenu.mouseReleaseEvent(event) || toolBar.mouseReleaseEvent(event)) { if (propertyMenu.mouseReleaseEvent(event)) {
return true; return true;
} }
if (placingEntityID) { if (placingEntityID) {
@ -1094,7 +920,6 @@ Script.scriptEnding.connect(function() {
Settings.setValue(SETTING_SHOW_ZONES_IN_EDIT_MODE, Menu.isOptionChecked(MENU_SHOW_ZONES_IN_EDIT_MODE)); Settings.setValue(SETTING_SHOW_ZONES_IN_EDIT_MODE, Menu.isOptionChecked(MENU_SHOW_ZONES_IN_EDIT_MODE));
progressDialog.cleanup(); progressDialog.cleanup();
toolBar.cleanup();
cleanupModelMenus(); cleanupModelMenus();
tooltip.cleanup(); tooltip.cleanup();
selectionDisplay.cleanup(); selectionDisplay.cleanup();