upgrade to SDL 2

Still currently in process, there are some issues:
Windows version doesn't work, mac version might not work, opengl might not work
Icon doesn't work (on Linux at least)
Lua will need some changes, there are some sdl 1.2 hacks in there
When entering fullscreen, the window loses focus
When holding down mouse out of bounds, mouse move events stop being sent
When letting go of mouse out of bounds, mouseup event doesn't take into account double scale mode
Clicking on startup without moving mouse will draw at 0,0 for a frame
Renderer probably won't compile because USE_SDL doesn't entirely work

... and maybe others

Some nice things were done though:
no more blit2, sdl can do the scaling itself
3d effect removed, no reason to support this joke any longer
No need to support copy/paste ourselves, sdl does it now
text handling done much better now, separate events for key presses and text input
when a new window is shown, all events ignored until next tick (ignore textinput event if window shown from key press event like console)
This commit is contained in:
jacob1 2018-04-15 18:53:02 -04:00
parent 38fac7046a
commit e5230b5b9f
55 changed files with 367 additions and 932 deletions

View File

@ -245,24 +245,24 @@ def findLibs(env, conf):
if not GetOption('renderer'):
#Look for SDL
runSdlConfig = platform == "Linux" or compilePlatform == "Linux" or platform == "FreeBSD"
if platform == "Darwin" and conf.CheckFramework("SDL"):
if False and platform == "Darwin" and conf.CheckFramework("SDL"):
runSdlConfig = False
elif not conf.CheckLib("SDL"):
elif not conf.CheckLib("SDL2"):
FatalError("SDL development library not found or not installed")
if runSdlConfig:
try:
env.ParseConfig('sdl-config --cflags')
env.ParseConfig('sdl2-config --cflags')
if GetOption('static'):
env.ParseConfig('sdl-config --static-libs')
env.ParseConfig('sdl2-config --static-libs')
else:
env.ParseConfig('sdl-config --libs')
env.ParseConfig('sdl2-config --libs')
except:
pass
#look for SDL.h
if not GetOption('renderer') and not conf.CheckCHeader('SDL.h'):
if conf.CheckCHeader('SDL/SDL.h'):
if not GetOption('renderer') and not conf.CheckCHeader('SDL2.h'):
if conf.CheckCHeader('SDL2/SDL.h'):
env.Append(CPPDEFINES=["SDL_INC"])
else:
FatalError("SDL.h not found")
@ -555,8 +555,8 @@ if platform == "Windows":
envCopy = env.Clone()
envCopy.Append(CCFLAGS='-mstackrealign')
sources += envCopy.Object('src/simulation/Gravity.cpp')
elif platform == "Darwin":
sources += ["src/SDLMain.m"]
#elif platform == "Darwin":
# sources += ["src/SDLMain.m"]
#Program output name

View File

@ -5,5 +5,4 @@ void ClipboardPush(ByteString text);
ByteString ClipboardPull();
int GetModifiers();
bool LoadWindowPosition(int scale);
void SetCursorEnabled(int enabled);
unsigned int GetTicks();

View File

@ -29,13 +29,6 @@
#ifndef WIN
#include <unistd.h>
#endif
#ifdef MACOSX
#include <ApplicationServices/ApplicationServices.h>
extern "C" {
char * readClipboard();
void writeClipboard(const char * clipboardData);
}
#endif
#include "Format.h"
@ -66,125 +59,22 @@ SDL_SysWMinfo sdl_wminfo;
Atom XA_CLIPBOARD, XA_TARGETS, XA_UTF8_STRING;
#endif
ByteString clipboardText = "";
int desktopWidth = 1280, desktopHeight = 1024;
SDL_Surface * sdl_scrn;
SDL_Window * sdl_window;
SDL_Renderer * sdl_renderer;
SDL_Texture * sdl_texture;
int scale = 1;
bool fullscreen = false;
void ClipboardPush(ByteString text)
{
clipboardText = text;
#ifdef MACOSX
writeClipboard(text.c_str());
#elif defined(WIN)
if (OpenClipboard(NULL))
{
HGLOBAL cbuffer;
char * glbuffer;
EmptyClipboard();
cbuffer = GlobalAlloc(GMEM_DDESHARE, text.size() + 1);
glbuffer = (char*)GlobalLock(cbuffer);
strcpy(glbuffer, text.c_str());
GlobalUnlock(cbuffer);
SetClipboardData(CF_TEXT, cbuffer);
CloseClipboard();
}
#elif defined(LIN) && defined(SDL_VIDEO_DRIVER_X11)
sdl_wminfo.info.x11.lock_func();
XSetSelectionOwner(sdl_wminfo.info.x11.display, XA_CLIPBOARD, sdl_wminfo.info.x11.window, CurrentTime);
XFlush(sdl_wminfo.info.x11.display);
sdl_wminfo.info.x11.unlock_func();
#else
printf("Not implemented: put text on clipboard \"%s\"\n", text.c_str());
#endif
SDL_SetClipboardText(text.c_str());
}
void EventProcess(SDL_Event event);
ByteString ClipboardPull()
{
#ifdef MACOSX
const char *text = readClipboard();
return text ? ByteString(text) : "";
#elif defined(WIN)
if (OpenClipboard(NULL))
{
HANDLE cbuffer;
char * glbuffer;
cbuffer = GetClipboardData(CF_TEXT);
glbuffer = (char*)GlobalLock(cbuffer);
GlobalUnlock(cbuffer);
CloseClipboard();
return glbuffer ? ByteString(glbuffer) : "";
}
#elif defined(LIN) && defined(SDL_VIDEO_DRIVER_X11)
ByteString text = "";
Window selectionOwner;
sdl_wminfo.info.x11.lock_func();
selectionOwner = XGetSelectionOwner(sdl_wminfo.info.x11.display, XA_CLIPBOARD);
if (selectionOwner != None)
{
unsigned char *data = NULL;
Atom type;
int format, result;
unsigned long len, bytesLeft;
XConvertSelection(sdl_wminfo.info.x11.display, XA_CLIPBOARD, XA_UTF8_STRING, XA_CLIPBOARD, sdl_wminfo.info.x11.window, CurrentTime);
XFlush(sdl_wminfo.info.x11.display);
sdl_wminfo.info.x11.unlock_func();
while (1)
{
SDL_Event event;
SDL_WaitEvent(&event);
if (event.type == SDL_SYSWMEVENT)
{
XEvent xevent = event.syswm.msg->event.xevent;
if (xevent.type == SelectionNotify && xevent.xselection.requestor == sdl_wminfo.info.x11.window)
break;
else
EventProcess(event);
}
else
EventProcess(event);
}
sdl_wminfo.info.x11.lock_func();
XGetWindowProperty(sdl_wminfo.info.x11.display, sdl_wminfo.info.x11.window, XA_CLIPBOARD, 0, 0, 0, AnyPropertyType, &type, &format, &len, &bytesLeft, &data);
if (data)
{
XFree(data);
data = NULL;
}
if (bytesLeft)
{
result = XGetWindowProperty(sdl_wminfo.info.x11.display, sdl_wminfo.info.x11.window, XA_CLIPBOARD, 0, bytesLeft, 0, AnyPropertyType, &type, &format, &len, &bytesLeft, &data);
if (result == Success)
{
text = data ? ByteString((char const *)data) : "";
XFree(data);
}
else
{
printf("Failed to pull from clipboard\n");
return "?";
}
}
else
return "";
XDeleteProperty(sdl_wminfo.info.x11.display, sdl_wminfo.info.x11.window, XA_CLIPBOARD);
}
sdl_wminfo.info.x11.unlock_func();
return text;
#else
printf("Not implemented: get text from clipboard\n");
#endif
return clipboardText;
return ByteString(SDL_GetClipboardText());
}
int mousex = 0, mousey = 0;
@ -194,259 +84,12 @@ void blit()
SDL_GL_SwapBuffers();
}
#else
void DrawPixel(pixel * vid, pixel color, int x, int y)
{
if (x >= 0 && x < WINDOWW && y >= 0 && y < WINDOWH)
vid[x+y*WINDOWW] = color;
}
// draws a custom cursor, used to make 3D mode work properly (normal cursor ruins the effect)
void DrawCursor(pixel * vid)
{
for (int j = 0; j <= 9; j++)
{
for (int i = 0; i <= j; i++)
{
if (i == 0 || i == j)
DrawPixel(vid, 0xFFFFFFFF, mousex+i, mousey+j);
else
DrawPixel(vid, 0xFF000000, mousex+i, mousey+j);
}
}
DrawPixel(vid, 0xFFFFFFFF, mousex, mousey+10);
for (int i = 0; i < 5; i++)
{
DrawPixel(vid, 0xFF000000, mousex+1+i, mousey+10);
DrawPixel(vid, 0xFFFFFFFF, mousex+6+i, mousey+10);
}
DrawPixel(vid, 0xFFFFFFFF, mousex, mousey+11);
DrawPixel(vid, 0xFF000000, mousex+1, mousey+11);
DrawPixel(vid, 0xFF000000, mousex+2, mousey+11);
DrawPixel(vid, 0xFFFFFFFF, mousex+3, mousey+11);
DrawPixel(vid, 0xFF000000, mousex+4, mousey+11);
DrawPixel(vid, 0xFF000000, mousex+5, mousey+11);
DrawPixel(vid, 0xFFFFFFFF, mousex+6, mousey+11);
DrawPixel(vid, 0xFFFFFFFF, mousex, mousey+12);
DrawPixel(vid, 0xFF000000, mousex+1, mousey+12);
DrawPixel(vid, 0xFFFFFFFF, mousex+2, mousey+12);
DrawPixel(vid, 0xFFFFFFFF, mousex+4, mousey+12);
DrawPixel(vid, 0xFF000000, mousex+5, mousey+12);
DrawPixel(vid, 0xFF000000, mousex+6, mousey+12);
DrawPixel(vid, 0xFFFFFFFF, mousex+7, mousey+12);
DrawPixel(vid, 0xFFFFFFFF, mousex, mousey+13);
DrawPixel(vid, 0xFFFFFFFF, mousex+1, mousey+13);
DrawPixel(vid, 0xFFFFFFFF, mousex+4, mousey+13);
DrawPixel(vid, 0xFF000000, mousex+5, mousey+13);
DrawPixel(vid, 0xFF000000, mousex+6, mousey+13);
DrawPixel(vid, 0xFFFFFFFF, mousex+7, mousey+13);
DrawPixel(vid, 0xFFFFFFFF, mousex, mousey+14);
for (int i = 0; i < 2; i++)
{
DrawPixel(vid, 0xFFFFFFFF, mousex+5, mousey+14+i);
DrawPixel(vid, 0xFF000000, mousex+6, mousey+14+i);
DrawPixel(vid, 0xFF000000, mousex+7, mousey+14+i);
DrawPixel(vid, 0xFFFFFFFF, mousex+8, mousey+14+i);
DrawPixel(vid, 0xFFFFFFFF, mousex+6, mousey+16+i);
DrawPixel(vid, 0xFF000000, mousex+7, mousey+16+i);
DrawPixel(vid, 0xFF000000, mousex+8, mousey+16+i);
DrawPixel(vid, 0xFFFFFFFF, mousex+9, mousey+16+i);
}
DrawPixel(vid, 0xFFFFFFFF, mousex+7, mousey+18);
DrawPixel(vid, 0xFFFFFFFF, mousex+8, mousey+18);
}
void blit(pixel * vid)
{
if (sdl_scrn)
{
int depth3d = ui::Engine::Ref().Get3dDepth();
if (depth3d)
DrawCursor(vid);
pixel * src = vid;
int j, x = 0, y = 0, w = WINDOWW, h = WINDOWH, pitch = WINDOWW;
pixel *dst;
if (SDL_MUSTLOCK(sdl_scrn))
if (SDL_LockSurface(sdl_scrn)<0)
return;
dst=(pixel *)sdl_scrn->pixels+y*sdl_scrn->pitch/PIXELSIZE+x;
if (SDL_MapRGB(sdl_scrn->format,0x33,0x55,0x77)!=PIXPACK(0x335577))
{
//pixel format conversion, used for strange formats (OS X specifically)
int i;
unsigned int red, green, blue;
pixel px, lastpx, nextpx;
SDL_PixelFormat *fmt = sdl_scrn->format;
if(depth3d)
{
for (j=0; j<h; j++)
{
for (i=0; i<w; i++)
{
lastpx = i >= depth3d && i < w+depth3d ? src[i-depth3d] : 0;
nextpx = i >= -depth3d && i < w-depth3d ? src[i+depth3d] : 0;
int redshift = PIXB(lastpx) + PIXG(lastpx);
if (redshift > 255)
redshift = 255;
int blueshift = PIXR(nextpx) + PIXG(nextpx);
if (blueshift > 255)
blueshift = 255;
red = ((int)(PIXR(lastpx)*.69f+redshift*.3f)>>fmt->Rloss)<<fmt->Rshift;
green = ((int)(PIXG(nextpx)*.3f)>>fmt->Gloss)<<fmt->Gshift;
blue = ((int)(PIXB(nextpx)*.69f+blueshift*.3f)>>fmt->Bloss)<<fmt->Bshift;
dst[i] = red|green|blue;
}
dst+=sdl_scrn->pitch/PIXELSIZE;
src+=pitch;
}
}
else
{
for (j=0; j<h; j++)
{
for (i=0; i<w; i++)
{
px = src[i];
red = (PIXR(px)>>fmt->Rloss)<<fmt->Rshift;
green = (PIXG(px)>>fmt->Gloss)<<fmt->Gshift;
blue = (PIXB(px)>>fmt->Bloss)<<fmt->Bshift;
dst[i] = red|green|blue;
}
dst+=sdl_scrn->pitch/PIXELSIZE;
src+=pitch;
}
}
}
else
{
int i;
if(depth3d)
{
pixel lastpx, nextpx;
for (j=0; j<h; j++)
{
for (i=0; i<w; i++)
{
lastpx = i >= depth3d && i < w+depth3d ? src[i-depth3d] : 0;
nextpx = i >= -depth3d && i < w-depth3d ? src[i+depth3d] : 0;
int redshift = PIXB(lastpx) + PIXG(lastpx);
if (redshift > 255)
redshift = 255;
int blueshift = PIXR(nextpx) + PIXG(nextpx);
if (blueshift > 255)
blueshift = 255;
dst[i] = PIXRGB((int)(PIXR(lastpx)*.69f+redshift*.3f), (int)(PIXG(nextpx)*.3f), (int)(PIXB(nextpx)*.69f+blueshift*.3f));
}
dst+=sdl_scrn->pitch/PIXELSIZE;
src+=pitch;
}
}
else
{
for (j=0; j<h; j++)
{
memcpy(dst, src, w*PIXELSIZE);
dst+=sdl_scrn->pitch/PIXELSIZE;
src+=pitch;
}
}
}
if (SDL_MUSTLOCK(sdl_scrn))
SDL_UnlockSurface(sdl_scrn);
SDL_UpdateRect(sdl_scrn,0,0,0,0);
}
}
void blit2(pixel * vid, int currentScale)
{
if (sdl_scrn)
{
int depth3d = ui::Engine::Ref().Get3dDepth();
if (depth3d)
DrawCursor(vid);
pixel * src = vid;
int j, x = 0, y = 0, w = WINDOWW, h = WINDOWH, pitch = WINDOWW;
pixel *dst;
pixel px, lastpx, nextpx;
int i,k,sx;
if (SDL_MUSTLOCK(sdl_scrn))
if (SDL_LockSurface(sdl_scrn)<0)
return;
dst=(pixel *)sdl_scrn->pixels+y*sdl_scrn->pitch/PIXELSIZE+x;
if (SDL_MapRGB(sdl_scrn->format,0x33,0x55,0x77)!=PIXPACK(0x335577))
{
//pixel format conversion
SDL_PixelFormat *fmt = sdl_scrn->format;
int red, green, blue;
for (j=0; j<h; j++)
{
for (k=0; k<currentScale; k++)
{
for (i=0; i<w; i++)
{
if (depth3d)
{
lastpx = i >= depth3d && i < w+depth3d ? src[i-depth3d] : 0;
nextpx = i >= -depth3d && i < w-depth3d ? src[i+depth3d] : 0;
int redshift = PIXB(lastpx) + PIXG(lastpx);
if (redshift > 255)
redshift = 255;
int blueshift = PIXR(nextpx) + PIXG(nextpx);
if (blueshift > 255)
blueshift = 255;
red = ((int)(PIXR(lastpx)*.69f+redshift*.3f)>>fmt->Rloss)<<fmt->Rshift;
green = ((int)(PIXG(nextpx)*.3f)>>fmt->Gloss)<<fmt->Gshift;
blue = ((int)(PIXB(nextpx)*.69f+blueshift*.3f)>>fmt->Bloss)<<fmt->Bshift;
}
else
{
px = src[i];
red = (PIXR(px)>>fmt->Rloss)<<fmt->Rshift;
green = (PIXG(px)>>fmt->Gloss)<<fmt->Gshift;
blue = (PIXB(px)>>fmt->Bloss)<<fmt->Bshift;
}
for (sx=0; sx<currentScale; sx++)
dst[i*currentScale+sx] = red|green|blue;
}
dst+=sdl_scrn->pitch/PIXELSIZE;
}
src+=pitch;
}
}
else
{
for (j=0; j<h; j++)
{
for (k=0; k<currentScale; k++)
{
for (i=0; i<w; i++)
{
px = src[i];
if (depth3d)
{
lastpx = i >= depth3d && i < w+depth3d ? src[i-depth3d] : 0;
nextpx = i >= -depth3d && i < w-depth3d ? src[i+depth3d] : 0;
int redshift = PIXB(lastpx) + PIXG(lastpx);
if (redshift > 255)
redshift = 255;
int blueshift = PIXR(nextpx) + PIXG(nextpx);
if (blueshift > 255)
blueshift = 255;
px = PIXRGB((int)(PIXR(lastpx)*.69f+redshift*.3f), (int)(PIXG(nextpx)*.3f), (int)(PIXB(nextpx)*.69f+blueshift*.3f));
}
for (sx=0; sx<currentScale; sx++)
dst[i*currentScale+sx] = px;
}
dst+=sdl_scrn->pitch/PIXELSIZE;
}
src+=pitch;
}
}
if (SDL_MUSTLOCK(sdl_scrn))
SDL_UnlockSurface(sdl_scrn);
SDL_UpdateRect(sdl_scrn,0,0,0,0);
}
SDL_UpdateTexture(sdl_texture, NULL, vid, WINDOWW * sizeof (Uint32));
SDL_RenderClear(sdl_renderer);
SDL_RenderCopy(sdl_renderer, sdl_texture, NULL, NULL);
SDL_RenderPresent(sdl_renderer);
}
#endif
@ -460,10 +103,12 @@ int SDLOpen()
fprintf(stderr, "Initializing SDL: %s\n", SDL_GetError());
return 1;
}
const SDL_VideoInfo * vidInfo = SDL_GetVideoInfo();
desktopWidth = vidInfo->current_w;
desktopHeight = vidInfo->current_h;
SDL_EnableUNICODE(1);
SDL_DisplayMode SDLDisplayMode;
SDL_GetCurrentDisplayMode(0, &SDLDisplayMode);
desktopWidth = SDLDisplayMode.w;
desktopHeight = SDLDisplayMode.h;
#if defined(WIN) && defined(WINCONSOLE)
//On Windows, SDL redirects stdout to stdout.txt, which can be annoying when debugging, here we redirect back to the console
if (console)
@ -490,33 +135,31 @@ int SDLOpen()
SendMessage(WindowHandle, WM_SETICON, ICON_BIG, (LPARAM)hIconBig);
#elif defined(LIN)
SDL_Surface *icon = SDL_CreateRGBSurfaceFrom((void*)app_icon, 48, 48, 24, 144, 0x00FF0000, 0x0000FF00, 0x000000FF, 0);
SDL_WM_SetIcon(icon, (Uint8*)app_icon_bitmap);
//SDL_WM_SetIcon(icon, (Uint8*)app_icon_bitmap);
SDL_SetWindowIcon(sdl_window, icon);
SDL_FreeSurface(icon);
#endif
SDL_WM_SetCaption("The Powder Toy", "Powder Toy");
//SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
atexit(SDL_Quit);
return 0;
}
SDL_Surface * SDLSetScreen(int newScale, bool newFullscreen)
void SDLSetScreen(int newScale, bool newFullscreen)
{
SDL_DestroyTexture(sdl_texture);
SDL_DestroyRenderer(sdl_renderer);
SDL_DestroyWindow(sdl_window);
scale = newScale;
fullscreen = newFullscreen;
SDL_Surface * surface;
#ifndef OGLI
surface = SDL_SetVideoMode(WINDOWW * newScale, WINDOWH * newScale, 32, SDL_SWSURFACE | (newFullscreen?SDL_FULLSCREEN:0));
#else
surface = SDL_SetVideoMode(WINDOWW * newScale, WINDOWH * newScale, 32, SDL_OPENGL | SDL_RESIZABLE | (newFullscreen?SDL_FULLSCREEN:0));
#endif
return surface;
}
void SetCursorEnabled(int enabled)
{
SDL_ShowCursor(enabled);
sdl_window = SDL_CreateWindow("The Powder Toy", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, WINDOWW * newScale, WINDOWH * newScale,
newFullscreen ? SDL_WINDOW_FULLSCREEN_DESKTOP : 0);
sdl_renderer = SDL_CreateRenderer(sdl_window, -1, 0);
if (newScale > 1)
SDL_RenderSetLogicalSize(sdl_renderer, WINDOWW, WINDOWH);
sdl_texture = SDL_CreateTexture(sdl_renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, WINDOWW, WINDOWH);
//SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "linear");
//SDL_SetWindowResizable(sdl_window, SDL_TRUE);
}
unsigned int GetTicks()
@ -588,7 +231,7 @@ std::map<ByteString, ByteString> readArguments(int argc, char * argv[])
return arguments;
}
SDLKey MapNumpad(SDLKey key)
/*SDLKey MapNumpad(SDLKey key)
{
switch(key)
{
@ -613,18 +256,20 @@ SDLKey MapNumpad(SDLKey key)
default:
return key;
}
}
}*/
int elapsedTime = 0, currentTime = 0, lastTime = 0, currentFrame = 0;
unsigned int lastTick = 0;
float fps = 0, delta = 1.0f, inputScale = 1.0f;
float fps = 0, delta = 1.0f;
float inputScaleH = 1.0f, inputScaleV = 1.0f;
ui::Engine * engine = NULL;
bool showDoubleScreenDialog = false;
float currentWidth, currentHeight;
void EventProcess(SDL_Event event)
{
if (event.type == SDL_KEYDOWN || event.type == SDL_KEYUP)
//inputScale= 1.0f;
/*if (event.type == SDL_KEYDOWN || event.type == SDL_KEYUP)
{
if (event.key.keysym.unicode==0)
{
@ -638,7 +283,7 @@ void EventProcess(SDL_Event event)
event.key.keysym.unicode = 0;
}
}
}
}*/
switch (event.type)
{
case SDL_QUIT:
@ -646,106 +291,61 @@ void EventProcess(SDL_Event event)
engine->Exit();
break;
case SDL_KEYDOWN:
if (event.key.keysym.sym == 'q' && (event.key.keysym.mod&KMOD_CTRL))
if (!event.key.repeat && event.key.keysym.sym == 'q' && (event.key.keysym.mod&KMOD_CTRL))
engine->ConfirmExit();
else
engine->onKeyPress(event.key.keysym.sym, event.key.keysym.unicode, event.key.keysym.mod&KMOD_SHIFT, event.key.keysym.mod&KMOD_CTRL, event.key.keysym.mod&KMOD_ALT);
engine->onKeyPress(event.key.keysym.sym, event.key.keysym.scancode, event.key.repeat, event.key.keysym.mod&KMOD_SHIFT, event.key.keysym.mod&KMOD_CTRL, event.key.keysym.mod&KMOD_ALT);
break;
case SDL_KEYUP:
engine->onKeyRelease(event.key.keysym.sym, event.key.keysym.unicode, event.key.keysym.mod&KMOD_SHIFT, event.key.keysym.mod&KMOD_CTRL, event.key.keysym.mod&KMOD_ALT);
engine->onKeyRelease(event.key.keysym.sym, event.key.keysym.scancode, event.key.repeat, event.key.keysym.mod&KMOD_SHIFT, event.key.keysym.mod&KMOD_CTRL, event.key.keysym.mod&KMOD_ALT);
break;
case SDL_TEXTINPUT:
engine->onTextInput(ByteString(event.text.text).FromUtf8());
break;
case SDL_MOUSEWHEEL:
{
int x = event.wheel.x;
int y = event.wheel.y;
if (event.wheel.direction == SDL_MOUSEWHEEL_FLIPPED)
{
x *= -1;
y *= -1;
}
bool positiveDir = y == 0 ? x > 0 : y > 0;
engine->onMouseWheel(event.motion.x * inputScaleH, event.motion.y * inputScaleV, positiveDir ? 1 : -1);
break;
}
case SDL_MOUSEMOTION:
engine->onMouseMove(event.motion.x*inputScale, event.motion.y*inputScale);
mousex = event.motion.x*inputScale;
mousey = event.motion.y*inputScale;
engine->onMouseMove(event.motion.x * inputScaleH, event.motion.y * inputScaleV);
mousex = event.motion.x * inputScaleH;
mousey = event.motion.y * inputScaleV;
break;
case SDL_MOUSEBUTTONDOWN:
if (event.button.button == SDL_BUTTON_WHEELUP)
{
engine->onMouseWheel(event.motion.x*inputScale, event.motion.y*inputScale, 1);
}
else if (event.button.button == SDL_BUTTON_WHEELDOWN)
{
engine->onMouseWheel(event.motion.x*inputScale, event.motion.y*inputScale, -1);
}
else
{
engine->onMouseClick(event.motion.x*inputScale, event.motion.y*inputScale, event.button.button);
}
mousex = event.motion.x*inputScale;
mousey = event.motion.y*inputScale;
engine->onMouseClick(event.motion.x * inputScaleH, event.motion.y * inputScaleV, event.button.button);
mousex = event.motion.x * inputScaleH;
mousey = event.motion.y * inputScaleV;
break;
case SDL_MOUSEBUTTONUP:
if (event.button.button != SDL_BUTTON_WHEELUP && event.button.button != SDL_BUTTON_WHEELDOWN)
engine->onMouseUnclick(event.motion.x*inputScale, event.motion.y*inputScale, event.button.button);
mousex = event.motion.x*inputScale;
mousey = event.motion.y*inputScale;
engine->onMouseUnclick(event.motion.x * inputScaleH, event.motion.y * inputScaleV, event.button.button);
mousex = event.motion.x * inputScaleH;
mousey = event.motion.y * inputScaleV;
break;
#ifdef OGLI
case SDL_VIDEORESIZE:
case SDL_WINDOWEVENT:
{
float ratio = (float)WINDOWW / WINDOWH;
float width = event.resize.w;
float height = width/ratio;
sdl_scrn = SDL_SetVideoMode(event.resize.w, height, 32, SDL_OPENGL | SDL_RESIZABLE);
glViewport(0, 0, width, height);
engine->g->Reset();
//glScaled(width/currentWidth, height/currentHeight, 1.0f);
if (event.window.event != SDL_WINDOWEVENT_RESIZED)
break;
float width = event.window.data1;
float height = event.window.data2;
currentWidth = width;
currentHeight = height;
inputScale = (float)WINDOWW/currentWidth;
// this "* scale" thing doesn't really work properly
// currently there is a bug where input doesn't scale properly after resizing, only when double scale mode is active
inputScaleH = (float)WINDOWW * scale / currentWidth;
inputScaleV = (float)WINDOWH * scale / currentHeight;
glLineWidth(currentWidth/(float)WINDOWW);
if(sdl_scrn == NULL)
{
std::cerr << "Oh bugger" << std::endl;
}
break;
}
#endif
#if defined (USE_SDL) && defined(LIN) && defined(SDL_VIDEO_DRIVER_X11)
case SDL_SYSWMEVENT:
if (event.syswm.msg->subsystem != SDL_SYSWM_X11)
break;
sdl_wminfo.info.x11.lock_func();
XEvent xe = event.syswm.msg->event.xevent;
if (xe.type==SelectionClear)
{
clipboardText = "";
}
else if (xe.type==SelectionRequest)
{
XEvent xr;
xr.xselection.type = SelectionNotify;
xr.xselection.requestor = xe.xselectionrequest.requestor;
xr.xselection.selection = xe.xselectionrequest.selection;
xr.xselection.target = xe.xselectionrequest.target;
xr.xselection.property = xe.xselectionrequest.property;
xr.xselection.time = xe.xselectionrequest.time;
if (xe.xselectionrequest.target==XA_TARGETS)
{
// send list of supported formats
Atom targets[] = {XA_TARGETS, XA_STRING, XA_UTF8_STRING};
xr.xselection.property = xe.xselectionrequest.property;
XChangeProperty(sdl_wminfo.info.x11.display, xe.xselectionrequest.requestor, xe.xselectionrequest.property, XA_ATOM, 32, PropModeReplace, (unsigned char*)targets, (int)(sizeof(targets)/sizeof(Atom)));
}
// TODO: Supporting more targets would be nice
else if ((xe.xselectionrequest.target==XA_STRING || xe.xselectionrequest.target==XA_UTF8_STRING))
{
XChangeProperty(sdl_wminfo.info.x11.display, xe.xselectionrequest.requestor, xe.xselectionrequest.property, xe.xselectionrequest.target, 8, PropModeReplace, (unsigned char*)clipboardText.c_str(), clipboardText.size()+1);
}
else
{
// refuse clipboard request
xr.xselection.property = None;
}
XSendEvent(sdl_wminfo.info.x11.display, xe.xselectionrequest.requestor, 0, 0, &xr);
}
sdl_wminfo.info.x11.unlock_func();
#endif
}
}
@ -786,17 +386,13 @@ void EngineProcess()
if(scale != engine->Scale || fullscreen != engine->Fullscreen)
{
sdl_scrn = SDLSetScreen(engine->Scale, engine->Fullscreen);
inputScale = 1.0f/(float)scale;
SDLSetScreen(engine->Scale, engine->Fullscreen);
}
#ifdef OGLI
blit();
#else
if(engine->Scale > 1)
blit2(engine->g->vid, engine->Scale);
else
blit(engine->g->vid);
blit(engine->g->vid);
#endif
int frameTime = SDL_GetTicks() - frameStart;
@ -914,7 +510,8 @@ bool SaveWindowPosition()
#endif
void BlueScreen(String detailMessage){
void BlueScreen(String detailMessage)
{
ui::Engine * engine = &ui::Engine::Ref();
engine->g->fillrect(0, 0, engine->GetWidth(), engine->GetHeight(), 17, 114, 169, 210);
@ -948,10 +545,7 @@ void BlueScreen(String detailMessage){
#ifdef OGLI
blit();
#else
if(engine->Scale > 1)
blit2(engine->g->vid, engine->Scale);
else
blit(engine->g->vid);
blit(engine->g->vid);
#endif
}
}
@ -1047,7 +641,7 @@ int main(int argc, char * argv[])
#ifdef WIN
LoadWindowPosition(tempScale);
#endif
sdl_scrn = SDLSetScreen(tempScale, tempFullscreen);
SDLSetScreen(tempScale, tempFullscreen);
#ifdef OGLI
SDL_GL_SetAttribute (SDL_GL_DOUBLEBUFFER, 1);
@ -1060,27 +654,9 @@ int main(int argc, char * argv[])
fprintf(stderr, "Initializing Glew: %d\n", status);
exit(-1);
}
#endif
#if defined (USE_SDL) && defined(LIN) && defined(SDL_VIDEO_DRIVER_X11)
SDL_EventState(SDL_SYSWMEVENT, SDL_ENABLE);
SDL_VERSION(&sdl_wminfo.version);
if(SDL_GetWMInfo(&sdl_wminfo) > 0)
{
sdl_wminfo.info.x11.lock_func();
XA_CLIPBOARD = XInternAtom(sdl_wminfo.info.x11.display, "CLIPBOARD", 1);
XA_TARGETS = XInternAtom(sdl_wminfo.info.x11.display, "TARGETS", 1);
XA_UTF8_STRING = XInternAtom(sdl_wminfo.info.x11.display, "UTF8_STRING", 1);
sdl_wminfo.info.x11.unlock_func();
}
else
{
fprintf(stderr, "X11 setup failed, X11 window info not found");
exit(-1);
}
#endif
ui::Engine::Ref().g = new Graphics();
ui::Engine::Ref().Scale = scale;
inputScale = 1.0f/float(scale);
ui::Engine::Ref().Fullscreen = fullscreen;
engine = &ui::Engine::Ref();
@ -1156,10 +732,7 @@ int main(int argc, char * argv[])
#ifdef OGLI
blit();
#else
if(engine->Scale > 1)
blit2(engine->g->vid, engine->Scale);
else
blit(engine->g->vid);
blit(engine->g->vid);
#endif
ByteString ptsaveArg = arguments["ptsave"];
try
@ -1208,7 +781,7 @@ int main(int argc, char * argv[])
//initial mouse coords
int sdl_x, sdl_y;
SDL_GetMouseState(&sdl_x, &sdl_y);
engine->onMouseMove(sdl_x*inputScale, sdl_y*inputScale);
engine->onMouseMove(sdl_x * inputScaleH, sdl_y * inputScaleV);
EngineProcess();
#ifdef WIN

View File

@ -1,7 +1,7 @@
#ifdef USE_SDL
#ifdef SDL_INC
#include "SDL/SDL.h"
#include "SDL2/SDL.h"
#else
#include "SDL.h"
#endif
@ -9,7 +9,7 @@
#ifdef INCLUDE_SYSWM
#if defined(WIN) || defined(LIN)
#ifdef SDL_INC
#include <SDL/SDL_syswm.h>
#include <SDL2/SDL_syswm.h>
#else
#include <SDL_syswm.h>
#endif

View File

@ -11,5 +11,5 @@ public:
unsigned int debugID;
virtual void Draw() {}
// currentMouse doesn't belong but I don't want to create more hooks at the moment
virtual bool KeyPress(int key, Uint16 character, bool shift, bool ctrl, bool alt, ui::Point currentMouse) { return true; }
virtual bool KeyPress(int key, int scan, bool shift, bool ctrl, bool alt, ui::Point currentMouse) { return true; }
};

View File

@ -57,7 +57,7 @@ void ParticleDebug::Debug(int mode, int x, int y)
}
}
bool ParticleDebug::KeyPress(int key, Uint16 character, bool shift, bool ctrl, bool alt, ui::Point currentMouse)
bool ParticleDebug::KeyPress(int key, int scan, bool shift, bool ctrl, bool alt, ui::Point currentMouse)
{
if (key == 'f')
{

View File

@ -12,7 +12,7 @@ class ParticleDebug : public DebugInfo
public:
ParticleDebug(unsigned int id, Simulation * sim, GameModel * model);
void Debug(int mode, int x, int y);
virtual bool KeyPress(int key, Uint16 character, bool shift, bool ctrl, bool alt, ui::Point currentMouse);
virtual bool KeyPress(int key, int scan, bool shift, bool ctrl, bool alt, ui::Point currentMouse);
virtual ~ParticleDebug();
};

View File

@ -239,8 +239,10 @@ void ColourPickerActivity::OnMouseUp(int x, int y, unsigned button)
}
}
void ColourPickerActivity::OnKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool alt)
void ColourPickerActivity::OnKeyPress(int key, int scan, bool repeat, bool shift, bool ctrl, bool alt)
{
if (repeat)
return;
if (key == SDLK_TAB)
{
if (rValue->IsFocused())

View File

@ -36,7 +36,7 @@ public:
virtual void OnMouseMove(int x, int y, int dx, int dy);
virtual void OnMouseDown(int x, int y, unsigned button);
virtual void OnMouseUp(int x, int y, unsigned button);
virtual void OnKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool alt);
virtual void OnKeyPress(int key, int scan, bool repeat, bool shift, bool ctrl, bool alt);
virtual void OnTryExit(ExitMethod method);
ColourPickerActivity(ui::Colour initialColour, ColourPickedCallback * callback = NULL);
virtual ~ColourPickerActivity();

View File

@ -24,17 +24,15 @@ ConsoleView::ConsoleView():
commandField->SetBorder(false);
}
void ConsoleView::DoKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool alt)
void ConsoleView::DoKeyPress(int key, int scan, bool repeat, bool shift, bool ctrl, bool alt)
{
if ((scan == SDL_SCANCODE_GRAVE && key != '~') || key == SDLK_ESCAPE)
{
c->CloseConsole();
return;
}
switch(key)
{
case SDLK_ESCAPE:
case '`':
if (character != '~')
c->CloseConsole();
else
Window::DoKeyPress(key, character, shift, ctrl, alt);
break;
case SDLK_RETURN:
case SDLK_KP_ENTER:
c->EvaluateCommand(commandField->GetText());
@ -48,7 +46,7 @@ void ConsoleView::DoKeyPress(int key, Uint16 character, bool shift, bool ctrl, b
c->PreviousCommand();
break;
default:
Window::DoKeyPress(key, character, shift, ctrl, alt);
Window::DoKeyPress(key, scan, repeat, shift, ctrl, alt);
break;
}
}

View File

@ -20,7 +20,7 @@ class ConsoleView: public ui::Window {
public:
ConsoleView();
virtual void OnDraw();
virtual void DoKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool alt);
virtual void DoKeyPress(int key, int scan, bool repeat, bool shift, bool ctrl, bool alt);
void AttachController(ConsoleController * c_) { c = c_; }
void NotifyPreviousCommandsChanged(ConsoleModel * sender);
void NotifyCurrentCommandChanged(ConsoleModel * sender);

View File

@ -219,8 +219,10 @@ void ElementSearchActivity::OnTick(float dt)
}
}
void ElementSearchActivity::OnKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool alt)
void ElementSearchActivity::OnKeyPress(int key, int scan, bool repeat, bool shift, bool ctrl, bool alt)
{
if (repeat)
return;
switch (key)
{
case SDLK_KP_ENTER:
@ -245,8 +247,10 @@ void ElementSearchActivity::OnKeyPress(int key, Uint16 character, bool shift, bo
}
}
void ElementSearchActivity::OnKeyRelease(int key, Uint16 character, bool shift, bool ctrl, bool alt)
void ElementSearchActivity::OnKeyRelease(int key, int scan, bool repeat, bool shift, bool ctrl, bool alt)
{
if (repeat)
return;
switch (key)
{
case SDLK_LSHIFT:

View File

@ -33,8 +33,8 @@ public:
void SetActiveTool(int selectionState, Tool * tool);
virtual ~ElementSearchActivity();
virtual void OnTick(float dt);
virtual void OnKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool alt);
virtual void OnKeyRelease(int key, Uint16 character, bool shift, bool ctrl, bool alt);
virtual void OnKeyPress(int key, int scan, bool repeat, bool shift, bool ctrl, bool alt);
virtual void OnKeyRelease(int key, int scan, bool repeat, bool shift, bool ctrl, bool alt);
virtual void OnDraw();
virtual void ToolTip(ui::Point senderPosition, String ToolTip);
};

View File

@ -713,9 +713,11 @@ bool GameController::MouseWheel(int x, int y, int d)
return commandInterface->OnMouseWheel(x, y, d);
}
bool GameController::KeyPress(int key, Uint16 character, bool shift, bool ctrl, bool alt)
bool GameController::KeyPress(int key, int scan, bool repeat, bool shift, bool ctrl, bool alt)
{
bool ret = commandInterface->OnKeyPress(key, character, shift, ctrl, alt);
bool ret = commandInterface->OnKeyPress(key, scan, repeat, shift, ctrl, alt);
if (repeat)
return ret;
if (ret)
{
Simulation * sim = gameModel->GetSimulation();
@ -783,16 +785,18 @@ bool GameController::KeyPress(int key, Uint16 character, bool shift, bool ctrl,
for(std::vector<DebugInfo*>::iterator iter = debugInfo.begin(), end = debugInfo.end(); iter != end; iter++)
{
if ((*iter)->debugID & debugFlags)
if (!(*iter)->KeyPress(key, character, shift, ctrl, alt, gameView->GetMousePosition()))
if (!(*iter)->KeyPress(key, scan, shift, ctrl, alt, gameView->GetMousePosition()))
ret = false;
}
}
return ret;
}
bool GameController::KeyRelease(int key, Uint16 character, bool shift, bool ctrl, bool alt)
bool GameController::KeyRelease(int key, int scan, bool repeat, bool shift, bool ctrl, bool alt)
{
bool ret = commandInterface->OnKeyRelease(key, character, shift, ctrl, alt);
bool ret = commandInterface->OnKeyRelease(key, scan, repeat, shift, ctrl, alt);
if (repeat)
return ret;
if (ret)
{
Simulation * sim = gameModel->GetSimulation();

View File

@ -64,8 +64,8 @@ public:
bool MouseDown(int x, int y, unsigned button);
bool MouseUp(int x, int y, unsigned button, char type);
bool MouseWheel(int x, int y, int d);
bool KeyPress(int key, Uint16 character, bool shift, bool ctrl, bool alt);
bool KeyRelease(int key, Uint16 character, bool shift, bool ctrl, bool alt);
bool KeyPress(int key, int scan, bool repeat, bool shift, bool ctrl, bool alt);
bool KeyRelease(int key, int scan, bool repeat, bool shift, bool ctrl, bool alt);
bool MouseTick();
void Tick();
void Exit();

View File

@ -1374,7 +1374,7 @@ void GameView::BeginStampSelection()
buttonTipShow = 120;
}
void GameView::OnKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool alt)
void GameView::OnKeyPress(int key, int scan, bool repeat, bool shift, bool ctrl, bool alt)
{
if (introText > 50)
{
@ -1400,6 +1400,8 @@ void GameView::OnKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool
c->TranslateSave(ui::Point(0, 1));
return;
case 'r':
if (repeat)
return;
if (ctrl && shift)
{
//Vertical flip
@ -1419,6 +1421,14 @@ void GameView::OnKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool
}
}
}
if (repeat)
return;
if (scan == SDL_SCANCODE_GRAVE)
{
c->ShowConsole();
return;
}
switch(key)
{
case SDLK_LALT:
@ -1456,9 +1466,6 @@ void GameView::OnKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool
case SDLK_TAB: //Tab
c->ChangeBrush();
break;
case '`':
c->ShowConsole();
break;
case 'p':
case SDLK_F2:
if (ctrl)
@ -1657,8 +1664,10 @@ void GameView::OnKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool
}
}
void GameView::OnKeyRelease(int key, Uint16 character, bool shift, bool ctrl, bool alt)
void GameView::OnKeyRelease(int key, int scan, bool repeat, bool shift, bool ctrl, bool alt)
{
if (repeat)
return;
switch(key)
{
case SDLK_LALT:
@ -1689,7 +1698,7 @@ void GameView::OnBlur()
drawMode = DrawPoints;
c->MouseUp(0, 0, 0, 1); // tell lua that mouse is up (even if it really isn't)
if (GetModifiers())
c->KeyRelease(0, 0, false, false, false);
c->KeyRelease(0, 0, false, false, false, false);
}
void GameView::OnTick(float dt)
@ -1823,16 +1832,16 @@ void GameView::DoMouseWheel(int x, int y, int d)
Window::DoMouseWheel(x, y, d);
}
void GameView::DoKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool alt)
void GameView::DoKeyPress(int key, int scan, bool repeat, bool shift, bool ctrl, bool alt)
{
if(c->KeyPress(key, character, shift, ctrl, alt))
Window::DoKeyPress(key, character, shift, ctrl, alt);
if (c->KeyPress(key, scan, repeat, shift, ctrl, alt))
Window::DoKeyPress(key, scan, repeat, shift, ctrl, alt);
}
void GameView::DoKeyRelease(int key, Uint16 character, bool shift, bool ctrl, bool alt)
void GameView::DoKeyRelease(int key, int scan, bool repeat, bool shift, bool ctrl, bool alt)
{
if(c->KeyRelease(key, character, shift, ctrl, alt))
Window::DoKeyRelease(key, character, shift, ctrl, alt);
if(c->KeyRelease(key, scan, repeat, shift, ctrl, alt))
Window::DoKeyRelease(key, scan, repeat, shift, ctrl, alt);
}
void GameView::DoTick(float dt)

View File

@ -191,8 +191,8 @@ public:
virtual void OnMouseDown(int x, int y, unsigned button);
virtual void OnMouseUp(int x, int y, unsigned button);
virtual void OnMouseWheel(int x, int y, int d);
virtual void OnKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool alt);
virtual void OnKeyRelease(int key, Uint16 character, bool shift, bool ctrl, bool alt);
virtual void OnKeyPress(int key, int scan, bool repeat, bool shift, bool ctrl, bool alt);
virtual void OnKeyRelease(int key, int scan, bool repeat, bool shift, bool ctrl, bool alt);
virtual void OnTick(float dt);
virtual void OnDraw();
virtual void OnBlur();
@ -204,8 +204,8 @@ public:
virtual void DoMouseDown(int x, int y, unsigned button);
virtual void DoMouseUp(int x, int y, unsigned button);
virtual void DoMouseWheel(int x, int y, int d);
virtual void DoKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool alt);
virtual void DoKeyRelease(int key, Uint16 character, bool shift, bool ctrl, bool alt);
virtual void DoKeyPress(int key, int scan, bool repeat, bool shift, bool ctrl, bool alt);
virtual void DoKeyRelease(int key, int scan, bool repeat, bool shift, bool ctrl, bool alt);
class MenuAction;
class ToolAction;

View File

@ -23,7 +23,7 @@ public:
PropertyWindow(PropertyTool *tool_, Simulation *sim);
void SetProperty();
virtual void OnDraw();
virtual void OnKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool alt);
virtual void OnKeyPress(int key, int scan, bool repeat, bool shift, bool ctrl, bool alt);
virtual void OnTryExit(ExitMethod method);
virtual ~PropertyWindow() {}
class OkayAction: public ui::ButtonAction
@ -217,7 +217,7 @@ void PropertyWindow::OnDraw()
g->drawrect(Position.X, Position.Y, Size.X, Size.Y, 200, 200, 200, 255);
}
void PropertyWindow::OnKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool alt)
void PropertyWindow::OnKeyPress(int key, int scan, bool repeat, bool shift, bool ctrl, bool alt)
{
if (key == SDLK_UP)
property->SetOption(property->GetOption().second-1);

View File

@ -27,8 +27,8 @@ public:
virtual void DoMouseDown(int x, int y, unsigned button);
virtual void DoMouseUp(int x, int y, unsigned button) { if(!signMoving) ui::Window::DoMouseUp(x, y, button); }
virtual void DoMouseWheel(int x, int y, int d) { if(!signMoving) ui::Window::DoMouseWheel(x, y, d); }
virtual void DoKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool alt) { if(!signMoving) ui::Window::DoKeyPress(key, character, shift, ctrl, alt); }
virtual void DoKeyRelease(int key, Uint16 character, bool shift, bool ctrl, bool alt) { if(!signMoving) ui::Window::DoKeyRelease(key, character, shift, ctrl, alt); }
virtual void DoKeyPress(int key, int scan, bool repeat, bool shift, bool ctrl, bool alt) { if(!signMoving) ui::Window::DoKeyPress(key, scan, repeat, shift, ctrl, alt); }
virtual void DoKeyRelease(int key, int scan, bool repeat, bool shift, bool ctrl, bool alt) { if(!signMoving) ui::Window::DoKeyRelease(key, scan, repeat, shift, ctrl, alt); }
virtual ~SignWindow() {}
virtual void OnTryExit(ui::Window::ExitMethod method);
class OkayAction: public ui::ButtonAction

View File

@ -190,11 +190,15 @@ void Component::Tick(float dt)
{
}
void Component::OnKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool alt)
void Component::OnKeyPress(int key, int scan, bool repeat, bool shift, bool ctrl, bool alt)
{
}
void Component::OnKeyRelease(int key, Uint16 character, bool shift, bool ctrl, bool alt)
void Component::OnKeyRelease(int key, int scan, bool repeat, bool shift, bool ctrl, bool alt)
{
}
void Component::OnTextInput(String text)
{
}

View File

@ -66,26 +66,6 @@ namespace ui
virtual void OnContextMenuAction(int item);
//UI functions:
/*
void Tick(float dt);
void Draw(const Point& screenPos);
void OnMouseHover(int localx, int localy);
void OnMouseMoved(int localx, int localy, int dx, int dy);
void OnMouseMovedInside(int localx, int localy, int dx, int dy);
void OnMouseEnter(int localx, int localy);
void OnMouseLeave(int localx, int localy);
void OnMouseDown(int x, int y, unsigned int button);
void OnMouseUp(int x, int y, unsigned int button);
void OnMouseClick(int localx, int localy, unsigned int button);
void OnMouseUnclick(int localx, int localy, unsigned int button);
void OnMouseWheel(int localx, int localy, int d);
void OnMouseWheelInside(int localx, int localy, int d);
void OnKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool alt);
void OnKeyRelease(int key, Uint16 character, bool shift, bool ctrl, bool alt);
*/
///
// Called: Every tick.
// Params:
@ -211,7 +191,7 @@ namespace ui
// ctrl: Control key is down.
// alt: Alternate key is down.
///
virtual void OnKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool alt);
virtual void OnKeyPress(int key, int scan, bool repeat, bool shift, bool ctrl, bool alt);
///
// Called: When a key is released.
@ -221,6 +201,8 @@ namespace ui
// ctrl: Control key is released.
// alt: Alternate key is released.
///
virtual void OnKeyRelease(int key, Uint16 character, bool shift, bool ctrl, bool alt);
virtual void OnKeyRelease(int key, int scan, bool repeat, bool shift, bool ctrl, bool alt);
virtual void OnTextInput(String text);
};
}

View File

@ -17,7 +17,6 @@ Engine::Engine():
FpsLimit(60.0f),
Scale(1),
Fullscreen(false),
Depth3d(0),
FrameIndex(0),
lastBuffer(NULL),
prevBuffers(stack<pixel*>()),
@ -93,6 +92,7 @@ void Engine::ConfirmExit()
void Engine::ShowWindow(Window * window)
{
windowOpenState = 0;
ignoreEvents = true;
if(window->Position.X==-1)
{
window->Position.X = (width_-window->Size.X)/2;
@ -199,6 +199,7 @@ void Engine::Tick()
lastTick = Platform::GetTime();
ignoreEvents = false;
/*if(statequeued_ != NULL)
{
if(state_ != NULL)
@ -250,29 +251,35 @@ void Engine::SetFps(float fps)
this->dt = 1.0f;
}
void Engine::onKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool alt)
void Engine::onKeyPress(int key, int scan, bool repeat, bool shift, bool ctrl, bool alt)
{
if(state_)
state_->DoKeyPress(key, character, shift, ctrl, alt);
if (state_ && !ignoreEvents)
state_->DoKeyPress(key, scan, repeat, shift, ctrl, alt);
}
void Engine::onKeyRelease(int key, Uint16 character, bool shift, bool ctrl, bool alt)
void Engine::onKeyRelease(int key, int scan, bool repeat, bool shift, bool ctrl, bool alt)
{
if(state_)
state_->DoKeyRelease(key, character, shift, ctrl, alt);
if (state_ && !ignoreEvents)
state_->DoKeyRelease(key, scan, repeat, shift, ctrl, alt);
}
void Engine::onTextInput(String text)
{
if (state_ && !ignoreEvents)
state_->DoTextInput(text);
}
void Engine::onMouseClick(int x, int y, unsigned button)
{
mouseb_ |= button;
if(state_)
if (state_ && !ignoreEvents)
state_->DoMouseDown(x, y, button);
}
void Engine::onMouseUnclick(int x, int y, unsigned button)
{
mouseb_ &= ~button;
if(state_)
if (state_ && !ignoreEvents)
state_->DoMouseUp(x, y, button);
}
@ -280,7 +287,7 @@ void Engine::onMouseMove(int x, int y)
{
mousex_ = x;
mousey_ = y;
if(state_)
if (state_ && !ignoreEvents)
{
state_->DoMouseMove(x, y, mousex_ - mousexp_, mousey_ - mouseyp_);
}
@ -290,7 +297,7 @@ void Engine::onMouseMove(int x, int y)
void Engine::onMouseWheel(int x, int y, int delta)
{
if(state_)
if (state_ && !ignoreEvents)
state_->DoMouseWheel(x, y, delta);
}
@ -301,6 +308,6 @@ void Engine::onResize(int newWidth, int newHeight)
void Engine::onClose()
{
if(state_)
if (state_)
state_->DoExit();
}

View File

@ -28,8 +28,9 @@ namespace ui
void onMouseClick(int x, int y, unsigned button);
void onMouseUnclick(int x, int y, unsigned button);
void onMouseWheel(int x, int y, int delta);
void onKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool alt);
void onKeyRelease(int key, Uint16 character, bool shift, bool ctrl, bool alt);
void onKeyPress(int key, int scan, bool repeat, bool shift, bool ctrl, bool alt);
void onKeyRelease(int key, int scan, bool repeat, bool shift, bool ctrl, bool alt);
void onTextInput(String text);
void onResize(int newWidth, int newHeight);
void onClose();
@ -47,8 +48,6 @@ namespace ui
inline bool GetFullscreen() { return Fullscreen; }
void SetScale(int scale) { Scale = scale; }
inline int GetScale() { return Scale; }
void Set3dDepth(int depth3d) { Depth3d = depth3d; if (Depth3d) SetCursorEnabled(0); else SetCursorEnabled(1);}
inline int Get3dDepth() { return Depth3d; }
void SetFastQuit(bool fastquit) { FastQuit = fastquit; }
inline bool GetFastQuit() {return FastQuit; }
@ -78,7 +77,6 @@ namespace ui
Graphics * g;
int Scale;
bool Fullscreen;
int Depth3d;
unsigned int FrameIndex;
private:
@ -92,6 +90,7 @@ namespace ui
Window* state_;
Point windowTargetPosition;
int windowOpenState;
bool ignoreEvents = false;
bool running_;
bool break_;

View File

@ -20,6 +20,8 @@
slouken@libsdl.org
*/
#include "SDLCompat.h"
#define _SDL_keysym_h
#ifndef _SDL_keysym_h
#define _SDL_keysym_h

View File

@ -229,8 +229,10 @@ void Label::OnMouseUp(int x, int y, unsigned button)
selecting = false;
}
void Label::OnKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool alt)
void Label::OnKeyPress(int key, int scan, bool repeat, bool shift, bool ctrl, bool alt)
{
if (repeat)
return;
if(ctrl && key == 'c')
{
copySelection();

View File

@ -62,7 +62,7 @@ namespace ui
virtual void OnMouseClick(int x, int y, unsigned button);
virtual void OnMouseUp(int x, int y, unsigned button);
virtual void OnMouseMoved(int localx, int localy, int dx, int dy);
virtual void OnKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool alt);
virtual void OnKeyPress(int key, int scan, bool repeat, bool shift, bool ctrl, bool alt);
virtual void Draw(const Point& screenPos);
virtual void Tick(float dt);
};

View File

@ -2,7 +2,7 @@
#ifdef USE_SDL
#ifdef SDL_INC
#include "SDL/SDL_mouse.h"
#include "SDL2/SDL_mouse.h"
#else
#include "SDL_mouse.h"
#endif

View File

@ -175,14 +175,14 @@ void Panel::Tick(float dt)
children[i]->Tick(dt);
}
void Panel::OnKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool alt)
void Panel::OnKeyPress(int key, int scan, bool repeat, bool shift, bool ctrl, bool alt)
{
XOnKeyPress(key, character, shift, ctrl, alt);
XOnKeyPress(key, scan, repeat, shift, ctrl, alt);
}
void Panel::OnKeyRelease(int key, Uint16 character, bool shift, bool ctrl, bool alt)
void Panel::OnKeyRelease(int key, int scan, bool repeat, bool shift, bool ctrl, bool alt)
{
XOnKeyRelease(key, character, shift, ctrl, alt);
XOnKeyRelease(key, scan, repeat, shift, ctrl, alt);
}
void Panel::OnMouseClick(int localx, int localy, unsigned button)
@ -401,11 +401,11 @@ void Panel::XTick(float dt)
{
}
void Panel::XOnKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool alt)
void Panel::XOnKeyPress(int key, int scan, bool repeat, bool shift, bool ctrl, bool alt)
{
}
void Panel::XOnKeyRelease(int key, Uint16 character, bool shift, bool ctrl, bool alt)
void Panel::XOnKeyRelease(int key, int scan, bool repeat, bool shift, bool ctrl, bool alt)
{
}

View File

@ -72,34 +72,14 @@ class Component;
void OnMouseUnclick(int localx, int localy, unsigned button);
void OnMouseWheel(int localx, int localy, int d);
void OnMouseWheelInside(int localx, int localy, int d);
void OnKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool alt);
void OnKeyRelease(int key, Uint16 character, bool shift, bool ctrl, bool alt);
void OnKeyPress(int key, int scan, bool repeat, bool shift, bool ctrl, bool alt);
void OnKeyRelease(int key, int scan, bool repeat, bool shift, bool ctrl, bool alt);
protected:
// child components
std::vector<ui::Component*> children;
bool mouseInside;
//UI functions:
/*
void XTick(float dt);
void XDraw(const Point& screenPos);
void XOnMouseHover(int localx, int localy);
void XOnMouseMoved(int localx, int localy, int dx, int dy);
void XOnMouseMovedInside(int localx, int localy, int dx, int dy);
void XOnMouseEnter(int localx, int localy);
void XOnMouseLeave(int localx, int localy);
void XOnMouseDown(int x, int y, unsigned int button);
void XOnMouseUp(int x, int y, unsigned int button);
void XOnMouseClick(int localx, int localy, unsigned int button);
void XOnMouseUnclick(int localx, int localy, unsigned int button);
void XOnMouseWheel(int localx, int localy, int d);
void XOnMouseWheelInside(int localx, int localy, int d);
void XOnKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool alt);
void XOnKeyRelease(int key, Uint16 character, bool shift, bool ctrl, bool alt);
*/
// Overridable. Called by XComponent::Tick()
virtual void XTick(float dt);
@ -141,10 +121,10 @@ class Component;
virtual void XOnMouseWheelInside(int localx, int localy, int d);
// Overridable. Called by XComponent::OnKeyPress()
virtual void XOnKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool alt);
virtual void XOnKeyPress(int key, int scan, bool repeat, bool shift, bool ctrl, bool alt);
// Overridable. Called by XComponent::OnKeyRelease()
virtual void XOnKeyRelease(int key, Uint16 character, bool shift, bool ctrl, bool alt);
virtual void XOnKeyRelease(int key, int scan, bool repeat, bool shift, bool ctrl, bool alt);
};
}

View File

@ -251,7 +251,7 @@ void Textbox::pasteIntoSelection()
actionCallback->TextChangedCallback(this);
}
bool Textbox::CharacterValid(Uint16 character)
bool Textbox::CharacterValid(int character)
{
switch(inputType)
{
@ -270,6 +270,15 @@ bool Textbox::CharacterValid(Uint16 character)
return false;
}
// TODO: proper unicode validation
bool Textbox::StringValid(String text)
{
for (String::value_type c : text)
if (!CharacterValid(c))
return false;
return true;
}
void Textbox::Tick(float dt)
{
Label::Tick(dt);
@ -281,29 +290,29 @@ void Textbox::Tick(float dt)
unsigned long time_pls = Platform::GetTime();
if ((keyDown || characterDown) && repeatTime <= time_pls)
{
OnVKeyPress(keyDown, characterDown, false, false, false);
//OnVKeyPress(keyDown, characterDown, false, false, false);
repeatTime = Platform::GetTime()+30;
}
}
void Textbox::OnKeyRelease(int key, Uint16 character, bool shift, bool ctrl, bool alt)
void Textbox::OnKeyRelease(int key, int scan, bool repeat, bool shift, bool ctrl, bool alt)
{
keyDown = 0;
characterDown = 0;
}
void Textbox::OnKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool alt)
void Textbox::OnKeyPress(int key, int scan, bool repeat, bool shift, bool ctrl, bool alt)
{
characterDown = character;
characterDown = scan;
keyDown = key;
repeatTime = Platform::GetTime()+300;
OnVKeyPress(key, character, shift, ctrl, alt);
OnVKeyPress(key, scan, repeat, shift, ctrl, alt);
}
void Textbox::OnVKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool alt)
void Textbox::OnVKeyPress(int key, int scan, bool repeat, bool shift, bool ctrl, bool alt)
{
bool changed = false;
if(ctrl && key == 'c' && !masked)
if(ctrl && key == 'c' && !masked && !repeat)
{
copySelection();
return;
@ -313,7 +322,7 @@ void Textbox::OnVKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool
pasteIntoSelection();
return;
}
if(ctrl && key == 'x' && !masked && !ReadOnly)
if(ctrl && key == 'x' && !masked && !repeat && !ReadOnly)
{
cutSelection();
return;
@ -406,38 +415,7 @@ void Textbox::OnVKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool
ClearSelection();
break;
case SDLK_RETURN:
character = '\n';
default:
if (CharacterValid(character) && !ReadOnly)
{
if (HasSelection())
{
if (getLowerSelectionBound() < 0 || getHigherSelectionBound() > (int)backingText.length())
return;
backingText.erase(backingText.begin()+getLowerSelectionBound(), backingText.begin()+getHigherSelectionBound());
cursor = getLowerSelectionBound();
}
int regionWidth = Size.X;
if (Appearance.icon)
regionWidth -= 13;
regionWidth -= Appearance.Margin.Left;
regionWidth -= Appearance.Margin.Right;
if ((limit==String::npos || backingText.length() < limit) && (Graphics::textwidth(backingText + character) <= regionWidth || multiline))
{
if (cursor == (int)backingText.length())
{
backingText += character;
}
else
{
backingText.Insert(cursor, character);
}
cursor++;
}
changed = true;
ClearSelection();
}
OnTextInput("\n");
break;
}
}
@ -446,16 +424,23 @@ void Textbox::OnVKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool
cursor = 0;
backingText = "";
}
if (inputType == Number)
{
//Remove extra preceding 0's
while(backingText[0] == '0' && backingText.length()>1)
backingText.erase(backingText.begin());
}
AfterTextChange(changed);
}
void Textbox::AfterTextChange(bool changed)
{
if (cursor > (int)backingText.length())
cursor = backingText.length();
if (changed)
{
if (inputType == Number)
{
//Remove extra preceding 0's
while(backingText[0] == '0' && backingText.length()>1)
backingText.erase(backingText.begin());
}
if (masked)
{
String maskedText = backingText;
@ -468,10 +453,10 @@ void Textbox::OnVKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool
}
}
if(multiline)
if (multiline)
updateMultiline();
updateSelection();
if(multiline)
if (multiline)
TextPosition(textLines);
else
TextPosition(text);
@ -488,6 +473,40 @@ void Textbox::OnVKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool
actionCallback->TextChangedCallback(this);
}
void Textbox::OnTextInput(String text)
{
if (StringValid(text) && !ReadOnly)
{
if (HasSelection())
{
if (getLowerSelectionBound() < 0 || getHigherSelectionBound() > (int)backingText.length())
return;
backingText.erase(backingText.begin()+getLowerSelectionBound(), backingText.begin()+getHigherSelectionBound());
cursor = getLowerSelectionBound();
}
int regionWidth = Size.X;
if (Appearance.icon)
regionWidth -= 13;
regionWidth -= Appearance.Margin.Left;
regionWidth -= Appearance.Margin.Right;
if ((limit==String::npos || backingText.length() < limit) && (Graphics::textwidth(backingText + text) <= regionWidth || multiline))
{
if (cursor == (int)backingText.length())
{
backingText += text;
}
else
{
backingText.Insert(cursor, text);
}
cursor++;
}
ClearSelection();
AfterTextChange(true);
}
}
void Textbox::OnMouseClick(int x, int y, unsigned button)
{
@ -551,162 +570,3 @@ void Textbox::Draw(const Point& screenPos)
if(Appearance.icon)
g->draw_icon(screenPos.X+iconPosition.X, screenPos.Y+iconPosition.Y, Appearance.icon);
}
/*
Textbox::Textbox(Point position, Point size, std::string textboxText):
Component(position, size),
text(textboxText),
actionCallback(NULL),
masked(false),
border(true)
{
SetText(textboxText);
cursor = text.length();
}
Textbox::~Textbox()
{
delete actionCallback;
}
void Textbox::TextPosition()
{
if(cursor)
{
cursorPosition = Graphics::textnwidth((char *)displayText.c_str(), cursor);
}
else
{
cursorPosition = 0;
}
Component::TextPosition(displayText);
}
void Textbox::SetText(std::string text)
{
cursor = text.length();
this->text = text;
this->displayText = text;
TextPosition();
}
void Textbox::SetDisplayText(std::string text)
{
displayText = text;
TextPosition();
}
std::string Textbox::GetText()
{
return text;
}
void Textbox::OnKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool alt)
{
bool changed = false;
try
{
switch(key)
{
case KEY_HOME:
cursor = 0;
break;
case KEY_END:
cursor = text.length();
break;
case KEY_LEFT:
if(cursor > 0)
cursor--;
break;
case KEY_RIGHT:
if(cursor < text.length())
cursor++;
break;
case KEY_DELETE:
if(text.length() && cursor < text.length())
{
if(ctrl)
text.erase(cursor, text.length()-cursor);
else
text.erase(cursor, 1);
changed = true;
}
break;
case KEY_BACKSPACE:
if(text.length() && cursor > 0)
{
if(ctrl)
{
text.erase(0, cursor);
cursor = 0;
}
else
{
text.erase(cursor-1, 1);
cursor--;
}
changed = true;
}
break;
}
if(character >= ' ' && character < 127)
{
if(cursor == text.length())
{
text += character;
}
else
{
text.insert(cursor, 1, (char)character);
}
cursor++;
changed = true;
}
}
catch(std::out_of_range &e)
{
cursor = 0;
text = "";
}
if(changed)
{
if(masked)
{
char * tempText = new char[text.length()+1];
std::fill(tempText, tempText+text.length(), 0x8d);
tempText[text.length()] = 0;
displayText = std::string(tempText);
delete tempText;
}
else
{
displayText = text;
}
if(actionCallback)
actionCallback->TextChangedCallback(this);
}
TextPosition();
}
void Textbox::Draw(const Point& screenPos)
{
if(!drawn)
{
TextPosition();
drawn = true;
}
Graphics * g = GetGraphics();
if(IsFocused())
{
if(border) g->drawrect(screenPos.X, screenPos.Y, Size.X, Size.Y, 255, 255, 255, 255);
g->draw_line(screenPos.X+textPosition.X+cursorPosition, screenPos.Y+3, screenPos.X+textPosition.X+cursorPosition, screenPos.Y+12, 255, 255, 255, WINDOWW);
}
else
{
if(border) g->drawrect(screenPos.X, screenPos.Y, Size.X, Size.Y, 160, 160, 160, 255);
}
g->drawtext(screenPos.X+textPosition.X, screenPos.Y+textPosition.Y, displayText, 255, 255, 255, 255);
if(Appearance.icon)
g->draw_icon(screenPos.X+iconPosition.X, screenPos.Y+iconPosition.Y, Appearance.icon);
}*/

View File

@ -19,6 +19,9 @@ public:
class Textbox : public Label
{
friend class TextboxAction;
void AfterTextChange(bool changed);
public:
bool ReadOnly;
enum ValidInput { All, Multiline, Numeric, Number }; // Numeric doesn't delete trailing 0's
@ -44,16 +47,18 @@ public:
void resetCursorPosition();
void TabFocus();
//Determines if the given character is valid given the input type
bool CharacterValid(Uint16 character);
bool CharacterValid(int character);
bool StringValid(String text);
virtual void Tick(float dt);
virtual void OnContextMenuAction(int item);
virtual void OnMouseClick(int x, int y, unsigned button);
virtual void OnMouseUp(int x, int y, unsigned button);
virtual void OnMouseMoved(int localx, int localy, int dx, int dy);
virtual void OnKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool alt);
virtual void OnVKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool alt);
virtual void OnKeyRelease(int key, Uint16 character, bool shift, bool ctrl, bool alt);
virtual void OnKeyPress(int key, int scan, bool repeat, bool shift, bool ctrl, bool alt);
virtual void OnVKeyPress(int key, int scan, bool repeat, bool shift, bool ctrl, bool alt);
virtual void OnKeyRelease(int key, int scan, bool repeat, bool shift, bool ctrl, bool alt);
void OnTextInput(String text) override;
virtual void Draw(const Point& screenPos);
protected:

View File

@ -283,7 +283,7 @@ void Window::DoTick(float dt)
finalise();
}
void Window::DoKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool alt)
void Window::DoKeyPress(int key, int scan, bool repeat, bool shift, bool ctrl, bool alt)
{
#ifdef DEBUG
if (key == SDLK_TAB && ctrl)
@ -374,11 +374,11 @@ void Window::DoKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool a
if (focusedComponent_ != NULL)
{
if (focusedComponent_->Enabled && focusedComponent_->Visible)
focusedComponent_->OnKeyPress(key, character, shift, ctrl, alt);
focusedComponent_->OnKeyPress(key, scan, repeat, shift, ctrl, alt);
}
if (!stop)
OnKeyPress(key, character, shift, ctrl, alt);
OnKeyPress(key, scan, repeat, shift, ctrl, alt);
if (key == SDLK_ESCAPE)
OnTryExit(Escape);
@ -390,7 +390,7 @@ void Window::DoKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool a
finalise();
}
void Window::DoKeyRelease(int key, Uint16 character, bool shift, bool ctrl, bool alt)
void Window::DoKeyRelease(int key, int scan, bool repeat, bool shift, bool ctrl, bool alt)
{
#ifdef DEBUG
if(debugMode)
@ -400,11 +400,30 @@ void Window::DoKeyRelease(int key, Uint16 character, bool shift, bool ctrl, bool
if (focusedComponent_ != NULL)
{
if (focusedComponent_->Enabled && focusedComponent_->Visible)
focusedComponent_->OnKeyRelease(key, character, shift, ctrl, alt);
focusedComponent_->OnKeyRelease(key, scan, repeat, shift, ctrl, alt);
}
if (!stop)
OnKeyRelease(key, character, shift, ctrl, alt);
OnKeyRelease(key, scan, repeat, shift, ctrl, alt);
if (destruct)
finalise();
}
void Window::DoTextInput(String text)
{
#ifdef DEBUG
if (debugMode)
return;
#endif
//on key unpress
if (focusedComponent_ != NULL)
{
if (focusedComponent_->Enabled && focusedComponent_->Visible)
focusedComponent_->OnTextInput(text);
}
if (!stop)
OnTextInput(text);
if (destruct)
finalise();
}

View File

@ -65,8 +65,9 @@ namespace ui
virtual void DoMouseDown(int x, int y, unsigned button);
virtual void DoMouseUp(int x, int y, unsigned button);
virtual void DoMouseWheel(int x, int y, int d);
virtual void DoKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool alt);
virtual void DoKeyRelease(int key, Uint16 character, bool shift, bool ctrl, bool alt);
virtual void DoKeyPress(int key, int scan, bool repeat, bool shift, bool ctrl, bool alt);
virtual void DoKeyRelease(int key, int scan, bool repeat, bool shift, bool ctrl, bool alt);
virtual void DoTextInput(String text);
// Sets halt and destroy, this causes the Windows to stop sending events and remove itself.
void SelfDestruct();
@ -102,8 +103,9 @@ namespace ui
virtual void OnMouseDown(int x, int y, unsigned button) {}
virtual void OnMouseUp(int x, int y, unsigned button) {}
virtual void OnMouseWheel(int x, int y, int d) {}
virtual void OnKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool alt) {}
virtual void OnKeyRelease(int key, Uint16 character, bool shift, bool ctrl, bool alt) {}
virtual void OnKeyPress(int key, int scan, bool repeat, bool shift, bool ctrl, bool alt) {}
virtual void OnKeyRelease(int key, int scan, bool repeat, bool shift, bool ctrl, bool alt) {}
virtual void OnTextInput(String text) {}
std::vector<Component*> Components;
Component *focusedComponent_;
Component *hoverComponent;

View File

@ -266,16 +266,20 @@ void LocalBrowserView::OnMouseWheel(int x, int y, int d)
c->PrevPage();
}
void LocalBrowserView::OnKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool alt)
void LocalBrowserView::OnKeyPress(int key, int scan, bool repeat, bool shift, bool ctrl, bool alt)
{
if (repeat)
return;
if (key == SDLK_ESCAPE)
c->Exit();
else if (key == SDLK_LCTRL || key == SDLK_RCTRL)
c->SetMoveToFront(false);
}
void LocalBrowserView::OnKeyRelease(int key, Uint16 character, bool shift, bool ctrl, bool alt)
void LocalBrowserView::OnKeyRelease(int key, int scan, bool repeat, bool shift, bool ctrl, bool alt)
{
if (repeat)
return;
if (key == SDLK_LCTRL || key == SDLK_RCTRL)
c->SetMoveToFront(true);
}

View File

@ -38,8 +38,8 @@ public:
void NotifySavesListChanged(LocalBrowserModel * sender);
void NotifySelectedChanged(LocalBrowserModel * sender);
virtual void OnMouseWheel(int x, int y, int d);
virtual void OnKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool alt);
virtual void OnKeyRelease(int key, Uint16 character, bool shift, bool ctrl, bool alt);
virtual void OnKeyPress(int key, int scan, bool repeat, bool shift, bool ctrl, bool alt);
virtual void OnKeyRelease(int key, int scan, bool repeat, bool shift, bool ctrl, bool alt);
virtual ~LocalBrowserView();
};

View File

@ -73,8 +73,10 @@ LoginView::LoginView():
passwordField->SetHidden(true);
}
void LoginView::OnKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool alt)
void LoginView::OnKeyPress(int key, int scan, bool repeat, bool shift, bool ctrl, bool alt)
{
if (repeat)
return;
switch(key)
{
case SDLK_TAB:

View File

@ -27,7 +27,7 @@ public:
class LoginAction;
class CancelAction;
LoginView();
virtual void OnKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool alt);
virtual void OnKeyPress(int key, int scan, bool repeat, bool shift, bool ctrl, bool alt);
virtual void OnTryExit(ExitMethod method);
void AttachController(LoginController * c_) { c = c_; }
void NotifyStatusChanged(LoginModel * sender);

View File

@ -8,7 +8,6 @@ OptionsController::OptionsController(GameModel * gModel_, ControllerCallback * c
callback(callback_),
HasExited(false)
{
this->depth3d = ui::Engine::Ref().Get3dDepth();
view = new OptionsView();
model = new OptionsModel(gModel);
model->AddObserver(view);
@ -71,11 +70,6 @@ void OptionsController::SetFastQuit(bool fastquit)
model->SetFastQuit(fastquit);
}
void OptionsController::Set3dDepth(int depth)
{
depth3d = depth;
}
OptionsView * OptionsController::GetView()
{
return view;
@ -84,8 +78,6 @@ OptionsView * OptionsController::GetView()
void OptionsController::Exit()
{
view->CloseActiveWindow();
// only update on close, it would be hard to edit if the changes were live
ui::Engine::Ref().Set3dDepth(depth3d);
if (callback)
callback->ControllerExit();

View File

@ -14,7 +14,6 @@ class OptionsController {
OptionsView * view;
OptionsModel * model;
ControllerCallback * callback;
int depth3d;
public:
bool HasExited;
OptionsController(GameModel * gModel_, ControllerCallback * callback_);
@ -29,7 +28,6 @@ public:
void SetScale(int scale);
void SetFastQuit(bool fastquit);
void SetShowAvatars(bool showAvatars);
void Set3dDepth(int depth);
void Exit();
OptionsView * GetView();
virtual ~OptionsController();

View File

@ -17,7 +17,7 @@
#include "gui/dialogues/ErrorMessage.h"
OptionsView::OptionsView():
ui::Window(ui::Point(-1, -1), ui::Point(300, 348)){
ui::Window(ui::Point(-1, -1), ui::Point(300, 329)){
ui::Label * tempLabel = new ui::Label(ui::Point(4, 5), ui::Point(Size.X-8, 14), "Simulation Options");
tempLabel->SetTextColour(style::Colour::InformationTitle);
@ -228,19 +228,7 @@ OptionsView::OptionsView():
AddComponent(tempLabel);
AddComponent(showAvatars);
class DepthAction: public ui::TextboxAction
{
OptionsView * v;
public:
DepthAction(OptionsView * v_) { v = v_; }
virtual void TextChangedCallback(ui::Textbox * sender) { v->c->Set3dDepth(sender->GetText().ToNumber<int>(true)); }
};
depthTextbox = new ui::Textbox(ui::Point(8, Size.Y-58), ui::Point(25, 16), String::Build(ui::Engine::Ref().Get3dDepth()));
depthTextbox->SetInputType(ui::Textbox::Numeric);
depthTextbox->SetActionCallback(new DepthAction(this));
AddComponent(depthTextbox);
tempLabel = new ui::Label(ui::Point(depthTextbox->Position.X+depthTextbox->Size.X+3, depthTextbox->Position.Y), ui::Point(Size.X-28, 16), "\bg- Change the depth of the 3D anaglyph effect");
tempLabel = new ui::Label(ui::Point(showAvatars->Position.X+showAvatars->Size.X+3, depthTextbox->Position.Y), ui::Point(Size.X-28, 16), "\bg- Change the depth of the 3D anaglyph effect");
tempLabel->Appearance.HorizontalAlign = ui::Appearance::AlignLeft; tempLabel->Appearance.VerticalAlign = ui::Appearance::AlignMiddle;
AddComponent(tempLabel);

View File

@ -479,8 +479,10 @@ void PreviewView::OnMouseUp(int x, int y, unsigned int button)
}
}
void PreviewView::OnKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool alt)
void PreviewView::OnKeyPress(int key, int scan, bool repeat, bool shift, bool ctrl, bool alt)
{
if (repeat)
return;
if ((key == SDLK_KP_ENTER || key == SDLK_RETURN) && (!addCommentBox || !addCommentBox->IsFocused()))
openButton->DoAction();
}

View File

@ -85,7 +85,7 @@ public:
virtual void OnTryExit(ExitMethod method);
virtual void OnMouseWheel(int x, int y, int d);
virtual void OnMouseUp(int x, int y, unsigned int button);
virtual void OnKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool alt);
virtual void OnKeyPress(int key, int scan, bool repeat, bool shift, bool ctrl, bool alt);
virtual ~PreviewView();
};

View File

@ -405,8 +405,10 @@ void RenderView::OnTick(float dt)
}
}
void RenderView::OnKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool alt)
void RenderView::OnKeyPress(int key, int scan, bool repeat, bool shift, bool ctrl, bool alt)
{
if (repeat)
return;
if (shift && key == '1')
c->LoadRenderPreset(10);
else if(key >= '0' && key <= '9')

View File

@ -37,7 +37,7 @@ public:
void OnTryExit(ExitMethod method);
virtual void OnDraw();
virtual void OnTick(float dt);
virtual void OnKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool alt);
virtual void OnKeyPress(int key, int scan, bool repeat, bool shift, bool ctrl, bool alt);
virtual void ToolTip(ui::Point senderPosition, String toolTip);
virtual ~RenderView();
};

View File

@ -798,16 +798,20 @@ void SearchView::OnMouseWheel(int x, int y, int d)
else
c->PrevPage();
}
void SearchView::OnKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool alt)
void SearchView::OnKeyPress(int key, int scan, bool repeat, bool shift, bool ctrl, bool alt)
{
if (repeat)
return;
if (key == SDLK_ESCAPE)
c->Exit();
else if (key == SDLK_LCTRL || key == SDLK_RCTRL)
c->InstantOpen(true);
}
void SearchView::OnKeyRelease(int key, Uint16 character, bool shift, bool ctrl, bool alt)
void SearchView::OnKeyRelease(int key, int scan, bool repeat, bool shift, bool ctrl, bool alt)
{
if (repeat)
return;
if (key == SDLK_LCTRL || key == SDLK_RCTRL)
c->InstantOpen(false);
}

View File

@ -69,8 +69,8 @@ public:
virtual void Search(String);
virtual void OnTick(float dt);
virtual void OnMouseWheel(int x, int y, int d);
virtual void OnKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool alt);
virtual void OnKeyRelease(int key, Uint16 character, bool shift, bool ctrl, bool alt);
virtual void OnKeyPress(int key, int scan, bool repeat, bool shift, bool ctrl, bool alt);
virtual void OnKeyRelease(int key, int scan, bool repeat, bool shift, bool ctrl, bool alt);
};

View File

@ -131,8 +131,10 @@ void TagsView::NotifyTagsChanged(TagsModel * sender)
}
}
void TagsView::OnKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool alt)
void TagsView::OnKeyPress(int key, int scan, bool repeat, bool shift, bool ctrl, bool alt)
{
if (repeat)
return;
switch(key)
{
case SDLK_KP_ENTER:

View File

@ -25,7 +25,7 @@ public:
TagsView();
virtual void OnDraw();
void AttachController(TagsController * c_) { c = c_; }
virtual void OnKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool alt);
virtual void OnKeyPress(int key, int scan, bool repeat, bool shift, bool ctrl, bool alt);
void NotifyTagsChanged(TagsModel * sender);
virtual ~TagsView();
};

View File

@ -25,8 +25,8 @@ public:
virtual bool OnMouseDown(int x, int y, unsigned button) {return true;}
virtual bool OnMouseUp(int x, int y, unsigned button, char type) {return true;}
virtual bool OnMouseWheel(int x, int y, int d) {return true;}
virtual bool OnKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool alt) {return true;}
virtual bool OnKeyRelease(int key, Uint16 character, bool shift, bool ctrl, bool alt) {return true;}
virtual bool OnKeyPress(int key, int scan, bool repeat, bool shift, bool ctrl, bool alt) {return true;}
virtual bool OnKeyRelease(int key, int scan, bool repeat, bool shift, bool ctrl, bool alt) {return true;}
virtual bool OnMouseTick() { return true; }
virtual void OnTick() { }
virtual int Command(String command);

View File

@ -244,7 +244,7 @@ int luacon_elementwrite(lua_State* l)
}
bool shortcuts = true;
int luacon_keyevent(int key, Uint16 character, int modifier, int event)
int luacon_keyevent(int key, int scan, int modifier, int event)
{
ui::Engine::Ref().LastTick(Platform::GetTime());
int kycontinue = 1;
@ -263,10 +263,10 @@ int luacon_keyevent(int key, Uint16 character, int modifier, int event)
for (int i = 1; i <= len && kycontinue; i++)
{
lua_rawgeti(l, -1, i);
if ((modifier & KMOD_CTRL) && (character < ' ' || character > '~') && key < 256)
if ((modifier & KMOD_CTRL) && (scan < ' ' || scan > '~') && key < 256)
lua_pushlstring(l, (const char*)&key, 1);
else
lua_pushlstring(l, (const char*)&character, 1);
lua_pushlstring(l, (const char*)&scan, 1);
lua_pushinteger(l, key);
lua_pushinteger(l, modifier);
lua_pushinteger(l, event);

View File

@ -23,7 +23,7 @@ extern int tptParts, tptPartsMeta, tptElementTransitions, tptPartsCData, tptPart
void luacon_hook(lua_State *L, lua_Debug *ar);
int luacon_step(int mx, int my);
int luacon_mouseevent(int mx, int my, int mb, int event, int mouse_wheel);
int luacon_keyevent(int key, Uint16 character, int modifier, int event);
int luacon_keyevent(int key, int scan, int modifier, int event);
int luacon_eval(const char *command);
String luacon_geterror();
void luacon_close();

View File

@ -2288,17 +2288,7 @@ int LuaScriptInterface::renderer_debugHUD(lua_State * l)
int LuaScriptInterface::renderer_depth3d(lua_State * l)
{
int acount = lua_gettop(l);
if (acount == 0)
{
lua_pushnumber(l, ui::Engine::Ref().Get3dDepth());
return 1;
}
int depth3d = luaL_optint(l, 1, -3);
if (depth3d < -30 || depth3d > 30)
return luaL_error(l, "3D depth is too large");
ui::Engine::Ref().Set3dDepth(depth3d);
return 0;
return luaL_error(l, "This feature is no longer supported");
}
void LuaScriptInterface::initElementsAPI()
@ -3374,12 +3364,12 @@ bool LuaScriptInterface::OnMouseWheel(int x, int y, int d)
return luacon_mouseevent(x, y, luacon_mousedown?luacon_mousebutton:0, 0, d);
}
bool LuaScriptInterface::OnKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool alt)
bool LuaScriptInterface::OnKeyPress(int key, int scan, bool repeat, bool shift, bool ctrl, bool alt)
{
return luacon_keyevent(key, character, GetModifiers(), LUACON_KDOWN);
return luacon_keyevent(key, scan, GetModifiers(), LUACON_KDOWN);
}
bool LuaScriptInterface::OnKeyRelease(int key, Uint16 character, bool shift, bool ctrl, bool alt)
bool LuaScriptInterface::OnKeyRelease(int key, int scan, bool repeat, bool shift, bool ctrl, bool alt)
{
int modifiers = 0;
if(shift)

View File

@ -182,8 +182,8 @@ public:
virtual bool OnMouseDown(int x, int y, unsigned button);
virtual bool OnMouseUp(int x, int y, unsigned button, char type);
virtual bool OnMouseWheel(int x, int y, int d);
virtual bool OnKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool alt);
virtual bool OnKeyRelease(int key, Uint16 character, bool shift, bool ctrl, bool alt);
virtual bool OnKeyPress(int key, int scan, bool repeat, bool shift, bool ctrl, bool alt);
virtual bool OnKeyRelease(int key, int scan, bool repeat, bool shift, bool ctrl, bool alt);
virtual bool OnMouseTick();
virtual void OnTick();
virtual void Init();

View File

@ -98,8 +98,8 @@ LuaWindow::LuaWindow(lua_State * l) :
virtual void OnMouseDown(int x, int y, unsigned button) { luaWindow->triggerOnMouseDown(x, y, button); }
virtual void OnMouseUp(int x, int y, unsigned button) { luaWindow->triggerOnMouseUp(x, y, button); }
virtual void OnMouseWheel(int x, int y, int d) { luaWindow->triggerOnMouseWheel(x, y, d); }
virtual void OnKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool alt) { luaWindow->triggerOnKeyPress(key, character, shift, ctrl, alt); }
virtual void OnKeyRelease(int key, Uint16 character, bool shift, bool ctrl, bool alt) { luaWindow->triggerOnKeyRelease(key, character, shift, ctrl, alt); }
virtual void OnKeyPress(int key, int scan, bool repeat, bool shift, bool ctrl, bool alt) { luaWindow->triggerOnKeyPress(key, scan, repeat, shift, ctrl, alt); }
virtual void OnKeyRelease(int key, int scan, bool repeat, bool shift, bool ctrl, bool alt) { luaWindow->triggerOnKeyRelease(key, scan, repeat, shift, ctrl, alt); }
};
window = new DrawnWindow(ui::Point(posX, posY), ui::Point(sizeX, sizeY), this);
@ -359,13 +359,13 @@ void LuaWindow::triggerOnMouseWheel(int x, int y, int d)
}
}
void LuaWindow::triggerOnKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool alt)
void LuaWindow::triggerOnKeyPress(int key, int scan, bool repeat, bool shift, bool ctrl, bool alt)
{
if(onKeyPressFunction)
{
lua_rawgeti(l, LUA_REGISTRYINDEX, onKeyPressFunction);
lua_pushinteger(l, key);
lua_pushinteger(l, character);
lua_pushinteger(l, scan);
lua_pushboolean(l, shift);
lua_pushboolean(l, ctrl);
lua_pushboolean(l, alt);
@ -376,13 +376,13 @@ void LuaWindow::triggerOnKeyPress(int key, Uint16 character, bool shift, bool ct
}
}
void LuaWindow::triggerOnKeyRelease(int key, Uint16 character, bool shift, bool ctrl, bool alt)
void LuaWindow::triggerOnKeyRelease(int key, int scan, bool repeat, bool shift, bool ctrl, bool alt)
{
if(onKeyReleaseFunction)
{
lua_rawgeti(l, LUA_REGISTRYINDEX, onKeyReleaseFunction);
lua_pushinteger(l, key);
lua_pushinteger(l, character);
lua_pushinteger(l, scan);
lua_pushboolean(l, shift);
lua_pushboolean(l, ctrl);
lua_pushboolean(l, alt);

View File

@ -60,8 +60,8 @@ class LuaWindow
void triggerOnMouseDown(int x, int y, unsigned button);
void triggerOnMouseUp(int x, int y, unsigned button);
void triggerOnMouseWheel(int x, int y, int d);
void triggerOnKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool alt);
void triggerOnKeyRelease(int key, Uint16 character, bool shift, bool ctrl, bool alt);
void triggerOnKeyPress(int key, int scan, bool repeat, bool shift, bool ctrl, bool alt);
void triggerOnKeyRelease(int key, int scan, bool repeat, bool shift, bool ctrl, bool alt);
public:
LuaScriptInterface * ci;