Esempio n. 1
0
        static void Main()
        {
            Window.CreateWindow("OpenGL UI: Example 4", 1280, 720);

            // add a reshape callback to update the UI
            Window.OnReshapeCallbacks.Add(() => OpenGL.UI.UserInterface.OnResize(Window.Width, Window.Height));

            // add a close callback to make sure we dispose of everything properly
            Window.OnCloseCallbacks.Add(OnClose);

            // enable depth testing to ensure correct z-ordering of our fragments
            Gl.Enable(EnableCap.DepthTest);
            Gl.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.OneMinusSrcAlpha);

            // initialize the user interface
            OpenGL.UI.UserInterface.InitUI(Window.Width, Window.Height);

            // create a container that will store all of our color picker content
            OpenGL.UI.UIContainer colorPickerContainer = new OpenGL.UI.UIContainer();
            colorPickerContainer.Size       = new Point(240, 190);
            colorPickerContainer.Position   = new Point(20, 20);
            colorPickerContainer.RelativeTo = OpenGL.UI.Corner.TopLeft;

            // create a menu bar that will have two different textures
            menuTexture         = new Texture("data/menu.png");
            menuSelectedTexture = new Texture("data/menuSelected.png");

            OpenGL.UI.Button menu = new OpenGL.UI.Button(menuTexture);
            colorPickerContainer.AddElement(menu);

            // place some text within the menu bar
            OpenGL.UI.Text menuText = new OpenGL.UI.Text(OpenGL.UI.Text.FontSize._12pt, "Color Picker");
            menuText.RelativeTo = OpenGL.UI.Corner.TopLeft;
            menuText.Position   = new Point(4, 17);
            colorPickerContainer.AddElement(menuText);

            // add some events that will move the entire color picker container with the menu bar
            bool moving = false;

            menu.OnMouseDown = (sender, e) =>
            {
                moving = true;
                menu.BackgroundTexture = menuSelectedTexture;       // make it look nice by swapping the menubar texture
            };
            menu.OnMouseUp = (sender, e) =>
            {
                moving = false;
                menu.BackgroundTexture = menuTexture;       // make sure to restore the menubar texture
            };
            menu.OnMouseMove = (sender, e) =>
            {
                if (moving)
                {
                    int x = colorPickerContainer.Position.X + OpenGL.UI.UserInterface.MousePosition.X - OpenGL.UI.UserInterface.LastMousePosition.X;
                    int y = colorPickerContainer.Position.Y + OpenGL.UI.UserInterface.MousePosition.Y - OpenGL.UI.UserInterface.LastMousePosition.Y;
                    colorPickerContainer.Position = new Point(x, y);
                    colorPickerContainer.OnResize();
                }
            };

            // create the color picker itself
            OpenGL.UI.ColorGradient gradient = new OpenGL.UI.ColorGradient();
            gradient.Position = new Point(30, 30);

            // and create a hue slider that can control the types of colors shown in the color picker
            OpenGL.UI.HueGradient hue = new OpenGL.UI.HueGradient();
            hue.Position = new Point(190, 30);

            // add the color picker and its hue slider to the UI
            colorPickerContainer.AddElement(gradient);
            colorPickerContainer.AddElement(hue);

            // add the entire container to the user interface
            OpenGL.UI.UserInterface.AddElement(colorPickerContainer);

            // subscribe the escape event using the OpenGL.UI class library
            Input.Subscribe((char)27, Window.OnClose);

            // make sure to set up mouse event handlers for the window
            Window.OnMouseCallbacks.Add(OpenGL.UI.UserInterface.OnMouseClick);
            Window.OnMouseMoveCallbacks.Add(OpenGL.UI.UserInterface.OnMouseMove);

            while (true)
            {
                Window.HandleEvents();
                OnRenderFrame();
            }
        }
Esempio n. 2
0
        static void Main()
        {
            Window.CreateWindow("OpenGL UI: Example 10", 1280, 720);

            // add a reshape callback to update the UI
            Window.OnReshapeCallbacks.Add(() => OpenGL.UI.UserInterface.OnResize(Window.Width, Window.Height));

            // add a close callback to make sure we dispose of everything properly
            Window.OnCloseCallbacks.Add(OnClose);

            // enable depth testing to ensure correct z-ordering of our fragments
            Gl.Enable(EnableCap.DepthTest);
            Gl.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.OneMinusSrcAlpha);

            // initialize the user interface
            OpenGL.UI.UserInterface.InitUI(Window.Width, Window.Height);

            // create the left and right containers
            leftContainer  = new OpenGL.UI.UIContainer(new Point(Window.Width / 2, Window.Height), "LeftContainer");
            rightContainer = new OpenGL.UI.UIContainer(new Point(Window.Width / 2, Window.Height), "RightContainer");

            leftContainer.RelativeTo  = OpenGL.UI.Corner.BottomLeft;
            rightContainer.RelativeTo = OpenGL.UI.Corner.BottomRight;

            // add the containers to the user interface
            OpenGL.UI.UserInterface.AddElement(leftContainer);
            OpenGL.UI.UserInterface.AddElement(rightContainer);

            // add some cool stuff to the containers
            scrollTexture = new Texture("data/scrollTexture.png");

            // create a textbox with word wrapping for the left container
            OpenGL.UI.TextBox textBox = new OpenGL.UI.TextBox(OpenGL.UI.BMFont.LoadFont("fonts/font16.fnt"), scrollTexture);
            textBox.Write("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis eget vehicula orci. Nulla nibh nulla, suscipit non neque sed, placerat efficitur velit. Sed convallis gravida tincidunt. Praesent vehicula nibh leo, at consequat nisi condimentum ullamcorper. Vivamus pulvinar accumsan maximus. Integer luctus elit porttitor nisi sollicitudin, eget porttitor odio tincidunt. Sed elit justo, suscipit non dui ut, eleifend euismod nibh. Nullam ac fermentum nisl. In convallis id leo sit amet eleifend. Suspendisse eu ligula pulvinar ex facilisis cursus ac ac velit. In ut turpis nec neque vehicula eleifend. Morbi in tempus est. Vivamus nisi nunc, pharetra quis scelerisque ut, tempor id dui.");
            textBox.RelativeTo = OpenGL.UI.Corner.Fill;
            textBox.Padding    = new Point(6, 0);
            leftContainer.AddElement(textBox);
            textBox.OnResize();

            // build 10 buttons for the right container
            for (int i = 0; i < 10; i++)
            {
                OpenGL.UI.Button button = new OpenGL.UI.Button(200, 30);
                button.Font       = OpenGL.UI.BMFont.LoadFont("fonts/font16.fnt");
                button.Text       = string.Format("Button {0}", i);
                button.RelativeTo = OpenGL.UI.Corner.Center;
                button.Position   = new Point(0, 200 - i * 40);
                rightContainer.AddElement(button);
                button.OnResize();

                button.OnMouseEnter = (sender, e) => button.BackgroundColor = new Vector4(0.5f, 0.5f, 1.0f, 1.0f);
                button.OnMouseLeave = (sender, e) => button.BackgroundColor = new Vector4(0.3f, 0.3f, 0.3f, 1.0f);
            }

            // add a control to resize the left/right containers
            divider = new OpenGL.UI.Button(8, Window.Height);
            divider.BackgroundColor = new Vector4(0.3f, 0.3f, 0.3f, 0.5f);
            divider.RelativeTo      = OpenGL.UI.Corner.BottomLeft;
            divider.Position        = new Point(Window.Width / 2 - divider.Size.X / 2, 0);

            bool onMouseDown = false;

            // handle mouse events on the divider
            divider.OnMouseDown = (sender, e) => onMouseDown = true;
            divider.OnMouseUp   = (sender, e) => onMouseDown = false;
            divider.OnMouseMove = (sender, e) =>
            {
                if (onMouseDown)
                {
                    ResizeControls(e.Location.X - e.LastLocaton.X);
                }
            };

            // make sure to layout the controls if the window is resized
            Window.OnReshapeCallbacks.Add(() => ResizeControls(0));

            // add the divider to the user interface
            OpenGL.UI.UserInterface.AddElement(divider);

            // subscribe the escape event using the OpenGL.UI class library
            Input.Subscribe((char)27, Window.OnClose);

            // make sure to set up mouse event handlers for the window
            Window.OnMouseCallbacks.Add(OpenGL.UI.UserInterface.OnMouseClick);
            Window.OnMouseMoveCallbacks.Add(OpenGL.UI.UserInterface.OnMouseMove);

            while (true)
            {
                Window.HandleEvents();
                OnRenderFrame();
            }
        }