package com.lucasdnd.ags.ui.popup; import org.newdawn.slick.Color; import org.newdawn.slick.Font; import org.newdawn.slick.GameContainer; import org.newdawn.slick.Graphics; import org.newdawn.slick.SlickException; import org.newdawn.slick.geom.Rectangle; import org.newdawn.slick.state.StateBasedGame; import com.lucasdnd.ags.map.OffsetUtil; import com.lucasdnd.ags.system.ResourceLoader; /** * Certain Entities will have a Floating Text object. They're used to show a speech ballon or other * important info to the player. * @author tulio * */ public class FloatingText extends Popup { // Line Size private final float lineSize = 22f; // Box private Rectangle box; // Text, Font and Colors private String text; private String text2; private Font font, shadowFont; private Color lineColor; private Color backgroundColor; public FloatingText(Font font, Font shadowFont, float xPos, float yPos, float xOffset, float yOffset, float entityWidth, float entityHeight, int displayTimeLimit) { // Basic attributes this.xPos = xPos; this.yPos = yPos; this.xOffset = xOffset; this.yOffset = yOffset; this.font = font; this.shadowFont = shadowFont; this.entityWidth = entityWidth; this.entityHeight = entityHeight; // Position, size width = 0f; height = 24f; leftMargin = 5f; localXOffset = 28f; // 0f with Character Scale 1f localYOffset = -38f; // -20f with Character Scale 1f // Creates the box box = new Rectangle(xPos + xOffset + localXOffset, yPos + yOffset + localYOffset, width, height); // Rendering properties lineColor = Color.black; backgroundColor = Color.white; // Visibility this.visible = false; this.displayTime = 0; this.displayTimeLimit = displayTimeLimit; } /** * Counts torwards the time limit and updates offsets. * @param container * @param game * @param delta * @throws SlickException */ public void update(GameContainer container, StateBasedGame game, int delta, float overrideWidth, float overrideHeight) throws SlickException { // Display Time Limit if(visible) { // Check if we need to set an specific Width and Height if(overrideWidth != 0f) { box.setWidth(overrideWidth); } else { box.setWidth(font.getWidth(text) + leftMargin * 2 + 4f); } if(overrideHeight != 0) { box.setHeight(overrideHeight); } else { box.setHeight(lineSize); } // Update the box position box.setX((xPos + xOffset + localXOffset) / 2 - (box.getWidth() / 2) + (entityWidth / 2)); box.setY((yPos + yOffset + localYOffset) / 2 - (entityHeight)); // Check if it reached the display time limit if(displayTime < displayTimeLimit) { displayTime += delta; if(displayTime >= displayTimeLimit) { visible = false; displayTime = 0; } } } } /** * Sets the text, resets the Timer and sets the floating text to visible at the same time * @throws SlickException */ public void show(String text) { this.text = text; displayTime = 0; visible = true; } public void show(String textLine1, String textLine2) throws SlickException { this.text = textLine1; this.text2 = textLine2; displayTime = 0; visible = true; ResourceLoader.getInstance().notificationSound.play(); } /** * Main Render * @param container * @param game * @param g * @throws SlickException */ public void render(GameContainer container, StateBasedGame game, Graphics g) throws SlickException { // Only render if it's visible if(visible) { // Save the previous color Color previousColor = g.getColor(); // Draw the box g.setColor(lineColor); g.setLineWidth(4f); g.draw(box); g.setColor(backgroundColor); g.fill(box); // Draw the Text shadowFont.drawString( OffsetUtil.getPreciseOffset(1 + (leftMargin + 8f + xPos + xOffset + localXOffset) / 2 - (box.getWidth() / 2) + (entityWidth / 2)), OffsetUtil.getPreciseOffset(1 + (yPos + yOffset + localYOffset) / 2 - (entityHeight) + 3f), text); font.drawString( OffsetUtil.getPreciseOffset((leftMargin + 8f + xPos + OffsetUtil.getPreciseOffset(xOffset) + localXOffset) / 2 - (box.getWidth() / 2) + (entityWidth / 2)), OffsetUtil.getPreciseOffset((yPos + OffsetUtil.getPreciseOffset(yOffset) + localYOffset) / 2 - (entityHeight) + 3f), text); if(text2 != null) { shadowFont.drawString( OffsetUtil.getPreciseOffset(1 + (leftMargin + 8f + xPos + xOffset + localXOffset) / 2 - (box.getWidth() / 2) + (entityWidth / 2)), OffsetUtil.getPreciseOffset(1 + (yPos + yOffset + localYOffset) / 2 - (entityHeight) + lineSize), text2); font.drawString( OffsetUtil.getPreciseOffset((leftMargin + 8f + xPos + xOffset + localXOffset) / 2 - (box.getWidth() / 2) + (entityWidth / 2)), OffsetUtil.getPreciseOffset((yPos + yOffset + localYOffset) / 2 - (entityHeight) + lineSize), text2); } // Revert back to the previous color g.setColor(previousColor); } } }