|
| 1 | +package com.factionrelationships; |
| 2 | + |
| 3 | +import com.fs.starfarer.api.Global; |
| 4 | +import com.fs.starfarer.api.campaign.FactionAPI; |
| 5 | +import com.fs.starfarer.api.campaign.econ.EconomyAPI; |
| 6 | +import com.fs.starfarer.api.campaign.econ.MarketAPI; |
| 7 | +import com.fs.starfarer.api.campaign.listeners.CampaignUIRenderingListener; |
| 8 | +import com.fs.starfarer.api.characters.RelationshipAPI; |
| 9 | +import com.fs.starfarer.api.combat.ViewportAPI; |
| 10 | +import com.fs.starfarer.api.ui.Fonts; |
| 11 | +import com.fs.starfarer.api.ui.LabelAPI; |
| 12 | +import com.fs.starfarer.api.ui.PositionAPI; |
| 13 | + |
| 14 | +import org.json.JSONException; |
| 15 | +import org.json.JSONObject; |
| 16 | + |
| 17 | +import java.awt.Color; |
| 18 | +import java.io.IOException; |
| 19 | +import java.util.ArrayList; |
| 20 | +import java.util.Collections; |
| 21 | +import java.util.Comparator; |
| 22 | +import java.util.HashSet; |
| 23 | +import java.util.List; |
| 24 | +import java.util.Set; |
| 25 | + |
| 26 | +public class FactionRelationshipsUIRenderer implements CampaignUIRenderingListener { |
| 27 | + |
| 28 | + private static final float X_PAD = 20f; |
| 29 | + private static final float Y_PAD = 80f; |
| 30 | + private static final float LINE_HEIGHT = 14f; |
| 31 | + private static final int DEFAULT_MAX_FACTIONS = 15; |
| 32 | + private static final int MIN_MAX_FACTIONS = 1; |
| 33 | + private static final int MAX_MAX_FACTIONS = 50; |
| 34 | + private static final String CONFIG_PATH = "config/faction_relationships_config.json"; |
| 35 | + private static final String MOD_ID = "factionrelationships"; |
| 36 | + |
| 37 | + private static Integer cachedMaxFactions = null; |
| 38 | + |
| 39 | + private static int getMaxFactions() { |
| 40 | + if (cachedMaxFactions != null) { |
| 41 | + return cachedMaxFactions.intValue(); |
| 42 | + } |
| 43 | + try { |
| 44 | + JSONObject config = Global.getSettings().loadJSON(CONFIG_PATH, MOD_ID); |
| 45 | + int value = config.optInt("maxFactions", DEFAULT_MAX_FACTIONS); |
| 46 | + cachedMaxFactions = Integer.valueOf(Math.max(MIN_MAX_FACTIONS, Math.min(MAX_MAX_FACTIONS, value))); |
| 47 | + } catch (IOException e) { |
| 48 | + cachedMaxFactions = Integer.valueOf(DEFAULT_MAX_FACTIONS); |
| 49 | + } catch (JSONException e) { |
| 50 | + cachedMaxFactions = Integer.valueOf(DEFAULT_MAX_FACTIONS); |
| 51 | + } |
| 52 | + return cachedMaxFactions.intValue(); |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + public void renderInUICoordsBelowUI(ViewportAPI viewport) { |
| 57 | + // no-op; draw above UI |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + public void renderInUICoordsAboveUIBelowTooltips(ViewportAPI viewport) { |
| 62 | + renderPanel(viewport); |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + public void renderInUICoordsAboveUIAndTooltips(ViewportAPI viewport) { |
| 67 | + // no-op; we use AboveUIBelowTooltips so tooltips can show on top |
| 68 | + } |
| 69 | + |
| 70 | + private void renderPanel(ViewportAPI viewport) { |
| 71 | + if (Global.getSector() == null || Global.getSector().getPlayerFleet() == null) { |
| 72 | + return; |
| 73 | + } |
| 74 | + |
| 75 | + Set<String> factionIdsWithMarkets = new HashSet<String>(); |
| 76 | + EconomyAPI economy = Global.getSector().getEconomy(); |
| 77 | + if (economy != null) { |
| 78 | + for (MarketAPI market : economy.getMarketsCopy()) { |
| 79 | + if (market.getFaction() != null) { |
| 80 | + factionIdsWithMarkets.add(market.getFactionId()); |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + List<FactionAPI> factions = new ArrayList<FactionAPI>(); |
| 86 | + for (FactionAPI faction : Global.getSector().getAllFactions()) { |
| 87 | + if (faction.isPlayerFaction()) { |
| 88 | + continue; |
| 89 | + } |
| 90 | + if (!factionIdsWithMarkets.contains(faction.getId())) { |
| 91 | + continue; |
| 92 | + } |
| 93 | + factions.add(faction); |
| 94 | + } |
| 95 | + |
| 96 | + Collections.sort(factions, new Comparator<FactionAPI>() { |
| 97 | + @Override |
| 98 | + public int compare(FactionAPI a, FactionAPI b) { |
| 99 | + float relA = a.getRelToPlayer().getRel(); |
| 100 | + float relB = b.getRelToPlayer().getRel(); |
| 101 | + return Float.compare(relA, relB); |
| 102 | + } |
| 103 | + }); |
| 104 | + |
| 105 | + int maxFactions = getMaxFactions(); |
| 106 | + int count = Math.min(factions.size(), maxFactions); |
| 107 | + if (count == 0) { |
| 108 | + return; |
| 109 | + } |
| 110 | + |
| 111 | + float screenW = Global.getSettings().getScreenWidth(); |
| 112 | + float screenH = Global.getSettings().getScreenHeight(); |
| 113 | + |
| 114 | + List<LabelAPI> labels = new ArrayList<LabelAPI>(); |
| 115 | + for (int i = 0; i < count; i++) { |
| 116 | + FactionAPI faction = factions.get(i); |
| 117 | + RelationshipAPI rel = faction.getRelToPlayer(); |
| 118 | + float repValue = rel.getRel(); |
| 119 | + String levelName = rel.getLevel().getDisplayName(); |
| 120 | + String line = faction.getDisplayName() + " " + formatRepValue(repValue) + " " + levelName; |
| 121 | + Color color = rel.getRelColor(); |
| 122 | + |
| 123 | + LabelAPI label = Global.getSettings().createLabel(line, Fonts.VICTOR_10); |
| 124 | + label.setColor(color); |
| 125 | + labels.add(label); |
| 126 | + } |
| 127 | + |
| 128 | + for (int i = 0; i < labels.size(); i++) { |
| 129 | + LabelAPI label = labels.get(i); |
| 130 | + float w = label.computeTextWidth(label.getText()); |
| 131 | + PositionAPI pos = label.getPosition(); |
| 132 | + if (i == 0) { |
| 133 | + pos.inTR(X_PAD, Y_PAD); |
| 134 | + } else { |
| 135 | + float y = Y_PAD + i * LINE_HEIGHT; |
| 136 | + pos.setLocation(screenW - X_PAD - w, screenH - y - LINE_HEIGHT); |
| 137 | + } |
| 138 | + label.render(1f); |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + private static String formatRepValue(float rel) { |
| 143 | + int pct = (int) Math.round(rel * 100f); |
| 144 | + if (pct >= 0) { |
| 145 | + return "+" + pct; |
| 146 | + } |
| 147 | + return String.valueOf(pct); |
| 148 | + } |
| 149 | +} |
0 commit comments