Skip to content

Commit

Permalink
Enable basic mouse button/wheel support (EasyRPG#634)
Browse files Browse the repository at this point in the history
- left button => decision
- right button => cancel
- wheel can be used in list selectables
Mouse support is disabled by default, add a CLI option to enable
  • Loading branch information
carstene1ns committed Mar 5, 2017
1 parent 8f5742e commit 8f5df09
Show file tree
Hide file tree
Showing 17 changed files with 109 additions and 34 deletions.
3 changes: 3 additions & 0 deletions resources/easyrpg-player.6.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ for some additional binary patches.
*--show-fps*::
Enable frames per second counter.

*--enable-mouse*::
Use mouse click for decision and scroll wheel for lists.

*--hide-title*::
Hide the title background image and center the command menu.

Expand Down
18 changes: 8 additions & 10 deletions resources/unix/bash-completion/easyrpg-player
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# bash completion script for easyrpg-player
# (c) carstene1ns <dev f4ke de> 2016
# (c) carstene1ns <dev f4ke de> 2016-2017
# available under the MIT license

_easyrpg-player ()
Expand All @@ -13,13 +13,12 @@ _easyrpg-player ()
prev=${COMP_WORDS[COMP_CWORD-1]}

# all possible options
ouropts='--battle-test --disable-audio --disable-rtp --encoding --engine \
--fullscreen --show-fps --hide-title --load-game-id --new-game \
--project-path --record-input --replay-input --seed --start-map-id \
--start-position --save-path --start-party --test-play --window -v \
--version -h --help'
rpgrtopts='BattleTest battletest HideTitle hidetitle TestPlay testplay \
Window window'
ouropts='--battle-test --disable-audio --disable-rtp --enable-mouse --encoding \
--engine --fullscreen -h --help --hide-title --load-game-id --new-game \
--project-path --record-input --replay-input --save-path --seed --show-fps \
--start-map-id --start-party --start-position --test-play --window -v \
--version'
rpgrtopts='BattleTest battletest HideTitle hidetitle TestPlay testplay Window window'
engines='rpg2k rpg2kv150 rpg2k3 rpg2k3v105 rpg2k3e'

# first list all special cases
Expand Down Expand Up @@ -52,8 +51,7 @@ _easyrpg-player ()
return
;;
# argument required but no completions available
--@(battle-test|encoding|seed|start-position|start-party)| \
BattleTest|battletest)
--@(battle-test|encoding|seed|start-position|start-party)|BattleTest|battletest)
return
;;
# these have no argument and shall be used exclusively
Expand Down
4 changes: 3 additions & 1 deletion src/input_buttons.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,11 @@ namespace Input {
TOGGLE_FPS,
TAKE_SCREENSHOT,
SHOW_LOG,
RESET,
PAGE_UP,
PAGE_DOWN,
RESET,
SCROLL_UP,
SCROLL_DOWN,
BUTTON_COUNT
};

Expand Down
2 changes: 2 additions & 0 deletions src/input_buttons_desktop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ void Input::InitButtons() {
buttons[DECISION].push_back(Keys::MOUSE_LEFT);
buttons[CANCEL].push_back(Keys::MOUSE_RIGHT);
buttons[SHIFT].push_back(Keys::MOUSE_MIDDLE);
buttons[SCROLL_UP].push_back(Keys::MOUSE_SCROLLUP);
buttons[SCROLL_DOWN].push_back(Keys::MOUSE_SCROLLDOWN);
#endif

#if defined(USE_JOYSTICK) && defined(SUPPORT_JOYSTICK)
Expand Down
2 changes: 2 additions & 0 deletions src/keys.h
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,8 @@ namespace Input {
MOUSE_MIDDLE,
MOUSE_XBUTTON1,
MOUSE_XBUTTON2,
MOUSE_SCROLLUP,
MOUSE_SCROLLDOWN,
#endif

#if defined(USE_JOYSTICK) && defined(SUPPORT_JOYSTICK)
Expand Down
2 changes: 1 addition & 1 deletion src/options.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@
#endif

#define USE_KEYBOARD
//#define USE_MOUSE
#define USE_MOUSE
#define USE_JOYSTICK
#define USE_JOYSTICK_HAT
#define USE_JOYSTICK_AXIS
Expand Down
6 changes: 6 additions & 0 deletions src/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ namespace Player {
int start_map_id;
bool no_rtp_flag;
bool no_audio_flag;
bool mouse_flag;
std::string encoding;
std::string escape_symbol;
int engine;
Expand Down Expand Up @@ -418,6 +419,7 @@ void Player::ParseCommandLine(int argc, char *argv[]) {
start_map_id = -1;
no_rtp_flag = false;
no_audio_flag = false;
mouse_flag = false;

std::vector<std::string> args;

Expand Down Expand Up @@ -445,6 +447,9 @@ void Player::ParseCommandLine(int argc, char *argv[]) {
else if (*it == "--show-fps") {
fps_flag = true;
}
else if (*it == "--enable-mouse") {
mouse_flag = true;
}
else if (*it == "testplay" || *it == "--test-play") {
debug_flag = true;
}
Expand Down Expand Up @@ -926,6 +931,7 @@ R"(EasyRPG Player - An open source interpreter for RPG Maker 2000/2003 games.
rpg2k3e - RPG Maker 2003 (English release) engine
--fullscreen Start in fullscreen mode.
--show-fps Enable frames per second counter.
--enable-mouse Use mouse click for decision and scroll wheel for lists
--hide-title Hide the title background image and center the
command menu.
--load-game-id N Skip the title scene and load SaveN.lsd
Expand Down
3 changes: 3 additions & 0 deletions src/player.h
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,9 @@ namespace Player {
/** FPS flag, if true will display frames per second counter. */
extern bool fps_flag;

/** Mouse flag, if true enables mouse click and scroll wheel */
extern bool mouse_flag;

/** Battle Test flag, if true will run battle test. */
extern bool battle_test_flag;

Expand Down
10 changes: 6 additions & 4 deletions src/scene_file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,16 +141,18 @@ void Scene_File::Update() {
unsigned int old_index = index;
unsigned int max_index = file_windows.size() - 1;

if (Input::IsRepeated(Input::DOWN)) {
if (Input::IsTriggered(Input::DOWN) || index < max_index) {
if (Input::IsRepeated(Input::DOWN) || Input::IsTriggered(Input::SCROLL_DOWN)) {
if (Input::IsTriggered(Input::DOWN) || Input::IsTriggered(Input::SCROLL_DOWN)
|| index < max_index) {
Game_System::SePlay(Game_System::GetSystemSE(Game_System::SFX_Cursor));
index = (index + 1) % file_windows.size();
}

//top_index = std::max(top_index, index - 3 + 1);
}
if (Input::IsRepeated(Input::UP)) {
if (Input::IsTriggered(Input::UP) || index >= 1) {
if (Input::IsRepeated(Input::UP) || Input::IsTriggered(Input::SCROLL_UP)) {
if (Input::IsTriggered(Input::UP) || Input::IsTriggered(Input::SCROLL_UP)
|| index >= 1) {
Game_System::SePlay(Game_System::GetSystemSE(Game_System::SFX_Cursor));
index = (index + max_index) % file_windows.size();
}
Expand Down
59 changes: 55 additions & 4 deletions src/sdl_ui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,12 @@ void SdlUi::ToggleZoom() {
void SdlUi::ProcessEvents() {
SDL_Event evnt;

// Reset Mouse scroll
if (Player::mouse_flag) {
keys[Input::Keys::MOUSE_SCROLLUP] = false;
keys[Input::Keys::MOUSE_SCROLLDOWN] = false;
}

// Poll SDL events and process them
while (SDL_PollEvent(&evnt)) {
ProcessEvent(evnt);
Expand Down Expand Up @@ -642,6 +648,12 @@ void SdlUi::ProcessEvent(SDL_Event &evnt) {
ProcessMouseMotionEvent(evnt);
return;

#if SDL_MAJOR_VERSION>1
case SDL_MOUSEWHEEL:
ProcessMouseWheelEvent(evnt);
return;
#endif

case SDL_MOUSEBUTTONDOWN:
case SDL_MOUSEBUTTONUP:
ProcessMouseButtonEvent(evnt);
Expand Down Expand Up @@ -681,11 +693,13 @@ void SdlUi::ProcessActiveEvent(SDL_Event &evnt) {
state = evnt.window.event;
#endif

if (
#if SDL_MAJOR_VERSION==1
if (state == SDL_APPINPUTFOCUS && !evnt.active.gain) {
(state == SDL_APPINPUTFOCUS && !evnt.active.gain)
#else
if (state == SDL_WINDOWEVENT_FOCUS_LOST) {
state == SDL_WINDOWEVENT_FOCUS_LOST
#endif
) {

Player::Pause();

Expand Down Expand Up @@ -792,16 +806,50 @@ void SdlUi::ProcessKeyUpEvent(SDL_Event &evnt) {
#endif
}

void SdlUi::ProcessMouseMotionEvent(SDL_Event& /* evnt */) {
void SdlUi::ProcessMouseMotionEvent(SDL_Event& evnt) {
#if defined(USE_MOUSE) && defined(SUPPORT_MOUSE)
mouse_focus = true;
mouse_x = evnt.motion.x;
mouse_y = evnt.motion.y;
#else
/* unused */
(void) evnt;
#endif
}

#if SDL_MAJOR_VERSION>1
void SdlUi::ProcessMouseWheelEvent(SDL_Event& evnt) {
#if defined(USE_MOUSE) && defined(SUPPORT_MOUSE)
if (!Player::mouse_flag)
return;

// Ignore Finger (touch) events here
if (evnt.wheel.which == SDL_TOUCH_MOUSEID)
return;

int amount = evnt.wheel.y;
// translate direction
if (evnt.wheel.direction == SDL_MOUSEWHEEL_FLIPPED)
amount *= -1;

keys[Input::Keys::MOUSE_SCROLLUP] = amount > 0;
keys[Input::Keys::MOUSE_SCROLLDOWN] = amount < 0;
#else
/* unused */
(void) evnt;
#endif
}
#endif

void SdlUi::ProcessMouseButtonEvent(SDL_Event& /* evnt */) {
void SdlUi::ProcessMouseButtonEvent(SDL_Event& evnt) {
#if defined(USE_MOUSE) && defined(SUPPORT_MOUSE)
if (!Player::mouse_flag)
return;

// Ignore Finger (touch) events here
if (evnt.button.which == SDL_TOUCH_MOUSEID)
return;

switch (evnt.button.button) {
case SDL_BUTTON_LEFT:
keys[Input::Keys::MOUSE_LEFT] = evnt.button.state == SDL_PRESSED;
Expand All @@ -813,6 +861,9 @@ void SdlUi::ProcessMouseButtonEvent(SDL_Event& /* evnt */) {
keys[Input::Keys::MOUSE_RIGHT] = evnt.button.state == SDL_PRESSED;
break;
}
#else
/* unused */
(void) evnt;
#endif
}

Expand Down
2 changes: 2 additions & 0 deletions src/sdl_ui.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ class SdlUi : public BaseUi {
void ProcessJoystickHatEvent(SDL_Event &evnt);
void ProcessJoystickAxisEvent(SDL_Event &evnt);
#if SDL_MAJOR_VERSION>1
void ProcessMouseWheelEvent(SDL_Event &evnt);

void ProcessFingerDownEvent(SDL_Event & evnt);
void ProcessFingerUpEvent(SDL_Event & evnt);
void ProcessFingerEvent(SDL_Event & evnt, bool finger_down);
Expand Down
4 changes: 2 additions & 2 deletions src/window_battlecommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,12 @@ void Window_BattleCommand::Update() {
size_t num_commands = commands.size();
int old_index = index;
if (active && num_commands > 0 && index >= 0) {
if (Input::IsRepeated(Input::DOWN)) {
if (Input::IsRepeated(Input::DOWN) || Input::IsTriggered(Input::SCROLL_DOWN)) {
Game_System::SePlay(Game_System::GetSystemSE(Game_System::SFX_Cursor));
index++;
}

if (Input::IsRepeated(Input::UP)) {
if (Input::IsRepeated(Input::UP) || Input::IsTriggered(Input::SCROLL_UP)) {
Game_System::SePlay(Game_System::GetSystemSE(Game_System::SFX_Cursor));
index--;
}
Expand Down
4 changes: 2 additions & 2 deletions src/window_battleoption.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,12 @@ void Window_BattleOption::Update() {
int num_commands = commands.size();

if (active && num_commands > 0 && index >= 0) {
if (Input::IsRepeated(Input::DOWN)) {
if (Input::IsRepeated(Input::DOWN) || Input::IsTriggered(Input::SCROLL_DOWN)) {
Game_System::SePlay(Game_System::GetSystemSE(Game_System::SFX_Cursor));
index++;
}

if (Input::IsRepeated(Input::UP)) {
if (Input::IsRepeated(Input::UP) || Input::IsTriggered(Input::SCROLL_UP)) {
Game_System::SePlay(Game_System::GetSystemSE(Game_System::SFX_Cursor));
index--;
}
Expand Down
4 changes: 2 additions & 2 deletions src/window_battlestatus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ void Window_BattleStatus::Update() {
}

if (active && index >= 0) {
if (Input::IsRepeated(Input::DOWN)) {
if (Input::IsRepeated(Input::DOWN) || Input::IsTriggered(Input::SCROLL_DOWN)) {
Game_System::SePlay(Game_System::GetSystemSE(Game_System::SFX_Cursor));
for (int i = 1; i < item_max; i++) {
int new_index = (index + i) % item_max;
Expand All @@ -252,7 +252,7 @@ void Window_BattleStatus::Update() {
}
}
}
if (Input::IsRepeated(Input::UP)) {
if (Input::IsRepeated(Input::UP) || Input::IsTriggered(Input::SCROLL_UP)) {
Game_System::SePlay(Game_System::GetSystemSE(Game_System::SFX_Cursor));
for (int i = item_max - 1; i > 0; i--) {
int new_index = (index + i) % item_max;
Expand Down
10 changes: 6 additions & 4 deletions src/window_selectable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,14 +124,16 @@ void Window_Selectable::UpdateCursorRect() {
void Window_Selectable::Update() {
Window_Base::Update();
if (active && item_max > 0 && index >= 0) {
if (Input::IsRepeated(Input::DOWN)) {
if ((column_max == 1 && Input::IsTriggered(Input::DOWN)) || index < item_max - column_max) {
if (Input::IsRepeated(Input::DOWN) || Input::IsTriggered(Input::SCROLL_DOWN)) {
if (index < item_max - column_max || (column_max == 1 &&
(Input::IsTriggered(Input::DOWN) || Input::IsTriggered(Input::SCROLL_DOWN)))) {
Game_System::SePlay(Game_System::GetSystemSE(Game_System::SFX_Cursor));
index = (index + column_max) % item_max;
}
}
if (Input::IsRepeated(Input::UP)) {
if ((column_max == 1 && Input::IsTriggered(Input::UP)) || index >= column_max) {
if (Input::IsRepeated(Input::UP) || Input::IsTriggered(Input::SCROLL_UP)) {
if (index >= column_max || (column_max == 1 &&
(Input::IsTriggered(Input::UP) || Input::IsTriggered(Input::SCROLL_UP)))) {
Game_System::SePlay(Game_System::GetSystemSE(Game_System::SFX_Cursor));
index = (index - column_max + item_max) % item_max;
}
Expand Down
4 changes: 2 additions & 2 deletions src/window_shop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ void Window_Shop::Update() {
switch (mode) {
case Scene_Shop::BuySellLeave:
case Scene_Shop::BuySellLeave2:
if (Input::IsRepeated(Input::DOWN)) {
if (Input::IsRepeated(Input::DOWN) || Input::IsTriggered(Input::SCROLL_DOWN)) {
if (index < leave_index) {
index++;
}
Expand All @@ -166,7 +166,7 @@ void Window_Shop::Update() {
}
Game_System::SePlay(Game_System::GetSystemSE(Game_System::SFX_Cursor));
}
if (Input::IsRepeated(Input::UP)) {
if (Input::IsRepeated(Input::UP) || Input::IsTriggered(Input::SCROLL_UP)) {
if (index > 1) {
index--;
}
Expand Down
6 changes: 4 additions & 2 deletions src/window_shopnumber.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,11 @@ void Window_ShopNumber::Update() {
number++;
} else if (Input::IsRepeated(Input::LEFT) && number > 1) {
number--;
} else if (Input::IsRepeated(Input::UP) && number < item_max) {
} else if ((Input::IsRepeated(Input::UP) || Input::IsTriggered(Input::SCROLL_UP))
&& number < item_max) {
number = min(number + 10, item_max);
} else if (Input::IsRepeated(Input::DOWN) && number > 1) {
} else if ((Input::IsRepeated(Input::DOWN) || Input::IsTriggered(Input::SCROLL_DOWN))
&& number > 1) {
number = max(number - 10, 1);
}

Expand Down

0 comments on commit 8f5df09

Please sign in to comment.