⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 unboundmodedemointminerfield.cpp

📁 DevExpress ExpressQuantumGrid Suite v5.9 Full Source
💻 CPP
📖 第 1 页 / 共 2 页
字号:
    if(CellState[ACol][ARow].CellState == csClosed) {
      FireMinerFieldEvent(ACol, ARow, meBombMarkCell);
      FireSetMineCountEvent(mcDecMineCount);
    }
    else
    if((CellState[ACol][ARow].CellState == csBombMarked) &&
      FQuestionMarkCell) {
      FireMinerFieldEvent(ACol, ARow, meQuestionMarkCell);
      FireSetMineCountEvent(mcIncMineCount);
    }
    else {
      FireMinerFieldEvent(ACol, ARow, meCloseCell);
      if(!FQuestionMarkCell) FireSetMineCountEvent(mcIncMineCount);
    };
    InvalidateCell(ACol, ARow);
  };
}
//---------------------------------------------------------------------------

void __fastcall TIntMinerField::MouseMoveHandler(System::TObject* Sender, Classes::TShiftState Shift, int X, int Y)
{
  int ACol, ARow;
  MouseToCell(X, Y, ACol, ARow);
  if((ACol == - 1) || (ARow == -1)) {
    if(FPressedCells.Length != 0)
      UnpressAndInvalidate();
    if(FSurprised) {
      FireImageChanged(imSmile);
      FSurprised = false;
    };
    return;
  };
  if(Shift == (TShiftState() << ssLeft)) {
    if((CellState[ACol][ARow].CellState == csOpened)) {
      if(FPressedCells.Length != 0) {
        UnpressAndInvalidate();
        InvalidateCell(ACol, ARow);
      };
    } else
    if((CellState[ACol][ARow].CellState == csClosed)
      || (CellState[ACol][ARow].CellState == csQuestionMarked))
      if(!IsExistsInArray(FPressedCells, ACol, ARow)) {
        UnpressAndInvalidate();
        AddPressedCell(ACol, ARow);
        InvalidateCell(ACol, ARow);
      }
    return;
  }

  if((Shift == (TShiftState() << ssLeft << ssRight)) || (Shift == (TShiftState() << ssLeft << ssRight))
    || (Shift == (TShiftState() << ssMiddle)) || (Shift == (TShiftState() << ssLeft << ssMiddle))
    || (Shift == (TShiftState() << ssRight << ssMiddle))) {
    UnpressAndInvalidate();
    for(int i=-1; i<=1; i++)
      for(int j=-1; j<=1; j++)
        if(CheckFieldBounds(ACol+i, ARow+j) &&
          ((CellState[ACol+i][ARow+j].CellState == csClosed) ||
          (CellState[ACol+i][ARow+j].CellState == csQuestionMarked))) {
            AddPressedCell(ACol+i, ARow+j);
            InvalidateCell(ACol+i, ARow+j);
          }
   }
}
//---------------------------------------------------------------------------

void __fastcall TIntMinerField::MouseUpHandler(System::TObject* Sender, Controls::TMouseButton Button, Classes::TShiftState Shift, int X, int Y)
{
  int ACol, ARow;
  SetCaptureControl(NULL);
  MouseToCell(X, Y, ACol, ARow);
  if((ACol == - 1) || (ARow == -1))
    return;
  if(((Shift == (TShiftState() << ssLeft)) && (Button == mbRight)) ||
    ((Shift == (TShiftState() << ssRight)) && (Button == mbLeft))
     || (Button == mbMiddle)) {
    FireImageChanged(imSmile);
    FSurprised = false;
    UnpressAndInvalidate();
    if(CellState[ACol][ARow].CellState == csOpened)
      FireMinerFieldEvent(ACol, ARow, meCheckSurround);
    return;
  };
  if(Button == mbLeft) {
    FireImageChanged(imSmile);
    FSurprised = false;
    if(IsExistsInArray(FPressedCells, ACol, ARow)) {
//      BeginUpdate();
      FireMinerFieldEvent(ACol, ARow, meOpenCell);
//      EndUpdate();
      UnpressAndInvalidate();
    };
    return;
  };
  if(Button == mbRight)
    UnpressAndInvalidate();
}
//---------------------------------------------------------------------------

void __fastcall TIntMinerField::HandleEvMinerFieldChanged(System::TObject* Sender, TCells &AChangedCells, TCells &ARedCells)
{
  InvalidateCells(AChangedCells, ARedCells);
//  AChangedCells.Length = 0;
//  ARedCells.Length = 0;
}
//---------------------------------------------------------------------------

void __fastcall TIntMinerField::HandleEvGameStatusChanged(System::TObject* Sender, TGameStatus AGameStatus, const TGameDifficulty &AGameDifficulty, TCells &AChangedCells, TCells &ARedCells)
{
  switch (AGameStatus) {
    case gsNew: {
      FGameStatus = gsNew;
      FGameDifficulty = AGameDifficulty;
      InitNewGame();
    }; break;
    case gsRun: FGameStatus = gsRun; break;
    case gsLost: {
      FGameStatus = gsLost;
      UpdateMinerFieldState(ARedCells);
      InvalidateCells(AChangedCells, ARedCells);
      Views[0]->Painter->Invalidate();
      Enabled = false;
    }; break;
    case gsWon: {
      FGameStatus = gsWon;
      UpdateMinerFieldState(ARedCells);
      InvalidateCells(AChangedCells, ARedCells);
      Views[0]->Painter->Invalidate();
      Enabled = false;
    }; break;
  };
  ARedCells.Length = 0;
  AChangedCells.Length = 0;
  FireGameStatusChanged(Sender, AGameStatus, AGameDifficulty);
}
//---------------------------------------------------------------------------

void __fastcall TIntMinerField::SetColorScheme(const UnboundModeDemoTypesH::TColorScheme Value)
{
  FColorScheme = Value;
  SetSchemeColors();
  Views[0]->Painter->Invalidate();
}
//---------------------------------------------------------------------------

void __fastcall TIntMinerField::SetNumberColors(void)
{
  FSurroundColors.Length = 8;
  FSurroundColors[0] = clBlue;
  FSurroundColors[1] = clGreen;
  FSurroundColors[2] = clRed;
  FSurroundColors[3] = clNavy;
  FSurroundColors[4] = clPurple;
  FSurroundColors[5] = clBlue;
  FSurroundColors[6] = clBlue;
  FSurroundColors[7] = clGray;
}
//---------------------------------------------------------------------------

void __fastcall TIntMinerField::FireMinerFieldEvent(int ACol, int ARow, TMinerFieldActionEventType AMinerFieldActionEventType)
{
  if(FOnMinerFieldAction != NULL)
    FOnMinerFieldAction(this, ACol, ARow, AMinerFieldActionEventType);
}
//---------------------------------------------------------------------------

void __fastcall TIntMinerField::FireNewGameEvent(void)
{
  if(FCreateNewGameEvent != NULL)
    FCreateNewGameEvent(this);
}
//---------------------------------------------------------------------------

void __fastcall TIntMinerField::FireImageChanged(int AImageIndex)
{
  if(FOnImageChanged != NULL)
    FOnImageChanged(this, AImageIndex);
}
//---------------------------------------------------------------------------

void __fastcall TIntMinerField::FireSetMineCountEvent(TMineCountChangedEventType AMineCountChangedEventType)
{
  if(FMineCountChanged != NULL)
    FMineCountChanged(this, AMineCountChangedEventType);
}
//---------------------------------------------------------------------------

void __fastcall TIntMinerField::FireGameStatusChanged(System::TObject* Sender, TGameStatus AGameStatus, const TGameDifficulty &AGameDifficulty)
{
  if(FGameStatusChanged != NULL)
    FGameStatusChanged(Sender, AGameStatus, AGameDifficulty);
}
//---------------------------------------------------------------------------

void __fastcall TIntMinerField::MouseToCell(int X, int Y, int &ACol, int &ARow)
{
  TcxCustomGridHitTest* AHitTest;
  ACol = -1;
  ARow = -1;
  AHitTest = ViewInfo->GetHitTest(X, Y);
  if(dynamic_cast<TcxGridRecordCellHitTest*>(AHitTest)) {
    ACol = ((TcxGridRecordCellHitTest*)AHitTest)->Item->Index;
    ARow = ((TcxGridRecordCellHitTest*)AHitTest)->GridRecord->Index;
  }
}
//---------------------------------------------------------------------------

void __fastcall TIntMinerField::CustomDrawCellHandler(Cxgridcustomtableview::TcxCustomGridTableView* Sender, Cxgraphics::TcxCanvas* ACanvas, Cxgridcustomtableview::TcxGridTableDataCellViewInfo* AViewInfo, bool &ADone)
{
  TCellStateRec CellRec;
  int ACol = AViewInfo->Item->Index;
  int ARow = AViewInfo->RecordViewInfo->Index;
  TRect DrawRect = AViewInfo->Bounds;
  int vType = VarType(AViewInfo->Value);
  if((vType == varEmpty) || (vType == varNull)) return;
  CellRec = *(PCellStateRec)(int)AViewInfo->Value;
  DrawCell(CellRec, ACol, ARow, DrawRect, ACanvas->Canvas);
  ADone = true;
}
//---------------------------------------------------------------------------

void __fastcall TIntMinerField::SetGridViewOptions(TcxGridTableView* GridView)
{
  GridView->OptionsData->Editing = false;
  GridView->OptionsData->Inserting = false;
  GridView->OptionsData->Deleting = false;
  GridView->OptionsView->GroupByBox = false;
  GridView->OptionsView->GridLines = glNone;
  GridView->OptionsView->FocusRect = false;

  GridView->OptionsSelection->CellSelect = false;
  GridView->OptionsSelection->HideSelection = false;
  GridView->OptionsSelection->InvertSelect = false;
  GridView->OptionsView->Header = false;
}
//---------------------------------------------------------------------------

__fastcall TIntMinerField::TIntMinerField(Classes::TComponent* AOwner, TMinerField* AMinerField) : TcxCustomGrid(AOwner)
{
  LookAndFeel->NativeStyle = false;
  TcxGridTableView* GridView = (TcxGridTableView*)CreateView(__classid(TcxGridTableViewNoScrollBars));

  TcxGridLevel* Level = Levels->Add();
  Level->GridView = GridView;

  FMinerFieldDataSource = new TMinerFieldDataSource();
  MinerField = AMinerField;
  MinerField->OnMinerFieldChanged = FMinerFieldDataSource->HandleEvMinerFieldChanged;
  MinerField->OnGameStatusChanged = FMinerFieldDataSource->HandleEvGameStatusChanged;

  FMinerFieldDataSource->OnMinerFieldChanged = HandleEvMinerFieldChanged;
  FMinerFieldDataSource->OnGameStatusChanged = HandleEvGameStatusChanged;

  GridView->DataController->CustomDataSource = FMinerFieldDataSource;
  SetGridViewOptions(GridView);

  GridView->OnCustomDrawCell = CustomDrawCellHandler;

  GridView->OnMouseDown = MouseDownHandler;
  GridView->OnMouseUp = MouseUpHandler;
  GridView->OnMouseMove = MouseMoveHandler;

  OnMinerFieldAction = MinerField->HandleMinerFieldActionEvent;

  FCellWidth = GridView->ViewInfo->RecordsViewInfo->RowHeight;
  FColorScheme = csBlue;
  SetNumberColors();
  SetSchemeColors();
}
//---------------------------------------------------------------------------

__fastcall TIntMinerField::~TIntMinerField(void)
{
  FOnMinerFieldAction = NULL;
  FMineCountChanged = NULL;
  FOnImageChanged = NULL;
  FGameStatusChanged = NULL;
  FPressedCells.Length = 0;
  FRedCells.Length = 0;
  delete FMinerFieldDataSource;
}
//---------------------------------------------------------------------------

void __fastcall TIntMinerField::CreateNewGame(void)
{
  FireNewGameEvent();
  InitNewGame();
}
//---------------------------------------------------------------------------

TMetaClass* __fastcall TcxGridTableViewNoScrollBars::GetControllerClass(void)
{
  return(__classid(TcxGridTableControllerNoScrollBars));
}
//---------------------------------------------------------------------------

void __fastcall TcxGridTableControllerNoScrollBars::InitScrollBarsParameters(void)
{
}
//---------------------------------------------------------------------------


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -