📄 mainmenu.cpp
字号:
// ********************************************************
// This is demo code. You may derive from, use, modify, and
// distribute it without limitation for any purpose.
// Obviously you don't get a warranty or an assurance of
// fitness for a particular purpose with this code. Your
// welcome to remove this header and claim original
// authorship. I really don't care.
// ********************************************************
#include "MainMenu.h"
#ifdef WIN32
#else // WIN32
#include <unistd.h>
#endif // !WIN32
#include "../GClasses/GTime.h"
#include "../GClasses/GThread.h"
#include "../GClasses/GMacros.h"
#include "../GClasses/GFile.h"
#include "PredAcc.h"
#include "Interpolate.h"
#include "Manifold.h"
#include "Test.h"
#include "RayTrace.h"
#include "Chart.h"
typedef void (*DoDemoFunc)();
void DemoApiDocs()
{
char szFilename[512];
strcpy(szFilename, ControllerBase::GetAppPath());
strcat(szFilename, "../doc/index.html");
GFile::CondensePath(szFilename);
OpenFile(szFilename);
}
void DemoAutomatedTests()
{
TestController c;
c.RunModal();
}
void DemoPredAcc()
{
PredAccController c;
c.RunModal();
}
void DemoInterpolateImageWithNeuralNet()
{
InterpolateController c;
c.RunModal();
}
void DemoManifoldSculpting()
{
ManifoldMenuController c;
c.RunModal();
}
void DemoRayTrace()
{
RayTraceController c;
c.RunModal();
}
void DemoChart()
{
ChartController c;
c.RunModal();
}
typedef struct
{
DoDemoFunc pFunc;
const wchar_t* wszDesc;
} DemoItem;
static const DemoItem g_DemoTable[] =
{
{ DemoApiDocs, L"See generated API Documentation" },
{ DemoAutomatedTests, L"Run a set of automated tests" },
{ DemoPredAcc, L"Test predicitive accuracy against an ARFF file" },
{ DemoChart, L"Make a chart" },
{ DemoInterpolateImageWithNeuralNet, L"Interpolate an image with a neural net" },
{ DemoManifoldSculpting, L"Manifold Sculpting Demos" },
{ DemoRayTrace, L"Demo of ray tracer" },
};
// -------------------------------------------------------------------------------
class MainMenuDialog : public GWidgetDialog
{
protected:
MainMenuController* m_pController;
GWidgetListBox* m_pDemoList;
int m_nValidationTechnique;
double m_dTrainingPercent;
public:
MainMenuDialog(MainMenuController* pController, int w, int h);
virtual ~MainMenuDialog();
virtual void OnChangeListSelection(GWidgetListBox* pListBox);
virtual void OnReleaseTextButton(GWidgetTextButton* pButton);
};
// ------------------------
#define BACKGROUND_COLOR 0xffaaaaaa
MainMenuDialog::MainMenuDialog(MainMenuController* pController, int w, int h)
: GWidgetDialog(w, h, BACKGROUND_COLOR)
{
m_pController = pController;
m_pDemoList = new GWidgetListBox(this, 25, 70, 350, 350);
int n;
for(n = 0; n < sizeof(g_DemoTable) / sizeof(DemoItem); n++)
new GWidgetListBoxItem(m_pDemoList, g_DemoTable[n].wszDesc);
m_pDemoList->SetSelection(-1);
GString s;
s.Copy(L"This is a small collection of demos to show off some of the capabilities");
new GWidgetTextLabel(this, 30, 10, 500, 16, &s, 0xff442266);
s.Copy(L"of the class library. Most classes don't have a corresponding demo yet,");
new GWidgetTextLabel(this, 30, 25, 500, 16, &s, 0xff442266);
s.Copy(L"and most demos aren't very polished. (Feel free to help make them better.)");
new GWidgetTextLabel(this, 30, 40, 500, 16, &s, 0xff442266);
s.Copy(L"Currently the API docs are the best way to see what this library can do.");
new GWidgetTextLabel(this, 30, 55, 500, 16, &s, 0xff442266);
}
/*virtual*/ MainMenuDialog::~MainMenuDialog()
{
}
/*virtual*/ void MainMenuDialog::OnChangeListSelection(GWidgetListBox* pListBox)
{
if(pListBox == m_pDemoList)
{
DoDemoFunc pFunc = g_DemoTable[m_pDemoList->GetSelection()].pFunc;
pFunc();
}
}
/*virtual*/ void MainMenuDialog::OnReleaseTextButton(GWidgetTextButton* pButton)
{
}
// -------------------------------------------------------------------------------
class MainMenuView : public ViewBase
{
protected:
MainMenuDialog* m_pDialog;
public:
MainMenuView(MainMenuController* pController);
virtual ~MainMenuView();
virtual void OnChar(char c);
virtual void OnMouseDown(int x, int y);
virtual void OnMouseUp(int x, int y);
virtual bool OnMousePos(int x, int y);
protected:
virtual void Draw(SDL_Surface *pScreen);
};
MainMenuView::MainMenuView(MainMenuController* pController)
: ViewBase()
{
m_pDialog = new MainMenuDialog(pController, 790, 590);
}
MainMenuView::~MainMenuView()
{
delete(m_pDialog);
}
/*virtual*/ void MainMenuView::Draw(SDL_Surface *pScreen)
{
// Clear the screen
SDL_FillRect(pScreen, NULL/*&r*/, 0x000000);
// Draw the dialog
GRect r;
BlitImage(pScreen, m_screenRect.x, m_screenRect.y, m_pDialog->GetImage(&r));
}
void MainMenuView::OnChar(char c)
{
m_pDialog->HandleChar(c);
}
void MainMenuView::OnMouseDown(int x, int y)
{
x -= m_screenRect.x;
y -= m_screenRect.y;
GWidgetAtomic* pNewWidget = m_pDialog->FindAtomicWidget(x, y);
m_pDialog->GrabWidget(pNewWidget, x, y);
}
void MainMenuView::OnMouseUp(int x, int y)
{
m_pDialog->ReleaseWidget();
}
bool MainMenuView::OnMousePos(int x, int y)
{
return m_pDialog->HandleMousePos(x - m_screenRect.x, y - m_screenRect.y);
}
// -------------------------------------------------------------------------------
MainMenuController::MainMenuController()
: ControllerBase()
{
m_pView = new MainMenuView(this);
}
MainMenuController::~MainMenuController()
{
delete(m_pView);
}
void MainMenuController::RunModal()
{
double timeOld = GTime::GetTime();
double time;
m_pView->Update();
while(m_bKeepRunning)
{
time = GTime::GetTime();
if(HandleEvents(time - timeOld)) // HandleEvents returns true if it thinks the view needs to be updated
{
m_pView->Update();
}
else
GThread::sleep(10);
timeOld = time;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -