org.shadebob.skineditor.dialog.FontPickerDialog.java Source code

Java tutorial

Introduction

Here is the source code for org.shadebob.skineditor.dialog.FontPickerDialog.java

Source

/*******************************************************************************
 * Copyright 2011 See AUTHORS file.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/
package org.shadebob.skineditor.dialog;

import java.util.Iterator;

import org.shadebob.skineditor.SkinEditorGame;
import org.shadebob.skineditor.utils.scenes.scene2d.ui.CustomSkin;

import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.ui.Dialog;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.ScrollPane;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener;
import com.badlogic.gdx.utils.ObjectMap;
import com.badlogic.gdx.utils.reflect.Field;

/**
 * Display a dialog that let you pick up a font. I re-use some of the Hiero code here to generate bitmap fonts from TTF files.
 * 
 * @author Yanick Bourbeau
 * 
 */
public class FontPickerDialog extends Dialog {

    SkinEditorGame game;
    Table tableFonts;
    ObjectMap<String, BitmapFont> fonts;
    Field field;

    public FontPickerDialog(final SkinEditorGame game, Field field) {
        super("Bitmap Font Picker", game.skin);
        this.game = game;
        this.field = field;

        tableFonts = new Table(game.skin);
        tableFonts.left().top().pad(5);
        tableFonts.defaults().pad(5);

        updateTable();

        ScrollPane scrollPane = new ScrollPane(tableFonts, game.skin);
        scrollPane.setFlickScroll(false);
        scrollPane.setFadeScrollBars(false);
        scrollPane.setScrollbarsOnTop(true);
        getContentTable().add(scrollPane).width(720).height(420).pad(20);

        button("Cancel", false).key(Keys.ESCAPE, false).getButtonTable().padBottom(15);
    }

    void updateTable() {
        fonts = game.skinProject.getAll(BitmapFont.class);

        tableFonts.clear();
        tableFonts.add("Font Name", "title").left().width(220);
        tableFonts.add("Value", "title").colspan(3).left().width(60).expandX().fillX();
        tableFonts.row();

        Iterator<String> it = fonts.keys().iterator();
        while (it.hasNext()) {
            final String key = it.next();
            final BitmapFont font = fonts.get(key);
            tableFonts.add(key).left();

            Label.LabelStyle labelStyle = new Label.LabelStyle();
            labelStyle.font = font;
            labelStyle.fontColor = Color.WHITE;
            tableFonts.add(new Label("Sample Text Content", labelStyle)).left().padRight(20);

            if (field != null) {
                tableFonts.add(new TextButton("Select", game.skin)).left().getActor()
                        .addListener(new ChangeListener() {
                            @Override
                            public void changed(ChangeEvent event, Actor actor) {
                                try {
                                    field.set(game.screenMain.paneOptions.currentStyleObj, font);
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                                game.screenMain.panePreview.refresh();
                                game.screenMain.paneOptions.updateSelectedTableFields();
                                game.screenMain.changeSkin();
                                hide();
                            }
                        });
            }
            tableFonts.add(new TextButton("Remove", game.skin)).left().expandX().getActor()
                    .addListener(new ChangeListener() {
                        @Override
                        public void changed(ChangeEvent event, Actor actor) {
                            Dialog dlg = new Dialog("Delete Font", game.skin) {
                                @Override
                                protected void result(Object object) {
                                    if (!(Boolean) object) {
                                        return;
                                    }
                                    if (CustomSkin.isResInUse(game.skinProject, font)) {
                                        game.showNotice("Error", "Bitmap font already in use!", getStage());
                                    } else {
                                        // Remove files from disk (fnt and png)
                                        FileHandle targetFont = new FileHandle("projects/"
                                                + game.screenMain.getcurrentProject() + "/" + key + ".fnt");
                                        FileHandle targetImage = new FileHandle("projects/"
                                                + game.screenMain.getcurrentProject() + "/assets/" + key + ".png");
                                        targetFont.delete();
                                        targetImage.delete();

                                        fonts.remove(key);
                                        // update table
                                        updateTable();
                                        game.screenMain.changeSkin();
                                    }
                                }
                            };
                            dlg.pad(20);
                            dlg.getContentTable().add("You are sure you want to delete this bitmap font?");
                            dlg.button("OK", true).key(Keys.ENTER, true);
                            dlg.button("Cancel", false).key(Keys.ESCAPE, false);
                            dlg.show(getStage());
                        }
                    });
            tableFonts.row();
        }
    }

}