// ----------------------------- *** txt class *** --------------------------------------------------------------------- class txt{ float s, x, xt, nx, y, yt, ny, del; //size, actual x, target x, normalized x, actual y, target y, normalized y, and delay String title, common; int index, passive_links, tlinks; ArrayList content, content_temp; int[] links; int common_counter = 0; boolean selected = false; boolean hover = false; //........... constructor public txt(float xpos, float ypos, float sz, Object node, int indx){ //placement size motion stuff x = xpos; xt = xpos; y = ypos; yt = ypos; s = sz; del = 10 + random(3); index = indx; //normalized vector float xdiff = width/2 - x; float ydiff = height/2 - y; float len = sqrt(sq(xdiff) + sq(ydiff)); nx = xdiff/len; ny = ydiff/len; // text stuff common = ""; XMLElement n = (XMLElement)node; title = (String)n.getAttribute("TITLE"); String[] a_content = split(n.getContent(), ' '); content = new ArrayList(); content_temp = new ArrayList(); for(int s = 0; s < a_content.length; s++){ String word = a_content[s].toLowerCase().replace('.', ' ').replace(',', ' ').trim(); if(!content.contains(word) && !noise_words.contains(word)){ content.add(word); content_temp.add(word); } } //init links links = new int[txt_cells.size()]; passive_links = 0; //this counter is for the number of other links discovered after this cell is initialized } //........... add link public void add_link(){ passive_links++; } //........... targeting public void target(float xpos, float ypos, boolean plusadd){ if(plusadd){ xt += xpos; yt += ypos; }else{ xt = xpos; yt = ypos; } } //........... get position public float[] get_pos(){ float[] ret = {x, y}; return ret; } //........... draw links private void draw_links(){ // should we draw links? // permission like this: // 1 = draw all links // 0 = check for txt_selected and txt_hover int permis = 1; if((txt_selected + txt_hover) > -2) permis = 0; if(index == txt_selected || index == txt_hover) permis = 1; colorMode(HSB, 100); color c = color(30, 51, 18); for(int il = 0; il < links.length; il++){ if(permis == 0 && il != txt_selected && il != txt_hover) continue; if(links[il] >= 1){ if(links[il] > most_between2) most_between2 = links[il]; c = color(30, 51, (82*(links[il]*1.0/most_between2))+18); stroke(c); float[] pos = ((txt)txt_cells.get(il)).get_pos(); line(x, y, pos[0], pos[1]); } } colorMode(RGB, 255); } //........... has a word? public boolean has(Object wrd){ return content.contains((String)wrd); } //........... process private void process_content(){ //checking my list of words against the others to see if there are matches //if so, add that index number to my links list if(content_temp.size() == 0) return; for(int others = 0; others < index; others++){ txt other = (txt)txt_cells.get(others); //loop for all other txt_cells up to me. if(other.has(content_temp.get(0))){ //links is a list, the position corresponds to the other txt_cell //value corresponds to number of matching words links[others] += 1; other.add_link(); } } content_temp.remove(0); //get rid of the processed word } //........... draw it public void render(int phase){ process_content(); if(phase == 0){ //calculate set_distances(); if(x != xt){ float diff = xt - x; if(abs(diff) < 0.5) x = xt; else x += diff/del; } if(y != yt){ float diff = yt - y; if(abs(diff) < 0.5) y = yt; else y += diff/del; } draw_links(); } if(phase == 1){ stroke(85,159,29); //check for mouseover if(txt_selected == index){ hover = false; // so I'm selected... if there is also a hover, I can start writing out shared words... if(txt_hover >= 0){ write_common(); }else{ if(common.length() > 0) common = ""; common_counter = 0; } fill(124,204,66); }else if(mouseOn()){ fill(124,204,66); hover = true; txt_hover = index; }else{ fill(79,111,55); hover = false; if(common.length() > 0) common = ""; common_counter = 0; } rect(x - (s/2), y - (s/2), s, s); draw_title(); } } // ............ write out common words... private void write_common(){ boolean got_next = false; while(!got_next && common_counter < content.size()){ txt other = (txt)txt_cells.get(txt_hover); String current_word = (String)content.get(common_counter); if(other.has(current_word)){ if(common.length() > 0) common += " "; if(textWidth(common) > width - 100) common += "\n"; common += current_word; got_next = true; } common_counter += 1; } fill(118, 191, 95); text(common, 5, top_banner_sz - 8); } // ............ mouse on me? public boolean mouseOn(){ return ((abs(x-mouseX) <= s/2) && (abs(y-mouseY) <= s/2)); } //........... set distance from center private void set_distances(){ // get the total number of links for this cell, and uses that to set how far from the center it is. int total_links = 0; for(int ix = 0; ix < links.length; ix++){ total_links += links[ix]; } total_links += passive_links; // add in the links which I'm not responsible for drawing, but still count tlinks = total_links; float targ = global_prop(total_links); float actual = dist(x, y, width/2, ((height - top_banner_sz)/2) + top_banner_sz); //println("target: " + target + ", actual: " + actual); if(abs(targ - actual) > 2){ //println("targeting: " + (nx*target) + ", " + (ny*target)); target(nx*targ + width/2, ny*targ + ((height-top_banner_sz)/2) + top_banner_sz, false); } } //........... title label private void draw_title(){ fill(220,220,220); noStroke(); text(index + ") " + tlinks, x+s*2/3, y-s*2/3);// + title } }