00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "PShaderWin.h"
00022 #include <qmenubar.h>
00023 #include <qapplication.h>
00024 #include <qpushbutton.h>
00025 #include <qlayout.h>
00026 #include "PShaderTxt.h"
00027 #include <iostream>
00028
00029
00030 using namespace std;
00031 using namespace glift;
00032 using namespace gutz;
00033
00034
00035 PShaderWin::PShaderWin(const QGLContext *ctx, QWidget *parent, const char *name, WFlags wf)
00036 : QMainWindow(parent,name,wf), _lpath("\\"),
00037 _qcentral(new QWidget(this,"central widget")),
00038 _ctx(ctx)
00039 {
00040 confMenu();
00041
00042 setCaption("Simian 3.0 ~ Shader Editor");
00043 setCentralWidget(_qcentral);
00044
00045 QHBoxLayout *topLayout = new QHBoxLayout( _qcentral, 5 );
00046
00047 QGridLayout *maing = new QGridLayout(topLayout, 2,1);
00048
00049
00050 _tabs = new QTabWidget(_qcentral, "prog shader tabs");
00051 _tabs->addTab(new PShaderTxt(_ctx,_tabs,"scratch"), "Scratch");
00052 maing->addWidget(_tabs,0,0);
00053
00054
00055 QPushButton *loadB = new QPushButton("Load Shader (F7)", _qcentral, "load shader button");
00056 maing->addWidget(loadB,1,0);
00057 connect(loadB,SIGNAL(pressed()),SLOT(loadShader()));
00058
00059 }
00060
00061
00062 PShaderWin::~PShaderWin()
00063 {
00064
00065 }
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076 void PShaderWin::loadShader()
00077 {
00078
00079 QWidget *w = _tabs->currentPage();
00080 PShaderTxt *pst = dynamic_cast<PShaderTxt*>(w);
00081 if(pst)
00082 pst->loadShader();
00083 }
00084
00085
00086
00087
00088 void PShaderWin::openShader()
00089 {
00090 bool active = isActiveWindow();
00091
00092 QString s;
00093 s = QFileDialog::getOpenFileName( _lpath, "Shaders (*.fp *.vp *.txt)");
00094
00095 if(QString::null == s)
00096 return;
00097
00098 _lpath = s;
00099
00100 PShaderTxt *pst = new PShaderTxt(_ctx, this, "a new shader");
00101
00102 pst->loadShaderFromFile(_lpath);
00103
00104
00105 if(pst->getShaderType() == PShaderTxt::NOT_LOADED)
00106 {
00107 cerr << "PShaderWin::openShader(), couldn't open shader" << endl;
00108 delete pst;
00109 return;
00110 }
00111
00112
00113 _tabs->addTab(pst,pst->getFileName().section('/',-1));
00114 _tabs->showPage(pst);
00115
00116 if(active)
00117 setActiveWindow();
00118 }
00119
00120
00121
00122
00123 void PShaderWin::saveShader()
00124 {
00125 bool active = isActiveWindow();
00126
00127 QWidget *w = _tabs->currentPage();
00128 PShaderTxt *pst = dynamic_cast<PShaderTxt*>(w);
00129 if(pst)
00130 {
00131 pst->saveShader(_lpath);
00132 _tabs->setTabLabel(pst,pst->getFileName().section('/',-1));
00133 }
00134
00135 if(active)
00136 setActiveWindow();
00137 }
00138
00139
00140
00141
00142 void PShaderWin::saveShaderAs()
00143 {
00144 bool active = isActiveWindow();
00145
00146 QWidget *w = _tabs->currentPage();
00147 PShaderTxt *pst = dynamic_cast<PShaderTxt*>(w);
00148 if(pst)
00149 {
00150 pst->saveShaderAs(_lpath);
00151 _tabs->setTabLabel(pst,pst->getFileName().section('/',-1));
00152 }
00153
00154 if(active)
00155 setActiveWindow();
00156 }
00157
00158
00159
00160
00161 void PShaderWin::closeShader()
00162 {
00163 bool active = isActiveWindow();
00164
00165 if(_tabs->currentPageIndex() == 0)
00166 {
00167 return;
00168 }
00169
00170 QWidget *w = _tabs->currentPage();
00171 PShaderTxt *pst = dynamic_cast<PShaderTxt*>(w);
00172 if(pst)
00173 {
00174 pst->closeShader(_lpath);
00175 _tabs->removePage(w);
00176 delete(w);
00177 }
00178
00179 if(active)
00180 setActiveWindow();
00181 }
00182
00183
00184
00185
00186
00187 const QString ARBFP_TXT("!!ARBfp1.0\n\n\nEND");
00188 const QString ARBVP_TXT("!!ARBvp1.0\n\n\nEND");
00189
00190
00191 void PShaderWin::addShader(glift::ProgShaderSP shader)
00192 {
00193 bool active = isActiveWindow();
00194
00195 PShaderTxt *pst = new PShaderTxt(_ctx, this, "a new shader");
00196
00197 pst->loadShader(shader);
00198
00199
00200 if(pst->getShaderType() == PShaderTxt::NOT_LOADED)
00201 {
00202 cerr << "PShaderWin::loadShader(), couldn't load shader" << endl;
00203 delete pst;
00204 return;
00205 }
00206
00207
00208 QString tname = pst->getFileName().section('/', -1);
00209
00210
00211 if(tname.isEmpty())
00212 {
00213 tname = "Unnamed";
00214 }
00215
00216
00217 _tabs->addTab(pst,tname);
00218 _tabs->showPage(pst);
00219
00220 if(active)
00221 setActiveWindow();
00222 }
00223
00224 void PShaderWin::newARBFP()
00225 {
00226 bool active = isActiveWindow();
00227
00228 PShaderTxt *pst = new PShaderTxt(_ctx, this, "a new shader");
00229
00230 pst->loadShaderFromText(ARBFP_TXT);
00231
00232
00233 if(pst->getShaderType() == PShaderTxt::NOT_LOADED)
00234 {
00235 cerr << "PShaderWin::openShader(), couldn't open shader" << endl;
00236 delete pst;
00237 return;
00238 }
00239
00240
00241 _tabs->addTab(pst,"Not Saved");
00242 _tabs->showPage(pst);
00243
00244 if(active)
00245 setActiveWindow();
00246 }
00247
00248 void PShaderWin::newARBVP()
00249 {
00250 bool active = isActiveWindow();
00251
00252 PShaderTxt *pst = new PShaderTxt(_ctx, this, "a new shader");
00253
00254 pst->loadShaderFromText(ARBVP_TXT);
00255
00256
00257 if(pst->getShaderType() == PShaderTxt::NOT_LOADED)
00258 {
00259 cerr << "PShaderWin::openShader(), couldn't open shader" << endl;
00260 delete pst;
00261 return;
00262 }
00263
00264
00265 _tabs->addTab(pst,"Not Saved");
00266 _tabs->showPage(pst);
00267
00268 if(active)
00269 setActiveWindow();
00270 }
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283 void PShaderWin::keyPressEvent(QKeyEvent *key)
00284 {
00285 if((key->key() == Qt::Key_F7))
00286 {
00287 loadShader();
00288 key->accept();
00289 return;
00290 }
00291
00292 switch(key->key())
00293 {
00294 case 4096:
00295 key->accept();
00296 close();
00297 break;
00298
00299 default:
00300 cerr << "Key event : " << key->key() << " = ' " << char(key->ascii()) << " ' " << endl;
00301 break;
00302 }
00303 }
00304
00305
00306
00307
00308 void PShaderWin::confMenu()
00309 {
00310
00311 QMenuBar* menu = menuBar();
00312
00313
00314 QPopupMenu* file = new QPopupMenu( menu );
00315 file->insertItem("&Open Shader", this, SLOT(openShader()), CTRL+Key_O);
00316 file->insertItem("&Save", this, SLOT(saveShader()), CTRL+Key_S);
00317 file->insertItem("Save &as", this, SLOT(saveShaderAs()), CTRL+SHIFT+Key_S);
00318 file->insertItem("&Close", this, SLOT(closeShader()), CTRL+Key_C);
00319 file->insertItem("E&xit", qApp, SLOT(close()), CTRL+Key_Q);
00320
00321
00322 QPopupMenu* psnew = new QPopupMenu( menu );
00323 psnew->insertItem("New ARB FP", this, SLOT(newARBFP()));
00324 psnew->insertItem("New ARB VP", this, SLOT(newARBVP()));
00325
00326 file->insertItem("New", psnew);
00327
00328 menu->insertItem("&File", file);
00329
00330
00331 }