added a lolcat mode

This commit is contained in:
Bjorn Pijnacker 2021-02-23 13:20:29 +01:00
parent 1cb513cb51
commit c8b114195d
No known key found for this signature in database
GPG Key ID: 68CC60CD9AC50D72
5 changed files with 146 additions and 81 deletions

View File

@ -8,6 +8,11 @@ import java.nio.charset.StandardCharsets;
public class EntityRelationMappingEditor {
public static final String VERSION = "1.3.1";
private static EditorFrame frame;
public static EditorFrame getFrame() {
return frame;
}
public static void main(String[] args) {
if (!FlatLightLaf.install()) {
@ -17,7 +22,7 @@ public class EntityRelationMappingEditor {
if (includeAdminActions) {
System.out.println("Admin actions have been enabled.");
}
final EditorFrame frame = new EditorFrame(includeAdminActions);
frame = new EditorFrame(includeAdminActions);
frame.setVisible(true);
}

View File

@ -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 = AttributeViewModel.getLolcatMode(); // save previous lolcat mode
AttributeViewModel.setLolcatMode(false);
List<Relation> 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)));
AttributeViewModel.setLolcatMode(lolcat); // revert previous lolcat mode
// Revert back to the normal image space, and render a watermark.
g2d.setTransform(originalTransform);

View File

@ -0,0 +1,27 @@
package nl.andrewlalis.erme.control.actions;
import nl.andrewlalis.erme.EntityRelationMappingEditor;
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;
public static LolcatAction getInstance() {
if (instance == null) {
instance = new LolcatAction();
}
return instance;
}
public LolcatAction() {
super("Toggle Lolcat Mode");
}
@Override
public void actionPerformed(ActionEvent actionEvent) {
AttributeViewModel.setLolcatMode(((AbstractButton)actionEvent.getSource()).getModel().isSelected());
EntityRelationMappingEditor.getFrame().getContentPane().repaint();
}
}

View File

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

View File

@ -1,8 +1,10 @@
package nl.andrewlalis.erme.view.view_models;
import nl.andrewlalis.erme.EntityRelationMappingEditor;
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,18 +20,28 @@ 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 static boolean lolcatMode;
private final Attribute attribute;
public AttributeViewModel(Attribute attribute) {
this.attribute = attribute;
}
public static boolean getLolcatMode() {
return lolcatMode;
}
public static void setLolcatMode(boolean lolcatMode) {
AttributeViewModel.lolcatMode = lolcatMode;
}
@Override
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 +55,22 @@ public class AttributeViewModel implements ViewModel {
}
}
private Color getBackgroundColor(int x, int y, Graphics2D g) {
if (!lolcatMode) return BACKGROUND_COLOR;
Dimension viewportSize = ((DiagramPanel)EntityRelationMappingEditor.getFrame().getContentPane()).getModel().getRelationBounds().getSize();
double dx = viewportSize.width;
double dy = viewportSize.height;
double mag = Math.sqrt(dx * dx + dy * dy);
dx /= mag;
dy /= mag;
double lambda = (dx * x + dy * y);
double diag_val = Math.sqrt(Math.pow(dx * lambda, 2) + Math.pow(dy * lambda, 2)) / mag;
return Color.getHSBColor((float) diag_val, LOLCAT_SAT, LOLCAT_BRIGHT);
}
@Override
public Rectangle getBounds(Graphics2D g) {
return this.getBounds(g, this.getAttributedString(g));