use of com.badlogic.gdx.scenes.scene2d.utils.Drawable in project libgdx by libgdx.
the class Tree method draw.
/** Draws selection, icons, and expand icons. */
private void draw(Batch batch, Array<Node> nodes, float indent) {
Drawable plus = style.plus, minus = style.minus;
float x = getX(), y = getY();
for (int i = 0, n = nodes.size; i < n; i++) {
Node node = nodes.get(i);
Actor actor = node.actor;
if (selection.contains(node) && style.selection != null) {
style.selection.draw(batch, x, y + actor.getY() - ySpacing / 2, getWidth(), node.height + ySpacing);
} else if (node == overNode && style.over != null) {
style.over.draw(batch, x, y + actor.getY() - ySpacing / 2, getWidth(), node.height + ySpacing);
}
if (node.icon != null) {
float iconY = actor.getY() + Math.round((node.height - node.icon.getMinHeight()) / 2);
batch.setColor(actor.getColor());
node.icon.draw(batch, x + node.actor.getX() - iconSpacingRight - node.icon.getMinWidth(), y + iconY, node.icon.getMinWidth(), node.icon.getMinHeight());
batch.setColor(Color.WHITE);
}
if (node.children.size == 0)
continue;
Drawable expandIcon = node.expanded ? minus : plus;
float iconY = actor.getY() + Math.round((node.height - expandIcon.getMinHeight()) / 2);
expandIcon.draw(batch, x + indent - iconSpacingLeft, y + iconY, expandIcon.getMinWidth(), expandIcon.getMinHeight());
if (node.expanded)
draw(batch, node.children, indent + indentSpacing);
}
}
use of com.badlogic.gdx.scenes.scene2d.utils.Drawable in project libgdx by libgdx.
the class SelectBox method layout.
@Override
public void layout() {
Drawable bg = style.background;
BitmapFont font = style.font;
if (bg != null) {
prefHeight = Math.max(bg.getTopHeight() + bg.getBottomHeight() + font.getCapHeight() - font.getDescent() * 2, bg.getMinHeight());
} else
prefHeight = font.getCapHeight() - font.getDescent() * 2;
float maxItemWidth = 0;
Pool<GlyphLayout> layoutPool = Pools.get(GlyphLayout.class);
GlyphLayout layout = layoutPool.obtain();
for (int i = 0; i < items.size; i++) {
layout.setText(font, toString(items.get(i)));
maxItemWidth = Math.max(layout.width, maxItemWidth);
}
layoutPool.free(layout);
prefWidth = maxItemWidth;
if (bg != null)
prefWidth += bg.getLeftWidth() + bg.getRightWidth();
ListStyle listStyle = style.listStyle;
ScrollPaneStyle scrollStyle = style.scrollStyle;
float listWidth = maxItemWidth + listStyle.selection.getLeftWidth() + listStyle.selection.getRightWidth();
if (scrollStyle.background != null)
listWidth += scrollStyle.background.getLeftWidth() + scrollStyle.background.getRightWidth();
if (selectBoxList == null || !selectBoxList.disableY)
listWidth += Math.max(style.scrollStyle.vScroll != null ? style.scrollStyle.vScroll.getMinWidth() : 0, style.scrollStyle.vScrollKnob != null ? style.scrollStyle.vScrollKnob.getMinWidth() : 0);
prefWidth = Math.max(prefWidth, listWidth);
}
use of com.badlogic.gdx.scenes.scene2d.utils.Drawable in project libgdx by libgdx.
the class Skin method getJsonLoader.
protected Json getJsonLoader(final FileHandle skinFile) {
final Skin skin = this;
final Json json = new Json() {
public <T> T readValue(Class<T> type, Class elementType, JsonValue jsonData) {
// If the JSON is a string but the type is not, look up the actual value by name.
if (jsonData.isString() && !ClassReflection.isAssignableFrom(CharSequence.class, type))
return get(jsonData.asString(), type);
return super.readValue(type, elementType, jsonData);
}
};
json.setTypeName(null);
json.setUsePrototypes(false);
json.setSerializer(Skin.class, new ReadOnlySerializer<Skin>() {
public Skin read(Json json, JsonValue typeToValueMap, Class ignored) {
for (JsonValue valueMap = typeToValueMap.child; valueMap != null; valueMap = valueMap.next) {
try {
readNamedObjects(json, ClassReflection.forName(valueMap.name()), valueMap);
} catch (ReflectionException ex) {
throw new SerializationException(ex);
}
}
return skin;
}
private void readNamedObjects(Json json, Class type, JsonValue valueMap) {
Class addType = type == TintedDrawable.class ? Drawable.class : type;
for (JsonValue valueEntry = valueMap.child; valueEntry != null; valueEntry = valueEntry.next) {
Object object = json.readValue(type, valueEntry);
if (object == null)
continue;
try {
add(valueEntry.name, object, addType);
if (addType != Drawable.class && ClassReflection.isAssignableFrom(Drawable.class, addType))
add(valueEntry.name, object, Drawable.class);
} catch (Exception ex) {
throw new SerializationException("Error reading " + ClassReflection.getSimpleName(type) + ": " + valueEntry.name, ex);
}
}
}
});
json.setSerializer(BitmapFont.class, new ReadOnlySerializer<BitmapFont>() {
public BitmapFont read(Json json, JsonValue jsonData, Class type) {
String path = json.readValue("file", String.class, jsonData);
int scaledSize = json.readValue("scaledSize", int.class, -1, jsonData);
Boolean flip = json.readValue("flip", Boolean.class, false, jsonData);
Boolean markupEnabled = json.readValue("markupEnabled", Boolean.class, false, jsonData);
FileHandle fontFile = skinFile.parent().child(path);
if (!fontFile.exists())
fontFile = Gdx.files.internal(path);
if (!fontFile.exists())
throw new SerializationException("Font file not found: " + fontFile);
// Use a region with the same name as the font, else use a PNG file in the same directory as the FNT file.
String regionName = fontFile.nameWithoutExtension();
try {
BitmapFont font;
Array<TextureRegion> regions = skin.getRegions(regionName);
if (regions != null)
font = new BitmapFont(new BitmapFontData(fontFile, flip), regions, true);
else {
TextureRegion region = skin.optional(regionName, TextureRegion.class);
if (region != null)
font = new BitmapFont(fontFile, region, flip);
else {
FileHandle imageFile = fontFile.parent().child(regionName + ".png");
if (imageFile.exists())
font = new BitmapFont(fontFile, imageFile, flip);
else
font = new BitmapFont(fontFile, flip);
}
}
font.getData().markupEnabled = markupEnabled;
// Scaled size is the desired cap height to scale the font to.
if (scaledSize != -1)
font.getData().setScale(scaledSize / font.getCapHeight());
return font;
} catch (RuntimeException ex) {
throw new SerializationException("Error loading bitmap font: " + fontFile, ex);
}
}
});
json.setSerializer(Color.class, new ReadOnlySerializer<Color>() {
public Color read(Json json, JsonValue jsonData, Class type) {
if (jsonData.isString())
return get(jsonData.asString(), Color.class);
String hex = json.readValue("hex", String.class, (String) null, jsonData);
if (hex != null)
return Color.valueOf(hex);
float r = json.readValue("r", float.class, 0f, jsonData);
float g = json.readValue("g", float.class, 0f, jsonData);
float b = json.readValue("b", float.class, 0f, jsonData);
float a = json.readValue("a", float.class, 1f, jsonData);
return new Color(r, g, b, a);
}
});
json.setSerializer(TintedDrawable.class, new ReadOnlySerializer() {
public Object read(Json json, JsonValue jsonData, Class type) {
String name = json.readValue("name", String.class, jsonData);
Color color = json.readValue("color", Color.class, jsonData);
Drawable drawable = newDrawable(name, color);
if (drawable instanceof BaseDrawable) {
BaseDrawable named = (BaseDrawable) drawable;
named.setName(jsonData.name + " (" + name + ", " + color + ")");
}
return drawable;
}
});
return json;
}
use of com.badlogic.gdx.scenes.scene2d.utils.Drawable in project libgdx by libgdx.
the class Skin method getDrawable.
/** Returns a registered drawable. If no drawable is found but a region, ninepatch, or sprite exists with the name, then the
* appropriate drawable is created and stored in the skin. */
public Drawable getDrawable(String name) {
Drawable drawable = optional(name, Drawable.class);
if (drawable != null)
return drawable;
// Use texture or texture region. If it has splits, use ninepatch. If it has rotation or whitespace stripping, use sprite.
try {
TextureRegion textureRegion = getRegion(name);
if (textureRegion instanceof AtlasRegion) {
AtlasRegion region = (AtlasRegion) textureRegion;
if (region.splits != null)
drawable = new NinePatchDrawable(getPatch(name));
else if (region.rotate || region.packedWidth != region.originalWidth || region.packedHeight != region.originalHeight)
drawable = new SpriteDrawable(getSprite(name));
}
if (drawable == null)
drawable = new TextureRegionDrawable(textureRegion);
} catch (GdxRuntimeException ignored) {
}
// Check for explicit registration of ninepatch, sprite, or tiled drawable.
if (drawable == null) {
NinePatch patch = optional(name, NinePatch.class);
if (patch != null)
drawable = new NinePatchDrawable(patch);
else {
Sprite sprite = optional(name, Sprite.class);
if (sprite != null)
drawable = new SpriteDrawable(sprite);
else
throw new GdxRuntimeException("No Drawable, NinePatch, TextureRegion, Texture, or Sprite registered with name: " + name);
}
}
if (drawable instanceof BaseDrawable)
((BaseDrawable) drawable).setName(name);
add(name, drawable, Drawable.class);
return drawable;
}
use of com.badlogic.gdx.scenes.scene2d.utils.Drawable in project libgdx by libgdx.
the class SplitPane method calculateHorizBoundsAndPositions.
private void calculateHorizBoundsAndPositions() {
Drawable handle = style.handle;
float height = getHeight();
float availWidth = getWidth() - handle.getMinWidth();
float leftAreaWidth = (int) (availWidth * splitAmount);
float rightAreaWidth = availWidth - leftAreaWidth;
float handleWidth = handle.getMinWidth();
firstWidgetBounds.set(0, 0, leftAreaWidth, height);
secondWidgetBounds.set(leftAreaWidth + handleWidth, 0, rightAreaWidth, height);
handleBounds.set(leftAreaWidth, 0, handleWidth, height);
}
Aggregations