Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Puzzle editor now has framework to add element buttons #146

Merged
merged 2 commits into from
Jun 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package edu.rpi.legup.controller;

import edu.rpi.legup.app.GameBoardFacade;
import edu.rpi.legup.app.LegupPreferences;
import edu.rpi.legup.history.*;
import edu.rpi.legup.model.Puzzle;
import edu.rpi.legup.model.elements.Element;
import edu.rpi.legup.model.gameboard.CaseBoard;
import edu.rpi.legup.model.rules.*;
import edu.rpi.legup.model.tree.TreeElement;
import edu.rpi.legup.model.tree.TreeElementType;
import edu.rpi.legup.ui.proofeditorui.rulesview.RuleButton;
import edu.rpi.legup.ui.proofeditorui.treeview.TreeElementView;
import edu.rpi.legup.ui.proofeditorui.treeview.TreePanel;
import edu.rpi.legup.ui.proofeditorui.treeview.TreeView;
import edu.rpi.legup.ui.proofeditorui.treeview.TreeViewSelection;
import edu.rpi.legup.ui.puzzleeditorui.elementsview.ElementButton;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import static edu.rpi.legup.app.GameBoardFacade.getInstance;

public class EditorElementController implements ActionListener {
protected Object lastSource;

public EditorElementController() {
super();
}

public void buttonPressed(Element element) {
// TODO: implement what happens when element is pressed
}

@Override
public void actionPerformed(ActionEvent e) {
lastSource = e.getSource();
ElementButton button = (ElementButton) lastSource;
buttonPressed(button.getElement());
}
}
77 changes: 77 additions & 0 deletions src/main/java/edu/rpi/legup/model/elements/Element.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package edu.rpi.legup.model.elements;

import edu.rpi.legup.model.rules.RuleType;

import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;

public abstract class Element {
protected String elementID;
protected String elementName;
protected String description;

protected String imageName;
protected ImageIcon image;

protected ElementType elementType;

private final String INVALID_USE_MESSAGE;

public Element(String elementID, String elementName, String description, String imageName) {
this.elementID = elementID;
this.elementName = elementName;
this.description = description;
this.imageName = imageName;
this.INVALID_USE_MESSAGE = "Invalid use of the rule " + this.elementName;
loadImage();
}

private void loadImage() {
if (imageName != null) {
this.image = new ImageIcon(ClassLoader.getSystemResource(imageName));
//Resize images to be 100px wide
Image image = this.image.getImage();
if(this.image.getIconWidth() < 120) return;
int height = (int) (100 * ((double) this.image.getIconHeight() / this.image.getIconWidth()));
if(height==0){
System.out.println("height is 0 error");
System.out.println("height: "+this.image.getIconHeight());
System.out.println("width: "+this.image.getIconWidth());
return;
}
BufferedImage bimage = new BufferedImage(100, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g = bimage.createGraphics();
g.drawImage(image, 0, 0, 100, height, null);
this.image = new ImageIcon(bimage);
}
}
public String getElementName() {
return elementName;
}

public void setElementName(String elementName) {
this.elementName = elementName;
}

public String getElementID() {
return elementID;
}

public String getDescription() {
return description;
}

public ImageIcon getImageIcon() {
return image;
}

public ElementType getElementType() {
return elementType;
}

public String getInvalidUseOfRuleMessage()
{
return this.INVALID_USE_MESSAGE;
}
}
5 changes: 5 additions & 0 deletions src/main/java/edu/rpi/legup/model/elements/ElementType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package edu.rpi.legup.model.elements;

public enum ElementType {
NURIKABE, TREETENT, LIGHTUP, SKYSCRAPERS
}
8 changes: 5 additions & 3 deletions src/main/java/edu/rpi/legup/ui/PuzzleEditorPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ public class PuzzleEditorPanel extends LegupPanel {
private JMenuBar menuBar;
private JFrame frame;
private JButton[] buttons;

private JPanel elementPanel;
private JPanel mainPanel;
private FileDialog fileDialog;
private JMenuItem undo, redo;
Expand Down Expand Up @@ -57,11 +59,11 @@ public void setMenuBar() {
newPuzzle.addActionListener((ActionEvent) -> promptPuzzle());
if(os.equals("mac")) newPuzzle.setAccelerator(KeyStroke.getKeyStroke('N', Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()));
else newPuzzle.setAccelerator(KeyStroke.getKeyStroke('N', InputEvent.CTRL_DOWN_MASK));
// file>open
JMenuItem openPuzzle = new JMenuItem("Open");
// file>save
JMenuItem savePuzzle = new JMenuItem("Save");

menus[0].add(newPuzzle);
menus[0].add(openPuzzle);
menus[0].add(savePuzzle);

// EDIT
menus[1] = new JMenu("Edit");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package edu.rpi.legup.ui.puzzleeditorui.elementsview;

import edu.rpi.legup.model.elements.Element;
import edu.rpi.legup.model.rules.Rule;

import javax.swing.*;

public class ElementButton extends JButton {

private Element element;
ElementButton(Element e) {
super(e.getImageIcon());
this.element = e;
}

public Element getElement() {
return element;
}

public void setElement(Element e) {
this.element = e;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package edu.rpi.legup.ui.puzzleeditorui.elementsview;

import edu.rpi.legup.controller.EditorElementController;
import edu.rpi.legup.controller.RuleController;
import edu.rpi.legup.ui.proofeditorui.rulesview.BasicRulePanel;
import edu.rpi.legup.ui.proofeditorui.rulesview.CaseRulePanel;
import edu.rpi.legup.ui.proofeditorui.rulesview.ContradictionRulePanel;
import edu.rpi.legup.ui.proofeditorui.rulesview.RulePanel;

import javax.swing.*;
import javax.swing.border.TitledBorder;
import java.awt.*;

public class ElementFrame extends JPanel {
private static final String checkBox = "<font style=\"color:#00CD00\"> \u2714 </font>";
private static final String xBox = "<font style=\"color:#FF0000\"> \u2718 </font>";
private static final String htmlHead = "<html>";
private static final String htmlTail = "</html>";

private JTabbedPane tabbedPane;
private JLabel status;
private ButtonGroup buttonGroup;

private EditorElementController controller;

public ElementFrame(EditorElementController controller) {
this.controller = controller;

this.tabbedPane = new JTabbedPane();
this.status = new JLabel();
this.buttonGroup = new ButtonGroup();

setLayout(new BorderLayout());
setMinimumSize(new Dimension(250, 256));
setPreferredSize(new Dimension(330, 256));

add(tabbedPane);
add(status, BorderLayout.SOUTH);

TitledBorder title = BorderFactory.createTitledBorder("Rules");
title.setTitleJustification(TitledBorder.CENTER);
setBorder(title);
}

public ButtonGroup getButtonGroup() {
return buttonGroup;
}

public void resetSize() {
int buttonWidth = ((ElementPanel) tabbedPane.getSelectedComponent()).getElementButtons()[0].getWidth();
this.setMinimumSize(new Dimension(2 * buttonWidth + 64, this.getHeight()));
}

public EditorElementController getController() {
return controller;
}

public JTabbedPane getTabbedPane() {
return tabbedPane;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,82 @@
package edu.rpi.legup.ui.puzzleeditorui.elementsview;

public class ElementPanel {
import edu.rpi.legup.model.rules.Rule;
import edu.rpi.legup.ui.WrapLayout;
import edu.rpi.legup.model.elements.Element;
import edu.rpi.legup.ui.proofeditorui.rulesview.RuleButton;

import javax.swing.*;
import java.util.ArrayList;
import java.util.List;

public abstract class ElementPanel extends JPanel {
protected ImageIcon icon;
protected String name;
protected String toolTip;
protected ElementButton[] elementButtons;
protected ElementFrame elementFrame;
protected List<? extends Element> elements;

public ElementPanel(ElementFrame eFrame) {
this.elementFrame = eFrame;
this.elements = new ArrayList<>();
setLayout(new WrapLayout());
}
public void setRules(List<? extends Element> elements) {
this.elements = elements;
clearButtons();

elementButtons = new ElementButton[elements.size()];

for (int i = 0; i < elements.size(); i++) {
Element element = elements.get(i);
elementButtons[i] = new ElementButton(element);
elementFrame.getButtonGroup().add(elementButtons[i]);

elementButtons[i].setToolTipText(element.getElementName() + ": " + element.getDescription());
elementButtons[i].addActionListener(elementFrame.getController());
add(elementButtons[i]);
}
revalidate();
}

protected void clearButtons() {
if (elementButtons != null) {
removeAll();
for (int x = 0; x < elementButtons.length; ++x) {
elementButtons[x].removeActionListener(elementFrame.getController());
}
}
}

public ElementButton[] getElementButtons() {
return elementButtons;
}

public ImageIcon getIcon() {
return icon;
}

public void setIcon(ImageIcon icon) {
this.icon = icon;
}

@Override
public String getName() {
return name;
}

@Override
public void setName(String name) {
this.name = name;
}

public String getToolTip() {
return toolTip;
}

public void setToolTip(String toolTip) {
this.toolTip = toolTip;
}
}