2015-06-16 20:52:24 +02:00
|
|
|
#include "main/main.h"
|
2015-06-11 21:57:41 +02:00
|
|
|
#include "haiku_direct_window.h"
|
|
|
|
|
|
|
|
HaikuDirectWindow::HaikuDirectWindow(BRect p_frame)
|
|
|
|
: BDirectWindow(p_frame, "Godot", B_TITLED_WINDOW, 0)
|
|
|
|
{
|
2015-06-16 20:52:24 +02:00
|
|
|
last_mouse_pos_valid = false;
|
|
|
|
last_buttons_state = 0;
|
2015-06-11 21:57:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-06-16 20:52:24 +02:00
|
|
|
HaikuDirectWindow::~HaikuDirectWindow() {
|
|
|
|
if (update_runner) {
|
|
|
|
delete update_runner;
|
|
|
|
}
|
2015-06-11 21:57:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void HaikuDirectWindow::SetHaikuGLView(HaikuGLView* p_view) {
|
|
|
|
view = p_view;
|
|
|
|
}
|
|
|
|
|
2015-06-16 20:52:24 +02:00
|
|
|
void HaikuDirectWindow::StartMessageRunner() {
|
|
|
|
update_runner = new BMessageRunner(BMessenger(this),
|
2015-06-11 21:57:41 +02:00
|
|
|
new BMessage(REDRAW_MSG), 1000000/60 /* 60 fps */);
|
|
|
|
}
|
|
|
|
|
2015-06-16 20:52:24 +02:00
|
|
|
void HaikuDirectWindow::StopMessageRunner() {
|
|
|
|
delete update_runner;
|
|
|
|
}
|
2015-06-11 21:57:41 +02:00
|
|
|
|
2015-06-16 20:52:24 +02:00
|
|
|
void HaikuDirectWindow::SetInput(InputDefault* p_input) {
|
|
|
|
input = p_input;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool HaikuDirectWindow::QuitRequested() {
|
2015-06-11 21:57:41 +02:00
|
|
|
view->EnableDirectMode(false);
|
|
|
|
be_app->PostMessage(B_QUIT_REQUESTED);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-06-16 20:52:24 +02:00
|
|
|
void HaikuDirectWindow::DirectConnected(direct_buffer_info* info) {
|
2015-06-17 21:27:45 +02:00
|
|
|
view->DirectConnected(info);
|
2015-06-11 21:57:41 +02:00
|
|
|
view->EnableDirectMode(true);
|
|
|
|
}
|
|
|
|
|
2015-06-16 20:52:24 +02:00
|
|
|
void HaikuDirectWindow::MessageReceived(BMessage* message)
|
|
|
|
{
|
|
|
|
switch (message->what) {
|
|
|
|
case REDRAW_MSG:
|
|
|
|
//ERR_PRINT("iteration 1");
|
|
|
|
Main::iteration();
|
2015-06-17 21:27:45 +02:00
|
|
|
|
2015-06-16 20:52:24 +02:00
|
|
|
//if (NeedsUpdate()) {
|
|
|
|
// ERR_PRINT("NEEDS UPDATE");
|
|
|
|
// Main::force_redraw();
|
|
|
|
//}
|
2015-06-17 21:27:45 +02:00
|
|
|
|
2015-06-16 20:52:24 +02:00
|
|
|
//ERR_PRINT("iteration 2");
|
|
|
|
break;
|
2015-06-17 21:27:45 +02:00
|
|
|
|
2015-06-16 20:52:24 +02:00
|
|
|
case B_INVALIDATE:
|
|
|
|
ERR_PRINT("WINDOW B_INVALIDATE");
|
|
|
|
//Main::force_redraw();
|
|
|
|
break;
|
|
|
|
|
2015-06-17 21:27:45 +02:00
|
|
|
default:
|
2015-06-16 20:52:24 +02:00
|
|
|
BDirectWindow::MessageReceived(message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void HaikuDirectWindow::DispatchMessage(BMessage* message, BHandler* handler) {
|
|
|
|
switch (message->what) {
|
|
|
|
case B_MOUSE_DOWN:
|
|
|
|
case B_MOUSE_UP:
|
|
|
|
DispatchMouseButton(message);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case B_MOUSE_MOVED:
|
|
|
|
DispatchMouseMoved(message);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
BDirectWindow::DispatchMessage(message, handler);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void HaikuDirectWindow::DispatchMouseButton(BMessage* message) {
|
|
|
|
message->PrintToStream();
|
|
|
|
|
|
|
|
BPoint where;
|
|
|
|
if (message->FindPoint("where", &where) != B_OK) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-06-17 21:27:45 +02:00
|
|
|
uint32 modifiers = message->FindInt32("modifiers");
|
2015-06-16 20:52:24 +02:00
|
|
|
uint32 buttons = message->FindInt32("buttons");
|
|
|
|
uint32 button = buttons ^ last_buttons_state;
|
|
|
|
last_buttons_state = buttons;
|
|
|
|
|
|
|
|
// TODO: implement the mouse_mode checks
|
|
|
|
//if (mouse_mode == MOUSE_MODE_CAPTURED) {
|
|
|
|
// event.xbutton.x=last_mouse_pos.x;
|
|
|
|
// event.xbutton.y=last_mouse_pos.y;
|
|
|
|
//}
|
2015-06-17 21:27:45 +02:00
|
|
|
|
2015-06-16 20:52:24 +02:00
|
|
|
InputEvent mouse_event;
|
|
|
|
mouse_event.ID = ++event_id;
|
|
|
|
mouse_event.type = InputEvent::MOUSE_BUTTON;
|
|
|
|
mouse_event.device = 0;
|
|
|
|
|
2015-06-17 21:27:45 +02:00
|
|
|
mouse_event.mouse_button.mod = GetKeyModifierState(modifiers);
|
|
|
|
mouse_event.mouse_button.button_mask = GetMouseButtonState(buttons);
|
2015-06-16 20:52:24 +02:00
|
|
|
mouse_event.mouse_button.x = where.x;
|
|
|
|
mouse_event.mouse_button.y = where.y;
|
|
|
|
mouse_event.mouse_button.global_x = where.x;
|
|
|
|
mouse_event.mouse_button.global_y = where.y;
|
|
|
|
|
|
|
|
switch (button) {
|
|
|
|
default:
|
|
|
|
case B_PRIMARY_MOUSE_BUTTON:
|
|
|
|
mouse_event.mouse_button.button_index = 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case B_SECONDARY_MOUSE_BUTTON:
|
|
|
|
mouse_event.mouse_button.button_index = 2;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case B_TERTIARY_MOUSE_BUTTON:
|
|
|
|
mouse_event.mouse_button.button_index = 3;
|
|
|
|
break;
|
|
|
|
}
|
2015-06-17 21:27:45 +02:00
|
|
|
|
2015-06-16 20:52:24 +02:00
|
|
|
mouse_event.mouse_button.pressed = (message->what == B_MOUSE_DOWN);
|
|
|
|
|
|
|
|
if (message->what == B_MOUSE_DOWN && mouse_event.mouse_button.button_index == 1) {
|
|
|
|
int32 clicks = message->FindInt32("clicks");
|
2015-06-17 21:27:45 +02:00
|
|
|
|
2015-06-16 20:52:24 +02:00
|
|
|
if (clicks > 1) {
|
|
|
|
mouse_event.mouse_button.doubleclick=true;
|
|
|
|
}
|
2015-06-17 21:27:45 +02:00
|
|
|
}
|
2015-06-16 20:52:24 +02:00
|
|
|
|
|
|
|
input->parse_input_event(mouse_event);
|
|
|
|
}
|
|
|
|
|
|
|
|
void HaikuDirectWindow::DispatchMouseMoved(BMessage* message) {
|
|
|
|
BPoint where;
|
|
|
|
if (message->FindPoint("where", &where) != B_OK) {
|
|
|
|
return;
|
|
|
|
}
|
2015-06-17 21:27:45 +02:00
|
|
|
|
2015-06-16 20:52:24 +02:00
|
|
|
Point2i pos(where.x, where.y);
|
2015-06-17 21:27:45 +02:00
|
|
|
uint32 modifiers = message->FindInt32("modifiers");
|
|
|
|
uint32 buttons = message->FindInt32("buttons");
|
|
|
|
|
2015-06-16 20:52:24 +02:00
|
|
|
if (!last_mouse_pos_valid) {
|
2015-06-17 21:27:45 +02:00
|
|
|
last_mouse_pos = pos;
|
|
|
|
last_mouse_pos_valid = true;
|
2015-06-16 20:52:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Point2i rel = pos - last_mouse_pos;
|
|
|
|
|
|
|
|
InputEvent motion_event;
|
|
|
|
motion_event.ID = ++event_id;
|
|
|
|
motion_event.type = InputEvent::MOUSE_MOTION;
|
|
|
|
motion_event.device = 0;
|
|
|
|
|
2015-06-17 21:27:45 +02:00
|
|
|
motion_event.mouse_motion.mod = GetKeyModifierState(modifiers);
|
|
|
|
motion_event.mouse_motion.button_mask = GetMouseButtonState(buttons);
|
2015-06-16 20:52:24 +02:00
|
|
|
motion_event.mouse_motion.x = pos.x;
|
|
|
|
motion_event.mouse_motion.y = pos.y;
|
|
|
|
input->set_mouse_pos(pos);
|
|
|
|
motion_event.mouse_motion.global_x = pos.x;
|
|
|
|
motion_event.mouse_motion.global_y = pos.y;
|
|
|
|
motion_event.mouse_motion.speed_x = input->get_mouse_speed().x;
|
|
|
|
motion_event.mouse_motion.speed_y = input->get_mouse_speed().y;
|
|
|
|
|
|
|
|
motion_event.mouse_motion.relative_x = rel.x;
|
|
|
|
motion_event.mouse_motion.relative_y = rel.y;
|
|
|
|
|
|
|
|
last_mouse_pos=pos;
|
|
|
|
|
|
|
|
input->parse_input_event(motion_event);
|
|
|
|
}
|
2015-06-17 21:27:45 +02:00
|
|
|
|
|
|
|
inline InputModifierState HaikuDirectWindow::GetKeyModifierState(uint32 p_state) {
|
|
|
|
InputModifierState state;
|
|
|
|
|
|
|
|
state.shift = (p_state & B_SHIFT_KEY) != 0;
|
|
|
|
state.control = (p_state & B_CONTROL_KEY) != 0;
|
|
|
|
state.alt = (p_state & B_OPTION_KEY) != 0;
|
|
|
|
state.meta = (p_state & B_COMMAND_KEY) != 0;
|
|
|
|
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline unsigned int HaikuDirectWindow::GetMouseButtonState(uint32 p_state) {
|
|
|
|
unsigned int state = 0;
|
|
|
|
|
|
|
|
if (p_state & B_PRIMARY_MOUSE_BUTTON) {
|
|
|
|
state |= 1 << 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (p_state & B_SECONDARY_MOUSE_BUTTON) {
|
|
|
|
state |= 1 << 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (p_state & B_TERTIARY_MOUSE_BUTTON) {
|
|
|
|
state |= 1 << 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
return state;
|
|
|
|
}
|