Skip to content

Commit

Permalink
Refactored Award Drawing Methods
Browse files Browse the repository at this point in the history
Replaced stream-based award filtering and sorting with explicit loops for clearer logic. Adjusted image scaling to maintain aspect ratios using `getScaledInstance` instead of `ImageHelpers`. This change enhances code readability and ensures consistent image scaling.
  • Loading branch information
IllianiCBT committed Sep 25, 2024
1 parent 8619d8e commit 259ff5a
Showing 1 changed file with 57 additions and 51 deletions.
108 changes: 57 additions & 51 deletions MekHQ/src/mekhq/gui/view/PersonViewPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,6 @@
*/
package mekhq.gui.view;

import static megamek.client.ui.WrapLayout.wordWrap;
import static mekhq.campaign.personnel.Person.getLoyaltyName;

import java.awt.*;
import java.awt.Dialog.ModalityType;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.List;
import java.util.ResourceBundle;
import java.util.stream.Collectors;

import javax.accessibility.AccessibleRelation;
import javax.swing.*;
import javax.swing.table.TableColumn;

import megamek.codeUtilities.MathUtility;
import megamek.common.options.IOption;
import megamek.logging.MMLogger;
Expand All @@ -48,11 +29,7 @@
import mekhq.campaign.event.PersonChangedEvent;
import mekhq.campaign.finances.Money;
import mekhq.campaign.log.LogEntry;
import mekhq.campaign.personnel.Award;
import mekhq.campaign.personnel.Injury;
import mekhq.campaign.personnel.Person;
import mekhq.campaign.personnel.PersonnelOptions;
import mekhq.campaign.personnel.SkillType;
import mekhq.campaign.personnel.*;
import mekhq.campaign.personnel.education.Academy;
import mekhq.campaign.personnel.education.EducationController;
import mekhq.campaign.personnel.enums.GenderDescriptors;
Expand All @@ -65,10 +42,24 @@
import mekhq.gui.enums.MHQTabType;
import mekhq.gui.model.PersonnelEventLogModel;
import mekhq.gui.model.PersonnelKillLogModel;
import mekhq.gui.utilities.ImageHelpers;
import mekhq.gui.utilities.MarkdownRenderer;
import mekhq.gui.utilities.WrapLayout;

import javax.accessibility.AccessibleRelation;
import javax.swing.*;
import javax.swing.table.TableColumn;
import java.awt.*;
import java.awt.Dialog.ModalityType;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.List;
import java.util.*;
import java.util.stream.Collectors;

import static megamek.client.ui.WrapLayout.wordWrap;
import static mekhq.campaign.personnel.Person.getLoyaltyName;

/**
* A custom panel that gets filled in with goodies from a Person record
*
Expand Down Expand Up @@ -440,11 +431,13 @@ private int getAwardTierCount(Award award, int maximumTiers) {
private JPanel drawMedals() {
JPanel pnlMedals = new JPanel();

List<Award> awards = person.getAwardController()
.getAwards()
.stream().filter(a -> a.getNumberOfMedalFiles() > 0)
.sorted()
.toList();
List<Award> awards = new ArrayList<>();
for (Award award : person.getAwardController().getAwards()) {
if (award.getNumberOfMedalFiles() > 0) {
awards.add(award);
}
}
Collections.sort(awards);

for (Award award : awards) {
JLabel medalLabel = new JLabel();
Expand All @@ -466,14 +459,11 @@ private JPanel drawMedals() {
int height = medal.getHeight(null);

if (width == height) {
medal = ImageHelpers.getScaledForBoundaries(medal, new Dimension(45, 45),
Image.SCALE_DEFAULT);
medal = medal.getScaledInstance(40, 40, Image.SCALE_FAST);
} else if (width < height) {
medal = ImageHelpers.getScaledForBoundaries(medal, new Dimension(30, 60),
Image.SCALE_DEFAULT);
medal = medal.getScaledInstance(20, 40, Image.SCALE_FAST);
} else {
medal = ImageHelpers.getScaledForBoundaries(medal, new Dimension(60, 30),
Image.SCALE_DEFAULT);
medal = medal.getScaledInstance(40, 20, Image.SCALE_FAST);
}

medalLabel.setIcon(new ImageIcon(medal));
Expand All @@ -491,36 +481,52 @@ private JPanel drawMedals() {
* Draws the misc awards below the medals.
*/
private JPanel drawMiscAwards() {
JPanel pnlMiscAwards = new JPanel();
ArrayList<Award> awards = person.getAwardController().getAwards().stream()
.filter(a -> a.getNumberOfMiscFiles() > 0)
.collect(Collectors.toCollection(ArrayList::new));
JPanel pnlMiscs = new JPanel();

List<Award> awards = new ArrayList<>();
for (Award award : person.getAwardController().getAwards()) {
if (award.getNumberOfMiscFiles() > 0) {
awards.add(award);
}
}
Collections.sort(awards);

for (Award award : awards) {
JLabel miscLabel = new JLabel();

Image miscAward;
Image misc;
try {
int maximumTiers = award.getNumberOfMiscFiles();
int maximumTiers = award.getNumberOfMedalFiles();
int awardTierCount = getAwardTierCount(award, maximumTiers);

String miscFileName = award.getMiscFileName(awardTierCount);

Image miscAwardBufferedImage = (Image) MHQStaticDirectoryManager.getAwardIcons()
.getItem(award.getSet() + "/misc/", miscFileName);
if (miscAwardBufferedImage == null) {
misc = (Image) MHQStaticDirectoryManager.getAwardIcons()
.getItem(award.getSet() + "/misc/", miscFileName);
if (misc == null) {
continue;
}
miscAward = ImageHelpers.getScaledForBoundaries(miscAwardBufferedImage, new Dimension(100, 100),
Image.SCALE_DEFAULT);
miscLabel.setIcon(new ImageIcon(miscAward));

int width = misc.getWidth(null);
int height = misc.getHeight(null);

if (width == height) {
misc = misc.getScaledInstance(40, 40, Image.SCALE_FAST);
} else if (width < height) {
misc = misc.getScaledInstance(20, 40, Image.SCALE_FAST);
} else {
misc = misc.getScaledInstance(40, 20, Image.SCALE_FAST);
}

miscLabel.setIcon(new ImageIcon(misc));
miscLabel.setToolTipText(award.getTooltip(campaign.getCampaignOptions(), person));
pnlMiscAwards.add(miscLabel);
pnlMiscs.add(miscLabel);
} catch (Exception e) {
logger.error("", e);
}
}
return pnlMiscAwards;

return pnlMiscs;
}

/**
Expand Down Expand Up @@ -1882,7 +1888,7 @@ private JPanel fillInjuries() {

/**
* Gets the advanced medical effects active for the person.
*
*
* @return an HTML encoded string of effects
*/
private String getAdvancedMedalEffectString(Person p) {
Expand Down

0 comments on commit 259ff5a

Please sign in to comment.