Skip to content

Commit

Permalink
Moved clipart config to JSON files (#2541)
Browse files Browse the repository at this point in the history
  • Loading branch information
breiler authored Jun 9, 2024
1 parent 794f0d8 commit bf72cd1
Show file tree
Hide file tree
Showing 63 changed files with 8,997 additions and 3,453 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
Copyright 2024 Will Winder
This file is part of Universal Gcode Sender (UGS).
UGS is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
UGS is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with UGS. If not, see <http://www.gnu.org/licenses/>.
*/
package com.willwinder.ugs.nbp.designer.gui.clipart;

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
@@ -0,0 +1,89 @@
/*
Copyright 2022-2024 Will Winder
This file is part of Universal Gcode Sender (UGS).
UGS is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
UGS is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with UGS. If not, see <http://www.gnu.org/licenses/>.
*/
package com.willwinder.ugs.nbp.designer.gui.clipart;

import com.google.gson.Gson;
import com.willwinder.ugs.nbp.designer.gui.clipart.model.FontMapping;
import org.apache.commons.io.IOUtils;

import java.awt.Font;
import java.awt.FontFormatException;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

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

public FontClipartSource(String mappingFile) {
fontMapping = loadFontMapping(mappingFile);
Font font = loadFont();
fontMapping.getCliparts()
.forEach(clipart -> clipart.getCategories().forEach(category ->
cliparts.add(new FontClipart(clipart.getName(), category, font, clipart.getText(), this))));
}

private FontMapping loadFontMapping(String mappingFile) {
Gson gson = new Gson();
try {
return gson.fromJson(IOUtils.toString(Objects.requireNonNull(FontClipartSource.class.getResourceAsStream(mappingFile)), StandardCharsets.UTF_8), FontMapping.class);
} catch (IOException e) {
throw new ClipartSourceException("Could not load clipart font mapping file", e);
}
}

private Font loadFont() {
try {
return Font.createFont(Font.TRUETYPE_FONT, Objects.requireNonNull(FontClipartSource.class.getResourceAsStream(fontMapping.getFont()))).deriveFont(FONT_SIZE);
} catch (IOException | FontFormatException e) {
throw new ClipartSourceException("Could not load font", e);
}
}

@Override
public String getName() {
return fontMapping.getName();
}

@Override
public String getCredits() {
return fontMapping.getCredits();
}

@Override
public String getUrl() {
return fontMapping.getUrl();
}

@Override
public List<? extends Clipart> getCliparts() {
return cliparts;
}

@Override
public String getLicense() {
return fontMapping.getLicense();
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2022 Will Winder
Copyright 2022-2024 Will Winder
This file is part of Universal Gcode Sender (UGS).
Expand All @@ -18,35 +18,6 @@ This file is part of Universal Gcode Sender (UGS).
*/
package com.willwinder.ugs.nbp.designer.gui.clipart;

import com.willwinder.ugs.nbp.designer.gui.clipart.sources.BuDingbatsSource;
import com.willwinder.ugs.nbp.designer.gui.clipart.sources.ChristmasSource;
import com.willwinder.ugs.nbp.designer.gui.clipart.sources.Corners2Source;
import com.willwinder.ugs.nbp.designer.gui.clipart.sources.CreepyCrawliesSource;
import com.willwinder.ugs.nbp.designer.gui.clipart.sources.DarriansFrames1Source;
import com.willwinder.ugs.nbp.designer.gui.clipart.sources.DarriansFrames2Source;
import com.willwinder.ugs.nbp.designer.gui.clipart.sources.DestinysBordersSource;
import com.willwinder.ugs.nbp.designer.gui.clipart.sources.EasterArtSource;
import com.willwinder.ugs.nbp.designer.gui.clipart.sources.EfonSource;
import com.willwinder.ugs.nbp.designer.gui.clipart.sources.EvilzSource;
import com.willwinder.ugs.nbp.designer.gui.clipart.sources.FredokaSource;
import com.willwinder.ugs.nbp.designer.gui.clipart.sources.GardenSource;
import com.willwinder.ugs.nbp.designer.gui.clipart.sources.HouseIconsSource;
import com.willwinder.ugs.nbp.designer.gui.clipart.sources.KomikaBubblesSource;
import com.willwinder.ugs.nbp.designer.gui.clipart.sources.LogoSkate1Source;
import com.willwinder.ugs.nbp.designer.gui.clipart.sources.LogoSkate2Source;
import com.willwinder.ugs.nbp.designer.gui.clipart.sources.MythicalSource;
import com.willwinder.ugs.nbp.designer.gui.clipart.sources.SealifeSource;
import com.willwinder.ugs.nbp.designer.gui.clipart.sources.SugarComaSource;
import com.willwinder.ugs.nbp.designer.gui.clipart.sources.ToolSource;
import com.willwinder.ugs.nbp.designer.gui.clipart.sources.TransdingsSource;
import com.willwinder.ugs.nbp.designer.gui.clipart.sources.TravelconsSource;
import com.willwinder.ugs.nbp.designer.gui.clipart.sources.TropicanaSource;
import com.willwinder.ugs.nbp.designer.gui.clipart.sources.VintageCorners23Source;
import com.willwinder.ugs.nbp.designer.gui.clipart.sources.VintageDecorativeSigns2Source;
import com.willwinder.ugs.nbp.designer.gui.clipart.sources.WorldOfScifiSource;
import com.willwinder.ugs.nbp.designer.gui.clipart.sources.WwfreebieSource;
import com.willwinder.ugs.nbp.designer.gui.clipart.sources.XmasSource;
import com.willwinder.ugs.nbp.designer.gui.clipart.sources.YourSignSource;
import net.miginfocom.swing.MigLayout;

import javax.swing.JPanel;
Expand All @@ -57,6 +28,7 @@ This file is part of Universal Gcode Sender (UGS).
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;
Expand All @@ -71,39 +43,49 @@ public class PreviewListPanel extends JPanel {
private final JScrollPane scrollPane;
private transient Clipart selectedClipart;

private static final List<String> FONT_SOURCES = Arrays.asList(
"/fonts/christmas/mapping.json",
"/fonts/your-sign/mapping.json",
"/fonts/xmas/mapping.json",
"/fonts/bu-dingbats/mapping.json",
"/fonts/darrians-frames-font/mapping1.json",
"/fonts/darrians-frames-font/mapping2.json",
"/fonts/creepy-crawlies-font/mapping.json",
"/fonts/house-icons/mapping.json",
"/fonts/travelcons/mapping.json",
"/fonts/tool/mapping.json",
"/fonts/garden/mapping.json",
"/fonts/sugar-coma-font/mapping.json",
"/fonts/corners2/mapping.json",
"/fonts/wwfreebie/mapping.json",
"/fonts/destinys-borders/mapping.json",
"/fonts/vintage-decorative-corners-23-font/mapping.json",
"/fonts/vintage-decorative-signs-2-font/mapping.json",
"/fonts/world-of-sci-fi-font/mapping.json",
"/fonts/tropicana/mapping.json",
"/fonts/transdings/mapping.json",
"/fonts/sealife/mapping.json",
"/fonts/logoskate-1/mapping.json",
"/fonts/logoskate-2/mapping.json",
"/fonts/mythical/mapping.json",
"/fonts/komika-bubbles/mapping.json",
"/fonts/fredoka-one/mapping.json",
"/fonts/evilz/mapping.json",
"/fonts/easterart/mapping.json",
"/fonts/efon/mapping.json"
);

public PreviewListPanel(ActionListener selectAction) {
this.selectAction = selectAction;
buttonsPanel.setLayout(new MigLayout("fill, insets 5, wrap 4, top, left", "", ""));
sources.add(new Corners2Source());
sources.add(new ChristmasSource());
sources.add(new DestinysBordersSource());
sources.add(new EasterArtSource());
sources.add(new EfonSource());
sources.add(new EvilzSource());
sources.add(new FredokaSource());
sources.add(new MythicalSource());
sources.add(new KomikaBubblesSource());
sources.add(new LogoSkate1Source());
sources.add(new LogoSkate2Source());
sources.add(new SealifeSource());
sources.add(new TransdingsSource());
sources.add(new TropicanaSource());
sources.add(new YourSignSource());
sources.add(new WwfreebieSource());
sources.add(new XmasSource());
sources.add(new BuDingbatsSource());
sources.add(new DarriansFrames1Source());
sources.add(new DarriansFrames2Source());
sources.add(new WorldOfScifiSource());
sources.add(new VintageDecorativeSigns2Source());
sources.add(new VintageCorners23Source());
sources.add(new CreepyCrawliesSource());
sources.add(new SugarComaSource());
sources.add(new HouseIconsSource());
sources.add(new TravelconsSource());
sources.add(new ToolSource());
sources.add(new GardenSource());
buttonsPanel.setLayout(new MigLayout("fill, insets 10, wrap 4, top, left", "", ""));

FONT_SOURCES.forEach(s -> {
try {
sources.add(new FontClipartSource(s));
} catch (Exception e) {
throw new ClipartSourceException("Could not load source " + s, e);
}
});

setLayout(new BorderLayout());
scrollPane = new JScrollPane(buttonsPanel, ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
Expand All @@ -118,19 +100,21 @@ public void setCategory(Category category) {
sources.stream()
.flatMap(source -> source.getCliparts(category).stream())
.sorted(Comparator.comparing(clipart -> clipart.getName().toLowerCase()))
.forEach(clipart -> {
ClipartButton roundedPanel = new ClipartButton(clipart, tooltip);
roundedPanel.addClickListener(() -> {
selectedClipart = clipart;
selectAction.actionPerformed(new ActionEvent(roundedPanel, 0, "selected_clipart"));
});
buttonsPanel.add(roundedPanel, "grow, w 100:100:400");
});
.forEach(clipart -> createAndAddButton(tooltip, clipart));
buttonsPanel.revalidate();
buttonsPanel.repaint();
SwingUtilities.invokeLater(() -> scrollPane.getVerticalScrollBar().setValue(0));
}

private void createAndAddButton(ClipartTooltip tooltip, Clipart clipart) {
ClipartButton roundedPanel = new ClipartButton(clipart, tooltip);
roundedPanel.addClickListener(() -> {
selectedClipart = clipart;
selectAction.actionPerformed(new ActionEvent(roundedPanel, 0, "selected_clipart"));
});
buttonsPanel.add(roundedPanel, "grow, w 100:100:400");
}

public Optional<Clipart> getSelectedClipart() {
return Optional.ofNullable(selectedClipart);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
Copyright 2024 Will Winder
This file is part of Universal Gcode Sender (UGS).
UGS is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
UGS is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with UGS. If not, see <http://www.gnu.org/licenses/>.
*/
package com.willwinder.ugs.nbp.designer.gui.clipart.model;

import java.util.Collections;
import java.util.List;

/**
* A model for deserialize font configuration JSON
*
* @author Joacim Breiler
*/
public class FontMapping {
private String font;
private String name;
private String credits;
private String url;
private String license;
private List<FontMappingClipart> cliparts;

public List<FontMappingClipart> getCliparts() {
if (cliparts == null) {
return Collections.emptyList();
}
return cliparts;
}

public String getFont() {
return font;
}

public String getName() {
return name;
}

public String getCredits() {
return credits;
}

public String getUrl() {
return url;
}

public String getLicense() {
return license;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
Copyright 2024 Will Winder
This file is part of Universal Gcode Sender (UGS).
UGS is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
UGS is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with UGS. If not, see <http://www.gnu.org/licenses/>.
*/
package com.willwinder.ugs.nbp.designer.gui.clipart.model;

import com.willwinder.ugs.nbp.designer.gui.clipart.Category;
import org.apache.commons.lang3.StringUtils;

import java.util.List;

/**
* A model for deserialize font configuration JSON
*
* @author Joacim Breiler
*/
public class FontMappingClipart {
private String name;
private List<Category> categories;
private String text;

public String getName() {
return StringUtils.defaultString(name, "Unknown");
}

public List<Category> getCategories() {
if (categories == null || categories.isEmpty()) {
return List.of(Category.UNSORTED);
}
return categories;
}

public String getText() {
return StringUtils.defaultString(text, "?");
}
}
Loading

0 comments on commit bf72cd1

Please sign in to comment.