Skip to content

Commit 1a26039

Browse files
committed
#158 Clean code observer, add hidden value
1 parent d7800e3 commit 1a26039

File tree

8 files changed

+76
-65
lines changed

8 files changed

+76
-65
lines changed

Projects/Editor/Source/Editor/AssetEditor/CMatEditor.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,6 @@ namespace Skylicht
3434
class CMatEditor : public CAssetEditor
3535
{
3636
protected:
37-
std::vector<ISubject*> m_subjects;
38-
3937
std::map<CMaterial*, GUI::CBoxLayout*> m_materialUI;
4038

4139
ArrayMaterial m_materials;

Projects/Editor/Source/Editor/Components/Default/CDefaultEditor.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ namespace Skylicht
9191
for (u32 i = 0, n = data->getNumProperty(); i < n; i++)
9292
{
9393
CValueProperty* valueProperty = data->getPropertyID(i);
94+
if (valueProperty->isHidden())
95+
continue;
9496

9597
// add ui space
9698
if (valueProperty->getUISpace() > 0)

Projects/Editor/Source/Editor/GUIEditor/CGUIEditor.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,14 @@ namespace Skylicht
4545
{
4646
if (m_guiData)
4747
delete m_guiData;
48+
49+
CSerializableEditor::closeGUI();
4850
}
4951

5052
void CGUIEditor::initGUI(CGUIElement* gui, CSpaceProperty* ui)
5153
{
5254
m_gui = gui;
53-
m_guiData = m_gui->createSerializable();
55+
m_guiData = m_gui->createSerializable();
5456
}
5557

5658
void CGUIEditor::onUpdateValue(CObjectSerializable* object)

Projects/Editor/Source/Editor/Serializable/CSerializableEditor.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ namespace Skylicht
5353
for (u32 i = 0, n = object->getNumProperty(); i < n; i++)
5454
{
5555
CValueProperty* valueProperty = object->getPropertyID(i);
56+
if (valueProperty->isHidden())
57+
continue;
5658

5759
if (valueProperty->getType() == EPropertyDataType::Bool)
5860
{

Projects/Editor/Source/Editor/Space/Property/CSpaceProperty.cpp

Lines changed: 43 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,6 @@ namespace Skylicht
9292

9393
for (SGroup* group : m_groups)
9494
{
95-
for (IObserver* o : group->Observer)
96-
{
97-
delete o;
98-
}
99-
10095
delete group;
10196
}
10297
m_groups.clear();
@@ -151,7 +146,6 @@ namespace Skylicht
151146

152147
for (SGroup* group : m_groups)
153148
{
154-
group->releaseObserver();
155149
group->GroupUI->remove();
156150
if (group->AssetOwner)
157151
group->AssetOwner->closeGUI();
@@ -459,6 +453,7 @@ namespace Skylicht
459453
CSpaceProperty::SGroup* group = getGroupByLayout(boxLayout);
460454
if (group != NULL)
461455
{
456+
// when check value change => update ui
462457
CObserver* onChange = new CObserver();
463458
onChange->Notify = [me = onChange, target = input](ISubject* subject, IObserver* from)
464459
{
@@ -469,16 +464,13 @@ namespace Skylicht
469464
}
470465
};
471466

472-
IObserver* observer = value->addObserver(onChange);
467+
value->addObserver(onChange, true);
473468

474469
// when input text change
475-
input->OnTextChanged = [value, input, observer](GUI::CBase* base) {
470+
input->OnTextChanged = [value, input, observer = onChange](GUI::CBase* base) {
476471
value->set(input->getValue());
477472
value->notify(observer);
478473
};
479-
480-
// hold observer to release later
481-
group->Observer.push_back(observer);
482474
}
483475
}
484476

@@ -503,6 +495,7 @@ namespace Skylicht
503495
CSpaceProperty::SGroup* group = getGroupByLayout(boxLayout);
504496
if (group != NULL)
505497
{
498+
// when check value change => update ui
506499
CObserver* onChange = new CObserver();
507500
onChange->Notify = [me = onChange, target = input](ISubject* subject, IObserver* from)
508501
{
@@ -513,16 +506,13 @@ namespace Skylicht
513506
}
514507
};
515508

516-
IObserver* observer = value->addObserver(onChange);
509+
value->addObserver(onChange, true);
517510

518511
// when input text change
519-
input->OnTextChanged = [value, input, observer](GUI::CBase* base) {
512+
input->OnTextChanged = [value, input, observer = onChange](GUI::CBase* base) {
520513
value->set((int)input->getValue());
521514
value->notify(observer);
522515
};
523-
524-
// hold observer to release later
525-
group->Observer.push_back(observer);
526516
}
527517
}
528518

@@ -547,6 +537,7 @@ namespace Skylicht
547537
CSpaceProperty::SGroup* group = getGroupByLayout(boxLayout);
548538
if (group != NULL)
549539
{
540+
// when check value change => update ui
550541
CObserver* onChange = new CObserver();
551542
onChange->Notify = [me = onChange, target = input](ISubject* subject, IObserver* from)
552543
{
@@ -557,16 +548,13 @@ namespace Skylicht
557548
}
558549
};
559550

560-
IObserver* observer = value->addObserver(onChange);
551+
value->addObserver(onChange, true);
561552

562553
// when input text change
563-
input->OnTextChanged = [value, input, observer](GUI::CBase* base) {
554+
input->OnTextChanged = [value, input, observer = onChange](GUI::CBase* base) {
564555
value->set((u32)input->getValue());
565556
value->notify(observer);
566557
};
567-
568-
// hold observer to release later
569-
group->Observer.push_back(observer);
570558
}
571559
}
572560

@@ -585,6 +573,7 @@ namespace Skylicht
585573
CSpaceProperty::SGroup* group = getGroupByLayout(boxLayout);
586574
if (group != NULL)
587575
{
576+
// when check value change => update ui
588577
CObserver* onChange = new CObserver();
589578
onChange->Notify = [me = onChange, target = input](ISubject* subject, IObserver* from)
590579
{
@@ -596,14 +585,11 @@ namespace Skylicht
596585
};
597586

598587
// when input text change
599-
IObserver* observer = value->addObserver(onChange);
600-
input->OnTextChanged = [value, input, observer](GUI::CBase* base) {
588+
value->addObserver(onChange, true);
589+
input->OnTextChanged = [value, input, observer = onChange](GUI::CBase* base) {
601590
value->set(input->getString());
602591
value->notify(observer);
603592
};
604-
605-
// hold observer to release later
606-
group->Observer.push_back(observer);
607593
}
608594

609595
boxLayout->endVertical();
@@ -629,6 +615,7 @@ namespace Skylicht
629615
CSpaceProperty::SGroup* group = getGroupByLayout(boxLayout);
630616
if (group != NULL)
631617
{
618+
// when check value change => update ui
632619
CObserver* onChange = new CObserver();
633620
onChange->Notify = [me = onChange, target = input](ISubject* subject, IObserver* from)
634621
{
@@ -640,14 +627,11 @@ namespace Skylicht
640627
};
641628

642629
// when input text change
643-
IObserver* observer = value->addObserver(onChange);
644-
input->OnLostKeyboardFocus = [value, input, observer](GUI::CBase* base) {
630+
value->addObserver(onChange, true);
631+
input->OnLostKeyboardFocus = [value, input, observer = onChange](GUI::CBase* base) {
645632
value->set(input->getValueInt());
646633
value->notify(observer);
647634
};
648-
649-
// hold observer to release later
650-
group->Observer.push_back(observer);
651635
}
652636

653637
boxLayout->endVertical();
@@ -670,7 +654,7 @@ namespace Skylicht
670654
CSpaceProperty::SGroup* group = getGroupByLayout(boxLayout);
671655
if (group != NULL)
672656
{
673-
// when check value c hange
657+
// when check value change => update ui
674658
CObserver* onChange = new CObserver();
675659
onChange->Notify = [me = onChange, target = check](ISubject* subject, IObserver* from)
676660
{
@@ -681,15 +665,12 @@ namespace Skylicht
681665
}
682666
};
683667

684-
// when check control change
685-
IObserver* observer = value->addObserver(onChange);
686-
check->OnChanged = [value, check, observer](GUI::CBase* base) {
668+
// when check control change => update value
669+
value->addObserver(onChange, true);
670+
check->OnChanged = [value, check, observer = onChange](GUI::CBase* base) {
687671
value->set(check->getToggle());
688672
value->notify(observer);
689673
};
690-
691-
// hold observer to release later
692-
group->Observer.push_back(observer);
693674
}
694675

695676
boxLayout->endVertical();
@@ -708,6 +689,8 @@ namespace Skylicht
708689
comboBox->setListValue(listValue);
709690

710691
CObserver* onChange = new CObserver();
692+
693+
// when check value change => update ui
711694
onChange->Notify = [me = onChange, target = comboBox](ISubject* subject, IObserver* from)
712695
{
713696
if (from != me)
@@ -717,9 +700,9 @@ namespace Skylicht
717700
}
718701
};
719702

720-
// when combobox change
721-
IObserver* observer = value->addObserver(onChange);
722-
comboBox->OnChanged = [value, comboBox, observer](GUI::CBase* base)
703+
// when combobox change => update value
704+
value->addObserver(onChange, true);
705+
comboBox->OnChanged = [value, comboBox, observer = onChange](GUI::CBase* base)
723706
{
724707
value->set(comboBox->getLabel());
725708
value->notify(observer);
@@ -743,6 +726,8 @@ namespace Skylicht
743726
slider->setValue(value->get(), min, max, false);
744727

745728
CObserver* onChange = new CObserver();
729+
730+
// when check value change => update ui
746731
onChange->Notify = [me = onChange, target = slider](ISubject* subject, IObserver* from)
747732
{
748733
if (from != me)
@@ -752,9 +737,9 @@ namespace Skylicht
752737
}
753738
};
754739

755-
// when slider change
756-
IObserver* observer = value->addObserver(onChange);
757-
slider->OnTextChanged = [value, slider, observer](GUI::CBase* base) {
740+
// when slider change => update value
741+
value->addObserver(onChange, true);
742+
slider->OnTextChanged = [value, slider, observer = onChange](GUI::CBase* base) {
758743
value->set(slider->getValue());
759744
value->notify(observer);
760745
};
@@ -775,6 +760,7 @@ namespace Skylicht
775760
GUI::CColorPicker* colorPicker = new GUI::CColorPicker(layout);
776761
colorPicker->setColor(GUI::SGUIColor(c.getAlpha(), c.getRed(), c.getGreen(), c.getBlue()));
777762

763+
// when check value change => update ui
778764
CObserver* onChange = new CObserver();
779765
onChange->Notify = [me = onChange, target = colorPicker](ISubject* subject, IObserver* from)
780766
{
@@ -786,9 +772,9 @@ namespace Skylicht
786772
}
787773
};
788774

789-
// when color pick change
790-
IObserver* observer = value->addObserver(onChange);
791-
colorPicker->OnChanged = [value, colorPicker, observer](GUI::CBase* base) {
775+
// when color pick change => update value
776+
value->addObserver(onChange, true);
777+
colorPicker->OnChanged = [value, colorPicker, observer = onChange](GUI::CBase* base) {
792778
const GUI::SGUIColor& guiColor = colorPicker->getColor();
793779
value->set(SColor(guiColor.A, guiColor.R, guiColor.G, guiColor.B));
794780
value->notify(observer);
@@ -853,6 +839,7 @@ namespace Skylicht
853839
return false;
854840
};
855841

842+
// when check value change => update ui
856843
CObserver* onChange = new CObserver();
857844
onChange->Notify = [me = onChange, target = input](ISubject* subject, IObserver* from)
858845
{
@@ -864,16 +851,17 @@ namespace Skylicht
864851
target->setString(L"None");
865852
};
866853

867-
IObserver* observer = value->addObserver(onChange);
868-
input->OnDrop = [s = value, obs = observer](GUI::SDragDropPackage* data, float mouseX, float mouseY)
854+
// when ui update => change value
855+
value->addObserver(onChange, true);
856+
input->OnDrop = [s = value, observer = onChange](GUI::SDragDropPackage* data, float mouseX, float mouseY)
869857
{
870858
if (data->Name == "ListFSItem")
871859
{
872860
GUI::CListRowItem* rowItem = (GUI::CListRowItem*)data->UserData;
873861
std::string path = rowItem->getTagString();
874862
path = CAssetManager::getInstance()->getShortPath(path.c_str());
875863
s->set(path);
876-
s->notify(obs);
864+
s->notify(observer);
877865
}
878866
};
879867
boxLayout->endVertical();
@@ -922,6 +910,7 @@ namespace Skylicht
922910
return false;
923911
};
924912

913+
// when check value change => update ui
925914
CObserver* onChange = new CObserver();
926915
onChange->Notify = [me = onChange, target = input](ISubject* subject, IObserver* from)
927916
{
@@ -933,24 +922,24 @@ namespace Skylicht
933922
target->setString(L"None");
934923
};
935924

936-
IObserver* observer = value->addObserver(onChange);
937-
input->OnDrop = [s = value, obs = observer](GUI::SDragDropPackage* data, float mouseX, float mouseY)
925+
value->addObserver(onChange, true);
926+
input->OnDrop = [s = value, observer = onChange](GUI::SDragDropPackage* data, float mouseX, float mouseY)
938927
{
939928
if (data->Name == "ListFSItem")
940929
{
941930
GUI::CListRowItem* rowItem = (GUI::CListRowItem*)data->UserData;
942931
std::string path = rowItem->getTagString();
943932
path = CAssetManager::getInstance()->getShortPath(path.c_str());
944933
s->set(path);
945-
s->notify(obs);
934+
s->notify(observer);
946935
}
947936
else if (data->Name == "TreeFSItem")
948937
{
949938
GUI::CTreeNode* node = (GUI::CTreeNode*)data->UserData;
950939
std::string path = node->getTagString();
951940
path = CAssetManager::getInstance()->getShortPath(path.c_str());
952941
s->set(path);
953-
s->notify(obs);
942+
s->notify(observer);
954943
}
955944
};
956945
boxLayout->endVertical();

Projects/Editor/Source/Editor/Space/Property/CSpaceProperty.h

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ namespace Skylicht
5353
CGUIEditor* GUIEditorOwner;
5454

5555
GUI::CBase* GroupUI;
56-
std::vector<IObserver*> Observer;
5756

5857
SGroup()
5958
{
@@ -63,13 +62,6 @@ namespace Skylicht
6362
EntityDataOwner = NULL;
6463
GUIEditorOwner = NULL;
6564
}
66-
67-
void releaseObserver()
68-
{
69-
for (IObserver* o : Observer)
70-
delete o;
71-
Observer.clear();
72-
}
7365
};
7466

7567
protected:

Projects/Skylicht/Engine/Source/Serializable/CValueProperty.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ namespace Skylicht
3232
m_owner(owner),
3333
m_dataType(dataType),
3434
m_uiSpace(0.0f),
35+
m_hidden(false),
36+
m_tagData(NULL),
3537
Name(name)
3638
{
3739
if (owner != NULL)

0 commit comments

Comments
 (0)