saving to file doesnt still work although i use options db.
Code: Select all
// -*- C++ -*-
//by strooka
#ifndef _HotKeyManager_h_
#define _HotKeyManager_h_
#include <../GG/GG/Base.h>
#include <../GG/GG/GUI.h>
#include <../GG/GG/SignalsAndSlots.h>
#include "CUIWnd.h"
#include "CUIControls.h"
#include "../util/OptionsDB.h"
#include <map>
typedef boost::signal< bool() > SignalType;
typedef boost::shared_ptr<SignalType> SignalPointerType;
/////////////////////////////////////////////
// Free Functions
/////////////////////////////////////////////
namespace HKM {
std::string KeyString(GG::Key key, GG::Flags<GG::ModKey> mod_keys ); //!< computes Key input
GG::Key KeyFromString(const std::string& str); //!< computes string input for a Key
std::pair< GG::Key, GG::Flags<GG::ModKey> > UserString(const std::string& str); //!< computes user input
class HotKey
{
public:
HotKey( const std::string& str, const std::string& option_name = "NO_NAME_SET" );
HotKey( GG::Key key, const std::string& option_name = "NO_NAME_SET" );
HotKey( GG::Key key = GG::GGK_UNKNOWN, GG::Flags<GG::ModKey> mod_keys = GG::Flags<GG::ModKey>(), const std::string& option_name = "NO_NAME_SET" );
friend std::ostream& operator<<(std::ostream& os, const HotKey& hotkey);
friend std::istream& operator>>(std::istream& is, HotKey& hotkey);
void SetKey( std::pair< GG::Key, GG::Flags<GG::ModKey> > Pair );
bool GetCtrl() const { return m_mod_keys & (GG::MOD_KEY_CTRL); }//!< Is CTRL key pressed to activate hotkey?
bool GetAlt() const { return m_mod_keys & (GG::MOD_KEY_ALT); } //!< ALT key?
bool GetShift() const { return m_mod_keys & (GG::MOD_KEY_LSHIFT | GG::MOD_KEY_RSHIFT); } //!< SHIFT key?
bool GetMeta() const { return m_mod_keys & (GG::MOD_KEY_META); } //!< META key?
GG::Key GetKey() const { return m_key; }
GG::Flags<GG::ModKey> GetModKeys() const { return m_mod_keys; }
static bool IsModKey(GG::Key key) {
if( key == (GG::GGK_RSHIFT || GG::GGK_LSHIFT || GG::GGK_RCTRL || GG::GGK_LCTRL || GG::GGK_LALT || GG::GGK_RALT || GG::GGK_LMETA || GG::GGK_RMETA) )
return true;
else
return false;
}
boost::signals::connection& GetConnection();
SignalPointerType GetWindowSignal() { return m_p_window_signal; }
void UpdateConnection(GG::Key last_key = GG::GGK_UNKNOWN, GG::Flags<GG::ModKey> last_mod_keys = GG::Flags<GG::ModKey>());
void SetOptionName(std::string str) { m_option_name = str; }
std::string GetOptionName() { return m_option_name; }
private:
GG::Key m_key; //!< GG code for key that was pressed
GG::Flags<GG::ModKey> m_mod_keys;
std::string m_option_name;
SignalPointerType m_p_window_signal;
boost::signals::connection m_connection;
};
class HKEdit : public CUIEdit
{
public:
/** \name Structors */ //@{
HKEdit(GG::X x, GG::Y y, GG::X w, const std::string& str, const std::string& option_name, const boost::shared_ptr<GG::Font>& font = boost::shared_ptr<GG::Font>(),
GG::Clr border_color = ClientUI::CtrlBorderColor(), GG::Clr text_color = ClientUI::TextColor(),
GG::Clr interior = ClientUI::CtrlColor(), GG::Flags<GG::WndFlag> flags = GG::INTERACTIVE); ///< basic ctor
//@}
/** \name Mutators */ //@{
virtual void LButtonDown(const GG::Pt& pt, GG::Flags<GG::ModKey> mod_keys);
//@}
private:
const std::string m_option_name;
};
class KeyInputDialog : public CUIWnd
{
public:
KeyInputDialog();
virtual void KeyPress(GG::Key key, boost::uint32_t key_code_point, GG::Flags<GG::ModKey> mod_keys);
const std::pair< GG::Key, GG::Flags<GG::ModKey> > Result() const { return m_result; }
private:
std::pair< GG::Key, GG::Flags<GG::ModKey> > m_result;
static const GG::X WND_WIDTH;
static const int CONTROL_MARGIN;
};
//can be used to implement a state machine
class HotKeyManager
{
public:
HotKeyManager();
static HotKeyManager* s_HotKeyManager;
private:
friend HotKeyManager& GetHotKeyManager();
};
}
/** returns the single instance of the class */
HKM::HotKeyManager& GetHotKeyManager();
namespace HKM {
template <class T>
struct Connect {
Connect(const std::string option_name, bool (T::*slot)(), T* pWnd) {
//update/initialize hotkey contents from db
HotKey hotkey = GetOptionsDB().Get<HotKey>(option_name);
//set signal this connection won't be altered
GG::Connect( *hotkey.GetWindowSignal(), slot, pWnd );
//set connection
hotkey.UpdateConnection();
GetOptionsDB().Set<HotKey>(option_name, hotkey);
}
};
}
#endif // _HotKeyManager_h_
you add commands through:
Code: Select all
db.Add("UI.HotKey.MapWnd.ReturnToMap", "Return to map", HKM::HotKey(GG::GGK_ESCAPE,"UI.HotKey.MapWnd.ReturnToMap"), Validator<HKM::HotKey>());
Code: Select all
HKM::Connect<MapWnd>("UI.HotKey.MapWnd.ReturnToMap", &MapWnd::ReturnToMap, this);
in options wnd, you add an key option:
Code: Select all
BeginPage(UserString("Key Bindings"));
BeginSection(UserString("Map Window"));
KeyOption("UI.HotKey.MapWnd.ReturnToMap", "Return To Map");