* Grab the keyboard and display the grabbed key.
* Include a tentative interface for the initial message. It's ugly.
This commit is contained in:
jean.chalard
2010-02-18 08:03:22 +00:00
parent 3c1377fe47
commit c87a98eb14
6 changed files with 82 additions and 37 deletions

View File

@@ -1,6 +1,7 @@
#ifndef _GAME_STATE_OPTIONS_H_ #ifndef _GAME_STATE_OPTIONS_H_
#define _GAME_STATE_OPTIONS_H_ #define _GAME_STATE_OPTIONS_H_
#include <JGE.h>
#include <JGui.h> #include <JGui.h>
#include "../include/GameState.h" #include "../include/GameState.h"
@@ -12,17 +13,21 @@ class WGuiTabMenu;
class SimpleMenu; class SimpleMenu;
class SimplePad; class SimplePad;
class GameStateOptions: public GameState, public JGuiListener struct KeybGrabber {
{ virtual void KeyPressed(LocalKeySym) = 0;
};
class GameStateOptions: public GameState, public JGuiListener {
private: private:
float timer; float timer;
bool mReload; bool mReload;
KeybGrabber* grabber;
public: public:
SimpleMenu * optionsMenu; SimpleMenu * optionsMenu;
WGuiTabMenu * optionsTabs; WGuiTabMenu * optionsTabs;
int mState; int mState;
GameStateOptions(GameApp* parent); GameStateOptions(GameApp* parent);
virtual ~GameStateOptions(); virtual ~GameStateOptions();
@@ -30,6 +35,8 @@ private:
virtual void End(); virtual void End();
virtual void Update(float dt); virtual void Update(float dt);
virtual void Render(); virtual void Render();
virtual void GrabKeyboard(KeybGrabber*);
virtual void UngrabKeyboard(const KeybGrabber*);
void ButtonPressed(int controllerId, int ControlId); void ButtonPressed(int controllerId, int ControlId);
string newProfile; string newProfile;

View File

@@ -5,6 +5,7 @@
#include <vector> #include <vector>
#include <string> #include <string>
#include "../include/GameApp.h" #include "../include/GameApp.h"
#include "../include/GameStateOptions.h"
#include "../include/GameOptions.h" #include "../include/GameOptions.h"
#include "../include/WFilter.h" #include "../include/WFilter.h"
#include "../include/WDataSrc.h" #include "../include/WDataSrc.h"
@@ -132,11 +133,18 @@ private:
size_t initialValue; size_t initialValue;
}; };
class OptionKey : public WGuiItem { class OptionKey : public WGuiItem, public KeybGrabber {
public: public:
OptionKey(LocalKeySym, JButton); OptionKey(GameStateOptions* g, LocalKeySym, JButton);
LocalKeySym from; LocalKeySym from;
JButton to; JButton to;
virtual void Render(); virtual void Render();
virtual void Overlay();
virtual bool CheckUserInput(JButton key);
virtual void KeyPressed(LocalKeySym key);
virtual bool isModal();
protected:
bool grabbed;
GameStateOptions* g;
}; };
#endif #endif

View File

@@ -2,6 +2,7 @@
#define _WGUI_H_ #define _WGUI_H_
class hgeDistortionMesh; class hgeDistortionMesh;
class GameStateOptions;
class WGuiColor{ class WGuiColor{
public: public:
@@ -486,10 +487,11 @@ struct WLFiltersSort{
class WGuiKeyBinder : public WGuiList { class WGuiKeyBinder : public WGuiList {
public: public:
WGuiKeyBinder(string name); WGuiKeyBinder(string name, GameStateOptions* parent);
virtual bool isModal(); virtual bool isModal();
virtual bool CheckUserInput(JButton); virtual bool CheckUserInput(JButton);
protected: protected:
GameStateOptions* parent;
bool modal; bool modal;
}; };

View File

@@ -7,16 +7,8 @@
#include "../include/GameOptions.h" #include "../include/GameOptions.h"
#include "../include/Translate.h" #include "../include/Translate.h"
GameStateOptions::GameStateOptions(GameApp* parent): GameState(parent) { GameStateOptions::GameStateOptions(GameApp* parent): GameState(parent), mReload(false), grabber(NULL), optionsMenu(NULL), optionsTabs(NULL) {}
optionsTabs = NULL; GameStateOptions::~GameStateOptions() {}
optionsMenu = NULL;
mReload = false;
}
GameStateOptions::~GameStateOptions() {
}
void GameStateOptions::Start() void GameStateOptions::Start()
{ {
@@ -80,7 +72,7 @@ void GameStateOptions::Start()
optionsList->Add(oGra); optionsList->Add(oGra);
optionsTabs->Add(optionsList); optionsTabs->Add(optionsList);
optionsList = NEW WGuiKeyBinder("Key Bindings"); optionsList = NEW WGuiKeyBinder("Key Bindings", this);
optionsTabs->Add(optionsList); optionsTabs->Add(optionsList);
optionsList = NEW WGuiList("Credits"); optionsList = NEW WGuiList("Credits");
@@ -123,15 +115,21 @@ void GameStateOptions::Update(float dt)
} }
else switch(mState){ else switch(mState){
default: default:
case SHOW_OPTIONS: case SHOW_OPTIONS: {
JButton key; JGE* j = JGE::GetInstance();
JButton key;
while ((key = JGE::GetInstance()->ReadButton())){ if (grabber) {
if(!optionsTabs->CheckUserInput(key) && key == JGE_BTN_MENU) LocalKeySym sym;
mState = SHOW_OPTIONS_MENU; if (LOCAL_NO_KEY != (sym = j->ReadLocalKey()))
grabber->KeyPressed(sym);
}
else while ((key = JGE::GetInstance()->ReadButton())){
if(!optionsTabs->CheckUserInput(key) && key == JGE_BTN_MENU)
mState = SHOW_OPTIONS_MENU;
}
optionsTabs->Update(dt);
break;
} }
optionsTabs->Update(dt);
break;
case SHOW_OPTIONS_MENU: case SHOW_OPTIONS_MENU:
optionsMenu->Update(dt); optionsMenu->Update(dt);
break; break;
@@ -240,3 +238,10 @@ void GameStateOptions::ButtonPressed(int controllerId, int controlId)
else else
optionsTabs->ButtonPressed(controllerId, controlId); optionsTabs->ButtonPressed(controllerId, controlId);
}; };
void GameStateOptions::GrabKeyboard(KeybGrabber* g) {
grabber = g;
}
void GameStateOptions::UngrabKeyboard(const KeybGrabber* g) {
if (g == grabber) grabber = NULL;
}

View File

@@ -430,9 +430,9 @@ void OptionTheme::confirmChange(bool confirmed){
} }
} }
OptionKey::OptionKey(LocalKeySym from, JButton to) : WGuiItem(""), from(from), to(to){} OptionKey::OptionKey(GameStateOptions* g, LocalKeySym from, JButton to) : WGuiItem(""), from(from), to(to), grabbed(false), g(g) {}
void OptionKey::Render(){ void OptionKey::Render() {
JLBFont * mFont = resources.GetJLBFont(Constants::OPTION_FONT); JLBFont * mFont = resources.GetJLBFont(Constants::OPTION_FONT);
mFont->SetColor(getColor(WGuiColor::TEXT)); mFont->SetColor(getColor(WGuiColor::TEXT));
JRenderer * renderer = JRenderer::GetInstance(); JRenderer * renderer = JRenderer::GetInstance();
@@ -448,3 +448,31 @@ void OptionKey::Render(){
else else
mFont->DrawString(rep2.text, width - 4, y + 2, JGETEXT_RIGHT); mFont->DrawString(rep2.text, width - 4, y + 2, JGETEXT_RIGHT);
} }
bool OptionKey::CheckUserInput(JButton key) {
if (JGE_BTN_OK == key)
{
grabbed = true;
g->GrabKeyboard(this);
return true;
}
return false;
}
void OptionKey::KeyPressed(LocalKeySym key) {
cout << "KEY : " << key << endl;
g->UngrabKeyboard(this);
grabbed = false;
}
bool OptionKey::isModal() { return grabbed; }
void OptionKey::Overlay()
{
JRenderer * renderer = JRenderer::GetInstance();
JLBFont * mFont = resources.GetJLBFont(Constants::OPTION_FONT);
mFont->SetColor(ARGB(255, 0, 0, 0));
if (grabbed)
{
// static const int x = 30, y = 45;
renderer->FillRoundRect(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, 2, ARGB(255, 255, 255, 255));
string msg = _("Press a key to associate.");
mFont->DrawString(msg, (SCREEN_WIDTH - mFont->GetStringWidth(msg.c_str())) / 2, y + 2);
}
}

View File

@@ -1742,26 +1742,21 @@ string WGuiFilterItem::getCode(){
return mCode; return mCode;
} }
WGuiKeyBinder::WGuiKeyBinder(string name) : WGuiList(name), modal(false) WGuiKeyBinder::WGuiKeyBinder(string name, GameStateOptions* parent) : WGuiList(name), parent(parent), modal(false) {
{
JGE* j = JGE::GetInstance(); JGE* j = JGE::GetInstance();
JGE::keybindings_it start = j->KeyBindings_begin(), end = j->KeyBindings_end(); JGE::keybindings_it start = j->KeyBindings_begin(), end = j->KeyBindings_end();
u32 y = 40; u32 y = 40;
for (JGE::keybindings_it it = start; it != end; ++it) for (JGE::keybindings_it it = start; it != end; ++it)
Add(NEW OptionKey(it->first, it->second)); Add(NEW OptionKey(parent, it->first, it->second));
} }
bool WGuiKeyBinder::isModal() { return modal; } bool WGuiKeyBinder::isModal() { return modal; }
bool WGuiKeyBinder::CheckUserInput(JButton key) bool WGuiKeyBinder::CheckUserInput(JButton key)
{ {
switch (key) switch (key) {
{ case JGE_BTN_OK: items[currentItem]->CheckUserInput(key); break;
case JGE_BTN_UP:
prevItem(); break;
case JGE_BTN_DOWN:
nextItem(); break;
default: default:
; return WGuiList::CheckUserInput(key);
} }
return false; return false;
} }