Browse Source

UI work and some refactoring

Panagiotis Christopoulos Charitos 8 years ago
parent
commit
31c8f5504a
6 changed files with 56 additions and 19 deletions
  1. 4 4
      src/anki/ui/Canvas.cpp
  2. 6 1
      src/anki/ui/Canvas.h
  3. 15 6
      src/anki/ui/Font.cpp
  4. 21 5
      src/anki/ui/Font.h
  5. 9 2
      tests/ui/Ui.cpp
  6. 1 1
      thirdparty

+ 4 - 4
src/anki/ui/Canvas.cpp

@@ -24,12 +24,12 @@ Canvas::~Canvas()
 	nk_buffer_free(&m_nkCmdsBuff);
 }
 
-Error Canvas::init(FontPtr font)
+Error Canvas::init(FontPtr font, U32 fontHeight)
 {
 	m_font = font;
 
 	nk_allocator alloc = makeNkAllocator(&getAllocator().getMemoryPool());
-	if(!nk_init(&m_nkCtx, &alloc, &m_font->getFont().handle))
+	if(!nk_init(&m_nkCtx, &alloc, &m_font->getFont(fontHeight)))
 	{
 		ANKI_UI_LOGE("nk_init() failed");
 		return ErrorCode::FUNCTION_FAILED;
@@ -162,8 +162,8 @@ void Canvas::appendToCommandBuffer(CommandBufferPtr cmdb)
 	config.curve_segment_count = 22;
 	config.arc_segment_count = 22;
 	config.global_alpha = 1.0f;
-	config.shape_AA = NK_ANTI_ALIASING_ON;
-	config.line_AA = NK_ANTI_ALIASING_ON;
+	config.shape_AA = NK_ANTI_ALIASING_OFF;
+	config.line_AA = NK_ANTI_ALIASING_OFF;
 
 	nk_convert(&m_nkCtx, &m_nkCmdsBuff, &vertBuff, &idxBuff, &config);
 

+ 6 - 1
src/anki/ui/Canvas.h

@@ -23,13 +23,18 @@ public:
 
 	virtual ~Canvas();
 
-	ANKI_USE_RESULT Error init(FontPtr font);
+	ANKI_USE_RESULT Error init(FontPtr font, U32 fontHeight);
 
 	nk_context& getContext()
 	{
 		return m_nkCtx;
 	}
 
+	FontPtr getDefaultFont() const
+	{
+		return m_font;
+	}
+
 	/// Handle input.
 	virtual void handleInput();
 

+ 15 - 6
src/anki/ui/Font.cpp

@@ -18,9 +18,10 @@ namespace anki
 Font::~Font()
 {
 	nk_font_atlas_clear(&m_atlas);
+	m_fonts.destroy(getAllocator());
 }
 
-Error Font::init(const CString& filename, U32 fontHeight)
+Error Font::init(const CString& filename, const std::initializer_list<U32>& fontHeights)
 {
 	// Load font in memory
 	ResourceFilePtr file;
@@ -29,15 +30,23 @@ Error Font::init(const CString& filename, U32 fontHeight)
 	fontData.create(file->getSize());
 	ANKI_CHECK(file->read(&fontData[0], file->getSize()));
 
+	m_fonts.create(getAllocator(), fontHeights.size());
+
 	// Bake font
 	nk_allocator nkAlloc = makeNkAllocator(&getAllocator().getMemoryPool());
 	nk_font_atlas_init_custom(&m_atlas, &nkAlloc, &nkAlloc);
 	nk_font_atlas_begin(&m_atlas);
 
-	struct nk_font_config cfg = nk_font_config(fontHeight);
-	cfg.oversample_h = 8;
-	cfg.oversample_v = 8;
-	m_font = nk_font_atlas_add_from_memory(&m_atlas, &fontData[0], fontData.getSize(), fontHeight, &cfg);
+	U count = 0;
+	for(U32 height : fontHeights)
+	{
+		struct nk_font_config cfg = nk_font_config(height);
+		cfg.oversample_h = 4;
+		cfg.oversample_v = 4;
+		m_fonts[count].m_font = nk_font_atlas_add_from_memory(&m_atlas, &fontData[0], fontData.getSize(), height, &cfg);
+		m_fonts[count].m_height = height;
+		++count;
+	}
 
 	int width, height;
 	const void* img = nk_font_atlas_bake(&m_atlas, &width, &height, NK_FONT_ATLAS_RGBA32);
@@ -75,7 +84,7 @@ void Font::createTexture(const void* data, U32 width, U32 height)
 	texInit.m_usage =
 		TextureUsageBit::TRANSFER_DESTINATION | TextureUsageBit::SAMPLED_FRAGMENT | TextureUsageBit::GENERATE_MIPMAPS;
 	texInit.m_usageWhenEncountered = TextureUsageBit::SAMPLED_FRAGMENT;
-	texInit.m_mipmapsCount = 2;
+	texInit.m_mipmapsCount = 4;
 	texInit.m_sampling.m_minMagFilter = SamplingFilter::LINEAR;
 	texInit.m_sampling.m_mipmapFilter = SamplingFilter::LINEAR;
 

+ 21 - 5
src/anki/ui/Font.h

@@ -7,6 +7,7 @@
 
 #include <anki/ui/UiObject.h>
 #include <anki/gr/Texture.h>
+#include <initializer_list>
 
 namespace anki
 {
@@ -26,7 +27,7 @@ public:
 	~Font();
 
 	/// Initialize the font.
-	ANKI_USE_RESULT Error init(const CString& filename, U32 fontHeight);
+	ANKI_USE_RESULT Error init(const CString& filename, const std::initializer_list<U32>& fontHeights);
 
 anki_internal:
 	/// Get font image atlas.
@@ -35,15 +36,30 @@ anki_internal:
 		return m_tex;
 	}
 
-	const nk_font& getFont() const
+	const nk_user_font& getFont(U32 fontHeight) const
 	{
-		ANKI_ASSERT(m_font);
-		return *m_font;
+		for(const NkFont& f : m_fonts)
+		{
+			if(f.m_height == fontHeight)
+			{
+				return f.m_font->handle;
+			}
+		}
+
+		ANKI_ASSERT(0);
+		return *static_cast<const nk_user_font*>(nullptr);
 	}
 
 private:
 	nk_font_atlas m_atlas = {};
-	nk_font* m_font = nullptr;
+
+	struct NkFont
+	{
+		nk_font* m_font;
+		U32 m_height;
+	};
+
+	DynamicArray<NkFont> m_fonts;
 
 	TexturePtr m_tex;
 

+ 9 - 2
tests/ui/Ui.cpp

@@ -37,8 +37,14 @@ public:
 			   NK_WINDOW_BORDER | NK_WINDOW_MOVABLE | NK_WINDOW_SCALABLE | NK_WINDOW_MINIMIZABLE | NK_WINDOW_TITLE))
 		{
 			nk_layout_row_dynamic(ctx, 30, 1);
+
+			nk_style_push_font(ctx, &canvas->getDefaultFont()->getFont(20));
 			nk_label(ctx, "Label0", NK_TEXT_ALIGN_LEFT);
+			nk_style_pop_font(ctx);
+
+			nk_style_push_font(ctx, &canvas->getDefaultFont()->getFont(30));
 			nk_label(ctx, "Label1", NK_TEXT_ALIGN_LEFT);
+			nk_style_pop_font(ctx);
 		}
 
 		nk_end(ctx);
@@ -70,10 +76,11 @@ ANKI_TEST(Ui, Ui)
 
 	{
 		FontPtr font;
-		ANKI_TEST_EXPECT_NO_ERR(ui->newInstance(font, "engine_data/UbuntuRegular.ttf", 30));
+		ANKI_TEST_EXPECT_NO_ERR(
+			ui->newInstance(font, "engine_data/UbuntuRegular.ttf", std::initializer_list<U32>{20, 30, 32}));
 
 		CanvasPtr canvas;
-		ANKI_TEST_EXPECT_NO_ERR(ui->newInstance(canvas, font));
+		ANKI_TEST_EXPECT_NO_ERR(ui->newInstance(canvas, font, 30));
 
 		IntrusivePtr<Label> label;
 		ANKI_TEST_EXPECT_NO_ERR(ui->newInstance(label));

+ 1 - 1
thirdparty

@@ -1 +1 @@
-Subproject commit ae947050b6b3546b88711bc6026e41f424f93f19
+Subproject commit 22199053d707731f3f8130ca747e016df5077821