use of org.terasology.rendering.nui.SubRegion in project Terasology by MovingBlocks.
the class CursorAttachment method onDraw.
@Override
public void onDraw(Canvas canvas) {
UIStyle style = canvas.getCurrentStyle();
Vector2i attachmentSize = canvas.calculatePreferredSize(attachment);
attachmentSize.add(style.getMargin().getTotals());
// TODO get rid of CoreRegistry. e.g. by updatin a mousePos field with an InteractionListener
MouseDevice mouse = CoreRegistry.get(InputSystem.class).getMouseDevice();
int top;
switch(style.getVerticalAlignment()) {
case TOP:
top = mouse.getPosition().y - attachmentSize.y;
break;
case MIDDLE:
top = mouse.getPosition().y - attachmentSize.y / 2;
break;
default:
top = mouse.getPosition().y + MOUSE_CURSOR_HEIGHT;
break;
}
top = TeraMath.clamp(top, 0, canvas.size().y - attachmentSize.y);
int left;
switch(style.getHorizontalAlignment()) {
case RIGHT:
left = mouse.getPosition().x - attachmentSize.x;
break;
case CENTER:
left = mouse.getPosition().x - attachmentSize.x / 2;
break;
default:
left = mouse.getPosition().x;
break;
}
left = TeraMath.clamp(left, 0, canvas.size().x - attachmentSize.x);
try (SubRegion ignored = canvas.subRegion(Rect2i.createFromMinAndSize(left, top, attachmentSize.x, attachmentSize.y), false)) {
canvas.drawBackground();
canvas.drawWidget(attachment, style.getBackgroundBorder().shrink(canvas.getRegion()));
}
}
use of org.terasology.rendering.nui.SubRegion in project Terasology by MovingBlocks.
the class UIText method drawAll.
protected void drawAll(Canvas canvas, int multilineWidth) {
if (text.get() == null) {
text.set("");
}
if (text.get().equals("")) {
text.set(hintText);
isShowingHintText = true;
}
if (isShowingHintText) {
setCursorPosition(0);
if (!text.get().equals(hintText) && text.get().endsWith(hintText)) {
text.set(text.get().substring(0, text.get().length() - hintText.length()));
setCursorPosition(text.get().length());
isShowingHintText = false;
}
}
lastFont = canvas.getCurrentStyle().getFont();
correctCursor();
String textToDraw = passwordMode ? buildPasswordString() : text.get();
int widthForDraw = (multiline) ? multilineWidth : lastFont.getWidth(textToDraw);
try (SubRegion ignored = canvas.subRegion(canvas.getRegion(), true);
SubRegion ignored2 = canvas.subRegion(Rect2i.createFromMinAndSize(-offset, 0, widthForDraw + 1, Integer.MAX_VALUE), false)) {
if (isShowingHintText && !readOnly) {
canvas.drawTextRaw(textToDraw, lastFont, canvas.getCurrentStyle().getHintTextColor(), canvas.getRegion());
} else {
canvas.drawText(textToDraw, canvas.getRegion());
}
if (isFocused()) {
if (hasSelection()) {
drawSelection(canvas);
} else {
drawCursor(canvas);
}
}
}
}
use of org.terasology.rendering.nui.SubRegion in project Terasology by MovingBlocks.
the class CanvasImpl method calculateRestrictedSize.
@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);
}
}
use of org.terasology.rendering.nui.SubRegion in project Terasology by MovingBlocks.
the class ScrollableArea method layoutWithJustVertical.
private void layoutWithJustVertical(Canvas canvas, Vector2i contentSize, int fullWidth, int availableHeight, int scrollbarWidth) {
int availableWidth = fullWidth - scrollbarWidth;
boolean atBottom = verticalBar.getRange() == verticalBar.getValue();
Rect2i contentRegion = Rect2i.createFromMinAndSize(0, 0, availableWidth, availableHeight);
verticalBar.setRange(contentSize.y - contentRegion.height());
if ((stickToBottom && atBottom) || moveToBottomPending) {
verticalBar.setValue(verticalBar.getRange());
moveToBottomPending = false;
}
if (moveToTopPending) {
verticalBar.setValue(0);
moveToTopPending = false;
}
canvas.addInteractionRegion(scrollListener);
canvas.drawWidget(verticalBar, Rect2i.createFromMinAndSize(availableWidth, 0, scrollbarWidth, availableHeight));
try (SubRegion ignored = canvas.subRegion(contentRegion, true)) {
canvas.drawWidget(content, Rect2i.createFromMinAndSize(0, -verticalBar.getValue(), availableWidth, contentSize.y));
}
}
use of org.terasology.rendering.nui.SubRegion in project Terasology by MovingBlocks.
the class UIDoubleSlider method drawTicker.
private void drawTicker(Canvas canvas, String part, Binding<Float> value, InteractionListener tickerListener, boolean rightTicker) {
canvas.setPart(part);
String display = String.format("%." + precision + "f", value.get());
int tickerWidth = canvas.getCurrentStyle().getFont().getWidth(formatString);
tickerWidth += canvas.getCurrentStyle().getMargin().getTotalWidth();
sliderWidth = canvas.size().x - tickerWidth * 2;
int drawLocation = pixelOffsetFor(value.get(), sliderWidth);
if (rightTicker) {
drawLocation += tickerWidth;
}
Rect2i tickerRegion = Rect2i.createFromMinAndSize(drawLocation, 0, tickerWidth, canvas.size().y);
try (SubRegion ignored = canvas.subRegion(tickerRegion, false)) {
canvas.drawBackground();
canvas.drawText(display);
if (isEnabled()) {
canvas.addInteractionRegion(tickerListener);
}
}
}
Aggregations