/
MouseHandler.cpp
966 lines (718 loc) · 29.7 KB
/
MouseHandler.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */
#include "MouseHandler.h"
#include "CommandColors.h"
#include "InputReceiver.h"
#include "GuiHandler.h"
#include "MiniMap.h"
#include "MouseCursor.h"
#include "TooltipConsole.h"
#include "Game/CameraHandler.h"
#include "Game/Camera.h"
#include "Game/Game.h"
#include "Game/GlobalUnsynced.h"
#include "Game/InMapDraw.h"
#include "Game/SelectedUnitsHandler.h"
#include "Game/TraceRay.h"
#include "Game/Camera/CameraController.h"
#include "Game/Players/Player.h"
#include "Game/Players/PlayerHandler.h"
#include "Game/UI/UnitTracker.h"
#include "Lua/LuaInputReceiver.h"
#include "Map/Ground.h"
#include "Rendering/GlobalRendering.h"
#include "Rendering/Fonts/glFont.h"
#include "Rendering/GL/myGL.h"
#include "Rendering/GL/RenderDataBuffer.hpp"
#include "Rendering/Textures/Bitmap.h"
#include "Sim/Features/Feature.h"
#include "Sim/Units/Unit.h"
#include "Game/UI/Groups/Group.h"
#include "System/Config/ConfigHandler.h"
#include "System/EventHandler.h"
#include "System/Exceptions.h"
#include "System/FastMath.h"
#include "System/myMath.h"
#include "System/SafeUtil.h"
#include "System/StringUtil.h"
#include "System/Input/KeyInput.h"
#include "System/Input/MouseInput.h"
#include <algorithm>
// can't be up there since those contain conflicting definitions
#include <SDL_mouse.h>
#include <SDL_events.h>
#include <SDL_keycode.h>
CONFIG(bool, HardwareCursor).defaultValue(false).description("Sets hardware mouse cursor rendering. If you have a low framerate, your mouse cursor will seem \"laggy\". Setting hardware cursor will render the mouse cursor separately from spring and the mouse will behave normally. Note, not all GPU drivers support it in fullscreen mode!");
CONFIG(bool, InvertMouse).defaultValue(false);
CONFIG(float, DoubleClickTime).defaultValue(200.0f).description("Double click time in milliseconds.");
CONFIG(float, ScrollWheelSpeed)
.defaultValue(25.0f)
.minimumValue(-255.f)
.maximumValue(255.f);
CONFIG(float, CrossSize).defaultValue(12.0f);
CONFIG(float, CrossAlpha).defaultValue(0.5f);
CONFIG(float, CrossMoveScale).defaultValue(1.0f);
CONFIG(float, MouseDragScrollThreshold).defaultValue(0.3f);
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CMouseHandler* mouse = nullptr;
static CInputReceiver*& activeReceiver = CInputReceiver::GetActiveReceiverRef();
CMouseHandler::CMouseHandler()
{
const int2 mousepos = mouseInput->GetPos();
lastx = mousepos.x;
lasty = mousepos.y;
#ifndef __APPLE__
hardwareCursor = configHandler->GetBool("HardwareCursor");
#endif
invertMouse = configHandler->GetBool("InvertMouse");
doubleClickTime = configHandler->GetFloat("DoubleClickTime") / 1000.0f;
scrollWheelSpeed = configHandler->GetFloat("ScrollWheelSpeed");
crossSize = configHandler->GetFloat("CrossSize");
crossAlpha = configHandler->GetFloat("CrossAlpha");
crossMoveScale = configHandler->GetFloat("CrossMoveScale") * 0.005f;
dragScrollThreshold = configHandler->GetFloat("MouseDragScrollThreshold");
configHandler->NotifyOnChange(this, {"MouseDragScrollThreshold", "ScrollWheelSpeed"});
}
CMouseHandler::~CMouseHandler()
{
if (hwHide)
SDL_ShowCursor(SDL_ENABLE);
configHandler->RemoveObserver(this);
}
void CMouseHandler::InitStatic()
{
assert(mouse == nullptr);
assert(mouseInput == nullptr);
mouseInput = IMouseInput::GetInstance();
mouse = new CMouseHandler();
}
void CMouseHandler::KillStatic()
{
IMouseInput::FreeInstance(mouseInput);
spring::SafeDelete(mouse);
}
void CMouseHandler::ReloadCursors()
{
const CMouseCursor::HotSpot mCenter = CMouseCursor::Center;
const CMouseCursor::HotSpot mTopLeft = CMouseCursor::TopLeft;
activeCursorIdx = -1;
loadedCursors.clear();
loadedCursors.reserve(32);
// null-cursor; always lives at index 0
loadedCursors.emplace_back();
cursorCommandMap.clear();
cursorCommandMap.reserve(32);
cursorFileMap.clear();
cursorFileMap.reserve(32);
cursorCommandMap["none"] = loadedCursors.size() - 1;
cursorFileMap["null"] = loadedCursors.size() - 1;
AssignMouseCursor("", "cursornormal", mTopLeft, false);
AssignMouseCursor("Area attack", "cursorareaattack", mCenter, false);
AssignMouseCursor("Area attack", "cursorattack", mCenter, false); // backup
AssignMouseCursor("Attack", "cursorattack", mCenter, false);
AssignMouseCursor("AttackBad", "cursorattackbad", mCenter, false);
AssignMouseCursor("AttackBad", "cursorattack", mCenter, false); // backup
AssignMouseCursor("BuildBad", "cursorbuildbad", mCenter, false);
AssignMouseCursor("BuildGood", "cursorbuildgood", mCenter, false);
AssignMouseCursor("Capture", "cursorcapture", mCenter, false);
AssignMouseCursor("Centroid", "cursorcentroid", mCenter, false);
AssignMouseCursor("DeathWait", "cursordwatch", mCenter, false);
AssignMouseCursor("DeathWait", "cursorwait", mCenter, false); // backup
AssignMouseCursor("ManualFire", "cursormanfire", mCenter, false);
AssignMouseCursor("ManualFire", "cursordgun", mCenter, false); // backup (backward compability)
AssignMouseCursor("ManualFire", "cursorattack", mCenter, false); // backup
AssignMouseCursor("Fight", "cursorfight", mCenter, false);
AssignMouseCursor("Fight", "cursorattack", mCenter, false); // backup
AssignMouseCursor("GatherWait", "cursorgather", mCenter, false);
AssignMouseCursor("GatherWait", "cursorwait", mCenter, false); // backup
AssignMouseCursor("Guard", "cursordefend", mCenter, false);
AssignMouseCursor("Load units", "cursorpickup", mCenter, false);
AssignMouseCursor("Move", "cursormove", mCenter, false);
AssignMouseCursor("Patrol", "cursorpatrol", mCenter, false);
AssignMouseCursor("Reclaim", "cursorreclamate", mCenter, false);
AssignMouseCursor("Repair", "cursorrepair", mCenter, false);
AssignMouseCursor("Resurrect", "cursorrevive", mCenter, false);
AssignMouseCursor("Resurrect", "cursorrepair", mCenter, false); // backup
AssignMouseCursor("Restore", "cursorrestore", mCenter, false);
AssignMouseCursor("Restore", "cursorrepair", mCenter, false); // backup
AssignMouseCursor("SelfD", "cursorselfd", mCenter, false);
AssignMouseCursor("SquadWait", "cursornumber", mCenter, false);
AssignMouseCursor("SquadWait", "cursorwait", mCenter, false); // backup
AssignMouseCursor("TimeWait", "cursortime", mCenter, false);
AssignMouseCursor("TimeWait", "cursorwait", mCenter, false); // backup
AssignMouseCursor("Unload units", "cursorunload", mCenter, false);
AssignMouseCursor("Wait", "cursorwait", mCenter, false);
// the default cursor must exist
const auto defCursorIt = cursorCommandMap.find("");
if (defCursorIt == cursorCommandMap.end()) {
throw content_error(
"Unable to load default cursor. Check that you have the required\n"
"content packages installed in your Spring \"base/\" directory.\n");
}
activeCursorIdx = defCursorIt->second;
}
/******************************************************************************/
void CMouseHandler::MouseMove(int x, int y, int dx, int dy)
{
// FIXME: don't update if locked?
lastx = std::abs(x);
lasty = std::abs(y);
// MouseInput passes negative coordinates when cursor leaves the window
offscreen = (x < 0 && y < 0);
const int2 screenCenter = globalRendering->GetScreenCenter();
scrollx += (lastx - screenCenter.x);
scrolly += (lasty - screenCenter.y);
dir = hide ? camera->GetDir() : camera->CalcPixelDir(x, y);
if (locked) {
camHandler->GetCurrentController().MouseMove(float3(dx, dy, invertMouse ? -1.0f : 1.0f));
return;
}
const int movedPixels = (int)fastmath::sqrt_sse(float(dx*dx + dy*dy));
buttons[SDL_BUTTON_LEFT].movement += movedPixels;
buttons[SDL_BUTTON_RIGHT].movement += movedPixels;
if (game != nullptr && !game->IsGameOver())
playerHandler.Player(gu->myPlayerNum)->currentStats.mousePixels += movedPixels;
if (activeReceiver != nullptr)
activeReceiver->MouseMove(x, y, dx, dy, activeButtonIdx);
if (inMapDrawer != nullptr && inMapDrawer->IsDrawMode())
inMapDrawer->MouseMove(x, y, dx, dy, activeButtonIdx);
if (buttons[SDL_BUTTON_MIDDLE].pressed && (activeReceiver == nullptr)) {
camHandler->GetCurrentController().MouseMove(float3(dx, dy, invertMouse ? -1.0f : 1.0f));
unitTracker.Disable();
return;
}
}
void CMouseHandler::MousePress(int x, int y, int button)
{
if (button > NUM_BUTTONS)
return;
dir = hide ? camera->GetDir() : camera->CalcPixelDir(x, y);
if (game != nullptr && !game->IsGameOver())
playerHandler.Player(gu->myPlayerNum)->currentStats.mouseClicks++;
ButtonPressEvt& bp = buttons[button];
bp.chorded = (buttons[SDL_BUTTON_LEFT].pressed || buttons[SDL_BUTTON_RIGHT].pressed);
bp.pressed = true;
bp.time = gu->gameTime;
bp.x = x;
bp.y = y;
bp.camPos = camera->GetPos();
bp.dir = dir;
bp.movement = 0;
activeButtonIdx = button;
if (activeReceiver && activeReceiver->MousePress(x, y, button))
return;
if (inMapDrawer && inMapDrawer->IsDrawMode()) {
inMapDrawer->MousePress(x, y, button);
return;
}
// limited receivers for MMB
if (button == SDL_BUTTON_MIDDLE) {
if (!locked) {
if (luaInputReceiver->MousePress(x, y, button)) {
activeReceiver = luaInputReceiver;
return;
}
if ((minimap != nullptr) && minimap->FullProxy()) {
if (minimap->MousePress(x, y, button)) {
activeReceiver = minimap;
return;
}
}
}
return;
}
if (luaInputReceiver->MousePress(x, y, button)) {
if (activeReceiver == nullptr)
activeReceiver = luaInputReceiver;
return;
}
if (game != nullptr && !game->hideInterface) {
for (CInputReceiver* recv: CInputReceiver::GetReceivers()) {
if (recv != nullptr && recv->MousePress(x, y, button)) {
if (activeReceiver == nullptr)
activeReceiver = recv;
return;
}
}
} else {
if (guihandler != nullptr && guihandler->MousePress(x, y, button)) {
if (activeReceiver == nullptr)
activeReceiver = guihandler; // for default (rmb) commands
return;
}
}
}
/**
* GetSelectionBoxCoeff
* returns the topright & bottomleft corner positions of the SelectionBox in (cam->right, cam->up)-space
*/
void CMouseHandler::GetSelectionBoxCoeff(const float3& pos1, const float3& dir1, const float3& pos2, const float3& dir2, float2& topright, float2& btmleft)
{
float dist = CGround::LineGroundCol(pos1, pos1 + dir1 * globalRendering->viewRange * 1.4f, false);
if (dist < 0.0f)
dist = globalRendering->viewRange * 1.4f;
float3 gpos1 = pos1 + dir1 * dist;
dist = CGround::LineGroundCol(pos2, pos2 + dir2 * globalRendering->viewRange * 1.4f, false);
if (dist < 0.0f)
dist = globalRendering->viewRange * 1.4f;
float3 gpos2 = pos2 + dir2 * dist;
const float3 cdir1 = (gpos1 - camera->GetPos()).ANormalize();
const float3 cdir2 = (gpos2 - camera->GetPos()).ANormalize();
// prevent DivByZero
float cdir1_fw = cdir1.dot(camera->GetDir()); if (cdir1_fw == 0.0f) cdir1_fw = 0.0001f;
float cdir2_fw = cdir2.dot(camera->GetDir()); if (cdir2_fw == 0.0f) cdir2_fw = 0.0001f;
// one corner of the rectangle
topright.x = cdir1.dot(camera->GetRight()) / cdir1_fw;
topright.y = cdir1.dot(camera->GetUp()) / cdir1_fw;
// opposite corner
btmleft.x = cdir2.dot(camera->GetRight()) / cdir2_fw;
btmleft.y = cdir2.dot(camera->GetUp()) / cdir2_fw;
// sort coeff so topright really is the topright corner
if (topright.x < btmleft.x) std::swap(topright.x, btmleft.x);
if (topright.y < btmleft.y) std::swap(topright.y, btmleft.y);
}
void CMouseHandler::MouseRelease(int x, int y, int button)
{
const CUnit *_lastClicked = lastClicked;
lastClicked = nullptr;
if (button > NUM_BUTTONS)
return;
dir = hide ? camera->GetDir() : camera->CalcPixelDir(x, y);
buttons[button].pressed = false;
if (inMapDrawer && inMapDrawer->IsDrawMode()){
inMapDrawer->MouseRelease(x, y, button);
return;
}
if (activeReceiver != nullptr) {
activeReceiver->MouseRelease(x, y, button);
if (!buttons[SDL_BUTTON_LEFT].pressed && !buttons[SDL_BUTTON_MIDDLE].pressed && !buttons[SDL_BUTTON_RIGHT].pressed)
activeReceiver = nullptr;
return;
}
// Switch camera mode on a middle click that wasn't a middle mouse drag scroll.
// the latter is determined by the time the mouse was held down:
// switch (dragScrollThreshold)
// <= means a camera mode switch
// > means a drag scroll
if (button == SDL_BUTTON_MIDDLE) {
if (buttons[SDL_BUTTON_MIDDLE].time > (gu->gameTime - dragScrollThreshold))
ToggleMiddleClickScroll();
return;
}
if (gu->fpsMode)
return;
// outside game, neither guiHandler nor quadField exist and TraceRay would crash
if (guihandler == nullptr)
return;
if ((button == SDL_BUTTON_LEFT) && !buttons[button].chorded) {
ButtonPressEvt& bp = buttons[SDL_BUTTON_LEFT];
if (!KeyInput::GetKeyModState(KMOD_SHIFT) && !KeyInput::GetKeyModState(KMOD_CTRL))
selectedUnitsHandler.ClearSelected();
if (bp.movement > 4) {
// select box
float2 topright, btmleft;
GetSelectionBoxCoeff(bp.camPos, bp.dir, camera->GetPos(), dir, topright, btmleft);
// GetSelectionBoxCoeff returns us the corner pos, but we want to do a inview frustum check.
// To do so we need the frustum planes (= plane normal + plane offset).
float3 norm1 = camera->GetUp();
float3 norm2 = -camera->GetUp();
float3 norm3 = camera->GetRight();
float3 norm4 = -camera->GetRight();
#define signf(x) ((x>0.0f) ? 1.0f : -1)
if (topright.y != 0) norm1 = (camera->GetDir() * signf(-topright.y)) + (camera->GetUp() / math::fabs(topright.y));
if (btmleft.y != 0) norm2 = (camera->GetDir() * signf( btmleft.y)) - (camera->GetUp() / math::fabs(btmleft.y));
if (topright.x != 0) norm3 = (camera->GetDir() * signf(-topright.x)) + (camera->GetRight() / math::fabs(topright.x));
if (btmleft.x != 0) norm4 = (camera->GetDir() * signf( btmleft.x)) - (camera->GetRight() / math::fabs(btmleft.x));
const float4 plane1(norm1, -(norm1.dot(camera->GetPos())));
const float4 plane2(norm2, -(norm2.dot(camera->GetPos())));
const float4 plane3(norm3, -(norm3.dot(camera->GetPos())));
const float4 plane4(norm4, -(norm4.dot(camera->GetPos())));
selectedUnitsHandler.HandleUnitBoxSelection(plane1, plane2, plane3, plane4);
} else {
const CUnit* unit = nullptr;
const CFeature* feature = nullptr;
TraceRay::GuiTraceRay(camera->GetPos(), dir, globalRendering->viewRange * 1.4f, nullptr, unit, feature, false);
lastClicked = unit;
const bool selectType = (bp.lastRelease >= (gu->gameTime - doubleClickTime) && unit == _lastClicked);
selectedUnitsHandler.HandleSingleUnitClickSelection(const_cast<CUnit*>(unit), true, selectType);
}
bp.lastRelease = gu->gameTime;
}
}
void CMouseHandler::MouseWheel(float delta)
{
if (eventHandler.MouseWheel(delta > 0.0f, delta))
return;
camHandler->GetCurrentController().MouseWheelMove(delta * scrollWheelSpeed);
}
void CMouseHandler::DrawSelectionBox()
{
dir = hide ? camera->GetDir() : camera->CalcPixelDir(lastx, lasty);
if (activeReceiver != nullptr)
return;
if (gu->fpsMode)
return;
const ButtonPressEvt& bp = buttons[SDL_BUTTON_LEFT];
if (!bp.pressed)
return;
if (bp.chorded)
return;
if (bp.movement <= 4)
return;
if (inMapDrawer != nullptr && inMapDrawer->IsDrawMode())
return;
float2 topRight;
float2 bttmLeft;
GetSelectionBoxCoeff(bp.camPos, bp.dir, camera->GetPos(), dir, topRight, bttmLeft);
const float3 xminVec = camera->GetRight() * bttmLeft.x;
const float3 xmaxVec = camera->GetRight() * topRight.x;
const float3 yminVec = camera->GetUp() * bttmLeft.y;
const float3 ymaxVec = camera->GetUp() * topRight.y;
const VA_TYPE_C selBoxVerts[] = {
{camera->GetPos() + (ymaxVec + xmaxVec + camera->GetDir()) * 30.0f, cmdColors.mouseBox},
{camera->GetPos() + (yminVec + xmaxVec + camera->GetDir()) * 30.0f, cmdColors.mouseBox},
{camera->GetPos() + (yminVec + xminVec + camera->GetDir()) * 30.0f, cmdColors.mouseBox},
{camera->GetPos() + (ymaxVec + xminVec + camera->GetDir()) * 30.0f, cmdColors.mouseBox},
};
GL::RenderDataBufferC* buffer = GL::GetRenderBufferC();
Shader::IProgramObject* shader = buffer->GetShader();
glAttribStatePtr->PushEnableBit();
glAttribStatePtr->DisableDepthTest();
glAttribStatePtr->EnableBlendMask();
glAttribStatePtr->BlendFunc((GLenum)cmdColors.MouseBoxBlendSrc(),
(GLenum)cmdColors.MouseBoxBlendDst());
glAttribStatePtr->LineWidth(cmdColors.MouseBoxLineWidth());
shader->Enable();
shader->SetUniformMatrix4x4<const char*, float>("u_movi_mat", false, camera->GetViewMatrix());
shader->SetUniformMatrix4x4<const char*, float>("u_proj_mat", false, camera->GetProjectionMatrix());
buffer->SafeAppend(&selBoxVerts[0], sizeof(selBoxVerts) / sizeof(selBoxVerts[0]));
buffer->Submit(GL_LINE_LOOP);
shader->Disable();
glAttribStatePtr->LineWidth(1.0f);
glAttribStatePtr->PopBits();
}
float3 CMouseHandler::GetWorldMapPos() const
{
const float3 cameraPos = camera->GetPos();
const float3 cursorVec = dir * globalRendering->viewRange * 1.4f;
const float dist = CGround::LineGroundCol(cameraPos, cameraPos + cursorVec, false);
if (dist < 0.0f)
return -OnesVector;
return ((cameraPos + dir * dist).cClampInBounds());
}
// CALLINFO:
// LuaUnsyncedRead::GetCurrentTooltip
// CTooltipConsole::Draw --> CMouseHandler::GetCurrentTooltip
std::string CMouseHandler::GetCurrentTooltip() const
{
if (!offscreen) {
std::string s;
if (luaInputReceiver->IsAbove(lastx, lasty)) {
s = std::move(luaInputReceiver->GetTooltip(lastx, lasty));
if (!s.empty())
return s;
}
for (CInputReceiver* recv: CInputReceiver::GetReceivers()) {
if (recv == nullptr)
continue;
if (!recv->IsAbove(lastx, lasty))
continue;
s = std::move(recv->GetTooltip(lastx, lasty));
if (!s.empty())
return s;
}
}
// outside game, neither guiHandler nor quadField exist and TraceRay would crash
if (guihandler == nullptr)
return "";
const std::string& buildTip = guihandler->GetBuildTooltip();
if (!buildTip.empty())
return buildTip;
const float range = globalRendering->viewRange * 1.4f;
float dist = 0.0f;
const CUnit* unit = nullptr;
const CFeature* feature = nullptr;
{
dist = TraceRay::GuiTraceRay(camera->GetPos(), dir, range, nullptr, unit, feature, true, false, true);
if (unit != nullptr) return CTooltipConsole::MakeUnitString(unit);
if (feature != nullptr) return CTooltipConsole::MakeFeatureString(feature);
}
const string selTip = std::move(selectedUnitsHandler.GetTooltip());
if (!selTip.empty())
return selTip;
if (dist <= range)
return CTooltipConsole::MakeGroundString(camera->GetPos() + (dir * dist));
return "";
}
void CMouseHandler::Update()
{
SetCursor(newCursor);
if (!hide)
return;
const int2 screenCenter = globalRendering->GetScreenCenter();
// Update MiddleClickScrolling
scrollx *= 0.5f;
scrolly *= 0.5f;
lastx = screenCenter.x;
lasty = screenCenter.y;
if (!globalRendering->active)
return;
mouseInput->SetPos(screenCenter);
}
void CMouseHandler::WarpMouse(int x, int y)
{
if (locked)
return;
lastx = x + globalRendering->viewPosX;
lasty = y + globalRendering->viewPosY;
mouseInput->SetWarpPos({lastx, lasty});
}
void CMouseHandler::ShowMouse()
{
if (!hide)
return;
hide = false;
cursorText = "none"; // force hardware cursor rebinding (else we have standard b&w cursor)
SDL_SetRelativeMouseMode(SDL_FALSE);
// don't use SDL_ShowCursor here since it would cause flickering with hwCursor
// (by switching between default cursor and later the real one, e.g. `attack`)
// instead update state and cursor at the same time
if (hardwareCursor) {
hwHide = true;
} else {
SDL_ShowCursor(SDL_DISABLE);
}
mouseInput->SetWarpPos(globalRendering->GetScreenCenter());
}
void CMouseHandler::HideMouse()
{
if (hide)
return;
hide = true;
hwHide = true;
SDL_ShowCursor(SDL_DISABLE);
// signal that we are only interested in relative motion events when MMB-scrolling
// this way the mouse position will never change so it is also unnecessary to call
// SDL_WarpMouseInWindow with associated warts (i.e. filtering of motion events by
// hand...); technically supercedes SDL_ShowCursor as well
SDL_SetRelativeMouseMode(SDL_TRUE);
const int2 screenCenter = globalRendering->GetScreenCenter();
scrollx = 0.0f;
scrolly = 0.0f;
lastx = screenCenter.x;
lasty = screenCenter.y;
mouseInput->SetWMMouseCursor(nullptr);
mouseInput->SetPos(screenCenter);
}
void CMouseHandler::ToggleMiddleClickScroll()
{
if (locked) {
locked = false;
ShowMouse();
} else {
locked = true;
HideMouse();
}
}
void CMouseHandler::ToggleHwCursor(const bool& enable)
{
hardwareCursor = enable;
if (hardwareCursor) {
hwHide = true;
} else {
mouseInput->SetWMMouseCursor(nullptr);
SDL_ShowCursor(SDL_DISABLE);
}
cursorText = "none";
}
/******************************************************************************/
void CMouseHandler::ChangeCursor(const std::string& cmdName, const float scale)
{
newCursor = cmdName;
cursorScale = scale;
}
void CMouseHandler::SetCursor(const std::string& cmdName, const bool forceRebind)
{
if ((cursorText == cmdName) && !forceRebind)
return;
cursorText = cmdName;
const auto it = cursorCommandMap.find(cmdName);
if (it != cursorCommandMap.end()) {
activeCursorIdx = it->second;
} else {
activeCursorIdx = cursorCommandMap[""];
}
if (!hardwareCursor || hide)
return;
if (loadedCursors[activeCursorIdx].IsHWValid()) {
hwHide = false;
loadedCursors[activeCursorIdx].BindHwCursor(); // calls SDL_ShowCursor(SDL_ENABLE);
} else {
hwHide = true;
SDL_ShowCursor(SDL_DISABLE);
mouseInput->SetWMMouseCursor(nullptr);
}
}
void CMouseHandler::UpdateCursors()
{
// we update all cursors (for the command queue icons)
for (auto it = cursorFileMap.begin(); it != cursorFileMap.end(); ++it) {
loadedCursors[it->second].Update();
}
}
void CMouseHandler::DrawScrollCursor(GL::RenderDataBufferC* buffer)
{
const float scaleL = math::fabs(std::min(0.0f, scrollx)) * crossMoveScale + 1.0f;
const float scaleT = math::fabs(std::min(0.0f, scrolly)) * crossMoveScale + 1.0f;
const float scaleR = math::fabs(std::max(0.0f, scrollx)) * crossMoveScale + 1.0f;
const float scaleB = math::fabs(std::max(0.0f, scrolly)) * crossMoveScale + 1.0f;
buffer->SafeAppend({{ 0.00f * scaleT, 1.00f * scaleT, 0.0f}, {1.0f, 1.0f, 1.0f, crossAlpha}});
buffer->SafeAppend({{ 0.33f * scaleT, 0.66f * scaleT, 0.0f}, {1.0f, 1.0f, 1.0f, crossAlpha}});
buffer->SafeAppend({{-0.33f * scaleT, 0.66f * scaleT, 0.0f}, {1.0f, 1.0f, 1.0f, crossAlpha}});
buffer->SafeAppend({{ 0.00f * scaleB, -1.00f * scaleB, 0.0f}, {1.0f, 1.0f, 1.0f, crossAlpha}});
buffer->SafeAppend({{ 0.33f * scaleB, -0.66f * scaleB, 0.0f}, {1.0f, 1.0f, 1.0f, crossAlpha}});
buffer->SafeAppend({{-0.33f * scaleB, -0.66f * scaleB, 0.0f}, {1.0f, 1.0f, 1.0f, crossAlpha}});
buffer->SafeAppend({{-1.00f * scaleL, 0.00f * scaleL, 0.0f}, {1.0f, 1.0f, 1.0f, crossAlpha}});
buffer->SafeAppend({{-0.66f * scaleL, 0.33f * scaleL, 0.0f}, {1.0f, 1.0f, 1.0f, crossAlpha}});
buffer->SafeAppend({{-0.66f * scaleL, -0.33f * scaleL, 0.0f}, {1.0f, 1.0f, 1.0f, crossAlpha}});
buffer->SafeAppend({{ 1.00f * scaleR, 0.00f * scaleR, 0.0f}, {1.0f, 1.0f, 1.0f, crossAlpha}});
buffer->SafeAppend({{ 0.66f * scaleR, 0.33f * scaleR, 0.0f}, {1.0f, 1.0f, 1.0f, crossAlpha}});
buffer->SafeAppend({{ 0.66f * scaleR, -0.33f * scaleR, 0.0f}, {1.0f, 1.0f, 1.0f, crossAlpha}});
buffer->SafeAppend({{-0.33f * scaleT, 0.66f * scaleT, 0.0f}, {1.0f, 1.0f, 1.0f, crossAlpha}});
buffer->SafeAppend({{ 0.33f * scaleT, 0.66f * scaleT, 0.0f}, {1.0f, 1.0f, 1.0f, crossAlpha}});
buffer->SafeAppend({{ 0.00f , 0.00f , 0.0f}, {0.2f, 0.2f, 0.2f, 0.0f}});
buffer->SafeAppend({{-0.33f * scaleB, -0.66f * scaleB, 0.0f}, {1.0f, 1.0f, 1.0f, crossAlpha}});
buffer->SafeAppend({{ 0.33f * scaleB, -0.66f * scaleB, 0.0f}, {1.0f, 1.0f, 1.0f, crossAlpha}});
buffer->SafeAppend({{ 0.00f , 0.00f, 0.0f}, {0.2f, 0.2f, 0.2f, 0.0f}});
buffer->SafeAppend({{-0.66f * scaleL, 0.33f * scaleL, 0.0f}, {1.0f, 1.0f, 1.0f, crossAlpha}});
buffer->SafeAppend({{-0.66f * scaleL, -0.33f * scaleL, 0.0f}, {1.0f, 1.0f, 1.0f, crossAlpha}});
buffer->SafeAppend({{ 0.00f , 0.00f , 0.0f}, {0.2f, 0.2f, 0.2f, 0.0f}});
buffer->SafeAppend({{ 0.66f * scaleR, -0.33f * scaleR, 0.0f}, {1.0f, 1.0f, 1.0f, crossAlpha}});
buffer->SafeAppend({{ 0.66f * scaleR, 0.33f * scaleR, 0.0f}, {1.0f, 1.0f, 1.0f, crossAlpha}});
buffer->SafeAppend({{ 0.00f , 0.00f , 0.0f}, {0.2f, 0.2f, 0.2f, 0.0f}});
// center dot
buffer->SafeAppend({{-crossSize * 0.03f, crossSize * 0.03f, 0.0f}, {1.0f, 1.0f, 1.0f, 1.2f * crossAlpha}});
buffer->SafeAppend({{-crossSize * 0.03f, -crossSize * 0.03f, 0.0f}, {1.0f, 1.0f, 1.0f, 1.2f * crossAlpha}});
buffer->SafeAppend({{ crossSize * 0.03f, -crossSize * 0.03f, 0.0f}, {1.0f, 1.0f, 1.0f, 1.2f * crossAlpha}});
buffer->SafeAppend({{ crossSize * 0.03f, -crossSize * 0.03f, 0.0f}, {1.0f, 1.0f, 1.0f, 1.2f * crossAlpha}});
buffer->SafeAppend({{ crossSize * 0.03f, crossSize * 0.03f, 0.0f}, {1.0f, 1.0f, 1.0f, 1.2f * crossAlpha}});
buffer->SafeAppend({{-crossSize * 0.03f, crossSize * 0.03f, 0.0f}, {1.0f, 1.0f, 1.0f, 1.2f * crossAlpha}});
buffer->Submit(GL_TRIANGLES);
}
void CMouseHandler::DrawFPSCursor(GL::RenderDataBufferC* buffer)
{
constexpr int stepNumHalf = 2;
const float wingHalf = math::PI * 0.111111f;
const float step = wingHalf / stepNumHalf;
for (float angle = 0.0f; angle < math::TWOPI; angle += (math::TWOPI * 0.333333f)) {
for (int i = -stepNumHalf; i < stepNumHalf; i++) {
buffer->SafeAppend({{0.1f * fastmath::sin(angle), 0.1f * fastmath::cos(angle ), 0.0f}, {1.0f, 1.0f, 1.0f, 0.5f}});
buffer->SafeAppend({{0.8f * fastmath::sin(angle + (i ) * step), 0.8f * fastmath::cos(angle + (i ) * step), 0.0f}, {1.0f, 1.0f, 1.0f, 0.5f}});
buffer->SafeAppend({{0.8f * fastmath::sin(angle + (i + 1) * step), 0.8f * fastmath::cos(angle + (i + 1) * step), 0.0f}, {1.0f, 1.0f, 1.0f, 0.5f}});
}
}
buffer->Submit(GL_TRIANGLES);
}
void CMouseHandler::DrawCursor()
{
assert(activeCursorIdx != -1);
if (guihandler != nullptr)
guihandler->DrawCentroidCursor();
if (locked) {
// draw procedural cursor if center-locked
if (crossSize > 0.0f) {
const float xscale = crossSize * globalRendering->pixelX;
const float yscale = crossSize * globalRendering->pixelY;
CMatrix44f cursorMat;
cursorMat.Translate(0.5f - globalRendering->pixelX * 0.5f, 0.5f - globalRendering->pixelY * 0.5f, 0.0f);
cursorMat.Scale({xscale, yscale, 1.0f});
GL::RenderDataBufferC* buffer = GL::GetRenderBufferC();
Shader::IProgramObject* shader = buffer->GetShader();
shader->Enable();
shader->SetUniformMatrix4x4<const char*, float>("u_movi_mat", false, cursorMat);
shader->SetUniformMatrix4x4<const char*, float>("u_proj_mat", false, CMatrix44f::ClipOrthoProj01(globalRendering->supportClipSpaceControl * 1.0f));
if (gu->fpsMode) {
DrawFPSCursor(buffer);
} else {
DrawScrollCursor(buffer);
}
shader->Disable();
}
return;
}
if (hide)
return;
if (hardwareCursor && loadedCursors[activeCursorIdx].IsHWValid())
return;
// draw the 'software' cursor
if (cursorScale >= 0.0f) {
loadedCursors[activeCursorIdx].Draw(lastx, lasty, cursorScale);
return;
}
// hovered minimap, show default cursor and draw `special` cursor scaled-down bottom right of the default one
const size_t normalCursorIndex = cursorFileMap["cursornormal"];
if (normalCursorIndex == 0) {
loadedCursors[activeCursorIdx].Draw(lastx, lasty, -cursorScale);
return;
}
CMouseCursor& normalCursor = loadedCursors[normalCursorIndex];
normalCursor.Draw(lastx, lasty, 1.0f);
if (activeCursorIdx == normalCursorIndex)
return;
loadedCursors[activeCursorIdx].Draw(lastx + normalCursor.GetMaxSizeX(), lasty + normalCursor.GetMaxSizeY(), -cursorScale);
}
bool CMouseHandler::AssignMouseCursor(
const std::string& cmdName,
const std::string& fileName,
CMouseCursor::HotSpot hotSpot,
bool overwrite
) {
const auto cmdIt = cursorCommandMap.find(cmdName);
const auto fileIt = cursorFileMap.find(fileName);
const bool haveCmd = (cmdIt != cursorCommandMap.end());
const bool haveFile = (fileIt != cursorFileMap.end());
if (haveCmd && !overwrite)
return false; // already assigned
if (haveFile) {
// cursor is already loaded, reuse it
cursorCommandMap[cmdName] = fileIt->second;
return true;
}
CMouseCursor newCursor = std::move(CMouseCursor::New(fileName, hotSpot));
if (!newCursor.IsValid())
return false;
const int commandCursorIdx = haveCmd? cmdIt->second: -1;
// assign the new cursor and remap indices
loadedCursors.emplace_back();
loadedCursors.back() = std::move(newCursor);
cursorFileMap[fileName] = loadedCursors.size() - 1;
cursorCommandMap[cmdName] = loadedCursors.size() - 1;
// switch to the null-cursor if our active one is being (re)assigned
if (activeCursorIdx == commandCursorIdx)
SetCursor("none", true);
return true;
}
bool CMouseHandler::ReplaceMouseCursor(
const string& oldName,
const string& newName,
CMouseCursor::HotSpot hotSpot
) {
const auto fileIt = cursorFileMap.find(oldName);
if (fileIt == cursorFileMap.end())
return false;
CMouseCursor newCursor = std::move(CMouseCursor::New(newName, hotSpot));
if (!newCursor.IsValid())
return false; // leave the old one
loadedCursors[fileIt->second] = std::move(newCursor);
// update current cursor if necessary
if (activeCursorIdx == fileIt->second)
SetCursor(cursorText, true);
return true;
}
/******************************************************************************/
void CMouseHandler::ConfigNotify(const std::string& key, const std::string& value)
{
dragScrollThreshold = configHandler->GetFloat("MouseDragScrollThreshold");
scrollWheelSpeed = configHandler->GetFloat("ScrollWheelSpeed");
}