axmol/extensions/cocostudio/WidgetReader/UserCameraReader/UserCameraReader.cpp

479 lines
15 KiB
C++
Raw Normal View History

/****************************************************************************
Copyright (c) 2014 cocos2d-x.org
2021-12-25 10:04:45 +08:00
2022-10-01 16:24:52 +08:00
https://axmolengine.github.io/
2021-12-25 10:04:45 +08:00
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
2021-12-25 10:04:45 +08:00
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
2021-12-25 10:04:45 +08:00
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#include "base/CCDirector.h"
#include "2d/CCCamera.h"
#include "platform/CCFileUtils.h"
#include "WidgetReader/UserCameraReader/UserCameraReader.h"
#include "CSParseBinary_generated.h"
#include "CSParse3DBinary_generated.h"
#include "FlatBuffersSerialize.h"
#include "WidgetReader/Node3DReader/Node3DReader.h"
#include "WidgetReader/GameNode3DReader/GameNode3DReader.h"
#include "flatbuffers/flatbuffers.h"
USING_NS_AX;
using namespace flatbuffers;
namespace cocostudio
{
2021-12-25 10:04:45 +08:00
IMPLEMENT_CLASS_NODE_READER_INFO(UserCameraReader)
UserCameraReader::UserCameraReader() {}
UserCameraReader::~UserCameraReader() {}
static UserCameraReader* _instanceUserCameraReader = nullptr;
UserCameraReader* UserCameraReader::getInstance()
{
if (!_instanceUserCameraReader)
{
2021-12-25 10:04:45 +08:00
_instanceUserCameraReader = new UserCameraReader();
}
2021-12-25 10:04:45 +08:00
return _instanceUserCameraReader;
}
void UserCameraReader::purge()
{
2022-07-15 19:17:01 +08:00
AX_SAFE_DELETE(_instanceUserCameraReader);
2021-12-25 10:04:45 +08:00
}
void UserCameraReader::destroyInstance()
{
2022-07-15 19:17:01 +08:00
AX_SAFE_DELETE(_instanceUserCameraReader);
2021-12-25 10:04:45 +08:00
}
Vec2 UserCameraReader::getVec2Attribute(pugi::xml_attribute attribute) const
{
if (!attribute)
return Vec2::ZERO;
Vec2 ret;
std::string attriname;
while (attribute)
{
2021-12-25 10:04:45 +08:00
attriname = attribute.name();
2021-12-31 15:49:45 +08:00
std::string_view value = attribute.value();
2021-12-25 10:04:45 +08:00
if (attriname == "X")
{
2021-12-31 15:49:45 +08:00
ret.x = atof(value.data());
}
2021-12-25 10:04:45 +08:00
else if (attriname == "Y")
{
2021-12-31 15:49:45 +08:00
ret.y = atof(value.data());
}
2021-12-25 10:04:45 +08:00
attribute = attribute.next_attribute();
}
2021-12-25 10:04:45 +08:00
return ret;
}
Offset<Table> UserCameraReader::createOptionsWithFlatBuffers(pugi::xml_node objectData,
flatbuffers::FlatBufferBuilder* builder)
{
auto temp = Node3DReader::getInstance()->createOptionsWithFlatBuffers(objectData, builder);
auto node3DOptions = *(Offset<Node3DOption>*)(&temp);
float fov = 60.f;
unsigned int cameraFlag = 0;
bool skyBoxEnabled = false;
bool skyBoxValid = true;
std::string attriname;
auto attribute = objectData.first_attribute();
while (attribute)
{
2021-12-25 10:04:45 +08:00
attriname = attribute.name();
2021-12-31 15:49:45 +08:00
std::string_view value = attribute.value();
2021-12-25 10:04:45 +08:00
if (attriname == "Fov")
{
2021-12-31 15:49:45 +08:00
fov = atof(value.data());
2021-12-25 10:04:45 +08:00
}
else if (attriname == "UserCameraFlagMode")
{
if (cameraFlag == 0)
2015-08-26 10:22:20 +08:00
{
2021-12-25 10:04:45 +08:00
if (value == "DEFAULT")
cameraFlag = 1;
else if (value == "USER1")
cameraFlag = 1 << 1;
else if (value == "USER2")
cameraFlag = 1 << 2;
else if (value == "USER3")
cameraFlag = 1 << 3;
else if (value == "USER4")
cameraFlag = 1 << 4;
2015-08-26 10:22:20 +08:00
}
}
2021-12-25 10:04:45 +08:00
else if (attriname == "CameraFlagData")
{
2021-12-31 15:49:45 +08:00
int flag = atoi(value.data());
2021-12-25 10:04:45 +08:00
if (flag != 0)
cameraFlag = flag;
}
else if (attriname == "SkyBoxEnabled")
{
skyBoxEnabled = (value == "True") ? true : false;
}
else if (attriname == "SkyBoxValid")
{
skyBoxValid = (value == "True") ? true : false;
}
attribute = attribute.next_attribute();
}
if (!skyBoxValid)
skyBoxEnabled = false;
Vec2 clipPlane(1, 1000);
2021-12-25 10:04:45 +08:00
std::string leftPath;
std::string leftPlistFile;
int leftResourceType = 0;
2015-08-26 10:22:20 +08:00
2021-12-25 10:04:45 +08:00
std::string rightPath;
std::string rightPlistFile;
int rightResourceType = 0;
2015-07-03 17:52:42 +08:00
2021-12-25 10:04:45 +08:00
std::string upPath;
std::string upPlistFile;
int upResourceType = 0;
2015-07-03 17:52:42 +08:00
2021-12-25 10:04:45 +08:00
std::string downPath;
std::string downPlistFile;
int downResourceType = 0;
2015-07-03 17:52:42 +08:00
2021-12-25 10:04:45 +08:00
std::string forwardPath;
std::string forwardPlistFile;
int forwardResourceType = 0;
2015-07-03 17:52:42 +08:00
2021-12-25 10:04:45 +08:00
std::string backPath;
std::string backPlistFile;
int backResourceType = 0;
2015-07-03 17:52:42 +08:00
2021-12-25 10:04:45 +08:00
// FileData
auto child = objectData.first_child();
while (child)
{
2021-12-31 15:49:45 +08:00
std::string_view name = child.name();
2015-07-03 17:52:42 +08:00
2021-12-25 10:04:45 +08:00
if (name == "ClipPlane")
{
2021-12-25 10:04:45 +08:00
attribute = child.first_attribute();
clipPlane = getVec2Attribute(attribute);
}
else if (name == "LeftImage")
{
attribute = child.first_attribute();
while (attribute)
2015-07-03 17:52:42 +08:00
{
2021-12-25 10:04:45 +08:00
name = attribute.name();
2021-12-31 15:49:45 +08:00
std::string_view value = attribute.value();
2015-07-03 17:52:42 +08:00
2021-12-25 10:04:45 +08:00
if (name == "Path")
2015-07-03 17:52:42 +08:00
{
2021-12-25 10:04:45 +08:00
leftPath = value;
2015-07-03 17:52:42 +08:00
}
2021-12-25 10:04:45 +08:00
else if (name == "Type")
2015-07-03 17:52:42 +08:00
{
2021-12-25 10:04:45 +08:00
leftResourceType = getResourceType(value);
2015-07-03 17:52:42 +08:00
}
2021-12-25 10:04:45 +08:00
else if (name == "Plist")
{
leftPlistFile = value;
}
attribute = attribute.next_attribute();
2015-07-03 17:52:42 +08:00
}
2021-12-25 10:04:45 +08:00
if (leftResourceType == 1)
2015-07-03 17:52:42 +08:00
{
2021-12-25 10:04:45 +08:00
FlatBuffersSerialize* fbs = FlatBuffersSerialize::getInstance();
fbs->_textures.emplace_back(builder->CreateString(leftPlistFile));
2021-12-25 10:04:45 +08:00
}
}
else if (name == "RightImage")
{
attribute = child.first_attribute();
2015-07-03 17:52:42 +08:00
2021-12-25 10:04:45 +08:00
while (attribute)
{
name = attribute.name();
2021-12-31 15:49:45 +08:00
std::string_view value = attribute.value();
2021-12-25 10:04:45 +08:00
if (name == "Path")
2015-07-03 17:52:42 +08:00
{
2021-12-25 10:04:45 +08:00
rightPath = value;
2015-07-03 17:52:42 +08:00
}
2021-12-25 10:04:45 +08:00
else if (name == "Type")
{
rightResourceType = getResourceType(value);
}
else if (name == "Plist")
2015-07-03 17:52:42 +08:00
{
2021-12-25 10:04:45 +08:00
rightPlistFile = value;
2015-07-03 17:52:42 +08:00
}
2021-12-25 10:04:45 +08:00
attribute = attribute.next_attribute();
2015-07-03 17:52:42 +08:00
}
2021-12-25 10:04:45 +08:00
if (rightResourceType == 1)
2015-07-03 17:52:42 +08:00
{
2021-12-25 10:04:45 +08:00
FlatBuffersSerialize* fbs = FlatBuffersSerialize::getInstance();
fbs->_textures.emplace_back(builder->CreateString(rightPlistFile));
2021-12-25 10:04:45 +08:00
}
}
else if (name == "UpImage")
{
attribute = child.first_attribute();
2015-07-03 17:52:42 +08:00
2021-12-25 10:04:45 +08:00
while (attribute)
{
name = attribute.name();
2021-12-31 15:49:45 +08:00
std::string_view value = attribute.value();
2021-12-25 10:04:45 +08:00
if (name == "Path")
2015-07-03 17:52:42 +08:00
{
2021-12-25 10:04:45 +08:00
upPath = value;
2015-07-03 17:52:42 +08:00
}
2021-12-25 10:04:45 +08:00
else if (name == "Type")
{
upResourceType = getResourceType(value);
}
else if (name == "Plist")
2015-07-03 17:52:42 +08:00
{
2021-12-25 10:04:45 +08:00
upPlistFile = value;
2015-07-03 17:52:42 +08:00
}
2021-12-25 10:04:45 +08:00
attribute = attribute.next_attribute();
}
if (upResourceType == 1)
{
FlatBuffersSerialize* fbs = FlatBuffersSerialize::getInstance();
fbs->_textures.emplace_back(builder->CreateString(upPlistFile));
2015-07-03 17:52:42 +08:00
}
2021-12-25 10:04:45 +08:00
}
else if (name == "DownImage")
{
attribute = child.first_attribute();
while (attribute)
2015-07-03 17:52:42 +08:00
{
2021-12-25 10:04:45 +08:00
name = attribute.name();
2021-12-31 15:49:45 +08:00
std::string_view value = attribute.value();
2015-07-03 17:52:42 +08:00
2021-12-25 10:04:45 +08:00
if (name == "Path")
2015-07-03 17:52:42 +08:00
{
2021-12-25 10:04:45 +08:00
downPath = value;
2015-07-03 17:52:42 +08:00
}
2021-12-25 10:04:45 +08:00
else if (name == "Type")
{
downResourceType = getResourceType(value);
}
else if (name == "Plist")
2015-07-03 17:52:42 +08:00
{
2021-12-25 10:04:45 +08:00
downPlistFile = value;
2015-07-03 17:52:42 +08:00
}
2021-12-25 10:04:45 +08:00
attribute = attribute.next_attribute();
2015-07-03 17:52:42 +08:00
}
2021-12-25 10:04:45 +08:00
if (downResourceType == 1)
2015-07-03 17:52:42 +08:00
{
2021-12-25 10:04:45 +08:00
FlatBuffersSerialize* fbs = FlatBuffersSerialize::getInstance();
fbs->_textures.emplace_back(builder->CreateString(downPlistFile));
2021-12-25 10:04:45 +08:00
}
}
else if (name == "ForwardImage")
{
attribute = child.first_attribute();
while (attribute)
{
name = attribute.name();
2021-12-31 15:49:45 +08:00
std::string_view value = attribute.value();
2015-07-03 17:52:42 +08:00
2021-12-25 10:04:45 +08:00
if (name == "Path")
2015-07-03 17:52:42 +08:00
{
2021-12-25 10:04:45 +08:00
forwardPath = value;
2015-07-03 17:52:42 +08:00
}
2021-12-25 10:04:45 +08:00
else if (name == "Type")
2015-07-03 17:52:42 +08:00
{
2021-12-25 10:04:45 +08:00
forwardResourceType = getResourceType(value);
2015-07-03 17:52:42 +08:00
}
2021-12-25 10:04:45 +08:00
else if (name == "Plist")
{
forwardPlistFile = value;
}
attribute = attribute.next_attribute();
}
if (forwardResourceType == 1)
{
FlatBuffersSerialize* fbs = FlatBuffersSerialize::getInstance();
fbs->_textures.emplace_back(builder->CreateString(forwardPlistFile));
2015-07-03 17:52:42 +08:00
}
2021-12-25 10:04:45 +08:00
}
else if (name == "BackImage")
{
attribute = child.first_attribute();
while (attribute)
2015-07-03 17:52:42 +08:00
{
2021-12-25 10:04:45 +08:00
name = attribute.name();
2021-12-31 15:49:45 +08:00
std::string_view value = attribute.value();
2015-07-03 17:52:42 +08:00
2021-12-25 10:04:45 +08:00
if (name == "Path")
2015-07-03 17:52:42 +08:00
{
2021-12-25 10:04:45 +08:00
backPath = value;
2015-07-03 17:52:42 +08:00
}
2021-12-25 10:04:45 +08:00
else if (name == "Type")
2015-07-03 17:52:42 +08:00
{
2021-12-25 10:04:45 +08:00
backResourceType = getResourceType(value);
2015-07-03 17:52:42 +08:00
}
2021-12-25 10:04:45 +08:00
else if (name == "Plist")
{
backPlistFile = value;
}
attribute = attribute.next_attribute();
}
if (backResourceType == 1)
{
FlatBuffersSerialize* fbs = FlatBuffersSerialize::getInstance();
fbs->_textures.emplace_back(builder->CreateString(backPlistFile));
2015-07-03 17:52:42 +08:00
}
}
2021-12-25 10:04:45 +08:00
child = child.next_sibling();
}
2021-12-25 10:04:45 +08:00
auto options = CreateUserCameraOptions(
*builder, node3DOptions, fov, clipPlane.x, clipPlane.y, cameraFlag, skyBoxEnabled,
CreateResourceData(*builder, builder->CreateString(leftPath), builder->CreateString(leftPlistFile),
leftResourceType),
CreateResourceData(*builder, builder->CreateString(rightPath), builder->CreateString(rightPlistFile),
rightResourceType),
CreateResourceData(*builder, builder->CreateString(upPath), builder->CreateString(upPlistFile), upResourceType),
CreateResourceData(*builder, builder->CreateString(downPath), builder->CreateString(downPlistFile),
downResourceType),
CreateResourceData(*builder, builder->CreateString(forwardPath), builder->CreateString(forwardPlistFile),
forwardResourceType),
CreateResourceData(*builder, builder->CreateString(backPath), builder->CreateString(backPlistFile),
backResourceType));
return *(Offset<Table>*)(&options);
}
2022-08-08 18:02:17 +08:00
void UserCameraReader::setPropsWithFlatBuffers(ax::Node* node, const flatbuffers::Table* userCameraDOptions)
2021-12-25 10:04:45 +08:00
{
auto options = (UserCameraOptions*)userCameraDOptions;
Camera* camera = static_cast<Camera*>(node);
int cameraFlag = options->cameraFlag();
camera->setCameraFlag((CameraFlag)cameraFlag);
auto node3DReader = Node3DReader::getInstance();
node3DReader->setPropsWithFlatBuffers(node, (Table*)(options->node3DOption()));
bool skyBoxEnabled = options->skyBoxEnabled() != 0;
if (skyBoxEnabled)
{
std::string leftFileData = options->leftFileData()->path()->c_str();
std::string rightFileData = options->rightFileData()->path()->c_str();
std::string upFileData = options->upFileData()->path()->c_str();
std::string downFileData = options->downFileData()->path()->c_str();
std::string forwardFileData = options->forwardFileData()->path()->c_str();
std::string backFileData = options->backFileData()->path()->c_str();
FileUtils* fileUtils = FileUtils::getInstance();
if (fileUtils->isFileExist(leftFileData) && fileUtils->isFileExist(rightFileData) &&
fileUtils->isFileExist(upFileData) && fileUtils->isFileExist(downFileData) &&
fileUtils->isFileExist(forwardFileData) && fileUtils->isFileExist(backFileData))
2015-07-03 17:52:42 +08:00
{
2021-12-25 10:04:45 +08:00
CameraBackgroundSkyBoxBrush* brush = CameraBackgroundSkyBoxBrush::create(
leftFileData, rightFileData, upFileData, downFileData, forwardFileData, backFileData);
camera->setBackgroundBrush(brush);
2015-08-25 13:20:19 +08:00
}
else
{
if (GameNode3DReader::getSceneBrushInstance() != nullptr)
camera->setBackgroundBrush(GameNode3DReader::getSceneBrushInstance());
2015-07-03 17:52:42 +08:00
}
}
2021-12-25 10:04:45 +08:00
else
{
2021-12-25 10:04:45 +08:00
if (GameNode3DReader::getSceneBrushInstance() != nullptr)
camera->setBackgroundBrush(GameNode3DReader::getSceneBrushInstance());
}
2021-12-25 10:04:45 +08:00
}
Node* UserCameraReader::createNodeWithFlatBuffers(const flatbuffers::Table* userCameraDOptions)
{
auto options = (UserCameraOptions*)userCameraDOptions;
float fov = options->fov();
float nearClip = options->nearClip();
float farClip = options->farClip();
auto size = Director::getInstance()->getWinSize();
Camera* camera = Camera::createPerspective(fov, size.width / size.height, nearClip, farClip);
setPropsWithFlatBuffers(camera, userCameraDOptions);
2015-07-03 17:52:42 +08:00
2021-12-25 10:04:45 +08:00
return camera;
}
2021-12-31 15:49:45 +08:00
int UserCameraReader::getResourceType(std::string_view key)
2021-12-25 10:04:45 +08:00
{
if (key == "Normal" || key == "Default")
2015-07-03 17:52:42 +08:00
{
2021-12-25 10:04:45 +08:00
return 0;
}
2015-07-03 17:52:42 +08:00
2021-12-25 10:04:45 +08:00
FlatBuffersSerialize* fbs = FlatBuffersSerialize::getInstance();
if (fbs->_isSimulator)
{
if (key == "MarkedSubImage")
2015-07-03 17:52:42 +08:00
{
2021-12-25 10:04:45 +08:00
return 0;
2015-07-03 17:52:42 +08:00
}
}
2021-12-25 10:04:45 +08:00
return 1;
}
2021-12-25 10:04:45 +08:00
} // namespace cocostudio