Tizen 2.1 base
[sdk/emulator/qemu.git] / tizen / src / skin / client / src / org / tizen / emulator / skin / layout / SkinPatches.java
1 /**
2  *
3  *
4  * Copyright (C) 2011 - 2012 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact:
7  * GiWoong Kim <giwoong.kim@samsung.com>
8  * YeongKyoon Lee <yeongkyoon.lee@samsung.com>
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
23  *
24  * Contributors:
25  * - S-Core Co., Ltd
26  *
27  */
28
29 package org.tizen.emulator.skin.layout;
30
31 import java.util.logging.Logger;
32
33 import org.eclipse.swt.SWT;
34 import org.eclipse.swt.graphics.Color;
35 import org.eclipse.swt.graphics.GC;
36 import org.eclipse.swt.graphics.Image;
37 import org.eclipse.swt.graphics.RGB;
38 import org.eclipse.swt.widgets.Display;
39 import org.tizen.emulator.skin.log.SkinLogger;
40
41 public class SkinPatches {
42         private Logger logger =
43                         SkinLogger.getSkinLogger(SkinPatches.class).getLogger();
44
45         private Display display;
46         private String pathImage;
47         private int patchWidth;
48         private int patchHeight;
49
50         /* nine patches */
51         private Image imageLT; /* left-top */
52         private Image imageT; /* middle-top */
53         private Image imageRT; /* right-top */
54
55         private Image imageL;
56         private Image imageR;
57
58         private Image imageLB; /* left-bottom */
59         private Image imageB;
60         private Image imageRB;
61
62         // TODO: configurable
63         private static final String SKIN_PATCH_IMG_LT = "LT.png";
64         private static final String SKIN_PATCH_IMG_T = "T.png";
65         private static final String SKIN_PATCH_IMG_RT = "RT.png";
66
67         private static final String SKIN_PATCH_IMG_L = "L.png";
68         private static final String SKIN_PATCH_IMG_R = "R.png";
69
70         private static final String SKIN_PATCH_IMG_LB = "LB.png";
71         private static final String SKIN_PATCH_IMG_B = "B.png";
72         private static final String SKIN_PATCH_IMG_RB = "RB.png";
73
74         public SkinPatches(String path) {
75                 this.display = Display.getCurrent();
76                 this.pathImage = path;
77
78                 loadPatches(pathImage);
79
80                 // TODO: configurable
81                 this.patchWidth = imageLT.getImageData().width;
82                 this.patchHeight = imageLT.getImageData().height;
83                 logger.info("patch size : " + patchWidth + "x" + patchHeight);
84         }
85
86         public int getPatchWidth() {
87                 return patchWidth;
88         }
89
90         public int getPatchHeight() {
91                 return patchHeight;
92         }
93
94         public Image getPatchedImage(int centerPatchWidth, int centerPatchHeight) {
95                 int patchedImageWidth = (patchWidth * 2) + centerPatchWidth;
96                 int patchedImageHeight = (patchHeight * 2) + centerPatchHeight;
97
98                 Image patchedImage = new Image(display,
99                                 patchedImageWidth, patchedImageHeight);
100
101                 // TODO: copy alphaData
102                 GC gc = new GC(patchedImage);
103                 gc.setBackground(display.getSystemColor(SWT.COLOR_MAGENTA));
104                 gc.fillRectangle(0, 0, patchedImageWidth, patchedImageHeight);
105
106                 /* top side */
107                 gc.drawImage(imageLT, 0, 0);
108                 gc.drawImage(imageT, 0, 0, imageT.getImageData().width, imageT.getImageData().height,
109                                 patchWidth, 0, centerPatchWidth, patchHeight);
110                 gc.drawImage(imageRT, patchWidth + centerPatchWidth, 0);
111
112                 /* middle side */
113                 gc.drawImage(imageL, 0, 0, imageL.getImageData().width, imageL.getImageData().height,
114                                 0, patchHeight, patchWidth, centerPatchHeight);
115                 gc.setBackground(new Color(display, new RGB(38, 38, 38)));
116                 gc.fillRectangle(patchWidth, patchHeight, centerPatchWidth, centerPatchHeight); /* center */
117                 gc.drawImage(imageR, 0, 0, imageR.getImageData().width, imageR.getImageData().height,
118                                 patchWidth + centerPatchWidth, patchHeight, patchWidth, centerPatchHeight);
119
120                 /* bottom side */
121                 gc.drawImage(imageLB, 0, patchHeight + centerPatchHeight);
122                 gc.drawImage(imageB, 0, 0, imageB.getImageData().width, imageB.getImageData().height,
123                                 patchWidth, patchHeight + centerPatchHeight, centerPatchWidth, patchHeight);
124                 gc.drawImage(imageRB, patchWidth + centerPatchWidth, patchHeight + centerPatchHeight);
125
126                 gc.dispose();
127
128                 return patchedImage;
129         }
130
131         private void loadPatches(String path) {
132                 ClassLoader loader = this.getClass().getClassLoader();
133
134                 imageLT = new Image(display,
135                                 loader.getResourceAsStream(path + SKIN_PATCH_IMG_LT));
136                 logger.info("left-top image is loaded from " +
137                                 path + SKIN_PATCH_IMG_LT);
138                 imageT = new Image(display,
139                                 loader.getResourceAsStream(path + SKIN_PATCH_IMG_T));
140                 imageRT = new Image(display,
141                                 loader.getResourceAsStream(path + SKIN_PATCH_IMG_RT));
142
143                 imageL = new Image(display,
144                                 loader.getResourceAsStream(path + SKIN_PATCH_IMG_L));
145                 imageR = new Image(display,
146                                 loader.getResourceAsStream(path + SKIN_PATCH_IMG_R));
147
148                 imageLB = new Image(display,
149                                 loader.getResourceAsStream(path + SKIN_PATCH_IMG_LB));
150                 imageB = new Image(display,
151                                 loader.getResourceAsStream(path + SKIN_PATCH_IMG_B));
152                 imageRB = new Image(display,
153                                 loader.getResourceAsStream(path + SKIN_PATCH_IMG_RB));
154         }
155
156         public void freePatches() {
157                 imageLT.dispose();
158                 imageT.dispose();
159                 imageRT.dispose();
160
161                 imageL.dispose();
162                 imageR.dispose();
163
164                 imageLB.dispose();
165                 imageB.dispose();
166                 imageRB.dispose();
167         }
168 }