Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/game/CAvaraApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,10 @@ bool CAvaraAppImpl::DoCommand(int theCommand) {
}
break;
}
case kKeyboardMappingReset: {
auto player = CPlayerManagerImpl::LocalPlayer();
if (player) player->ResetKeyMap();
}
default:
break;
}
Expand Down
50 changes: 27 additions & 23 deletions src/game/CPlayerManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,33 @@ void CPlayerManagerImpl::IPlayerManager(CAvaraGame *theGame, short id, CNetManag
oldMouse.v = mouseCenterPosition.v;
lastMouseControlTime = 0;

ResetKeyMap();
// mainScreenRect = &(*GetMainDevice())->gdRect;
// mouseCenterPosition.h = (mainScreenRect->left + mainScreenRect->right) / 2;
// mouseCenterPosition.v = (mainScreenRect->top + mainScreenRect->bottom) / 2;

// theRoster = aRoster;
theNetManager = aNetManager;
levelCRC = 0;
levelTag = "";
position = id;
itsPlayer = NULL;

mugPict = NULL;
mugSize = -1;
mugState = 0;

NetDisconnect();
SetLocal();
if (slot == 0) {
CPlayerManagerImpl::theServerPlayer = this;
}

prevKeyboardActive = keyboardActive;
}

void CPlayerManagerImpl::ResetKeyMap() {
keyMap.clear();
// Mirrors what's in Preferences.h for kKeyboardMappingTag
json commandBits = {{"forward", 1 << kfuForward},
{"backward", 1 << kfuReverse},
Expand Down Expand Up @@ -126,29 +153,6 @@ void CPlayerManagerImpl::IPlayerManager(CAvaraGame *theGame, short id, CNetManag
controllerButtonMap[SDL_CONTROLLER_BUTTON_RIGHTSHOULDER] = 1 << kfuLoadGrenade;
controllerButtonMap[SDL_CONTROLLER_BUTTON_BACK] = 1 << kfuAbortGame;
controllerButtonMap[SDL_CONTROLLER_BUTTON_START] = 1 << kfuPauseGame;

// mainScreenRect = &(*GetMainDevice())->gdRect;
// mouseCenterPosition.h = (mainScreenRect->left + mainScreenRect->right) / 2;
// mouseCenterPosition.v = (mainScreenRect->top + mainScreenRect->bottom) / 2;

// theRoster = aRoster;
theNetManager = aNetManager;
levelCRC = 0;
levelTag = "";
position = id;
itsPlayer = NULL;

mugPict = NULL;
mugSize = -1;
mugState = 0;

NetDisconnect();
SetLocal();
if (slot == 0) {
CPlayerManagerImpl::theServerPlayer = this;
}

prevKeyboardActive = keyboardActive;
}

void CPlayerManagerImpl::SetPlayer(CAbstractPlayer *thePlayer) {
Expand Down
3 changes: 2 additions & 1 deletion src/game/CPlayerManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ class CPlayerManager {
virtual void IncrementAskAgainTime(int) = 0;
virtual void SetShowScoreboard(bool b) = 0;
virtual bool GetShowScoreboard() = 0;
virtual void ResetKeyMap() = 0;
};

class CPlayerManagerImpl : public CDirectObject, public CPlayerManager, public std::enable_shared_from_this<CPlayerManagerImpl> {
Expand Down Expand Up @@ -315,6 +316,6 @@ class CPlayerManagerImpl : public CDirectObject, public CPlayerManager, public s
virtual void IncrementAskAgainTime(int);
virtual void SetShowScoreboard(bool b);
virtual bool GetShowScoreboard();

virtual void ResetKeyMap();
void PlaybackAndRecord(FunctionTable &ft);
};
45 changes: 20 additions & 25 deletions src/gui/CKeyboardMappingWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@
#include "CKeyboardMappingWindow.h"
#include "Preferences.h"
#include "CApplication.h"
#include "CommandList.h"

CKeyboardMappingWindow::CKeyboardMappingWindow(CApplication *app, const std::string &actionDesc, const std::string &key, int imageOffset, int imageHandle) : CWindow(app, "Keyboard Mapping") {
CKeyboardMappingWindow::CKeyboardMappingWindow(CApplication *app, const std::string &actionDesc, const std::string &action, int imageOffset, int imageHandle) : CWindow(app, "Keyboard Mapping") {
setTitle("Map Keys to Action");
theKey = new std::string(key);
theAction = new std::string(action);
auto layout = new nanogui::GridLayout(nanogui::Orientation::Horizontal, 2, nanogui::Alignment::Fill, 25, 25);
layout->setRowAlignment(nanogui::Alignment::Middle);
layout->setColAlignment(nanogui::Alignment::Middle);
Expand All @@ -29,20 +30,18 @@ CKeyboardMappingWindow::CKeyboardMappingWindow(CApplication *app, const std::str
//actionLabel->setFixedWidth(300);
actionLabel->setCaption(actionDesc);

currentlyMapping = new std::string(key);

json allmap = app->Get(kKeyboardMappingTag);
auto k = allmap.at(key);
auto k = allmap.at(action);
if (k.is_array()) {
for (auto ik : k.items()) {
std::string sdlkey = ik.value();
add<nanogui::Label>(sdlkey);
removeMappingButton(key, sdlkey);
removeMappingButton(action, sdlkey);
}
}
else {
add<nanogui::Label>(k);
removeMappingButton(key, k);
removeMappingButton(action, k);
}
addbtn = add<nanogui::Button>("Add New");
addbtn->setBackgroundColor(kGUIAccentPositive);
Expand All @@ -55,8 +54,8 @@ CKeyboardMappingWindow::CKeyboardMappingWindow(CApplication *app, const std::str
closeBtn->setCallback([this] {
setModal(false);
setVisible(false);
gathering = false;
if (mCallback) mCallback(0);
dispose();
});
setNeedsLayout();
setVisible(true);
Expand All @@ -74,30 +73,24 @@ void CKeyboardMappingWindow::removeMappingButton(const std::string &action, cons
keybtn->setCallback([this, action, key] {
SDL_Log("Delete %s bound to %s", key.c_str(), action.c_str());
removeKeyBind(key);
gathering = false;
if (mCallback) mCallback(1);
dispose();
});
}

void CKeyboardMappingWindow::gatherKey() {
gathering = true;
}

bool CKeyboardMappingWindow::editing() {
return true;
return gathering;
}

bool CKeyboardMappingWindow::handleSDLEvent(SDL_Event &event) {
if (gathering) {
if (event.type == SDL_KEYDOWN) {
auto sym = event.key.keysym.sym;
auto name = SDL_GetKeyName(sym);
//actionLabel->setCaption(name);
SDL_Log("%d %s", sym, name);
addKeyBind(name);
gathering = false;
if (mCallback) mCallback(1);
dispose();
return true;
}
}
Expand All @@ -107,33 +100,35 @@ bool CKeyboardMappingWindow::handleSDLEvent(SDL_Event &event) {

void CKeyboardMappingWindow::addKeyBind(const std::string &keyname) {
json allmap = mApplication->Get(kKeyboardMappingTag);
auto theKeyCStr = theKey->c_str();
auto k = allmap.at(theKeyCStr);
if (!(allmap.size()) || !theAction) return;
auto theActionCStr = theAction->c_str();
auto k = allmap.at(theActionCStr);
if (k.is_array()) {
bool found = false;
for (auto ik : k.items()) {
if (ik.value() == keyname) {
found = true;
SDL_Log("Not binding %s to %s because it is already bound", theKeyCStr, keyname.c_str());
SDL_Log("Not binding %s to %s because it is already bound", theActionCStr, keyname.c_str());
}
}
if (!found) {
k.push_back(json(keyname));
allmap[theKeyCStr] = k;
allmap[theActionCStr] = k;
}
}
else if (k != keyname) {
json new_arr = nlohmann::json::array();
new_arr = {k, json(keyname)};
allmap[theKeyCStr] = new_arr;
allmap[theActionCStr] = new_arr;
}
mApplication->Set(kKeyboardMappingTag, allmap);
}

void CKeyboardMappingWindow::removeKeyBind(const std::string &keyname) {

json allmap = mApplication->Get(kKeyboardMappingTag);
auto k = allmap.at(theKey->c_str());
if (!(allmap.size()) || !theAction) return;
auto theActionCStr = theAction->c_str();
auto k = allmap.at(theActionCStr);
auto remove_idxs = std::vector<int>();
if (k.is_array()) {
int ik_idx = 0;
Expand All @@ -146,10 +141,10 @@ void CKeyboardMappingWindow::removeKeyBind(const std::string &keyname) {
for (auto idx : remove_idxs) {
k.erase(idx);
}
allmap[theKey->c_str()] = k;
allmap[theActionCStr] = k;
}
else {
allmap[theKey->c_str()] = json("");
allmap[theActionCStr] = json("");
}
mApplication->Set(kKeyboardMappingTag, allmap);
}
7 changes: 3 additions & 4 deletions src/gui/CKeyboardMappingWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,18 @@

class CKeyboardMappingWindow : public CWindow {
public:
CKeyboardMappingWindow(CApplication *app, const std::string &actionDesc, const std::string &key, int imageOffset, int imageHandle);
CKeyboardMappingWindow(CApplication *app, const std::string &actionDesc, const std::string &action, int imageOffset, int imageHandle);
virtual bool editing();
bool handleSDLEvent(SDL_Event &event);
void gatherKey();
bool gathering = false;
std::function<void(int)> callback() const { return mCallback; }
void setCallback(const std::function<void(int)> &callback) { mCallback = callback; }
protected:
bool gathering = false;
std::function<void(int)> mCallback;
void removeMappingButton(const std::string &action, const std::string &key);
void addKeyBind(const std::string &keyname);
void removeKeyBind(const std::string &keyname);
std::string *theKey;
std::string *theAction;
SpriteWidget *actionIcon;
nanogui::Label *actionLabel;
nanogui::Label *currentLabel;
Expand Down
14 changes: 9 additions & 5 deletions src/gui/CSettingsWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "AssetManager.h"
#include "ARGBColor.h"
#include "NVGUtil.h"
#include "CommandList.h"

std::string stringForAction(std::string action) {
json theKeys = gApplication->Get(kKeyboardMappingTag);
Expand All @@ -38,6 +39,7 @@ CSettingsWindow::CSettingsWindow(CApplication *app) : CWindow(app, "Avara Settin

NVGcontext *ctx = app->nvgContext();
currentlyMappingKey = false;
keyMapWindow = 0;

std::string kbIconsPath = AssetManager::GetImagePath(NoPackage, "control48px.png");
keyboardIconsDataHandle = nvgCreateImage(ctx, kbIconsPath.c_str(), 0);
Expand Down Expand Up @@ -184,9 +186,9 @@ CSettingsWindow::CSettingsWindow(CApplication *app) : CWindow(app, "Avara Settin
currentlyMappingActionDesc = actionDesc;
currentlyMappingButton = longbutton;
keyMapWindow = new CKeyboardMappingWindow(this->mApplication, actionDesc, actionKey, keyboardIconOffset, keyboardIconsDataHandle);
keyMapWindow->setCallback([this, longbutton, actionKey] (int status) {
keyMapWindow->setCallback([this, longbutton] (int status) {
longbutton->setCaption(stringForAction(currentlyMappingAction));
refreshKeyboardMappingWindow(status);
longbutton->setCaption(stringForAction(actionKey));
});
});
keyboardConfigIndex++;
Expand All @@ -207,19 +209,21 @@ CSettingsWindow::CSettingsWindow(CApplication *app) : CWindow(app, "Avara Settin

void CSettingsWindow::refreshKeyboardMappingWindow(int status) {
if (status) {
keyMapWindow->dispose();
keyMapWindow = new CKeyboardMappingWindow(this->mApplication, currentlyMappingActionDesc, currentlyMappingAction, currentlyMappingKeyboardIconOffset, keyboardIconsDataHandle);
keyMapWindow->setCallback([this] (int status) {
refreshKeyboardMappingWindow(status);
currentlyMappingButton->setCaption(stringForAction(currentlyMappingAction));
refreshKeyboardMappingWindow(status);
});
}
else {
currentlyMappingKey = false;
}
this->mApplication->DoCommand(kKeyboardMappingReset);
}

bool CSettingsWindow::currentlyMapping() {
return keyMapWindow && keyMapWindow->gathering;
return keyMapWindow && keyMapWindow->editing();
}

CKeyboardMappingWindow* CSettingsWindow::getKeyMapWindow() {
Expand All @@ -232,7 +236,7 @@ bool CSettingsWindow::editing() {

bool CSettingsWindow::handleSDLEvent(SDL_Event &event) {
SDL_Log("CSettingsWindowEvent");
if (keyMapWindow && keyMapWindow->gathering) {
if (keyMapWindow && keyMapWindow->editing()) {
return keyMapWindow->handleSDLEvent(event);
}
return false;
Expand Down
2 changes: 1 addition & 1 deletion src/gui/CSettingsWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class CSettingsWindow : public CWindow {
virtual bool editing();
bool handleSDLEvent(SDL_Event &event);
bool currentlyMapping();
CKeyboardMappingWindow* getKeyMapWindow();
CKeyboardMappingWindow *getKeyMapWindow();
void refreshKeyboardMappingWindow(int status);
protected:
nanogui::TabWidget *mPrefTabs;
Expand Down
1 change: 1 addition & 0 deletions src/gui/CommandList.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ enum {
kShowInfoPanel,
kShowLevelListWind,
kPrepareShowLevelListWind,
kKeyboardMappingReset,

kUseDefaultDirectory = 250,
kAutoSaveScores,
Expand Down
1 change: 1 addition & 0 deletions src/tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ class TestPlayerManager : public CPlayerManager {
virtual void IncrementAskAgainTime(int) {}
virtual void SetShowScoreboard(bool b) {}
virtual bool GetShowScoreboard() { return false; }
virtual void ResetKeyMap(){}

private:
FunctionTable *ft;
Expand Down
7 changes: 7 additions & 0 deletions src/tui/CommandManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,13 @@ CommandManager::CommandManager(CAvaraAppImpl *theApp) : itsApp(theApp) {
return false;
});
TextCommand::Register(cmd);

cmd = new TextCommand("/tracker <- show the tracker window",
[this](VectorOfArgs vargs) -> bool {
itsApp->trackerWindow->setVisible(!itsApp->trackerWindow->visible());
return false;
});
TextCommand::Register(cmd);
}


Expand Down
Loading