diff options
| author | Simon Robertshaw <simon@hardwired.org.uk> | 2012-01-28 14:51:39 (GMT) |
|---|---|---|
| committer | Simon Robertshaw <simon@hardwired.org.uk> | 2012-01-28 14:51:39 (GMT) |
| commit | b5728a9e3e983460a957fa0e576b751ebfe87172 (patch) | |
| tree | cb20500bc94da3fd00782981a45c9b05c6af36eb /src/game | |
| parent | 58ba7f8800403c572bae99115facc808c6dc34a3 (diff) | |
| download | powder-b5728a9e3e983460a957fa0e576b751ebfe87172.zip powder-b5728a9e3e983460a957fa0e576b751ebfe87172.tar.gz | |
Zoom
Diffstat (limited to 'src/game')
| -rw-r--r-- | src/game/GameController.cpp | 66 | ||||
| -rw-r--r-- | src/game/GameController.h | 4 | ||||
| -rw-r--r-- | src/game/GameModel.cpp | 63 | ||||
| -rw-r--r-- | src/game/GameModel.h | 12 | ||||
| -rw-r--r-- | src/game/GameView.cpp | 60 | ||||
| -rw-r--r-- | src/game/GameView.h | 4 |
6 files changed, 189 insertions, 20 deletions
diff --git a/src/game/GameController.cpp b/src/game/GameController.cpp index 277c32c..752a0ba 100644 --- a/src/game/GameController.cpp +++ b/src/game/GameController.cpp @@ -107,6 +107,36 @@ void GameController::AdjustBrushSize(int direction) gameModel->GetBrush()->SetRadius(newSize); } +void GameController::AdjustZoomSize(int direction) +{ + int newSize = gameModel->GetZoomSize()+direction; + if(newSize<5) + newSize = 5; + if(newSize>64) + newSize = 64; + gameModel->SetZoomSize(newSize); + + int newZoomFactor = 256/newSize; + if(newZoomFactor<3) + newZoomFactor = 3; + gameModel->SetZoomFactor(newZoomFactor); +} + +ui::Point GameController::PointTranslate(ui::Point point) +{ + bool zoomEnabled = gameModel->GetZoomEnabled(); + if(!zoomEnabled) + return point; + //If we try to draw inside the zoom window, normalise the coordinates + int zoomFactor = gameModel->GetZoomFactor(); + ui::Point zoomWindowPosition = gameModel->GetZoomWindowPosition(); + ui::Point zoomWindowSize = ui::Point(gameModel->GetZoomSize()*zoomFactor, gameModel->GetZoomSize()*zoomFactor); + + if(point.X > zoomWindowPosition.X && point.X > zoomWindowPosition.Y && point.X < zoomWindowPosition.X+zoomWindowSize.X && point.Y < zoomWindowPosition.Y+zoomWindowSize.Y) + return ((point-zoomWindowPosition)/gameModel->GetZoomFactor())+gameModel->GetZoomPosition(); + return point; +} + void GameController::DrawPoints(queue<ui::Point*> & pointQueue) { Simulation * sim = gameModel->GetSimulation(); @@ -123,26 +153,27 @@ void GameController::DrawPoints(queue<ui::Point*> & pointQueue) } } } + if(!pointQueue.empty()) { - ui::Point * sPoint = NULL; + ui::Point sPoint(0, 0); + bool first = true; while(!pointQueue.empty()) { - ui::Point * fPoint = pointQueue.front(); + ui::Point fPoint = PointTranslate(*pointQueue.front()); + delete pointQueue.front(); pointQueue.pop(); - if(sPoint) + if(!first) { - activeTool->DrawLine(sim, cBrush, *fPoint, *sPoint); - delete sPoint; + activeTool->DrawLine(sim, cBrush, fPoint, sPoint); } else { - activeTool->Draw(sim, cBrush, *fPoint); + first = false; + activeTool->Draw(sim, cBrush, fPoint); } sPoint = fPoint; } - if(sPoint) - delete sPoint; } } @@ -168,6 +199,25 @@ void GameController::Update() } } +void GameController::SetZoomEnabled(bool zoomEnabled) +{ + gameModel->SetZoomEnabled(zoomEnabled); +} + +void GameController::SetZoomPosition(ui::Point position) +{ + ui::Point zoomPosition = position-(gameModel->GetZoomSize()/2); + if(zoomPosition.X < 0) + zoomPosition.X = 0; + if(zoomPosition.Y < 0) + zoomPosition.Y = 0; + if(zoomPosition.X >= XRES-gameModel->GetZoomSize()) + zoomPosition.X = XRES-gameModel->GetZoomSize(); + if(zoomPosition.Y >= YRES-gameModel->GetZoomSize()) + zoomPosition.Y = YRES-gameModel->GetZoomSize(); + gameModel->SetZoomPosition(zoomPosition); +} + void GameController::SetPaused(bool pauseState) { gameModel->SetPaused(pauseState); diff --git a/src/game/GameController.h b/src/game/GameController.h index 2ec4523..7498e47 100644 --- a/src/game/GameController.h +++ b/src/game/GameController.h @@ -31,7 +31,10 @@ public: GameController(); ~GameController(); GameView * GetView(); + void SetZoomEnabled(bool zoomEnable); + void SetZoomPosition(ui::Point position); void AdjustBrushSize(int direction); + void AdjustZoomSize(int direction); void DrawPoints(queue<ui::Point*> & pointQueue); void Update(); void SetPaused(bool pauseState); @@ -48,6 +51,7 @@ public: void ReloadSim(); void Vote(int direction); void ChangeBrush(); + ui::Point PointTranslate(ui::Point point); }; #endif // GAMECONTROLLER_H diff --git a/src/game/GameModel.cpp b/src/game/GameModel.cpp index 5dfc521..456d9c4 100644 --- a/src/game/GameModel.cpp +++ b/src/game/GameModel.cpp @@ -160,6 +160,61 @@ User GameModel::GetUser() return currentUser; } +void GameModel::SetZoomEnabled(bool enabled) +{ + ren->zoomEnabled = enabled; + notifyZoomChanged(); +} + +bool GameModel::GetZoomEnabled() +{ + return ren->zoomEnabled; +} + +void GameModel::SetZoomPosition(ui::Point position) +{ + ren->zoomScopePosition = position; + notifyZoomChanged(); +} + +ui::Point GameModel::GetZoomPosition() +{ + return ren->zoomScopePosition; +} + +void GameModel::SetZoomWindowPosition(ui::Point position) +{ + ren->zoomWindowPosition = position; + notifyZoomChanged(); +} + +ui::Point GameModel::GetZoomWindowPosition() +{ + return ren->zoomWindowPosition; +} + +void GameModel::SetZoomSize(int size) +{ + ren->zoomScopeSize = size; + notifyZoomChanged(); +} + +int GameModel::GetZoomSize() +{ + return ren->zoomScopeSize; +} + +void GameModel::SetZoomFactor(int factor) +{ + ren->ZFACTOR = factor; + notifyZoomChanged(); +} + +int GameModel::GetZoomFactor() +{ + return ren->ZFACTOR; +} + void GameModel::SetUser(User user) { currentUser = user; @@ -253,3 +308,11 @@ void GameModel::notifyUserChanged() observers[i]->NotifyUserChanged(this); } } + +void GameModel::notifyZoomChanged() +{ + for(int i = 0; i < observers.size(); i++) + { + observers[i]->NotifyZoomChanged(this); + } +} diff --git a/src/game/GameModel.h b/src/game/GameModel.h index 72e2875..3c26bf3 100644 --- a/src/game/GameModel.h +++ b/src/game/GameModel.h @@ -32,6 +32,7 @@ private: Renderer * ren; Tool * activeTool; User currentUser; + //bool zoomEnabled; void notifyRendererChanged(); void notifySimulationChanged(); void notifyPausedChanged(); @@ -41,6 +42,7 @@ private: void notifyToolListChanged(); void notifyActiveToolChanged(); void notifyUserChanged(); + void notifyZoomChanged(); public: GameModel(); ~GameModel(); @@ -64,6 +66,16 @@ public: int GetBrushID(); Simulation * GetSimulation(); Renderer * GetRenderer(); + void SetZoomEnabled(bool enabled); + bool GetZoomEnabled(); + void SetZoomSize(int size); + int GetZoomSize(); + void SetZoomFactor(int factor); + int GetZoomFactor(); + void SetZoomPosition(ui::Point position); + ui::Point GetZoomPosition(); + void SetZoomWindowPosition(ui::Point position); + ui::Point GetZoomWindowPosition(); }; #endif // GAMEMODEL_H diff --git a/src/game/GameView.cpp b/src/game/GameView.cpp index 3a20b3e..3b1cd26 100644 --- a/src/game/GameView.cpp +++ b/src/game/GameView.cpp @@ -380,7 +380,7 @@ void GameView::OnMouseMove(int x, int y, int dx, int dy) void GameView::OnMouseDown(int x, int y, unsigned button) { - if(currentMouse.X > 0 && currentMouse.X < XRES && currentMouse.Y > 0 && currentMouse.Y < YRES) + if(currentMouse.X > 0 && currentMouse.X < XRES && currentMouse.Y > 0 && currentMouse.Y < YRES && !(zoomEnabled && !zoomCursorFixed)) { isMouseDown = true; pointQueue.push(new ui::Point(x, y)); @@ -389,10 +389,15 @@ void GameView::OnMouseDown(int x, int y, unsigned button) void GameView::OnMouseUp(int x, int y, unsigned button) { - if(isMouseDown) + if(zoomEnabled && !zoomCursorFixed) + zoomCursorFixed = true; + else { - isMouseDown = false; - pointQueue.push(new ui::Point(x, y)); + if(isMouseDown) + { + isMouseDown = false; + pointQueue.push(new ui::Point(x, y)); + } } } @@ -400,10 +405,17 @@ void GameView::OnMouseWheel(int x, int y, int d) { if(!d) return; - c->AdjustBrushSize(d); - if(isMouseDown) + if(zoomEnabled && !zoomCursorFixed) { - pointQueue.push(new ui::Point(x, y)); + c->AdjustZoomSize(d); + } + else + { + c->AdjustBrushSize(d); + if(isMouseDown) + { + pointQueue.push(new ui::Point(x, y)); + } } } @@ -417,11 +429,29 @@ void GameView::OnKeyPress(int key, bool shift, bool ctrl, bool alt) case KEY_TAB: //Tab c->ChangeBrush(); break; + case 'z': + isMouseDown = false; + zoomCursorFixed = false; + c->SetZoomEnabled(true); + break; } } +void GameView::OnKeyRelease(int key, bool shift, bool ctrl, bool alt) +{ + //switch(key) + //{ + //case 'z': + if(!zoomCursorFixed) + c->SetZoomEnabled(false); + // break; + //} +} + void GameView::OnTick(float dt) { + if(zoomEnabled && !zoomCursorFixed) + c->SetZoomPosition(currentMouse); if(isMouseDown) { pointQueue.push(new ui::Point(currentMouse)); @@ -433,17 +463,23 @@ void GameView::OnTick(float dt) c->Update(); } +void GameView::NotifyZoomChanged(GameModel * sender) +{ + zoomEnabled = sender->GetZoomEnabled(); +} + void GameView::OnDraw() { if(ren) { ren->render_parts(); ren->render_fire(); - ren->DrawSigns(); ren->DrawWalls(); - } - if(activeBrush && currentMouse.X > 0 && currentMouse.X < XRES && currentMouse.Y > 0 && currentMouse.Y < YRES) - { - activeBrush->Render(ui::Engine::Ref().g, currentMouse); + if(activeBrush && currentMouse.X > 0 && currentMouse.X < XRES && currentMouse.Y > 0 && currentMouse.Y < YRES) + { + activeBrush->Render(ui::Engine::Ref().g, c->PointTranslate(currentMouse)); + } + ren->RenderZoom(); + ren->DrawSigns(); } } diff --git a/src/game/GameView.h b/src/game/GameView.h index f0c3e82..610b953 100644 --- a/src/game/GameView.h +++ b/src/game/GameView.h @@ -18,6 +18,8 @@ class GameView: public ui::Window { private: bool isMouseDown; + bool zoomEnabled; + bool zoomCursorFixed; queue<ui::Point*> pointQueue; GameController * c; Renderer * ren; @@ -49,11 +51,13 @@ public: void NotifyToolListChanged(GameModel * sender); void NotifyActiveToolChanged(GameModel * sender); void NotifyUserChanged(GameModel * sender); + void NotifyZoomChanged(GameModel * sender); virtual void OnMouseMove(int x, int y, int dx, int dy); virtual void OnMouseDown(int x, int y, unsigned button); virtual void OnMouseUp(int x, int y, unsigned button); virtual void OnMouseWheel(int x, int y, int d); virtual void OnKeyPress(int key, bool shift, bool ctrl, bool alt); + virtual void OnKeyRelease(int key, bool shift, bool ctrl, bool alt); //virtual void OnKeyPress(int key, bool shift, bool ctrl, bool alt) {} //virtual void OnKeyRelease(int key, bool shift, bool ctrl, bool alt) {} virtual void OnTick(float dt); |
