Skip to content

Commit

Permalink
initial
Browse files Browse the repository at this point in the history
  • Loading branch information
beatthat committed May 11, 2020
1 parent 178643e commit 07bb22a
Show file tree
Hide file tree
Showing 22 changed files with 1,324 additions and 0 deletions.
20 changes: 20 additions & 0 deletions .gitattributes
@@ -0,0 +1,20 @@
# Set the default behavior, in case people don't have core.autocrlf set.
* text=auto

# Explicitly declare text files you want to always be normalized and converted
# to native line endings on checkout.
*.c text
*.h text
*.cs text

# assumes that unity projects have serialization to force text
*.unity text
*.prefab text
*.asset text

# Declare files that will always have CRLF line endings on checkout.
*.sln text eol=crlf

# Denote all files that are truly binary and should not be modified.
*.png binary
*.jpg binary
45 changes: 45 additions & 0 deletions .gitignore
@@ -0,0 +1,45 @@
/[Ll]ibrary/
/[Tt]emp/
/[Oo]bj/
/[Bb]uild/
/[Bb]uilds/
/Assets

# Visual Studio 2015 cache directory
/.vs/
**/.vs

# Autogenerated VS/MD/Consulo solution and project files
ExportedObj/
.consulo/
*.csproj
*.unityproj
*.sln
*.suo
*.tmp
*.user
*.userprefs
*.pidb
*.booproj
*.svd
*.pdb

npm-debug.log
*.tgz

# Unity3D generated meta files
*.pidb.meta

# Unity3D Generated File On Crash Reports
sysinfo.txt

# Builds
*.apk
*.unitypackage

node_modules
package-lock.json
unpm-local.json
unpm-packages.json

config.doxygen
1 change: 1 addition & 0 deletions .npmrc
@@ -0,0 +1 @@
package-lock=false
21 changes: 21 additions & 0 deletions LICENSE
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2017 larry kirschner

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
15 changes: 15 additions & 0 deletions README.md
@@ -1 +1,16 @@
# ui-rects-to-graphics

Use to create a single Unity3d UI Graphic component with geometry composed of a set of RectTransforms.

## Usage

TBD create an example and maybe an image here

## Install

From your unity project folder:

npm init
npm install --save beatthat/ui-rects-to-graphics

The package and all its dependencies will be installed under Assets/Plugins/packages.
218 changes: 218 additions & 0 deletions Runtime/ui-rects-to-graphics/RectsToGraphic.cs
@@ -0,0 +1,218 @@
using BeatThat.GetComponentsExt;
using BeatThat.Pools;
using BeatThat.Rects;
using BeatThat.SafeRefs;
using BeatThat.UIGeometryUtils;
using BeatThat.UIRectTransformEvents;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;

namespace BeatThat.UIRectsToGraphics
{
public class RectsToGraphic : Graphic
{
public List<RectTransform> m_rects;

[Tooltip("set TRUE connect the rects into a single contiguous graphic")]
public bool m_connectEnds = true;
public bool m_debugVerts = false;

[Tooltip("set TRUE to block raycasts everywhere EXCEPT the rect areas.")]
public bool m_raycastsPassThroughRects = true;

private IList<RectTransform> rects { get { return m_rects; } }

public void SetRects(IEnumerable<RectTransform> rects)
{
var localRects = m_rects?? (m_rects = new List<RectTransform>());
localRects.Clear();
localRects.AddRange(rects);
UpdateRects();
}

override public bool Raycast(Vector2 sp, Camera eventCamera)
{
if(!this.raycastTarget) {
return false;
}
if(!RectTransformUtility.RectangleContainsScreenPoint(this.rectTransform, sp, eventCamera)) {
// if the screen point doesn't hit the container rect, then ignore it
return false;
}
if(!m_raycastsPassThroughRects) {
return true;
}
var rectList = this.rectRefs;
if(rectList == null) {
return true;
}
foreach(var r in rectList) {
if(r.value == null) {
continue;
}
if(RectTransformUtility.RectangleContainsScreenPoint(r.value, sp, eventCamera)) {
return false;
}
}
return true;
}

protected override void OnPopulateMesh(VertexHelper vh)
{
if(m_debugVerts) {
using(vh.DebugStart()) {
DoOnPopulateMesh(vh);
}
}
else {
DoOnPopulateMesh(vh);
}
}

private void DoOnPopulateMesh(VertexHelper vh)
{
vh.Clear();
var thisRT = this.rectTransform;
var thisRect = this.rectTransform.rect;
int vOff = 0;
var rectArray = this.rectRefs;
if(rectArray == null || rectArray.Count == 0) {
return;
}
using(var quad = ArrayPool<UIVertex>.Get(4)) {
Rect prevRect = default(Rect);
for(var i = 0; i < rectArray.Count; i++) {
var c = rectArray[i].value;
if(c == null) {
continue;
}
var curRect = thisRT.InverseTransformRect(c);
if(i > 0 && m_connectEnds) {
var uiVert = UIVertex.simpleVert;
uiVert.color = this.color;
var zPos = thisRT.position.z;
uiVert.position = new Vector3(prevRect.xMax, prevRect.yMin, zPos);
uiVert.uv0 = new Vector2(0, 0);
quad.array[0] = uiVert;
uiVert.position = new Vector3(prevRect.xMax, prevRect.yMax, zPos);
uiVert.uv0 = new Vector2(0, 1);
quad.array[1] = uiVert;
uiVert.position = new Vector3(curRect.xMin, curRect.yMax, zPos);
uiVert.uv0 = new Vector2(1, 1);
quad.array[2] = uiVert;
uiVert.position = new Vector3(curRect.xMin, curRect.yMin, zPos);
uiVert.uv0 = new Vector2(1, 0);
quad.array[3] = uiVert;
vOff += vh.AddQuadClipped(quad.array, thisRect, vOff);
}
vOff += vh.AddRectClipped(curRect, this.color, thisRect, vOff);
prevRect = curRect;
}
}
}


private void UnregisterRects()
{
var list = this.rectRefs;
if(list == null) {
return;
}
for(int i = 0; i < list.Count; i++) {
var c = list[i].value;
if(c == null) {
continue;
}
var e = c.GetComponent<RectTransformEvents>();
if(e == null) {
continue;
}
e.onScreenRectChanged.RemoveListener(this.onRectUpdatedAction);
}
}

private void UpdateRects()
{
UnregisterRects();
var list = this.rectRefs;
if(list == null) {
list = new List<SafeRef<RectTransform>>();
}
else {
list.Clear();
}
foreach (var c in this.rects) {
list.Add (new SafeRef<RectTransform> (c));
if (c == null) {
continue;
}
var e = c.AddIfMissing<RectTransformEvents> ();
e.onScreenRectChanged.RemoveListener (this.onRectUpdatedAction);
e.onScreenRectChanged.AddListener (this.onRectUpdatedAction);
}
this.rectRefs = list;
SetVerticesDirty();
}

private void OnRectUpdated()
{
SetVerticesDirty();
}
private UnityAction onRectUpdatedAction { get { return m_onRectUpdatedAction?? (m_onRectUpdatedAction = this.OnRectUpdated); } }
private UnityAction m_onRectUpdatedAction;

#if UNITY_EDITOR
override protected void OnValidate()
{
UpdateRects();
base.OnValidate();
}
#endif

override protected void OnDestroy()
{
UnregisterRects();
base.OnDestroy();
}

override protected void OnEnable()
{
UpdateRects();
base.OnEnable();
}

void OnDrawGizmosSelected()
{
var rectList = this.rectRefs;
if(rectList == null) {
return;
}
var saveColor = Gizmos.color;
foreach(var c in rectList) {
if(c.value == null) {
continue;
}
var r = c.value.TransformRect(c.value.rect);
var fillColor = Color.cyan;
fillColor.a = 0.15f;
Gizmos.color = fillColor;
Gizmos.DrawCube((Vector3)r.center, new Vector3(r.width, r.height));
Gizmos.color = Color.blue;
Gizmos.DrawLine((Vector3)r.min, new Vector3(r.xMin, r.yMax));
Gizmos.DrawLine(new Vector3(r.xMin, r.yMax), (Vector3)r.max);
Gizmos.DrawLine((Vector3)r.max, new Vector3(r.xMax, r.yMin));
Gizmos.DrawLine(new Vector3(r.xMax, r.yMin), (Vector3)r.min);
}
Gizmos.color = saveColor;
}

private List<SafeRef<RectTransform>> rectRefs { get; set; }
}
}





13 changes: 13 additions & 0 deletions Runtime/ui-rects-to-graphics/RectsToGraphic.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Empty file added docs/npm.md
Empty file.
1 change: 1 addition & 0 deletions index.js
@@ -0,0 +1 @@
module.exports = function() { throw new Error('Not a javascript module'); };

0 comments on commit 07bb22a

Please sign in to comment.