Merge pull request #7 from bjornpijnacker/main

Implement a colorful lolcat mode
This commit is contained in:
Andrew Lalis 2021-02-23 19:25:32 +01:00 committed by GitHub
commit a0eb582f69
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 75 additions and 3 deletions

View File

@ -17,7 +17,7 @@ public class EntityRelationMappingEditor {
if (includeAdminActions) { if (includeAdminActions) {
System.out.println("Admin actions have been enabled."); System.out.println("Admin actions have been enabled.");
} }
final EditorFrame frame = new EditorFrame(includeAdminActions); EditorFrame frame = new EditorFrame(includeAdminActions);
frame.setVisible(true); frame.setVisible(true);
} }

View File

@ -4,6 +4,7 @@ import lombok.Setter;
import nl.andrewlalis.erme.model.MappingModel; import nl.andrewlalis.erme.model.MappingModel;
import nl.andrewlalis.erme.model.Relation; import nl.andrewlalis.erme.model.Relation;
import nl.andrewlalis.erme.view.DiagramPanel; import nl.andrewlalis.erme.view.DiagramPanel;
import nl.andrewlalis.erme.view.view_models.AttributeViewModel;
import nl.andrewlalis.erme.view.view_models.MappingModelViewModel; import nl.andrewlalis.erme.view.view_models.MappingModelViewModel;
import javax.imageio.ImageIO; import javax.imageio.ImageIO;
@ -113,10 +114,13 @@ public class ExportToImageAction extends AbstractAction {
DiagramPanel.prepareGraphics(g2d); DiagramPanel.prepareGraphics(g2d);
// Render the model. // Render the model.
boolean lolcat = LolcatAction.getInstance().isLolcatEnabled(); // save previous lolcat mode
LolcatAction.getInstance().setLolcatEnabled(false);
List<Relation> selectedRelations = this.model.getSelectedRelations(); List<Relation> selectedRelations = this.model.getSelectedRelations();
this.model.getSelectedRelations().forEach(r -> r.setSelected(false)); this.model.getSelectedRelations().forEach(r -> r.setSelected(false));
new MappingModelViewModel(this.model).draw(g2d); new MappingModelViewModel(this.model).draw(g2d);
this.model.getRelations().forEach(r -> r.setSelected(selectedRelations.contains(r))); 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. // Revert back to the normal image space, and render a watermark.
g2d.setTransform(originalTransform); g2d.setTransform(originalTransform);

View File

@ -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();
}
}

View File

@ -128,6 +128,7 @@ public class DiagramPanel extends JPanel implements ModelChangeListener {
AddAttributeAction.getInstance().setModel(this.model); AddAttributeAction.getInstance().setModel(this.model);
RemoveAttributeAction.getInstance().setModel(this.model); RemoveAttributeAction.getInstance().setModel(this.model);
LoadSampleModelAction.getInstance().setDiagramPanel(this); LoadSampleModelAction.getInstance().setDiagramPanel(this);
LolcatAction.getInstance().setDiagramPanel(this);
} }
public static void prepareGraphics(Graphics2D g) { public static void prepareGraphics(Graphics2D g) {

View File

@ -39,6 +39,7 @@ public class EditorMenuBar extends JMenuBar {
menu.add(RemoveRelationAction.getInstance()); menu.add(RemoveRelationAction.getInstance());
menu.add(AddAttributeAction.getInstance()); menu.add(AddAttributeAction.getInstance());
menu.add(RemoveAttributeAction.getInstance()); menu.add(RemoveAttributeAction.getInstance());
menu.add(new JCheckBoxMenuItem(LolcatAction.getInstance()));
menu.addSeparator(); menu.addSeparator();
menu.add(UndoAction.getInstance()); menu.add(UndoAction.getInstance());
menu.add(RedoAction.getInstance()); menu.add(RedoAction.getInstance());

View File

@ -1,8 +1,11 @@
package nl.andrewlalis.erme.view.view_models; 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.Attribute;
import nl.andrewlalis.erme.model.AttributeType; import nl.andrewlalis.erme.model.AttributeType;
import nl.andrewlalis.erme.model.ForeignKeyAttribute; import nl.andrewlalis.erme.model.ForeignKeyAttribute;
import nl.andrewlalis.erme.view.DiagramPanel;
import java.awt.*; import java.awt.*;
import java.awt.font.TextAttribute; 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 BACKGROUND_COLOR = Color.LIGHT_GRAY;
public static final Color FONT_COLOR = Color.BLACK; public static final Color FONT_COLOR = Color.BLACK;
public static final float FK_FONT_SIZE = 11.0f; 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; private final Attribute attribute;
public AttributeViewModel(Attribute attribute) { public AttributeViewModel(Attribute attribute) {
@ -29,7 +33,7 @@ public class AttributeViewModel implements ViewModel {
public void draw(Graphics2D g) { public void draw(Graphics2D g) {
AttributedString as = this.getAttributedString(g); AttributedString as = this.getAttributedString(g);
Rectangle r = this.getBounds(g, as); 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.fillRect(r.x, r.y, r.width, r.height);
g.setColor(FONT_COLOR); g.setColor(FONT_COLOR);
g.drawRect(r.x, r.y, r.width, r.height); 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 @Override
public Rectangle getBounds(Graphics2D g) { public Rectangle getBounds(Graphics2D g) {
return this.getBounds(g, this.getAttributedString(g)); return this.getBounds(g, this.getAttributedString(g));