Skip to content

Commit

Permalink
Implement support of the academic year
Browse files Browse the repository at this point in the history
  • Loading branch information
dymanoid committed May 19, 2019
1 parent f7f4dca commit 0a0c94a
Show file tree
Hide file tree
Showing 6 changed files with 144 additions and 2 deletions.
4 changes: 4 additions & 0 deletions src/RealTime/Core/RealTimeCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ public static RealTimeCore Run(
{
WorldInfoPanelPatches.CitizenInfoPanel = CustomCitizenInfoPanel.Enable(ResidentAIPatch.RealTimeAI, localizationProvider);
WorldInfoPanelPatches.VehicleInfoPanel = CustomVehicleInfoPanel.Enable(ResidentAIPatch.RealTimeAI, localizationProvider);
WorldInfoPanelPatches.CampusWorldInfoPanel = CustomCampusWorldInfoPanel.Enable(localizationProvider);
}

AwakeSleepSimulation.Install(configProvider.Configuration);
Expand Down Expand Up @@ -266,6 +267,9 @@ public void Stop()
WorldInfoPanelPatches.VehicleInfoPanel?.Disable();
WorldInfoPanelPatches.VehicleInfoPanel = null;

WorldInfoPanelPatches.CampusWorldInfoPanel?.Disable();
WorldInfoPanelPatches.CampusWorldInfoPanel = null;

isEnabled = false;
}

Expand Down
6 changes: 6 additions & 0 deletions src/RealTime/Events/VanillaEvents.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,12 @@ private static void Customize(EventAI eventAI)
disorganizeDuration = 4f;
break;

case EventManager.EventType.AcademicYear:
eventDuration = 7f * 24f;
prepareDuration = 2f;
disorganizeDuration = 2f;
break;

default:
return;
}
Expand Down
11 changes: 9 additions & 2 deletions src/RealTime/GameConnection/Patches/WorldInfoPanelPatches.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ internal static class WorldInfoPanelPatches
/// <summary>Gets or sets the customized vehicle information panel.</summary>
public static CustomVehicleInfoPanel VehicleInfoPanel { get; set; }

/// <summary>Gets or sets the customized campus information panel.</summary>
public static CustomCampusWorldInfoPanel CampusWorldInfoPanel { get; set; }

/// <summary>Gets the patch for the update bindings method.</summary>
public static IPatch UpdateBindings { get; } = new WorldInfoPanel_UpdateBindings();

Expand All @@ -42,11 +45,15 @@ private static void Postfix(WorldInfoPanel __instance, ref InstanceID ___m_Insta
switch (__instance)
{
case CitizenWorldInfoPanel _:
CitizenInfoPanel.UpdateCustomInfo(ref ___m_InstanceID);
CitizenInfoPanel?.UpdateCustomInfo(ref ___m_InstanceID);
break;

case VehicleWorldInfoPanel _:
VehicleInfoPanel.UpdateCustomInfo(ref ___m_InstanceID);
VehicleInfoPanel?.UpdateCustomInfo(ref ___m_InstanceID);
break;

case CampusWorldInfoPanel _:
CampusWorldInfoPanel?.UpdateCustomInfo(ref ___m_InstanceID);
break;
}
}
Expand Down
9 changes: 9 additions & 0 deletions src/RealTime/Localization/TranslationKeys.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,14 @@ internal static class TranslationKeys

/// <summary>The key for the vacation text.</summary>
public const string WorkStatusOnVacation = "WorkStatus.OnVacation";

/// <summary>The key for the academic year ETA text (in days).</summary>
public const string AcademicYearDaysLeft = "AcademicYear.DaysLeft";

/// <summary>The key for the academic year ETA text (in hours).</summary>
public const string AcademicYearHoursLeft = "AcademicYear.HoursLeft";

/// <summary>The key for the academic year ETA text (ends soon).</summary>
public const string AcademicYearEndsSoon = "AcademicYear.EndsSoon";
}
}
1 change: 1 addition & 0 deletions src/RealTime/RealTime.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@
<Compile Include="GameConnection\TimeInfo.cs" />
<Compile Include="LogCategory.cs" />
<Compile Include="Simulation\TimeControlCompatibility.cs" />
<Compile Include="UI\CustomCampusWorldInfoPanel.cs" />
<Compile Include="UI\CustomCitizenInfoPanel.cs" />
<Compile Include="UI\CustomTimeBarClickEventArgs.cs" />
<Compile Include="UI\ConfigUI.cs" />
Expand Down
115 changes: 115 additions & 0 deletions src/RealTime/UI/CustomCampusWorldInfoPanel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
// <copyright file="CustomCampusWorldInfoPanel.cs" company="dymanoid">
// Copyright (c) dymanoid. All rights reserved.
// </copyright>

namespace RealTime.UI
{
using System;
using ColossalFramework.UI;
using RealTime.Localization;
using SkyTools.Localization;
using SkyTools.UI;
using UnityEngine;

/// <summary>
/// A customized campus info panel that shows the correct length of the academic year.
/// </summary>
internal sealed class CustomCampusWorldInfoPanel : CustomInfoPanelBase<CampusWorldInfoPanel>
{
private const string GameInfoPanelName = "(Library) CampusWorldInfoPanel";
private const string ProgressTooltipLabelName = "ProgressTooltipLabel";
private const string ComponentId = "RealTimeAcademicYearProgress";

private readonly ILocalizationProvider localizationProvider;
private UILabel progressTooltipLabel;
private UILabel originalProgressTooltipLabel;

private CustomCampusWorldInfoPanel(string infoPanelName, ILocalizationProvider localizationProvider)
: base(infoPanelName)
{
this.localizationProvider = localizationProvider;
}

/// <summary>Enables the campus info panel customization. Can return null on failure.</summary>
///
/// <param name="localizationProvider">The localization provider to use for text translation.</param>
///
/// <returns>An instance of the <see cref="CustomCampusWorldInfoPanel"/> object that can be used for disabling
/// the customization, or null when the customization fails.</returns>
///
/// <exception cref="ArgumentNullException">Thrown when <paramref name="localizationProvider"/> is <c>null</c>.</exception>
public static CustomCampusWorldInfoPanel Enable(ILocalizationProvider localizationProvider)
{
if (localizationProvider == null)
{
throw new ArgumentNullException(nameof(localizationProvider));
}

var result = new CustomCampusWorldInfoPanel(GameInfoPanelName, localizationProvider);
return result.Initialize() ? result : null;
}

/// <summary>Updates the custom information in this panel.</summary>
/// <param name="instance">The game object instance to get the information from.</param>
public override void UpdateCustomInfo(ref InstanceID instance)
{
ushort mainGate = DistrictManager.instance.m_parks.m_buffer[instance.Park].m_mainGate;
ushort eventIndex = BuildingManager.instance.m_buildings.m_buffer[mainGate].m_eventIndex;
ref EventData eventData = ref EventManager.instance.m_events.m_buffer[eventIndex];

if (!(eventData.Info.m_eventAI is AcademicYearAI academicYearAI))
{
return;
}

var endFrame = eventData.m_startFrame + (int)(academicYearAI.m_eventDuration * SimulationManager.DAYTIME_HOUR_TO_FRAME);
var framesLeft = endFrame - SimulationManager.instance.m_currentFrameIndex;
if (framesLeft < 0)
{
framesLeft = 0;
}

float hoursLeft = framesLeft * SimulationManager.DAYTIME_FRAME_TO_HOUR;
if (hoursLeft < 1f)
{
progressTooltipLabel.text = localizationProvider.Translate(TranslationKeys.AcademicYearEndsSoon);
}
else if (hoursLeft < 24f)
{
string template = localizationProvider.Translate(TranslationKeys.AcademicYearHoursLeft);
progressTooltipLabel.text = string.Format(template, Mathf.RoundToInt(hoursLeft));
}
else
{
float daysLeft = hoursLeft / 24f;
string template = localizationProvider.Translate(TranslationKeys.AcademicYearDaysLeft);
progressTooltipLabel.text = string.Format(template, (int)Math.Ceiling(daysLeft));
}
}

/// <summary>Destroys the custom UI objects for the info panel.</summary>
protected override void DisableCore()
{
progressTooltipLabel.parent.RemoveUIComponent(progressTooltipLabel);
UnityEngine.Object.Destroy(progressTooltipLabel.gameObject);
originalProgressTooltipLabel.isVisible = true;
progressTooltipLabel = null;
originalProgressTooltipLabel = null;
}

/// <summary>Builds up the custom UI objects for the info panel.</summary>
/// <returns><c>true</c> on success; otherwise, <c>false</c>.</returns>
protected override bool InitializeCore()
{
originalProgressTooltipLabel = ItemsPanel.parent.Find<UILabel>(ProgressTooltipLabelName);
if (originalProgressTooltipLabel == null)
{
return false;
}

progressTooltipLabel = UIComponentTools.CreateCopy(originalProgressTooltipLabel, originalProgressTooltipLabel.parent, ComponentId);
originalProgressTooltipLabel.isVisible = false;
return true;
}
}
}

0 comments on commit 0a0c94a

Please sign in to comment.