Index: /trunk/nv/gui/gui_common.hh
===================================================================
--- /trunk/nv/gui/gui_common.hh	(revision 266)
+++ /trunk/nv/gui/gui_common.hh	(revision 267)
@@ -14,5 +14,5 @@
 #define NV_GUI_COMMON_HH
 
-#include <nv/object.hh>
+#include <nv/common.hh>
 
 namespace nv
Index: /trunk/nv/gui/gui_element.hh
===================================================================
--- /trunk/nv/gui/gui_element.hh	(revision 266)
+++ /trunk/nv/gui/gui_element.hh	(revision 267)
@@ -28,39 +28,12 @@
 
 			/**
-			 * Creates a new GUI element with the give root node and postioning data.
+			 * Creates a new GUI element with the give root node and positioning data.
 			 *
 			 * @param r The rectangle representing the position and size of this element.
 			 */
-			explicit element( const rectangle& r );
+			explicit element( const rectangle& r )
+					: object( "" ), m_class(""), m_relative( r ), m_absolute( r ), m_enabled( true ), m_visible( true ), m_dirty( true ), m_render_data( nullptr ) {}
 
 		public:
-
-			/**
-			 * Gets the deepest child element that contains the indicated point.
-			 *
-			 * @param p The position to check.
-			 * @returns The last child element that contains the given point.
-			 */
-			element* get_element( const position& p );
-
-			/**
-			 * Event handler for update events. 
-			 *
-			 * @param elapsed Time since last update.
-			 */
-			virtual void on_update( uint32 /*elapsed*/ ) {}
-
-			/**
-			 * Event handler for draw events. 
-			 */
-			virtual void on_draw() {}
-
-			/**
-			 * Event handler for IO events.  Calls on_event for all affected children.
-			 *
-			 * @param event The event data.
-			 * @returns ???
-			 */
-			virtual bool on_event( const io_event& event );
 
 			/**
@@ -70,19 +43,8 @@
 			 * @returns True if the point is within the region of the element, false otherwise.
 			 */
-			virtual bool contains( const position& p ) const;
-
-			/**
-			 * Sets the position and size of this element relative to its parent.
-			 *
-			 * @param r The new position and size of the element.
-			 */
-			virtual void set_relative( const rectangle& r );
-
-			/**
-			 * Sets the position of this element relative to its parent.
-			 *
-			 * @param p The new position of the element.
-			 */
-			virtual void set_relative( const position& p );
+			virtual bool contains( const position& p ) const
+			{
+				return m_absolute.contains( p );
+			}
 
 			/**
@@ -99,11 +61,4 @@
 			 */
 			virtual const rectangle& get_absolute() const { return m_absolute; }
-
-			/**
-			 * Gets the parent element of this element.
-			 *
-			 * @returns The parent element.
-			 */
-			virtual element* get_parent() const { return (element*)m_parent; }
 
 			/**
@@ -177,33 +132,9 @@
 			virtual void set_class( const string& class_ ) { m_class = class_; m_dirty = true; }
 
-			/**
-			 * Recalcuates the absolute position of the element and the element's children.
-			 */
-			virtual void recalculate_absolute();
-
-			/**
-			 * Recalculates the aboslute position of the element's children.
-			 */
-			virtual void recalculate_absolute_children();
-
-			/**
-			 * Moves element to back of child list (top of stack)
-			 *
-			 * @returns true if object found, false otherwise
-			 */
-			bool move_to_top( element* child );
-
-			/**
-			 * Moves element to front of child list (bottom of stack)
-			 *
-			 * @returns true if object found, false otherwise
-			 */
-			bool move_to_bottom( element* child );
-
 		protected:
 			/**
 			 * Destroys the element.
 			 */
-			virtual ~element();
+			virtual ~element() { delete m_render_data; }
 		protected:
 			friend class environment;
Index: /trunk/nv/gui/gui_environment.hh
===================================================================
--- /trunk/nv/gui/gui_environment.hh	(revision 266)
+++ /trunk/nv/gui/gui_environment.hh	(revision 267)
@@ -17,6 +17,7 @@
 #include <nv/gui/gui_style.hh>
 #include <nv/lua/lua_state.hh>
+#include <nv/io_event.hh>
 #include <nv/interface/window.hh>
-#include <nv/root.hh>
+#include <nv/array.hh>
 
 namespace nv
@@ -25,11 +26,4 @@
 	namespace gui
 	{
-		class screen : public element
-		{
-		public:
-			screen( const rectangle& r ) : element( r ) {}
-			virtual bool on_event( const io_event& ) { return false; }
-			virtual void recalculate_absolute() { recalculate_absolute_children(); }
-		};
 
 		class environment
@@ -40,17 +34,26 @@
 			void load_style( const std::string& filename );
 			element* create_element( element* parent, const rectangle& r );
+			void update();
+			void draw();
+			void destroy_element( element* e );
+			bool process_io_event( const io_event& ev );
+			virtual ~environment();
+		protected:
+			element* get_element( const position& p );
+			void add_child( element* child );
+			void destroy_children( element* e );
 			void update( element* e, uint32 elapsed );
 			void draw( element* e );
-			void update();
-			void draw();
-			void add_child( element* child );
-			void destroy_element( element* e );
-			void destroy_children( element* e );
-			virtual ~environment();
-		protected:
-
+			bool process_io_event( element* e, const io_event& ev );
+			element* get_deepest_child( element* e, const position& p );
+			void move_to_top( element* child );
+			void move_to_bottom( element* child );
+			void set_relative( element* e, const rectangle& r );
+			void set_relative( element* e, const position& p );
+			void recalculate_absolute( element* e );
+			
 			renderer*    m_renderer;
 			window*      m_window;
-			screen*      m_screen;
+			element*     m_screen;
 			rectangle    m_area;
 		};
Index: /trunk/nv/gui/gui_renderer.hh
===================================================================
--- /trunk/nv/gui/gui_renderer.hh	(revision 266)
+++ /trunk/nv/gui/gui_renderer.hh	(revision 267)
@@ -22,4 +22,5 @@
 #include <nv/interface/device.hh>
 #include <nv/interface/context.hh>
+#include <nv/interface/camera.hh>
 #include <nv/gfx/sliced_buffer.hh>
 #include <nv/gfx/texture_atlas.hh>
@@ -68,4 +69,5 @@
 			bool          m_reupload;
 
+			scene_state   m_scene_state;
 			render_state  m_render_state;
 			render_data*  m_render_data;
Index: /trunk/nv/interface/camera.hh
===================================================================
--- /trunk/nv/interface/camera.hh	(revision 266)
+++ /trunk/nv/interface/camera.hh	(revision 267)
@@ -33,4 +33,8 @@
 		{
 			m_projection = glm::perspective( fov, aspect, near, far );
+		}
+		void set_ortho( f32 left, f32 right, f32 bottom, f32 top, f32 near = -1.0f, f32 far = 1.0f )
+		{
+			m_projection = glm::ortho( left, right, bottom, top, near, far );
 		}
 		const mat4& get_projection() const 
Index: unk/src/gui/gui_element.cc
===================================================================
--- /trunk/src/gui/gui_element.cc	(revision 266)
+++ 	(revision )
@@ -1,111 +1,0 @@
-// Copyright (C) 2012-2013 ChaosForge / Kornel Kisielewicz
-// http://chaosforge.org/
-//
-// This file is part of NV Libraries.
-// For conditions of distribution and use, see copyright notice in nv.hh
-
-#include "nv/gui/gui_element.hh"
-
-using namespace nv;
-using namespace nv::gui;
-
-element::element( const rectangle& r ) 
-	: object( "" ), m_class(""), m_relative( r ), m_absolute( r ), m_enabled( true ), m_visible( true ), m_dirty( true ), m_render_data( nullptr )
-{
-
-}
-
-bool element::on_event( const io_event& event )
-{
-	return m_parent ? ((element*)m_parent)->on_event( event ) : false;
-}
-
-element* element::get_element( const position& p )
-{
-	if ( !is_visible() ) return nullptr;
-
-	element* result = nullptr;
-	list::reverse_iterator it = m_children.rbegin();
-
-	while ( it != m_children.rend() )
-	{
-		result = ((element*)(*it))->get_element( p );
-		if ( result ) return result;
-		++it;
-	}
-
-	if ( contains( p ) ) return this;
-	return nullptr;
-}
-
-bool element::contains( const position& p ) const
-{
-	return m_absolute.contains( p );
-}
-
-void element::set_relative( const rectangle& r )
-{
-	m_dirty    = true;
-	m_relative = r;
-	recalculate_absolute();
-}
-
-void element::set_relative( const position& p )
-{
-	set_relative( rectangle( p, p + m_relative.get_size() ) );
-}
-
-void element::recalculate_absolute()
-{
-	rectangle pabsolute;
-
-	if ( m_parent )
-	{
-		pabsolute = ((element*)(m_parent))->m_absolute;
-	}
-
-	m_absolute = m_relative + pabsolute.ul;
-
-	for ( object* o : *this )
-	{
-		((element*)o)->recalculate_absolute();
-	}
-}
-
-void element::recalculate_absolute_children()
-{
-	for ( object* o : *this )
-	{
-		((element*)o)->recalculate_absolute();
-	}
-}
-
-
-bool element::move_to_top( element* child )
-{
-	list::iterator it = std::find( m_children.begin(), m_children.end(), (object*)child );
-	if ( it != m_children.end() )
-	{
-		m_children.erase( it );
-		m_children.push_back( child );
-		return true;
-	}	
-	return false;
-}
-
-bool element::move_to_bottom( element* child )
-{
-	list::iterator it = std::find( m_children.begin(), m_children.end(), (object*)child );
-	if ( it != m_children.end() )
-	{
-		m_children.erase( it );
-		m_children.push_front( child );
-		return true;
-	}	
-	return false;
-}
-
-element::~element()
-{
-	delete m_render_data;
-}
Index: /trunk/src/gui/gui_environment.cc
===================================================================
--- /trunk/src/gui/gui_environment.cc	(revision 266)
+++ /trunk/src/gui/gui_environment.cc	(revision 267)
@@ -25,14 +25,9 @@
 #include "nv/gfx/texture_atlas.hh"
 
-#include <vector>
-
-using namespace nv;
-using namespace nv::gui;
-
-environment::environment( window* w, const std::string& shader_path )
+nv::gui::environment::environment( window* w, const std::string& shader_path )
 	: m_renderer( nullptr ), m_window( w ), m_screen( nullptr )
 {
 	m_area.dim( dimension( w->get_width(), w->get_height() ) );
-	m_screen = new screen( m_area );
+	m_screen = new element( m_area );
 	m_renderer = new renderer( w, shader_path );
 }
@@ -43,5 +38,5 @@
 }
 
-element* nv::gui::environment::create_element( element* parent, const rectangle& r )
+nv::gui::element* nv::gui::environment::create_element( element* parent, const rectangle& r )
 {
 	element* result = new element( r );
@@ -58,7 +53,7 @@
 }
 
-void environment::update( element* e, uint32 elapsed )
-{
-	e->on_update( elapsed );
+void nv::gui::environment::update( element* e, uint32 elapsed )
+{
+//	e->on_update( elapsed );
 	if ( e->is_visible() )
 	{
@@ -75,9 +70,9 @@
 }
 
-void environment::draw( element* e )
+void nv::gui::environment::draw( element* e )
 {
 	if ( e->is_visible() )
 	{
-		e->on_draw();
+//		e->on_draw();
 		m_renderer->draw( e );
 		for ( object* i : *e )
@@ -88,10 +83,10 @@
 }
 
-void environment::update()
+void nv::gui::environment::update()
 {
 	update( m_screen, 0 );
 }
 
-void environment::draw()
+void nv::gui::environment::draw()
 {
 	draw( m_screen );
@@ -99,10 +94,10 @@
 }
 
-void environment::add_child( element* child )
+void nv::gui::environment::add_child( element* child )
 {
 	m_screen->add_child( child );
 }
 
-void environment::destroy_children( element* e )
+void nv::gui::environment::destroy_children( element* e )
 {
 	while ( !e->m_children.empty() )
@@ -113,5 +108,5 @@
 
 
-environment::~environment()
+nv::gui::environment::~environment()
 {
 	destroy_element( m_screen );
@@ -119,2 +114,94 @@
 }
 
+bool nv::gui::environment::process_io_event( const io_event& )
+{
+	return false;
+}
+
+bool nv::gui::environment::process_io_event( element* e, const io_event& ev )
+{
+	return e->m_parent ? process_io_event( (element*)e->m_parent, ev ) : false;
+}
+
+nv::gui::element* nv::gui::environment::get_element( const position& p )
+{
+	return get_deepest_child( m_screen, p );
+}
+
+nv::gui::element* nv::gui::environment::get_deepest_child( element* e, const position& p )
+{
+	if ( !e->is_visible() ) return nullptr;
+
+	element* result = nullptr;
+	element::list::reverse_iterator it = e->m_children.rbegin();
+
+	while ( it != e->m_children.rend() )
+	{
+		result = get_deepest_child( (element*)(*it), p );
+		if ( result ) return result;
+		++it;
+	}
+
+	if ( e->contains( p ) ) return e;
+	return nullptr;
+}
+
+void nv::gui::environment::move_to_top( element* child )
+{
+	element* parent = (element*)child->m_parent;
+	if ( parent )
+	{
+		auto it = std::find( parent->m_children.begin(), parent->m_children.end(), (object*)child );
+		if ( it != parent->m_children.end() )
+		{
+			parent->m_children.erase( it );
+			parent->m_children.push_back( child );
+			parent->m_dirty = true;
+		}	
+	}
+}
+
+void nv::gui::environment::move_to_bottom( element* child )
+{
+	element* parent = (element*)child->m_parent;
+	if ( parent )
+	{
+		auto it = std::find( parent->m_children.begin(), parent->m_children.end(), (object*)child );
+		if ( it != parent->m_children.end() )
+		{
+			parent->m_children.erase( it );
+			parent->m_children.push_front( child );
+			parent->m_dirty = true;
+		}
+	}
+}
+
+void nv::gui::environment::set_relative( element* e, const rectangle& r )
+{
+	e->m_dirty    = true;
+	e->m_relative = r;
+	recalculate_absolute( e );
+}
+
+void nv::gui::environment::set_relative( element* e, const position& p )
+{
+	set_relative( e, rectangle( p, p + e->m_relative.get_size() ) );
+}
+
+void nv::gui::environment::recalculate_absolute( element* e )
+{
+	rectangle pabsolute;
+
+	if ( e->m_parent )
+	{
+		pabsolute = ((element*)(e->m_parent))->m_absolute;
+	}
+
+	e->m_absolute = e->m_relative + pabsolute.ul;
+
+	for ( object* o : e->m_children )
+	{
+		recalculate_absolute( ((element*)o) );
+	}
+}
+
Index: /trunk/src/gui/gui_renderer.cc
===================================================================
--- /trunk/src/gui/gui_renderer.cc	(revision 266)
+++ /trunk/src/gui/gui_renderer.cc	(revision 267)
@@ -112,7 +112,5 @@
 	sr->varray     = m_window->get_device()->create_vertex_array();
 	sr->shader     = m_window->get_device()->create_program( nv::slurp( shader_path + ".vert" ), nv::slurp( shader_path + ".frag" ) );
-	sr->shader->set_uniform( "tex", 0 );
-	glm::mat4 projection = glm::ortho( 0.0f, float( m_window->get_width() ), float( m_window->get_height() ), 0.0f, -1.0f, 1.0f );
-	sr->shader->set_uniform( "nv_projection", projection );
+	m_scene_state.get_camera().set_ortho( 0.0f, float( m_window->get_width() ), float( m_window->get_height() ), 0.0f );
 
 	vertex_buffer* vb = (vertex_buffer*)sr->buffer.get_buffer();
@@ -274,7 +272,6 @@
 		sr->varray->update_vertex_buffer( nv::COLOR,    vb, false );
 	}
-	sr->texture->bind( 0 );
-	sr->shader->set_uniform( "tex", 0 );
-	m_window->get_context()->draw( TRIANGLES, m_render_state, sr->shader, sr->varray, sr->buffer.get_size() * 6 );
+	sr->texture->bind( nv::TEX_DIFFUSE );
+	m_window->get_context()->draw( TRIANGLES, m_render_state, m_scene_state, sr->shader, sr->varray, sr->buffer.get_size() * 6 );
 }
 
Index: /trunk/tests/gui_test/gui.frag
===================================================================
--- /trunk/tests/gui_test/gui.frag	(revision 266)
+++ /trunk/tests/gui_test/gui.frag	(revision 267)
@@ -2,8 +2,8 @@
 varying vec4 v_color;
 varying vec2 v_texcoord;
-uniform sampler2D tex;
+uniform sampler2D nv_t_diffuse;
  
 void main(void) {
-	vec4 texture = texture2D(tex,v_texcoord);
+	vec4 texture = texture2D(nv_t_diffuse,v_texcoord);
 	gl_FragColor = v_color * texture;
 }
Index: /trunk/tests/gui_test/gui.vert
===================================================================
--- /trunk/tests/gui_test/gui.vert	(revision 266)
+++ /trunk/tests/gui_test/gui.vert	(revision 267)
@@ -3,5 +3,5 @@
 attribute vec2 nv_texcoord;
 attribute vec4 nv_color;
-uniform mat4 nv_projection;
+uniform mat4 nv_m_projection;
 varying vec4 v_color;
 varying vec2 v_texcoord;
@@ -10,4 +10,4 @@
 	v_color     = nv_color;
 	v_texcoord  = nv_texcoord;
-	gl_Position = nv_projection * vec4( nv_position.x, nv_position.y, 0.0, 1.0 );
+	gl_Position = nv_m_projection * vec4( nv_position.x, nv_position.y, 0.0, 1.0 );
 }
Index: /trunk/tests/gui_test/nv_gui_test.cc
===================================================================
--- /trunk/tests/gui_test/nv_gui_test.cc	(revision 266)
+++ /trunk/tests/gui_test/nv_gui_test.cc	(revision 267)
@@ -16,4 +16,5 @@
 	void spawn_window();
 	void recolor_window();
+//	void bring_to_front( int i );
 	~application();
 protected:
@@ -64,4 +65,9 @@
 					switch (event.key.code) 
 					{
+// 					case nv::KEY_1      : bring_to_front(0); break;
+// 					case nv::KEY_2      : bring_to_front(1); break;
+// 					case nv::KEY_3      : bring_to_front(2); break;
+// 					case nv::KEY_4      : bring_to_front(3); break;
+// 					case nv::KEY_5      : bring_to_front(4); break;
 					case nv::KEY_N      : spawn_window(); break;
 					case nv::KEY_K      : kill_window(); break;
@@ -80,8 +86,7 @@
 	glm::ivec2 a( std::rand() % 600, std::rand() % 400 );
 	glm::ivec2 b( std::rand() % 200 + 40, std::rand() % 200 + 40 );
-	nv::gui::element* e = new nv::gui::element( m_guienv, nv::rectangle(a).dim(b) );
+	nv::gui::element* e = m_guienv->create_element( nullptr, nv::rectangle(a).dim(b) );
 	e->set_class( "window" );
 	e->set_text("Window "+nv::to_string(m_windows.size()+1) );
-	m_guienv->add_child( e );
 	NV_LOG( nv::LOG_INFO, "Spawn (" << a.x << "," << a.y << "x" << b.x << "," << b.y << ")" );
 	m_windows.push_back( e );
@@ -92,5 +97,5 @@
 	if ( m_windows.size() == 0 ) return;
 	size_t index = rand() % m_windows.size();
-	delete m_windows[ index ];
+	m_guienv->destroy_element( m_windows[index] );
 	m_windows.erase( m_windows.begin() + index );
 }
@@ -102,4 +107,12 @@
 	delete m_device;
 }
+
+// void application::bring_to_front( int i )
+// {
+// 	if ( i >= 0 && i < m_windows.size() )
+// 	{
+// 		m_guienv->bring_to_front( m_windows[i] );
+// 	}
+// }
 
 int main(int, char* [])
