📄 cmainboard.cpp
字号:
/*
Crazy Eggs Editor
Author : Kevin Lynx
Date : 2007.1.26
*/
#include "CMainBoard.h"
#include "Editor.h"
#include "../mainMenuGUI/CInputDlg.h"
#include "../resourceGen/res.h"
//directory for the level file
const char *G_DIR = "data/levels/";
CMainBoard::CMainBoard( Editor *editor )
{
m_Editor = editor;
m_dlgSave = NULL;
m_dlgInput = NULL;
}
CMainBoard::~CMainBoard()
{
delete m_btnSave;
delete m_btnPutfull;
if( m_dlgSave != NULL )
{
m_Editor->KillDialog( m_dlgSave );
}
if( m_dlgInput != NULL )
{
m_Editor->KillDialog( m_dlgInput );
}
}
void CMainBoard::Init()
{
//create button
int btnW = STONE_BTN_ON->GetWidth();
int btnH = STONE_BTN_ON->GetHeight();
//'save' button
m_btnSave = new ButtonWidget( BTN_SAVE_ID, this );
m_btnSave->SetFont( NORMAL_FONT );
( (ImageFont*)m_btnSave->mFont)->SetScale( 1.2 );
m_btnSave->Resize( 630, 480, btnW, btnH );
m_btnSave->mLabel = "Save";
m_btnSave->SetColor( ButtonWidget::COLOR_LABEL, Color( 255, 237, 125 ) );
m_btnSave->mOverImage = STONE_BTN_ON;
m_btnSave->mDownImage = STONE_BTN_PUSH;
m_btnSave->mButtonImage=STONE_BTN_OFF;
//'put full' button
m_btnPutfull = new ButtonWidget( BTN_PUTFULL_ID, this );
m_btnPutfull->SetFont( NORMAL_FONT );
( (ImageFont*)m_btnPutfull->mFont)->SetScale( 1.2 );
m_btnPutfull->Resize( 630, 420, btnW, btnH );
m_btnPutfull->mLabel = "Put Full";
m_btnPutfull->SetColor( ButtonWidget::COLOR_LABEL, Color( 255, 237, 125 ) );
m_btnPutfull->mOverImage = STONE_BTN_ON;
m_btnPutfull->mDownImage = STONE_BTN_PUSH;
m_btnPutfull->mButtonImage=STONE_BTN_OFF;
Reset();
}
void CMainBoard::Update()
{
Widget::Update();
//update the cursor anim
if( mUpdateCnt % 50 == 0 )
{
m_cursorLeft = !m_cursorLeft;
}
MarkDirty();
}
void CMainBoard::Draw( Graphics *g )
{
g->DrawImage( SKY, 0, 0 );
g->DrawImage( INFO_PANNEL, 0, 0 );
g->SetColorizeImages( true );
g->SetColor( Color( 255, 255, 255, 210 ) );
g->DrawImage( FLOAT_ISLAND, G_START_X - 50, 40 );
g->SetColorizeImages( false );
//draw grid
g->SetColor( Color( 0, 0, 255 ) );
for( int y = 1; y < G_AREA_HEIGHT; ++ y )
{
g->DrawLine( G_START_X + G_EGG_WIDTH, y * 40 + G_START_Y,
G_START_X + G_EGG_WIDTH * G_AREA_WIDTH - G_EGG_WIDTH, y * 40 + G_START_Y );
}
for( int x = 1; x < G_AREA_WIDTH; ++ x )
{
g->DrawLine( x * 40 + G_START_X, G_START_Y + G_EGG_HEIGHT,
x * 40 + G_START_X, G_START_Y + G_AREA_HEIGHT * G_EGG_HEIGHT - G_EGG_HEIGHT );
}
//draw eggs
DrawEggs( g );
//draw cursor
int x = m_mouseX - ( m_mouseX - G_START_X ) % G_EGG_WIDTH;
int y = m_mouseY - ( m_mouseY - G_START_Y ) % G_EGG_HEIGHT;
if( mouseInBoard( x, y ) )
{
if( m_cursorLeft )
{
g->DrawImage( LOCK, x, y, Rect( 0, 0, G_EGG_WIDTH, G_EGG_HEIGHT ) );
}
else
{
g->DrawImage( LOCK,x, y, Rect( G_EGG_WIDTH, 0, G_EGG_WIDTH, G_EGG_HEIGHT ) );
}
g->DrawImage( UNKNOWN_EGG, x, y );
}
g->SetFont( NORMAL_FONT );
g->DrawString( "Left Button to put eggs", 0, 540 );
g->DrawString( "Right Button to delete eggs", 0, 560 );
}
void CMainBoard::DrawEggs( Graphics *g )
{
for( int y = 1; y < G_AREA_HEIGHT - 1; ++ y )
{
for( int x = 1; x < G_AREA_WIDTH - 1; ++ x )
{
if( m_area[y][x] == -1 )
{
int px = G_START_X + x * G_EGG_WIDTH;
int py = G_START_Y + y * G_EGG_HEIGHT;
g->DrawImage( UNKNOWN_EGG, px, py );
}
}
}
}
void CMainBoard::AddedToManager( WidgetManager *theWidgetManager )
{
Widget::AddedToManager( theWidgetManager );
theWidgetManager->AddWidget( m_btnSave );
theWidgetManager->AddWidget( m_btnPutfull );
}
void CMainBoard::RemovedFromManager( WidgetManager *theWidgetManager )
{
Widget::RemovedFromManager( theWidgetManager );
theWidgetManager->RemoveWidget( m_btnSave );
theWidgetManager->RemoveWidget( m_btnPutfull );
}
void CMainBoard::ButtonDepress( int theId )
{
if( theId == BTN_SAVE_ID )
{
CreateSaveDlg();
}
if( theId == BTN_PUTFULL_ID )
{
PutFull();
}
}
void CMainBoard::DialogButtonDepress( int theDialogId,int theButtonId )
{
if( theDialogId == DLG_SAVE_ID )
{
if( theButtonId == Dialog::ID_YES )
{
if( m_eggsCount % 2 != 0 )
{
//invalid data
m_Editor->Popup( "ERROR : You should put an EVEN number eggs.\n \
Please ADD or DEL an egg on the board!" );
}
else
{
//valid data, so save them
if( SaveLevel( GetFileName().c_str() ))
{
m_Editor->MsgBox( "Save Level OK" );
}
else
{
m_Editor->MsgBox( "Save Level FAILED" );
}
}
}
if( theButtonId == Dialog::ID_NO )
{
CreateInputDlg();
}
m_Editor->KillDialog( m_dlgSave );
}
if( theDialogId == DLG_INPUT_ID )
{
if( m_eggsCount % 2 != 0 )
{
//invalid data
m_Editor->Popup( "ERROR : You should put an EVEN number eggs.\n \
Please ADD or DEL an egg on the board!" );
}
else
{
//valid data
std::string file = G_DIR + m_dlgInput->m_editor->mString;
if( SaveLevel( file.c_str() ))
{
m_Editor->MsgBox( "Save Level OK" );
}
else
{
m_Editor->MsgBox( "Save Level FAILED" );
}
}
m_Editor->KillDialog( m_dlgInput );
}
}
void CMainBoard::MouseMove( int x, int y )
{
Widget::MouseMove( x, y );
m_mouseX = x;
m_mouseY = y;
}
void CMainBoard::MouseDown( int x, int y, int theClickCount )
{
Widget::MouseDown( x, y, theClickCount );
if( mouseInBoard( x, y ) )
{
//set area
int ax = ( x - G_START_X ) / G_EGG_WIDTH;
int ay = ( y - G_START_Y ) / G_EGG_HEIGHT;
if( theClickCount > 0 &&
m_area[ay][ax] == 0 )
{
m_area[ay][ax] = -1;
m_eggsCount ++;
}
if( theClickCount < 0 &&
m_area[ay][ax] == -1 )
{
m_area[ay][ax] = 0; //undo
m_eggsCount --;
}
}
}
void CMainBoard::Reset()
{
for( int y = 0; y < G_AREA_HEIGHT; ++ y )
{
for( int x = 0; x < G_AREA_WIDTH; ++ x )
{
m_area[y][x] = 0;
}
}
m_eggsCount = 0;
}
bool CMainBoard::mouseInBoard( int x, int y )
{
if( x >= G_START_X + G_EGG_WIDTH &&
x < G_START_X + G_AREA_WIDTH * G_EGG_WIDTH - G_EGG_WIDTH &&
y >= G_START_Y + G_EGG_HEIGHT &&
y < G_START_Y + G_AREA_HEIGHT * G_EGG_HEIGHT - G_EGG_HEIGHT )
return true;
else
return false;
}
bool CMainBoard::SaveLevel( const char *fileName )
{
FILE *fp = fopen( fileName, "w" );
if( fp == NULL )
return false;
for( int y = 0; y < G_AREA_HEIGHT; ++ y )
{
for( int x = 0; x < G_AREA_WIDTH; ++ x )
{
fprintf( fp, "%d ", m_area[y][x] );
}
fputc( '\n', fp );
}
fclose( fp );
return true;
}
std::string CMainBoard::GetFileName()
{
std::string file;
for( int i = 1; true; i ++ )
{
file = G_DIR + StrFormat( "level%d", i );
if( !m_Editor->FileExists( file ) )
{
return file;
}
}
return file;
}
void CMainBoard::CreateSaveDlg()
{
m_dlgSave = new Dialog( DIALOG_SMALL, DIALOG_BTN, DLG_SAVE_ID, true,
"Save", "", "", Dialog::BUTTONS_YES_NO );
m_dlgSave->mDialogListener = this;
m_dlgSave->mDialogLines = "Do you want to save this level using the default file name :\n " +
GetFileName();
m_dlgSave->mContentInsets = Insets( 45, 60, 40, 50 );
m_dlgSave->mSpaceAfterHeader = 20;
m_dlgSave->SetColor( Dialog::COLOR_LINES, Color( 0, 0, 255 ) );
m_dlgSave->Resize( ( G_WINDOW_WIDTH - 400 ) / 2, ( G_WINDOW_HEIGHT - 350 ) / 2, 400, 350 );
m_Editor->AddDialog( m_dlgSave );
}
void CMainBoard::CreateInputDlg()
{
m_dlgInput = new CInputDlg( DLG_INPUT_ID );
m_dlgInput->mDialogHeader = "Input File Name";
m_dlgInput->mDialogLines = "Input the Level File Name : data/levels/";
m_dlgInput->mDialogListener = this;
m_Editor->AddDialog( m_dlgInput );
}
void CMainBoard::PutFull()
{
for( int y = 1; y < G_AREA_HEIGHT - 1; ++ y )
{
for( int x = 1; x < G_AREA_WIDTH - 1; ++ x )
{
m_area[y][x] = -1;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -