translate and scale and then check mouseX and mouseY!? Flowchart
in
Programming Questions
•
1 year ago
Hello,
this is probably simple but I couldn't find it on the forum.
I did a flowchart and you can click the single items and drag them.
This is done in the class with checking if mouseX and mouseY are within the item
and when it is the item returns mouseOver == true and can be dragged. Works fine.
Line 406/407 are bold.
But when I scale or translate the whole graphic (right mouse button drag)
the items wander on the screen but mouseX and mouseY don't.
So the checking if mouseX and mouseY are within the item doesn't work anymore.
I tried to multiply the mouseX and mouseY with my var myScale but didn't work.
What is the formula please to check if mouseX and mouseY are within the item?
Thank you so much!
Greetings, Chrisir
- // this prgram provides a flowchart / diagram
- // http://en.wikipedia.org/wiki/Flowchart
- ArrayList<GraphicalItem> graphicalItems;
- //
- final color lightGreen = color (2, 244, 2, 22);
- final color lightBlue = color(2, 0, 244, 22);
- // State of program
- final int stateNormal = 0;
- int state=stateNormal;
- //
- // for graphical:
- final int typeOfGraphicalItemStandardWithHeader = 0;
- final int typeOfGraphicalItemStandardWithoutHeader = 1;
- final int typeOfGraphicalItemConditional = 2;
- final int typeOfGraphicalItemStartEnd = 3;
- //
- // for graphical: which adaptor is used on a box?
- // (clockwise) north, east, south or west.
- final int adaptorNorth=0;
- final int adaptorEast=1;
- final int adaptorSouth=2;
- final int adaptorWest=3;
- //
- // for scale and translating
- float myScale = 1.0;
- float myTranslate = 0.0; // only y-direction
- // where to place next box vertically
- int lineY = 40;
- //
- // for drag and drop:
- // mouse dragged?
- boolean hold=false;
- GraphicalItem holdItem ; // which item is dragged
- // for drag and drop: difference between the upper left corner
- // x,y position and where mouse clicked in the graphical item
- PVector offSet;
- // ---------------------------------------------------------------
- void setup() {
- // size( screen.width, screen.height );
- size( 800, 600 );
- smooth();
- noStroke();
- fill(0);
- offSet=new PVector();
- // Create an empty ArrayList
- graphicalItems = new ArrayList();
- // fill the ArrayList
- initGraphical() ;
- } // func
- //
- //
- void draw() {
- background(255);
- //
- if (state==stateNormal) {
- scale(myScale);
- translate( 0, myTranslate ) ;
- showGraphical();
- }
- else {
- println ("Error 182" );
- }
- } // func
- //
- //
- // =================================================================
- // Init items
- void initGraphical() {
- // fills the graphicalItems in the arrayList
- graphicalItems.clear();
- lineY = 40;
- //
- graphicalItems.add ( new GraphicalItem(
- typeOfGraphicalItemStandardWithHeader, 20, lineY,
- "Entry 0", "This is interesting",
- null,
- lightGreen ));
- int EntryGreen = graphicalItems.size()-1;
- graphicalItems.add ( new GraphicalItem(
- typeOfGraphicalItemStandardWithHeader, 320, lineY,
- "Entry 1", "This is lightblue ",
- new AdaptorType(EntryGreen, adaptorWest, adaptorEast),
- lightBlue ));
- int EntryBlue = graphicalItems.size()-1;
- graphicalItems.add ( new GraphicalItem(
- typeOfGraphicalItemConditional,
- 100, lineY+100,
- "", "Conditional",
- new AdaptorType(EntryGreen, adaptorNorth, adaptorSouth),
- color (0) ));
- int EntryConditional = graphicalItems.size()-1;
- lineY+=70;
- graphicalItems.add ( new GraphicalItem(
- typeOfGraphicalItemStartEnd,
- 180, lineY+100,
- "", "Test2",
- new AdaptorType(EntryConditional, adaptorNorth, adaptorEast),
- color (0) ));
- lineY+=70;
- graphicalItems.add ( new GraphicalItem(
- typeOfGraphicalItemStartEnd,
- 40, lineY+100,
- "", "Test3",
- new AdaptorType(EntryConditional, adaptorNorth, adaptorWest),
- color (0) ));
- int EntryStartItem = graphicalItems.size()-1;
- graphicalItems.add ( new GraphicalItem(
- typeOfGraphicalItemStandardWithoutHeader,
- 240, lineY+200,
- "", "Test3",
- new AdaptorType(EntryBlue, adaptorNorth, adaptorSouth),
- color (0) ));
- //
- // add further Line to last item. This line
- // goes from the last item (it's west side) to item
- // number EntryStartItem (south)
- AddLineToElement( graphicalItems.size()-1,
- EntryStartItem,
- adaptorWest,
- adaptorSouth );
- //
- } // func
- //
- // Inputs ****************************************************
- //
- void mousePressed() {
- if (state==stateNormal) {
- if (mouseButton==LEFT) {
- for (int i = 0; i <= graphicalItems.size()-1; i++ ) {
- GraphicalItem myGraphicalItem1 = graphicalItems.get(i);
- if (myGraphicalItem1.isMouseOver()) {
- hold = true;
- holdItem = myGraphicalItem1;
- offSet.x= myGraphicalItem1.Position.x-mouseX;
- offSet.y= myGraphicalItem1.Position.y-mouseY;
- }
- } // for
- } // if
- } // if
- } // func
- //
- void mouseDragged() {
- if (state==stateNormal) {
- if (hold) {
- holdItem.Position.x = mouseX+offSet.x;
- holdItem.Position.y = mouseY+offSet.y;
- }
- if (mousePressed && mouseButton == RIGHT) {
- myScale = map(mouseX, 0, width, 0, 2);
- myTranslate = map(mouseY, 0, height, -height, height);
- } // if
- } // if
- } // func
- //
- void mouseReleased () {
- if (state==stateNormal) {
- if (mouseButton == LEFT) {
- hold = false;
- }
- }
- } // function
- //
- void keyPressed () {
- if (state==stateNormal) {
- //
- if (key=='r') {
- myScale = 1.0;
- myTranslate = 0.0;
- }
- }
- else {
- println("Error 429: unknown state ");
- }
- } // keyPressed
- //-----------------------------------------------------------
- //
- void AddLineToElement( int NumberOfStartItem, int NumberOfTargetItem,
- int adaptor1, int adaptor2) {
- // add a line
- GraphicalItem itemStart = graphicalItems.get(NumberOfStartItem);
- itemStart.addAdaptor ( new AdaptorType(NumberOfTargetItem, adaptor1, adaptor2) );
- }
- //
- void showGraphical() {
- // show the graphicalItems from the arrayList
- textAlign(LEFT);
- text ( "Graphical Representation"
- + " - "
- + "Move items with left Mouse button. Drag with right mouse button: scale "
- + "(x-direction) and up/down (y-direction).", 20, 14 );
- if (graphicalItems.isEmpty()) {
- text("Graphical Diagram is empty.", 220, 170 );
- }
- else
- {
- for (int i = 0; i <= graphicalItems.size()-1; i++ ) {
- GraphicalItem myGraphicalItem1 = graphicalItems.get(i);
- myGraphicalItem1.display() ;
- } // for
- } // if else
- } // func
- //
- GraphicalItem getElement( String textHeaderParam, String textNormalParam ) {
- // returns object or null
- for (int i = 0; i <= graphicalItems.size()-1; i++ ) {
- GraphicalItem myGraphicalItem1 = graphicalItems.get(i);
- if (myGraphicalItem1.textHeader.equals(textHeaderParam) &&
- myGraphicalItem1.textNormal.equals(textNormalParam)) {
- return myGraphicalItem1;
- } // if
- } // for
- return null;
- } // func
- //
- int getElementNumber( String textHeaderParam, String textNormalParam ) {
- // returns number of object or -1
- for (int i = 0; i <= graphicalItems.size()-1; i++ ) {
- GraphicalItem myGraphicalItem1 = graphicalItems.get(i);
- if (myGraphicalItem1.textHeader.equals(textHeaderParam) &&
- myGraphicalItem1.textNormal.equals(textNormalParam)) {
- return i;
- } // if
- } // for
- return -1;
- } // func
- //
- // ==============================================================
- class GraphicalItem {
- //
- PVector Position; // Pos and size of the whole box
- PVector boxSize; // Size
- final int headerHeight = 20; // Height of header text box
- color headerColor; // color of header box
- // the type of the item
- int typeOfGraphicalItem;
- // texts
- String textHeader="";
- String textNormal="";
- // Positions of adaptors
- PVector adaptorLeft, adaptorRight;
- PVector adaptorUp, adaptorDown;
- //
- // where the adaptors lead to: array of
- // AdaptorType (see class AdaptorType)
- AdaptorType adaptorTo[] = new AdaptorType [1] ;
- //
- //
- // constructor ----------
- GraphicalItem(
- int _typeOfGraphicalItem,
- int _x, int _y,
- String _header, String _normalText,
- AdaptorType _adaptorTo,
- color _tempColor) {
- //
- // constructor
- Position = new PVector( _x, _y);
- boxSize = new PVector( 100, 70 );
- textHeader = _header;
- textNormal = _normalText;
- adaptorTo [0] = _adaptorTo;
- //
- typeOfGraphicalItem=_typeOfGraphicalItem;
- //
- if (typeOfGraphicalItem == typeOfGraphicalItemConditional) {
- // Conditional item = ttiangle
- // calculate the four adaptors pos
- adaptorUp = new PVector( boxSize.x/2, 0 );
- adaptorRight = new PVector( 2*boxSize.x/3+11, boxSize.y/2 );
- // adaptorDown = new PVector( boxSize.x/2, boxSize.y );
- adaptorLeft = new PVector( boxSize.x/3-7, boxSize.y/2 );
- }
- else {
- // calculate the four adaptors pos
- adaptorUp = new PVector( boxSize.x/2, 0 );
- adaptorRight = new PVector( boxSize.x, boxSize.y/2 );
- adaptorDown = new PVector( boxSize.x/2, boxSize.y );
- adaptorLeft = new PVector( 0, boxSize.y/2 );
- }
- //
- headerColor = _tempColor;
- plausibilityCheck();
- }// constructor
- //
- void plausibilityCheck () {
- // checks whether there are not plausible parameters, e.g.
- // a defined header with a typeOfGraphicalItem that has no
- // header.
- if (typeOfGraphicalItem == typeOfGraphicalItemConditional ||
- typeOfGraphicalItem == typeOfGraphicalItemStandardWithoutHeader ||
- typeOfGraphicalItem == typeOfGraphicalItemStartEnd ) {
- if (!textHeader.trim().equals("")) {
- println("Remark 285: StandardWithoutHeader or Conditional etc. has defined header (not shown) with header: '"
- + textHeader
- + "' and normal text: '"
- + textNormal
- + "'. "
- + "Type being: "
- + typeOfGraphicalItem
- + ". Header ignored.");
- }
- }
- }
- //
- void display() {
- switch (typeOfGraphicalItem) {
- case typeOfGraphicalItemStandardWithHeader:
- displayStandardWithHeader() ;
- break;
- case typeOfGraphicalItemStandardWithoutHeader:
- displayStandardWithoutHeader();
- break;
- case typeOfGraphicalItemConditional:
- displayConditional();
- break;
- case typeOfGraphicalItemStartEnd:
- displayStartEnd();
- break;
- default:
- println("Error occured 245");
- break;
- } // switch
- //
- } // method
- //
- void displayStandardWithHeader() {
- fill (headerColor);
- stroke(0);
- // header rect
- rect (Position.x, Position.y,
- boxSize.x, headerHeight);
- fill(244);
- // rect for normal text field (lower half)
- rect (Position.x, Position.y+headerHeight,
- boxSize.x, boxSize.y-headerHeight);
- fill (0);
- text (textHeader, Position.x+4, Position.y+2, boxSize.x-5, headerHeight);
- text (textNormal, Position.x+4, Position.y+headerHeight+2, boxSize.x-5, boxSize.y-headerHeight);
- // adaptors --------
- for (int i=0; i < adaptorTo.length; i++) {
- if (adaptorTo[i] != null) {
- adaptorTo[i].display ( this ) ;
- } // if
- } // for
- } // method
- //
- void displayStandardWithoutHeader() {
- fill(244);
- // rect for text field
- rect (Position.x, Position.y,
- boxSize.x, boxSize.y);
- fill (0);
- text (textNormal, Position.x+4, Position.y+2, boxSize.x-5, boxSize.y);
- // adaptors --------
- for (int i=0; i < adaptorTo.length; i++) {
- if (adaptorTo[i] != null) {
- adaptorTo[i].display ( this ) ;
- } // if
- } // for
- } // method
- //
- void displayConditional() {
- // Conditional is triangle
- fill(244);
- triangle (Position.x, Position.y,
- Position.x+boxSize.x+0, Position.y,
- Position.x+boxSize.x/2+5, Position.y + boxSize.y);
- fill (0);
- textAlign(CENTER);
- text (textNormal,
- Position.x+2, Position.y+2,
- boxSize.x-5, boxSize.y );
- // adaptors ----------
- for (int i=0; i < adaptorTo.length; i++) {
- if (adaptorTo[i] != null) {
- adaptorTo[i].display ( this ) ;
- } // if
- } // for
- } // method
- //
- void displayStartEnd() {
- fill(244);
- ellipseMode(CORNER);
- ellipse (Position.x, Position.y, boxSize.x, boxSize.y);
- fill (0);
- textAlign(CENTER);
- text (textNormal,
- Position.x+7, Position.y+18,
- boxSize.x-5, boxSize.y );
- // adaptors ------------
- for (int i=0; i < adaptorTo.length; i++) {
- if (adaptorTo[i] != null) {
- adaptorTo[i].display ( this ) ;
- } // if
- } // for
- } // method
- //
- void addAdaptor(AdaptorType _adaptorTo) {
- // appends one line on a existing GraphicalItem
- if (_adaptorTo.adaptorToGraphicalItem > -1) {
- adaptorTo = (AdaptorType[])append( adaptorTo, _adaptorTo );
- }
- } // method
- //
- boolean isMouseOver() {
- if (mouseX*1>Position.x && mouseX*1<Position.x+boxSize.x &&
- mouseY*1>Position.y && mouseY*1<Position.y+boxSize.y) {
- return true;
- }
- else
- {
- return false;
- }
- } // method
- //
- boolean isMouseOver_OLD() {
- if (mouseX>Position.x && mouseX<Position.x+boxSize.x &&
- mouseY>Position.y && mouseY<Position.y+boxSize.y) {
- return true;
- }
- else
- {
- return false;
- }
- } // method
- } // class
- // ==============================================================
- class AdaptorType {
- // Represents one connecting line.
- //
- // Start point (FROM) is a point at
- // the GraphicalItem (a box) this
- // AdaptorType-object belongs to.
- // End point (TO) is another GraphicalItem
- // marked by the var adaptorToGraphicalItem
- // which is an Index for the ArrayList
- // graphicalItems.
- // To specify the exakt point at
- // the from-GraphicalItem, the var
- // fromAdaptor denotes whether it's
- // (clockwise) north, east, south or west.
- // And: To specify the exakt point at
- // the to-GraphicalItem, the var
- // toAdaptor denotes whether it's
- // (clockwise) north, east, south or west.
- //
- // Indices of another GraphicalItem
- int adaptorToGraphicalItem = -1; // other graphical item
- // from which Adaptor of the four
- int fromAdaptor = 1; // 0 = north adaptor, 1 = east adaptor, 2 = south adaptor, 3 = west adaptor
- // to which Adaptor of the four
- int toAdaptor = 1; // 0 = north adaptor, 1 = east adaptor, 2 = south adaptor, 3 = west adaptor
- //
- AdaptorType (int tempAdaptorToGraphicalItem,
- int tempFromAdaptor,
- int tempToAdaptor ) {
- adaptorToGraphicalItem = tempAdaptorToGraphicalItem;
- fromAdaptor = tempFromAdaptor;
- toAdaptor = tempToAdaptor;
- } //
- //
- void display(GraphicalItem startGraphicalItem) {
- if (adaptorToGraphicalItem > -1) {
- PVector startVector = getPVector
- (startGraphicalItem,
- fromAdaptor);
- PVector endVector = getPVector
- ( (GraphicalItem) graphicalItems.get( adaptorToGraphicalItem),
- toAdaptor);
- //
- line ( startVector.x, startVector.y,
- endVector.x, endVector.y );
- } // if
- } // method
- PVector getPVector(GraphicalItem startGraphicalItem, int whichAdaptor) {
- PVector buffer = new PVector ();
- //
- switch (whichAdaptor) {
- case adaptorNorth:
- buffer.x=startGraphicalItem.Position.x+startGraphicalItem.adaptorUp.x;
- buffer.y=startGraphicalItem.Position.y+startGraphicalItem.adaptorUp.y;
- break;
- case adaptorEast:
- buffer.x=startGraphicalItem.Position.x+startGraphicalItem.adaptorRight.x;
- buffer.y=startGraphicalItem.Position.y+startGraphicalItem.adaptorRight.y;
- break;
- case adaptorSouth:
- buffer.x=startGraphicalItem.Position.x+startGraphicalItem.adaptorDown.x;
- buffer.y=startGraphicalItem.Position.y+startGraphicalItem.adaptorDown.y;
- break;
- case adaptorWest:
- buffer.x=startGraphicalItem.Position.x+startGraphicalItem.adaptorLeft.x;
- buffer.y=startGraphicalItem.Position.y+startGraphicalItem.adaptorLeft.y;
- break;
- default:
- println ("Error 337");
- break;
- } // switch
- return buffer;
- } // method
- } // class
- // ==============================================================
1