-
Notifications
You must be signed in to change notification settings - Fork 368
Add processing of multiple fluff images (WIP) #5490
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
SJuliez
wants to merge
25
commits into
MegaMek:main
Choose a base branch
from
SJuliez:multi-fluff
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+486
−42
Open
Changes from all commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
da88a4a
add processing of multiple fluff images
SJuliez bd2d0fa
allow clicking the fluff in the unit view to advance to the next image
SJuliez f26549a
cleanup
SJuliez 2c6663f
allow clan mek chassis variants, require chassis in model subdir name
SJuliez 8fa601d
clean up chassis name generation
SJuliez 494ca0d
Merging PR's
HammerGS 4878705
show next/prev buttons for fluff images, show filename (WIP)
SJuliez 9382bef
Merge branch 'refs/heads/master' into WIP-multi-fluff
SJuliez 890ac0f
remove image click mouse listener
SJuliez 6696ed3
Merge branch 'master' into multi-fluff
HammerGS 60b69b6
Fixing
HammerGS 0985559
Add a doc on AI usage (not Princess related) to MegaMek
HammerGS 4f4c794
only take image files as fluff images
SJuliez 8c253ae
add yaml parsed fluff tooltip to the MechViewPanel
SJuliez 4d1af5c
show placeholder for units without fluff
SJuliez 15eef9a
show info below fluff image
SJuliez ce52f03
Merge branch 'master' into WIP-multi-fluff
SJuliez d5953a3
after merge changes
SJuliez 7033c71
Updating history.txts
HammerGS 005179b
Merge branch 'main' into multi-fluff
SJuliez d72c30e
aftermerge corrections
SJuliez b5fafaa
Merge main into multi-fluff
HammerGS 3218c68
Fix: Use findFluffFiles instead of undefined findFluffFile method
HammerGS 1fd5a25
Address Copilot review feedback
HammerGS e5e14d7
Merge branch 'main' into multi-fluff
HammerGS File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,155 @@ | ||
| /* | ||
| * Copyright (c) 2024 - The MegaMek Team. All Rights Reserved. | ||
| * | ||
| * This file is part of MegaMek. | ||
| * | ||
| * MegaMek 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. | ||
| * | ||
| * MegaMek 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 MegaMek. If not, see <http://www.gnu.org/licenses/>. | ||
| */ | ||
| package megamek.client.ui; | ||
|
|
||
| import com.fasterxml.jackson.databind.JsonNode; | ||
| import com.fasterxml.jackson.databind.ObjectMapper; | ||
| import com.fasterxml.jackson.dataformat.yaml.YAMLFactory; | ||
| import megamek.client.ui.clientGUI.GUIPreferences; | ||
| import megamek.client.ui.util.FluffImageHelper; | ||
| import megamek.client.ui.util.UIUtil; | ||
| import org.apache.logging.log4j.LogManager; | ||
|
|
||
| import java.awt.*; | ||
| import java.io.File; | ||
| import java.io.IOException; | ||
| import java.nio.file.Files; | ||
| import java.nio.file.Path; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.Optional; | ||
| import java.util.stream.Stream; | ||
|
|
||
| /** | ||
| * This class is very specialized. It provides tooltip information for the fluff image tooltip in the | ||
| * MechViewPanel, taken from yaml files that are supplied with painted minis images. | ||
| */ | ||
| public class FluffImageTooltip { | ||
|
|
||
| private static final ObjectMapper yamlMapper = new ObjectMapper(new YAMLFactory()); | ||
|
|
||
| public static String styles() { | ||
| int labelSize = UIUtil.scaleForGUI(UIUtil.FONT_SCALE1); | ||
| Color color = GUIPreferences.getInstance().getToolTipLightFGColor(); | ||
| String styleColor = Integer.toHexString(color.getRGB() & 0xFFFFFF); | ||
| return "span { font-family:Noto Sans; font-size:" + labelSize + "; }" | ||
| + ".label { color:" + styleColor + "; }"; | ||
| } | ||
|
|
||
| /** | ||
| * Returns the tooltip text for the supplied FluffImageRecord, if any can be found, null otherwise. | ||
| * | ||
| * @param record The FluffImageRecord that is currently shown as an image | ||
| * @return A tooltip text or null if no yaml info is available | ||
| */ | ||
| public static String getTooltip(FluffImageHelper.FluffImageRecord record) { | ||
| return findYamlInfo(record).map(FluffImageTooltip::getTooltip).orElse(null); | ||
| } | ||
|
|
||
| private static Optional<File> findYamlInfo(FluffImageHelper.FluffImageRecord record) { | ||
| return (record.file() == null) ? Optional.empty() : getYamlFile(record.file()); | ||
| } | ||
|
|
||
| private static String getTooltip(File yamlFile) { | ||
| try { | ||
| JsonNode node = yamlMapper.readTree(yamlFile); | ||
|
|
||
| StringBuilder result = new StringBuilder("<HTML><HEAD><STYLE>" + styles() + "</STYLE></HEAD><BODY>"); | ||
| int width = UIUtil.scaleForGUI(360); | ||
| result.append("<div width=").append(width).append(">"); | ||
|
|
||
| if (node.has("title")) { | ||
| String unit = node.get("title").asText(); | ||
| if (!unit.isBlank()) { | ||
| result.append(UIUtil.spanCSS("label", "Unit: ")) | ||
| .append(UIUtil.spanCSS("value", unit)); | ||
| } | ||
| } | ||
| if (node.has("author")) { | ||
| String artist = node.get("author").asText(); | ||
| if (!artist.isBlank()) { | ||
| result.append(UIUtil.spanCSS("label", "<BR>Artist: ")) | ||
| .append(UIUtil.spanCSS("value", artist)); | ||
| } | ||
| } | ||
| if (node.has("content")) { | ||
| JsonNode contentNode = node.get("content"); | ||
| String description = findInsignia(contentNode); | ||
| if (!description.isBlank()) { | ||
| result.append(UIUtil.spanCSS("label", "<BR>Insignia: ")) | ||
| .append(UIUtil.spanCSS("value", description)); | ||
| } | ||
| } | ||
| result.append("</div></BODY></HTML>"); | ||
| return result.toString(); | ||
| } catch (IOException e) { | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
| private static String findInsignia(JsonNode contentNode) { | ||
| List<JsonNode> nodes = new ArrayList<>(); | ||
| contentNode.iterator().forEachRemaining(nodes::add); | ||
| for (JsonNode node : nodes) { | ||
| if (node.has("type") && node.get("type").asText().equals("insignia")) { | ||
| return node.get("content").asText(); | ||
| } | ||
| } | ||
| return ""; | ||
| } | ||
|
|
||
| private static Optional<File> getYamlFile(File imageFile) { | ||
| File parent = imageFile.getParentFile(); | ||
| if (parent == null) { | ||
| LogManager.getLogger().warn("Image file {} has no parent directory; cannot search for YAML.", imageFile); | ||
| return Optional.empty(); | ||
| } | ||
| try (Stream<Path> entries = Files.walk(parent.toPath(), 1)) { | ||
| return entries.filter(p -> isSuitableYamlFile(p, imageFile)).map(Path::toFile).findFirst(); | ||
| } catch (Exception e) { | ||
| LogManager.getLogger().warn("Error while reading files from {}", parent, e); | ||
| return Optional.empty(); | ||
| } | ||
| } | ||
|
|
||
| private static boolean isSuitableYamlFile(Path yamlFile, File imageFile) { | ||
| if (Files.isDirectory(yamlFile)) { | ||
| return false; | ||
| } | ||
|
|
||
| Path yamlFileNamePath = yamlFile.getFileName(); | ||
| if (yamlFileNamePath == null) { | ||
| return false; | ||
| } | ||
|
|
||
| String yamlFileName = yamlFileNamePath.toString(); | ||
| String suffix = "data.yaml"; | ||
| if (!yamlFileName.endsWith(suffix)) { | ||
| return false; | ||
| } | ||
|
|
||
| int baseLength = yamlFileName.length() - suffix.length(); | ||
| if (baseLength <= 0) { | ||
| return false; | ||
| } | ||
|
|
||
| String baseName = yamlFileName.substring(0, baseLength); | ||
| return imageFile.getName().contains(baseName); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.