/// <summary> /// Calculate the correct position and size of a tab button, based on the /// index it is due to be placed at. /// </summary> /// <param name="index"> /// The index of the tab button /// </param> protected void CalculateTabButtonSizePosition(int index) { var btn = d_tabButtonVector[index]; // relative height is always 1.0 for buttons since they are embedded in a // panel of the correct height already var position = new UVector2(UDim.Absolute(0.0f), UDim.Absolute(0.0f)); var size = new USize(UDim.Absolute(0.0f), UDim.Relative(1.0f)); // x position is based on previous button if (index == 0) { // First button position.d_x = UDim.Absolute(d_firstTabOffset); } else { var prevButton = d_tabButtonVector[index - 1]; // position is prev pos + width position.d_x = prevButton.GetArea().d_max.d_x; } size.d_width = UDim.Absolute(btn.GetRenderedString().GetHorizontalExtent(btn)) + GetTabTextPadding() + GetTabTextPadding(); btn.SetPosition(position); btn.SetSize(size); var leftX = position.d_x.d_offset; btn.SetVisible((leftX < GetPixelSize().Width) && (leftX + btn.GetPixelSize().Width > 0)); btn.Invalidate(false); }
/// <summary> /// Move the window by the pixel offsets specified in \a offset. /// This is intended for internal system use - it is the method by /// which the title bar moves the frame window. /// </summary> /// <param name="offset"> /// Vector2 object containing the offsets to apply (offsets are in screen pixels). /// </param> public void OffsetPixelPosition(Lunatics.Mathematics.Vector2 offset) { var uOffset = new UVector2(UDim.Absolute(/*PixelAligned(*/ offset.X /*)*/), UDim.Absolute(/*PixelAligned(*/ offset.Y /*)*/)); SetPosition(d_area.Position + uOffset); }
protected override void LayoutItemWidgets() { // get render area var renderRect = GetItemRenderArea(); // get starting position var x0 = CoordConverter.AlignToPixels(renderRect.d_min.X); var y0 = CoordConverter.AlignToPixels(renderRect.d_min.Y); var sz = new UVector2(UDim.Absolute(CoordConverter.AlignToPixels(renderRect.Width)), UDim.Absolute(0)); // set item width // iterate through all items attached to this window foreach (var item in ListItems) { // get the "optimal" height of the item and use that! sz.d_y.d_offset = CoordConverter.AlignToPixels(item.GetItemPixelSize().Height); // rounding errors ? // set destination rect var rect = new URect { Position = new UVector2(UDim.Absolute(x0), UDim.Absolute(y0)), Size = new USize(sz.d_x, sz.d_y) }; // todo: vector vs size item.SetArea(rect); // next position y0 += CoordConverter.AlignToPixels(sz.d_y.d_offset + ItemSpacing); } }
public TestWindow() { Size = new USize(UDim.Relative(0.5f)); Position = new UVector2(UDim.Absolute(200), UDim.Absolute(50)); Title = "MY BEAUTIJFUL TEST WINDOW!?"; CanClose = true; Icon = "Inventory"; }
/// <summary> /// Update state for window dragging. /// </summary> /// <param name="localMouse"> /// Mouse position as a pixel offset from the top-left corner of this window. /// </param> protected void DoDragging(Lunatics.Mathematics.Vector2 localMouse) { // calculate amount to move var offset = new UVector2(UDim.Absolute(localMouse.X), UDim.Absolute(localMouse.Y)); offset -= (_usingFixedDragOffset) ? _fixedDragOffset : _dragPoint; // set new position SetPosition(GetPosition() + offset); // Perform event notification OnDragPositionChanged(new WindowEventArgs(this)); }
/// <summary> /// Opens the PopupMenu. /// </summary> /// <param name="notify"> /// true if the parent menu bar or menu popup (if any) is to handle the open. /// </param> public void OpenPopupMenu(bool notify = true) { // no popup? or already open... if (_popup == null || _opened) { return; } _popupOpening = false; _popupClosing = false; // should we notify ? // if so, and we are attached to a menu bar or popup menu, we let it handle the "activation" var p = OwnerList; if (notify && p != null) { var menubar = p as Menubar; if (menubar != null) { // align the popup to the bottom-left of the menuitem var pos = new UVector2(UDim.Absolute(0), UDim.Absolute(d_pixelSize.Height)); _popup.SetPosition(pos + _popupOffset); menubar.ChangePopupMenuItem(this); return; // the rest is handled when the menu bar eventually calls us itself } // or maybe a popup menu? var popupMenu = p as PopupMenu; if (popupMenu != null) { // align the popup to the top-right of the menuitem var pos = new UVector2(UDim.Absolute(d_pixelSize.Width), UDim.Absolute(0)); _popup.SetPosition(pos + _popupOffset); popupMenu.ChangePopupMenuItem(this); return; // the rest is handled when the popup menu eventually calls us itself } } // by now we must handle it ourselves // match up with Menubar::changePopupMenu _popup.OpenPopupMenu(false); _opened = true; Invalidate(false); }
/// <summary> /// returns bounding size for window, including margins /// </summary> /// <param name="window"></param> /// <returns></returns> protected virtual UVector2 GetBoundingSizeForWindow(Window window) { var pixelSize = window.GetPixelSize(); // we rely on pixelSize rather than mixed absolute and relative getSize // this seems to solve problems when windows overlap because their size // is constrained by min size var size = new UVector2(new UDim(0, pixelSize.Width), new UDim(0, pixelSize.Height)); // TODO: we still do mixed absolute/relative margin, should we convert the value to absolute? var margin = window.GetMargin(); return(new UVector2(margin.d_left + size.d_x + margin.d_right, margin.d_top + size.d_y + margin.d_bottom)); }
/// <summary> /// Update the content container position according to the current /// state of the widget (like scrollbar positions, etc). /// </summary> protected void UpdateContainerPosition() { // basePos is the position represented by the scrollbars // (these are negated so pane is scrolled in the correct directions) var basePos = new UVector2(UDim.Absolute(-GetHorzScrollbar().GetScrollPosition()), UDim.Absolute(-GetVertScrollbar().GetScrollPosition())); // this bias is the absolute position that 0 on the scrollbars represent. // Allows the pane to function correctly with negatively positioned content. var bias = new UVector2(UDim.Absolute(_contentRect.d_min.X), UDim.Absolute(_contentRect.d_min.Y)); // set the new container pane position to be what the scrollbars request // minus any bias generated by the location of the content. GetScrolledContainer().SetPosition(basePos - bias); }
public HotbarPanel() : base("PanelAlt1", false) { Size = new USize(UDim.Relative(0.2f), UDim.Absolute(41f)); Position = new UVector2(UDim.Relative(0.4f), new UDim(1f, -41f)); InterestedKeys.Add(Keys.D1); InterestedKeys.Add(Keys.D2); InterestedKeys.Add(Keys.D3); InterestedKeys.Add(Keys.D4); InterestedKeys.Add(Keys.D5); InterestedKeys.Add(Keys.D6); InterestedKeys.Add(Keys.D7); InterestedKeys.Add(Keys.D8); InterestedKeys.Add(Keys.D9); InterestedKeys.Add(Keys.D0); SelectedIndex = 1; }
private void ReinitializeSlots() { var basePosition = new UVector2(UDim.Absolute(3f)); Children.Clear(); for (int i = 0; i < HotbarSlots; i++) { Add(new ImageElement("InventorySlot") { Position = basePosition + new UVector2(UDim.Absolute(i * 38f), UDim.Zero) }); if (i == 0) { Add(new ImageElement("ShovelIcon") { Position = basePosition + new UVector2(UDim.Absolute(i * 38f + 2f), UDim.Absolute(1f)) }); } var text = (i + 1).ToString(); if (i + 1 == 10) { text = "0"; } Add(new TextElement() { Text = text, Position = basePosition + new UVector2(UDim.Absolute(i * 38f + 3f), UDim.Absolute(1f)), Color = (i == 0) ? Color.Yellow : Color.White }); } foreach (var child in Children) { child.LoadContent(_contentManager); } _slotsInitialized = HotbarSlots; }
// overridden from Slider base class. public override void UpdateThumb() { var w = (Slider)Window; // get area the thumb is supposed to use as it's area. var wlf = GetLookNFeel(); var area = wlf.GetNamedArea("ThumbTrackArea").GetArea().GetPixelRect(w); // get accesss to the thumb var theThumb = w.GetThumb(); var pixelSize = w.GetPixelSize(); var thumbRelXPos = pixelSize.Width == 0.0f ? 0.0f : (area.Left / pixelSize.Width); var thumbRelYPos = pixelSize.Height == 0.0f ? 0.0f : (area.Top / pixelSize.Height); // get base location for thumb widget var thumbPosition = new UVector2(UDim.Relative(thumbRelXPos), UDim.Relative(thumbRelYPos)); // Is this a vertical slider if (_vertical) { // pixel extent of total available area the thumb moves in var slideExtent = area.Height - theThumb.GetPixelSize().Height; // Set range of motion for the thumb widget if (pixelSize.Height != 0.0f) { theThumb.SetVertRange(area.Top / pixelSize.Height, (area.Top + slideExtent) / pixelSize.Height); } else { theThumb.SetVertRange(0.0f, 0.0f); } // calculate vertical positon for thumb var thumbOffset = w.GetCurrentValue() * (slideExtent / w.GetMaxValue()); if (pixelSize.Height != 0.0f) { thumbPosition.d_y.d_scale += (_reversed ? thumbOffset : slideExtent - thumbOffset) / pixelSize.Height; } } // Horizontal slider else { // pixel extent of total available area the thumb moves in var slideExtent = area.Width - theThumb.GetPixelSize().Width; // Set range of motion for the thumb widget if (pixelSize.Width != 0.0f) { theThumb.SetHorzRange(area.Left / pixelSize.Width, (area.Left + slideExtent) / pixelSize.Width); } else { theThumb.SetHorzRange(0.0f, 0.0f); } // calculate horizontal positon for thumb var thumbOffset = w.GetCurrentValue() * (slideExtent / w.GetMaxValue()); if (pixelSize.Width != 0.0f) { thumbPosition.d_x.d_scale += (_reversed ? slideExtent - thumbOffset : thumbOffset) / pixelSize.Width; } } // set new position for thumb. theThumb.SetPosition(thumbPosition); }
/// <summary> /// sets the current offset for popup placement. /// </summary> /// <param name="popupOffset"></param> public void SetPopupOffset(UVector2 popupOffset) { _popupOffset = popupOffset; }
/// <summary> /// Set the fixed mouse cursor dragging offset to be used for this DragContainer. /// </summary> /// <param name="offset"> /// UVector2 describing the fixed offset to be used when dragging this DragContainer. /// </param> /// <remarks> /// This offset is only used if it's use is enabled via the setUsingFixedDragOffset function. /// </remarks> public void SetFixedDragOffset(UVector2 offset) { _fixedDragOffset = offset; }
/// <summary> /// Helper methods to make drawing uniform. If it ever changes, we only have to do it here. /// </summary> public void Draw(Sprite sprite, UVector2 position) { sprite.Draw(SpriteBatch, position.ToVector2()); }
/// <summary> /// Helper methods to make drawing uniform. If it ever changes, we only have to do it here. /// Note: The color parameter doesn't actually specify it's color. It does some tinting /// because of the way SpriteFonts work. /// </summary> public void DrawString(SpriteFont font, string text, UVector2 position, Color color) { SpriteBatch.DrawString(font, text, position.ToVector2(), color); }
protected override void SetAreaImpl(UVector2 pos, USize size, bool topLeftSizing = false, bool fireEvents = true) { _clientChildContentArea.InvalidateCache(); base.SetAreaImpl(pos, size, topLeftSizing, fireEvents); }
/// <summary> /// Called before every visual draw, the context updates the Visual being drawn. If we can't move anything, in the future it might be /// handy TODO: only calculate it once and only do it again if the elements update/viewport changes. /// /// Method calculates based on USize, UVector and most importantly UDim. Everything is based off of the parent if any, otherwise screen. /// </summary> /// <param name="visual">Visual being drawn.</param> public void InitializeVisualDraw(Visual visual) { Visual parent = visual.Parent; // Get screen bounds var visualPosition = visual.Position; UVector2 actualPosition; var visualSize = visual.Size; USize actualSize; float screenWidth = ViewportWidth; float screenHeight = ViewportHeight; if (parent == null) { // Sizing based on parent null which means screen resolution if (visualSize.IsAbsolute()) { actualSize = visualSize; } else { var width = visualSize.Width.Scale * screenWidth + visualSize.Width.Offset; var height = visualSize.Height.Scale * screenHeight + visualSize.Height.Offset; actualSize = new USize(UDim.Absolute(width), UDim.Absolute(height)); } // Positioning based on parent null which means screen resolution if (visualPosition.IsAbsolute()) { // This is easy, it's a top level absolute actualPosition = visualPosition; } else { var xDim = visualPosition.X; var yDim = visualPosition.Y; var x = xDim.Scale * screenWidth + xDim.Offset; var y = yDim.Scale * screenHeight + yDim.Offset; actualPosition = new UVector2(UDim.Absolute(x), UDim.Absolute(y)); } } else { // Positioning based on parent var parentActualPosition = parent.ActualPosition; var parentActualSize = parent.ActualSize; if (visualPosition.IsAbsolute()) { // Child is using absolute positioning _under_ the parents area. actualPosition = parentActualPosition + visualPosition; } else { var xDim = visualPosition.X; var yDim = visualPosition.Y; var x = parentActualPosition.X.Absolute() + xDim.Scale * parentActualSize.Width.Absolute() + xDim.Offset; var y = parentActualPosition.Y.Absolute() + yDim.Scale * parentActualSize.Height.Absolute() + yDim.Offset; actualPosition = new UVector2(UDim.Absolute(x), UDim.Absolute(y)); } // Sizing based on parent if (visualSize.IsAbsolute()) { // Child using absolute sizing, in this case it doesn't really matter..children should never do this though because // if a child is 200 pixels wide but the parent is 0.2 (relative) wide the child could be larger than the parent. actualSize = visualSize; } else { var width = visualSize.Width.Scale * parentActualSize.Width.Absolute() + visualSize.Width.Offset; var height = visualSize.Height.Scale * parentActualSize.Height.Absolute() + visualSize.Height.Offset; actualSize = new USize(UDim.Absolute(width), UDim.Absolute(height)); } } visual.ActualPosition = actualPosition; visual.ActualSize = actualSize; }