2 * Copyright (c) 2021 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include <dali-toolkit/dali-toolkit.h>
19 #include <dali-toolkit/devel-api/visuals/image-visual-properties-devel.h>
20 #include <dali-toolkit/public-api/image-loader/image-url.h>
21 #include <dali-toolkit/public-api/image-loader/image.h>
22 #include <dali/dali.h>
23 #include <dali/public-api/adaptor-framework/encoded-image-buffer.h>
25 #include "shared/view.h"
28 using namespace Dali::Toolkit;
32 const char* BACKGROUND_IMAGE(DEMO_IMAGE_DIR "background-gradient.jpg");
33 const char* TOOLBAR_IMAGE(DEMO_IMAGE_DIR "top-bar.png");
34 const char* APPLICATION_TITLE("Image view with encoded image buffer");
36 const char* IMAGE_PATH[] = {
37 DEMO_IMAGE_DIR "gallery-small-23.jpg",
38 DEMO_IMAGE_DIR "woodEffect.jpg",
39 DEMO_IMAGE_DIR "wood.png", // 32bits image
42 const unsigned int NUMBER_OF_RESOURCES = sizeof(IMAGE_PATH) / sizeof(char*);
44 const unsigned int NUMBER_OF_IMAGES_ROW = 3;
45 const unsigned int NUMBER_OF_IMAGES_COLUMN = 2;
46 const unsigned int NUMBER_OF_IMAGES = NUMBER_OF_IMAGES_ROW * NUMBER_OF_IMAGES_COLUMN;
48 constexpr Vector2 IMAGE_VIEW_SIZE(200.0f, 200.0f);
50 const unsigned int TIMER_INTERVAL = 1000; // ms
52 EncodedImageBuffer ConvertFileToEncodedImageBuffer(const char* url)
54 EncodedImageBuffer buffer;
56 fp = fopen(url, "rb");
59 fseek(fp, 0, SEEK_END);
60 size_t size = ftell(fp);
61 Dali::Vector<uint8_t> data;
63 fseek(fp, 0, SEEK_SET);
64 size_t realSize = fread(data.Begin(), sizeof(uint8_t), size, fp);
66 data.Resize(realSize);
67 buffer = EncodedImageBuffer::New(data);
74 class ImageViewEncodedImageBufferApp : public ConnectionTracker
77 ImageViewEncodedImageBufferApp(Application& application)
78 : mApplication(application)
80 // Connect to the Application's Init signal
81 mApplication.InitSignal().Connect(this, &ImageViewEncodedImageBufferApp::Create);
84 ~ImageViewEncodedImageBufferApp()
89 void Create(Application& application)
91 // The Init signal is received once (only) during the Application lifetime
93 ImageUrl backgroundImageUrl = Image::GenerateUrl(ConvertFileToEncodedImageBuffer(BACKGROUND_IMAGE));
94 ImageUrl toolbarImageUrl = Image::GenerateUrl(ConvertFileToEncodedImageBuffer(TOOLBAR_IMAGE));
96 // Creates a default view with a default tool bar.
97 // The view is added to the window.
98 mContentLayer = DemoHelper::CreateView(application,
101 backgroundImageUrl.GetUrl(),
102 toolbarImageUrl.GetUrl(),
111 // Create automatic unparent and create ticker.
112 mTimer = Timer::New(TIMER_INTERVAL);
113 mTimer.TickSignal().Connect(this, &ImageViewEncodedImageBufferApp::OnTick);
116 application.GetWindow().GetRootLayer().TouchedSignal().Connect(this, &ImageViewEncodedImageBufferApp::OnTouch);
118 application.GetWindow().KeyEventSignal().Connect(this, &ImageViewEncodedImageBufferApp::OnKeyEvent);
122 void UpdateImageUrl()
124 mImageBuffer = ConvertFileToEncodedImageBuffer(IMAGE_PATH[mImageIndex]);
125 mImageUrl = Image::GenerateUrl(mImageBuffer);
127 void UpdateImageViews()
129 for(int i = 0; i < static_cast<int>(NUMBER_OF_IMAGES_ROW); i++)
131 for(int j = 0; j < static_cast<int>(NUMBER_OF_IMAGES_COLUMN); j++)
133 int viewId = i * NUMBER_OF_IMAGES_COLUMN + j;
134 // Remove old image and set null
135 if(mImageViews[viewId])
137 mImageViews[viewId].Unparent();
138 mImageViews[viewId] = ImageView(); // Set null image view
141 bool needToCreate = true;
143 // If current state don't wanna create current view, just skip
144 unsigned int currentViewState = 1u << ((i + j) & 1);
145 if(mState & currentViewState)
147 needToCreate = false;
152 mImageViews[viewId] = CreateImageView(i, j);
153 mContentLayer.Add(mImageViews[viewId]);
158 ImageView CreateImageView(int offset_y, int offset_x)
160 ImageView view = ImageView::New();
161 view.SetProperty(Actor::Property::SIZE, IMAGE_VIEW_SIZE);
162 view.SetProperty(Actor::Property::POSITION, Vector2(IMAGE_VIEW_SIZE.x * offset_x, IMAGE_VIEW_SIZE.y * offset_y + mToolBar.GetNaturalSize().y));
163 view.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_LEFT);
165 Property::Map imagePropertyMap;
166 imagePropertyMap.Insert(Visual::Property::TYPE, Visual::IMAGE);
167 imagePropertyMap.Insert(ImageVisual::Property::URL, mImageUrl.GetUrl());
169 view.SetProperty(ImageView::Property::IMAGE, imagePropertyMap);
176 mState = (mState + 1) % 4;
178 return true; // Keep tick always
181 bool OnTouch(Actor actor, const TouchEvent& touch)
183 if(touch.GetState(0) == PointState::UP)
186 mImageIndex = (mImageIndex + 1) % NUMBER_OF_RESOURCES;
187 // Change resource's url
189 // Update image views
196 * Main key event handler
198 void OnKeyEvent(const KeyEvent& event)
200 if(event.GetState() == KeyEvent::DOWN)
202 if(IsKey(event, DALI_KEY_ESCAPE) || IsKey(event, DALI_KEY_BACK))
204 if(mTimer.IsRunning())
207 mTimer.TickSignal().Disconnect(this, &ImageViewEncodedImageBufferApp::OnTick);
215 Application& mApplication;
217 Toolkit::Control mView; ///< The View instance.
218 Toolkit::ToolBar mToolBar; ///< The View's Toolbar.
219 Layer mContentLayer; ///< Content layer
221 Toolkit::ImageView mImageViews[NUMBER_OF_IMAGES];
223 unsigned int mImageIndex{0u};
224 EncodedImageBuffer mImageBuffer;
227 // Automatic unparent and create ticker.
229 unsigned int mState{0u}; ///< 0 : draw all images, 1 : draw half, 2 : draw another half, 3 : don't draw images.
232 int DALI_EXPORT_API main(int argc, char** argv)
234 Application application = Application::New(&argc, &argv, DEMO_THEME_PATH);
235 ImageViewEncodedImageBufferApp test(application);
236 application.MainLoop();