trouble draging items

edited May 2016 in Library Questions

Hi All I am trying to write a sketch to draw the track plan of my model railroad on my screen using Process I have the data in a table which i load and run, ok but to edit the location of a {turnout/point/switch) depending on which country you come from I have to edit the table in notepad each time. what i would like to do is be able to drag the {turnout/point/switch) to a new location and have the table up dated automatically. by dragging it with the mouse. i have tried doing something like what is in the mouse function demo sketch, but with no luck

can any one help me.

//import controlP5.*;
import processing.serial.*;
int tableRows=20;
Serial myPort;
Table myTrack;  //used to draw track point/turnouts on screen
String FileName2="track.csv";
int id, ls, le ;
String na, lrs, rt, vis;
PFont f1;
PFont f2;
PFont f3;
Turnout turnout;  // class
float bx;
float by;
int boxSize = 10;
boolean overBox = false;
boolean locked = false;
float xOffset = 0.0; 
float yOffset = 0.0; 

void setup() {
  size(1400, 900);
  bx = width/2.0;
  by = height/2.0;
  turnout = new Turnout() ; //new class
  printArray(Serial.list());
  // myPort = new Serial(this, Serial.list()[0], 9600);
  f1 = createFont("Arial", 16, true);
  f2 = createFont("Arial Black", 18, true);
  f3 = createFont("Arial", 14, true);
  strokeWeight(1);
  stroke(0); 
  textFont(f1);
  // uncomment the next  line to creat new tables
  //track();
 // then  edit table in notpad
  //---- load tables ---------------
  myTrack = loadTable(FileName2, "header");
  println(myTrack.getRowCount() + " total rows in myTrack");
}

void draw() {
  background(0, 250, 250); 
  strokeWeight(0);
  stroke(200);   
  fill(0);
  text((Serial.list()[0]), 10, 100); 
  for (int i = 0; i <= tableRows-1; i++) 
  {
    list_turnouts(i);  // get info from table
    String letter= vis.toUpperCase();
    switch(letter) {
    case "Y"  :   // is it visable ?
      turnout.setit( id, na, ls, le, lrs, rt, vis);
      turnout.display();
      break;
    }
  }
  stroke(0);
  fill(255);
  saveTable(myTrack, FileName2);
}

// now read table and set all lower case to uper case
void list_turnouts(int temp_i) {
  int i=temp_i;
  id=  myTrack.getInt(i, "T_Id");  
  na=  myTrack.getString(i, "Name"); 
  String na1= na.toUpperCase();
  myTrack.setString(i,"Name", na1);
   ls = myTrack.getInt(i, "Loc_S");  
  le = myTrack.getInt(i, "Loc_E");
    lrs =myTrack.getString(i, "L_R_S");
  String lrs1= lrs.toUpperCase();
  myTrack.setString(i,"L_R_S", lrs1);
   rt = myTrack.getString(i, "Rotate");  
   String rt1= rt.toUpperCase();
  myTrack.setString(i,"Rotate", rt1);
    vis = myTrack.getString(i, "Visable"); 
   String vis1= vis.toUpperCase();
  myTrack.setString(i,"Visable", vis1);

}

void mousePressed() {
  if (overBox) { 
    locked = true; 
    fill(0);
  } else {
    locked = false;
  }
  xOffset = mouseX-bx; 
  yOffset = mouseY-by;
}

void mouseDragged() {
  if (locked) {
    bx = mouseX-xOffset; 
    by = mouseY-yOffset;
  }
}

void mouseReleased() {
  locked = false;
}

class Turnout {  
  // verible used inside class Point
  int px; 
  int py; 
  int pid;
  String pl;
  String pa;
  String pv;
  String pn;
  String temps;

  Turnout() {
  };

  void setit(int id, String na, int x1, int y1, String sl, String sa, String vis) {
    pid=id;
    pn=na;
    px = x1+300;// screen off set
    py = y1+50;  // screen off set
    pl = sl;
    pa = sa;
    pv=vis;
  }

  void display() {
    int ex=0;
    int ey=0;
    textFont(f3); 
    strokeWeight(4); 
    stroke(0);
    ex=0; 
    ey=0;
    String letter1= pl.toUpperCase();
    switch(letter1) {
    case "R": 
      ex=px+25;
      text(pn, py-10, px-10);//print name above line
      break;
    case "L": 
      ex=px-25;
      text(pn, py-10, px+20);//print name below line
      break;
    case "": 
      ex=px;
      break;
    } 
    String letter2= pa.toUpperCase();
    switch(letter2) {
    case "W": 
      ey=py-25;
      break;
    case "E": 
      ey=py+25;
      break;
    case "": 
      ey=py   ;
      break;
    }
    //draw switch
    ellipse(py, px, 15, 15);
    line( py, px, ey, ex);
    line( py-25, px, py+25, px);
     strokeWeight(1);
// hylight turnout with red dot
    if (mousePressed == true) {
      if (dist(mouseX, mouseY, py, px) < 15) { //hylight 
        fill(255, 0, 0);
        ellipse(py, px, 15, 15);
        textFont(f1);
        //next 3 line for debug
        text(pid, 10, 20);
        text(py-50, 10, 40);
        text(px-300, 10, 60);     
      } 
       fill(0);
    }
    // now if draged
    // move it ??
  }
}//

content of table looks like this

T_Id,Name,Loc_S,Loc_E,L_R_S,Rotate,Visable
1,00,10,100,R,E,Y
1,N1,40,80,L,W,Y
2,N2,70,60,R,E,Y
3,N3,100,40,L,W,Y
4,N1,0,0,,,
5,N1,0,0,,,
6,N1,0,0,,,
7,N1,0,0,,,
8,N1,0,0,,,
9,N1,0,0,,,
10,N1,0,0,,,
11,N1,0,0,,,
12,N1,0,0,,,
13,N1,0,0,,,
14,N1,0,0,,,
15,N1,0,0,,,
16,N1,0,0,,,
17,N1,0,0,,,
18,N1,0,0,,,
19,N1,0,0,,,
20,N1,0,0,,,

Answers

  • So you want eg use english, french, german lnguage?

    make a 2D array with the text in each language

    One index is the current command you want to display, one is the current languageIndex (0=englisch, 1=german, 2=french)

  • hi Chrisir each row of the table hold the location and shape of a turnout i want to move around the screen eg. turn id 1 from x100,y100 to x300,y350. then save that change in the table at id1.

  • Oh, I misunderstood you

  • Answer ✓

    found the answer used keycode

    void keyPressed() { String Mystring; int i=0; // first enter row index in text box Mystring= cp6.get(Textfield.class, "T_Id").getText(); i=int(Mystring);
    text(i,300,20); // now read row data id= myTrack.getInt(i, "T_Id");
    na= myTrack.getString(i, "Name");
    ls = myTrack.getInt(i, "Loc_S"); le = myTrack.getInt(i, "Loc_E"); lrs =myTrack.getString(i, "L_R_S"); // 1 or 2 rt = myTrack.getString(i, "Rotate"); // pt.Point(ls,le,rt,lrs); vis = myTrack.getString(i, "Visable"); //1 or 2 if (key == CODED) { if (keyCode == UP) { ls--;text("up-",340,20); myTrack.setInt(id, 2, ls); } if (keyCode == DOWN){text("down-",340,20); ls++; myTrack.setInt(id, 2, ls); } if (keyCode == LEFT) {text("lext-",340,20); le--; myTrack.setInt(id, 3, le); } if (keyCode == RIGHT){text("right-",340,20); le++; myTrack.setInt(id, 3, le); } } // now save change back into table and save table myTrack.setInt(id, 2, ls); myTrack.setInt(id, 3, le); saveTable(myTrack, FileName2); }

Sign In or Register to comment.