/// <summary> /// Constructor. /// </summary> /// <param name="manager_">Gui manager to associate the widget with.</param> /// <param name="parent_">widget's parent</param> public Widget(UIManager manager_, Widget parent_) { if (manager_ == null) throw new Exception("The manager is null"); myManager = manager_; Parent = parent_; // notify the wm that this widget type exists. Manager.RegisterWidgetType("Widget", null); }
/// <summary> /// Returns the maximum size that can use a chikld without any parent resizing. /// Must be implemented by widgets like layouts, frames etc... /// </summary> /// <param name="child">Child willing to resize</param> /// <returns>Maximum size that can occupy the child.</returns> public override Vector2f GetMaxSizeForChild(Widget child) { float maxSizeY = Size.Y; foreach (Widget widget in Widgets) { if (widget.Visible && widget != child) maxSizeY -= widget.Size.Y; } return new Vector2f(Size.X, maxSizeY); }
/// <summary> /// Adds a widget at the end. /// When you add a widget to a layout, it's Position will be changed /// to fit with the one the layout want to assign to it. /// </summary> public void Add(Widget obj) { obj.Parent = this; widgets.Add(obj); // No need to update size if the widget isn't visible. if (obj.Visible) { updateSize(); updatePositions(); } }
public BasicLabel(UIManager manager_, Widget parent_, DCFont font, TextStyle style, Color color, string str, uint size = 30 ) : base(manager_, parent_) { Manager.RegisterWidgetType("BasicLabel", "Widget"); myFont = font; myText = str; myTextSize = size; myStyle = style; myTextColor = color; updateSize(); }
/// <summary> /// If possible, gives the focus to the specified widget. /// It's possible that the widget that owned previously the focus /// REFUSE to give the focus to the one you want to give it. /// It's also possible that the desired receiver REFUSES /// to get the focus. /// If you pass a null reference in focusReceiver, then no one /// widget will get the focus from this player. /// </summary> /// <param name="index">Associated player to the focus.</param> /// <param name="focusReceiver">New receiver of the focus.</param> /// <returns>True if the widget received the focus, false otherwise.</returns> public bool SetFocus(Widget focusReceiver) { FocusChangedEvent evt = new FocusChangedEvent(FocusChangedEvent.FocusStateChange.LostFocus); Widget oldFocusedWidget = myFocusedWidget; // the old owner of the focus can refuse the lost of the focus. if (oldFocusedWidget != null) { oldFocusedWidget.OnEvent(evt); if (evt.Accepted) { oldFocusedWidget.OnFocusChangedEvent(evt); if (evt.Accepted) myFocusedWidget = null; } } // the old owner is ready to loose focus // or there is no old owner. if (evt.Accepted && focusReceiver != null) { if (HasFocus(focusReceiver) == false) { // the focus receiver doesn't have the focus from another player. evt.StateChange = FocusChangedEvent.FocusStateChange.GainedFocus; focusReceiver.OnEvent(evt); if (evt.Accepted) { focusReceiver.OnFocusChangedEvent(evt); if (evt.Accepted) { //the widget has accepted the focus. myFocusedWidget = focusReceiver; } } } else evt.Accepted = false;//the receiver has the focus from another player. } return evt.Accepted; }
/// <summary> /// Must be called by a widget if it doesn't have any parent. /// Will affect the style of the widget by applying the default style to it. /// </summary> /// <param name="widget"></param> public void ManageWidget(Widget widget) { if (widget != null) myManagedWidgets.Add(widget); }
/// <summary> /// Called when the child has finished its resize operation. /// </summary> /// <param name="child"></param> /// <param name="size"></param> public virtual void EndChildResize(Widget child, Vector2f size) { }
/// <summary> /// Called by the child to notice that it wants /// to change its size or Visibility. /// </summary> /// <param name="child">Child willing to resize.</param> /// <param name="requestedSize">Size the child wants in the widget.</param> /// <returns>True if resizement granted, false otherwise.</returns> public bool ResizeForChild(Widget child, Vector2f requestedSize) { // May be position of the child widget has changed // and we just want to update the parent accordingly // so we musn't check if requestedSize is the same as before // in the view to NOT froward properly the event. ChildResizeEvent ev = new ChildResizeEvent(child, requestedSize); OnEvent(ev); if (ev.Accepted) { OnChildResizeEvent(ev); if (ev.Accepted) { // We actually need to resize. if (ev.NecessaryParentSize.X > Size.X || ev.NecessaryParentSize.Y > Size.Y) { // Tries to resize to the given size. Vector2f newSize = Resize(ev.NecessaryParentSize); // Check if we resized enough to place the widget in... if (newSize.X < ev.NecessaryParentSize.X || newSize.Y < ev.NecessaryParentSize.Y) return false; } return true; } } return false; }
public Label(UIManager manager_, Widget parent_, DCFont font, string str, uint size = 30 ) : base(manager_, parent_) { Manager.RegisterWidgetType("Label", "Widget"); myCharacterSize = size; Font = font; Text = str; }
public HorizontalSpacer(UIManager manager_, Widget parent_) : base(manager_, parent_) { Manager.RegisterWidgetType("HorizontalSpacer", "Widget"); }
/// <summary> /// Place the widget as much as possible on the top of the rendering stack. /// </summary> /// <param name="widget">Widget to push on top of the rendering stack.</param> void PushWidgetOnTop(Widget widget) { if (myManagedWidgets.Contains(widget) && myManagedWidgets.Count > 1 ) { int index = myManagedWidgets.FindIndex(delegate(Widget widget_) { return widget_ == widget; }); myManagedWidgets.RemoveAt(index); RenderingOrderChangedEvent evt = new RenderingOrderChangedEvent(RenderingOrderChangedEvent.RenderingOrderChange.RenderedLater); //now we must find a new place for this poor widget ;) for (int i = myManagedWidgets.Count - 1; i >= 0; i--) { evt.Accepted = true; myManagedWidgets[i].OnEvent(evt); if (evt.Accepted) { myManagedWidgets[i].OnRenderingOrderChangedEvent(evt); if (evt.Accepted) { myManagedWidgets.Insert(i + 1, widget); break; } } } } }
/// <summary> /// Adds a widget at the specified index. /// When you insert a widget to a layout, it's Position will be changed /// to fit with the one the layout want to assign to it. /// </summary> public void Insert(Int32 index, Widget obj) { obj.Parent = this; widgets.Insert(index, obj); // No need to update size if the widget isn't visible. if (obj.Visible) { updateSize(); updatePositions(); } }
/// <summary> /// Detailed constructor. /// </summary> /// <param name="manager_"></param> /// <param name="parent_"></param> /// <param name="imagePart"></param> public Caption(UIManager manager_, Widget parent_, ImagePart imagePart ) : base(manager_, parent_) { Manager.RegisterWidgetType("Caption", "Widget"); ImagePart = imagePart; }
/// <summary> /// Default construtor too. /// </summary> /// <param name="manager_"></param> /// <param name="parent_"></param> public Caption(UIManager manager_, Widget parent_) : base(manager_, parent_) { Manager.RegisterWidgetType("Caption", "Widget"); }
/// <summary> /// Method that must be implemented if you have a widget that contains widgets. /// This method compute the new widget size so that the child widget can be /// resized. /// </summary> /// <param name="child">Child widget requesting the resizement.</param> /// <param name="requestedSize">Size requested by the widget.</param> /// <returns></returns> protected virtual Vector2f _computeNewSizeForChild(Widget child, Vector2f requestedSize) { return new Vector2f(0f,0f); }
/// <summary> /// Remove a widget from the children list. /// </summary> /// <param name="widget">child to remove.</param> protected void RemoveChild(Widget widget) { myChildrenWidgets.Remove(widget); }
/// <summary> /// Add a widget to the children list. /// </summary> /// <param name="widget">child to add</param> protected void AddChild(Widget widget) { myChildrenWidgets.Add(widget); }
/// <summary> /// Must be called if a widget gain a parent. /// </summary> /// <param name="widget"></param> public void UnmanageWidget(Widget widget) { if (myManagedWidgets.Contains(widget)) myManagedWidgets.Remove(widget); }
/// <summary> /// Update all the managed widgets according to the input devices states. /// </summary> /// <param name="gameTime">game time reference</param> public void Update(Stopwatch gameTime) { /// Dispatch the hovering events. Widget receiver = PickWidget(myCursor.Position); if (myHoveredWidget != receiver) { if (myHoveredWidget != null) { HoverEndEvent evt = new HoverEndEvent(); // the old owner of the focus can refuse the lost of the focus. myHoveredWidget.OnEvent(evt); if (evt.Accepted) myHoveredWidget.OnHoverEndEvent(evt); } myHoveredWidget = receiver; if (receiver != null) { HoverEvent evt = new HoverEvent(); // the old owner of the focus can refuse the lost of the focus. receiver.OnEvent(evt); if (evt.Accepted) receiver.OnHoverEvent(evt); } } foreach (Widget widget in myManagedWidgets) { UpdateEvent raisedEvent = new UpdateEvent(gameTime); widget.OnEvent(raisedEvent); if (raisedEvent.Accepted) widget.OnUpdate(raisedEvent); } }
/// <summary>Removes the widget from the layout.</summary> public void Remove(Widget obj) { obj.Parent = null; widgets.Remove(obj); // No need to update size if the widget wasn't visible. if (obj.Visible) { updateSize(); updatePositions(); } }
public VerticalSpacer(UIManager manager_, Widget parent_) : base(manager_, parent_) { Manager.RegisterWidgetType("VerticalSpacer", "Widget"); }
/// <summary> /// Constructor. /// </summary> /// <param name="manager_"></param> /// <param name="parent_"></param> public Layout(UIManager manager_, Widget parent_) : base(manager_, parent_) { Manager.RegisterWidgetType("Layout", "Widget"); }
public HorizontalSpacer(UIManager manager_, Widget parent_, float length ) : base(manager_, parent_) { Manager.RegisterWidgetType("HorizontalSpacer", "Widget"); Length = length; }
public VerticalLayout(UIManager manager_, Widget parent_) : base(manager_, parent_) { Manager.RegisterWidgetType("VerticalLayout", "Layout"); }
public BasicLabel(UIManager manager_, Widget parent_) : base(manager_, parent_) { Manager.RegisterWidgetType("BasicLabel", "Widget"); }
/// <summary> /// Returns the maximum size that can use a chikld without any parent resizing. /// Must be implemented by widgets like layouts, frames etc... /// </summary> /// <param name="child">Child willing to resize</param> /// <returns>Maximum size that can occupy the child.</returns> public virtual Vector2f GetMaxSizeForChild(Widget child) { return new Vector2f(0f, 0f); }
/// <summary> /// Method that must be implemented if you have a widget that contains widgets. /// This method compute the new widget size so that the child widget can be /// resized. /// </summary> /// <param name="child">Child widget requesting the resizement.</param> /// <param name="requestedSize">Size requested by the widget.</param> /// <returns></returns> protected override Vector2f _computeNewSizeForChild(Widget child, Vector2f requestedSize) { Vector2f size = new Vector2f(0f, 0f); foreach (Widget widg in Widgets) { if (child == widg) { size.Y += requestedSize.Y; size.X = requestedSize.X > size.X ? requestedSize.X : size.X; } else { size.Y += widg.Size.Y; size.X = widg.Size.X > size.X ? widg.Size.X : size.X; } } return size; }
public override void EndChildResize(Widget child, Vector2f size) { base.EndChildResize(child, size); updatePositions(); }
/// <summary> /// Tells wether or not this widget has the focus. /// </summary> /// <param name="widget">Widget to test.</param> /// <returns>True if the widget has the focus, false otherwise.</returns> public bool HasFocus(Widget widget) { return myFocusedWidget == widget; }
/// <summary> /// Constructor. /// </summary> /// <param name="manager_"></param> /// <param name="parent_"></param> public HorizontalLayout(UIManager manager_, Widget parent_) : base(manager_, parent_) { Manager.RegisterWidgetType("HorizontalLayout", "Layout"); }