diff --git a/projects/mtg/include/TranslateKeys.h b/projects/mtg/include/TranslateKeys.h index e174a47ac..735480e60 100644 --- a/projects/mtg/include/TranslateKeys.h +++ b/projects/mtg/include/TranslateKeys.h @@ -1,17 +1,11 @@ #ifndef _TRANSLATEKEYS_H_ #define _TRANSLATEKEYS_H_ +#include #include #include "JGE.h" -struct KeyRep -{ - KeyRep(JQuad*, std::string); - JQuad* icon; - std::string text; -}; - - +typedef pair KeyRep; const KeyRep& translateKey(LocalKeySym); const KeyRep& translateKey(JButton); diff --git a/projects/mtg/include/WGui.h b/projects/mtg/include/WGui.h index a27562049..07c8c8b99 100644 --- a/projects/mtg/include/WGui.h +++ b/projects/mtg/include/WGui.h @@ -493,6 +493,7 @@ class WGuiKeyBinder : public WGuiList { virtual bool isModal(); virtual bool CheckUserInput(JButton); virtual void setData(); + virtual void Update(float); protected: GameStateOptions* parent; bool modal; diff --git a/projects/mtg/src/OptionItem.cpp b/projects/mtg/src/OptionItem.cpp index 65e451f4b..dc440922c 100644 --- a/projects/mtg/src/OptionItem.cpp +++ b/projects/mtg/src/OptionItem.cpp @@ -446,15 +446,15 @@ void OptionKey::Render() { else { const KeyRep& rep = translateKey(from); - if (rep.icon) - renderer->RenderQuad(rep.icon, x + 4, y + 2, 0, 0.5, 0.5); + if (rep.second) + renderer->RenderQuad(rep.second, x + 4, y + 2, 0, 16.0 / rep.second->mHeight, 16.0 / rep.second->mHeight); else - mFont->DrawString(rep.text, x + 4, y + 2, JGETEXT_LEFT); + mFont->DrawString(rep.first, x + 4, y + 2, JGETEXT_LEFT); const KeyRep& rep2 = translateKey(to); - if (rep2.icon) - renderer->RenderQuad(rep2.icon, x + 4, y + 2, 0, 0.5, 0.5); + if (rep2.second) + renderer->RenderQuad(rep2.second, x + 4, y + 2, 0, 16.0 / rep2.second->mHeight, 16.0 / rep2.second->mHeight); else - mFont->DrawString(rep2.text, width - 4, y + 2, JGETEXT_RIGHT); + mFont->DrawString(rep2.first, width - 4, y + 2, JGETEXT_RIGHT); } } bool OptionKey::CheckUserInput(JButton key) { @@ -479,13 +479,12 @@ void OptionKey::KeyPressed(LocalKeySym key) { grabbed = false; btnMenu = NEW SimpleMenu(0, this, Constants::MENU_FONT, 80, 10); - for (int i = sizeof(btnList) / sizeof(btnList[0]) - 1; i >= 0; --i) - { + for (int i = sizeof(btnList) / sizeof(btnList[0]) - 1; i >= 0; --i) { const KeyRep& rep = translateKey(btnList[i]); - btnMenu->Add(i, rep.text.c_str()); + btnMenu->Add(i, rep.first.c_str()); } } -bool OptionKey::isModal() { return grabbed; } +bool OptionKey::isModal() { return grabbed || btnMenu; } void OptionKey::Overlay() { JRenderer * renderer = JRenderer::GetInstance(); @@ -500,8 +499,7 @@ void OptionKey::Overlay() else if (btnMenu) btnMenu->Render(); } -void OptionKey::ButtonPressed(int controllerId, int controlId) -{ +void OptionKey::ButtonPressed(int controllerId, int controlId) { to = btnList[controlId]; SAFE_DELETE(btnMenu); btnMenu = NULL; diff --git a/projects/mtg/src/TranslateKeys.cpp b/projects/mtg/src/TranslateKeys.cpp index 5e1decb4b..2bda7e8e6 100644 --- a/projects/mtg/src/TranslateKeys.cpp +++ b/projects/mtg/src/TranslateKeys.cpp @@ -6,17 +6,15 @@ using std::string; using std::map; -static map fattable; -static map slimtable; - -KeyRep::KeyRep(JQuad* icon, string text) : icon(icon), text(text) {} +static map fattable; +static map slimtable; #ifdef LINUX const KeyRep& translateKey(LocalKeySym key) { { - map::iterator res; + map::iterator res; if ((res = fattable.find(key)) != fattable.end()) - return *(res->second); + return res->second; } char* str = XKeysymToString(key); @@ -25,82 +23,121 @@ const KeyRep& translateKey(LocalKeySym key) { str = new char[11]; sprintf(str, "%lu", key); } - KeyRep* k = new KeyRep(NULL, str); + const KeyRep k = make_pair(str, static_cast(NULL)); fattable[key] = k; - return *k; + return fattable[key]; } #else #ifdef WIN32 const KeyRep& translateKey(LocalKeySym key) { { - map::iterator res; + map::iterator res; if ((res = fattable.find(key)) != fattable.end()) return *(res->second); } + /* I think the code should look like this ? + Documentation from : http://msdn.microsoft.com/en-us/library/ms171538.aspx + + Keys keyCode = (Keys)((int)key) & Keys::KeyCode; + string s = keyCode.ToString(); + KeyRep k; + if (0 == s.length()) { + char*str = new char[11]; + sprintf(str, "%d", key); + k = make_pair(str, static_cast(NULL)); + } + else k = make_pair(s, static_cast(NULL)); + fattable[key] = k; + return fattable[key]; + + ... Instead of the following : + */ + + char* str = new char[11]; sprintf(str, "%d", key); - KeyRep* k = new KeyRep(NULL, str); + const KeyRep k = make_pair(str, static_cast(NULL)); fattable[key] = k; - return *k; + return fattable[key]; } #else // PSP -const KeyRep& translateKey(LocalKeySym key) { - map::iterator res; - if ((res = fattable.find(key)) != fattable.end()) - return *(res->second); - if (fattable.end() == fattable.find(PSP_CTRL_SELECT)) +const KeyRep& translateKey(LocalKeySym key) { + map::iterator res; + if ((res = fattable.find(key)) == fattable.end()) { - fattable[PSP_CTRL_SELECT] = new KeyRep(NULL, _("Select")); - fattable[PSP_CTRL_START] = new KeyRep(NULL, _("Start")); - fattable[PSP_CTRL_UP] = new KeyRep(NULL, _("Up")); - fattable[PSP_CTRL_RIGHT] = new KeyRep(NULL, _("Right")); - fattable[PSP_CTRL_DOWN] = new KeyRep(NULL, _("Down")); - fattable[PSP_CTRL_LEFT] = new KeyRep(NULL, _("Left")); - fattable[PSP_CTRL_LTRIGGER] = new KeyRep(NULL, _("Left trigger")); - fattable[PSP_CTRL_RTRIGGER] = new KeyRep(NULL, _("Right trigger")); - fattable[PSP_CTRL_TRIANGLE] = new KeyRep(NULL, _("Triangle")); - fattable[PSP_CTRL_CIRCLE] = new KeyRep(NULL, _("Circle")); - fattable[PSP_CTRL_CROSS] = new KeyRep(NULL, _("Cross")); - fattable[PSP_CTRL_SQUARE] = new KeyRep(NULL, _("Square")); - fattable[PSP_CTRL_HOLD] = new KeyRep(NULL, _("Hold")); + if (fattable.end() == fattable.find(PSP_CTRL_SELECT)) + { + fattable[PSP_CTRL_SELECT] = make_pair(_("Select"), static_cast(NULL)); + fattable[PSP_CTRL_START] = make_pair(_("Start"), static_cast(NULL)); + fattable[PSP_CTRL_UP] = make_pair(_("Up"), static_cast(NULL)); + fattable[PSP_CTRL_RIGHT] = make_pair(_("Right"), static_cast(NULL)); + fattable[PSP_CTRL_DOWN] = make_pair(_("Down"), static_cast(NULL)); + fattable[PSP_CTRL_LEFT] = make_pair(_("Left"), static_cast(NULL)); + fattable[PSP_CTRL_LTRIGGER] = make_pair(_("Left trigger"), static_cast(NULL)); + fattable[PSP_CTRL_RTRIGGER] = make_pair(_("Right trigger"), static_cast(NULL)); + fattable[PSP_CTRL_TRIANGLE] = make_pair(_("Triangle"), static_cast(NULL)); + fattable[PSP_CTRL_CIRCLE] = make_pair(_("Circle"), static_cast(NULL)); + fattable[PSP_CTRL_CROSS] = make_pair(_("Cross"), static_cast(NULL)); + fattable[PSP_CTRL_SQUARE] = make_pair(_("Square"), static_cast(NULL)); + fattable[PSP_CTRL_HOLD] = make_pair(_("Hold"), static_cast(NULL)); + } + else + { + char* str = new char[11]; + sprintf(str, "%d", key); + fattable[key] = make_pair(str, static_cast(static_cast(NULL))); + } + res = fattable.find(key); } - if ((res = fattable.find(key)) != fattable.end()) - return *(res->second); - char* str = new char[11]; - sprintf(str, "%d", key); - KeyRep* k = new KeyRep(NULL, str); - fattable[key] = k; - return *k; + KeyRep& k = res->second; + switch(key) + { + case PSP_CTRL_SELECT : k.second = resources.RetrieveQuad("iconspsp.png", (float)2*32, 32, 64, 32, "PSP_CTRL_SELECT", RETRIEVE_NORMAL); break; + case PSP_CTRL_START : k.second = resources.RetrieveQuad("iconspsp.png", (float)0*32, 32, 64, 32, "PSP_CTRL_START", RETRIEVE_NORMAL); break; + case PSP_CTRL_UP : k.second = resources.RetrieveQuad("iconspsp.png", (float)0*32, 0, 32, 32, "PSP_CTRL_UP", RETRIEVE_NORMAL); break; + case PSP_CTRL_RIGHT : k.second = resources.RetrieveQuad("iconspsp.png", (float)3*32, 0, 32, 32, "PSP_CTRL_RIGHT", RETRIEVE_NORMAL); break; + case PSP_CTRL_DOWN : k.second = resources.RetrieveQuad("iconspsp.png", (float)1*32, 0, 32, 32, "PSP_CTRL_DOWN", RETRIEVE_NORMAL); break; + case PSP_CTRL_LEFT : k.second = resources.RetrieveQuad("iconspsp.png", (float)2*32, 0, 32, 32, "PSP_CTRL_LEFT", RETRIEVE_NORMAL); break; + case PSP_CTRL_LTRIGGER : k.second = resources.RetrieveQuad("iconspsp.png", (float)6*32, 32, 64, 32, "PSP_CTRL_LTRIGGER", RETRIEVE_NORMAL); break; + case PSP_CTRL_RTRIGGER : k.second = resources.RetrieveQuad("iconspsp.png", (float)8*32, 32, 64, 32, "PSP_CTRL_RTRIGGER", RETRIEVE_NORMAL); break; + case PSP_CTRL_TRIANGLE : k.second = resources.RetrieveQuad("iconspsp.png", (float)5*32, 0, 32, 32, "PSP_CTRL_TRIANGLE", RETRIEVE_NORMAL); break; + case PSP_CTRL_CIRCLE : k.second = resources.RetrieveQuad("iconspsp.png", (float)4*32, 0, 32, 32, "PSP_CTRL_CIRCLE", RETRIEVE_NORMAL); break; + case PSP_CTRL_CROSS : k.second = resources.RetrieveQuad("iconspsp.png", (float)7*32, 0, 32, 32, "PSP_CTRL_CROSS", RETRIEVE_NORMAL); break; + case PSP_CTRL_SQUARE : k.second = resources.RetrieveQuad("iconspsp.png", (float)6*32, 0, 32, 32, "PSP_CTRL_SQUARE", RETRIEVE_NORMAL); break; + case PSP_CTRL_HOLD : k.second = resources.RetrieveQuad("iconspsp.png", (float)4*32, 0, 32, 32, "PSP_CTRL_HOLD", RETRIEVE_NORMAL); break; + default: /* Unknown key : no icon */ ; + } + return k; } #endif #endif const KeyRep& translateKey(JButton key) { { - map::iterator res; + map::iterator res; if ((res = slimtable.find(key)) != slimtable.end()) - return *(res->second); + return res->second; } - slimtable[JGE_BTN_NONE] = new KeyRep(NULL, _("None")); - slimtable[JGE_BTN_QUIT] = new KeyRep(NULL, _("Quit")); - slimtable[JGE_BTN_MENU] = new KeyRep(NULL, _("Menu")); - slimtable[JGE_BTN_CTRL] = new KeyRep(NULL, _("Control")); - slimtable[JGE_BTN_POWER] = new KeyRep(NULL, _("Power")); - slimtable[JGE_BTN_SOUND] = new KeyRep(NULL, _("Sound")); - slimtable[JGE_BTN_RIGHT] = new KeyRep(NULL, _("Right")); - slimtable[JGE_BTN_LEFT] = new KeyRep(NULL, _("Left")); - slimtable[JGE_BTN_UP] = new KeyRep(NULL, _("Up")); - slimtable[JGE_BTN_DOWN] = new KeyRep(NULL, _("Down")); - slimtable[JGE_BTN_OK] = new KeyRep(NULL, _("Ok")); - slimtable[JGE_BTN_CANCEL] = new KeyRep(NULL, _("Cancel")); - slimtable[JGE_BTN_PRI] = new KeyRep(NULL, _("Primary")); - slimtable[JGE_BTN_SEC] = new KeyRep(NULL, _("Secondary")); - slimtable[JGE_BTN_PREV] = new KeyRep(NULL, _("Next phase/Previous item")); - slimtable[JGE_BTN_NEXT] = new KeyRep(NULL, _("Open hand/Next item")); + slimtable[JGE_BTN_NONE] = make_pair(_("None"), static_cast(NULL)); + slimtable[JGE_BTN_QUIT] = make_pair(_("Quit"), static_cast(NULL)); + slimtable[JGE_BTN_MENU] = make_pair(_("Menu"), static_cast(NULL)); + slimtable[JGE_BTN_CTRL] = make_pair(_("Control"), static_cast(NULL)); + slimtable[JGE_BTN_POWER] = make_pair(_("Power"), static_cast(NULL)); + slimtable[JGE_BTN_SOUND] = make_pair(_("Sound"), static_cast(NULL)); + slimtable[JGE_BTN_RIGHT] = make_pair(_("Right"), static_cast(NULL)); + slimtable[JGE_BTN_LEFT] = make_pair(_("Left"), static_cast(NULL)); + slimtable[JGE_BTN_UP] = make_pair(_("Up"), static_cast(NULL)); + slimtable[JGE_BTN_DOWN] = make_pair(_("Down"), static_cast(NULL)); + slimtable[JGE_BTN_OK] = make_pair(_("Ok"), static_cast(NULL)); + slimtable[JGE_BTN_CANCEL] = make_pair(_("Cancel"), static_cast(NULL)); + slimtable[JGE_BTN_PRI] = make_pair(_("Primary"), static_cast(NULL)); + slimtable[JGE_BTN_SEC] = make_pair(_("Secondary"), static_cast(NULL)); + slimtable[JGE_BTN_PREV] = make_pair(_("Next phase/Previous item"), static_cast(NULL)); + slimtable[JGE_BTN_NEXT] = make_pair(_("Open hand/Next item"), static_cast(NULL)); - return *slimtable[key]; + return slimtable[key]; } diff --git a/projects/mtg/src/WGui.cpp b/projects/mtg/src/WGui.cpp index 8daec08d4..06e7ebcd3 100644 --- a/projects/mtg/src/WGui.cpp +++ b/projects/mtg/src/WGui.cpp @@ -1775,7 +1775,20 @@ WGuiKeyBinder::WGuiKeyBinder(string name, GameStateOptions* parent) : WGuiList(n for (JGE::keybindings_it it = start; it != end; ++it) Add(NEW OptionKey(parent, it->first, it->second)); } -bool WGuiKeyBinder::isModal() { return modal; } +void WGuiKeyBinder::Update(float dt) { + OptionKey* o = dynamic_cast(items[0]); + if (!o) return; + if (LOCAL_KEY_NONE != o->from) { + items.insert(items.begin(), NEW OptionKey(parent, LOCAL_KEY_NONE, JGE_BTN_NONE)); + if (0 == currentItem) ++currentItem; + } + for (vector::iterator it = items.begin(); it != items.end(); ++it) (*it)->Update(dt); +} +bool WGuiKeyBinder::isModal() { + for (vector::iterator it = items.begin(); it != items.end(); ++it) + if ((*it)->isModal()) return true; + return modal; +} bool WGuiKeyBinder::CheckUserInput(JButton key) { if (!items[currentItem]->CheckUserInput(key)) @@ -1787,8 +1800,7 @@ bool WGuiKeyBinder::CheckUserInput(JButton key) void WGuiKeyBinder::setData(){ JGE* j = JGE::GetInstance(); j->ClearBindings(); - for (vector::iterator it = items.begin(); it != items.end(); ++it) - { + for (vector::iterator it = items.begin(); it != items.end(); ++it) { OptionKey* o = dynamic_cast(*it); if (o && LOCAL_KEY_NONE != o->from && JGE_BTN_NONE != o->to) j->BindKey(o->from, o->to);