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