/*******************************************************************************
 * Copyright 2014 Rafael Garcia Moreno.
 * 
 * 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 com.bladecoder.engineeditor.ui.panels;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Map;

import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.bladecoder.engine.actions.Param;
import com.bladecoder.engine.model.BaseActor;
import com.bladecoder.engine.model.CharacterActor;
import com.bladecoder.engine.model.InteractiveActor;
import com.bladecoder.engine.model.Scene;
import com.bladecoder.engine.model.SpriteActor;
import com.bladecoder.engine.model.WalkZoneActor;
import com.bladecoder.engineeditor.Ctx;

public class ActorInputPanel extends EditableOptionsInputPanel<String> {

	ActorInputPanel(Skin skin, String title, String desc, boolean mandatory, String defaultValue, Param.Type type) {
		super(skin, title, desc, mandatory, defaultValue, getValues(mandatory, type));

		if (mandatory)
			if (type == Param.Type.ACTOR)
				setText(Ctx.project.getSelectedActor().getId());
			else
				input.setSelectedIndex(0);
	}

	private static String[] getValues(boolean mandatory, Param.Type type) {
		Map<String, BaseActor> actors = Ctx.project.getSelectedScene().getActors();

		ArrayList<BaseActor> filteredActors = new ArrayList<BaseActor>();

		for (BaseActor a : actors.values()) {
			if (type == Param.Type.CHARACTER_ACTOR) {
				if (a instanceof CharacterActor)
					filteredActors.add(a);
			} else if (type == Param.Type.INTERACTIVE_ACTOR) {
				if (a instanceof InteractiveActor)
					filteredActors.add(a);
			} else if (type == Param.Type.WALKZONE_ACTOR) {
				if (a instanceof WalkZoneActor)
					filteredActors.add(a);
			} else if (type == Param.Type.SPRITE_ACTOR) {
				if (a instanceof SpriteActor)
					filteredActors.add(a);
			} else {
				filteredActors.add(a);
			}
		}

		String[] result = null;

		if (type != Param.Type.WALKZONE_ACTOR) {
			// Add player variable to the list
			result = new String[filteredActors.size() + 1];

			result[0] = Scene.VAR_PLAYER;

			for (int i = 0; i < filteredActors.size(); i++) {
				result[i + 1] = filteredActors.get(i).getId();
			}
		} else {
			result = new String[filteredActors.size()];
			
			for (int i = 0; i < filteredActors.size(); i++) {
				result[i] = filteredActors.get(i).getId();
			}

		}

		Arrays.sort(result);
		return result;
	}
}

@@ -1794,156 +1794,198 @@
 ) %7B%0A
+%0A
 %09%09
-Map%3CString, BaseActor%3E actors = Ctx.project.getSelectedScene().getActors();%0A%0A%09%09ArrayList%3CBaseActor%3E filteredActors = new ArrayList%3CBase
+ArrayList%3CBaseActor%3E filteredActors = new ArrayList%3C%3E();%0A%0A%09%09if (Ctx.project.getSelectedScene() != null) %7B%0A%09%09%09Map%3CString, BaseActor%3E actors = Ctx.project.getSelectedScene().get
 Actor
-%3E
+s
 ();%0A%0A
+%09
 %09%09fo
@@ -2020,16 +2020,17 @@
 es()) %7B%0A
+%09
 %09%09%09if (t
@@ -2062,32 +2062,33 @@
 ER_ACTOR) %7B%0A%09%09%09%09
+%09
 if (a instanceof
@@ -2101,32 +2101,33 @@
 cterActor)%0A%09%09%09%09%09
+%09
 filteredActors.a
@@ -2125,32 +2125,33 @@
 dActors.add(a);%0A
+%09
 %09%09%09%7D else if (ty
@@ -2184,32 +2184,33 @@
 VE_ACTOR) %7B%0A%09%09%09%09
+%09
 if (a instanceof
@@ -2225,32 +2225,33 @@
 tiveActor)%0A%09%09%09%09%09
+%09
 filteredActors.a
@@ -2249,32 +2249,33 @@
 dActors.add(a);%0A
+%09
 %09%09%09%7D else if (ty
@@ -2305,32 +2305,33 @@
 NE_ACTOR) %7B%0A%09%09%09%09
+%09
 if (a instanceof
@@ -2338,32 +2338,33 @@
  WalkZoneActor)%0A
+%09
 %09%09%09%09%09filteredAct
@@ -2374,24 +2374,25 @@
 .add(a);%0A%09%09%09
+%09
 %7D else if (t
@@ -2425,24 +2425,25 @@
 CTOR) %7B%0A%09%09%09%09
+%09
 if (a instan
@@ -2456,24 +2456,25 @@
 priteActor)%0A
+%09
 %09%09%09%09%09filtere
@@ -2492,16 +2492,17 @@
 (a);%0A%09%09%09
+%09
 %7D else %7B
@@ -2502,16 +2502,17 @@
  else %7B%0A
+%09
 %09%09%09%09filt
@@ -2530,16 +2530,22 @@
 add(a);%0A
+%09%09%09%09%7D%0A
 %09%09%09%7D%0A%09%09%7D
@@ -2907,19 +2907,16 @@
 ize()%5D;%0A
-%09%09%09
 %0A%09%09%09for 

Fix nullpointer when creating scene.