Skip to content

Commit

Permalink
Update Text drawing to more accurately place on texture
Browse files Browse the repository at this point in the history
  • Loading branch information
tony56a committed Jul 8, 2019
1 parent 3ed124e commit b4a82ab
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 27 deletions.
24 changes: 10 additions & 14 deletions TLM/TLM/Manager/Impl/LanePropManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
namespace TrafficManager.Manager.Impl {

public class LanePropManager : AbstractCustomManager, ILanePropManager, IPreLoadManager {


public static readonly LanePropManager Instance = new LanePropManager();

Expand All @@ -32,10 +31,6 @@ public class LanePropManager : AbstractCustomManager, ILanePropManager, IPreLoad
/// </summary>
private Dictionary<string, Dictionary<string, PropInfo>> speedLimitProps;

public Dictionary<string, CustomProp> textures = new Dictionary<string, CustomProp>();

public PropInfo blah;

/// <summary>
/// The key name for the fallback(vanilla) prop collections
/// </summary>
Expand Down Expand Up @@ -100,20 +95,21 @@ public override void OnLevelLoading() {
}

public void PreLoadData() {
Font font = Font.CreateDynamicFontFromOSFont("Electronic Highway Sign", 36);
font.RequestCharactersInTexture("ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890", 36);
Font font = Font.CreateDynamicFontFromOSFont("Arial", 60);
font.RequestCharactersInTexture("ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890", 60);
var fakeObj = new GameObject("Fake faking dynamicsign");
var fakeTextMesh = fakeObj.AddComponent<TextMesh>();
fakeTextMesh.font = font;
fakeTextMesh.GetComponent<Renderer>().material = font.material;
fakeTextMesh.fontSize = 36;
fakeTextMesh.fontSize = 60;
fakeTextMesh.text = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";

Material fontMat = font.material;
fontMat.color = Color.black;
font.material.mainTexture.MakeReadable();
signFont = font;
gameObj = fakeObj;
gameObj.SetActive(false);
}


Expand Down Expand Up @@ -160,7 +156,6 @@ private void LoadFallbackProps() {
foreach(var propInfo in allPropInfos) {
if (propInfo.name.ToLower().Contains("UKR-S Blank".ToLower())) {
originalProp = propInfo as PropInfo;
blah = propInfo as PropInfo;
}
}

Expand All @@ -169,8 +164,9 @@ private void LoadFallbackProps() {
return;
}
Dictionary<string, PropInfo> propCollectionToAdd = new Dictionary<string, PropInfo>();
for (int i = 5; i < 140; i += 5) {
var propName = "TMPE sign " + i;
for (int i = 5; i <= 140; i += 5) {
var speedString = i.ToString();
var propName = $"TMPE sign {speedString}";
GameObject signObj = GameObject.Instantiate<GameObject>(originalProp.gameObject);
signObj.SetActive(true);
signObj.name = propName;
Expand All @@ -180,12 +176,12 @@ private void LoadFallbackProps() {

var newTexture = new Texture2D(material.mainTexture.width, material.mainTexture.height, TextureFormat.ARGB32, false);
newTexture.SetPixels(((Texture2D)material.mainTexture).GetPixels());
TextureUtil.DrawText(ref newTexture, i.ToString(), signFont, 923, 429, 36);

TextureUtil.DrawText(ref newTexture, speedString, signFont, 953, 432, 60);
material.SetTexture("_MainTex", newTexture);
meshRenderer.material = material;
textures.Add(propName, new CustomProp(material, originalProp.m_mesh));
PrefabCollection<PropInfo>.InitializePrefabs("Custom Assets", replacementProp, null);
replacementProp.m_lodRenderDistance = originalProp.m_lodRenderDistance;
replacementProp.m_lodRenderDistance = originalProp.m_lodRenderDistance * 2;
replacementProp.m_maxRenderDistance = 20000;

if (replacementProp != null) {
Expand Down
5 changes: 1 addition & 4 deletions TLM/TLM/Patch/_NetLane/RenderInstancePatch.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
using ColossalFramework.Math;
using CSUtil.Commons;
using CSUtil.Commons;
using Harmony;
using System.Collections.Generic;
using System.Linq;
using System.Reflection.Emit;
using TrafficManager.Manager.Impl;
using UnityEngine;

namespace TrafficManager.Patch._NetLane
{
[HarmonyPatch(typeof(NetLane), "RenderInstance")]
public static class RenderInstancePatch
{

/// <summary>
/// Overrides the PropInfo retrieval method for Netlane.RenderInstancePatch to allow the LanePropManager to override the prop selection
/// </summary>
Expand Down
1 change: 0 additions & 1 deletion TLM/TLM/TLM.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,6 @@
<Compile Include="Manager\Impl\SegmentEndManager.cs" />
<Compile Include="Patch\_CommonBuildingAI\SimulationStepPatch.cs" />
<Compile Include="Patch\_HumanAI\ArriveAtDestinationPatch.cs" />
<Compile Include="Patch\_NetLane\PropRenderInstancePatch.cs" />
<Compile Include="Patch\_NetLane\RenderInstancePatch.cs" />
<Compile Include="Patch\_RoadBaseAI\GetTrafficLightNodeStatePatch.cs" />
<Compile Include="Patch\_TrainTrackBaseAI\LevelCrossingSimulationStepPatch.cs" />
Expand Down
27 changes: 19 additions & 8 deletions TLM/TLM/Util/TextureUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,14 @@ public static Texture2D DrawText(ref Texture2D tx, string sText, Font myFont, in
RenderTexture.ReleaseTemporary(rt);
fontTx = img2;

int totalWidth = 0;
for (int i = 0; i < cText.Length; i++) {
myFont.GetCharacterInfo(cText[i], out ci, size);
totalWidth += ci.glyphWidth;
}

int x, y, w, h;
int posX = startX;
int posX = startX - (totalWidth/2);

for (int i = 0; i < cText.Length; i++)
{
Expand All @@ -87,19 +93,24 @@ public static Texture2D DrawText(ref Texture2D tx, string sText, Font myFont, in
Color[] cChar = fontTx.GetPixels(x, y, w, h);
for (int row = 0; row < h; ++row)
{
Array.Reverse(cChar, row * w, w);
Array.Reverse(cChar, row * w, w);
}
Array.Reverse(cChar);
var blah = cChar.ToList().ConvertAll<Color32>(pixel => ( pixel.a > 0 ) ? Color.red : Color.white).ToArray();
var blah = cChar.ToList().ConvertAll<Color32>(pixel => ( pixel.a > 0 ) ? Color.black : Color.clear).ToArray();

for (int j = 0; j < h; j++) {
for (int k = 0; k < w; k++) {
var index = ( j * w ) + k;
if (blah[index].a > 0) {
tx.SetPixel(posX + k, startY + j, blah[index]);
}
}
}

tx.SetPixels32(posX, startY, w, h, blah);
tx.Apply();
posX += ci.advance;
posX += ci.glyphWidth;
}

byte[] bytes = tx.EncodeToPNG();
File.WriteAllBytes(Application.dataPath + $"/../{sText}.png", bytes);

return tx;
}
}
Expand Down

0 comments on commit b4a82ab

Please sign in to comment.