Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- PImage backgroundImage;
- PFont font;
- int x = 0;
- int xoffset = 0;
- int deltax = 5;
- float scale = 512/30;
- float deltascale = 0.02;
- ArrayList<Object> ObjectList;
- String[] objectfile;
- String[] configfile;
- int savetimer = 0;
- class Object {
- String name;
- float diameter;
- String unit;
- String sprite;
- PImage img;
- int wid;
- int hei;
- int xpos;
- Object(String Oname, float Odiameter, String Ounit, String Oimg) {
- name = Oname;
- diameter = Odiameter;
- unit = Ounit;
- sprite = Oimg;
- img = loadImage(Oimg);
- wid = img.width;
- hei = img.height;
- }
- }
- void setup() {
- size(1500, 800);
- frameRate(200);
- objectfile = loadStrings("objects.txt");
- configfile = loadStrings("config.ini");
- backgroundImage = loadImage("background.png");
- font = createFont("Franklin Gothic Book Regular.ttf", 32);
- textFont(font, 32);
- textAlign(CENTER, CENTER);
- ObjectList = new ArrayList();
- readObjects();
- readConfig();
- loadOffset();
- }
- void addObject(String Aname, float Adiameter, String Aunit, String Aimg) {
- if (Aimg.equals("null")) {
- ObjectList.add(new Object(Aname, Adiameter, Aunit, "images/"+Aname+".png"));
- } else {
- ObjectList.add(new Object(Aname, Adiameter, Aunit, "images/"+Aimg));
- }
- }
- void readObjects() {
- for (int i = 0; i < objectfile.length; i++) {
- String[] currentObject = split(objectfile[i], ",");
- addObject(currentObject[0], float(currentObject[1]), currentObject[2], currentObject[3]);
- }
- }
- void readConfig() {
- for (int i = 0; i < configfile.length; i++) {
- char slash = configfile[i].charAt(0);
- if (slash != '/') {
- //getting config values based on line number
- if (i == 1) {
- xoffset = int(configfile[i]);
- } else if (i == 3) {
- scale = float(configfile[i]);
- }
- }
- }
- }
- void loadOffset() {
- for (int i = 0; i <= xoffset; i++) {
- int x = 0 + xoffset;
- for ( Object drawObject : ObjectList ) {
- if (x+drawObject.img.width >= 0 && x <= 1500) {
- float dia;
- if (drawObject.unit.equals("km")) {
- dia = drawObject.diameter * 1000;
- } else {
- dia = drawObject.diameter;
- }
- //checks if image is too small to be drawn
- int drawWidth = int(scale * dia);
- int drawHeight = int(drawWidth * drawObject.hei / drawObject.wid);
- if (drawWidth > 0 && drawHeight > 0) {
- drawObject.img.resize(drawWidth, drawHeight);
- image(drawObject.img, x, 400-(drawObject.img.height/2));
- //calculates font size relative to object's current size
- int textSize;
- //checks if the name or diameter is longer, then uses the longer one for calculation
- if (drawObject.name.length() < str(drawObject.diameter).length()) {
- textSize = int(drawObject.img.width/str(drawObject.diameter).length());
- } else {
- textSize = int(drawObject.img.width/drawObject.name.length());
- }
- if (textSize > 0) {
- textSize(textSize);
- float texty = 418+(drawObject.img.height/2);
- if (texty < (418+(drawObject.img.height/2))) {
- texty = 418;
- }
- text(drawObject.name, x+(drawObject.img.width/2), texty);
- text(drawObject.diameter+drawObject.unit, x+(drawObject.img.width/2), texty+textSize);
- }
- }
- }
- drawObject.xpos = x;
- x += (drawObject.img.width*0.08 + drawObject.img.width);
- }
- }
- }
- void draw() {
- image(backgroundImage, 0, 0);
- int x = 0 + xoffset;
- for ( Object drawObject : ObjectList ) {
- if (x+drawObject.img.width >= 0 && x <= 1500) {
- float dia;
- if (drawObject.unit.equals("km")) {
- dia = drawObject.diameter * 1000;
- } else {
- dia = drawObject.diameter;
- }
- //checks if image is too small to be drawn
- int drawWidth = int(scale * dia);
- int drawHeight = int(drawWidth * drawObject.hei / drawObject.wid);
- if (drawWidth > 0 && drawHeight > 0) {
- drawObject.img.resize(drawWidth, drawHeight);
- image(drawObject.img, x, 400-(drawObject.img.height/2));
- //calculates font size relative to object's current size
- int textSize;
- //checks if the name or diameter is longer, then uses the longer one for calculation
- if (drawObject.name.length() < str(drawObject.diameter).length()) {
- textSize = int(drawObject.img.width/str(drawObject.diameter).length());
- } else {
- textSize = int(drawObject.img.width/drawObject.name.length());
- }
- if (textSize > 0) {
- textSize(textSize);
- float texty = 418+(drawObject.img.height/2);
- if (texty < (418+(drawObject.img.height/2))) {
- texty = 418;
- }
- text(drawObject.name, x+(drawObject.img.width/2), texty);
- text(drawObject.diameter+drawObject.unit, x+(drawObject.img.width/2), texty+textSize);
- }
- //if (drawObject.name == "2007 TU24") {
- // 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))));
- //}
- }
- }
- drawObject.xpos = x;
- x += (drawObject.img.width*0.08 + drawObject.img.width);
- }
- //display deltax
- textSize(32);
- text(str(deltax), 1450, 10);
- text(str(deltascale),1450,50);
- //display Saved
- if (savetimer > 0) {
- text("Saved", 1450, 90);
- //text(str(savetimer),1450,130);
- }
- if (savetimer > 0) {
- savetimer -= 1;
- }
- println(str(scale));
- }
- void mouseWheel(MouseEvent event) {
- float e = event.getCount();
- if (e < 0) {
- scale *= (1 + deltascale);
- } else if (e > 0) {
- scale *= (1 - deltascale) ;
- }
- }
- void keyPressed() {
- if (key == CODED) {
- if (keyCode == LEFT) {
- xoffset += deltax;
- } else if (keyCode == RIGHT) {
- xoffset -= deltax;
- } else if (keyCode == UP) {
- deltax += 5;
- } else if (keyCode == DOWN) {
- deltax -= 5;
- }
- }
- if (key == 'r') {
- reloadObjects();
- } else if (key == 's') {
- for (int i = 0; i < configfile.length; i++) {
- if (i == 1) {
- configfile[i] = str(xoffset);
- } else if (i == 3) {
- configfile[i] = str(scale);
- }
- }
- saveStrings("config.ini", configfile);
- savetimer = 500;
- } else if (key == 'q') {
- deltascale += 0.02;
- } else if (key == 'a') {
- deltascale -= 0.02;
- }
- if (deltax < 0) {
- deltax = 0;
- } else if (deltascale < 0) {
- deltascale = 0;
- }
- }
- void keyReleased() {
- reloadObjects();
- }
- void reloadObjects() {
- for ( Object reloadObject : ObjectList ) {
- if (reloadObject.xpos > 0 && reloadObject.xpos+reloadObject.img.width <= 1500) {
- reloadObject.img = loadImage(reloadObject.sprite);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement