@Override public void keyDown(KeyEvent e) { if (activeIndex != -1) { // If there is an active tool (activeIndex > -1), then it's key down method should // be called if (Controls.bank.status(Controls.WORKSPACE_TAKE_SCREENSHOT)) { activeChild.fill( new SaveFile("Save screenshot:", VagueWindow.getWindow().getBuffer(), activeChild)); } else if (Controls.bank.status(Controls.WORKSPACE_SAVE_IMAGE)) { BufferedImage save = Context.getContext().renderAsSave(); if (!SaveFile.save(save, lastSavePath.toString())) { lastSavePath = new StringBuilder(); // Clear save path activeChild.fill(new SaveFile("Save image:", save, activeChild, lastSavePath)); } } else if (Controls.bank.status(Controls.WORKSPACE_SAVE_AS)) { System.err.println("save as!"); lastSavePath = new StringBuilder(); // Clear save path activeChild.fill( new SaveFile( "Save image:", Context.getContext().renderAsSave(), activeChild, lastSavePath)); } else { activeChild.keyDown(e); } } }
@Override public void paint(GraphicsHandle graphics) { // this.fillBackground(); //Fill the background with color just in case for (int x = 0; x < width(); x += BG_WIDTH) { // Draw the tiled grey stone background image for (int y = 0; y < height(); y += BG_HEIGHT) { graphics.drawImage(Resources.bank.BACKGROUND, x, y); } } /* 'WORKSPACE' BUFFER EXPLANATION All child children except for one currently being moved are drawn because this Workspace's own buffer is then drawn to the 'workspace' buffer, which is a buffer of all Modules not being moved / resized. Doing it like this cuts down on the amount of graphical operations greatly because instead of drawing all children to both buffers, all static children can be drawn to the Module buffer and then that buffer can be drawn to the 'workspace' buffer, containing all the static children but not the dynamic child. The dynamaic child is then drawn onto this buffer because it needs to, just like every other child. */ for (WorkTool wt : children) { if (wt != moveTool) { wt.repaint(); } } if (moveTool != null) { // If a tool is being moved, it should not be drawn to the static buffer, but // does need to be drawn to the normal // graphics buffer moveTool.repaint(); } }
public void stopMoving() { for (WorkTool wt : children) { // Reset the position of the moved tool if it intersects any other tools if (wt != moveTool) { // If the module is not the moveTool, check for intersection if (wt.intersects(moveTool)) { moveTool .resetMovePosition(); // If the tool being moved is intersecting others, it should be // reset } } } moveTool = null; // Nullify the move tool so no tool will be given special graphical / other treatment repaint(); // Redraw the module because tools have been updated updateBuffer(); }
@Override public void keyType(KeyEvent e) { if (activeIndex != -1) { // If there is an active tool (activeIndex > -1), then it's key up method should be // updated activeChild.keyType(e); } }
/** * Adds a new child to the Container. * * @param m The child to add to the Container. */ protected final void addChild(WorkTool m) { m.setParent(this); // Set the parent of the child WorkTool[] tmp = children; // Expand the children array and add the new child. children = new WorkTool[children.length + 1]; System.arraycopy( tmp, 0, children, 0, tmp.length); // Copy over the old values so they will be retained. children[tmp.length] = m; }
public void stopResizing() { if (moveTool != null) { if (!validSize(moveTool)) { moveTool.resetResize(); } moveTool = null; repaint(); updateBuffer(); } }
public boolean validPosition(WorkTool t) { boolean valid = true; for (WorkTool wt : children) { if (wt != t) { if (t.intersects(wt)) { valid = false; } } } return valid; }
@Override public void mouseUp(MouseEvent e) { if (activeIndex == -1) { // If the activeIndex is -1, a tool may be being created if (createTool) { // If a tool was being created: createTool = false; // update createTool so mouseMove() will behave properly createTool(); // Create the new tool child and add it to the Workspace } } else { // If there is an active tool (activeIndex > -1), then that tool should be updated activeChild.mouseUp(e); } }
public void createTool() { if (Controls.bank.status( Controls .WORKSPACE_GRID_SNAP)) { // If the snapping control is activated, the new tool needs to // be snapped to grid toolStart.snap(GRID_SIZE); // Snaps the tool start to grid toolEnd.snap(GRID_SIZE); // Snaps the tool end to grid } WorkTool newTool = WorkTool.create(toolStart, toolEnd); // Create the new tool /* Check if new tool is legitimate - this requires two checks: - The new tool has a size that is at least the minimum size - The new tool does not intersect any currently existing tools */ if (newTool.width() >= MIN_SIZE && newTool.height() >= MIN_SIZE) { boolean create = true; // Stores whether the new tool should be added for (WorkTool wt : children) { if (newTool.intersects(wt)) { create = false; } // If the new tool intersects an existing tool, it should not be created } if (create) { // Create is only true if the new tool did not intersect any other tools newTool.setWorkspace(this); // Allow the new WorkTool to use move and resize functions addChild(newTool); // Add the new tool as a child // children[children.length - 1].draw(); //if a child was added, it needs to be drawn // initially } } repaint(); // If createTool, the Workspace always needs to be re-drawn because even if a tool // wasn't created, the red square needs to be un-drawn }
@Override public void mouseMove(Vector pos, Vector dif) { if (activeIndex > -1) { // If the activeIndex is greater than 1, the active Module needs to be checked for // activeness retaint and if so, updated with its own mouseMove() if (!activeChild .retainFocus()) { // If the child is retaining focus, there is no point in checking if it // is no longer // active, because it wants to retain focus even if the mouse is no longer inside its // bounds. if (!activeChild.containsPoint(pos) || !activeChild .visible()) { // if the child is not retaining focus, check to make sure that // that child should still have focus activeChild .onUnfocus(); // If the child is no longer focused, it's onUnfocus() method should be // called updateActiveChild(pos); // Update the active child in case a different one is now active } } // Pass mouse coordinates onto child module but where the coordinates passed will have an // origin // at the top left corner of the child module if (activeIndex > -1) { activeChild.mouseMove( pos.getDif(activeChild.position()), dif); // Pass the mouse movement on to the active child (or the new active child, if it // was just updated) } } else if (createTool) { // If there is not an active child, but tools are being created, // nothing other than tool creation should be happending if (Controls.bank.status( Controls.WORKSPACE_SQUARE_TOOL)) { // If the square tool control is active, make the tool // square toolEnd = new Vector( pos.x, pos.x + (toolStart.y - toolStart .x)); // Right now, squaring the tool is done using the x coordinate //// TODO: Make much better square tool creation algorithm (need to consider the most // user-friendly way to do so) } else { toolEnd = pos; // The toolEnd is updated the mouse position and the toolStart remains anchored } drawTool(); // Draw the tool that is potentialaly going to be created } else { // If absolutely nothing else is happening, update the active child because the mouse // may have moved into focus of a child module updateActiveChild(pos); } }
@Override public void mouseDown(MouseEvent e) { if (activeIndex == -1) { // If there is no active tool, mouse down creates new tools createTool = true; // Set the tool creation flag so that the mouseMove() method will do tool creation toolStart = new Vector(mousePosition()); // Copy the mousePosition into both the toolStart and toolEnd toolEnd = new Vector( toolStart); // both are equal to mousePosition because there is no other info yet repaint(); updateBuffer(); // draw(); //Draw to update the workspace buffer } else { // If there is an active tool (activeIndex > -1), then it should be updated activeChild.mouseDown(e); } }
private void drawTool() { /// argh this wording tho GraphicsHandle handle = beginDraw(); handle.drawImage(workspace, 0, 0, null); // Draw the current buffer of the workspace Vector start = new Vector(), size = new Vector(); // Store where the being created tool should be drawn /* WORKTOOL VECTOR ORGANIZATION Vector start - stores the start draw position of the tool Vector size - stores the size of the tool being draw The start will consist of the lesser x and y out of toolStart and toolEnd. The size will consist of the size required to draw based on this - it will consist of the difference between the greater coordinate and the lesser coordinate becauase that is the size of the tool. */ if (toolStart.x < toolEnd.x) { start.x = toolStart.x; size.x = toolEnd.x - toolStart.x; } else { start.x = toolEnd.x; size.x = toolStart.x - toolEnd.x; } if (toolStart.y < toolEnd.y) { start.y = toolStart.y; size.y = toolEnd.y - toolStart.y; } else { start.y = toolEnd.y; size.y = toolStart.y - toolEnd.y; } /* How snapping works with new tools - The toolStart and toolEnd are not themselves snapped because if the snapping control becomes inactive, toolStart and toolEnd will need to revert to what they were before snapping started. When drawing the tools, however, if they are being snapped, the Vectors used for drawing ('start' and 'size') do need to be snapped such that an accurate picture of the new tool is drawn. If the snapping control continues to be active when the tool is actually created, then both toolStart and toolEnd will be snapped because they had not been snapped previously. */ if (Controls.bank.status(Controls.WORKSPACE_GRID_SNAP)) { start.snap(GRID_SIZE); // Snap the drawing Vectors if the snapping control is active size.snap(GRID_SIZE); } /* The drawn rectangle should appear blue if the tool is valid and red if the tool is invalid. The only cases where the tool is invalid (currently) are if it is smaller than the minimum size or if it intersects a currently existing tool. Therefore, before drawaing the tools, it is checked if they are valid to present a real-time picture to the user if the tool is valid or not. */ boolean valid = true; // If true, the tool is valid and is drawn blue; otherwise, it is invalid and drawn // red (and not created when the mouse is released.) if (size.x >= MIN_SIZE && size.y >= MIN_SIZE) { // Checks to see if the tool is of a valid size Rectangle validator = new Rectangle( start, size); // Create a rectangle with the potential bounds to check for intersections for (WorkTool wt : children) { // Check to see if the bounds of the potential tool intersect any existing // tools if (wt.intersects(validator)) { valid = false; // if the bounds intersect and existing tool, the tool should be drawn as red } } } else { valid = false; // If the tool is not of a valid size, it should be drawn red } handle.setColor( valid ? TOOL_FILL_COLOR : BAD_TOOL_FILL_COLOR); // Set the fill color of the tool based on whether the tool is // valid or not handle.fillRect( start.x + 1, start.y + 1, size.x - 2, size.y - 2); // Fill the projected bounds of the created tool handle.setColor( valid ? TOOL_BORDER_COLOR : BAD_TOOL_BORDER_COLOR); // Set the border color based on whether the tool is valid or // not handle.drawRect( start.x, start.y, size.x - 1, size.y - 1); // Border in the projected bounds of the created tool endDraw(handle); }
public boolean validSize(WorkTool t) { return t.width() >= MIN_SIZE && t.height() >= MIN_SIZE && validPosition(t); }
@Override public void mouseScroll(MouseWheelEvent e) { if (activeIndex != -1) { activeChild.mouseScroll(e); } }