Skip to content

Commit

Permalink
feat(mod): Add skeleton for integration with Progress Renderer
Browse files Browse the repository at this point in the history
  • Loading branch information
AamuLumi committed Nov 20, 2022
1 parent ec90382 commit af8370f
Show file tree
Hide file tree
Showing 7 changed files with 348 additions and 1 deletion.
Binary file modified Assemblies/Diary.dll
Binary file not shown.
25 changes: 25 additions & 0 deletions Source/DiaryService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.IO;
using System.IO.Packaging;
using System.Xml;
using HarmonyLib;
using RimWorld;
using RTFExporter;
using Verse;
Expand All @@ -12,10 +13,12 @@ namespace Diary
public class DiaryService : GameComponent
{
private Dictionary<string, string> entries;
private Dictionary<string, List<DiaryImageEntry>> images;

public DiaryService(Game game)
{
entries = new Dictionary<string, string>();
images = new Dictionary<string, List<DiaryImageEntry>>();
}

private string[] GetTextEntriesToExport()
Expand Down Expand Up @@ -130,6 +133,28 @@ public void AppendEntryNow(string data, bool onNewLine = true, bool writeCurrent
entries.SetOrAdd(key, $"{entries[key]}{data}");
}

public List<DiaryImageEntry> ReadImages(int day, Quadrum quadrum, int year)
{
DefaultMessage defaultMessageSetting = LoadedModManager.GetMod<Diary>().GetSettings<DiarySettings>().DefaultMessage;

return images.TryGetValue(GetDictionaryKey(day, quadrum, year));
}

public void AddImageNow(string path)
{
string key = GetDictionaryKey(TimeTools.GetCurrentDay(), TimeTools.GetCurrentQuadrum(), TimeTools.GetCurrentYear());
List<DiaryImageEntry> currentImages = images.TryGetValue(key);

if (currentImages == null)
{
currentImages = new List<DiaryImageEntry>();
}

currentImages.Add(new DiaryImageEntry(path, TimeTools.GetCurrentHour()));

images.SetOrAdd(key, currentImages);
}

public void Export()
{
string folder = LoadedModManager.GetMod<Diary>().GetSettings<DiarySettings>().FolderPath;
Expand Down
12 changes: 12 additions & 0 deletions Source/DiaryTypes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,16 @@ public static string GetLogWriterFilterName(LogWriterFilter f)
}
}
}

public class DiaryImageEntry
{
public string Path;
public int Hours;

public DiaryImageEntry(string path, int hours)
{
Path = path;
Hours = hours;
}
}
}
212 changes: 212 additions & 0 deletions Source/GUIDraggableTexture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,212 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

using UnityEngine;
using HarmonyLib;
using RimWorld;
using Verse;
using Verse.AI;
using Verse.Sound;
using RimWorld.Planet;
using UnityEngine.Networking;
using System.Windows;
using Rect = UnityEngine.Rect;
using Verse.Noise;
using RTFExporter;

namespace Diary
{
public class GUIDraggableTexture
{
private const float ZoomRatio = 0.2f;

private float currentImageScale;
private Rect imageRect;
private int imageWidth;
private int imageHeight;
private Texture2D currentImageDisplayed;
private UnityWebRequest imageLoadRequest;
private bool imageLoading;
private Rect outerRect;
private Rect initialOuterRect;
private bool mustRecomputeOuterRect;

public GUIDraggableTexture()
{
currentImageScale = 1.0f;

imageRect = new Rect(0.0f, 0.0f, 1.0f, 1.0f);
mustRecomputeOuterRect = false;
}

public bool HasImageLoaded()
{
return currentImageDisplayed != null;
}

public bool IsLoading()
{
return imageLoading;
}

public void LoadTexture(string path)
{
Log.Message($"Laod {path}");
imageLoading = true;
imageLoadRequest = UnityWebRequestTexture.GetTexture($"file://{path}");

imageLoadRequest.SendWebRequest().completed += delegate
{
currentImageDisplayed = DownloadHandlerTexture.GetContent(imageLoadRequest);

imageWidth = currentImageDisplayed.width;
imageHeight = currentImageDisplayed.height;

imageLoading = false;
mustRecomputeOuterRect = true;
};
}

private void TryFixImageCoordinates()
{
if (imageRect.xMin < 0f)
{
float diff = imageRect.xMin;

imageRect.xMin -= diff;
imageRect.xMax -= diff;
}
else if (imageRect.xMax > 1.0f)
{
float diff = imageRect.xMax - 1.0f;

imageRect.xMin -= diff;
imageRect.xMax -= diff;
}

if (imageRect.yMin < 0f)
{
float diff = imageRect.yMin;

imageRect.yMin -= diff;
imageRect.yMax -= diff;
}
else if (imageRect.yMax > 1.0f)
{
float diff = imageRect.yMax - 1.0f;

imageRect.yMin -= diff;
imageRect.yMax -= diff;
}
}

private void OnScrollWheel()
{
Event.current.Use();

float xRatio = outerRect.width / (float)imageWidth;
float yRatio = outerRect.height / (float)imageHeight;

if (Event.current.delta.y > 0 && currentImageScale > 1.00f)
{

imageRect.xMin -= xRatio * ZoomRatio;
imageRect.xMax += xRatio * ZoomRatio;
imageRect.yMin -= yRatio * ZoomRatio;
imageRect.yMax += yRatio * ZoomRatio;
currentImageScale -= 0.1f;

TryFixImageCoordinates();
}
else if (Event.current.delta.y < 0 && currentImageScale < 2.0f)
{

imageRect.xMin += xRatio * ZoomRatio;
imageRect.xMax -= xRatio * ZoomRatio;
imageRect.yMin += yRatio * ZoomRatio;
imageRect.yMax -= yRatio * ZoomRatio;
currentImageScale += 0.1f;

TryFixImageCoordinates();
}
}

private void OnDrag()
{
var currentCenter = imageRect.center;

float xDiff = Event.current.delta.x * -0.001f / currentImageScale / currentImageScale;
float yDiff = Event.current.delta.y * -0.001f / currentImageScale / currentImageScale;

if ((xDiff > 0f && imageRect.xMax < 1.0f) || (xDiff < 0f && imageRect.xMin > 0.0f))
{
currentCenter.x += xDiff;
}


if ((yDiff > 0f && imageRect.yMax < 1.0f) || (yDiff < 0f && imageRect.yMin > 0.0f))
{
currentCenter.y += yDiff;
}

imageRect.center = currentCenter;

TryFixImageCoordinates();
}

private void ComputeDefaultRects(Rect inRect)
{
outerRect = new Rect(0.0f, inRect.yMin, inRect.width, inRect.height);
initialOuterRect = new Rect(0.0f, inRect.yMin, inRect.width, inRect.height);

float displayRatio = inRect.width / inRect.height;
float imageRatio = (float)imageWidth / (float)imageHeight;

if (displayRatio > imageRatio)
{
float updateRatio = imageRatio / displayRatio;

imageRect = new Rect(0f, (1f - updateRatio) * 0.5f, 1f, updateRatio);
}
else
{
float updateRatio = displayRatio / imageRatio;

imageRect = new Rect(0.5f - updateRatio * 0.5f, 0f, updateRatio, 1f);
}

mustRecomputeOuterRect = false;
}

public void Draw(Rect inRect)
{
if (HasImageLoaded())
{
if (mustRecomputeOuterRect)
{
ComputeDefaultRects(inRect);
}


Widgets.DrawTexturePart(outerRect, imageRect, currentImageDisplayed);
}
if (Mouse.IsOver(outerRect))
{
if (Event.current.type == EventType.ScrollWheel)
{
OnScrollWheel();
}

if (Input.GetMouseButton(0) && Event.current.type == EventType.MouseDrag)
{
OnDrag();
}
}
}
}
}
57 changes: 56 additions & 1 deletion Source/MainTabWindow_Diary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
using Verse.AI;
using Verse.Sound;
using RimWorld.Planet;
using static HarmonyLib.Code;
using UnityEngine.Networking;
using Verse.Noise;

namespace Diary
{
Expand All @@ -26,6 +27,10 @@ public class MainTabWindow_Diary : MainTabWindow
private int displayedMessageIndex = -1;
private float messageLastHeight = 0f;
private LogFilter logFilter;
private bool imageDisplayMode;
private GUIDraggableTexture draggableImage;
private List<DiaryImageEntry> dayImages;
private int selectedDayImagesIndex;

private readonly List<string> fastHourStrings;
private Dictionary<string, string> truncationCache = new Dictionary<string, string>();
Expand Down Expand Up @@ -80,7 +85,9 @@ public MainTabWindow_Diary()

DiarySettings settings = LoadedModManager.GetMod<Diary>().GetSettings<DiarySettings>();

imageDisplayMode = false;
logFilter = settings.DefaultLogFilter;
draggableImage = new GUIDraggableTexture();
}

public bool CanAccessToPreviousDay()
Expand Down Expand Up @@ -363,13 +370,36 @@ private void DoLogEntryRow(Rect rect, LogEntry logEntry, int index)
}
}

public void DoImageDisplayContents(Rect inRect)
{
if (draggableImage.HasImageLoaded())
{
draggableImage.Draw(inRect);
}

Widgets.EndGroup();
}

public override void DoWindowContents(Rect inRect)
{
Rect dateRect = new Rect(0f, 0f, inRect.width, 40f);
float widthPerButton = inRect.width / 5;

Text.Font = GameFont.Small;
Widgets.BeginGroup(inRect);


if (Widgets.ButtonText(new Rect(0.0f, dateRect.yMin, widthPerButton / 2, dateRect.yMax), "IMG"))
{
imageDisplayMode = !imageDisplayMode;
dayImages = Current.Game.GetComponent<DiaryService>().ReadImages(this.day, this.quadrum, this.year);
selectedDayImagesIndex = 0;
if (dayImages.Count > 0)
{
draggableImage.LoadTexture(dayImages[0].Path);
}
}

if (CanAccessToPreviousDay())
{
if (Widgets.ButtonText(new Rect(widthPerButton * 0.5f, dateRect.yMin, widthPerButton / 2, dateRect.yMax), "<"))
Expand Down Expand Up @@ -429,6 +459,31 @@ public override void DoWindowContents(Rect inRect)
}
}

if (imageDisplayMode)
{
DiaryImageEntry d = dayImages[selectedDayImagesIndex];

if (d != null && Widgets.ButtonText(new Rect(widthPerButton * 4.5f, dateRect.yMin, widthPerButton / 2, dateRect.yMax), fastHourStrings[d.Hours]))
{

List<FloatMenuOption> list = new List<FloatMenuOption>();
for (int i = 0; i < dayImages.Count; i++)
{
int current = i;
list.Add(new FloatMenuOption(fastHourStrings[dayImages[current].Hours], delegate
{
selectedDayImagesIndex = current;
draggableImage.LoadTexture(dayImages[current].Path);
}));
}
Find.WindowStack.Add(new FloatMenu(list));
}

DoImageDisplayContents(new Rect(0f, dateRect.yMax + 10f, inRect.width, inRect.height - dateRect.yMax));

return;
}

Rect entryWritingRect = new Rect(0f, dateRect.yMax + 10f, inRect.width, 300f);
Current.Game.GetComponent<DiaryService>().WriteEntry(Widgets.TextArea(entryWritingRect, Current.Game.GetComponent<DiaryService>().ReadEntry(this.day, this.quadrum, this.year)), this.day, this.quadrum, this.year);

Expand Down
17 changes: 17 additions & 0 deletions Source/Patches/ListenProgressRenderer_CreateFilePath.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System.Linq;

using HarmonyLib;
using Verse;

namespace Diary
{
[HarmonyPatch(typeof(ProgressRenderer.MapComponent_RenderManager), "CreateFilePath")]
public static class ListenProgressRenderer_CreateFilePath
{
static void Postfix(ref string __result)
{
Log.Message("Image created");
Current.Game.GetComponent<DiaryService>().AddImageNow(__result);
}
}
}
Loading

0 comments on commit af8370f

Please sign in to comment.