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