Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scale unity GUI #1152

Merged
merged 18 commits into from
Sep 7, 2021
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 20 additions & 7 deletions TLM/CSUtil.Commons/Log.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ namespace CSUtil.Commons {
using System;
using System.Diagnostics;
using System.IO;
using System.Threading;
using System.Linq;
using UnityEngine;
using Debug = UnityEngine.Debug;

Expand Down Expand Up @@ -40,9 +40,9 @@ namespace CSUtil.Commons {
public static class Log {
private static readonly object LogLock = new object();

// TODO refactor log filename to configuration
private static readonly string LogFilename
= Path.Combine(Application.dataPath, "TMPE.log");
private const string LOG_FILE_NAME = "TMPE.log";

private static readonly string LogFilePath;

private enum LogLevel {
Trace,
Expand All @@ -56,8 +56,21 @@ private enum LogLevel {

static Log() {
try {
if (File.Exists(LogFilename)) {
File.Delete(LogFilename);
string dir = Application.dataPath;
LogFilePath = Path.Combine(dir, LOG_FILE_NAME);
if (File.Exists(LogFilePath)) {
File.Delete(LogFilePath); // delete old file to avoid confusion.
}

var args = Environment.GetCommandLineArgs().ToList();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to convert ToList() here since you don't use it as a list further down in the method.
Better to keep it a string array and use Array.FindIndex(args, a => a == "-logFile") in the next line.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is not part of this review. I updated the branch

int index = args.IndexOf("-logFile");
if (index >= 0) {
dir = args[index + 1];
dir = Path.GetDirectoryName(dir); // drop output_log.txt
LogFilePath = Path.Combine(dir, LOG_FILE_NAME);
if (File.Exists(LogFilePath)) {
File.Delete(LogFilePath);
}
}
}
catch (Exception e) {
Expand Down Expand Up @@ -189,7 +202,7 @@ public static void NotImpl(string what) {

private static void LogToFile(string log, LogLevel level) {
lock(LogLock){
using (StreamWriter w = File.AppendText(LogFilename)) {
using (StreamWriter w = File.AppendText(LogFilePath)) {
long secs = _sw.ElapsedTicks / Stopwatch.Frequency;
long fraction = _sw.ElapsedTicks % Stopwatch.Frequency;
w.WriteLine(
Expand Down
7 changes: 3 additions & 4 deletions TLM/TLM/U/UIScaler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,14 @@ private static float BaseResolutionY {
public static float MaxWidth {
get {
float ret = Config.GuiScaleToResolution ? BaseResolutionX : Screen.width;
return ret / Config.GuiScale;
return ret / (Config.GuiScale * 0.01f);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no magic numbers plz. make the 0.01f a variable.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kianzarrin I don't remember what range GuiScale is (probably 30...200+) but if it's percentage then it does not look right to me. For 100 it will return ret/1f which is ok, but for 200 it will be ret/2f, so 2x smaller?? 🤔

}
}

public static float MaxHeight {
get {
float ret = Config.GuiScaleToResolution ? BaseResolutionY : Screen.height;
return ret / Config.GuiScale;
return ret / (Config.GuiScale * 0.01f);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same var

}
}

Expand All @@ -70,8 +70,7 @@ public static float UIAspectScale {

public static float UIScale => GlobalConfig.Instance.Main.GuiScale * 0.01f;


public static Matrix4x4 ScaleMatrix => Matrix4x4.Scale(Vector3.one * UIScaler.UIScale);
public static Matrix4x4 ScaleMatrix => Matrix4x4.Scale(Vector3.one * UIAspectScale);

public static Vector2 MousePosition {
get {
Expand Down
6 changes: 4 additions & 2 deletions TLM/TLM/UI/SubTools/SpeedLimits/SpeedLimitsTool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ namespace TrafficManager.UI.SubTools.SpeedLimits {
using UnityEngine;
using static TrafficManager.Util.Shortcuts;
using TrafficManager.Util.Extensions;
using TrafficManager.U;

public class SpeedLimitsTool : LegacySubTool {
public const int
Expand Down Expand Up @@ -142,6 +143,8 @@ public override void OnSecondaryClickOverlay() {

public override void OnToolGUI(Event e) {
base.OnToolGUI(e);
var oldMatrix = GUI.matrix;
GUI.matrix = UIScaler.ScaleMatrix;
Copy link
Contributor

@DaEgi01 DaEgi01 Sep 3, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is this matrix switching thing?
currently on the train, can't look it up easily.
is GUI.matrix our code?
if not and we can't change it than this is ok.
if its ours we really should change it :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GUI.matrix is Unity code for immediate gui, works pretty much like GUI.color, but obviously changes different thing 😉

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

kk .. in that case the code is fine.


string unitTitle = string.Format(
" ({0})",
Expand Down Expand Up @@ -176,8 +179,7 @@ public override void OnToolGUI(Event e) {
&& defaultsWindowRect.Contains(
Event.current.mousePosition));

// overlayHandleHovered = false;
// ShowSigns(false);
GUI.matrix = oldMatrix;
}

private static NetLane[] laneBuffer => NetManager.instance.m_lanes.m_buffer;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ namespace TrafficManager.UI.SubTools.TimedTrafficLights {
using TrafficManager.API.TrafficLight;
using TrafficManager.Manager.Impl;
using TrafficManager.State;
using TrafficManager.U;
using TrafficManager.UI.MainMenu.OSD;
using TrafficManager.UI.Textures;
using TrafficManager.Util;
Expand Down Expand Up @@ -308,6 +309,8 @@ public override void OnPrimaryClickOverlay() {

public override void OnToolGUI(Event e) {
base.OnToolGUI(e);
var oldMatrix = GUI.matrix;
GUI.matrix = UIScaler.ScaleMatrix;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as for speedlimitstool.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

all fine


switch (ttlToolMode_) {
case TTLToolMode.SelectNode: {
Expand All @@ -330,6 +333,8 @@ public override void OnToolGUI(Event e) {
break;
}
}

GUI.matrix = oldMatrix;
}

public override void RenderOverlay(RenderManager.CameraInfo cameraInfo) {
Expand Down Expand Up @@ -1125,10 +1130,6 @@ private void GuiTimedTrafficLights() {

_cursorInSecondaryPanel = _windowRect.Contains(Event.current.mousePosition);

GUI.matrix = Matrix4x4.TRS(
new Vector3(0, 0, 0),
Quaternion.identity,
new Vector3(1, 1, 1)); // revert scaling
ShowGUI();
}

Expand Down