Refactor event handling and controller initialization for improved clarity
- Moved event handling logic from the Controller class to a new HandleEvents function in the Window class, enhancing separation of concerns. - Simplified the OnEntry method by removing the Initialize function call and directly initializing the editor manager. - Updated the OnLoad method to check for window activity, improving the logic for managing the controller's active state. - Cleaned up the controller header by removing the obsolete Initialize method, streamlining the interface.
This commit is contained in:
@@ -16,64 +16,19 @@ absl::Status Controller::OnEntry(std::string filename) {
|
|||||||
RETURN_IF_ERROR(CreateWindow(window_, SDL_WINDOW_RESIZABLE));
|
RETURN_IF_ERROR(CreateWindow(window_, SDL_WINDOW_RESIZABLE));
|
||||||
editor_manager_.emulator().set_audio_buffer(window_.audio_buffer_.get());
|
editor_manager_.emulator().set_audio_buffer(window_.audio_buffer_.get());
|
||||||
editor_manager_.emulator().set_audio_device_id(window_.audio_device_);
|
editor_manager_.emulator().set_audio_device_id(window_.audio_device_);
|
||||||
Initialize(filename);
|
editor_manager_.Initialize(filename);
|
||||||
|
active_ = true;
|
||||||
return absl::OkStatus();
|
return absl::OkStatus();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Controller::Initialize(std::string filename) {
|
|
||||||
editor_manager_.Initialize(filename);
|
|
||||||
active_ = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
void Controller::OnInput() {
|
void Controller::OnInput() {
|
||||||
ImGuiIO &io = ImGui::GetIO();
|
PRINT_IF_ERROR(HandleEvents(window_));
|
||||||
SDL_Event event;
|
|
||||||
|
|
||||||
SDL_WaitEvent(&event);
|
|
||||||
ImGui_ImplSDL2_ProcessEvent(&event);
|
|
||||||
switch (event.type) {
|
|
||||||
case SDL_KEYDOWN:
|
|
||||||
case SDL_KEYUP: {
|
|
||||||
io.KeyShift = ((SDL_GetModState() & KMOD_SHIFT) != 0);
|
|
||||||
io.KeyCtrl = ((SDL_GetModState() & KMOD_CTRL) != 0);
|
|
||||||
io.KeyAlt = ((SDL_GetModState() & KMOD_ALT) != 0);
|
|
||||||
io.KeySuper = ((SDL_GetModState() & KMOD_GUI) != 0);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case SDL_WINDOWEVENT:
|
|
||||||
switch (event.window.event) {
|
|
||||||
case SDL_WINDOWEVENT_CLOSE:
|
|
||||||
active_ = false;
|
|
||||||
break;
|
|
||||||
case SDL_WINDOWEVENT_SIZE_CHANGED:
|
|
||||||
io.DisplaySize.x = static_cast<float>(event.window.data1);
|
|
||||||
io.DisplaySize.y = static_cast<float>(event.window.data2);
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
int mouseX;
|
|
||||||
int mouseY;
|
|
||||||
const int buttons = SDL_GetMouseState(&mouseX, &mouseY);
|
|
||||||
|
|
||||||
io.DeltaTime = 1.0f / 60.0f;
|
|
||||||
io.MousePos = ImVec2(static_cast<float>(mouseX), static_cast<float>(mouseY));
|
|
||||||
io.MouseDown[0] = buttons & SDL_BUTTON(SDL_BUTTON_LEFT);
|
|
||||||
io.MouseDown[1] = buttons & SDL_BUTTON(SDL_BUTTON_RIGHT);
|
|
||||||
io.MouseDown[2] = buttons & SDL_BUTTON(SDL_BUTTON_MIDDLE);
|
|
||||||
|
|
||||||
int wheel = 0;
|
|
||||||
io.MouseWheel = static_cast<float>(wheel);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
absl::Status Controller::OnLoad() {
|
absl::Status Controller::OnLoad() {
|
||||||
if (editor_manager_.quit()) {
|
if (editor_manager_.quit() || !window_.active_) {
|
||||||
active_ = false;
|
active_ = false;
|
||||||
|
return absl::OkStatus();
|
||||||
}
|
}
|
||||||
|
|
||||||
#if TARGET_OS_IPHONE != 1
|
#if TARGET_OS_IPHONE != 1
|
||||||
@@ -120,9 +75,7 @@ void Controller::DoRender() const {
|
|||||||
SDL_RenderPresent(Renderer::Get().renderer());
|
SDL_RenderPresent(Renderer::Get().renderer());
|
||||||
}
|
}
|
||||||
|
|
||||||
void Controller::OnExit() {
|
void Controller::OnExit() { PRINT_IF_ERROR(ShutdownWindow(window_)); }
|
||||||
PRINT_IF_ERROR(ShutdownWindow(window_));
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace core
|
} // namespace core
|
||||||
} // namespace yaze
|
} // namespace yaze
|
||||||
|
|||||||
@@ -24,7 +24,6 @@ class Controller {
|
|||||||
public:
|
public:
|
||||||
bool IsActive() const { return active_; }
|
bool IsActive() const { return active_; }
|
||||||
absl::Status OnEntry(std::string filename = "");
|
absl::Status OnEntry(std::string filename = "");
|
||||||
void Initialize(std::string filename = "");
|
|
||||||
void OnInput();
|
void OnInput();
|
||||||
absl::Status OnLoad();
|
absl::Status OnLoad();
|
||||||
void DoRender() const;
|
void DoRender() const;
|
||||||
|
|||||||
@@ -83,5 +83,46 @@ absl::Status ShutdownWindow(Window& window) {
|
|||||||
return absl::OkStatus();
|
return absl::OkStatus();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
absl::Status HandleEvents(Window &window) {
|
||||||
|
SDL_Event event;
|
||||||
|
ImGuiIO &io = ImGui::GetIO();
|
||||||
|
SDL_WaitEvent(&event);
|
||||||
|
ImGui_ImplSDL2_ProcessEvent(&event);
|
||||||
|
switch (event.type) {
|
||||||
|
case SDL_KEYDOWN:
|
||||||
|
case SDL_KEYUP: {
|
||||||
|
io.KeyShift = ((SDL_GetModState() & KMOD_SHIFT) != 0);
|
||||||
|
io.KeyCtrl = ((SDL_GetModState() & KMOD_CTRL) != 0);
|
||||||
|
io.KeyAlt = ((SDL_GetModState() & KMOD_ALT) != 0);
|
||||||
|
io.KeySuper = ((SDL_GetModState() & KMOD_GUI) != 0);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case SDL_WINDOWEVENT:
|
||||||
|
switch (event.window.event) {
|
||||||
|
case SDL_WINDOWEVENT_CLOSE:
|
||||||
|
window.active_ = false;
|
||||||
|
break;
|
||||||
|
case SDL_WINDOWEVENT_SIZE_CHANGED:
|
||||||
|
io.DisplaySize.x = static_cast<float>(event.window.data1);
|
||||||
|
io.DisplaySize.y = static_cast<float>(event.window.data2);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
int mouseX;
|
||||||
|
int mouseY;
|
||||||
|
const int buttons = SDL_GetMouseState(&mouseX, &mouseY);
|
||||||
|
|
||||||
|
io.DeltaTime = 1.0f / 60.0f;
|
||||||
|
io.MousePos = ImVec2(static_cast<float>(mouseX), static_cast<float>(mouseY));
|
||||||
|
io.MouseDown[0] = buttons & SDL_BUTTON(SDL_BUTTON_LEFT);
|
||||||
|
io.MouseDown[1] = buttons & SDL_BUTTON(SDL_BUTTON_RIGHT);
|
||||||
|
io.MouseDown[2] = buttons & SDL_BUTTON(SDL_BUTTON_MIDDLE);
|
||||||
|
|
||||||
|
int wheel = 0;
|
||||||
|
io.MouseWheel = static_cast<float>(wheel);
|
||||||
|
return absl::OkStatus();
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace core
|
} // namespace core
|
||||||
} // namespace yaze
|
} // namespace yaze
|
||||||
@@ -17,10 +17,11 @@ struct Window {
|
|||||||
std::shared_ptr<SDL_Window> window_;
|
std::shared_ptr<SDL_Window> window_;
|
||||||
SDL_AudioDeviceID audio_device_;
|
SDL_AudioDeviceID audio_device_;
|
||||||
std::shared_ptr<int16_t> audio_buffer_;
|
std::shared_ptr<int16_t> audio_buffer_;
|
||||||
|
bool active_ = true;
|
||||||
};
|
};
|
||||||
|
|
||||||
absl::Status CreateWindow(Window &window, int flags);
|
absl::Status CreateWindow(Window &window, int flags);
|
||||||
|
absl::Status HandleEvents(Window &window);
|
||||||
absl::Status ShutdownWindow(Window &window);
|
absl::Status ShutdownWindow(Window &window);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user