forked from rubenwardy/NodeBoxEditor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Editor.cpp
285 lines (238 loc) · 8.4 KB
/
Editor.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
#include "Editor.h"
Editor::Editor()
:_state(NULL),_device(NULL),target(NULL),pivot(NULL),currentWindow(-1)
{
for (int i=0;i<4;i++){
camera[i] = NULL;
}
}
bool Editor::run(IrrlichtDevice* irr_device,Configuration* conf){
// Do Irrlicht Stuff
_device = irr_device;
IVideoDriver* driver = GetDevice()->getVideoDriver();
ISceneManager* smgr = GetDevice()->getSceneManager();
IGUIEnvironment* guienv = GetDevice()->getGUIEnvironment();
GetDevice()->setEventReceiver(this);
if (!conf->getSettingAsBool("fullscreen"))
GetDevice()->setResizable(true);
// Project and state
Project* proj = new Project();
_state = new EditorState(GetDevice(),proj,conf);
// Menu State
MenuState* ms = new MenuState(GetState());
GetState()->SetMenu(ms);
// Add editor modes
GetState()->AddMode(new NBEditor(GetState()));
// Set up project
proj->AddNode(new Node(GetDevice(),GetState()));
proj->SelectNode(0);
// Load user interface
LoadScene();
GetState()->Menu()->init();
GetState()->Mode()->load();
printf("Complete!\n");
int LastX = driver->getScreenSize().Width;
if (!GetState()->Settings()->getSettingAsBool("hide_sidebar"))
LastX -= 256;
int LastY = driver->getScreenSize().Height;
int lastFPS = -1;
while (GetDevice()->run()){
if (GetState()->NeedsClose()){
GetDevice()->closeDevice();
return true;
}
driver->beginScene(true, true, irr::video::SColor(255,150,150,150));
int ResX = driver->getScreenSize().Width;
if (!GetState()->Settings()->getSettingAsBool("hide_sidebar"))
ResX -= 256;
int ResY = driver->getScreenSize().Height;
if (currentWindow == -1)
{
// Draw Camera 0
if (camera[0]){
smgr->setActiveCamera(camera[0]);
rect<s32> offset = rect<s32>(0,0,ResX/2,ResY/2);
driver->setViewPort(offset);
smgr->drawAll();
if (GetState()->Mode())
GetState()->Mode()->viewportTick(EVIEW_PERS, driver, offset);
}
// Draw Camera 1
if (camera[1]){
smgr->setActiveCamera(camera[1]);
rect<s32> offset = rect<s32>(ResX/2,0,ResX,ResY/2);
driver->setViewPort(offset);
smgr->drawAll();
if (GetState()->Mode())
GetState()->Mode()->viewportTick(EVIEW_XZ, driver, offset);
}
// Draw Camera 2
if (camera[2]){
smgr->setActiveCamera(camera[2]);
rect<s32> offset = rect<s32>(0,ResY/2,ResX/2,ResY);
driver->setViewPort(offset);
smgr->drawAll();
if (GetState()->Mode())
GetState()->Mode()->viewportTick(EVIEW_XY, driver, offset);
}
// Draw Camera 3
if (camera[3]){
smgr->setActiveCamera(camera[3]);
rect<s32> offset = rect<s32>(ResX/2,ResY/2,ResX,ResY);
driver->setViewPort(offset);
smgr->drawAll();
if (GetState()->Mode())
GetState()->Mode()->viewportTick(EVIEW_ZY, driver, offset);
}
// Draw GUI
driver->setViewPort(rect<s32>(0,0,driver->getScreenSize().Width,driver->getScreenSize().Height));
driver->draw2DLine(vector2d<irr::s32>(0,ResY/2),vector2d<irr::s32>(ResX,ResY/2),SColor(175,255,255,255));
driver->draw2DLine(vector2d<irr::s32>(0,ResY/2-1),vector2d<irr::s32>(ResX,ResY/2-1),SColor(175,255,255,255));
driver->draw2DLine(vector2d<irr::s32>(ResX/2,0),vector2d<irr::s32>(ResX/2,ResY),SColor(175,255,255,255));
driver->draw2DLine(vector2d<irr::s32>(ResX/2+1,0),vector2d<irr::s32>(ResX/2+1,ResY),SColor(175,255,255,255));
}else if (camera[currentWindow]){
smgr->setActiveCamera(camera[currentWindow]);
driver->setViewPort(rect<s32>(0,0,ResX,ResY));
smgr->drawAll();
if (GetState()->Mode())
GetState()->Mode()->viewportTick((VIEWPORT)currentWindow, driver, rect<s32>(0,0,ResX,ResY));
driver->setViewPort(rect<s32>(0,0,driver->getScreenSize().Width,driver->getScreenSize().Height));
}
if (GetState()->Menu())
GetState()->Menu()->draw(driver);
if (GetState()->Mode())
GetState()->Mode()->draw(driver);
guienv->drawAll();
driver->endScene();
int fps = driver->getFPS();
if (lastFPS != fps)
{
irr::core::stringw str = L"Nodebox Editor [FPS: ";
str += fps;
str += "]";
GetDevice()->setWindowCaption(str.c_str());
lastFPS = fps;
}
if (LastX != ResX || LastY != ResY){
printf("Adjusting FOV to new screen size...\n");
LastX = ResX;
LastY = ResY;
camera[0]->setAspectRatio((float)ResX/(float)ResY);
// reset matrix
matrix4 projMat;
irr::f32 orth_w = (float)ResX / (float)ResY;
orth_w = 3 * orth_w;
projMat.buildProjectionMatrixOrthoLH(orth_w,3,1,100);
for (int i=1;i<4;i++){
if (camera[i]){
camera[i]->remove();
camera[i]=NULL;
}
}
// Remake cameras
camera[1]=smgr->addCameraSceneNode(target,vector3df(0,2,-0.01),vector3df(0,0,0));
camera[1]->setProjectionMatrix(projMat,true);
camera[2]=smgr->addCameraSceneNode(target,vector3df(0,0,-5),vector3df(0,0,0));
camera[2]->setProjectionMatrix(projMat,true);
camera[3]=smgr->addCameraSceneNode(target,vector3df(-5,0,0),vector3df(0,0,0));
camera[3]->setProjectionMatrix(projMat,true);
}
}
return true;
}
bool Editor::OnEvent(const SEvent& event)
{
if (event.EventType == irr::EET_MOUSE_INPUT_EVENT &&
event.MouseInput.Event == EMIE_LMOUSE_LEFT_UP ){
GetState()->mousedown=false;
}else if (event.EventType == irr::EET_MOUSE_INPUT_EVENT &&
event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN ){
GetState()->mousedown=true;
}else if (event.EventType == irr::EET_MOUSE_INPUT_EVENT &&
event.MouseInput.Event == EMIE_MOUSE_MOVED ){
GetState()->mouse_position.X=event.MouseInput.X;
GetState()->mouse_position.Y=event.MouseInput.Y;
}
if (GetState()->Mode()){
if (GetState()->Mode()->OnEvent(event)){
return true;
}
}
if (GetState()->Menu()){
if (GetState()->Menu()->OnEvent(event)){
return true;
}
}
if (event.EventType == EET_KEY_INPUT_EVENT && event.KeyInput.Key < NUMBER_OF_KEYS){
if (event.KeyInput.PressedDown){
GetState()->keys[event.KeyInput.Key] = EKS_DOWN;
}else{
GetState()->keys[event.KeyInput.Key] = EKS_UP;
}
}
if (event.EventType == EET_GUI_EVENT){
if (event.GUIEvent.EventType == EGET_MENU_ITEM_SELECTED){
IGUIContextMenu* menu = (IGUIContextMenu*)event.GUIEvent.Caller;
switch (menu->getItemCommandId(menu->getSelectedItem())){
case GUI_VIEW_SP_ALL:
currentWindow = -1;
break;
case GUI_VIEW_SP_PER:
currentWindow = 0;
break;
case GUI_VIEW_SP_TOP:
currentWindow = 1;
break;
case GUI_VIEW_SP_FRT:
currentWindow = 2;
break;
case GUI_VIEW_SP_RHT:
currentWindow = 3;
break;
}
}
}
return false;
}
void Editor::LoadScene(){
IVideoDriver* driver = GetDevice()->getVideoDriver();
ISceneManager* smgr = GetDevice()->getSceneManager();
// Calculate Projection Matrix
matrix4 projMat;
irr::f32 orth_w = (float)(driver->getScreenSize().Width-256) / (float)driver->getScreenSize().Height;
orth_w = 3 * orth_w;
projMat.buildProjectionMatrixOrthoLH(orth_w,3,1,100);
// Create target
target = smgr->addEmptySceneNode(0,200);
target->setPosition(vector3df(0,0,0));
// Add rotational camera
pivot=smgr->addEmptySceneNode(target,199);
camera[0]=smgr->addCameraSceneNode(NULL,vector3df(0,0,-2),vector3df(0,0,0));
camera[0]->setParent(pivot);
pivot->setRotation(vector3df(25,-45,0));
// Add Topdown camera
camera[1]=smgr->addCameraSceneNode(target,vector3df(0,2,-0.01),vector3df(0,0,0));
camera[1]->setProjectionMatrix(projMat,true);
// Add front camera
camera[2]=smgr->addCameraSceneNode(target,vector3df(0,0,-5),vector3df(0,0,0));
camera[2]->setProjectionMatrix(projMat,true);
// Add side camera
camera[3]=smgr->addCameraSceneNode(target,vector3df(5,0,0),vector3df(0,0,0));
camera[3]->setProjectionMatrix(projMat,true);
// Add Light
ILightSceneNode* light=smgr->addLightSceneNode(target,vector3df(25,50,0));
light->setLightType(ELT_POINT);
light->setRadius(2000);
// Add Plane
IMeshSceneNode* plane = smgr->addCubeSceneNode(1,0,-1,vector3df(0.5,-5.5,0.5),vector3df(0,0,0),vector3df(10,10,10));
plane->setMaterialTexture(0, driver->getTexture("media/texture_terrain.png"));
plane->setMaterialFlag(video::EMF_BILINEAR_FILTER, false);
plane->getMaterial(0).getTextureMatrix(0).setTextureScale(10,10);
// Add sky box
scene::IMeshSceneNode* skybox=smgr->addCubeSceneNode(50);
skybox->setMaterialTexture(0, driver->getTexture("media/sky.jpg"));
skybox->setMaterialFlag(video::EMF_BILINEAR_FILTER, false);
skybox->setMaterialFlag(video::EMF_LIGHTING,false);
smgr->getMeshManipulator()->flipSurfaces(skybox->getMesh());
GetState()->plane_tri=smgr->createOctreeTriangleSelector(skybox->getMesh(),skybox);
}