Search Unity

Vertical Slider in EditorWindow

Discussion in 'Scripting' started by IzzySoft, Dec 27, 2013.

  1. IzzySoft

    IzzySoft

    Joined:
    Feb 11, 2013
    Posts:
    376
    hi All,

    I'm curious.. is there a way to Draw a Vertical version of the Min Max Slider?

    ex:


    I rather not do...
    Code (csharp):
    1.  
    2.         origMatrix = GUI.matrix;
    3.  
    4.         pivotPoint = new Vector2( rSlider2.x + 122, rSlider2.y + 138);
    5.         GUIUtility.RotateAroundPivot( -90.0f, pivotPoint );
    6.         EditorGUILayout.MinMaxSlider(ref minVal, ref maxVal, minLimit, maxLimit, GUILayout.Width(256) );
    7.        
    8.         GUI.matrix = origMatrix;
    9.  
    ...since the clipping of the slider can happen when the window is shrunk.

    thanks all,
    Izzy.
     
    Last edited: Dec 28, 2013
    ocimum likes this.
  2. IzzySoft

    IzzySoft

    Joined:
    Feb 11, 2013
    Posts:
    376
    No One? :sad:
     
  3. IzzySoft

    IzzySoft

    Joined:
    Feb 11, 2013
    Posts:
    376
    If I give you the Draw Linear Gradient function, would someone help? :D

    Code (csharp):
    1.  
    2. public static void DrawGradient_Linear( this Texture2D tex, Rect rArea, int nSteps, Color col1, Color col2 )
    3. {
    4.     int fromX = (int) rArea.x;
    5.     int toX = (int) (rArea.x + rArea.width);
    6.     int fromY = (int) rArea.y;
    7.     int toY = (int) (rArea.y + rArea.height);
    8.     float fStepSize = (float) rArea.width / (float) nSteps;
    9.     float fCurStepPos = fromX;
    10.  
    11.     for( int x = fromX; x < toX; x++ )
    12.     {
    13.         if( x >= fStepSize + fCurStepPos )
    14.             fCurStepPos += fStepSize;
    15.  
    16.         float fPercent = (fCurStepPos - fromX) / (rArea.width - fStepSize);
    17.  
    18.         Color c = Color.Lerp( col1, col2, fPercent );
    19.  
    20.         for( int y = fromY; y < toY; y++ )
    21.             tex.SetPixel( x, y, c );
    22.     }
    23. }
    24.  
    and call it:

    Code (csharp):
    1.  
    2. void Start ()
    3. {
    4.     int texSize = 256;
    5.     Texture2D tex = new Texture2D(texSize, texSize, TextureFormat.ARGB32, false, true);
    6.     tex.alphaIsTransparency = true;
    7.  
    8.     tex.FloodFillArea( texSize, texSize, new Color(0, 0, 0, 0) );
    9.  
    10.     Color color1 = new Color( .8f, .3f, .8f, 0.2f );
    11.     Color color2 = new Color( .3f, .2f, .3f, 0.2f );
    12.  
    13.     tex.DrawCheckers( texSize, checkerSize, color1, color2 );
    14.  
    15.     int nRectSize = 64;
    16.     int nSteps = 16;
    17.     tex.DrawGradient_Linear( new Rect(0, 0, nRectSize, nRectSize), nSteps, Color.white, new Color(0,0,0,1) );
    18.  
    19.     tex.Apply();
    20.  
    21.     renderer.material.mainTexture = tex;
    22. }
    23.  


    nSteps controls when the gradient color is used.
     
    Last edited: Dec 28, 2013
  4. IzzySoft

    IzzySoft

    Joined:
    Feb 11, 2013
    Posts:
    376
    I found the DumpARGB32ToDisk in one of the forum threads and was wondering how that worked. (using System.Drawing.dll)

    I tried it and it works. I think the Rows have to be Flipped Vertically because GetPixels32 starts reading from bottom left , and Bitmap reads it from top left. :)
    Is this code below correct? Did i miss anything?

    Code (csharp):
    1.  
    2. ...
    3.     void Start ()
    4.     {
    5.         int texSize = 128;     
    6.         int checkerSize = 64;      
    7.         //int circles = 10;
    8.  
    9.         Color color1 = new Color( .8f, .3f, .8f, 0.2f );
    10.         Color color2 = new Color( .3f, .2f, .3f, 0.2f );
    11.  
    12.         Texture2D tex = new Texture2D(texSize, texSize, TextureFormat.ARGB32, false, true);
    13.         tex.alphaIsTransparency = true;
    14.  
    15.         tex.FloodFillArea( texSize, texSize, new Color(0, 0, 0, 0) );
    16.  
    17.         tex.DrawCheckers( texSize, checkerSize, color1, color2 );
    18.  
    19.         int nRectSize = 64;
    20.         int nSteps = 16;
    21.         tex.DrawGradient_Linear( new Rect(0, 0, nRectSize, nRectSize), nSteps, color1, color2 );
    22.  
    23.         tex.Apply();
    24.         renderer.material.mainTexture = tex;
    25.  
    26.         Color32[] pix = tex.GetPixels32();
    27.         int cSize = System.Runtime.InteropServices.Marshal.SizeOf( typeof(Color32) );
    28.         byte[] pix4 = new byte[ pix.Length * cSize ];
    29.         int row, col, rowOffset;
    30.         int targetRow, targetRowStart;
    31.         int rowPixels = tex.width * cSize;
    32.  
    33.         for( int i = 0; i < pix.Length;  )
    34.         {
    35.             row = i / tex.width;
    36.             col = i % tex.width;
    37.             targetRow = tex.height - row;
    38.             targetRowStart = (targetRow - 1) * rowPixels;
    39.  
    40.             for( int j = targetRowStart; j < targetRowStart + rowPixels; j += cSize, i++ )
    41.             {
    42.                 pix4[j] = pix[i].r;
    43.                 pix4[j+1] = pix[i].g;
    44.                 pix4[j+2] = pix[i].b;
    45.                 pix4[j+3] = pix[i].a;
    46.             }
    47.         }
    48.        
    49.         DumpARGB32ToDisk( pix4, (uint) tex.width, (uint) tex.height );
    50.     }
    51.  
    52.     public static void DumpARGB32ToDisk(byte[] argb, uint width, uint height)
    53.     {
    54.         System.Drawing.Bitmap bmp = new System.Drawing.Bitmap((int)width, (int)height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
    55.         System.Drawing.Imaging.BitmapData bmpData = bmp.LockBits(new System.Drawing.Rectangle(0, 0, bmp.Width, bmp.Height), System.Drawing.Imaging.ImageLockMode.WriteOnly, bmp.PixelFormat);
    56.         System.Runtime.InteropServices.Marshal.Copy(argb, 0, bmpData.Scan0, argb.Length);
    57.         bmp.UnlockBits(bmpData);
    58.         bmp.Save("Assets/_GeneratedImage.png", System.Drawing.Imaging.ImageFormat.Png);
    59.     }
    60.  
     
    Last edited: Dec 30, 2013
  5. IzzySoft

    IzzySoft

    Joined:
    Feb 11, 2013
    Posts:
    376
    Ehhh.. No Help?

    Ok.
    I guess i'll start working on a vertical min max slider.

    so far:
     
    Last edited: Dec 31, 2013
    ocimum likes this.
  6. JohnnyA

    JohnnyA

    Joined:
    Apr 9, 2010
    Posts:
    5,041
    I was going to write a reply about manually setting the rect (using GUI not GUI Layout), but it seemed pretty clear that you know what you are doing so this was considered and rejected ;)
     
  7. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,717
    Little note, System.Drawing.dll does not work on Mac/Linux. So careful if your tool is to be multi-platform.
     
  8. IzzySoft

    IzzySoft

    Joined:
    Feb 11, 2013
    Posts:
    376
    hey johnny

    You're the Best. ;D
     
  9. IzzySoft

    IzzySoft

    Joined:
    Feb 11, 2013
    Posts:
    376
    Ohhh?
    I took the "C:\Program Files (x86)\Unity\Editor\Data\Mono\lib\mono\2.0\System.Drawing.dll"
    ..and dropped it in the "Assets/Plugins" folder in my project.

    I thought i read that some got it to work on a MAC with the Mono dll. (extracting it from the *.app container)
    Not sure about Linux. :/

    Hmm... Ok.
    Know where i can get some more SetPixel Drawing routines that are in c# source form? :)

    I'm putting together a .CS that has a few Texture2D Drawing Functions
    Here is what ive scavenged from the forums, wiki's, etc...
    link: http://pastebin.com/Vu4ifNQP
     
    Last edited: Dec 31, 2013
  10. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,717
    System.Drawing.dll, like many .NET library got references towards other libraries. It's possible to make some method in it to work, while some don't. In our case, we were trying to load a .PNG from a custom DLL and sadly the library had external reference that were not supported by Mono.

    Unity already allows you to change pixel, read and write textures. Which feature is missing for what you need?
     
  11. IzzySoft

    IzzySoft

    Joined:
    Feb 11, 2013
    Posts:
    376
    :)

    Everything that CAIRO does would be Sweet! ;)
    If you can find functions that do what Image Magick does... even better. ;D

    But for now, i'd settle for simple functions like drawing a Filled Triangle (5 or more sides preferably), and possibly apply a Gradient as the Fill type.

    Ohhhh.. a .DrawTEXT() function that would draw to a Texture2D. Maybe pass in an existing Font Settings asset Name as a parameter, and it can look up and use the correct texture/material for that already imported font. Might need to use Font.GetChacterInfo and do a GetPixels() and draw each letter to the Texture2D?!?
    I MUST HAVE that! :eek:

    And later, i'm hoping to use a procedural approach to create textures for a slew of particle effects (atlas.. UV offset, etc..), including masks for some of the shaders or decals or whatever is needed.
     
    Last edited: Dec 31, 2013
  12. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    Re: The OP
    Sadly the editor skin is lacking in terms of vertical sliders, the regular GUI.Vertical slider uses the in-game skin images, not the slick, minimal editor-color-scheme bars.
    Interestingly, there is a "MiniMinMaxSliderVertical" in the editor skin, but since you can't use a style parameter with minmax sliders, IDK what it's for.

    You can't even fake one by overlapping 2 regular vertical sliders, since one of them will never take focus.

    As for these other 'features', surely better to leave them to an actual image editor? (tho you're about spot on for adding text to a texture)
     
  13. IzzySoft

    IzzySoft

    Joined:
    Feb 11, 2013
    Posts:
    376
    Before i start on my custom MinMax Slider.. i wanted to try some other things 1st.

    So far:

    [video=youtube_share;-nqUgoaf1zw]http://www.youtube.com/embed/-nqUgoaf1zw
     
    Last edited: Jan 5, 2014
  14. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    http://pastebin.com/sC6dE3fb

    Vertical MinMax slider
    I've done another version that doesn't rely on the private bools, and (i think) is closer to what Unity does internally with the other sliders, but its twice as much code.