home *** CD-ROM | disk | FTP | other *** search
- #include "stdafx.h"
-
- #include "NewObject.h"
-
- cEditable *left_editable;
-
- static cEditable *right_editable;
- static CPoint last_game_point, last_back_point;
- static int dragging;
-
- IMPLEMENT_DYNCREATE(CBlasterView, CScrollView)
-
- BEGIN_MESSAGE_MAP(CBlasterView, CScrollView)
- //{{AFX_MSG_MAP(CBlasterView)
- ON_WM_KEYDOWN()
- ON_WM_SETCURSOR()
- ON_WM_ERASEBKGND()
- ON_COMMAND(ID_NEW_OBJECT, OnNewObject)
- ON_COMMAND(ID_SCROLL_UP, OnScrollUp)
- ON_COMMAND(ID_SCROLL_DOWN, OnScrollDown)
- ON_COMMAND(ID_PAGE_DOWN, OnPageDown)
- ON_COMMAND(ID_PAGE_UP, OnPageUp)
- ON_COMMAND(ID_SCROLL_END, OnScrollEnd)
- ON_COMMAND(ID_SCROLL_HOME, OnScrollHome)
- ON_WM_LBUTTONDOWN()
- ON_WM_LBUTTONUP()
- ON_WM_MOUSEMOVE()
- ON_COMMAND(ID_DELETE, OnDelete)
- ON_COMMAND(ID_EDIT_SNAPTOGRID, OnEditSnaptogrid)
- ON_UPDATE_COMMAND_UI(ID_EDIT_SNAPTOGRID, OnUpdateEditSnaptogrid)
- ON_COMMAND(ID_SCROLL_LEFT, OnScrollLeft)
- ON_COMMAND(ID_SCROLL_RIGHT, OnScrollRight)
- ON_WM_RBUTTONDOWN()
- ON_COMMAND(ID_CONTEXT_0, OnContext0)
- ON_COMMAND(ID_CONTEXT_1, OnContext1)
- ON_COMMAND(ID_CONTEXT_2, OnContext2)
- ON_COMMAND(ID_CONTEXT_3, OnContext3)
- ON_COMMAND(ID_CONTEXT_4, OnContext4)
- ON_COMMAND(ID_CONTEXT_5, OnContext5)
- ON_COMMAND(ID_CONTEXT_6, OnContext6)
- ON_UPDATE_COMMAND_UI(ID_DELETE, OnUpdateDelete)
- ON_UPDATE_COMMAND_UI(ID_EDIT_COPY, OnUpdateEditCopy)
- ON_UPDATE_COMMAND_UI(ID_EDIT_CUT, OnUpdateEditCut)
- ON_UPDATE_COMMAND_UI(ID_EDIT_PASTE, OnUpdateEditPaste)
- ON_WM_PAINT()
- ON_COMMAND(ID_EDIT_COPY, OnEditCopy)
- ON_COMMAND(ID_EDIT_CUT, OnEditCut)
- ON_COMMAND(ID_EDIT_PASTE, OnEditPaste)
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
-
- CBlasterView::CBlasterView()
- {
- }
-
- CBlasterView::~CBlasterView()
- {
- }
-
- BOOL CBlasterView::PreCreateWindow(CREATESTRUCT& cs)
- {
- return CScrollView::PreCreateWindow(cs);
- }
-
- void CBlasterView::OnDraw(CDC* pDC)
- {
- write_frame();
- }
-
- #ifdef _DEBUG
- void CBlasterView::AssertValid() const
- {
- CScrollView::AssertValid();
- }
-
- void CBlasterView::Dump(CDumpContext& dc) const
- {
- CScrollView::Dump(dc);
- }
-
- CBlasterDoc* CBlasterView::GetDocument()
- {
- ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CBlasterDoc)));
- return (CBlasterDoc*)m_pDocument;
- }
- #endif
-
- BOOL CBlasterView::Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID, CCreateContext* pContext)
- {
- return CWnd::Create(lpszClassName, lpszWindowName, dwStyle, rect, pParentWnd, nID, pContext);
- }
-
- void CBlasterView::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
- {
- CScrollView::OnKeyDown(nChar, nRepCnt, nFlags);
- }
-
- BOOL CBlasterView::DestroyWindow()
- {
- return CScrollView::DestroyWindow();
- }
-
- BOOL CBlasterView::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message)
- {
- SetCursor(LoadCursor(0, IDC_ARROW));
-
- return TRUE;
- }
-
- void CBlasterView::OnActivateView(BOOL bActivate, CView* pActivateView, CView* pDeactiveView)
- {
- if (bActivate)
- {
- if (!end_game)
- cTimer::unpause();
- }
- else
- {
- selectionbox_active = FALSE;
-
- ClipCursor(0);
-
- cTimer::pause();
- }
-
- CScrollView::OnActivateView(bActivate, pActivateView, pDeactiveView);
- }
-
- void CBlasterView::OnUpdate(CView* pSender, LPARAM lHint, CObject* pHint)
- {
- // Setup scroll bars
-
- SetScrollSizes(MM_TEXT, CSize(SCREEN_X, LEVEL_SIZE), CSize(10, SCROLL_PAGE), CSize(10, SCROLL_STEP));
-
- // Scroll to start of level
-
- ScrollTo(0, 0);
- }
-
- BOOL CBlasterView::OnEraseBkgnd(CDC* pDC)
- {
- // Erase everything except the game area
-
- CBrush br(GetSysColor(COLOR_APPWORKSPACE));
-
- CRect rect;
-
- GetClientRect(rect);
-
- rect.left = SCREEN_X;
-
- if (!rect.IsRectEmpty())
- pDC->FillRect(rect, &br);
-
- rect.left = 0;
- rect.right = SCREEN_X;
- rect.top = SCREEN_Y;
-
- if (!rect.IsRectEmpty())
- pDC->FillRect(rect, &br);
-
- return TRUE;
- }
-
- void CBlasterView::ScrollTo(int x, int y)
- {
- // Check boundaries of x
-
- CRect r;
- GetClientRect(&r);
-
- if (x + r.right - r.left > SCREEN_X)
- x = SCREEN_X - r.right + r.left;
-
- if (x < 0)
- x = 0;
-
- // Do scroll
-
- set_scroll_position(y);
-
- // Create new editables
-
- cEditable::make_editables();
-
- // Redraw our window
-
- Invalidate();
-
- // Update scroll bar
-
- ScrollToPosition(CPoint(x, LEVEL_SIZE - GAME_DY + 1 - game_surface->start));
-
- // Make sure OnMouseMove is called
-
- CPoint p;
- GetCursorPos(&p);
- SetCursorPos(p.x, p.y);
- }
-
- BOOL CBlasterView::OnScrollBy(CSize sizeScroll, BOOL bDoScroll)
- {
- ScrollTo(GetScrollPosition().x, LEVEL_SIZE - GAME_DY + 1 - GetScrollPosition().y);
-
- return CScrollView::OnScrollBy(sizeScroll, bDoScroll);
- }
-
- void CBlasterView::OnScrollUp()
- {
- ScrollTo(GetScrollPosition().x, game_surface->start + SCROLL_STEP);
- }
-
- void CBlasterView::OnScrollDown()
- {
- ScrollTo(GetScrollPosition().x, game_surface->start - SCROLL_STEP);
- }
-
- void CBlasterView::OnPageUp()
- {
- ScrollTo(GetScrollPosition().x, game_surface->start + SCROLL_PAGE);
- }
-
- void CBlasterView::OnPageDown()
- {
- ScrollTo(GetScrollPosition().x, game_surface->start - SCROLL_PAGE);
- }
-
- void CBlasterView::OnScrollHome()
- {
- if (!selectionbox_active && !cEditable::anything_selected())
- ScrollTo(GetScrollPosition().x, 0);
- }
-
- void CBlasterView::OnScrollEnd()
- {
- if (!selectionbox_active && !cEditable::anything_selected())
- ScrollTo(GetScrollPosition().x, LEVEL_SIZE);
- }
-
- void CBlasterView::OnScrollLeft()
- {
- ScrollTo(GetScrollPosition().x - SCROLL_STEP, game_surface->start);
- }
-
- void CBlasterView::OnScrollRight()
- {
- ScrollTo(GetScrollPosition().x + SCROLL_STEP, game_surface->start);
- }
-
- void CBlasterView::OnNewObject()
- {
- CNewObject n;
- n.DoModal();
- }
-
- void CBlasterView::ClientToSurface(CPoint &p, cSurface *surface, CPoint &s)
- {
- int x = GetScrollPosition().x;
-
- // Clip point
-
- if (p.x < GAME_X - x)
- p.x = GAME_X - x;
- else if (p.x >= GAME_X + GAME_DX - x)
- p.x = GAME_X + GAME_DX - x - 1;
-
- if (p.y < 0)
- p.y = 0;
- else if (p.y >= SCREEN_Y)
- p.y = SCREEN_Y - 1;
-
- // Translate point
-
- s.x = p.x - GAME_X - GetScrollPosition().x;
- s.y = GAME_DY - 1 - p.y + surface->start;
- }
-
- void CBlasterView::SurfaceToClient(CPoint &p, cSurface *surface, CPoint &s)
- {
- // Translate point
-
- s.x = p.x + GAME_X - GetScrollPosition().x;
- s.y = GAME_DY - 1 - p.y + surface->start;
-
- // Clip point
-
- if (s.x < 0)
- s.x = 0;
- else if (s.x >= SCREEN_X)
- s.x = SCREEN_X - 1;
-
- if (s.y < 0)
- s.y = 0;
- else if (s.y >= SCREEN_Y)
- s.y = SCREEN_Y - 1;
- }
-
- void CBlasterView::OnLButtonDown(UINT nFlags, CPoint point)
- {
- selectionbox_active = FALSE;
- dragging = FALSE;
-
- // Remember last point
-
- ClientToSurface(point, game_surface, last_game_point);
- ClientToSurface(point, back_surface, last_back_point);
-
- // Search for editable on current mouse spot
-
- left_editable = cEditable::find(last_game_point.x, last_game_point.y);
-
- if (left_editable == 0)
- {
- // Not an editable, if not shift held then unselect all
-
- if (!(nFlags & MK_SHIFT))
- cEditable::unselect_editables();
-
- // Start rectangle
-
- cEditable::set_rectangle(last_game_point.x, last_game_point.y);
-
- // Redraw our window
-
- Invalidate();
- }
- else if (!left_editable->selected)
- {
- // Current is not selected, if not shift held then unselect all
-
- if (!(nFlags & MK_SHIFT))
- cEditable::unselect_editables();
-
- // Redraw our window
-
- Invalidate();
- }
-
- // Clip mouse to window
-
- CRect r;
- GetClientRect(&r);
- ClientToScreen(&r);
- ClipCursor(&r);
-
- // Call base class
-
- CScrollView::OnLButtonDown(nFlags, point);
- }
-
- void CBlasterView::OnLButtonUp(UINT nFlags, CPoint point)
- {
- if (selectionbox_active)
- {
- // Translate selection box to selection
-
- cEditable::rectangle_to_selection();
-
- // Redraw our window
-
- Invalidate();
- }
-
- else if (snap_to_grid && dragging)
- {
- // We were dragging and snap to grid is on
-
- cEditable::snap_editables_to_grid();
-
- // Redraw our window
-
- Invalidate();
- }
-
- else if (left_editable != 0 && !dragging)
- {
- // Unselect all if we're not dragging and clicking in mid air
-
- if (!(nFlags & MK_SHIFT))
- cEditable::unselect_editables();
-
- // Toggle selection for current editable
-
- left_editable->selected = !left_editable->selected;
-
- // Redraw our window
-
- Invalidate();
- }
-
- // Not dragging anymore
-
- dragging = FALSE;
-
- // Release clip
-
- ClipCursor(0);
-
- // Call base class
-
- CScrollView::OnLButtonUp(nFlags, point);
- }
-
- void CBlasterView::OnRButtonDown(UINT nFlags, CPoint point)
- {
- CPoint p;
- CMenu popup;
-
- // Unselect all editables
-
- cEditable::unselect_editables();
-
- // Redraw our window
-
- Invalidate();
-
- // Get editable
-
- ClientToSurface(point, game_surface, p);
- right_editable = cEditable::find(p.x, p.y);
-
- // Check if anything was found
-
- if (right_editable == 0)
- return;
-
- // Select this editable
-
- right_editable->selected = TRUE;
-
- // Create popup
-
- popup.CreatePopupMenu();
- right_editable->create_context_menu(&popup);
-
- // Show pop-up menu
-
- GetCursorPos(&p);
- popup.TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON, p.x, p.y, this);
-
- // Call base class
-
- CScrollView::OnRButtonDown(nFlags, point);
- }
-
- void CBlasterView::OnMouseMove(UINT nFlags, CPoint point)
- {
- CPoint p_back, p_game;
-
- ClientToSurface(point, back_surface, p_back),
- ClientToSurface(point, game_surface, p_game);
-
- if (nFlags & MK_LBUTTON && p_game != last_game_point)
- {
- if (selectionbox_active)
- {
- // Update rectangle edge
-
- cEditable::set_edge_rectangle(p_game.x, p_game.y);
-
- // Move mouse cursor
-
- CPoint m;
- SurfaceToClient(p_game, game_surface, m);
- ClientToScreen(&m);
- SetCursorPos(m.x, m.y);
-
- // Set last point
-
- last_game_point = p_game;
- last_back_point = p_back;
-
- // Redraw our window
-
- Invalidate();
- }
-
- else if (left_editable != 0)
- {
- if (dragging)
- {
- // Select editable
-
- left_editable->selected = TRUE;
-
- // Drag editables
-
- cEditable::drag_editables(p_back.x - last_back_point.x, p_back.y - last_back_point.y, p_game.x - last_game_point.x, p_game.y - last_game_point.y);
-
- // Move mouse cursor to the editable
-
- CPoint m;
- SurfaceToClient(CPoint(left_editable->x, left_editable->y), left_editable->surface, m);
- ClientToScreen(&m);
- SetCursorPos(m.x, m.y);
-
- // Set last point
-
- last_back_point.x = left_editable->x;
- last_back_point.y = left_editable->y - left_editable->surface->start + back_surface->start;
-
- last_game_point.x = left_editable->x;
- last_game_point.y = left_editable->y - left_editable->surface->start + game_surface->start;
-
- // Document dirty
-
- GetDocument()->SetModifiedFlag();
-
- // Redraw our window
-
- Invalidate();
- }
- else if (d_square(p_game.x - last_game_point.x, p_game.y - last_game_point.y) > 25)
- {
- // Start dragging
-
- dragging = TRUE;
-
- // Set last_point
-
- last_game_point = p_game;
- last_back_point = p_back;
- }
- }
- }
-
- // Call base class
-
- CScrollView::OnMouseMove(nFlags, point);
- }
-
- void CBlasterView::OnEditCopy()
- {
- save_level(clipboard_level, TRUE);
- }
-
- void CBlasterView::OnUpdateEditCopy(CCmdUI* pCmdUI)
- {
- pCmdUI->Enable(cEditable::anything_selected());
- }
-
- void CBlasterView::OnEditCut()
- {
- save_level(clipboard_level, TRUE);
-
- cEditable::delete_selected_editables();
-
- GetDocument()->SetModifiedFlag();
-
- Invalidate();
- }
-
- void CBlasterView::OnUpdateEditCut(CCmdUI* pCmdUI)
- {
- pCmdUI->Enable(cEditable::anything_selected());
- }
-
- void CBlasterView::OnEditPaste()
- {
- // Unselect everything
-
- cEditable::unselect_editables();
-
- // Load the clipboard level
-
- load_level(clipboard_level, TRUE);
-
- // Document is dirty
-
- GetDocument()->SetModifiedFlag();
-
- // Rewrite everything
-
- cSurface::all_surfaces_dirty();
-
- Invalidate();
- }
-
- void CBlasterView::OnUpdateEditPaste(CCmdUI* pCmdUI)
- {
- pCmdUI->Enable(file_exists(clipboard_level));
- }
-
- void CBlasterView::OnDelete()
- {
- cEditable::delete_selected_editables();
-
- GetDocument()->SetModifiedFlag();
-
- Invalidate();
- }
-
- void CBlasterView::OnUpdateDelete(CCmdUI* pCmdUI)
- {
- pCmdUI->Enable(cEditable::anything_selected());
- }
-
- void CBlasterView::OnEditSnaptogrid()
- {
- snap_to_grid = !snap_to_grid;
- }
-
- void CBlasterView::OnUpdateEditSnaptogrid(CCmdUI* pCmdUI)
- {
- pCmdUI->SetCheck(snap_to_grid);
- }
-
- void CBlasterView::OnContext0()
- {
- right_editable->execute_context_menu(0);
-
- GetDocument()->SetModifiedFlag();
-
- Invalidate();
- }
-
- void CBlasterView::OnContext1()
- {
- right_editable->execute_context_menu(1);
-
- GetDocument()->SetModifiedFlag();
-
- Invalidate();
- }
-
- void CBlasterView::OnContext2()
- {
- right_editable->execute_context_menu(2);
-
- GetDocument()->SetModifiedFlag();
-
- Invalidate();
- }
-
- void CBlasterView::OnContext3()
- {
- right_editable->execute_context_menu(3);
-
- GetDocument()->SetModifiedFlag();
-
- Invalidate();
- }
-
- void CBlasterView::OnContext4()
- {
- right_editable->execute_context_menu(4);
-
- GetDocument()->SetModifiedFlag();
-
- Invalidate();
- }
-
- void CBlasterView::OnContext5()
- {
- right_editable->execute_context_menu(5);
-
- GetDocument()->SetModifiedFlag();
-
- Invalidate();
- }
-
- void CBlasterView::OnContext6()
- {
- right_editable->execute_context_menu(6);
-
- GetDocument()->SetModifiedFlag();
-
- Invalidate();
- }
-