diff options
author | Thorbjørn Lindeijer <bjorn@lindeijer.nl> | 2024-01-25 15:41:57 +0100 |
---|---|---|
committer | Thorbjørn Lindeijer <bjorn@lindeijer.nl> | 2024-01-25 15:41:57 +0100 |
commit | 2c51c98625b225cecfb9628c30d62d4e30f7e3e1 (patch) | |
tree | 5f8f85a40785439b6a9ea249a75e81e26d1b44f1 /src/gui | |
parent | 8fdbae08d7f269c72889f89b56493071a2279350 (diff) | |
download | mana-2c51c98625b225cecfb9628c30d62d4e30f7e3e1.tar.gz mana-2c51c98625b225cecfb9628c30d62d4e30f7e3e1.tar.bz2 mana-2c51c98625b225cecfb9628c30d62d4e30f7e3e1.tar.xz mana-2c51c98625b225cecfb9628c30d62d4e30f7e3e1.zip |
Ported to SDL2
Diffstat (limited to 'src/gui')
-rw-r--r-- | src/gui/gui.cpp | 18 | ||||
-rw-r--r-- | src/gui/gui.h | 2 | ||||
-rw-r--r-- | src/gui/sdlinput.cpp | 120 | ||||
-rw-r--r-- | src/gui/sdlinput.h | 22 | ||||
-rw-r--r-- | src/gui/setup_keyboard.cpp | 4 | ||||
-rw-r--r-- | src/gui/setup_video.cpp | 34 | ||||
-rw-r--r-- | src/gui/truetypefont.cpp | 4 | ||||
-rw-r--r-- | src/gui/viewport.cpp | 2 | ||||
-rw-r--r-- | src/gui/widgets/desktop.cpp | 24 | ||||
-rw-r--r-- | src/gui/widgets/emoteshortcutcontainer.cpp | 2 | ||||
-rw-r--r-- | src/gui/widgets/itemshortcutcontainer.cpp | 2 | ||||
-rw-r--r-- | src/gui/widgets/textfield.cpp | 43 | ||||
-rw-r--r-- | src/gui/widgets/textfield.h | 6 | ||||
-rw-r--r-- | src/gui/windowmenu.cpp | 2 |
14 files changed, 156 insertions, 129 deletions
diff --git a/src/gui/gui.cpp b/src/gui/gui.cpp index 019d3002..64509e21 100644 --- a/src/gui/gui.cpp +++ b/src/gui/gui.cpp @@ -26,6 +26,7 @@ #include "gui/sdlinput.h" #include "gui/truetypefont.h" +#include "gui/widgets/textfield.h" #include "gui/widgets/window.h" #include "gui/widgets/windowcontainer.h" @@ -190,6 +191,12 @@ void Gui::logic() Palette::advanceGradients(); gcn::Gui::logic(); + + while (!guiInput->isTextQueueEmpty()) + { + TextInput textInput = guiInput->dequeueTextInput(); + handleTextInput(textInput); + } } void Gui::draw() @@ -264,3 +271,14 @@ void Gui::handleMouseMoved(const gcn::MouseInput &mouseInput) gcn::Gui::handleMouseMoved(mouseInput); mMouseInactivityTimer = 0; } + +void Gui::handleTextInput(const TextInput &textInput) +{ + if (auto focused = mFocusHandler->getFocused()) + { + if (auto textField = dynamic_cast<TextField*>(focused)) + { + textField->textInput(textInput); + } + } +} diff --git a/src/gui/gui.h b/src/gui/gui.h index 006987da..998b6ec8 100644 --- a/src/gui/gui.h +++ b/src/gui/gui.h @@ -26,6 +26,7 @@ #include <guichan/gui.hpp> +class TextInput; class Graphics; class GuiConfigListener; class ImageSet; @@ -114,6 +115,7 @@ class Gui : public gcn::Gui protected: void handleMouseMoved(const gcn::MouseInput &mouseInput); + void handleTextInput(const TextInput &textInput); private: GuiConfigListener *mConfigListener; diff --git a/src/gui/sdlinput.cpp b/src/gui/sdlinput.cpp index 4ccb7580..6eb58a36 100644 --- a/src/gui/sdlinput.cpp +++ b/src/gui/sdlinput.cpp @@ -106,6 +106,23 @@ gcn::MouseInput SDLInput::dequeueMouseInput() return mouseInput; } +bool SDLInput::isTextQueueEmpty() const +{ + return mTextInputQueue.empty(); +} + +TextInput SDLInput::dequeueTextInput() +{ + if (mTextInputQueue.empty()) + { + throw GCN_EXCEPTION("The queue is empty."); + } + + TextInput textInput(mTextInputQueue.front()); + mTextInputQueue.pop(); + return textInput; +} + void SDLInput::pushInput(SDL_Event event) { gcn::KeyInput keyInput; @@ -119,8 +136,8 @@ void SDLInput::pushInput(SDL_Event event) keyInput.setShiftPressed(event.key.keysym.mod & KMOD_SHIFT); keyInput.setControlPressed(event.key.keysym.mod & KMOD_CTRL); keyInput.setAltPressed(event.key.keysym.mod & KMOD_ALT); - keyInput.setMetaPressed(event.key.keysym.mod & KMOD_META); - keyInput.setNumericPad(event.key.keysym.sym >= SDLK_KP0 + keyInput.setMetaPressed(event.key.keysym.mod & KMOD_GUI); + keyInput.setNumericPad(event.key.keysym.sym >= SDLK_KP_0 && event.key.keysym.sym <= SDLK_KP_EQUALS); mKeyInputQueue.push(keyInput); @@ -132,8 +149,8 @@ void SDLInput::pushInput(SDL_Event event) keyInput.setShiftPressed(event.key.keysym.mod & KMOD_SHIFT); keyInput.setControlPressed(event.key.keysym.mod & KMOD_CTRL); keyInput.setAltPressed(event.key.keysym.mod & KMOD_ALT); - keyInput.setMetaPressed(event.key.keysym.mod & KMOD_META); - keyInput.setNumericPad(event.key.keysym.sym >= SDLK_KP0 + keyInput.setMetaPressed(event.key.keysym.mod & KMOD_GUI); + keyInput.setNumericPad(event.key.keysym.sym >= SDLK_KP_0 && event.key.keysym.sym <= SDLK_KP_EQUALS); mKeyInputQueue.push(keyInput); @@ -144,19 +161,7 @@ void SDLInput::pushInput(SDL_Event event) mouseInput.setX(event.button.x); mouseInput.setY(event.button.y); mouseInput.setButton(convertMouseButton(event.button.button)); - - if (event.button.button == SDL_BUTTON_WHEELDOWN) - { - mouseInput.setType(gcn::MouseInput::WHEEL_MOVED_DOWN); - } - else if (event.button.button == SDL_BUTTON_WHEELUP) - { - mouseInput.setType(gcn::MouseInput::WHEEL_MOVED_UP); - } - else - { - mouseInput.setType(gcn::MouseInput::PRESSED); - } + mouseInput.setType(gcn::MouseInput::PRESSED); mouseInput.setTimeStamp(SDL_GetTicks()); mMouseInputQueue.push(mouseInput); break; @@ -180,6 +185,35 @@ void SDLInput::pushInput(SDL_Event event) mMouseInputQueue.push(mouseInput); break; + case SDL_MOUSEWHEEL: + if (event.wheel.y) { +#if SDL_VERSION_ATLEAST(2, 26, 0) + mouseInput.setX(event.wheel.mouseX); + mouseInput.setY(event.wheel.mouseY); +#else + int x, y; + SDL_GetMouseState(&x, &y); + mouseInput.setX(x); + mouseInput.setY(y); +#endif + mouseInput.setButton(gcn::MouseInput::EMPTY); + mouseInput.setType(event.wheel.y > 0 ? gcn::MouseInput::WHEEL_MOVED_UP + : gcn::MouseInput::WHEEL_MOVED_DOWN); + mouseInput.setTimeStamp(SDL_GetTicks()); + mMouseInputQueue.push(mouseInput); + } + break; + + case SDL_TEXTINPUT: + mTextInputQueue.emplace(event.text.text); + break; + + // TODO_SDL2: This is now a window event + // case SDL_WINDOWEVENT: + // switch (event.window.event) { + // } + // break; +#if 0 case SDL_ACTIVEEVENT: /* * This occurs when the mouse leaves the window and the Gui-chan @@ -206,7 +240,7 @@ void SDLInput::pushInput(SDL_Event event) mMouseInWindow = true; } break; - +#endif } // end switch } @@ -228,9 +262,9 @@ int SDLInput::convertMouseButton(int button) int SDLInput::convertKeyCharacter(SDL_Event event) { - SDL_keysym keysym = event.key.keysym; + SDL_Keysym keysym = event.key.keysym; - int value = keysym.unicode; + int value = keysym.sym; switch (keysym.sym) { @@ -262,16 +296,10 @@ int SDLInput::convertKeyCharacter(SDL_Event event) value = Key::PAUSE; break; case SDLK_SPACE: - // Special characters like ~ (tilde) ends up - // with the keysym.sym SDLK_SPACE which - // without this check would be lost. The check - // is only valid on key down events in SDL. - if (event.type == SDL_KEYUP || keysym.unicode == ' ') - { - value = Key::SPACE; - } + value = Key::SPACE; break; case SDLK_ESCAPE: + case SDLK_AC_BACK: value = Key::ESCAPE; break; case SDLK_DELETE: @@ -289,7 +317,7 @@ int SDLInput::convertKeyCharacter(SDL_Event event) case SDLK_PAGEUP: value = Key::PAGE_UP; break; - case SDLK_PRINT: + case SDLK_PRINTSCREEN: value = Key::PRINT_SCREEN; break; case SDLK_PAGEDOWN: @@ -340,27 +368,21 @@ int SDLInput::convertKeyCharacter(SDL_Event event) case SDLK_F15: value = Key::F15; break; - case SDLK_NUMLOCK: + case SDLK_NUMLOCKCLEAR: value = Key::NUM_LOCK; break; case SDLK_CAPSLOCK: value = Key::CAPS_LOCK; break; - case SDLK_SCROLLOCK: + case SDLK_SCROLLLOCK: value = Key::SCROLL_LOCK; break; - case SDLK_RMETA: + case SDLK_RGUI: value = Key::RIGHT_META; break; - case SDLK_LMETA: + case SDLK_LGUI: value = Key::LEFT_META; break; - case SDLK_LSUPER: - value = Key::LEFT_SUPER; - break; - case SDLK_RSUPER: - value = Key::RIGHT_SUPER; - break; case SDLK_MODE: value = Key::ALT_GR; break; @@ -391,34 +413,34 @@ int SDLInput::convertKeyCharacter(SDL_Event event) { switch (keysym.sym) { - case SDLK_KP0: + case SDLK_KP_0: value = Key::INSERT; break; - case SDLK_KP1: + case SDLK_KP_1: value = Key::END; break; - case SDLK_KP2: + case SDLK_KP_2: value = Key::DOWN; break; - case SDLK_KP3: + case SDLK_KP_3: value = Key::PAGE_DOWN; break; - case SDLK_KP4: + case SDLK_KP_4: value = Key::LEFT; break; - case SDLK_KP5: + case SDLK_KP_5: value = 0; break; - case SDLK_KP6: + case SDLK_KP_6: value = Key::RIGHT; break; - case SDLK_KP7: + case SDLK_KP_7: value = Key::HOME; break; - case SDLK_KP8: + case SDLK_KP_8: value = Key::UP; break; - case SDLK_KP9: + case SDLK_KP_9: value = Key::PAGE_UP; break; default: diff --git a/src/gui/sdlinput.h b/src/gui/sdlinput.h index 90f70df3..09bb788e 100644 --- a/src/gui/sdlinput.h +++ b/src/gui/sdlinput.h @@ -61,13 +61,15 @@ #include <queue> -#include <SDL/SDL.h> +#include <SDL.h> #include <guichan/input.hpp> #include <guichan/keyinput.hpp> #include <guichan/mouseinput.hpp> #include <guichan/platform.hpp> +#include <string> + namespace Key { enum @@ -122,6 +124,19 @@ namespace Key }; } +class TextInput +{ +public: + TextInput(const char *text) + : mText(text) + {} + + const std::string &getText() const { return mText; } + +private: + std::string mText; +}; + /** * SDL implementation of Input. */ @@ -160,6 +175,10 @@ public: virtual gcn::MouseInput dequeueMouseInput(); + bool isTextQueueEmpty() const; + + TextInput dequeueTextInput(); + protected: /** * Converts a mouse button from SDL to a Guichan mouse button @@ -181,6 +200,7 @@ protected: std::queue<gcn::KeyInput> mKeyInputQueue; std::queue<gcn::MouseInput> mMouseInputQueue; + std::queue<TextInput> mTextInputQueue; bool mMouseDown; bool mMouseInWindow; diff --git a/src/gui/setup_keyboard.cpp b/src/gui/setup_keyboard.cpp index 5e29519b..b66ba3aa 100644 --- a/src/gui/setup_keyboard.cpp +++ b/src/gui/setup_keyboard.cpp @@ -178,8 +178,8 @@ void Setup_Keyboard::refreshAssignedKey(int index) caption = keyboard.getKeyCaption(index) + ": "; else { - char *temp = SDL_GetKeyName( - (SDLKey) keyboard.getKeyValue(index)); + const char *temp = SDL_GetKeyName( + (SDL_Scancode) keyboard.getKeyValue(index)); caption = strprintf("%-25s", (keyboard.getKeyCaption(index) + ": ").c_str()) + toString(temp); diff --git a/src/gui/setup_video.cpp b/src/gui/setup_video.cpp index c4fe60a1..c9d1dbc9 100644 --- a/src/gui/setup_video.cpp +++ b/src/gui/setup_video.cpp @@ -26,7 +26,6 @@ #include "game.h" #include "graphics.h" #include "localplayer.h" -#include "log.h" #include "main.h" #include "particle.h" @@ -91,26 +90,21 @@ class ModeListModel : public gcn::ListModel ModeListModel::ModeListModel() { /* Get available fullscreen/hardware modes */ - SDL_Rect **modes = SDL_ListModes(NULL, SDL_FULLSCREEN | SDL_HWSURFACE); - - /* Check which modes are available */ - if (modes == (SDL_Rect **)0) - logger->log("No modes available"); - else if (modes == (SDL_Rect **)-1) - logger->log("All resolutions available"); - else + const int numModes = SDL_GetNumDisplayModes(0); + for (int i = 0; i < numModes; i++) { - for (int i = 0; modes[i]; ++i) - { - const int width = modes[i]->w; - const int height = modes[i]->h; + SDL_DisplayMode mode; + if (SDL_GetDisplayMode(0, i, &mode) != 0) + continue; - // Skip the unreasonably small modes - if (width < 640 || height < 480) - continue; + // Skip the unreasonably small modes + if (mode.w < 640 || mode.h < 480) + continue; - mVideoModes.push_back(toString(width) + "x" + toString(height)); - } + // TODO_SDL2: Modes now dinstinguish between pixel format and refresh rate as well + // TODO_SDL2: Fullscreen mode needs display selection + + mVideoModes.push_back(toString(mode.w) + "x" + toString(mode.h)); } } @@ -326,9 +320,7 @@ void Setup_Video::apply() { if (!graphics->changeVideoMode(screenWidth, screenHeight, - graphics->getBpp(), - fullscreen, - graphics->getHWAccel())) + fullscreen)) { std::stringstream errorMessage; if (fullscreen) diff --git a/src/gui/truetypefont.cpp b/src/gui/truetypefont.cpp index 620ba191..443adaa5 100644 --- a/src/gui/truetypefont.cpp +++ b/src/gui/truetypefont.cpp @@ -28,6 +28,7 @@ #include "utils/stringutils.h" +#include <guichan/color.hpp> #include <guichan/exception.hpp> const unsigned int CACHE_SIZE = 256; @@ -53,9 +54,10 @@ class TextChunk void generate(TTF_Font *font) { SDL_Color sdlCol; - sdlCol.b = color.b; sdlCol.r = color.r; sdlCol.g = color.g; + sdlCol.b = color.b; + sdlCol.a = color.a; const char *str = getSafeUtf8String(text); SDL_Surface *surface = TTF_RenderUTF8_Blended( diff --git a/src/gui/viewport.cpp b/src/gui/viewport.cpp index e06c5f8d..b4ad1b1d 100644 --- a/src/gui/viewport.cpp +++ b/src/gui/viewport.cpp @@ -310,7 +310,7 @@ void Viewport::_followMouse() if (mPlayerFollowMouse && button & SDL_BUTTON(1)) { // We create a mouse event and send it to mouseDragged. - Uint8 *keys = SDL_GetKeyState(NULL); + const Uint8 *keys = SDL_GetKeyboardState(NULL); gcn::MouseEvent mouseEvent(NULL, (keys[SDLK_LSHIFT] || keys[SDLK_RSHIFT]), false, diff --git a/src/gui/widgets/desktop.cpp b/src/gui/widgets/desktop.cpp index ce609891..07d1d887 100644 --- a/src/gui/widgets/desktop.cpp +++ b/src/gui/widgets/desktop.cpp @@ -86,14 +86,9 @@ void Desktop::draw(gcn::Graphics *graphics) if (mWallpaper) { - if (!Image::useOpenGL()) - g->drawImage(mWallpaper, - (getWidth() - mWallpaper->getWidth()) / 2, - (getHeight() - mWallpaper->getHeight()) / 2); - else - g->drawRescaledImage(mWallpaper, 0, 0, 0, 0, - mWallpaper->getWidth(), mWallpaper->getHeight(), - getWidth(), getHeight(), false); + g->drawRescaledImage(mWallpaper, 0, 0, 0, 0, + mWallpaper->getWidth(), mWallpaper->getHeight(), + getWidth(), getHeight(), false); } // Draw a thin border under the application version... @@ -125,19 +120,6 @@ void Desktop::setBestFittingWallpaper() mWallpaper->decRef(Resource::DeleteImmediately); mWallpaper = wallpaper; - - // In software mode we try to prescale the image for performance - if (!wallpaper->useOpenGL() && - (wallpaper->getWidth() != width || - wallpaper->getHeight() != height)) - { - if (Image *prescaled = wallpaper->SDLgetScaledImage(width, height)) - { - // Make sure the original can be freed - wallpaper->decRef(); - mWallpaper = prescaled; - } - } } else { diff --git a/src/gui/widgets/emoteshortcutcontainer.cpp b/src/gui/widgets/emoteshortcutcontainer.cpp index 3105a762..cdb2d3c4 100644 --- a/src/gui/widgets/emoteshortcutcontainer.cpp +++ b/src/gui/widgets/emoteshortcutcontainer.cpp @@ -92,7 +92,7 @@ void EmoteShortcutContainer::draw(gcn::Graphics *graphics) // Draw emote keyboard shortcut. const char *key = SDL_GetKeyName( - (SDLKey) keyboard.getKeyValue(keyboard.KEY_EMOTE_1 + i)); + (SDL_Scancode) keyboard.getKeyValue(keyboard.KEY_EMOTE_1 + i)); graphics->setColor(Theme::getThemeColor(Theme::TEXT)); g->drawText(key, emoteX + 2, emoteY + 2, gcn::Graphics::LEFT); diff --git a/src/gui/widgets/itemshortcutcontainer.cpp b/src/gui/widgets/itemshortcutcontainer.cpp index f1690d43..396de3bd 100644 --- a/src/gui/widgets/itemshortcutcontainer.cpp +++ b/src/gui/widgets/itemshortcutcontainer.cpp @@ -86,7 +86,7 @@ void ItemShortcutContainer::draw(gcn::Graphics *graphics) // Draw item keyboard shortcut. const char *key = SDL_GetKeyName( - (SDLKey) keyboard.getKeyValue(keyboard.KEY_SHORTCUT_1 + i)); + (SDL_Scancode) keyboard.getKeyValue(keyboard.KEY_SHORTCUT_1 + i)); graphics->setColor(Theme::getThemeColor(Theme::TEXT)); g->drawText(key, itemX + 2, itemY + 2, gcn::Graphics::LEFT); diff --git a/src/gui/widgets/textfield.cpp b/src/gui/widgets/textfield.cpp index e7c279b2..c4c00ae4 100644 --- a/src/gui/widgets/textfield.cpp +++ b/src/gui/widgets/textfield.cpp @@ -36,6 +36,8 @@ #include <guichan/font.hpp> +#include <SDL.h> + #undef DELETE //Win32 compatibility hack int TextField::instances = 0; @@ -162,33 +164,7 @@ int TextField::getValue() const void TextField::keyPressed(gcn::KeyEvent &keyEvent) { - int val = keyEvent.getKey().getValue(); - - if (val >= 32) - { - int l; - if (val < 128) l = 1; // 0xxxxxxx - else if (val < 0x800) l = 2; // 110xxxxx 10xxxxxx - else if (val < 0x10000) l = 3; // 1110xxxx 10xxxxxx 10xxxxxx - else l = 4; // 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx - - char buf[4]; - for (int i = 0; i < l; ++i) - { - buf[i] = val >> (6 * (l - i - 1)); - if (i > 0) buf[i] = (buf[i] & 63) | 128; - } - - if (l > 1) buf[0] |= 255 << (8 - l); - - mText.insert(mCaretPosition, std::string(buf, buf + l)); - mCaretPosition += l; - } - - /* In UTF-8, 10xxxxxx is only used for inner parts of characters. So skip - them when processing key presses. */ - - switch (val) + switch (keyEvent.getKey().getValue()) { case Key::LEFT: { @@ -302,8 +278,9 @@ void TextField::keyPressed(gcn::KeyEvent &keyEvent) return; break; - case 22: // Control code 22, SYNCHRONOUS IDLE, sent on Ctrl+v - handlePaste(); + case SDLK_v: + if (keyEvent.isControlPressed()) + handlePaste(); break; } @@ -311,6 +288,12 @@ void TextField::keyPressed(gcn::KeyEvent &keyEvent) fixScroll(); } +void TextField::textInput(const TextInput &textInput) +{ + mText.insert(mCaretPosition, textInput.getText()); + mCaretPosition += textInput.getText().length(); +} + void TextField::autoComplete() { if (mAutoComplete && mText.size() > 0) @@ -383,7 +366,7 @@ void TextField::handlePaste() std::string text = getText(); std::string::size_type caretPos = getCaretPosition(); - if (RetrieveBuffer(text, caretPos)) { + if (insertFromClipboard(text, caretPos)) { setText(text); setCaretPosition(caretPos); } diff --git a/src/gui/widgets/textfield.h b/src/gui/widgets/textfield.h index 94cada41..bf60cbc3 100644 --- a/src/gui/widgets/textfield.h +++ b/src/gui/widgets/textfield.h @@ -26,6 +26,7 @@ #include <vector> +class TextInput; class ImageRect; class TextField; @@ -116,6 +117,11 @@ class TextField : public gcn::TextField void keyPressed(gcn::KeyEvent &keyEvent); /** + * Handle text input (should possibly be new event in Guichan). + */ + void textInput(const TextInput &textInput); + + /** * Set the minimum value for a range */ void setMinimum(int min) { mMinimum = min; } diff --git a/src/gui/windowmenu.cpp b/src/gui/windowmenu.cpp index 12624bf1..9a53479e 100644 --- a/src/gui/windowmenu.cpp +++ b/src/gui/windowmenu.cpp @@ -170,7 +170,7 @@ static std::string createShortcutCaption(const std::string& text, if (key != KeyboardConfig::KEY_NO_VALUE) { caption += " ("; - caption += SDL_GetKeyName((SDLKey) keyboard.getKeyValue(key)); + caption += SDL_GetKeyName((SDL_Scancode) keyboard.getKeyValue(key)); caption += ")"; } return caption; |