public static void RenderBorderAndArea(ICanvas canvas, IShapef shape, IPathf border, Area.AreaStyle style, Area.AreaStyle nextStyle, float alpha) { // We first fill background. { // We create background fill. ITransform mappingTransform; IMapper mapper; IFill fill = BackgroundStyle.Merge(style != null ? style.Background : null, nextStyle != null ? nextStyle.Background : null, alpha, out mappingTransform, out mapper); if (fill != null) { if (mappingTransform != null) { canvas.TextureTransform = mappingTransform; } // We fill shape canvas.FillShape(fill, shape, mapper); // Must reset. if (mappingTransform != null) { canvas.TextureTransform = null; } } } // Now we add border over it. { ITransform transform; IMapper mapper; Pen pen = BorderStyle.Merge(style != null ? style.Border : null, nextStyle != null ? nextStyle.Border : null, alpha, out transform, out mapper); if (pen != null && pen.Fill != null) { if (transform != null) { canvas.TextureTransform = transform; } canvas.DrawShape(pen, border, mapper); if (transform != null) { canvas.TextureTransform = null; } } } }
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(); } }
public DefaultTheme(Font defaultFont) { // AREA rendererMap.Add(typeof(Area).FullName, new AreaRenderer()); // Area style: white with no border. { Area.AreaStyle normalStyle = new Area.AreaStyle(); normalStyle.Background = new BackgroundStyle(new SolidFill(Colour.White)); normalStyle.Border = null; Style xstyle = Style.CreateImmutable <Area.AreaStyle>(true, new KeyValuePair <StyleState, Area.AreaStyle>(CommonStyleStates.Normal, normalStyle)); styleMap.Add(typeof(Area).FullName, xstyle); } // LABEL rendererMap.Add(typeof(Label).FullName, new LabelRenderer()); // Label style: white with black border, black text, blue selection. { Label.LabelStyle style = new Label.LabelStyle(); // For now we do not have font library. style.SelectedTextFont = new PartialFontStyle(new SolidFill(Colour.White)); style.TextFont = new FontStyle(defaultFont, 24, 1.2f, TextOptions.None, new SolidFill(Colour.Black)); style.Background = new BackgroundStyle(new SolidFill(Colour.White)); style.Border = null; style.SelectionBackground = new BackgroundStyle(new SolidFill(Colour.Blue)); Label.LabelStyle pointerOver = new Label.LabelStyle(); pointerOver.Parent = style; Label.LabelStyle focused = new Label.LabelStyle(); focused.Parent = pointerOver; Style xstyle = Style.CreateImmutable <Label.LabelStyle>(true, new KeyValuePair <StyleState, Label.LabelStyle>(CommonStyleStates.Normal, style), new KeyValuePair <StyleState, Label.LabelStyle>(CommonStyleStates.Focused, focused), new KeyValuePair <StyleState, Label.LabelStyle>(CommonStyleStates.PointerOver, pointerOver)); styleMap.Add(typeof(Label).FullName, xstyle); } // BUTTON: rendererMap.Add(typeof(Button).FullName, new ButtonRenderer()); { Button.ButtonStyle normalStyle = new Button.ButtonStyle(); normalStyle.Background = new BackgroundStyle(new SolidFill(Colour.White)); normalStyle.Border = new BorderStyle(new Pen(new SolidFill(Colour.Black), 0.0f, 3.0f, LineMode.Dot)); Button.ButtonStyle pointerOverStyle = new Button.ButtonStyle(); pointerOverStyle.Parent = normalStyle; Button.ButtonStyle clickedStyle = new Button.ButtonStyle(); clickedStyle.Parent = pointerOverStyle; clickedStyle.Border = new BorderStyle(new Pen(new SolidFill(Colour.Red), 0.0f, 3.5f, LineMode.Dot)); Style xstyle = Style.CreateImmutable <Button.ButtonStyle>(true, new KeyValuePair <StyleState, Button.ButtonStyle>(CommonStyleStates.Normal, normalStyle), new KeyValuePair <StyleState, Button.ButtonStyle>(CommonStyleStates.Clicked, clickedStyle), new KeyValuePair <StyleState, Button.ButtonStyle>(CommonStyleStates.PointerOver, pointerOverStyle)); styleMap.Add(typeof(Button).FullName, xstyle); } // POINTER rendererMap.Add(typeof(Standalone.GuiPointer).FullName, new PointerRenderer()); // Pointer style: { Standalone.GuiPointer.GuiPointerStyle style = new SharpMedia.Graphics.GUI.Standalone.GuiPointer.GuiPointerStyle(); style.Background = new BackgroundStyle(new SolidFill(Colour.Black)); style.Border = null; Style xstyle = Style.CreateImmutable <Standalone.GuiPointer.GuiPointerStyle>(true, new KeyValuePair <StyleState, Standalone.GuiPointer.GuiPointerStyle>(CommonStyleStates.Normal, style)); styleMap.Add(typeof(Standalone.GuiPointer).FullName, xstyle); } }