/** * Removes the widget from this layout's widget list. * * @param widget The {@code UIWidget} to be removed from this {@code CardLayout} */ @Override public void removeWidget(UIWidget widget) { String id = widget.getId(); if (id != null) { widgets.remove(id); } }
@Override public void setFocus(UIWidget widget) { if (widget != null && !widget.canBeFocus()) { return; } if (!Objects.equal(widget, focus)) { if (focus != null) { focus.onLoseFocus(); } focus = widget; if (focus != null) { focus.onGainFocus(); } } }
@Override public final <T extends UIWidget> T find(String targetId, Class<T> type) { if (this.id.equals(targetId)) { if (type.isInstance(this)) { return type.cast(this); } return null; } for (UIWidget contents : this) { if (contents != null) { T result = contents.find(targetId, type); if (result != null) { return result; } } } return null; }
private SubRegion subRegionForWidget(UIWidget widget, Rect2i region, boolean crop) { SubRegion result = subRegion(region, crop); state.element = widget; if (widget.getSkin() != null) { setSkin(widget.getSkin()); } if (widget.getFamily() != null) { setFamily(widget.getFamily()); } setPart(UIWidget.BASE_PART); setMode(widget.getMode()); return result; }
private void drawStyledWidget(UIWidget element) { if (element.getTooltip() != null) { // Integrated tooltip support - without this, setting a tooltip value does not make a tooltip work // unless an interaction listener is explicitly added by the widget. addInteractionRegion(new BaseInteractionListener()); } element.onDraw(this); }
@Override public Vector2i calculateRestrictedSize(UIWidget widget, Vector2i sizeRestrictions) { if (widget == null) { return sizeRestrictions; } String family = (widget.getFamily() != null) ? widget.getFamily() : state.family; UISkin skin = (widget.getSkin() != null) ? widget.getSkin() : state.skin; UIStyle elementStyle = skin.getStyleFor(family, widget.getClass(), UIWidget.BASE_PART, widget.getMode()); Rect2i region = applyStyleToSize(Rect2i.createFromMinAndSize(Vector2i.zero(), sizeRestrictions), elementStyle); try (SubRegion ignored = subRegionForWidget(widget, region, false)) { Vector2i preferredSize = widget.getPreferredContentSize(this, elementStyle.getMargin().shrink(sizeRestrictions)); preferredSize = elementStyle.getMargin().grow(preferredSize); return applyStyleToSize(preferredSize, elementStyle); } }
@Override public void drawWidget(UIWidget element, Rect2i region) { if (element == null || !element.isVisible()) { return; } if (nuiManager.getFocus() == element) { focusDrawn = true; } String family = (element.getFamily() != null) ? element.getFamily() : state.family; UISkin skin = (element.getSkin() != null) ? element.getSkin() : state.skin; UIStyle newStyle = skin.getStyleFor(family, element.getClass(), UIWidget.BASE_PART, element.getMode()); Rect2i regionArea; try (SubRegion ignored = subRegionForWidget(element, region, false)) { regionArea = applyStyleToSize(region, newStyle, calculateMaximumSize(element)); } try (SubRegion ignored = subRegionForWidget(element, regionArea, false)) { if (element.isSkinAppliedByCanvas()) { drawBackground(); try (SubRegion withMargin = subRegionForWidget(element, newStyle.getMargin().shrink(Rect2i.createFromMinAndSize(Vector2i.zero(), regionArea.size())), false)) { drawStyledWidget(element); } } else { drawStyledWidget(element); } } }
@Before public void setup() { cardLayout = new CardLayout(); widget1 = mock(UIWidget.class); widget2 = mock(UIWidget.class); widget3 = mock(UIWidget.class); canvas = mock(Canvas.class); // +-----------------------------------+ +---+ +-------+ // | | |1x2| | | // | 1x1 | +---+ | | // | | | 1x3 | // +-----------------------------------+ | | // | | // +-------+ when(widget1.getPreferredContentSize(eq(canvas), any(Vector2i.class))).thenReturn(new Vector2i(50, 10)); when(widget2.getPreferredContentSize(eq(canvas), any(Vector2i.class))).thenReturn(new Vector2i(5, 5)); when(widget3.getPreferredContentSize(eq(canvas), any(Vector2i.class))).thenReturn(new Vector2i(10, 15)); when(widget1.getId()).thenReturn("widget1"); when(widget2.getId()).thenReturn("widget2"); when(widget3.getId()).thenReturn("widget3"); Vector2i availableSize = new Vector2i(200, 200); when(canvas.size()).thenReturn(availableSize); cardLayout.addWidget(widget1); cardLayout.addWidget(widget2); cardLayout.addWidget(widget3); }
@Override public Vector2i calculateMaximumSize(UIWidget widget) { if (widget == null) { return new Vector2i(Integer.MAX_VALUE, Integer.MAX_VALUE); } String family = (widget.getFamily() != null) ? widget.getFamily() : state.family; UIStyle elementStyle = state.skin.getStyleFor(family, widget.getClass(), UIWidget.BASE_PART, widget.getMode()); try (SubRegion ignored = subRegionForWidget(widget, getRegion(), false)) { return applyStyleToSize(elementStyle.getMargin().grow(widget.getMaxContentSize(this)), elementStyle); } }
/** * Retrieves the preferred content size of this {@code CardLayout}. * This is the minimum size this layout will take, given no space restrictions. * * @param canvas The {@code Canvas} on which this {@code CardLayout} is drawn * @param sizeHint A {@link Vector2i} representing the space available for widgets to be drawn in this layout * @return A {@code Vector2i} representing the preferred content size of this {@code CardLayout} */ @Override public Vector2i getPreferredContentSize(Canvas canvas, Vector2i sizeHint) { int maxX = 0; int maxY = 0; for (UIWidget uiWidget : widgets.values()) { Vector2i preferredContentSize = uiWidget.getPreferredContentSize(canvas, sizeHint); maxX = Math.max(maxX, preferredContentSize.x); maxY = Math.max(maxY, preferredContentSize.y); } return new Vector2i(maxX, maxY); }
public UIWidget getTooltip() { if (tooltipOverride == null) { return element.getTooltip(); } return tooltipOverride; }
/** * Retrieves the maximum content size of this {@code CardLayout}. * * @param canvas The {@code Canvas} on which this {@code CardLayout} is drawn * @return A {@code Vector2i} representing the maximum content size of this {@code CardLayout} */ @Override public Vector2i getMaxContentSize(Canvas canvas) { int maxX = 0; int maxY = 0; for (UIWidget uiWidget : widgets.values()) { Vector2i maxContentSize = uiWidget.getMaxContentSize(canvas); maxX = Math.max(maxX, maxContentSize.x); maxY = Math.max(maxY, maxContentSize.y); } return new Vector2i(maxX, maxY); }
if (!newTopMouseOverRegion.equals(topMouseOverRegion)) { topMouseOverRegion = newTopMouseOverRegion; tooltipTime = time.getGameTime() + newTopMouseOverRegion.element.getTooltipDelay(); lastTooltipPosition.set(position); } else { if (lastTooltipPosition.gridDistance(position) > MAX_DOUBLE_CLICK_DISTANCE) { tooltipTime = time.getGameTime() + newTopMouseOverRegion.element.getTooltipDelay(); lastTooltipPosition.set(position);
if (hint != null) { if (hint.isUseContentWidth()) { Vector2i contentSize = contents.get(i).getPreferredContentSize(canvas, new Vector2i(remainingWidthPerElement, canvas.size().y)); results.set(i, contentSize.x); totalWidthUsed += contentSize.x;
/** * Adds the widget to this layout's widget list. * * @param widget The {@link UIWidget} to be added to this {@code CardLayout} * @throws IllegalArgumentException if the widget does not have an id */ public void addWidget(UIWidget widget) { String id = widget.getId(); if (id == null) { throw new IllegalArgumentException("CardLayout requires for each widget to be added to it to have an id"); } widgets.put(id, widget); }
public static void trySubscribe(UIWidget widget, String id, ActivateEventListener listener) { UIButton button = widget.find(id, UIButton.class); if (button != null) { button.subscribe(listener); } else { logger.warn("Contents of {} missing button with id '{}'", widget, id); } }
@Override public void removeWidget(UIWidget widget) { String id = widget.getId(); WidgetInfo info = contentLookup.get(id); contentLookup.remove(id); contents.remove(info); cachedRegions.remove(info); }
public static void tryBindCheckbox(UIWidget widget, String id, Binding<Boolean> binding) { UICheckbox checkbox = widget.find(id, UICheckbox.class); if (checkbox != null) { checkbox.bindChecked(binding); } }
@Override public void addWidget(UIWidget widget, RelativeLayoutHint hint) { if (widget != null && hint != null) { WidgetInfo info = new WidgetInfo(widget, hint); contents.add(info); if (!widget.getId().isEmpty()) { contentLookup.put(widget.getId(), info); } } else if (widget != null) { logger.error("Attempted to add widget '{}' of type '{}' with no layout hint", widget.getId(), widget.getClass().getSimpleName()); } }
/** * Bind a check box and boolean, and a listener will be subscribed to the checkbox. * @param widget the widget. * @param id the id of the checkbox. * @param binding the boolean bound with the checkbox. * @param listener the listener which will activated when the check box is pressed. */ public static void tryBindCheckBoxWithListener(UIWidget widget, String id, Binding<Boolean> binding, ActivateEventListener listener) { UICheckbox checkbox = widget.find(id, UICheckbox.class); if (checkbox != null) { checkbox.bindChecked(binding); checkbox.subscribe(listener); } } }