Yet displaying twitter entities - working code, library independent. I could use some advice ;-)
in
Programming Questions
•
1 year ago
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
- // note if you change the string, index must be upadated manually
- // i did not took care of thiscause twitter will provide this in a real tweet
- // if you want to try another String,
- // you can uncomment the indicated line at TwTextBox constructor to see proper indexes
- // that must be adjusted in setup, don't forget to adjust arrays size
- // but with this string there is no need to change nothing
- // i got this from a real tweet
- String s = "@legend_of_zelda: #ReasonsWhyWeDontGetAlong You've http://kidnapped.com my girlfriend #repeatedly. As the great rapper Shaggy once said: "wasn't me!"";
- int xPos = 20;
- int yPos = 50;
- int[] htIndex = new int [2];
- int[] urlIndex = new int [1];
- int[] mentIndex = new int [1];
- PFont font;
- TwTextBox tb = new TwTextBox();
- boolean isClicked = false;
- void setup() {
- size(1200, 200);
- background(0);
- smooth();
- font = createFont ("arial", 40);
- htIndex[1] = 18;
- htIndex[0] = 86;
- urlIndex[0] = 51;
- mentIndex[0] = 0;
- tb = new TwTextBox ( s, htIndex, urlIndex, mentIndex, xPos, yPos, font, 16);
- }
- void draw() {
- background(0);
- stroke(60);
- line(xPos, yPos, width, yPos);
- line(xPos, 0, xPos, height);
- tb.display();
- }
- void mousePressed()
- {
- isClicked = true;
- }
- void mouseReleased()
- {
- isClicked = false;
- }
- //////////////////******************//////////////////////
- class TwTextBox {
- // the full text
- String rawText;
- // to hold splited words
- String[] words;
- char splitter = ' ';
- // receivers for twitter data
- int[] hashIndex;
- int[] urlIndex;
- int[] mentionIndex;
- // coordinates
- float x;
- float y;
- // relative position holder and calculator
- float[] relativeX;
- float wordX = 0;
- //original string indexes holder and calculator
- float[] stringLegacyIndex;
- float indexSum;
- // font
- int fontSize;
- PFont font;
- //colors
- color regularTextColor = color(240);
- color hashtagColor = color(200, 33, 200);
- color urlColor = color(33, 200, 200);
- color mentionColor = color(200, 200, 33);
- color colorToDraw; // to hold the color to use
- TwTextBox()
- {
- //default
- }
- TwTextBox(String _rawText, int[] _hashIndex, int[] _urlIndex, int[] _mentionIndex, float _x, float _y
- , PFont _font, int _fontSize)
- {
- // passing vars
- rawText = _rawText;
- hashIndex = _hashIndex;
- urlIndex = _urlIndex;
- mentionIndex = _mentionIndex;
- x = _x;
- y = _y;
- wordX = _x;
- font = _font;
- fontSize = _fontSize;
- // create words array
- words = split(rawText, splitter);
- // init arrays based on words size
- relativeX = new float[words.length];
- stringLegacyIndex = new float[words.length];
- // add spaces to the end of words
- for (int i = 0; i < words.length; i++)
- {
- // font and size are crucial to calcualtions even not drawing
- textFont(font, fontSize);
- if (i < words.length -1) {
- String spacedWord = words[i] + " "; // don't add to space to last word
- words[i] = spacedWord;
- }
- // calc and store each word position in x
- relativeX[i] = wordX;
- wordX = wordX + textWidth(words[i]);
- // calc and store indexes from string
- indexSum = (i == 0)? 0 : indexSum + words[i-1].length();
- stringLegacyIndex[i] = indexSum;
- }
- //if the string is changed the indexes has to be redefined
- // uncomment folowing line to see the proper indexes that must be inputed in setup()
- //println("word: " + words[i]+ " is at index " + stringLegacyIndex[i] + " ");
- } // end constructor
- void display()
- {
- for (int i = 0; i < words.length; i++) {
- textSize(fontSize);
- // if no match below that's the color
- colorToDraw = regularTextColor;
- //test for entities
- //hashTag
- for (int j = 0; j < hashIndex.length; j++)
- {
- if(stringLegacyIndex[i] == hashIndex[j])
- {
- colorToDraw = hashtagColor;
- // form string will be a little different with real twitter
- StringBuilder sb = new StringBuilder("http://twitter.com/search?q=#");
- StringBuilder capHash = new StringBuilder(words[i]);
- capHash.deleteCharAt(0);
- sb.append(capHash.toString());
- if(sb.charAt(sb.length()-1) == ' ')
- sb.deleteCharAt(sb.length()-1);
- if(sb.charAt(sb.length()-1) == '.')
- sb.deleteCharAt(sb.length()-1);
- createBox(words[i], relativeX[i], sb.toString());
- }
- }
- // url
- for (int j = 0; j < urlIndex.length; j++)
- {
- if(stringLegacyIndex[i] == urlIndex[j])
- {
- colorToDraw = urlColor;
- createBox(words[i], relativeX[i], "http://forum.processing.org"); //placeholder
- }
- }
- // mentions
- for (int j = 0; j < mentionIndex.length; j++)
- {
- if(stringLegacyIndex[i] == mentionIndex[j])
- {
- colorToDraw = mentionColor;
- createBox(words[i], relativeX[i], "http://processing.org"); //placeholder
- }
- }
- fill(colorToDraw);
- noStroke();
- text(words[i], relativeX[i], y);
- //those were to build will vanish...
- stroke(180, 180, 20);
- textFont(font, 12);
- text(nf(i, 2), relativeX[i], y*2);
- textSize(30);
- }
- }
- // wraps the word in a sensitive bounding box
- void createBox(String word, float relativeX, String toUrl)
- {
- if(isClicked(relativeX, word))
- {
- link(toUrl);
- isClicked = false;
- }
- /// mouseOver colors to be implemented
- }
- boolean isOver(float relativeX, String word) /// thats not good... Objects? Arrays?
- {
- float upperY = y - textAscent();
- float lowerY = y + textDescent();
- float rightX = relativeX + textWidth(word);
- if (mouseY < lowerY && mouseY > upperY && mouseX > x && mouseX < rightX)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- boolean isClicked(float relativeX, String word) /// thats not good... Objects? arrays?
- {
- return (isOver(relativeX, word) && isClicked);
- }
- }//end TwTextBox
1