Skip to content

Commit

Permalink
Merge pull request #96 from Vasheel/zoom-panel
Browse files Browse the repository at this point in the history
Implementation of zoom panel
  • Loading branch information
creme332 authored Jun 22, 2024
2 parents 38be088 + c9c4e34 commit 1ec32a9
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
import com.github.creme332.view.CanvasConsole;

public class CanvasConsoleController {
public CanvasConsoleController(CanvasConsole console, AppState app) {
public CanvasConsoleController(AppState app, CanvasConsole console) {
new ToolBarController(console.getToolbar(), app.getCanvasModel());
new SideMenuController(app, console.getSidebar());
new ToastController(app, console.getToast());
new ZoomPanelController(app.getCanvasModel(), console.getZoomPanel());
new ZoomPanelController(app, console.getZoomPanel());
}
}
5 changes: 2 additions & 3 deletions src/main/java/com/github/creme332/controller/Controller.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,10 @@ public Controller() {
Frame frame = new Frame(app);
frameController = new FrameController(app, frame);

// create controllers for views
new MenuBarController(app, frame.getMyMenuBar());
// create controllers for main screens
new CanvasController(app, frame.getMyCanvas());
new CanvasConsoleController(app, frame.getCanvasConsole());
new TutorialScreenController(app, frame.getTutorialCenter());
new CanvasConsoleController(frame.getCanvasConsole(), app);

// play start animation
frameController.playStartAnimation();
Expand Down
45 changes: 34 additions & 11 deletions src/main/java/com/github/creme332/controller/FrameController.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ public FrameController(AppState model, Frame frame) {
this.frame = frame;
this.app = model;

// create controller for menubar of frame
new MenuBarController(app, frame.getMyMenuBar());

model.addPropertyChangeListener(this);

frame.addComponentListener(new ComponentAdapter() {
Expand All @@ -52,26 +55,36 @@ public void keyPressed(KeyEvent e) {
}

// if key number k is pressed, select mode in k-th menu where k = 1, 2, ...

for (int i = 0; i < menuModels.length; i++) {
if (e.getKeyCode() == (KeyEvent.VK_1 + i))

model.setMode(menuModels[i].getActiveItem().getMode());
}
}
});
// Set initial frame state
if (model.isMaximizeFrame()) {
frame.setExtendedState(frame.getExtendedState() |
java.awt.Frame.MAXIMIZED_BOTH);
}
}

private void resizeEverything() {
int frameWidth = frame.getWidth();
int frameHeight = frame.getHeight();
System.out.format("Frame dimensions = %d x %d %n", frameWidth, frameHeight);

System.out.format("Frame dimensions = %d x %d %n", frameWidth,
frameHeight);
int menuBarHeight = frame.getMyMenuBar().getHeight();
System.out.format("Menubar dimensions = %d x %d %n", frameWidth, menuBarHeight);

System.out.format("Menubar dimensions = %d x %d %n", frameWidth,
menuBarHeight);
// update pane dimensions
JLayeredPane canvasScreen = frame.getCanvasScreen();
canvasScreen.setPreferredSize(new Dimension(frameWidth, frameHeight - menuBarHeight));
System.out.format("Pane dimensions = %d x %d %n", canvasScreen.getWidth(),
canvasScreen.setPreferredSize(new Dimension(frameWidth, frameHeight -
menuBarHeight));
System.out.format("Pane dimensions = %d x %d %n",
canvasScreen.getWidth(),

canvasScreen.getHeight());

Component canvasControl = canvasScreen.getComponent(0);
Expand All @@ -81,14 +94,16 @@ private void resizeEverything() {
canvasControl.setBounds(0, 0, frameWidth - 80,
frameHeight - menuBarHeight - 100);

// temporarily hide the canvas control. without this, the canvas console does
// not render its new size when frame is maximized.
// temporarily hide the canvas control. without this, the canvas console
// does not render its new size when frame is maximized.
canvasControl.setVisible(false);
canvasControl.setVisible(true);

// update sidebar dimensions
int sideBarWidth = Math.min(500, frameWidth / 3);
System.out.format("Sidebar dimensions = %d x %d %n", sideBarWidth, frameHeight - menuBarHeight);

System.out.format("Sidebar dimensions = %d x %d %n", sideBarWidth,
frameHeight - menuBarHeight);
frame.getCanvasConsole().getSidebar().setPreferredSize(new Dimension(sideBarWidth,
frameHeight - menuBarHeight));

Expand Down Expand Up @@ -136,6 +151,14 @@ public void propertyChange(PropertyChangeEvent e) {

resizeEverything();
}
}

}
if ("maximizeFrame".equals(property)) {
boolean maximizeFrame = (boolean) e.getNewValue();
if (maximizeFrame) {
frame.setExtendedState(frame.getExtendedState() | java.awt.Frame.MAXIMIZED_BOTH);
} else {
frame.setExtendedState(java.awt.Frame.NORMAL);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
package com.github.creme332.controller;

import com.github.creme332.model.AppState;
import com.github.creme332.model.CanvasModel;
import com.github.creme332.view.ZoomPanel;

/**
* Controller for ZoomPanel in CanvasConsole.
*/
public class ZoomPanelController {
CanvasModel model;
CanvasModel canvasModel;
ZoomPanel view;

public ZoomPanelController(CanvasModel model, ZoomPanel view) {
this.model = model;
public ZoomPanelController(AppState appState, ZoomPanel view) {
this.canvasModel = appState.getCanvasModel();
this.view = view;

// Add action listeners for the zoom panel buttons
view.getHomeButton().addActionListener(e -> model.resetZoom());
view.getZoomInButton().addActionListener(e -> model.updateCanvasZoom(true));
view.getZoomOutButton().addActionListener(e -> model.updateCanvasZoom(false));
view.getHomeButton().addActionListener(e -> canvasModel.resetZoom());
view.getZoomInButton().addActionListener(e -> canvasModel.updateCanvasZoom(true));
view.getZoomOutButton().addActionListener(e -> canvasModel.updateCanvasZoom(false));
view.getFullScreenButton().addActionListener(e -> appState.setMaximizeFrame(!appState.isMaximizeFrame()));
}
}
15 changes: 12 additions & 3 deletions src/main/java/com/github/creme332/model/AppState.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class AppState {
private Mode mode = Mode.MOVE_CANVAS;

private boolean visibleSidebar = false;

private boolean maximizeFrame = false;
private CanvasModel canvasModel = new CanvasModel();

TutorialScreenModel tutorialModel = new TutorialScreenModel();
Expand Down Expand Up @@ -141,8 +141,7 @@ public void addPropertyChangeListener(PropertyChangeListener listener) {
support.addPropertyChangeListener("sidebarVisibility", listener);
support.addPropertyChangeListener("mode", listener);
support.addPropertyChangeListener("screen", listener);
support.addPropertyChangeListener("printingCanvas", listener);
support.addPropertyChangeListener("activateToast", listener);
support.addPropertyChangeListener("maximizeFrame", listener);
}

public boolean getSideBarVisibility() {
Expand Down Expand Up @@ -172,6 +171,16 @@ public void setMode(Mode newMode) {
mode = newMode;
}

public boolean isMaximizeFrame() {
return maximizeFrame;
}

public void setMaximizeFrame(boolean maximizeFrame) {
support.firePropertyChange("maximizeFrame", this.maximizeFrame,
maximizeFrame);
this.maximizeFrame = maximizeFrame;
}

public void startPrintingProcess() {
support.firePropertyChange("printingCanvas", null, true);
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/github/creme332/view/MenuBar.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public MenuBar(MenuModel[] menus) throws InvalidIconSizeException, InvalidPathEx
guidelinesButton.setIcon(FontIcon.of(BootstrapIcons.GRID_3X3, 37));
guidelinesButton.setBorderPainted(false);
leftPanel.add(guidelinesButton);
guidelinesButton.setToolTipText("Guidelines");
guidelinesButton.setToolTipText("Toggle guidelines");

// toggle axes button
toggleAxesButton = new JButton();
Expand All @@ -77,7 +77,7 @@ public MenuBar(MenuModel[] menus) throws InvalidIconSizeException, InvalidPathEx
helpButton = new JButton();
helpButton.setIcon(FontIcon.of(BootstrapIcons.QUESTION_CIRCLE, 37));
helpButton.setBorderPainted(false);
helpButton.setToolTipText("Toggle axes");
helpButton.setToolTipText("Help");
leftPanel.add(helpButton);

// export button
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/com/github/creme332/view/ZoomPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public class ZoomPanel extends JPanel {
private JButton homeButton = new CircularButton();
private JButton zoomInButton = new CircularButton();
private JButton zoomOutButton = new CircularButton();
private JButton fullScreenButton = new CircularButton();

public ZoomPanel() {
setOpaque(false);
Expand All @@ -28,6 +29,7 @@ public ZoomPanel() {
homeButton = createZoomPanelButton(BootstrapIcons.HOUSE);
zoomInButton = createZoomPanelButton(BootstrapIcons.ZOOM_IN);
zoomOutButton = createZoomPanelButton(BootstrapIcons.ZOOM_OUT);
fullScreenButton = createZoomPanelButton(BootstrapIcons.ARROWS_FULLSCREEN);

// add tooltips to zoom panel buttons
homeButton.setToolTipText("Reset zoom");
Expand All @@ -49,6 +51,8 @@ public ZoomPanel() {
gbc.gridx = 0;
gbc.gridy = 2;
this.add(zoomOutButton, gbc);
gbc.gridy = 3;
this.add(fullScreenButton, gbc); // Add new button to panel
}

private JButton createZoomPanelButton(Ikon ikon) {
Expand Down Expand Up @@ -76,4 +80,8 @@ public JButton getZoomInButton() {
public JButton getZoomOutButton() {
return zoomOutButton;
}

public JButton getFullScreenButton() {
return fullScreenButton;
}
}

0 comments on commit 1ec32a9

Please sign in to comment.