Merge branch 'fixes' into main/rendor-staging
[ryzomcore.git] / ryzom / tools / leveldesign / georges_dll / icon_wnd.cpp
blob9eb9661d9dbfe74f56a69c68ef31e714d86537ad
1 // Ryzom - MMORPG Framework <http://dev.ryzom.com/projects/ryzom/>
2 // Copyright (C) 2010 Winch Gate Property Limited
3 //
4 // This source file has been modified by the following contributors:
5 // Copyright (C) 2019 Jan BOON (Kaetemi) <jan.boon@kaetemi.be>
6 //
7 // This program is free software: you can redistribute it and/or modify
8 // it under the terms of the GNU Affero General Public License as
9 // published by the Free Software Foundation, either version 3 of the
10 // License, or (at your option) any later version.
12 // This program is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU Affero General Public License for more details.
17 // You should have received a copy of the GNU Affero General Public License
18 // along with this program. If not, see <http://www.gnu.org/licenses/>.
20 // icon_wnd.cpp: implementation of the CIconWnd class.
23 #include "stdafx.h"
24 #include "georges_edit.h"
25 #include "icon_wnd.h"
27 #include "nel/misc/path.h"
28 #include "nel/misc/file.h"
29 #include "nel/misc/bitmap.h"
31 using namespace NLMISC;
32 using namespace std;
34 string CIconWnd::IconPath;
37 CIconWnd::CIconWnd()
39 pWndIcon = NULL;
40 pWndIconColor = NULL;
41 pWndIconBack = NULL;
42 pWndIconBackColor = NULL;
43 pWndIconOver = NULL;
44 pWndIconOverColor = NULL;
45 pWndIconOver2 = NULL;
46 pWndIconOver2Color = NULL;
49 CIconWnd::~CIconWnd()
54 BEGIN_MESSAGE_MAP(CIconWnd, CWnd)
55 //{{AFX_MSG_MAP(CIconWnd)
56 ON_WM_PAINT()
57 //}}AFX_MSG_MAP
58 END_MESSAGE_MAP()
61 void CIconWnd::OnPaint()
63 CPaintDC dc(this);
65 // update data
66 if (updateStr())
67 updateIcon();
69 // Get client dc
70 RECT client;
71 GetClientRect(&client);
72 dc.Rectangle(&client);
74 for (uint y=0 ; y<bitmap.getHeight() ; y++)
76 for (uint x=0 ; x<bitmap.getWidth() ; x++)
78 CRGBA c = bitmap.getPixelColor(x, y);
79 dc.SetPixel(x, y, RGB(c.R, c.G, c.B));
84 void CIconWnd::create (DWORD wStyle, RECT &pos, CWnd *parent, uint dialogIndex)
86 Id = dialogIndex;
87 // Register window class
88 LPCTSTR className = AfxRegisterWndClass(CS_OWNDC);
90 // Create this window
91 CWnd::Create(className, _T("empty"), wStyle, pos, parent, dialogIndex);
94 bool CIconWnd::updateStr()
96 // check if icons need to be recomputed
97 bool needUpdate = false;
99 if (updateWnd(pWndIcon, strIcon))
100 needUpdate = true;
102 if (updateWnd(pWndIconColor, strIconColor))
103 needUpdate = true;
105 if (updateWnd(pWndIconBack, strIconBack))
106 needUpdate = true;
108 if (updateWnd(pWndIconBackColor, strIconBackColor))
109 needUpdate = true;
111 if (updateWnd(pWndIconOver, strIconOver))
112 needUpdate = true;
114 if (updateWnd(pWndIconOverColor, strIconOverColor))
115 needUpdate = true;
117 if (updateWnd(pWndIconOver2, strIconOver2))
118 needUpdate = true;
120 if (updateWnd(pWndIconOver2Color, strIconOver2Color))
121 needUpdate = true;
123 return needUpdate;
126 void CIconWnd::updateIcon()
128 // Get IconPath
129 if (IconPath == "")
131 CConfigFile::CVar *var = theApp.ConfigFile.getVarPtr("IconPath");
132 if (var)
134 IconPath = var->asString();
135 for (uint i=0 ; i<var->size() ; i++)
136 CPath::addSearchPath(var->asString(i), true, false, NULL);
138 else
140 nlinfo("!! IconPath missing in .cfg, Please complete the config file !!");
141 IconPath = "\\\\amiga\\3d\\Database\\Interfaces\\";
142 nlinfo("default to : IconPath = \"\\\\amiga\\3d\\Database\\Interfaces\\\"");
143 CPath::addSearchPath(IconPath, true, false, NULL);
147 NLMISC::CBitmap icon;
148 NLMISC::CRGBA color;
150 bitmap.reset();
151 bitmap.convertToType(NLMISC::CBitmap::RGBA);
152 bitmap.resample(40, 40);
154 // back icon
155 if (loadIcon(strIconBack, icon))
157 // back icon color
158 if (getColorFromStr(strIconBackColor, color))
159 modulateIcon(icon, color);
161 bitmap = icon;
163 // base icon
164 addIconLayer(bitmap, strIcon, strIconColor);
166 else
168 // base icon
169 loadIcon(strIcon, icon);
171 // base icon color
172 if (getColorFromStr(strIconColor, color))
173 modulateIcon(icon, color);
175 bitmap = icon;
178 // overlay icon
179 addIconLayer(bitmap, strIconOver, strIconOverColor);
181 // overlay 2 icon
182 addIconLayer(bitmap, strIconOver2, strIconOver2Color);
185 bool CIconWnd::loadIcon(const std::string &filename, NLMISC::CBitmap &bmp)
187 // Try to get the file path
188 string filepath = CPath::lookup(filename, false, false);
189 if (filepath == "")
191 bmp.makeDummy();
192 bmp.convertToType(NLMISC::CBitmap::RGBA);
193 bmp.resample(40, 40);
194 return false;
197 // load icon
198 CIFile f;
199 f.open(filepath);
201 bmp.load(f);
202 bmp.convertToType(NLMISC::CBitmap::RGBA);
203 bmp.resample(40, 40);
205 f.close();
207 return true;
210 bool CIconWnd::getColorFromStr(const std::string &s, NLMISC::CRGBA &c)
212 // Convert string to color
214 sint r, g, b;
215 if (sscanf (s.c_str(), "%d,%d,%d", &r, &g, &b) == 3)
217 clamp (r, 0, 255);
218 clamp (g, 0, 255);
219 clamp (b, 0, 255);
220 c = CRGBA(r, g, b);
221 return true;
224 return false;
227 void CIconWnd::blendIcons(NLMISC::CBitmap &dst, const NLMISC::CBitmap &src)
229 // blend between two icons
231 nlassert(dst.getWidth() == src.getWidth());
232 nlassert(dst.getHeight() == src.getHeight());
234 CObjectVector<uint8> &data = dst.getPixels();
236 for (uint y=0 ; y<dst.getHeight() ; y++)
238 for (uint x=0 ; x<dst.getWidth() ; x++)
240 CRGBA c;
241 c.blendFromui(dst.getPixelColor(x, y), src.getPixelColor(x, y), src.getPixelColor(x, y).A);
243 data[(x+y*dst.getWidth())*4] = c.R;
244 data[(x+y*dst.getWidth())*4+1] = c.G;
245 data[(x+y*dst.getWidth())*4+2] = c.B;
246 data[(x+y*dst.getWidth())*4+3] = c.A;
252 void CIconWnd::modulateIcon(NLMISC::CBitmap &dst, const NLMISC::CRGBA &col)
254 // modulate an icon by a color
256 CObjectVector<uint8> &data = dst.getPixels();
258 for (uint y=0 ; y<dst.getHeight() ; y++)
260 for (uint x=0 ; x<dst.getWidth() ; x++)
262 CRGBA c;
263 c.modulateFromColor(col, dst.getPixelColor(x, y));
265 data[(x+y*dst.getWidth())*4] = c.R;
266 data[(x+y*dst.getWidth())*4+1] = c.G;
267 data[(x+y*dst.getWidth())*4+2] = c.B;
268 data[(x+y*dst.getWidth())*4+3] = dst.getPixelColor(x, y).A;
273 void CIconWnd::addIconLayer(NLMISC::CBitmap &dst, const std::string iconStr, const std::string iconCol)
275 NLMISC::CBitmap icon;
276 NLMISC::CRGBA color;
278 if (loadIcon(iconStr, icon))
280 if (getColorFromStr(iconCol, color))
281 modulateIcon(icon, color);
283 blendIcons(dst, icon);
287 bool CIconWnd::updateWnd(CWnd *pWnd, std::string &str)
289 TCHAR buffer[512];
291 if (pWnd)
293 pWnd->GetWindowText(buffer, 512);
294 std::string buf = tStrToUtf8(buffer);
295 if (buf != str)
297 str = buf;
298 return true;
302 return false;