Daniele Bartolini 11 лет назад
Родитель
Сommit
c9b01bfdfd
3 измененных файлов с 37 добавлено и 19 удалено
  1. 12 4
      engine/lua/LuaGui.cpp
  2. 22 14
      engine/renderers/Gui.cpp
  3. 3 1
      engine/renderers/Gui.h

+ 12 - 4
engine/lua/LuaGui.cpp

@@ -57,12 +57,18 @@ static int gui_move(lua_State* L)
 	LuaStack stack(L);
 
 	Gui* gui = stack.get_gui(1);
+	gui->move(stack.get_vector2(2));
+	return 0;
+}
 
-	const Vector3 pos = stack.get_vector3(2);
-
-	gui->move(pos);
+//-----------------------------------------------------------------------------
+static int gui_screen_to_gui(lua_State* L)
+{
+	LuaStack stack(L);
 
-	return 0;
+	Gui* gui = stack.get_gui(1);
+	stack.push_vector2(gui->screen_to_gui(stack.get_vector2(2)));
+	return 1;
 }
 
 //-----------------------------------------------------------------------------
@@ -89,6 +95,7 @@ static int gui_hide(lua_State* L)
 	return 0;
 }
 
+//-----------------------------------------------------------------------------
 static int gui_draw_rectangle(lua_State* L)
 {
 	LuaStack stack(L);
@@ -123,6 +130,7 @@ void load_gui(LuaEnvironment& env)
 {
 	env.load_module_function("Gui", "resolution",		gui_resolution);
 	env.load_module_function("Gui", "move",				gui_move);
+	env.load_module_function("Gui", "screen_to_gui",	gui_screen_to_gui);
 	env.load_module_function("Gui", "show",				gui_show);
 	env.load_module_function("Gui", "hide",				gui_hide);
 

+ 22 - 14
engine/renderers/Gui.cpp

@@ -107,7 +107,7 @@ Gui::Gui(RenderWorld& render_world, uint16_t width, uint16_t height)
 	, m_height(height)
 	, m_visible(true)
 {
-	set_orthographic_rh(m_projection, 0, width, height, 0, -0.01f, 100.0f);
+	set_orthographic_rh(m_projection, 0, width, 0, height, -0.01f, 100.0f);
 }
 
 //-----------------------------------------------------------------------------
@@ -129,10 +129,16 @@ Vector2 Gui::resolution() const
 }
 
 //-----------------------------------------------------------------------------
-void Gui::move(const Vector3& pos)
+void Gui::move(const Vector2& pos)
 {
 	set_identity(m_pose);
-	set_translation(m_pose, pos);
+	set_translation(m_pose, Vector3(pos.x, pos.y, 0));
+}
+
+//-----------------------------------------------------------------------------
+Vector2 Gui::screen_to_gui(const Vector2& pos)
+{
+	return Vector2(pos.x, m_height - pos.y);
 }
 
 //-----------------------------------------------------------------------------
@@ -165,10 +171,10 @@ void Gui::draw_rectangle(const Vector3& pos, const Vector2& size, const Color4&
 	verts[3] = pos.y;
 
 	verts[4] = pos.x + size.x;
-	verts[5] = pos.y - size.y;
+	verts[5] = pos.y + size.y;
 
 	verts[6] = pos.x;
-	verts[7] = pos.y - size.y;
+	verts[7] = pos.y + size.y;
 
 	uint16_t* inds = (uint16_t*) tib.data;
 	inds[0] = 0;
@@ -185,7 +191,7 @@ void Gui::draw_rectangle(const Vector3& pos, const Vector2& size, const Color4&
 					| STATE_CULL_CW
 					| STATE_BLEND_EQUATION_ADD 
 					| STATE_BLEND_FUNC(STATE_BLEND_FUNC_SRC_ALPHA, STATE_BLEND_FUNC_ONE_MINUS_SRC_ALPHA));
-
+	r->set_pose(m_pose);
 	r->set_program(render_world_globals::default_color_program());
 	r->set_uniform(render_world_globals::default_color_uniform(), UniformType::FLOAT_4, color4::to_float_ptr(color), 1);
 	r->set_vertex_buffer(tvb);
@@ -215,12 +221,12 @@ void Gui::draw_image(const char* material, const Vector3& pos, const Vector2& si
 	verts[7] = 0;
 
 	verts[8] = pos.x + size.x;
-	verts[9] = pos.y - size.y;
+	verts[9] = pos.y + size.y;
 	verts[10] = 1;
 	verts[11] = 1;
 
 	verts[12] = pos.x;
-	verts[13] = pos.y - size.y;
+	verts[13] = pos.y + size.y;
 	verts[14] = 0;
 	verts[15] = 1;
 
@@ -242,7 +248,7 @@ void Gui::draw_image(const char* material, const Vector3& pos, const Vector2& si
 					| STATE_CULL_CW
 					| STATE_BLEND_EQUATION_ADD 
 					| STATE_BLEND_FUNC(STATE_BLEND_FUNC_SRC_ALPHA, STATE_BLEND_FUNC_ONE_MINUS_SRC_ALPHA));
-
+	r->set_pose(m_pose);
 	r->set_program(render_world_globals::default_texture_program());
 	r->set_texture(0, render_world_globals::default_albedo_uniform(), tr->texture(),
 					TEXTURE_FILTER_LINEAR | TEXTURE_WRAP_U_CLAMP_REPEAT | TEXTURE_WRAP_V_CLAMP_REPEAT);
@@ -282,7 +288,7 @@ void Gui::draw_text(const char* str, const char* font, uint32_t font_size, const
 			case '\n':
 			{
 				x_pen_advance = 0.0f;
-				y_pen_advance += resource->font_size();
+				y_pen_advance -= resource->font_size();
 				continue;
 			}
 			case '\t':
@@ -296,15 +302,17 @@ void Gui::draw_text(const char* str, const char* font, uint32_t font_size, const
 		{
 			FontGlyphData g = resource->get_glyph(code_point);
 
+			const float baseline = g.height - g.y_offset;
+
 			// Set pen position
 			m_pen.x = pos.x + g.x_offset;
-			m_pen.y = pos.y + (g.height - g.y_offset);
+			m_pen.y = pos.y - baseline;
 
 			// Position coords
 			const float x0 = (m_pen.x + x_pen_advance) * scale;
 			const float y0 = (m_pen.y + y_pen_advance) * scale;
 			const float x1 = (m_pen.x + g.width + x_pen_advance) * scale;
-			const float y1 = (m_pen.y - g.height + y_pen_advance) * scale;
+			const float y1 = (m_pen.y + g.height + y_pen_advance) * scale;
 
 			// Texture coords
 			const float u0 = (float) g.x / 512;
@@ -321,7 +329,7 @@ void Gui::draw_text(const char* str, const char* font, uint32_t font_size, const
 
 			(*(VertexData*)(vb.data)).x		= x1;
 			(*(VertexData*)(vb.data)).y		= y0;
-			(*(VertexData*)(vb.data)).u		= u1; 
+			(*(VertexData*)(vb.data)).u		= u1;
 			(*(VertexData*)(vb.data)).v		= v1;
 			vb.data += sizeof(VertexData);
 
@@ -367,7 +375,7 @@ void Gui::draw_text(const char* str, const char* font, uint32_t font_size, const
 					| STATE_CULL_CW
 					| STATE_BLEND_EQUATION_ADD 
 					| STATE_BLEND_FUNC(STATE_BLEND_FUNC_SRC_ALPHA, STATE_BLEND_FUNC_ONE_MINUS_SRC_ALPHA));
-
+	r->set_pose(m_pose);
 	r->set_program(render_world_globals::default_font_program());
 	r->set_texture(0, render_world_globals::default_font_uniform(), tr->texture(),
 					TEXTURE_FILTER_LINEAR | TEXTURE_WRAP_U_CLAMP_REPEAT | TEXTURE_WRAP_V_CLAMP_REPEAT);

+ 3 - 1
engine/renderers/Gui.h

@@ -43,7 +43,9 @@ struct Gui
 	void set_id(const GuiId id);
 
 	Vector2 resolution() const;
-	void move(const Vector3& pos);
+	void move(const Vector2& pos);
+
+	Vector2 screen_to_gui(const Vector2& pos);
 
 	void show();
 	void hide();