1 /*
2 * Copyright (C) 2005-2018 Team Kodi
3 * This file is part of Kodi - https://kodi.tv
4 *
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 * See LICENSES/README.md for more information.
7 */
8
9 #include "Action.h"
10
11 #include "ActionIDs.h"
12 #include "ActionTranslator.h"
13 #include "input/Key.h"
14
CAction()15 CAction::CAction() : m_id(ACTION_NONE)
16 {
17 }
18
CAction(int actionID,float amount1,float amount2,const std::string & name,unsigned int holdTime)19 CAction::CAction(int actionID,
20 float amount1 /* = 1.0f */,
21 float amount2 /* = 0.0f */,
22 const std::string& name /* = "" */,
23 unsigned int holdTime /*= 0*/)
24 {
25 m_id = actionID;
26 m_amount[0] = amount1;
27 m_amount[1] = amount2;
28 m_name = name;
29 m_repeat = 0;
30 m_buttonCode = 0;
31 m_unicode = 0;
32 m_holdTime = holdTime;
33 }
34
CAction(int actionID,unsigned int state,float posX,float posY,float offsetX,float offsetY,float velocityX,float velocityY,const std::string & name)35 CAction::CAction(int actionID,
36 unsigned int state,
37 float posX,
38 float posY,
39 float offsetX,
40 float offsetY,
41 float velocityX,
42 float velocityY,
43 const std::string& name)
44 : m_name(name)
45 {
46 m_id = actionID;
47 m_amount[0] = posX;
48 m_amount[1] = posY;
49 m_amount[2] = offsetX;
50 m_amount[3] = offsetY;
51 m_amount[4] = velocityX;
52 m_amount[5] = velocityY;
53 m_repeat = 0;
54 m_buttonCode = 0;
55 m_unicode = 0;
56 m_holdTime = state;
57 }
58
CAction(int actionID,wchar_t unicode)59 CAction::CAction(int actionID, wchar_t unicode)
60 {
61 m_id = actionID;
62 m_repeat = 0;
63 m_buttonCode = 0;
64 m_unicode = unicode;
65 m_holdTime = 0;
66 }
67
CAction(int actionID,const std::string & name,const CKey & key)68 CAction::CAction(int actionID, const std::string& name, const CKey& key) : m_name(name)
69 {
70 m_id = actionID;
71 m_amount[0] = 1; // digital button (could change this for repeat acceleration)
72 m_repeat = key.GetRepeat();
73 m_buttonCode = key.GetButtonCode();
74 m_unicode = key.GetUnicode();
75 m_holdTime = key.GetHeld();
76 // get the action amounts of the analog buttons
77 if (key.GetButtonCode() == KEY_BUTTON_LEFT_ANALOG_TRIGGER)
78 m_amount[0] = (float)key.GetLeftTrigger() / 255.0f;
79 else if (key.GetButtonCode() == KEY_BUTTON_RIGHT_ANALOG_TRIGGER)
80 m_amount[0] = (float)key.GetRightTrigger() / 255.0f;
81 else if (key.GetButtonCode() == KEY_BUTTON_LEFT_THUMB_STICK)
82 {
83 m_amount[0] = key.GetLeftThumbX();
84 m_amount[1] = key.GetLeftThumbY();
85 }
86 else if (key.GetButtonCode() == KEY_BUTTON_RIGHT_THUMB_STICK)
87 {
88 m_amount[0] = key.GetRightThumbX();
89 m_amount[1] = key.GetRightThumbY();
90 }
91 else if (key.GetButtonCode() == KEY_BUTTON_LEFT_THUMB_STICK_UP)
92 m_amount[0] = key.GetLeftThumbY();
93 else if (key.GetButtonCode() == KEY_BUTTON_LEFT_THUMB_STICK_DOWN)
94 m_amount[0] = -key.GetLeftThumbY();
95 else if (key.GetButtonCode() == KEY_BUTTON_LEFT_THUMB_STICK_LEFT)
96 m_amount[0] = -key.GetLeftThumbX();
97 else if (key.GetButtonCode() == KEY_BUTTON_LEFT_THUMB_STICK_RIGHT)
98 m_amount[0] = key.GetLeftThumbX();
99 else if (key.GetButtonCode() == KEY_BUTTON_RIGHT_THUMB_STICK_UP)
100 m_amount[0] = key.GetRightThumbY();
101 else if (key.GetButtonCode() == KEY_BUTTON_RIGHT_THUMB_STICK_DOWN)
102 m_amount[0] = -key.GetRightThumbY();
103 else if (key.GetButtonCode() == KEY_BUTTON_RIGHT_THUMB_STICK_LEFT)
104 m_amount[0] = -key.GetRightThumbX();
105 else if (key.GetButtonCode() == KEY_BUTTON_RIGHT_THUMB_STICK_RIGHT)
106 m_amount[0] = key.GetRightThumbX();
107 }
108
CAction(int actionID,const std::string & name)109 CAction::CAction(int actionID, const std::string& name) : m_name(name)
110 {
111 m_id = actionID;
112 m_repeat = 0;
113 m_buttonCode = 0;
114 m_unicode = 0;
115 m_holdTime = 0;
116 }
117
operator =(const CAction & rhs)118 CAction& CAction::operator=(const CAction& rhs)
119 {
120 if (this != &rhs)
121 {
122 m_id = rhs.m_id;
123 for (unsigned int i = 0; i < max_amounts; i++)
124 m_amount[i] = rhs.m_amount[i];
125 m_name = rhs.m_name;
126 m_repeat = rhs.m_repeat;
127 m_buttonCode = rhs.m_buttonCode;
128 m_unicode = rhs.m_unicode;
129 m_holdTime = rhs.m_holdTime;
130 m_text = rhs.m_text;
131 }
132 return *this;
133 }
134
ClearAmount()135 void CAction::ClearAmount()
136 {
137 for (float& amount : m_amount)
138 amount = 0;
139 }
140
IsMouse() const141 bool CAction::IsMouse() const
142 {
143 return (m_id >= ACTION_MOUSE_START && m_id <= ACTION_MOUSE_END);
144 }
145
IsGesture() const146 bool CAction::IsGesture() const
147 {
148 return (m_id >= ACTION_GESTURE_NOTIFY && m_id <= ACTION_GESTURE_END);
149 }
150
IsAnalog() const151 bool CAction::IsAnalog() const
152 {
153 return CActionTranslator::IsAnalog(m_id);
154 }
155