001package com.fs.starfarer.api.util; 002 003import java.util.Iterator; 004import java.util.LinkedHashMap; 005import java.util.Map; 006 007public class ValueShifterUtil implements ValueShifterAPI { 008 009 public static class ShiftData2 { 010 public float to; 011 public FaderUtil fader; 012 public float shift; 013 public boolean nudged; 014 } 015 016 protected float base; 017 protected float curr; 018 protected boolean useSquareOfProgress = true; 019 transient protected float averageShift; 020 protected Map<Object, ShiftData2> data = new LinkedHashMap<Object, ShiftData2>(); 021 022 public ValueShifterUtil(float base) { 023 this.base = base; 024 this.curr = base; 025 } 026 027 public boolean isUseSquareOfProgress() { 028 return useSquareOfProgress; 029 } 030 031 public void setUseSquareOfProgress(boolean useSquareOfProgress) { 032 this.useSquareOfProgress = useSquareOfProgress; 033 } 034 035 036 public boolean isShifted() { 037 //return base != curr; 038 return !data.isEmpty(); 039 } 040 041 public float getBase() { 042 return base; 043 } 044 045 public void setBase(float base) { 046 this.base = base; 047 } 048 049 public float getCurr() { 050 return curr; 051 } 052 053 public void shift(Object source, float to, float durIn, float durOut, float shift) { 054 ShiftData2 sd = data.get(source); 055 if (sd == null) { 056 sd = new ShiftData2(); 057 sd.fader = new FaderUtil(0, durIn, durOut); 058 sd.fader.setBounceDown(true); 059 data.put(source, sd); 060 } 061 sd.to = to; 062 sd.shift = shift; 063 sd.fader.setDuration(durIn, durOut); 064 sd.fader.fadeIn(); 065 sd.nudged = true; 066 } 067 068 public void advance(float amount) { 069 Iterator<ShiftData2> iter = data.values().iterator(); 070 while (iter.hasNext()) { 071 ShiftData2 sd = iter.next(); 072 if (!sd.nudged) sd.fader.fadeOut(); 073 sd.nudged = false; 074 sd.fader.advance(amount); 075 if (sd.fader.isFadedOut()) { 076 iter.remove(); 077 } 078 } 079 updateCurr(); 080 } 081 082 protected void updateCurr() { 083 if (data.isEmpty()) { 084 curr = base; 085 averageShift = 0f; 086 return; 087 } 088 089 float totalWeight = 0f; 090 for (ShiftData2 sd : data.values()) { 091 float progress = sd.fader.getBrightness(); 092 if (useSquareOfProgress) progress *= progress; 093 totalWeight += progress; 094 } 095 averageShift = totalWeight / (float) data.size(); 096 //totalWeight = data.size(); 097 098 if (totalWeight <= 0) { 099 curr = base; 100 return; 101 } 102 103 float result = base; 104 105 for (ShiftData2 sd : data.values()) { 106 float progress = sd.fader.getBrightness(); 107 if (useSquareOfProgress) progress *= progress; 108 109 result += (sd.to - base) * sd.shift * progress / (totalWeight - progress + 1f); 110 } 111 112 curr = result; 113 } 114 115 public float getAverageShift() { 116 return averageShift; 117 } 118 119 public float getShiftProgress(Object key) { 120 if (data.containsKey(key)) { 121 return data.get(key).fader.getBrightness(); 122 } 123 return 0f; 124 } 125 126 public static void main(String[] args) { 127 ValueShifterUtil c = new ValueShifterUtil(10f); 128 129 for (int i = 0; i < 10; i++) { 130 c.shift("c1", 25f, 1f, 1f, 1f); 131 c.shift("c2", 20f, 1f, 1f, 1f); 132 c.advance(0.1f); 133 } 134 135 System.out.println(c.getCurr()); 136 137// c.advance(0.1f); 138// System.out.println(c.getCurr()); 139 } 140 141} 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158