Created
March 26, 2015 09:37
-
-
Save Adrianl3d/1e4223eed8e9834a85eb to your computer and use it in GitHub Desktop.
Domain Entity Chat
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// domainChat.js | |
// 25 March 2015 Version 0.3 | |
// Created by Adrian McCarlie 18 March 2015 | |
// Sends text chat to a Text Entity in the Domain. | |
// This script is copy-write (c) 2015 Adrian McCarlie | |
// Distributed under "Free for all to use." | |
// "All rights reserved" | |
// for free copy go to https://gist.github.com/Adrianl3d and find it there. | |
// | |
var width = 400; //width of input window | |
var height = 50; // height of input window | |
var chatWindowHeight = 500; | |
var chatTextStart = chatWindowHeight - 20; | |
var windowDimensions = Controller.getViewportDimensions(); // get the size of the interface window | |
var locationX = (windowDimensions.x - (width + 60));// positions window to right of the interface window | |
var locationY = 75; // on screen location Y | |
var topMargin = 4; | |
var leftMargin = 10; | |
var textColor = {red: 228, green: 228, blue: 228}; // text color | |
var backColor = {red: 150, green: 150, blue: 150}; // default background color | |
var readyColor = {red: 50, green: 50, blue: 50}; | |
var backgroundAlpha = 0.6; | |
var fontSize = 12; | |
var thisUser = GlobalServices.username; | |
var position = MyAvatar.position; | |
var writing = "\n\nClick here to begin typing" ; | |
var clickedText = false; | |
var cursor = "|"; | |
var frame = 0; | |
var lines = 0; | |
var isVisible = false; | |
var hasFocus = false; | |
var keyString = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz\ | |
~!@#$%^&*()_+`1234567890-={}|[]\\:\";'<>?,./"; //permitted characters | |
var last_users = []; | |
var panel = {}; | |
var list = ""; | |
var existingChat = "No recent chat!"; | |
var found = Entities.findEntities(position, 10000.0); | |
for( i = 0; i < found.length; i++){ | |
panel = Entities.getEntityProperties(found[i]); | |
if(panel.userData == "adrians text panel writable"){ // covert self promotion | |
existingChat = panel.text; | |
} | |
} | |
// This will create a text overlay that displays what you type | |
var inputWindow = Overlays.addOverlay("text", { | |
x: locationX, | |
y: locationY + chatWindowHeight + 5, | |
width: width, | |
height: height, | |
color: textColor, | |
backgroundColor: backColor, | |
alpha: backgroundAlpha, | |
topMargin: topMargin, | |
leftMargin: leftMargin, | |
font: {size: fontSize}, | |
text: "Click here to begin typing", | |
visible: false | |
}); | |
// this will create a text overlay that displays the world chat | |
var chatWindow = Overlays.addOverlay("text", { | |
x: locationX, | |
y: locationY, | |
width: width, | |
height: chatWindowHeight, | |
color: textColor, | |
backgroundColor: backColor, | |
alpha: backgroundAlpha, | |
topMargin: chatWindowHeight, | |
leftMargin: leftMargin, | |
font: {size: fontSize}, | |
visible: false, | |
text: "No recent chat!" | |
}); | |
// When our script shuts down, we should clean up all of our overlays | |
function scriptEnding() { | |
Overlays.deleteOverlay(inputWindow); | |
Overlays.deleteOverlay(chatWindow); | |
//Return control of keys to default on script ending | |
exitCapture(); | |
} | |
function exitCapture(){ | |
for(var i=0; i<keyString.length; i++){ | |
var nextChar = keyString.charAt(i); | |
Controller.releaseKeyEvents({ text: nextChar}); | |
} | |
Controller.releaseKeyEvents({ text: "page up"}); | |
Controller.releaseKeyEvents({ text: "SHIFT"}); | |
Controller.releaseKeyEvents({ text: "BACKSPACE"}); | |
Controller.releaseKeyEvents({ text: "SPACE"}); | |
Controller.releaseKeyEvents({"key":16777220} ); //Enter | |
} | |
Script.scriptEnding.connect(scriptEnding); | |
function resetForm(){ | |
writing = ""; // Start with a blank string | |
writingOutput = ""; // Clear previous messages | |
Overlays.editOverlay(inputWindow, {backgroundColor: readyColor }); | |
clickedText = true; | |
} | |
function submitForm(){ | |
if (writing != ""){ | |
var tag = thisUser.concat( ": "); | |
writingOutput = existingChat + "\n" + tag + writing ; // writingOutput is the data output | |
clickedText = false; | |
String.prototype.count=function(s1) { | |
return (this.length - this.replace(new RegExp(s1,"g"), '').length) / s1.length; | |
} | |
var newLines = writingOutput.count('\n'); | |
if(newLines > 28) { | |
var spill = newLines - 28; | |
var newText = writingOutput.split("\n").slice(spill).join("\n") | |
writingOutput = newText; | |
} | |
Overlays.editOverlay(chatWindow, {text: writingOutput}); | |
findPanel(); | |
Entities.editEntity(panel, {text: writingOutput}); | |
resetForm(); | |
} | |
} | |
function clearChat(){ | |
Overlays.editOverlay(chatWindow, {text: ""}); | |
} | |
// handle click detection | |
function mousePressEvent(event) { | |
var clickedOverlay = Overlays.getOverlayAtPoint({x: event.x, y: event.y}); | |
if (clickedOverlay == inputWindow || clickedOverlay == chatWindow) { | |
if (!hasFocus) { | |
setFocus(true); | |
} | |
if (clickedText == false){ | |
resetForm(); | |
} | |
} else { | |
if (hasFocus) { | |
setFocus(false); | |
} | |
} | |
if (clickedOverlay == inputWindow) { | |
if (clickedText == false){ | |
} | |
} | |
} | |
function setVisible(visible) { | |
isVisible = visible; | |
Overlays.editOverlay(inputWindow, { visible: visible }); | |
Overlays.editOverlay(chatWindow, { visible: visible }); | |
} | |
function setFocus(focus) { | |
if (isVisible) { | |
hasFocus = focus; | |
if (hasFocus) { | |
setupCapture(); | |
Overlays.editOverlay(inputWindow, { visible: true }); | |
} else { | |
exitCapture(); | |
clickedText =false; | |
Overlays.editOverlay(inputWindow, { visible: false }); | |
} | |
} | |
} | |
// handle Key press detection | |
function keyPressEvent(key) { | |
print("keyPressEvent key.text="+key.key); //debug | |
if ((!isVisible || !hasFocus) && key.text == "SPACE") { | |
setVisible(true); | |
setFocus(true); | |
return; | |
} | |
if ((clickedText == true) && isVisible == true){ | |
if (key.text == "SPACE") { | |
writing = writing + " "; | |
key.text =""; | |
} | |
else if (key.text == "BACKSPACE") { | |
var myString = writing; | |
writing = myString.substr(0, myString.length-1); | |
key.text =""; | |
} | |
if (key.key == 16777220) { //special conditions for enter key | |
submitForm() | |
key.text =""; | |
} | |
else if (keyString.indexOf(key.text) == -1) { // prevent all other keys not in keyString | |
key.text =""; | |
} | |
if (key.key == 92) { //special conditions for back SLASH | |
key.text =""; | |
setVisible(false); | |
setFocus(false); | |
exitCapture(); | |
} | |
// build the string | |
writing = writing + key.text; | |
} | |
} | |
function findPanel(){ | |
var found = Entities.findEntities(position, 100.0); | |
for( i = 0; i < found.length; i++){ | |
panel = Entities.getEntityProperties(found[i]); | |
if(panel.userData =="adrians text panel writable"){ | |
list = panel.text; | |
return panel; | |
} | |
} | |
} | |
var count = 0; | |
function updateWriting(){ | |
count++; | |
// every half second or so, remove and replace the pipe to create a blinking cursor | |
if (count % 30 == 0) { | |
if (cursor == "|") { | |
cursor=""; | |
} else { | |
cursor = "|"; | |
} | |
} | |
if ((writing.length % (width/10)) == 0 && writing.length > 0) { | |
writing = writing + "\n"; | |
lines = lines++; | |
} | |
var addCursor = writing + cursor; | |
if (clickedText == true){ | |
Overlays.editOverlay(inputWindow, { text: addCursor }); | |
}else{ | |
Overlays.editOverlay(inputWindow, { text: writing }); | |
} | |
} | |
function updateOverlay(deltaTime){ | |
frame++; | |
// Only update every second | |
if ((frame % 60) == 0) { | |
findPanel(); | |
list = panel.text; | |
Overlays.editOverlay(chatWindow, { text: list, topMargin: 10 }); | |
existingChat = list; | |
} | |
} | |
function setupCapture(){ | |
for (var i = 0; i < keyString.length; i++) { | |
var nextChar = keyString.charAt(i); | |
Controller.captureKeyEvents({text: nextChar}); | |
} | |
Controller.captureKeyEvents({ text: "SHIFT"}); | |
Controller.captureKeyEvents({ "key": 16777219}); | |
Controller.captureKeyEvents({ text: "PAGE UP"}); | |
Controller.captureKeyEvents({ text: "SPACE"}); | |
Controller.captureKeyEvents({"key":16777220} ); // enter key | |
} | |
Controller.captureKeyEvents({ text: "//"}); | |
Controller.keyPressEvent.connect(keyPressEvent); | |
Controller.mousePressEvent.connect(mousePressEvent); | |
Script.update.connect(updateWriting); | |
Script.update.connect(updateOverlay); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment