summaryrefslogtreecommitdiffstats
path: root/o3d/import
diff options
context:
space:
mode:
authorgman@google.com <gman@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2009-07-24 06:06:34 +0000
committergman@google.com <gman@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2009-07-24 06:06:34 +0000
commit9108d4c2b521e8e58bbc125787550772f585ef6c (patch)
tree2d7959d73c9545dfa19537abf04fc1e053e7c9cc /o3d/import
parentac49d93303918e3eb99c030673432c7be876c87b (diff)
downloadchromium_src-9108d4c2b521e8e58bbc125787550772f585ef6c.zip
chromium_src-9108d4c2b521e8e58bbc125787550772f585ef6c.tar.gz
chromium_src-9108d4c2b521e8e58bbc125787550772f585ef6c.tar.bz2
Prep for moving Camera Info out of Params into JSON
I thought it was best to give you what I have so far for your input and code review instead of giving you everything all at once. This CL implements JSONObject which has a mechanism for adding stuff to be serialized as JSON to the serialization code. Since JSONObject is a serialization only object it seemed okay to put Serialize code inside. I opted to pre-declare JSONFloat and JSONOptionalFloat because I felt it made the code more error free. I had made it were you could just have JSONFloat and then with RegisterJSONValue you'd pass in optional or not but this way, declaring the field in a class makes it more explicit. CameraInfo is the first class that uses it. I'm not set on exactly how it is serialized. Whether it's as "object: { ... }" inside the "properties" section or whether it should have its own section. I think I won't know what until I actually write the deserialization code. That's not imporant for this CL This code, even if checked it, is not used yet as the import code, collada.cc, is not yet creating any of these objects. That's in another CL if you want to take a look http://codereview.chromium.org/160007 That CL will have to have lots of o3djs changes, corresponding sample changes and corresponding selenium test changes. Review URL: http://codereview.chromium.org/160008 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@21515 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'o3d/import')
-rw-r--r--o3d/import/build.scons2
-rw-r--r--o3d/import/cross/camera_info.cc98
-rw-r--r--o3d/import/cross/camera_info.h221
-rw-r--r--o3d/import/cross/camera_info_test.cc223
-rw-r--r--o3d/import/cross/json_object.cc67
-rw-r--r--o3d/import/cross/json_object.h289
-rw-r--r--o3d/import/cross/json_object_test.cc449
7 files changed, 1349 insertions, 0 deletions
diff --git a/o3d/import/build.scons b/o3d/import/build.scons
index a695a0a..e831685 100644
--- a/o3d/import/build.scons
+++ b/o3d/import/build.scons
@@ -62,9 +62,11 @@ else:
env.Append(CCFLAGS = [['-include', 'import/cross/precompile.h']])
collada_inputs = [
+ 'cross/camera_info.cc',
'cross/collada.cc',
'cross/collada_zip_archive.cc',
'cross/destination_buffer.cc',
+ 'cross/json_object.cc',
'cross/zip_archive.cc',
'cross/gz_compressor.cc',
'cross/file_output_stream_processor.cc',
diff --git a/o3d/import/cross/camera_info.cc b/o3d/import/cross/camera_info.cc
new file mode 100644
index 0000000..905c337
--- /dev/null
+++ b/o3d/import/cross/camera_info.cc
@@ -0,0 +1,98 @@
+/*
+ * Copyright 2009, Google Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following disclaimer
+ * in the documentation and/or other materials provided with the
+ * distribution.
+ * * Neither the name of Google Inc. nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "import/cross/camera_info.h"
+
+namespace o3d {
+
+O3D_OBJECT_BASE_DEFN_CLASS(
+ "o3djs.CameraInfo", CameraInfo, JSONObject);
+O3D_OBJECT_BASE_DEFN_CLASS(
+ "o3djs.PerspectiveCameraInfo", PerspectiveCameraInfo, CameraInfo);
+O3D_OBJECT_BASE_DEFN_CLASS(
+ "o3djs.OrthographicCameraInfo", OrthographicCameraInfo, CameraInfo);
+
+const char* CameraInfo::kAspectRatioParamName = "aspectRatio";
+const char* CameraInfo::kNearZParamName = "nearZ";
+const char* CameraInfo::kFarZParamName = "farZ";
+const char* CameraInfo::kTransformValueName = "transform";
+const char* CameraInfo::kEyeValueName = "eye";
+const char* CameraInfo::kTargetValueName = "target";
+const char* CameraInfo::kUpValueName = "up";
+
+CameraInfo::CameraInfo(ServiceLocator* service_locator)
+ : JSONObject(service_locator) {
+ RegisterParamRef(kAspectRatioParamName, &aspect_ratio_param_);
+ RegisterParamRef(kNearZParamName, &near_z_param_);
+ RegisterParamRef(kFarZParamName, &far_z_param_);
+ RegisterJSONValue(kTransformValueName, &transform_value_);
+ RegisterJSONValue(kEyeValueName, &eye_value_);
+ RegisterJSONValue(kTargetValueName, &target_value_);
+ RegisterJSONValue(kUpValueName, &up_value_);
+
+ set_aspect_ratio(1.0f);
+ set_near_z(0.01f);
+ set_far_z(10000.0f);
+}
+
+const char* OrthographicCameraInfo::kMagXParamName = "magX";
+const char* OrthographicCameraInfo::kMagYParamName = "magY";
+
+OrthographicCameraInfo::OrthographicCameraInfo(ServiceLocator* service_locator)
+ : CameraInfo(service_locator) {
+ RegisterParamRef(kMagXParamName, &mag_x_param_);
+ RegisterParamRef(kMagYParamName, &mag_y_param_);
+
+ set_mag_x(1.0f);
+ set_mag_y(1.0f);
+}
+
+ObjectBase::Ref OrthographicCameraInfo::Create(
+ ServiceLocator* service_locator) {
+ return ObjectBase::Ref(new OrthographicCameraInfo(service_locator));
+}
+
+const char* PerspectiveCameraInfo::kFieldOfViewYParamName = "fieldOfViewY";
+
+PerspectiveCameraInfo::PerspectiveCameraInfo(ServiceLocator* service_locator)
+ : CameraInfo(service_locator) {
+ RegisterParamRef(kFieldOfViewYParamName, &field_of_view_y_param_);
+
+ set_field_of_view_y(30.0f * 3.14159f / 180.0f);
+}
+
+ObjectBase::Ref PerspectiveCameraInfo::Create(ServiceLocator* service_locator) {
+ return ObjectBase::Ref(new PerspectiveCameraInfo(service_locator));
+}
+
+} // namespace o3d
+
+
diff --git a/o3d/import/cross/camera_info.h b/o3d/import/cross/camera_info.h
new file mode 100644
index 0000000..a221fbb
--- /dev/null
+++ b/o3d/import/cross/camera_info.h
@@ -0,0 +1,221 @@
+/*
+ * Copyright 2009, Google Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following disclaimer
+ * in the documentation and/or other materials provided with the
+ * distribution.
+ * * Neither the name of Google Inc. nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+
+// This file declares the CameraInfo class.
+
+#ifndef O3D_IMPORT_CROSS_CAMERA_INFO_H_
+#define O3D_IMPORT_CROSS_CAMERA_INFO_H_
+
+#include "import/cross/json_object.h"
+
+namespace o3d {
+
+// CameraInfo is a used for serialization only and is not part of the
+// normal O3D plugin. It is used for information about the camera.
+class CameraInfo : public JSONObject {
+ public:
+ typedef SmartPointer<CameraInfo> Ref;
+
+ static const char* kAspectRatioParamName;
+ static const char* kNearZParamName;
+ static const char* kFarZParamName;
+ static const char* kTransformValueName;
+ static const char* kEyeValueName;
+ static const char* kTargetValueName;
+ static const char* kUpValueName;
+
+ // Gets the near_z.
+ float near_z() const {
+ return near_z_param_->value();
+ }
+
+ // Sets the near_z.
+ void set_near_z(float value) {
+ near_z_param_->set_value(value);
+ }
+
+ // Gets the far_z.
+ float far_z() const {
+ return far_z_param_->value();
+ }
+
+ // Sets the far_z.
+ void set_far_z(float value) {
+ far_z_param_->set_value(value);
+ }
+
+ // Gets the aspect_ratio.
+ float aspect_ratio() const {
+ return aspect_ratio_param_->value();
+ }
+
+ // Sets the aspect_ratio.
+ void set_aspect_ratio(float value) {
+ aspect_ratio_param_->set_value(value);
+ }
+
+ // Gets the transform.
+ Transform* transform() const {
+ return transform_value_->value();
+ }
+
+ // Sets the transform.
+ void set_transform(Transform* value) {
+ transform_value_->set_value(value);
+ }
+
+ // Gets the eye.
+ Float3 eye() const {
+ return eye_value_->value();
+ }
+
+ // Sets the eye.
+ void set_eye(const Float3& value) {
+ eye_value_->set_value(value);
+ }
+
+ // Gets the target.
+ Float3 target() const {
+ return target_value_->value();
+ }
+
+ // Sets the target.
+ void set_target(const Float3& value) {
+ target_value_->set_value(value);
+ }
+
+ // Gets the up.
+ Float3 up() const {
+ return up_value_->value();
+ }
+
+ // Sets the up.
+ void set_up(const Float3& value) {
+ up_value_->set_value(value);
+ }
+
+ protected:
+ explicit CameraInfo(ServiceLocator* service_locator);
+
+ private:
+ // We make these params so we can easily attach animation to the camera's
+ // parameters.
+ ParamFloat::Ref aspect_ratio_param_;
+ ParamFloat::Ref near_z_param_;
+ ParamFloat::Ref far_z_param_;
+
+ // These are not params, just JSON values.
+ JSONTransform::Ref transform_value_;
+ JSONOptionalFloat3::Ref eye_value_;
+ JSONOptionalFloat3::Ref target_value_;
+ JSONOptionalFloat3::Ref up_value_;
+
+ O3D_OBJECT_BASE_DECL_CLASS(CameraInfo, JSONObject);
+ DISALLOW_COPY_AND_ASSIGN(CameraInfo);
+};
+
+class OrthographicCameraInfo : public CameraInfo {
+ public:
+ typedef SmartPointer<OrthographicCameraInfo> Ref;
+
+ static const char* kMagXParamName;
+ static const char* kMagYParamName;
+
+ // Gets the mag_x.
+ float mag_x() const {
+ return mag_x_param_->value();
+ }
+
+ // Sets the mag_x.
+ void set_mag_x(float value) {
+ mag_x_param_->set_value(value);
+ }
+
+ // Gets the mag_y.
+ float mag_y() const {
+ return mag_y_param_->value();
+ }
+
+ // Sets the mag_y.
+ void set_mag_y(float value) {
+ mag_y_param_->set_value(value);
+ }
+
+ private:
+ explicit OrthographicCameraInfo(ServiceLocator* service_locator);
+
+ friend class IClassManager;
+ static ObjectBase::Ref Create(ServiceLocator* service_locator);
+
+ // We make these params so we can easily attach animation to the camera's
+ // parameters.
+ ParamFloat::Ref mag_x_param_;
+ ParamFloat::Ref mag_y_param_;
+
+ O3D_OBJECT_BASE_DECL_CLASS(OrthographicCameraInfo, CameraInfo);
+ DISALLOW_COPY_AND_ASSIGN(OrthographicCameraInfo);
+};
+
+class PerspectiveCameraInfo : public CameraInfo {
+ public:
+ typedef SmartPointer<PerspectiveCameraInfo> Ref;
+
+ static const char* kFieldOfViewYParamName;
+
+ // Gets the field_of_view_y.
+ float field_of_view_y() const {
+ return field_of_view_y_param_->value();
+ }
+
+ // Sets the field_of_view_y.
+ void set_field_of_view_y(float value) {
+ field_of_view_y_param_->set_value(value);
+ }
+
+ private:
+ explicit PerspectiveCameraInfo(ServiceLocator* service_locator);
+
+ friend class IClassManager;
+ static ObjectBase::Ref Create(ServiceLocator* service_locator);
+
+ // We make these params so we can easily attach animation to the camera's
+ // parameters.
+ ParamFloat::Ref field_of_view_y_param_;
+
+ O3D_OBJECT_BASE_DECL_CLASS(PerspectiveCameraInfo, CameraInfo);
+ DISALLOW_COPY_AND_ASSIGN(PerspectiveCameraInfo);
+};
+
+} // namespace o3d
+
+#endif // O3D_IMPORT_CROSS_CAMERA_INFO_H_
+
diff --git a/o3d/import/cross/camera_info_test.cc b/o3d/import/cross/camera_info_test.cc
new file mode 100644
index 0000000..b21cf4a
--- /dev/null
+++ b/o3d/import/cross/camera_info_test.cc
@@ -0,0 +1,223 @@
+/*
+ * Copyright 2009, Google Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following disclaimer
+ * in the documentation and/or other materials provided with the
+ * distribution.
+ * * Neither the name of Google Inc. nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "tests/common/win/testing_common.h"
+#include "import/cross/camera_info.h"
+#include "core/cross/class_manager.h"
+#include "core/cross/object_manager.h"
+#include "core/cross/pack.h"
+#include "core/cross/service_dependency.h"
+#include "utils/cross/math_gtest.h"
+
+namespace o3d {
+
+namespace {
+
+// A class to test CameraInfo.
+class TestCameraInfo : public CameraInfo {
+ public:
+ typedef SmartPointer<TestCameraInfo> Ref;
+
+ private:
+ explicit TestCameraInfo(ServiceLocator* service_locator)
+ : CameraInfo(service_locator) {
+ }
+
+ friend class IClassManager;
+ static ObjectBase::Ref Create(ServiceLocator* service_locator);
+
+ O3D_OBJECT_BASE_DECL_CLASS(TestCameraInfo, CameraInfo);
+ DISALLOW_COPY_AND_ASSIGN(TestCameraInfo);
+};
+
+O3D_OBJECT_BASE_DEFN_CLASS(
+ "o3djs.TestCameraInfo", TestCameraInfo, CameraInfo);
+
+ObjectBase::Ref TestCameraInfo::Create(
+ ServiceLocator* service_locator) {
+ return ObjectBase::Ref(new TestCameraInfo(service_locator));
+}
+
+} // anonymous namespace
+
+class TestCameraInfoTest : public testing::Test {
+ protected:
+ TestCameraInfoTest()
+ : class_manager_(g_service_locator),
+ object_manager_(g_service_locator),
+ class_register_(g_service_locator) {
+ }
+
+ virtual void SetUp();
+ virtual void TearDown();
+
+ Pack* pack() { return pack_; }
+
+ private:
+ ServiceDependency<ClassManager> class_manager_;
+ ServiceDependency<ObjectManager> object_manager_;
+ ClassManager::Register<TestCameraInfo> class_register_;
+ Pack* pack_;
+};
+
+void TestCameraInfoTest::SetUp() {
+ pack_ = object_manager_->CreatePack();
+}
+
+void TestCameraInfoTest::TearDown() {
+ object_manager_->DestroyPack(pack_);
+}
+
+// Creates a TestCameraInfo, tests basic properties.
+TEST_F(TestCameraInfoTest, TestTestCameraInfo) {
+ TestCameraInfo *camera_info = pack()->Create<TestCameraInfo>();
+ EXPECT_TRUE(camera_info->IsA(TestCameraInfo::GetApparentClass()));
+ EXPECT_TRUE(camera_info->IsA(CameraInfo::GetApparentClass()));
+ EXPECT_TRUE(camera_info->IsA(JSONObject::GetApparentClass()));
+ EXPECT_TRUE(camera_info->IsA(ParamObject::GetApparentClass()));
+
+ EXPECT_EQ(camera_info->aspect_ratio(), 1.0f);
+ EXPECT_EQ(camera_info->near_z(), 0.01f);
+ EXPECT_EQ(camera_info->far_z(), 10000.0f);
+ EXPECT_TRUE(camera_info->transform() == NULL);
+
+ Transform* transform = pack()->Create<Transform>();
+ ASSERT_TRUE(transform != NULL);
+
+ camera_info->set_aspect_ratio(2.0f);
+ camera_info->set_near_z(0.02f);
+ camera_info->set_far_z(20000.0f);
+ camera_info->set_transform(transform);
+ camera_info->set_eye(Float3(10.0f, 20.0f, 30.0f));
+ camera_info->set_target(Float3(11.0f, 22.0f, 33.0f));
+ camera_info->set_up(Float3(12.0f, 23.0f, 34.0f));
+
+ EXPECT_EQ(camera_info->aspect_ratio(), 2.0f);
+ EXPECT_EQ(camera_info->near_z(), 0.02f);
+ EXPECT_EQ(camera_info->far_z(), 20000.0f);
+ EXPECT_TRUE(camera_info->transform() == transform);
+ EXPECT_EQ(camera_info->eye(), Float3(10.0f, 20.0f, 30.0f));
+ EXPECT_EQ(camera_info->target(), Float3(11.0f, 22.0f, 33.0f));
+ EXPECT_EQ(camera_info->up(), Float3(12.0f, 23.0f, 34.0f));
+}
+
+class PerspectiveCameraInfoTest : public testing::Test {
+ protected:
+ PerspectiveCameraInfoTest()
+ : class_manager_(g_service_locator),
+ object_manager_(g_service_locator),
+ class_register_(g_service_locator) {
+ }
+
+ virtual void SetUp();
+ virtual void TearDown();
+
+ Pack* pack() { return pack_; }
+
+ private:
+ ServiceDependency<ClassManager> class_manager_;
+ ServiceDependency<ObjectManager> object_manager_;
+ ClassManager::Register<PerspectiveCameraInfo> class_register_;
+ Pack* pack_;
+};
+
+void PerspectiveCameraInfoTest::SetUp() {
+ pack_ = object_manager_->CreatePack();
+}
+
+void PerspectiveCameraInfoTest::TearDown() {
+ object_manager_->DestroyPack(pack_);
+}
+
+// Creates a PerspectiveCameraInfo, tests basic properties.
+TEST_F(PerspectiveCameraInfoTest, TestPerspectiveCameraInfo) {
+ PerspectiveCameraInfo *camera_info = pack()->Create<PerspectiveCameraInfo>();
+ EXPECT_TRUE(camera_info->IsA(PerspectiveCameraInfo::GetApparentClass()));
+ EXPECT_TRUE(camera_info->IsA(CameraInfo::GetApparentClass()));
+ EXPECT_TRUE(camera_info->IsA(JSONObject::GetApparentClass()));
+ EXPECT_TRUE(camera_info->IsA(ParamObject::GetApparentClass()));
+
+ EXPECT_EQ(camera_info->field_of_view_y(), 30.0f * 3.14159f / 180.0f);
+
+ camera_info->set_field_of_view_y(60.0f * 3.14159f / 180.0f);
+
+ EXPECT_EQ(camera_info->field_of_view_y(), 60.0f * 3.14159f / 180.0f);
+}
+
+class OrthographicCameraInfoTest : public testing::Test {
+ protected:
+ OrthographicCameraInfoTest()
+ : class_manager_(g_service_locator),
+ object_manager_(g_service_locator),
+ class_register_(g_service_locator) {
+ }
+
+ virtual void SetUp();
+ virtual void TearDown();
+
+ Pack* pack() { return pack_; }
+
+ private:
+ ServiceDependency<ClassManager> class_manager_;
+ ServiceDependency<ObjectManager> object_manager_;
+ ClassManager::Register<OrthographicCameraInfo> class_register_;
+ Pack* pack_;
+};
+
+void OrthographicCameraInfoTest::SetUp() {
+ pack_ = object_manager_->CreatePack();
+}
+
+void OrthographicCameraInfoTest::TearDown() {
+ object_manager_->DestroyPack(pack_);
+}
+
+// Creates a OrthographicCameraInfo, tests basic properties.
+TEST_F(OrthographicCameraInfoTest, TestOrthographicCameraInfo) {
+ OrthographicCameraInfo *camera_info =
+ pack()->Create<OrthographicCameraInfo>();
+ EXPECT_TRUE(camera_info->IsA(OrthographicCameraInfo::GetApparentClass()));
+ EXPECT_TRUE(camera_info->IsA(CameraInfo::GetApparentClass()));
+ EXPECT_TRUE(camera_info->IsA(JSONObject::GetApparentClass()));
+ EXPECT_TRUE(camera_info->IsA(ParamObject::GetApparentClass()));
+
+ EXPECT_EQ(camera_info->mag_x(), 1.0f);
+ EXPECT_EQ(camera_info->mag_y(), 1.0f);
+
+ camera_info->set_mag_x(2.0f);
+ camera_info->set_mag_y(3.0f);
+
+ EXPECT_EQ(camera_info->mag_x(), 2.0f);
+ EXPECT_EQ(camera_info->mag_y(), 3.0f);
+}
+
+} // namespace o3d
+
diff --git a/o3d/import/cross/json_object.cc b/o3d/import/cross/json_object.cc
new file mode 100644
index 0000000..bf3f79e
--- /dev/null
+++ b/o3d/import/cross/json_object.cc
@@ -0,0 +1,67 @@
+/*
+ * Copyright 2009, Google Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following disclaimer
+ * in the documentation and/or other materials provided with the
+ * distribution.
+ * * Neither the name of Google Inc. nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+// This file defines the JSON Object class.
+
+#include "core/cross/precompile.h"
+#include "import/cross/json_object.h"
+
+namespace o3d {
+
+O3D_OBJECT_BASE_DEFN_CLASS("o3djs.JSONObject", JSONObject, ParamObject);
+
+JSONObject::JSONObject(ServiceLocator* service_locator)
+ : ParamObject(service_locator) {
+}
+
+void JSONObject::AddProperty(const String& name, JSONValue* value) {
+ std::pair<NameValueMap::iterator, bool> result = properties_.insert(
+ std::pair<String, JSONValue::Ref>(name, JSONValue::Ref(value)));
+ DCHECK(result.second);
+}
+
+void JSONObject::Serialize(StructuredWriter* writer) const {
+ writer->WritePropertyName("object");
+ writer->OpenObject();
+ for (NameValueMap::const_iterator it(properties_.begin());
+ it != properties_.end();
+ ++it) {
+ if (it->second->exists()) {
+ writer->WritePropertyName(it->first);
+ it->second->Serialize(writer);
+ }
+ }
+ writer->CloseObject();
+}
+
+} // namespace o3d
+
+
diff --git a/o3d/import/cross/json_object.h b/o3d/import/cross/json_object.h
new file mode 100644
index 0000000..3cf2815
--- /dev/null
+++ b/o3d/import/cross/json_object.h
@@ -0,0 +1,289 @@
+/*
+ * Copyright 2009, Google Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following disclaimer
+ * in the documentation and/or other materials provided with the
+ * distribution.
+ * * Neither the name of Google Inc. nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+// This file declares the JSONObject class.
+
+#ifndef O3D_IMPORT_CROSS_JSON_OBJECT_H_
+#define O3D_IMPORT_CROSS_JSON_OBJECT_H_
+
+#include <map>
+#include "core/cross/param_object.h"
+#include "core/cross/material.h"
+#include "core/cross/transform.h"
+#include "serializer/cross/serializer.h"
+
+namespace o3d {
+
+class StructuredWriter;
+
+// A JSONValue is a base class for all JSON values stored in a JSONObject.
+class JSONValue : public RefCounted {
+ public:
+ typedef SmartPointer<JSONValue> Ref;
+
+ virtual ~JSONValue() {
+ }
+
+ // Whether or not this value exists.
+ bool exists() const {
+ return exists_;
+ }
+
+ // Sets the existence of this value.
+ void set_exists(bool exists) {
+ exists_ = exists;
+ }
+
+ // Function to serialize the value of this JSON value.
+ virtual void Serialize(StructuredWriter* writer) const = 0;
+
+ protected:
+ // optional means the value is optional. If true the value defaults to
+ // not existing. If false the value defaults to existing.
+ explicit JSONValue(bool optional)
+ : optional_(optional), exists_(!optional) {
+ }
+
+ private:
+ bool optional_;
+ bool exists_; // used for optional values.
+};
+
+// A Template for optional typed non-ref JSON values.
+template<class T, bool optional>
+class TypedJSONValue : public JSONValue {
+ public:
+ typedef T DataType;
+ TypedJSONValue()
+ : JSONValue(optional) {
+ }
+ virtual ~TypedJSONValue() {}
+
+ // Sets the value stored in the JSONValue.
+ void set_value(const DataType& value) {
+ set_exists(true);
+ value_ = value;
+ }
+
+ // Returns the current value stored in the JSONValue.
+ DataType value() const {
+ DCHECK(exists());
+ return value_;
+ }
+
+ // Overridden from JSONValue.
+ virtual void Serialize(StructuredWriter* writer) const {
+ DCHECK(exists());
+ o3d::Serialize(writer, value());
+ }
+
+ private:
+ // The value stored in the JSONValue.
+ DataType value_;
+
+ DISALLOW_COPY_AND_ASSIGN(TypedJSONValue);
+};
+
+// A Template for typed ref JSON values.
+template<typename T>
+class TypedRefJSONValue : public JSONValue {
+ public:
+ typedef T* Pointer;
+ typedef T DataType;
+
+ TypedRefJSONValue()
+ : JSONValue(false) {
+ }
+
+ virtual ~TypedRefJSONValue() {
+ }
+
+ // Set the value stored in the.
+ void set_value(Pointer const value) {
+ value_ = typename T::Ref(value);
+ }
+
+ // Returns the current value stored in the Param.
+ Pointer value() const {
+ return value_.Get();
+ }
+
+ // Overridden from JSONValue.
+ virtual void Serialize(StructuredWriter* writer) const {
+ o3d::Serialize(writer, static_cast<ObjectBase*>(value()));
+ }
+
+ protected:
+ typename T::Ref value_;
+
+ private:
+ DISALLOW_COPY_AND_ASSIGN(TypedRefJSONValue);
+};
+
+// Classes for various types of JSON Data.
+class JSONFloat : public TypedJSONValue<float, false> {
+ public:
+ typedef SmartPointer<JSONFloat> Ref;
+ JSONFloat() {
+ set_value(0.0f);
+ }
+};
+class JSONFloat2 : public TypedJSONValue<Float2, false> {
+ public:
+ typedef SmartPointer<JSONFloat2> Ref;
+ JSONFloat2() {
+ set_value(Float2(0.0f, 0.0f));
+ }
+};
+class JSONFloat3 : public TypedJSONValue<Float3, false> {
+ public:
+ typedef SmartPointer<JSONFloat3> Ref;
+ JSONFloat3() {
+ set_value(Float3(0.0f, 0.0f, 0.0f));
+ }
+};
+class JSONFloat4 : public TypedJSONValue<Float4, false> {
+ public:
+ typedef SmartPointer<JSONFloat4> Ref;
+ JSONFloat4() {
+ set_value(Float4(0.0f, 0.0f, 0.0f, 0.0f));
+ }
+};
+class JSONMatrix4 : public TypedJSONValue<Matrix4, false> {
+ public:
+ typedef SmartPointer<JSONMatrix4> Ref;
+ JSONMatrix4() {
+ set_value(Matrix4::identity());
+ }
+};
+class JSONInteger : public TypedJSONValue<int, false> {
+ public:
+ typedef SmartPointer<JSONInteger> Ref;
+ JSONInteger() {
+ set_value(0);
+ }
+};
+class JSONBoolean : public TypedJSONValue<bool, false> {
+ public:
+ typedef SmartPointer<JSONBoolean> Ref;
+ JSONBoolean() {
+ set_value(false);
+ }
+};
+class JSONString : public TypedJSONValue<String, false> {
+ public:
+ typedef SmartPointer<JSONString> Ref;
+};
+class JSONTransform : public TypedRefJSONValue<Transform> {
+ public:
+ typedef SmartPointer<JSONTransform> Ref;
+};
+class JSONMaterial : public TypedRefJSONValue<Material> {
+ public:
+ typedef SmartPointer<JSONMaterial> Ref;
+};
+class JSONOptionalFloat : public TypedJSONValue<float, true> {
+ public:
+ typedef SmartPointer<JSONOptionalFloat> Ref;
+};
+class JSONOptionalFloat2 : public TypedJSONValue<Float2, true> {
+ public:
+ typedef SmartPointer<JSONOptionalFloat2> Ref;
+};
+class JSONOptionalFloat3 : public TypedJSONValue<Float3, true> {
+ public:
+ typedef SmartPointer<JSONOptionalFloat3> Ref;
+};
+class JSONOptionalFloat4 : public TypedJSONValue<Float4, true> {
+ public:
+ typedef SmartPointer<JSONOptionalFloat4> Ref;
+};
+class JSONOptionalMatrix4 : public TypedJSONValue<Matrix4, true> {
+ public:
+ typedef SmartPointer<JSONOptionalMatrix4> Ref;
+};
+class JSONOptionalInteger : public TypedJSONValue<int, true> {
+ public:
+ typedef SmartPointer<JSONOptionalInteger> Ref;
+};
+class JSONOptionalBoolean : public TypedJSONValue<bool, true> {
+ public:
+ typedef SmartPointer<JSONOptionalBoolean> Ref;
+};
+class JSONOptionalString : public TypedJSONValue<String, true> {
+ public:
+ typedef SmartPointer<JSONOptionalString> Ref;
+};
+
+// A JSONObject is a used for serialization only and is not part of the normal
+// O3D plugin. It is used to easily add name/value pairs to be serialized by the
+// serializer as JSON objects. Being that it's ParamObject you have 2 choices
+// for getting data serialized. As O3D Params or as JSONValues. The reason to
+// use one over the other... Params can be animated but are relatively heavy.
+// JSONValues become plane JavaScript when deserialized.
+// To add a Param use RegisterParamRef, to add a JSONValue use
+// RegisterJSONValue.
+class JSONObject : public ParamObject {
+ public:
+ typedef SmartPointer<JSONObject> Ref;
+ typedef std::map<String, JSONValue::Ref> NameValueMap;
+
+ // Seralizes the JSONObject.
+ void Serialize(StructuredWriter* writer) const;
+
+ protected:
+ explicit JSONObject(ServiceLocator* service_locator);
+
+ // Registers a pointer to a reference to a JSONValue.
+ // Parameters:
+ // name: name of JSONValue
+ // ref_pointer: Pointer to typed reference to JSONValue.
+ template<typename T>
+ void RegisterJSONValue(const String& name, T* ref_pointer) {
+ *ref_pointer = T(new typename T::ClassType());
+ AddProperty(name, ref_pointer->Get());
+ }
+
+ private:
+ // Adds a property to this JSON Object.
+ void AddProperty(const String& name, JSONValue* value);
+
+ NameValueMap properties_;
+
+ O3D_OBJECT_BASE_DECL_CLASS(JSONObject, ParamObject);
+ DISALLOW_COPY_AND_ASSIGN(JSONObject);
+};
+
+} // namespace o3d
+
+#endif // O3D_IMPORT_CROSS_JSON_OBJECT_H_
+
+
diff --git a/o3d/import/cross/json_object_test.cc b/o3d/import/cross/json_object_test.cc
new file mode 100644
index 0000000..ff745a2
--- /dev/null
+++ b/o3d/import/cross/json_object_test.cc
@@ -0,0 +1,449 @@
+/*
+ * Copyright 2009, Google Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following disclaimer
+ * in the documentation and/or other materials provided with the
+ * distribution.
+ * * Neither the name of Google Inc. nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "tests/common/win/testing_common.h"
+#include "import/cross/json_object.h"
+#include "core/cross/class_manager.h"
+#include "core/cross/object_manager.h"
+#include "core/cross/pack.h"
+#include "core/cross/service_dependency.h"
+#include "utils/cross/string_writer.h"
+#include "utils/cross/json_writer.h"
+#include "utils/cross/math_gtest.h"
+
+namespace o3d {
+
+namespace {
+
+// A class to test JSONObject
+class TestJSONObject : public JSONObject {
+ public:
+ typedef SmartPointer<TestJSONObject> Ref;
+
+ static const char* kFloatValueName;
+ static const char* kFloat2ValueName;
+ static const char* kFloat3ValueName;
+ static const char* kFloat4ValueName;
+ static const char* kMatrix4ValueName;
+ static const char* kIntegerValueName;
+ static const char* kBooleanValueName;
+ static const char* kStringValueName;
+ static const char* kTransformValueName;
+ static const char* kMaterialValueName;
+ static const char* kOptionalFloatValueName;
+ static const char* kOptionalFloat2ValueName;
+ static const char* kOptionalFloat3ValueName;
+ static const char* kOptionalFloat4ValueName;
+ static const char* kOptionalMatrix4ValueName;
+ static const char* kOptionalIntegerValueName;
+ static const char* kOptionalBooleanValueName;
+ static const char* kOptionalStringValueName;
+
+ float float_value() const {
+ return float_value_->value();
+ }
+
+ void set_float_value(float value) {
+ float_value_->set_value(value);
+ }
+
+ Float2 float2_value() const {
+ return float2_value_->value();
+ }
+
+ void set_float2_value(const Float2& value) {
+ float2_value_->set_value(value);
+ }
+
+ Float3 float3_value() const {
+ return float3_value_->value();
+ }
+
+ void set_float3_value(const Float3& value) {
+ float3_value_->set_value(value);
+ }
+
+ Float4 float4_value() const {
+ return float4_value_->value();
+ }
+
+ void set_float4_value(const Float4& value) {
+ float4_value_->set_value(value);
+ }
+
+ Matrix4 matrix4_value() const {
+ return matrix4_value_->value();
+ }
+
+ void set_matrix4_value(const Matrix4& value) {
+ matrix4_value_->set_value(value);
+ }
+
+ int integer_value() const {
+ return integer_value_->value();
+ }
+
+ void set_integer_value(int value) {
+ integer_value_->set_value(value);
+ }
+
+ bool boolean_value() const {
+ return boolean_value_->value();
+ }
+
+ void set_boolean_value(bool value) {
+ boolean_value_->set_value(value);
+ }
+
+ String string_value() const {
+ return string_value_->value();
+ }
+
+ void set_string_value(const String& value) {
+ string_value_->set_value(value);
+ }
+
+ Transform* transform_value() const {
+ return transform_value_->value();
+ }
+
+ void set_transform_value(Transform* value) {
+ transform_value_->set_value(value);
+ }
+
+ Material* material_value() const {
+ return material_value_->value();
+ }
+
+ void set_material_value(Material* value) {
+ material_value_->set_value(value);
+ }
+
+ float optional_float_value() const {
+ return optional_float_value_->value();
+ }
+
+ void set_optional_float_value(float value) {
+ optional_float_value_->set_value(value);
+ }
+
+ Float2 optional_float2_value() const {
+ return optional_float2_value_->value();
+ }
+
+ void set_optional_float2_value(const Float2& value) {
+ optional_float2_value_->set_value(value);
+ }
+
+ Float3 optional_float3_value() const {
+ return optional_float3_value_->value();
+ }
+
+ void set_optional_float3_value(const Float3& value) {
+ optional_float3_value_->set_value(value);
+ }
+
+ Float4 optional_float4_value() const {
+ return optional_float4_value_->value();
+ }
+
+ void set_optional_float4_value(const Float4& value) {
+ optional_float4_value_->set_value(value);
+ }
+
+ Matrix4 optional_matrix4_value() const {
+ return optional_matrix4_value_->value();
+ }
+
+ void set_optional_matrix4_value(const Matrix4& value) {
+ optional_matrix4_value_->set_value(value);
+ }
+
+ int optional_integer_value() const {
+ return optional_integer_value_->value();
+ }
+
+ void set_optional_integer_value(int value) {
+ optional_integer_value_->set_value(value);
+ }
+
+ bool optional_boolean_value() const {
+ return optional_boolean_value_->value();
+ }
+
+ void set_optional_boolean_value(bool value) {
+ optional_boolean_value_->set_value(value);
+ }
+
+ String optional_string_value() const {
+ return optional_string_value_->value();
+ }
+
+ void set_optional_string_value(const String& value) {
+ optional_string_value_->set_value(value);
+ }
+
+ private:
+ explicit TestJSONObject(ServiceLocator* service_locator)
+ : JSONObject(service_locator) {
+ RegisterJSONValue(kFloatValueName, &float_value_);
+ RegisterJSONValue(kFloat2ValueName, &float2_value_);
+ RegisterJSONValue(kFloat3ValueName, &float3_value_);
+ RegisterJSONValue(kFloat4ValueName, &float4_value_);
+ RegisterJSONValue(kMatrix4ValueName, &matrix4_value_);
+ RegisterJSONValue(kIntegerValueName, &integer_value_);
+ RegisterJSONValue(kBooleanValueName, &boolean_value_);
+ RegisterJSONValue(kStringValueName, &string_value_);
+ RegisterJSONValue(kTransformValueName, &transform_value_);
+ RegisterJSONValue(kMaterialValueName, &material_value_);
+ RegisterJSONValue(kOptionalFloatValueName, &optional_float_value_);
+ RegisterJSONValue(kOptionalFloat2ValueName, &optional_float2_value_);
+ RegisterJSONValue(kOptionalFloat3ValueName, &optional_float3_value_);
+ RegisterJSONValue(kOptionalFloat4ValueName, &optional_float4_value_);
+ RegisterJSONValue(kOptionalMatrix4ValueName, &optional_matrix4_value_);
+ RegisterJSONValue(kOptionalIntegerValueName, &optional_integer_value_);
+ RegisterJSONValue(kOptionalBooleanValueName, &optional_boolean_value_);
+ RegisterJSONValue(kOptionalStringValueName, &optional_string_value_);
+ }
+
+ friend class IClassManager;
+ static ObjectBase::Ref Create(ServiceLocator* service_locator) {
+ return ObjectBase::Ref(new TestJSONObject(service_locator));
+ }
+
+ // One of each type of JSONValue
+ JSONFloat::Ref float_value_;
+ JSONFloat2::Ref float2_value_;
+ JSONFloat3::Ref float3_value_;
+ JSONFloat4::Ref float4_value_;
+ JSONMatrix4::Ref matrix4_value_;
+ JSONInteger::Ref integer_value_;
+ JSONBoolean::Ref boolean_value_;
+ JSONString::Ref string_value_;
+ JSONTransform::Ref transform_value_;
+ JSONMaterial::Ref material_value_;
+ JSONOptionalFloat::Ref optional_float_value_;
+ JSONOptionalFloat2::Ref optional_float2_value_;
+ JSONOptionalFloat3::Ref optional_float3_value_;
+ JSONOptionalFloat4::Ref optional_float4_value_;
+ JSONOptionalMatrix4::Ref optional_matrix4_value_;
+ JSONOptionalInteger::Ref optional_integer_value_;
+ JSONOptionalBoolean::Ref optional_boolean_value_;
+ JSONOptionalString::Ref optional_string_value_;
+
+ O3D_OBJECT_BASE_DECL_CLASS(TestJSONObject, JSONObject);
+ DISALLOW_COPY_AND_ASSIGN(TestJSONObject);
+};
+
+O3D_OBJECT_BASE_DEFN_CLASS("TestJSONObject", TestJSONObject, JSONObject);
+
+const char* TestJSONObject::kFloatValueName = "floatValue";
+const char* TestJSONObject::kFloat2ValueName = "float2Value";
+const char* TestJSONObject::kFloat3ValueName = "float3Value";
+const char* TestJSONObject::kFloat4ValueName = "float4Value";
+const char* TestJSONObject::kMatrix4ValueName = "matrix4Value";
+const char* TestJSONObject::kIntegerValueName = "integerValue";
+const char* TestJSONObject::kBooleanValueName = "booleanValue";
+const char* TestJSONObject::kStringValueName = "stringValue";
+const char* TestJSONObject::kTransformValueName = "transformValue";
+const char* TestJSONObject::kMaterialValueName = "materialValue";
+const char* TestJSONObject::kOptionalFloatValueName = "optionalFloatValue";
+const char* TestJSONObject::kOptionalFloat2ValueName = "optionalFloat2Value";
+const char* TestJSONObject::kOptionalFloat3ValueName = "optionalFloat3Value";
+const char* TestJSONObject::kOptionalFloat4ValueName = "optionalFloat4Value";
+const char* TestJSONObject::kOptionalMatrix4ValueName = "optionalMatrix4Value";
+const char* TestJSONObject::kOptionalIntegerValueName = "optionalIntegerValue";
+const char* TestJSONObject::kOptionalStringValueName = "optionalStringValue";
+const char* TestJSONObject::kOptionalBooleanValueName = "optionalBooleanValue";
+
+} // anonymous namespace
+
+class JSONObjectTest : public testing::Test {
+ protected:
+ JSONObjectTest()
+ : class_manager_(g_service_locator),
+ object_manager_(g_service_locator),
+ class_register_(g_service_locator) {
+ }
+
+ virtual void SetUp();
+ virtual void TearDown();
+
+ Pack* pack() { return pack_; }
+
+ private:
+ ServiceDependency<ClassManager> class_manager_;
+ ServiceDependency<ObjectManager> object_manager_;
+ ClassManager::Register<TestJSONObject> class_register_;
+ Pack* pack_;
+};
+
+void JSONObjectTest::SetUp() {
+ pack_ = object_manager_->CreatePack();
+}
+
+void JSONObjectTest::TearDown() {
+ object_manager_->DestroyPack(pack_);
+}
+
+// Creates a JSONObject, tests basic properties.
+TEST_F(JSONObjectTest, BasicTest) {
+ TestJSONObject* object = pack()->Create<TestJSONObject>();
+ ASSERT_TRUE(object != NULL);
+ EXPECT_TRUE(object->IsA(TestJSONObject::GetApparentClass()));
+ EXPECT_TRUE(object->IsA(JSONObject::GetApparentClass()));
+ EXPECT_TRUE(object->IsA(ParamObject::GetApparentClass()));
+
+ // Test everything has its default value.
+ EXPECT_EQ(object->float_value(), 0.0f);
+ EXPECT_EQ(object->float2_value(), Float2(0.0f, 0.0f));
+ EXPECT_EQ(object->float2_value(), Float2(0.0f, 0.0f));
+ EXPECT_EQ(object->float3_value(), Float3(0.0f, 0.0f, 0.0f));
+ EXPECT_EQ(object->float4_value(), Float4(0.0f, 0.0f, 0.0f, 0.0f));
+ EXPECT_EQ(object->matrix4_value(), Matrix4::identity());
+ EXPECT_EQ(object->integer_value(), 0);
+ EXPECT_FALSE(object->boolean_value());
+ EXPECT_EQ(object->string_value(), "");
+ EXPECT_TRUE(object->transform_value() == NULL);
+ EXPECT_TRUE(object->material_value() == NULL);
+
+ // Test that all the values got created by serializing it.
+ {
+ StringWriter output(StringWriter::LF);
+ JsonWriter json_writer(&output, 2);
+ object->Serialize(&json_writer);
+ json_writer.Close();
+ // There is an assumption here that objects will be serialized in this
+ // order.
+ static const char *kExpected =
+ "\"object\": {\n"
+ " \"booleanValue\": false,\n"
+ " \"float2Value\": [0,0],\n"
+ " \"float3Value\": [0,0,0],\n"
+ " \"float4Value\": [0,0,0,0],\n"
+ " \"floatValue\": 0,\n"
+ " \"integerValue\": 0,\n"
+ " \"materialValue\": null,\n"
+ " \"matrix4Value\": [[1,0,0,0],[0,1,0,0],[0,0,1,0],[0,0,0,1]],\n"
+ " \"stringValue\": \"\",\n"
+ " \"transformValue\": null\n"
+ "}\n";
+ EXPECT_EQ(kExpected, output.ToString());
+ }
+}
+
+TEST_F(JSONObjectTest, SetTest) {
+ TestJSONObject* object = pack()->Create<TestJSONObject>();
+ ASSERT_TRUE(object != NULL);
+ // Test setting the values.
+ Matrix4 test_matrix(Vector4(1.0f, 2.0f, 3.0f, 4.0f),
+ Vector4(2.0f, 2.0f, 8.0f, 9.0f),
+ Vector4(3.0f, 5.0f, 3.0f, 10.0f),
+ Vector4(4.0f, 6.0f, 7.0f, 4.0f));
+ Matrix4 test_matrix2(Vector4(11.0f, 12.0f, 13.0f, 14.0f),
+ Vector4(12.0f, 12.0f, 18.0f, 19.0f),
+ Vector4(13.0f, 15.0f, 13.0f, 110.0f),
+ Vector4(14.0f, 16.0f, 17.0f, 14.0f));
+
+ object->set_float_value(1.2f);
+ object->set_float2_value(Float2(3.4f, 5.6f));
+ object->set_float3_value(Float3(7.8f, 9.0f, 10.0f));
+ object->set_float4_value(Float4(11.0f, 12.0f, 13.0f, 14.0f));
+ object->set_matrix4_value(test_matrix);
+ object->set_integer_value(123);
+ object->set_boolean_value(true);
+ object->set_string_value("test");
+ object->set_optional_float_value(15.0f);
+ object->set_optional_float2_value(Float2(14.0f, 15.0f));
+ object->set_optional_float3_value(Float3(13.0f, 14.0f, 15.0f));
+ object->set_optional_float4_value(Float4(12.0f, 13.0f, 14.0f, 15.0f));
+ object->set_optional_matrix4_value(test_matrix2);
+ object->set_optional_integer_value(456);
+ object->set_optional_string_value("hello");
+ object->set_optional_boolean_value(false);
+
+ Transform* transform = pack()->Create<Transform>();
+ Material* material = pack()->Create<Material>();
+ ASSERT_TRUE(transform != NULL);
+ ASSERT_TRUE(material != NULL);
+
+ object->set_transform_value(transform);
+ object->set_material_value(material);
+
+ // Check the new values
+ EXPECT_EQ(object->float_value(), 1.2f);
+ EXPECT_EQ(object->float2_value(), Float2(3.4f, 5.6f));
+ EXPECT_EQ(object->float3_value(), Float3(7.8f, 9.0f, 10.0f));
+ EXPECT_EQ(object->float4_value(), Float4(11.0f, 12.0f, 13.0f, 14.0f));
+ EXPECT_EQ(object->matrix4_value(), test_matrix);
+ EXPECT_EQ(object->integer_value(), 123);
+ EXPECT_TRUE(object->boolean_value());
+ EXPECT_EQ(object->string_value(), "test");
+ EXPECT_TRUE(object->transform_value() == transform);
+ EXPECT_TRUE(object->material_value() == material);
+
+ // See that they got set by serializing them.
+ {
+ StringWriter output(StringWriter::LF);
+ JsonWriter json_writer(&output, 2);
+ object->Serialize(&json_writer);
+ json_writer.Close();
+
+ char buffer[1000];
+ static const char* kExpected =
+ "\"object\": {\n"
+ " \"booleanValue\": true,\n"
+ " \"float2Value\": [3.4,5.6],\n"
+ " \"float3Value\": [7.8,9,10],\n"
+ " \"float4Value\": [11,12,13,14],\n"
+ " \"floatValue\": 1.2,\n"
+ " \"integerValue\": 123,\n"
+ " \"materialValue\": {\"ref\":%d},\n"
+ " \"matrix4Value\": [[1,2,3,4],[2,2,8,9],[3,5,3,10],[4,6,7,4]],\n"
+ " \"optionalBooleanValue\": false,\n"
+ " \"optionalFloat2Value\": [14,15],\n"
+ " \"optionalFloat3Value\": [13,14,15],\n"
+ " \"optionalFloat4Value\": [12,13,14,15],\n"
+ " \"optionalFloatValue\": 15,\n"
+ " \"optionalIntegerValue\": 456,\n"
+ " \"optionalMatrix4Value\": "
+ "[[11,12,13,14],[12,12,18,19],[13,15,13,110],[14,16,17,14]],\n"
+ " \"optionalStringValue\": \"hello\",\n"
+ " \"stringValue\": \"test\",\n"
+ " \"transformValue\": {\"ref\":%d}\n"
+ "}\n";
+ sprintf(buffer, kExpected, material->id(), transform->id()); // NOLINT
+ EXPECT_EQ(static_cast<const char*>(buffer), output.ToString());
+ }
+}
+
+} // namespace o3d
+