
de.lessvoid.nifty.examples.helloworld.TextTyping Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of nifty-examples Show documentation
Show all versions of nifty-examples Show documentation
Lots of Nifty example code! You can find the source for nearly all demos/tutorials in here.
package de.lessvoid.nifty.examples.helloworld;
import de.lessvoid.nifty.Nifty;
import de.lessvoid.nifty.effects.EffectImpl;
import de.lessvoid.nifty.effects.EffectProperties;
import de.lessvoid.nifty.effects.Falloff;
import de.lessvoid.nifty.elements.Element;
import de.lessvoid.nifty.elements.render.TextRenderer;
import de.lessvoid.nifty.render.NiftyRenderEngine;
import de.lessvoid.nifty.spi.render.RenderFont;
import de.lessvoid.nifty.tools.Color;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
public class TextTyping implements EffectImpl {
// length of the effect in ms
private int effectLength;
// start size of the text size effect
private float startSize;
// end size of the text size effect
private float endSize;
// time we have available for each step (char)
private float stepTime;
// the original text
@Nonnull
private String originalText = "";
// the text renderer (for easy access)
@Nullable
private TextRenderer textRenderer;
@Override
public void activate(@Nonnull Nifty nifty, @Nonnull Element element, @Nonnull EffectProperties parameter) {
textRenderer = element.getRenderer(TextRenderer.class);
if (textRenderer == null) {
originalText = "";
} else {
originalText = textRenderer.getOriginalText();
}
effectLength = Integer.valueOf(parameter.getProperty("length", "1000"));
startSize = Float.valueOf(parameter.getProperty("startSize", "5.0"));
endSize = Float.valueOf(parameter.getProperty("endSize", "1.0"));
stepTime = effectLength / originalText.length();
updateText(null);
}
@Override
public void execute(
@Nonnull Element element,
float effectTime,
@Nullable Falloff falloff,
@Nonnull NiftyRenderEngine r) {
int currentIndex = (int) (originalText.length() * effectTime);
String currentText = originalText.substring(0, currentIndex);
if (currentIndex < originalText.length()) {
String nextChar = originalText.substring(currentIndex, currentIndex + 1);
int textWidth = textRenderer.getFont().getWidth(currentText);
// current time in ms (instead of being in the [0.0,1.0] interval what the effectTime is in)
float currentTime = effectTime * effectLength;
// normalizedStepTime is between 0.0f and 1.0f
float normalizedStepTime = (currentTime % stepTime) / stepTime;
// use normalizedStepTime to calculate the character size for the text size effect
float charSize = startSize + normalizedStepTime * (endSize - startSize);
r.saveStates();
r.setFont(textRenderer.getFont());
r.setRenderTextSize(charSize);
// this part makes sure the text gets correctly centered when the RenderTextSize has been changed
TextRenderer textRenderer = element.getRenderer(TextRenderer.class);
if (textRenderer != null) {
String text = textRenderer.getWrappedText();
RenderFont font = textRenderer.getFont();
float originalWidth = font.getWidth(text, 1.0f);
float sizedWidth = font.getWidth(text, charSize);
float originalHeight = font.getHeight();
float sizedHeight = font.getHeight() * charSize;
r.moveTo(-(sizedWidth - originalWidth) / 2, -(sizedHeight - originalHeight) / 2);
}
r.renderText(nextChar, element.getX() + textWidth, element.getY(), -1, -1, Color.WHITE);
r.restoreStates();
}
updateText(currentText);
}
@Override
public void deactivate() {
}
private void updateText(@Nullable final String currentText) {
if (textRenderer != null) {
textRenderer.setText(currentText);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy