/*
This file is part of Massive Comprehension Machine
by Lot AmorĂ³s
http://mcm.feenelcaos.org
Massive Comprehension Machine is free software:
you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License.
Massive Comprehension Machine is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Massive Comprehension Machine. If not, see .
One part of this code is based on the book Visualizing Data from Ben Fry.
*/
class SearchBox{
RiTextField searchBox;
String txtBack = "";
String lastNode="";
int baseX;
int baseY;
int findButtonX, findButtonY; // Position of find button
int clearButtonX, clearButtonY; // Position of clear button
int searchX, searchY; // Position of searchbox field
int zoomButtonX; // Position of zoom button X
float zoomButtonY; // VARIABLE position of zoom button Y
int perCent=0;
int buttonSize; // Diameter of circle
float circle; //circle of legend
float nextZoom;
float mouseDistanceDragged =0; //For zoom with the mouse buton
color circleColor;
color circleHighlight;
color findHigh;
color rectBox;
static final color found = #000000;
static final color notFound = #E8001F;
SearchBox(PApplet p, int _x, int _y){
this.baseX = _x;
this.baseY = _y;
this.circleColor = color(125);
this.circleHighlight = color(100);
this.rectBox = color(0,180); //Color of the SearchBox
this.findHigh = color(255);
this.buttonSize = 42; // Diameter of circle buttons
this.circle = 10;
this.searchX = baseX+10;
this.searchY = baseY+20;
this.findButtonX = baseX+buttonSize/2+130;
this.findButtonY = baseY+35;
this.clearButtonX = baseX + buttonSize/2+180;
this.clearButtonY = baseY+35;
this.zoomButtonX = (width/2)-50;
this.zoomButtonY = baseY+15 +map(zoom,-52,10,70,0);
searchBox = new RiTextField(p, searchX, searchY);
}
//################## Drawing Methods #######################
void draw()
{
fill(rectBox); //Draw the rect box
stroke(255);
strokeWeight(2);
rect(baseX,baseY, width,height-baseY);
drawText();
drawButtons();
noStroke();
drawSlider();
drawScale();
zoomMode(); //Make Zoom if zoomMode is ON
}
void drawText(){
String percent;
percent = nf(((float)allVisibleNodes()/(float)allNodes())*100,2, 2);
fill(edgeColor);
textFont(font, 10);
textAlign(LEFT);
text(txtBack,searchX,searchY+20,130,50);
textAlign(LEFT, BOTTOM);
text(snPalestina.visibleNodes + " of "+snPalestina.nodeCount +" words in GAZA Network ",width/5,baseY+30);
text(snIsrael.visibleNodes + " of "+ snIsrael.nodeCount + " words in ISRAEL Network",width/5,baseY+40);
text(allVisibleNodes() + " visible words of "+ allNodes()+" total words.",width/5,baseY+50);
text(percent + "% of total information.",width/5,baseY+60);
fill(nodeColor);
textAlign(LEFT);
text("Turn OFF music on your computer. Switch ON your speakers.",baseX+10,baseY+80);
if (searchMode)
text("mouse wheel: ZOOM || left click: FIX node || double click: EXPAND node || right click: RELEASE node",baseX+10,baseY+90);
else
text("mouse wheel: ZOOM || left click: FIX node || right click: RELEASE node",baseX+10,baseY+90);
textFont(font, 20);
text("Massive Comprehension Machine, audiovisual device for generate in real time and navigating in Semantic Nets about Gaza Conflict.",width/2,baseY+10,(width/2)-20,height);
}
void drawButtons(){
textFont(font, 10);
if(overFindButton()) { //Draw Find Button
mouseOver=true;
//draw circle
fill(circleHighlight);
stroke(nodeColor);
ellipse(findButtonX, findButtonY, buttonSize, buttonSize);
//draw find
fill(findHigh);
textAlign(CENTER, CENTER);
text("Find",findButtonX,findButtonY);
} else {
//draw circle
fill(circleColor);
stroke(0);
ellipse(findButtonX, findButtonY, buttonSize, buttonSize);
//draw find
fill(0);
textAlign(CENTER, CENTER);
text("Find",findButtonX,findButtonY);
}
if(overClearButton()) { //Draw Clear Button
mouseOver=true;
//draw circle
fill(fixedColor);
stroke(nodeColor);
ellipse(clearButtonX, clearButtonY, buttonSize, buttonSize);
//draw find
fill(findHigh);
textAlign(CENTER, CENTER);
text("Clear",clearButtonX,clearButtonY);
} else {
//draw circle
fill(fixedColor);
stroke(0);
ellipse(clearButtonX, clearButtonY, buttonSize, buttonSize);
//draw find
fill(0);
textAlign(CENTER, CENTER);
text("Clear",clearButtonX,clearButtonY);
}
}
void drawSlider(){ //Draw Zoom Slider
zoomButtonY = baseY+15 +map(zoom,-52,10,70,0); //Actualize button position
perCent = (int)map(zoom,-52,10,0,100); //Actualice perCent unit
perCent = constrain(perCent, 0, 100);
fill(126);
rect(zoomButtonX-3,baseY+15, 6,70); //Draw Slider background
if(overZoomButton()){ //Draw button
mouseOver=true;
stroke(nodeColor);
fill(fixedColor);
ellipse(zoomButtonX, zoomButtonY, 30, 30);
fill(255);
textFont(font, 10);
text(perCent+"%",zoomButtonX,zoomButtonY);
}
else{
stroke(0);
fill(nodeColor);
ellipse(zoomButtonX, zoomButtonY, 30, 30);
fill(0);
textFont(font, 10);
text(perCent+"%",zoomButtonX,zoomButtonY);
}
}
void drawScale(){ //Draw Scale
stroke(0);
fill(nodeColor);
ellipse(baseX+450, baseY+40, 20, 20);
fill(edgeColor);
textAlign(LEFT, CENTER);
text(" = "+(int)(20-zoom)+" words.",baseX+20+450,baseY+40);
}
//################################## Handle Events #########################################
void mousePressed(){
if(overFindButton()) {
textEntered(searchBox.getValue() );
}
else
if(overClearButton()) {
vozSelected.speak("Showing all nodes.");
searchOff();
}
if(overZoomButton()){
mouseDistanceDragged=0;
}
}
void mouseDragged() {
if(overZoomButton()){
if(pmouseYmouseY)
mouseDistanceDragged-=0.1;
makeZoom(zoom-mouseDistanceDragged);
}
}
/*void enterZoomMode(Node n1, Node n2){
zoomMode = true;
}*/
void zoomMode(){
if(searchMode && zoomMode){
makeZoom( lerp( zoom, nextZoom, ani.position() ));
//println("zoom: " + zoom + " nextZoom: "+ nextZoom + " ani: " + ani.position());
if (ani.position() == 1) //visibleNodes in all semantic nets
zoomMode = false;
}
}
void textEntered(String txt){
searchOff();
Node found = snIsrael.searchNode(txt);
Node found2 = snPalestina.searchNode(txt);
if ((found != null) && (found2 != null)) { //If founded in both semantic nets
vozSelected.speak( txt + "found in two semantic nets.");
lastNode = txt;
searchMode = true; //Enter in searchmode
searchBox.setText(txt);
txtBack=txt + " found in two semantic nets.";
nextZoom = filtro - min(found.count,found2.count)+1; //level of Zoom to go
/* println("filtro: " + filtro);
println("found: " + found.count);
println("found2: " + found2.count);
println("NEXTZOOM: " + nextZoom); */
zoomMode = true; //Enter in zoom mode
ani.start(); //Begin the zoom travelling
}
else{
if (found != null){ //If found only in Israel
searchMode = true;
searchBox.setText(txt);
lastNode = txt;
nextZoom = filtro - found.count+1;
zoomMode = true;
ani.start();
if (found2 == null)
snIsrael.speak(found);
txtBack=txt + " found only in Israel semantic net.";
snIsrael.showContext(found);
lastNode = txt;
}
if (found2 != null){ //If found only in Palestina
searchMode = true;
searchBox.setText(txt);
nextZoom = filtro - found2.count;
println("filtro: " + filtro);
println("found2: " + found2.count);
println("NXT Zoom: "+ nextZoom);
zoomMode = true;
ani.start();
lastNode = txt;
if (found == null)
snPalestina.speak(found2);
txtBack=txt + " found only in Palestina semantic net.";
snPalestina.showContext(found2);
}
}
if ((found == null) && (found2 == null)){ //Not Found
fill(notFound);
searchOff();
txtBack="Not found";
vozSelected.speak("Sorry. " + txt + " Not found.");
lastNode = "";
}
}
void searchOff(){ //Function for exit of search mode
searchMode = false;
txtBack="";
searchBox.setText("");
snIsrael.clearSearch();
snPalestina.clearSearch();
}
boolean isOver(){ //True if mouse is over searchbox
if (mouseY>baseY)
return true;
else
return false;
}
boolean overZoomButton(){ // true if mouse is over the zoom button
float disX = zoomButtonX - mouseX;
float disY = zoomButtonY - mouseY;
if(sqrt(sq(disX) + sq(disY)) < 20 ) {
return true;
} else {
return false;
}
}
boolean overFindButton()
{
float disX = findButtonX - mouseX;
float disY = findButtonY - mouseY;
if(sqrt(sq(disX) + sq(disY)) < buttonSize/2 ) {
return true;
} else {
return false;
}
}
boolean overClearButton()
{
float disX = clearButtonX - mouseX;
float disY = clearButtonY - mouseY;
if(sqrt(sq(disX) + sq(disY)) < buttonSize/2 ) {
return true;
} else {
return false;
}
}
}