Advertisement
Guest User

Untitled

a guest
Sep 2nd, 2014
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.00 KB | None | 0 0
  1. package com.dusty.legendofZelda.screen;
  2.  
  3. import java.util.Random;
  4. import com.badlogic.gdx.graphics.g2d.SpriteBatch;
  5. import com.badlogic.gdx.graphics.g2d.TextureRegion;
  6. import com.badlogic.gdx.math.Matrix4;
  7. import com.dusty.legendofZelda.Art;
  8. import com.dusty.legendofZelda.Input;
  9. import com.dusty.legendofZelda.LegendOfZelda;
  10.  
  11. public abstract class Screen {
  12. public int tileFrame = 0;
  13. protected static Random random = new Random();
  14. private LegendOfZelda legendofZelda;
  15. public SpriteBatch spriteBatch;
  16. public Matrix4 projection = new Matrix4();
  17. private final String[] chars = {
  18. "ABCDEFGHIJKLMNOP",
  19. "QRSTUVWXYZabcdef",
  20. "ghijklmnopqrstuv",
  21. "wxyz0123456789!?",
  22. "-., ¿()@#$[]\"^¡<",
  23. ">'¼¹½¾²©®«»"
  24. };
  25. private final String shortchars = "Iil1!.,\"'";
  26. private final String mediumchars = "r";
  27. private final String fullchars = "¼¹½¾²©®«»";
  28. private final String numbers = "0123456789";
  29.  
  30. public void removed() {
  31. spriteBatch.dispose();
  32. }
  33.  
  34. public final void init(LegendOfZelda legendofZelda) {
  35. this.legendofZelda = legendofZelda;
  36.  
  37. projection.setToOrtho(0,LegendOfZelda.GAME_WIDTH,LegendOfZelda.GAME_HEIGHT, 0, -1, 1);
  38.  
  39. spriteBatch = new SpriteBatch(2500);
  40. spriteBatch.setProjectionMatrix(projection);
  41. }
  42.  
  43. protected void setScreen(Screen screen) {
  44. legendofZelda.setScreen(screen);
  45. }
  46.  
  47. protected void changeScreen(Screen screen) {
  48. legendofZelda.changeScreen(screen);
  49. }
  50.  
  51. public void draw(TextureRegion region, int x, int y) {
  52. int width = region.getRegionWidth();
  53. if (width < 0) width = -width;
  54. spriteBatch.draw(region, x,y, width,region.getRegionHeight());
  55. }
  56.  
  57. public void drawPart(TextureRegion region, int x, int y, int w,int h) {
  58. if (w <= 0) return;
  59. if (h <= 0) return;
  60. spriteBatch.draw(region,x,y, w,h);
  61. }
  62.  
  63. public void drawAlpha(TextureRegion region, float x, float y,float a) {
  64. int width = region.getRegionWidth();
  65. if (width < 0) width = -width;
  66. spriteBatch.setColor(1,1,1,a);
  67. spriteBatch.draw(region,x,y, width,region.getRegionHeight());
  68. spriteBatch.setColor(1,1,1,1);
  69. }
  70.  
  71. public void draw(TextureRegion region, float x, float y) {
  72. int width = region.getRegionWidth();
  73. if (width < 0) width = -width;
  74.  
  75. spriteBatch.draw(region,(int)x,(int)y,width,region.getRegionHeight());
  76. }
  77.  
  78. public void drawAlpha(TextureRegion region, int x, int y,float a,float scale) {
  79. int width = region.getRegionWidth();
  80. if (width < 0) width = -width;
  81. spriteBatch.setColor(1,1,1,a);
  82. spriteBatch.draw(region,x + (width/2*(1-scale)),y + (region.getRegionHeight()/2*(1-scale)), width * scale,region.getRegionHeight()*scale);
  83. spriteBatch.setColor(1,1,1,1);
  84. }
  85.  
  86. public void draw(TextureRegion region, int x, int y,float scale) {
  87. int width = region.getRegionWidth();
  88. if (width < 0) width = -width;
  89. //spriteBatch.draw(region,x - (width/(1-scale)),y - (region.getRegionHeight()*(1-scale)), width * scale,-region.getRegionHeight() * scale);
  90. //spriteBatch.draw(region,x + (width/2*(1-scale)),y, width * scale,-region.getRegionHeight());
  91. spriteBatch.draw(region,x + (width/2*(1-scale)),y + (region.getRegionHeight()/2*(1-scale)), width * scale,region.getRegionHeight()*scale);
  92. }
  93.  
  94. public int getTextWidth(String txt) {
  95. txt = filterText(txt);
  96. txt = txt.replace("#","");
  97. txt = txt.replace("#C","");
  98. int width = 0;
  99. for (int i = 0; i < txt.length(); i++) {
  100. char ch = txt.charAt(i);
  101. for (int ys = 0; ys < chars.length; ys++) {
  102. int xs = chars[ys].indexOf(ch);
  103. if (xs >= 0) {
  104. if (mediumchars.indexOf(ch) >= 0) width += 4;
  105. else if (fullchars.indexOf(ch) >= 0) width += 8;
  106. else if (shortchars.indexOf(ch) >= 0) width += 3;
  107. else width += 6;
  108. }
  109. }
  110. }
  111. return width;
  112. }
  113.  
  114. public String filterText(String text) {
  115. text = text.replace("#HEAD","[]");
  116. text = text.replace("#QUARTER","¼¹");
  117. text = text.replace("#HALF","½¹");
  118. text = text.replace("#THIRD","¾²");
  119. text = text.replace("#FULL","©®");
  120. text = text.replace("#ARROWDOWN","¡");
  121. return text;
  122. }
  123.  
  124. public void drawText(String txt,int x, int y) {
  125. String[] newtxt = txt.split("#N");
  126. for (int i = 0; i < newtxt.length; i++) {
  127. newtxt[i] = filterText(newtxt[i]);
  128. drawTextLine(newtxt[i],x,y+i*16);
  129. }
  130. }
  131.  
  132. public void drawNumbers(int value,int x, int y,int len) {
  133. String txt = String.valueOf(value);
  134.  
  135. for (int i = 0;i < len;i++) {
  136. if (txt.length() < len) txt = "0" + txt;
  137. }
  138. for (int i = 0; i < txt.length(); i++) {
  139. char ch = txt.charAt(i);
  140. int xs = numbers.indexOf(ch);
  141. if (xs >= 0) spriteBatch.draw(Art.numbers[xs],x + i*6, y);
  142. }
  143. }
  144.  
  145. public void drawNumbers(int value,int x, int y) {
  146. String txt = String.valueOf(value);
  147. drawNumbers(value,x,y,txt.length());
  148. }
  149.  
  150. public void drawTextLine(String txt,int x, int y) {
  151. boolean center = false;
  152. if (txt.startsWith("#C")) {
  153. center = true;
  154. txt = txt.substring(txt.indexOf("#C")+2);
  155. }
  156.  
  157. if (txt.contains("#")) txt = txt.substring(0,txt.indexOf("#"));
  158.  
  159. int drawx = x + (center ? (int)(getTextWidth(txt)/2+4) : 0);
  160. if (center) drawx = (LegendOfZelda.GAME_WIDTH - getTextWidth(txt))/2;
  161. for (int i = 0; i < txt.length(); i++) {
  162. char ch = txt.charAt(i);
  163. for (int ys = 0; ys < chars.length; ys++) {
  164. int xs = chars[ys].indexOf(ch);
  165. if (xs >= 0) {
  166. spriteBatch.draw(Art.dialog[xs][ys],drawx, y);
  167. if (mediumchars.indexOf(ch) >= 0) drawx += 4;
  168. else if (fullchars.indexOf(ch) >= 0) drawx += 8;
  169. else if (shortchars.indexOf(ch) >= 0) drawx += 3;
  170. else drawx += 6;
  171. }
  172. }
  173. }
  174. }
  175.  
  176. public abstract void render();
  177.  
  178. public void tick(Input input) {
  179. }
  180.  
  181. public void hideInterface() {
  182. }
  183.  
  184. public void showInterface() {
  185. }
  186. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement