Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
117 changes: 69 additions & 48 deletions include/game_window.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,43 @@
#include "key_mapping.h"

enum class GraphicsApi {
OPENGL, OPENGL_ES2
OPENGL,
OPENGL_ES2
};
enum class KeyAction {
PRESS, REPEAT, RELEASE
PRESS,
REPEAT,
RELEASE
};
enum class MouseButtonAction {
PRESS, RELEASE
PRESS,
RELEASE
};
enum class GamepadButtonId {
A, B, X, Y, LB, RB, BACK, START, GUIDE, LEFT_STICK, RIGHT_STICK,
DPAD_UP, DPAD_RIGHT, DPAD_DOWN, DPAD_LEFT,
A,
B,
X,
Y,
LB,
RB,
BACK,
START,
GUIDE,
LEFT_STICK,
RIGHT_STICK,
DPAD_UP,
DPAD_RIGHT,
DPAD_DOWN,
DPAD_LEFT,
UNKNOWN = -1
};
enum class GamepadAxisId {
LEFT_X, LEFT_Y, RIGHT_X, RIGHT_Y, LEFT_TRIGGER, RIGHT_TRIGGER,
LEFT_X,
LEFT_Y,
RIGHT_X,
RIGHT_Y,
LEFT_TRIGGER,
RIGHT_TRIGGER,
UNKNOWN = -1
};
struct FullscreenMode {
Expand All @@ -29,24 +51,23 @@ struct FullscreenMode {
};

class GameWindow {

public:
using DrawCallback = std::function<void ()>;
using WindowSizeCallback = std::function<void (int, int)>;
using MouseButtonCallback = std::function<void (double, double, int, MouseButtonAction)>;
using MousePositionCallback = std::function<void (double, double)>;
using MouseScrollCallback = std::function<void (double, double, double, double)>;
using TouchStartCallback = std::function<void (int, double, double)>;
using TouchUpdateCallback = std::function<void (int, double, double)>;
using TouchEndCallback = std::function<void (int, double, double)>;
using KeyboardCallback = std::function<void (KeyCode, KeyAction)>;
using KeyboardTextCallback = std::function<void (std::string const&)>;
using DropCallback = std::function<void (std::string const&)>;
using PasteCallback = std::function<void (std::string const&)>;
using GamepadStateCallback = std::function<void (int, bool)>;
using GamepadButtonCallback = std::function<void (int, GamepadButtonId, bool)>;
using GamepadAxisCallback = std::function<void (int, GamepadAxisId, float)>;
using CloseCallback = std::function<void ()>;
using DrawCallback = std::function<void()>;
using WindowSizeCallback = std::function<void(int, int)>;
using MouseButtonCallback = std::function<void(double, double, int, MouseButtonAction)>;
using MousePositionCallback = std::function<void(double, double)>;
using MouseScrollCallback = std::function<void(double, double, double, double)>;
using TouchStartCallback = std::function<void(int, double, double)>;
using TouchUpdateCallback = std::function<void(int, double, double)>;
using TouchEndCallback = std::function<void(int, double, double)>;
using KeyboardCallback = std::function<void(KeyCode, KeyAction, int)>;
using KeyboardTextCallback = std::function<void(std::string const&)>;
using DropCallback = std::function<void(std::string const&)>;
using PasteCallback = std::function<void(std::string const&)>;
using GamepadStateCallback = std::function<void(int, bool)>;
using GamepadButtonCallback = std::function<void(int, GamepadButtonId, bool)>;
using GamepadAxisCallback = std::function<void(int, GamepadAxisId, float)>;
using CloseCallback = std::function<void()>;

private:
DrawCallback drawCallback;
Expand All @@ -67,7 +88,6 @@ class GameWindow {
CloseCallback closeCallback;

public:

GameWindow(std::string const& title, int width, int height, GraphicsApi api) {}

virtual ~GameWindow() {}
Expand Down Expand Up @@ -100,19 +120,23 @@ class GameWindow {
virtual void setSwapInterval(int interval) = 0;

virtual void startTextInput() {}

virtual void stopTextInput() {}

virtual void setFullscreenMode(const FullscreenMode& mode) {}

virtual FullscreenMode getFullscreenMode() {
return { -1 };
return {-1};
}

virtual std::vector<FullscreenMode> getFullscreenModes() {
return {};
}

virtual uint32_t getKeyFromKeyCode(KeyCode code, int metaState) {
return 0;
}

void setDrawCallback(DrawCallback callback) { drawCallback = std::move(callback); }

void setWindowSizeCallback(WindowSizeCallback callback) { windowSizeCallback = std::move(callback); }
Expand Down Expand Up @@ -148,77 +172,74 @@ class GameWindow {

void setCloseCallback(CloseCallback callback) { closeCallback = std::move(callback); }


protected:

void onDraw() {
if (drawCallback != nullptr)
if(drawCallback != nullptr)
drawCallback();
}
void onWindowSizeChanged(int w, int h) {
if (windowSizeCallback != nullptr)
if(windowSizeCallback != nullptr)
windowSizeCallback(w, h);
}
void onMouseButton(double x, double y, int button, MouseButtonAction action) {
if (mouseButtonCallback != nullptr)
if(mouseButtonCallback != nullptr)
mouseButtonCallback(x, y, button, action);
}
void onMousePosition(double x, double y) {
if (mousePositionCallback != nullptr)
if(mousePositionCallback != nullptr)
mousePositionCallback(x, y);
}
void onMouseRelativePosition(double x, double y) {
if (mouseRelativePositionCallback != nullptr)
if(mouseRelativePositionCallback != nullptr)
mouseRelativePositionCallback(x, y);
}
void onMouseScroll(double x, double y, double dx, double dy) {
if (mouseScrollCallback != nullptr)
if(mouseScrollCallback != nullptr)
mouseScrollCallback(x, y, dx, dy);
}
void onTouchStart(int id, double x, double y) {
if (touchStartCallback != nullptr)
if(touchStartCallback != nullptr)
touchStartCallback(id, x, y);
}
void onTouchUpdate(int id, double x, double y) {
if (touchUpdateCallback != nullptr)
if(touchUpdateCallback != nullptr)
touchUpdateCallback(id, x, y);
}
void onTouchEnd(int id, double x, double y) {
if (touchEndCallback != nullptr)
if(touchEndCallback != nullptr)
touchEndCallback(id, x, y);
}
void onKeyboard(KeyCode key, KeyAction action) {
if (keyboardCallback != nullptr)
keyboardCallback(key, action);
void onKeyboard(KeyCode key, KeyAction action, int mods) {
if(keyboardCallback != nullptr)
keyboardCallback(key, action, mods);
}
void onKeyboardText(std::string const& c) {
if (keyboardTextCallback != nullptr)
if(keyboardTextCallback != nullptr)
keyboardTextCallback(c);
}
void onDrop(std::string const& path) {
if (dropCallback != nullptr) {
if(dropCallback != nullptr) {
dropCallback(path);
}
}
void onPaste(std::string const& c) {
if (pasteCallback != nullptr)
if(pasteCallback != nullptr)
pasteCallback(c);
}
void onGamepadState(int id, bool connected) {
if (gamepadStateCallback != nullptr)
if(gamepadStateCallback != nullptr)
gamepadStateCallback(id, connected);
}
void onGamepadButton(int id, GamepadButtonId btn, bool pressed) {
if (gamepadButtonCallback != nullptr && btn != GamepadButtonId::UNKNOWN)
if(gamepadButtonCallback != nullptr && btn != GamepadButtonId::UNKNOWN)
gamepadButtonCallback(id, btn, pressed);
}
void onGamepadAxis(int id, GamepadAxisId axis, float val) {
if (gamepadAxisCallback != nullptr && axis != GamepadAxisId::UNKNOWN)
if(gamepadAxisCallback != nullptr && axis != GamepadAxisId::UNKNOWN)
gamepadAxisCallback(id, axis, val);
}
void onClose() {
if (closeCallback != nullptr)
if(closeCallback != nullptr)
closeCallback();
}

};
86 changes: 76 additions & 10 deletions include/key_mapping.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ enum class KeyCode {
TAB = 9,
ENTER = 13,
LEFT_SHIFT = 16,
RIGHT_SHIFT = 16|256,
RIGHT_SHIFT = 16 | 256,
LEFT_CTRL = 17,
RIGHT_CTRL = 17|256,
RIGHT_CTRL = 17 | 256,
PAUSE = 19,
CAPS_LOCK = 20,
ESCAPE = 27,
Expand All @@ -24,11 +24,70 @@ enum class KeyCode {
DOWN = 40,
INSERT = 45,
DELETE = 46,
NUM_0 = 48, NUM_1, NUM_2, NUM_3, NUM_4, NUM_5, NUM_6, NUM_7, NUM_8, NUM_9 = 57,
NUMPAD_0 = 0x60, NUMPAD_1, NUMPAD_2, NUMPAD_3, NUMPAD_4, NUMPAD_5, NUMPAD_6, NUMPAD_7, NUMPAD_8, NUMPAD_9 = 0x69,
NUMPAD_MULTIPLY = 0x6a, NUMPAD_ADD, NUMPAD_SEPERATOR, NUMPAD_SUBTRACT, NUMPAD_DECIMAL, NUMPAD_DIVIDE = 0x6F,
A = 65, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z = 90,
FN1 = 112, FN2, FN3, FN4, FN5, FN6, FN7, FN8, FN9, FN10, FN11, FN12,
NUM_0 = 48,
NUM_1,
NUM_2,
NUM_3,
NUM_4,
NUM_5,
NUM_6,
NUM_7,
NUM_8,
NUM_9 = 57,
NUMPAD_0 = 0x60,
NUMPAD_1,
NUMPAD_2,
NUMPAD_3,
NUMPAD_4,
NUMPAD_5,
NUMPAD_6,
NUMPAD_7,
NUMPAD_8,
NUMPAD_9 = 0x69,
NUMPAD_MULTIPLY = 0x6a,
NUMPAD_ADD,
NUMPAD_SEPERATOR,
NUMPAD_SUBTRACT,
NUMPAD_DECIMAL,
NUMPAD_DIVIDE = 0x6F,
A = 65,
B,
C,
D,
E,
F,
G,
H,
I,
J,
K,
L,
M,
N,
O,
P,
Q,
R,
S,
T,
U,
V,
W,
X,
Y,
Z = 90,
FN1 = 112,
FN2,
FN3,
FN4,
FN5,
FN6,
FN7,
FN8,
FN9,
FN10,
FN11,
FN12,
NUM_LOCK = 144,
SCROLL_LOCK = 145,
SEMICOLON = 186,
Expand All @@ -46,7 +105,14 @@ enum class KeyCode {

// Keys not defined in Minecraft but we use them anyways
LEFT_SUPER = 1,
RIGHT_SUPER = 1|256,
RIGHT_SUPER = 1 | 256,
LEFT_ALT = 0x12,
RIGHT_ALT = 0x12|256
};
RIGHT_ALT = 0x12 | 256
};

#define KEY_MOD_SHIFT (1 << 0)
#define KEY_MOD_CTRL (1 << 1)
#define KEY_MOD_SUPER (1 << 2)
#define KEY_MOD_ALT (1 << 3)
#define KEY_MOD_CAPSLOCK (1 << 4)
#define KEY_MOD_NUMLOCK (1 << 5)
Loading