void GridMapEditor::_item_selected_cbk() {

	TreeItem *it = theme_pallete->get_selected();
	if (it) {

		selected_pallete=it->get_metadata(theme_pallete->get_selected_column());

	} else {

		selected_pallete=-1;

	}

	_update_cursor_instance();

}
bool GridMapEditor::do_input_action(Camera* p_camera,const Point2& p_point,bool p_click) {

	if (!spatial_editor)
		return false;


	if (selected_pallete<0 && input_action!=INPUT_COPY && input_action!=INPUT_SELECT && input_action!=INPUT_DUPLICATE)
		return false;
	Ref<MeshLibrary> theme = node->get_theme();
	if (theme.is_null())
		return false;
	if (input_action!=INPUT_COPY && input_action!=INPUT_SELECT && input_action!=INPUT_DUPLICATE && !theme->has_item(selected_pallete))
		return false;

	Camera *camera = p_camera;
	Vector3 from = camera->project_ray_origin(p_point);
	Vector3 normal = camera->project_ray_normal(p_point);
	Transform local_xform = node->get_global_transform().affine_inverse();
	Vector<Plane> planes=camera->get_frustum();
	from=local_xform.xform(from);
	normal=local_xform.basis.xform(normal).normalized();


	Plane p;
	p.normal[edit_axis]=1.0;
	p.d=edit_floor[edit_axis]*node->get_cell_size();

	Vector3 inters;
	if (!p.intersects_segment(from, from + normal * settings_pick_distance->get_value(), &inters))
		return false;


	//make sure the intersection is inside the frustum planes, to avoid
	//painting on invisible regions
	for(int i=0;i<planes.size();i++) {

		Plane fp = local_xform.xform(planes[i]);
		if (fp.is_point_over(inters))
			return false;
	}


	int cell[3];
	float cell_size[3]={node->get_cell_size(),node->get_cell_size(),node->get_cell_size()};

	last_mouseover=Vector3(-1,-1,-1);

	for(int i=0;i<3;i++) {

		if (i==edit_axis)
			cell[i]=edit_floor[i];
		else {

			cell[i]=inters[i]/node->get_cell_size();
			if (inters[i]<0)
				cell[i]-=1; //compensate negative
			grid_ofs[i]=cell[i]*cell_size[i];
		}

		/*if (cell[i]<0 || cell[i]>=grid_size[i]) {

			cursor_visible=false;
			_update_cursor_transform();
			return false;
		}*/
	}

	last_mouseover=Vector3(cell[0],cell[1],cell[2]);
	VS::get_singleton()->instance_set_transform(grid_instance[edit_axis],Transform(Basis(),grid_ofs));


	if (cursor_instance.is_valid()) {

		cursor_origin=(Vector3(cell[0],cell[1],cell[2])+Vector3(0.5*node->get_center_x(),0.5*node->get_center_y(),0.5*node->get_center_z()))*node->get_cell_size();
		cursor_visible=true;

		_update_cursor_transform();

	}

	if (input_action==INPUT_DUPLICATE) {

		selection.current=Vector3(cell[0],cell[1],cell[2]);
		_update_duplicate_indicator();

	} else if (input_action==INPUT_SELECT) {

		selection.current=Vector3(cell[0],cell[1],cell[2]);
		if (p_click)
			selection.click=selection.current;
		selection.active=true;
		_validate_selection();

		return true;
	} else if (input_action==INPUT_COPY) {

		int item=node->get_cell_item(cell[0],cell[1],cell[2]);
		if (item>=0) {
			selected_pallete=item;
			theme_pallete->set_current(item);
			update_pallete();
			_update_cursor_instance();
		}
		return true;
	} if (input_action==INPUT_PAINT) {
		SetItem si;
		si.pos=Vector3(cell[0],cell[1],cell[2]);
		si.new_value=selected_pallete;
		si.new_orientation=cursor_rot;
		si.old_value=node->get_cell_item(cell[0],cell[1],cell[2]);
		si.old_orientation=node->get_cell_item_orientation(cell[0],cell[1],cell[2]);
		set_items.push_back(si);
		node->set_cell_item(cell[0],cell[1],cell[2],selected_pallete,cursor_rot);
		return true;
	} else if (input_action==INPUT_ERASE) {
		SetItem si;
		si.pos=Vector3(cell[0],cell[1],cell[2]);
		si.new_value=-1;
		si.new_orientation=0;
		si.old_value=node->get_cell_item(cell[0],cell[1],cell[2]);
		si.old_orientation=node->get_cell_item_orientation(cell[0],cell[1],cell[2]);
		set_items.push_back(si);
		node->set_cell_item(cell[0],cell[1],cell[2],-1);
		return true;
	}


	return false;

}
void GridMapEditor::_item_selected_cbk(int idx) {
	selected_pallete=theme_pallete->get_item_metadata(idx);

	_update_cursor_instance();

}