Loading...
Logo
Processing Forum
Well this is what i got so far. It is not finished, but it's working. Still missing line break handling (so far, it is kind of a big window in width :) among other things
So... Is this a good approach? Should i create a "Word (String, pos x, color, booleanLinked)" class to use in TwTextBox instead of String[] words? Or making like the way i'm doing handling all in TwTextBox is better? How to improve this?
Any suggestions or thoughts please.
And as a side question, how declare Strings in more than one line?
tx


Copy code

  1. // note if you change the string, index must be upadated manually
  2. // i did not took care of thiscause twitter will provide this in a real tweet
  3. // if you want to try another String,
  4. // you can uncomment the indicated line at TwTextBox constructor to see proper indexes
  5. // that must be adjusted in setup, don't forget to adjust arrays size
  6. // but with this string there is no need to change nothing
  7. // i got this from a real tweet

  8. String s = "@legend_of_zelda: #ReasonsWhyWeDontGetAlong You've http://kidnapped.com my girlfriend #repeatedly. As the great rapper Shaggy once said: "wasn't me!"";
  9. int xPos = 20;
  10. int yPos = 50;
  11. int[] htIndex = new int [2];
  12. int[] urlIndex = new int [1];
  13. int[] mentIndex = new int [1];

  14. PFont font;
  15. TwTextBox tb = new TwTextBox();
  16. boolean isClicked = false;


  17. void setup() {  
  18.   size(1200, 200);  
  19.   background(0); 
  20.   smooth();
  21.   font = createFont ("arial", 40);
  22.   htIndex[1]   = 18;
  23.   htIndex[0]   = 86;
  24.   urlIndex[0]  = 51;
  25.   mentIndex[0] = 0;
  26.   tb = new TwTextBox ( s, htIndex, urlIndex, mentIndex, xPos, yPos, font, 16);

  27. }


  28. void draw() {
  29.   background(0);
  30.   stroke(60);
  31.   line(xPos, yPos, width, yPos);
  32.   line(xPos, 0, xPos, height);
  33.   tb.display();
  34. }


  35. void mousePressed()
  36. {
  37.   isClicked = true;
  38. }

  39. void mouseReleased()
  40. {
  41.   isClicked = false;
  42. }




  43. //////////////////******************//////////////////////


  44. class TwTextBox {

  45.   // the full text
  46.   String rawText;

  47.   // to hold splited words
  48.   String[]  words;
  49.   char   splitter = ' ';

  50.   // receivers for twitter data
  51.   int[] hashIndex;
  52.   int[] urlIndex;
  53.   int[] mentionIndex;
  54.   

  55.   // coordinates
  56.   float x;
  57.   float y;

  58.   // relative position holder and calculator
  59.   float[] relativeX;
  60.   float   wordX = 0;
  61.   
  62.   //original string indexes holder and calculator
  63.   float[] stringLegacyIndex;  
  64.   float   indexSum;

  65.   // font
  66.   int fontSize;
  67.   PFont font;

  68.   //colors
  69.   color regularTextColor  =  color(240);
  70.   color hashtagColor =  color(200, 33, 200);
  71.   color urlColor     =  color(33, 200, 200);
  72.   color mentionColor     =  color(200, 200, 33);
  73.   color colorToDraw;  // to hold the color to use


  74.   TwTextBox()
  75.   {
  76.     //default
  77.   }

  78.   TwTextBox(String _rawText, int[] _hashIndex, int[] _urlIndex, int[] _mentionIndex, float _x, float _y
  79.     , PFont _font, int _fontSize)
  80.   {  
  81.     // passing vars
  82.     rawText      = _rawText;
  83.     hashIndex    = _hashIndex;
  84.     urlIndex     = _urlIndex;
  85.     mentionIndex = _mentionIndex;
  86.     x            = _x;
  87.     y            = _y;
  88.     wordX        = _x;
  89.     font         = _font;
  90.     fontSize     = _fontSize;

  91.     // create words array
  92.     words = split(rawText, splitter);  
  93.     
  94.     // init arrays based on words size
  95.     relativeX = new float[words.length];  
  96.     stringLegacyIndex = new float[words.length]; 
  97.     
  98.     
  99.     // add spaces to the end of words
  100.     for (int i = 0; i < words.length; i++) 
  101.     {  
  102.       // font and size are crucial to calcualtions even not drawing
  103.       textFont(font, fontSize);
  104.       
  105.       if (i < words.length -1) {      
  106.         String spacedWord  = words[i] + " "; // don't add to space to last word     
  107.         words[i] = spacedWord;
  108.       }    
  109.       
  110.       // calc and store each word position in x 
  111.       relativeX[i] = wordX;    
  112.       wordX = wordX + textWidth(words[i]);  
  113.       
  114.       // calc and store indexes from string  
  115.       indexSum  = (i == 0)?  0 : indexSum + words[i-1].length();    
  116.       stringLegacyIndex[i] = indexSum;    
  117.           
  118.       
  119.     }  
  120.       
  121.       //if the string is changed the indexes has to be redefined
  122.       // uncomment folowing line to see the proper indexes that must be inputed in setup()
  123.       //println("word: " + words[i]+ " is at index " + stringLegacyIndex[i] + " "); 
  124.     

  125.   } // end constructor
  126.   
  127.   
  128.   
  129.   void display()
  130.   {
  131.     for (int i = 0; i < words.length; i++) {      
  132.       
  133.       textSize(fontSize); 
  134.       
  135.       // if no match below that's the color
  136.       colorToDraw = regularTextColor;
  137.       
  138.       //test for entities
  139.       //hashTag
  140.       for (int j = 0; j < hashIndex.length; j++)
  141.       {
  142.         if(stringLegacyIndex[i] == hashIndex[j])
  143.         {
  144.           colorToDraw = hashtagColor;
  145.           // form string will be  a little different with real twitter
  146.           StringBuilder sb = new StringBuilder("http://twitter.com/search?q=#");
  147.           StringBuilder capHash = new StringBuilder(words[i]);
  148.           capHash.deleteCharAt(0);
  149.           sb.append(capHash.toString());
  150.           if(sb.charAt(sb.length()-1) == ' ')
  151.           sb.deleteCharAt(sb.length()-1);
  152.           if(sb.charAt(sb.length()-1) == '.')
  153.           sb.deleteCharAt(sb.length()-1);
  154.           
  155.           createBox(words[i], relativeX[i], sb.toString());
  156.         }
  157.          
  158.       }
  159.       
  160.       // url
  161.       for (int j = 0; j < urlIndex.length; j++)
  162.       {
  163.         if(stringLegacyIndex[i] == urlIndex[j])
  164.         {
  165.           colorToDraw = urlColor;
  166.           createBox(words[i], relativeX[i], "http://forum.processing.org"); //placeholder
  167.         }
  168.       }
  169.       
  170.       // mentions
  171.       for (int j = 0; j < mentionIndex.length; j++)
  172.       {
  173.         if(stringLegacyIndex[i] == mentionIndex[j])
  174.         {
  175.           colorToDraw = mentionColor;
  176.           createBox(words[i], relativeX[i], "http://processing.org"); //placeholder
  177.         }
  178.       }
  179.       
  180.       
  181.       fill(colorToDraw);
  182.       noStroke();
  183.       text(words[i], relativeX[i], y);
  184.       
  185.       //those were to build will vanish...
  186.       stroke(180, 180, 20);    
  187.       textFont(font, 12);    
  188.       text(nf(i, 2), relativeX[i], y*2);    
  189.       textSize(30);
  190.     }
  191.     
  192.     
  193.   }
  194.   
  195.   
  196.   // wraps the word in a sensitive  bounding box
  197.   void createBox(String word, float relativeX, String toUrl)
  198.   {
  199.     if(isClicked(relativeX, word)) 
  200.     {
  201.       link(toUrl);
  202.       isClicked = false;
  203.     }
  204.     
  205.     /// mouseOver colors to be implemented
  206.   }
  207.   
  208.   
  209.   
  210.   boolean isOver(float relativeX, String word) /// thats not good... Objects? Arrays?
  211.   {
  212.     float upperY = y - textAscent();
  213.     float lowerY = y + textDescent();
  214.     float rightX  = relativeX + textWidth(word);
  215.     if (mouseY < lowerY && mouseY > upperY && mouseX > x && mouseX < rightX)
  216.     {
  217.       return true;
  218.     }
  219.     else
  220.     {
  221.       return false;
  222.     }
  223.   }

  224.   boolean isClicked(float relativeX, String word) /// thats not good... Objects? arrays?
  225.   {
  226.     return (isOver(relativeX, word) && isClicked);
  227.   }
  228.   
  229.   
  230.   
  231.   
  232. }//end TwTextBox
  233.