efa14d
#pragma once
efa14d
efa14d
#ifndef INPUTSTATE_INCLUDED
efa14d
#define INPUTSTATE_INCLUDED
efa14d
efa14d
// TnzTools includes
efa14d
#include <tools tooltimer.h=""></tools>
efa14d
#include <tools keyhistory.h=""></tools>
efa14d
efa14d
// TnzCore includes
efa14d
#include <tcommon.h></tcommon.h>
efa14d
#include <tsmartpointer.h></tsmartpointer.h>
49945e
#include <tgeometry.h></tgeometry.h>
efa14d
efa14d
// Qt includes
efa14d
#include <qt></qt>
efa14d
efa14d
// std includes
efa14d
#include <map></map>
49945e
#include <vector></vector>
d8eddc
#include <iostream></iostream>
efa14d
efa14d
efa14d
#undef DVAPI
efa14d
#undef DVVAR
efa14d
#ifdef TNZTOOLS_EXPORTS
efa14d
#define DVAPI DV_EXPORT_API
efa14d
#define DVVAR DV_EXPORT_VAR
efa14d
#else
efa14d
#define DVAPI DV_IMPORT_API
efa14d
#define DVVAR DV_IMPORT_VAR
efa14d
#endif
efa14d
efa14d
49945e
//====================================================
49945e
49945e
//  Forward declarations
49945e
49945e
typedef std::vector<tpointd> THoverList;</tpointd>
49945e
efa14d
//===================================================================
efa14d
efa14d
efa14d
//*****************************************************************************************
49945e
//    TKey definition
49945e
//*****************************************************************************************
49945e
6be163
class DVAPI TKey {
49945e
public:
49945e
  Qt::Key key;
49945e
  bool generic;
49945e
  bool numPad;
49945e
49945e
  static const TKey shift;
49945e
  static const TKey control;
49945e
  static const TKey alt;
49945e
  static const TKey meta;
49945e
49945e
  inline explicit TKey(Qt::Key key = Qt::Key(), bool generic = true, bool numPad = false):
49945e
    key(key),
49945e
    generic(generic),
49945e
    numPad(numPad)
49945e
    { }
49945e
49945e
  inline bool operator== (const TKey &other) const {
49945e
    if (generic || other.generic)
49945e
      return is(other.key);
49945e
    return key == other.key && numPad == other.numPad;
49945e
  }
49945e
49945e
  inline bool is(Qt::Key key) const
49945e
    { return mapKey(this->key) == mapKey(key); }
49945e
49945e
  inline bool isModifier() const
49945e
    { return isModifier(key); }
49945e
  inline bool isNumber() const
49945e
    { return isNumber(key); }
49945e
49945e
  static Qt::Key mapKey(Qt::Key key);
49945e
  static bool isNumber(Qt::Key key);
49945e
  static bool isModifier(Qt::Key key);
49945e
};
49945e
49945e
49945e
//*****************************************************************************************
16421e
//    export template implementations for win32
16421e
//*****************************************************************************************
16421e
16421e
#ifdef _WIN32
16421e
template class DVAPI TKeyStateT<tkey>;</tkey>
16421e
template class DVAPI TSmartPointerT< TKeyStateT<tkey> >;</tkey>
16421e
template class DVAPI TKeyHistoryT<tkey>;</tkey>
16421e
template class DVAPI TKeyHistoryT<tkey>::Holder;</tkey>
16421e
16421e
template class DVAPI TKeyStateT<qt::mousebutton>;</qt::mousebutton>
16421e
template class DVAPI TSmartPointerT< TKeyStateT<qt::mousebutton> >;</qt::mousebutton>
16421e
template class DVAPI TKeyHistoryT<qt::mousebutton>;</qt::mousebutton>
16421e
template class DVAPI TKeyHistoryT<qt::mousebutton>::Holder;</qt::mousebutton>
16421e
#endif
16421e
16421e
16421e
//*****************************************************************************************
efa14d
//    TInputState definition
efa14d
//*****************************************************************************************
efa14d
6be163
class DVAPI TInputState {
efa14d
public:
efa14d
  typedef qint64 DeviceId;
efa14d
  typedef long long TouchId;
efa14d
49945e
  typedef TKey Key;
efa14d
  typedef TKeyHistoryT<key> KeyHistory;</key>
efa14d
  typedef KeyHistory::State KeyState;
efa14d
efa14d
  typedef Qt::MouseButton Button;
efa14d
  typedef TKeyHistoryT<button> ButtonHistory;</button>
efa14d
  typedef ButtonHistory::State ButtonState;
efa14d
  typedef std::map<deviceid, buttonhistory::pointer=""> ButtonHistoryMap;</deviceid,>
efa14d
efa14d
private:
49945e
  TTimerTicks m_ticks;
49945e
  KeyHistory::Pointer m_keyHistory;
49945e
  mutable ButtonHistoryMap m_buttonHistories;
efa14d
efa14d
public:
efa14d
  TInputState();
efa14d
  ~TInputState();
efa14d
49945e
  TTimerTicks ticks() const
49945e
    { return m_ticks; }
49945e
  void touch(TTimerTicks ticks);
49945e
49945e
  inline const KeyHistory::Pointer& keyHistory() const
49945e
    { return m_keyHistory; }
efa14d
  inline KeyState::Pointer keyState() const
efa14d
    { return keyHistory()->current(); }
efa14d
efa14d
  inline void keyEvent(bool press, Key key, TTimerTicks ticks)
49945e
    { touch(ticks); keyHistory()->change(press, key, m_ticks); }
efa14d
  inline void keyPress(Key key, TTimerTicks ticks)
efa14d
    { keyEvent(true, key, ticks); }
efa14d
  inline void keyRelease(Key key, TTimerTicks ticks)
efa14d
    { keyEvent(false, key, ticks); }
efa14d
efa14d
  inline KeyState::Pointer keyFind(Key key) const
efa14d
    { return keyState()->find(key); }
efa14d
  inline bool isKeyPressed(Key key) const
efa14d
    { return keyFind(key); }
efa14d
  inline double howLongKeyPressed(Key key, TTimerTicks ticks, double timeOffset = 0.0)
efa14d
    { return KeyState::Holder::howLongPressed(keyFind(key), ticks, timeOffset); }
efa14d
  inline double howLongKeyPressed(Key key)
49945e
    { return howLongKeyPressed(key, m_ticks); }
efa14d
49945e
  inline KeyState::Holder keyStateHolder(TTimerTicks ticks, double timeOffset = 0.0) const
49945e
    { return KeyState::Holder(keyState(), ticks, timeOffset); }
49945e
  inline KeyState::Holder keyStateHolder() const
49945e
    { return keyStateHolder(m_ticks); }
49945e
  inline KeyHistory::Holder keyHistoryHolder(TTimerTicks ticks, double timeOffset = 0.0) const
49945e
    { return KeyHistory::Holder(keyHistory(), ticks, timeOffset); }
49945e
  inline KeyHistory::Holder keyHistoryHolder() const
49945e
    { return keyHistoryHolder(m_ticks); }
49945e
49945e
  ButtonHistory::Pointer buttonHistory(DeviceId deviceId) const;
efa14d
efa14d
  inline const ButtonHistoryMap buttonHistories() const
49945e
    { return m_buttonHistories; }
49945e
  inline ButtonState::Pointer buttonState(DeviceId deviceId) const
49945e
    { return buttonHistory(deviceId)->current(); }
49945e
49945e
  inline void buttonEvent(bool press, DeviceId deviceId, Button button, TTimerTicks ticks)
49945e
    { touch(ticks); buttonHistory(deviceId)->change(press, button, m_ticks); }
49945e
  inline void buttonPress(DeviceId deviceId, Button button, TTimerTicks ticks)
49945e
    { buttonEvent(true, deviceId, button, ticks); }
49945e
  inline void buttonRelease(DeviceId deviceId, Button button, TTimerTicks ticks)
49945e
    { buttonEvent(false, deviceId, button, ticks); }
efa14d
  inline void buttonEvent(bool press, Button button, TTimerTicks ticks)
efa14d
    { buttonEvent(press, 0, button, ticks); }
efa14d
  inline void buttonPress(Button button, TTimerTicks ticks)
efa14d
    { buttonEvent(true, button, ticks); }
efa14d
  inline void buttonRelease(Button button, TTimerTicks ticks)
efa14d
    { buttonEvent(false, button, ticks); }
efa14d
49945e
  inline ButtonState::Pointer buttonFind(DeviceId deviceId, Button button)
49945e
    { return buttonState(deviceId)->find(button); }
49945e
  inline bool isButtonPressed(DeviceId deviceId, Button button)
49945e
    { return buttonFind(deviceId, button); }
49945e
  inline double howLongButtonPressed(DeviceId deviceId, Button button, TTimerTicks ticks, double timeOffset = 0.0)
49945e
    { return ButtonState::Holder::howLongPressed(buttonFind(deviceId, button), ticks, timeOffset); }
49945e
  inline double howLongButtonPressed(DeviceId deviceId, Button button)
49945e
    { return howLongButtonPressed(deviceId, button, m_ticks); }
efa14d
efa14d
  inline ButtonState::Pointer buttonFindDefault(Button button)
efa14d
    { return buttonFind(DeviceId(), button); }
efa14d
  inline bool isButtonPressedDefault(Button button)
efa14d
    { return isButtonPressed(DeviceId(), button); }
efa14d
  inline double howLongButtonPressedDefault(Button button, TTimerTicks ticks, double timeOffset = 0.0)
efa14d
    { return howLongButtonPressed(DeviceId(), button, ticks, timeOffset); }
efa14d
  inline double howLongButtonPressedDefault(Button button)
49945e
    { return howLongButtonPressedDefault(button, m_ticks); }
efa14d
efa14d
  ButtonState::Pointer buttonFindAny(Button button, DeviceId &outDevice);
efa14d
efa14d
  inline ButtonState::Pointer buttonFindAny(Button button)
49945e
    { DeviceId deviceId; return buttonFindAny(button, deviceId); }
efa14d
  inline bool isButtonPressedAny(Button button)
efa14d
    { return buttonFindAny(button); }
efa14d
  inline double howLongButtonPressedAny(Button button, TTimerTicks ticks, double timeOffset = 0.0)
efa14d
    { return ButtonState::Holder::howLongPressed(buttonFindAny(button), ticks, timeOffset); }
efa14d
  inline double howLongButtonPressedAny(Button button)
49945e
    { return howLongButtonPressedAny(button, m_ticks); }
49945e
49945e
  inline ButtonState::Holder buttonStateHolder(DeviceId deviceId, TTimerTicks ticks, double timeOffset = 0.0)
49945e
    { return ButtonState::Holder(buttonState(deviceId), ticks, timeOffset); }
49945e
  inline ButtonState::Holder buttonStateHolder(DeviceId deviceId)
49945e
    { return buttonStateHolder(deviceId, m_ticks); }
49945e
  inline ButtonHistory::Holder buttonHistoryHolder(DeviceId deviceId, long ticks, double timeOffset = 0.0)
49945e
    { return ButtonHistory::Holder(buttonHistory(deviceId), ticks, timeOffset); }
49945e
  inline ButtonHistory::Holder buttonHistoryHolder(DeviceId deviceId)
49945e
    { return buttonHistoryHolder(deviceId, m_ticks); }
d8eddc
    
d8eddc
  void print(std::ostream &stream, const std::string &tab = std::string()) const;
d8eddc
  inline void print(const std::string &tab = std::string()) const
d8eddc
    { print(std::cout, tab); }
efa14d
};
efa14d
efa14d
efa14d
#endif