Skip to content

Commit 6d9a35d

Browse files
committed
添加多表情设置#84; 添加画布大小获取#85
1 parent 16e351e commit 6d9a35d

File tree

10 files changed

+272
-98
lines changed

10 files changed

+272
-98
lines changed

Main/src/LAppModel.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1066,3 +1066,21 @@ const char* LAppModel::GetSoundPath(const char *group, int index)
10661066
{
10671067
return _modelSetting->GetMotionSoundFileName(group, index);
10681068
}
1069+
1070+
void LAppModel::GetCanvasSize(float &w, float &h)
1071+
{
1072+
w = _model->GetCanvasWidth();
1073+
h = _model->GetCanvasHeight();
1074+
}
1075+
1076+
void LAppModel::GetCanvasSizePixel(float &w, float &h)
1077+
{
1078+
w = _model->GetCanvasWidthPixel();
1079+
h = _model->GetCanvasHeightPixel();
1080+
}
1081+
1082+
float LAppModel::GetPixelsPerUnit()
1083+
{
1084+
return _model->GetPixelsPerUnit();
1085+
}
1086+

Main/src/LAppModel.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,12 @@ class LAppModel : public Csm::CubismUserModel
206206

207207
const char* GetSoundPath(const char* group, int index);
208208

209+
void GetCanvasSize(float& w, float& h);
210+
211+
void GetCanvasSizePixel(float& w, float& h);
212+
213+
float GetPixelsPerUnit();
214+
209215
protected:
210216
/**
211217
* @brief モデルを描画する処理。モデルを描画する空間のView-Projection行列を渡す。

Main/src/fine-grained/Model.cpp

Lines changed: 78 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ namespace
5555

5656
Model::Model() : CubismUserModel(), _modelSetting(nullptr), _matrixManager(),
5757
_parameterCount(0), _parameterDefaultValues(nullptr), _parameterValues(nullptr),
58-
_tmpOrderedDrawIndice(nullptr), _expFadeOutTimeMillis(0), _defaultExpressionId("")
58+
_tmpOrderedDrawIndice(nullptr)
5959
{
6060
_mocConsistency = true;
6161

@@ -110,7 +110,7 @@ void Model::Update(float deltaSecs)
110110
_model->LoadParameters();
111111
if (!_motionManager->IsFinished())
112112
{
113-
motionUpdated = _motionManager->UpdateMotion(_model, deltaSecs);
113+
motionUpdated = _motionManager->UpdateMotion(_model, deltaSecs);
114114
}
115115
_model->SaveParameters();
116116

@@ -191,12 +191,19 @@ void Model::SetupModel()
191191
ACubismMotion *motion = LoadExpression(buffer, size, name.GetRawString());
192192
if (motion)
193193
{
194+
std::string key = name.GetRawString();
194195
if (_expressions[name] != nullptr)
195196
{
196197
ACubismMotion::Delete(_expressions[name]);
197198
_expressions[name] = nullptr;
198199
}
200+
if (_expManagers[key] != nullptr)
201+
{
202+
CSM_DELETE(_expManagers[key]);
203+
_expManagers.erase(key);
204+
}
199205
_expressions[name] = motion;
206+
_expManagers[key] = CSM_NEW CubismExpressionMotionManager();
200207
}
201208
});
202209
}
@@ -326,8 +333,8 @@ bool Model::IsHit(CubismIdHandle drawableId, csmFloat32 pointX, csmFloat32 point
326333
return false; // 存在しない場合はfalse
327334
}
328335

329-
const csmInt32 count = _model->GetDrawableVertexCount(drawIndex);
330-
const csmFloat32* vertices = _model->GetDrawableVertices(drawIndex);
336+
const csmInt32 count = _model->GetDrawableVertexCount(drawIndex);
337+
const csmFloat32 *vertices = _model->GetDrawableVertices(drawIndex);
331338

332339
csmFloat32 left = vertices[0];
333340
csmFloat32 right = vertices[0];
@@ -405,26 +412,17 @@ void Model::UpdateBlink(float deltaSecs)
405412

406413
void Model::UpdateExpression(float deltaSecs)
407414
{
408-
if (_expFadeOutTimeMillis > 0)
415+
if (_expressionManager->IsFinished())
409416
{
410-
_expFadeOutTimeMillis -= deltaSecs * 1000;
411-
if (_expFadeOutTimeMillis <= 0)
417+
for (auto &pair : _expManagers)
412418
{
413-
if (_defaultExpressionId.empty())
414-
{
415-
_expressionManager->StopAllMotions();
416-
Info("reset expression");
417-
}
418-
else
419-
{
420-
SetExpression(_defaultExpressionId.c_str());
421-
Info("reset expression: %s", _defaultExpressionId.c_str());
422-
}
423-
_expFadeOutTimeMillis = 0;
419+
pair.second->UpdateMotion(_model, deltaSecs);
424420
}
425421
}
426-
427-
_expressionManager->UpdateMotion(_model, deltaSecs);
422+
else
423+
{
424+
_expressionManager->UpdateMotion(_model, deltaSecs);
425+
}
428426
}
429427

430428
void Model::UpdatePhysics(float deltaSecs)
@@ -730,7 +728,7 @@ int Model::GetMotionCount(const char *group)
730728
return _modelSetting->GetMotionCount(group);
731729
}
732730

733-
void Model::GetMotions(void *collector, void (*collect)(void *collector, const char *group, int no, const char *file, const char* sound))
731+
void Model::GetMotions(void *collector, void (*collect)(void *collector, const char *group, int no, const char *file, const char *sound))
734732
{
735733
const int count = _modelSetting->GetMotionGroupCount();
736734
for (int i = 0; i < count; i++)
@@ -1107,39 +1105,50 @@ const unsigned short *Model::GetDrawableIndices(int index)
11071105
return _model->GetDrawableVertexIndices(index);
11081106
}
11091107

1110-
void Model::SetExpression(const char *expressionId)
1108+
void Model::AddExpression(const char *expressionId)
11111109
{
11121110
ACubismMotion *motion = _expressions[expressionId];
11131111

11141112
Info("expression: [%s]", expressionId);
11151113

11161114
if (motion != nullptr)
11171115
{
1118-
_expressionManager->StartMotion(motion, false);
1116+
_expManagers[expressionId]->StartMotion(motion, false);
11191117
}
11201118
else
11211119
{
11221120
Info("expression[%s] is null ", expressionId);
11231121
}
11241122
}
11251123

1126-
int Model::GetExpressionCount()
1124+
void Model::RemoveExpression(const char *expressionId)
11271125
{
1128-
return _modelSetting->GetExpressionCount();
1126+
if (_expManagers.find(expressionId) == _expManagers.end())
1127+
{
1128+
return;
1129+
}
1130+
_expManagers[expressionId]->StopAllMotions();
1131+
1132+
Info("reset expression: [%s]", expressionId);
11291133
}
11301134

1131-
void Model::GetExpressions(void *collector, void (*collect)(void *collector, const char *id, const char *file))
1135+
void Model::SetExpression(const char *expressionId)
11321136
{
1133-
const int count = _modelSetting->GetExpressionCount();
1134-
for (int i = 0; i < count; i++)
1137+
ACubismMotion *motion = _expressions[expressionId];
1138+
1139+
Info("expression: [%s]", expressionId);
1140+
1141+
if (motion != nullptr)
11351142
{
1136-
const char *file = _modelSetting->GetExpressionFileName(i);
1137-
const char *id = _modelSetting->GetExpressionName(i);
1138-
collect(collector, id, file);
1143+
_expressionManager->StartMotion(motion, false);
1144+
}
1145+
else
1146+
{
1147+
Info("expression[%s] is null ", expressionId);
11391148
}
11401149
}
11411150

1142-
const char *Model::SetRandomExpression()
1151+
const char* Model::SetRandomExpression()
11431152
{
11441153
const int size = _expressions.GetSize();
11451154
if (size == 0)
@@ -1162,34 +1171,36 @@ const char *Model::SetRandomExpression()
11621171
return nullptr;
11631172
}
11641173

1165-
void Model::ResetExpression()
1174+
void Model::ResetExpressions()
11661175
{
1176+
for (auto& [id, expMgr] : _expManagers)
1177+
{
1178+
expMgr->StopAllMotions();
1179+
}
11671180
_expressionManager->StopAllMotions();
1181+
1182+
Info("reset expressions");
11681183
}
11691184

1170-
void Model::SetDefaultExpression(const char *expressionId)
1185+
void Model::ResetExpression()
11711186
{
1172-
if (expressionId != nullptr || _expressions.IsExist(expressionId))
1173-
{
1174-
_defaultExpressionId = expressionId;
1175-
Info("set default expression: [%s]", expressionId);
1176-
}
1177-
else
1178-
{
1179-
Info("expression[%s] does not exist", expressionId);
1180-
}
1187+
_expressionManager->StopAllMotions();
1188+
}
1189+
1190+
int Model::GetExpressionCount()
1191+
{
1192+
return _modelSetting->GetExpressionCount();
11811193
}
11821194

1183-
void Model::SetFadeOutExpression(const char *expressionId, double fadeOutTime)
1195+
void Model::GetExpressions(void *collector, void (*collect)(void *collector, const char *id, const char *file))
11841196
{
1185-
if (fadeOutTime < 0)
1197+
const int count = _modelSetting->GetExpressionCount();
1198+
for (int i = 0; i < count; i++)
11861199
{
1187-
Info("fadeOutTime[%f] is invalid", fadeOutTime);
1188-
return;
1200+
const char *file = _modelSetting->GetExpressionFileName(i);
1201+
const char *id = _modelSetting->GetExpressionName(i);
1202+
collect(collector, id, file);
11891203
}
1190-
1191-
SetExpression(expressionId);
1192-
_expFadeOutTimeMillis = fadeOutTime;
11931204
}
11941205

11951206
void Model::StopAllMotions()
@@ -1214,6 +1225,23 @@ void Model::ResetPose()
12141225
}
12151226
}
12161227

1228+
void Model::GetCanvasSize(float &w, float &h)
1229+
{
1230+
w = _model->GetCanvasWidth();
1231+
h = _model->GetCanvasHeight();
1232+
}
1233+
1234+
void Model::GetCanvasSizePixel(float &w, float &h)
1235+
{
1236+
w = _model->GetCanvasWidthPixel();
1237+
h = _model->GetCanvasHeightPixel();
1238+
}
1239+
1240+
float Model::GetPixelsPerUnit()
1241+
{
1242+
return _model->GetPixelsPerUnit();
1243+
}
1244+
12171245
void Model::ReleaseMotions()
12181246
{
12191247
for (csmMap<csmString, ACubismMotion *>::const_iterator iter = _motions.Begin(); iter != _motions.End(); ++iter)

Main/src/fine-grained/Model.hpp

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
#include <vector>
1111
#include <string>
12+
#include <unordered_map>
1213

1314
#include <Model/CubismUserModel.hpp>
1415
#include <Motion/ACubismMotion.hpp>
@@ -149,18 +150,21 @@ class Model : public Csm::CubismUserModel
149150
const unsigned short* GetDrawableIndices(int index);
150151

151152
// expression
153+
void AddExpression(const char *expressionId);
154+
155+
void RemoveExpression(const char *expressionId);
156+
152157
void SetExpression(const char *expressionId);
153158

154-
int GetExpressionCount();
155-
void GetExpressions(void *collector, void(*collect)(void* collector, const char* id, const char* file));
156-
157159
const char* SetRandomExpression();
158160

161+
void ResetExpressions();
162+
159163
void ResetExpression();
160164

161-
void SetDefaultExpression(const char *expressionId);
165+
int GetExpressionCount();
162166

163-
void SetFadeOutExpression(const char *expressionId, double fadeOutTime);
167+
void GetExpressions(void *collector, void(*collect)(void* collector, const char* id, const char* file));
164168

165169
// reset
166170
void StopAllMotions();
@@ -169,6 +173,13 @@ class Model : public Csm::CubismUserModel
169173

170174
void ResetPose();
171175

176+
// sizes
177+
void GetCanvasSize(float& w, float& h);
178+
179+
void GetCanvasSizePixel(float& w, float& h);
180+
181+
float GetPixelsPerUnit();
182+
172183
private:
173184
void ReleaseMotions();
174185
void ReleaseExpressions();
@@ -184,6 +195,8 @@ class Model : public Csm::CubismUserModel
184195
csmString _modelHomeDir;
185196
csmMap<Csm::csmString, ACubismMotion*> _motions;
186197
csmMap<Csm::csmString, ACubismMotion*> _expressions;
198+
std::unordered_map<std::string, CubismExpressionMotionManager*> _expManagers;
199+
187200

188201
const Csm::CubismId* _idParamAngleX;
189202
const Csm::CubismId* _idParamAngleY;
@@ -213,7 +226,4 @@ class Model : public Csm::CubismUserModel
213226

214227
std::vector<csmString> _motionGroupNames;
215228
std::vector<int> _motionCounts;
216-
217-
std::string _defaultExpressionId;
218-
double _expFadeOutTimeMillis;
219229
};

Wrapper/PyLAppModel.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -728,6 +728,25 @@ static PyObject* PyLAppModel_GetSoundPath(PyLAppModelObject* self, PyObject* arg
728728
return Py_BuildValue("s", self->model->GetSoundPath(group, index));
729729
}
730730

731+
static PyObject* PyLAppModel_GetCanvasSize(PyLAppModelObject* self, PyObject* args, PyObject* kwargs)
732+
{
733+
float w, h;
734+
self->model->GetCanvasSize(w, h);
735+
return Py_BuildValue("ff", w, h);
736+
}
737+
738+
static PyObject* PyLAppModel_GetCanvasSizePixel(PyLAppModelObject* self, PyObject* args, PyObject* kwargs)
739+
{
740+
float w, h;
741+
self->model->GetCanvasSizePixel(w, h);
742+
return Py_BuildValue("ff", w, h);
743+
}
744+
745+
static PyObject* PyLAppModel_GetPixelsPerUnit(PyLAppModelObject* self, PyObject* args, PyObject* kwargs)
746+
{
747+
return Py_BuildValue("f", self->model->GetPixelsPerUnit());
748+
}
749+
731750
// 包装模块方法的方法列表
732751
static PyMethodDef PyLAppModel_methods[] = {
733752
{"LoadModelJson", (PyCFunction)PyLAppModel_LoadModelJson, METH_VARARGS, ""},
@@ -783,6 +802,10 @@ static PyMethodDef PyLAppModel_methods[] = {
783802

784803
{"GetSoundPath", (PyCFunction)PyLAppModel_GetSoundPath, METH_VARARGS | METH_KEYWORDS, ""},
785804

805+
{"GetCanvasSize", (PyCFunction)PyLAppModel_GetCanvasSize, METH_VARARGS, ""},
806+
{"GetCanvasSizePixel", (PyCFunction)PyLAppModel_GetCanvasSizePixel, METH_VARARGS, ""},
807+
{"GetPixelsPerUnit", (PyCFunction)PyLAppModel_GetPixelsPerUnit, METH_VARARGS, ""},
808+
786809
{NULL} // 方法列表结束的标志
787810
};
788811

0 commit comments

Comments
 (0)