public void Colors_Stay_In_Scope() { // this test will only pass when debugging // which I believe has something to do with the impl of the Console.BackfroundColor getter // when there is no actual console window open // see https://docs.microsoft.com/en-us/dotnet/api/system.console.backgroundcolor?view=netcore-3.1#remarks // "A get operation for a Windows-based application, in which a console does not exist, returns ConsoleColor.Black." if (!Debugger.IsAttached) { Assert.Pass(); } // arrange Console.ForegroundColor = ConsoleColor.White; Console.BackgroundColor = ConsoleColor.Black; // act, assert using (var colorScope = new ColorScope( foreColor: ConsoleColor.Gray, backColor: ConsoleColor.Cyan)) { Assert.AreEqual(ConsoleColor.Gray, Console.ForegroundColor); Assert.AreEqual(ConsoleColor.Cyan, Console.BackgroundColor); } Assert.AreEqual(ConsoleColor.White, Console.ForegroundColor); Assert.AreEqual(ConsoleColor.Black, Console.BackgroundColor); }
public void Warning(string logmessage) { if (_warning) { using (var colorScope = new ColorScope(ConsoleColor.Black, ConsoleColor.Yellow)) Console.WriteLine("[WARN]\t" + logmessage); } }
public void Error(string logmessage) { if (_error) { using (var colorScope = new ColorScope(ConsoleColor.Black, ConsoleColor.Red)) Console.WriteLine("[ERROR]\t" + logmessage); } }
/// <summary> /// Can be used within a using-block that will set the given colors again /// when disposed. /// </summary> /// <param name="background"></param> /// <param name="foreground"></param> /// <returns></returns> public static IDisposable WithColorScope(ConsoleColor background, ConsoleColor foreground) { semaphore.Wait(); var cc = new ColorScope(background, foreground); semaphore.Release(); return(cc); }
/// <summary> /// Can be used within a using-block that will restore the previous colors /// after disposal. /// </summary> /// <returns></returns> public static IDisposable WithColorScope() { semaphore.Wait(); var cc = new ColorScope(Console.BackgroundColor, Console.ForegroundColor); semaphore.Release(); return(cc); }
protected override void OnProcessOutputSchema(MutableObject newSchema) { foreach (var entry in ColorScope.GetEntries(newSchema)) { ColorTarget.SetValue(Color.magenta, entry); } Router.TransmitAllSchema(newSchema); }
protected override MutableObject Mutate(MutableObject mutable) { foreach (var entry in ColorScope.GetEntries(mutable)) { ColorTarget.SetValue(ColorLerp(mutable, FromColor.GetValue(entry), ToColor.GetValue(entry), Proportion.GetValue(entry)), entry); } return(mutable); }
protected override void DrawPropertyLayout(GUIContent label) { var colorAttribute = Property.GetAttribute <GUIColorLookupAttribute>(); ColorScope.Begin(colorAttribute.Main, GUI.contentColor, colorAttribute.Fields); CallNextDrawer(label); ColorScope.End(); }
static void Main() { Console.WriteLine("Welcome to the example!"); using (var colorScope = new ColorScope( foreColor: ConsoleColor.DarkGray, backColor: ConsoleColor.White)) { string name = _console.Prompt("Enter your name: "); Console.WriteLine($"Hi {name}!"); } Console.WriteLine("Have a great day."); Console.ReadLine(); }
static public void OnGUI <T>(ref T value, string displayName = null) { var op = new[] { GUILayout.MinWidth(70f) }; using (var h = new GUILayout.HorizontalScope()) { GUILayout.Label(displayName == null ? nameof(value) : displayName); var hash = value.GetHashCode(); var target = value.ToString(); var hasUnparsedStr = stringTable.ContainsKey(hash); if (hasUnparsedStr) { target = stringTable[hash]; } var color = hasUnparsedStr ? Color.red : GUI.color; using (var cs = new ColorScope(color)) { var ret = GUILayout.TextField(target, op); var newValue = default(T); var canParse = false; try { newValue = (T)Convert.ChangeType(ret, typeof(T)); canParse = newValue.ToString() == ret; } catch (Exception) { } if (canParse) { value = newValue; if (hasUnparsedStr) { stringTable.Remove(hash); } } else { stringTable[hash] = ret; } } } }
static object StandardField <T>(T v, ref string unparsedStr) { object ret = v; var type = typeof(T); // フォーカスが外れたときにバリデーションしたい(unparsedStr=nullにすることでv.ToString()に更新される) // うまい実装がわからないので簡易的にタブ時に行う // マウスイベントも対応したいがこれより前のGUIでイベント食われたとき対応できないので一旦無しで if (Event.current.keyCode == KeyCode.Tab) { unparsedStr = null; } var hasUnparsedStr = !string.IsNullOrEmpty(unparsedStr); var canParse = false; try { canParse = Convert.ChangeType(unparsedStr, type).ToString() == unparsedStr; } catch (Exception) { } var color = (hasUnparsedStr && !canParse) ? Color.red : GUI.color; using (var cs = new ColorScope(color)) { unparsedStr = GUILayout.TextField(hasUnparsedStr ? unparsedStr : v.ToString(), GUILayout.MinWidth(70f)); try { ret = Convert.ChangeType(unparsedStr, type); if (ret.ToString() == unparsedStr) { unparsedStr = null; } } catch (Exception) { } } return(ret); }
private float DrawHeader(bool playing) { var lineWidth = 2.0f; var lineColor = new Color(0.3f, 0.3f, 0.3f); EditorGUILayout.Space(); DrawFilePicker(playing); GUILayout.Space(EditorGUIUtility.standardVerticalSpacing); DrawZonePicker(); GUILayout.Space(EditorGUIUtility.standardVerticalSpacing); DrawSpawn(); EditorGUILayout.Space(); var top = GUILayoutUtility.GetLastRect().yMax; using (ColorScope.Color(lineColor)) GUI.Box(new Rect(0, top, position.width, lineWidth), GUIContent.none); return(top + lineWidth); }
static object StandardField <T>(T v, ref string unparsedStr, params GUILayoutOption[] options) { object ret = v; var type = typeof(T); // validate when unfocused (unparsedStr=null then v.ToString will to be set)) if (_forcusChecker.IsChanged()) { unparsedStr = null; } var hasUnparsedStr = !string.IsNullOrEmpty(unparsedStr); var canParse = false; try { canParse = Convert.ChangeType(unparsedStr, type).ToString() == unparsedStr; } catch (Exception) { } var color = (hasUnparsedStr && !canParse) ? Color.red : GUI.color; using (var cs = new ColorScope(color)) { unparsedStr = GUILayout.TextField(hasUnparsedStr ? unparsedStr : v.ToString(), options.Concat(new[] { GUILayout.MinWidth(70f) }).ToArray()); try { ret = Convert.ChangeType(unparsedStr, type); if (ret.ToString() == unparsedStr) { unparsedStr = null; } } catch (Exception) { } } return(ret); }