here is a rough approach (B)
Usage
paint with the mouse
q abort current rect while dragging it
backspace: delete old ones
cursor left and right: select a rect (it has red frame)
return: enter edit mode for that red rect; then :
- left mouse: pos
- right mouse: size
return again : return to draw mode
Explanation:
The rects are stored as data and the data is drawn (see above).
Each rect is an object of the class Rectangle. All the rects properties and
methods are within this class.
All rects are stored in an arraylist (which is a kind of array but with variable size).
The members of the arraylist are objects of the type class Rectangle. Each of
those objects has it's own x,y and width and height etc.
The variable editorMode
Whether you are in draw mode or edit mode is denoted by the var editorMode.
It is
false at the beginning of course.
Since the var editorMode is very fundamental for the behaviour of the program, it occurs often.
E.g. in draw() where two different main functions are called based on editorMode.
I hope this is not too much new information.
Greetings, Chrisir
- //
- ArrayList<Rectangle> rectangles;
- //
- // mouse dragging
- boolean mouseHold=false;
- int startX, startY;
- //
- // editing rects
- int selectNum;
- boolean editorMode=false;
- //
- void setup() {
- size(700, 700);
- smooth();
- rectangles = new ArrayList();
- }
- void draw() {
- background(255);
- if (editorMode) {
- editingDraw();
- }
- else
- {
- normalDraw();
- }
- }
- // -----------------------------------------------------------------
- void normalDraw() {
- //
- // show existing rects
- for (int i = rectangles.size()-1; i >= 0; i--) {
- Rectangle ball = rectangles.get(i);
- if (i==selectNum)
- ball.selected=true;
- else
- ball.selected=false;
- ball.display();
- } // for
- // println( rectangles.size());
- // this is during dragging
- if (mouseHold) {
- fill(0);
- text("Release mouse to draw, press 'q' to abort rect. Rect is from "
- +startX+", "
- +startY+" to "
- +mouseX+", "
- +mouseY+".", 20, 20);
- stroke(30);
- noFill();
- rect(startX, startY, mouseX-startX, mouseY-startY);
- }
- else
- {
- // when he is not dragging
- fill(0);
- text("Draw mode: Backspace to undo. Click and hold mouse to draw. Mouse is at "
- +mouseX+", "
- +mouseY
- +".", 20, 20);
- } // else
- }
- // -----------------------------------------------------------------
- void editingDraw() {
- // show existing rects
- for (int i = rectangles.size()-1; i >= 0; i--) {
- Rectangle ball = rectangles.get(i);
- if (i==selectNum)
- ball.selected=true;
- else
- ball.selected=false;
- ball.display();
- }
- // println( rectangles.size());
- // this is during dragging
- if (mouseHold) {
- if (mouseButton == LEFT) {
- //
- fill(0);
- text("Drag to change position", 20, 20);
- stroke(30);
- noFill();
- Rectangle ball = rectangles.get(selectNum);
- ball.x=mouseX;
- ball.y=mouseY;
- }
- else if (mouseButton == RIGHT) {
- //
- fill(0);
- text("Drag to change size", 20, 20);
- Rectangle ball = rectangles.get(selectNum);
- ball.w=mouseX-ball.x;
- ball.h=mouseY-ball.y;
- } // if (Button==RIGHT)
- } // if (mouseHold)
- else
- {
- // do nothing
- // when he is not dragging
- fill(0);
- text("Edit mode: Left drag Pos, Right drag Size. Return to go back to draw. Mouse is at "
- +mouseX+", "
- +mouseY
- +".", 20, 20);
- }
- }
- // -----------------------------------------------------------------
- void mousePressed() {
- // depending
- if (editorMode) {
- // starts dragging
- mouseHold=true;
- }
- else
- {
- // starts dragging
- mouseHold=true;
- startX=mouseX;
- startY=mouseY;
- }
- }
- void mouseReleased() {
- if (editorMode) {
- // ends dragging
- mouseHold=false;
- }
- else
- {
- // ends dragging
- if (mouseHold) {
- rectangles.add(new Rectangle(startX, startY, mouseX-startX, mouseY-startY));
- mouseHold=false;
- selectNum=( rectangles.size());
- }
- }
- }
- void keyPressed() {
- // key
- if (key!=CODED) {
- if (key=='q') {
- // println("abort rect");
- // ends dragging without drawing
- mouseHold=false;
- }
- else if (key==BACKSPACE) {
- // undo
- mouseHold=false;
- if (rectangles.size()>0)
- rectangles.remove(rectangles.size()-1);
- }
- else if (key==RETURN||key==ENTER) {
- // toggle
- editorMode=!editorMode;
- }
- else
- {
- // do nothing
- }
- }
- else
- {
- // if (key == CODED) from here on
- if (keyCode == LEFT)
- {
- // println("selected -- ");
- selectNum--;
- if (selectNum<0)
- selectNum=0;
- }
- else if (keyCode== RIGHT)
- {
- // println("selected ++ ");
- selectNum++;
- if (selectNum>rectangles.size())
- selectNum=rectangles.size();
- }
- else
- {
- // do nothing
- }
- } // if (key == CODED)
- }
- // ==================================================================
- class Rectangle {
- float x;
- float y;
- float w;
- float h;
- boolean selected = false ;
- //
- // constr
- Rectangle(float tempX, float tempY, float tempW, float tempH) {
- x = tempX;
- y = tempY;
- w = tempW;
- h = tempH;
- } // constr
- //
- void display() {
- if (editorMode) {
- //
- fill(0);
- noStroke();
- if (selected) {
- stroke (255, 2, 2);
- noFill();
- }
- rect(x, y, w, h);
- }
- else
- {
- //
- fill(0);
- noStroke();
- if (selected)
- stroke (255, 2, 2);
- rect(x, y, w, h); // used w here
- }
- } // method
- //
- } // class
- //