CompassProMiniMap.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697
  1. using System;
  2. using UnityEngine;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. namespace CompassNavigatorPro {
  6. public enum MINIMAP_STYLE {
  7. TornPaper = 0,
  8. SolidBox = 1,
  9. SolidCircle = 2,
  10. Custom = 100
  11. }
  12. public enum MINIMAP_LOCATION {
  13. TopLeft,
  14. TopCenter,
  15. TopRight,
  16. MiddleLeft,
  17. MiddleCenter,
  18. MiddleRight,
  19. BottomLeft,
  20. BottomCenter,
  21. BottomRight,
  22. Custom
  23. }
  24. public enum MINIMAP_CAMERA_MODE {
  25. Perspective = 0,
  26. Orthographic = 1
  27. }
  28. public enum MINIMAP_CAMERA_SNAPSHOT_FREQUENCY {
  29. Continuous = 0,
  30. TimeInterval = 1,
  31. DistanceTravelled = 2
  32. }
  33. public partial class CompassPro : MonoBehaviour {
  34. #region Events
  35. /// <summary>
  36. /// Event fired when this POI appears in the Mini-Map.
  37. /// </summary>
  38. public Action<CompassProPOI> OnPOIVisibleInMiniMap;
  39. /// <summary>
  40. /// Event fired when the POI disappears from the Mini-Map
  41. /// </summary>
  42. public Action<CompassProPOI> OnPOIHidesInMiniMap;
  43. #endregion
  44. #region Public MiniMap properties
  45. [SerializeField]
  46. bool _showMiniMap = false;
  47. /// <summary>
  48. /// Show/Hide minimap
  49. /// </summary>
  50. public bool showMiniMap {
  51. get { return _showMiniMap; }
  52. set {
  53. if (value != _showMiniMap) {
  54. _showMiniMap = value;
  55. SetupMiniMap ();
  56. isDirty = true;
  57. }
  58. }
  59. }
  60. [SerializeField]
  61. MINIMAP_LOCATION _miniMapLocation = MINIMAP_LOCATION.BottomRight;
  62. /// <summary>
  63. /// Minimap screen location
  64. /// </summary>
  65. public MINIMAP_LOCATION miniMapLocation {
  66. get { return _miniMapLocation; }
  67. set {
  68. if (value != _miniMapLocation) {
  69. _miniMapLocation = value;
  70. SetupMiniMap ();
  71. isDirty = true;
  72. }
  73. }
  74. }
  75. [SerializeField]
  76. Vector2 _miniMapLocationOffset;
  77. /// <summary>
  78. /// Minimap screen location offset
  79. /// </summary>
  80. public Vector2 miniMapLocationOffset {
  81. get { return _miniMapLocationOffset; }
  82. set {
  83. if (value != _miniMapLocationOffset) {
  84. _miniMapLocationOffset = value;
  85. SetupMiniMap ();
  86. isDirty = true;
  87. }
  88. }
  89. }
  90. [SerializeField]
  91. bool _miniMapKeepStraight = false;
  92. /// <summary>
  93. /// Keep the mini-map oriented to North
  94. /// </summary>
  95. public bool miniMapKeepStraight {
  96. get { return _miniMapKeepStraight; }
  97. set {
  98. if (value != _miniMapKeepStraight) {
  99. _miniMapKeepStraight = value;
  100. needMiniMapShot = true;
  101. needUpdateBarContents = true;
  102. isDirty = true;
  103. }
  104. }
  105. }
  106. [SerializeField]
  107. float _miniMapSize = 0.2f;
  108. /// <summary>
  109. /// The screen size of the mini-map
  110. /// </summary>
  111. public float miniMapSize {
  112. get { return _miniMapSize; }
  113. set {
  114. if (value != _miniMapSize) {
  115. _miniMapSize = value;
  116. SetupMiniMap ();
  117. isDirty = true;
  118. }
  119. }
  120. }
  121. /// <summary>
  122. /// Where to center the mini map
  123. /// </summary>
  124. public Transform miniMapFollow;
  125. /// <summary>
  126. /// Optional mini-map mask texture
  127. /// </summary>
  128. [SerializeField]
  129. Sprite _miniMapMaskSprite;
  130. /// <summary>
  131. /// The sprite for the mini-map mask
  132. /// </summary>
  133. public Sprite miniMapMaskSprite {
  134. get { return _miniMapMaskSprite; }
  135. set {
  136. if (value != _miniMapMaskSprite) {
  137. _miniMapMaskSprite = value;
  138. SetupMiniMap ();
  139. isDirty = true;
  140. }
  141. }
  142. }
  143. /// <summary>
  144. /// Optional mini-map border texture
  145. /// </summary>
  146. [SerializeField]
  147. Texture2D _miniMapBorderTexture;
  148. /// <summary>
  149. /// Show/Hide minimap
  150. /// </summary>
  151. public Texture2D miniMapBorderTexture {
  152. get { return _miniMapBorderTexture; }
  153. set {
  154. if (value != _miniMapBorderTexture) {
  155. _miniMapBorderTexture = value;
  156. SetupMiniMap ();
  157. isDirty = true;
  158. }
  159. }
  160. }
  161. /// <summary>
  162. /// Style for the mini-map
  163. /// </summary>
  164. [SerializeField]
  165. MINIMAP_STYLE _miniMapStyle = MINIMAP_STYLE.TornPaper;
  166. /// <summary>
  167. /// Style of mini-map
  168. /// </summary>
  169. public MINIMAP_STYLE miniMapStyle {
  170. get { return _miniMapStyle; }
  171. set {
  172. if (value != _miniMapStyle) {
  173. _miniMapStyle = value;
  174. SetupMiniMap ();
  175. isDirty = true;
  176. }
  177. }
  178. }
  179. [SerializeField]
  180. int _miniMapResolutionNormalSize = 256;
  181. /// <summary>
  182. /// The capture resolution when minimap is in full-screen mode.
  183. /// </summary>
  184. public int miniMapResolutionNormalSize {
  185. get { return _miniMapResolutionNormalSize; }
  186. set {
  187. if (value != _miniMapResolutionNormalSize) {
  188. _miniMapResolutionNormalSize = value;
  189. SetupMiniMap ();
  190. isDirty = true;
  191. }
  192. }
  193. }
  194. [SerializeField]
  195. float _miniMapFullScreenSize = 0.9f;
  196. /// <summary>
  197. /// The percentage of screen size when minimap is in full screen mode.
  198. /// </summary>
  199. public float miniMapFullScreenSize {
  200. get { return _miniMapFullScreenSize; }
  201. set {
  202. if (value != _miniMapFullScreenSize) {
  203. _miniMapFullScreenSize = value;
  204. SetupMiniMap ();
  205. isDirty = true;
  206. }
  207. }
  208. }
  209. [SerializeField]
  210. bool _miniMapKeepAspectRatio = true;
  211. /// <summary>
  212. /// Keep aspect ration in full screen mode
  213. /// </summary>
  214. public bool miniMapKeepAspectRatio {
  215. get { return _miniMapKeepAspectRatio; }
  216. set {
  217. if (value != _miniMapKeepAspectRatio) {
  218. _miniMapKeepAspectRatio = value;
  219. SetupMiniMap ();
  220. isDirty = true;
  221. }
  222. }
  223. }
  224. [SerializeField]
  225. bool _miniMapDisableMainCameraInFullScreen = true;
  226. /// <summary>
  227. /// Keep aspect ration in full screen mode
  228. /// </summary>
  229. public bool miniMapDisableMainCameraInFullScreen {
  230. get { return _miniMapDisableMainCameraInFullScreen; }
  231. set {
  232. if (value != _miniMapDisableMainCameraInFullScreen) {
  233. _miniMapDisableMainCameraInFullScreen = value;
  234. SetupMiniMap ();
  235. isDirty = true;
  236. }
  237. }
  238. }
  239. [SerializeField] MINIMAP_CAMERA_MODE _miniMapCameraMode = MINIMAP_CAMERA_MODE.Orthographic;
  240. /// <summary>
  241. /// Mini-map projection mode
  242. /// </summary>
  243. public MINIMAP_CAMERA_MODE miniMapCameraMode {
  244. get { return _miniMapCameraMode; }
  245. set {
  246. if (value != _miniMapCameraMode) {
  247. _miniMapCameraMode = value;
  248. SetupMiniMap ();
  249. isDirty = true;
  250. }
  251. }
  252. }
  253. [SerializeField] MINIMAP_CAMERA_SNAPSHOT_FREQUENCY _miniMapCameraSnapshotFrequency = MINIMAP_CAMERA_SNAPSHOT_FREQUENCY.DistanceTravelled;
  254. /// <summary>
  255. /// How often the mini-map camera will capture the scene
  256. /// </summary>
  257. public MINIMAP_CAMERA_SNAPSHOT_FREQUENCY miniMapCameraSnapshotFrequency {
  258. get { return _miniMapCameraSnapshotFrequency; }
  259. set {
  260. if (value != _miniMapCameraSnapshotFrequency) {
  261. _miniMapCameraSnapshotFrequency = value;
  262. SetupMiniMap ();
  263. isDirty = true;
  264. }
  265. }
  266. }
  267. [SerializeField]
  268. float _miniMapCaptureSize = 256f;
  269. /// <summary>
  270. /// The orthographic camera size
  271. /// </summary>
  272. public float miniMapCaptureSize {
  273. get { return _miniMapCaptureSize; }
  274. set {
  275. if (value != _miniMapCaptureSize) {
  276. _miniMapCaptureSize = value;
  277. needUpdateBarContents = true;
  278. needMiniMapShot = true;
  279. isDirty = true;
  280. }
  281. }
  282. }
  283. [SerializeField]
  284. float _miniMapSnapshotInterval = 10f;
  285. /// <summary>
  286. /// The time interval between minimap camera shots
  287. /// </summary>
  288. public float miniMapSnapshotInterval {
  289. get { return _miniMapSnapshotInterval; }
  290. set {
  291. if (value != _miniMapSnapshotInterval) {
  292. _miniMapSnapshotInterval = value;
  293. isDirty = true;
  294. }
  295. }
  296. }
  297. [SerializeField]
  298. float _miniMapSnapshotDistance = 10f;
  299. /// <summary>
  300. /// The distance interval between minimap camera shots
  301. /// </summary>
  302. public float miniMapSnapshotDistance {
  303. get { return _miniMapSnapshotDistance; }
  304. set {
  305. if (value != _miniMapSnapshotDistance) {
  306. _miniMapSnapshotDistance = value;
  307. isDirty = true;
  308. }
  309. }
  310. }
  311. [SerializeField]
  312. float _miniMapContrast = 1.02f;
  313. /// <summary>
  314. /// Contrast of the mini-map image
  315. /// </summary>
  316. public float miniMapContrast {
  317. get { return _miniMapContrast; }
  318. set {
  319. if (value != _miniMapContrast) {
  320. _miniMapContrast = value;
  321. miniMapMaterialRefresh = true;
  322. isDirty = true;
  323. }
  324. }
  325. }
  326. [SerializeField]
  327. float _miniMapBrightness = 1.05f;
  328. /// <summary>
  329. /// Brightness of the mini-map image
  330. /// </summary>
  331. public float miniMapBrightness {
  332. get { return _miniMapBrightness; }
  333. set {
  334. if (value != _miniMapBrightness) {
  335. _miniMapBrightness = value;
  336. miniMapMaterialRefresh = true;
  337. isDirty = true;
  338. }
  339. }
  340. }
  341. [SerializeField]
  342. bool _miniMapEnableShadows = false;
  343. /// <summary>
  344. /// Enables/disables shadow casting when rendering mini-map
  345. /// </summary>
  346. public bool miniMapEnableShadows {
  347. get { return _miniMapEnableShadows; }
  348. set {
  349. if (value != _miniMapEnableShadows) {
  350. _miniMapEnableShadows = value;
  351. isDirty = true;
  352. }
  353. }
  354. }
  355. [SerializeField, Range (0, 1)]
  356. float _miniMapZoomMin = 0.01f;
  357. /// <summary>
  358. /// The orthographic minimum size for the camera
  359. /// </summary>
  360. public float miniMapZoomMin {
  361. get { return _miniMapZoomMin; }
  362. set {
  363. if (value != _miniMapZoomMin) {
  364. _miniMapZoomMin = value;
  365. needUpdateBarContents = true;
  366. needMiniMapShot = true;
  367. isDirty = true;
  368. }
  369. }
  370. }
  371. [SerializeField, Range (0, 1)]
  372. float _miniMapZoomMax = 1f;
  373. /// <summary>
  374. /// The orthographic maximum size for the camera
  375. /// </summary>
  376. public float miniMapZoomMax {
  377. get { return _miniMapZoomMax; }
  378. set {
  379. if (value != _miniMapZoomMax) {
  380. _miniMapZoomMax = value;
  381. needUpdateBarContents = true;
  382. needMiniMapShot = true;
  383. isDirty = true;
  384. }
  385. }
  386. }
  387. [SerializeField, Range (0, 1f)]
  388. float _miniMapZoomLevel = 0.5f;
  389. /// <summary>
  390. /// The current mini-map zoom based on the min/max size (orthographic mode) or altitude (perspective mode)
  391. /// </summary>
  392. public float miniMapZoomLevel {
  393. get { return _miniMapZoomLevel; }
  394. set {
  395. if (value != _miniMapZoomLevel) {
  396. _miniMapZoomLevel = Mathf.Clamp01 (value);
  397. needMiniMapShot = true;
  398. needUpdateBarContents = true;
  399. isDirty = true;
  400. }
  401. }
  402. }
  403. [SerializeField]
  404. float _miniMapCameraMinAltitude = 10;
  405. /// <summary>
  406. /// The min distance from the camera to the following target
  407. /// </summary>
  408. public float miniMapCameraMinAltitude {
  409. get { return _miniMapCameraMinAltitude; }
  410. set {
  411. if (value != _miniMapCameraMinAltitude) {
  412. _miniMapCameraMinAltitude = value;
  413. needUpdateBarContents = true;
  414. needMiniMapShot = true;
  415. isDirty = true;
  416. }
  417. }
  418. }
  419. [SerializeField]
  420. float _miniMapCameraMaxAltitude = 100f;
  421. /// <summary>
  422. /// The max distance from the camera to the following target
  423. /// </summary>
  424. public float miniMapCameraMaxAltitude {
  425. get { return _miniMapCameraMaxAltitude; }
  426. set {
  427. if (value != _miniMapCameraMaxAltitude) {
  428. _miniMapCameraMaxAltitude = value;
  429. needUpdateBarContents = true;
  430. needMiniMapShot = true;
  431. isDirty = true;
  432. }
  433. }
  434. }
  435. [SerializeField]
  436. float _miniMapCameraHeightVSFollow = 200f;
  437. /// <summary>
  438. /// When mini-map is in orthographic projection, an optional height for the camera with respect to the main camera or followed item
  439. /// </summary>
  440. public float miniMapCameraHeightVSFollow {
  441. get { return _miniMapCameraHeightVSFollow; }
  442. set {
  443. if (value != _miniMapCameraHeightVSFollow) {
  444. _miniMapCameraHeightVSFollow = value;
  445. needUpdateBarContents = true;
  446. needMiniMapShot = true;
  447. isDirty = true;
  448. }
  449. }
  450. }
  451. [SerializeField] int _miniMapLayerMask = -1;
  452. /// <summary>
  453. /// The layer mask for the mini-map camera
  454. /// </summary>
  455. public int miniMapLayerMask {
  456. get { return _miniMapLayerMask; }
  457. set {
  458. if (value != _miniMapLayerMask) {
  459. _miniMapLayerMask = value;
  460. SetupMiniMap ();
  461. isDirty = true;
  462. }
  463. }
  464. }
  465. [SerializeField]
  466. float _miniMapIconSize = 0.5f;
  467. /// <summary>
  468. /// The size for the icons on the mini-map
  469. /// </summary>
  470. public float miniMapIconSize {
  471. get { return _miniMapIconSize; }
  472. set {
  473. if (value != _miniMapIconSize) {
  474. _miniMapIconSize = value;
  475. isDirty = true;
  476. }
  477. }
  478. }
  479. [SerializeField]
  480. float _miniMapClampBorder = 0.02f;
  481. /// <summary>
  482. /// The distance to the edge for the clamped icons on the minimap
  483. /// </summary>
  484. public float miniMapClampBorder {
  485. get { return _miniMapClampBorder; }
  486. set {
  487. if (value != _miniMapClampBorder) {
  488. _miniMapClampBorder = value;
  489. needUpdateBarContents = true;
  490. isDirty = true;
  491. }
  492. }
  493. }
  494. [SerializeField]
  495. float _miniMapAlpha = 1.0f;
  496. /// <summary>
  497. /// The alpha (transparency) of the mini-map.
  498. /// </summary>
  499. public float miniMapAlpha {
  500. get { return _miniMapAlpha; }
  501. set {
  502. if (value != _miniMapAlpha) {
  503. _miniMapAlpha = value;
  504. isDirty = true;
  505. }
  506. }
  507. }
  508. [SerializeField]
  509. bool _miniMapShowButtons;
  510. public bool miniMapShowButtons {
  511. get { return _miniMapShowButtons; }
  512. set {
  513. if (value != _miniMapShowButtons) {
  514. _miniMapShowButtons = value;
  515. isDirty = true;
  516. SetupMiniMap ();
  517. }
  518. }
  519. }
  520. [SerializeField]
  521. bool _miniMapIconEvents;
  522. public bool miniMapIconEvents {
  523. get { return _miniMapIconEvents; }
  524. set {
  525. if (_miniMapIconEvents != value) {
  526. _miniMapIconEvents = value;
  527. }
  528. }
  529. }
  530. public void MiniMapZoomIn (float speed = 1f) {
  531. miniMapZoomLevel += Time.deltaTime * speed;
  532. }
  533. public void MiniMapZoomOut (float speed = 1f) {
  534. miniMapZoomLevel -= Time.deltaTime * speed;
  535. }
  536. bool _miniMapZoomState;
  537. /// <summary>
  538. /// Sets mini-map in full-screen mode or normal mode
  539. /// </summary>
  540. public bool miniMapZoomState {
  541. get {
  542. return _miniMapZoomState;
  543. }
  544. set {
  545. if (_miniMapZoomState != value) {
  546. MiniMapZoomToggle (value);
  547. }
  548. }
  549. }
  550. /// <summary>
  551. /// Forces an update of mini-map contents
  552. /// </summary>
  553. public void UpdateMiniMapContents () {
  554. needMiniMapShot = true;
  555. needUpdateBarContents = true;
  556. }
  557. /// <summary>
  558. /// Returns true if mouse pointer is over the mini-map
  559. /// </summary>
  560. /// <returns><c>true</c> if this instance is pointer over mini map; otherwise, <c>false</c>.</returns>
  561. public bool IsMouseOverMiniMap () {
  562. if (miniMapUIRootRT == null)
  563. return false;
  564. return RectTransformUtility.RectangleContainsScreenPoint (miniMapUIRootRT, Input.mousePosition);
  565. }
  566. #endregion
  567. }
  568. }