//Aligning all elements and calculating hover public void Update() { //if (updateTime > 0.1f && updateTimeLeft > 0) { updateTimeLeft-=Time.deltaTime; return; } //updateTimeLeft = updateTime; //if (!element) element = GetComponent<InstantGuiElement>(); pointed = null; //getting game view size #if UNITY_EDITOR /* if (!gameView) { System.Type type = System.Type.GetType("UnityEditor.GameView,UnityEditor"); System.Reflection.MethodInfo GetMainGameView = type.GetMethod("GetMainGameView",System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static); gameView = (UnityEditor.EditorWindow)GetMainGameView.Invoke(null,null); } width = (int)gameView.position.width; height = (int)gameView.position.height-16; */ //It is not possible to use Screen.height from an OnInspectorGUI (returns inspector height) string[] res = UnityEditor.UnityStats.screenRes.Split('x'); width = int.Parse(res[0]); height = int.Parse(res[1]); #else width = Screen.width; height = Screen.height; #endif Profiler.BeginSample ("CheckChildren"); element.CheckChildren(); Profiler.EndSample (); Profiler.BeginSample ("Align"); element.Align(); Profiler.EndSample (); Profiler.BeginSample ("Prevent Zero Size"); //element.PreventZeroSize(); Profiler.EndSample (); Profiler.BeginSample ("Point"); element.Point(); Profiler.EndSample (); Profiler.BeginSample ("Action"); element.Action(); Profiler.EndSample (); Profiler.BeginSample ("Style"); element.ApplyStyle(); Profiler.EndSample (); oldScreenWidth = Screen.width; oldScreenHeight = Screen.height; }
public override void Align () { base.Align(); //setting first shown value if (slider!=null) { slider.min = 0; slider.max = Mathf.Max(0, labels.Length - elements.Length); slider.shownValue = elements.Length; //scrolling slider slider.value -= Input.GetAxisRaw("Mouse ScrollWheel")*10; slider.value = Mathf.Clamp(slider.value, slider.min, slider.max); firstShown = (int)slider.value; } //calculating number of lines int linesNum; if (lineHeight <= 0) linesNum = elements.Length; else { linesNum = Mathf.FloorToInt( (absolute.bottom - absolute.top)*1.0f/lineHeight ); linesNum = Mathf.Max(linesNum, 0); } //re-creating elements if line num changed if (elements.Length != linesNum) { //destroying overcount for (int i=elements.Length-1;i>=linesNum;i--) if (elements[i]!=null) StartCoroutine(elements[i].YieldAndDestroy()); //DestroyImmediate(elements[i].gameObject); //resizing array InstantGuiElement[] newElements = new InstantGuiElement[linesNum]; int count = Mathf.Min(linesNum, elements.Length); for (int i=0;i<count;i++) newElements[i] = elements[i]; elements = newElements; //creating undefened elements for (int i=0;i<elements.Length;i++) if (!elements[i]) elements[i] = InstantGuiElement.Create("ListElement", typeof(InstantGuiElement), this); } //setting text and position for (int i=0;i<elements.Length;i++) { if (i+firstShown < labels.Length && firstShown >= 0) elements[i].text = labels[i+firstShown]; else elements[i].text = ""; elements[i].offset = new InstantGuiElementPos(0,-sliderMargin,0,0); elements[i].relative = new InstantGuiElementPos(0,100,(int)((100.0f/linesNum)*i),(int)((100.0f/linesNum)*(i+1))); elements[i].editable = false; //elements[i].style = elementStyle; } }
public override void OnInspectorGUI () { InstantGuiTabs script = (InstantGuiTabs)target; base.OnInspectorGUI(); //EditorGUILayout.Space(); script.guiElementProps = EditorGUILayout.Foldout(script.guiElementProps, "Tabs"); //some non-gui actions for (int i=0; i<script.tabs.Length; i++) { //creating field if it does not exists if (!script.fields[i]) script.fields[i] = script.CreateField(); //fields names if (script.fields[i].transform.name.Length==0) script.fields[i].transform.name = script.tabs[i].transform.name + "_Field"; } if (script.guiElementProps) { EditorGUI.indentLevel = 1; script.selected = EditorGUILayout.IntField("Selected:", script.selected); //array Rect rect = GUILayoutUtility.GetRect (150, 16, "TextField"); rect.width = rect.width*0.5f-40; EditorGUI.LabelField(rect, "Tabs:"); rect.x += rect.width; EditorGUI.LabelField(rect, "Fields:"); script.tabs = InstantGuiInspector.DrawElementsTable(script.tabs, script.fields); if (script.tabs.Length != script.fields.Length) //changing fields count (creating if necessary) { InstantGuiElement[] newFields = new InstantGuiElement[script.tabs.Length]; for (int j=0; j<script.tabs.Length; j++) { if (j>=script.fields.Length || !script.fields[j]) { newFields[j] = script.CreateField();continue; } newFields[j] = script.fields[j]; } script.fields = newFields; } EditorGUI.indentLevel = 0; } InstantGuiInspector.DrawActivator ("On Checked:", script.onChecked); InstantGuiInspector.DrawActivator ("On Unchecked:", script.onUnchecked); }
public void InvertAlign ( InstantGuiElement element ) { int tmp = element.relative.top; element.relative.top = 100-element.relative.bottom; element.relative.bottom = 100-tmp; tmp = element.offset.top; element.offset.top = -element.offset.bottom; element.offset.bottom = -tmp; invertedAlign = !invertedAlign; element.Align(); }
static public InstantGuiElement Create ( string name , string style , System.Type type , InstantGuiElement parent ){ //finding gui parent if (InstantGui.instance==null) InstantGui.instance = (InstantGui)FindObjectOfType(typeof(InstantGui)); if (InstantGui.instance==null) { InstantGui.CreateRoot(); parent = InstantGui.instance.element; } GameObject obj = new GameObject (name); //the object component will be assigned to //adding component InstantGuiElement element = (InstantGuiElement)obj.AddComponent(type); //parenting and setting styleset if (parent!=null) { element.transform.parent = parent.transform; element.styleSet = parent.styleSet; //do not assign parent to element! It will get it automaticly } //resetting transform if (obj.transform.parent!=null) obj.transform.localPosition = new Vector3(0,0,obj.transform.parent.localPosition.z + 1); else obj.transform.localPosition = new Vector3(0,0,0); obj.transform.localScale = new Vector3(0,0,1); //setting style element.styleName = style; if (element.styleSet!=null) for (int i=0; i<element.styleSet.styles.Length; i++) if (element.styleSet.styles[i].name == element.styleName) element.style = element.styleSet.styles[i]; //setting default size if (element.style!=null) { element.relative.Set(element.style.relative); element.offset.Set(element.style.offset); element.layerOffset = element.style.layerOffset; } //pureGui.Update(); return element; }
public virtual void Align() { if (!this) { return; //element could be deleted to this time! } //assigning vars if they do not exist if (guiTexts == null) { guiTexts = new GUIText[0]; } //using style position if (styleSet != null && useStylePlacement) { //if (style==null) style = styleSet.FindStyle(styleName, styleNum); if (style != null) { relative.Set(style.relative); offset.Set(style.offset); } } else { if (relative.isStyle) { relative = new InstantGuiElementPos(relative); } if (offset.isStyle) { offset = new InstantGuiElementPos(offset); } } //setting layer Transform parentTfm = transform.parent; if (parentTfm != null) { Vector3 localPos = parentTfm.localPosition; localPos.z += layerOffset; transform.localPosition = localPos; } //refreshing parent absolute pos InstantGuiElement parentElement = null; if (parentTfm != null) { parentElement = parentTfm.GetComponent <InstantGuiElement>(); } if (parentElement != null) { parentpos = parentElement.absolute; } else //parentpos = InstantGuiElementPos(0,Screen.width,0,Screen.height); { if (InstantGui.instance == null) { InstantGui.instance = (InstantGui)FindObjectOfType(typeof(InstantGui)); } parentpos = new InstantGuiElementPos(0, InstantGui.width, 0, InstantGui.height); } //checking if there is a need to re-allign if (!InstantGuiElementPos.Equals(parentpos, oldParentpos) || !InstantGuiElementPos.Equals(relative, oldRelative) || !InstantGuiElementPos.Equals(offset, oldOffset) || InstantGui.oldScreenWidth != UnityEngine.Screen.width || InstantGui.oldScreenHeight != UnityEngine.Screen.height || true) { //transforming relative pos to absolute //if (!absolute || !parentpos || !relative || !offset) return; absolute.GetAbsolute(parentpos, relative, offset); //setting fixed size (if on) if (style != null) { if (style.fixedWidth) { absolute.right = absolute.left + style.fixedWidthSize; } if (style.fixedHeight) { absolute.bottom = absolute.top + style.fixedHeightSize; } } //preventing negative size int minWidth = 10; int minHeight = 10; if (style != null) { minWidth = style.borders.left + style.borders.right; minHeight = style.borders.bottom + style.borders.top; } if (absolute.right < absolute.left + minWidth) { absolute.right = absolute.left + minWidth; } if (absolute.bottom < absolute.top + minHeight) { absolute.bottom = absolute.top + minHeight; } //writing compare-data oldParentpos = new InstantGuiElementPos(parentpos); oldRelative = new InstantGuiElementPos(relative); oldOffset = new InstantGuiElementPos(offset); } //recursive for (int i = 0; i < childElements.Count; i++) { childElements[i].Align(); } }
static public void EditFrame(InstantGuiElement element) { Undo.RecordObject(element, "InstantGui Move"); //getting mouse pos Vector2 mousePos = Event.current.mousePosition; mousePos.y = InstantGui.Invert(mousePos.y); //shift-aligning if (Event.current.shift) { if (Mathf.Abs(mousePos.x - dragStart.x) < Mathf.Abs(mousePos.y - dragStart.y)) { mousePos.x = dragStart.x; } else { mousePos.y = dragStart.y; } } //getting mouse button bool mouseDown = false; bool mouseUp = false; if (Event.current.type == EventType.MouseDown && Event.current.button == 0) { mouseDown = true; dragStart = mousePos; } if (Event.current.type == EventType.MouseUp && Event.current.button == 0) { mouseUp = true; } //selecting locked element if (lockedElement != null) { //element = lockedElement; //Selection.activeGameObject = lockedElement.gameObject; } //duplicating element if (Event.current.type == EventType.ValidateCommand && Event.current.commandName == "Duplicate") { GameObject newElement = (GameObject)GameObject.Instantiate(element.gameObject); newElement.name = element.gameObject.name; if (element.transform.parent != null) { newElement.transform.parent = element.transform.parent; } newElement.transform.localPosition = element.transform.localPosition; newElement.transform.localRotation = element.transform.localRotation; newElement.transform.localScale = element.transform.localScale; Selection.activeGameObject = newElement; element = newElement.GetComponent <InstantGuiElement>(); } //key events if (Event.current.type == EventType.keyDown) { //Undo.RegisterUndo(element, "Move PureGUI"); //locking element if (Event.current.keyCode == KeyCode.L) { if (!lockedElement) { lockedElement = element; } else { lockedElement = null; } } //hiding frame if (Event.current.keyCode == KeyCode.H) { drawFrames = !drawFrames; } //moving with keys if (Event.current.keyCode == KeyCode.UpArrow) { element.offset.top--; element.offset.bottom--; } if (Event.current.keyCode == KeyCode.DownArrow) { element.offset.top++; element.offset.bottom++; } if (Event.current.keyCode == KeyCode.LeftArrow) { element.offset.left--; element.offset.right--; } if (Event.current.keyCode == KeyCode.RightArrow) { element.offset.left++; element.offset.right++; } } Rect rect = new Rect(0, 0, frameSize, frameSize); Rect textRect = new Rect(0, 0, 40, 20); //getting if any of dimension is fixed bool fixedWidth = false; bool fixedHeight = false; if (element.style != null) { if (element.style.fixedWidth) { fixedWidth = true; } if (element.style.fixedHeight) { fixedHeight = true; } } if (mouseDown) { nonGridOffset.left = element.offset.left; nonGridOffset.right = element.offset.right; nonGridOffset.top = element.offset.top; nonGridOffset.bottom = element.offset.bottom; nonGridRelative.left = element.relative.left; nonGridRelative.right = element.relative.right; nonGridRelative.top = element.relative.top; nonGridRelative.bottom = element.relative.bottom; } if (mouseUp || mouseDown) { dragging = 0; } //storing borders to assign comfortable int left = element.absolute.left; int right = element.absolute.right; int top = element.absolute.top; int bottom = element.absolute.bottom; //relative left rect.width = frameSize * 1.5f; rect.height = frameSize; rect.x = left - element.offset.left - rect.width * 0.5f; rect.y = element.absolute.GetCenter().y - rect.height * 0.5f; textRect.x = rect.x; textRect.y = rect.y + 16; if (rect.Contains(Event.current.mousePosition) && mouseDown && dragging == 0 && drawFrames && !Event.current.alt && !element.lockPosition) { dragging = 1; } if (dragging == 1) { nonGridRelative.left = Mathf.RoundToInt(InstantGuiElementPos.ScreenPointToRelative(element.parentpos, Event.current.mousePosition).x); if (Event.current.control) { element.relative.left = (Mathf.RoundToInt(nonGridRelative.left * 0.2f)) * 5; } else { element.relative.left = nonGridRelative.left; } element.offset.GetOffset(element.parentpos, element.relative, element.absolute); } //relative right if (!fixedWidth) { rect.x = right - element.offset.right - rect.width * 0.5f; rect.y = element.absolute.GetCenter().y - rect.height * 0.5f; textRect.x = rect.x; textRect.y = rect.y - 20; if (rect.Contains(Event.current.mousePosition) && mouseDown && dragging == 0 && drawFrames && !Event.current.alt && !element.lockPosition) { dragging = 2; } if (dragging == 2) { nonGridRelative.right = Mathf.RoundToInt(InstantGuiElementPos.ScreenPointToRelative(element.parentpos, Event.current.mousePosition).x); if (Event.current.control) { element.relative.right = (Mathf.RoundToInt(nonGridRelative.right * 0.2f)) * 5; } else { element.relative.right = nonGridRelative.right; } element.offset.GetOffset(element.parentpos, element.relative, element.absolute); } } else { element.relative.right = element.relative.left; } //relative top rect.width = frameSize; rect.height = frameSize * 1.5f; rect.x = element.absolute.GetCenter().x - rect.width * 0.5f; rect.y = top - element.offset.top - rect.height * 0.5f; textRect.x = rect.x + 20; textRect.y = rect.y - 2; if (rect.Contains(Event.current.mousePosition) && mouseDown && dragging == 0 && drawFrames && !Event.current.alt && !element.lockPosition) { dragging = 3; } if (dragging == 3) { nonGridRelative.top = Mathf.RoundToInt(InstantGuiElementPos.ScreenPointToRelative(element.parentpos, Event.current.mousePosition).y); if (Event.current.control) { element.relative.top = (Mathf.RoundToInt(nonGridRelative.top * 0.2f)) * 5; } else { element.relative.top = nonGridRelative.top; } element.offset.GetOffset(element.parentpos, element.relative, element.absolute); } //relative bottom if (!fixedHeight) { rect.x = element.absolute.GetCenter().x - rect.width * 0.5f; rect.y = bottom - element.offset.bottom - rect.height * 0.5f; textRect.x = rect.x - 30; textRect.y = rect.y - 2; if (rect.Contains(Event.current.mousePosition) && mouseDown && dragging == 0 && drawFrames && !Event.current.alt && !element.lockPosition) { dragging = 4; } if (dragging == 4) { nonGridRelative.bottom = Mathf.RoundToInt(InstantGuiElementPos.ScreenPointToRelative(element.parentpos, Event.current.mousePosition).y); if (Event.current.control) { element.relative.bottom = (Mathf.RoundToInt(nonGridRelative.bottom * 0.2f)) * 5; } else { element.relative.bottom = nonGridRelative.bottom; } element.offset.GetOffset(element.parentpos, element.relative, element.absolute); } } else { element.relative.bottom = element.relative.top; } rect = new Rect(0, 0, frameSize, frameSize); if (!fixedWidth || !fixedHeight) { //left-top rect.x = element.absolute.left - frameSize * 0.5f; rect.y = element.absolute.top - frameSize * 0.5f; textRect.x = rect.x - 25; textRect.y = (top + bottom) * 0.5f + (top - bottom) * 0.25f; if (rect.Contains(Event.current.mousePosition) && mouseDown && dragging == 0 && drawFrames && !Event.current.alt && !element.lockPosition) { dragging = 5; } if (dragging == 5) { element.offset.left = (int)(nonGridOffset.left + (mousePos.x - dragStart.x)); element.offset.top = (int)(nonGridOffset.top + (dragStart.y - mousePos.y)); if (Event.current.control) { element.offset.left = Mathf.RoundToInt(element.offset.left * 0.1f) * 10; element.offset.top = Mathf.RoundToInt(element.offset.top * 0.1f) * 10; } } //left-bottom rect.x = element.absolute.left - frameSize * 0.5f; rect.y = element.absolute.bottom - frameSize * 0.5f; textRect.x = (left + right) * 0.5f + (right - left) * 0.25f; textRect.y = rect.y + 10; if (rect.Contains(Event.current.mousePosition) && mouseDown && dragging == 0 && drawFrames && !Event.current.alt && !element.lockPosition) { dragging = 6; } if (dragging == 6) { element.offset.left = (int)(nonGridOffset.left + (mousePos.x - dragStart.x)); element.offset.bottom = (int)(nonGridOffset.bottom + (dragStart.y - mousePos.y)); if (Event.current.control) { element.offset.left = Mathf.RoundToInt(element.offset.left * 0.1f) * 10; element.offset.bottom = Mathf.RoundToInt(element.offset.bottom * 0.1f) * 10; } } //right-top rect.x = element.absolute.right - frameSize * 0.5f; rect.y = element.absolute.top - frameSize * 0.5f; textRect.x = (left + right) * 0.5f + (left - right) * 0.25f; textRect.y = rect.y - 13; if (rect.Contains(Event.current.mousePosition) && mouseDown && dragging == 0 && drawFrames && !Event.current.alt && !element.lockPosition) { dragging = 7; } if (dragging == 7) { element.offset.right = (int)(nonGridOffset.right + (mousePos.x - dragStart.x)); element.offset.top = (int)(nonGridOffset.top + (dragStart.y - mousePos.y)); if (Event.current.control) { element.offset.right = Mathf.RoundToInt(element.offset.right * 0.1f) * 10; element.offset.top = Mathf.RoundToInt(element.offset.top * 0.1f) * 10; } } //right-bottom rect.x = element.absolute.right - frameSize * 0.5f; rect.y = element.absolute.bottom - frameSize * 0.5f; textRect.x = rect.x + 15; textRect.y = (top + bottom) * 0.5f + (bottom - top) * 0.25f; if (rect.Contains(Event.current.mousePosition) && mouseDown && dragging == 0 && drawFrames && !Event.current.alt && !element.lockPosition) { dragging = 8; } if (dragging == 8) { element.offset.right = (int)(nonGridOffset.right + (mousePos.x - dragStart.x)); element.offset.bottom = (int)(nonGridOffset.bottom + (dragStart.y - mousePos.y)); if (Event.current.control) { element.offset.right = Mathf.RoundToInt(element.offset.right * 0.1f) * 10; element.offset.bottom = Mathf.RoundToInt(element.offset.bottom * 0.1f) * 10; } } } //aligning if (!InstantGui.instance) { InstantGui.instance = (InstantGui)FindObjectOfType(typeof(InstantGui)); } if (!EditorApplication.isPlaying) { //InstantGui.instance.Update(); //InstantGui.instance.element.Point(mousePos); InstantGui.width = Screen.width; InstantGui.height = Screen.height; InstantGui.instance.element = InstantGui.instance.GetComponent <InstantGuiElement>(); InstantGui.pointed = null; InstantGui.instance.element.GetChildren(); InstantGui.instance.element.Align(); //element.PreventZeroSize(true); InstantGui.instance.element.Point(true); InstantGui.instance.element.Action(); InstantGui.instance.element.ApplyStyle(); } //moving selected element (or setting fixed) rect = element.absolute.ToRect(); if (InstantGui.pointed == element && mouseDown && dragging == 0 && !Event.current.alt && !element.lockPosition && !selectionChanged) { dragging = 9; } if (dragging == 9) { //Undo.RecordObject(element, "InstantGui Move"); int sizeX = element.absolute.right - element.absolute.left; int sizeY = element.absolute.bottom - element.absolute.top; element.offset.left = (int)(nonGridOffset.left + (mousePos.x - dragStart.x)); element.offset.top = (int)(nonGridOffset.top + (dragStart.y - mousePos.y)); if (Event.current.control) { element.offset.left = Mathf.RoundToInt(element.offset.left * 0.1f) * 10; element.offset.top = Mathf.RoundToInt(element.offset.top * 0.1f) * 10; } element.absolute.GetAbsolute(element.parentpos, element.relative, element.offset); element.offset.GetOffset(element.parentpos, element.relative, new InstantGuiElementPos(element.absolute.left, element.absolute.left + sizeX, element.absolute.top, element.absolute.top + sizeY)); } //selecting other objs if (mouseDown && dragging == 0 && InstantGui.pointed != element && InstantGui.pointed != null && !lockedElement && !Event.current.alt) { Selection.activeGameObject = InstantGui.pointed.gameObject; element = InstantGui.pointed; nonGridOffset.left = element.offset.left; nonGridOffset.right = element.offset.right; nonGridOffset.top = element.offset.top; nonGridOffset.bottom = element.offset.bottom; nonGridRelative.left = element.relative.left; nonGridRelative.right = element.relative.right; nonGridRelative.top = element.relative.top; nonGridRelative.bottom = element.relative.bottom; selectionChanged = true; } else if (selectionChanged) { selectionChanged = false; } }
static public void DrawFrame ( InstantGuiElement element ) { if (!drawFrames) return; //setting textures if (frameTexture==null) frameTexture = GetTexture("GuiFrameSquare.png"); if (lineTexture==null) lineTexture = GetTexture("GuiFrameLine.png"); if (squareTexture==null) squareTexture = GetTexture("GuiFrameSquare.png"); if (circleTexture==null) circleTexture = GetTexture("GuiFrameCircle.png"); if (triBottomTexture==null) triBottomTexture = GetTexture("GuiFrameArrowBottom.png"); if (triTopTexture==null) triTopTexture = GetTexture("GuiFrameArrowTop.png"); if (triLeftTexture==null) triLeftTexture = GetTexture("GuiFrameArrowLeft.png"); if (triRightTexture==null) triRightTexture = GetTexture("GuiFrameArrowRight.png"); if (lockTexture==null) lockTexture = GetTexture("GuiFrameLock.png"); //getting if any of dimension is fixed bool fixedWidth = false; bool fixedHeight = false; if (element.style!=null) { if (element.style.fixedWidth) fixedWidth = true; if (element.style.fixedHeight) fixedHeight = true; } Rect rect = new Rect(0,0,frameSize,frameSize); Rect textRect = new Rect(0,0,40,20); //storing borders to assign comfortable int left = element.absolute.left; int right = element.absolute.right; int top = element.absolute.top; int bottom = element.absolute.bottom; //drawing lines GUI.DrawTexture( new Rect(left, (top+bottom)*0.5f, -element.offset.left, 1), lineTexture); if (!fixedWidth) GUI.DrawTexture( new Rect(right, (top+bottom)*0.5f, -element.offset.right, 1), lineTexture); GUI.DrawTexture( new Rect((left+right)*0.5f, top, 1, -element.offset.top), lineTexture); if (!fixedHeight) GUI.DrawTexture( new Rect((left+right)*0.5f, bottom, 1, -element.offset.bottom), lineTexture); GUI.DrawTexture( new Rect(left, top, right-left, 1), lineTexture); GUI.DrawTexture( new Rect(left, bottom, right-left, 1), lineTexture); GUI.DrawTexture( new Rect(left, top, 1, bottom-top), lineTexture); GUI.DrawTexture( new Rect(right, top, 1, bottom-top), lineTexture); //drawing point offset if (element.style!=null && (element.style.pointOffset.left!=0 || element.style.pointOffset.right!=0 || element.style.pointOffset.top!=0 || element.style.pointOffset.bottom!=0)) { RectOffset pointOffset = element.style.pointOffset; GUI.DrawTexture( new Rect(left+pointOffset.left, top+pointOffset.top, right-left-pointOffset.left-pointOffset.right, 1), lineTexture); GUI.DrawTexture( new Rect(left+pointOffset.left, bottom-pointOffset.bottom, right-left-pointOffset.left-pointOffset.right, 1), lineTexture); GUI.DrawTexture( new Rect(left+pointOffset.left, top+pointOffset.top, 1, bottom-top-pointOffset.bottom-pointOffset.top), lineTexture); GUI.DrawTexture( new Rect(right-pointOffset.right, top+pointOffset.top, 1, bottom-top-pointOffset.bottom-pointOffset.top), lineTexture); } //lock if (lockedElement!=null) { GUI.DrawTexture( new Rect(left+5, top+5, 24, 24), lockTexture); } //relative right rect.width = frameSize * 1.5f; rect.height = frameSize; if (!fixedWidth) { rect.x = right - element.offset.right - rect.width*0.5f; rect.y = element.absolute.GetCenter().y - rect.height*0.5f; GUI.DrawTexture(rect, triRightTexture); if (showValues==InstantGuiFrameShowValues.always || (showValues==InstantGuiFrameShowValues.onChange && dragging==2)) { textRect.x = rect.x-frameSize*0.25f-15; textRect.y = rect.y-frameSize*0.25f-15; GUI.Label(textRect, element.relative.right.ToString() + "%"); } } else element.relative.right = element.relative.left; //relative left rect.x = left - element.offset.left - rect.width*0.5f; rect.y = element.absolute.GetCenter().y - rect.height*0.5f; GUI.DrawTexture(rect, triLeftTexture); if (showValues==InstantGuiFrameShowValues.always || (showValues==InstantGuiFrameShowValues.onChange && dragging==1)) { //textRect.x = rect.x+frameSize*0.25f+15; textRect.y = rect.y+frameSize*0.1f+17; textRect.x = rect.x-frameSize*0.25f-15; textRect.y = rect.y-frameSize*0.25f-15; GUI.Label(textRect, element.relative.left.ToString() + "%"); } //relative bottom rect.width = frameSize; rect.height = frameSize * 1.5f; if (!fixedHeight) { rect.x = element.absolute.GetCenter().x - rect.width*0.5f; rect.y = bottom - element.offset.bottom - rect.height*0.5f; GUI.DrawTexture(rect, triBottomTexture); if (showValues==InstantGuiFrameShowValues.always || (showValues==InstantGuiFrameShowValues.onChange && dragging==4)) { //textRect.x = rect.x-frameSize-10; textRect.y = rect.y-frameSize*0.05f-20; textRect.x = rect.x-frameSize*0.25f-15; textRect.y = rect.y-frameSize*0.25f-15; GUI.Label(textRect, element.relative.bottom.ToString() + "%"); } } else element.relative.bottom = element.relative.top; //relative top rect.x = element.absolute.GetCenter().x - rect.width*0.5f; rect.y = top - element.offset.top - rect.height*0.5f; GUI.DrawTexture(rect, triTopTexture); if (showValues==InstantGuiFrameShowValues.always || (showValues==InstantGuiFrameShowValues.onChange && dragging==3)) { //textRect.x = rect.x+frameSize*1.1f; textRect.y = rect.y+frameSize; textRect.x = rect.x-frameSize*0.25f-15; textRect.y = rect.y-frameSize*0.25f-15; GUI.Label(textRect, element.relative.top.ToString() + "%"); } rect = new Rect(0,0,frameSize,frameSize); if (!fixedWidth || !fixedHeight) { //left-top 5 rect.x = element.absolute.left-frameSize*0.5f; rect.y = element.absolute.top-frameSize*0.5f; textRect.x = rect.x-25; textRect.y = (top+bottom)*0.5f + (top-bottom)*0.25f; GUI.DrawTexture(rect, circleTexture); if (showValues==InstantGuiFrameShowValues.always || (showValues==InstantGuiFrameShowValues.onChange && (dragging==6 || dragging==5 || dragging==9))) GUI.Label(textRect, element.offset.left.ToString() + "p"); //left-bottom 6 rect.x = element.absolute.left-frameSize*0.5f; rect.y = element.absolute.bottom-frameSize*0.5f; textRect.x = (left+right)*0.5f + (right-left)*0.25f; textRect.y = rect.y+10; GUI.DrawTexture(rect, circleTexture); if (showValues==InstantGuiFrameShowValues.always || (showValues==InstantGuiFrameShowValues.onChange && (dragging==8 || dragging==6 || dragging==9))) GUI.Label(textRect, element.offset.bottom.ToString() + "p"); //right-top 7 rect.x = element.absolute.right-frameSize*0.5f; rect.y = element.absolute.top-frameSize*0.5f; textRect.x = (left+right)*0.5f + (left-right)*0.25f; textRect.y = rect.y-13; GUI.DrawTexture(rect, circleTexture); if (showValues==InstantGuiFrameShowValues.always || (showValues==InstantGuiFrameShowValues.onChange && (dragging==5 || dragging==7 || dragging==9))) GUI.Label(textRect, element.offset.top.ToString() + "p"); //right-bottom 8 rect.x = element.absolute.right-frameSize*0.5f; rect.y = element.absolute.bottom-frameSize*0.5f; textRect.x = rect.x+15; textRect.y = (top+bottom)*0.5f + (bottom-top)*0.25f; GUI.DrawTexture(rect, circleTexture); if (showValues==InstantGuiFrameShowValues.always || (showValues==InstantGuiFrameShowValues.onChange && (dragging==7 || dragging==8 || dragging==9))) GUI.Label(textRect, element.offset.right.ToString() + "p"); } }
static public void DrawSaveToStyleButton(string label, InstantGuiElement element, bool saveStyle, bool savePos) { //if (!saveStyle && savePos && element.customStyle) return; Rect rect = GUILayoutUtility.GetRect(50, 18, "TextField"); rect.x = 30; rect.width -= 15; if (GUI.Button(rect, label)) { //finding in set bool foundInSet = false; for (int j = 0; j < element.styleSet.styles.Length; j++) { if (element.styleSet.styles[j].name == element.style.name) { if (saveStyle) { element.styleSet.styles[j] = element.style.Clone(); } if (savePos) { element.styleSet.styles[j].relative = element.relative.Clone(); element.styleSet.styles[j].offset = element.offset.Clone(); element.styleSet.styles[j].layerOffset = element.layerOffset; } foundInSet = true; } } //adding new if (!foundInSet) { #if UNITY_EDITOR if (!saveStyle) { if (EditorUtility.DisplayDialog("No Such Style", "Cannot find style in Style Set. Do you want to create it?", "Create", "Cancel")) { saveStyle = true; } else { EditorUtility.SetDirty(element.styleSet); return; } } #endif InstantGuiStyle[] newStyles = new InstantGuiStyle[element.styleSet.styles.Length + 1]; for (int j = 0; j < element.styleSet.styles.Length; j++) { newStyles[j] = element.styleSet.styles[j]; } if (saveStyle) { newStyles[newStyles.Length - 1] = element.style.Clone(); } if (savePos) { newStyles[newStyles.Length - 1].relative = element.relative.Clone(); newStyles[newStyles.Length - 1].offset = element.offset.Clone(); newStyles[newStyles.Length - 1].layerOffset = element.layerOffset; } element.styleSet.styles = newStyles; } #if UNITY_EDITOR EditorUtility.SetDirty(element.styleSet); #endif } }
public override void OnInspectorGUI() { InstantGuiTabs script = (InstantGuiTabs)target; base.OnInspectorGUI(); //EditorGUILayout.Space(); script.guiElementProps = EditorGUILayout.Foldout(script.guiElementProps, "Tabs"); //some non-gui actions for (int i = 0; i < script.tabs.Length; i++) { //creating field if it does not exists if (!script.fields[i]) { script.fields[i] = script.CreateField(); } //fields names if (script.fields[i].transform.name.Length == 0) { script.fields[i].transform.name = script.tabs[i].transform.name + "_Field"; } } if (script.guiElementProps) { EditorGUI.indentLevel = 1; script.selected = EditorGUILayout.IntField("Selected:", script.selected); //array Rect rect = GUILayoutUtility.GetRect(150, 16, "TextField"); rect.width = rect.width * 0.5f - 40; EditorGUI.LabelField(rect, "Tabs:"); rect.x += rect.width; EditorGUI.LabelField(rect, "Fields:"); script.tabs = InstantGuiInspector.DrawElementsTable(script.tabs, script.fields); if (script.tabs.Length != script.fields.Length) //changing fields count (creating if necessary) { InstantGuiElement[] newFields = new InstantGuiElement[script.tabs.Length]; for (int j = 0; j < script.tabs.Length; j++) { if (j >= script.fields.Length || !script.fields[j]) { newFields[j] = script.CreateField(); continue; } newFields[j] = script.fields[j]; } script.fields = newFields; } EditorGUI.indentLevel = 0; } InstantGuiInspector.DrawActivator("On Checked:", script.onChecked); InstantGuiInspector.DrawActivator("On Unchecked:", script.onUnchecked); }
public RectOffset pointOffset;// = new RectOffset(0,0,0,0); /* public InstantGuiStyle() { main = new SubStyle(); pointed = result.pointed.Clone(); active = result.active.Clone(); result.disabled = result.disabled.Clone(); result.relative = result.relative.Clone(); result.offset = result.offset.Clone(); result.pointOffset = new RectOffset(result.pointOffset.left, result.pointOffset.right, result.pointOffset.top, result.pointOffset.bottom); result.borders = new RectOffset(result.borders.left, result.borders.right, result.borders.top, result.borders.bottom); return result; } */ public void Apply (SubStyle sub, InstantGuiElement element) { //getting number of texts that should be in element int textsLength=0; if (element.text.Length==0) textsLength = 0; else switch (textEffect) { case InstantGuiTextEffect.simple: textsLength = 1; break; case InstantGuiTextEffect.shadow: textsLength = 2; break; case InstantGuiTextEffect.stroke: textsLength = 5; break; } //should a texture ot text be removed? bool removeTexture=false; bool removeTexts=false; if (!sub.enabled) { removeTexture = true; removeTexts = true; } if (!sub.texture) removeTexture = true; if (element.guiTexts.Length != textsLength) removeTexts = true; //if text Length changed. No text included if (font!=element.currentFont) removeTexts = true; //if font changed for (int i=0; i<element.guiTexts.Length; i++) if (!element.guiTexts[i]) removeTexts = true; //if any of guitexts array do not exists //removing if (removeTexture && element.mainGuiTexture!=null) { GameObject.DestroyImmediate(element.mainGuiTexture.gameObject); } if (removeTexts) { for(int i=element.guiTexts.Length-1; i>=0 ; i--) if (element.guiTexts[i]!=null) GameObject.DestroyImmediate(element.guiTexts[i].gameObject); //creating new array element.guiTexts = new GUIText[textsLength]; } if (!sub.enabled || (!sub.texture && element.text.Length==0)) return; //setting texture if (sub.texture!=null) { if (element.mainGuiTexture==null) { GameObject obj = new GameObject (element.transform.name + "_mainTexture"); obj.transform.parent = element.transform; obj.hideFlags = HideFlags.HideInHierarchy; element.mainGuiTexture = obj.AddComponent<GUITexture>(); } if (element.mainGuiTexture.texture != sub.texture) element.mainGuiTexture.texture = sub.texture; element.mainGuiTexture.transform.localPosition = new Vector3(0,0,element.transform.localPosition.z); element.mainGuiTexture.pixelInset = InstantGui.Invert(element.absolute.ToRect(InstantGui.scale)); if (borders!=null) element.mainGuiTexture.border = borders; } //proportional if (proportional && element.mainGuiTexture!=null) { Rect newInset = element.mainGuiTexture.pixelInset; if (1.0f * element.mainGuiTexture.pixelInset.width / element.mainGuiTexture.pixelInset.height < proportionalAspect) { newInset.width = element.mainGuiTexture.pixelInset.height*proportionalAspect; newInset.x -= (newInset.width-element.mainGuiTexture.pixelInset.width)*0.5f; } else { newInset.height = element.mainGuiTexture.pixelInset.width*(1.0f/proportionalAspect); newInset.y -= (newInset.height-element.mainGuiTexture.pixelInset.height)*0.5f; } element.mainGuiTexture.pixelInset = newInset; } //setting text if (element.text.Length!=0) { //on font change - remove materials if (!font) font = (Font)Resources.GetBuiltinResource(typeof(Font), "Arial.ttf"); if (font != element.currentFont) { //element.textMaterial=null; //if (element.guiTexts.Length > 1) element.textEffectMaterial=null; for (int i=0;i<element.guiTexts.Length;i++) if (element.guiTexts[i]!=null) GameObject.DestroyImmediate(element.guiTexts[i].gameObject); element.currentFont = font; } //creating texts for (int i=0;i<element.guiTexts.Length;i++) { if (element.guiTexts[i]==null) { GameObject obj = new GameObject (element.transform.name + "_text" + i.ToString()); obj.transform.parent = element.transform; obj.hideFlags = HideFlags.HideInHierarchy; element.guiTexts[i] = obj.AddComponent<GUIText>(); //setting materials if (i==0) { element.textMaterial = new Material (font.material); element.guiTexts[i].material = element.textMaterial; } else if (i>0 && i==element.guiTexts.Length-1) //on the last one effect text { element.textEffectMaterial = new Material (font.material); for (int j=1;j<element.guiTexts.Length;j++) element.guiTexts[j].material = element.textEffectMaterial; } } } //setting text params for (int i=0;i<element.guiTexts.Length;i++) { if (i==0) element.textMaterial.color = sub.textColor; else element.textEffectMaterial.color = textEffectColor; element.guiTexts[i].transform.localPosition = new Vector3(0,0,element.transform.localPosition.z+0.01f - i*0.001f); //setting text if (element.guiTexts[i].text != element.text && !element.password) element.guiTexts[i].text = element.text; else if (element.password) { element.guiTexts[i].text = ""; for (int j=0;j<element.text.Length;j++) element.guiTexts[i].text += "*"; } if (element.guiTexts[i].font != font) element.guiTexts[i].font = font; if (element.guiTexts[i].fontSize != fontSize) element.guiTexts[i].fontSize = fontSize; Vector2 pixelOffset = element.absolute.GetCenter(); switch (textAligment) { case InstantGuiTextAligment.center: element.guiTexts[i].anchor = TextAnchor.MiddleCenter; break; case InstantGuiTextAligment.text: pixelOffset.y = element.absolute.top; pixelOffset.x = element.absolute.left; element.guiTexts[i].anchor = TextAnchor.UpperLeft; break; case InstantGuiTextAligment.heading: pixelOffset.y = element.absolute.top; element.guiTexts[i].anchor = TextAnchor.UpperCenter; break; case InstantGuiTextAligment.above: pixelOffset.y = element.absolute.top; element.guiTexts[i].anchor = TextAnchor.LowerCenter; break; case InstantGuiTextAligment.label: pixelOffset.x = element.absolute.left; element.guiTexts[i].anchor = TextAnchor.MiddleRight; break; case InstantGuiTextAligment.rightLabel: pixelOffset.x = element.absolute.right; element.guiTexts[i].anchor = TextAnchor.MiddleLeft; break; } pixelOffset += new Vector2(textOffsetX+sub.textOffsetX, textOffsetY+sub.textOffsetY); element.guiTexts[i].pixelOffset = InstantGui.Invert(pixelOffset); } //aligning text effect if (textEffect==InstantGuiTextEffect.shadow) { element.guiTexts[1].pixelOffset = element.guiTexts[0].pixelOffset + new Vector2(textEffectSize*0.75f,-textEffectSize); } else if (textEffect==InstantGuiTextEffect.stroke) { element.guiTexts[1].pixelOffset = element.guiTexts[0].pixelOffset + new Vector2(textEffectSize,textEffectSize); element.guiTexts[2].pixelOffset = element.guiTexts[0].pixelOffset + new Vector2(-textEffectSize,textEffectSize); element.guiTexts[3].pixelOffset = element.guiTexts[0].pixelOffset + new Vector2(-textEffectSize,-textEffectSize); element.guiTexts[4].pixelOffset = element.guiTexts[0].pixelOffset + new Vector2(textEffectSize,-textEffectSize); } } }
static public void Clear ( InstantGuiElement element ){ if (element.mainGuiTexture!=null) GameObject.DestroyImmediate(element.mainGuiTexture.gameObject); for(int i=element.guiTexts.Length-1; i>=0 ; i--) if (element.guiTexts[i]!=null) GameObject.DestroyImmediate(element.guiTexts[i].gameObject); if (element.blendGuiTexture!=null) GameObject.DestroyImmediate(element.blendGuiTexture.gameObject); }
public void Unblend ( InstantGuiElement element ) //removes any blend, done after base style apply { if (element.blendGuiTexture!=null) GameObject.DestroyImmediate(element.blendGuiTexture.gameObject); if (element.mainGuiTexture!=null && !Mathf.Approximately(element.mainGuiTexture.color.a, 0.5f)) element.mainGuiTexture.color = new Color (element.mainGuiTexture.color.r, element.mainGuiTexture.color.g, element.mainGuiTexture.color.b, 0.5f); }
public void Blend ( SubStyle sub , InstantGuiElement element , float blend ) //done after base style apply { //removing a blended texture if necessary if ((!sub.enabled || sub.texture==null) && element.blendGuiTexture!=null) GameObject.DestroyImmediate(element.blendGuiTexture.gameObject); //setting texture if (sub.texture!=null) { if (!element.blendGuiTexture) { GameObject obj = new GameObject (element.transform.name + "_blendTexture"); obj.transform.parent = element.transform; //obj.hideFlags = HideFlags.HideInHierarchy; element.blendGuiTexture = obj.AddComponent<GUITexture>(); } if (element.blendGuiTexture.texture != sub.texture) element.blendGuiTexture.texture = sub.texture; element.blendGuiTexture.transform.localPosition = new Vector3(0,0,element.transform.localPosition.z+0.005f); element.blendGuiTexture.pixelInset = InstantGui.Invert(element.absolute.ToRect(InstantGui.scale)); if (borders!=null) element.blendGuiTexture.border = borders; element.blendGuiTexture.color = new Color(element.blendGuiTexture.color.r, element.blendGuiTexture.color.g, element.blendGuiTexture.color.b, blend*0.5f); if (element.mainGuiTexture!=null) element.mainGuiTexture.color = new Color(element.mainGuiTexture.color.r, element.mainGuiTexture.color.g, element.mainGuiTexture.color.b, (1-blend*blend)*0.5f); } if (element.guiTexts!=null && element.guiTexts.Length > 0) element.textMaterial.color = element.textMaterial.color*(1-blend) + sub.textColor*blend; }
public RectOffset pointOffset; // = new RectOffset(0,0,0,0); /* * public InstantGuiStyle() * { * main = new SubStyle(); * pointed = result.pointed.Clone(); * active = result.active.Clone(); * result.disabled = result.disabled.Clone(); * result.relative = result.relative.Clone(); * result.offset = result.offset.Clone(); * result.pointOffset = new RectOffset(result.pointOffset.left, result.pointOffset.right, result.pointOffset.top, result.pointOffset.bottom); * result.borders = new RectOffset(result.borders.left, result.borders.right, result.borders.top, result.borders.bottom); * return result; * } */ public void Apply(SubStyle sub, InstantGuiElement element) { //getting number of texts that should be in element int textsLength = 0; if (element.text.Length == 0) { textsLength = 0; } else { switch (textEffect) { case InstantGuiTextEffect.simple: textsLength = 1; break; case InstantGuiTextEffect.shadow: textsLength = 2; break; case InstantGuiTextEffect.stroke: textsLength = 5; break; } } //should a texture ot text be removed? bool removeTexture = false; bool removeTexts = false; if (!sub.enabled) { removeTexture = true; removeTexts = true; } if (!sub.texture) { removeTexture = true; } if (element.guiTexts.Length != textsLength) { removeTexts = true; //if text Length changed. No text included } if (font != element.currentFont) { removeTexts = true; //if font changed } for (int i = 0; i < element.guiTexts.Length; i++) { if (!element.guiTexts[i]) { removeTexts = true; //if any of guitexts array do not exists } } //removing if (removeTexture && element.mainGuiTexture != null) { GameObject.DestroyImmediate(element.mainGuiTexture.gameObject); } if (removeTexts) { for (int i = element.guiTexts.Length - 1; i >= 0; i--) { if (element.guiTexts[i] != null) { GameObject.DestroyImmediate(element.guiTexts[i].gameObject); } } //creating new array element.guiTexts = new GUIText[textsLength]; } if (!sub.enabled || (!sub.texture && element.text.Length == 0)) { return; } //setting texture if (sub.texture != null) { if (element.mainGuiTexture == null) { GameObject obj = new GameObject(element.transform.name + "_mainTexture"); obj.transform.parent = element.transform; obj.hideFlags = HideFlags.HideInHierarchy; element.mainGuiTexture = obj.AddComponent <GUITexture>(); } if (element.mainGuiTexture.texture != sub.texture) { element.mainGuiTexture.texture = sub.texture; } element.mainGuiTexture.transform.localPosition = new Vector3(0, 0, element.transform.localPosition.z); element.mainGuiTexture.pixelInset = InstantGui.Invert(element.absolute.ToRect(InstantGui.scale)); if (borders != null) { element.mainGuiTexture.border = borders; } } //proportional if (proportional && element.mainGuiTexture != null) { Rect newInset = element.mainGuiTexture.pixelInset; if (1.0f * element.mainGuiTexture.pixelInset.width / element.mainGuiTexture.pixelInset.height < proportionalAspect) { newInset.width = element.mainGuiTexture.pixelInset.height * proportionalAspect; newInset.x -= (newInset.width - element.mainGuiTexture.pixelInset.width) * 0.5f; } else { newInset.height = element.mainGuiTexture.pixelInset.width * (1.0f / proportionalAspect); newInset.y -= (newInset.height - element.mainGuiTexture.pixelInset.height) * 0.5f; } element.mainGuiTexture.pixelInset = newInset; } //setting text if (element.text.Length != 0) { //on font change - remove materials if (!font) { font = (Font)Resources.GetBuiltinResource(typeof(Font), "Arial.ttf"); } if (font != element.currentFont) { //element.textMaterial=null; //if (element.guiTexts.Length > 1) element.textEffectMaterial=null; for (int i = 0; i < element.guiTexts.Length; i++) { if (element.guiTexts[i] != null) { GameObject.DestroyImmediate(element.guiTexts[i].gameObject); } } element.currentFont = font; } //creating texts for (int i = 0; i < element.guiTexts.Length; i++) { if (element.guiTexts[i] == null) { GameObject obj = new GameObject(element.transform.name + "_text" + i.ToString()); obj.transform.parent = element.transform; obj.hideFlags = HideFlags.HideInHierarchy; element.guiTexts[i] = obj.AddComponent <GUIText>(); //setting materials if (i == 0) { element.textMaterial = new Material(font.material); element.guiTexts[i].material = element.textMaterial; } else if (i > 0 && i == element.guiTexts.Length - 1) //on the last one effect text { element.textEffectMaterial = new Material(font.material); for (int j = 1; j < element.guiTexts.Length; j++) { element.guiTexts[j].material = element.textEffectMaterial; } } } } //setting text params for (int i = 0; i < element.guiTexts.Length; i++) { if (i == 0) { element.textMaterial.color = sub.textColor; } else { element.textEffectMaterial.color = textEffectColor; } element.guiTexts[i].transform.localPosition = new Vector3(0, 0, element.transform.localPosition.z + 0.01f - i * 0.001f); //setting text if (element.guiTexts[i].text != element.text && !element.password) { element.guiTexts[i].text = element.text; } else if (element.password) { element.guiTexts[i].text = ""; for (int j = 0; j < element.text.Length; j++) { element.guiTexts[i].text += "*"; } } if (element.guiTexts[i].font != font) { element.guiTexts[i].font = font; } if (element.guiTexts[i].fontSize != fontSize) { element.guiTexts[i].fontSize = fontSize; } Vector2 pixelOffset = element.absolute.GetCenter(); switch (textAligment) { case InstantGuiTextAligment.center: element.guiTexts[i].anchor = TextAnchor.MiddleCenter; break; case InstantGuiTextAligment.text: pixelOffset.y = element.absolute.top; pixelOffset.x = element.absolute.left; element.guiTexts[i].anchor = TextAnchor.UpperLeft; break; case InstantGuiTextAligment.heading: pixelOffset.y = element.absolute.top; element.guiTexts[i].anchor = TextAnchor.UpperCenter; break; case InstantGuiTextAligment.above: pixelOffset.y = element.absolute.top; element.guiTexts[i].anchor = TextAnchor.LowerCenter; break; case InstantGuiTextAligment.label: pixelOffset.x = element.absolute.left; element.guiTexts[i].anchor = TextAnchor.MiddleRight; break; case InstantGuiTextAligment.rightLabel: pixelOffset.x = element.absolute.right; element.guiTexts[i].anchor = TextAnchor.MiddleLeft; break; } pixelOffset += new Vector2(textOffsetX + sub.textOffsetX, textOffsetY + sub.textOffsetY); element.guiTexts[i].pixelOffset = InstantGui.Invert(pixelOffset); } //aligning text effect if (textEffect == InstantGuiTextEffect.shadow) { element.guiTexts[1].pixelOffset = element.guiTexts[0].pixelOffset + new Vector2(textEffectSize * 0.75f, -textEffectSize); } else if (textEffect == InstantGuiTextEffect.stroke) { element.guiTexts[1].pixelOffset = element.guiTexts[0].pixelOffset + new Vector2(textEffectSize, textEffectSize); element.guiTexts[2].pixelOffset = element.guiTexts[0].pixelOffset + new Vector2(-textEffectSize, textEffectSize); element.guiTexts[3].pixelOffset = element.guiTexts[0].pixelOffset + new Vector2(-textEffectSize, -textEffectSize); element.guiTexts[4].pixelOffset = element.guiTexts[0].pixelOffset + new Vector2(textEffectSize, -textEffectSize); } } }
static public InstantGuiElement[] DrawElementsTable(InstantGuiElement[] array, InstantGuiElement[] secondary) //returns only the first array, but secondary members could be assigned { int space = 2; int buttonSize = 20; InstantGuiElement[] newArray = array; int indent = EditorGUI.indentLevel; EditorGUI.indentLevel = 0; int scrollSize = Mathf.Min(array.Length * 20, 110); if (array.Length > 6) { scrolls[3] = EditorGUILayout.BeginScrollView(new Vector2(0, scrolls[3]), GUILayout.Height(scrollSize)).y; } Rect rect = new Rect(); for (int i = 0; i < array.Length; i++) { rect = GUILayoutUtility.GetRect(150, 16, "TextField"); rect.x += indent * indentPixels; rect.width -= indent * indentPixels; rect.width -= (buttonSize + space) * 3; rect.width = rect.width * 0.5f - 2; array[i] = (InstantGuiElement)EditorGUI.ObjectField(rect, array[i], typeof(InstantGuiElement), true); rect.x += rect.width + 4; secondary[i] = (InstantGuiElement)EditorGUI.ObjectField(rect, secondary[i], typeof(InstantGuiElement), true); //move up rect.x += rect.width + space; rect.width = buttonSize; if (GUI.Button(rect, "∧ ") && i != 0) { InstantGuiElement tmp = array[i - 1]; array[i - 1] = array[i]; array[i] = tmp; tmp = secondary[i - 1]; secondary[i - 1] = secondary[i]; secondary[i] = tmp; } //move down rect.x += buttonSize + space; if (GUI.Button(rect, "∨ ") && i != array.Length - 1) { InstantGuiElement tmp = array[i + 1]; array[i + 1] = array[i]; array[i] = tmp; tmp = secondary[i + 1]; secondary[i + 1] = secondary[i]; secondary[i] = tmp; } //delete rect.x += buttonSize + space; if (GUI.Button(rect, "X")) { newArray = new InstantGuiElement[array.Length - 1]; // for (int j = 0; j < newArray.Length; j++) { if (j < i) { newArray[j] = array[j]; } else { newArray[j] = array[j + 1]; } } } } if (array.Length > 6) { EditorGUILayout.EndScrollView(); } //adding a little space after if (array.Length > 0) { GUILayoutUtility.GetRect(150, 2, "TextField"); } //adding new obj rect = GUILayoutUtility.GetRect(150, 16, "TextField"); rect.x += indent * indentPixels; rect.width -= indent * indentPixels; rect.width -= (buttonSize + space) * 3; rect.width = rect.width * 0.5f - 2; InstantGuiElement addedVal = (InstantGuiElement)EditorGUI.ObjectField(rect, null, typeof(InstantGuiElement), true); // if (addedVal != null) { newArray = new InstantGuiElement[array.Length + 1]; // for (int j = 0; j < array.Length; j++) { newArray[j] = array[j]; } newArray[newArray.Length - 1] = addedVal; } //adding a little space after rect = GUILayoutUtility.GetRect(150, 3, "TextField"); EditorGUI.indentLevel = indent; return(newArray); }
public override void OnInspectorGUI() { EditorGUI.BeginChangeCheck(); InstantGuiElement script = (InstantGuiElement)target; Undo.RecordObject(script, "InstantGui Change"); //EditorGUIUtility.LookLikeInspector (); //DrawDefaultInspector (); //text string newText = EditorGUILayout.TextField("Text:", script.text); script.guiLinkText = EditorGUILayout.Toggle("Use Object Name:", script.guiLinkText); if (script.guiLinkText && !EditorApplication.isPlayingOrWillChangePlaymode && script.GetType() != typeof(InstantGuiTextArea)) { if (script.text != newText) //if text changed { if (newText.Length != 0) { script.gameObject.name = newText; } else { script.gameObject.name = "GuiElement"; } script.text = newText; } if (script.text != script.gameObject.name && newText.Length != 0) { script.text = script.gameObject.name; } } else { script.text = newText; } //setting style script.guiStyle = EditorGUILayout.Foldout(script.guiStyle, "Style"); if (script.guiStyle) { EditorGUI.indentLevel = 1; script.styleSet = (InstantGuiStyleSet)EditorGUILayout.ObjectField("Style Set:", script.styleSet, typeof(InstantGuiStyleSet), false); bool customStyle = EditorGUILayout.Toggle("Custom Style:", script.customStyle); //custom style if (customStyle) { if (!script.customStyle) { if (script.style != null) { script.style = script.style.Clone(); } else { script.style = new InstantGuiStyle(); } } script.style.name = EditorGUILayout.TextField("Name:", script.style.name); script.styleName = script.style.name; InstantGuiInspector.DrawStyle(script.style, false); EditorGUILayout.Space(); if (script.styleSet != null && script.style != null && script.style.name.Length > 0) { InstantGuiInspector.DrawSaveToStyleButton("Add to StyleSet", script, true, false); } } //else if (!customStyle && script.styleSet != null) { string[] styleNames = new string[script.styleSet.styles.Length]; int selectedStyle = -1; //default if style could not be found //compiling popup array for (int i = 0; i < script.styleSet.styles.Length; i++) { styleNames[i] = script.styleSet.styles[i].name; } //finding popup selected if (script.style == null) { selectedStyle = 0; } else { for (int i = 0; i < styleNames.Length; i++) { if (styleNames[i] == script.styleName) { selectedStyle = i; } } } styleNames[0] = "None"; int newSelected = EditorGUILayout.Popup("Style:", selectedStyle, styleNames); if (newSelected != selectedStyle) { script.style = script.styleSet.styles[newSelected]; script.styleName = script.style.name; //script.styleNum = newSelected; } } script.customStyle = customStyle; EditorGUI.indentLevel = 0; } //position script.guiPosition = EditorGUILayout.Foldout(script.guiPosition, "Position"); if (script.guiPosition) { EditorGUI.indentLevel = 1; script.useStylePlacement = EditorGUILayout.ToggleLeft("Use Style Placement", script.useStylePlacement); if (!script.useStylePlacement) { int preset = EditorGUILayout.Popup("Preset:", -1, positionPresets); switch (preset) { //case 0: if (script.style!=null) { script.relative.Set(script.style.relative); script.offset.Set(script.style.offset); script.layerOffset = script.style.layerOffset; } break; case 0: script.relative.Set(0, 100, 0, 100); script.offset.Set(0, 0, 0, 0); break; case 1: script.relative.Set(10, 90, 10, 90); script.offset.Set(10, -10, 10, -10); break; case 2: script.offset.right = -script.offset.left; script.offset.bottom = -script.offset.top; break; case 3: script.relative.Set(50, 50, 50, 50); script.offset.Set(20, -20, 20, -20); break; case 4: if (script.style != null) { script.offset.left = (int)(-script.style.fixedWidthSize * 0.5f); script.offset.right = (int)(script.style.fixedWidthSize * 0.5f); } break; case 5: if (script.style != null) { script.offset.top = (int)(-script.style.fixedHeightSize * 0.5f); script.offset.bottom = (int)(script.style.fixedHeightSize * 0.5f); } break; } InstantGuiInspector.DrawElementPosLabels("", "Left", "Right", "Top", "Bottom"); script.relative = InstantGuiInspector.DrawElementPos("Relative:", script.relative); script.offset = InstantGuiInspector.DrawElementPos("Offset:", script.offset); InstantGuiInspector.DrawElementPosLabels("Absolute:", script.absolute.left.ToString(), script.absolute.right.ToString(), script.absolute.top.ToString(), script.absolute.bottom.ToString()); script.layerOffset = InstantGuiInspector.DrawLayerOffset("Layer Offset:", script.layerOffset); script.lockPosition = EditorGUILayout.Toggle("Lock Position", script.lockPosition); if (script.styleSet != null && script.style != null && script.style.name != null && script.style.name.Length > 0) { InstantGuiInspector.DrawSaveToStyleButton("Set as Default in Style", script, false, true); } } EditorGUI.indentLevel = 0; } script.guiAttributes = EditorGUILayout.Foldout(script.guiAttributes, "Attributes"); if (script.guiAttributes) { EditorGUI.indentLevel = 1; //EditorGUI.indentLevel = 20; script.dynamic = EditorGUILayout.Toggle("Dynamic", script.dynamic); script.editable = EditorGUILayout.Toggle("Editable", script.editable); script.pointed = EditorGUILayout.Toggle("Pointed", script.pointed); script.disabled = EditorGUILayout.Toggle("Disabled", script.disabled); script.activated = EditorGUILayout.Toggle("Activated", script.activated); script.pressed = EditorGUILayout.Toggle("Pressed", script.pressed); //script.checkbutton = EditorGUILayout.Toggle("\tCheckbutton", script.checkbutton); script.check = EditorGUILayout.Toggle("Checked", script.check); script.instant = EditorGUILayout.Toggle("Instant", script.instant); script.password = EditorGUILayout.Toggle("Password", script.password); EditorGUI.indentLevel = 0; } if (EditorGUI.EndChangeCheck()) { EditorUtility.SetDirty(script); InstantGui.instance.Update(); } else { Undo.ClearUndo(script); } }
static public InstantGuiElement Create(string name, string style, System.Type type, InstantGuiElement parent) { //finding gui parent if (InstantGui.instance == null) { InstantGui.instance = (InstantGui)FindObjectOfType(typeof(InstantGui)); } if (InstantGui.instance == null) { InstantGui.CreateRoot(); parent = InstantGui.instance.element; } GameObject obj = new GameObject(name); //the object component will be assigned to //adding component InstantGuiElement element = (InstantGuiElement)obj.AddComponent(type); //parenting and setting styleset if (parent != null) { element.transform.parent = parent.transform; element.styleSet = parent.styleSet; //do not assign parent to element! It will get it automaticly } //resetting transform if (obj.transform.parent != null) { obj.transform.localPosition = new Vector3(0, 0, obj.transform.parent.localPosition.z + 1); } else { obj.transform.localPosition = new Vector3(0, 0, 0); } obj.transform.localScale = new Vector3(0, 0, 1); //setting style element.styleName = style; if (element.styleSet != null) { for (int i = 0; i < element.styleSet.styles.Length; i++) { if (element.styleSet.styles[i].name == element.styleName) { element.style = element.styleSet.styles[i]; } } } //setting default size if (element.style != null) { element.relative.Set(element.style.relative); element.offset.Set(element.style.offset); element.layerOffset = element.style.layerOffset; } //pureGui.Update(); return(element); }
static public InstantGuiElement[] DrawElementsTable ( InstantGuiElement[] array , InstantGuiElement[] secondary ) //returns only the first array, but secondary members could be assigned { int space = 2; int buttonSize = 20; InstantGuiElement[] newArray = array; int indent= EditorGUI.indentLevel; EditorGUI.indentLevel = 0; int scrollSize = Mathf.Min(array.Length*20, 110); if (array.Length>6) scrolls[3] = EditorGUILayout.BeginScrollView(new Vector2(0,scrolls[3]), GUILayout.Height(scrollSize)).y; Rect rect = new Rect(); for (int i=0; i<array.Length; i++) { rect = GUILayoutUtility.GetRect (150, 16, "TextField"); rect.x+=indent*indentPixels; rect.width-=indent*indentPixels; rect.width -= (buttonSize+space)*3; rect.width = rect.width*0.5f - 2; array[i] = (InstantGuiElement)EditorGUI.ObjectField (rect, array[i], typeof(InstantGuiElement), true); rect.x += rect.width + 4; secondary[i] = (InstantGuiElement)EditorGUI.ObjectField (rect, secondary[i], typeof(InstantGuiElement), true); //move up rect.x += rect.width+space; rect.width = buttonSize; if (GUI.Button(rect, "∧ ") && i != 0) { InstantGuiElement tmp= array[i-1]; array[i-1] = array[i]; array[i] = tmp; tmp = secondary[i-1]; secondary[i-1] = secondary[i]; secondary[i] = tmp; } //move down rect.x += buttonSize+space; if (GUI.Button(rect, "∨ ") && i != array.Length-1) { InstantGuiElement tmp = array[i+1]; array[i+1] = array[i]; array[i] = tmp; tmp = secondary[i+1]; secondary[i+1] = secondary[i]; secondary[i] = tmp; } //delete rect.x += buttonSize+space; if (GUI.Button (rect, "X")) { newArray = new InstantGuiElement[array.Length-1]; // for (int j=0; j<newArray.Length; j++) { if (j<i) newArray[j] = array[j]; else newArray[j] = array[j+1]; } } } if (array.Length>6) EditorGUILayout.EndScrollView(); //adding a little space after if (array.Length>0) GUILayoutUtility.GetRect (150, 2, "TextField"); //adding new obj rect = GUILayoutUtility.GetRect (150, 16, "TextField"); rect.x+=indent*indentPixels; rect.width-=indent*indentPixels; rect.width -= (buttonSize+space)*3; rect.width = rect.width*0.5f - 2; InstantGuiElement addedVal = (InstantGuiElement)EditorGUI.ObjectField (rect, null, typeof(InstantGuiElement), true); // if (addedVal!=null) { newArray = new InstantGuiElement[array.Length+1]; // for (int j=0; j<array.Length; j++) newArray[j] = array[j]; newArray[newArray.Length-1] = addedVal; } //adding a little space after rect = GUILayoutUtility.GetRect (150, 3, "TextField"); EditorGUI.indentLevel = indent; return newArray; }
static public void EditFrame ( InstantGuiElement element ) { Undo.RecordObject(element, "InstantGui Move"); //getting mouse pos Vector2 mousePos= Event.current.mousePosition; mousePos.y = InstantGui.Invert(mousePos.y); //shift-aligning if (Event.current.shift) { if (Mathf.Abs(mousePos.x-dragStart.x) < Mathf.Abs(mousePos.y-dragStart.y)) mousePos.x = dragStart.x; else mousePos.y = dragStart.y; } //getting mouse button bool mouseDown=false; bool mouseUp=false; if (Event.current.type == EventType.MouseDown && Event.current.button == 0) { mouseDown = true; dragStart = mousePos; } if (Event.current.type == EventType.MouseUp && Event.current.button == 0) { mouseUp = true; } //selecting locked element if (lockedElement!=null) { //element = lockedElement; //Selection.activeGameObject = lockedElement.gameObject; } //duplicating element if (Event.current.type == EventType.ValidateCommand && Event.current.commandName == "Duplicate") { GameObject newElement = (GameObject)GameObject.Instantiate(element.gameObject); newElement.name = element.gameObject.name; if (element.transform.parent!=null) newElement.transform.parent = element.transform.parent; newElement.transform.localPosition = element.transform.localPosition; newElement.transform.localRotation = element.transform.localRotation; newElement.transform.localScale = element.transform.localScale; Selection.activeGameObject = newElement; element = newElement.GetComponent<InstantGuiElement>(); } //key events if (Event.current.type == EventType.keyDown) { //Undo.RegisterUndo(element, "Move PureGUI"); //locking element if (Event.current.keyCode == KeyCode.L) { if (!lockedElement) lockedElement = element; else lockedElement = null; } //hiding frame if (Event.current.keyCode == KeyCode.H) { drawFrames = !drawFrames; } //moving with keys if (Event.current.keyCode == KeyCode.UpArrow) { element.offset.top--; element.offset.bottom--; } if (Event.current.keyCode == KeyCode.DownArrow) { element.offset.top++; element.offset.bottom++; } if (Event.current.keyCode == KeyCode.LeftArrow) { element.offset.left--; element.offset.right--; } if (Event.current.keyCode == KeyCode.RightArrow) { element.offset.left++; element.offset.right++; } } Rect rect = new Rect(0,0,frameSize,frameSize); Rect textRect = new Rect(0,0,40,20); //getting if any of dimension is fixed bool fixedWidth = false; bool fixedHeight = false; if (element.style!=null) { if (element.style.fixedWidth) fixedWidth = true; if (element.style.fixedHeight) fixedHeight = true; } if (mouseDown) { nonGridOffset.left = element.offset.left; nonGridOffset.right = element.offset.right; nonGridOffset.top = element.offset.top; nonGridOffset.bottom = element.offset.bottom; nonGridRelative.left = element.relative.left; nonGridRelative.right = element.relative.right; nonGridRelative.top = element.relative.top; nonGridRelative.bottom = element.relative.bottom; } if (mouseUp || mouseDown) dragging = 0; //storing borders to assign comfortable int left = element.absolute.left; int right = element.absolute.right; int top = element.absolute.top; int bottom = element.absolute.bottom; //relative left rect.width = frameSize * 1.5f; rect.height = frameSize; rect.x = left - element.offset.left - rect.width*0.5f; rect.y = element.absolute.GetCenter().y - rect.height*0.5f; textRect.x = rect.x; textRect.y = rect.y+16; if (rect.Contains(Event.current.mousePosition) && mouseDown && dragging==0 && drawFrames && !Event.current.alt && !element.lockPosition) { dragging = 1; } if (dragging==1) { nonGridRelative.left = Mathf.RoundToInt(InstantGuiElementPos.ScreenPointToRelative(element.parentpos, Event.current.mousePosition).x); if (Event.current.control) { element.relative.left = (Mathf.RoundToInt(nonGridRelative.left*0.2f))*5; } else { element.relative.left = nonGridRelative.left; } element.offset.GetOffset(element.parentpos, element.relative, element.absolute); } //relative right if (!fixedWidth) { rect.x = right - element.offset.right - rect.width*0.5f; rect.y = element.absolute.GetCenter().y - rect.height*0.5f; textRect.x = rect.x; textRect.y = rect.y-20; if (rect.Contains(Event.current.mousePosition) && mouseDown && dragging==0 && drawFrames && !Event.current.alt && !element.lockPosition) { dragging = 2; } if (dragging==2) { nonGridRelative.right = Mathf.RoundToInt(InstantGuiElementPos.ScreenPointToRelative(element.parentpos, Event.current.mousePosition).x); if (Event.current.control) { element.relative.right = (Mathf.RoundToInt(nonGridRelative.right*0.2f))*5; } else { element.relative.right = nonGridRelative.right; } element.offset.GetOffset(element.parentpos, element.relative, element.absolute); } } else element.relative.right = element.relative.left; //relative top rect.width = frameSize; rect.height = frameSize * 1.5f; rect.x = element.absolute.GetCenter().x - rect.width*0.5f; rect.y = top - element.offset.top - rect.height*0.5f; textRect.x = rect.x+20; textRect.y = rect.y-2; if (rect.Contains(Event.current.mousePosition) && mouseDown && dragging==0 && drawFrames && !Event.current.alt && !element.lockPosition) { dragging = 3; } if (dragging==3) { nonGridRelative.top = Mathf.RoundToInt(InstantGuiElementPos.ScreenPointToRelative(element.parentpos, Event.current.mousePosition).y); if (Event.current.control) { element.relative.top = (Mathf.RoundToInt(nonGridRelative.top*0.2f))*5; } else { element.relative.top = nonGridRelative.top; } element.offset.GetOffset(element.parentpos, element.relative, element.absolute); } //relative bottom if (!fixedHeight) { rect.x = element.absolute.GetCenter().x - rect.width*0.5f; rect.y = bottom - element.offset.bottom - rect.height*0.5f; textRect.x = rect.x-30; textRect.y = rect.y-2; if (rect.Contains(Event.current.mousePosition) && mouseDown && dragging==0 && drawFrames && !Event.current.alt && !element.lockPosition) { dragging = 4; } if (dragging==4) { nonGridRelative.bottom = Mathf.RoundToInt(InstantGuiElementPos.ScreenPointToRelative(element.parentpos, Event.current.mousePosition).y); if (Event.current.control) { element.relative.bottom = (Mathf.RoundToInt(nonGridRelative.bottom*0.2f))*5; } else { element.relative.bottom = nonGridRelative.bottom; } element.offset.GetOffset(element.parentpos, element.relative, element.absolute); } } else element.relative.bottom = element.relative.top; rect = new Rect(0,0,frameSize,frameSize); if (!fixedWidth || !fixedHeight) { //left-top rect.x = element.absolute.left-frameSize*0.5f; rect.y = element.absolute.top-frameSize*0.5f; textRect.x = rect.x-25; textRect.y = (top+bottom)*0.5f + (top-bottom)*0.25f; if (rect.Contains(Event.current.mousePosition) && mouseDown && dragging==0 && drawFrames && !Event.current.alt && !element.lockPosition) { dragging = 5; } if (dragging==5) { element.offset.left = (int)(nonGridOffset.left + (mousePos.x-dragStart.x)); element.offset.top = (int)(nonGridOffset.top + (dragStart.y-mousePos.y)); if (Event.current.control) { element.offset.left = Mathf.RoundToInt(element.offset.left*0.1f)*10; element.offset.top = Mathf.RoundToInt(element.offset.top*0.1f)*10; } } //left-bottom rect.x = element.absolute.left-frameSize*0.5f; rect.y = element.absolute.bottom-frameSize*0.5f; textRect.x = (left+right)*0.5f + (right-left)*0.25f; textRect.y = rect.y+10; if (rect.Contains(Event.current.mousePosition) && mouseDown && dragging==0 && drawFrames && !Event.current.alt && !element.lockPosition) { dragging = 6; } if (dragging==6) { element.offset.left = (int)(nonGridOffset.left + (mousePos.x-dragStart.x)); element.offset.bottom = (int)(nonGridOffset.bottom + (dragStart.y-mousePos.y)); if (Event.current.control) { element.offset.left = Mathf.RoundToInt(element.offset.left*0.1f)*10; element.offset.bottom = Mathf.RoundToInt(element.offset.bottom*0.1f)*10; } } //right-top rect.x = element.absolute.right-frameSize*0.5f; rect.y = element.absolute.top-frameSize*0.5f; textRect.x = (left+right)*0.5f + (left-right)*0.25f; textRect.y = rect.y-13; if (rect.Contains(Event.current.mousePosition) && mouseDown && dragging==0 && drawFrames && !Event.current.alt && !element.lockPosition) { dragging = 7; } if (dragging==7) { element.offset.right = (int)(nonGridOffset.right + (mousePos.x-dragStart.x)); element.offset.top = (int)(nonGridOffset.top + (dragStart.y-mousePos.y)); if (Event.current.control) { element.offset.right = Mathf.RoundToInt(element.offset.right*0.1f)*10; element.offset.top = Mathf.RoundToInt(element.offset.top*0.1f)*10; } } //right-bottom rect.x = element.absolute.right-frameSize*0.5f; rect.y = element.absolute.bottom-frameSize*0.5f; textRect.x = rect.x+15; textRect.y = (top+bottom)*0.5f + (bottom-top)*0.25f; if (rect.Contains(Event.current.mousePosition) && mouseDown && dragging==0 && drawFrames && !Event.current.alt && !element.lockPosition) { dragging = 8; } if (dragging==8) { element.offset.right = (int)(nonGridOffset.right + (mousePos.x-dragStart.x)); element.offset.bottom = (int)(nonGridOffset.bottom + (dragStart.y-mousePos.y)); if (Event.current.control) { element.offset.right = Mathf.RoundToInt(element.offset.right*0.1f)*10; element.offset.bottom = Mathf.RoundToInt(element.offset.bottom*0.1f)*10; } } } //aligning if (!InstantGui.instance) InstantGui.instance = (InstantGui)FindObjectOfType(typeof(InstantGui)); if (!EditorApplication.isPlaying) { //InstantGui.instance.Update(); //InstantGui.instance.element.Point(mousePos); InstantGui.width = Screen.width; InstantGui.height = Screen.height; InstantGui.instance.element = InstantGui.instance.GetComponent<InstantGuiElement>(); InstantGui.pointed = null; InstantGui.instance.element.GetChildren(); InstantGui.instance.element.Align(); //element.PreventZeroSize(true); InstantGui.instance.element.Point(true); InstantGui.instance.element.Action(); InstantGui.instance.element.ApplyStyle(); } //moving selected element (or setting fixed) rect = element.absolute.ToRect(); if (InstantGui.pointed==element && mouseDown && dragging==0 && !Event.current.alt && !element.lockPosition && !selectionChanged) { dragging = 9; } if (dragging==9) { //Undo.RecordObject(element, "InstantGui Move"); int sizeX = element.absolute.right-element.absolute.left; int sizeY = element.absolute.bottom-element.absolute.top; element.offset.left = (int)(nonGridOffset.left + (mousePos.x-dragStart.x)); element.offset.top = (int)(nonGridOffset.top + (dragStart.y-mousePos.y)); if (Event.current.control) { element.offset.left = Mathf.RoundToInt(element.offset.left*0.1f)*10; element.offset.top = Mathf.RoundToInt(element.offset.top*0.1f)*10; } element.absolute.GetAbsolute (element.parentpos, element.relative, element.offset); element.offset.GetOffset(element.parentpos, element.relative, new InstantGuiElementPos(element.absolute.left, element.absolute.left + sizeX, element.absolute.top, element.absolute.top + sizeY)); } //selecting other objs if (mouseDown && dragging==0 && InstantGui.pointed!=element && InstantGui.pointed!=null && !lockedElement && !Event.current.alt) { Selection.activeGameObject = InstantGui.pointed.gameObject; element = InstantGui.pointed; nonGridOffset.left = element.offset.left; nonGridOffset.right = element.offset.right; nonGridOffset.top = element.offset.top; nonGridOffset.bottom = element.offset.bottom; nonGridRelative.left = element.relative.left; nonGridRelative.right = element.relative.right; nonGridRelative.top = element.relative.top; nonGridRelative.bottom = element.relative.bottom; selectionChanged = true; } else if (selectionChanged) selectionChanged = false; }
static public void DrawSaveToStyleButton ( string label , InstantGuiElement element , bool saveStyle , bool savePos ){ //if (!saveStyle && savePos && element.customStyle) return; Rect rect= GUILayoutUtility.GetRect (50, 18, "TextField"); rect.x=30; rect.width-=15; if (GUI.Button(rect, label)) { //finding in set bool foundInSet = false; for (int j=0; j<element.styleSet.styles.Length; j++) if (element.styleSet.styles[j].name == element.style.name) { if (saveStyle) element.styleSet.styles[j] = element.style.Clone(); if (savePos) { element.styleSet.styles[j].relative = element.relative.Clone(); element.styleSet.styles[j].offset = element.offset.Clone(); element.styleSet.styles[j].layerOffset = element.layerOffset; } foundInSet = true; } //adding new if (!foundInSet) { #if UNITY_EDITOR if (!saveStyle) { if (EditorUtility.DisplayDialog("No Such Style", "Cannot find style in Style Set. Do you want to create it?", "Create", "Cancel")) saveStyle = true; else { EditorUtility.SetDirty(element.styleSet); return; } } #endif InstantGuiStyle[] newStyles = new InstantGuiStyle[element.styleSet.styles.Length+1]; for (int j=0; j<element.styleSet.styles.Length; j++) newStyles[j] = element.styleSet.styles[j]; if (saveStyle) newStyles[newStyles.Length-1] = element.style.Clone(); if (savePos) { newStyles[newStyles.Length-1].relative = element.relative.Clone(); newStyles[newStyles.Length-1].offset = element.offset.Clone(); newStyles[newStyles.Length-1].layerOffset = element.layerOffset; } element.styleSet.styles = newStyles; } #if UNITY_EDITOR EditorUtility.SetDirty(element.styleSet); #endif } }
static public void DrawFrame(InstantGuiElement element) { if (!drawFrames) { return; } //setting textures if (frameTexture == null) { frameTexture = GetTexture("GuiFrameSquare.png"); } if (lineTexture == null) { lineTexture = GetTexture("GuiFrameLine.png"); } if (squareTexture == null) { squareTexture = GetTexture("GuiFrameSquare.png"); } if (circleTexture == null) { circleTexture = GetTexture("GuiFrameCircle.png"); } if (triBottomTexture == null) { triBottomTexture = GetTexture("GuiFrameArrowBottom.png"); } if (triTopTexture == null) { triTopTexture = GetTexture("GuiFrameArrowTop.png"); } if (triLeftTexture == null) { triLeftTexture = GetTexture("GuiFrameArrowLeft.png"); } if (triRightTexture == null) { triRightTexture = GetTexture("GuiFrameArrowRight.png"); } if (lockTexture == null) { lockTexture = GetTexture("GuiFrameLock.png"); } //getting if any of dimension is fixed bool fixedWidth = false; bool fixedHeight = false; if (element.style != null) { if (element.style.fixedWidth) { fixedWidth = true; } if (element.style.fixedHeight) { fixedHeight = true; } } Rect rect = new Rect(0, 0, frameSize, frameSize); Rect textRect = new Rect(0, 0, 40, 20); //storing borders to assign comfortable int left = element.absolute.left; int right = element.absolute.right; int top = element.absolute.top; int bottom = element.absolute.bottom; //drawing lines GUI.DrawTexture(new Rect(left, (top + bottom) * 0.5f, -element.offset.left, 1), lineTexture); if (!fixedWidth) { GUI.DrawTexture(new Rect(right, (top + bottom) * 0.5f, -element.offset.right, 1), lineTexture); } GUI.DrawTexture(new Rect((left + right) * 0.5f, top, 1, -element.offset.top), lineTexture); if (!fixedHeight) { GUI.DrawTexture(new Rect((left + right) * 0.5f, bottom, 1, -element.offset.bottom), lineTexture); } GUI.DrawTexture(new Rect(left, top, right - left, 1), lineTexture); GUI.DrawTexture(new Rect(left, bottom, right - left, 1), lineTexture); GUI.DrawTexture(new Rect(left, top, 1, bottom - top), lineTexture); GUI.DrawTexture(new Rect(right, top, 1, bottom - top), lineTexture); //drawing point offset if (element.style != null && (element.style.pointOffset.left != 0 || element.style.pointOffset.right != 0 || element.style.pointOffset.top != 0 || element.style.pointOffset.bottom != 0)) { RectOffset pointOffset = element.style.pointOffset; GUI.DrawTexture(new Rect(left + pointOffset.left, top + pointOffset.top, right - left - pointOffset.left - pointOffset.right, 1), lineTexture); GUI.DrawTexture(new Rect(left + pointOffset.left, bottom - pointOffset.bottom, right - left - pointOffset.left - pointOffset.right, 1), lineTexture); GUI.DrawTexture(new Rect(left + pointOffset.left, top + pointOffset.top, 1, bottom - top - pointOffset.bottom - pointOffset.top), lineTexture); GUI.DrawTexture(new Rect(right - pointOffset.right, top + pointOffset.top, 1, bottom - top - pointOffset.bottom - pointOffset.top), lineTexture); } //lock if (lockedElement != null) { GUI.DrawTexture(new Rect(left + 5, top + 5, 24, 24), lockTexture); } //relative right rect.width = frameSize * 1.5f; rect.height = frameSize; if (!fixedWidth) { rect.x = right - element.offset.right - rect.width * 0.5f; rect.y = element.absolute.GetCenter().y - rect.height * 0.5f; GUI.DrawTexture(rect, triRightTexture); if (showValues == InstantGuiFrameShowValues.always || (showValues == InstantGuiFrameShowValues.onChange && dragging == 2)) { textRect.x = rect.x - frameSize * 0.25f - 15; textRect.y = rect.y - frameSize * 0.25f - 15; GUI.Label(textRect, element.relative.right.ToString() + "%"); } } else { element.relative.right = element.relative.left; } //relative left rect.x = left - element.offset.left - rect.width * 0.5f; rect.y = element.absolute.GetCenter().y - rect.height * 0.5f; GUI.DrawTexture(rect, triLeftTexture); if (showValues == InstantGuiFrameShowValues.always || (showValues == InstantGuiFrameShowValues.onChange && dragging == 1)) { //textRect.x = rect.x+frameSize*0.25f+15; textRect.y = rect.y+frameSize*0.1f+17; textRect.x = rect.x - frameSize * 0.25f - 15; textRect.y = rect.y - frameSize * 0.25f - 15; GUI.Label(textRect, element.relative.left.ToString() + "%"); } //relative bottom rect.width = frameSize; rect.height = frameSize * 1.5f; if (!fixedHeight) { rect.x = element.absolute.GetCenter().x - rect.width * 0.5f; rect.y = bottom - element.offset.bottom - rect.height * 0.5f; GUI.DrawTexture(rect, triBottomTexture); if (showValues == InstantGuiFrameShowValues.always || (showValues == InstantGuiFrameShowValues.onChange && dragging == 4)) { //textRect.x = rect.x-frameSize-10; textRect.y = rect.y-frameSize*0.05f-20; textRect.x = rect.x - frameSize * 0.25f - 15; textRect.y = rect.y - frameSize * 0.25f - 15; GUI.Label(textRect, element.relative.bottom.ToString() + "%"); } } else { element.relative.bottom = element.relative.top; } //relative top rect.x = element.absolute.GetCenter().x - rect.width * 0.5f; rect.y = top - element.offset.top - rect.height * 0.5f; GUI.DrawTexture(rect, triTopTexture); if (showValues == InstantGuiFrameShowValues.always || (showValues == InstantGuiFrameShowValues.onChange && dragging == 3)) { //textRect.x = rect.x+frameSize*1.1f; textRect.y = rect.y+frameSize; textRect.x = rect.x - frameSize * 0.25f - 15; textRect.y = rect.y - frameSize * 0.25f - 15; GUI.Label(textRect, element.relative.top.ToString() + "%"); } rect = new Rect(0, 0, frameSize, frameSize); if (!fixedWidth || !fixedHeight) { //left-top 5 rect.x = element.absolute.left - frameSize * 0.5f; rect.y = element.absolute.top - frameSize * 0.5f; textRect.x = rect.x - 25; textRect.y = (top + bottom) * 0.5f + (top - bottom) * 0.25f; GUI.DrawTexture(rect, circleTexture); if (showValues == InstantGuiFrameShowValues.always || (showValues == InstantGuiFrameShowValues.onChange && (dragging == 6 || dragging == 5 || dragging == 9))) { GUI.Label(textRect, element.offset.left.ToString() + "p"); } //left-bottom 6 rect.x = element.absolute.left - frameSize * 0.5f; rect.y = element.absolute.bottom - frameSize * 0.5f; textRect.x = (left + right) * 0.5f + (right - left) * 0.25f; textRect.y = rect.y + 10; GUI.DrawTexture(rect, circleTexture); if (showValues == InstantGuiFrameShowValues.always || (showValues == InstantGuiFrameShowValues.onChange && (dragging == 8 || dragging == 6 || dragging == 9))) { GUI.Label(textRect, element.offset.bottom.ToString() + "p"); } //right-top 7 rect.x = element.absolute.right - frameSize * 0.5f; rect.y = element.absolute.top - frameSize * 0.5f; textRect.x = (left + right) * 0.5f + (left - right) * 0.25f; textRect.y = rect.y - 13; GUI.DrawTexture(rect, circleTexture); if (showValues == InstantGuiFrameShowValues.always || (showValues == InstantGuiFrameShowValues.onChange && (dragging == 5 || dragging == 7 || dragging == 9))) { GUI.Label(textRect, element.offset.top.ToString() + "p"); } //right-bottom 8 rect.x = element.absolute.right - frameSize * 0.5f; rect.y = element.absolute.bottom - frameSize * 0.5f; textRect.x = rect.x + 15; textRect.y = (top + bottom) * 0.5f + (bottom - top) * 0.25f; GUI.DrawTexture(rect, circleTexture); if (showValues == InstantGuiFrameShowValues.always || (showValues == InstantGuiFrameShowValues.onChange && (dragging == 7 || dragging == 8 || dragging == 9))) { GUI.Label(textRect, element.offset.right.ToString() + "p"); } } }
static public InstantGuiElement Create ( string name , System.Type type , InstantGuiElement parent ){ return Create(name, name, type, parent); }
static public InstantGuiElement Create(string name, System.Type type, InstantGuiElement parent) { return(Create(name, name, type, parent)); }
public override void Align() { base.Align(); //setting first shown value if (slider != null) { slider.min = 0; slider.max = Mathf.Max(0, labels.Length - elements.Length); slider.shownValue = elements.Length; //scrolling slider slider.value -= Input.GetAxisRaw("Mouse ScrollWheel") * 10; slider.value = Mathf.Clamp(slider.value, slider.min, slider.max); firstShown = (int)slider.value; } //calculating number of lines int linesNum; if (lineHeight <= 0) { linesNum = elements.Length; } else { linesNum = Mathf.FloorToInt((absolute.bottom - absolute.top) * 1.0f / lineHeight); linesNum = Mathf.Max(linesNum, 0); } //re-creating elements if line num changed if (elements.Length != linesNum) { //destroying overcount for (int i = elements.Length - 1; i >= linesNum; i--) { if (elements[i] != null) { StartCoroutine(elements[i].YieldAndDestroy()); //DestroyImmediate(elements[i].gameObject); } } //resizing array InstantGuiElement[] newElements = new InstantGuiElement[linesNum]; int count = Mathf.Min(linesNum, elements.Length); for (int i = 0; i < count; i++) { newElements[i] = elements[i]; } elements = newElements; //creating undefened elements for (int i = 0; i < elements.Length; i++) { if (!elements[i]) { elements[i] = InstantGuiElement.Create("ListElement", typeof(InstantGuiElement), this); } } } //setting text and position for (int i = 0; i < elements.Length; i++) { if (i + firstShown < labels.Length && firstShown >= 0) { elements[i].text = labels[i + firstShown]; } else { elements[i].text = ""; } elements[i].offset = new InstantGuiElementPos(0, -sliderMargin, 0, 0); elements[i].relative = new InstantGuiElementPos(0, 100, (int)((100.0f / linesNum) * i), (int)((100.0f / linesNum) * (i + 1))); elements[i].editable = false; //elements[i].style = elementStyle; } }