📄 laomapedtview.cpp
字号:
// LaoMapEdtView.cpp : implementation of the CLaoMapEdtView class
//
#include "stdafx.h"
#include "LaoMapEdt.h"
#include "LaoMapEdtDoc.h"
#include "LaoMapEdtView.h"
#include "IJL.h"
#include "DlgSelLayer.h"
#include "MainFrm.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CLaoMapEdtView
IMPLEMENT_DYNCREATE(CLaoMapEdtView, CScrollView)
BEGIN_MESSAGE_MAP(CLaoMapEdtView, CScrollView)
//{{AFX_MSG_MAP(CLaoMapEdtView)
ON_WM_CREATE()
ON_WM_DESTROY()
ON_COMMAND(IDM_LAYER, OnLayer)
ON_COMMAND(IDM_CLOG, OnClog)
ON_WM_RBUTTONDOWN()
ON_WM_LBUTTONDOWN()
ON_WM_LBUTTONUP()
ON_WM_MOUSEMOVE()
ON_WM_HSCROLL()
ON_WM_VSCROLL()
//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CScrollView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CScrollView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CScrollView::OnFilePrintPreview)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CLaoMapEdtView construction/destruction
CLaoMapEdtView::CLaoMapEdtView()
{
// TODO: add construction code here
m_pbmBackPic = NULL;
m_euType = _NULL;
m_bIsLBtDown = FALSE;
m_pLastRect = NULL;
m_lBkWidth = 0;
m_lBkHeight = 0;
m_pArray = NULL;
m_csPicName.Empty();
m_bIsLoad = FALSE;
ZeroMemory(m_pLayerRect, 0xff*sizeof(CRectList*));
}
CLaoMapEdtView::~CLaoMapEdtView()
{
if (m_pbmBackPic)
{
if (m_pbmBackPic->m_hObject)
{
m_pbmBackPic->DeleteObject();
}
delete m_pbmBackPic;
}
SAFE_DELETE(m_pLastRect);
SAFE_DELETE_ALL(m_pArray);
ReleaseAllLayerRect();
}
BOOL CLaoMapEdtView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CScrollView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CLaoMapEdtView drawing
void CLaoMapEdtView::OnDraw(CDC* pDC)
{
CLaoMapEdtDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
HDC bdc;
g_lpBackBuffer->GetDC(&bdc);
pDC->BitBlt(0, 0, m_lBkWidth, m_lBkHeight, CDC::FromHandle(bdc), 0, 0, SRCCOPY);
g_lpBackBuffer->ReleaseDC(bdc);
long lLogicalX = m_lBkWidth / _BLOCK_SIZE;
long lLogicalY = m_lBkHeight / _BLOCK_SIZE;
CBrush *pbr1 = new CBrush(RGB(255,0,0));
CBrush *pbr2 = new CBrush(RGB(0,255,0));
switch (m_euType)
{
case _CLOG:
{
for (long y=0; y<lLogicalY; y++)
{
for (long x=0; x<lLogicalX; x++)
{
if (m_pArray[x+y*lLogicalX] & 1)
{
CRect rc(x*_BLOCK_SIZE+_BLOCK_SIZE/4, y*_BLOCK_SIZE+_BLOCK_SIZE/4, x*_BLOCK_SIZE+3*_BLOCK_SIZE/4, y*_BLOCK_SIZE+3*_BLOCK_SIZE/4);
pDC->Rectangle(&rc);
pDC->FillRect(&rc, pbr1);
}
}
}
break;
}
case _LAYER:
{
BYTE btCurLayer = ((CMainFrame*)AfxGetMainWnd())->GetLayer();
for (long y=0; y<lLogicalY; y++)
{
for (long x=0; x<lLogicalX; x++)
{
BYTE lLayer = m_pArray[x+y*lLogicalX] >> 8;
if (lLayer == btCurLayer)
{
CRect rc(x*_BLOCK_SIZE+_BLOCK_SIZE/4, y*_BLOCK_SIZE+_BLOCK_SIZE/4, x*_BLOCK_SIZE+3*_BLOCK_SIZE/4, y*_BLOCK_SIZE+3*_BLOCK_SIZE/4);
pDC->Rectangle(&rc);
pDC->FillRect(&rc, pbr2);
}
}
}
break;
}
default:
{
break;
}
}
pbr1->DeleteObject();
pbr2->DeleteObject();
SAFE_DELETE(pbr1);
SAFE_DELETE(pbr2);
}
void CLaoMapEdtView::OnInitialUpdate()
{
CScrollView::OnInitialUpdate();
while (!OpenBk())
{
if (AfxMessageBox("继续么?", MB_YESNO) == IDNO)
{
exit(-1);
}
}
CSize sizeTotal;
// TODO: calculate the total size of this view
sizeTotal.cx = m_lBkWidth;
sizeTotal.cy = m_lBkHeight;
SetScrollSizes(MM_TEXT, sizeTotal);
if (!m_bIsLoad)
{
SAFE_DELETE_ALL(m_pArray);
ReleaseAllLayerRect();
m_pArray = new WORD[m_lBkWidth/_BLOCK_SIZE * m_lBkHeight/_BLOCK_SIZE];
ASSERT(m_pArray);
ZeroMemory(m_pArray, m_lBkWidth/_BLOCK_SIZE * m_lBkHeight/_BLOCK_SIZE * sizeof(WORD));
m_pLayerRect[0] = new CRectList(0, 0, m_lBkWidth-1, m_lBkHeight-1);
}
else
{
m_bIsLoad = FALSE;
}
Invalidate(FALSE);
}
/////////////////////////////////////////////////////////////////////////////
// CLaoMapEdtView printing
BOOL CLaoMapEdtView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CLaoMapEdtView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CLaoMapEdtView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CLaoMapEdtView diagnostics
#ifdef _DEBUG
void CLaoMapEdtView::AssertValid() const
{
CScrollView::AssertValid();
}
void CLaoMapEdtView::Dump(CDumpContext& dc) const
{
CScrollView::Dump(dc);
}
CLaoMapEdtDoc* CLaoMapEdtView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CLaoMapEdtDoc)));
return (CLaoMapEdtDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CLaoMapEdtView message handlers
int CLaoMapEdtView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CScrollView::OnCreate(lpCreateStruct) == -1)
return -1;
// TODO: Add your specialized creation code here
return 0;
}
void CLaoMapEdtView::OnDestroy()
{
ReleaseDDraw();
CScrollView::OnDestroy();
// TODO: Add your message handler code here
}
CBitmap* CLaoMapEdtView::JPG2CBitmap(CString &filename)
{
JPEG_CORE_PROPERTIES image;
ZeroMemory( &image, sizeof( JPEG_CORE_PROPERTIES ) );
BYTE* imageData;
TRY
if( ijlInit( &image ) != IJL_OK )
{
TRACE( "Cannot initialize Intel JPEG library\n" );
AfxThrowUserException();
}
image.JPGFile = const_cast<char*>(filename.GetBuffer(filename.GetLength()));
if( ijlRead( &image, IJL_JFILE_READPARAMS ) != IJL_OK )
{
TRACE( "Cannot read JPEG file header from %s file\n",
image.JPGFile );
AfxThrowUserException();
}
// Set the JPG color space ... this will always be
// somewhat of an educated guess at best because JPEG
// is "color blind" (i.e., nothing in the bit stream
// tells you what color space the data was encoded from).
// However, in this example we assume that we are
// reading JFIF files which means that 3 channel images
// are in the YCbCr color space and 1 channel images are
// in the Y color space.
switch(image.JPGChannels)
{
case 1:
image.JPGColor = IJL_G;
image.DIBChannels = 3;
image.DIBColor = IJL_BGR;
break;
case 3:
image.JPGColor = IJL_YCBCR;
image.DIBChannels = 3;
image.DIBColor = IJL_BGR;
break;
case 4:
image.JPGColor = IJL_YCBCRA_FPX;
image.DIBChannels = 4;
image.DIBColor = IJL_RGBA_FPX;
break;
default:
// This catches everything else, but no
// color twist will be performed by the IJL.
image.DIBColor = (IJL_COLOR)IJL_OTHER;
image.JPGColor = (IJL_COLOR)IJL_OTHER;
image.DIBChannels = image.JPGChannels;
break;
}
image.DIBWidth = image.JPGWidth;
image.DIBHeight = image.JPGHeight;
image.DIBPadBytes = IJL_DIB_PAD_BYTES(image.DIBWidth,image.DIBChannels);
int imageSize = (image.DIBWidth * image.DIBChannels + image.DIBPadBytes) *
image.DIBHeight;
imageData = new BYTE[ imageSize ];
if( imageData == NULL )
{
TRACE( "Cannot allocate memory for image\n" );
AfxThrowUserException();
}
image.DIBBytes = imageData;
if( ijlRead( &image, IJL_JFILE_READWHOLEIMAGE ) != IJL_OK )
{
TRACE( "Cannot read image data from %s file\n", image.JPGFile );
delete[] imageData;
AfxThrowUserException();
}
if( ijlFree( &image ) != IJL_OK )
{
TRACE( "Cannot free Intel(R) JPEG library" );
}
if(image.DIBColor == IJL_RGBA_FPX)
{
RGBA_FPX_to_BGRA(imageData,image.DIBWidth,image.DIBHeight);
}
CATCH_ALL( e )
ijlFree( &image );
AfxMessageBox( "Error opening JPEG file" );
return NULL;
END_CATCH_ALL
// initializing incapsulated image with correct values
// imageData = imageData;
// imageDims.cx = image.DIBWidth;
// imageDims.cy = image.DIBHeight;
// imageChannels = image.DIBChannels;
CBitmap *pbm = new CBitmap;
if (!pbm)
{
AfxMessageBox("内存不够!");
return NULL;
}
DWORD bitcount = 0;
switch (image.DIBChannels)
{
case 3:
bitcount = 24;
break;
case 4:
bitcount = 32;
break;
default:
delete pbm;
return NULL;
}
if (!pbm->CreateBitmap(image.JPGWidth, image.JPGHeight,
1, bitcount, imageData))
{
delete pbm;
return NULL;
}
return pbm;
}
void CLaoMapEdtView::RGBA_FPX_to_BGRA(BYTE* data,int width,int height)
{
int i;
int j;
int pad;
int line_width;
BYTE r, g, b, a;
BYTE* ptr;
ptr = data;
pad = IJL_DIB_PAD_BYTES(width,4);
line_width = width * 4 + pad;
for(i = 0; i < height; i++)
{
ptr = data + line_width*i;
for(j = 0; j < width; j++)
{
r = ptr[0];
g = ptr[1];
b = ptr[2];
a = ptr[3];
ptr[2] = (BYTE)( (r*a+1) >> 8 );
ptr[1] = (BYTE)( (g*a+1) >> 8 );
ptr[0] = (BYTE)( (b*a+1) >> 8 );
ptr += 4;
}
}
return;
} // CJPGViewDoc::RGBA_FPX_to_BGRA()
void CLaoMapEdtView::BGRA_to_RGBA(BYTE* data,int width,int height)
{
int i;
int j;
int pad;
int line_width;
BYTE r, g, b, a;
BYTE* ptr;
ptr = data;
pad = IJL_DIB_PAD_BYTES(width,4);
line_width = width * 4 + pad;
for(i = 0; i < height; i++)
{
ptr = data + line_width*i;
for(j = 0; j < width; j++)
{
b = ptr[0];
g = ptr[1];
r = ptr[2];
a = ptr[3];
ptr[0] = r;
ptr[1] = g;
ptr[2] = b;
ptr += 4;
}
}
return;
}
void CLaoMapEdtView::OnLayer()
{
// TODO: Add your command handler code here
// CDlgSelLayer dlg;
// if (dlg.DoModal() == IDOK)
// {
// }
m_euType = _LAYER;
CMainFrame *pmainfrm = (CMainFrame*)AfxGetMainWnd();
CStatusBar *bar = pmainfrm->GetStatusBar();
pmainfrm->m_wndComboBox.EnableWindow(TRUE);
bar->SetPaneText(1, "设置层次");
Invalidate(FALSE);
}
void CLaoMapEdtView::OnClog()
{
// TODO: Add your command handler code here
m_euType = _CLOG;
CMainFrame *pmainfrm = (CMainFrame*)AfxGetMainWnd();
CStatusBar *bar = pmainfrm->GetStatusBar();
pmainfrm->m_wndComboBox.EnableWindow(FALSE);
bar->SetPaneText(1, "设置障碍");
Invalidate(FALSE);
}
void CLaoMapEdtView::OnRButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
m_euType = _NULL;
SetCursor(LoadCursor(NULL, IDC_ARROW));
CMainFrame *pmainfrm = (CMainFrame*)AfxGetMainWnd();
CStatusBar *bar = pmainfrm->GetStatusBar();
pmainfrm->m_wndComboBox.EnableWindow(FALSE);
bar->SetPaneText(1, "普通状态");
Invalidate(FALSE);
CScrollView::OnRButtonDown(nFlags, point);
}
void CLaoMapEdtView::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
m_bIsLBtDown = TRUE;
SetCapture();
m_ptStart = point;
Pos2Map(m_ptStart);
CRect rc(0, 0, m_lBkWidth/_BLOCK_SIZE, m_lBkHeight/_BLOCK_SIZE);
switch (m_euType)
{
case _CLOG:
case _LAYER:
{
if (rc.PtInRect(m_ptStart))
DrawSelRect(m_ptStart, 1, 1);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -