LLFloaterStats::LLFloaterStats(const LLSD& val)
	:   LLFloater("floater_stats"),
		mStatsContainer(NULL),
		mScrollContainer(NULL)

{
	LLUICtrlFactory::getInstance()->buildFloater(this, "floater_statistics.xml", NULL, FALSE);
	
	LLRect stats_rect(0, getRect().getHeight() - LLFLOATER_HEADER_SIZE,
					  getRect().getWidth() - LLFLOATER_CLOSE_BOX_SIZE, 0);
	mStatsContainer = new LLContainerView("statistics_view", stats_rect);
	mStatsContainer->showLabel(FALSE);

	LLRect scroll_rect(LL_SCROLL_BORDER, getRect().getHeight() - LLFLOATER_HEADER_SIZE - LL_SCROLL_BORDER,
					   getRect().getWidth() - LL_SCROLL_BORDER, LL_SCROLL_BORDER);
		mScrollContainer = new LLScrollableContainerView(std::string("statistics_scroll"), scroll_rect, mStatsContainer);
	mScrollContainer->setFollowsAll();
	mScrollContainer->setReserveScrollCorner(TRUE);

	mStatsContainer->setScrollContainer(mScrollContainer);
	
	addChild(mScrollContainer);

	buildStats();
}
void SpriteRegionEditor::_region_draw()
{
	Ref<Texture> base_tex = node->get_texture();
	if (base_tex.is_null())
		return;

	Matrix32 mtx;
	mtx.elements[2]=-draw_ofs;
	mtx.scale_basis(Vector2(draw_zoom,draw_zoom));

	VS::get_singleton()->canvas_item_set_clip(edit_draw->get_canvas_item(),true);
	VS::get_singleton()->canvas_item_add_set_transform(edit_draw->get_canvas_item(),mtx);
	edit_draw->draw_texture(base_tex,Point2());
	VS::get_singleton()->canvas_item_add_set_transform(edit_draw->get_canvas_item(),Matrix32());

	if (snap_show_grid) {
		Size2 s = edit_draw->get_size();
		int last_cell;

		if (snap_step.x!=0) {
			for(int i=0;i<s.width;i++) {
				int cell = Math::fast_ftoi(Math::floor((mtx.affine_inverse().xform(Vector2(i,0)).x-snap_offset.x)/snap_step.x));
				if (i==0)
					last_cell=cell;
				if (last_cell!=cell)
					edit_draw->draw_line(Point2(i,0),Point2(i,s.height),Color(0.3,0.7,1,0.3));
				last_cell=cell;
			}
		}

		if (snap_step.y!=0) {
			for(int i=0;i<s.height;i++) {
				int cell = Math::fast_ftoi(Math::floor((mtx.affine_inverse().xform(Vector2(0,i)).y-snap_offset.y)/snap_step.y));
				if (i==0)
					last_cell=cell;
				if (last_cell!=cell)
					edit_draw->draw_line(Point2(0,i),Point2(s.width,i),Color(0.3,0.7,1,0.3));
				last_cell=cell;
			}
		}
	}

	Ref<Texture> select_handle = get_icon("EditorHandle","EditorIcons");

	Rect2 scroll_rect(Point2(),mtx.basis_xform(base_tex->get_size()));
	scroll_rect.expand_to(mtx.basis_xform(edit_draw->get_size()));

	Vector2 endpoints[4]={
		mtx.basis_xform(rect.pos),
		mtx.basis_xform(rect.pos+Vector2(rect.size.x,0)),
		mtx.basis_xform(rect.pos+rect.size),
		mtx.basis_xform(rect.pos+Vector2(0,rect.size.y))
	};

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

		int prev = (i+3)%4;
		int next = (i+1)%4;

		Vector2 ofs = ((endpoints[i] - endpoints[prev]).normalized() + ((endpoints[i] - endpoints[next]).normalized())).normalized();
		ofs*=1.4144*(select_handle->get_size().width/2);

		edit_draw->draw_line(endpoints[i]-draw_ofs, endpoints[next]-draw_ofs, Color(0.9,0.5,0.5), 2);

		edit_draw->draw_texture(select_handle,(endpoints[i]+ofs-(select_handle->get_size()/2)).floor()-draw_ofs);

		ofs = (endpoints[next]-endpoints[i])/2;
		ofs += (endpoints[next]-endpoints[i]).tangent().normalized()*(select_handle->get_size().width/2);

		edit_draw->draw_texture(select_handle,(endpoints[i]+ofs-(select_handle->get_size()/2)).floor()-draw_ofs);

		scroll_rect.expand_to(endpoints[i]);
	}

	scroll_rect=scroll_rect.grow(200);
	updating_scroll=true;
	hscroll->set_min(scroll_rect.pos.x);
	hscroll->set_max(scroll_rect.pos.x+scroll_rect.size.x);
	hscroll->set_page(edit_draw->get_size().x);
	hscroll->set_val(draw_ofs.x);
	hscroll->set_step(0.001);

	vscroll->set_min(scroll_rect.pos.y);
	vscroll->set_max(scroll_rect.pos.y+scroll_rect.size.y);
	vscroll->set_page(edit_draw->get_size().y);
	vscroll->set_val(draw_ofs.y);
	vscroll->set_step(0.001);
	updating_scroll=false;
}
void TextureRegionEditor::_region_draw() {
	Ref<Texture> base_tex = NULL;
	if (node_sprite)
		base_tex = node_sprite->get_texture();
	else if (node_patch9)
		base_tex = node_patch9->get_texture();
	else if (obj_styleBox.is_valid())
		base_tex = obj_styleBox->get_texture();
	else if (atlas_tex.is_valid())
		base_tex = atlas_tex->get_atlas();
	if (base_tex.is_null())
		return;

	Transform2D mtx;
	mtx.elements[2] = -draw_ofs;
	mtx.scale_basis(Vector2(draw_zoom, draw_zoom));

	VS::get_singleton()->canvas_item_add_set_transform(edit_draw->get_canvas_item(), mtx);
	edit_draw->draw_texture(base_tex, Point2());
	VS::get_singleton()->canvas_item_add_set_transform(edit_draw->get_canvas_item(), Transform2D());

	if (snap_mode == SNAP_GRID) {
		Size2 s = edit_draw->get_size();
		int last_cell;

		if (snap_step.x != 0) {
			if (snap_separation.x == 0)
				for (int i = 0; i < s.width; i++) {
					int cell = Math::fast_ftoi(Math::floor((mtx.affine_inverse().xform(Vector2(i, 0)).x - snap_offset.x) / snap_step.x));
					if (i == 0)
						last_cell = cell;
					if (last_cell != cell)
						edit_draw->draw_line(Point2(i, 0), Point2(i, s.height), Color(0.3, 0.7, 1, 0.3));
					last_cell = cell;
				}
			else
				for (int i = 0; i < s.width; i++) {
					int cell = Math::fast_ftoi(Math::floor((mtx.affine_inverse().xform(Vector2(i, 0)).x - snap_offset.x) / (snap_step.x + snap_separation.x)));
					if (i == 0)
						last_cell = cell;
					if (last_cell != cell)
						edit_draw->draw_rect(Rect2(i - snap_separation.x * draw_zoom, 0, snap_separation.x * draw_zoom, s.height), Color(0.3, 0.7, 1, 0.3));
					last_cell = cell;
				}
		}

		if (snap_step.y != 0) {
			if (snap_separation.y == 0)
				for (int i = 0; i < s.height; i++) {
					int cell = Math::fast_ftoi(Math::floor((mtx.affine_inverse().xform(Vector2(0, i)).y - snap_offset.y) / snap_step.y));
					if (i == 0)
						last_cell = cell;
					if (last_cell != cell)
						edit_draw->draw_line(Point2(0, i), Point2(s.width, i), Color(0.3, 0.7, 1, 0.3));
					last_cell = cell;
				}
			else
				for (int i = 0; i < s.height; i++) {
					int cell = Math::fast_ftoi(Math::floor((mtx.affine_inverse().xform(Vector2(0, i)).y - snap_offset.y) / (snap_step.y + snap_separation.y)));
					if (i == 0)
						last_cell = cell;
					if (last_cell != cell)
						edit_draw->draw_rect(Rect2(0, i - snap_separation.y * draw_zoom, s.width, snap_separation.y * draw_zoom), Color(0.3, 0.7, 1, 0.3));
					last_cell = cell;
				}
		}
	} else if (snap_mode == SNAP_AUTOSLICE) {
		for (List<Rect2>::Element *E = autoslice_cache.front(); E; E = E->next()) {
			Rect2 r = E->get();
			Vector2 endpoints[4] = {
				mtx.basis_xform(r.position),
				mtx.basis_xform(r.position + Vector2(r.size.x, 0)),
				mtx.basis_xform(r.position + r.size),
				mtx.basis_xform(r.position + Vector2(0, r.size.y))
			};
			for (int i = 0; i < 4; i++) {
				int next = (i + 1) % 4;
				edit_draw->draw_line(endpoints[i] - draw_ofs, endpoints[next] - draw_ofs, Color(0.3, 0.7, 1, 1), 2);
			}
		}
	}

	Ref<Texture> select_handle = get_icon("EditorHandle", "EditorIcons");

	Rect2 scroll_rect(Point2(), mtx.basis_xform(base_tex->get_size()));
	scroll_rect.expand_to(mtx.basis_xform(edit_draw->get_size()));

	Vector2 endpoints[4] = {
		mtx.basis_xform(rect.position),
		mtx.basis_xform(rect.position + Vector2(rect.size.x, 0)),
		mtx.basis_xform(rect.position + rect.size),
		mtx.basis_xform(rect.position + Vector2(0, rect.size.y))
	};
	Color color(0.9, 0.5, 0.5);
	for (int i = 0; i < 4; i++) {

		int prev = (i + 3) % 4;
		int next = (i + 1) % 4;

		Vector2 ofs = ((endpoints[i] - endpoints[prev]).normalized() + ((endpoints[i] - endpoints[next]).normalized())).normalized();
		ofs *= 1.4144 * (select_handle->get_size().width / 2);

		edit_draw->draw_line(endpoints[i] - draw_ofs, endpoints[next] - draw_ofs, color, 2);

		if (snap_mode != SNAP_AUTOSLICE)
			edit_draw->draw_texture(select_handle, (endpoints[i] + ofs - (select_handle->get_size() / 2)).floor() - draw_ofs);

		ofs = (endpoints[next] - endpoints[i]) / 2;
		ofs += (endpoints[next] - endpoints[i]).tangent().normalized() * (select_handle->get_size().width / 2);

		if (snap_mode != SNAP_AUTOSLICE)
			edit_draw->draw_texture(select_handle, (endpoints[i] + ofs - (select_handle->get_size() / 2)).floor() - draw_ofs);

		scroll_rect.expand_to(endpoints[i]);
	}

	scroll_rect = scroll_rect.grow(200);
	updating_scroll = true;
	hscroll->set_min(scroll_rect.position.x);
	hscroll->set_max(scroll_rect.position.x + scroll_rect.size.x);
	hscroll->set_page(edit_draw->get_size().x);
	hscroll->set_value(draw_ofs.x);
	hscroll->set_step(0.001);

	vscroll->set_min(scroll_rect.position.y);
	vscroll->set_max(scroll_rect.position.y + scroll_rect.size.y);
	vscroll->set_page(edit_draw->get_size().y);
	vscroll->set_value(draw_ofs.y);
	vscroll->set_step(0.001);
	updating_scroll = false;

	float margins[4];
	if (node_patch9 || obj_styleBox.is_valid()) {
		if (node_patch9) {
			margins[0] = node_patch9->get_patch_margin(MARGIN_TOP);
			margins[1] = node_patch9->get_patch_margin(MARGIN_BOTTOM);
			margins[2] = node_patch9->get_patch_margin(MARGIN_LEFT);
			margins[3] = node_patch9->get_patch_margin(MARGIN_RIGHT);
		} else if (obj_styleBox.is_valid()) {
			margins[0] = obj_styleBox->get_margin_size(MARGIN_TOP);
			margins[1] = obj_styleBox->get_margin_size(MARGIN_BOTTOM);
			margins[2] = obj_styleBox->get_margin_size(MARGIN_LEFT);
			margins[3] = obj_styleBox->get_margin_size(MARGIN_RIGHT);
		}
		Vector2 pos[4] = {
			mtx.basis_xform(Vector2(0, margins[0])) + Vector2(0, endpoints[0].y - draw_ofs.y),
			-mtx.basis_xform(Vector2(0, margins[1])) + Vector2(0, endpoints[2].y - draw_ofs.y),
			mtx.basis_xform(Vector2(margins[2], 0)) + Vector2(endpoints[0].x - draw_ofs.x, 0),
			-mtx.basis_xform(Vector2(margins[3], 0)) + Vector2(endpoints[2].x - draw_ofs.x, 0)
		};

		draw_margin_line(edit_draw, pos[0], pos[0] + Vector2(edit_draw->get_size().x, 0));
		draw_margin_line(edit_draw, pos[1], pos[1] + Vector2(edit_draw->get_size().x, 0));
		draw_margin_line(edit_draw, pos[2], pos[2] + Vector2(0, edit_draw->get_size().y));
		draw_margin_line(edit_draw, pos[3], pos[3] + Vector2(0, edit_draw->get_size().y));
	}
}