From c665185b6b5017d5a065ba20c0f6893adc0d15a8 Mon Sep 17 00:00:00 2001 From: "nathan.sweet" Date: Sun, 10 Apr 2011 01:00:47 +0000 Subject: [PATCH] [added] Layout class so scene2d actors can support being laid out. [changed] Label to support multiline and wrapped text. --- .../badlogic/gdx/backends/openal/OpenALAudio.java | 1 + gdx/src/com/badlogic/gdx/math/Matrix4.java | 2 +- .../com/badlogic/gdx/scenes/scene2d/Layout.java | 10 ++ .../badlogic/gdx/scenes/scene2d/actors/Label.java | 101 ++++++++++++++++++++- 4 files changed, 108 insertions(+), 6 deletions(-) create mode 100644 gdx/src/com/badlogic/gdx/scenes/scene2d/Layout.java diff --git a/backends/gdx-openal/src/com/badlogic/gdx/backends/openal/OpenALAudio.java b/backends/gdx-openal/src/com/badlogic/gdx/backends/openal/OpenALAudio.java index c6cfd90e6..36f42df94 100644 --- a/backends/gdx-openal/src/com/badlogic/gdx/backends/openal/OpenALAudio.java +++ b/backends/gdx-openal/src/com/badlogic/gdx/backends/openal/OpenALAudio.java @@ -164,6 +164,7 @@ public class OpenALAudio implements Audio { } public AudioDevice newAudioDevice (boolean isMono) { + // BOZO - Write OpenAL device. return new JavaSoundAudioDevice(isMono); } diff --git a/gdx/src/com/badlogic/gdx/math/Matrix4.java b/gdx/src/com/badlogic/gdx/math/Matrix4.java index 1c229d3a4..7a6eb5342 100644 --- a/gdx/src/com/badlogic/gdx/math/Matrix4.java +++ b/gdx/src/com/badlogic/gdx/math/Matrix4.java @@ -597,12 +597,12 @@ public class Matrix4 implements Serializable { * @param angle The angle in degrees * @return This matrix for chaining */ - static final Vector3 tmpV = new Vector3(); public Matrix4 setToRotation (float axisX, float axisY, float axisZ, float angle) { idt(); if (angle == 0) return this; return this.set(quat.set(tmpV.set(axisX, axisY, axisZ), angle)); } + static final Vector3 tmpV = new Vector3(); /** * Sets this matrix to a rotation matrix from the given euler angles. diff --git a/gdx/src/com/badlogic/gdx/scenes/scene2d/Layout.java b/gdx/src/com/badlogic/gdx/scenes/scene2d/Layout.java new file mode 100644 index 000000000..bf303bba3 --- /dev/null +++ b/gdx/src/com/badlogic/gdx/scenes/scene2d/Layout.java @@ -0,0 +1,10 @@ + +package com.badlogic.gdx.scenes.scene2d; + +public interface Layout { + public void layout (); + + public float getPrefWidth (); + + public float getPrefHeight (); +} diff --git a/gdx/src/com/badlogic/gdx/scenes/scene2d/actors/Label.java b/gdx/src/com/badlogic/gdx/scenes/scene2d/actors/Label.java index c451095df..a44625141 100644 --- a/gdx/src/com/badlogic/gdx/scenes/scene2d/actors/Label.java +++ b/gdx/src/com/badlogic/gdx/scenes/scene2d/actors/Label.java @@ -14,30 +14,86 @@ package com.badlogic.gdx.scenes.scene2d.actors; import com.badlogic.gdx.graphics.g2d.BitmapFont; +import com.badlogic.gdx.graphics.g2d.BitmapFont.HAlignment; import com.badlogic.gdx.graphics.g2d.BitmapFont.TextBounds; import com.badlogic.gdx.graphics.g2d.BitmapFontCache; import com.badlogic.gdx.graphics.g2d.SpriteBatch; import com.badlogic.gdx.scenes.scene2d.Actor; +import com.badlogic.gdx.scenes.scene2d.Layout; -public class Label extends Actor { +public class Label extends Actor implements Layout { public BitmapFontCache cache; + public VAlignment valignment = VAlignment.BOTTOM; + public String text; + public TextBounds bounds; - public Label (String name, BitmapFont font, String text) { + private WrapType wrapType; + private HAlignment halignment; + private float lastWidth = -1; + + public Label (String name, BitmapFont font) { super(name); cache = new BitmapFontCache(font); + } + + public Label (String name, BitmapFont font, String text) { + this(name, font); setText(text); } public void setText (String text) { - cache.setText(text, 0, cache.getFont().isFlipped() ? 0 : cache.getFont().getCapHeight()); - TextBounds bounds = cache.getBounds(); + this.text = text; + wrapType = WrapType.singleLine; + bounds = cache.setText(text, 0, cache.getFont().isFlipped() ? 0 : cache.getFont().getCapHeight()); + width = bounds.width; + height = bounds.height; + } + + public void setMultiLineText (String text) { + this.text = text; + wrapType = WrapType.multiLine; + bounds = cache.getFont().getMultiLineBounds(text); + cache.setMultiLineText(text, 0, cache.getFont().isFlipped() ? 0 : bounds.height); width = bounds.width; height = bounds.height; } + public void setWrappedText (String text, HAlignment alignment) { + this.text = text; + this.halignment = alignment; + wrapType = WrapType.wrapped; + bounds = cache.getFont().getWrappedBounds(text, width); + cache.setWrappedText(text, 0, cache.getFont().isFlipped() ? 0 : bounds.height, width, alignment); + } + + public void setFont (BitmapFont font) { + cache = new BitmapFontCache(font); + switch (wrapType) { + case singleLine: + setText(text); + break; + case multiLine: + setMultiLineText(text); + break; + case wrapped: + setWrappedText(text, halignment); + break; + } + } + @Override protected void draw (SpriteBatch batch, float parentAlpha) { cache.setColor(color.r, color.g, color.b, color.a * parentAlpha); - cache.setPosition(x, y); + switch (valignment) { + case TOP: + cache.setPosition(x, y + height - bounds.height); + break; + case CENTER: + cache.setPosition(x, y + (height - bounds.height) / 2); + break; + case BOTTOM: + cache.setPosition(x, y); + break; + } cache.draw(batch); } @@ -59,4 +115,39 @@ public class Label extends Actor { @Override public Actor hit (float x, float y) { return x > 0 && y > 0 && x < width && y < height ? this : null; } + + public void layout () { + if (wrapType == WrapType.wrapped && lastWidth != width) setWrappedText(text, halignment); + lastWidth = width; + } + + public float getPrefWidth () { + switch (wrapType) { + case singleLine: + return cache.getFont().getBounds(text).width; + case multiLine: + return cache.getFont().getMultiLineBounds(text).width; + case wrapped: + } + return 0; + } + + public float getPrefHeight () { + switch (wrapType) { + case singleLine: + return cache.getFont().getBounds(text).height; + case multiLine: + return cache.getFont().getMultiLineBounds(text).width; + case wrapped: + } + return 0; + } + + static public enum VAlignment { + TOP, CENTER, BOTTOM + } + + static private enum WrapType { + singleLine, multiLine, wrapped + } } -- 2.11.0