void DrawLeftNavigation() { GUI.Box(new Rect(0,50,300,canvas.height+30),""); GUILayoutOption[] leftOptions = new GUILayoutOption[3]; leftOptions[0] = GUILayout.MinWidth(300); leftOptions[1] = GUILayout.MaxWidth(300); leftOptions[2] = GUILayout.MinHeight(480); GUILayout.BeginArea(new Rect(0,0,300,480)); EditorGUILayout.BeginVertical(leftOptions); string[] titles; #if QUADUI_1_2 GUILayout.Space(20); titles = new string[3]{"Quad","Camera","Settings"}; #elif QUADUI_1_3 titles = new string[4]{"Quad","Complex","Camera","Settings"}; #endif currentPage = GUILayout.Toolbar(currentPage, titles); GUILayout.Space(10); switch(currentPage) { case 0: GUILayout.Label ("Create a New Quad:", EditorStyles.boldLabel); GUILayout.Space(20); _material = (Material) EditorGUILayout.ObjectField (_material, typeof(Material)); GUILayout.Space(10); _name = EditorGUILayout.TextField ("Name:", _name); GUILayout.Space(10); #if QUADUI_1_2 _reg = (RegistrationPoint) EditorGUILayout.EnumPopup ("Pivot Point:", _reg); #elif QUADUI_1_3 EditorGUILayout.PrefixLabel("Quad Pivot Point:"); GUILayout.BeginHorizontal(new GUILayoutOption[1]{GUILayout.Width(200)}); GUILayout.Space(100); _reg = (RegistrationPoint) GUILayout.SelectionGrid((int)_reg,new string[9]{"","","","","","","","",""},3,new GUILayoutOption[1]{GUILayout.Width(100)}); GUILayout.EndHorizontal(); GUILayout.Space(20); #endif _layer = EditorGUILayout.LayerField("Layer:", _layer); GUILayout.Space(20); _prevClass = _class; _class = (QuadUIClass) EditorGUILayout.EnumPopup ("Class:", _class); GUILayout.Space(10); switch(_class) { case QuadUIClass.None: //We don't want to be part of the framework, just generate a quad. currentFrame = -1; numFrames = 0; FixFrames(); _uv = EditorGUILayout.Vector2Field("UV Top-Left:", _uv); widthHeight = EditorGUILayout.Vector2Field("Quad Width and Height", widthHeight); break; case QuadUIClass.Static: numFrames = 0; currentFrame = -1; FixFrames(); _uv = EditorGUILayout.Vector2Field("UV Top-Left:", _uv); widthHeight = EditorGUILayout.Vector2Field("Quad Width and Height", widthHeight); break; case QuadUIClass.Button: numFrames = 3; FixFrames(); if(_prevClass != _class) _frames[0].position = _uv; _frames[0] = new Frame(0, _frames[0].selected, (int) widthHeight.x, (int) widthHeight.y, _frames[0].position, "Up State"); _frames[1] = new Frame(1, _frames[1].selected, (int) widthHeight.x, (int) widthHeight.y, _frames[1].position, "Down State"); _frames[2] = new Frame(2, _frames[2].selected, (int) widthHeight.x, (int) widthHeight.y, _frames[2].position, "Over State"); _frames[currentFrame].position = EditorGUILayout.Vector2Field(_frames[currentFrame].state + "'s UV Top-Left:", _frames[currentFrame].position); widthHeight = EditorGUILayout.Vector2Field("Quad Width and Height:", widthHeight); break; case QuadUIClass.Toggle: numFrames = 4; FixFrames(); if(_prevClass != _class) _frames[0].position = _uv; _frames[0] = new Frame(0, _frames[0].selected, (int) widthHeight.x, (int) widthHeight.y, _frames[0].position, "Off State"); _frames[1] = new Frame(1, _frames[1].selected, (int) widthHeight.x, (int) widthHeight.y, _frames[1].position, "Off Hover State"); _frames[2] = new Frame(2, _frames[2].selected, (int) widthHeight.x, (int) widthHeight.y, _frames[2].position, "On State"); _frames[3] = new Frame(3, _frames[3].selected, (int) widthHeight.x, (int) widthHeight.y, _frames[3].position, "On Hover State"); _frames[currentFrame].position = EditorGUILayout.Vector2Field(_frames[currentFrame].state + "'s UV Top-Left:", _frames[currentFrame].position); widthHeight = EditorGUILayout.Vector2Field("Quad Width and Height:", widthHeight); break; case QuadUIClass.Sprite: numFrames = Mathf.Clamp(EditorGUILayout.IntField("# Frames:",numFrames),2,1000); FixFrames(); if(_prevClass != _class) _frames[0].position = _uv; for(int i = 0; i < numFrames; i++) { _frames[i] = new Frame(i, _frames[i].selected, (int) widthHeight.x, (int) widthHeight.y, _frames[i].position, "Frame "+(i+1)); } _frames[currentFrame].position = EditorGUILayout.Vector2Field(_frames[currentFrame].state + "'s UV Top-Left:", _frames[currentFrame].position); GUILayout.Space(20); widthHeight = EditorGUILayout.Vector2Field("Quad Width and Height:", widthHeight); break; case QuadUIClass.InteractiveSprite: numFrames = Mathf.Clamp(EditorGUILayout.IntField("# Frames:",numFrames),2,1000); FixFrames(); if(_prevClass != _class) _frames[0].position = _uv; for(int i = 0; i < numFrames; i++) { _frames[i] = new Frame(i, _frames[i].selected, (int) widthHeight.x, (int) widthHeight.y, _frames[i].position, "Frame "+(i+1)); } _frames[currentFrame].position = EditorGUILayout.Vector2Field(_frames[currentFrame].state + "'s UV Top-Left:", _frames[currentFrame].position); GUILayout.Space(20); widthHeight = EditorGUILayout.Vector2Field("Quad Width and Height:", widthHeight); break; } GUILayout.Space(20); if(GUILayout.Button("Bake!")) BakeQuad(); //GUILayout.Space(40); //_zoom = EditorGUILayout.IntSlider(_zoom,1,4); break; #if QUADUI_1_3 case 1: //GUILayout.Label ("Complex Component Definition", EditorStyles.boldLabel); complexComponentType = (QuadUIComplexComponent) EditorGUILayout.EnumPopup("Class:", complexComponentType); break; case 2: #elif QUADUI_1_2 case 1: #endif GUILayout.Label ("Define Screen Space", EditorStyles.boldLabel); _camName = EditorGUILayout.TextField ("Name", _camName); #if UNITY_IPHONE _specialRes = (QuadUI.Resolution) EditorGUILayout.EnumPopup("Resolution: ", _specialRes, new GUILayoutOption[0]{}); switch(_specialRes) { case QuadUI.Resolution.iPadLandscape: res = new Vector2(1024,768); break; case QuadUI.Resolution.iPadPortrait: res = new Vector2(768,1024); break; case QuadUI.Resolution.iPhone4Landscape: res = new Vector2(960,640); break; case QuadUI.Resolution.iPhone4Portrait: res = new Vector2(640,960); break; case QuadUI.Resolution.iPhoneLandscape: res = new Vector2(480,320); break; case QuadUI.Resolution.iPhonePortrait: res = new Vector2(320,480); break; } #elif UNITY_ANDROID _specialRes = (QuadUI.Resolution) EditorGUILayout.EnumPopup("Resolution: ", _specialRes, new GUILayoutOption[0]{}); switch(_specialRes) { case QuadUI.Resolution.HTCLegendTall: res = new Vector2(320,480); break; case QuadUI.Resolution.HTCLegendWide: res = new Vector2(480,320); break; case QuadUI.Resolution.NexusOneTall: res = new Vector2(480,800); break; case QuadUI.Resolution.NexusOneWide: res = new Vector2(800,480); break; case QuadUI.Resolution.MotorolaDroidTall: res = new Vector2(480,854); break; case QuadUI.Resolution.MotorolaDroidWide: res = new Vector2(854,480); break; case QuadUI.Resolution.TegraTabletTall: res = new Vector2(600,1024); break; case QuadUI.Resolution.TegraTabletWide: res = new Vector2(1024,600); break; case QuadUI.Resolution.Custom: res = EditorGUILayout.Vector2Field("Screen Size", res); break; } #else res = EditorGUILayout.Vector2Field("Screen Size", res); #endif _cullingMask[0] = EditorGUILayout.LayerField("Culling Mask ",_cullingMask[0]); GUILayout.Space(20); if(GUILayout.Button("Generate 2D Camera")) GenerateCamera(); break; #if QUADUI_1_3 case 3: #elif QUADUI_1_2 case 2: #endif GUILayout.Label ("Tool Settings", EditorStyles.boldLabel); _selectedColor = EditorGUILayout.ColorField("Selected", _selectedColor); _dragColor = EditorGUILayout.ColorField("Drag", _dragColor); _gridColor = EditorGUILayout.ColorField("Grid", _gridColor); _crosshairColor = EditorGUILayout.ColorField("Crosshairs", _crosshairColor); _showGrid = EditorGUILayout.BeginToggleGroup("Show Grid",_showGrid); _gridSize = Mathf.Max(EditorGUILayout.IntField("Grid Size", _gridSize),2); EditorGUILayout.EndToggleGroup(); _meshOutputPath = EditorGUILayout.TextField("Mesh Path", _meshOutputPath); _prefabOutputPath = EditorGUILayout.TextField("Prefab Path", _prefabOutputPath); break; } EditorGUILayout.EndVertical(); GUILayout.EndArea(); }
void OnGUI () { if(!_window) { QuadUIEditor window = (QuadUIEditor)EditorWindow.GetWindow (typeof (QuadUIEditor),false,"Quad UI"); window._window = window; } if(EditorApplication.isPlayingOrWillChangePlaymode) { GUI.Label(new Rect(_window.position.width/2-100,_window.position.height/2-10,200,20),"Tool not available during Play Mode."); //_material = null; return; } else if(EditorApplication.isCompiling) { GUI.Label(new Rect(_window.position.width/2-70,_window.position.height/2-10,200,20),"Compiling. Please wait."); //_material = null; return; } #if QUADUI_1_3 canvas = new Rect(canvas.x,65, Mathf.Clamp(_window.position.width - 350,512,Mathf.Infinity), Mathf.Clamp(_window.position.height - 280,512,Mathf.Infinity)); #endif Rect activeArea = new Rect(canvas.x - 15, canvas.y - 15, canvas.width + 30, canvas.height + 30); GUI.Box(activeArea,""); //our QuadUI.Resolution enum is based on compiler arguements, so this will clamp the length to avoid out of bounds errors when swapping build platforms. int __enumLength = 0; #if UNITY_IPHONE __enumLength = 6; #elif UNITY_ANDROID __enumLength = 9; #endif _specialRes = (QuadUI.Resolution) ((int) Mathf.Clamp((int) _specialRes,0,__enumLength - 1)); GUI.Box(canvas,""); //let's grab our event Event evt = Event.current; if(evt.type == EventType.MouseDown) { mouseDown = true; /* if(evt.shift) constrainToSquare = true; else constrainToSquare = false; */ if(_snapGrid && _showGrid) { currentPos = pressPos = snappedPos; } else { currentPos = pressPos = evt.mousePosition; } } else if(evt.type == EventType.MouseUp && mouseDown == true) { mouseDown = false; } else if(evt.type == EventType.MouseDrag) { currentPos = evt.mousePosition; if(_snapGrid && _showGrid) { currentPos = snappedPos; } else { currentPos = evt.mousePosition; } /* if(evt.shift) constrainToSquare = true; else constrainToSquare = false; */ } //Debug.Log("global: " + evt.mousePosition + ", local: " + ConvertToCanvasSpace(evt.mousePosition) + ", back to global: " + ConvertToScreenSpace(ConvertToCanvasSpace(evt.mousePosition))); DrawLeftNavigation(); //Debug.Log(constrainToSquare); if(_material) { Texture img = _material.GetTexture("_MainTex"); if(edMat == null) edMat = new Material(Shader.Find("Hidden/QuadUI/editor")); //setup basic GL stuff here if(img) { if(img.width > 2048 || img.height > 2048) { if(EditorUtility.DisplayDialog("Oversized Texture!", "The Texture you are using exceeds the maximum size (2048px) in at least one dimension. Textures can be no larger than 2048px.","OK")) { _material = null; } } GUILayout.BeginArea(canvas); //Rect imgRect = new Rect(320,20,img.width,img.height); Rect imgRect = new Rect(-scrollX,-scrollY,img.width*_zoom,img.height*_zoom);//new Rect(0,0,img.width,img.height); //draw sprite sheet texture if(showAlpha) EditorGUI.DrawTextureAlpha(imgRect,img); else EditorGUI.DrawPreviewTexture(imgRect,img,null); GUILayout.EndArea(); GUILayout.BeginArea(new Rect(canvas.x-15,canvas.y+canvas.height+15,canvas.width+30,15)); if(_zoom * img.width > canvas.width) scrollX = GUILayout.HorizontalScrollbar(scrollX,0,0,(img.width*_zoom) - (img.width / (img.width/canvas.width)),NONE);//skin.GetStyle("horizontalscrollbar")); GUILayout.EndArea(); GUILayout.BeginArea(new Rect(canvas.x+canvas.width+15,canvas.y-15,15,canvas.height+30)); if(_zoom * img.height > canvas.height) scrollY = GUILayout.VerticalScrollbar(scrollY,0,0,(img.height*_zoom) - (img.height / (img.height/canvas.height)),new GUILayoutOption[1]{GUILayout.Height(canvas.height+30)});//skin.GetStyle("verticalscrollbar")); GUILayout.EndArea(); imgRect = new Rect(canvas.x,canvas.y,img.width,img.height); if(_zoom == 1 && (img.width <= canvas.width && img.height <= canvas.height)) scrollX = scrollY = 0; scrollOffset = new Vector2(-scrollX,-scrollY); if(_showGrid) { GL.PushMatrix(); edMat.SetPass(0); GL.LoadPixelMatrix(); GL.Begin(GL.LINES); GL.Color(_gridColor); for(int i = 1; i < canvas.width / _gridSize; i++) { int _lineX = (int) (canvas.x + scrollOffset.x) + (_gridSize * _zoom) * i; int _lineY = (int) (canvas.y + scrollOffset.y) + (_gridSize * _zoom) * i; //vertical if(_lineX <= canvas.x + canvas.width && _lineX >= canvas.x) { //top GL.Vertex(new Vector3(_lineX, canvas.y, 0)); //(canvas.y + scrollOffset.y) //bottom GL.Vertex(new Vector3(_lineX, canvas.y + canvas.height, 0)); //(canvas.y + scrollOffset.y) + (imgRect.height * _zoom) } //horizontal if(_lineY <= canvas.y + canvas.height && _lineY >= canvas.y) { //left GL.Vertex(new Vector3(canvas.x, _lineY, 0)); //(canvas.x + scrollOffset.x) //right GL.Vertex(new Vector3(canvas.x + canvas.width, _lineY, 0)); //(canvas.x + scrollOffset.x) + (imgRect.width * _zoom) } } GL.End(); GL.PopMatrix(); } //draw selection outline GL.PushMatrix(); edMat.SetPass(0); GL.LoadPixelMatrix(); GL.Begin(GL.LINES); Vector2 tl = ConvertUVToScreenSpace(); Vector2 br = new Vector2(widthHeight.x * _zoom, widthHeight.y * _zoom); GL.Color(_selectedColor); //topLine if(tl.y >= canvas.y && tl.y <= canvas.y + canvas.height) { GL.Vertex(new Vector3(Mathf.Clamp(tl.x,canvas.x,canvas.x+canvas.width), tl.y, 0)); GL.Vertex(new Vector3(Mathf.Clamp(tl.x + br.x,canvas.x,canvas.x+canvas.width), tl.y, 0)); } //left line if(tl.x >= canvas.x && tl.x <= canvas.x + canvas.width) { GL.Vertex(new Vector3(tl.x, Mathf.Clamp(tl.y,canvas.y,canvas.y+canvas.height), 0)); GL.Vertex(new Vector3(tl.x, Mathf.Clamp(tl.y + br.y,canvas.y,canvas.y+canvas.height), 0)); } //right line if(tl.x + br.x >= canvas.x && tl.x + br.x <= canvas.x + canvas.width) { GL.Vertex(new Vector3(tl.x + br.x, Mathf.Clamp(tl.y,canvas.y,canvas.y+canvas.height), 0)); GL.Vertex(new Vector3(tl.x + br.x, Mathf.Clamp(tl.y + br.y,canvas.y,canvas.y+canvas.height), 0)); } //bottom line if(tl.y + br.y >= canvas.y && tl.y + br.y <= canvas.y + canvas.height) { GL.Vertex(new Vector3(Mathf.Clamp(tl.x,canvas.x,canvas.x+canvas.width), tl.y + br.y, 0)); GL.Vertex(new Vector3(Mathf.Clamp(tl.x + br.x,canvas.x,canvas.x+canvas.width), tl.y + br.y, 0)); } GL.End(); GL.PopMatrix(); //Handle User Interaction Here!!! if(mouseDown) { if(activeArea.Contains(pressPos))//(pressPos.x > canvas.x - 10 && pressPos.x < canvas.x+canvas.width + 13) && pressPos.y < canvas.y + canvas.height + 8) { if(toolMode == ToolMode.Draw) { dragRect = new Rect(pressPos.x,pressPos.y,currentPos.x-pressPos.x,currentPos.y-pressPos.y); //dragBox = ConvertQuadToCanvasSpace(dragRect); } else if(toolMode == ToolMode.Move) { dragRect = new Rect(currentPos.x,currentPos.y,widthHeight.x*_zoom,widthHeight.y*_zoom); } GL.PushMatrix(); edMat.SetPass(0); GL.LoadPixelMatrix(); GL.Begin(GL.LINES); DrawQuadBox(dragRect,_dragColor); GL.End(); GL.PopMatrix(); } } else { if(evt.type == EventType.MouseUp) { if(activeArea.Contains(evt.mousePosition))//(evt.mousePosition.x > canvas.x - 20 && evt.mousePosition.x < canvas.x+canvas.width + 13) && evt.mousePosition.y < canvas.y + canvas.height + 18) { Rect r = new Rect(0,0,0,0); if(toolMode == ToolMode.Draw) { //During our clicking and dragging we were working in global space, but we need to change that information to localized units so the UV maps are correct Vector2 localizedCurrentPos = ConvertToCanvasSpace(currentPos); Vector2 localizedPressPos = ConvertToCanvasSpace(pressPos); //Find the delta of these 2 localized vectors and we will then know what direction the user was dragging in float dX = localizedCurrentPos.x - localizedPressPos.x; float dY = localizedCurrentPos.y - localizedPressPos.y; //We also want to clamp our 2 localized vectors so that they are no larger than the image max size and dont start in negatives. localizedPressPos = new Vector2(Mathf.Clamp(localizedPressPos.x,0,img.width),Mathf.Clamp(localizedPressPos.y,0,img.height)); localizedCurrentPos = new Vector2(Mathf.Clamp(localizedCurrentPos.x,0,img.width),Mathf.Clamp(localizedCurrentPos.y,0,img.height)); //check our drag delta to find what direction we're moving in if(dX > 0 && dY > 0) { //started TL r = new Rect(localizedPressPos.x,localizedPressPos.y,localizedCurrentPos.x-localizedPressPos.x,localizedCurrentPos.y-localizedPressPos.y); } else if(dX > 0 && dY < 0) { //started BL r = new Rect(localizedPressPos.x,localizedCurrentPos.y,localizedCurrentPos.x-localizedPressPos.x,localizedPressPos.y - localizedCurrentPos.y); } else if(dX < 0 && dY < 0) { //started BR r = new Rect(localizedCurrentPos.x,localizedCurrentPos.y,localizedPressPos.x - localizedCurrentPos.x,localizedPressPos.y - localizedCurrentPos.y); } else if(dX < 0 && dY > 0) { //started TR r = new Rect(localizedCurrentPos.x,localizedPressPos.y,localizedPressPos.x - localizedCurrentPos.x,localizedCurrentPos.y - localizedPressPos.y); } /* if(constrainToSquare) { r = new Rect(r.x,r.y,r.height,r.height); } */ } else if(toolMode == ToolMode.Move) { Vector2 localizedCurrentPos = ConvertToCanvasSpace(currentPos); r = new Rect(localizedCurrentPos.x,localizedCurrentPos.y,widthHeight.x,widthHeight.y); if(localizedCurrentPos.x + widthHeight.x > img.width || localizedCurrentPos.y+widthHeight.y > img.height || localizedCurrentPos.x < 0 || localizedCurrentPos.y < 0) { if(!EditorUtility.DisplayDialog("Out of Bounds!", "The Rectangle you are assigning has some of it's area outside of the bounds of your Texture. Depending on your Texture's wrap mode, this can produce some unexpected results.","OK","Cancel")) { r = new Rect(_uv.x,_uv.y,widthHeight.x,widthHeight.y); } } //if(_frames.Length > 0)_frames[currentFrame].position = new Vector2(Mathf.Clamp(r.x-320,0,img.width),Mathf.Clamp(r.y-20,0,img.height)); } _uv = new Vector2(r.x,r.y); widthHeight = new Vector2(r.width,r.height); if(_frames.Length > 0)_frames[currentFrame].position = _uv; } } } Rect _infoRect = new Rect(0,canvas.y+canvas.height+20,300,185); GUI.Box(_infoRect,"Info:"); //mouse position considering we're within the texture if(activeArea.Contains(evt.mousePosition))//new Rect(canvas.x-20,canvas.y-20,(canvas.x-20)+(canvas.width+28),(canvas.y-20)+(canvas.height+28)).Contains(evt.mousePosition)) { Vector2 _lS = ConvertToCanvasSpace(evt.mousePosition); GUILayout.BeginArea(_infoRect); GUILayout.Space(20); GUILayout.Label(" Mouse: x: " + _lS.x + " y: " + _lS.y); GUILayout.EndArea(); if(_snapGrid && _showGrid) { int _x = (int) evt.mousePosition.x - (int) (canvas.x + scrollOffset.x); int _y = (int) evt.mousePosition.y - (int) (canvas.y + scrollOffset.y); float uX = _x / (_gridSize * _zoom); float uY = _y / (_gridSize * _zoom); _x = (int) Mathf.Round(uX) * (_gridSize * _zoom) + (int) (canvas.x + scrollOffset.x); _y = (int) Mathf.Round(uY) * (_gridSize * _zoom) + (int) (canvas.y + scrollOffset.y); snappedPos = new Vector2(_x,_y); GL.PushMatrix(); edMat.SetPass(0); GL.LoadPixelMatrix(); GL.Begin(GL.LINES); GL.Color(_crosshairColor); //vertical if((_x >= canvas.x && _x <= canvas.x+canvas.width) && (_y >= canvas.y && _y <= canvas.y+canvas.height)) { GL.Vertex(new Vector3(_x,_y-10,0)); GL.Vertex(new Vector3(_x,_y+10,0)); } //horizontal if((_x >= canvas.x && _x <= canvas.x+canvas.width) && (_y >= canvas.y && _y <= canvas.y+canvas.height)) { GL.Vertex(new Vector3(_x-10,_y,0)); GL.Vertex(new Vector3(_x+10,_y,0)); } GL.End(); GL.PopMatrix(); } } } #if DEBUG GUI.Box(new Rect(10,_window.position.height-120,130,80),""); GUILayout.BeginArea(new Rect(15,_window.position.height-120,150,100)); GUILayout.Label("DEBUG:", EditorStyles.boldLabel); //GUILayout.Label("uv: " + ConvertUVToScreenSpace()); GUILayout.Label("scrollX: " + scrollX); GUILayout.Label("scrollY: " + scrollY); //GUILayout.Label("Snap (G): " + snappedPos); //GUILayout.Label("Snap (L): " + ConvertToCanvasSpace(snappedPos)); //GUILayout.Label("Pressed: " + ConvertToCanvasSpace(pressPos)); //GUILayout.Label("Current: " + ConvertToCanvasSpace(currentPos)); GUILayout.Label("Width: " + Mathf.Abs(ConvertToCanvasSpace(currentPos).x - ConvertToCanvasSpace(pressPos).x)); GUILayout.Label("Height: " + Mathf.Abs(ConvertToCanvasSpace(currentPos).y - ConvertToCanvasSpace(pressPos).y)); //GUILayout.Label("Tool: " + toolMode); //GUILayout.Label("Current Frame: " + currentFrame); //if(_frames != null) GUILayout.Label("Frames: " + _frames.Length); //else GUILayout.Label("Frames: null"); GUILayout.EndArea(); #endif } /* Thumbs */ DrawThumbsArea(); /* Tools */ DrawRightToolbar(); Repaint(); }