* Handle text input properly from SDL

* Removed duplicate keyboard.cpp
This commit is contained in:
Ben Lankamp
2020-06-16 15:11:17 +02:00
parent 2007adbda3
commit 7091282433
5 changed files with 45 additions and 500 deletions
+12 -10
View File
@@ -376,6 +376,8 @@ void EditClass::Draw_Text(char const * text)
*=============================================================================================*/
bool EditClass::Handle_Key(KeyASCIIType ascii)
{
unsigned char testAscii = (unsigned char)ascii;
switch (ascii) {
/*
** Handle the special case of a non-keyboard event. It is possible that this
@@ -416,7 +418,7 @@ bool EditClass::Handle_Key(KeyASCIIType ascii)
/*
** Don't add a character if the length is greater than edit width.
*/
if (((int)String_Pixel_Width(String) + (int)Char_Pixel_Width(ascii) ) >= (Width-2)) {
if (((int)String_Pixel_Width(String) + (int)Char_Pixel_Width(testAscii) ) >= (Width-2)) {
break;
}
@@ -429,21 +431,21 @@ bool EditClass::Handle_Key(KeyASCIIType ascii)
** Invisible characters are never added to the string. This is
** especially true for spaces at the beginning of the string.
*/
if (!isgraph(ascii) && ascii != ' ') break;
if (ascii == ' ' && Length == 0) break;
if (!isgraph(testAscii) && testAscii != ' ') break;
if (testAscii == ' ' && Length == 0) break;
/*
** If this is an upper case only edit gadget, then force the alphabetic
** character to upper case.
*/
if ((EditFlags & UPPERCASE) && isalpha(ascii)) {
ascii = (KeyASCIIType)toupper(ascii);
if ((EditFlags & UPPERCASE) && isalpha(testAscii)) {
testAscii = (KeyASCIIType)toupper(testAscii);
}
if ((!(EditFlags & NUMERIC) || !isdigit(ascii)) &&
(!(EditFlags & ALPHA) || !isalpha(ascii)) &&
(!(EditFlags & MISC) || isalnum(ascii)) &&
ascii != ' ') {
if ((!(EditFlags & NUMERIC) || !isdigit(testAscii)) &&
(!(EditFlags & ALPHA) || !isalpha(testAscii)) &&
(!(EditFlags & MISC) || isalnum(testAscii)) &&
testAscii != ' ') {
break;
}
@@ -453,7 +455,7 @@ bool EditClass::Handle_Key(KeyASCIIType ascii)
** because the event flag has been cleared. This prevents the gadget's ID
** number from being returned just because the gadget has been edited.
*/
String[Length++] = ascii;
String[Length++] = testAscii;
String[Length] = '\0';
Flag_To_Redraw();
break;
+2 -1
View File
@@ -192,7 +192,6 @@ bool WWKeyboardClass::Put(unsigned short key)
return(false);
}
/***********************************************************************************************
* WWKeyboardClass::Put_Key_Message -- Translates and inserts wParam into Keyboard Buffer *
* *
@@ -517,6 +516,7 @@ bool WWKeyboardClass::Is_Buffer_Empty(void) const
*=============================================================================================*/
void WWKeyboardClass::Fill_Buffer_From_System(void)
{
/*
if (!Is_Buffer_Full())
{
MSG msg;
@@ -528,6 +528,7 @@ void WWKeyboardClass::Fill_Buffer_From_System(void)
DispatchMessage(&msg);
}
}
*/
}
+1 -14
View File
@@ -33,10 +33,6 @@
#ifndef KEYBOARD_H
#define KEYBOARD_H
#ifndef WIN32
#include "..\wwflat32\include\keyboard.h"
#else
#include <windows.h>
/*
@@ -44,18 +40,10 @@
** November of '94. Until the compiler supports this, use the following
** definition.
*/
#ifndef __BORLANDC__
#ifndef TRUE_FALSE_DEFINED
#define TRUE_FALSE_DEFINED
enum {false=0,true=1};
typedef int bool;
#endif
#endif
#pragma warning(disable : 4005)
typedef enum {
WWKEY_SHIFT_BIT = 0x100,
WWKEY_SHIFT_BIT = 0x100,
WWKEY_CTRL_BIT = 0x200,
WWKEY_ALT_BIT = 0x400,
WWKEY_RLS_BIT = 0x800,
@@ -656,4 +644,3 @@ typedef enum KeyNumType {
//extern WWKeyboardClass *_Kbd;
#endif
#endif
-466
View File
@@ -1,466 +0,0 @@
//
// Copyright 2020 Electronic Arts Inc.
//
// TiberianDawn.DLL and RedAlert.dll and corresponding source code is free
// software: you can redistribute it and/or modify it under the terms of
// the GNU General Public License as published by the Free Software Foundation,
// either version 3 of the License, or (at your option) any later version.
// TiberianDawn.DLL and RedAlert.dll and corresponding source code is distributed
// in the hope that it will be useful, but with permitted additional restrictions
// under Section 7 of the GPL. See the GNU General Public License in LICENSE.TXT
// distributed with this program. You should have received a copy of the
// GNU General Public License along with permitted additional restrictions
// with this program. If not, see https://github.com/electronicarts/CnC_Remastered_Collection
/* $Header: F:\projects\c&c0\vcs\code\conquer.cpv 4.74 23 Sep 1996 12:36:00 JOE_BOSTIC $ */
/***********************************************************************************************
* *
* Project Name : Westwood Keyboard Library *
* *
* File Name : KEYBOARD.CPP *
* *
* Programmer : Philip W. Gorrow *
* *
* Start Date : 10/16/95 *
* *
* Last Update : September 24, 1996 [JLB] *
* *
*---------------------------------------------------------------------------------------------*
* Functions: *
* WWKeyboardClass::Put -- Logic to insert a key into the keybuffer] *
* WWKeyboardClass::Get -- Logic to get a metakey from the buffer *
* WWKeyboardClass::Check -- Checks to see if a key is in the buffer *
* WWKeyboardClass::Put_Key_Message -- Translates and inserts wParam into Keyboard Buffer *
* WWKeyboardClass::Buff_Get -- Lowlevel function to get a key from key buffer *
* WWKeyboardClass::Get_Mouse_X -- Returns the mouses current x position in pixels *
* WWKeyboardClass::Get_Mouse_Y -- returns the mouses current y position in pixels *
* WWKeyboardClass::Get_Mouse_XY -- Returns the mouses x,y position via reference vars *
* Check_Key -- compatability routine for old 32 bit library *
* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
#include "keyboard.h"
#include "monoc.h"
//void Message_Loop(void);
WWKeyboardClass * _Kbd = NULL;
#define ARRAY_SIZE(x) int(sizeof(x)/sizeof(x[0]))
/***********************************************************************************************
* WWKeyboardClass::WWKeyBoardClass -- Construction for Westwood Keyboard Class *
* *
* INPUT: none *
* *
* OUTPUT: none *
* *
* HISTORY: *
* 10/16/1995 PWG : Created. *
*=============================================================================================*/
WWKeyboardClass::WWKeyboardClass(void) :
MouseQX(0),
MouseQY(0),
Head(0),
Tail(0)
// MState(0),
// Conditional(0),
// CurrentCursor(0)
{
_Kbd = this;
memset(KeyState, '\0', sizeof(KeyState));
}
/***********************************************************************************************
* WWKeyboardClass::Buff_Get -- Lowlevel function to get a key from key buffer *
* *
* INPUT: none *
* *
* OUTPUT: int - the key value that was pulled from buffer (includes bits) * *
* *
* WARNINGS: If the key was a mouse event MouseQX and MouseQY will be updated *
* *
* HISTORY: *
* 10/17/1995 PWG : Created. *
*=============================================================================================*/
unsigned short WWKeyboardClass::Buff_Get(void)
{
while (!Check()) {} // wait for key in buffer
unsigned short temp = Fetch_Element();
if (Is_Mouse_Key(temp)) {
MouseQX = Fetch_Element();
MouseQY = Fetch_Element();
}
return(temp);
}
bool WWKeyboardClass::Is_Mouse_Key(unsigned short key)
{
key &= 0xFF;
return (key == VK_LBUTTON || key == VK_MBUTTON || key == VK_RBUTTON);
}
/***********************************************************************************************
* WWKeyboardClass::Check -- Checks to see if a key is in the buffer *
* *
* INPUT: *
* *
* OUTPUT: *
* *
* WARNINGS: *
* *
* HISTORY: *
* 10/16/1995 PWG : Created. *
* 09/24/1996 JLB : Converted to new style keyboard system. *
*=============================================================================================*/
unsigned short WWKeyboardClass::Check(void) const
{
((WWKeyboardClass *)this)->Fill_Buffer_From_System();
if (Is_Buffer_Empty()) return(false);
return(Peek_Element());
}
/***********************************************************************************************
* WWKeyboardClass::Get -- Logic to get a metakey from the buffer *
* *
* INPUT: none *
* *
* OUTPUT: int - the meta key taken from the buffer. *
* *
* WARNINGS: This routine will not return until a keypress is received *
* *
* HISTORY: *
* 10/16/1995 PWG : Created. *
*=============================================================================================*/
unsigned short WWKeyboardClass::Get(void)
{
while (!Check()) {} // wait for key in buffer
return (Buff_Get());
}
/***********************************************************************************************
* WWKeyboardClass::Put -- Logic to insert a key into the keybuffer] *
* *
* INPUT: int - the key to insert into the buffer *
* *
* OUTPUT: bool - true if key is sucessfuly inserted. *
* *
* WARNINGS: none *
* *
* HISTORY: *
* 10/16/1995 PWG : Created. *
*=============================================================================================*/
bool WWKeyboardClass::Put(unsigned short key)
{
if (!Is_Buffer_Full()) {
Put_Element(key);
return(true);
}
return(false);
}
/***********************************************************************************************
* WWKeyboardClass::Put_Key_Message -- Translates and inserts wParam into Keyboard Buffer *
* *
* INPUT: *
* *
* OUTPUT: *
* *
* WARNINGS: *
* *
* HISTORY: *
* 10/16/1995 PWG : Created. *
*=============================================================================================*/
bool WWKeyboardClass::Put_Key_Message(unsigned short vk_key, bool release)
{
//
// Get the status of all of the different keyboard modifiers. Note, only pay attention
// to numlock and caps lock if we are dealing with a key that is affected by them. Note
// that we do not want to set the shift, ctrl and alt bits for Mouse keypresses as this
// would be incompatible with the dos version.
//
if (!Is_Mouse_Key(vk_key)) {
if (((GetKeyState(VK_SHIFT) & 0x8000) != 0) ||
((GetKeyState(VK_CAPITAL) & 0x0008) != 0) ||
((GetKeyState(VK_NUMLOCK) & 0x0008) != 0)) {
vk_key |= WWKEY_SHIFT_BIT;
}
if ((GetKeyState(VK_CONTROL) & 0x8000) != 0) {
vk_key |= WWKEY_CTRL_BIT;
}
if ((GetKeyState(VK_MENU) & 0x8000) != 0) {
vk_key |= WWKEY_ALT_BIT;
}
}
if (release) {
vk_key |= WWKEY_RLS_BIT;
}
//
// Finally use the put command to enter the key into the keyboard
// system.
//
return(Put(vk_key));
}
#pragma warning (disable : 4065)
char WWKeyboardClass::To_ASCII(unsigned short key)
{
/*
** Released keys never translate into an ASCII value.
*/
if (key & WWKEY_RLS_BIT) {
return('\0');
}
/*
** Set the KeyState buffer to reflect the shift bits stored in the key value.
*/
if (key & WWKEY_SHIFT_BIT) {
KeyState[VK_SHIFT] = 0x80;
}
if (key & WWKEY_CTRL_BIT) {
KeyState[VK_CONTROL] = 0x80;
}
if (key & WWKEY_ALT_BIT) {
KeyState[VK_MENU] = 0x80;
}
/*
** Ask windows to translate the key into an ASCII equivalent.
*/
char buffer[10];
int result = 1;
int scancode = 0;
char override = '\0';
switch (key & 0xFF) {
// case KN_RETURN:
// override = KA_RETURN;
// break;
// case KN_BACKSPACE:
// override = KA_BACKSPACE;
// break;
default:
scancode = MapVirtualKey(key & 0xFF, 0);
result = ToAscii((UINT)(key & 0xFF), (UINT)scancode, (PBYTE)KeyState, (LPWORD)buffer, (UINT)0);
break;
}
/*
** Restore the KeyState buffer back to pristine condition.
*/
if (key & WWKEY_SHIFT_BIT) {
KeyState[VK_SHIFT] = 0;
}
if (key & WWKEY_CTRL_BIT) {
KeyState[VK_CONTROL] = 0;
}
if (key & WWKEY_ALT_BIT) {
KeyState[VK_MENU] = 0;
}
/*
** If Windows could not perform the translation as expected, then
** return with a null ASCII value.
*/
if (result != 1) {
return('\0');
}
if (override != 0) {
return(override);
}
return(buffer[0]);
}
bool WWKeyboardClass::Down(unsigned short key)
{
return(GetAsyncKeyState(key & 0xFF) == 0 ? false : true);
}
extern "C" {
void __cdecl Stop_Execution (void);
}
unsigned short WWKeyboardClass::Fetch_Element(void)
{
unsigned short val = 0;
if (Head != Tail) {
val = Buffer[Head];
Head = (Head + 1) % ARRAY_SIZE(Buffer);
}
return(val);
}
unsigned short WWKeyboardClass::Peek_Element(void) const
{
if (!Is_Buffer_Empty()) {
return(Buffer[Head]);
}
return(0);
}
bool WWKeyboardClass::Put_Element(unsigned short val)
{
if (!Is_Buffer_Full()) {
int temp = (Tail+1) % ARRAY_SIZE(Buffer);
Buffer[Tail] = val;
Tail = temp;
return(true);
}
return(false);
}
bool WWKeyboardClass::Is_Buffer_Full(void) const
{
if ((Tail + 1) % ARRAY_SIZE(Buffer) == Head) {
return(true);
}
return(false);
}
bool WWKeyboardClass::Is_Buffer_Empty(void) const
{
if (Head == Tail) {
return(true);
}
return(false);
}
void WWKeyboardClass::Fill_Buffer_From_System(void)
{
if (!Is_Buffer_Full()) {
MSG msg;
while (PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE)) {
if (!GetMessage( &msg, NULL, 0, 0 )) {
return;
}
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
}
void WWKeyboardClass::Clear(void)
{
Head = Tail;
}
void WWKeyboardClass::Message_Handler(HWND , UINT message, UINT wParam, LONG lParam)
{
switch (message) {
case WM_SYSKEYDOWN:
case WM_KEYDOWN:
if ( wParam==VK_SCROLL ){
Stop_Execution();
} else {
Put_Key_Message((unsigned short)wParam);
}
break;
case WM_SYSKEYUP:
case WM_KEYUP:
Put_Key_Message((unsigned short)wParam, true);
break;
case WM_LBUTTONDOWN:
Put_Key_Message(VK_LBUTTON);
Put(LOWORD(lParam));
Put(HIWORD(lParam));
break;
case WM_LBUTTONUP:
Put_Key_Message(VK_LBUTTON, true);
Put(LOWORD(lParam));
Put(HIWORD(lParam));
break;
case WM_LBUTTONDBLCLK:
Put_Key_Message(VK_LBUTTON, true);
Put(LOWORD(lParam));
Put(HIWORD(lParam));
Put_Key_Message(VK_LBUTTON, true);
Put(LOWORD(lParam));
Put(HIWORD(lParam));
break;
case WM_MBUTTONDOWN:
Put_Key_Message(VK_MBUTTON);
Put(LOWORD(lParam));
Put(HIWORD(lParam));
break;
case WM_MBUTTONUP:
Put_Key_Message(VK_MBUTTON, true);
Put(LOWORD(lParam));
Put(HIWORD(lParam));
break;
case WM_MBUTTONDBLCLK:
Put_Key_Message(VK_MBUTTON, true);
Put(LOWORD(lParam));
Put(HIWORD(lParam));
Put_Key_Message(VK_MBUTTON, true);
Put(LOWORD(lParam));
Put(HIWORD(lParam));
break;
case WM_RBUTTONDOWN:
Put_Key_Message(VK_RBUTTON);
Put(LOWORD(lParam));
Put(HIWORD(lParam));
break;
case WM_RBUTTONUP:
Put_Key_Message(VK_RBUTTON, true);
Put(LOWORD(lParam));
Put(HIWORD(lParam));
break;
case WM_RBUTTONDBLCLK:
Put_Key_Message(VK_RBUTTON, true);
Put(LOWORD(lParam));
Put(HIWORD(lParam));
Put_Key_Message(VK_RBUTTON, true);
Put(LOWORD(lParam));
Put(HIWORD(lParam));
break;
// case WM_MOUSEMOVE:
// if (CurrentCursor)
// SetCursor(CurrentCursor);
// break;
}
}
+30 -9
View File
@@ -60,6 +60,8 @@
#include <imgui.h>
#include <examples/imgui_impl_opengl3.h>
#include <string>
#include <algorithm>
GLuint backbuffer_texture = -1;
byte* backbuffer_data;
@@ -86,6 +88,16 @@ void Show_OldFrameBuffer(bool show) {
std::vector<Image_t *> loaded_images;
auto utf8_decode = [](const std::string& str) -> std::wstring
{
if (str.empty()) return std::wstring();
int size_needed = MultiByteToWideChar(CP_UTF8, 0, &str[0], (int)str.size(), NULL, 0);
std::wstring wstrTo(size_needed, 0);
MultiByteToWideChar(CP_UTF8, 0, &str[0], (int)str.size(), &wstrTo[0], size_needed);
return wstrTo;
};
Image_t* Image_LoadImage(const char* name) {
// Check to see if the image is already loaded.
for (int i = 0; i < loaded_images.size(); i++) {
@@ -679,7 +691,10 @@ void WWSDL_ProcessEvents(KeyNumType& key, int& flags) {
// flags = 0;
SDL_Event event;
while (SDL_PollEvent(&event)) {
const Uint8* state = SDL_GetKeyboardState(NULL);
switch (event.type)
{
case SDL_SYSWMEVENT:
@@ -753,16 +768,24 @@ void WWSDL_ProcessEvents(KeyNumType& key, int& flags) {
}
break;
}
// handle backspace and ENTER
if (event.key.keysym.scancode == SDL_SCANCODE_BACKSPACE) Keyboard->Put_Element( KN_BACKSPACE );
if (event.key.keysym.scancode == ( SDL_SCANCODE_RETURN || SDL_SCANCODE_KP_ENTER ) ) Keyboard->Put_Element( KN_RETURN );
const Uint8* state = SDL_GetKeyboardState(NULL);
break;
case SDL_TEXTINPUT:
int keyFlags = 0x00;
if (state[SDL_SCANCODE_RALT] || state[SDL_SCANCODE_LALT])
if (state[SDL_SCANCODE_RSHIFT] || state[SDL_SCANCODE_LSHIFT]) keyFlags |= KN_SHIFT_BIT;
if (state[SDL_SCANCODE_RALT] || state[SDL_SCANCODE_LALT]) keyFlags |= KN_ALT_BIT;
std::wstring input_utf16 = utf8_decode( std::string( event.text.text ) );
if( input_utf16.length() == 1 )
{
Keyboard->Put_Element(event.key.keysym.sym | KN_ALT_BIT);
}
else
{
Keyboard->Put_Element(event.key.keysym.sym);
short vKey = VkKeyScanW( input_utf16[0] );
Keyboard->Put_Element( vKey | keyFlags );
}
break;
@@ -783,8 +806,6 @@ void WWSDL_ProcessEvents(KeyNumType& key, int& flags) {
Device_Present();
}
#ifndef NDEBUG
/***********************************************************************************************
* Assert_Failure -- display the line and source file where a failed assert occurred *