Merge branch 'fixes' into main/rendor-staging
[ryzomcore.git] / nel / tools / 3d / object_viewer / texture_chooser.cpp
blobf8416ca36b41b235987826e420a4817f906a0209
1 // NeL - MMORPG Framework <http://dev.ryzom.com/projects/nel/>
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 #include "std_afx.h"
21 #include "object_viewer.h"
22 #include "texture_chooser.h"
23 #include "multi_tex_dlg.h"
25 #include "nel/3d/texture_file.h"
26 #include "nel/3d/texture_bump.h"
27 #include "nel/3d/ps_particle_basic.h"
29 #include "nel/misc/path.h"
31 // size of the bitmap that is displayed
32 const uint tSize = 25;
34 /////////////////////////////////////////////////////////////////////////////
35 // CTextureChooser dialog
38 CTextureChooser::CTextureChooser(NL3D::CPSMultiTexturedParticle *mtp /*= NULL*/, CParticleWorkspace::CNode *ownerNode)
39 : _CurrBitmap(0),
40 _Wrapper(NULL),
41 _Texture(NULL),
42 _EnableRemoveButton(false),
43 _MTP(mtp),
44 _Node(ownerNode),
45 _MultiTexDlg(NULL)
48 //{{AFX_DATA_INIT(CTextureChooser)
49 //}}AFX_DATA_INIT
52 CTextureChooser::~CTextureChooser()
54 if (_MultiTexDlg)
56 _MultiTexDlg->DestroyWindow();
57 delete _MultiTexDlg;
59 if (_CurrBitmap)
60 ::DeleteObject(_CurrBitmap);
63 BOOL CTextureChooser::EnableWindow( BOOL bEnable)
65 GetDlgItem(IDC_BROWSE_TEXTURE)->EnableWindow(bEnable);
66 GetDlgItem(IDC_REMOVE_TEXTURE)->EnableWindow(bEnable);
67 if (_MTP)
69 GetDlgItem(IDC_ENABLE_MULTITEXTURING)->EnableWindow(bEnable);
70 GetDlgItem(IDC_EDIT_MULTITEXTURING)->EnableWindow((bEnable ? true: false /* VC WARNING */) & _MTP->isMultiTextureEnabled());
72 return CEditAttribDlg::EnableWindow(bEnable);
75 void CTextureChooser::DoDataExchange(CDataExchange* pDX)
77 CDialog::DoDataExchange(pDX);
78 //{{AFX_DATA_MAP(CTextureChooser)
79 DDX_Control(pDX, IDC_ENABLE_MULTITEXTURING, m_MultiTexCtrl);
80 //}}AFX_DATA_MAP
84 void CTextureChooser::init(uint32 x, uint32 y, CWnd *pParent)
86 Create(IDD_TEXTURE_CHOOSER, pParent);
87 RECT r;
88 GetClientRect(&r);
89 MoveWindow(x, y, r.right, r.bottom);
91 if (!_EnableRemoveButton)
93 GetDlgItem(IDC_REMOVE_TEXTURE)->ShowWindow(SW_HIDE);
95 if (!_MTP)
97 GetDlgItem(IDC_EDIT_MULTITEXTURING)->ShowWindow(SW_HIDE);
98 m_MultiTexCtrl.ShowWindow(SW_HIDE);
99 GetDlgItem(IDC_MULTITEX_BORDER)->ShowWindow(SW_HIDE);
101 else
103 if (_MTP->isMultiTextureEnabled())
105 m_MultiTexCtrl.SetCheck(1);
107 updateMultiTexCtrl();
109 ShowWindow(SW_SHOW);
113 void CTextureChooser::textureToBitmap()
115 if (!_Texture)
117 if (_CurrBitmap)
119 ::DeleteObject(_CurrBitmap);
120 _CurrBitmap = NULL;
123 return;
126 if (_CurrBitmap)
128 ::DeleteObject(_CurrBitmap);
130 // for a bumpmap, show its heightmap
131 NL3D::CTextureBump *tb = dynamic_cast<NL3D::CTextureBump *>( (NL3D::ITexture *) _Texture);
132 NL3D::ITexture *tex = tb ? tb->getHeightMap() : _Texture;
133 if (!tex) tex = _Texture;
134 tex->generate();
135 // make copy of the texture
136 NLMISC::CBitmap cb(* ((NL3D::ITexture *) tex));
138 cb.convertToType(NLMISC::CBitmap::RGBA);
139 cb.resample(tSize, tSize);
141 uint32 *dat = (uint32 *) &(cb.getPixels()[0]);
142 _CurrBitmap = ::CreateBitmap(tSize, tSize, 1, 32, dat);
143 tex->release();
144 Invalidate();
145 UpdateData(TRUE);
150 BEGIN_MESSAGE_MAP(CTextureChooser, CDialog)
151 //{{AFX_MSG_MAP(CTextureChooser)
152 ON_WM_PAINT()
153 ON_BN_CLICKED(IDC_REMOVE_TEXTURE, OnRemoveTexture)
154 ON_BN_CLICKED(IDC_EDIT_MULTITEXTURING, OnEditMultitexturing)
155 ON_BN_CLICKED(IDC_ENABLE_MULTITEXTURING, OnEnableMultitexturing)
156 ON_BN_CLICKED(IDC_BROWSE_TEXTURE, OnBrowseTexture)
157 //}}AFX_MSG_MAP
158 END_MESSAGE_MAP()
160 /////////////////////////////////////////////////////////////////////////////
161 // CTextureChooser message handlers
163 BOOL CTextureChooser::OnInitDialog()
165 CDialog::OnInitDialog();
166 nlassert(_Wrapper);
167 _Texture = _Wrapper->get();
169 // generate the bitmap
171 textureToBitmap();
173 return TRUE; // return TRUE unless you set the focus to a control
174 // EXCEPTION: OCX Property Pages should return FALSE
177 void CTextureChooser::OnBrowseTexture()
179 std::string texName("*.tga");
180 /// get the name of the previously set texture if there is one
181 if (dynamic_cast<NL3D::CTextureFile *>(_Wrapper->get()))
183 texName = (static_cast<NL3D::CTextureFile *>(_Wrapper->get()))->getFileName();
185 CFileDialog fd(TRUE, _T(".tga"), nlUtf8ToTStr(texName), 0, NULL, this);
186 if (fd.DoModal() == IDOK)
188 // Add search path for the texture
189 NLMISC::CPath::addSearchPath(NLMISC::CFile::getPath(NLMISC::tStrToUtf8(fd.GetPathName())));
193 NL3D::CTextureFile *tf = new NL3D::CTextureFile(NLMISC::tStrToUtf8(fd.GetFileName()));
194 _Wrapper->setAndUpdateModifiedFlag(tf);
195 _Texture = tf;
196 textureToBitmap();
198 catch (const NLMISC::Exception &e)
200 MessageBox(nlUtf8ToTStr(e.what()), _T("error loading texture"));
206 void CTextureChooser::OnPaint()
208 CPaintDC dc(this); // device context for painting
210 if (_CurrBitmap)
212 HDC bitmapDc = ::CreateCompatibleDC(dc);
213 HGDIOBJ old = ::SelectObject(bitmapDc, _CurrBitmap);
215 ::BitBlt(dc, 10, 10, tSize, tSize, bitmapDc, 0, 0, SRCCOPY);
217 ::SelectObject(bitmapDc, old);
218 ::DeleteDC(bitmapDc);
222 void CTextureChooser::OnRemoveTexture()
224 if (_Texture)
226 _Texture->release();
227 _Texture = NULL;
229 _Wrapper->setAndUpdateModifiedFlag(NULL);
230 textureToBitmap();
231 Invalidate();
236 void CTextureChooser::childPopupClosed(CWnd *child)
238 nlassert(child == _MultiTexDlg);
239 _MultiTexDlg->DestroyWindow();
240 delete _MultiTexDlg;
241 _MultiTexDlg = NULL;
242 EnableWindow(TRUE);
245 void CTextureChooser::updateMultiTexCtrl()
247 GetDlgItem(IDC_EDIT_MULTITEXTURING)->EnableWindow(_MTP->isMultiTextureEnabled());
251 void CTextureChooser::OnEditMultitexturing()
253 nlassert(_MTP);
254 EnableWindow(FALSE);
255 _MultiTexDlg = new CMultiTexDlg(_Node, _MTP, this, this);
256 _MultiTexDlg->init(this);
259 void CTextureChooser::OnEnableMultitexturing()
261 _MTP->enableMultiTexture(m_MultiTexCtrl.GetCheck() ? true : false /* VC warning */);
262 updateMultiTexCtrl();