-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(mod): Add skeleton for integration with Progress Renderer
- Loading branch information
Showing
7 changed files
with
348 additions
and
1 deletion.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
Oops, something went wrong.