292 lines
8.0 KiB
C++
292 lines
8.0 KiB
C++
#include "entities/ConeWaveComponent.h"
|
|
|
|
#include "common/SpdLogger.h"
|
|
#include "common/RecourceHelper.h"
|
|
#include "effects/ConeWave.h"
|
|
#include "utils/StringUtils.h"
|
|
#include "utils/TimeAction.h"
|
|
#include "entities/Entity.h"
|
|
#include "workspace/WorkSpace.h"
|
|
#include "workspace/Timestep.h"
|
|
#include "workspace/LampStatus.h"
|
|
|
|
|
|
ConeWaveComponent::ConeWaveComponent(SceneComponent* parent)
|
|
: SceneComponent(parent) {
|
|
coneWave_ = new ConeWave();
|
|
coneWave_->InitGeode();
|
|
|
|
colorMap_[1] = osg::Vec4(1.0f, 1.0f, 1.0f, 1.0f);
|
|
colorMap_[2] = osg::Vec4(1.0f, 1.0f, 1.0f, 1.0f);
|
|
colorMap_[3] = osg::Vec4(1.0f, 1.0f, 1.0f, 1.0f);
|
|
}
|
|
|
|
ConeWaveComponent::~ConeWaveComponent() {
|
|
// coneWave_->Destory();
|
|
}
|
|
|
|
std::string ConeWaveComponent::GetTypeName() {
|
|
return "ConeWaveComponent";
|
|
}
|
|
|
|
bool ConeWaveComponent::SetAttribute(const char* name, const char* value) {
|
|
if (0 == strcmp("color1" ,name)) {
|
|
AddColor(1, StringUtils::StringToVec4(value));
|
|
} else if (0 == strcmp("color2" ,name)) {
|
|
AddColor(2, StringUtils::StringToVec4(value));
|
|
}else if (0 == strcmp("color3" ,name)) {
|
|
AddColor(3, StringUtils::StringToVec4(value));
|
|
} else if (0 == strcmp("radius", name)) {
|
|
SetRadius(atof(value));
|
|
} else if (0 == strcmp("height", name)) {
|
|
SetHeight(atof(value));
|
|
} else if (0 == strcmp("event", name)) {
|
|
SetTimeAction(value);
|
|
} else if (0 == strcmp("levelCount", name)) {
|
|
SetLevelCount(atof(value));
|
|
} else if (0 == strcmp("levelHeihgt", name)) {
|
|
SetLevelHeight(atof(value));
|
|
} else if (0 == strcmp("waveRadius", name)) {
|
|
SetWaveRadius(atof(value));
|
|
} else if (0 == strcmp("waveSpeed", name)) {
|
|
SetWaveSpeed(atof(value));
|
|
} else if (0 == strcmp("waveCount", name)) {
|
|
SetWaveCount(atoi(value));
|
|
} else if (0 == strcmp("waveColor", name)) {
|
|
SetWaveColor(StringUtils::StringToVec4(value));
|
|
}
|
|
|
|
return SceneComponent::SetAttribute(name, value);
|
|
}
|
|
|
|
bool ConeWaveComponent::SaveAttribute(tinyxml2::XMLElement* element) {
|
|
if (colorMap_.find(1) != colorMap_.end()) {
|
|
element->SetAttribute("color1", StringUtils::Vec4ToString(colorMap_[1]).c_str());
|
|
} else {
|
|
element->SetAttribute("color1", StringUtils::Vec4ToString(osg::Vec4(1.0f, 1.0f, 1.0f, 1.0f)).c_str());
|
|
}
|
|
if (colorMap_.find(2) != colorMap_.end()) {
|
|
element->SetAttribute("color2", StringUtils::Vec4ToString(colorMap_[2]).c_str());
|
|
} else {
|
|
element->SetAttribute("color2", StringUtils::Vec4ToString(osg::Vec4(1.0f, 1.0f, 1.0f, 1.0f)).c_str());
|
|
}
|
|
if (colorMap_.find(3) != colorMap_.end()) {
|
|
element->SetAttribute("color3", StringUtils::Vec4ToString(colorMap_[3]).c_str());
|
|
} else {
|
|
element->SetAttribute("color3", StringUtils::Vec4ToString(osg::Vec4(1.0f, 1.0f, 1.0f, 1.0f)).c_str());
|
|
}
|
|
element->SetAttribute("radius", std::to_string(GetRadius()).c_str());
|
|
element->SetAttribute("height", std::to_string(GetHeight()).c_str());
|
|
element->SetAttribute("levelCount", std::to_string(GetLevelCount()).c_str());
|
|
element->SetAttribute("levelHeihgt", std::to_string(GetLevelHeight()).c_str());
|
|
element->SetAttribute("event", timeAction_ == nullptr ? "" : timeAction_->GetPath().toStdString().c_str());
|
|
return SceneComponent::SaveAttribute(element);
|
|
}
|
|
|
|
void ConeWaveComponent::Begin() {
|
|
|
|
}
|
|
|
|
void ConeWaveComponent::Update(double dt) {
|
|
UpdateEvent();
|
|
// 更新雷达扫描波效果
|
|
if (coneWave_.valid()) {
|
|
coneWave_->Render(dt);
|
|
}
|
|
}
|
|
|
|
void ConeWaveComponent::SetHeight(float height) {
|
|
coneWave_->SetHeight(height);
|
|
if (nullptr != mt_) {
|
|
mt_->setMatrix(osg::Matrix::translate(osg::Vec3(0.0f, 0.0f, -coneWave_->GetHeght() * 0.75f)));
|
|
}
|
|
}
|
|
|
|
float ConeWaveComponent::GetHeight() const {
|
|
return coneWave_->GetHeght();
|
|
}
|
|
|
|
void ConeWaveComponent::SetRadius(float radius) {
|
|
coneWave_->SetRadius(radius);
|
|
}
|
|
|
|
float ConeWaveComponent::GetRadius() const {
|
|
return coneWave_->GetRadius();
|
|
}
|
|
|
|
void ConeWaveComponent::SetLevelCount(int count) {
|
|
coneWave_->SetLevelCount(count);
|
|
}
|
|
|
|
float ConeWaveComponent::GetLevelCount() const {
|
|
return coneWave_->GetLevelCount();
|
|
}
|
|
|
|
void ConeWaveComponent::SetLevelHeight(float height) {
|
|
coneWave_->SetLevelHeight(height);
|
|
}
|
|
|
|
float ConeWaveComponent::GetLevelHeight() const {
|
|
return coneWave_->GetLevelHeihgt();
|
|
}
|
|
|
|
void ConeWaveComponent::SetBaseColor(const osg::Vec4& color) {
|
|
coneWave_->SetBaseColor(color);
|
|
}
|
|
|
|
const osg::Vec4 ConeWaveComponent::GetBaseColor() const {
|
|
return coneWave_->GetBaseColor();
|
|
}
|
|
|
|
void ConeWaveComponent::SetColor1(const osg::Vec4& color) {
|
|
colorMap_[1] = color;
|
|
}
|
|
|
|
const osg::Vec4 ConeWaveComponent::GetColor1() const {
|
|
const auto color = colorMap_.find(1);
|
|
return color->second;
|
|
}
|
|
|
|
void ConeWaveComponent::SetColor2(const osg::Vec4& color) {
|
|
colorMap_[2] = color;
|
|
}
|
|
|
|
const osg::Vec4 ConeWaveComponent::GetColor2() const {
|
|
const auto color = colorMap_.find(2);
|
|
return color->second;
|
|
}
|
|
|
|
void ConeWaveComponent::SetColor3(const osg::Vec4& color) {
|
|
colorMap_[3] = color;
|
|
}
|
|
|
|
const osg::Vec4 ConeWaveComponent::GetColor3() const {
|
|
const auto color = colorMap_.find(3);
|
|
return color->second;
|
|
}
|
|
|
|
void ConeWaveComponent::SetTimeAction(const QString& path) {
|
|
if (nullptr != timeAction_) {
|
|
timeAction_->deleteLater();
|
|
}
|
|
|
|
timeAction_ = TimeAction::LoadFromFile(path);
|
|
}
|
|
|
|
void ConeWaveComponent::AddToRender() {
|
|
Initialize();
|
|
|
|
SceneComponent::AddToRender();
|
|
}
|
|
|
|
void ConeWaveComponent::Initialize() {
|
|
mt_ = new osg::MatrixTransform;
|
|
mt_->addChild(coneWave_);
|
|
mt_->setMatrix(osg::Matrix::translate(osg::Vec3(0.0f, 0.0f, -coneWave_->GetHeght() * 0.75f)));
|
|
}
|
|
|
|
void ConeWaveComponent::UpdateEvent() {
|
|
if (nullptr == timeAction_) {
|
|
return;
|
|
}
|
|
WorkSpace* workspace = GetEntity()->GetWorkspace();
|
|
if (nullptr == workspace) {
|
|
LOG_WARN("workspace is nullptr");
|
|
return;
|
|
}
|
|
|
|
LampStatus* lampStatus = workspace->GetLampStatus();
|
|
if (nullptr == lampStatus) {
|
|
return;
|
|
}
|
|
|
|
if (currentStatus_ == lampStatus->GetCurrent()) {
|
|
return;
|
|
}
|
|
currentStatus_ = lampStatus->GetCurrent();
|
|
if (colorMap_.find(currentStatus_) == colorMap_.end()) {
|
|
coneWave_->setNodeMask(0x0);
|
|
return;
|
|
}
|
|
|
|
osg::Vec4& color = colorMap_[currentStatus_];
|
|
coneWave_->SetBaseColor(color);
|
|
coneWave_->setNodeMask(0xff);
|
|
|
|
|
|
Timestep* timeStep = workspace->GetTimestep();
|
|
if (nullptr == timeStep) {
|
|
LOG_WARN("timeStep is nullptr");
|
|
return;
|
|
}
|
|
|
|
double dt = timeStep->GetCurrent();
|
|
int value = timeAction_->GetValue(dt);
|
|
if (-1 == value) {
|
|
return;
|
|
}
|
|
|
|
|
|
coneWave_->setNodeMask(value == 0 ? 0x0 : 0xff);
|
|
|
|
}
|
|
|
|
void ConeWaveComponent::AddColor(int32_t status, const osg::Vec4& color) {
|
|
colorMap_[status] = color;
|
|
}
|
|
|
|
// 雷达扫描波效果相关方法实现
|
|
void ConeWaveComponent::SetWaveRadius(float radius) {
|
|
if (coneWave_.valid()) {
|
|
coneWave_->SetWaveRadius(radius);
|
|
}
|
|
}
|
|
|
|
float ConeWaveComponent::GetWaveRadius() const {
|
|
if (coneWave_.valid()) {
|
|
return coneWave_->GetWaveRadius();
|
|
}
|
|
return 0.0f;
|
|
}
|
|
|
|
void ConeWaveComponent::SetWaveSpeed(float speed) {
|
|
if (coneWave_.valid()) {
|
|
coneWave_->SetWaveSpeed(speed);
|
|
}
|
|
}
|
|
|
|
float ConeWaveComponent::GetWaveSpeed() const {
|
|
if (coneWave_.valid()) {
|
|
return coneWave_->GetWaveSpeed();
|
|
}
|
|
return 0.0f;
|
|
}
|
|
|
|
void ConeWaveComponent::SetWaveCount(int count) {
|
|
if (coneWave_.valid()) {
|
|
coneWave_->SetWaveCount(count);
|
|
}
|
|
}
|
|
|
|
int ConeWaveComponent::GetWaveCount() const {
|
|
if (coneWave_.valid()) {
|
|
return coneWave_->GetWaveCount();
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
void ConeWaveComponent::SetWaveColor(const osg::Vec4& color) {
|
|
if (coneWave_.valid()) {
|
|
coneWave_->SetWaveColor(color);
|
|
}
|
|
}
|
|
|
|
const osg::Vec4& ConeWaveComponent::GetWaveColor() const {
|
|
static osg::Vec4 defaultColor(0.0f, 0.5f, 1.0f, 0.8f);
|
|
if (coneWave_.valid()) {
|
|
return coneWave_->GetWaveColor();
|
|
}
|
|
return defaultColor;
|
|
}
|
|
|