add initial examples, replacing the 'Demos/Demos3'. Will make it work cross-platform, OpenGL3/OpenGL2 and add more examples to it.

This commit is contained in:
erwincoumans
2015-04-16 09:55:32 -07:00
parent d9feaf2d2a
commit a1bf9c5556
425 changed files with 255913 additions and 0 deletions

View File

@@ -0,0 +1,117 @@
/*
GWEN
Copyright (c) 2011 Facepunch Studios
See license in Gwen.h
*/
#ifndef GWEN_INPUT_SDL13_H
#define GWEN_INPUT_SDL13_H
#include "Gwen/InputHandler.h"
#include "Gwen/Gwen.h"
#include "Gwen/Controls/Canvas.h"
#ifdef _WIN32
#define UCS_STRING "UCS-2"
#else
#define UCS_STRING "UCS-4"
#endif
namespace Gwen
{
namespace Input
{
class SDL13
{
public:
SDL13()
{
m_Canvas = NULL;
}
void Initialize( Gwen::Controls::Canvas* c )
{
m_Canvas = c;
}
bool ProcessEvent( SDL_Event* Event )
{
if (!m_Canvas) return false;
switch(Event->type)
{
case SDL_KEYUP:
case SDL_KEYDOWN:
{
SDL_KeyboardEvent* E = &Event->key;
int iKey = -1;
SDL_scancode scancode = E->keysym.scancode;
switch (scancode) {
case SDL_SCANCODE_RETURN: iKey = Gwen::Key::Return; break;
case SDL_SCANCODE_BACKSPACE: iKey = Gwen::Key::Backspace; break;
case SDL_SCANCODE_DELETE: iKey = Gwen::Key::Delete; break;
case SDL_SCANCODE_LEFT: iKey = Gwen::Key::Left; break;
case SDL_SCANCODE_RIGHT: iKey = Gwen::Key::Right; break;
case SDL_SCANCODE_LSHIFT: iKey = Gwen::Key::Shift; break;
case SDL_SCANCODE_RSHIFT: iKey = Gwen::Key::Shift; break;
case SDL_SCANCODE_TAB: iKey = Gwen::Key::Tab; break;
case SDL_SCANCODE_SPACE: iKey = Gwen::Key::Space; break;
case SDL_SCANCODE_HOME: iKey = Gwen::Key::Home; break;
case SDL_SCANCODE_END: iKey = Gwen::Key::End; break;
case SDL_SCANCODE_LCTRL: iKey = Gwen::Key::Control; break;
case SDL_SCANCODE_RCTRL: iKey = Gwen::Key::Control; break;
case SDL_SCANCODE_UP: iKey = Gwen::Key::Up; break;
case SDL_SCANCODE_DOWN: iKey = Gwen::Key::Down; break;
case SDL_SCANCODE_ESCAPE: iKey = Gwen::Key::Escape; break;
case SDL_SCANCODE_LALT: iKey = Gwen::Key::Alt; break;
case SDL_SCANCODE_RALT: iKey = Gwen::Key::Alt; break;
default: return false;
}
return m_Canvas->InputKey(iKey, E->state);
}
case SDL_TEXTINPUT:
{
SDL_TextInputEvent* E = &Event->text;
wchar_t* widechar = (wchar_t*)SDL_iconv_string(UCS_STRING, "UTF-8", E->text, SDL_strlen(E->text)+1);
bool ret = m_Canvas->InputCharacter(*widechar);
SDL_free(widechar);
return ret;
}
case SDL_MOUSEMOTION:
{
SDL_MouseMotionEvent* E = &Event->motion;
return m_Canvas->InputMouseMoved(E->x, E->y, E->xrel, E->yrel);
}
case SDL_MOUSEBUTTONDOWN:
case SDL_MOUSEBUTTONUP:
{
SDL_MouseButtonEvent* E = &Event->button;
int Button = -1;
switch (E->button) {
case SDL_BUTTON_LEFT: Button = 0; break;
case SDL_BUTTON_MIDDLE: Button = 2; break;
case SDL_BUTTON_RIGHT: Button = 1; break;
default: return false;
}
return m_Canvas->InputMouseButton(Button, E->state);
}
case SDL_MOUSEWHEEL:
{
SDL_MouseWheelEvent* E = &Event->wheel;
return m_Canvas->InputMouseWheel(E->y);
}
default:
{
return false;
}
}
}
protected:
Gwen::Controls::Canvas* m_Canvas;
};
}
}
#endif

View File

@@ -0,0 +1,148 @@
/*
GWEN
Copyright (c) 2011 Facepunch Studios
See license in Gwen.h
*/
#pragma once
#ifndef GWEN_INPUT_SFML_H
#define GWEN_INPUT_SFML_H
#include "Gwen/InputHandler.h"
#include "Gwen/Gwen.h"
#include "Gwen/Controls/Canvas.h"
#include <SFML/Graphics.hpp>
namespace Gwen
{
namespace Input
{
class SFML
{
public:
SFML()
{
m_Canvas = NULL;
m_MouseX = 0;
m_MouseY = 0;
}
void Initialize( Gwen::Controls::Canvas* c )
{
m_Canvas = c;
}
unsigned char TranslateKeyCode( int iKeyCode )
{
switch ( iKeyCode )
{
#if SFML_VERSION_MAJOR == 2
case sf::Keyboard::Back: return Gwen::Key::Backspace;
case sf::Keyboard::Return: return Gwen::Key::Return;
case sf::Keyboard::Escape: return Gwen::Key::Escape;
case sf::Keyboard::Tab: return Gwen::Key::Tab;
case sf::Keyboard::Space: return Gwen::Key::Space;
case sf::Keyboard::Up: return Gwen::Key::Up;
case sf::Keyboard::Down: return Gwen::Key::Down;
case sf::Keyboard::Left: return Gwen::Key::Left;
case sf::Keyboard::Right: return Gwen::Key::Right;
case sf::Keyboard::Home: return Gwen::Key::Home;
case sf::Keyboard::End: return Gwen::Key::End;
case sf::Keyboard::Delete: return Gwen::Key::Delete;
case sf::Keyboard::LControl: return Gwen::Key::Control;
case sf::Keyboard::LAlt: return Gwen::Key::Alt;
case sf::Keyboard::LShift: return Gwen::Key::Shift;
case sf::Keyboard::RControl: return Gwen::Key::Control;
case sf::Keyboard::RAlt: return Gwen::Key::Alt;
case sf::Keyboard::RShift: return Gwen::Key::Shift;
#else
case sf::Key::Back: return Gwen::Key::Backspace;
case sf::Key::Return: return Gwen::Key::Return;
case sf::Key::Escape: return Gwen::Key::Escape;
case sf::Key::Tab: return Gwen::Key::Tab;
case sf::Key::Space: return Gwen::Key::Space;
case sf::Key::Up: return Gwen::Key::Up;
case sf::Key::Down: return Gwen::Key::Down;
case sf::Key::Left: return Gwen::Key::Left;
case sf::Key::Right: return Gwen::Key::Right;
case sf::Key::Home: return Gwen::Key::Home;
case sf::Key::End: return Gwen::Key::End;
case sf::Key::Delete: return Gwen::Key::Delete;
case sf::Key::LControl: return Gwen::Key::Control;
case sf::Key::LAlt: return Gwen::Key::Alt;
case sf::Key::LShift: return Gwen::Key::Shift;
case sf::Key::RControl: return Gwen::Key::Control;
case sf::Key::RAlt: return Gwen::Key::Alt;
case sf::Key::RShift: return Gwen::Key::Shift;
#endif
}
return Gwen::Key::Invalid;
}
bool ProcessMessage( sf::Event& event )
{
if ( !m_Canvas ) return false;
switch(event.Type)
{
case sf::Event::MouseMoved:
{
int dx = event.MouseMove.X - m_MouseX;
int dy = event.MouseMove.Y - m_MouseY;
m_MouseX = event.MouseMove.X;
m_MouseY = event.MouseMove.Y;
return m_Canvas->InputMouseMoved( m_MouseX, m_MouseY, dx, dy );
}
case sf::Event::MouseButtonPressed:
case sf::Event::MouseButtonReleased:
{
return m_Canvas->InputMouseButton( event.MouseButton.Button, event.Type == sf::Event::MouseButtonPressed );
}
case sf::Event::MouseWheelMoved:
{
return m_Canvas->InputMouseWheel( event.MouseWheel.Delta * 60 );
}
case sf::Event::TextEntered:
{
return m_Canvas->InputCharacter( event.Text.Unicode );
}
case sf::Event::KeyPressed:
case sf::Event::KeyReleased:
{
bool bPressed = (event.Type == sf::Event::KeyPressed);
if ( event.Key.Control && bPressed && event.Key.Code >= 'a' && event.Key.Code <= 'z' )
{
return m_Canvas->InputCharacter( event.Key.Code );
}
unsigned char iKey = TranslateKeyCode( event.Key.Code );
return m_Canvas->InputKey( iKey, bPressed );
}
}
return false;
}
protected:
Gwen::Controls::Canvas* m_Canvas;
int m_MouseX;
int m_MouseY;
};
}
}
#endif

View File

@@ -0,0 +1,164 @@
/*
GWEN
Copyright (c) 2011 Facepunch Studios
See license in Gwen.h
*/
#ifndef GWEN_INPUT_WINDOWS_H
#define GWEN_INPUT_WINDOWS_H
#include "Gwen/InputHandler.h"
#include "Gwen/Gwen.h"
#include "Gwen/Controls/Canvas.h"
#include <windows.h>
namespace Gwen
{
namespace Input
{
class Windows
{
public:
Windows()
{
m_Canvas = NULL;
m_MouseX = 0;
m_MouseY = 0;
}
void Initialize( Gwen::Controls::Canvas* c )
{
m_Canvas = c;
}
bool ProcessMessage( MSG msg )
{
if ( !m_Canvas ) return false;
switch ( msg.message )
{
case WM_MOUSEMOVE:
{
int x = (signed short)LOWORD( msg.lParam );
int y = (signed short)HIWORD( msg.lParam );
int dx = x - m_MouseX;
int dy = y - m_MouseY;
m_MouseX = x;
m_MouseY = y;
return m_Canvas->InputMouseMoved( x, y, dx, dy );
}
case WM_CHAR:
{
Gwen::UnicodeChar chr = (Gwen::UnicodeChar)msg.wParam;
return m_Canvas->InputCharacter( chr );
}
case WM_MOUSEWHEEL:
{
return m_Canvas->InputMouseWheel( (short)HIWORD( msg.wParam ) );
}
case WM_LBUTTONDOWN:
{
SetCapture( msg.hwnd );
return m_Canvas->InputMouseButton( 0, true );
}
case WM_LBUTTONUP:
{
ReleaseCapture();
return m_Canvas->InputMouseButton( 0, false );
}
case WM_RBUTTONDOWN:
{
SetCapture( msg.hwnd );
return m_Canvas->InputMouseButton( 1, true );
}
case WM_RBUTTONUP:
{
ReleaseCapture();
return m_Canvas->InputMouseButton( 1, false );
}
case WM_MBUTTONDOWN:
{
SetCapture( msg.hwnd );
return m_Canvas->InputMouseButton( 2, true );
}
case WM_MBUTTONUP:
{
ReleaseCapture();
return m_Canvas->InputMouseButton( 2, true );
}
case WM_LBUTTONDBLCLK:
case WM_RBUTTONDBLCLK:
case WM_MBUTTONDBLCLK:
{
// Filter out those events from the application
return true;
}
case WM_KEYDOWN:
case WM_KEYUP:
{
bool bDown = msg.message == WM_KEYDOWN;
int iKey = -1;
// These aren't sent by WM_CHAR when CTRL is down - but we need
// them internally for copy and paste etc..
if ( bDown && GetKeyState( VK_CONTROL ) & 0x80 && msg.wParam >= 'A' && msg.wParam <= 'Z' )
{
Gwen::UnicodeChar chr = (Gwen::UnicodeChar)msg.wParam;
return m_Canvas->InputCharacter( chr );
}
if ( msg.wParam == VK_SHIFT ) iKey = Gwen::Key::Shift;
else if ( msg.wParam == VK_RETURN ) iKey = Gwen::Key::Return;
else if ( msg.wParam == VK_BACK ) iKey = Gwen::Key::Backspace;
else if ( msg.wParam == VK_DELETE ) iKey = Gwen::Key::Delete;
else if ( msg.wParam == VK_LEFT ) iKey = Gwen::Key::Left;
else if ( msg.wParam == VK_RIGHT ) iKey = Gwen::Key::Right;
else if ( msg.wParam == VK_TAB ) iKey = Gwen::Key::Tab;
else if ( msg.wParam == VK_SPACE ) iKey = Gwen::Key::Space;
else if ( msg.wParam == VK_HOME ) iKey = Gwen::Key::Home;
else if ( msg.wParam == VK_END ) iKey = Gwen::Key::End;
else if ( msg.wParam == VK_CONTROL ) iKey = Gwen::Key::Control;
else if ( msg.wParam == VK_SPACE ) iKey = Gwen::Key::Space;
else if ( msg.wParam == VK_UP ) iKey = Gwen::Key::Up;
else if ( msg.wParam == VK_DOWN ) iKey = Gwen::Key::Down;
if ( iKey != -1 )
{
return m_Canvas->InputKey( iKey, bDown );
}
break;
}
default:
{
break;
}
}
return false;
}
protected:
Gwen::Controls::Canvas* m_Canvas;
int m_MouseX;
int m_MouseY;
};
}
}
#endif