Skip to content

Commit b366519

Browse files
unknownunknown
authored andcommitted
add preliminary touch support for windows 8/10 via mouse input
1 parent e5e6a16 commit b366519

File tree

4 files changed

+49
-1
lines changed

4 files changed

+49
-1
lines changed

pseuthe/include/InputComponent.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ class InputComponent final : public Component
7272
float m_health;
7373
bool m_parseInput;
7474
bool m_controllerEnabled;
75+
bool m_mouseClickPending;
76+
sf::Vector2f m_mouseClick;
7577

7678
float m_mass;
7779
float m_invMass;
@@ -84,6 +86,8 @@ class InputComponent final : public Component
8486

8587
sf::Vector2f getKeyboardArcade(float);
8688
sf::Vector2f getControllerArcade(float);
89+
90+
sf::Vector2f getMouse(float, const sf::Vector2f&);
8791
};
8892

8993
#endif //INPUT_COMPONENT_HPP_

pseuthe/include/MessageBus.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,11 +99,13 @@ class Message final
9999
RequestControlsArcade,
100100
RequestControllerEnable,
101101
RequestControllerDisable,
102-
ResizedWindow
102+
ResizedWindow,
103+
MouseClicked
103104
}type;
104105
float value;
105106
States::ID stateId;
106107
Difficulty difficulty;
108+
float mouseX, mouseY;
107109
};
108110

109111
struct PlayerEvent

pseuthe/src/GameState.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,19 @@ GameState::GameState(StateStack& stateStack, Context context)
109109

110110
bool GameState::update(float dt)
111111
{
112+
if (sf::Mouse::isButtonPressed(sf::Mouse::Left))
113+
{
114+
auto& rw = getContext().renderWindow;
115+
auto pos = rw.mapPixelToCoords(sf::Mouse::getPosition(rw));
116+
Message msg;
117+
msg.type = Message::Type::UI;
118+
msg.ui.mouseX = pos.x;
119+
msg.ui.mouseY = pos.y;
120+
msg.ui.type = Message::UIEvent::MouseClicked;
121+
m_messageBus.send(msg);
122+
}
123+
124+
112125
//probably ok to do here, although we could always raise an event when resizing window
113126
m_vignette.setPosition(getContext().defaultView.getCenter()); //TODO this should be part of the scene
114127
m_scene.setView(getContext().defaultView);
@@ -156,6 +169,17 @@ bool GameState::handleEvent(const sf::Event& evt)
156169
default: break;
157170
}
158171
}
172+
//else if (evt.type == sf::Event::MouseButtonPressed)
173+
//{
174+
// auto& rw = getContext().renderWindow;
175+
// auto pos = rw.mapPixelToCoords(sf::Mouse::getPosition(rw));
176+
// Message msg;
177+
// msg.type = Message::Type::UI;
178+
// msg.ui.mouseX = pos.x;
179+
// msg.ui.mouseY = pos.y;
180+
// msg.ui.type = Message::UIEvent::MouseClicked;
181+
// m_messageBus.send(msg);
182+
//}
159183
return true;
160184
}
161185

pseuthe/src/InputComponent.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ InputComponent::InputComponent(MessageBus& mb)
8888
m_health (maxHealth),
8989
m_parseInput (true),
9090
m_controllerEnabled (true),
91+
m_mouseClickPending (false),
9192
m_mass (0.f),
9293
m_invMass (1.f)
9394
{
@@ -112,6 +113,12 @@ void InputComponent::entityUpdate(Entity& entity, float dt)
112113
{
113114
forceVec = getController(dt);
114115
}
116+
117+
if (Util::Vector::lengthSquared(forceVec) < 0.1f
118+
&& m_mouseClickPending)
119+
{
120+
forceVec = getMouse(dt, entity.getPosition());
121+
}
115122
}
116123

117124
//limit speed
@@ -194,6 +201,11 @@ void InputComponent::handleMessage(const Message& msg)
194201
m_physicsComponent->setVelocity(m_physicsComponent->getVelocity() * 0.1f);
195202
}
196203
break;
204+
case Message::UIEvent::MouseClicked:
205+
m_mouseClickPending = true;
206+
m_mouseClick.x = msg.ui.mouseX;
207+
m_mouseClick.y = msg.ui.mouseY;
208+
break;
197209
default:break;
198210
}
199211
}
@@ -495,4 +507,10 @@ sf::Vector2f InputComponent::getControllerArcade(float dt)
495507
}
496508

497509
return forceVec;
510+
}
511+
512+
sf::Vector2f InputComponent::getMouse(float dt, const sf::Vector2f& position)
513+
{
514+
m_mouseClickPending = false;
515+
return (force * 0.05f) * Util::Vector::normalise(m_mouseClick - position) * m_invMass;
498516
}

0 commit comments

Comments
 (0)