1 // NeL - MMORPG Framework <http://dev.ryzom.com/projects/nel/>
2 // Copyright (C) 2010 Winch Gate Property Limited
4 // This source file has been modified by the following contributors:
5 // Copyright (C) 2019 Jan BOON (Kaetemi) <jan.boon@kaetemi.be>
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/>.
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
)
42 _EnableRemoveButton(false),
48 //{{AFX_DATA_INIT(CTextureChooser)
52 CTextureChooser::~CTextureChooser()
56 _MultiTexDlg
->DestroyWindow();
60 ::DeleteObject(_CurrBitmap
);
63 BOOL
CTextureChooser::EnableWindow( BOOL bEnable
)
65 GetDlgItem(IDC_BROWSE_TEXTURE
)->EnableWindow(bEnable
);
66 GetDlgItem(IDC_REMOVE_TEXTURE
)->EnableWindow(bEnable
);
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
);
84 void CTextureChooser::init(uint32 x
, uint32 y
, CWnd
*pParent
)
86 Create(IDD_TEXTURE_CHOOSER
, pParent
);
89 MoveWindow(x
, y
, r
.right
, r
.bottom
);
91 if (!_EnableRemoveButton
)
93 GetDlgItem(IDC_REMOVE_TEXTURE
)->ShowWindow(SW_HIDE
);
97 GetDlgItem(IDC_EDIT_MULTITEXTURING
)->ShowWindow(SW_HIDE
);
98 m_MultiTexCtrl
.ShowWindow(SW_HIDE
);
99 GetDlgItem(IDC_MULTITEX_BORDER
)->ShowWindow(SW_HIDE
);
103 if (_MTP
->isMultiTextureEnabled())
105 m_MultiTexCtrl
.SetCheck(1);
107 updateMultiTexCtrl();
113 void CTextureChooser::textureToBitmap()
119 ::DeleteObject(_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
;
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
);
150 BEGIN_MESSAGE_MAP(CTextureChooser
, CDialog
)
151 //{{AFX_MSG_MAP(CTextureChooser)
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
)
160 /////////////////////////////////////////////////////////////////////////////
161 // CTextureChooser message handlers
163 BOOL
CTextureChooser::OnInitDialog()
165 CDialog::OnInitDialog();
167 _Texture
= _Wrapper
->get();
169 // generate the bitmap
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
);
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
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()
229 _Wrapper
->setAndUpdateModifiedFlag(NULL
);
236 void CTextureChooser::childPopupClosed(CWnd
*child
)
238 nlassert(child
== _MultiTexDlg
);
239 _MultiTexDlg
->DestroyWindow();
245 void CTextureChooser::updateMultiTexCtrl()
247 GetDlgItem(IDC_EDIT_MULTITEXTURING
)->EnableWindow(_MTP
->isMultiTextureEnabled());
251 void CTextureChooser::OnEditMultitexturing()
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();