Event queue refactor

This commit is contained in:
elasota
2019-12-31 03:55:17 -05:00
parent 84e4f9fb0b
commit 52338a3983
25 changed files with 244 additions and 297 deletions

View File

@@ -8,6 +8,7 @@
#include "PLAppleEvents.h"
#include "PLKeyEncoding.h"
#include "PLTimeTaggedVOSEvent.h"
#include "PLToolUtils.h"
#include "PLQDraw.h"
#include "Externs.h"
@@ -17,8 +18,8 @@
short BitchAboutColorDepth (void);
void HandleMouseEvent (EventRecord *);
void HandleKeyEvent (EventRecord *);
void HandleMouseEvent (const GpMouseInputEvent &, uint32_t);
void HandleKeyEvent (const KeyDownStates &keyStates, const GpKeyboardInputEvent &);
void HandleUpdateEvent (EventRecord *);
void HandleOSEvent (EventRecord *);
void HandleHighLevelEvent (EventRecord *);
@@ -59,24 +60,25 @@ short BitchAboutColorDepth (void)
//-------------------------------------------------------------- HandleMouseEvent
// Handle a mouse click event.
void HandleMouseEvent (EventRecord *theEvent)
void HandleMouseEvent (const GpMouseInputEvent &theEvent, uint32_t tick)
{
WindowPtr whichWindow;
long menuChoice, newSize;
short thePart, hDelta, vDelta;
Boolean isDoubleClick;
Point evtPoint = Point::Create(theEvent.m_x, theEvent.m_y);
thePart = FindWindow(theEvent->where, &whichWindow);
thePart = FindWindow(evtPoint, &whichWindow);
switch (thePart)
{
case inMenuBar:
menuChoice = MenuSelect(theEvent->where);
menuChoice = MenuSelect(evtPoint);
DoMenuChoice(menuChoice);
break;
case inDrag:
DragWindow(whichWindow, theEvent->where, &thisMac.screen);
DragWindow(whichWindow, evtPoint, &thisMac.screen);
if (whichWindow == mainWindow)
{
SendBehind(mainWindow, (WindowPtr)0L);
@@ -94,7 +96,7 @@ void HandleMouseEvent (EventRecord *theEvent)
break;
case inGoAway:
if (TrackGoAway(whichWindow,theEvent->where))
if (TrackGoAway(whichWindow, evtPoint))
{
if (whichWindow == mapWindow)
ToggleMapWindow();
@@ -110,43 +112,43 @@ void HandleMouseEvent (EventRecord *theEvent)
case inGrow:
if (whichWindow == mapWindow)
{
newSize = GrowWindow(mapWindow, theEvent->where, &thisMac.gray);
newSize = GrowWindow(mapWindow, evtPoint, &thisMac.gray);
ResizeMapWindow(LoWord(newSize), HiWord(newSize));
}
break;
case inZoomIn:
case inZoomOut:
if (TrackBox(whichWindow, theEvent->where, thePart))
if (TrackBox(whichWindow, evtPoint, thePart))
ZoomWindow(whichWindow, thePart, true);
break;
case inContent:
if (whichWindow == mainWindow)
{
hDelta = theEvent->where.h - lastWhere.h;
hDelta = evtPoint.h - lastWhere.h;
if (hDelta < 0)
hDelta = -hDelta;
vDelta = theEvent->where.v - lastWhere.v;
vDelta = evtPoint.v - lastWhere.v;
if (vDelta < 0)
vDelta = -vDelta;
if (((theEvent->when - lastUp) < doubleTime) && (hDelta < 5) &&
if (((tick - lastUp) < doubleTime) && (hDelta < 5) &&
(vDelta < 5))
isDoubleClick = true;
else
{
isDoubleClick = false;
lastUp = theEvent->when;
lastWhere = theEvent->where;
lastUp = tick;
lastWhere = evtPoint;
}
HandleMainClick(theEvent->where, isDoubleClick);
HandleMainClick(evtPoint, isDoubleClick);
}
else if (whichWindow == mapWindow)
HandleMapClick(theEvent);
else if (whichWindow == toolsWindow)
HandleToolsClick(theEvent->where);
HandleToolsClick(evtPoint);
else if (whichWindow == linkWindow)
HandleLinkClick(theEvent->where);
HandleLinkClick(evtPoint);
break;
default:
@@ -157,15 +159,12 @@ void HandleMouseEvent (EventRecord *theEvent)
//-------------------------------------------------------------- HandleKeyEvent
// Handle a key-down event.
void HandleKeyEvent (EventRecord *theEvent)
void HandleKeyEvent (const KeyDownStates &keyStates, const GpKeyboardInputEvent &theEvent)
{
intptr_t theChar;
Boolean shiftDown, commandDown, optionDown;
theChar = theEvent->message;
shiftDown = ((theEvent->modifiers & shiftKey) != 0);
commandDown = ((theEvent->modifiers & cmdKey) != 0);
optionDown = ((theEvent->modifiers & optionKey) != 0);
const intptr_t theChar = PackVOSKeyCode(theEvent);
const bool shiftDown = BitTst(keyStates, PL_KEY_EITHER_SPECIAL(kShift));
const bool commandDown = BitTst(keyStates, PL_KEY_EITHER_SPECIAL(kControl));
const bool optionDown = BitTst(keyStates, PL_KEY_EITHER_SPECIAL(kAlt));
if ((commandDown) && (!optionDown))
DoMenuChoice(MenuKey(static_cast<int>(theChar)));
@@ -324,6 +323,7 @@ void HandleKeyEvent (EventRecord *theEvent)
//-------------------------------------------------------------- HandleUpdateEvent
// Handle an update event.
#if 0
void HandleUpdateEvent (EventRecord *theEvent)
{
if ((WindowPtr)theEvent->message == mainWindow)
@@ -431,6 +431,7 @@ void HandleHighLevelEvent (EventRecord *theEvent)
if ((theErr != PLErrors::kNone) && (theErr != errAEEventNotHandled))
YellowAlert(kYellowAppleEventErr, theErr);
}
#endif
//-------------------------------------------------------------- HandleIdleTask
// Handle some processing during event lulls.
@@ -457,10 +458,10 @@ void HandleIdleTask (void)
void HandleEvent (void)
{
KeyMap eventKeys;
EventRecord theEvent;
long sleep = 2;
Boolean itHappened = true;
KeyDownStates eventKeys;
TimeTaggedVOSEvent theEvent;
uint32_t sleep = 2;
bool itHappened = true;
GetKeys(eventKeys);
if ((BitTst(eventKeys, PL_KEY_EITHER_SPECIAL(kControl))) &&
@@ -475,39 +476,32 @@ void HandleEvent (void)
SelectTool(kSelectTool);
}
// GP: Use WaitNextEvent to yield to the host
//if (thisMac.hasWNE)
itHappened = WaitNextEvent(everyEvent, &theEvent, sleep, nil);
//else
//{
// // SystemTask();
// itHappened = GetNextEvent(everyEvent, &theEvent);
//}
itHappened = WaitForEvent(&theEvent, sleep);
if (itHappened)
{
switch (theEvent.what)
if (theEvent.m_vosEvent.m_eventType == GpVOSEventTypes::kMouseInput)
{
case mouseDown:
HandleMouseEvent(&theEvent);
break;
case keyDown:
case autoKey:
HandleKeyEvent(&theEvent);
break;
case updateEvt:
HandleUpdateEvent(&theEvent);
break;
case osEvt:
HandleOSEvent(&theEvent);
break;
case kHighLevelEvent:
HandleHighLevelEvent(&theEvent);
break;
switch (theEvent.m_vosEvent.m_event.m_mouseInputEvent.m_eventType)
{
case GpMouseEventTypes::kDown:
HandleMouseEvent(theEvent.m_vosEvent.m_event.m_mouseInputEvent, theEvent.m_timestamp);
break;
default:
break;
}
}
else if (theEvent.m_vosEvent.m_eventType == GpVOSEventTypes::kKeyboardInput)
{
switch (theEvent.m_vosEvent.m_event.m_keyboardInputEvent.m_eventType)
{
case GpKeyboardInputEventTypes::kDown:
case GpKeyboardInputEventTypes::kAuto:
HandleKeyEvent(eventKeys, theEvent.m_vosEvent.m_event.m_keyboardInputEvent);
break;
default:
break;
}
}
}
else

View File

@@ -7,7 +7,9 @@
#include "PLToolUtils.h"
#include "PLPasStr.h"
#include "PLEventQueue.h"
#include "PLKeyEncoding.h"
#include "PLTimeTaggedVOSEvent.h"
#include "Externs.h"
#include "Environ.h"
#include "FontManager.h"
@@ -138,8 +140,8 @@ void SetUpFinalScreen (void)
void DoGameOverStarAnimation (void)
{
#define kStarFalls 8
EventRecord theEvent;
KeyMap theKeys;
TimeTaggedVOSEvent theEvent;
KeyDownStates theKeys;
Rect angelDest;
long nextLoop;
short which, i, count, pass;
@@ -213,9 +215,12 @@ void DoGameOverStarAnimation (void)
GetKeys(theKeys);
if ((BitTst(theKeys, PL_KEY_EITHER_SPECIAL(kControl))) || (BitTst(theKeys, PL_KEY_EITHER_SPECIAL(kAlt))) || (BitTst(theKeys, PL_KEY_EITHER_SPECIAL(kShift))))
noInteruption = false;
if (GetNextEvent(everyEvent, &theEvent))
if ((theEvent.what == mouseDown) || (theEvent.what == keyDown))
if (PortabilityLayer::EventQueue::GetInstance()->Dequeue(&theEvent))
{
if (theEvent.IsLMouseDownEvent() || theEvent.IsKeyDownEvent())
noInteruption = false;
}
Delay(1, nullptr);
}
@@ -436,10 +441,10 @@ void DrawPages (void)
void DoDiedGameOver (void)
{
EventRecord theEvent;
KeyMap theKeys;
long nextLoop;
Boolean userAborted;
TimeTaggedVOSEvent theEvent;
KeyDownStates theKeys;
long nextLoop;
Boolean userAborted;
userAborted = false;
InitDiedGameOver();
@@ -460,12 +465,15 @@ void DoDiedGameOver (void)
pagesStuck = 8;
userAborted = true;
}
if (GetNextEvent(everyEvent, &theEvent))
if ((theEvent.what == mouseDown) || (theEvent.what == keyDown))
if (PortabilityLayer::EventQueue::GetInstance()->Dequeue(&theEvent))
{
if (theEvent.IsLMouseDownEvent() || theEvent.IsKeyDownEvent())
{
pagesStuck = 8;
userAborted = true;
}
}
Delay(1, nullptr);
}

View File

@@ -5,6 +5,7 @@
//----------------------------------------------------------------------------
//============================================================================
struct GpMouseInputEvent;
//-------------------------------------------------------------- Prototypes
@@ -163,7 +164,7 @@ void ResizeMapWindow (SInt16, SInt16);
void OpenMapWindow (void);
void CloseMapWindow (void);
void ToggleMapWindow (void);
void HandleMapClick (EventRecord *);
void HandleMapClick (const GpMouseInputEvent &);
void MoveRoom (Point);
void DoMarquee (void); // --- Marquee.c

View File

@@ -31,7 +31,7 @@ Boolean QuerySaveGame (void);
demoPtr demoData;
KeyMap theKeys;
KeyDownStates theKeys;
Dialog *saveDial;
short demoIndex, batteryFrame;
Boolean isEscPauseKey, paused, batteryWasEngaged;

View File

@@ -384,6 +384,8 @@ int gpAppMain()
// numSMWarnings++;
// BitchAboutSM3();
// }
UpdateMainWindow();
while (!quitting) // this is the main loop
HandleEvent();

View File

@@ -159,6 +159,8 @@ void UpdateMainWindow (void)
DrawOnSplash(mainWindow->GetDrawSurface());
}
mainWindow->m_graf.m_port.SetDirty(PortabilityLayer::QDPortDirtyFlag_Contents);
splashDrawn = true;
}
@@ -370,7 +372,7 @@ void UpdateEditWindowTitle (void)
void HandleMainClick (Point wherePt, Boolean isDoubleClick)
{
KeyMap theseKeys;
KeyDownStates theseKeys;
if ((theMode != kEditMode) || (mainWindow == nil) ||
(!houseUnlocked))

View File

@@ -554,7 +554,7 @@ void LiveVScrollAction (ControlHandle theControl, short thePart)
//-------------------------------------------------------------- HandleMapClick
void HandleMapClick (EventRecord *theEvent)
void HandleMapClick (const GpMouseInputEvent &theEvent)
{
#ifndef COMPILEDEMO
Rect aRoom;
@@ -565,7 +565,7 @@ void HandleMapClick (EventRecord *theEvent)
short roomH, roomV, itsNumber;
ControlActionUPP scrollHActionUPP, scrollVActionUPP;
wherePt = theEvent->where;
wherePt = Point::Create(theEvent.m_x, theEvent.m_y);
scrollHActionUPP = NewControlActionUPP(LiveHScrollAction);
scrollVActionUPP = NewControlActionUPP(LiveVScrollAction);

View File

@@ -48,7 +48,7 @@ short wasFlower;
Boolean AddNewObject (Point where, short what, Boolean showItNow)
{
KeyMap theseKeys;
KeyDownStates theseKeys;
Rect srcRect, newRect;
short direction, dist;
Boolean handled, drawWholeRoom;

View File

@@ -2683,7 +2683,7 @@ void DrawThisRoomsObjects (void)
void HiliteAllObjects (void)
{
#ifndef COMPILEDEMO
KeyMap theseKeys;
KeyDownStates theseKeys;
short i;
Pattern dummyPattern;

View File

@@ -35,7 +35,6 @@ typedef struct
void InitGlider (gliderPtr, short);
void SetHouseToFirstRoom (void);
void SetHouseToSavedRoom (void);
void HandlePlayEvent (void);
void PlayGame (void);
void HandleRoomVisitation (void);
void SetObjectsToDefaults (void);
@@ -364,48 +363,6 @@ void SetHouseToSavedRoom (void)
ForceThisRoom(smallGame.roomNumber);
}
//-------------------------------------------------------------- HandlePlayEvent
void HandlePlayEvent (void)
{
EventRecord theEvent;
GrafPtr wasPort;
long sleep = 2;
if (WaitNextEvent(everyEvent, &theEvent, sleep, nil))
{
if ((theEvent.what == updateEvt) &&
((WindowPtr)theEvent.message == mainWindow))
{
GetPort(&wasPort);
SetPortWindowPort(mainWindow);
CopyBits((BitMap *)*GetGWorldPixMap(workSrcMap),
GetPortBitMapForCopyBits(GetWindowPort(mainWindow)),
&justRoomsRect, &justRoomsRect, srcCopy);
RefreshScoreboard(kNormalTitleMode);
EndUpdate(mainWindow);
SetPort(wasPort);
}
else if ((theEvent.what == osEvt) && (theEvent.message & 0x01000000))
{
if (theEvent.message & 0x00000001) // resume event
{
switchedOut = false;
ToggleMusicWhilePlaying();
HideCursor();
// HideMenuBarOld(); // TEMP
}
else // suspend event
{
InitCursor();
switchedOut = true;
ToggleMusicWhilePlaying();
// ShowMenuBarOld(); // TEMP replace with Carbon calls
}
}
}
}
//-------------------------------------------------------------- PlayGame
void PlayGame (void)
@@ -417,10 +374,7 @@ void PlayGame (void)
if (doBackground)
{
do
{
HandlePlayEvent();
} while (switchedOut);
Delay(2, nil);
}
HandleTelephone();

View File

@@ -160,7 +160,7 @@ void SetInitialTiles (short background, Boolean doRoom)
#ifndef COMPILEDEMO
Boolean CreateNewRoom (short h, short v)
{
KeyMap theKeys;
KeyDownStates theKeys;
long howMuch;
PLError_t theErr;
short i, availableRoom;

View File

@@ -4,11 +4,13 @@
//----------------------------------------------------------------------------
//============================================================================
#include "PLEventQueue.h"
#include "PLKeyEncoding.h"
#include "PLQDraw.h"
#include "PLPasStr.h"
#include "PLResources.h"
#include "PLSound.h"
#include "PLTimeTaggedVOSEvent.h"
#include "Externs.h"
#include "Utilities.h"
@@ -372,10 +374,10 @@ long LongSquareRoot (long theNumber)
Boolean WaitForInputEvent (short seconds)
{
EventRecord theEvent;
KeyMap theKeys;
long timeToBail;
Boolean waiting, didResume;
TimeTaggedVOSEvent theEvent;
KeyDownStates theKeys;
long timeToBail;
Boolean waiting, didResume;
timeToBail = TickCount() + 60L * (long)seconds;
FlushEvents(everyEvent, 0);
@@ -387,22 +389,11 @@ Boolean WaitForInputEvent (short seconds)
GetKeys(theKeys);
if (BitTst(theKeys, PL_KEY_EITHER_SPECIAL(kControl)) || BitTst(theKeys, PL_KEY_EITHER_SPECIAL(kAlt)) || BitTst(theKeys, PL_KEY_EITHER_SPECIAL(kShift)))
waiting = false;
if (GetNextEvent(everyEvent, &theEvent))
if (PortabilityLayer::EventQueue::GetInstance()->Dequeue(&theEvent))
{
if ((theEvent.what == mouseDown) || (theEvent.what == keyDown))
if (theEvent.IsLMouseDownEvent() || theEvent.IsKeyDownEvent())
waiting = false;
else if ((theEvent.what == osEvt) && (theEvent.message & 0x01000000))
{
if (theEvent.message & 0x00000001) // resuming
{
didResume = true;
waiting = false;
}
else // suspending
{
InitCursor();
}
}
}
if ((seconds != -1) && (TickCount() >= timeToBail))
waiting = false;
@@ -419,7 +410,7 @@ Boolean WaitForInputEvent (short seconds)
void WaitCommandQReleased (void)
{
KeyMap theKeys;
KeyDownStates theKeys;
Boolean waiting;
waiting = true;
@@ -469,7 +460,7 @@ void GetKeyName (intptr_t message, StringPtr theName)
Boolean OptionKeyDown (void)
{
KeyMap theKeys;
KeyDownStates theKeys;
GetKeys(theKeys);
if (BitTst(theKeys, PL_KEY_EITHER_SPECIAL(kAlt)))