Skip to content

Commit

Permalink
Enable basic mouse button/wheel support (#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
  • Loading branch information
carstene1ns committed Oct 7, 2016
1 parent df6755e commit f2545d9
Show file tree
Hide file tree
Showing 13 changed files with 80 additions and 23 deletions.
2 changes: 2 additions & 0 deletions src/input_buttons.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ namespace Input {
SHOW_LOG,
PAGE_UP,
PAGE_DOWN,
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 @@ -84,6 +84,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
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
51 changes: 47 additions & 4 deletions src/sdl_ui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,10 @@ void SdlUi::ToggleZoom() {
void SdlUi::ProcessEvents() {
SDL_Event evnt;

// Reset Mouse scroll
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 +646,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 +691,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 @@ -797,16 +809,44 @@ 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
}

void SdlUi::ProcessMouseButtonEvent(SDL_Event& /* evnt */) {
#if SDL_MAJOR_VERSION>1
void SdlUi::ProcessMouseWheelEvent(SDL_Event& evnt) {
#if defined(USE_MOUSE) && defined(SUPPORT_MOUSE)
// 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) {
#if defined(USE_MOUSE) && defined(SUPPORT_MOUSE)
// 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 @@ -818,6 +858,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 f2545d9

Please sign in to comment.