private void HandleNewLevels() { List <TWLevel> levels = TWGameManager.instance.TimeWalkLevels; if (levels == null || levelTextPrefab == null || slider == null) { return; } if (levels.Count <= 1) { slider.gameObject.SetActive(false); return; } slider.gameObject.SetActive(true); RectTransform lastItemRectTransform = null; Slider sliderComp = slider.GetComponent <Slider>(); Rect sliderRect = slider.GetComponent <RectTransform>().rect; float levelItemHeight = levelTextPrefab.GetComponent <RectTransform>().rect.height; int maxYear = levels[0].year; int minYear = levels[levels.Count - 1].year; levelYearRange = new TWRange(minYear, maxYear); // Figured out the appropriate pixel range through trial and error... // There MUST be a better way. slidePixelRange = new TWRange(levelItemHeight * 2, sliderRect.height + levelItemHeight + 10); ClearLevels(); sliderComp.maxValue = maxYear; sliderComp.minValue = minYear; levels.ForEach(delegate(TWLevel l) { // Create menu item Button item = Instantiate(levelTextPrefab, track.transform).GetComponent <Button>(); item.GetComponent <Text>().text = l.year.ToString(); item.name = "LevelTextItem" + l.year.ToString(); // TODO Listen for button clicks RectTransform itemRect = item.GetComponent <RectTransform>(); Vector3 pos = itemRect.position; // Convert current year to corresponding pixel in y range float yearInPixels = levelYearRange.Translate(l.year, slidePixelRange); // Place level on slide as close as possible to year in pixels pos.y = yearInPixels; itemRect.position = pos; lastItemRectTransform = itemRect; }); }
// Use this for initialization void Start() { skyMat = RenderSettings.skybox; mainLight = GetComponent <Light>(); flare = GetComponent <LensFlare>(); flareBrightness = flare.brightness; skySpeed = TWGameManager.timeSpeedUpHours / 3600; twentyFour = new TWRange(0f, 24f); fullRotation = new TWRange(0f, 360f); flareBrightnessRange = new TWRange(0f, flareBrightness); downRangeForFlare = new TWRange(intensityCutOff, 0f); RotateSun(); TWGameManager.instance.TWNightDayChanged += RotateSun; }
//<summary>Translates value from Range to target Range</summary> //<param name=x>Value from the current Range to translate</param> //<param name=targetRange>The range to which x should be translated</param> public float Translate(float x, TWRange targetRange) { if (this.Delta() <= 0f) { throw new ArgumentException(String.Format("current range does not have a positive delta")); } if (targetRange.Delta() <= 0f) { throw new ArgumentException(String.Format("target does not have a positive delta")); } if (x > max || x < min) { throw new ArgumentException(String.Format("x ({0}) is out of current range", x)); } float ratio = targetRange.Delta() / Delta(); float converted = ratio * (max - x); float translated = targetRange.max - converted; // Debug.Log(String.Format("{0} in range ({4},{5}) translated to range ({1},{2}) is {3}", x, targetRange.min, targetRange.max, translated, min, max)); return(translated); }