Move keyboard functions to she::System

This commit is contained in:
David Capello 2016-11-18 11:15:02 -03:00
parent d8d9f8072c
commit 5dc8f9c8a0
16 changed files with 167 additions and 94 deletions

View File

@ -1345,7 +1345,7 @@ bool Editor::onProcessMessage(Message* msg)
case kFocusLeaveMessage:
// As we use keys like Space-bar as modifier, we can clear the
// keyboard buffer when we lost the focus.
she::clear_keyboard_buffer();
she::instance()->clearKeyboardBuffer();
break;
case kMouseWheelMessage:

View File

@ -47,6 +47,7 @@
#include "gfx/point.h"
#include "gfx/rect.h"
#include "she/font.h"
#include "she/system.h"
#include "ui/scroll_helper.h"
#include "ui/ui.h"
@ -335,7 +336,8 @@ bool Timeline::onProcessMessage(Message* msg)
if (!m_document)
break;
if (mouseMsg->middle() || she::is_key_pressed(kKeySpace)) {
if (mouseMsg->middle() ||
she::instance()->isKeyPressed(kKeySpace)) {
captureMouse();
m_state = STATE_SCROLLING;
m_oldPos = static_cast<MouseMessage*>(msg)->position();
@ -847,7 +849,7 @@ bool Timeline::onProcessMessage(Message* msg)
m_scroll = false;
// We have to clear all the kKeySpace keys in buffer.
she::clear_keyboard_buffer();
she::instance()->clearKeyboardBuffer();
used = true;
break;
}

View File

@ -200,6 +200,33 @@ public:
return sur;
}
bool isKeyPressed(KeyScancode scancode) override {
#ifdef ALLEGRO_UNIX
if (scancode == kKeyLShift || scancode == kKeyRShift) {
return key_shifts & KB_SHIFT_FLAG;
}
else if (scancode == kKeyLControl || scancode == kKeyRControl) {
return key_shifts & KB_CTRL_FLAG;
}
else if (scancode == kKeyAlt) {
return key_shifts & KB_ALT_FLAG;
}
#endif
return key[scancode] ? true: false;
}
int getUnicodeFromScancode(KeyScancode scancode) override {
if (isKeyPressed(scancode))
return scancode_to_ascii(scancode);
else
return false;
}
void clearKeyboardBuffer() override {
clear_keybuf();
}
void setTranslateDeadKeys(bool state) override {
// Do nothing
}
@ -225,35 +252,6 @@ void error_message(const char* msg)
#endif
}
bool is_key_pressed(KeyScancode scancode)
{
#ifdef ALLEGRO_UNIX
if (scancode == kKeyLShift || scancode == kKeyRShift) {
return key_shifts & KB_SHIFT_FLAG;
}
else if (scancode == kKeyLControl || scancode == kKeyRControl) {
return key_shifts & KB_CTRL_FLAG;
}
else if (scancode == kKeyAlt) {
return key_shifts & KB_ALT_FLAG;
}
#endif
return key[scancode] ? true: false;
}
int get_unicode_from_scancode(KeyScancode scancode)
{
if (is_key_pressed(scancode))
return scancode_to_ascii(scancode);
else
return false;
}
void clear_keyboard_buffer()
{
clear_keybuf();
}
} // namespace she
// It must be defined by the user program code.

View File

@ -78,6 +78,25 @@ public:
return loadFreeTypeFont(filename, height);
}
KeyModifiers keyModifiers() override {
return
(KeyModifiers)
((isKeyPressed(kKeyLShift) ||
isKeyPressed(kKeyRShift) ? kKeyShiftModifier: 0) |
(isKeyPressed(kKeyLControl) ||
isKeyPressed(kKeyRControl) ? kKeyCtrlModifier: 0) |
(isKeyPressed(kKeyAlt) ? kKeyAltModifier: 0) |
(isKeyPressed(kKeyAltGr) ? (kKeyCtrlModifier | kKeyAltModifier): 0) |
(isKeyPressed(kKeyCommand) ? kKeyCmdModifier: 0) |
(isKeyPressed(kKeySpace) ? kKeySpaceModifier: 0) |
(isKeyPressed(kKeyLWin) ||
isKeyPressed(kKeyRWin) ? kKeyWinModifier: 0));
}
void clearKeyboardBuffer() override {
// Do nothing
}
private:
NativeDialogs* m_nativeDialogs;
};

View File

@ -156,19 +156,6 @@ namespace she {
kKeyScancodes = 127
};
// TODO move these functions to she::System
// Returns true if the the given scancode key is pressed/actived.
bool is_key_pressed(KeyScancode scancode);
// Returns the latest unicode character that activated the given
// scancode.
int get_unicode_from_scancode(KeyScancode scancode);
// Clears the keyboard buffer (used only in the Allegro port).
// TODO (deprecated)
void clear_keyboard_buffer();
} // namespace she
#endif

31
src/she/osx/system.h Normal file
View File

@ -0,0 +1,31 @@
// SHE library
// Copyright (C) 2016 David Capello
//
// This file is released under the terms of the MIT license.
// Read LICENSE.txt for more information.
#ifndef SHE_OSX_SYSTEM_H
#define SHE_OSX_SYSTEM_H
#pragma once
#include "she/common/system.h"
namespace she {
bool osx_is_key_pressed(KeyScancode scancode);
int osx_get_unicode_from_scancode(KeyScancode scancode);
class OSXSystem : public CommonSystem {
public:
bool isKeyPressed(KeyScancode scancode) override {
return osx_is_key_pressed(scancode);
}
int getUnicodeFromScancode(KeyScancode scancode) override {
return osx_get_unicode_from_scancode(scancode);
}
};
} // namespace she
#endif

View File

@ -16,6 +16,7 @@
#include "she/keys.h"
#include "she/osx/generate_drop_files.h"
#include "she/osx/window.h"
#include "she/system.h"
#include <Carbon/Carbon.h> // For VK codes
@ -23,7 +24,7 @@ using namespace she;
namespace {
// Internal array of pressed keys used in is_key_pressed()
// Internal array of pressed keys used in isKeyPressed()
int g_pressedKeys[kKeyScancodes];
bool g_translateDeadKeys = false;
UInt32 g_lastDeadKeyState = 0;
@ -73,7 +74,7 @@ KeyModifiers get_modifiers_from_nsevent(NSEvent* event)
if (nsFlags & NSControlKeyMask) modifiers |= kKeyCtrlModifier;
if (nsFlags & NSAlternateKeyMask) modifiers |= kKeyAltModifier;
if (nsFlags & NSCommandKeyMask) modifiers |= kKeyCmdModifier;
if (she::is_key_pressed(kKeySpace)) modifiers |= kKeySpaceModifier;
if (she::instance()->isKeyPressed(kKeySpace)) modifiers |= kKeySpaceModifier;
return (KeyModifiers)modifiers;
}
@ -120,7 +121,7 @@ CFStringRef get_unicode_from_key_code(NSEvent* event,
namespace she {
bool is_key_pressed(KeyScancode scancode)
bool osx_is_key_pressed(KeyScancode scancode)
{
if (scancode >= 0 && scancode < kKeyScancodes)
return (g_pressedKeys[scancode] != 0);
@ -128,7 +129,7 @@ bool is_key_pressed(KeyScancode scancode)
return false;
}
int get_unicode_from_scancode(KeyScancode scancode)
int osx_get_unicode_from_scancode(KeyScancode scancode)
{
if (scancode >= 0 && scancode < kKeyScancodes)
return g_pressedKeys[scancode];

View File

@ -34,11 +34,6 @@ void error_message(const char* msg)
// TODO
}
void clear_keyboard_buffer()
{
// Do nothing
}
} // namespace she
extern int app_main(int argc, char* argv[]);

View File

@ -21,7 +21,8 @@
#elif __APPLE__
#include "she/osx/app.h"
#include "she/osx/event_queue.h"
#define SkiaSystemBase CommonSystem
#include "she/osx/system.h"
#define SkiaSystemBase OSXSystem
#else
#include "she/x11/event_queue.h"
#define SkiaSystemBase CommonSystem

View File

@ -10,6 +10,7 @@
#include "gfx/fwd.h"
#include "she/capabilities.h"
#include "she/keys.h"
#include <stdexcept>
@ -50,10 +51,25 @@ namespace she {
virtual Font* loadSpriteSheetFont(const char* filename, int scale = 1) = 0;
virtual Font* loadTrueTypeFont(const char* filename, int height) = 0;
// Returns true if the the given scancode key is pressed/actived.
virtual bool isKeyPressed(KeyScancode scancode) = 0;
// Returns the active pressed modifiers.
virtual KeyModifiers keyModifiers() = 0;
// Returns the latest unicode character that activated the given
// scancode.
virtual int getUnicodeFromScancode(KeyScancode scancode) = 0;
// Clears the keyboard buffer (used only in the Allegro port).
// TODO (deprecated)
virtual void clearKeyboardBuffer() = 0;
// Indicates if you want to use dead keys or not. By default it's
// false, which behaves as regular shortcuts. You should set this
// to true when you're inside a text field in your app.
virtual void setTranslateDeadKeys(bool state) = 0;
};
System* create_system();

View File

@ -13,6 +13,9 @@
namespace she {
bool win_is_key_pressed(KeyScancode scancode);
int win_get_unicode_from_scancode(KeyScancode scancode);
class WindowSystem : public CommonSystem {
public:
WindowSystem() { }
@ -22,6 +25,14 @@ public:
return m_penApi;
}
bool isKeyPressed(KeyScancode scancode) override {
return win_is_key_pressed(scancode);
}
int getUnicodeFromScancode(KeyScancode scancode) override {
return win_get_unicode_from_scancode(scancode);
}
private:
PenAPI m_penApi;
};

View File

@ -335,9 +335,7 @@ static int scancode_to_win32vk(KeyScancode scancode)
return keymap[scancode];
}
// TODO Move these functions to she::System class
bool is_key_pressed(KeyScancode scancode)
bool win_is_key_pressed(KeyScancode scancode)
{
int vk = scancode_to_win32vk(scancode);
if (vk)
@ -346,7 +344,7 @@ bool is_key_pressed(KeyScancode scancode)
return false;
}
int get_unicode_from_scancode(KeyScancode scancode)
int win_get_unicode_from_scancode(KeyScancode scancode)
{
int vk = scancode_to_win32vk(scancode);
if (vk && (GetAsyncKeyState(vk) & 0x8000 ? true: false)) {

View File

@ -12,9 +12,14 @@
namespace she {
bool is_key_pressed(KeyScancode scancode)
bool x11_is_key_pressed(KeyScancode scancode)
{
return false; // TODO
}
int x11_get_unicode_from_scancode(KeyScancode scancode)
{
return 0; // TODO
}
} // namespace she

31
src/she/x11/system.h Normal file
View File

@ -0,0 +1,31 @@
// SHE library
// Copyright (C) 2016 David Capello
//
// This file is released under the terms of the MIT license.
// Read LICENSE.txt for more information.
#ifndef SHE_X11_SYSTEM_H
#define SHE_X11_SYSTEM_H
#pragma once
#include "she/common/system.h"
namespace she {
bool x11_is_key_pressed(KeyScancode scancode);
int x11_get_unicode_from_scancode(KeyScancode scancode);
class X11System : public CommonSystem {
public:
bool isKeyPressed(KeyScancode scancode) override {
return x11_is_key_pressed(scancode);
}
int getUnicodeFromScancode(KeyScancode scancode) override {
return x11_get_unicode_from_scancode(scancode);
}
};
} // namespace she
#endif

View File

@ -15,6 +15,7 @@
#include "base/split_string.h"
#include "base/string.h"
#include "base/unique_ptr.h"
#include "she/system.h"
#include <cctype>
#include <cstdlib>
@ -141,20 +142,6 @@ const char* scancode_to_string[] = { // Same order that she::KeyScancode
int scancode_to_string_size =
sizeof(scancode_to_string) / sizeof(scancode_to_string[0]);
KeyModifiers get_pressed_modifiers_from_she()
{
KeyModifiers mods = kKeyNoneModifier;
if (she::is_key_pressed(kKeyLShift) ) mods = KeyModifiers(int(mods) | int(kKeyShiftModifier));
if (she::is_key_pressed(kKeyRShift) ) mods = KeyModifiers(int(mods) | int(kKeyShiftModifier));
if (she::is_key_pressed(kKeyLControl)) mods = KeyModifiers(int(mods) | int(kKeyCtrlModifier));
if (she::is_key_pressed(kKeyRControl)) mods = KeyModifiers(int(mods) | int(kKeyCtrlModifier));
if (she::is_key_pressed(kKeyAlt) ) mods = KeyModifiers(int(mods) | int(kKeyAltModifier));
if (she::is_key_pressed(kKeyCommand) ) mods = KeyModifiers(int(mods) | int(kKeyCmdModifier));
if (she::is_key_pressed(kKeyLWin) ) mods = KeyModifiers(int(mods) | int(kKeyWinModifier));
if (she::is_key_pressed(kKeyRWin) ) mods = KeyModifiers(int(mods) | int(kKeyWinModifier));
return mods;
}
} // anonymous namespace
Accelerator::Accelerator()
@ -349,7 +336,8 @@ bool Accelerator::isPressed(KeyModifiers modifiers, KeyScancode scancode, int un
bool Accelerator::isPressed() const
{
KeyModifiers pressedModifiers = get_pressed_modifiers_from_she();
she::System* sys = she::instance();
KeyModifiers pressedModifiers = sys->keyModifiers();
// Check if this shortcut is only
if (m_scancode == kKeyNil && m_unicodeChar == 0)
@ -357,10 +345,10 @@ bool Accelerator::isPressed() const
// Compare with all pressed scancodes
for (int s=int(kKeyNil); s<int(kKeyFirstModifierScancode); ++s) {
if (she::is_key_pressed(KeyScancode(s)) &&
if (sys->isKeyPressed(KeyScancode(s)) &&
isPressed(pressedModifiers,
KeyScancode(s),
she::get_unicode_from_scancode(KeyScancode(s))))
sys->getUnicodeFromScancode(KeyScancode(s))))
return true;
}
@ -369,7 +357,8 @@ bool Accelerator::isPressed() const
bool Accelerator::isLooselyPressed() const
{
KeyModifiers pressedModifiers = get_pressed_modifiers_from_she();
she::System* sys = she::instance();
KeyModifiers pressedModifiers = sys->keyModifiers();
if ((m_modifiers & pressedModifiers) != m_modifiers)
return false;
@ -380,10 +369,10 @@ bool Accelerator::isLooselyPressed() const
// Compare with all pressed scancodes
for (int s=int(kKeyNil); s<int(kKeyFirstModifierScancode); ++s) {
if (she::is_key_pressed(KeyScancode(s)) &&
if (sys->isKeyPressed(KeyScancode(s)) &&
isPressed(m_modifiers, // Use same modifiers (we've already compared the modifiers)
KeyScancode(s),
she::get_unicode_from_scancode(KeyScancode(s))))
sys->getUnicodeFromScancode(KeyScancode(s))))
return true;
}

View File

@ -23,21 +23,10 @@ Message::Message(MessageType type, KeyModifiers modifiers)
: m_type(type)
, m_used(false)
{
if (modifiers == kKeyUninitializedModifier) {
// Get modifiers from the deprecated API
// TODO remove this
m_modifiers = (KeyModifiers)
((she::is_key_pressed(kKeyLShift) || she::is_key_pressed(kKeyRShift) ? kKeyShiftModifier: 0) |
(she::is_key_pressed(kKeyLControl) || she::is_key_pressed(kKeyRControl) ? kKeyCtrlModifier: 0) |
(she::is_key_pressed(kKeyAlt) ? kKeyAltModifier: 0) |
(she::is_key_pressed(kKeyAltGr) ? (kKeyCtrlModifier | kKeyAltModifier): 0) |
(she::is_key_pressed(kKeyCommand) ? kKeyCmdModifier: 0) |
(she::is_key_pressed(kKeySpace) ? kKeySpaceModifier: 0) |
(she::is_key_pressed(kKeyLWin) || she::is_key_pressed(kKeyRWin) ? kKeyWinModifier: 0));
}
else {
if (modifiers == kKeyUninitializedModifier)
m_modifiers = she::instance()->keyModifiers();
else
m_modifiers = modifiers;
}
}
Message::~Message()