Add partially-finished SDL wrapper code to betawidfet.

git-svn-id: svn+ssh://svn.gna.org/svn/warzone/trunk@5881 4a71c877-e1ca-e34f-864e-861f7616d084
master
Freddie Witherden 2008-08-28 15:34:21 +00:00
parent d5b341334d
commit a06bb014df
4 changed files with 236 additions and 0 deletions

View File

@ -0,0 +1,184 @@
/*
This file is part of Warzone 2100.
Copyright (C) 2008 Freddie Witherden
Copyright (C) 2008 Warzone Resurrection Project
Warzone 2100 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 2 of the License, or
(at your option) any later version.
Warzone 2100 is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Warzone 2100; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "event.h"
#include "../../widget.h"
#include "../../window.h"
/**
* Converts an SDLkey to an eventKeycode (as used by the widget system).
*
* @param key The SDL key to convert from.
* @return The equivalent eventKeycode, or EVT_KEYCODE_UNKNOWN if there is no
* equivalent.
*/
static eventKeycode SDLKeyToEventKeycode(SDLKey key)
{
switch (key)
{
case SDLK_BACKSPACE:
return EVT_KEYCODE_BACKSPACE;
case SDLK_TAB:
return EVT_KEYCODE_TAB;
case SDLK_RETURN:
return EVT_KEYCODE_RETURN;
case SDLK_ESCAPE:
return EVT_KEYCODE_ESCAPE;
case SDLK_KP_ENTER:
return EVT_KEYCODE_ENTER;
case SDLK_LEFT:
return EVT_KEYCODE_LEFT;
case SDLK_RIGHT:
return EVT_KEYCODE_RIGHT;
case SDLK_UP:
return EVT_KEYCODE_UP;
case SDLK_DOWN:
return EVT_KEYCODE_DOWN;
case SDLK_INSERT:
return EVT_KEYCODE_INSERT;
case SDLK_DELETE:
return EVT_KEYCODE_DELETE;
case SDLK_HOME:
return EVT_KEYCODE_HOME;
case SDLK_END:
return EVT_KEYCODE_END;
case SDLK_PAGEUP:
return EVT_KEYCODE_PAGEUP;
case SDLK_PAGEDOWN:
return EVT_KEYCODE_PAGEDOWN;
default:
return EVT_KEYCODE_UNKNOWN;
}
}
/**
* Converts SDL mouse button ids to their corresponding mouseButton enumeration.
*
* @param buttonId The button which was pressed.
* @return The corresponding mouseButton enum.
*/
static mouseButton SDLButtonToMouseButton(int buttonId)
{
switch (buttonId)
{
case 1:
return BUTTON_LEFT;
case 2:
return BUTTON_RIGHT;
case 4:
return BUTTON_WHEEL_UP;
case 5:
return BUTTON_WHEEL_DOWN;
default:
return BUTTON_OTHER;
}
}
void widgetHandleSDLEvent(const SDL_Event *sdlEvt)
{
// Last known location of the mouse
static point previousMouseLoc = { -1, -1 };
switch (sdlEvt->type)
{
case SDL_MOUSEMOTION:
{
eventMouse evtMouse;
evtMouse.event.time = widgetGetTime();
evtMouse.event.type = EVT_MOUSE_MOVE;
// Location
evtMouse.loc.x = sdlEvt->motion.x;
evtMouse.loc.y = sdlEvt->motion.y;
// Previous location
evtMouse.previousLoc = previousMouseLoc;
// Update the previous location
previousMouseLoc = evtMouse.loc;
windowHandleEventForWindowVector((event *) &evtMouse);
break;
}
case SDL_MOUSEBUTTONDOWN:
case SDL_MOUSEBUTTONUP:
{
eventMouseBtn evtMouseBtn;
evtMouseBtn.event.time = widgetGetTime();
evtMouseBtn.event.type = (sdlEvt->button.state == SDL_PRESSED) ?
EVT_MOUSE_DOWN : EVT_MOUSE_UP;
// Location
evtMouseBtn.loc.x = sdlEvt->button.x;
evtMouseBtn.loc.y = sdlEvt->button.y;
// Update the previous location
previousMouseLoc = evtMouseBtn.loc;
// Button pressed/released
evtMouseBtn.button = SDLButtonToMouseButton(sdlEvt->button.button);
windowHandleEventForWindowVector((event *) &evtMouseBtn);
break;
}
case SDL_KEYDOWN:
case SDL_KEYUP:
{
eventKey evtKey;
evtKey.event.time = widgetGetTime();
evtKey.event.type = (sdlEvt->key.type == SDL_KEYDOWN) ?
EVT_KEY_DOWN : EVT_KEY_UP;
// Key pressed/released
evtKey.keycode = SDLKeyToEventKeycode(sdlEvt->key.keysym.sym);
// Modifier keys
if (sdlEvt->key.keysym.mod & KMOD_CTRL)
{
evtKey.ctrl = true;
}
if (sdlEvt->key.keysym.mod & KMOD_ALT)
{
evtKey.alt = true;
}
if (sdlEvt->key.keysym.mod & KMOD_SHIFT)
{
evtKey.shift = true;
}
// FIXME: Add support for text events!
windowHandleEventForWindowVector((event *) &evtKey);
break;
}
}
}
void widgetFireTimers()
{
// Create the generic timer event
eventTimer evtTimer;
evtTimer.event.time = widgetGetTime();
evtTimer.event.type = EVT_TIMER;
// Dispatch
windowHandleEventForWindowVector((event *) &evtTimer);
}

View File

@ -0,0 +1,31 @@
/*
This file is part of Warzone 2100.
Copyright (C) 2008 Freddie Witherden
Copyright (C) 2008 Warzone Resurrection Project
Warzone 2100 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 2 of the License, or
(at your option) any later version.
Warzone 2100 is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Warzone 2100; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef PLATFORM_SDL_EVENT_H_
#define PLATFORM_SDL_EVENT_H_
#include <SDL.h>
void widgetHandleSDLEvent(const SDL_Event *sdlEvt);
void widgetFireTimers(void);
#endif /*PLATFORM_SDL_EVENT_H_*/

View File

@ -171,6 +171,20 @@ vector *windowGetWindowVector()
return windowVector;
}
void windowHandleEventForWindowVector(const event *evt)
{
int i;
// Make sure the vector is valid
assert(windowVector != NULL);
// Pass the event to each of the windows
for (i = 0; i < vectorSize(windowVector); i++)
{
widgetHandleEvent(vectorAt(windowVector, i), evt);
}
}
void windowSetScreenSize(int w, int h)
{
screenWidth = w;

View File

@ -96,6 +96,13 @@ void windowSetWindowVector(vector *v);
*/
vector *windowGetWindowVector(void);
/**
* Passes the event evt to each window in the current window vector.
*
* @param evt The event to handle.
*/
void windowHandleEventForWindowVector(const event *evt);
/**
* Sets the size of the screen to (w,h). It is important that the screen size
* be set before any calls to windowRepositionFromScreen are made.