forked from rubenwardy/NodeBoxEditor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NBEditor.cpp
333 lines (284 loc) · 8.91 KB
/
NBEditor.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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
#include "NBEditor.h"
NBEditor::NBEditor(EditorState* st)
:EditorMode(st),current(-1)
{
// Top Window
cdrs[0] = CDR(EVIEW_XZ,CDR_X_P);
cdrs[1] = CDR(EVIEW_XZ,CDR_X_N);
cdrs[2] = CDR(EVIEW_XZ,CDR_Z_P);
cdrs[3] = CDR(EVIEW_XZ,CDR_Z_N);
cdrs[4] = CDR(EVIEW_XZ,CDR_XZ);
// Front window
cdrs[5] = CDR(EVIEW_XY,CDR_X_P);
cdrs[6] = CDR(EVIEW_XY,CDR_X_N);
cdrs[7] = CDR(EVIEW_XY,CDR_Y_P);
cdrs[8] = CDR(EVIEW_XY,CDR_Y_N);
cdrs[9] = CDR(EVIEW_XY,CDR_XY);
// Side window
cdrs[10] = CDR(EVIEW_ZY,CDR_Y_P);
cdrs[11] = CDR(EVIEW_ZY,CDR_Y_N);
cdrs[12] = CDR(EVIEW_ZY,CDR_Z_P);
cdrs[13] = CDR(EVIEW_ZY,CDR_Z_N);
cdrs[14] = CDR(EVIEW_ZY,CDR_ZY);
// Snappers
for (int i=0;i<17;i++){
snappers[i]=NULL;
}
printf("Making 16 pixel snap grid: \n");
for (int a=-(NODE_RES/2);a<(NODE_RES/2)+1;a++){
snappers[a+(NODE_RES/2)] = a*((float)1/(float)NODE_RES);
printf(">> %f\n",snappers[a+8]);
}
}
void NBEditor::load(){
IGUIStaticText* sidebar = GetState()->Menu()->GetSideBar();
IGUIEnvironment* guienv = GetState()->GetDevice()->getGUIEnvironment();
sidebar->setText(L"Node boxes");
IGUIStaticText* t = guienv->addStaticText(L"No node selected",rect<s32>(20,30,140,100),false,true,sidebar,GUI_SIDEBAR_LABEL);
if (t)
t->setVisible(false);
IGUIListBox* lb = guienv->addListBox(rect<s32>(20,30,230,128),sidebar,GUI_SIDEBAR_LISTBOX,true);
if (lb){
lb->setVisible(false);
IGUIButton* b = guienv->addButton(rect<s32>(20-20,130-30,70-20,155-30),lb,GUI_PROJ_NEW_BOX,L"+",L"Add a node box");
IGUIButton* c = guienv->addButton(rect<s32>(80-20,130-30,130-20,155-30),lb,GUI_PROJ_DELETE_BOX,L"-",L"Delete node box");
b->setNotClipped(true);
c->setNotClipped(true);
}
load_ui();
}
void NBEditor::load_ui(){
IGUIStaticText* sidebar = GetState()->Menu()->GetSideBar();
IGUIEnvironment* guienv = GetState()->GetDevice()->getGUIEnvironment();
Node* node = GetState()->project->GetCurrentNode();
if (!node){
sidebar->getElementFromId(GUI_SIDEBAR_LABEL)->setVisible(true);
sidebar->getElementFromId(GUI_SIDEBAR_LISTBOX)->setVisible(false);
}else{
IGUIListBox* lb = (IGUIListBox*) sidebar->getElementFromId(GUI_SIDEBAR_LISTBOX);
sidebar->getElementFromId(GUI_SIDEBAR_LABEL)->setVisible(false);
if (lb){
lb->clear();
lb->setVisible(true);
for (int i = 0;i<NODEB_MAX;i++){
NodeBox* box = node->GetNodeBox(i);
if (box){
size_t origsize = strlen(box->name.c_str()) + 1;
static wchar_t wcstring[1024];
mbstowcs(wcstring, box->name.c_str(), origsize);
wcscat(wcstring, L"");
lb->addItem(wcstring);
}
}
lb->setSelected(lb->getListItem(node->GetId()));
}
}
}
void NBEditor::unload(){
}
void NBEditor::update(){
}
void NBEditor::draw(irr::video::IVideoDriver* driver){
if (wasmd && !GetState()->mousedown){
current = -1;
}
for (int i=0;i<15;i++){
if (cdrs[i].visible){
rect<s32> drawarea = rect<s32>(cdrs[i].position.X-5,cdrs[i].position.Y-5,cdrs[i].position.X+5,cdrs[i].position.Y+5);
if (!wasmd && GetState()->mousedown && drawarea.isPointInside(GetState()->mouse_position)){
current = i;
}
SColor color = SColor(255,255,255,255);
if (current == i){
color = SColor(255,0,0,255);
}else if (GetState()->keys[KEY_LCONTROL]==EKS_DOWN){
color = SColor(255,255,255,0);
}
driver->draw2DImage(
driver->getTexture("media/gui_scale.png"),
drawarea.UpperLeftCorner,
rect<s32>(0,0,10,10),NULL,color,true
);
cdrs[i].visible = false;
}
}
if (!wasmd && GetState()->mousedown){
wasmd = true;
}else if (wasmd && !GetState()->mousedown){
wasmd = false;
}
}
void NBEditor::viewportTick(VIEWPORT window,irr::video::IVideoDriver* driver,rect<s32> offset){
for (int i=0;i<15;i++){
if (cdrs[i].window == window){
cdrs[i].update(this,(current == i),offset);
}
}
}
void CDR::update(NBEditor* editor,bool drag,rect<s32> offset){
if (!editor->GetState()->project){
printf("Project for NBEditor::updatePoint() not found!\n");
return;
}
if (!editor->GetState()->Settings()->getSettingAsBool("always_show_position_handle")){
if (editor->GetState()->keys[KEY_LSHIFT]==EKS_UP){
if (type == CDR_XY || type == CDR_XZ || type == CDR_ZY){
return;
}
}else{
if (!(type == CDR_XY || type == CDR_XZ || type == CDR_ZY)){
return;
}
}
}
Node* node = editor->GetState()->project->GetCurrentNode();
if (!node)
return;
NodeBox* box = node->GetCurrentNodeBox();
if (!box)
return;
if (drag){
// get mouse position
position2di target = editor->GetState()->mouse_position;
target.X -= 5;
target.Y -= 5;
target -= offset.UpperLeftCorner;
// get the ray
line3d<irr::f32> ray = editor->GetState()->GetDevice()->getSceneManager()->getSceneCollisionManager()->getRayFromScreenCoordinates(target,editor->GetState()->GetDevice()->getSceneManager()->getActiveCamera());
// contains the output values
vector3df wpos = vector3df(0,0,0); // the collision position
#if IRRLICHT_VERSION_MAJOR == 1 && IRRLICHT_VERSION_MINOR < 8
const ISceneNode* tmpNode; // not needed, but required for function
#else
ISceneNode* tmpNode; // not needed, but required for function
#endif
triangle3df tmpTri; // not needed, but required for function
// Execute ray
editor->GetState()->GetDevice()->getSceneManager()->getSceneCollisionManager()->getCollisionPoint(ray,editor->GetState()->plane_tri,wpos,tmpTri,tmpNode);
// Snapping
wpos -= vector3df(node->getPosition().X,node->getPosition().Y,node->getPosition().Z);
if (editor->GetState()->Settings()->getSettingAsBool("snapping")==true){
for (int i=0;i<15;i++){
if (wpos.X > editor->snappers[i]-0.0313 && wpos.X < editor->snappers[i]+0.0313){
wpos.X = editor->snappers[i];
}
if (wpos.Y > editor->snappers[i]-0.0313 && wpos.Y < editor->snappers[i]+0.0313){
wpos.Y = editor->snappers[i];
}
if (wpos.Z > editor->snappers[i]-0.0313 && wpos.Z < editor->snappers[i]+0.0313){
wpos.Z = editor->snappers[i];
}
}
}
// Do node limiting
if (editor->GetState()->Settings()->getSettingAsBool("limiting")==true){
// X Axis
if (wpos.X < -0.5)
wpos.X = -0.5;
else if (wpos.X > 0.5)
wpos.X = 0.5;
// Y Axis
if (wpos.Y < -0.5)
wpos.Y = -0.5;
else if (wpos.Y > 0.5)
wpos.Y = 0.5;
// Z Axis
if (wpos.Z < -0.5)
wpos.Z = -0.5;
else if (wpos.Z > 0.5)
wpos.Z = 0.5;
}
// Call required function
if (type < CDR_XZ){
box->resizeNodeBoxFace(editor->GetState(),type,wpos,editor->GetState()->keys[KEY_LCONTROL]==EKS_DOWN);
}else{
box->moveNodeBox(editor->GetState(),type,wpos);
}
node->remesh();
}
vector3df pos;
vector3df center = box->GetCenter();
switch (type){
case CDR_X_P:
pos = center;
pos.X = box->two.X;
break;
case CDR_X_N:
pos = center;
pos.X = box->one.X;
break;
case CDR_Y_P:
pos = center;
pos.Y = box->two.Y;
break;
case CDR_Y_N:
pos = center;
pos.Y = box->one.Y;
break;
case CDR_Z_P:
pos = center;
pos.Z = box->two.Z;
break;
case CDR_Z_N:
pos = center;
pos.Z = box->one.Z;
break;
case CDR_XZ:
pos = center;
break;
case CDR_XY:
pos = center;
break;
case CDR_ZY:
pos = center;
break;
}
pos.X+=node->getPosition().X;
pos.Y+=node->getPosition().Y;
pos.Z+=node->getPosition().Z;
#if IRRLICHT_VERSION_MAJOR == 1 && IRRLICHT_VERSION_MINOR < 8
vector2d<irr::s32> cpos = editor->GetState()->GetDevice()->getSceneManager()->getSceneCollisionManager()->getScreenCoordinatesFrom3DPosition(pos,editor->GetState()->GetDevice()->getSceneManager()->getActiveCamera());
#else
vector2d<irr::s32> cpos = editor->GetState()->GetDevice()->getSceneManager()->getSceneCollisionManager()->getScreenCoordinatesFrom3DPosition(pos,editor->GetState()->GetDevice()->getSceneManager()->getActiveCamera(),true);
#endif
position = cpos += offset.UpperLeftCorner;
visible = true;
return;
}
bool NBEditor::OnEvent(const irr::SEvent &event){
if (event.EventType == EET_GUI_EVENT){
if (event.GUIEvent.EventType == EGET_BUTTON_CLICKED){
switch (event.GUIEvent.Caller->getID()){
case GUI_PROJ_NEW_BOX:
{
Node* node = GetState()->project->GetCurrentNode();
if (node){
printf("Clicked add nb!\n");
node->addNodeBox();
load_ui();
}
}
break;
case GUI_PROJ_DELETE_BOX:
{
Node* node = GetState()->project->GetCurrentNode();
IGUIListBox* lb = (IGUIListBox*) GetState()->Menu()->GetSideBar()->getElementFromId(GUI_SIDEBAR_LISTBOX);
if (node && node->GetNodeBox(lb->getSelected())){
printf("Clicked delete nb!\n");
node->deleteNodebox(lb->getSelected());
load_ui();
}
}
break;
}
}else if (event.GUIEvent.EventType == EGET_LISTBOX_CHANGED){
Node* node = GetState()->project->GetCurrentNode();
IGUIListBox* lb = (IGUIListBox*) GetState()->Menu()->GetSideBar()->getElementFromId(GUI_SIDEBAR_LISTBOX);
if (node && lb && node->GetNodeBox(lb->getSelected())){
node->select(lb->getSelected());
load_ui();
}
}
}
return false;
}