Skip to content

Commit

Permalink
Scale the clipart when resizing the dialog (#2538)
Browse files Browse the repository at this point in the history
  • Loading branch information
breiler authored Jun 8, 2024
1 parent 99ae4ef commit 794f0d8
Show file tree
Hide file tree
Showing 37 changed files with 187 additions and 170 deletions.
1 change: 1 addition & 0 deletions ugs-core/src/resources/MessagesBundle_en_US.properties
Original file line number Diff line number Diff line change
Expand Up @@ -726,6 +726,7 @@ platform.plugin.toolbox.title=Toolbox
platform.plugin.toolbox.tooltip=Toolbox
platform.plugin.toolbox.settings.title=Edit toolbox buttons...
platform.plugin.editor.showOnOpen=Show editor when opening g-code files
platform.plugin.designer.clipart.all=All
platform.plugin.designer.clipart.animals=Animals
platform.plugin.designer.clipart.buildings=Buildings
platform.plugin.designer.clipart.computer=Computer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ This file is part of Universal Gcode Sender (UGS).
* @author Joacim Breiler
*/
public enum Category {
ALL("platform.plugin.designer.clipart.all"),
ANIMALS("platform.plugin.designer.clipart.animals"),
BUILDINGS("platform.plugin.designer.clipart.buildings"),
DECORATIONS("platform.plugin.designer.clipart.decorations"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,14 @@ This file is part of Universal Gcode Sender (UGS).
* @author Joacim Breiler
*/
public class ClipartButton extends RoundedPanel {
private final Clipart clipart;
private final transient Clipart clipart;

public ClipartButton(Clipart clipart, ClipartTooltip tooltip) {
super(12);
this.clipart = clipart;
setLayout(new MigLayout("fill, inset 0"));
setMinimumSize(new Dimension(128, 128));
setMaximumSize(new Dimension(512, 512));
setForeground(ThemeColors.LIGHT_GREY);
setBackground(Color.WHITE);
setHoverBackground(ThemeColors.LIGHT_GREY);
Expand All @@ -49,4 +50,11 @@ public ClipartButton(Clipart clipart, ClipartTooltip tooltip) {
public Clipart getClipart() {
return clipart;
}

@Override
public Dimension getPreferredSize() {
Dimension d = super.getSize();
int width = (int) Math.round(d.getWidth());
return new Dimension(width, width); // Make the buttion square
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.willwinder.ugs.nbp.designer.gui.clipart;

import javax.swing.JLabel;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.font.FontRenderContext;
import java.awt.geom.AffineTransform;
import java.awt.geom.Rectangle2D;

public class ClipartLabel extends JLabel {
public ClipartLabel(String text) {
super(text);
AffineTransform affinetransform = new AffineTransform();
FontRenderContext frc = new FontRenderContext(affinetransform, true, true);

addComponentListener(new ComponentAdapter() {
@Override
public void componentResized(ComponentEvent e) {
Rectangle2D stringBounds = getFont().getStringBounds(getText(), frc);
int currentSize = (int) Math.round(Math.max(stringBounds.getWidth(), stringBounds.getHeight()));
float sizeChange = (float) Math.min(getWidth(), getHeight()) / (float) currentSize;
if (sizeChange < 1f || sizeChange > 1.1f) {
int newFontSize = Math.min(Math.round((getFont().getSize() * sizeChange)), getHeight());
setFont(getFont().deriveFont((float) newFontSize));
}
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public interface ClipartSource {

String getUrl();

List<Clipart> getCliparts(Category category);
List<? extends Clipart> getCliparts(Category category);

String getLicense();
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,12 @@ This file is part of Universal Gcode Sender (UGS).
import com.willwinder.ugs.nbp.designer.entities.cuttable.Path;
import com.willwinder.universalgcodesender.uielements.helpers.ThemeColors;

import javax.swing.*;
import java.awt.*;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import java.awt.Component;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.font.GlyphVector;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
Expand All @@ -48,7 +52,7 @@ public FontClipart(String name, Category category, Font font, String text, Clipa
this.font = font;
this.source = source;

label = new JLabel(text);
label = new ClipartLabel(text);
label.setFont(font);
label.setHorizontalAlignment(SwingConstants.CENTER);
label.setForeground(ThemeColors.VERY_DARK_GREY);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public void setCategory(Category category) {
selectedClipart = clipart;
selectAction.actionPerformed(new ActionEvent(roundedPanel, 0, "selected_clipart"));
});
buttonsPanel.add(roundedPanel);
buttonsPanel.add(roundedPanel, "grow, w 100:100:400");
});
buttonsPanel.revalidate();
buttonsPanel.repaint();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.willwinder.ugs.nbp.designer.gui.clipart.sources;

import com.willwinder.ugs.nbp.designer.gui.clipart.Category;
import com.willwinder.ugs.nbp.designer.gui.clipart.Clipart;
import com.willwinder.ugs.nbp.designer.gui.clipart.ClipartSource;

import java.util.List;

public abstract class AbstractClipartSource implements ClipartSource {
public abstract List<? extends Clipart> getCliparts();

@Override
public List<? extends Clipart> getCliparts(Category category) {
return getCliparts()
.stream()
.filter(clipart -> clipart.getCategory() == category || category == Category.ALL)
.toList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ This file is part of Universal Gcode Sender (UGS).

import com.willwinder.ugs.nbp.designer.gui.clipart.Category;
import com.willwinder.ugs.nbp.designer.gui.clipart.Clipart;
import com.willwinder.ugs.nbp.designer.gui.clipart.ClipartSource;
import com.willwinder.ugs.nbp.designer.gui.clipart.ClipartSourceException;
import com.willwinder.ugs.nbp.designer.gui.clipart.FontClipart;

Expand All @@ -29,12 +28,11 @@ This file is part of Universal Gcode Sender (UGS).
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author Joacim Breiler
*/
public class BuDingbatsSource implements ClipartSource {
public class BuDingbatsSource extends AbstractClipartSource {
private final List<FontClipart> cliparts = new ArrayList<>();

public BuDingbatsSource() {
Expand Down Expand Up @@ -117,8 +115,8 @@ public String getUrl() {
}

@Override
public List<Clipart> getCliparts(Category category) {
return cliparts.stream().filter(clipart -> clipart.getCategory() == category).collect(Collectors.toList());
public List<? extends Clipart> getCliparts() {
return cliparts;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,19 @@ This file is part of Universal Gcode Sender (UGS).

import com.willwinder.ugs.nbp.designer.gui.clipart.Category;
import com.willwinder.ugs.nbp.designer.gui.clipart.Clipart;
import com.willwinder.ugs.nbp.designer.gui.clipart.ClipartSource;
import com.willwinder.ugs.nbp.designer.gui.clipart.ClipartSourceException;
import com.willwinder.ugs.nbp.designer.gui.clipart.FontClipart;

import java.awt.*;
import java.awt.Font;
import java.awt.FontFormatException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author Joacim Breiler
*/
public class ChristmasSource implements ClipartSource {
public class ChristmasSource extends AbstractClipartSource {
private final List<FontClipart> cliparts = new ArrayList<>();

public ChristmasSource() {
Expand Down Expand Up @@ -153,11 +152,9 @@ public String getUrl() {
return "https://www.fontspace.com/christmas-font-f4808";
}



@Override
public List<Clipart> getCliparts(Category category) {
return cliparts.stream().filter(clipart -> clipart.getCategory() == category).collect(Collectors.toList());
public List<? extends Clipart> getCliparts() {
return cliparts;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ This file is part of Universal Gcode Sender (UGS).

import com.willwinder.ugs.nbp.designer.gui.clipart.Category;
import com.willwinder.ugs.nbp.designer.gui.clipart.Clipart;
import com.willwinder.ugs.nbp.designer.gui.clipart.ClipartSource;
import com.willwinder.ugs.nbp.designer.gui.clipart.ClipartSourceException;
import com.willwinder.ugs.nbp.designer.gui.clipart.FontClipart;

Expand All @@ -29,12 +28,11 @@ This file is part of Universal Gcode Sender (UGS).
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author Joacim Breiler
*/
public class Corners2Source implements ClipartSource {
public class Corners2Source extends AbstractClipartSource {

private final List<FontClipart> cliparts = new ArrayList<>();

Expand All @@ -55,7 +53,7 @@ public Corners2Source() {
cliparts.add(new FontClipart("Corner with star and leaves", Category.DECORATIONS, font, "E", this));
cliparts.add(new FontClipart("Corner with eagle", Category.DECORATIONS, font, "F", this));
cliparts.add(new FontClipart("Corner with bells", Category.DECORATIONS, font, "G", this));
cliparts.add(new FontClipart("Banner with heart and petals", Category.DECORATIONS, font.deriveFont(font.getSize() * 0.8f), "H", this));
cliparts.add(new FontClipart("Banner with heart and petals", Category.DECORATIONS, font, "H", this));
cliparts.add(new FontClipart("Banner", Category.DECORATIONS, font, "I", this));
cliparts.add(new FontClipart("Corner with flowers 2", Category.DECORATIONS, font, "J", this));
cliparts.add(new FontClipart("Corner with fairy", Category.DECORATIONS, font, "K", this));
Expand All @@ -78,8 +76,8 @@ public String getUrl() {
}

@Override
public List<Clipart> getCliparts(Category category) {
return cliparts.stream().filter(clipart -> clipart.getCategory() == category).collect(Collectors.toList());
public List<? extends Clipart> getCliparts() {
return cliparts;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ This file is part of Universal Gcode Sender (UGS).

import com.willwinder.ugs.nbp.designer.gui.clipart.Category;
import com.willwinder.ugs.nbp.designer.gui.clipart.Clipart;
import com.willwinder.ugs.nbp.designer.gui.clipart.ClipartSource;
import com.willwinder.ugs.nbp.designer.gui.clipart.ClipartSourceException;
import com.willwinder.ugs.nbp.designer.gui.clipart.FontClipart;

Expand All @@ -29,12 +28,11 @@ This file is part of Universal Gcode Sender (UGS).
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author Joacim Breiler
*/
public class CreepyCrawliesSource implements ClipartSource {
public class CreepyCrawliesSource extends AbstractClipartSource {
private final List<FontClipart> cliparts = new ArrayList<>();

public CreepyCrawliesSource() {
Expand Down Expand Up @@ -103,8 +101,8 @@ public String getUrl() {
}

@Override
public List<Clipart> getCliparts(Category category) {
return cliparts.stream().filter(clipart -> clipart.getCategory() == category).collect(Collectors.toList());
public List<? extends Clipart> getCliparts() {
return cliparts;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ This file is part of Universal Gcode Sender (UGS).

import com.willwinder.ugs.nbp.designer.gui.clipart.Category;
import com.willwinder.ugs.nbp.designer.gui.clipart.Clipart;
import com.willwinder.ugs.nbp.designer.gui.clipart.ClipartSource;
import com.willwinder.ugs.nbp.designer.gui.clipart.ClipartSourceException;
import com.willwinder.ugs.nbp.designer.gui.clipart.FontClipart;

Expand All @@ -29,12 +28,11 @@ This file is part of Universal Gcode Sender (UGS).
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author Joacim Breiler
*/
public class DarriansFrames1Source implements ClipartSource {
public class DarriansFrames1Source extends AbstractClipartSource {
private final List<FontClipart> cliparts = new ArrayList<>();

public DarriansFrames1Source() {
Expand Down Expand Up @@ -91,8 +89,8 @@ public String getUrl() {
}

@Override
public List<Clipart> getCliparts(Category category) {
return cliparts.stream().filter(clipart -> clipart.getCategory() == category).collect(Collectors.toList());
public List<? extends Clipart> getCliparts() {
return cliparts;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ This file is part of Universal Gcode Sender (UGS).

import com.willwinder.ugs.nbp.designer.gui.clipart.Category;
import com.willwinder.ugs.nbp.designer.gui.clipart.Clipart;
import com.willwinder.ugs.nbp.designer.gui.clipart.ClipartSource;
import com.willwinder.ugs.nbp.designer.gui.clipart.ClipartSourceException;
import com.willwinder.ugs.nbp.designer.gui.clipart.FontClipart;

Expand All @@ -29,12 +28,11 @@ This file is part of Universal Gcode Sender (UGS).
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author Joacim Breiler
*/
public class DarriansFrames2Source implements ClipartSource {
public class DarriansFrames2Source extends AbstractClipartSource {
private final List<FontClipart> cliparts = new ArrayList<>();

public DarriansFrames2Source() {
Expand Down Expand Up @@ -91,8 +89,8 @@ public String getUrl() {
}

@Override
public List<Clipart> getCliparts(Category category) {
return cliparts.stream().filter(clipart -> clipart.getCategory() == category).collect(Collectors.toList());
public List<? extends Clipart> getCliparts() {
return cliparts;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,19 @@ This file is part of Universal Gcode Sender (UGS).

import com.willwinder.ugs.nbp.designer.gui.clipart.Category;
import com.willwinder.ugs.nbp.designer.gui.clipart.Clipart;
import com.willwinder.ugs.nbp.designer.gui.clipart.ClipartSource;
import com.willwinder.ugs.nbp.designer.gui.clipart.ClipartSourceException;
import com.willwinder.ugs.nbp.designer.gui.clipart.FontClipart;

import java.awt.*;
import java.awt.Font;
import java.awt.FontFormatException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author Joacim Breiler
*/
public class DestinysBordersSource implements ClipartSource {
public class DestinysBordersSource extends AbstractClipartSource {

private final List<FontClipart> cliparts = new ArrayList<>();

Expand Down Expand Up @@ -75,8 +74,8 @@ public String getUrl() {
}

@Override
public List<Clipart> getCliparts(Category category) {
return cliparts.stream().filter(clipart -> clipart.getCategory() == category).collect(Collectors.toList());
public List<? extends Clipart> getCliparts() {
return cliparts;
}

@Override
Expand Down
Loading

0 comments on commit 794f0d8

Please sign in to comment.