瀏覽代碼

input: move code from input device to mouse device.

legacy
Sam Hocevar 5 年之前
父節點
當前提交
635b597e34
共有 4 個文件被更改,包括 66 次插入59 次删除
  1. +6
    -2
      src/lol-core.vcxproj
  2. +4
    -5
      src/ui/input.cpp
  3. +54
    -50
      src/ui/input.h
  4. +2
    -2
      src/ui/sdl-input.cpp

+ 6
- 2
src/lol-core.vcxproj 查看文件

@@ -121,10 +121,14 @@
<ClCompile Include="gpu\texture.cpp" />
<ClCompile Include="gpu\vertexbuffer.cpp" />
<ClCompile Include="gradient.cpp" />
<ClCompile Include="image\codec\android-image.cpp" />
<ClCompile Include="image\codec\android-image.cpp">
<ExcludedFromBuild>true</ExcludedFromBuild>
</ClCompile>
<ClCompile Include="image\codec\dummy-image.cpp" />
<ClCompile Include="image\codec\gdiplus-image.cpp" />
<ClCompile Include="image\codec\ios-image.cpp" />
<ClCompile Include="image\codec\ios-image.cpp">
<ExcludedFromBuild>true</ExcludedFromBuild>
</ClCompile>
<ClCompile Include="image\codec\oric-image.cpp" />
<ClCompile Include="image\codec\sdl-image.cpp" />
<ClCompile Include="image\codec\zed-image.cpp" />


+ 4
- 5
src/ui/input.cpp 查看文件

@@ -2,7 +2,6 @@
// Lol Engine
//
// Copyright © 2017—2019 Sam Hocevar <sam@hocevar.net>
// © 2010—2015 Benjamin Litzelmann
//
// Lol Engine is free software. It comes without any warranty, to
// the extent permitted by applicable law. You can redistribute it
@@ -105,24 +104,24 @@ std::shared_ptr<input::device::joystick> input::joystick(int n)
// input::device
//

std::string input::device::text()
std::string input::device::keyboard::text()
{
std::string ret = m_text;
m_text = "";
return ret;
}

bool input::device::capture_text()
bool input::device::keyboard::capture_text()
{
return m_input_active;
}

void input::device::capture_text(bool status)
void input::device::keyboard::capture_text(bool status)
{
m_input_active = status;
}

void input::device::internal_add_key(input::key key, const char* name)
void input::device::keyboard::internal_add_key(input::key key, const char* name)
{
while ((int)key >= (int)m_key_names.size())
{


+ 54
- 50
src/ui/input.h 查看文件

@@ -2,7 +2,6 @@
// Lol Engine
//
// Copyright © 2017—2019 Sam Hocevar <sam@hocevar.net>
// © 2010—2015 Benjamin Litzelmann
//
// Lol Engine is free software. It comes without any warranty, to
// the extent permitted by applicable law. You can redistribute it
@@ -21,6 +20,8 @@
namespace lol
{

// FIXME: maybe m_key_names is no longer required?

class input
{
public:
@@ -66,11 +67,8 @@ public:
class keyboard;
class joystick;

/** Gets the name of this input device */
const std::string& GetName() const
{
return m_name;
}
// Get the name of this input device
const std::string& name() const { return m_name; }

//
// Bindings section
@@ -134,35 +132,10 @@ public:

// TODO: axis sensitivity was removed

//
// Keyboard-specific section
//

/** Get the names of all available keys on this device */
std::vector<std::string> const& key_names() const { return m_key_names; }

/** Get the current state of all keys */
std::vector<bool> const &keys() const { return m_keys; }

/** Gets the latest contents of text input. */
std::string text();

bool capture_text();
void capture_text(bool status);

//
// Mouse-specific section
//

// Gets and sets whether the mouse cursor should be captured.
void capture(bool value) { m_capture = value; }
bool capture() const { return m_capture; }

public:
/** Internal functions that allow to construct an input device
* dynamically, when the keys, axis and cursors are not known at
* compile time. */
void internal_add_key(input::key, char const *name);
void internal_add_button(input::button, char const *name);
void internal_add_axis(input::axis, char const *name);

@@ -176,13 +149,6 @@ public:
m_changed_axes.clear();
}

void internal_set_key(input::key key, bool state)
{
if (m_keys[(int)key] != state)
(state ? m_pressed_keys : m_released_keys).insert(key);
m_keys[(int)key] = state;
}

void internal_set_button(input::button button, bool state)
{
if (m_buttons[(int)button] != state)
@@ -197,11 +163,6 @@ public:
m_axes[(int)axis] = value;
}

void internal_add_text(std::string const &text)
{
m_text += text;
}

protected:
std::string m_name;

@@ -216,13 +177,6 @@ public:
std::unordered_set<input::button> m_pressed_buttons, m_released_buttons;
std::vector<float> m_axes;
std::unordered_set<input::axis> m_changed_axes;

// Text input state
std::string m_text;
bool m_input_active = false;

// Capture (for mouse devices)
bool m_capture = false;
};

// Default devices
@@ -245,18 +199,68 @@ private:
std::map<int, std::shared_ptr<device::joystick>> m_joysticks;
};

//
// The mouse class
//

class input::device::mouse : public input::device
{
public:
mouse(std::string const &name) : input::device(name) {}

// Gets and sets whether the mouse cursor should be captured.
void capture(bool value) { m_capture = value; }
bool capture() const { return m_capture; }

private:
// Capture mouse pointer
bool m_capture = false;
};

//
// The keyboard class
//

class input::device::keyboard : public input::device
{
public:
keyboard(std::string const &name) : input::device(name) {}

// Get the names of all available keys on this device
std::vector<std::string> const& key_names() const { return m_key_names; }

// Get the current state of all keys
std::vector<bool> const &keys() const { return m_keys; }

// Gets the latest contents of text input.
std::string text();

bool capture_text();
void capture_text(bool status);

void internal_add_key(input::key, char const *name);
void internal_set_key(input::key key, bool state)
{
if (m_keys[(int)key] != state)
(state ? m_pressed_keys : m_released_keys).insert(key);
m_keys[(int)key] = state;
}

void internal_add_text(std::string const &text)
{
m_text += text;
}

private:
// Text input state
std::string m_text;
bool m_input_active = false;
};

//
// The joystick class
//

class input::device::joystick : public input::device
{
public:


+ 2
- 2
src/ui/sdl-input.cpp 查看文件

@@ -267,8 +267,8 @@ void SdlInput::tick(float seconds)
vec2 vprevmouse = vec2(m_prev_mouse_pos);
mouse->internal_set_axis(input::axis::X, vmouse.x / window_size.x);
mouse->internal_set_axis(input::axis::Y, vmouse.y / window_size.y);
mouse->internal_set_axis(input::axis::ScreenX, mouse_pos.x);
mouse->internal_set_axis(input::axis::ScreenY, mouse_pos.y);
mouse->internal_set_axis(input::axis::ScreenX, (float)mouse_pos.x);
mouse->internal_set_axis(input::axis::ScreenY, (float)mouse_pos.y);

// Note: 100.0f is an arbitrary value that makes it feel about the same than an xbox controller joystick
mouse->internal_set_axis(input::axis::MoveX, (mouse_pos.x - vprevmouse.x) * MOUSE_SPEED_MOD / max_screen_size);


Loading…
取消
儲存