Files

597 lines
18 KiB
C++
Raw Permalink Normal View History

2025-05-13 02:46:21 +03:00
#include "Editor.h"
#include <gui/GUIPlane.h>
#include <gui/GUISplitPanel.h>
#include <gui/GUIScrollablePlane.h>
#include <gui/GUICheckBox.h>
#include <gui/GUIRadioButton.h>
#include <gui/GUITab.h>
#include <gui/GUITreeView.h>
#include <gui/GUIComboBox.h>
#include <gui/GUITextInput.h>
#include <gui/CImage.h>
#include <gui/GUIProgress.h>
#include "gui/GUIToolStrip.h"
#include "gui/GUIToolStripButton.h"
#include <gui/GUIToolStripImage.h>
#include <gui/GUIToolStripMenu.h>
#include <gui/GUIMenu.h>
#include <gui/GUIFileBrowserDialog.h>
#include "EditorShared.h"
#include <shaders/IShaderGraphNode.h>
#include <shaders/IShaderGraphNodeFactory.h>
#include <shaders/IShaderGraphCompiler.h>
#include <shaders/IShaderFactorySource.h>
#include <shaders/ShaderGraphCompilerHLSL.h>
#include <vfs/pak/IPakFileContentProvider.h>
#include <shaders/IShaderGraph.h>
#include "GUIShaderNode.h"
#include "GUIShaderNodeIO.h"
#include <gui/GUIForm.h>
Editor::Editor()
:bRunning(false)
{
}
Editor::~Editor()
{
}
void Editor::menu_exit(GUIMenuItem* m)
{
printf("Exit!!!!!!!\n");
exit(0);
}
void Editor::AddShaderLink(GUIShaderNodeIO* left, GUIShaderNodeIO* right)
{
//make spline link
nodeContainer->MakeLink(left, right);
if(right->IsInput())
{
GUIShaderNodeIO* tmp = right;
right = left;
left = tmp;
}
//do shader graph connection
IShaderGraphNode* leftNode = left->Owner->GetGraphicsNode();
IShaderGraphNode* rightNode = right->Owner->GetGraphicsNode();
IShaderGraphNodePort* leftPort = leftNode->Inputs[left->id];
IShaderGraphNodePort* rightPort = rightNode->Outputs[right->id];
leftPort->MakeLink(leftPort, rightPort);
}
void Editor::OnButtonClick(GUIWindow* wnd, int key)
{
GUIButton* b = reinterpret_cast<GUIButton*>(wnd);
b->SetCaption(b->GetCaption() + STR("/"));
}
void Editor::OnScroll(GUIScroll* wnd, int key)
{
char buf[32];
int val = wnd->GetValue();
itoa(val, &buf[0], 10);
String s(buf);
b2->SetCaption(s);
}
void Editor::SwitchPlayback(GUIWindow* wnd, int t)
{
if(snd->IsPlaying())
{
snd->Stop();
reinterpret_cast<GUIButton*>(wnd)->SetCaption(STR("Play"));
}
else
{
snd->Play();
reinterpret_cast<GUIButton*>(wnd)->SetCaption(STR("Stop"));
}
}
void Editor::OnResizeOutput(GUIWindow* wnd)
{
printf("!!!!!!!!OMG!!! OUTPUT RESIZE!!!!\n");
Rect2i r = sceneOutput->GetClientRectangleAbs();
pGraphics->GetSceneManager()->GetActiveScene()->ResizeOutput(r.width, r.height);
CImage* img = (CImage*)sceneOutput->GetPicture();
if(img)
{
img->Reset(pGraphics->GetSceneManager()->GetActiveScene()->GetOutput().ptr());
sceneOutput->SetPicture(img);
}
}
GUIShaderNode* Editor::AddNode(const String& name)
{
GUIShaderNode* node = nodeContainer->AddNode(name);
node->SetPosition(Vec2i(10,10));
node->Event_Attach += NewDelegate(this, &Editor::AddShaderLink);
return node;
}
void Editor::OnAddNode( GUIWindow* w, const String& s, void* d)
{
int id = (int)d;
NodeInfo* info = node_info[id];
GUIShaderNode* node = this->AddNode(info->ClassName);
node->SetCaption(info->Name);
}
void Editor::OnSaveGraph(GUIWindow* wnd, int p)
{
Core::IFile* graph_file;
Core::IFile* gui_file;
//save shader graph
if(ResourceManager::me()->getFileSystem()->GetFile("root/Data/MyMaterial.sg", &graph_file) != SC_OK){
ResourceManager::me()->getFileSystem()->NewFile("root/Data/MyMaterial.sg", 0);
ResourceManager::me()->getFileSystem()->GetFile("root/Data/MyMaterial.sg", &graph_file);
}
IDataStreamPtr stm = graph_file->OpenWrite(0);
IMemoryStream mem_stm(1024*128);
sg->Serialize(&mem_stm);
stm->Write(mem_stm.GetData(), mem_stm.Tell());
stm->Close();
//save GUI extra graph data
if(ResourceManager::me()->getFileSystem()->GetFile("root/Data/MyMaterial.ext", &gui_file) != SC_OK){
ResourceManager::me()->getFileSystem()->NewFile("root/Data/MyMaterial.ext", 0);
ResourceManager::me()->getFileSystem()->GetFile("root/Data/MyMaterial.ext", &gui_file);
}
stm = gui_file->OpenWrite(0);
mem_stm.Seek(0);
nodeContainer->Serialize(&mem_stm);
stm->Write(mem_stm.GetData(), mem_stm.Tell());
stm->Close();
}
void Editor::OnGraphNodeDelete(GUINodeContainer* container, GUIShaderNode* node)
{
if(EditorShared::me()->propGrid->GetTarget() ==
(Core::IPropertyHolder*)node)
{
EditorShared::me()->propGrid->SetTarget(nullptr);
}
}
void Editor::OnLoadGraph(GUIWindow* wnd, int p)
{
Core::IFile* graph_file ;
if(ResourceManager::me()->getFileSystem()->GetFile("root/Data/MyMaterial.sg", &graph_file) != SC_OK)
return;
IDataStreamPtr stm = graph_file->OpenRead();
char* buffer = new char[stm->Length()];
stm->Read(buffer, stm->Length());
IMemoryStream mem_stm(stm->Length());
mem_stm.Write(buffer, stm->Length());
mem_stm.Seek(0);
sg->Clear();
sg->Deserialize(&mem_stm);
nodeContainer->RebuildGraph();
int cnt = nodeContainer->GetNodeCount();
for(int i = 0; i < cnt; i++){
nodeContainer->getNode(i)->Event_Attach += NewDelegate(this, &Editor::AddShaderLink);
}
stm->Close();
delete[] buffer;
Core::IFile* gui_file ;
if(ResourceManager::me()->getFileSystem()->GetFile("root/Data/MyMaterial.ext", &gui_file) != SC_OK)
return;
stm = gui_file->OpenRead();
buffer = new char[stm->Length()];
stm->Read(buffer, stm->Length());
IMemoryStream ext_stm(stm->Length());
ext_stm.Write(buffer, stm->Length());
ext_stm.Seek(0);
nodeContainer->Deserialize(&ext_stm);
stm->Close();
delete[] buffer;
}
void Editor::OnCompileShader(GUIWindow* wnd, int p)
{
IMaterial* mat = new IMaterial(sg, new ShaderGraphCompilerHLSL());
NodeCollection* texNodes = nodeContainer->GetTextureUnits();
for(int i = 0; i < (*texNodes).size(); i++)
{
GUIShaderNodeTexture* tex = (GUIShaderNodeTexture*)((*texNodes)[i]);
texture_ptr texture = ResourceManager::me()->GetTexture(tex->GetFilename());
mat->SetTexture(tex->GetGraphicsNode()->id, texture);
}
tower->setMaterial(mat, 0, false);
//tower->getMaterial(0)->
//assert(false && "Not implemented yet");
/*ShaderGraphCompilerHLSL* shaderCompiler = new ShaderGraphCompilerHLSL();
String ps, vs;
shaderCompiler->CompileNode(sg->GetNodeByID(0), NULL, ps, vs);
IShader* pixelShader = CG::me()->GetRenderer()->CompilePixelShader(ps.c_str(), ps.length());
IShader* vertexShader = CG::me()->GetRenderer()->CompileVertexShader(vs.c_str(), vs.length());
//grab texture info from graph
IMaterial* mat = new IMaterial();
mat->g_buffer_pass_ps = pixelShader;
mat->g_buffer_pass_vs = vertexShader;
NodeCollection* texNodes = nodeContainer->GetTextureUnits();
for(int i = 0; i < (*texNodes).size(); i++)
{
GUIShaderNodeTexture* tex = (GUIShaderNodeTexture*)((*texNodes)[i]);
texture_ptr texture = ResourceManager::me()->GetTexture(tex->FileName());
mat->SetTexture(texture, tex->GraphicsNode->resource_id);
}
tower->setMaterial(mat, 0, false);*/
}
void Editor::Run()
{
node_info.push_back(new NodeInfo(STR("Float1"), STR("ConstFloat1")));
node_info.push_back(new NodeInfo(STR("Float2"), STR("ConstFloat2")));
node_info.push_back(new NodeInfo(STR("Float3"), STR("ConstFloat3")));
node_info.push_back(new NodeInfo(STR("Float4"), STR("ConstFloat4")));
node_info.push_back(new NodeInfo(STR("Add"), STR("OpAdd")));
node_info.push_back(new NodeInfo(STR("Sub"), STR("OpSub")));
node_info.push_back(new NodeInfo(STR("Mul"), STR("OpMul")));
node_info.push_back(new NodeInfo(STR("Div"), STR("OpDiv")));
node_info.push_back(new NodeInfo(STR("Lerp"), STR("OpLerp")));
node_info.push_back(new NodeInfo(STR("Normalize"), STR("OpNormalize")));
node_info.push_back(new NodeInfo(STR("Saturate"), STR("OpSaturate")));
node_info.push_back(new NodeInfo(STR("Power"), STR("OpPow")));
node_info.push_back(new NodeInfo(STR("Texture2D"), STR("SrcTex2D")));
node_info.push_back(new NodeInfo(STR("TEXCOORD"), STR("SrcUV")));
node_info.push_back(new NodeInfo(STR("DecodeNormal"), STR("DecodeNormal")));
node_info.push_back(new NodeInfo(STR("Time"), STR("Time")));
IShaderGraphNodeFactory* shaderFactory = IShaderGraphNodeFactory::me();
IShaderFactorySource::Register(shaderFactory);
sg = new IShaderGraph(shaderFactory);
//getchar();
//SetCurrentDirectory("C:\\development\\LifeProject\\Release");
/*CLog::me()->LogToConsole(true);
ResourceManager::me()->SetCentralDirectory(STR("Resources\\"));
IFile* f;
if(SC_OK != ResourceManager::me()->getFileSystem()->GetFile("root/Resources/Data.pak", &f))
{
printf("Can not load Data.pak!");
system("pause");
return;
}
IDataStreamPtr pr = f->OpenRead();
IPakFileContentProvider pp(pr.get(), STR("Data")); //pp() _text("Data.pak"), STR("Data")
String mp(_text("root"));
ResourceManager::me()->MountContentProvider(&pp, mp);*/
ResourceManager::me()->SetCentralDirectory(STR("D:\\development\\Programming\\Projects\\Data\\"));
pWindow = new RenderWindowWin();
pWindow->SetSize(900, 600);
pWindow->CenterWindow();
pWindow->SetCursorVisible(false);
pInput = new InputSystemOIS();
pInput->Init(900, 600, false, pWindow->GetWindowHandle());
pInput->AddListener(this);
Video::RenderParams params;
params.AllowSwitchToSoftware = false;
params.ForceWideScreen = false;
params.ScreenWidth = 900;
params.ScreenHeight = 600;
params.Window = pWindow;
params.Windowed = true;
pGraphics = new LifeEngine();
pGraphics->Init(RT_DIRECTX_11, params);
pGraphics->GetGUI()->AddOverlay("GUI");
GUIOverlay* ovrl = pGraphics->GetGUI()->GetOverlay("GUI");
nodeContainer = new GUINodeContainer(sg);
nodeContainer->Event_DeleteNode += NewDelegate(this, &Editor::OnGraphNodeDelete);
GUIToolStrip* strp = new GUIToolStrip();
strp->SetDock(DOCK_TOP);
strp->SetHeight(20);
strp->SetWidth(20);
ovrl->AddChild(strp);
//strp->SetBackColor(Vec4f(0.9f, 0.5f, 0.5f, 1.0f));
GUIToolStripMenu* fileMenu = new GUIToolStripMenu();
fileMenu->SetCaption(STR("File"));
GUIMenu* fm = fileMenu->GetMenu();
GUIMenu* fm2;
fm->AddItem(STR("Open"), false);
fm2 = fm->AddItem(STR("Save as"), true)->GetSubmenu();
fm->AddItem(STR("Recent"), false);
fm->AddItem(STR("Exit"), false)->Event_Select += NewDelegate(this, &Editor::menu_exit);
fm2->AddItem(STR("BMP"), false);
fm2->AddItem(STR("DDS"), false);
fm2->AddItem(STR("OMG"), false);
strp->AddItem(fileMenu);
GUIToolStripButton* tsb = new GUIToolStripButton();
tsb->AsButton()->SetCaption("Compile shader");
tsb->AsButton()->Event_MouseClick += NewDelegate(this, &Editor::OnCompileShader);
strp->AddItem(tsb);
GUIToolStripButton* tsb2 = new GUIToolStripButton();
strp->AddItem(tsb2);
GUIToolStripButton* tsb3 = new GUIToolStripButton();
strp->AddItem(tsb3);
GUIToolStripImage* tsim = new GUIToolStripImage();
strp->AddItem(tsim);
GUIPicture* pic = (GUIPicture*)tsim->GetInternalWindow();
pic->SetPicture(GUIManager::me()->GetRenderer()->GetImage(STR("save_icn.bmp")));
GUIToolStripButton* save_button = new GUIToolStripButton();
save_button->AsButton()->SetCaption("Save");
save_button->AsButton()->Event_MouseClick += NewDelegate(this, &Editor::OnSaveGraph);
strp->AddItem(save_button);
GUIToolStripButton* load_button = new GUIToolStripButton();
load_button->AsButton()->SetCaption("Load");
load_button->AsButton()->Event_MouseClick += NewDelegate(this, &Editor::OnLoadGraph);
strp->AddItem(load_button);
GUIScrollablePlane* p11 = new GUIScrollablePlane();
p11->SetWidth(900);
p11->SetHeight(600);
p11->SetDock(DOCK_FILL);
p11->SetPosition(LifeMath::Vec2i(0, 0));
ovrl->AddChild(p11);
p11->GetVScroll()->Event_Scroll += NewDelegate(this, &Editor::OnScroll);
p11->SetBorderEnabled(false);
sndSystem = new Sound::SoundSystem();
if(sndSystem->Init(ResourceManager::me()->getFileSystem()) != 0)
{
printf("Error initializing sound system!");
}
else
printf("Sound system ready!\n");
snd = sndSystem->getSound(STR("root/Data/Sounds/Wwise_Main_Menu_Streaming.005.ogg"), Sound::SM_ENABLED);
GUINodeList* node_list = new GUINodeList();
node_list->SetDock(DOCK_RIGHT);
node_list->SetWidth(150);
node_list->Event_ItemSelected += NewDelegate(this, &Editor::OnAddNode);
int node_cnt = node_info.size();
for(int i = 0; i < node_cnt; i++)
{
node_list->AddItem(node_info[i]->Name, (void*)i);
}
AddNode(STR("Material"))->SetCaption(STR("Material"));;
p11->AddChild(node_list);
GUISplitPanel* sp = new GUISplitPanel();
p11->AddChild(sp);
sp->SetWidth(900);
sp->SetHeight(600);
sp->SetDock(DOCK_FILL);
sp->SetPosition(Vec2i(0,0));
sp->SetSplitterHorisontal(false);
sp->SetRealtimeResize(true);
sp->SetSplitterPosition(450);
GUISplitPanel* sp2 = new GUISplitPanel();
sp->GetContainer1()->AddChild(sp2);
sp2->SetWidth(300);
sp2->SetHeight(300);
sp2->SetPosition(Vec2i(80,80));
sp2->SetSplitterHorisontal(true);
sp2->SetRealtimeResize(true);
sp2->SetDock(Video::DOCK_FILL);
sp2->SetBorderEnabled(false);
sp->SetBorderEnabled(false);
sp->GetContainer2()->AddChild(nodeContainer);
nodeContainer->SetDock(DOCK_FILL);
sceneOutput = new GUIPicture();
sceneOutput->SetPosition(Vec2i(2,2));
sceneOutput->SetWidth(200);
sceneOutput->SetHeight(160);
sceneOutput->SetDock(DOCK_FILL);
sceneOutput->Event_Resize += NewDelegate(this, &Editor::OnResizeOutput);
sp2->GetContainer1()->AddChild(sceneOutput);
sp2->GetContainer1()->SetBackColor(Vec4f(0.3f, 0.3f, 0.3f, 1.0f));
sp2->GetContainer2()->SetBackColor(Vec4f(0.3f, 0.3f, 0.3f, 1.0f));
sp->GetContainer1()->SetBackColor(Vec4f(0.3f, 0.3f, 0.3f, 1.0f));
sp->GetContainer2()->SetBackColor(Vec4f(0.3f, 0.3f, 0.3f, 1.0f));
CImage* img = CImage::FromTexture(ResourceManager::me()->GetTexture(STR("root/Data/Textures/me_small.jpg")).ptr());
sceneOutput->SetPicture(img);
sceneOutput->SetWidth(img->GetWidth() + 2);
sceneOutput->SetHeight(img->GetHeight() + 2);
sceneOutput->SetBackColor(Vec4f(0,0,0,1));
tower = new Video::IObjectMesh("root/Data/Objects/Tower_opt.vmf");
pGraphics->GetSceneManager()->GetActiveScene()->PushNode(tower);
tower->getTransform()->SetPosition(Vec3f(0,0,0));
Video::Light* l = new Video::Light(Video::LT_POINT);
l->setRange(20);
pGraphics->GetSceneManager()->GetActiveScene()->PushNode(l);
l->getTransform()->SetPosition(Vec3f(4,0.5f,-4.0f));
l->color = Vec3f(1,0,0);
l->intensity = 1.0f;
propGrid = new GUIPropertyGrid();
propGrid->SetWidth(300);
propGrid->SetHeight(300);
propGrid->AddEditor(new GUIFilePathPropertyEditor(propGrid));
propGrid->SetDock(DOCK_FILL);
sp2->GetContainer2()->AddChild(propGrid);
EditorShared::me()->propGrid = propGrid;
pGraphics->GetSceneManager()->GetActiveScene()->mainCam->setAspect(1.3333f);
pGraphics->GetSceneManager()->GetActiveScene()->mainCam->setFar(100.0f);
pGraphics->GetSceneManager()->GetActiveScene()->mainCam->setNear(0.1f);
pGraphics->GetSceneManager()->GetActiveScene()->mainCam->setFov(3.1415f / 3.0f);
/*GUIList* testList = new GUIList();
for(int i = 0; i < 50; i++)
{
testList->AddItem(STR("Trololo1"), 0);
}
ovrl->AddChild(testList);
testList->SetWidth(200);
testList->SetHeight(200);
*/
//pGraphics->GetSceneManager()->GetActiveScene()->mainCam->setPosition(0,1,-20);
bRunning = true;
float angle = 0.0f;
while(bRunning)
{
angle += 0.001f;
DoLogics();
DoRendering();
frame_counter.OnFrameEnd();
if(frame_counter.GetFrameId() % 500 == 0)
{
printf("Average FPS: %f GUIBatch size=%i\r", frame_counter.GetFpsAvg(), sizeof(GUIVertex));
}
tower->getTransform()->SetRotation(Vec3f(0,1,0), angle);
if(frame_counter.GetFrameId() % 100 == 0)
{
//float val = (float)(snd->GetTimePosition() / snd->GetTimeLength());
//slider->SetValue(val);
//progress->SetValue(val);
//int time = snd->GetTimePosition();
//char tmp[128];
//sprintf(&tmp[0], "Playback time: %i m %i s", time / 60, time % 60);
//time_info->SetText(String(tmp));
}
}
DoCleanup();
}
void Editor::SetTrackPosition(GUIWindow* sender, int args)
{
double d = snd->GetTimeLength();
snd->SeekTime(snd->GetTimeLength() * slider->GetValue());
}
void Editor::DoLogics()
{
pWindow->Update();
if(bRunning)
{
sndSystem->Update();
if(pWindow->IsActive())
pInput->Capture();
pGraphics->GetSceneManager()->GetActiveScene()->Update(frame_counter.GetTimeElapled());
}
}
void Editor::DoRendering()
{
if(bRunning)
{
pGraphics->GetMainRenderer()->SetDefaultTargets();
pGraphics->GetMainRenderer()->ClearBuffers(CF_COLOR_TARGET, 0xffffffff, 1.0f, 0);
pGraphics->GetSceneManager()->GetActiveScene()->mainCam->setPosition(0.0f, 0.6f, -0.5f);
pGraphics->GetSceneManager()->GetActiveScene()->mainCam->setDirection(Vec3f(0,0.3f,0));
pGraphics->GetSceneManager()->GetActiveScene()->Render();
pGraphics->GetGUI()->Render();
/*Video::DebugVertex lines[2];
lines[0].position = Vec3f(10, 10, 0);
lines[1].position = Vec3f(210, 210, 0);
lines[0].color = Vec3f(1, 1, 1);
lines[1].color = Vec3f(1, 1, 1);
pGraphics->GetMainRenderer()->SetScissorRectangle(LifeMath::Rect2i(640, 480));*/
/*Video::DebugVertex pts[4];
pts[0].color = LifeMath::Vec3f(1,0,0);
pts[0].position = LifeMath::Vec3f(10,90,1);
pts[1].color = LifeMath::Vec3f(0,1,0);
pts[1].position = LifeMath::Vec3f(80,90,1);
pts[2].color = LifeMath::Vec3f(0,0,1);
pts[2].position = LifeMath::Vec3f(20,310,1);
pts[3].color = LifeMath::Vec3f(1,1,0);
pts[3].position = LifeMath::Vec3f(90,310,1);*/
// for(int i = 0; i < 100; i++)
//pGraphics->debugDrawer->DrawSpline2D(pts, 20);
//pGraphics->debugDrawer->DrawLines2D(lines, 1);
pGraphics->GetMainRenderer()->SwapBuffers();
pGraphics->FrameEnd();
}
}
void Editor::DoCleanup()
{
}
bool Editor::KeyDown( Key key, unsigned int text, const std::string& str )
{
pGraphics->GetGUI()->KeyDown(key, text, str);
return true;
}
bool Editor::KeyUp( Key key, unsigned int text, const std::string& str )
{
return true;
}
bool Editor::MouseMove( int absX, int relX, int absY, int relY)
{
pGraphics->GetGUI()->MouseMove(absX, relX, absY, relY);
return true;
}
bool Editor::MousePressed(Core::MouseKey key)
{
pGraphics->GetGUI()->MousePressed(key);
return true;
}
bool Editor::MouseReleased(Core::MouseKey key)
{
pGraphics->GetGUI()->MouseReleased(key);
return true;
}