changeset 25:dc47ce56ce36

Client: add properties for Malikania.Sprite, #461 - cell, - columns, - margins, - rows, - space.
author David Demelier <markand@malikania.fr>
date Wed, 06 Apr 2016 13:33:30 +0200
parents 7f7c2607ace3
children 56cc058200b5
files libclient/malikania/js-sprite.cpp tests/libclient/js-sprite/main.cpp
diffstat 2 files changed, 159 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/libclient/malikania/js-sprite.cpp	Wed Apr 06 13:17:33 2016 +0200
+++ b/libclient/malikania/js-sprite.cpp	Wed Apr 06 13:33:30 2016 +0200
@@ -26,6 +26,41 @@
 
 namespace {
 
+duk::Ret cell(duk::ContextPtr ctx)
+{
+	duk::push(ctx, duk::self<duk::Pointer<Sprite>>(ctx)->cell());
+
+	return 1;
+}
+
+duk::Ret columns(duk::ContextPtr ctx)
+{
+	duk::push(ctx, static_cast<int>(duk::self<duk::Pointer<Sprite>>(ctx)->columns()));
+
+	return 1;
+}
+
+duk::Ret margins(duk::ContextPtr ctx)
+{
+	duk::push(ctx, duk::self<duk::Pointer<Sprite>>(ctx)->margin());
+
+	return 1;
+}
+
+duk::Ret rows(duk::ContextPtr ctx)
+{
+	duk::push(ctx, static_cast<int>(duk::self<duk::Pointer<Sprite>>(ctx)->rows()));
+
+	return 1;
+}
+
+duk::Ret space(duk::ContextPtr ctx)
+{
+	duk::push(ctx, duk::self<duk::Pointer<Sprite>>(ctx)->space());
+
+	return 1;
+}
+
 duk::Ret constructor(duk::ContextPtr ctx)
 {
 	if (!duk_is_constructor_call(ctx)) {
@@ -37,6 +72,34 @@
 		auto sprite = loader->loadSprite(duk::require<std::string>(ctx, 0));
 
 		duk::construct(ctx, duk::Pointer<Sprite>{new Sprite(std::move(sprite))});
+		duk::push(ctx, duk::This());
+
+		/* Cell */
+		duk::push(ctx, "cell");
+		duk::push(ctx, duk::Function{cell});
+		duk::defineProperty(ctx, -3, DUK_DEFPROP_HAVE_GETTER);
+
+		/* Columns */
+		duk::push(ctx, "columns");
+		duk::push(ctx, duk::Function{columns});
+		duk::defineProperty(ctx, -3, DUK_DEFPROP_HAVE_GETTER);
+
+		/* Margins */
+		duk::push(ctx, "margins");
+		duk::push(ctx, duk::Function{margins});
+		duk::defineProperty(ctx, -3, DUK_DEFPROP_HAVE_GETTER);
+
+		/* Rows */
+		duk::push(ctx, "rows");
+		duk::push(ctx, duk::Function{rows});
+		duk::defineProperty(ctx, -3, DUK_DEFPROP_HAVE_GETTER);
+
+		/* Space */
+		duk::push(ctx, "space");
+		duk::push(ctx, duk::Function{space});
+		duk::defineProperty(ctx, -3, DUK_DEFPROP_HAVE_GETTER);
+
+		duk::pop(ctx);
 	} catch (const std::exception &ex) {
 		duk::raise(ctx, DUK_ERR_ERROR, "%s", ex.what());
 	}
--- a/tests/libclient/js-sprite/main.cpp	Wed Apr 06 13:17:33 2016 +0200
+++ b/tests/libclient/js-sprite/main.cpp	Wed Apr 06 13:33:30 2016 +0200
@@ -54,6 +54,102 @@
 	}
 };
 
+TEST_F(TestSprite, cell)
+{
+	try {
+		auto ret = duk::pevalString(m_ctx,
+			"s = new Malikania.Sprite('sprites/margins.json');"
+			"w = s.cell.width;"
+			"h = s.cell.height;"
+		);
+
+		if (ret != 0) {
+			throw duk::error(m_ctx, -1);
+		}
+
+		ASSERT_EQ(32, duk::getGlobal<int>(m_ctx, "w"));
+		ASSERT_EQ(32, duk::getGlobal<int>(m_ctx, "h"));
+	} catch (const std::exception &ex) {
+		FAIL() << ex.what();
+	}
+}
+
+TEST_F(TestSprite, columns)
+{
+	try {
+		auto ret = duk::pevalString(m_ctx,
+			"s = new Malikania.Sprite('sprites/margins.json');"
+			"n = s.columns;"
+		);
+
+		if (ret != 0) {
+			throw duk::error(m_ctx, -1);
+		}
+
+		ASSERT_EQ(4, duk::getGlobal<int>(m_ctx, "n"));
+	} catch (const std::exception &ex) {
+		FAIL() << ex.what();
+	}
+}
+
+TEST_F(TestSprite, margins)
+{
+	try {
+		auto ret = duk::pevalString(m_ctx,
+			"s = new Malikania.Sprite('sprites/margins.json');"
+			"w = s.margins.width;"
+			"h = s.margins.height;"
+		);
+
+		if (ret != 0) {
+			throw duk::error(m_ctx, -1);
+		}
+
+		ASSERT_EQ(4, duk::getGlobal<int>(m_ctx, "w"));
+		ASSERT_EQ(6, duk::getGlobal<int>(m_ctx, "h"));
+	} catch (const std::exception &ex) {
+		FAIL() << ex.what();
+	}
+}
+
+TEST_F(TestSprite, rows)
+{
+	try {
+		auto ret = duk::pevalString(m_ctx,
+			"s = new Malikania.Sprite('sprites/margins.json');"
+			"n = s.rows;"
+		);
+
+		if (ret != 0) {
+			throw duk::error(m_ctx, -1);
+		}
+
+		ASSERT_EQ(3, duk::getGlobal<int>(m_ctx, "n"));
+	} catch (const std::exception &ex) {
+		FAIL() << ex.what();
+	}
+}
+
+TEST_F(TestSprite, space)
+{
+	try {
+		auto ret = duk::pevalString(m_ctx,
+			"s = new Malikania.Sprite('sprites/margins.json');"
+			"w = s.space.width;"
+			"h = s.space.height;"
+		);
+
+		if (ret != 0) {
+			throw duk::error(m_ctx, -1);
+		}
+
+		ASSERT_EQ(2, duk::getGlobal<int>(m_ctx, "w"));
+		ASSERT_EQ(3, duk::getGlobal<int>(m_ctx, "h"));
+	} catch (const std::exception &ex) {
+		FAIL() << ex.what();
+	}
+}
+
 TEST_F(TestSprite, basic)
 {
 	try {