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:
scawful
2025-08-03 16:42:06 -04:00
parent b5f6930d38
commit a9a9cc888b
4 changed files with 49 additions and 55 deletions

View File

@@ -16,64 +16,19 @@ absl::Status Controller::OnEntry(std::string filename) {
RETURN_IF_ERROR(CreateWindow(window_, SDL_WINDOW_RESIZABLE));
editor_manager_.emulator().set_audio_buffer(window_.audio_buffer_.get());
editor_manager_.emulator().set_audio_device_id(window_.audio_device_);
Initialize(filename);
editor_manager_.Initialize(filename);
active_ = true;
return absl::OkStatus();
}
void Controller::Initialize(std::string filename) {
editor_manager_.Initialize(filename);
active_ = true;
}
void Controller::OnInput() {
ImGuiIO &io = ImGui::GetIO();
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);
PRINT_IF_ERROR(HandleEvents(window_));
}
absl::Status Controller::OnLoad() {
if (editor_manager_.quit()) {
if (editor_manager_.quit() || !window_.active_) {
active_ = false;
return absl::OkStatus();
}
#if TARGET_OS_IPHONE != 1
@@ -120,9 +75,7 @@ void Controller::DoRender() const {
SDL_RenderPresent(Renderer::Get().renderer());
}
void Controller::OnExit() {
PRINT_IF_ERROR(ShutdownWindow(window_));
}
void Controller::OnExit() { PRINT_IF_ERROR(ShutdownWindow(window_)); }
} // namespace core
} // namespace yaze

View File

@@ -24,7 +24,6 @@ class Controller {
public:
bool IsActive() const { return active_; }
absl::Status OnEntry(std::string filename = "");
void Initialize(std::string filename = "");
void OnInput();
absl::Status OnLoad();
void DoRender() const;

View File

@@ -83,5 +83,46 @@ absl::Status ShutdownWindow(Window& window) {
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 yaze

View File

@@ -17,10 +17,11 @@ struct Window {
std::shared_ptr<SDL_Window> window_;
SDL_AudioDeviceID audio_device_;
std::shared_ptr<int16_t> audio_buffer_;
bool active_ = true;
};
absl::Status CreateWindow(Window &window, int flags);
absl::Status HandleEvents(Window &window);
absl::Status ShutdownWindow(Window &window);
/**