Skip to content

Commit

Permalink
[ui] Event handling for user shot #19
Browse files Browse the repository at this point in the history
  • Loading branch information
gbaudic committed Apr 15, 2020
1 parent 91c0b92 commit ada8807
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 50 deletions.
8 changes: 4 additions & 4 deletions src/common/messages.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@
#include <map>
#include <SDL2/SDL.h>

enum class WeaponType {
ONE,
TWO,
SUPERSHOT
enum class WeaponType : unsigned int {
ONE = 1,
TWO = 2,
SUPERSHOT = 55
};

/**
Expand Down
110 changes: 66 additions & 44 deletions src/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ namespace linbound {
* \param angle the angle to normalize, in degrees
* \return a value between 0 and 359 degrees
*/
Sint16 normalizeAngle(Sint16 angle){
Sint16 normalizeAngle(Sint16 angle) {
while(angle < 0)
angle += 360;

Expand Down Expand Up @@ -70,15 +70,15 @@ namespace linbound {
* Determine if mirroring is needed
* \param angle the angle in degree
*/
bool flip(Sint16 angle){
bool flip(Sint16 angle) {
angle = normalizeAngle(angle) - 180;
return abs(angle) > 90;
}

/**
* User-friendly version display
*/
string getVersionString(){
string getVersionString() {
string result = to_string(MAJOR_VERSION);
result += "." + to_string(MINOR_VERSION);
result += "." + to_string(PATCH_VERSION);
Expand All @@ -88,7 +88,7 @@ namespace linbound {
/**
* Version number to compare compatibilities
*/
int getVersion(){
int getVersion() {
return PATCH_VERSION + MINOR_VERSION*10 + MAJOR_VERSION*1000;
}

Expand All @@ -97,7 +97,7 @@ namespace linbound {
* \param address the Uint32 containing the address, using system endianness
* \return an IPv4 in the usual form x.y.z.a
*/
string prettifyIP(Uint32 address){
string prettifyIP(Uint32 address) {
int ip32 = (address >> 24) & 0xff;
int ip24 = (address >> 16) & 0xff;
int ip16 = (address >> 8) & 0xff;
Expand All @@ -106,20 +106,19 @@ namespace linbound {
return to_string(ip32)+"."+to_string(ip24)+"."+to_string(ip16)+"."+to_string(ip8);
}

/**
* Opposite operation of prettifyIP
* \param input user-specified input
* \return the IPv4 as an Uint32, using system endianness; 0 indicates an error
*/
Uint32 stringToIP(const std::string &input)
{
Uint32 result = 0x0;
int part = 3;
size_t pos = input.find('.');
size_t startPos = 0;
/**
* Opposite operation of prettifyIP
* \param input user-specified input
* \return the IPv4 as an Uint32, using system endianness; 0 indicates an error
*/
Uint32 stringToIP(const std::string &input) {
Uint32 result = 0x0;
int part = 3;
size_t pos = input.find('.');
size_t startPos = 0;
int converted = 0;
while (part >= 0) {
string val = input.substr(startPos);
while (part >= 0) {
string val = input.substr(startPos);

if(pos != string::npos) {
// Case for the first three parts
Expand All @@ -138,37 +137,60 @@ namespace linbound {
startPos = pos + 1;
pos = input.find('.', startPos);
}
part -= 1;
part -= 1;
}

} catch (invalid_argument) {
result = 0;
break;
}
}

if (pos != string::npos) {
// There should be nothing left at this point...
result = 0;
}

return result;
}

vector<string> split(const string & str, const char delim) {
vector<string> result;
size_t currentIndex = 0;
size_t end = str.find(delim);

while (end != string::npos) {
result.push_back(str.substr(currentIndex, end - currentIndex));

currentIndex = end + 1;
end = str.find(delim, currentIndex);
}
result.push_back(str.substr(currentIndex));

return result;
}
}

if (pos != string::npos) {
// There should be nothing left at this point...
result = 0;
}

return result;
}

/**
* \brief Split a string according to a delimiter
* \param str string to split
* \param delim character on which the split should be made
* \return a list of tokens
*/
vector<string> split(const string & str, const char delim) {
vector<string> result;
size_t currentIndex = 0;
size_t end = str.find(delim);

while (end != string::npos) {
result.push_back(str.substr(currentIndex, end - currentIndex));

currentIndex = end + 1;
end = str.find(delim, currentIndex);
}
result.push_back(str.substr(currentIndex));

return result;
}

/**
* \brief Make sure a value stays in an assigned range
* This is absolutely not like the minmax method in <algorithm>
* \param value value to check
* \param min lower bound of the range
* \param max upper bound of the range
* \return a value within the range [min, max]
*/
int minMax(const int value, const int min, const int max) {
if (value < min) {
return min;
} else if (value > max) {
return max;
}
return value;
}

}
5 changes: 3 additions & 2 deletions src/utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@ namespace linbound {
std::string getVersionString();
int getVersion();
std::string prettifyIP(Uint32 address);
Uint32 stringToIP(const std::string &input);
std::vector<std::string> split(const std::string &str, const char delim);
Uint32 stringToIP(const std::string &input);
std::vector<std::string> split(const std::string &str, const char delim);
int minMax(const int value, const int min, const int max);
}

#endif
28 changes: 28 additions & 0 deletions src/views/roomview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@
*/

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

Expand Down Expand Up @@ -141,6 +143,15 @@ void RoomView::processEvent(SDL_Event &event) {
if (event.key.keysym.sym == SDLK_SPACE && !tf_chat.isFocused() && currentMode == InteractionMode::TURN) {
// Player has finished shooting
currentMode = InteractionMode::IDLE;

stringstream msg;
// user name should go here
msg << ";" << static_cast<int>(getSelectedType());
msg << ";" << pb_power.getValue();
msg << ";" << lbl_currentAngle.getCaption();

network.send(SHOT_MSG, msg.str());
lbl_lastAngle.setCaption(lbl_currentAngle.getCaption());
}
// Support here arrow up/down (angle), left/right (motion), F7 (mobile), F8 (pass turn), F1-6 (item use)...
} else if (event.type == SDL_MOUSEMOTION) {
Expand Down Expand Up @@ -281,6 +292,23 @@ void RoomView::moveViewportTo(const int x, const int y) {
bg_rect.y = fg_rect.y * (bgH - getHeight()) / (fgH - getHeight());
}

/**
* \brief Get the weapon the player currently wants to fire
* \return the weapon type
*/
WeaponType RoomView::getSelectedType() {
if (btn_1.isFocused()) {
return WeaponType::ONE;
} else if (btn_2.isFocused()) {
return WeaponType::TWO;
} else if (btn_supershot.isFocused()) {
return WeaponType::SUPERSHOT;
}

// Degraded case, should never happen
return WeaponType::ONE;
}

/**
* Constructor
*/
Expand Down
2 changes: 2 additions & 0 deletions src/views/roomview.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "../ui/resultsdialog.hpp"
#include "../ui/scoreboard.hpp"
#include "../common/commonplayer.hpp" // temporary, use clientplayer
#include "../common/messages.hpp" // for WeaponType
#include "lobbyview.hpp"

/**
Expand Down Expand Up @@ -115,6 +116,7 @@ class RoomView: public Context, public gcn::ActionListener {
void updateMagicEdge(const int coordinate, Uint32 &target);
void moveViewport(const int xDelta, const int yDelta);
void moveViewportTo(const int x, const int y);
WeaponType getSelectedType();
};

#endif /* _H_ROOMVIEW_ */

0 comments on commit ada8807

Please sign in to comment.