Updated demos to use DALi clang-format
[platform/core/uifw/dali-demo.git] / examples / simple-visuals-control / my-control-impl.cpp
1 /*
2  * Copyright (c) 2020 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 // CLASS HEADER
18
19 #include "my-control-impl.h"
20
21 // EXTERNAL INCLUDES
22
23 #include <dali-toolkit/dali-toolkit.h>
24 #include <dali-toolkit/devel-api/controls/control-devel.h>
25 #include <dali-toolkit/devel-api/focus-manager/keyinput-focus-manager.h>
26 #include <dali-toolkit/devel-api/visual-factory/visual-factory.h>
27 #include <dali/devel-api/scripting/enum-helper.h>
28
29 using namespace Dali;
30 using namespace Dali::Toolkit;
31
32 namespace Demo
33 {
34 namespace Internal
35 {
36 namespace
37 {
38 Dali::BaseHandle Create()
39 {
40   return Demo::MyControl::New();
41 }
42
43 // Required code for Property set up.
44
45 DALI_TYPE_REGISTRATION_BEGIN(MyControl, Dali::Toolkit::Control, Create);
46
47 DALI_PROPERTY_REGISTRATION(Demo, MyControl, "iconVisual", MAP, ICON_VISUAL)
48
49 DALI_TYPE_REGISTRATION_END();
50
51 // Add an enum to string conversion entry for the control's visuals.  In this case just the icon visual.
52 // Enables Setting of the property using enums or strings.
53 DALI_ENUM_TO_STRING_TABLE_BEGIN(VISUAL_PROPERTIES)
54   {"iconVisual", Demo::MyControl::Property::ICON_VISUAL},
55 DALI_ENUM_TO_STRING_TABLE_END(VISUAL_PROPERTIES)
56
57 } // anonymous namespace
58
59 Internal::MyControl::MyControl()
60 : Control(ControlBehaviour(CONTROL_BEHAVIOUR_DEFAULT))
61 {
62 }
63
64 Demo::MyControl Internal::MyControl::New()
65 {
66   IntrusivePtr<Internal::MyControl> impl   = new Internal::MyControl();
67   Demo::MyControl                   handle = Demo::MyControl(*impl);
68   impl->Initialize();
69   return handle;
70 }
71
72 void MyControl::OnInitialize()
73 {
74   Dali::Actor self = Self();
75   self.SetProperty(Actor::Property::KEYBOARD_FOCUSABLE, true);
76 }
77
78 void MyControl::SetProperty(BaseObject* object, Property::Index index, const Property::Value& value)
79 {
80   Demo::MyControl myControl = Demo::MyControl::DownCast(Dali::BaseHandle(object));
81
82   if(myControl)
83   {
84     MyControl& impl = GetImpl(myControl);
85     switch(index)
86     {
87       case Demo::MyControl::Property::ICON_VISUAL:
88       {
89         Toolkit::Visual::Base  iconVisual;
90         Toolkit::VisualFactory visualFactory = Toolkit::VisualFactory::Get();
91         Property::Map*         map           = value.GetMap();
92         if(map && !map->Empty())
93         {
94           iconVisual = visualFactory.CreateVisual(*map);
95         }
96
97         if(iconVisual)
98         {
99           DevelControl::RegisterVisual(impl, index, iconVisual);
100         }
101         break;
102       }
103     }
104   }
105 }
106
107 Property::Value MyControl::GetProperty(BaseObject* object, Property::Index propertyIndex)
108 {
109   Property::Value value;
110
111   Demo::MyControl myControl = Demo::MyControl::DownCast(Dali::BaseHandle(object));
112
113   if(myControl)
114   {
115     switch(propertyIndex)
116     {
117       case Demo::MyControl::Property::ICON_VISUAL:
118       {
119         Property::Map         map;
120         MyControl&            impl   = GetImpl(myControl);
121         Toolkit::Visual::Base visual = DevelControl::GetVisual(impl, propertyIndex);
122         if(visual)
123         {
124           visual.CreatePropertyMap(map); // Creates a Property map containing the Visual that ICON_VISUAL currently is. Can change if state changes.
125           value = map;
126         }
127         break;
128       }
129       default:
130         break;
131     }
132   }
133
134   return value;
135 }
136
137 } // namespace Internal
138 } // namespace Demo