[dali_1.0.9] Merge branch 'tizen'
[platform/core/uifw/dali-core.git] / dali / internal / event / actor-attachments / image-attachment-impl.cpp
1 /*
2  * Copyright (c) 2014 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
18 // CLASS HEADER
19 #include <dali/internal/event/actor-attachments/image-attachment-impl.h>
20
21 // INTERNAL INCLUDES
22 #include <dali/internal/update/node-attachments/scene-graph-image-attachment.h>
23 #include <dali/internal/event/common/stage-impl.h>
24 #include <dali/public-api/common/dali-common.h>
25
26 using namespace std;
27
28 namespace
29 {
30 static Dali::Internal::SceneGraph::ImageAttachment::PixelArea EMPTY_PIXEL_AREA(0,0,0,0);
31 }
32
33 namespace Dali
34 {
35
36 namespace Internal
37 {
38
39 ImageAttachmentPtr ImageAttachment::New( const SceneGraph::Node& parentNode )
40 {
41   StagePtr stage = Stage::GetCurrent();
42
43   ImageAttachmentPtr attachment( new ImageAttachment( *stage ) );
44
45   // Transfer object ownership of scene-object to message
46   SceneGraph::ImageAttachment* sceneObject = CreateSceneObject();
47   AttachToNodeMessage( stage->GetUpdateManager(), parentNode, sceneObject );
48
49   // Keep raw pointer for message passing
50   attachment->mSceneObject = sceneObject;
51
52   return attachment;
53 }
54
55 ImageAttachment::ImageAttachment( Stage& stage )
56 : RenderableAttachment(stage),
57   mSceneObject(NULL),
58   mPixelArea(EMPTY_PIXEL_AREA),
59   mStyle(Dali::ImageActor::STYLE_QUAD),
60   mBorder(0.45,0.45,0.1,0.1),
61   mIsPixelAreaSet(false),
62   mBorderInPixels(false)
63 {
64   mImageConnectable.Set( NULL, false );
65 }
66
67 ImageAttachment::~ImageAttachment()
68 {
69 }
70
71 void ImageAttachment::SetImage( ImagePtr& image )
72 {
73   bool onStage = OnStage();
74   // keep a reference to Image object
75   mImageConnectable.Set( image, onStage );
76
77   // Wait until the scene-graph attachment is connected, before providing resource ID
78   if ( OnStage() )
79   {
80     unsigned int resourceId = (image) ? image->GetResourceId() : 0u;
81
82     // sceneObject is being used in a separate thread; queue a message to set
83     SetTextureIdMessage( mStage->GetUpdateInterface(), *mSceneObject, resourceId );
84   }
85 }
86
87 ImagePtr ImageAttachment::GetImage()
88 {
89   return mImageConnectable.Get();
90 }
91
92 void ImageAttachment::SetPixelArea(const PixelArea& pixelArea)
93 {
94   // check to see if pixel area is actually different, using rect::operator==
95   if( pixelArea != mPixelArea )
96   {
97     // Cache for public getters
98     mPixelArea = pixelArea;
99     mIsPixelAreaSet = true;
100
101     // sceneObject is being used in a separate thread; queue a message to set
102     SetPixelAreaMessage( mStage->GetUpdateInterface(), *mSceneObject, mPixelArea );
103   }
104 }
105
106 void ImageAttachment::ClearPixelArea()
107 {
108   // Cache for public getters
109   mIsPixelAreaSet = false;
110
111   // sceneObject is being used in a separate thread; queue a message to set
112   ClearPixelAreaMessage( mStage->GetUpdateInterface(), *mSceneObject );
113 }
114
115 void ImageAttachment::SetStyle(Style style)
116 {
117   // Cache for public getters
118   mStyle = style;
119
120   // sceneObject is being used in a separate thread; queue a message to set
121   SetStyleMessage( mStage->GetUpdateInterface(), *mSceneObject, style );
122 }
123
124 void ImageAttachment::SetNinePatchBorder(const Vector4& border, bool inPixels)
125 {
126   // Cache for public getters
127   mBorder = border;
128   mBorderInPixels = inPixels;
129
130   // sceneObject is being used in a separate thread; queue a message to set
131   SetNinePatchBorderMessage( mStage->GetUpdateInterface(), *mSceneObject, border, inPixels );
132 }
133
134 SceneGraph::ImageAttachment* ImageAttachment::CreateSceneObject()
135 {
136   return SceneGraph::ImageAttachment::New( 0u );
137 }
138
139 void ImageAttachment::OnStageConnection2()
140 {
141   mImageConnectable.OnStageConnect();
142
143   // Provide resource ID when scene-graph attachment is connected
144   ImagePtr image = mImageConnectable.Get();
145   unsigned int resourceId = (image) ? image->GetResourceId() : 0u;
146   if ( 0u != resourceId )
147   {
148     SetTextureIdMessage( mStage->GetUpdateInterface(), *mSceneObject, resourceId );
149   }
150 }
151
152 void ImageAttachment::OnStageDisconnection2()
153 {
154   // Remove resource ID when scene-graph attachment is disconnected
155   SetTextureIdMessage( mStage->GetUpdateInterface(), *mSceneObject, 0u );
156
157   mImageConnectable.OnStageDisconnect();
158 }
159
160 const SceneGraph::RenderableAttachment& ImageAttachment::GetSceneObject() const
161 {
162   DALI_ASSERT_DEBUG( mSceneObject != NULL );
163   return *mSceneObject;
164 }
165
166 } // namespace Internal
167
168 } // namespace Dali