From 72ee43e9470c6fdbc6ed7ff92b85dfa5062c5762 Mon Sep 17 00:00:00 2001 From: Stijn Buys Date: Tue, 6 Jan 2015 18:51:37 +0000 Subject: Added separate event handlers for mouse button clicks and mouse wheel movement. --- src/Makefile.am | 4 +- src/client/client.cc | 2 +- src/client/input.cc | 190 +++++++++++++--------- src/client/joystick.cc | 16 +- src/client/key.cc | 4 +- src/client/key.h | 10 +- src/client/keyboard.cc | 381 ++++++++++++++++++++++----------------------- src/client/keyboard.h | 16 +- src/client/savegamemenu.cc | 24 ++- src/client/video.cc | 229 ++++++++++++--------------- src/client/video.h | 3 + src/osirion.cc | 2 - src/render/camera.cc | 17 +- src/render/screenshot.cc | 25 +-- src/render/state.cc | 2 +- src/ui/button.cc | 17 +- src/ui/button.h | 12 +- src/ui/console.cc | 36 +++-- src/ui/console.h | 3 + src/ui/iconbutton.cc | 9 +- src/ui/iconbutton.h | 13 +- src/ui/listitem.cc | 4 +- src/ui/listitem.h | 19 ++- src/ui/listview.cc | 31 ++-- src/ui/listview.h | 4 +- src/ui/modelview.cc | 26 +++- src/ui/modelview.h | 26 +++- src/ui/scrollbar.cc | 63 ++++---- src/ui/scrollbar.h | 26 +++- src/ui/scrollpane.cc | 23 ++- src/ui/scrollpane.h | 7 +- src/ui/slider.cc | 66 ++++---- src/ui/slider.h | 43 +++-- src/ui/ui.cc | 72 ++++++--- src/ui/ui.h | 8 +- src/ui/widget.cc | 56 ++++++- src/ui/widget.h | 30 +++- 37 files changed, 846 insertions(+), 673 deletions(-) diff --git a/src/Makefile.am b/src/Makefile.am index ccb90f4..3ed4537 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -71,7 +71,7 @@ osirion_DEPENDENCIES = \ $(top_builddir)/src/game/libgame.la \ $(SDLMAIN) $(ICON_CLIENT) -osirion_CFLAGS = $(LIBVORBISFILE_CFLAGS) $(LIBSDL_CFLAGS) $(GL_CFLAGS) $(GLUT_CFLAGS) +osirion_CFLAGS = $(LIBVORBISFILE_CFLAGS) $(LIBSDL2_CFLAGS) $(GL_CFLAGS) $(GLUT_CFLAGS) osirion_LDADD = \ $(top_builddir)/src/game/libgame.la \ @@ -86,5 +86,5 @@ osirion_LDADD = \ $(top_builddir)/src/math/libmath.la \ $(top_builddir)/src/auxiliary/libauxiliary.la \ $(SDLMAIN) $(ICON_CLIENT) \ - $(BULLET_LIBS) $(LIBSDL_LIBS) $(LIBVORBISFILE_LIBS) $(AL_LIBS) $(GL_LIBS) \ + $(BULLET_LIBS) $(LIBSDL2_LIBS) $(LIBVORBISFILE_LIBS) $(AL_LIBS) $(GL_LIBS) \ $(LIBJPG_LIBS) $(LIBPNG_LIBS) $(LIBZ_LIBS) $(HOST_LIBS) $(STATIC_LDADD) diff --git a/src/client/client.cc b/src/client/client.cc index 336baee..5ab5a0b 100644 --- a/src/client/client.cc +++ b/src/client/client.cc @@ -4,7 +4,7 @@ the terms and conditions of the GNU General Public License version 2 */ -#include +#include #include #include diff --git a/src/client/input.cc b/src/client/input.cc index 7f3a021..cf0c986 100644 --- a/src/client/input.cc +++ b/src/client/input.cc @@ -4,7 +4,7 @@ the terms and conditions of the GNU General Public License version 2 */ -#include "SDL/SDL.h" +#include "SDL2/SDL.h" #include "audio/audio.h" #include "auxiliary/functions.h" @@ -265,10 +265,6 @@ void init() keyboard = new Keyboard(); - SDL_ShowCursor(SDL_DISABLE); - SDL_WM_GrabInput(SDL_GRAB_ON); -// SDL_EnableUNICODE(1); - Joystick::init(); input_mousecontrol = core::Cvar::get("input_mousecontrol", 1.0f, core::Cvar::Archive); @@ -286,9 +282,6 @@ void init() input_grab = core::Cvar::get("input_grab", 1.0f, core::Cvar::Archive); input_grab->set_info("[bool] grab input"); - if (!input_grab->value()) { - SDL_WM_GrabInput(SDL_GRAB_OFF); - } core::Func *func = 0; func = core::Func::add("ui_control", func_ui_control); @@ -351,11 +344,6 @@ void shutdown() delete keyboard; keyboard = 0; } - - SDL_ShowCursor(SDL_ENABLE); - SDL_WM_GrabInput(SDL_GRAB_OFF); -// SDL_DisableUNICODE(0); - } void action_press(Key *key) @@ -424,16 +412,36 @@ void action_press(Key *key) /* -- camera control ------------------------------ */ case Action::CamLeft: - render::Camera::set_direction(math::min(key->pressed() - core::application()->time(), 1.0f)); + if (render::Camera::mode() == render::Camera::Free) + { + render::Camera::set_direction(math::min(key->pressed() - core::application()->time(), 1.0f)); + } else { + render::Camera::set_direction(-math::min(key->pressed() - core::application()->time(), 1.0f)); + } break; case Action::CamRight: - render::Camera::set_direction(-math::min(key->pressed() - core::application()->time(), 1.0f)); + if (render::Camera::mode() == render::Camera::Free) + { + render::Camera::set_direction(-math::min(key->pressed() - core::application()->time(), 1.0f)); + } else { + render::Camera::set_direction(math::min(key->pressed() - core::application()->time(), 1.0f)); + } break; case Action::CamUp: - render::Camera::set_pitch(math::min(key->pressed() - core::application()->time(), 1.0f)); + if (render::Camera::mode() == render::Camera::Free) + { + render::Camera::set_pitch(math::min(key->pressed() - core::application()->time(), 1.0f)); + } else { + render::Camera::set_pitch(-math::min(key->pressed() - core::application()->time(), 1.0f)); + } break; case Action::CamDown: - render::Camera::set_pitch(-math::min(key->pressed() - core::application()->time(), 1.0f)); + if (render::Camera::mode() == render::Camera::Free) + { + render::Camera::set_pitch(-math::min(key->pressed() - core::application()->time(), 1.0f)); + } else { + render::Camera::set_pitch(math::min(key->pressed() - core::application()->time(), 1.0f)); + } break; case Action::ZoomIn: render::Camera::set_zoom(-0.1f); @@ -545,10 +553,10 @@ void action_release(Key *key) render::Camera::set_direction(0.0f); break; case Action::CamUp: - render::Camera::set_pitch(0); + render::Camera::set_pitch(0.0f); break; case Action::CamDown: - render::Camera::set_pitch(0); + render::Camera::set_pitch(0.0f); break; case Action::ZoomIn: break; @@ -586,7 +594,7 @@ Key::Modifier modifier() return Key::None; } -void key_pressed(Key *key) +bool console_key_pressed(const Key *key) { if (key->action() && (key->action()->id() == Action::Console)) { local_direction = 0.0f; @@ -597,57 +605,48 @@ void key_pressed(Key *key) render::Camera::set_pitch(0.0f); ui::console()->toggle(); - return; - - } else if (ui::root()->input_key(true, Keyboard::translate_keysym(key->sym(), keyboard_modifiers), keyboard_modifiers)) { - return; + return true; + } else + { + return false; + } +} - } else if (key->bind(modifier()).size() && core::application()->connected()) { +void key_pressed(Key *key) +{ + if (!core::localplayer()->view() && core::localcontrol()) { - if (!core::localplayer()->view() && core::localcontrol()) { - - if (key->action()) { - action_press(key); - return; - } - - const char c = key->bind(modifier()).c_str()[0]; - if (c == '@') { - // target bind - if (targets::current_id()) { - core::cmd() << key->bind(modifier()) << " " << targets::current_id() << "\n"; - } - return; - } else if (c) { - // normal bind - core::cmd() << key->bind(modifier()) << "\n"; - return; - } - - } else { - const char c = key->bind(modifier()).c_str()[0]; - if (c && c != '+' && c != '@') { - // normal bind - core::cmd() << key->bind(modifier()) << "\n"; - return; + if (key->action()) { + action_press(key); + return; + } + + const char c = key->bind(modifier()).c_str()[0]; + if (c == '@') { + // target bind + if (targets::current_id()) { + core::cmd() << key->bind(modifier()) << " " << targets::current_id() << "\n"; } + return; + } else if (c) { + // normal bind + core::cmd() << key->bind(modifier()) << "\n"; + return; + } + + } else { + const char c = key->bind(modifier()).c_str()[0]; + if (c && c != '+' && c != '@') { + // normal bind + core::cmd() << key->bind(modifier()) << "\n"; + return; } } } void key_released(Key *key) { - ui::root()->input_key(false, Keyboard::translate_keysym(key->sym(), keyboard_modifiers), keyboard_modifiers); - - if (core::application()->connected() && core::localcontrol()) { - - // FIXME mouse release selection should be handled inside the hud - // note: mouse button can double as an action key - if ((key->sym() == 512 + SDL_BUTTON_LEFT) && targets::hover() && (key->waspressed() <= (input_mousedelay->value() / 1000.0f))) { - // hovering target selected - targets::set_target(targets::hover()); - } - + if (core::localcontrol()) { if (key->action()) { action_release(key); } @@ -776,7 +775,6 @@ void frame() SDL_Event event; Key *key = 0; bool pressed = false; - bool mouse_moved = false; if (last_key_time + 1.0f < client()->time()) { last_key = 0; @@ -789,25 +787,63 @@ void frame() key = 0; switch (event.type) { - case SDL_VIDEORESIZE: - video::resize(event.resize.w, event.resize.h); + case SDL_WINDOWEVENT: + if (event.window.event == SDL_WINDOWEVENT_RESIZED) + { + video::resize(event.window.data1, event.window.data2); + } break; case SDL_MOUSEMOTION: mouse_x = event.motion.x; mouse_y = event.motion.y; - mouse_moved = true; mouse_lastmoved = client()->time(); + ui::root()->input_mouse((float) mouse_x, (float) mouse_y); break; case SDL_MOUSEBUTTONDOWN: key = keyboard->press(512 + event.button.button); pressed = true; + if (key && (console_key_pressed(key) || ui::root()->input_mouse_button(true, event.button.button))) + { + key = 0; + } break; case SDL_MOUSEBUTTONUP: key = keyboard->release(512 + event.button.button); pressed = false; + if (ui::root()->input_mouse_button(false, event.button.button)) + { + key = 0; + } + else if (key && core::localcontrol()) + { + // FIXME mouse release selection should be handled inside the hud + // note: mouse button can double as an action key + if ((event.button.button == SDL_BUTTON_LEFT) && targets::hover() && (key->waspressed() <= (input_mousedelay->value() / 1000.0f))) + { + // hovering target selected + targets::set_target(targets::hover()); + } + } + break; + + case SDL_MOUSEWHEEL: + if (ui::root()->input_mouse_wheel(math::Vector2f((float)event.wheel.x, (float) event.wheel.y))) + { + key = 0; + } else + { + if (event.wheel.y > 0) + { + key = keyboard->press(600); + } else if (event.wheel.y < 0) + { + key = keyboard->press(601); + } + pressed = true; + } break; case SDL_JOYBUTTONDOWN: @@ -827,14 +863,22 @@ void frame() case SDL_KEYDOWN: keyboard_modifiers = event.key.keysym.mod; - key = keyboard->press(event.key.keysym.sym); + key = keyboard->press(event.key.keysym.scancode); pressed = true; + if (console_key_pressed(key) || ui::root()->input_key(true, Keyboard::translate_keysym(event.key.keysym.sym, keyboard_modifiers), keyboard_modifiers)) + { + key = 0; + } break; case SDL_KEYUP: keyboard_modifiers = event.key.keysym.mod; - key = keyboard->release(event.key.keysym.sym); + key = keyboard->release(event.key.keysym.scancode); pressed = false; + if (ui::root()->input_key(false, Keyboard::translate_keysym(event.key.keysym.sym, keyboard_modifiers), keyboard_modifiers)) + { + key = 0; + } break; case SDL_QUIT: @@ -843,6 +887,7 @@ void frame() break; } + // handle binds if (key) { if (pressed) { key_pressed(key); @@ -854,9 +899,6 @@ void frame() } } - if (mouse_moved) - ui::root()->input_mouse((float) mouse_x, (float) mouse_y); - /* -- handle key repeat --------------------------- */ float delay = 200.0f; // key delay time-out in milliseconds if (input_keydelay) { @@ -880,7 +922,7 @@ void frame() for (Keyboard::Keys::iterator it = keyboard->keys().begin(); it != keyboard->keys().end(); ++it) { key = (*it).second; - if (key && (key->sym() < 512) && key->pressed()) { + if (key && (key->scancode() < 512) && key->pressed()) { if ((key->pressed() + delay < core::application()->time()) && (key->lastpressed() + repeat < core::application()->time())) { key->key_lastpressed = core::application()->time(); key_pressed(key); @@ -999,8 +1041,8 @@ void frame() } } else { - render::Camera::set_direction(0.0f); - render::Camera::set_pitch(0.0f); + //render::Camera::set_direction(0.0f); + //render::Camera::set_pitch(0.0f); // disable autopilot override local_controlflags = local_controlflags & ~core::EntityControlable::ControlFlagOverride; diff --git a/src/client/joystick.cc b/src/client/joystick.cc index 0ea05a8..77d0b51 100644 --- a/src/client/joystick.cc +++ b/src/client/joystick.cc @@ -4,7 +4,7 @@ the terms and conditions of the GNU General Public License version 2 */ -#include "SDL/SDL.h" +#include "SDL2/SDL.h" #include "auxiliary/functions.h" #include "client/joystick.h" @@ -38,7 +38,7 @@ void Joystick::init() SDL_Joystick *joystick = SDL_JoystickOpen(i); if (joystick) { con_print << " joystick " << i + 1 << ": " << - SDL_JoystickName(i) << " " << + SDL_JoystickName(joystick) << " " << SDL_JoystickNumAxes(joystick) << " axes " << SDL_JoystickNumButtons(joystick) << " buttons " << std::endl; @@ -84,18 +84,14 @@ void Joystick::list() SDL_JoystickClose(current_joystick); current_joystick = 0; } - /* - // reset makes glorious segfaults - //SDL_QuitSubSystem(SDL_INIT_JOYSTICK); - //SDL_InitSubSystem(SDL_INIT_JOYSTICK); - */ + int nbjoysticks = SDL_NumJoysticks(); if (nbjoysticks) { for (int i = 0; i < nbjoysticks; i++) { SDL_Joystick *joystick = SDL_JoystickOpen(i); if (joystick) { con_print << " joystick " << i + 1 << ": " << - SDL_JoystickName(i) << " " << + SDL_JoystickName(joystick) << " " << SDL_JoystickNumAxes(joystick) << " axes " << SDL_JoystickNumButtons(joystick) << " buttons " << std::endl; @@ -115,7 +111,7 @@ void Joystick::list() } if (current_joystick) { - con_debug << " using joystick " << SDL_JoystickName(current_joystick_number - 1) << std::endl; + con_debug << " using joystick " << SDL_JoystickName(current_joystick) << std::endl; SDL_JoystickEventState(SDL_ENABLE); } } @@ -145,7 +141,7 @@ void Joystick::frame() } if (current_joystick) { - con_debug << " using joystick " << SDL_JoystickName(current_joystick_number - 1) << std::endl; + con_debug << " using joystick " << SDL_JoystickName(current_joystick) << std::endl; SDL_JoystickEventState(SDL_ENABLE); } } diff --git a/src/client/key.cc b/src/client/key.cc index 2ab32ae..36c9787 100644 --- a/src/client/key.cc +++ b/src/client/key.cc @@ -10,9 +10,9 @@ namespace client { -Key::Key(const char *name, int keysym, char ascii) +Key::Key(const unsigned int scancode, const char *name, const char ascii) { - key_sym = keysym; + key_scancode = scancode; key_ascii = ascii; key_pressed = 0; key_lastpressed = 0; diff --git a/src/client/key.h b/src/client/key.h index 3d97dd1..2c25609 100644 --- a/src/client/key.h +++ b/src/client/key.h @@ -7,7 +7,7 @@ #ifndef __INCLUDED_CLIENT_KEY_H__ #define __INCLUDED_CLIENT_KEY_H__ -#include "SDL/SDL.h" +#include "SDL2/SDL.h" #include @@ -28,7 +28,7 @@ class Key { public: /// define a new key - Key(const char *name, int keysym, char ascii = 0); + Key(const unsigned int scancode, const char *name, const char ascii = 0); ~Key(); /// key modifiers @@ -74,8 +74,8 @@ public: return key_ascii; } - inline int sym() const { - return key_sym; + inline unsigned int scancode() const { + return key_scancode; } float key_pressed; @@ -84,7 +84,7 @@ public: private: std::string key_name; - int key_sym; + unsigned int key_scancode; char key_ascii; Action *key_action; diff --git a/src/client/keyboard.cc b/src/client/keyboard.cc index 86fb2d4..e21fc91 100644 --- a/src/client/keyboard.cc +++ b/src/client/keyboard.cc @@ -66,188 +66,173 @@ Keyboard::Keyboard() // ------------------ KEYS Key *key = 0; - add_key("backspace", SDLK_BACKSPACE); - add_key("tab", SDLK_TAB, 0, "impulse"); - add_key("clear", SDLK_CLEAR); - key = add_key("enter", SDLK_RETURN, 0, "ui_chat"); + add_key(SDL_SCANCODE_BACKSPACE, "backspace"); + add_key(SDL_SCANCODE_TAB, "tab", 0, "impulse"); + add_key(SDL_SCANCODE_CLEAR, "clear"); + key = add_key( SDL_SCANCODE_RETURN, "enter", 0, "ui_chat"); key->assign(Key::Alt, "toggle r_fullscreen"); - add_key("pause", SDLK_PAUSE); - add_key("esc", SDLK_ESCAPE); - add_key("space", SDLK_SPACE, ' ', "ui_control"); - add_key("!", SDLK_EXCLAIM, '!'); - add_key("\"", SDLK_QUOTEDBL, '"'); - add_key("#", SDLK_HASH, '#'); - add_key("$", SDLK_DOLLAR, '$'); - add_key("&", SDLK_AMPERSAND, '&'); - add_key("'", SDLK_QUOTE, '\''); - add_key("(", SDLK_LEFTPAREN, '('); - add_key(")", SDLK_RIGHTPAREN, ')'); - add_key("*", SDLK_ASTERISK, '*'); - add_key("+", SDLK_PLUS, '+'); - add_key(",", SDLK_COMMA, ','); - add_key("-", SDLK_MINUS, '-'); - add_key(".", SDLK_PERIOD, '.'); - add_key("/", SDLK_SLASH, '/'); - - add_key("0", SDLK_0, '0'); - add_key("1", SDLK_1, '1'); - add_key("2", SDLK_2, '2'); - add_key("3", SDLK_3, '3'); - add_key("4", SDLK_4, '4'); - add_key("5", SDLK_5, '5'); - add_key("6", SDLK_6, '6'); - add_key("7", SDLK_7, '7'); - add_key("8", SDLK_8, '8'); - add_key("9", SDLK_9, '9'); - - add_key(":", SDLK_COLON, ':'); - add_key(";", SDLK_SEMICOLON, ';'); - add_key("<", SDLK_LESS, '<'); - add_key("=", SDLK_EQUALS, '='); - add_key(">", SDLK_GREATER, '>'); - add_key("?", SDLK_QUESTION, '?'); - add_key("@", SDLK_AT, '@'); - add_key("[", SDLK_LEFTBRACKET, '['); - add_key("\\", SDLK_BACKSLASH, '\\'); - add_key("]", SDLK_RIGHTBRACKET, ']'); - add_key("^", SDLK_CARET, '^'); - add_key("_", SDLK_UNDERSCORE, '_'); - add_key("`", SDLK_BACKQUOTE, '`', "+console"); - - add_key("a", SDLK_a, 'a', "+strafeleft"); - add_key("b", SDLK_b, 'b', "beam"); - add_key("c", SDLK_c, 'c'); - add_key("d", SDLK_d, 'd', "+straferight"); - add_key("e", SDLK_e, 'e', "+rollright"); - add_key("f", SDLK_f, 'f', "+strafedown"); - add_key("g", SDLK_g, 'g'); - add_key("h", SDLK_h, 'h'); - add_key("i", SDLK_i, 'i', "ui_inventory"); - add_key("j", SDLK_j, 'j'); - add_key("k", SDLK_k, 'k'); - add_key("l", SDLK_l, 'l'); - add_key("m", SDLK_m, 'm', "ui_map"); - key = add_key("n", SDLK_n, 'n', "target_next"); + add_key(SDL_SCANCODE_PAUSE, "pause"); + add_key(SDL_SCANCODE_ESCAPE, "esc"); + add_key(SDL_SCANCODE_SPACE, "space", ' ', "ui_control"); + + add_key(SDL_SCANCODE_0, "0", '0'); + add_key(SDL_SCANCODE_1, "1", '1'); + add_key(SDL_SCANCODE_2, "2", '2'); + add_key(SDL_SCANCODE_3, "3", '3'); + add_key(SDL_SCANCODE_4, "4", '4'); + add_key(SDL_SCANCODE_5, "5", '5'); + add_key(SDL_SCANCODE_6, "6", '6'); + add_key(SDL_SCANCODE_7, "7", '7'); + add_key(SDL_SCANCODE_8, "8", '8'); + add_key(SDL_SCANCODE_9, "9", '9'); + + add_key(SDL_SCANCODE_MINUS, "-", '-'); + add_key(SDL_SCANCODE_EQUALS, "=", '='); + add_key(SDL_SCANCODE_LEFTBRACKET, "[", '['); + add_key(SDL_SCANCODE_RIGHTBRACKET, "]", ']'); + add_key(SDL_SCANCODE_SEMICOLON, ";", ';'); + add_key(SDL_SCANCODE_APOSTROPHE,"'", '\''); + add_key(SDL_SCANCODE_BACKSLASH, "\\", '\\'); + + + add_key(SDL_SCANCODE_COMMA, ",", ','); + add_key(SDL_SCANCODE_PERIOD, ".", '.'); + add_key(SDL_SCANCODE_SLASH, "/", '/'); + add_key(SDL_SCANCODE_GRAVE,"`", '`', "+console"); + + add_key(SDL_SCANCODE_A, "a", 'a', "+strafeleft"); + add_key(SDL_SCANCODE_B, "b", 'b', "beam"); + add_key(SDL_SCANCODE_C, "c", 'c'); + add_key(SDL_SCANCODE_D, "d", 'd', "+straferight"); + add_key(SDL_SCANCODE_E, "e", 'e', "+rollright"); + add_key(SDL_SCANCODE_F, "f", 'f', "+strafedown"); + add_key(SDL_SCANCODE_G, "g", 'g'); + add_key(SDL_SCANCODE_H, "h", 'h'); + add_key(SDL_SCANCODE_I, "i", 'i', "ui_inventory"); + add_key(SDL_SCANCODE_J, "j", 'j'); + add_key(SDL_SCANCODE_K, "k", 'k'); + add_key(SDL_SCANCODE_L, "l", 'l'); + add_key(SDL_SCANCODE_M, "m", 'm', "ui_map"); + key = add_key(SDL_SCANCODE_N, "n", 'n', "target_next"); key->assign(Key::Shift, "target_prev"); key->assign(Key::Ctrl, "target_none"); - add_key("o", SDLK_o, 'o'); - add_key("p", SDLK_p, 'p'); - add_key("q", SDLK_q, 'q', "+rollleft"); - add_key("r", SDLK_r, 'r', "+strafeup"); - add_key("s", SDLK_s, 's', "+reverse"); - add_key("t", SDLK_t, 't', "ui_chatbar"); - add_key("u", SDLK_u, 'u'); - key = add_key("v", SDLK_v, 'v', "view_next"); + add_key(SDL_SCANCODE_O, "o", 'o'); + add_key(SDL_SCANCODE_P, "p", 'p'); + add_key(SDL_SCANCODE_Q, "q", 'q', "+rollleft"); + add_key(SDL_SCANCODE_R, "r", 'r', "+strafeup"); + add_key(SDL_SCANCODE_S, "s", 's', "+reverse"); + add_key(SDL_SCANCODE_T, "t", 't', "ui_chatbar"); + add_key(SDL_SCANCODE_U, "u", 'u'); + key = add_key(SDL_SCANCODE_V, "v", 'v', "view_next"); key->assign(Key::Shift, "view_prev"); - add_key("w", SDLK_w, 'w', "+afterburner"); - key = add_key("x", SDLK_x, 'x', "target_controlable_next"); + add_key(SDL_SCANCODE_W, "w", 'w', "+afterburner"); + key = add_key(SDL_SCANCODE_X, "x", 'x', "target_controlable_next"); key->assign(Key::Shift, "target_controlable_prev"); key->assign(Key::Ctrl, "target_center"); - add_key("y", SDLK_y, 'y'); - add_key("z", SDLK_z, 'z'); - - add_key("del", SDLK_DELETE); - add_key("kp0", SDLK_KP0); - add_key("kp1", SDLK_KP1); - add_key("kp2", SDLK_KP2, 0, "+up"); - add_key("kp3", SDLK_KP3); - add_key("kp4", SDLK_KP4, 0, "+left"); - add_key("kp5", SDLK_KP5); - add_key("kp6", SDLK_KP6, 0, "+right"); - add_key("kp7", SDLK_KP7, 0, "+rollleft"); - add_key("kp8", SDLK_KP8, 0, "+down"); - add_key("kp9", SDLK_KP9, 0, "+rollright"); - - add_key("kpperiod", SDLK_KP_PERIOD, '.'); - add_key("kpdiv", SDLK_KP_DIVIDE, '/', "+zoomin"); - add_key("kpmul", SDLK_KP_MULTIPLY, '*', "+zoomout"); - add_key("kpmin", SDLK_KP_MINUS, '-', "+thrustdown"); - add_key("kpplus", SDLK_KP_PLUS, '+', "+thrustup"); - add_key("kpenter", SDLK_KP_ENTER, '\n', "ui_chat"); - add_key("kpequal", SDLK_KP_EQUALS, '='); - - add_key("up", SDLK_UP, 0, "+camup"); - add_key("down", SDLK_DOWN, 0, "+camdown"); - add_key("right", SDLK_RIGHT, 0, "+camright"); - add_key("left", SDLK_LEFT, 0, "+camleft"); - - add_key("insert", SDLK_INSERT); - add_key("home", SDLK_HOME); - add_key("end", SDLK_END); - add_key("pageup", SDLK_PAGEUP); - add_key("pagedown", SDLK_PAGEDOWN); - - add_key("f1", SDLK_F1, 0, "freeflight"); - add_key("f2", SDLK_F2, 0, "@goto"); - add_key("f3", SDLK_F3, 0, "@dock"); - key = add_key("f4", SDLK_F4, 0, "@formation"); + add_key(SDL_SCANCODE_Y, "y", 'y'); + add_key(SDL_SCANCODE_Z, "z", 'z'); + + add_key(SDL_SCANCODE_DELETE, "del"); + add_key(SDL_SCANCODE_KP_0, "kp0"); + add_key(SDL_SCANCODE_KP_1, "kp1"); + add_key(SDL_SCANCODE_KP_2, "kp2", 0, "+up"); + add_key(SDL_SCANCODE_KP_3, "kp3"); + add_key(SDL_SCANCODE_KP_4, "kp4", 0, "+left"); + add_key(SDL_SCANCODE_KP_5, "kp5"); + add_key(SDL_SCANCODE_KP_6, "kp6", 0, "+right"); + add_key(SDL_SCANCODE_KP_7, "kp7", 0, "+rollleft"); + add_key(SDL_SCANCODE_KP_8, "kp8", 0, "+down"); + add_key(SDL_SCANCODE_KP_9, "kp9", 0, "+rollright"); + + add_key(SDL_SCANCODE_KP_PERIOD, "kpperiod", '.'); + add_key(SDL_SCANCODE_KP_DIVIDE, "kpdiv", '/', "+zoomin"); + add_key(SDL_SCANCODE_KP_MULTIPLY, "kpmul", '*', "+zoomout"); + add_key(SDL_SCANCODE_KP_MINUS, "kpmin", '-', "+thrustdown"); + add_key(SDL_SCANCODE_KP_PLUS, "kpplus", '+', "+thrustup"); + add_key(SDL_SCANCODE_KP_ENTER, "kpenter", '\n', "ui_chat"); + add_key(SDL_SCANCODE_KP_EQUALS, "kpequal", '='); + + add_key(SDL_SCANCODE_UP, "up", 0, "+camup"); + add_key(SDL_SCANCODE_DOWN, "down", 0, "+camdown"); + add_key(SDL_SCANCODE_RIGHT, "right", 0, "+camright"); + add_key(SDL_SCANCODE_LEFT, "left", 0, "+camleft"); + + add_key(SDL_SCANCODE_INSERT, "insert"); + add_key(SDL_SCANCODE_HOME, "home"); + add_key(SDL_SCANCODE_END, "end"); + add_key(SDL_SCANCODE_PAGEUP, "pageup"); + add_key(SDL_SCANCODE_PAGEDOWN, "pagedown"); + + add_key(SDL_SCANCODE_F1, "f1", 0, "freeflight"); + add_key(SDL_SCANCODE_F2, "f2", 0, "@goto"); + add_key(SDL_SCANCODE_F3, "f3", 0, "@dock"); + key = add_key(SDL_SCANCODE_F4, "f4", 0, "@formation"); #ifdef _WIN32 key->assign(Key::Alt, "quit"); #endif - add_key("f5", SDLK_F5, 0, "savegame"); - add_key("f6", SDLK_F6); - add_key("f7", SDLK_F7); - add_key("f8", SDLK_F8); - add_key("f9", SDLK_F9, 0, "loadgame"); - add_key("f10", SDLK_F10); - add_key("f11", SDLK_F11); - add_key("f12", SDLK_F12); - add_key("f13", SDLK_F13); - add_key("f14", SDLK_F14); - add_key("f15", SDLK_F15); - - add_key("numlock", SDLK_NUMLOCK); - add_key("capslock", SDLK_CAPSLOCK); - add_key("scrollock", SDLK_SCROLLOCK); - - add_key("rshift", SDLK_RSHIFT); - add_key("lshift", SDLK_LSHIFT); - - add_key("rctrl", SDLK_RCTRL); - add_key("lctrl", SDLK_LCTRL); - - add_key("ralt", SDLK_RALT); - add_key("lalt", SDLK_LALT); - - add_key("rmeta", SDLK_RMETA); - add_key("lmeta", SDLK_LMETA); - add_key("lwin", SDLK_LSUPER); - add_key("rwin", SDLK_RSUPER); - add_key("mode", SDLK_MODE); - - add_key("help", SDLK_HELP); - add_key("print", SDLK_PRINT, 0, "screenshot"); - add_key("sysrq", SDLK_SYSREQ); - add_key("break", SDLK_BREAK); - add_key("menu", SDLK_MENU); - add_key("power", SDLK_POWER); - add_key("euro", SDLK_EURO); + add_key(SDL_SCANCODE_F5, "f5", 0, "savegame"); + add_key(SDL_SCANCODE_F6, "f6"); + add_key(SDL_SCANCODE_F7, "f7"); + add_key(SDL_SCANCODE_F8, "f8"); + add_key(SDL_SCANCODE_F9, "f9", 0, "loadgame"); + add_key(SDL_SCANCODE_F10, "f10"); + add_key(SDL_SCANCODE_F11, "f11"); + add_key(SDL_SCANCODE_F12, "f12"); + add_key(SDL_SCANCODE_F13, "f13"); + add_key(SDL_SCANCODE_F14, "f14"); + add_key(SDL_SCANCODE_F15, "F15"); + + add_key(SDL_SCANCODE_NUMLOCKCLEAR, "numlock"); + add_key(SDL_SCANCODE_CAPSLOCK, "capslock"); + add_key(SDL_SCANCODE_SCROLLLOCK, "scrollock"); + + add_key(SDL_SCANCODE_RSHIFT, "rshift"); + add_key(SDL_SCANCODE_LSHIFT, "lshift"); + + add_key(SDL_SCANCODE_RCTRL, "rctrl"); + add_key(SDL_SCANCODE_LCTRL, "lctrl"); + + add_key(SDL_SCANCODE_RALT, "ralt"); + add_key(SDL_SCANCODE_LALT, "lalt"); + + add_key(SDL_SCANCODE_RGUI, "rgui"); + add_key(SDL_SCANCODE_LGUI, "lgui"); + add_key(SDL_SCANCODE_MODE, "mode"); + + add_key(SDL_SCANCODE_HELP, "help"); + add_key(SDL_SCANCODE_PRINTSCREEN, "print", 0, "screenshot"); + add_key(SDL_SCANCODE_SYSREQ, "sysrq"); + add_key(SDL_SCANCODE_PAUSE, "pause"); + add_key(SDL_SCANCODE_MENU, "menu"); + add_key(SDL_SCANCODE_POWER, "power"); - // mouse button aliases - add_key("mouse1", 512 + SDL_BUTTON_LEFT, 0, "+freelook"); - add_key("mouse2", 512 + SDL_BUTTON_RIGHT, 0, "+fire"); - add_key("mouse3", 512 + SDL_BUTTON_MIDDLE); - add_key("mouse4", 512 + SDL_BUTTON_WHEELUP, 0, "+thrustup"); - add_key("mouse5", 512 + SDL_BUTTON_WHEELDOWN, 0, "+thrustdown"); + // mouse button aliases + add_key(512 + SDL_BUTTON_LEFT, "mouse1", 0, "+control"); + add_key(512 + SDL_BUTTON_RIGHT, "mouse2", 0, "+fire"); + add_key(512 + SDL_BUTTON_MIDDLE, "mouse3"); + + // mouse wheel aliases + add_key(600, "mwheelup", 0, "+thrustup"); + add_key(601, "mwheeldown", 0, "+thrustdown"); // joystick button aliases - add_key("joy0", 564); - add_key("joy1", 565, 0, "target_center"); - add_key("joy2", 566); - add_key("joy3", 567); - add_key("joy4", 568); - add_key("joy5", 569); - add_key("joy6", 570); - add_key("joy7", 571); - add_key("joy8", 572); - add_key("joy9", 573); - add_key("joy10", 574); - add_key("joy11", 575); - add_key("joy12", 576); - add_key("joy13", 577); - add_key("joy14", 578); - add_key("joy15", 579); + add_key(1024, "joy0"); + add_key(1025, "joy1", 0, "target_center"); + add_key(1026, "joy2"); + add_key(1027, "joy3"); + add_key(1028, "joy4"); + add_key(1029, "joy5"); + add_key(1030, "joy6"); + add_key(1031, "joy7"); + add_key(1032, "joy8"); + add_key(1033, "joy9"); + add_key(1034, "joy10"); + add_key(1035, "joy11"); + add_key(1036, "joy12"); + add_key(1037, "joy13"); + add_key(1038, "joy14"); + add_key(1039, "joy15"); } Keyboard::~Keyboard() @@ -342,9 +327,9 @@ void Keyboard::reset() } } -Key * Keyboard::release(unsigned int sym) +Key * Keyboard::release(const unsigned int scancode) { - Key *key = find(sym); + Key *key = find(scancode); if (!key) { return 0; } @@ -356,9 +341,9 @@ Key * Keyboard::release(unsigned int sym) return key; } -Key * Keyboard::press(unsigned int sym) +Key * Keyboard::press(const unsigned int scancode) { - Key *key = find(sym); + Key *key = find(scancode); if (!key) { return 0; } @@ -389,9 +374,9 @@ Key *Keyboard::find(std::string const & name) return key; } -Key *Keyboard::find(unsigned int keysym) +Key *Keyboard::find(const unsigned int scancode) { - Keys::iterator it = _keys.find(keysym); + Keys::iterator it = _keys.find(scancode); if (it == _keys.end()) { return 0; @@ -511,10 +496,10 @@ void Keyboard::unbindall() } } -Key * Keyboard::add_key(const char *name, const unsigned int keysym, const char ascii, const char *bind) +Key * Keyboard::add_key(const unsigned int scancode, const char *name, const char ascii, const char *bind) { - Key *key = new Key(name, keysym, ascii); - _keys[keysym] = key; + Key *key = new Key(scancode, name, ascii); + _keys[scancode] = key; if (bind) { std::string bindstr(bind); @@ -579,7 +564,7 @@ void Keyboard::list_binds() const con_print << n << " registered binds" << std::endl; } -unsigned int Keyboard::translate_keysym(const int keysym, const int modifiers) +unsigned int Keyboard::translate_keysym(const unsigned int keysym, const int modifiers) { bool shift = false; @@ -588,34 +573,34 @@ unsigned int Keyboard::translate_keysym(const int keysym, const int modifiers) { switch (keysym) { - case SDLK_KP0: + case SDLK_KP_0: return '0'; break; - case SDLK_KP1: + case SDLK_KP_1: return '1'; break; - case SDLK_KP2: + case SDLK_KP_2: return '2'; break; - case SDLK_KP3: + case SDLK_KP_3: return '3'; break; - case SDLK_KP4: + case SDLK_KP_4: return '4'; break; - case SDLK_KP5: + case SDLK_KP_5: return '5'; break; - case SDLK_KP6: + case SDLK_KP_6: return '6'; break; - case SDLK_KP7: + case SDLK_KP_7: return '7'; break; - case SDLK_KP8: + case SDLK_KP_8: return '8'; break; - case SDLK_KP9: + case SDLK_KP_9: return '9'; break; case SDLK_KP_PERIOD: @@ -627,31 +612,31 @@ unsigned int Keyboard::translate_keysym(const int keysym, const int modifiers) { switch (keysym) { - case SDLK_KP0: + case SDLK_KP_0: return SDLK_INSERT; break; - case SDLK_KP1: + case SDLK_KP_1: return SDLK_END; break; - case SDLK_KP2: + case SDLK_KP_2: return SDLK_DOWN; break; - case SDLK_KP3: + case SDLK_KP_3: return SDLK_PAGEDOWN; break; - case SDLK_KP4: + case SDLK_KP_4: return SDLK_LEFT; break; - case SDLK_KP6: + case SDLK_KP_6: return SDLK_RIGHT; break; - case SDLK_KP7: + case SDLK_KP_7: return SDLK_HOME; break; - case SDLK_KP8: + case SDLK_KP_8: return SDLK_UP; break; - case SDLK_KP9: + case SDLK_KP_9: return SDLK_PAGEUP; break; case SDLK_KP_PERIOD: diff --git a/src/client/keyboard.h b/src/client/keyboard.h index 0742b9c..2ebb568 100644 --- a/src/client/keyboard.h +++ b/src/client/keyboard.h @@ -11,7 +11,7 @@ #include #include -#include "SDL/SDL.h" +#include "SDL2/SDL.h" #include "client/action.h" #include "client/key.h" @@ -29,10 +29,10 @@ public: Keyboard(); ~Keyboard(); - /// find a key on a keysym - Key *find(unsigned int keysym); + /// find a key by scancode + Key *find(unsigned int scancode); - /// find a key on name + /// find a key by name Key *find(std::string const & name); /// bind a string to a key, if str is empty, just list @@ -63,16 +63,16 @@ public: void save_binds() const; /// a key has been pressed - Key *press(unsigned int sym); + Key *press(const unsigned int scancode); /// a key has been pressed Key *press(Key *key); /// a key has been pressed - Key *release(unsigned int sym); + Key *release(const unsigned int scancode); /// convert SDL_keysym to a keystroke - static unsigned int translate_keysym(const int keysym, const int modifiers); + static unsigned int translate_keysym(const unsigned int keysym, const int modifiers); inline Keys & keys() { @@ -80,7 +80,7 @@ public: } private: - Key *add_key(const char *name, const unsigned int keysym, const char ascii = 0, const char *bind = 0); + Key *add_key(const unsigned int scancode, const char *name, const char ascii = 0, const char *bind = 0); Action *add_action(const char *name, Action::Identifier id, const char *description = 0); diff --git a/src/client/savegamemenu.cc b/src/client/savegamemenu.cc index b91a45c..c2601a5 100644 --- a/src/client/savegamemenu.cc +++ b/src/client/savegamemenu.cc @@ -12,9 +12,14 @@ #include "core/gameserver.h" #include "model/material.h" #include "client/savegamemenu.h" +#include "client/video.h" #include "filesystem/filesystem.h" #include "render/screenshot.h" +#include "render/camera.h" +#include "render/draw.h" #include "render/textures.h" +#include "render/gl.h" + #include "ui/ui.h" #include "sys/sys.h" @@ -549,8 +554,25 @@ void SaveGameMenu::savescreenshot(std::string savename) { std::string filename(filesystem::writedir() + "savegames/" + savename + ".jpg"); - render::Screenshot::savegameshot(filename); + // redraw the world without ui + + // Clear the color and depth buffers. + gl::clear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + + // set camera transformation + render::Camera::frame(0.0f); + + render::Camera::frustum(); + + // draw the world + render::draw(0.0f); + + gl::disable(GL_TEXTURE_2D); + gl::disable(GL_BLEND); + + video::swap_buffers(); + render::Screenshot::savegameshot(filename); } void SaveGameMenu::savegame(std::string savename, const std::string & description) diff --git a/src/client/video.cc b/src/client/video.cc index 0f0c984..f3ba136 100644 --- a/src/client/video.cc +++ b/src/client/video.cc @@ -17,7 +17,7 @@ #include "ui/ui.h" #include "ui/paint.h" -#include +#include using namespace render; @@ -41,32 +41,49 @@ core::Cvar *draw_clock = 0; namespace video { -float fullscreen = 0; - -int bpp = 0; -int flags = 0; +bool fullscreen = 0; int width = 0; int height = 0; -int width_prev = 0; -int height_prev = 0; - // default resolution and window size const int width_default = 1024; const int height_default = 768; +SDL_Window* sdlwindow = 0; +Uint32 sdlwindow_flags = 0; + std::string loader_message; bool is_loading = false; -void set_caption() +void draw_loader() { - // set window caption - std::string window_title(core::name()); - window_title += ' '; - window_title.append(core::version()); + render::Camera::ortho(); + + gl::enable(GL_BLEND); + + gl::color(1.0f, 1.0f, 1.0f, 1.0f); + + + const std::string loader_texture("bitmaps/loader"); + const math::Vector2f size(render::State::width(), render::State::height()); + const math::Vector2f pos; + ui::Paint::draw_bitmap(pos, size, loader_texture, true); + + if (loader_message.size()) { + using render::Text; + gl::enable(GL_TEXTURE_2D); + Text::setfont("gui", 12, 18); + Text::setcolor('N'); //set normal color + Text::draw(Text::fontwidth(), Text::fontheight(), loader_message); + gl::disable(GL_TEXTURE_2D); + } + + gl::disable(GL_BLEND); + + swap_buffers(); - SDL_WM_SetCaption(window_title.c_str(), core::name().c_str()); + is_loading = true; } bool init() @@ -110,12 +127,19 @@ bool init() } - // set the window icon + // window caption + std::string sdlwindow_title(core::name()); + sdlwindow_title += ' '; + sdlwindow_title.append(core::version()); + + // window icon /* * FIXME * store the icon as binary data * and use SDL_CreateRGBSurfaceFrom to create the icon */ + /* + * filesystem::File *iconfile = filesystem::open("bitmaps/icon.bmp"); if (iconfile) { std::string iconfilename = iconfile->path(); @@ -130,109 +154,69 @@ bool init() SDL_WM_SetIcon(image, NULL); } } - - const SDL_VideoInfo* sdl_videoinfo = SDL_GetVideoInfo(); - if (!sdl_videoinfo) { - con_error << "SDL_GetVideoInfo() failed: " << SDL_GetError() << std::endl; - return false; - } - - bpp = sdl_videoinfo->vfmt->BitsPerPixel; - if (bpp == 32) { - SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8); - SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8); - SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8); - SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8); - } else if (bpp == 24) { - SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 6); - SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 6); - SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 6); - SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 6); - } else if (bpp == 16) { - SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 4); - SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 4); - SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 4); - SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 4); - } else { - con_warn << "Display depth " << bpp << " is not supported!" << std::endl; - } - - SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); - + */ - width_prev = width; - height_prev = height; - - width = (int) r_width->value(); - height = (int) r_height->value(); + SDL_InitSubSystem(SDL_INIT_VIDEO); - fullscreen = r_fullscreen->value(); + fullscreen = (r_fullscreen->value() > 0.0f); - if (r_fullscreen->value()) { - flags = SDL_OPENGL | SDL_FULLSCREEN; - } else { - if (r_windowwidth->value() && r_windowheight->value()) { + if (fullscreen) + { + sdlwindow_flags = SDL_WINDOW_OPENGL | SDL_WINDOW_FULLSCREEN_DESKTOP | SDL_WINDOW_BORDERLESS; + sdlwindow = SDL_CreateWindow(sdlwindow_title.c_str(), SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 0, 0, sdlwindow_flags); + } + else + { + if (r_windowwidth->value() && r_windowheight->value()) + { width = (int) r_windowwidth->value(); height = (int) r_windowheight->value(); + } + else + { + width = width_default; + height = height_default; } - flags = SDL_OPENGL; + sdlwindow_flags = SDL_WINDOW_OPENGL; #ifndef _WIN32 - flags |= SDL_RESIZABLE; + sdlwindow_flags |= SDL_WINDOW_RESIZABLE; #endif + sdlwindow = SDL_CreateWindow(sdlwindow_title.c_str(), SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, width, height, sdlwindow_flags); } - - if (!SDL_SetVideoMode(width, height, bpp, flags)) { - con_warn << "Failed to set video mode " << width << "x" << height << "x" << bpp << "bpp" << std::endl; - if (width_prev && height_prev) { - width = width_prev; - height = height_prev; - if (!SDL_SetVideoMode(width, height, bpp, flags)) { - con_error << "Failed to restore video mode " << width << "x" << height << "x" << bpp << "bpp" << std::endl; - return false; - } - } else - return false; + + if (!sdlwindow) + { + con_error << "Failed to initialize SDL window: " << SDL_GetError() << std::endl; + return false; + } + + SDL_GLContext glcontext = SDL_GL_CreateContext(sdlwindow); + if (!glcontext) + { + con_error << "Failed to initialize OpenGL context: " << SDL_GetError() << std::endl; + return false; } - con_print << " video mode " << width << "x" << height << "x" << bpp << "bpp " << (fullscreen ? "fullscreen " : "window") << std::endl; - -#ifdef HAVE_DEBUG_MESSAGES - - int red, green, blue, alpha, depth; - - SDL_GL_GetAttribute(SDL_GL_RED_SIZE, &red); - SDL_GL_GetAttribute(SDL_GL_GREEN_SIZE, &green); - SDL_GL_GetAttribute(SDL_GL_RED_SIZE, &blue); - SDL_GL_GetAttribute(SDL_GL_ALPHA_SIZE, &alpha); - SDL_GL_GetAttribute(SDL_GL_DEPTH_SIZE, &depth); - - con_debug << " visual r: " << red << " g: " << green << " blue: " << blue << " alpha: " << alpha << " depth: " << depth << std::endl; - -#endif // HAVE_DEBUG_MESSAGES - - // set window caption - set_caption(); + SDL_GetWindowSize(sdlwindow, &width, &height); + // save window width and height if (fullscreen) { (*r_width) = width; (*r_height) = height; } else { (*r_windowwidth) = width; - (*r_windowheight) = height; - + (*r_windowwidth) = height; } // resize user interface ui::root()->set_size((float) width, (float) height); ui::root()->event_resize(); - // to grab or not to grab + // grab input core::Cvar *input_grab = core::Cvar::find("input_grab"); - if (ui::console()->visible() || (input_grab && input_grab->value())) { - SDL_WM_GrabInput(SDL_GRAB_OFF); - SDL_ShowCursor(SDL_ENABLE); + if (!ui::console()->visible() || (input_grab && input_grab->value())) { + SDL_SetRelativeMouseMode(SDL_TRUE); } else { - SDL_WM_GrabInput(SDL_GRAB_ON); - SDL_ShowCursor(SDL_DISABLE); + SDL_SetRelativeMouseMode(SDL_FALSE); } // initialize renderer @@ -243,6 +227,9 @@ bool init() // initialize target drawer targets::init(); + + // draw loader screen + draw_loader(); return true; } @@ -255,13 +242,12 @@ void resize(int w, int h) if (w < 320) w = 320; if (h < 200) h = 200; - if (SDL_SetVideoMode(w, h, bpp, flags)) { - render::resize(w, h); - ui::root()->set_size(w, h); - ui::root()->event_resize(); - } else { - con_warn << "Could not resize window!" << std::endl; - } + render::resize(w, h); + ui::root()->set_size(w, h); + ui::root()->event_resize(); + + (*r_windowwidth) = w; + (*r_windowwidth) = h; } void restart() @@ -305,32 +291,9 @@ void set_loader_message(const char *message) frame_loader(); } -void draw_loader() +void swap_buffers() { - render::Camera::ortho(); - - gl::enable(GL_BLEND); - - gl::color(1.0f, 1.0f, 1.0f, 1.0f); - - - const std::string loader_texture("bitmaps/loader"); - const math::Vector2f size(render::State::width(), render::State::height()); - const math::Vector2f pos; - ui::Paint::draw_bitmap(pos, size, loader_texture, true); - - if (loader_message.size()) { - using render::Text; - gl::enable(GL_TEXTURE_2D); - Text::setfont("gui", 12, 18); - Text::setcolor('N'); //set normal color - Text::draw(Text::fontwidth(), Text::fontheight(), loader_message); - gl::disable(GL_TEXTURE_2D); - } - - gl::disable(GL_BLEND); - - is_loading = true; + SDL_GL_SwapWindow(sdlwindow); } void frame_loader() @@ -340,13 +303,13 @@ void frame_loader() draw_loader(); - SDL_GL_SwapBuffers(); + swap_buffers(); } void frame(float elapsed) { // detect fullscreen/windowed mode switch - if (fullscreen != r_fullscreen->value()) + if (fullscreen != (r_fullscreen->value() > 0.0f)) restart(); using namespace render; @@ -401,7 +364,7 @@ void frame(float elapsed) gl::disable(GL_TEXTURE_2D); gl::disable(GL_BLEND); - SDL_GL_SwapBuffers(); + swap_buffers(); } void shutdown() @@ -411,11 +374,15 @@ void shutdown() targets::shutdown(); render::shutdown(); + + SDL_SetRelativeMouseMode(SDL_FALSE); + + SDL_DestroyWindow(sdlwindow); + SDL_QuitSubSystem(SDL_INIT_VIDEO); + width = 0; height = 0; - - SDL_QuitSubSystem(SDL_INIT_VIDEO); } } // namespace video diff --git a/src/client/video.h b/src/client/video.h index 0a3ed73..195fd2d 100644 --- a/src/client/video.h +++ b/src/client/video.h @@ -42,6 +42,9 @@ void set_loader_message(const std::string message); /// update the loader screen message void set_loader_message(const char *message = 0); +/// swap render buffers +void swap_buffers(); + } // namespace video extern core::Cvar *r_width; diff --git a/src/osirion.cc b/src/osirion.cc index e6dd419..8a2a1d5 100644 --- a/src/osirion.cc +++ b/src/osirion.cc @@ -4,8 +4,6 @@ the terms and conditions of the GNU General Public License version 2 */ -#include "SDL/SDL.h" - #include "client/client.h" #include "game/game.h" diff --git a/src/render/camera.cc b/src/render/camera.cc index 881d9ae..d6cf562 100644 --- a/src/render/camera.cc +++ b/src/render/camera.cc @@ -328,12 +328,21 @@ void Camera::frame(float seconds) distance = math::max(FRUSTUMFRONT / WORLDSCALE, FRUSTUMFRONT / WORLDSCALE + camera_zoom * core::localcontrol()->radius()) + 0.001f; } else if (mode() == Cockpit) { + + camera_scene_axis.assign(target_axis); + + direction_target = + 90 * target_direction; + pitch_target = + 90 * target_pitch; - // freelook target - target_axis.change_direction(90 * target_direction); - target_axis.change_pitch(90 * target_pitch); + // adjust direction + d = degrees180f(direction_current - direction_target); + direction_current = degrees360f(direction_current - d * seconds); + camera_scene_axis.change_direction(direction_current); - camera_scene_axis.assign(target_axis); + // adjust pitch + d = degrees180f(pitch_current - pitch_target); + pitch_current = degrees360f(pitch_current - d * seconds); + camera_scene_axis.change_pitch(pitch_current); if (core::localcontrol()->model()) { const float modelscale = core::localcontrol()->radius() / core::localcontrol()->model()->radius(); diff --git a/src/render/screenshot.cc b/src/render/screenshot.cc index 2fd7e9a..0142ad3 100644 --- a/src/render/screenshot.cc +++ b/src/render/screenshot.cc @@ -20,7 +20,7 @@ #include "render/tgafile.h" #include "render/gl.h" -#include +#include namespace render { @@ -34,25 +34,6 @@ int Screenshot::current_date = 0; void Screenshot::savegameshot(const std::string & filename) { - // TODO do the actual drawing - - // Clear the color and depth buffers. - gl::clear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - - // set camera transformation - Camera::frame(0.0f); - - render::Camera::frustum(); - - // draw the world - draw(0.0f); - - gl::disable(GL_TEXTURE_2D); - gl::disable(GL_BLEND); - - // swap GL buffers - SDL_GL_SwapBuffers(); - int w = State::width(); int h = State::height(); @@ -62,7 +43,7 @@ void Screenshot::savegameshot(const std::string & filename) if ((w > 0) && (h > 0)) { // read pixels into an image instance - render::Image image(w, h, 3); + Image image(w, h, 3); glReadPixels( (GLsizei) ((State::width() - w) / 2), @@ -76,7 +57,7 @@ void Screenshot::savegameshot(const std::string & filename) con_debug << "Saving " << filename << std::endl; - render::JPG::save(filename.c_str(), image, 85); + JPG::save(filename.c_str(), image, 85); } } void Screenshot::save() diff --git a/src/render/state.cc b/src/render/state.cc index 8ada29c..e88ce06 100644 --- a/src/render/state.cc +++ b/src/render/state.cc @@ -7,7 +7,7 @@ #include #include -#include "SDL/SDL.h" +#include "SDL2/SDL.h" #include "render/state.h" #include "render/gl.h" diff --git a/src/ui/button.cc b/src/ui/button.cc index 6c34a4f..d356db9 100644 --- a/src/ui/button.cc +++ b/src/ui/button.cc @@ -93,15 +93,14 @@ void Button::draw() // } } -bool Button::on_keypress(const int key, const unsigned int modifier) +bool Button::on_mousepress(const unsigned int button) { - if (key == 512 + SDL_BUTTON_LEFT) { + if (button == SDL_BUTTON_LEFT) { if (enabled()) { if (button_command.size()) { core::cmd() << button_command << std::endl; } audio::play("ui/clicked"); - emit(EventButtonClicked); } return true; @@ -110,16 +109,4 @@ bool Button::on_keypress(const int key, const unsigned int modifier) return false; } -bool Button::on_keyrelease(const int key, const unsigned int modifier) -{ - return false; -} - -void Button::on_mouseover(const math::Vector2f &cursor) -{ - if (enabled()) { - //audio::play("ui/select"); - } -} - } diff --git a/src/ui/button.h b/src/ui/button.h index 7d264a9..15d5d05 100644 --- a/src/ui/button.h +++ b/src/ui/button.h @@ -33,14 +33,10 @@ public: /// print button description virtual void print(const size_t indent) const; - /// called when the mouse enters the widget - virtual void on_mouseover(const math::Vector2f &cursor); - - /// called when the widget receives a key press - virtual bool on_keypress(const int key, const unsigned int modifier); - - /// called when the widget receives a key release - virtual bool on_keyrelease(const int key, const unsigned int modifier); + /** + * @brief mouse button press event handler + * */ + virtual bool on_mousepress(const unsigned int button); protected: /// draw the button background diff --git a/src/ui/console.cc b/src/ui/console.cc index 3799392..b683778 100644 --- a/src/ui/console.cc +++ b/src/ui/console.cc @@ -25,6 +25,8 @@ namespace ui const float DEFAULT_CONSOLE_HEIGHT = 0.7f; const size_t DEFAULT_MAX_HISTO_LINES = 512; +// number of lines to scroll +const size_t SCROLL_LINES = 3; // the global console buffer object ConsoleBuffer Console::con_buffer; @@ -87,8 +89,8 @@ void Console::show() { ui::Window::show(); raise(); - SDL_WM_GrabInput(SDL_GRAB_OFF); - SDL_ShowCursor(SDL_ENABLE); + + SDL_SetRelativeMouseMode(SDL_FALSE); console_scrollpane->set_scroll(0); console_scrollpane->set_offset(3); @@ -106,12 +108,10 @@ void Console::hide() ui::Window::hide(); core::Cvar *input_grab = core::Cvar::find("input_grab"); - if (!input_grab || input_grab->value()) { - SDL_WM_GrabInput(SDL_GRAB_ON); - SDL_ShowCursor(SDL_DISABLE); + if (input_grab && input_grab->value()) { + SDL_SetRelativeMouseMode(SDL_TRUE); } else { - SDL_WM_GrabInput(SDL_GRAB_OFF); - SDL_ShowCursor(SDL_ENABLE); + SDL_SetRelativeMouseMode(SDL_FALSE); } audio::play("ui/console"); @@ -139,9 +139,6 @@ bool Console::on_emit(Widget *sender, const Event event, void *data) bool Console::on_keypress(const int key, const unsigned int modifier) { - // number of lines to scroll - const size_t scroll_offset = 3; - ui::Text::reverse_iterator upit; switch (key) { @@ -191,14 +188,12 @@ bool Console::on_keypress(const int key, const unsigned int modifier) } return true; break; - case 512 + SDL_BUTTON_WHEELUP: case SDLK_PAGEUP: - console_scrollpane->inc_scroll(scroll_offset); + console_scrollpane->inc_scroll(SCROLL_LINES); return true; break; - case 512 + SDL_BUTTON_WHEELDOWN: case SDLK_PAGEDOWN: - console_scrollpane->dec_scroll(scroll_offset); + console_scrollpane->dec_scroll(SCROLL_LINES); return true; break; } @@ -206,6 +201,19 @@ bool Console::on_keypress(const int key, const unsigned int modifier) return false; } +bool Console::on_mousewheel(const math::Vector2f & direction) +{ + if (direction.y() > 0 ) + { + console_scrollpane->inc_scroll(SCROLL_LINES); + return true; + } else if (direction.y() < 0) { + console_scrollpane->dec_scroll(SCROLL_LINES); + return true; + } + return false; +} + void Console::draw() { console_scrollbar->set_range(0, (float) con_buffer.log().size()); diff --git a/src/ui/console.h b/src/ui/console.h index 93b6934..ea17e8b 100644 --- a/src/ui/console.h +++ b/src/ui/console.h @@ -63,6 +63,9 @@ protected: /// handle keypress events virtual bool on_keypress(const int key, const unsigned int modifier); + /// handle mousewheel events + virtual bool on_mousewheel(const math::Vector2f & direction); + /// handle emit events virtual bool on_emit(Widget *sender, const Event event, void *data); diff --git a/src/ui/iconbutton.cc b/src/ui/iconbutton.cc index b8dda87..944ae6c 100644 --- a/src/ui/iconbutton.cc +++ b/src/ui/iconbutton.cc @@ -105,9 +105,9 @@ void IconButton::draw_border() } } -bool IconButton::on_keypress(const int key, const unsigned int modifier) +bool IconButton::on_mousepress(const unsigned int button) { - if (key == 512 + SDL_BUTTON_LEFT) { + if (button == SDL_BUTTON_LEFT) { if (enabled()) { if (iconbutton_command.size()) { core::cmd() << iconbutton_command << std::endl; @@ -121,11 +121,6 @@ bool IconButton::on_keypress(const int key, const unsigned int modifier) return false; } -bool IconButton::on_keyrelease(const int key, const unsigned int modifier) -{ - return false; -} - void IconButton::on_mouseover(const math::Vector2f &cursor) { if (enabled()) { diff --git a/src/ui/iconbutton.h b/src/ui/iconbutton.h index c9a7c5b..a7aee39 100644 --- a/src/ui/iconbutton.h +++ b/src/ui/iconbutton.h @@ -51,14 +51,15 @@ public: /// print button description virtual void print(const size_t indent) const; - /// called when the mouse enters the widget + /** + * @brief mouse over event handler + * */ virtual void on_mouseover(const math::Vector2f &cursor); - /// called when the widget receives a key press - virtual bool on_keypress(const int key, const unsigned int modifier); - - /// called when the widget receives a key release - virtual bool on_keyrelease(const int key, const unsigned int modifier); + /** + * @brief mouse button press event handler + * */ + virtual bool on_mousepress(const unsigned int button); protected: /// draw the button border diff --git a/src/ui/listitem.cc b/src/ui/listitem.cc index d7c3f1e..ad821ed 100644 --- a/src/ui/listitem.cc +++ b/src/ui/listitem.cc @@ -70,9 +70,9 @@ void ListItem::on_mouseover(const math::Vector2f &cursor) } } -bool ListItem::on_keypress(const int key, const unsigned int modifier) +bool ListItem::on_mousepress(const unsigned int button) { - if (key == 512 + SDL_BUTTON_LEFT) { + if (button == SDL_BUTTON_LEFT) { //audio::play("ui/button"); emit(EventListItemClicked); diff --git a/src/ui/listitem.h b/src/ui/listitem.h index a0f4d5e..c2ccf6f 100644 --- a/src/ui/listitem.h +++ b/src/ui/listitem.h @@ -65,14 +65,21 @@ public: void select(); protected: - /// keypress event handler - virtual bool on_keypress(const int key, const unsigned int modifier); - - /// draw the button border + /** + * @brief mouseover event handler + * */ + virtual void on_mouseover(const math::Vector2f &cursor); + + /** + * @brief mouse button press event handler + * */ + virtual bool on_mousepress(const unsigned int button); + + /** + * @brief draw the button border + * */ virtual void draw_border(); - virtual void on_mouseover(const math::Vector2f &cursor); - virtual void draw(); private: diff --git a/src/ui/listview.cc b/src/ui/listview.cc index 0c01596..c72cd59 100644 --- a/src/ui/listview.cc +++ b/src/ui/listview.cc @@ -155,26 +155,21 @@ bool ListView::on_emit(Widget *sender, const Event event, void *data) return false; } -bool ListView::on_keypress(const int key, const unsigned int modifier) +bool ListView::on_mousewheel(const math::Vector2f & direction) { - switch (key) { - - case 512 + SDL_BUTTON_WHEELDOWN: - inc_scroll(1.0f); - resize(); - return true; - break; - - case 512 + SDL_BUTTON_WHEELUP: - dec_scroll(1.0f); - resize(); - return true; - break; - - default: - break; - } + if (direction.y() > 0 ) + { + dec_scroll(1.0f); + resize(); + return true; + } + else if (direction.y() < 0) + { + inc_scroll(1.0f); + resize(); + return true; + } return false; } diff --git a/src/ui/listview.h b/src/ui/listview.h index 7678b8b..616233d 100644 --- a/src/ui/listview.h +++ b/src/ui/listview.h @@ -79,9 +79,9 @@ protected: virtual bool on_emit(Widget *sender, const Event event, void *data); /** - * @brief keypress event handler + * @brief mouse hweel event handler * */ - virtual bool on_keypress(const int key, const unsigned int modifier); + virtual bool on_mousewheel(const math::Vector2f & direction); private: float listview_scroll; diff --git a/src/ui/modelview.cc b/src/ui/modelview.cc index 679c7b6..f1676b1 100755 --- a/src/ui/modelview.cc +++ b/src/ui/modelview.cc @@ -100,19 +100,29 @@ void ModelView::set_zoom(const float zoom) math::clamp(modelview_zoom, 1.0f, 5.0f); } -bool ModelView::on_keypress(const int key, const unsigned int modifier) +bool ModelView::on_mousewheel(const math::Vector2f & direction) { - if (key == 512 + SDL_BUTTON_WHEELUP) { + if (direction.y() > 0 ) + { modelview_zoom -= 0.25f; if (modelview_zoom < 1.0f) modelview_zoom = 1.0f; return true; - } else if (key == 512 + SDL_BUTTON_WHEELDOWN) { + } else if (direction.y() < 0 ) + { modelview_zoom += 0.25f; if (modelview_zoom > 5.0f) modelview_zoom = 5.0f; return true; - } else if (key == 512 + SDL_BUTTON_LEFT) { + } + + return false; +} + +bool ModelView::on_mousepress(const unsigned int button) +{ + if (button == SDL_BUTTON_LEFT) + { modelview_dragging = true; return true; } @@ -120,9 +130,10 @@ bool ModelView::on_keypress(const int key, const unsigned int modifier) return false; } -bool ModelView::on_keyrelease(const int key, const unsigned int modifier) +bool ModelView::on_mouserelease(const unsigned int button) { - if (key == 512 + SDL_BUTTON_LEFT) { + if (button == SDL_BUTTON_LEFT) + { modelview_dragging = false; return true; } @@ -132,7 +143,8 @@ bool ModelView::on_keyrelease(const int key, const unsigned int modifier) void ModelView::on_mousemove(const math::Vector2f &cursor) { - if ((width() <= 0) || (height() <= 0)) { + if ((width() <= 0) || (height() <= 0)) + { return; } diff --git a/src/ui/modelview.h b/src/ui/modelview.h index e23fd1c..f9566cd 100755 --- a/src/ui/modelview.h +++ b/src/ui/modelview.h @@ -88,16 +88,30 @@ protected: /// draw border virtual void draw_background(); - /// keypress event handler - virtual bool on_keypress(const int key, const unsigned int modifier); - - /// keyrelease event handler - virtual bool on_keyrelease(const int key, const unsigned int modifier); + /** + * @brief mouse button press event handler + * */ + virtual bool on_mousepress(const unsigned int button); + + /** + * @brief mouse button release event handler + * */ + virtual bool on_mouserelease(const unsigned int button); + /** + * @brief mouse over event handler + * */ virtual void on_mouseover(const math::Vector2f &cursor); - /// mouse movement handler + /** + * @brief mouse movement event handler + * */ virtual void on_mousemove(const math::Vector2f &cursor); + + /** + * @brief mouse hweel event handler + * */ + virtual bool on_mousewheel(const math::Vector2f & direction); private: void reset(); diff --git a/src/ui/scrollbar.cc b/src/ui/scrollbar.cc index a8826a5..a766b38 100644 --- a/src/ui/scrollbar.cc +++ b/src/ui/scrollbar.cc @@ -108,44 +108,45 @@ bool ScrollBar::on_emit(Widget *sender, const Event event, void *data) return false; } -bool ScrollBar::on_keypress(const int key, const unsigned int modifier) +bool ScrollBar::on_mousewheel(const math::Vector2f & direction) { - switch (key) { - - case 512 + SDL_BUTTON_WHEELDOWN: - if (scrollbar_value < scrollbar_maximum) { - scrollbar_value++; - emit(EventScrollBarChanged, this); - } - return true; - break; - - case 512 + SDL_BUTTON_WHEELUP: - if (scrollbar_value > scrollbar_minimum) { - scrollbar_value--; - emit(EventScrollBarChanged, this); - } - return true; - break; - - case 512 + SDL_BUTTON_LEFT: - if (scrollbar_maximum > scrollbar_minimum) { - // TODO position hit test - scrollbar_dragging = true; - } - return true; - break; - - default: - break; + if (direction.y() > 0 ) + { + if (scrollbar_value < scrollbar_maximum) { + scrollbar_value--; + emit(EventScrollBarChanged, this); + } + return true; + } else if (direction.y() < 0 ) + { + if (scrollbar_value < scrollbar_maximum) { + scrollbar_value++; + emit(EventScrollBarChanged, this); + } + return true; + } + + return false; +} + +bool ScrollBar::on_mousepress(const unsigned int button) +{ + if (button == SDL_BUTTON_LEFT) + { + if (scrollbar_maximum > scrollbar_minimum) { + // TODO position hit test + scrollbar_dragging = true; + } + return true; } return false; } -bool ScrollBar::on_keyrelease(const int key, const unsigned int modifier) +bool ScrollBar::on_mouserelease(const unsigned int button) { - if (key == 512 + SDL_BUTTON_LEFT) { + if (button == SDL_BUTTON_LEFT) + { scrollbar_dragging = false; return true; } diff --git a/src/ui/scrollbar.h b/src/ui/scrollbar.h index 0689969..c8eccf9 100644 --- a/src/ui/scrollbar.h +++ b/src/ui/scrollbar.h @@ -89,17 +89,31 @@ protected: /// emit event handler virtual bool on_emit(Widget *sender, const Event event, void *data=0); - /// keypress event handler - virtual bool on_keypress(const int key, const unsigned int modifier); - - /// keyrelease event handler - virtual bool on_keyrelease(const int key, const unsigned int modifier); + /** + * @brief mouse button press event handler + * */ + virtual bool on_mousepress(const unsigned int button); + + /** + * @brief mouse button release event handler + * */ + virtual bool on_mouserelease(const unsigned int button); + /** + * @brief mouseover event handler + * */ virtual void on_mouseover(const math::Vector2f &cursor); - /// mouse movement handler + /** + * @brief mouse movement event handler + * */ virtual void on_mousemove(const math::Vector2f &cursor); + /** + * @brief mousehweel event handler + * */ + virtual bool on_mousewheel(const math::Vector2f & direction); + private: /// validate slider value void validate(); diff --git a/src/ui/scrollpane.cc b/src/ui/scrollpane.cc index 48391c5..57c957f 100644 --- a/src/ui/scrollpane.cc +++ b/src/ui/scrollpane.cc @@ -65,24 +65,21 @@ void ScrollPane::set_offset(const int offset) scrollpane_offset = offset; } -bool ScrollPane::on_keypress(const int key, const unsigned int modifier) +bool ScrollPane::on_mousewheel(const math::Vector2f & direction) { // number of lines to scroll int alignmentmodifier =( (alignment() & AlignTop) == AlignTop) ? -1 : 1; - switch (key) { - - case 512 + SDL_BUTTON_WHEELUP: - inc_scroll(alignmentmodifier * scrollpane_offset); - return true; - break; - - case 512 + SDL_BUTTON_WHEELDOWN: - dec_scroll(alignmentmodifier * scrollpane_offset); - return true; - break; + if (direction.y() > 0 ) + { + inc_scroll(alignmentmodifier * scrollpane_offset); + return true; + } else if (direction.y() < 0 ) + { + dec_scroll(alignmentmodifier * scrollpane_offset); + return true; } - + return false; } diff --git a/src/ui/scrollpane.h b/src/ui/scrollpane.h index 8fe8395..eac2b17 100644 --- a/src/ui/scrollpane.h +++ b/src/ui/scrollpane.h @@ -58,8 +58,11 @@ protected: /// draw the scroll pane virtual void draw(); - /// key event handler provides mouse scrolling - virtual bool on_keypress(const int key, const unsigned int modifier); + + /** + * @brief mousehweel event handler + * */ + virtual bool on_mousewheel(const math::Vector2f & direction); private: ui::Text &scrollpane_text; diff --git a/src/ui/slider.cc b/src/ui/slider.cc index 9166656..ee29f05 100644 --- a/src/ui/slider.cc +++ b/src/ui/slider.cc @@ -108,44 +108,48 @@ bool Slider::on_emit(Widget *sender, const Event event, void *data) return false; } -bool Slider::on_keypress(const int key, const unsigned int modifier) +bool Slider::on_mousewheel(const math::Vector2f & direction) { - switch (key) { - - case 512 + SDL_BUTTON_WHEELUP: - if (slider_value < slider_maximum) { - slider_value++; - emit(EventSliderChanged, this); - } - return true; - break; - - case 512 + SDL_BUTTON_WHEELDOWN: - if (slider_value > slider_minimum) { - slider_value--; - emit(EventSliderChanged, this); - } - return true; - break; - - case 512 + SDL_BUTTON_LEFT: - if (slider_maximum > slider_minimum) { - // TODO position hit test - slider_dragging = true; - } - return true; - break; - - default: - break; + if (direction.y() > 0 ) + { + if (slider_value < slider_maximum) + { + slider_value++; + emit(EventSliderChanged, this); + } + return true; + } else if (direction.y() < 0 ) + { + if (slider_value > slider_minimum) + { + slider_value--; + emit(EventSliderChanged, this); + } + return true; + } + + return false; +} + +bool Slider::on_mousepress(const unsigned int button) +{ + if (button == SDL_BUTTON_LEFT) + { + if (slider_maximum > slider_minimum) + { + // TODO position hit test + slider_dragging = true; + } + return true; } return false; } -bool Slider::on_keyrelease(const int key, const unsigned int modifier) +bool Slider::on_mouserelease(const unsigned int button) { - if (key == 512 + SDL_BUTTON_LEFT) { + if (button == SDL_BUTTON_LEFT) + { slider_dragging = false; return true; } diff --git a/src/ui/slider.h b/src/ui/slider.h index 70a57ac..2f71c39 100644 --- a/src/ui/slider.h +++ b/src/ui/slider.h @@ -74,30 +74,53 @@ public: */ void set_range(const float minimum, const float maximum); - /// show the widget + /** + * @brief show the widget + * */ virtual void show(); protected: - /// resize event handler + /** + * @brief resize event handler + * */ virtual void resize(); - /// draw event handler + /** + * @brief draw event handler + * */ virtual void draw(); - /// emit event handler + /** + * @brief emit event handler + * */ virtual bool on_emit(Widget *sender, const Event event, void *data=0); - /// keypress event handler - virtual bool on_keypress(const int key, const unsigned int modifier); - - /// keyrelease event handler - virtual bool on_keyrelease(const int key, const unsigned int modifier); + /** + * @brief mouse button press event handler + * */ + virtual bool on_mousepress(const unsigned int button); + + /** + * @brief mouse button release event handler + * */ + virtual bool on_mouserelease(const unsigned int button); + /** + * @brief mouseover event handler + * */ virtual void on_mouseover(const math::Vector2f &cursor); - /// mouse movement handler + /** + * @brief mouse movement event handler + * */ virtual void on_mousemove(const math::Vector2f &cursor); + + /** + * @brief mousehweel event handler + * */ + virtual bool on_mousewheel(const math::Vector2f & direction); + private: /// validate slider value void validate(); diff --git a/src/ui/ui.cc b/src/ui/ui.cc index e10d77a..e3aeb31 100644 --- a/src/ui/ui.cc +++ b/src/ui/ui.cc @@ -284,31 +284,63 @@ void UI::input_mouse(const float x, const float y) mouse_cursor.assign(x, y); } +bool UI::input_mouse_button(const bool pressed, unsigned int button) +{ + bool handled = false; + + if (button == SDL_BUTTON_LEFT) + { + mouse_buttonleft_pressed = pressed; + } + + // set mouse focus + Widget *f = find_mouse_focus(mouse_cursor); + if (f) + { + f->event_mouse(mouse_cursor); + } + ui_mouse_focus = f; + + // send mouse button events + if (ui_mouse_focus) + { + handled = ui_mouse_focus->event_mouse_button(pressed, button); + } + return handled; +} + +bool UI::input_mouse_wheel(const math::Vector2f & direction) +{ + bool handled = false; + + // set mouse focus + Widget *f = find_mouse_focus(mouse_cursor); + if (f) + { + f->event_mouse(mouse_cursor); + } + ui_mouse_focus = f; + + // send mouse wheel events + if (ui_mouse_focus) + { + handled = ui_mouse_focus->event_mouse_wheel(direction); + } + return handled; +} + bool UI::input_key(const bool pressed, const int key, const unsigned int modifier) { bool handled = false; - if (key < 512) { - // keyboard keys - Widget *f = find_input_focus(); - if (f) { - handled = f->event_key(pressed, key, modifier); - } - ui_input_focus = f; - - } else if (key < 564) { - if ( key == 512 + SDL_BUTTON_LEFT) { - mouse_buttonleft_pressed = pressed; - } - // mouse buttons - Widget *f = find_mouse_focus(mouse_cursor); - if (f) { - f->event_mouse(mouse_cursor); - } - ui_mouse_focus = f; - if (ui_mouse_focus) - handled = ui_mouse_focus->event_key(pressed, key, modifier); + // send keyboard events + Widget *f = find_input_focus(); + if (f) + { + handled = f->event_key(pressed, key, modifier); } + ui_input_focus = f; + return handled; } diff --git a/src/ui/ui.h b/src/ui/ui.h index 77b04f5..5c44643 100644 --- a/src/ui/ui.h +++ b/src/ui/ui.h @@ -48,8 +48,14 @@ public: /// receive global mouse movement void input_mouse(const float x, const float y); + + /// receive global mouse button events + bool input_mouse_button(const bool pressed, unsigned int button); + + /// receive global mouse wheel events + bool input_mouse_wheel(const math::Vector2f & direction); - /// receive global key input + /// receive global keyboard events bool input_key(const bool pressed, const int key, const unsigned int modifier); /// run a user interface frame diff --git a/src/ui/widget.cc b/src/ui/widget.cc index e931147..982b792 100644 --- a/src/ui/widget.cc +++ b/src/ui/widget.cc @@ -438,7 +438,8 @@ bool Widget::event_key(const bool pressed, const int key, const unsigned int mod { bool handled = false; - if (enabled()) { + if (enabled()) + { if (pressed) { handled = on_keypress(key, modifier); } else { @@ -452,19 +453,51 @@ bool Widget::event_key(const bool pressed, const int key, const unsigned int mod return handled; } -bool Widget::event_mouse(const math::Vector2f &cursor) +void Widget::event_mouse(const math::Vector2f & cursor) { if (disabled()) - return false; + return; math::Vector2f local_cursor = to_local_coords(cursor); - bool handled = false; if (root()->mouse_focus() != this) { on_mouseover(local_cursor); } on_mousemove(local_cursor); +} + +bool Widget::event_mouse_button(const bool pressed, const unsigned int button) +{ + bool handled = false; + + if (enabled()) + { + if (pressed) { + handled = on_mousepress(button); + } else { + handled = on_mouserelease(button); + } + } + + if (!handled && parent()) + handled = parent()->event_mouse_button(pressed, button); + + return handled; +} + +bool Widget::event_mouse_wheel(const math::Vector2f & direction) +{ + bool handled = false; + + if (enabled()) + { + handled = on_mousewheel(direction); + } + + if (!handled && parent()) + handled = parent()->event_mouse_wheel(direction); + return handled; } @@ -512,6 +545,21 @@ void Widget::on_mousemove(const math::Vector2f &cursor) return; } +bool Widget::on_mousepress(const unsigned int button) +{ + return false; +} + +bool Widget::on_mouserelease(const unsigned int button) +{ + return false; +} + +bool Widget::on_mousewheel(const math::Vector2f & direction) +{ + return false; +} + bool Widget::on_keypress(const int key, const unsigned int modifier) { return false; diff --git a/src/ui/widget.h b/src/ui/widget.h index fafdf58..a3994af 100644 --- a/src/ui/widget.h +++ b/src/ui/widget.h @@ -7,7 +7,7 @@ #ifndef __INCLUDED_UI_WIDGET_H__ #define __INCLUDED_UI_WIDGET_H__ -#include "SDL/SDL.h" +#include "SDL2/SDL.h" #include #include @@ -270,13 +270,26 @@ public: * @see on_keyrelease **/ bool event_key(const bool pressed, const int key, const unsigned int modifier); + + /** + * @brief calls the on_mousepress and on_mouserelease event handlers and sends undhandled events to the parent widget + * @see on_mousepress + * @see on_mouserelease + * */ + bool event_mouse_button(const bool pressed, const unsigned int button); + + /** + * @brief calls the on_mousewheel event handlers and sends unhandled events to the parent widget + * @see on_mousewheel + * */ + bool event_mouse_wheel(const math::Vector2f & direction); /** * @brief calls the mouse event handlers and sends unhandled keys to the parent widget * @see on_mousemove * @see on_mouseover **/ - bool event_mouse(const math::Vector2f &cursor); + void event_mouse(const math::Vector2f & cursor); /** * @brief calls the custom event handler and sends unhandled events to the parent widget @@ -284,7 +297,9 @@ public: **/ bool event_emit(Widget *sender, const Event event, void *data = 0); - /// emit a custom event + /** + * @brief emit a custom event + * */ inline void emit(const Event event, void *data=0) { event_emit(this, event, data); } @@ -355,6 +370,15 @@ protected: /// called when the mouse enters the widget virtual void on_mouseover(const math::Vector2f &cursor); + + /// called when a mouse button is pressed + virtual bool on_mousepress(const unsigned int button); + + /// called when a mouse button is released + virtual bool on_mouserelease(const unsigned int button); + + /// called when the scrollwheel is used + virtual bool on_mousewheel(const math::Vector2f & direction); /// called when the widget receives a key press virtual bool on_keypress(const int key, const unsigned int modifier); -- cgit v1.2.3