Magic_Game/src/core/Event.hpp

164 lines
3.2 KiB
C++
Raw Normal View History

2018-10-03 22:02:46 +08:00
// Copyright (c) 2016-2018 Easy2D - Nomango
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#pragma once
#include "macros.h"
2019-01-24 12:21:01 +08:00
#include "keys.hpp"
2018-11-08 00:21:59 +08:00
namespace easy2d
{
2019-01-24 12:21:01 +08:00
// <20><><EFBFBD><EFBFBD><EFBFBD>¼<EFBFBD>
struct MouseEvent
2018-11-22 19:31:44 +08:00
{
2019-01-24 12:21:01 +08:00
float x;
float y;
bool left_btn_down; // <20><><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD>
bool right_btn_down; // <20>Ҽ<EFBFBD><D2BC>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD>
struct // Events::MouseDown | Events::MouseUp | Events::MouseClick
2019-01-24 12:21:01 +08:00
{
int button;
2019-01-24 12:21:01 +08:00
};
struct // Events::MouseWheel
2019-01-24 12:21:01 +08:00
{
float wheel;
2019-01-24 12:21:01 +08:00
};
static bool Check(UINT type);
2019-01-24 12:21:01 +08:00
};
// <20><><EFBFBD><EFBFBD><EFBFBD>¼<EFBFBD>
struct KeyboardEvent
{
int code; // enum KeyCode
2019-01-24 12:21:01 +08:00
int count;
static bool Check(UINT type);
2018-11-22 19:31:44 +08:00
};
2019-01-24 12:21:01 +08:00
// <20><><EFBFBD><EFBFBD><EFBFBD>¼<EFBFBD>
struct WindowEvent
2018-11-22 19:31:44 +08:00
{
2019-01-24 22:22:11 +08:00
union
{
struct // Events::WindowMoved
2019-01-24 22:22:11 +08:00
{
int x;
int y;
};
struct // Events::WindowResized
2019-01-24 22:22:11 +08:00
{
int width;
int height;
};
struct // Events::WindowFocusChanged
2019-01-24 22:22:11 +08:00
{
bool focus;
};
struct // Events::WindowTitleChanged
2019-01-24 22:22:11 +08:00
{
const wchar_t* title;
};
};
static bool Check(UINT type);
};
// <20>Զ<EFBFBD><D4B6><EFBFBD><EFBFBD>¼<EFBFBD>
struct CustomEvent
{
void* data;
2019-01-24 12:21:01 +08:00
};
// <20>¼<EFBFBD>
struct E2D_API Event
2019-01-24 12:21:01 +08:00
{
enum Type : UINT
{
First,
// <20><><EFBFBD><EFBFBD><EFBFBD>¼<EFBFBD>
MouseFirst,
MouseMove, // <20>ƶ<EFBFBD>
MouseBtnDown, // <20><><EFBFBD><EFBFBD><EAB0B4>
MouseBtnUp, // <20><><EFBFBD><EFBFBD>̧<EFBFBD><CCA7>
MouseWheel, // <20><><EFBFBD>ֹ<EFBFBD><D6B9><EFBFBD>
MouseHover, // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
MouseOut, // <20><><EFBFBD><EFBFBD><EFBFBD>Ƴ<EFBFBD>
Click, // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
MouseLast,
// <20><><EFBFBD><EFBFBD><EFBFBD>¼<EFBFBD>
KeyFirst,
KeyDown, // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
KeyUp, // <20><><EFBFBD><EFBFBD>̧<EFBFBD><CCA7>
KeyLast,
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ
WindowFirst,
WindowMoved, // <20><><EFBFBD><EFBFBD><EFBFBD>ƶ<EFBFBD>
WindowResized, // <20><><EFBFBD>ڴ<EFBFBD>С<EFBFBD>
WindowFocusChanged, // <20><><EFBFBD>û<EFBFBD>ʧȥ<CAA7><C8A5><EFBFBD><EFBFBD>
WindowTitleChanged, // <20><><EFBFBD><EFBFBD><EFBFBD>
WindowClosed, // <20><><EFBFBD>ڱ<EFBFBD><DAB1>ر<EFBFBD>
WindowLast,
Last
};
UINT type;
2019-01-26 13:09:58 +08:00
Node* target;
2019-01-24 12:21:01 +08:00
union
{
MouseEvent mouse;
KeyboardEvent key;
WindowEvent win;
CustomEvent custom;
2019-01-24 12:21:01 +08:00
};
Event(UINT type = Type::First) : type(type), target(nullptr) {}
};
// Check-functions
inline bool MouseEvent::Check(UINT type)
{
return type > Event::MouseFirst && type < Event::MouseLast;
}
inline bool KeyboardEvent::Check(UINT type)
{
return type > Event::KeyFirst && type < Event::KeyLast;
}
inline bool WindowEvent::Check(UINT type)
{
return type > Event::WindowFirst && type < Event::WindowLast;
}
}