From ce550f08fa91598612c016ebebc89c4af2e9505e Mon Sep 17 00:00:00 2001 From: Aki Date: Thu, 15 Sep 2022 00:53:01 +0200 Subject: Renamed EventDispatch and EventTarget by adding UI prefix --- StarsEx/ActiveWindow.cpp | 16 +-- StarsEx/ActiveWindow.h | 4 +- StarsEx/CMakeLists.txt | 2 +- StarsEx/CmpnScreen.cpp | 1 - StarsEx/ComboBox.cpp | 4 +- StarsEx/EventDispatch.cpp | 273 ------------------------------------------- StarsEx/EventDispatch.h | 61 ---------- StarsEx/EventTarget.h | 58 ---------- StarsEx/Game.cpp | 4 +- StarsEx/GameScreen.cpp | 1 - StarsEx/MapView.cpp | 15 +-- StarsEx/MapView.h | 4 +- StarsEx/MenuScreen.cpp | 1 - StarsEx/PlanScreen.cpp | 1 - StarsEx/RichTextBox.cpp | 1 - StarsEx/RichTextBox.h | 1 - StarsEx/Starshatter.cpp | 8 +- StarsEx/TacRefDlg.cpp | 6 +- StarsEx/UIEventDispatch.cpp | 274 ++++++++++++++++++++++++++++++++++++++++++++ StarsEx/UIEventDispatch.h | 60 ++++++++++ StarsEx/UIEventTarget.h | 57 +++++++++ 21 files changed, 423 insertions(+), 429 deletions(-) delete mode 100644 StarsEx/EventDispatch.cpp delete mode 100644 StarsEx/EventDispatch.h delete mode 100644 StarsEx/EventTarget.h create mode 100644 StarsEx/UIEventDispatch.cpp create mode 100644 StarsEx/UIEventDispatch.h create mode 100644 StarsEx/UIEventTarget.h diff --git a/StarsEx/ActiveWindow.cpp b/StarsEx/ActiveWindow.cpp index 4c99ef8..e5e8e33 100644 --- a/StarsEx/ActiveWindow.cpp +++ b/StarsEx/ActiveWindow.cpp @@ -13,7 +13,7 @@ #include #include "ActiveWindow.h" -#include "EventDispatch.h" +#include "UIEventDispatch.h" #include "Color.h" #include "Bitmap.h" #include "Font.h" @@ -85,7 +85,7 @@ ActiveWindow::~ActiveWindow() void ActiveWindow::Show() { - EventDispatch* dispatch = EventDispatch::GetInstance(); + UIEventDispatch* dispatch = UIEventDispatch::GetInstance(); if (dispatch) dispatch->Register(this); @@ -109,7 +109,7 @@ ActiveWindow::Show() void ActiveWindow::Hide() { - EventDispatch* dispatch = EventDispatch::GetInstance(); + UIEventDispatch* dispatch = UIEventDispatch::GetInstance(); if (dispatch) { dispatch->Unregister(this); focus = false; @@ -896,7 +896,7 @@ Rect ActiveWindow::TargetRect() const void ActiveWindow::SetFocus() { - EventDispatch* dispatch = EventDispatch::GetInstance(); + UIEventDispatch* dispatch = UIEventDispatch::GetInstance(); if (dispatch) dispatch->SetFocus(this); @@ -906,7 +906,7 @@ void ActiveWindow::SetFocus() void ActiveWindow::KillFocus() { - EventDispatch* dispatch = EventDispatch::GetInstance(); + UIEventDispatch* dispatch = UIEventDispatch::GetInstance(); if (dispatch) dispatch->KillFocus(this); @@ -919,7 +919,7 @@ void ActiveWindow::KillFocus() ActiveWindow* ActiveWindow::GetCapture() { - EventDispatch* dispatch = EventDispatch::GetInstance(); + UIEventDispatch* dispatch = UIEventDispatch::GetInstance(); if (dispatch) return (ActiveWindow*) dispatch->GetCapture(); @@ -931,7 +931,7 @@ ActiveWindow::GetCapture() bool ActiveWindow::SetCapture() { - EventDispatch* dispatch = EventDispatch::GetInstance(); + UIEventDispatch* dispatch = UIEventDispatch::GetInstance(); if (dispatch) return dispatch->CaptureMouse(this) == 1; @@ -943,7 +943,7 @@ ActiveWindow::SetCapture() int ActiveWindow::ReleaseCapture() { - EventDispatch* dispatch = EventDispatch::GetInstance(); + UIEventDispatch* dispatch = UIEventDispatch::GetInstance(); if (dispatch) return dispatch->ReleaseMouse(this); diff --git a/StarsEx/ActiveWindow.h b/StarsEx/ActiveWindow.h index ebcbdd2..0ad6388 100644 --- a/StarsEx/ActiveWindow.h +++ b/StarsEx/ActiveWindow.h @@ -20,7 +20,7 @@ #include "Geometry.h" #include "Bitmap.h" #include "Window.h" -#include "EventTarget.h" +#include "UIEventTarget.h" #include "List.h" #include "Text.h" @@ -114,7 +114,7 @@ struct AWMap // +--------------------------------------------------------------------+ class ActiveWindow : public Window, -public EventTarget +public UIEventTarget { public: static const char* TYPENAME() { return "ActiveWindow"; } diff --git a/StarsEx/CMakeLists.txt b/StarsEx/CMakeLists.txt index b99f765..91e82d8 100644 --- a/StarsEx/CMakeLists.txt +++ b/StarsEx/CMakeLists.txt @@ -75,7 +75,7 @@ add_library( Element.cpp Encrypt.cpp EngDlg.cpp - EventDispatch.cpp + UIEventDispatch.cpp ExceptionHandler.cpp ExitDlg.cpp Explosion.cpp diff --git a/StarsEx/CmpnScreen.cpp b/StarsEx/CmpnScreen.cpp index 70fcf8a..7faffb6 100644 --- a/StarsEx/CmpnScreen.cpp +++ b/StarsEx/CmpnScreen.cpp @@ -39,7 +39,6 @@ #include "Bitmap.h" #include "Font.h" #include "FontMgr.h" -#include "EventDispatch.h" #include "DataLoader.h" #include "Game.h" diff --git a/StarsEx/ComboBox.cpp b/StarsEx/ComboBox.cpp index 4269a96..cb16723 100644 --- a/StarsEx/ComboBox.cpp +++ b/StarsEx/ComboBox.cpp @@ -18,7 +18,7 @@ #include "Screen.h" #include "Bitmap.h" #include "Font.h" -#include "EventDispatch.h" +#include "UIEventDispatch.h" // +--------------------------------------------------------------------+ // DECLARE MAPPING FUNCTIONS: @@ -311,7 +311,7 @@ void ComboBox::ShowList() list->Show(); list_showing = true; - EventDispatch* dispatch = EventDispatch::GetInstance(); + UIEventDispatch* dispatch = UIEventDispatch::GetInstance(); if (dispatch) { dispatch->MouseEnter(list); dispatch->SetFocus(list); diff --git a/StarsEx/EventDispatch.cpp b/StarsEx/EventDispatch.cpp deleted file mode 100644 index 0a9b95c..0000000 --- a/StarsEx/EventDispatch.cpp +++ /dev/null @@ -1,273 +0,0 @@ -/* Starshatter: The Open Source Project - Copyright (c) 2021-2022, Starshatter: The Open Source Project Contributors - Copyright (c) 2011-2012, Starshatter OpenSource Distribution Contributors - Copyright (c) 1997-2006, Destroyer Studios LLC. - - AUTHOR: John DiCamillo - -*/ - -#include "EventDispatch.h" -#include "Mouse.h" -#include "Keyboard.h" - -// +--------------------------------------------------------------------+ - -int GetKeyPlus(int& key, int& shift); - -// +--------------------------------------------------------------------+ - -EventDispatch* EventDispatch::dispatcher = 0; - -// +--------------------------------------------------------------------+ - -EventDispatch::EventDispatch() - : capture(0), current(0), focus(0), click_tgt(0), - mouse_x(0), mouse_y(0), mouse_l(0), mouse_r(0) -{ } - -EventDispatch::~EventDispatch() -{ } - -// +--------------------------------------------------------------------+ - -void -EventDispatch::Create() -{ - dispatcher = new EventDispatch; -} - -// +--------------------------------------------------------------------+ - -void -EventDispatch::Close() -{ - delete dispatcher; - dispatcher = 0; -} - -// +--------------------------------------------------------------------+ - -void -EventDispatch::Dispatch() -{ - int ml = Mouse::LButton(); - int mr = Mouse::RButton(); - int mx = Mouse::X(); - int my = Mouse::Y(); - int mw = Mouse::Wheel(); - - EventTarget* mouse_tgt = capture; - EventTarget* key_tgt = focus; - EventTarget* do_click = 0; - - if (!mouse_tgt) { - ListIter iter = clients; - while (++iter) { - EventTarget* test = iter.value(); - if (test->IsFormActive()) { - if (test->TargetRect().Contains(mx,my)) - mouse_tgt = test; - - if (test->HasFocus()) - key_tgt = test; - } - } - } - - // Mouse Events: - - if (mouse_tgt != current) { - if (current && current->IsEnabled() && current->IsVisible()) - current->OnMouseExit(mx,my); - - current = mouse_tgt; - - if (current && current->IsEnabled() && current->IsVisible()) - current->OnMouseEnter(mx,my); - } - - if (mouse_tgt && mouse_tgt->IsEnabled()) { - if (mx != mouse_x || my != mouse_y) - mouse_tgt->OnMouseMove(mx,my); - - if (mw != 0) - mouse_tgt->OnMouseWheel(mw); - - if (ml != mouse_l) { - if (ml) { - mouse_tgt->OnLButtonDown(mx,my); - click_tgt = mouse_tgt; - } - else { - mouse_tgt->OnLButtonUp(mx,my); - - if (click_tgt == mouse_tgt) { - if (click_tgt->TargetRect().Contains(mx,my)) - do_click = click_tgt; - click_tgt = 0; - } - } - } - - if (mr != mouse_r) { - if (mr) - mouse_tgt->OnRButtonDown(mx,my); - else - mouse_tgt->OnRButtonUp(mx,my); - } - } - - mouse_l = ml; - mouse_r = mr; - mouse_x = mx; - mouse_y = my; - - // Keyboard Events: - - if (click_tgt && click_tgt != key_tgt) { - if (key_tgt) key_tgt->KillFocus(); - key_tgt = click_tgt; - - if (key_tgt != focus) { - if (focus) focus->KillFocus(); - - if (key_tgt && key_tgt->IsEnabled() && key_tgt->IsVisible()) - focus = key_tgt; - else - key_tgt = 0; - - if (focus) focus->SetFocus(); - } - } - - if (key_tgt && key_tgt->IsEnabled()) { - int key = 0; - int shift = 0; - - while (GetKeyPlus(key, shift)) { - if (key == VK_ESCAPE) { - key_tgt->KillFocus(); - focus = 0; - break; - } - - else if (key == VK_TAB && focus) { - int key_index = clients.index(focus) + 1; - - if (shift & 1) key_index -= 2; - - if (key_index >= clients.size()) - key_index = 0; - else if (key_index < 0) - key_index = clients.size()-1; - - if (focus) focus->KillFocus(); - focus = clients[key_index]; - if (focus) focus->SetFocus(); - - break; - } - - key_tgt->OnKeyDown(key, shift); - } - } - - if (do_click) - do_click->OnClick(); -} - -// +--------------------------------------------------------------------+ - -void -EventDispatch::MouseEnter(EventTarget* mouse_tgt) -{ - if (mouse_tgt != current) { - if (current) current->OnMouseExit(0,0); - current = mouse_tgt; - if (current) current->OnMouseEnter(0,0); - } -} - -// +--------------------------------------------------------------------+ - -void -EventDispatch::Register(EventTarget* tgt) -{ - if (!clients.contains(tgt)) - clients.append(tgt); -} - -// +--------------------------------------------------------------------+ - -void -EventDispatch::Unregister(EventTarget* tgt) -{ - clients.remove(tgt); - - if (capture == tgt) capture = 0; - if (current == tgt) current = 0; - if (focus == tgt) focus = 0; - if (click_tgt == tgt) click_tgt = 0; -} - -// +--------------------------------------------------------------------+ - -EventTarget* -EventDispatch::GetCapture() -{ - return capture; -} - -int -EventDispatch::CaptureMouse(EventTarget* tgt) -{ - if (tgt) { - capture = tgt; - return 1; - } - - return 0; -} - -int -EventDispatch::ReleaseMouse(EventTarget* tgt) -{ - if (capture == tgt) { - capture = 0; - return 1; - } - - return 0; -} - -// +--------------------------------------------------------------------+ - -EventTarget* -EventDispatch::GetFocus() -{ - return focus; -} - -void -EventDispatch::SetFocus(EventTarget* tgt) -{ - if (focus != tgt) { - if (focus) - focus->KillFocus(); - - focus = tgt; - - if (focus && focus->IsEnabled() && focus->IsVisible()) - focus->SetFocus(); - } -} - -void -EventDispatch::KillFocus(EventTarget* tgt) -{ - if (focus && focus == tgt) { - focus = 0; - tgt->KillFocus(); - } -} diff --git a/StarsEx/EventDispatch.h b/StarsEx/EventDispatch.h deleted file mode 100644 index 29b48a0..0000000 --- a/StarsEx/EventDispatch.h +++ /dev/null @@ -1,61 +0,0 @@ -/* Starshatter: The Open Source Project - Copyright (c) 2021-2022, Starshatter: The Open Source Project Contributors - Copyright (c) 2011-2012, Starshatter OpenSource Distribution Contributors - Copyright (c) 1997-2006, Destroyer Studios LLC. - - AUTHOR: John DiCamillo - - - OVERVIEW - ======== - Event Dispatch class -*/ - -#ifndef EventDispatch_h -#define EventDispatch_h - -#include "Types.h" -#include "EventTarget.h" -#include "List.h" - -// +--------------------------------------------------------------------+ - -class EventDispatch -{ -public: - static const char* TYPENAME() { return "EventDispatch"; } - - EventDispatch(); - virtual ~EventDispatch(); - - static void Create(); - static void Close(); - static EventDispatch* GetInstance() { return dispatcher; } - - virtual void Dispatch(); - virtual void Register(EventTarget* tgt); - virtual void Unregister(EventTarget* tgt); - - virtual EventTarget* GetCapture(); - virtual int CaptureMouse(EventTarget* tgt); - virtual int ReleaseMouse(EventTarget* tgt); - - virtual EventTarget* GetFocus(); - virtual void SetFocus(EventTarget* tgt); - virtual void KillFocus(EventTarget* tgt); - - virtual void MouseEnter(EventTarget* tgt); - -protected: - int mouse_x, mouse_y, mouse_l, mouse_r; - List clients; - EventTarget* capture; - EventTarget* current; - EventTarget* focus; - EventTarget* click_tgt; - - static EventDispatch* dispatcher; -}; - -#endif // EventDispatch_h - diff --git a/StarsEx/EventTarget.h b/StarsEx/EventTarget.h deleted file mode 100644 index 0c56944..0000000 --- a/StarsEx/EventTarget.h +++ /dev/null @@ -1,58 +0,0 @@ -/* Starshatter: The Open Source Project - Copyright (c) 2021-2022, Starshatter: The Open Source Project Contributors - Copyright (c) 2011-2012, Starshatter OpenSource Distribution Contributors - Copyright (c) 1997-2006, Destroyer Studios LLC. - - AUTHOR: John DiCamillo - - - OVERVIEW - ======== - Event Target interface class -*/ - -#ifndef EventTarget_h -#define EventTarget_h - -#include "Types.h" -#include "Geometry.h" - -// +--------------------------------------------------------------------+ - -class EventTarget -{ -public: - static const char* TYPENAME() { return "EventTarget"; } - - virtual ~EventTarget() { } - - int operator == (const EventTarget& t) const { return this == &t; } - - virtual int OnMouseMove(int x, int y) { return 0; } - virtual int OnLButtonDown(int x, int y) { return 0; } - virtual int OnLButtonUp(int x, int y) { return 0; } - virtual int OnClick() { return 0; } - virtual int OnSelect() { return 0; } - virtual int OnRButtonDown(int x, int y) { return 0; } - virtual int OnRButtonUp(int x, int y) { return 0; } - virtual int OnMouseEnter(int x, int y) { return 0; } - virtual int OnMouseExit(int x, int y) { return 0; } - virtual int OnMouseWheel(int wheel) { return 0; } - - virtual int OnKeyDown(int vk, int flags) { return 0; } - - virtual void SetFocus() { } - virtual void KillFocus() { } - virtual bool HasFocus() const { return false; } - - virtual bool IsEnabled() const { return true; } - virtual bool IsVisible() const { return true; } - virtual bool IsFormActive() const { return true; } - - virtual Rect TargetRect() const { return Rect(); } - - virtual const char* GetDescription() const { return "EventTarget"; } -}; - -#endif // EventTarget_h - diff --git a/StarsEx/Game.cpp b/StarsEx/Game.cpp index 4c15163..420d7d3 100644 --- a/StarsEx/Game.cpp +++ b/StarsEx/Game.cpp @@ -11,7 +11,7 @@ #include "Mouse.h" #include "Universe.h" #include "Window.h" -#include "EventDispatch.h" +#include "UIEventDispatch.h" #include "Panic.h" #include "Pcx.h" #include "Bitmap.h" @@ -130,7 +130,7 @@ void Game::GameLoop() { if (active) { - auto dispatch = EventDispatch::GetInstance(); + auto dispatch = UIEventDispatch::GetInstance(); if (dispatch) dispatch->Dispatch(); } diff --git a/StarsEx/GameScreen.cpp b/StarsEx/GameScreen.cpp index 87ce925..82e9ffd 100644 --- a/StarsEx/GameScreen.cpp +++ b/StarsEx/GameScreen.cpp @@ -48,7 +48,6 @@ #include "Bitmap.h" #include "Font.h" #include "FontMgr.h" -#include "EventDispatch.h" #include "DataLoader.h" // +--------------------------------------------------------------------+ diff --git a/StarsEx/MapView.cpp b/StarsEx/MapView.cpp index dee3152..930a762 100644 --- a/StarsEx/MapView.cpp +++ b/StarsEx/MapView.cpp @@ -35,7 +35,8 @@ #include "Game.h" #include "ContentBundle.h" -#include "EventDispatch.h" +#include "UIEventDispatch.h" +#include "UIEventTarget.h" #include "Video.h" #include "Button.h" #include "Bitmap.h" @@ -599,7 +600,7 @@ MapView::ProcessMenuItem(int action) bool MapView::SetCapture() { - EventDispatch* dispatch = EventDispatch::GetInstance(); + UIEventDispatch* dispatch = UIEventDispatch::GetInstance(); if (dispatch) return dispatch->CaptureMouse(this) ? true : false; @@ -611,7 +612,7 @@ MapView::SetCapture() bool MapView::ReleaseCapture() { - EventDispatch* dispatch = EventDispatch::GetInstance(); + UIEventDispatch* dispatch = UIEventDispatch::GetInstance(); if (dispatch) return dispatch->ReleaseMouse(this) ? true : false; @@ -3211,7 +3212,7 @@ MapView::ZoomOut() void MapView::OnShow() { - EventDispatch* dispatch = EventDispatch::GetInstance(); + UIEventDispatch* dispatch = UIEventDispatch::GetInstance(); if (dispatch) dispatch->Register(this); } @@ -3219,7 +3220,7 @@ MapView::OnShow() void MapView::OnHide() { - EventDispatch* dispatch = EventDispatch::GetInstance(); + UIEventDispatch* dispatch = UIEventDispatch::GetInstance(); if (dispatch) dispatch->Unregister(this); @@ -3276,8 +3277,8 @@ int MapView::OnMouseMove(int x, int y) { if (captured) { - EventTarget* test = 0; - EventDispatch* dispatch = EventDispatch::GetInstance(); + UIEventTarget* test = 0; + UIEventDispatch* dispatch = UIEventDispatch::GetInstance(); if (dispatch) test = dispatch->GetCapture(); diff --git a/StarsEx/MapView.h b/StarsEx/MapView.h index 0e8d599..d89ac92 100644 --- a/StarsEx/MapView.h +++ b/StarsEx/MapView.h @@ -17,7 +17,7 @@ #include "Types.h" #include "SimObject.h" #include "View.h" -#include "EventTarget.h" +#include "UIEventTarget.h" #include "Bitmap.h" #include "List.h" #include "Text.h" @@ -45,7 +45,7 @@ const int EID_MAP_CLICK = 1000; // +--------------------------------------------------------------------+ class MapView : public View, -public EventTarget, +public UIEventTarget, public SimObserver { public: diff --git a/StarsEx/MenuScreen.cpp b/StarsEx/MenuScreen.cpp index f45d2c5..3f4ebbd 100644 --- a/StarsEx/MenuScreen.cpp +++ b/StarsEx/MenuScreen.cpp @@ -58,7 +58,6 @@ #include "Bitmap.h" #include "Font.h" #include "FontMgr.h" -#include "EventDispatch.h" #include "DataLoader.h" // +--------------------------------------------------------------------+ diff --git a/StarsEx/PlanScreen.cpp b/StarsEx/PlanScreen.cpp index e560be4..47f1c73 100644 --- a/StarsEx/PlanScreen.cpp +++ b/StarsEx/PlanScreen.cpp @@ -32,7 +32,6 @@ #include "Bitmap.h" #include "Font.h" #include "FontMgr.h" -#include "EventDispatch.h" #include "DataLoader.h" // +--------------------------------------------------------------------+ diff --git a/StarsEx/RichTextBox.cpp b/StarsEx/RichTextBox.cpp index fe64f0d..5f29867 100644 --- a/StarsEx/RichTextBox.cpp +++ b/StarsEx/RichTextBox.cpp @@ -12,7 +12,6 @@ */ #include "RichTextBox.h" -#include "EventDispatch.h" #include "Color.h" #include "Bitmap.h" #include "Font.h" diff --git a/StarsEx/RichTextBox.h b/StarsEx/RichTextBox.h index 826d483..96838d1 100644 --- a/StarsEx/RichTextBox.h +++ b/StarsEx/RichTextBox.h @@ -18,7 +18,6 @@ #include "Color.h" #include "Bitmap.h" #include "ScrollWindow.h" -#include "EventTarget.h" #include "List.h" #include "Text.h" diff --git a/StarsEx/Starshatter.cpp b/StarsEx/Starshatter.cpp index b3a4be3..6b87fb8 100644 --- a/StarsEx/Starshatter.cpp +++ b/StarsEx/Starshatter.cpp @@ -103,7 +103,7 @@ #include "MouseController.h" #include "Mouse.h" #include "TrackIR.h" -#include "EventDispatch.h" +#include "UIEventDispatch.h" #include "MultiController.h" #include "DataLoader.h" #include "Random.h" @@ -278,7 +278,7 @@ Starshatter::~Starshatter() NetServerConfig::Close(); Mouse::Close(); - EventDispatch::Close(); + UIEventDispatch::Close(); FontMgr::Close(); Button::Close(); DataLoader::Close(); @@ -383,7 +383,7 @@ Starshatter::InitGame() InitMouse(); Button::Initialize(); - EventDispatch::Create(); + UIEventDispatch::Create(); NetClientConfig::Initialize(); Player::Initialize(); HUDSounds::Initialize(); @@ -2063,7 +2063,7 @@ Starshatter::SetChatMode(int mode) void Starshatter::DoMouseFrame() { - EventDispatch* event_dispatch = EventDispatch::GetInstance(); + UIEventDispatch* event_dispatch = UIEventDispatch::GetInstance(); if (event_dispatch && event_dispatch->GetCapture()) return; diff --git a/StarsEx/TacRefDlg.cpp b/StarsEx/TacRefDlg.cpp index ff623bc..6afa3ed 100644 --- a/StarsEx/TacRefDlg.cpp +++ b/StarsEx/TacRefDlg.cpp @@ -22,7 +22,7 @@ #include "Game.h" #include "ContentBundle.h" -#include "EventDispatch.h" +#include "UIEventDispatch.h" #include "Mouse.h" #include "Button.h" #include "ListBox.h" @@ -430,7 +430,7 @@ TacRefDlg::ExecFrame() bool TacRefDlg::SetCaptureBeauty() { - EventDispatch* dispatch = EventDispatch::GetInstance(); + UIEventDispatch* dispatch = UIEventDispatch::GetInstance(); if (dispatch && beauty) return dispatch->CaptureMouse(beauty) ? true : false; @@ -440,7 +440,7 @@ TacRefDlg::SetCaptureBeauty() bool TacRefDlg::ReleaseCaptureBeauty() { - EventDispatch* dispatch = EventDispatch::GetInstance(); + UIEventDispatch* dispatch = UIEventDispatch::GetInstance(); if (dispatch && beauty) return dispatch->ReleaseMouse(beauty) ? true : false; diff --git a/StarsEx/UIEventDispatch.cpp b/StarsEx/UIEventDispatch.cpp new file mode 100644 index 0000000..8c69bea --- /dev/null +++ b/StarsEx/UIEventDispatch.cpp @@ -0,0 +1,274 @@ +/* Starshatter: The Open Source Project + Copyright (c) 2021-2022, Starshatter: The Open Source Project Contributors + Copyright (c) 2011-2012, Starshatter OpenSource Distribution Contributors + Copyright (c) 1997-2006, Destroyer Studios LLC. + + AUTHOR: John DiCamillo + +*/ + +#include "UIEventDispatch.h" + +#include "Mouse.h" +#include "Keyboard.h" + +// +--------------------------------------------------------------------+ + +int GetKeyPlus(int& key, int& shift); + +// +--------------------------------------------------------------------+ + +UIEventDispatch* UIEventDispatch::dispatcher = 0; + +// +--------------------------------------------------------------------+ + +UIEventDispatch::UIEventDispatch() + : capture(0), current(0), focus(0), click_tgt(0), + mouse_x(0), mouse_y(0), mouse_l(0), mouse_r(0) +{ } + +UIEventDispatch::~UIEventDispatch() +{ } + +// +--------------------------------------------------------------------+ + +void +UIEventDispatch::Create() +{ + dispatcher = new UIEventDispatch; +} + +// +--------------------------------------------------------------------+ + +void +UIEventDispatch::Close() +{ + delete dispatcher; + dispatcher = 0; +} + +// +--------------------------------------------------------------------+ + +void +UIEventDispatch::Dispatch() +{ + int ml = Mouse::LButton(); + int mr = Mouse::RButton(); + int mx = Mouse::X(); + int my = Mouse::Y(); + int mw = Mouse::Wheel(); + + UIEventTarget* mouse_tgt = capture; + UIEventTarget* key_tgt = focus; + UIEventTarget* do_click = 0; + + if (!mouse_tgt) { + ListIter iter = clients; + while (++iter) { + UIEventTarget* test = iter.value(); + if (test->IsFormActive()) { + if (test->TargetRect().Contains(mx,my)) + mouse_tgt = test; + + if (test->HasFocus()) + key_tgt = test; + } + } + } + + // Mouse Events: + + if (mouse_tgt != current) { + if (current && current->IsEnabled() && current->IsVisible()) + current->OnMouseExit(mx,my); + + current = mouse_tgt; + + if (current && current->IsEnabled() && current->IsVisible()) + current->OnMouseEnter(mx,my); + } + + if (mouse_tgt && mouse_tgt->IsEnabled()) { + if (mx != mouse_x || my != mouse_y) + mouse_tgt->OnMouseMove(mx,my); + + if (mw != 0) + mouse_tgt->OnMouseWheel(mw); + + if (ml != mouse_l) { + if (ml) { + mouse_tgt->OnLButtonDown(mx,my); + click_tgt = mouse_tgt; + } + else { + mouse_tgt->OnLButtonUp(mx,my); + + if (click_tgt == mouse_tgt) { + if (click_tgt->TargetRect().Contains(mx,my)) + do_click = click_tgt; + click_tgt = 0; + } + } + } + + if (mr != mouse_r) { + if (mr) + mouse_tgt->OnRButtonDown(mx,my); + else + mouse_tgt->OnRButtonUp(mx,my); + } + } + + mouse_l = ml; + mouse_r = mr; + mouse_x = mx; + mouse_y = my; + + // Keyboard Events: + + if (click_tgt && click_tgt != key_tgt) { + if (key_tgt) key_tgt->KillFocus(); + key_tgt = click_tgt; + + if (key_tgt != focus) { + if (focus) focus->KillFocus(); + + if (key_tgt && key_tgt->IsEnabled() && key_tgt->IsVisible()) + focus = key_tgt; + else + key_tgt = 0; + + if (focus) focus->SetFocus(); + } + } + + if (key_tgt && key_tgt->IsEnabled()) { + int key = 0; + int shift = 0; + + while (GetKeyPlus(key, shift)) { + if (key == VK_ESCAPE) { + key_tgt->KillFocus(); + focus = 0; + break; + } + + else if (key == VK_TAB && focus) { + int key_index = clients.index(focus) + 1; + + if (shift & 1) key_index -= 2; + + if (key_index >= clients.size()) + key_index = 0; + else if (key_index < 0) + key_index = clients.size()-1; + + if (focus) focus->KillFocus(); + focus = clients[key_index]; + if (focus) focus->SetFocus(); + + break; + } + + key_tgt->OnKeyDown(key, shift); + } + } + + if (do_click) + do_click->OnClick(); +} + +// +--------------------------------------------------------------------+ + +void +UIEventDispatch::MouseEnter(UIEventTarget* mouse_tgt) +{ + if (mouse_tgt != current) { + if (current) current->OnMouseExit(0,0); + current = mouse_tgt; + if (current) current->OnMouseEnter(0,0); + } +} + +// +--------------------------------------------------------------------+ + +void +UIEventDispatch::Register(UIEventTarget* tgt) +{ + if (!clients.contains(tgt)) + clients.append(tgt); +} + +// +--------------------------------------------------------------------+ + +void +UIEventDispatch::Unregister(UIEventTarget* tgt) +{ + clients.remove(tgt); + + if (capture == tgt) capture = 0; + if (current == tgt) current = 0; + if (focus == tgt) focus = 0; + if (click_tgt == tgt) click_tgt = 0; +} + +// +--------------------------------------------------------------------+ + +UIEventTarget* +UIEventDispatch::GetCapture() +{ + return capture; +} + +int +UIEventDispatch::CaptureMouse(UIEventTarget* tgt) +{ + if (tgt) { + capture = tgt; + return 1; + } + + return 0; +} + +int +UIEventDispatch::ReleaseMouse(UIEventTarget* tgt) +{ + if (capture == tgt) { + capture = 0; + return 1; + } + + return 0; +} + +// +--------------------------------------------------------------------+ + +UIEventTarget* +UIEventDispatch::GetFocus() +{ + return focus; +} + +void +UIEventDispatch::SetFocus(UIEventTarget* tgt) +{ + if (focus != tgt) { + if (focus) + focus->KillFocus(); + + focus = tgt; + + if (focus && focus->IsEnabled() && focus->IsVisible()) + focus->SetFocus(); + } +} + +void +UIEventDispatch::KillFocus(UIEventTarget* tgt) +{ + if (focus && focus == tgt) { + focus = 0; + tgt->KillFocus(); + } +} diff --git a/StarsEx/UIEventDispatch.h b/StarsEx/UIEventDispatch.h new file mode 100644 index 0000000..442a539 --- /dev/null +++ b/StarsEx/UIEventDispatch.h @@ -0,0 +1,60 @@ +/* Starshatter: The Open Source Project + Copyright (c) 2021-2022, Starshatter: The Open Source Project Contributors + Copyright (c) 2011-2012, Starshatter OpenSource Distribution Contributors + Copyright (c) 1997-2006, Destroyer Studios LLC. + + AUTHOR: John DiCamillo + + + OVERVIEW + ======== + Event Dispatch class +*/ + +#ifndef UIEventDispatch_h +#define UIEventDispatch_h + +#include "List.h" +#include "Types.h" +#include "UIEventTarget.h" + + +class UIEventDispatch +{ +public: + static const char* TYPENAME() { return "UIEventDispatch"; } + + UIEventDispatch(); + virtual ~UIEventDispatch(); + + static void Create(); + static void Close(); + static UIEventDispatch* GetInstance() { return dispatcher; } + + virtual void Dispatch(); + virtual void Register(UIEventTarget* tgt); + virtual void Unregister(UIEventTarget* tgt); + + virtual UIEventTarget* GetCapture(); + virtual int CaptureMouse(UIEventTarget* tgt); + virtual int ReleaseMouse(UIEventTarget* tgt); + + virtual UIEventTarget* GetFocus(); + virtual void SetFocus(UIEventTarget* tgt); + virtual void KillFocus(UIEventTarget* tgt); + + virtual void MouseEnter(UIEventTarget* tgt); + +protected: + int mouse_x, mouse_y, mouse_l, mouse_r; + List clients; + UIEventTarget* capture; + UIEventTarget* current; + UIEventTarget* focus; + UIEventTarget* click_tgt; + + static UIEventDispatch* dispatcher; +}; + + +#endif // UIEventDispatch_h diff --git a/StarsEx/UIEventTarget.h b/StarsEx/UIEventTarget.h new file mode 100644 index 0000000..c8acd4c --- /dev/null +++ b/StarsEx/UIEventTarget.h @@ -0,0 +1,57 @@ +/* Starshatter: The Open Source Project + Copyright (c) 2021-2022, Starshatter: The Open Source Project Contributors + Copyright (c) 2011-2012, Starshatter OpenSource Distribution Contributors + Copyright (c) 1997-2006, Destroyer Studios LLC. + + AUTHOR: John DiCamillo + + + OVERVIEW + ======== + Event Target interface class +*/ + +#ifndef UIEventTarget_h +#define UIEventTarget_h + +#include "Types.h" +#include "Geometry.h" + + +class UIEventTarget +{ +public: + static const char* TYPENAME() { return "UIEventTarget"; } + + virtual ~UIEventTarget() { } + + int operator == (const UIEventTarget& t) const { return this == &t; } + + virtual int OnMouseMove(int x, int y) { return 0; } + virtual int OnLButtonDown(int x, int y) { return 0; } + virtual int OnLButtonUp(int x, int y) { return 0; } + virtual int OnClick() { return 0; } + virtual int OnSelect() { return 0; } + virtual int OnRButtonDown(int x, int y) { return 0; } + virtual int OnRButtonUp(int x, int y) { return 0; } + virtual int OnMouseEnter(int x, int y) { return 0; } + virtual int OnMouseExit(int x, int y) { return 0; } + virtual int OnMouseWheel(int wheel) { return 0; } + + virtual int OnKeyDown(int vk, int flags) { return 0; } + + virtual void SetFocus() { } + virtual void KillFocus() { } + virtual bool HasFocus() const { return false; } + + virtual bool IsEnabled() const { return true; } + virtual bool IsVisible() const { return true; } + virtual bool IsFormActive() const { return true; } + + virtual Rect TargetRect() const { return Rect(); } + + virtual const char* GetDescription() const { return "UIEventTarget"; } +}; + + +#endif // UIEventTarget_h -- cgit v1.1