001package com.fs.starfarer.api.impl.codex; 002 003import java.util.ArrayList; 004import java.util.LinkedHashSet; 005import java.util.List; 006import java.util.Set; 007 008import com.fs.starfarer.api.ModSpecAPI; 009import com.fs.starfarer.api.campaign.CustomUIPanelPlugin; 010import com.fs.starfarer.api.impl.campaign.ids.Tags; 011import com.fs.starfarer.api.loading.WithSourceMod; 012import com.fs.starfarer.api.ui.CustomPanelAPI; 013import com.fs.starfarer.api.ui.TagDisplayAPI; 014import com.fs.starfarer.api.ui.TooltipMakerAPI; 015import com.fs.starfarer.api.ui.UIPanelAPI; 016import com.fs.starfarer.api.util.Misc; 017 018public class CodexEntryV2 implements CodexEntryPlugin { 019 020 protected String id; 021 protected String title; 022 protected String icon; 023 protected CodexEntryPlugin parent; 024 protected Object param; 025 protected Object param2; 026 protected List<CodexEntryPlugin> children = new ArrayList<>(); 027 //protected Set<CodexEntryPlugin> related = new LinkedHashSet<>(); 028 protected Set<String> related = new LinkedHashSet<>(); 029 protected boolean retainOrderOfChildren = false; 030 protected boolean retainOrderOfRelatedEntries = false; 031 protected float categorySortTierForRelatedEntries = 1000; 032 protected Set<String> tags = new LinkedHashSet<>(); 033 034 public CodexEntryV2(String id, String title, String icon) { 035 this.id = id; 036 this.title = title; 037 this.icon = icon; 038 } 039 040 public CodexEntryV2(String id, String title, String icon, Object param) { 041 this.id = id; 042 this.title = title; 043 this.icon = icon; 044 this.param = param; 045 } 046 047 @Override 048 public String getId() { 049 return id; 050 } 051 052 @Override 053 public String getTitle() { 054 return title; 055 } 056 057 @Override 058 public String getSortTitle() { 059 return getTitle(); 060 } 061 062 @Override 063 public String getSearchString() { 064 return getTitle(); 065 } 066 067 @Override 068 public String getIcon() { 069 return icon; 070 } 071 072 @Override 073 public void setIcon(String icon) { 074 this.icon = icon; 075 } 076 077 @Override 078 public CodexEntryPlugin getParent() { 079 return parent; 080 } 081 082 @Override 083 public void setParent(CodexEntryPlugin parent) { 084 this.parent = parent; 085 } 086 087 @Override 088 public Object getParam() { 089 return param; 090 } 091 092 public void setParam(Object param) { 093 this.param = param; 094 } 095 096 @Override 097 public List<CodexEntryPlugin> getChildren() { 098 return children; 099 } 100 101 @Override 102 public void addChild(CodexEntryPlugin entry) { 103 if (entry == null) return; 104 entry.setParent(this); 105 children.add(entry); 106 } 107 108 @Override 109 public boolean isRetainOrderOfChildren() { 110 return retainOrderOfChildren; 111 } 112 113 @Override 114 public void setRetainOrderOfChildren(boolean retainOrderOfChildren) { 115 this.retainOrderOfChildren = retainOrderOfChildren; 116 } 117 118 @Override 119 public boolean isCategory() { 120 return !getChildren().isEmpty() || getParam() == null; 121 } 122 123 124 @Override 125 public void createTitleForList(TooltipMakerAPI info, float width, ListMode mode) { 126 if (isCategory()) { 127 info.setParaSmallInsignia(); 128 } 129 info.addPara(getTitle(), Misc.getBasePlayerColor(), 0f); 130 } 131 132 @Override 133 public boolean hasDetail() { 134 return !isCategory(); 135 } 136 137 138 @Override 139 public boolean matchesTags(Set<String> tags) { 140 return true; 141 } 142 143 @Override 144 public boolean hasTagDisplay() { 145 return false; 146 } 147 148 @Override 149 public void configureTagDisplay(TagDisplayAPI tags) { 150 151 } 152 153 @Override 154 public Set<String> getRelatedEntryIds() { 155 return related; 156 } 157 158 @Override 159 public Set<CodexEntryPlugin> getRelatedEntries() { 160 Set<CodexEntryPlugin> result = new LinkedHashSet<>(); 161 for (String id : related) { 162 CodexEntryPlugin entry = CodexDataV2.getEntry(id); 163 if (entry != null && entry != this) { 164 result.add(entry); 165 } 166 } 167 return result; 168 } 169 170 @Override 171 public void addRelatedEntry(CodexEntryPlugin entry) { 172 if (entry == null || entry.getId().equals(getId())) return; 173 //related.add(entry); 174 related.add(entry.getId()); 175 } 176 177 @Override 178 public void addRelatedEntry(String id) { 179 if (id == null) return; 180 //related.add(entry); 181 related.add(id); 182 } 183 184 public void removeRelatedEntry(CodexEntryPlugin entry) { 185 if (entry == null) return; 186 related.remove(entry.getId()); 187 } 188 189 public void removeRelatedEntry(String id) { 190 related.remove(id); 191 } 192 193 @Override 194 public boolean isRetainOrderOfRelatedEntries() { 195 return retainOrderOfRelatedEntries; 196 } 197 198 @Override 199 public void setRetainOrderOfRelatedEntries(boolean retainOrderOfRelatedEntries) { 200 this.retainOrderOfRelatedEntries = retainOrderOfRelatedEntries; 201 } 202 203 @Override 204 public float getCategorySortTierForRelatedEntries() { 205 return categorySortTierForRelatedEntries; 206 } 207 208 @Override 209 public void setCategorySortTierForRelatedEntries(float categorySortTierForRelatedEntries) { 210 this.categorySortTierForRelatedEntries = categorySortTierForRelatedEntries; 211 } 212 213 214 @Override 215 public List<CodexEntryPlugin> getChildrenRecursive(boolean includeCategories) { 216 List<CodexEntryPlugin> result = new ArrayList<>(); 217 findChildren(this, result, includeCategories); 218 return result; 219 } 220 221 public void findChildren(CodexEntryPlugin curr, List<CodexEntryPlugin> result, boolean includeCategories) { 222 if (includeCategories || !curr.isCategory()) { 223 result.add(curr); 224 } 225 for (CodexEntryPlugin child : curr.getChildren()) { 226 findChildren(child, result, includeCategories); 227 } 228 } 229 230 231 @Override 232 public boolean hasCustomDetailPanel() { 233 return false; 234 } 235 236 @Override 237 public CustomUIPanelPlugin getCustomPanelPlugin() { 238 return null; 239 } 240 241 @Override 242 public void createCustomDetail(CustomPanelAPI panel, UIPanelAPI relatedEntries, CodexDialogAPI codex) { 243 244 } 245 246 public void destroyCustomDetail() { 247 248 } 249 250 @Override 251 public boolean isVignetteIcon() { 252 return false; 253 } 254 255 protected boolean checking = false; 256 public boolean areAnyRelatedEntriesVisible() { 257 // recursed back to this entry due to something else circularly checking its visibility 258 if (checking) return false; 259 checking = true; 260 boolean found = false; 261 for (CodexEntryPlugin rel : getRelatedEntries()) { 262 if (rel.isVisible()) { 263 found = true; 264 break; 265 } 266 } 267 checking = false; 268 return found; 269 } 270 271 public boolean areAnyRelatedEntriesUnlocked() { 272 // recursed back to this entry due to something else circularly checking its locked status 273 if (checking) return isUnlockedIfRequiresUnlock(); 274 checking = true; 275 boolean found = false; 276 for (CodexEntryPlugin rel : getRelatedEntries()) { 277 if (!rel.isLocked()) { 278 found = true; 279 break; 280 } 281 } 282 checking = false; 283 return found; 284 } 285 286 @Override 287 public boolean isVisible() { 288 return isVisibleStandard(getUnlockRelatedTags(), isUnlockedIfRequiresUnlock()); 289 } 290 291 @Override 292 public boolean isLocked() { 293 return isLockedStandard(getUnlockRelatedTags(), isUnlockedIfRequiresUnlock()); 294 } 295 296 @Override 297 public boolean checkTagsWhenLocked() { 298 return false; 299 } 300 301 public Set<String> getUnlockRelatedTags() { 302 return null; 303 } 304 305 public boolean isUnlockedIfRequiresUnlock() { 306 return true; 307 } 308 309 public boolean isVisibleStandard(Set<String> tags, boolean thingUnlocked) { 310 if (tags == null) return true; 311 312 if (tags.contains(Tags.INVISIBLE_IN_CODEX)) return false; 313 if (tags.contains(Tags.CODEX_UNLOCKABLE)) return true; 314 315 if (tags.contains(Tags.CODEX_REQUIRE_RELATED)) { 316 return areAnyRelatedEntriesVisible(); 317 } 318 return true; 319 } 320 321 public boolean isLockedStandard(Set<String> tags, boolean thingUnlocked) { 322 if (tags == null) return false; 323 324 if (CodexDataV2.codexFullyUnlocked()) return false; 325 if (tags.contains(Tags.CODEX_UNLOCKABLE) && !thingUnlocked) { 326 return true; 327 } 328 if (tags.contains(Tags.CODEX_REQUIRE_RELATED)) { 329 return !areAnyRelatedEntriesUnlocked(); 330 } 331 return false; 332 } 333 334 @Override 335 public Object getParam2() { 336 return param2; 337 } 338 339 public void setParam2(Object param2) { 340 this.param2 = param2; 341 } 342 343 @Override 344 public boolean skipForTags() { 345 return false; 346 } 347 348 349 @Override 350 public Set<String> getTags() { 351 return tags; 352 } 353 354 @Override 355 public void addTag(String tag) { 356 tags.add(tag); 357 } 358 359 @Override 360 public boolean hasTag(String tag) { 361 return tags.contains(tag); 362 } 363 364 @Override 365 public ModSpecAPI getSourceMod() { 366 if (getParam() instanceof WithSourceMod) { 367 return ((WithSourceMod)getParam()).getSourceMod(); 368 } 369 return null; 370 } 371} 372 373 374 375 376 377 378 379 380 381 382 383