Skip to content

Commit

Permalink
[view] Implementation of autoscroll in Room #19
Browse files Browse the repository at this point in the history
  • Loading branch information
gbaudic committed Dec 27, 2019
1 parent 9d4d05c commit 0139147
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 15 deletions.
3 changes: 3 additions & 0 deletions src/views/menu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,9 @@ void Menu::drawBackground(SDL_Renderer * screen) {
SDL_RenderCopy(screen, backTexture, NULL, NULL);
}

/**
* \brief Add the widgets to the container
*/
void Menu::addWidgets() {
addWidget(&btn_newGame, 70, 225);
addWidget(&btn_settings, 50 + 2 * 20 + 150, 225);
Expand Down
129 changes: 120 additions & 9 deletions src/views/roomview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,20 @@
* as defined by the Mozilla Public License, v. 2.0.
*/

#include <algorithm>
#include "../utils.hpp"
#include "roomview.hpp"
using namespace std;

const Uint16 RoomView::MAX_POWER;

/**
* Constructor
*/
RoomView::RoomView(const GameMode mode, GameMap *map) : Context(ContextName::ROOM), cursorLeft(0),
cursorRight(0), cursorTop(0), turnCount(0),
RoomView::RoomView(const GameMode mode, GameMap *map) : Context(ContextName::ROOM),
btn_1("1"), btn_2("2"), btn_supershot("SS"),
currentMode(InteractionMode::IDLE), gameMode(mode),
currentMap(map),
currentPower(0), motionLeft(0) {
currentMap(map) {

fg_rect.w = getWidth();
fg_rect.h = getHeight();
Expand All @@ -36,44 +37,154 @@ currentPower(0), motionLeft(0) {
btn_supershot.adjustSize();
btn_1.adjustSize();
btn_2.adjustSize();

btn_1.setWidth(btn_supershot.getWidth());
btn_2.setWidth(btn_supershot.getWidth());

tf_chat.setBackgroundColor(gcn::Color(0xff, 0xff, 0xff, 0)); // transparent
tf_chat.setForegroundColor(gcn::Color(0xff, 0xff, 0xff)); // white text and caret
tf_chat.setBorderSize(0);
tf_chat.setWidth(400);
tf_chat.setTabInEnabled(false);
tf_chat.setTabOutEnabled(false);
tf_chat.setActionEventId("text");
tf_chat.addActionListener(this);

currentMap->load();

}

RoomView::~RoomView() {
// TODO Auto-generated destructor stub
}

void RoomView::action(const gcn::ActionEvent &actionEvent) {

if(actionEvent.getId() == "text") {
// TODO send the message to the correct scope
}
}

void RoomView::drawBackground(SDL_Renderer *screen) {
// Check if we should move
const Uint32 currentTime = SDL_GetTicks();
Sint16 xdelta = 0;
Sint16 ydelta = 0;
if (cursorTop > 0 && currentTime - cursorTop >= AUTOSCROLL_DELAY) {
ydelta = -SCROLL_DELTA;
}
if (cursorBottom > 0 && currentTime - cursorBottom >= AUTOSCROLL_DELAY) {
ydelta = SCROLL_DELTA;
}
if (cursorLeft > 0 && currentTime - cursorLeft >= AUTOSCROLL_DELAY) {
xdelta = -SCROLL_DELTA;
}
if (cursorRight > 0 && currentTime - cursorRight >= AUTOSCROLL_DELAY) {
xdelta = SCROLL_DELTA;
}
moveViewport(xdelta, ydelta);

// Draw map background

// Draw map foreground

// Draw mobiles

}

void RoomView::drawOverlay(SDL_Renderer *screen) {

// TODO overlays appear for sudden deaths, game start or exceptional achievements
}

void RoomView::processEvent(SDL_Event &event) {
if (event.type == SDL_KEYDOWN) {
// Shooting
if (event.key.keysym.sym == SDLK_SPACE && !tf_chat.isFocused() && currentMode == InteractionMode::TURN) {
currentPower += POWER_INCREMENT;
currentPower = min(currentPower, MAX_POWER);
}
} else if (event.type == SDL_KEYUP) {
if (event.key.keysym.sym == SDLK_SPACE && !tf_chat.isFocused() && currentMode == InteractionMode::TURN) {
// Player has finished shooting
currentMode = InteractionMode::IDLE;
}
} else if (event.type == SDL_MOUSEMOTION) {
SDL_MouseMotionEvent mouseEvent = event.motion;

if (mouseEvent.x >= 0 && mouseEvent.x <= getWidth() && mouseEvent.y >= 0 && mouseEvent.y <= getHeight() - 100) {
// We are in the window, check if we are in an edge
// Left edge
updateMagicEdge(mouseEvent.x, cursorLeft);

// Right edge
updateMagicEdge(getWidth() - mouseEvent.x, cursorRight);

// Top edge
updateMagicEdge(mouseEvent.y, cursorTop);

// Bottom edge
updateMagicEdge(getHeight() - 100 - mouseEvent.y, cursorBottom);
}
}
}

/**
* Set timings for auto scrolling feature
* @param coordinate coordinate to test
* @param target time variable to update
*/
void RoomView::updateMagicEdge(const int coordinate, Uint32 &target) {
if (coordinate <= MAGIC_EDGE_WIDTH) {
if (target == 0) {
target = SDL_GetTicks();
}
} else {
target = 0;
}
}

/**
* \brief Add the widgets to the container
*/
void RoomView::addWidgets() {

addWidget(&tf_chat, 50, getHeight() - 100);
addWidget(&btn_supershot, 40, getHeight() - 5 - btn_supershot.getHeight());
addWidget(&btn_2, 40, btn_supershot.getY() - 5 - btn_2.getHeight());
addWidget(&btn_1, 40, btn_2.getY() - 5 - btn_1.getHeight());
}

/**
* \brief Prepare the view so the player can shoot and move
*/
void RoomView::setTurn() {
currentMode = InteractionMode::TURN;
currentPower = 0;
motionLeft = MOTION_LIMIT;
}

/**
* Move the viewport from a given offset
* @param xDelta x offset
* @param yDelta y offset
*/
void RoomView::moveViewport(const int xDelta, const int yDelta) {
moveViewportTo(fg_rect.x + xDelta, fg_rect.y + yDelta);
}

/**
* Move the viewport to a specified point
* This method will ensure the final coordinates are not off-limits
* TODO: implement a limit so we do not move too harshly
* @param x new x coordinate
* @param y new y coordinate
*/
void RoomView::moveViewportTo(const int x, const int y) {
int fgW, fgH;
SDL_QueryTexture(currentMap->getForeground(), NULL, NULL, &fgW, &fgH);
int bgW, bgH;
SDL_QueryTexture(currentMap->getBackground(), NULL, NULL, &bgW, &bgH);

fg_rect.x = max(0, min(x, fgW - getWidth() - 1));
fg_rect.y = max(0, min(x, fgH - getHeight() - 1));

// Now propagate the movement to the background rect
bg_rect.x = fg_rect.x * (bgW - getWidth()) / (fgW - getWidth());
bg_rect.y = fg_rect.y * (bgH - getHeight()) / (fgH - getHeight());
}
17 changes: 11 additions & 6 deletions src/views/roomview.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,10 @@ class RoomView: public Context, public gcn::ActionListener {
SDL_Rect fg_rect;
SDL_Rect bg_rect;

Uint32 cursorLeft;
Uint32 cursorRight;
Uint32 cursorTop;
Uint32 cursorLeft = 0;
Uint32 cursorRight = 0;
Uint32 cursorTop = 0;
Uint32 cursorBottom = 0;

// Widgets
gcn::TextField tf_chat;
Expand All @@ -61,21 +62,25 @@ class RoomView: public Context, public gcn::ActionListener {
gcn::Label lbl_currentAngle;
gcn::Label lbl_wind;

Uint16 turnCount;
Uint16 turnCount = 0;
InteractionMode currentMode;
GameMode gameMode;
GameMap *currentMap;

Uint16 currentPower;
Uint16 motionLeft;
Uint16 currentPower = 0;
Uint16 motionLeft = 0;

static const Uint32 AUTOSCROLL_DELAY = 2000; // in ms
static const Uint16 SCROLL_DELTA = 4; // in pixels, for the foreground
static const Uint16 MAX_POWER = 1000;
static const Uint16 POWER_INCREMENT = 5; // for power bar, per frame
static const Uint16 MOTION_LIMIT = 50;
static const Uint16 MAGIC_EDGE_WIDTH = 20; // in pixels

void addWidgets();
void updateMagicEdge(const int coordinate, Uint32 &target);
void moveViewport(const int xDelta, const int yDelta);
void moveViewportTo(const int x, const int y);
};

#endif /* _H_ROOMVIEW_ */

0 comments on commit 0139147

Please sign in to comment.