public MouseHandler()
{
//Initialization of trigger events and actions
var trgDown = CreateTrigger();
var trgUp = CreateTrigger();
var trgSyncRead = CreateTrigger();
foreach (var p in PlayerInfo.Players)
{
TriggerRegisterPlayerEvent(trgDown, p.BlzPlayer, EVENT_PLAYER_MOUSE_DOWN);
TriggerRegisterPlayerEvent(trgUp, p.BlzPlayer, EVENT_PLAYER_MOUSE_UP);
BlzTriggerRegisterPlayerSyncEvent(trgSyncRead, p.BlzPlayer, "SyncCamX", false);
BlzTriggerRegisterPlayerSyncEvent(trgSyncRead, p.BlzPlayer, "SyncCamY", false);
BlzTriggerRegisterPlayerSyncEvent(trgSyncRead, p.BlzPlayer, "SyncCamZ", false);
}
TriggerAddAction(trgDown, PlayerMouseDown);
TriggerAddAction(trgUp, PlayerMouseUp);
TriggerAddAction(trgSyncRead, SyncCamDataRead);
TimerStart(CreateTimer(), 0.1f, true, SyncCamDataSend);
}
public void SyncCamDataSend()
{
//This sends the current camera position of each player to the others to sync it
//Note: I didn't test it in a multiplayer game yet.
//Thx Tasyen https://www.hiveworkshop.com/threads/target-of-current-camera-desync.316384/#post-3355262
BlzSendSyncData("SyncCamX", R2S(GetCameraTargetPositionX()));
BlzSendSyncData("SyncCamY", R2S(GetCameraTargetPositionY()));
BlzSendSyncData("SyncCamZ", R2S(GetCameraEyePositionZ()));
}
public void SyncCamDataRead()
{
//This saves the current camera position of each player in float variables (CamXSynced, ...)
var p = GetTriggerPlayer();
int pid = GetPlayerId(p);
string prefix = BlzGetTriggerSyncPrefix();
if (prefix == "SyncCamX")
{
CamXSynced[pid] = S2R(BlzGetTriggerSyncData());
}
else if (prefix == "SyncCamY")
{
CamYSynced[pid] = S2R(BlzGetTriggerSyncData());
}
else if (prefix == "SyncCamZ")
{
CamZSynced[pid] = S2R(BlzGetTriggerSyncData());
}
}
public bool WithinCurrentCamView(int pid, float x, float y)
{
//This function is called on click. x and y are the coordinates of the mouse click. Returns true if click is in current view.
//Note: If your top/bottom UI is larger or smaller, you can just change the values 0.317 and 0.5 below.
float dx = x - CamXSynced[pid];
float dy = y - CamYSynced[pid];
//For the default camera angle on a 16:10 screen with cliff level 2, for a camera ...
//... with height 1000, the y-range is [-317, 445]
//... with height 2000, the y-range is [-645, 990] - so just linear/proportional.
//In x-direction the range is the same in both directions, obviously.
//Let's just use 0.9, which will cover all aspect ratios (except maybe ultra-wide).
return dx > -0.9f * CamZSynced[pid] && dx < 0.9f * CamZSynced[pid] && dy > -0.317f * CamZSynced[pid] && dy < 0.5f * CamZSynced[pid];
}