//----- public void pickRect(int x, int y) { //save current selected rectangle //to avoid double save of undo int lastPick = -1; if (selectedIndex >= 0) { lastPick = selectedIndex; } unpick(); //undo any selection //see if one is being selected for (int i = 0; i < drawings.Count; i++) { VisRectangle v = (VisRectangle)drawings[i]; if (v.contains(x, y)) //did click inside a rectangle { selectedIndex = i; //save it rectSelected = true; if (selectedIndex != lastPick) //but don't save twice { caretakr.rememberPosition(v); } v.setSelected(true); //turn on handles repaint(); //and redraw } } }
//----- public void reDraw(Graphics g) { for (int i = 0; i < drawings.Count; i++) { VisRectangle v = (VisRectangle)drawings[i]; v.draw(g); } }
//------ public Memento(VisRectangle vrect) { visRect = vrect; rect = visRect.rects; x = rect.x; y = rect.y; w = rect.w; h = rect.h; }
//----- public void unpick() { if (rectSelected && (selectedIndex >= 0) && (selectedIndex < drawings.Count)) { VisRectangle vis = (VisRectangle)drawings[selectedIndex]; vis.setSelected(false); selectedIndex = -1; rectSelected = false; canvas.Refresh(); } }
//----- public void drag(int x, int y) { if (rectSelected) { VisRectangle v = (VisRectangle)drawings[selectedIndex]; if (v.contains(x, y)) { v.move(x, y); repaint(); } } }
//----- public void createRect(int x, int y) { unpick(); //make sure no rectangle is selected if (startRect) //if rect button is depressed { int count = drawings.Count; caretakr.Add(count); //Save previous drawing list size VisRectangle v = new VisRectangle(x, y); //create a rectangle drawings.Add(v); //add new element to list startRect = false; //done with this rectangle rect.setSelected(false); //unclick button canvas.Refresh(); } else { pickRect(x, y); //if not pressed look for rect to select } }
public void rememberPosition(VisRectangle vr) { mem = new Memento(vr); undoList.Add(mem); }