public bool CheckMouseOver(Rect rect) { if (!root) { root = OGRoot.GetInstance(); } Vector2 pos = new Vector2(Input.mousePosition.x * root.reverseRatio.x, Input.mousePosition.y * root.reverseRatio.y); if (SystemInfo.deviceType == DeviceType.Handheld && Input.touchCount == 0) { return(false); } return(rect.Contains(pos)); }
public void UpdateStyles() { foreach (OGWidget w in this.transform.GetComponentsInChildren <OGWidget>(true)) { if (w.root == null) { w.root = OGRoot.GetInstance(); } if (w.styles == null) { w.ApplyDefaultStyles(); } w.styles.Refresh(w.root.skin); } }
public void ApplyDefaultStyles() { if (!root) { root = OGRoot.GetInstance(); } OGSkin skin = root.skin; if (!skin) { Debug.LogWarning("OpenGUI | No OGSkin attached to OGRoot"); } else { skin.ApplyDefaultStyles(this); } }
//////////////////// // Update //////////////////// override public void UpdateWidget() { // Persistent vars isSelectable = true; if (CheckMouseOver()) { currentStyle = styles.hover; } else { currentStyle = styles.basic; } // Mouse if (Input.GetMouseButtonUp(0) || GetTouch() == TouchPhase.Ended) { if (OGRoot.GetInstance().downWidget&& OGRoot.GetInstance().downWidget.isDraggable&& CheckMouseOver()) { if (containedObject) { Destroy(containedObject.gameObject); } containedObject = (OGWidget)Instantiate(OGRoot.GetInstance().downWidget); containedObject.gameObject.name = containedObject.gameObject.name.Replace("(Clone)", ""); containedObject.transform.parent = this.transform; containedObject.transform.localScale = new Vector3(containedScale, containedScale, 1); containedObject.transform.localPosition = new Vector3(1 - containedScale, 1 - containedScale, -1); containedObject.isSelectable = false; containedObject.isDraggable = false; if (target && !string.IsNullOrEmpty(droppedMessage)) { target.SendMessage(droppedMessage, this); } } } mouseRct = drawRct; }
override public void OnMouseCancel() { isUp = false; OGRoot.GetInstance().ReleaseWidget(); }
////////////////// // Init ////////////////// public void Awake() { root = OGRoot.GetInstance(); }
public static void DrawSprite(Rect rect, Rect uvRect, float depth, Color color, Color tint, OGWidget clipping) { if (!root) { root = OGRoot.GetInstance(); return; } // Check screen if (rect.xMin > root.screenWidth || rect.xMax < 0 || rect.yMax < 0 || rect.yMin > root.screenHeight) { return; } // Color color.r *= tint.r; color.g *= tint.g; color.b *= tint.b; color.a *= tint.a; GL.Color(color); // Quad corners float left = rect.x; float right = rect.x + rect.width; float bottom = rect.y; float top = rect.y + rect.height; // Check clipping if (clipping != null) { if (rect.xMin > clipping.drawRct.xMax || rect.xMax < clipping.drawRct.xMin || rect.yMax < clipping.drawRct.yMin || rect.yMin > clipping.drawRct.yMax) { return; } else { if (left < clipping.drawRct.xMin) { left = clipping.drawRct.xMin; } if (right > clipping.drawRct.xMax) { right = clipping.drawRct.xMax; } if (bottom < clipping.drawRct.yMin) { bottom = clipping.drawRct.yMin; } if (top > clipping.drawRct.yMax) { top = clipping.drawRct.yMax; } } } uvRect.x /= texSize.x; uvRect.y /= texSize.y; uvRect.width /= texSize.x; uvRect.height /= texSize.y; // Bottom Left GL.TexCoord2(uvRect.x, uvRect.y); GL.Vertex3(left, bottom, depth); // Top left GL.TexCoord2(uvRect.x, uvRect.y + uvRect.height); GL.Vertex3(left, top, depth); // Top right GL.TexCoord2(uvRect.x + uvRect.width, uvRect.y + uvRect.height); GL.Vertex3(right, top, depth); // Bottom right GL.TexCoord2(uvRect.x + uvRect.width, uvRect.y); GL.Vertex3(right, bottom, depth); // Reset color GL.Color(Color.white); }
public static void DrawLabel(Rect rect, string str, OGTextStyle style, int intSize, TextAnchor alignment, float depth, Color tint, OGWidget clipping, OGTextEditor editor) { // Check root if (root == null) { root = OGRoot.GetInstance(); return; } // Check font if (style.font == null) { style.font = OGRoot.GetInstance().skin.fonts [style.fontIndex]; return; } // Check string if (string.IsNullOrEmpty(str)) { if (editor != null) { editor.cursorIndex = 0; editor.cursorSelectIndex = 0; editor.cursorPos.x = rect.xMin; editor.cursorPos.y = rect.yMin - style.fontSize; } return; } // Check screen if (rect.xMin > root.screenWidth || rect.xMax < 0 || rect.yMax < 0 || rect.yMin > root.screenHeight) { return; } // Check clipping if (clipping != null) { if (rect.xMin > clipping.drawRct.xMax || rect.xMax < clipping.drawRct.xMin || rect.yMax < clipping.drawRct.yMin || rect.yMin > clipping.drawRct.yMax) { return; } } // Scale float size = (intSize * 1.0f) / style.font.size; Vector2 atlasSize = style.font.atlasSize; // Bounds float left = style.padding.left; float right = rect.width - style.padding.right - style.padding.left; float top = rect.height - style.padding.top; float bottom = style.padding.bottom; float middle = (rect.height / 2) + ((style.font.font.lineHeight * size) / 2); float center = left + right / 2; // Positioning Vector2 anchor = Vector2.zero; float space = (style.font.GetCharacterInfo(" "[0]).advance *size); // Line and progression management Vector2 advance = Vector2.zero; int nextLineStart = 0; int thisLineStart = 0; int lastSpace = 0; float lineWidth = 0; float lineWidthAtLastSpace = 0; float lineHeight = style.font.font.lineHeight * size; int emergencyBrake = 0; // Temp vars CharacterInfo info; // Set anchor switch (alignment) { case TextAnchor.UpperLeft: anchor.x = left; anchor.y = top; break; case TextAnchor.MiddleLeft: anchor.x = left; anchor.y = middle; break; case TextAnchor.LowerLeft: anchor.x = left; anchor.y = bottom; break; case TextAnchor.UpperCenter: anchor.x = center; anchor.y = top; break; case TextAnchor.MiddleCenter: anchor.x = center; anchor.y = middle; break; case TextAnchor.LowerCenter: anchor.x = center; anchor.y = bottom; break; case TextAnchor.UpperRight: anchor.x = right; anchor.y = top; break; case TextAnchor.MiddleRight: anchor.x = right; anchor.y = middle; break; case TextAnchor.LowerRight: anchor.x = right; anchor.y = bottom; break; } // Color Color color = style.fontColor; color.r *= tint.r; color.g *= tint.g; color.b *= tint.b; color.a *= tint.a; GL.Color(color); // Draw loop while (nextLineStart < str.Length && advance.y - style.padding.top > -(rect.height - style.padding.top - style.padding.bottom)) { int c = 0; // Get next line lastSpace = 0; lineWidth = 0; thisLineStart = nextLineStart; // ^ Parse remaining string, set start and end integers for (c = thisLineStart; c < str.Length; c++) { info = style.font.GetCharacterInfo(str[c]); // This character is a carriage return if (str[c] == "\n"[0]) { nextLineStart = c + 1; break; // This character is a space } else if (str[c] == " "[0]) { lineWidthAtLastSpace = lineWidth; lineWidth += space; lastSpace = c; // This character is a regular glyph } else { lineWidth += info.advance * size; } // The line width has exceeded the border if (lineWidth >= right) { nextLineStart = lastSpace == 0 ? lastSpace : c + 1; lineWidth = lineWidthAtLastSpace; break; } } // The string has ended if (c >= str.Length - 1) { nextLineStart = str.Length; } // Alignment advance adjustments if (anchor.x == center) { advance.x -= lineWidth / 2; } else if (anchor.x == right) { advance.x -= lineWidth; } // Draw glyphs for (int g = thisLineStart; g < nextLineStart; g++) { info = style.font.GetCharacterInfo(str[g]); Rect vert = Rect.MinMaxRect(info.minX * size, info.maxY * size, info.maxX * size, info.minY * size); Vector2 uvBottomLeft = info.uvBottomLeft; Vector2 uvTopLeft = info.uvTopLeft; Vector2 uvTopRight = info.uvTopRight; Vector2 uvBottomRight = info.uvBottomRight; // Quad corners float gLeft = anchor.x + vert.x + rect.x + advance.x; float gRight = anchor.x + vert.x + rect.x + advance.x + vert.width; float gBottom = anchor.y + vert.height + vert.y + rect.y + advance.y - lineHeight; float gTop = anchor.y + vert.height + vert.y + rect.y + advance.y - vert.height - lineHeight; // If it's a space, set appropriate corners if (str[g] == " "[0]) { gRight += space; } // Set cursor position if (editor != null) { if (editor.cursorIndex == g) { editor.cursorPos.x = gLeft; editor.cursorPos.y = gBottom; } else if (editor.cursorIndex >= editor.str.Length && g == editor.str.Length - 1) { editor.cursorPos.x = gRight; editor.cursorPos.y = gBottom; } if (editor.cursorSelectIndex == g) { editor.cursorSelectPos.x = gLeft; editor.cursorSelectPos.y = gBottom; } else if (editor.cursorSelectIndex >= editor.str.Length && g == editor.str.Length - 1) { editor.cursorSelectPos.x = gRight; editor.cursorSelectPos.y = gBottom; } editor.cursorSize.x = 1; editor.cursorSize.y = style.fontSize; } // If it's a space, continue the loop if (str[g] == " "[0]) { advance.x += space; continue; } // Advance regardless if the glyph is drawn or not advance.x += info.advance * size; // Clipping if (clipping != null) { if (gLeft < clipping.drawRct.xMin) { uvBottomLeft.x += (clipping.drawRct.xMin - gLeft) / atlasSize.x; uvTopLeft.x += (clipping.drawRct.xMin - gLeft) / atlasSize.x; gLeft = clipping.drawRct.xMin; } if (gRight > clipping.drawRct.xMax) { uvTopRight.x -= (gRight - clipping.drawRct.xMax) / atlasSize.x; uvBottomRight.x -= (gRight - clipping.drawRct.xMax) / atlasSize.x; gRight = clipping.drawRct.xMax; } if (gBottom < clipping.drawRct.yMin) { uvBottomLeft.y += (clipping.drawRct.yMin - gBottom) / atlasSize.y; uvBottomRight.y += (clipping.drawRct.yMin - gBottom) / atlasSize.y; gBottom = clipping.drawRct.yMin; } if (gTop > clipping.drawRct.yMax) { uvTopLeft.y += (gTop - clipping.drawRct.yMax) / atlasSize.y; uvTopRight.y += (gTop - clipping.drawRct.yMax) / atlasSize.y; gTop = clipping.drawRct.yMax; } // If the sides overlap, the glyph shouldn't be drawn if (gLeft >= gRight || gBottom >= gTop) { continue; } } // Bottom Left GL.TexCoord2(uvBottomLeft.x, uvBottomLeft.y); GL.Vertex3(gLeft, gBottom, depth); // Top left GL.TexCoord2(uvTopLeft.x, uvTopLeft.y); GL.Vertex3(gLeft, gTop, depth); // Top right GL.TexCoord2(uvTopRight.x, uvTopRight.y); GL.Vertex3(gRight, gTop, depth); // Bottom right GL.TexCoord2(uvBottomRight.x, uvBottomRight.y); GL.Vertex3(gRight, gBottom, depth); } // Next line advance.y -= lineHeight; advance.x = 0; // Emergency if (emergencyBrake > 1000) { Debug.Log("OGDrawHelper | Label exceeded 1000 lines!"); return; } else { emergencyBrake++; } } GL.Color(Color.white); }
override public void OnInspectorGUI() { serializedObject.Update(); OGPage page = (OGPage)target; OGRoot root = OGRoot.GetInstance(); if (!page || !root) { return; } DrawDefaultInspector(); EditorGUILayout.Space(); GUI.backgroundColor = Color.red; if (GUILayout.Button("Reset styles")) { page.ResetStyles(); } GUI.backgroundColor = Color.green; foreach (OGPage p in root.currentPages) { if (p == page) { GUILayout.BeginHorizontal(); if (GUILayout.Button("Update", GUILayout.Height(30))) { page.UpdateStyles(); GUI.backgroundColor = Color.white; } if (GUILayout.Button("-", GUILayout.Height(30), GUILayout.Width(40))) { OGRoot.GetInstance().RemoveFromCurrentPages(page); page.gameObject.SetActive(false); } GUILayout.EndHorizontal(); return; } } GUILayout.BeginHorizontal(); if (GUILayout.Button("Set current page", GUILayout.Height(30))) { OGRoot.GetInstance().SetCurrentPage(page); page.gameObject.SetActive(true); } if (GUILayout.Button("+", GUILayout.Height(30), GUILayout.Width(40))) { OGRoot.GetInstance().AddToCurrentPages(page); page.gameObject.SetActive(true); } GUILayout.EndHorizontal(); GUI.backgroundColor = Color.white; }
// You must use a custom function to switch pages public void SwitchPage() { OGRoot.GetInstance().GoToPage("MyOtherPage"); }