diff --git a/src/main/java/nl/andrewlalis/erme/EntityRelationMappingEditor.java b/src/main/java/nl/andrewlalis/erme/EntityRelationMappingEditor.java index 6099536..b9cafa6 100644 --- a/src/main/java/nl/andrewlalis/erme/EntityRelationMappingEditor.java +++ b/src/main/java/nl/andrewlalis/erme/EntityRelationMappingEditor.java @@ -17,7 +17,7 @@ public class EntityRelationMappingEditor { if (includeAdminActions) { System.out.println("Admin actions have been enabled."); } - final EditorFrame frame = new EditorFrame(includeAdminActions); + EditorFrame frame = new EditorFrame(includeAdminActions); frame.setVisible(true); } diff --git a/src/main/java/nl/andrewlalis/erme/control/actions/ExportToImageAction.java b/src/main/java/nl/andrewlalis/erme/control/actions/ExportToImageAction.java index 96c6fba..a040a3d 100644 --- a/src/main/java/nl/andrewlalis/erme/control/actions/ExportToImageAction.java +++ b/src/main/java/nl/andrewlalis/erme/control/actions/ExportToImageAction.java @@ -4,6 +4,7 @@ import lombok.Setter; import nl.andrewlalis.erme.model.MappingModel; import nl.andrewlalis.erme.model.Relation; import nl.andrewlalis.erme.view.DiagramPanel; +import nl.andrewlalis.erme.view.view_models.AttributeViewModel; import nl.andrewlalis.erme.view.view_models.MappingModelViewModel; import javax.imageio.ImageIO; @@ -113,10 +114,13 @@ public class ExportToImageAction extends AbstractAction { DiagramPanel.prepareGraphics(g2d); // Render the model. + boolean lolcat = LolcatAction.getInstance().isLolcatEnabled(); // save previous lolcat mode + LolcatAction.getInstance().setLolcatEnabled(false); List selectedRelations = this.model.getSelectedRelations(); this.model.getSelectedRelations().forEach(r -> r.setSelected(false)); new MappingModelViewModel(this.model).draw(g2d); this.model.getRelations().forEach(r -> r.setSelected(selectedRelations.contains(r))); + LolcatAction.getInstance().setLolcatEnabled(lolcat); // revert previous lolcat mode // Revert back to the normal image space, and render a watermark. g2d.setTransform(originalTransform); diff --git a/src/main/java/nl/andrewlalis/erme/control/actions/LolcatAction.java b/src/main/java/nl/andrewlalis/erme/control/actions/LolcatAction.java new file mode 100644 index 0000000..f2ffc74 --- /dev/null +++ b/src/main/java/nl/andrewlalis/erme/control/actions/LolcatAction.java @@ -0,0 +1,39 @@ +package nl.andrewlalis.erme.control.actions; + +import lombok.Getter; +import lombok.Setter; +import nl.andrewlalis.erme.EntityRelationMappingEditor; +import nl.andrewlalis.erme.view.DiagramPanel; +import nl.andrewlalis.erme.view.view_models.AttributeViewModel; + +import javax.swing.*; +import java.awt.event.ActionEvent; + +public class LolcatAction extends AbstractAction { + private static LolcatAction instance; + + @Getter + @Setter + private boolean lolcatEnabled = false; + + public static LolcatAction getInstance() { + if (instance == null) { + instance = new LolcatAction(); + } + return instance; + } + + @Setter + private DiagramPanel diagramPanel; + + public LolcatAction() { + super("Toggle Lolcat Mode"); + this.putValue(SHORT_DESCRIPTION, "Does some wacky color stuff."); + } + + @Override + public void actionPerformed(ActionEvent actionEvent) { + lolcatEnabled = ((AbstractButton)actionEvent.getSource()).getModel().isSelected(); + diagramPanel.repaint(); + } +} diff --git a/src/main/java/nl/andrewlalis/erme/view/DiagramPanel.java b/src/main/java/nl/andrewlalis/erme/view/DiagramPanel.java index 8210b1e..54bc65e 100644 --- a/src/main/java/nl/andrewlalis/erme/view/DiagramPanel.java +++ b/src/main/java/nl/andrewlalis/erme/view/DiagramPanel.java @@ -128,6 +128,7 @@ public class DiagramPanel extends JPanel implements ModelChangeListener { AddAttributeAction.getInstance().setModel(this.model); RemoveAttributeAction.getInstance().setModel(this.model); LoadSampleModelAction.getInstance().setDiagramPanel(this); + LolcatAction.getInstance().setDiagramPanel(this); } public static void prepareGraphics(Graphics2D g) { diff --git a/src/main/java/nl/andrewlalis/erme/view/EditorMenuBar.java b/src/main/java/nl/andrewlalis/erme/view/EditorMenuBar.java index f2a5770..8710c3d 100644 --- a/src/main/java/nl/andrewlalis/erme/view/EditorMenuBar.java +++ b/src/main/java/nl/andrewlalis/erme/view/EditorMenuBar.java @@ -39,6 +39,7 @@ public class EditorMenuBar extends JMenuBar { menu.add(RemoveRelationAction.getInstance()); menu.add(AddAttributeAction.getInstance()); menu.add(RemoveAttributeAction.getInstance()); + menu.add(new JCheckBoxMenuItem(LolcatAction.getInstance())); menu.addSeparator(); menu.add(UndoAction.getInstance()); menu.add(RedoAction.getInstance()); diff --git a/src/main/java/nl/andrewlalis/erme/view/view_models/AttributeViewModel.java b/src/main/java/nl/andrewlalis/erme/view/view_models/AttributeViewModel.java index 6612554..48617ed 100644 --- a/src/main/java/nl/andrewlalis/erme/view/view_models/AttributeViewModel.java +++ b/src/main/java/nl/andrewlalis/erme/view/view_models/AttributeViewModel.java @@ -1,8 +1,11 @@ package nl.andrewlalis.erme.view.view_models; +import nl.andrewlalis.erme.EntityRelationMappingEditor; +import nl.andrewlalis.erme.control.actions.LolcatAction; import nl.andrewlalis.erme.model.Attribute; import nl.andrewlalis.erme.model.AttributeType; import nl.andrewlalis.erme.model.ForeignKeyAttribute; +import nl.andrewlalis.erme.view.DiagramPanel; import java.awt.*; import java.awt.font.TextAttribute; @@ -18,7 +21,8 @@ public class AttributeViewModel implements ViewModel { public static final Color BACKGROUND_COLOR = Color.LIGHT_GRAY; public static final Color FONT_COLOR = Color.BLACK; public static final float FK_FONT_SIZE = 11.0f; - + private static final float LOLCAT_SAT = 0.75f; + private static final float LOLCAT_BRIGHT = 1f; private final Attribute attribute; public AttributeViewModel(Attribute attribute) { @@ -29,7 +33,7 @@ public class AttributeViewModel implements ViewModel { public void draw(Graphics2D g) { AttributedString as = this.getAttributedString(g); Rectangle r = this.getBounds(g, as); - g.setColor(BACKGROUND_COLOR); + g.setColor(this.getBackgroundColor(r.x + r.width / 2, r.y + r.height / 2, g)); g.fillRect(r.x, r.y, r.width, r.height); g.setColor(FONT_COLOR); g.drawRect(r.x, r.y, r.width, r.height); @@ -43,6 +47,29 @@ public class AttributeViewModel implements ViewModel { } } + private Color getBackgroundColor(int x, int y, Graphics2D g) { + if (!LolcatAction.getInstance().isLolcatEnabled()) return BACKGROUND_COLOR; + Point offset = g.getClipBounds().getLocation(); + g.translate(offset.x, offset.y); + Dimension viewportSize = g.getClipBounds().getSize(); + g.drawRect(0, 0, viewportSize.width, viewportSize.height); + g.fillRect(-5, -5, 10, 10); + + double diagonal_slope = (double) viewportSize.width / (double) viewportSize.height; + double perp_slope = -1f / diagonal_slope; + + double perp_offset = y - perp_slope * x; + + double x_intersect = perp_offset / (diagonal_slope - perp_slope); + double y_intersect = diagonal_slope * (perp_offset / (diagonal_slope - perp_slope)); + + double total_dist = Math.sqrt(viewportSize.height * viewportSize.height + viewportSize.width * viewportSize.width); + double dist_frac = Math.sqrt(x_intersect * x_intersect + y_intersect * y_intersect) / total_dist; + + g.translate(-offset.x, -offset.y); + return Color.getHSBColor((float) dist_frac, LOLCAT_SAT, LOLCAT_BRIGHT); + } + @Override public Rectangle getBounds(Graphics2D g) { return this.getBounds(g, this.getAttributedString(g));