Advertisement
Guest User

Untitled

a guest
Apr 24th, 2017
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.90 KB | None | 0 0
  1. PImage backgroundImage;
  2. PFont font;
  3. int x = 0;
  4. int xoffset = 0;
  5. int deltax = 5;
  6. float scale = 512/30;
  7. float deltascale = 0.02;
  8. ArrayList<Object> ObjectList;
  9. String[] objectfile;
  10. String[] configfile;
  11. int savetimer = 0;
  12.  
  13. class Object {
  14. String name;
  15. float diameter;
  16. String unit;
  17. String sprite;
  18. PImage img;
  19. int wid;
  20. int hei;
  21. int xpos;
  22.  
  23. Object(String Oname, float Odiameter, String Ounit, String Oimg) {
  24. name = Oname;
  25. diameter = Odiameter;
  26. unit = Ounit;
  27. sprite = Oimg;
  28. img = loadImage(Oimg);
  29. wid = img.width;
  30. hei = img.height;
  31. }
  32. }
  33.  
  34. void setup() {
  35. size(1500, 800);
  36. frameRate(200);
  37. objectfile = loadStrings("objects.txt");
  38. configfile = loadStrings("config.ini");
  39. backgroundImage = loadImage("background.png");
  40. font = createFont("Franklin Gothic Book Regular.ttf", 32);
  41. textFont(font, 32);
  42. textAlign(CENTER, CENTER);
  43. ObjectList = new ArrayList();
  44. readObjects();
  45. readConfig();
  46. loadOffset();
  47. }
  48.  
  49. void addObject(String Aname, float Adiameter, String Aunit, String Aimg) {
  50. if (Aimg.equals("null")) {
  51. ObjectList.add(new Object(Aname, Adiameter, Aunit, "images/"+Aname+".png"));
  52. } else {
  53. ObjectList.add(new Object(Aname, Adiameter, Aunit, "images/"+Aimg));
  54. }
  55. }
  56.  
  57. void readObjects() {
  58. for (int i = 0; i < objectfile.length; i++) {
  59. String[] currentObject = split(objectfile[i], ",");
  60. addObject(currentObject[0], float(currentObject[1]), currentObject[2], currentObject[3]);
  61. }
  62. }
  63.  
  64. void readConfig() {
  65. for (int i = 0; i < configfile.length; i++) {
  66. char slash = configfile[i].charAt(0);
  67. if (slash != '/') {
  68. //getting config values based on line number
  69. if (i == 1) {
  70. xoffset = int(configfile[i]);
  71. } else if (i == 3) {
  72. scale = float(configfile[i]);
  73. }
  74. }
  75. }
  76. }
  77.  
  78. void loadOffset() {
  79. for (int i = 0; i <= xoffset; i++) {
  80. int x = 0 + xoffset;
  81. for ( Object drawObject : ObjectList ) {
  82. if (x+drawObject.img.width >= 0 && x <= 1500) {
  83. float dia;
  84. if (drawObject.unit.equals("km")) {
  85. dia = drawObject.diameter * 1000;
  86. } else {
  87. dia = drawObject.diameter;
  88. }
  89. //checks if image is too small to be drawn
  90. int drawWidth = int(scale * dia);
  91. int drawHeight = int(drawWidth * drawObject.hei / drawObject.wid);
  92. if (drawWidth > 0 && drawHeight > 0) {
  93. drawObject.img.resize(drawWidth, drawHeight);
  94. image(drawObject.img, x, 400-(drawObject.img.height/2));
  95. //calculates font size relative to object's current size
  96. int textSize;
  97. //checks if the name or diameter is longer, then uses the longer one for calculation
  98. if (drawObject.name.length() < str(drawObject.diameter).length()) {
  99. textSize = int(drawObject.img.width/str(drawObject.diameter).length());
  100. } else {
  101. textSize = int(drawObject.img.width/drawObject.name.length());
  102. }
  103. if (textSize > 0) {
  104. textSize(textSize);
  105. float texty = 418+(drawObject.img.height/2);
  106. if (texty < (418+(drawObject.img.height/2))) {
  107. texty = 418;
  108. }
  109. text(drawObject.name, x+(drawObject.img.width/2), texty);
  110. text(drawObject.diameter+drawObject.unit, x+(drawObject.img.width/2), texty+textSize);
  111. }
  112. }
  113. }
  114. drawObject.xpos = x;
  115. x += (drawObject.img.width*0.08 + drawObject.img.width);
  116. }
  117. }
  118. }
  119.  
  120. void draw() {
  121. image(backgroundImage, 0, 0);
  122. int x = 0 + xoffset;
  123. for ( Object drawObject : ObjectList ) {
  124. if (x+drawObject.img.width >= 0 && x <= 1500) {
  125. float dia;
  126. if (drawObject.unit.equals("km")) {
  127. dia = drawObject.diameter * 1000;
  128. } else {
  129. dia = drawObject.diameter;
  130. }
  131. //checks if image is too small to be drawn
  132. int drawWidth = int(scale * dia);
  133. int drawHeight = int(drawWidth * drawObject.hei / drawObject.wid);
  134. if (drawWidth > 0 && drawHeight > 0) {
  135. drawObject.img.resize(drawWidth, drawHeight);
  136. image(drawObject.img, x, 400-(drawObject.img.height/2));
  137. //calculates font size relative to object's current size
  138. int textSize;
  139. //checks if the name or diameter is longer, then uses the longer one for calculation
  140. if (drawObject.name.length() < str(drawObject.diameter).length()) {
  141. textSize = int(drawObject.img.width/str(drawObject.diameter).length());
  142. } else {
  143. textSize = int(drawObject.img.width/drawObject.name.length());
  144. }
  145. if (textSize > 0) {
  146. textSize(textSize);
  147. float texty = 418+(drawObject.img.height/2);
  148. if (texty < (418+(drawObject.img.height/2))) {
  149. texty = 418;
  150. }
  151. text(drawObject.name, x+(drawObject.img.width/2), texty);
  152. text(drawObject.diameter+drawObject.unit, x+(drawObject.img.width/2), texty+textSize);
  153. }
  154. //if (drawObject.name == "2007 TU24") {
  155. // println(drawObject.name+" "+str(int(drawObject.img.width*(scale*drawObject.diameter/drawObject.img.width))), str(int(drawObject.img.height*(scale*drawObject.diameter/drawObject.img.width))));
  156. //}
  157. }
  158. }
  159. drawObject.xpos = x;
  160. x += (drawObject.img.width*0.08 + drawObject.img.width);
  161. }
  162. //display deltax
  163. textSize(32);
  164. text(str(deltax), 1450, 10);
  165. text(str(deltascale),1450,50);
  166. //display Saved
  167. if (savetimer > 0) {
  168. text("Saved", 1450, 90);
  169. //text(str(savetimer),1450,130);
  170. }
  171. if (savetimer > 0) {
  172. savetimer -= 1;
  173. }
  174. println(str(scale));
  175. }
  176.  
  177. void mouseWheel(MouseEvent event) {
  178. float e = event.getCount();
  179. if (e < 0) {
  180. scale *= (1 + deltascale);
  181. } else if (e > 0) {
  182. scale *= (1 - deltascale) ;
  183. }
  184. }
  185.  
  186. void keyPressed() {
  187. if (key == CODED) {
  188. if (keyCode == LEFT) {
  189. xoffset += deltax;
  190. } else if (keyCode == RIGHT) {
  191. xoffset -= deltax;
  192. } else if (keyCode == UP) {
  193. deltax += 5;
  194. } else if (keyCode == DOWN) {
  195. deltax -= 5;
  196. }
  197. }
  198. if (key == 'r') {
  199. reloadObjects();
  200. } else if (key == 's') {
  201. for (int i = 0; i < configfile.length; i++) {
  202. if (i == 1) {
  203. configfile[i] = str(xoffset);
  204. } else if (i == 3) {
  205. configfile[i] = str(scale);
  206. }
  207. }
  208. saveStrings("config.ini", configfile);
  209. savetimer = 500;
  210. } else if (key == 'q') {
  211. deltascale += 0.02;
  212. } else if (key == 'a') {
  213. deltascale -= 0.02;
  214. }
  215. if (deltax < 0) {
  216. deltax = 0;
  217. } else if (deltascale < 0) {
  218. deltascale = 0;
  219. }
  220. }
  221.  
  222.  
  223. void keyReleased() {
  224. reloadObjects();
  225. }
  226.  
  227. void reloadObjects() {
  228. for ( Object reloadObject : ObjectList ) {
  229. if (reloadObject.xpos > 0 && reloadObject.xpos+reloadObject.img.width <= 1500) {
  230. reloadObject.img = loadImage(reloadObject.sprite);
  231. }
  232. }
  233. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement