-
-
Save nuno1212s/fe4324a2dfc91a25d002 to your computer and use it in GitHub Desktop.
Implementation of a scrolling string.
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
public class ScrollingString { | |
private String original; | |
private int width; | |
private int position; | |
private ChatColor color = ChatColor.RESET; | |
public ScrollingString(String original, int width) { | |
this.original = original; | |
//Allow for colors | |
this.width = width-2; | |
if (width <= 0) | |
throw new IllegalArgumentException("Width value has to be greater than 0"); | |
else if (original.length() < width) | |
throw new IllegalArgumentException("String length has to be greater than the width value"); | |
} | |
public String scrollForward() { | |
position++; | |
if (position == original.length()) | |
reset(); | |
return getScrolled(); | |
} | |
public void scrollBackward() { | |
position--; | |
if (position < 0) | |
position = original.length() - 1; | |
} | |
public void reset() { | |
position = 0; | |
} | |
public void setOriginal(String original) { | |
if (original.length() < width) | |
throw new IllegalArgumentException("String length has to be greater than the width value"); | |
this.original = original; | |
reset(); | |
} | |
public void append(String s) { | |
original += s; | |
} | |
public String getOriginal() { | |
return this.original; | |
} | |
public String getScrolled() { | |
StringBuilder sb = new StringBuilder(original); | |
int e = position + width; | |
if (position > 0) { | |
//Check for color codes before the current iteration | |
if (original.charAt(position - 1) == ChatColor.COLOR_CHAR) { | |
color = ChatColor.getByChar(original.charAt(position)); | |
//Move one further so the letter does not appear | |
position++; | |
//Return the string with the new position | |
return getScrolled(); | |
} | |
//Check for color in the first letter of the current iteration | |
else if (original.charAt(position) == ChatColor.COLOR_CHAR) { | |
color = ChatColor.getByChar(original.charAt(position+1)); | |
//If found, move two positions forward to remove the color code spaces | |
position += 2; | |
//Return the string with the new position | |
return getScrolled(); | |
} | |
} | |
if (e >= original.length()) { | |
//Checks for color codes in the last letter of the current iteration | |
int secondPosition = (width - (original.length() - position) - 1 < 0) ? 0 : (width - (original.length() - position) - 1); | |
if (sb.charAt(secondPosition) == ChatColor.COLOR_CHAR) { | |
sb.setCharAt(secondPosition, ' '); | |
} | |
} else { | |
//Checks for color codes in the last letter of the current iteration | |
if (sb.charAt(e - 1) == ChatColor.COLOR_CHAR) { | |
sb.setCharAt(e - 1, ' '); | |
} | |
} | |
return e > original.length() ? color + sb.substring(position, original.length()) + sb.substring(0, width - (original.length() - position)) : color + sb.substring(position, e); | |
} | |
public int getWidth() { | |
return this.width; | |
} | |
public int getPosition() { | |
return this.position; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment