280 lines
8.5 KiB
C++
280 lines
8.5 KiB
C++
bool AppIsRunning = true;
|
|
bool WaitForEvents = true;
|
|
|
|
enum EventKind {
|
|
EVENT_NONE,
|
|
EVENT_UPDATE,
|
|
EVENT_QUIT,
|
|
|
|
EVENT_MOUSE_LEFT,
|
|
EVENT_MOUSE_RIGHT,
|
|
EVENT_MOUSE_MIDDLE,
|
|
EVENT_MOUSE_LEFT_UP,
|
|
EVENT_MOUSE_RIGHT_UP,
|
|
EVENT_MOUSE_MIDDLE_UP,
|
|
EVENT_MOUSE_WHEEL,
|
|
EVENT_MOUSE_MOVE,
|
|
|
|
EVENT_KEY_PRESS,
|
|
EVENT_TEXT_INPUT,
|
|
};
|
|
|
|
bool IsMouseEvent(EventKind kind) { return kind >= EVENT_MOUSE_LEFT && kind <= EVENT_MOUSE_MOVE; }
|
|
|
|
struct Event {
|
|
EventKind kind;
|
|
SDL_Keycode key;
|
|
int16_t xmouse;
|
|
int16_t ymouse;
|
|
int16_t xwindow;
|
|
int16_t ywindow;
|
|
struct {
|
|
uint8_t shift : 1;
|
|
uint8_t ctrl : 1;
|
|
uint8_t alt : 1;
|
|
uint8_t super : 1;
|
|
uint8_t mouse_double_click : 1;
|
|
};
|
|
Vec2 wheel;
|
|
const char *text;
|
|
};
|
|
|
|
const int DIR_RIGHT = 0;
|
|
const int DIR_LEFT = 1;
|
|
const int DIR_DOWN = 2;
|
|
const int DIR_UP = 3;
|
|
const int DIR_COUNT = 4;
|
|
const bool CTRL_PRESSED = true;
|
|
bool SHIFT_PRESSED = true;
|
|
|
|
#define Press(KEY) (event.key == KEY)
|
|
#define Ctrl(KEY) (event.key == KEY && event.ctrl)
|
|
#define Shift(KEY) (event.key == KEY && event.shift)
|
|
#define Alt(KEY) (event.key == KEY && event.alt)
|
|
#define CtrlShift(KEY) (event.key == KEY && event.ctrl && event.shift)
|
|
#define CtrlAlt(KEY) (event.key == KEY && event.ctrl && event.alt)
|
|
#define AltShift(KEY) (event.key == KEY && event.shift && event.alt)
|
|
#define MouseVec2() \
|
|
Vec2 { (float)event.xmouse, (float)event.ymouse }
|
|
#define MouseVec2I() \
|
|
Vec2I { (Int) event.xmouse, (Int)event.ymouse }
|
|
#define Mouse(x) (event.kind == EVENT_MOUSE_##x)
|
|
#define MousePress() (Mouse(LEFT) || Mouse(RIGHT) || Mouse(MIDDLE))
|
|
|
|
void ToggleFullscreen() {
|
|
if (IsInFullscreen) {
|
|
SDL_SetWindowSize(SDLWindow, FullScreenSizeX, FullScreenSizeY);
|
|
SDL_SetWindowPosition(SDLWindow, FullScreenPositionX, FullScreenPositionY);
|
|
} else {
|
|
SDL_GetWindowSize(SDLWindow, &FullScreenSizeX, &FullScreenSizeY);
|
|
SDL_GetWindowPosition(SDLWindow, &FullScreenPositionX, &FullScreenPositionY);
|
|
|
|
SDL_DisplayID display = SDL_GetDisplayForWindow(SDLWindow);
|
|
const SDL_DisplayMode *dm = SDL_GetCurrentDisplayMode(display);
|
|
SDL_SetWindowSize(SDLWindow, dm->w, dm->h);
|
|
SDL_SetWindowPosition(SDLWindow, 0, 0);
|
|
}
|
|
|
|
IsInFullscreen = !IsInFullscreen;
|
|
}
|
|
|
|
bool GlobalCommand(Event event) {
|
|
ProfileFunction();
|
|
bool run_window_command = true;
|
|
{
|
|
Vec2I mouse = MouseVec2I();
|
|
Window *window = GetActiveWindow();
|
|
View *view = GetView(window->active_view);
|
|
bool mouse_in_document = CheckCollisionPointRec(mouse, window->document_rect);
|
|
bool mouse_in_total = CheckCollisionPointRec(mouse, window->total_rect);
|
|
bool mouse_in_line_numbers = CheckCollisionPointRec(mouse, window->line_numbers_rect);
|
|
bool mouse_in_scrollbar = CheckCollisionPointRec(mouse, window->scrollbar_rect);
|
|
window->mouse_in_scrollbar = mouse_in_scrollbar;
|
|
|
|
static SDL_Cursor *SDL_MouseCursor;
|
|
if (SDL_MouseCursor) SDL_DestroyCursor(SDL_MouseCursor);
|
|
|
|
if (view->mouse_selecting || mouse_in_document) {
|
|
SDL_MouseCursor = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_TEXT);
|
|
SDL_SetCursor(SDL_MouseCursor);
|
|
} else if (mouse_in_scrollbar || window->mouse_selecting_scrollbar || mouse_in_line_numbers) {
|
|
SDL_MouseCursor = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_DEFAULT);
|
|
SDL_SetCursor(SDL_MouseCursor);
|
|
} else {
|
|
SDL_MouseCursor = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_POINTER);
|
|
SDL_SetCursor(SDL_MouseCursor);
|
|
}
|
|
}
|
|
|
|
if (MousePress()) {
|
|
Scratch scratch;
|
|
Array<Int> order = GetWindowZOrder(scratch);
|
|
Vec2I mouse = MouseVec2I();
|
|
|
|
For(order) {
|
|
Window *window = &Windows[it];
|
|
if (!window->visible) continue;
|
|
bool mouse_in_window = CheckCollisionPointRec(mouse, window->total_rect);
|
|
if (mouse_in_window) {
|
|
SetActiveWindow(window->id);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
if (event.wheel.x || event.wheel.y) {
|
|
Scratch scratch;
|
|
Array<Int> order = GetWindowZOrder(scratch);
|
|
Vec2I mouse = MouseVec2I();
|
|
|
|
For(order) {
|
|
Window *window = &Windows[it];
|
|
if (!window->visible) continue;
|
|
|
|
bool mouse_in_window = CheckCollisionPointRec(mouse, window->total_rect);
|
|
if (mouse_in_window) {
|
|
View *view = GetView(window->active_view);
|
|
view->scroll.y -= (Int)(event.wheel.y * 48);
|
|
view->scroll.x += (Int)(event.wheel.x * 48);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
if (Ctrl(SDLK_GRAVE)) {
|
|
Window *window = GetWindow(ConsoleWindowID);
|
|
if (ToggleVisibility(window)) {
|
|
SetActiveWindow(window->id);
|
|
} else {
|
|
SetActiveWindow(GetLastActiveWindow());
|
|
}
|
|
}
|
|
|
|
if (CtrlShift(SDLK_BACKSLASH)) {
|
|
AddRowWindow();
|
|
} else if (Ctrl(SDLK_BACKSLASH)) {
|
|
AddColumnWindow();
|
|
}
|
|
|
|
if (Ctrl(SDLK_0)) {
|
|
Window *window = GetWindow(DebugWindowID);
|
|
ToggleVisibility(window);
|
|
}
|
|
|
|
if (Press(SDLK_F5)) {
|
|
AppIsRunning = false;
|
|
run_window_command = false;
|
|
}
|
|
|
|
if (Press(SDLK_F11)) {
|
|
ToggleFullscreen();
|
|
}
|
|
|
|
if (Ctrl(SDLK_P)) {
|
|
Window *command_window = GetWindow(CommandWindowID);
|
|
if (command_window->visible) {
|
|
SetActiveWindow(GetLastActiveWindow());
|
|
} else {
|
|
View *view = GetView(command_window->active_view);
|
|
Command_EvalLua(view, L"list_buffers()");
|
|
SetActiveWindow(command_window->id);
|
|
}
|
|
run_window_command = false;
|
|
}
|
|
|
|
if (Ctrl(SDLK_F)) {
|
|
Window *search_window = GetWindow(SearchWindowID);
|
|
if (search_window->visible) {
|
|
SetActiveWindow(GetLastActiveWindow());
|
|
} else {
|
|
SetActiveWindow(search_window->id);
|
|
View *view = GetActiveView(search_window);
|
|
Command_SelectEntireBuffer(view);
|
|
Command_Replace(view, {});
|
|
}
|
|
run_window_command = false;
|
|
}
|
|
|
|
if (Ctrl(SDLK_1)) {
|
|
Window *window = GetLayoutWindow(0);
|
|
if (window) SetActiveWindow(window->id);
|
|
run_window_command = false;
|
|
}
|
|
if (Ctrl(SDLK_2)) {
|
|
Window *window = GetLayoutWindow(1);
|
|
if (window) SetActiveWindow(window->id);
|
|
run_window_command = false;
|
|
}
|
|
if (Ctrl(SDLK_3)) {
|
|
Window *window = GetLayoutWindow(2);
|
|
if (window) SetActiveWindow(window->id);
|
|
run_window_command = false;
|
|
}
|
|
|
|
if (Ctrl(SDLK_MINUS)) {
|
|
ReloadFont((int32_t)MainFont.size - 1);
|
|
run_window_command = false;
|
|
} else if (Ctrl(SDLK_EQUALS)) {
|
|
ReloadFont((int32_t)MainFont.size + 1);
|
|
run_window_command = false;
|
|
}
|
|
|
|
return run_window_command;
|
|
}
|
|
|
|
View *FindView(BufferID buffer_id) {
|
|
For(Views) {
|
|
if (it.active_buffer.id == buffer_id.id) {
|
|
return ⁢
|
|
}
|
|
}
|
|
return NULL;
|
|
}
|
|
|
|
void AppendToConsole(String16 string) {
|
|
Buffer *buffer = GetBuffer("*console*");
|
|
|
|
// @todo: ?
|
|
View *view = FindView(buffer->id);
|
|
|
|
bool scroll_to_end = false;
|
|
if (view) {
|
|
Int line = PosToLine(*buffer, GetFront(view->carets[0]));
|
|
if (line == buffer->line_starts.len - 1) scroll_to_end = true;
|
|
}
|
|
ReplaceText(buffer, GetEndAsRange(*buffer), string);
|
|
ReplaceText(buffer, GetEndAsRange(*buffer), L"\n");
|
|
|
|
if (scroll_to_end) {
|
|
view->carets[0] = MakeCaret(GetEndAsRange(*buffer).min);
|
|
}
|
|
}
|
|
|
|
void AppendToConsole(String string) {
|
|
Scratch scratch;
|
|
String16 string16 = ToString16(scratch, string);
|
|
AppendToConsole(string16);
|
|
}
|
|
|
|
void ReportErrorf(const char *fmt, ...) {
|
|
Scratch scratch;
|
|
STRING_FORMAT(scratch, fmt, string);
|
|
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Error!", string.data, NULL);
|
|
AppendToConsole(string);
|
|
}
|
|
|
|
void ReportConsolef(const char *fmt, ...) {
|
|
Scratch scratch;
|
|
STRING_FORMAT(scratch, fmt, string);
|
|
AppendToConsole(string);
|
|
}
|
|
|
|
void ReportWarningf(const char *fmt, ...) {
|
|
Scratch scratch;
|
|
STRING_FORMAT(scratch, fmt, string);
|
|
String16 string16 = ToString16(scratch, string);
|
|
AppendToConsole(string16);
|
|
Window *window = GetWindow(ConsoleWindowID);
|
|
SetVisibility(window, true);
|
|
SetActiveWindow(window->id);
|
|
} |