Skip to content

Commit

Permalink
Merge pull request #97 from ashblue/feature/dark-mode-fix
Browse files Browse the repository at this point in the history
Feature/dark mode fix
  • Loading branch information
ashblue authored Nov 9, 2024
2 parents 9d8dc6a + 321e47e commit 244a9ed
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 39 deletions.
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
using System.Linq;
using CleverCrow.Fluid.BTs.TaskParents;
using UnityEditor;
using UnityEngine;

namespace CleverCrow.Fluid.BTs.Trees.Editors {
public interface IGraphNodePrinter {
void Print (GraphNode node);
}

public class GraphNodePrinter : IGraphNodePrinter {
private Texture2D _verticalBottom;
private Texture2D _verticalTop;

private static Color LineColor => EditorGUIUtility.isProSkin ? Color.white : Color.black;

public void Print (GraphNode node) {
var rect = new Rect(node.Position, node.Size);
GUI.Box(rect, node.Task.Name);

PaintVerticalBottom(node, rect);

if (!(node.Task is TaskRoot)) {
Expand All @@ -23,15 +26,15 @@ public void Print (GraphNode node) {
}

private void PaintVerticalBottom (GraphNode node, Rect nodeRect) {
if (_verticalBottom == null) _verticalBottom = CreateTexture(1, node.VerticalConnectorBottomHeight, Color.black);
if (_verticalBottom == null) _verticalBottom = CreateTexture(1, node.VerticalConnectorBottomHeight, LineColor);
var verticalBottomRect = new Rect(nodeRect);
verticalBottomRect.x += node.Size.x / 2 - 0.5f;
verticalBottomRect.y += node.Size.y;
GUI.Label(verticalBottomRect, _verticalBottom);
}

private void PaintVerticalTop (GraphNode node, Rect nodeRect) {
if (_verticalTop == null) _verticalTop = CreateTexture(1, node.VerticalConnectorTopHeight, Color.black);
if (_verticalTop == null) _verticalTop = CreateTexture(1, node.VerticalConnectorTopHeight, LineColor);
var verticalTopRect = new Rect(nodeRect);
verticalTopRect.x += node.Size.x / 2 - 0.5f;
verticalTopRect.y -= node.VerticalConnectorTopHeight;
Expand All @@ -42,7 +45,7 @@ private Texture2D CreateTexture (int width, int height, Color color) {
var texture = new Texture2D(width, height, TextureFormat.ARGB32, false);
texture.SetPixels(Enumerable.Repeat(color, width * height).ToArray());
texture.Apply();

return texture;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,25 @@ public NodeBoxStyle (Color32 border, Color background) {
texture.SetPixels(1, 1, 17, 17,
Enumerable.Repeat(background, 17 * 17).ToArray());
texture.Apply();
Style = new GUIStyle(GUI.skin.box) {

Style = new GUIStyle {
border = new RectOffset(1, 1, 1, 1),
normal = {
background = texture,
textColor = Color.white,
},
alignment = TextAnchor.MiddleCenter,
fontSize = 12,
padding = new RectOffset(5, 5, 5, 5),
};
}

private static Texture2D CreateTexture (int width, int height, Color color) {
var texture = new Texture2D(width, height, TextureFormat.ARGB32, false);
texture.SetPixels(Enumerable.Repeat(color, width * height).ToArray());
texture.Apply();

return texture;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Linq;
using CleverCrow.Fluid.BTs.TaskParents;
using UnityEditor;
using UnityEngine;

namespace CleverCrow.Fluid.BTs.Trees.Editors {
Expand All @@ -16,20 +17,21 @@ public class NodePrintController {
private Texture2D _verticalTop;

private static GuiStyleCollection Styles => BehaviorTreePrinter.SharedStyles;
private static Color LineColor => EditorGUIUtility.isProSkin ? Color.white : Color.black;

public NodePrintController (VisualTask node) {
_node = node;
_box = node.Box;
_divider = node.Divider;
_iconMain = new TextureLoader(_node.Task.IconPath);
}

public void Print (bool taskIsActive) {
if (!(_node.Task is TaskRoot)) PaintVerticalTop();
_faders.Update(taskIsActive);

PaintBody();

if (_node.Children.Count > 0) {
PaintDivider();
PaintVerticalBottom();
Expand All @@ -38,23 +40,23 @@ public void Print (bool taskIsActive) {

private void PaintBody () {
var prevBackgroundColor = GUI.backgroundColor;

var rect = new Rect(
_box.GlobalPositionX + _box.PaddingX,
_box.GlobalPositionX + _box.PaddingX,
_box.GlobalPositionY + _box.PaddingY,
_box.Width - _box.PaddingX,
_box.Width - _box.PaddingX,
_box.Height - _box.PaddingY);

if (_node.Task.HasBeenActive) {
GUI.backgroundColor = _faders.BackgroundFader.CurrentColor;
GUI.Box(rect, GUIContent.none, Styles.BoxActive.Style);
GUI.backgroundColor = prevBackgroundColor;

PrintLastStatus(rect);
} else {
GUI.Box(rect, GUIContent.none, Styles.BoxInactive.Style);
}

PrintIcon();

Styles.Title.normal.textColor = _faders.TextFader.CurrentColor;
Expand Down Expand Up @@ -87,57 +89,57 @@ private void PrintIcon () {

private void PaintDivider () {
const int graphicSizeIncrease = 5;

if (_dividerGraphic == null) {
_dividerGraphic = CreateTexture(
(int)_divider.Width + graphicSizeIncrease,
1,
Color.black);
(int)_divider.Width + graphicSizeIncrease,
1,
LineColor);
}

var position = new Rect(
_divider.GlobalPositionX + _box.PaddingY / 2 + _node.DividerLeftOffset - 2,
// @TODO Should not need to offset this
_divider.GlobalPositionX + _box.PaddingX / 2 + _node.DividerLeftOffset - 2,
_divider.GlobalPositionY + _box.PaddingY / 2,
_divider.Width + graphicSizeIncrease,
10);

_divider.Width + graphicSizeIncrease,
// @NOTE I have no clue why 3 works here...
3);

GUI.Label(position, _dividerGraphic);
}

private void PaintVerticalBottom () {
if (_verticalBottom == null) {
_verticalBottom = CreateTexture(1, (int)_box.PaddingY, Color.black);
_verticalBottom = CreateTexture(1, (int)_box.PaddingY, LineColor);
}

var position = new Rect(
_box.GlobalPositionX + _node.Width / 2 + _box.PaddingX - 2,
_box.GlobalPositionX + _node.Width / 2 + _box.PaddingX - 2,
_box.GlobalPositionY + _node.Height + _box.PaddingY - 1,
100,
_box.PaddingY - 1);
100,
_box.PaddingY);

GUI.Label(position, _verticalBottom);
}

private void PaintVerticalTop () {
if (_verticalTop == null) {
_verticalTop = CreateTexture(1, Mathf.RoundToInt(_box.PaddingY / 2), Color.black);
_verticalTop = CreateTexture(1, Mathf.RoundToInt(_box.PaddingY / 2), LineColor);
}

var position = new Rect(
_box.GlobalPositionX + _node.Width / 2 + _box.PaddingX - 2,
_box.GlobalPositionX + _node.Width / 2 + _box.PaddingX - 2,
_box.GlobalPositionY + _box.PaddingY / 2,
100,
10);
100,
_box.PaddingY / 2);

GUI.Label(position, _verticalTop);
}

private static Texture2D CreateTexture (int width, int height, Color color) {
var texture = new Texture2D(width, height, TextureFormat.ARGB32, false);
texture.SetPixels(Enumerable.Repeat(color, width * height).ToArray());
texture.Apply();

return texture;
}
}
Expand Down

0 comments on commit 244a9ed

Please sign in to comment.