protected override GuiSize DoArrange(GuiSize arrangeSize) { GuiMinMax mm = new GuiMinMax(this); arrangeSize.Width = Math.Max(mm.minWidth, Math.Min(arrangeSize.Width, mm.maxWidth)); arrangeSize.Height = Math.Max(mm.minHeight, Math.Min(arrangeSize.Height, mm.maxHeight)); if (Childs != null && Childs.Count > 0) { int offsetY = 0; int childLeft = Childs.Count; int heightLeft = arrangeSize.Height; foreach (var child in Childs) { int childHeight = child.DesiredSize.Height; //if (childLeft > 1) //{ // childHeight = heightLeft / childLeft; //} //else //{ // childHeight = heightLeft; //} GuiRect childRect = new GuiRect(FinalRect.X + Margin.Left, FinalRect.Y + Margin.Top + offsetY, arrangeSize.Width, childHeight); child.Arrange(childRect); offsetY += childHeight; heightLeft -= childHeight; childLeft--; } } return(arrangeSize); }
/// <summary> /// Area with preferred rect and style. /// </summary> /// <param name="preferredRect"></param> /// <param name="style"></param> public Area(GuiRect preferredRect, Style style) : this(preferredRect) { if (!IsStyleCompatible(style)) { throw new ArgumentException("Style is not compatible with area."); } this.style = style; }
public override void DrawCore(SpriteBatch spriteBatch, GuiRect finalRect) { base.DrawCore(spriteBatch, finalRect); Rectangle r = new Rectangle( Convert.ToInt32(finalRect.X), Convert.ToInt32(finalRect.Y), Convert.ToInt32(finalRect.Width), Convert.ToInt32(finalRect.Height)); GuiPainter.DrawRectangle(spriteBatch, r, Color.White, fTexture); }
protected override GuiSize DoArrange(GuiSize arrangeSize) { int totalChildrenCount = Childs.Count; int nonFillChildrenCount = totalChildrenCount - (LastChildFill ? 1 : 0); int accumulatedLeft = 0; int accumulatedTop = 0; int accumulatedRight = 0; int accumulatedBottom = 0; for (int i = 0; i < totalChildrenCount; ++i) { GuiDockChild child = Childs[i]; GuiSize childDesiredSize = child.Control.DesiredSize; GuiRect rcChild = new GuiRect( accumulatedLeft, accumulatedTop, Math.Max(0, arrangeSize.Width - (accumulatedLeft + accumulatedRight)), Math.Max(0, arrangeSize.Height - (accumulatedTop + accumulatedBottom))); if (i < nonFillChildrenCount) { switch (child.Dock) { case GuiDock.Left: accumulatedLeft += childDesiredSize.Width; rcChild.Width = childDesiredSize.Width; break; case GuiDock.Right: accumulatedRight += childDesiredSize.Width; rcChild.X = Math.Max(0, arrangeSize.Width - accumulatedRight); rcChild.Width = childDesiredSize.Width; break; case GuiDock.Top: accumulatedTop += childDesiredSize.Height; rcChild.Height = childDesiredSize.Height; break; case GuiDock.Bottom: accumulatedBottom += childDesiredSize.Height; rcChild.Y = Math.Max(0, arrangeSize.Height - accumulatedBottom); rcChild.Height = childDesiredSize.Height; break; } } child.Control.Arrange(new GuiSize(rcChild.Width, rcChild.Height)); child.ContainerPosition = new GuiPoint(rcChild.Left, rcChild.Top); } return(arrangeSize); }
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(); } }
/// <summary> /// Creates a display object based button. /// </summary> /// <param name="displayObject"></param> public Button(GuiRect preferredRect, IDisplayObject displayObject) : this(displayObject) { this.preferredRect = preferredRect; }
/// <summary> /// Creates a label based button. /// </summary> /// <param name="text"></param> /// <param name="labelStyle"></param> public Button(GuiRect preferredRect, string text, Style labelStyle) : this(text, labelStyle) { this.preferredRect = preferredRect; }
/// <summary> /// Area with preferred rect initialization. /// </summary> /// <param name="preferredRect"></param> public Area(GuiRect preferredRect) : this() { this.preferredRect = preferredRect; }
protected Area(SerializationInfo info, StreamingContext context) { serializationOptions = (WidgetSerialization)info.GetValue("SerializeOptions", typeof(WidgetSerialization)); zOrder = info.GetUInt32("ZOrder"); parent = (IWidget)info.GetValue("Parent", typeof(IWidget)); description = info.GetString("Description"); containedDescriptionShowUpTime = info.GetSingle("ContainedDescriptionShowUpTime"); containedDescriptionPositioning = (LayoutAnchor)info.GetValue("ContainedDescriptionPositioning", typeof(LayoutAnchor)); containedDescriptionObject = (IDisplayObject)info.GetValue("ContainedDescriptionObject", typeof(IDisplayObject)); anchor = (LayoutAnchor)info.GetValue("Anchor", typeof(LayoutAnchor)); minSize = (GuiVector2)info.GetValue("MinSize", typeof(GuiVector2)); maxSize = (GuiVector2)info.GetValue("MaxSize", typeof(GuiVector2)); preferredSize = (GuiVector2)info.GetValue("PreferredSize", typeof(GuiVector2)); preserveRatio = info.GetBoolean("PreserveRatio"); preferredRect = (GuiRect)info.GetValue("PreferredRect", typeof(GuiRect)); marginLeft = (GuiScalar)info.GetValue("MarginLeft", typeof(GuiScalar)); marginRight = (GuiScalar)info.GetValue("MarginRight", typeof(GuiScalar)); marginTop = (GuiScalar)info.GetValue("MarginTop", typeof(GuiScalar)); marginBottom = (GuiScalar)info.GetValue("MarginBottom", typeof(GuiScalar)); if ((serializationOptions & WidgetSerialization.PersistAllStyles) > 0) { style = (Style)info.GetValue("Style", typeof(Style)); } else if ((serializationOptions & WidgetSerialization.PersistNonThemeStyles) > 0) { style = (Style)info.GetValue("Style", typeof(Style)); } if ((serializationOptions & WidgetSerialization.PersistRenderer) > 0) { renderer = (Themes.IGuiRenderer)info.GetValue("Renderer", typeof(Themes.IGuiRenderer)); } if ((serializationOptions & WidgetSerialization.PersistState) > 0) { isVisible = info.GetBoolean("IsVisible"); } if ((serializationOptions & WidgetSerialization.PersistAnimations) > 0) { animationState = (StyleAnimationController)info.GetValue("StyleAnimationController", typeof(StyleAnimationController)); activeAnimations = (List <AnimationProcess>)info.GetValue("ActiveAnimations", typeof(List <AnimationProcess>)); } if ((serializationOptions & WidgetSerialization.PersistEventBindings) > 0) { onChange = (Action <IPreChangeNotifier>)info.GetValue("OnChange", typeof(Action <IPreChangeNotifier>)); onMouseOver = (Action2 <Area, IPointer>)info.GetValue("OnMouseOver", typeof(Action2 <Area, IPointer>)); onMouseLeave = (Action2 <Area, IPointer>)info.GetValue("OnMouseLeave", typeof(Action2 <Area, IPointer>)); onMousePressed = (Action4 <Area, IPointer, uint, InputEventModifier>)info.GetValue("OnMousePressed", typeof(Action4 <Area, IPointer, uint, InputEventModifier>)); onMouseReleased = (Action3 <Area, IPointer, uint>)info.GetValue("OnMouseReleased", typeof(Action3 <Area, IPointer, uint>)); onWheel = (Action3 <Area, IPointer, float>)info.GetValue("OnWheel", typeof(Action3 <Area, IPointer, float>)); onMouseMove = (Action3 <Area, IPointer, Vector2f>)info.GetValue("OnMouseMove", typeof(Action3 <Area, IPointer, Vector2f>)); onFocusGain = (Action <Area>)info.GetValue("OnFocusGain", typeof(Action <Area>)); onFocusLost = (Action <Area>)info.GetValue("OnFocusLost", typeof(Action <Area>)); onKeyPressed = (Action5 <Area, IPointer, KeyCodes, KeyboardModifiers, InputEventModifier>)info.GetValue( "OnKeyPressed", typeof(Action5 <Area, IPointer, KeyCodes, KeyboardModifiers, InputEventModifier>)); onKeyReleased = (Action4 <Area, IPointer, KeyCodes, KeyboardModifiers>)info.GetValue("OnKeyReleased", typeof(Action4 <Area, IPointer, KeyCodes, KeyboardModifiers>)); onDescriptionShow = (Action2 <Area, IWidget>)info.GetValue("OnDescriptionShow", typeof(Action2 <Area, IWidget>)); onDescriptionHide = (Action2 <Area, IWidget>)info.GetValue("OnDescriptionHide", typeof(Action2 <Area, IWidget>)); onStyleStateChange = (Action3 <Area, StyleState, StyleState>)info.GetValue("OnStyleStateChange", typeof(Action3 <Area, StyleState, StyleState>)); } }
public void Parse(CompileContext context, Stack <Token> stream) { // We have following parsing: // [Type:]GuiVector2, GuiVector2 // where Type = MinMax | LeftBottomAndSize | RightBottomAndSize | LeftTopAndSize | RightTopAndSize // We first check for type. Token token = stream.Pop(); RectangleMode mode = RectangleMode.MinMax; if (token.TokenId == TokenId.Identifier) { string id = token.Identifier; switch (id.ToLower()) { case "minmax": mode = RectangleMode.MinMax; break; case "leftbottomandsize": mode = RectangleMode.LeftBottomAndSize; break; case "rightbottomandsize": mode = RectangleMode.RightBottomAndSize; break; case "lefttopandsize": mode = RectangleMode.LeftTopAndSize; break; case "righttopandsize": mode = RectangleMode.RightTopAndSize; break; default: stream.Push(token); throw new ParseException("Expected a valid type identifier for rectangle."); } token = stream.Pop(); if (token.TokenId != TokenId.Colon) { stream.Push(token); throw new ParseException("If specifier for rectangle is used, it must be followed by ':'."); } token = stream.Pop(); } stream.Push(token); ASTVector2 v1 = new ASTVector2(), v2 = new ASTVector2(); v1.Parse(context, stream); token = stream.Pop(); if (token.TokenId != TokenId.Comma) { stream.Push(token); throw new ParseException("Comma expected to seperate vectors in rectangle."); } v2.Parse(context, stream); // We now apply data. rectangle = new GuiRect(mode, v1.Vector, v2.Vector); }
public void Parse(CompileContext context, System.Xml.XmlNode node) { XmlAttributeCollection attr = node.Attributes; ASTScalar left = null, right = null, top = null, bottom = null, width = null, height = null; if (attr["Left"] != null) { left = new ASTScalar(); left.Parse(context, Token.StackForm(Token.Tokenize(attr["Left"].InnerText))); } if (attr["Right"] != null) { right = new ASTScalar(); right.Parse(context, Token.StackForm(Token.Tokenize(attr["Right"].InnerText))); } if (attr["Top"] != null) { top = new ASTScalar(); top.Parse(context, Token.StackForm(Token.Tokenize(attr["Top"].InnerText))); } if (attr["Bottom"] != null) { bottom = new ASTScalar(); bottom.Parse(context, Token.StackForm(Token.Tokenize(attr["Bottom"].InnerText))); } if (attr["Width"] != null) { width = new ASTScalar(); width.Parse(context, Token.StackForm(Token.Tokenize(attr["Width"].InnerText))); } if (attr["Height"] != null) { height = new ASTScalar(); height.Parse(context, Token.StackForm(Token.Tokenize(attr["Height"].InnerText))); } // We now try combinations, must be defined somehow. We check four corners. if (left != null && right != null && top != null && bottom != null && height == null && width == null) { this.rectangle = new GuiRect(RectangleMode.MinMax, new GuiVector2(left.Scalar, bottom.Scalar), new GuiVector2(right.Scalar, top.Scalar)); return; } // We default width/height. if (width == null) { width = new ASTScalar(); width.Scalar = new GuiScalar(0.0f); } if (height == null) { height = new ASTScalar(); height.Scalar = new GuiScalar(0.0f); } // Now we check combinations. if (left != null && right == null && top == null && bottom != null) { this.rectangle = new GuiRect(RectangleMode.LeftBottomAndSize, new GuiVector2(left.Scalar, bottom.Scalar), new GuiVector2(width.Scalar, height.Scalar)); return; } if (left != null && right == null && top != null && bottom == null) { this.rectangle = new GuiRect(RectangleMode.LeftTopAndSize, new GuiVector2(left.Scalar, top.Scalar), new GuiVector2(width.Scalar, height.Scalar)); return; } if (left == null && right != null && top != null && bottom == null) { this.rectangle = new GuiRect(RectangleMode.RightTopAndSize, new GuiVector2(right.Scalar, top.Scalar), new GuiVector2(width.Scalar, height.Scalar)); return; } if (left == null && right != null && top == null && bottom != null) { this.rectangle = new GuiRect(RectangleMode.RightBottomAndSize, new GuiVector2(right.Scalar, bottom.Scalar), new GuiVector2(width.Scalar, height.Scalar)); return; } throw new ParseException("The rectangle is malformed."); }