Skip to content

Commit

Permalink
Map Tools - Code cleanup (#9750)
Browse files Browse the repository at this point in the history
* Maptools cleanup

* Changed conditions to check

* Minor cleanup + fix
  • Loading branch information
johnb432 authored Feb 3, 2024
1 parent c150d92 commit 6ba46ff
Show file tree
Hide file tree
Showing 17 changed files with 134 additions and 119 deletions.
1 change: 0 additions & 1 deletion addons/maptools/CfgEventHandlers.hpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

class Extended_PreStart_EventHandlers {
class ADDON {
init = QUOTE(call COMPILE_SCRIPT(XEH_preStart));
Expand Down
10 changes: 5 additions & 5 deletions addons/maptools/CfgVehicles.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ class CfgVehicles {
};
class ACE_PlottingBoard {
displayName = CSTRING(ShowPlottingBoard);
condition = QUOTE(GVAR(plottingBoard_Shown) < 1 && {call FUNC(canUsePlottingBoard)});
condition = QUOTE(GVAR(plottingBoard_Shown) == 0 && {call FUNC(canUsePlottingBoard)});
statement = QUOTE(GVAR(plottingBoard_Shown) = 1);
EXCEPTIONS;
showDisabled = 0;
Expand Down Expand Up @@ -127,7 +127,7 @@ class CfgVehicles {
};
class ACE_PlottingBoardAlign {
displayName = CSTRING(AlignTo);
condition = QUOTE(GVAR(plottingBoard_Shown) > 0);
condition = QUOTE(GVAR(plottingBoard_Shown) != 0);
statement = "";
EXCEPTIONS;
showDisabled = 0;
Expand All @@ -141,7 +141,7 @@ class CfgVehicles {

class ACE_PlottingBoardAlignBoardMaptool {
displayName = CSTRING(ToMapToolLabel);
condition = QUOTE(GVAR(mapTool_Shown) > 0 && GVAR(plottingBoard_angle) != GVAR(mapTool_angle));
condition = QUOTE(GVAR(mapTool_Shown) != 0 && GVAR(plottingBoard_angle) != GVAR(mapTool_angle));
statement = QUOTE(GVAR(plottingBoard_angle) = GVAR(mapTool_angle));
EXCEPTIONS;
showDisabled = 0;
Expand All @@ -163,7 +163,7 @@ class CfgVehicles {

class ACE_PlottingBoardAlignAcrylicMaptool {
displayName = CSTRING(ToMapToolLabel);
condition = QUOTE(GVAR(mapTool_Shown) > 0 && GVAR(plottingBoard_acrylicAngle) != GVAR(mapTool_angle));
condition = QUOTE(GVAR(mapTool_Shown) != 0 && GVAR(plottingBoard_acrylicAngle) != GVAR(mapTool_angle));
statement = QUOTE(GVAR(plottingBoard_acrylicAngle) = GVAR(mapTool_angle));
EXCEPTIONS;
showDisabled = 0;
Expand All @@ -185,7 +185,7 @@ class CfgVehicles {

class ACE_PlottingBoardAlignRulerMaptool {
displayName = CSTRING(ToMapToolLabel);
condition = QUOTE(GVAR(mapTool_Shown) > 0 && GVAR(plottingBoard_rulerAngle) != GVAR(mapTool_angle));
condition = QUOTE(GVAR(mapTool_Shown) != 0 && GVAR(plottingBoard_rulerAngle) != GVAR(mapTool_angle));
statement = QUOTE(GVAR(plottingBoard_rulerAngle) = GVAR(mapTool_angle));
EXCEPTIONS;
showDisabled = 0;
Expand Down
17 changes: 10 additions & 7 deletions addons/maptools/XEH_postInitClient.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,16 @@ GVAR(plottingBoard_isRotating) = -1;
GVAR(plottingBoard_moveToMouse) = true; // used to display it in center of screen when opened
GVAR(plottingBoard_markers) = createHashMap;

//Install the event handers for the map tools on the main in-game map
[{!isNull findDisplay 12},
{
((findDisplay 12) displayCtrl 51) ctrlAddEventHandler ["MouseMoving", {_this call FUNC(handleMouseMove);}];
((findDisplay 12) displayCtrl 51) ctrlAddEventHandler ["MouseButtonDown", {[1, _this] call FUNC(handleMouseButton);}];
((findDisplay 12) displayCtrl 51) ctrlAddEventHandler ["MouseButtonUp", {[0, _this] call FUNC(handleMouseButton)}];
((findDisplay 12) displayCtrl 51) ctrlAddEventHandler ["Draw", {call FUNC(updateMapToolMarkers); call FUNC(openMapGpsUpdate);}];
// Install the event handers for the map tools on the main in-game map
[{
!isNull findDisplay 12
}, {
private _map = (findDisplay 12) displayCtrl 51;

_map ctrlAddEventHandler ["MouseMoving", LINKFUNC(handleMouseMove)];
_map ctrlAddEventHandler ["MouseButtonDown", {[1, _this] call FUNC(handleMouseButton);}];
_map ctrlAddEventHandler ["MouseButtonUp", {[0, _this] call FUNC(handleMouseButton)}];
_map ctrlAddEventHandler ["Draw", {call FUNC(updateMapToolMarkers); call FUNC(openMapGpsUpdate);}];
}, []] call CBA_fnc_waitUntilAndExecute;

["visibleMap", {
Expand Down
11 changes: 6 additions & 5 deletions addons/maptools/functions/fnc_calculateMapScale.sqf
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
#include "..\script_component.hpp"
/*
* Author: esteldunedain
* Returns the equivalent of 100m in screen coordinates
* Returns the equivalent of 100m in screen coordinates.
*
* Arguments:
* None
*
* Return Value:
* None
* Map scale <NUMBER>
*
* Example:
* call ACE_maptools_fnc_calculateMapScale
* call ace_maptools_fnc_calculateMapScale
*
* Public: No
*/

private _pos = ((findDisplay 12) displayCtrl 51) ctrlMapScreenToWorld [0.5, 0.5];
private _screenOffset = ((findDisplay 12) displayCtrl 51) posWorldToScreen [(_pos select 0) + 100, (_pos select 1)];
private _mapCtrl = (findDisplay 12) displayCtrl 51;
private _pos = _mapCtrl ctrlMapScreenToWorld [0.5, 0.5];
private _screenOffset = _mapCtrl posWorldToScreen (_pos vectorAdd [100, 0]);

(_screenOffset select 0) - 0.5
6 changes: 3 additions & 3 deletions addons/maptools/functions/fnc_canUseMapGPS.sqf
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
#include "..\script_component.hpp"
/*
* Author: esteldunedain
* canUseMapGPS
* Returns if the GPS on the map can be used.
*
* Arguments:
* None
*
* Return Value:
* Boolean <BOOL>
* GPS can be used <BOOL>
*
* Example:
* call ACE_maptools_fnc_canUseMapGPS
* call ace_maptools_fnc_canUseMapGPS
*
* Public: No
*/
Expand Down
10 changes: 5 additions & 5 deletions addons/maptools/functions/fnc_canUseMapTools.sqf
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
#include "..\script_component.hpp"
/*
* Author: esteldunedain
* canUseMapTools
* Returns if the map tools can be used.
*
* Arguments:
* None
*
* Return Value:
* Boolean <BOOL>
* Map tools can be used <BOOL>
*
* Example:
* call ACE_maptools_fnc_canUseMapTools
* call ace_maptools_fnc_canUseMapTools
*
* Public: No
*/

visibleMap &&
{alive ACE_player} &&
{"ACE_MapTools" in (ACE_player call EFUNC(common,uniqueItems))} &&
{!GVAR(mapTool_isDragging)} &&
{!GVAR(mapTool_isRotating)} &&
{getUnitLoadout ACE_player param [9, []] param [0, ""] != ""}
{ACE_player getSlotItemName TYPE_MAP != ""} &&
{[ACE_player, "ACE_MapTools"] call EFUNC(common,hasItem)}
4 changes: 2 additions & 2 deletions addons/maptools/functions/fnc_canUsePlottingBoard.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@

visibleMap &&
{alive ACE_player} &&
{[ACE_player, "ACE_PlottingBoard"] call EFUNC(common,hasItem)} &&
{!GVAR(plottingBoard_isDragging)} &&
{GVAR(plottingBoard_isRotating) == -1}
{GVAR(plottingBoard_isRotating) == -1} &&
{[ACE_player, "ACE_PlottingBoard"] call EFUNC(common,hasItem)}
95 changes: 59 additions & 36 deletions addons/maptools/functions/fnc_drawLinesOnRoamer.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -4,141 +4,164 @@
* Prevents the cursor from entering the roamer when drawing lines and records the positions
*
* Arguments:
* 0: The Map <CONTROL>
* 0: Map control <CONTROL>
* 1: Roamer Width <NUMBER>
*
* Return Value:
* None
*
* Example:
* [map, 300] call ace_maptools_fnc_drawLinesOnRoamer
* [CONTROL, 300] call ace_maptools_fnc_drawLinesOnRoamer
*
* Public: No
*/

if (!GVAR(drawStraightLines)) exitWith {};

params ["_theMap", "_roamerWidth"];
params ["_mapCtrl", "_roamerWidth"];
GVAR(mapTool_pos) params ["_roamerPosX", "_roamerPosY"];

private _posCenter = [_roamerPosX, _roamerPosY, 0];

private _posTopRight = [
_roamerPosX + (cos GVAR(mapTool_angle)) * DIST_LEFT_TO_CENTER_PERC * _roamerWidth + (sin GVAR(mapTool_angle)) * DIST_TOP_TO_CENTER_PERC * _roamerWidth,
_roamerPosY + (-sin GVAR(mapTool_angle)) * DIST_LEFT_TO_CENTER_PERC * _roamerWidth + (cos GVAR(mapTool_angle)) * DIST_TOP_TO_CENTER_PERC * _roamerWidth,
0];
_roamerPosX + (cos GVAR(mapTool_angle)) * DIST_LEFT_TO_CENTER_PERC * _roamerWidth + (sin GVAR(mapTool_angle)) * DIST_TOP_TO_CENTER_PERC * _roamerWidth,
_roamerPosY + (-sin GVAR(mapTool_angle)) * DIST_LEFT_TO_CENTER_PERC * _roamerWidth + (cos GVAR(mapTool_angle)) * DIST_TOP_TO_CENTER_PERC * _roamerWidth,
0
];

private _posTopLeft = [
_roamerPosX + (-cos GVAR(mapTool_angle)) * DIST_LEFT_TO_CENTER_PERC * _roamerWidth + (sin GVAR(mapTool_angle)) * DIST_TOP_TO_CENTER_PERC * _roamerWidth,
_roamerPosY + (sin GVAR(mapTool_angle)) * DIST_LEFT_TO_CENTER_PERC * _roamerWidth + (cos GVAR(mapTool_angle)) * DIST_TOP_TO_CENTER_PERC * _roamerWidth,
0];
_roamerPosX + (-cos GVAR(mapTool_angle)) * DIST_LEFT_TO_CENTER_PERC * _roamerWidth + (sin GVAR(mapTool_angle)) * DIST_TOP_TO_CENTER_PERC * _roamerWidth,
_roamerPosY + (sin GVAR(mapTool_angle)) * DIST_LEFT_TO_CENTER_PERC * _roamerWidth + (cos GVAR(mapTool_angle)) * DIST_TOP_TO_CENTER_PERC * _roamerWidth,
0
];

private _posBottomLeft = [
_roamerPosX + (-cos GVAR(mapTool_angle)) * DIST_LEFT_TO_CENTER_PERC * _roamerWidth + (sin GVAR(mapTool_angle)) * DIST_BOTTOM_TO_CENTER_PERC * _roamerWidth,
_roamerPosY + (sin GVAR(mapTool_angle)) * DIST_LEFT_TO_CENTER_PERC * _roamerWidth + (cos GVAR(mapTool_angle)) * DIST_BOTTOM_TO_CENTER_PERC * _roamerWidth,
0];
_roamerPosX + (-cos GVAR(mapTool_angle)) * DIST_LEFT_TO_CENTER_PERC * _roamerWidth + (sin GVAR(mapTool_angle)) * DIST_BOTTOM_TO_CENTER_PERC * _roamerWidth,
_roamerPosY + (sin GVAR(mapTool_angle)) * DIST_LEFT_TO_CENTER_PERC * _roamerWidth + (cos GVAR(mapTool_angle)) * DIST_BOTTOM_TO_CENTER_PERC * _roamerWidth,
0
];

private _posBottomRight = [
_roamerPosX + (cos GVAR(mapTool_angle)) * DIST_LEFT_TO_CENTER_PERC * _roamerWidth + (sin GVAR(mapTool_angle)) * DIST_BOTTOM_TO_CENTER_PERC * _roamerWidth,
_roamerPosY + (-sin GVAR(mapTool_angle)) * DIST_LEFT_TO_CENTER_PERC * _roamerWidth + (cos GVAR(mapTool_angle)) * DIST_BOTTOM_TO_CENTER_PERC * _roamerWidth,
0];
_roamerPosX + (cos GVAR(mapTool_angle)) * DIST_LEFT_TO_CENTER_PERC * _roamerWidth + (sin GVAR(mapTool_angle)) * DIST_BOTTOM_TO_CENTER_PERC * _roamerWidth,
_roamerPosY + (-sin GVAR(mapTool_angle)) * DIST_LEFT_TO_CENTER_PERC * _roamerWidth + (cos GVAR(mapTool_angle)) * DIST_BOTTOM_TO_CENTER_PERC * _roamerWidth,
0
];

private _fnc_Distance = { // Get distance point _p is from a line made from _a to _b (uses 3d array commands, but z should be 0)
private _fnc_distance = { // Get distance point _p is from a line made from _a to _b (uses 3d array commands, but z should be 0)
// Ref: https://en.wikipedia.org/wiki/Distance_from_a_point_to_a_line#Vector_formulation
params ["_a", "_b", "_p"];

private _n = _b vectorDiff _a;
private _pa = _a vectorDiff _p;
private _c = _n vectorMultiply ((_pa vectorDotProduct _n) / (_n vectorDotProduct _n));
private _d = _pa vectorDiff _c;
sqrt (_d vectorDotProduct _d);

sqrt (_d vectorDotProduct _d)
};

private _currentMousePos = _theMap ctrlMapScreenToWorld getMousePosition;
private _currentMousePos = _mapCtrl ctrlMapScreenToWorld getMousePosition;
_currentMousePos set [2, 0];

// Break the roamer rectangle into 4 triangle, one for each side
switch (true) do {
case (_currentMousePos inPolygon [_posCenter, _posTopLeft, _posBottomLeft]): { // Left
private _distanceToRoamerLine = ([_posTopLeft, _posBottomLeft, _currentMousePos] call _fnc_Distance);
_currentMousePos = _currentMousePos vectorAdd ([_distanceToRoamerLine, (GVAR(mapTool_angle) - 90) ,0] call CBA_fnc_polar2vect);
private _distanceToRoamerLine = [_posTopLeft, _posBottomLeft, _currentMousePos] call _fnc_distance;

_currentMousePos = _currentMousePos vectorAdd ([_distanceToRoamerLine, GVAR(mapTool_angle) - 90, 0] call CBA_fnc_polar2vect);

if (GVAR(freeDrawingData) isEqualTo []) then { // We start drawing on the line
GVAR(freeDrawingData) = ["left", _currentMousePos, _currentMousePos];
} else {
if ((GVAR(freeDrawingData) select 0) == "left") then { // We are already drawing on this line, find best spot
if ((_currentMousePos distance2d _posTopLeft) < ((GVAR(freeDrawingData) select 1) distance2d _posTopLeft)) then {
GVAR(freeDrawingData) set [1, _currentMousePos];
};

if ((_currentMousePos distance2d _posBottomLeft) < ((GVAR(freeDrawingData) select 2) distance2d _posBottomLeft)) then {
GVAR(freeDrawingData) set [2, _currentMousePos];
};
};
};
private _screenPosOfCorrectedPos = _theMap ctrlMapWorldToScreen _currentMousePos;

private _screenPosOfCorrectedPos = _mapCtrl ctrlMapWorldToScreen _currentMousePos;
setMousePosition _screenPosOfCorrectedPos;
};
case (_currentMousePos inPolygon [_posCenter, _posTopLeft, _posTopRight]): { // Top
private _distanceToRoamerLine = ([_posTopLeft, _posTopRight, _currentMousePos] call _fnc_Distance);
_currentMousePos = _currentMousePos vectorAdd ([_distanceToRoamerLine, (GVAR(mapTool_angle) + 0) ,0] call CBA_fnc_polar2vect);
private _distanceToRoamerLine = [_posTopLeft, _posTopRight, _currentMousePos] call _fnc_distance;

_currentMousePos = _currentMousePos vectorAdd ([_distanceToRoamerLine, GVAR(mapTool_angle), 0] call CBA_fnc_polar2vect);

if (GVAR(freeDrawingData) isEqualTo []) then { // We start drawing on the line
GVAR(freeDrawingData) = ["top", _currentMousePos, _currentMousePos];
} else {
if ((GVAR(freeDrawingData) select 0) == "top") then { // We are already drawing on this line, find best spot
if ((_currentMousePos distance2d _posTopLeft) < ((GVAR(freeDrawingData) select 1) distance2d _posTopLeft)) then {
GVAR(freeDrawingData) set [1, _currentMousePos];
};

if ((_currentMousePos distance2d _posTopRight) < ((GVAR(freeDrawingData) select 2) distance2d _posTopRight)) then {
GVAR(freeDrawingData) set [2, _currentMousePos];
};
};
};
private _screenPosOfCorrectedPos = _theMap ctrlMapWorldToScreen _currentMousePos;

private _screenPosOfCorrectedPos = _mapCtrl ctrlMapWorldToScreen _currentMousePos;
setMousePosition _screenPosOfCorrectedPos;
};
case (_currentMousePos inPolygon [_posCenter, _posTopRight, _posBottomRight]): { // Right
private _distanceToRoamerLine = ([_posTopRight, _posBottomRight, _currentMousePos] call _fnc_Distance);
_currentMousePos = _currentMousePos vectorAdd ([_distanceToRoamerLine, (GVAR(mapTool_angle) + 90) ,0] call CBA_fnc_polar2vect);
private _distanceToRoamerLine = [_posTopRight, _posBottomRight, _currentMousePos] call _fnc_distance;

_currentMousePos = _currentMousePos vectorAdd ([_distanceToRoamerLine, GVAR(mapTool_angle) + 90, 0] call CBA_fnc_polar2vect);

if (GVAR(freeDrawingData) isEqualTo []) then { // We start drawing on the line
GVAR(freeDrawingData) = ["right", _currentMousePos, _currentMousePos];
} else {
if ((GVAR(freeDrawingData) select 0) == "right") then { // We are already drawing on this line, find best spot
if ((_currentMousePos distance2d _posTopRight) < ((GVAR(freeDrawingData) select 1) distance2d _posTopRight)) then {
GVAR(freeDrawingData) set [1, _currentMousePos];
};

if ((_currentMousePos distance2d _posBottomRight) < ((GVAR(freeDrawingData) select 2) distance2d _posBottomRight)) then {
GVAR(freeDrawingData) set [2, _currentMousePos];
};
};
};
private _screenPosOfCorrectedPos = _theMap ctrlMapWorldToScreen _currentMousePos;

private _screenPosOfCorrectedPos = _mapCtrl ctrlMapWorldToScreen _currentMousePos;
setMousePosition _screenPosOfCorrectedPos;
};
case (_currentMousePos inPolygon [_posCenter, _posBottomLeft, _posBottomRight]): { // Bottom
private _distanceToRoamerLine = ([_posBottomLeft, _posBottomRight, _currentMousePos] call _fnc_Distance);
_currentMousePos = _currentMousePos vectorAdd ([_distanceToRoamerLine, (GVAR(mapTool_angle) + 180) ,0] call CBA_fnc_polar2vect);
private _distanceToRoamerLine = [_posBottomLeft, _posBottomRight, _currentMousePos] call _fnc_distance;

_currentMousePos = _currentMousePos vectorAdd ([_distanceToRoamerLine, GVAR(mapTool_angle) + 180, 0] call CBA_fnc_polar2vect);

if (GVAR(freeDrawingData) isEqualTo []) then { // We start drawing on the line
GVAR(freeDrawingData) = ["bottom", _currentMousePos, _currentMousePos];
} else {
if ((GVAR(freeDrawingData) select 0) == "bottom") then { // We are already drawing on this line, find best spot
if ((_currentMousePos distance2d _posBottomLeft) < ((GVAR(freeDrawingData) select 1) distance2d _posBottomLeft)) then {
GVAR(freeDrawingData) set [1, _currentMousePos];
};

if ((_currentMousePos distance2d _posBottomRight) < ((GVAR(freeDrawingData) select 2) distance2d _posBottomRight)) then {
GVAR(freeDrawingData) set [2, _currentMousePos];
};
};
};
private _screenPosOfCorrectedPos = _theMap ctrlMapWorldToScreen _currentMousePos;

private _screenPosOfCorrectedPos = _mapCtrl ctrlMapWorldToScreen _currentMousePos;
setMousePosition _screenPosOfCorrectedPos;
};
};

#ifdef DEBUG_MODE_FULL
_theMap drawIcon ['iconStaticMG',[1,0,0,1],_posTopRight,24,24,getDir player,'1,1',1,0.03,'TahomaB','right'];
_theMap drawIcon ['iconStaticMG',[1,0,0,1],_posTopLeft,24,24,getDir player,'-1,1',1,0.03,'TahomaB','right'];
_theMap drawIcon ['iconStaticMG',[1,0,0,1],_posBottomLeft,24,24,getDir player,'-1,-1',1,0.03,'TahomaB','right'];
_theMap drawIcon ['iconStaticMG',[1,0,0,1],_posBottomRight,24,24,getDir player,'1,-1',1,0.03,'TahomaB','right'];
_mapCtrl drawIcon ['iconStaticMG',[1,0,0,1],_posTopRight,24,24,getDir player,'1,1',1,0.03,'TahomaB','right'];
_mapCtrl drawIcon ['iconStaticMG',[1,0,0,1],_posTopLeft,24,24,getDir player,'-1,1',1,0.03,'TahomaB','right'];
_mapCtrl drawIcon ['iconStaticMG',[1,0,0,1],_posBottomLeft,24,24,getDir player,'-1,-1',1,0.03,'TahomaB','right'];
_mapCtrl drawIcon ['iconStaticMG',[1,0,0,1],_posBottomRight,24,24,getDir player,'1,-1',1,0.03,'TahomaB','right'];

if (GVAR(freeDrawingData) isNotEqualTo []) then {
_theMap drawIcon ['iconStaticMG',[0,0,1,1],GVAR(freeDrawingData) select 1,24,24,getDir player,'1,-1',1,0.03,'TahomaB','right'];
_theMap drawIcon ['iconStaticMG',[0,0,1,1],GVAR(freeDrawingData) select 2,24,24,getDir player,'1,-1',1,0.03,'TahomaB','right'];
_mapCtrl drawIcon ['iconStaticMG',[0,0,1,1],GVAR(freeDrawingData) select 1,24,24,getDir player,'1,-1',1,0.03,'TahomaB','right'];
_mapCtrl drawIcon ['iconStaticMG',[0,0,1,1],GVAR(freeDrawingData) select 2,24,24,getDir player,'1,-1',1,0.03,'TahomaB','right'];
};
#endif
Loading

0 comments on commit 6ba46ff

Please sign in to comment.