!I (1670414, 1670415, 1670416, 1670424, 1670431):
[CRYENGINE.git] / Code / Sandbox / EditorQt / MaterialSender.h
blob597507b2ebba6cd142957d73818cd452c30b9c11
1 // Copyright 2001-2018 Crytek GmbH / Crytek Group. All rights reserved.
3 #ifndef __MaterialSender_h__
4 #define __MaterialSender_h__
5 #pragma once
7 #define WM_MATEDITSEND (WM_USER + 315)
9 enum EMaterialSenderMessage
11 eMSM_Create = 1,
12 eMSM_GetSelectedMaterial = 2,
13 eMSM_Init = 3,
16 struct SMaterialMapFileHeader
18 // max
19 void SetMaxHWND(HWND hWnd)
21 hwndMax = (int64)hWnd;
23 HWND GetMaxHWND() const
25 return (HWND)hwndMax;
27 // editor
28 void SetEditorHWND(HWND hWnd)
30 hwndMatEdit = (int64)hWnd;
32 HWND GetEditorHWND() const
34 return (HWND)hwndMatEdit;
36 int64 msg;// 64bits for both 32 and 64
37 int64 Reserved;// 64bits for both 32 and 64
38 protected:
39 uint64 hwndMax;// HWND for 32 and 64 is different
40 uint64 hwndMatEdit;// HWND for 32 and 64 is different
43 class CMaterialSender
45 public:
47 CMaterialSender(bool bIsMatEditor) : m_bIsMatEditor(bIsMatEditor)
49 m_h.SetEditorHWND(0);
50 m_h.SetMaxHWND(0);
51 m_h.msg = 0;
52 hMapFile = 0;
55 CMaterialSender::~CMaterialSender()
57 if (hMapFile)
58 CloseHandle(hMapFile);
59 hMapFile = 0;
62 bool GetMessage()
64 LoadMapFile();
65 return true;
68 bool CheckWindows()
70 if (!m_h.GetMaxHWND() || !m_h.GetEditorHWND() || !::IsWindow(m_h.GetMaxHWND()) || !::IsWindow(m_h.GetEditorHWND()))
71 LoadMapFile();
72 if (!m_h.GetMaxHWND() || !m_h.GetEditorHWND() || !::IsWindow(m_h.GetMaxHWND()) || !::IsWindow(m_h.GetEditorHWND()))
73 return false;
74 return true;
77 bool Create()
79 if (hMapFile)
80 CloseHandle(hMapFile);
81 hMapFile = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, 1024 * 1024, "EditMatMappingObject");
82 if (hMapFile)
83 return true;
85 CryLog("Can't create File Map");
87 return false;
90 bool SendMessage(int msg, const XmlNodeRef& node)
92 bool bRet = false;
94 if (!CheckWindows())
95 return false;
97 m_h.msg = msg;
99 int nDataSize = sizeof(SMaterialMapFileHeader) + strlen(node->getXML().c_str());
101 //hMapFile = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, nDataSize, "EditMatMappingObject");
103 HANDLE hMapFile = OpenFileMapping(FILE_MAP_ALL_ACCESS, FALSE, "EditMatMappingObject");
104 if (hMapFile)
106 void* pMes = MapViewOfFile(hMapFile, FILE_MAP_ALL_ACCESS, 0, 0, nDataSize);
107 if (pMes)
109 memcpy(pMes, &m_h, sizeof(SMaterialMapFileHeader));
110 strcpy(((char*)pMes) + sizeof(SMaterialMapFileHeader), node->getXML().c_str());
111 UnmapViewOfFile(pMes);
112 if (m_bIsMatEditor)
113 ::SendMessage(m_h.GetMaxHWND(), WM_MATEDITSEND, msg, 0);
114 else
115 ::SendMessage(m_h.GetEditorHWND(), WM_MATEDITSEND, msg, 0);
116 bRet = true;
118 CloseHandle(hMapFile);
120 else
121 CryLog("No File Map");
123 return bRet;
126 void SetupWindows(HWND hwndMax, HWND hwndMatEdit)
128 m_h.SetMaxHWND(hwndMax);
129 m_h.SetEditorHWND(hwndMatEdit);
132 private:
134 bool LoadMapFile()
136 bool bRet = false;
137 const HANDLE hMapFile = OpenFileMapping(FILE_MAP_ALL_ACCESS, FALSE, "EditMatMappingObject");
138 if (hMapFile)
140 void* const pMes = MapViewOfFile(hMapFile, FILE_MAP_ALL_ACCESS, 0, 0, 0);
142 if (pMes)
144 memcpy(&m_h, pMes, sizeof(SMaterialMapFileHeader));
145 const char* const pXml = ((const char*)pMes) + sizeof(SMaterialMapFileHeader);
146 m_node = XmlHelpers::LoadXmlFromBuffer(pXml, strlen(pXml));
147 UnmapViewOfFile(pMes);
148 bRet = true;
151 CloseHandle(hMapFile);
154 return bRet;
157 public:
158 SMaterialMapFileHeader m_h;
159 XmlNodeRef m_node;
160 private:
161 bool m_bIsMatEditor;
162 HANDLE hMapFile;
165 #endif //__MaterialSender_h__