Ejemplo n.º 1
0
        /// <summary>
        /// Constructs a default window manager that holds the device (owner) in shared mode.
        /// </summary>
        public DefaultWindowManager(GraphicsDevice device, RenderTargetView renderTarget,
                                    Input.InputService input, IGuiTheme theme)
        {
            // Rendering data.
            this.device       = device;
            this.renderTarget = renderTarget;

            // Input data.
            this.processor = new EventProcessor(new EventPump(input.CreateDevice(InputDeviceType.Mouse),
                                                              input.CreateDevice(InputDeviceType.Keyboard)));

            // We bind events.
            this.processor.KeyDown         += KeyDown;
            this.processor.KeyUp           += KeyUp;
            this.processor.MouseButtonDown += MouseButtonDown;
            this.processor.MouseButtonUp   += MouseButtonUp;
            this.processor.MouseAxis       += MouseAxis;

            // We create "desktop manager".
            this.desktopManager = new GuiManager(new GraphicsCanvas(device, renderTarget, new Vector2f(1.0f, 1.0f)));
            this.pointer        = new SharpMedia.Graphics.GUI.Standalone.GuiPointer(desktopManager, processor,
                                                                                    null, null, new SharpMedia.Graphics.GUI.Standalone.Sensitivity());

            theme.AutomaticApply(this.pointer, false);

            // FIXME: add pointer somehow to be "forward"
            this.desktopManager.AddNLObject(pointer);

            // We create composition.
            this.compositor = new Compositor(device);
        }
Ejemplo n.º 2
0
        public void SimpleGUI(IDeviceCanvas canvas, IGuiTheme theme, Input.InputService input)
        {
            canvas.PointsPerPixel = 0.001f;

            using (GuiManager manager = new GuiManager(canvas))
            {
                // We create a simple area.
                Area area = new Area();



                using (area.Enter())
                {
                    // We create a hiearchical style.
                    Style style = Style.Create <Area.AreaStyle>();

                    Area.AreaStyle astyle = new Area.AreaStyle();
                    astyle.Background.Fill = new SolidFill(Colour.LightBlue);
                    astyle.Border.Pen      = new Pen(new SolidFill(Colour.Gray), 0.003f, 0.0f, LineMode.Square);
                    style.AddStyle(CommonStyleStates.Normal, astyle);

                    astyle = new Area.AreaStyle();
                    astyle.Background.Fill = new SolidFill(Colour.LightGreen);
                    astyle.Border.Pen      = new Pen(new SolidFill(Colour.Gray), 0.003f, 0.0f, LineMode.Square);
                    style.AddStyle(CommonStyleStates.PointerOver, astyle);


                    // We apply style.
                    area.Style = style;

                    area.PreferredRect = GuiRect.CreateRectangle(new GuiVector2(new Vector2f(0.1f, 0.1f)),
                                                                 new GuiVector2(new Vector2f(0.4f, 0.4f)));
                }


                Button button = new Button(
                    GuiRect.CreateRectangle(
                        new GuiVector2(new Vector2f(0.1f, 0.6f)),
                        new GuiVector2(new Vector2f(0.4f, 0.9f))
                        ),
                    "Click me!", null);

                Label label = new Label();

                using (label.Enter())
                {
                    label.Text = "SharpMedia rocks!";

                    label.PreferredRect = GuiRect.CreateRectangle(new GuiVector2(new Vector2f(0.6f, 0.6f)),
                                                                  new GuiVector2(new Vector2f(0.9f, 0.9f)));
                    label.TextSelectedRange = new Vector2i(1, 5);
                }

                Label label2 = new Label();
                using (label2.Enter())
                {
                    label2.PreferredRect = GuiRect.CreateRectangle(new GuiVector2(new Vector2f(0.6f, 0.1f)),
                                                                   new GuiVector2(new Vector2f(0.9f, 0.4f)));
                    label2.IsEnabled = false;

                    label.Events.TextSelect += delegate(Label xlabel, Vector2i sel)
                    {
                        using (label2.Enter())
                        {
                            label2.Text = xlabel.SelectedText;
                        }
                    };

                    button.Events.ButtonClicked += delegate(Button button2)
                    {
                        using (label2.Enter())
                        {
                            label2.Text += label2.Text;
                        }
                    };
                }

                Container sheet = new Container();
                using (sheet.Enter())
                {
                    sheet.AddChild(area);
                    sheet.AddChild(label2);
                    sheet.AddChild(button);
                    sheet.AddChild(label);
                }

                // We apply theme.
                theme.AutomaticApply(sheet, true);

                manager.RootObject    = sheet;
                manager.PreRendering += new Action <GuiManager>(PreRendering);
                manager.Rendered     += new Action <GuiManager>(PostRendering);

                EventPump pump = new EventPump(input.CreateDevice(InputDeviceType.Mouse),
                                               input.CreateDevice(InputDeviceType.Keyboard),
                                               input.CreateDevice(InputDeviceType.Cursor));
                EventProcessor processor = new EventProcessor(pump);

                // We bind input.
                Standalone.InputRouter router = new Standalone.InputRouter(manager,
                                                                           processor, theme.ObtainStyle(typeof(Standalone.GuiPointer), null),
                                                                           theme.ObtainRenderer(typeof(Standalone.GuiPointer), null), null);

                bool end = false;



                canvas.Device.SwapChain.Window.Closed += delegate(Window w) { end = true; };
                DateTime time = DateTime.Now;
                while (!end)
                {
                    canvas.Device.SwapChain.Window.DoEvents();
                    while (processor.Process() != null)
                    {
                        ;
                    }

                    // We update iut.
                    DateTime t = DateTime.Now;
                    manager.Update((float)(t - time).TotalSeconds);
                    time = t;

                    // We render it.
                    manager.Render();

                    Console.WriteLine(canvas.Device.DevicePerformance.CurrentFPS);

                    canvas.Device.SwapChain.Present();
                }

                // Must dispose before it, devices bound so also disposed.
                pump.Dispose();
            }
        }
Ejemplo n.º 3
0
        public override int Start(string verb, string[] args)
        {
            // We initialize the service.
            if (service.DeviceExists)
            {
                throw new InvalidOperationException("Test application cannot be contructed, " +
                                                    "device already exists.");
            }

            if (renderTargetParameters == null)
            {
                // We use defaults.
                renderTargetParameters                          = new RenderTargetParameters();
                renderTargetParameters.Windowed                 = true;
                renderTargetParameters.FormatCommon             = CommonPixelFormatLayout.X8Y8Z8W8_UNORM;
                renderTargetParameters.DepthStencilCommonFormat = CommonPixelFormatLayout.D24_UNORM_S8_UINT;
                renderTargetParameters.BackBufferCount          = 1;
                renderTargetParameters.BackBufferWidth          = 800;
                renderTargetParameters.BackBufferHeight         = 600;
                renderTargetParameters.MultiSampleType          = 4;
                renderTargetParameters.MultiSampleQuality       = 3;
            }


            Window window;
            Font   font = new Font(SVGFontProvider.ImportSVG("dressel.svg"));

            using (GraphicsDevice device =
                       service.CreateDevice(true, false, renderTargetParameters, out window))
            {
                // We craete input device.
                using (input = new Input.InputService(new Input.Driver.DirectInput.DIInput()))
                {
                    /*
                     * DefaultTheme theme = new DefaultTheme(font);
                     * input.Initialize(window);
                     *
                     * bool isClosed = false;
                     * window.Closed += delegate(Window w) { isClosed = true; };
                     * IWindowManager manager = new DefaultWindowManager(device, device.SwapChain, input, theme);
                     *
                     * Container container = new Container();
                     *
                     * // We create a simple area.
                     * Area area = new Area();
                     * using (area.Enter())
                     * {
                     *  Area.AreaStyle s = new Area.AreaStyle();
                     *  area.PreferredRect = new GuiRect(RectangleMode.MinMax,
                     *      new GuiVector2(new Vector2f(0.2f, 0.2f)),
                     *      new GuiVector2(new Vector2f(0.8f, 0.8f)));
                     *  s.Background = new BackgroundStyle(new SolidFill(Colour.Red));
                     *  area.Style = Style.Create<Area.AreaStyle>(null,
                     *      new KeyValuePair<StyleState, Area.AreaStyle>(CommonStyleStates.Normal, s));
                     * }
                     *
                     * using (container.Enter())
                     * {
                     *  container.AddChild(area);
                     * }
                     *
                     *
                     * theme.AutomaticApply(container, true);
                     *
                     *
                     *
                     * RootWindow client = new RootWindow(device, manager, "Window Title", "SomeGroup",
                     *  WindowOptions.Close|WindowOptions.Maximimize|WindowOptions.Minimize, new Vector2i(100, 100), new Vector2i(300,300),
                     *  null, null, WindowState.Normal, container, 1, 0);
                     *
                     * client.GuiManager.PreRendering += delegate(GuiManager guiManager)
                     * {
                     *  guiManager.Canvas.Device.Clear(guiManager.Canvas.Target, Colour.Blue);
                     * };
                     *
                     * client.IsVisible = true;
                     *
                     * manager.PreRendering += delegate(IWindowManager y)
                     * {
                     *  device.Clear(device.SwapChain, Colour.Yellow);
                     * };
                     * manager.PostRendering += delegate(IWindowManager z)
                     * {
                     *  device.SwapChain.Present();
                     * };
                     *
                     * while (!isClosed)
                     * {
                     *  client.GuiManager.Invalidate();
                     *  client.GuiManager.Render();
                     *  (manager as IWindowManagerControl).Update();
                     *  window.DoEvents();
                     * }
                     * (manager as IWindowManagerControl).Dispose();
                     *
                     */


                    /*
                     * input.Initialize(window);
                     *
                     * // We default to font rendering 2.
                     * if (args.Length == 0)
                     * {
                     *
                     *  IGuiTheme theme = new DefaultTheme(font);
                     *  GuiTest test = new GuiTest();
                     *
                     *  using (GraphicsCanvas canvas = new GraphicsCanvas(device, device.SwapChain, new Vector2f(1, 1)))
                     *  {
                     *      test.SimpleGUI(canvas, theme, input);
                     *
                     *  }
                     *
                     *
                     *  // We now run demos.
                     *
                     *  VectorTest test = new VectorTest();
                     *  test.FontRendering2(device);
                     * }
                     * /*
                     * else
                     * {
                     *
                     *  VectorTest test = new VectorTest();
                     *  foreach (string arg in args)
                     *  {
                     *      MethodInfo info = typeof(VectorTest).GetMethod(arg);
                     *      if (info != null)
                     *      {
                     *          try
                     *          {
                     *              info.Invoke(test, new object[] { device });
                     *          }
                     *          catch (Exception)
                     *          {
                     *              Console.WriteLine("Could not run or error method {0}.", arg);
                     *          }
                     *      }
                     *      else
                     *      {
                     *          Console.WriteLine("Could not run method {0}.", arg);
                     *      }
                     *  }
                     * }*/
                    //}
                }
            }
            return(0);
        }