1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
|
#include "../../../include/odfaeg/Core/action.h"
#include "../../../include/odfaeg/Core/actionMap.h"
using namespace std;
namespace odfaeg {
Action::Action (EVENT_TYPE type) : type(type) {
leaf = true;
pressed = false;
switch (type) {
case CLOSED :
startEvent.type = sf::Event::Closed;
break;
case RESIZED :
startEvent.type = sf::Event::Resized;
break;
case LOST_FOCUS :
startEvent.type = sf::Event::LostFocus;
break;
case GAIGNED_FOCUS :
startEvent.type = sf::Event::GainedFocus;
break;
case TEXT_ENTERED :
startEvent.type = sf::Event::TextEntered;
break;
case MOUSE_WHEEL_MOVED :
startEvent.type = sf::Event::MouseWheelMoved;
break;
case MOUSE_MOVED :
startEvent.type = sf::Event::MouseMoved;
break;
case MOUSE_ENTERED :
startEvent.type = sf::Event::MouseEntered;
break;
case MOUSE_LEFT :
startEvent.type = sf::Event::MouseEntered;
break;
case JOYSTICK_MOVED :
startEvent.type = sf::Event::MouseLeft;
break;
case JOYSTICK_CONNECTED :
startEvent.type = sf::Event::JoystickConnected;
break;
case JOYSTICK_DISCONNECTED :
startEvent.type = sf::Event::JoystickDisconnected;
break;
}
is_not = false;
}
Action::Action (EVENT_TYPE type, sf::Keyboard::Key key) : type(type) {
leaf = true;
pressed = false;
switch (type) {
case KEY_PRESSED_ONCE :
startEvent.type = sf::Event::KeyPressed;
startEvent.key.code = key;
break;
case KEY_HELD_DOWN :
startEvent.type = sf::Event::KeyPressed;
startEvent.key.code = key;
break;
case KEY_RELEASED :
startEvent.type = sf::Event::KeyReleased;
startEvent.key.code = key;
break;
}
is_not = false;
}
Action::Action (EVENT_TYPE type, sf::Mouse::Button button) : type(type) {
leaf = true;
pressed = false;
switch (type) {
case MOUSE_BUTTON_PRESSED_ONCE :
startEvent.type = sf::Event::MouseButtonPressed;
startEvent.mouseButton.button = button;
break;
case MOUSE_BUTTON_HELD_DOWN :
startEvent.type = sf::Event::MouseButtonPressed;
startEvent.mouseButton.button = button;
break;
case MOUSE_BUTTON_RELEASED :
startEvent.type = sf::Event::MouseButtonReleased;
startEvent.mouseButton.button = button;
break;
}
is_not = false;
}
Action::Action(const Action& other) {
leaf = other.leaf;
pressed = other.pressed;
startEvent = other.startEvent;
is_not = other.is_not;
leftChild = std::make_unique<Action>(*other.leftChild);
rightChild = std::make_unique<Action>(*other.rightChild);
comparator = other.comparator;
pressed = other.pressed;
type = other.type;
}
/*Action (EVENT_TYPE type, sf::Joystick::Button button) : type(type) {
cptype = NONE;
switch (type) {
case JOYSTICK_BUTTON_PRESSED_ONCE :
startEvent.type = sf::Event::MouseButtonPressed;
startEvent.joystickButton.button = button;
break;
case JOYSTICK_BUTTON_HELD_DOWN :
startEvent.joystickButton.button = button;
break;
case JOYSTICK_BUTTON_RELEASED :
startEvent.type = sf::Event::MouseButtonReleased;
startEvent.joystickButton.button = button;
break;
}
}*/
bool Action::andComparator (std::unique_ptr<Action>& a1, std::unique_ptr<Action>& a2) {
return a1->isTriggered() && a2->isTriggered();
}
bool Action::orComparator (std::unique_ptr<Action>& a1, std::unique_ptr<Action>& a2) {
return a1->isTriggered() || a2->isTriggered();
}
bool Action::xorComparator (std::unique_ptr<Action>& a1, std::unique_ptr<Action>& a2) {
return a1->isTriggered() | a2->isTriggered();
}
void Action::operator! () {
if (!is_not)
is_not = true;
else
is_not = false;
}
std::unique_ptr<Action> Action::operator| (Action &other) {
std::unique_ptr<Action> result = std::make_unique<Action>(COMBINED_WITH_XOR, *this, other);
return result;
}
std::unique_ptr<Action> Action::operator|| (Action &other) {
std::unique_ptr<Action> result = std::make_unique<Action>(COMBINED_WITH_XOR, *this, other);
return result;
}
std::unique_ptr<Action> Action::operator&& (Action &other) {
std::unique_ptr<Action> result = std::make_unique<Action>(COMBINED_WITH_XOR, *this, other);
return result;
}
bool Action::isTriggered () {
vector<sf::Event> events = Command::getEvents();
if (leaf) {
if (type == KEY_HELD_DOWN && !is_not) {
return sf::Keyboard::isKeyPressed(startEvent.key.code);
}
if (type == KEY_HELD_DOWN && is_not) {
return !sf::Keyboard::isKeyPressed(startEvent.key.code);
}
if (type == MOUSE_BUTTON_HELD_DOWN && !is_not) {
return sf::Mouse::isButtonPressed(startEvent.mouseButton.button);
}
if (type == MOUSE_BUTTON_HELD_DOWN && is_not) {
return !sf::Mouse::isButtonPressed(startEvent.mouseButton.button);
}
/*else if (type == JOYSTICK_BUTTON_HELD_DOWN)
return sf::Joystick::isButtonPressed(startEvent.joystickButton.button);*/
for (unsigned int i = 0; i < events.size(); i++) {
if (!is_not && !pressed) {
return equalEvent(events[i], startEvent);
} else if (is_not) {
return !equalEvent(events[i], startEvent);
} else {
return false;
}
}
} else {
return comparator(std::ref(this->leftChild), std::ref(this->rightChild));
}
}
Action::Action (EVENT_TYPE type, Action leftChild, Action rightChild) {
leaf = false;
this->type = type;
this->leftChild = std::make_unique<Action>(leftChild);
this->rightChild = std::make_unique<Action>(rightChild);
is_not = false;
pressed = false;
if (type == COMBINED_WITH_AND) {
comparator = std::bind(&Action::andComparator, this, std::ref(this->leftChild), std::ref(this->rightChild));
} else if (type == COMBINED_WITH_OR) {
comparator = std::bind(&Action::orComparator, this, std::ref(this->leftChild), std::ref(this->rightChild));
} else {
comparator = std::bind(&Action::xorComparator, this, std::ref(this->leftChild), std::ref(this->rightChild));
}
}
bool Action::equalEvent (sf::Event event, sf::Event other) {
if (event.type != other.type)
return false;
if (event.type == sf::Event::Resized) {
return event.size.width == other.size.width && event.size.height == other.size.height;
}
if (event.type == sf::Event::TextEntered) {
return event.text.unicode == other.text.unicode;
}
if (event.type == sf::Event::KeyPressed || event.type == sf::Event::KeyReleased) {
if (event.type == sf::Event::KeyPressed && event.key.code == other.key.code)
pressed = true;
return event.key.code == other.key.code;
}
if (event.type == sf::Event::MouseWheelMoved) {
return event.mouseWheel.delta == other.mouseWheel.delta;
}
if (event.type == sf::Event::MouseButtonPressed || event.type == sf::Event::MouseButtonReleased) {
if (event.type == sf::Event::MouseButtonPressed && event.mouseButton.button == other.mouseButton.button)
pressed = true;
return event.mouseButton.button == other.mouseButton.button;
}
if (event.type == sf::Event::MouseMoved) {
return event.mouseMove.x == other.mouseMove.x && event.mouseMove.y == other.mouseMove.y;
}
if (event.type == sf::Event::JoystickButtonPressed || event.type == sf::Event::JoystickButtonReleased) {
return event.joystickButton.joystickId == other.joystickButton.joystickId
&& event.joystickButton.button == other.joystickButton.button;
}
if (event.type == sf::Event::JoystickMoved) {
return event.joystickMove.joystickId == other.joystickMove.joystickId
&& event.joystickMove.position == other.joystickMove.position;
}
return false;
}
void Action::setPressed(sf::Event event, bool pressed) {
if (!leaf) {
leftChild->setPressed(event, false);
rightChild->setPressed(event, false);
} else {
if ((event.type == sf::Event::KeyReleased && startEvent.type == sf::Event::KeyPressed && event.key.code == startEvent.key.code) ||
(event.type == sf::Event::MouseButtonReleased && startEvent.type == sf::Event::MouseButtonPressed && event.mouseButton.button == startEvent.mouseButton.button)) {
this->pressed = pressed;
}
}
}
bool Action::containsEvent(sf::Event& event) {
if (!leaf) {
return leftChild->containsEvent(event) || rightChild->containsEvent(event);
} else {
return Command::equalEvent(event, startEvent);
}
}
void Action::getEvents(std::vector<sf::Event>& events) {
if (!leaf) {
leftChild->getEvents(events);
rightChild->getEvents(events);
} else {
events.push_back(startEvent);
}
}
Action& Action::operator=(const Action& other) {
leaf = other.leaf;
pressed = other.pressed;
startEvent = other.startEvent;
is_not = other.is_not;
leftChild = std::make_unique<Action>(*other.leftChild);
rightChild = std::make_unique<Action>(*other.rightChild);
comparator = other.comparator;
pressed = other.pressed;
type = other.type;
return *this;
}
} |
Partager