import gdxstudio.Content;
import gdxstudio.GdxStudio;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.dnd.DnDConstants;
import java.awt.dnd.DropTarget;
import java.awt.dnd.DropTargetDropEvent;
import java.nio.ByteBuffer;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.GLContext;
import scene2d.EffectType;
import scene2d.ImageJson;
import scene2d.InterpolationType;
import web.laf.lite.layout.HorizontalFlowLayout;
import web.laf.lite.layout.VerticalFlowLayout;
import web.laf.lite.utils.UIUtils;
import web.laf.lite.widget.CenterPanel;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.backends.lwjgl.LwjglAWTCanvas;
import com.badlogic.gdx.backends.lwjgl.LwjglApplicationConfiguration;
import com.badlogic.gdx.backends.lwjgl.LwjglCanvas;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.graphics.Pixmap.Format;
import com.badlogic.gdx.graphics.PixmapIO;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.ui.Button;
import com.badlogic.gdx.scenes.scene2d.ui.CheckBox;
import com.badlogic.gdx.scenes.scene2d.ui.Dialog;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.Label.LabelStyle;
import com.badlogic.gdx.scenes.scene2d.ui.List;
import com.badlogic.gdx.scenes.scene2d.ui.SelectBox;
import com.badlogic.gdx.scenes.scene2d.ui.Slider;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.scenes.scene2d.ui.TextField;
import com.badlogic.gdx.scenes.scene2d.ui.Touchpad;
import com.badlogic.gdx.tools.hiero.HieroPanel;
import com.badlogic.gdx.tools.particleeditor.ParticlePanel;
import com.badlogic.gdx.tools.particleeditor.ParticleRenderer;
import com.badlogic.gdx.utils.Timer.Task;
final public class StudioPanel extends JPanel {
private static final long serialVersionUID = 1L;
public LwjglAWTCanvas canvas;
LwjglApplicationConfiguration config2 = new LwjglApplicationConfiguration();
super(new VerticalFlowLayout());
hbox = new JPanel(new HorizontalFlowLayout());
hbox.add(new HeaderLabel("Screen Size"));
hbox.add(new HeaderLabel("Target Size"));
UIUtils.setMargin(this, new Insets(15,15,15,15));
config2.fullscreen = false;
public void paint(Graphics g){
Style.drawScreen(g, getWidth(), getHeight());
+"keepAspectRatio: false,"
public void createHieroCanvas(){
HieroPanel panel = new HieroPanel();
canvas = new LwjglAWTCanvas(new HieroPanel.FontRenderer() , false);
HieroPanel.gamePanel.add(canvas.getCanvas());
JScrollPane scrollPane = new JScrollPane(panel);
scrollPane.setPreferredSize(new Dimension(860, 650));
UIUtils.setDrawBorder(scrollPane, false);
public void createParticleCanvas(){
ParticlePanel panel = new ParticlePanel();
canvas = new LwjglAWTCanvas(new ParticleRenderer(panel) , false);
panel.initializeComponents(canvas);
JScrollPane scrollPane = new JScrollPane(panel);
scrollPane.setPreferredSize(new Dimension(860, 650));
UIUtils.setDrawBorder(scrollPane, false);
public void createStudioCanvas(){
GdxStudio.log("Creating Studio Canvas");
Scene.configJson = Scene.jsonReader.parse(config);
Scene.scenesMap.put("gdxstudio.SceneEditor", "");
canvas = new LwjglAWTCanvas(Scene.app , false);
scrollPane = new JScrollPane(canvas.getCanvas());
scrollPane.setPreferredSize(new Dimension(800, 480));
UIUtils.setDrawBorder(scrollPane, false);
Style.setScreenSize(scrollPane.getPreferredSize().width, scrollPane.getPreferredSize().height);
centerPanel = new CenterPanel(scrollPane, true, false);
centerPanel.setBackground(Color.black);
UIUtils.setUndecorated(centerPanel, true);
UIUtils.setMargin(centerPanel, new Insets(0,0,0,0));
Style.setScreenPosition(scrollPane.getLocation().x, scrollPane.getLocation().y);
canvas.getCanvas().setDropTarget(new DropTarget() {
private static final long serialVersionUID = 1L;
public synchronized void drop(DropTargetDropEvent event) {
Transferable transferable = event.getTransferable();
if( transferable.isDataFlavorSupported(DataFlavor.stringFlavor )){
event.acceptDrop( DnDConstants.ACTION_MOVE );
String s = (String)transferable.getTransferData(DataFlavor.stringFlavor);
event.getDropTargetContext().dropComplete(true);
catch( Exception exception ){
System.err.println( "Exception" + exception.getMessage() );
public void setGdxScreenSize(int width, int height){
canvas.getCanvas().setSize(width, height);
scrollPane = new JScrollPane(canvas.getCanvas());
scrollPane.setPreferredSize(new Dimension(width, height));
UIUtils.setDrawBorder(scrollPane, false);
Style.setScreenSize(scrollPane.getPreferredSize().width, scrollPane.getPreferredSize().height);
scrollPane.setPreferredSize(new Dimension(width, height));
centerPanel = new CenterPanel(scrollPane, true, false);
centerPanel.setBackground(Color.black);
UIUtils.setUndecorated(centerPanel, true);
UIUtils.setMargin(centerPanel, new Insets(0,0,0,0));
Style.setScreenPosition(scrollPane.getLocation().x, scrollPane.getLocation().y);
GLContext.useContext(canvas);
} catch (LWJGLException e) {
public void revalidateScreenPosition(){
Style.setScreenPosition(canvas.getCanvas().getLocation().x, canvas.getCanvas().getLocation().y);
public void destroyCanvas(){
GdxStudio.log("Destroying Scene Canvas");
remove(canvas.getCanvas());
private int getNameCount(Actor actor){
for(Actor child: Scene.getCurrentScene().getChildren())
if(actor.getClass().equals(child.getClass())) count++;
public void setName(final Actor actor){
com.badlogic.gdx.utils.Timer.schedule(new Task(){
String name = actor.getClass().getSimpleName()+getNameCount(actor);
if(Scene.getCurrentScene().findActor(name) == null)
actor.setName(name+"_1");
actor.setX(Scene.mouse.x-actor.getWidth()/2);
actor.setY(Scene.mouse.y-actor.getHeight()/2);
Scene.getCurrentScene().addActor(actor);
Frame.actorPanel.addActor(actor.getName());
Effect.createEffect(actor, EffectType.ScaleInOut, 1.5f, 0.5f, InterpolationType.Linear);
public void createActor(String type){
if(Asset.fontMap.size != 0){
LabelStyle ls = new LabelStyle();
ls.font = Asset.fontMap.firstValue();
Label label = new Label("Text", ls);
if(Asset.texMap.size != 0){
setName(new ImageJson(Asset.texMap.firstKey()));
case "Texture":setName(new ImageJson(Content.assetPanel.list.getSelectedValue()));break;
case "Sprite":setName(new Sprite(1f, Asset.texMap.firstKey()));break;
//ParticleEffect pe = new ParticleEffect();
//pe.load(effectFile, imagesDir);
case "Button":setName(new Button(Asset.skin));break;
case "TextButton":setName(new TextButton("Text", Asset.skin));break;
case "TextField":setName(new TextField("", Asset.skin));break;
case "Table":setName(new Table(Asset.skin));break;
case "CheckBox":setName(new CheckBox("Check", Asset.skin));break;
case "SelectBox":setName(new SelectBox(new String[]{"First","Second","Third"}, Asset.skin));break;
case "List":setName(new List(new String[]{"First","Second","Third"}, Asset.skin));break;
case "Slider":setName(new Slider(0, 10, 1, false, Asset.skin));break;
case "Dialog":setName(new Dialog("Title", Asset.skin));break;
case "Touchpad":setName(new Touchpad(5, Asset.skin));break;
case "Map":setName(new Map(1, 24));break;
public static void updateAssets(){
StringBuilder sb = new StringBuilder();
for(String filename: new File(Asset.basePath+"font").list()){
if(filename.endsWith(".fnt")){
GdxStudio.log("Loading :"+filename);
Asset.assetMap.put("font", sb.toString());
sb = new StringBuilder();
for(String filename: new File(Asset.basePath+"atlas").list()){
if(filename.endsWith(".atlas")){
GdxStudio.log("Loading :"+filename);
Asset.assetMap.put("atlas", sb.toString());
sb = new StringBuilder();
for(String filename: new File(Asset.basePath+"sound").list()){
if(filename.endsWith(".mp3")){
GdxStudio.log("Loading :"+filename);
Asset.assetMap.put("sound", sb.toString());
sb = new StringBuilder();
for(String filename: new File(Asset.basePath+"music").list()){
if(filename.endsWith(".mp3")){
GdxStudio.log("Loading :"+filename);
Asset.assetMap.put("music", sb.toString());
sb = new StringBuilder();
for(String filename: new File(Asset.basePath+"model").list()){
if(filename.endsWith(".obj") || filename.endsWith(".g3db")){
GdxStudio.log("Loading :"+filename);
Asset.modelMap.add(filename);
private static int counter = 1;
public static void saveScreenshot(){
fh = new FileHandle("shots/shot" + counter++ + ".png");
Pixmap pixmap = getScreenshot(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), false);
PixmapIO.writePNG(fh, pixmap);
private static Pixmap getScreenshot(int x, int y, int w, int h, boolean flipY){
Gdx.gl.glPixelStorei(GL20.GL_PACK_ALIGNMENT, 1);
final Pixmap pixmap = new Pixmap(w, h, Format.RGBA8888);
ByteBuffer pixels = pixmap.getPixels();
Gdx.gl.glReadPixels(x, y, w, h, GL20.GL_RGBA, GL20.GL_UNSIGNED_BYTE, pixels);
final int numBytes = w * h * 4;
byte[] lines = new byte[numBytes];
final int numBytesPerLine = w * 4;
for (int i = 0; i < h; i++){
pixels.position((h - i - 1) * numBytesPerLine);
pixels.get(lines, i * numBytesPerLine, numBytesPerLine);