private void StartScaleMode() { if (scaleMode != ScaleMode.FitToAreaWidth && scaleMode != ScaleMode.FitToAreaHeight) { return; } var root = MadTransform.FindParent <MadRootNode>(transform); Vector3 scale; switch (scaleMode) { case ScaleMode.FitToAreaWidth: float width = dragBounds.size.x; float screenWidth = root.screenWidth; scale = Vector3.one * screenWidth / width; break; case ScaleMode.FitToAreaHeight: float height = dragBounds.size.y; float screenHeight = root.screenHeight; scale = Vector3.one * screenHeight / height; break; default: Debug.Log("Unknown scale mode: " + scaleMode); scale = Vector3.one; break; } transform.localScale = scale; }
static Transform ActiveParentOrPanel() { Transform parentTransform = null; var transforms = Selection.transforms; if (transforms.Length > 0) { var firstTransform = transforms[0]; if (firstTransform.GetComponent <MadPanel>() != null || MadTransform.FindParent <MadPanel>(firstTransform) != null) { parentTransform = firstTransform; } } if (parentTransform == null) { var selected = Selection.activeGameObject; Transform selectedTransform = null; if (selected != null) { selectedTransform = selected.transform; } var panel = MadPanel.FirstOrNull(selectedTransform); if (panel != null) { parentTransform = panel.transform; } } return(parentTransform); }
static Transform ActiveParentOrPanel() { Transform parentTransform = null; var transforms = Selection.transforms; if (transforms.Length > 0) { var firstTransform = transforms[0]; if (MadTransform.FindParent <MadPanel>(firstTransform) != null) { parentTransform = firstTransform; } } if (parentTransform == null) { var panel = MadPanel.UniqueOrNull(); if (panel != null) { parentTransform = panel.transform; } } return(parentTransform); }
void ActivateAction( SerializedProperty playAudio, SerializedProperty playAudioClip, SerializedProperty playAudioVolume, SerializedProperty message, SerializedProperty messageReceiver, SerializedProperty messageMethodName, SerializedProperty messageIncludeChildren ) { MadGUI.PropertyField(playAudio, "Play Audio"); MadGUI.ConditionallyEnabled(playAudio.boolValue, () => { MadGUI.Indent(() => { if (playAudio.boolValue && !FoundAudioListener()) { if (MadGUI.ErrorFix("There's no AudioListener on the scene. Do you want me to add an " + "AudioListener to Camera 2D?", "Add")) { var camera = MadTransform.FindParent <Camera>((target as Component).transform); if (camera == null) { camera = FindObjectOfType(typeof(Camera)) as Camera; } if (camera != null) { camera.gameObject.AddComponent <AudioListener>(); } else { Debug.LogError("There's no camera on this scene!"); } } } MadGUI.PropertyField(playAudioClip, "Audio Clip", MadGUI.ObjectIsSet); MadGUI.PropertyFieldSlider(playAudioVolume, 0, 1, "Volume"); }); }); MadGUI.PropertyField(message, "Send Message"); MadGUI.ConditionallyEnabled(message.boolValue, () => { MadGUI.Indent(() => { MadGUI.PropertyField(messageReceiver, "Receiver", MadGUI.ObjectIsSet); MadGUI.PropertyField(messageMethodName, "Method Name", MadGUI.StringNotEmpty); MadGUI.PropertyField(messageIncludeChildren, "Include Children"); if (message.boolValue) { MadGUI.Info("This should look like this:\nvoid " + messageMethodName.stringValue + "(MadLevelIcon icon)"); } }); }); }
void ClampPosition() { var position = cameraPos; var rootNode = MadTransform.FindParent <MadRootNode>(transform); var areaBottomLeft = new Vector2(dragArea.xMin, dragArea.yMin); var areaTopRight = new Vector2(dragArea.xMax, dragArea.yMax); var screenBottomLeft = transform.InverseTransformPoint(rootNode.ScreenGlobal(0, 0)); var screenTopRight = transform.InverseTransformPoint(rootNode.ScreenGlobal(1, 1)); float deltaLeft = screenBottomLeft.x - areaBottomLeft.x; float deltaRight = screenTopRight.x - areaTopRight.x; float deltaTop = screenTopRight.y - areaTopRight.y; float deltaBottom = screenBottomLeft.y - areaBottomLeft.y; // apply scale because transform matrix does not contain it float scale = transform.localScale.x; deltaLeft *= scale; deltaRight *= scale; deltaTop *= scale; deltaBottom *= scale; if (dragArea.width < (screenTopRight.x - screenBottomLeft.x)) // drag area smaller { position.x = (areaTopRight.x + areaBottomLeft.x) / 2; } else if (deltaLeft < 0) { position.x -= deltaLeft; } else if (deltaRight > 0) { position.x -= deltaRight; } if (dragArea.height < (screenTopRight.y - screenBottomLeft.y)) { position.y = (areaBottomLeft.y + areaTopRight.y) / 2; } else if (deltaBottom < 0) { position.y -= deltaBottom; } else if (deltaTop > 0) { position.y -= deltaTop; } cameraPos = position; }
private bool SetupMethodGenerate() { var gridLayout = MadTransform.FindParent <MadLevelGridLayout>(sprite.transform); if (gridLayout != null && gridLayout.setupMethod == MadLevelGridLayout.SetupMethod.Generate) { return(true); } else { return(false); } }
void Start() { icon = MadTransform.FindParent <MadLevelIcon>(transform); sprite = GetComponent <MadSprite>(); if (icon != null) { AssignTexture(); } else { Debug.LogError("MadLevelImage may be set only as a MadLevelIcon child"); } }
void Start() { sprite = GetComponent <MadSprite>(); if (sprite == null) { Debug.LogError("Condition needs a MadSprite to be attached to this game object.", this); return; } icon = MadTransform.FindParent <MadLevelIcon>(transform); if (icon == null) { Debug.LogError("Condition need to be set under MadLevelIcon.", this); return; } Apply(); }
// =========================================================== // Static Methods // =========================================================== public static MadPanel FirstOrNull(Transform currentTransform) { // first try to find panel as parent if (currentTransform != null) { var panel = MadTransform.FindParent <MadPanel>(currentTransform); if (panel != null) { return(panel); } } // then try to locate the panel on the static list if (panels.Count > 0) { return(panels[0]); } // if all above fails, try to locate panel using the slow FindObjectOfType method return(GameObject.FindObjectOfType(typeof(MadPanel)) as MadPanel); }
void UpdateObjectAnchor() { if (anchorObject == null) { return; } Camera camera = anchorCamera; if (camera == null) { if (Application.isPlaying) { MadDebug.LogOnce("Anchor camera not set. Using main camera.", this); } camera = Camera.main; } float z = transform.position.z; Camera guiCamera = MadTransform.FindParent <Camera>(transform); var pos = camera.WorldToScreenPoint(anchorObject.transform.position); pos = guiCamera.ScreenToWorldPoint(pos); if (!moveIn3D) { pos.z = z; } transform.position = pos; if (faceCamera) { transform.LookAt(anchorCamera.transform); } }
protected virtual void Start() { root = MadTransform.FindParent <MadRootNode>(transform); cachedCamPos = cameraPos; lastTouchTime = -1000; // prevent touch time on start }
protected virtual void OnEnable() { if (currentConfiguration == null) { // not bound to any configuration. Bound it to active if (MadLevel.hasActiveConfiguration) { configuration = MadLevel.activeConfiguration; } else { Debug.LogWarning("There's no active level configuration. Please prepare one and activate it."); } } else if (!useCurrentlyActiveConfiguration && currentConfiguration != MadLevel.activeConfiguration) { if (Application.isPlaying) { Debug.LogWarning("This layout was prepared for different level configuration than the active one. " + "http://goo.gl/AxZqW2", this); } } if (Application.isPlaying) { if (useCurrentlyActiveConfiguration && !CurrentConfigurationValid()) { Debug.LogError("Your currently active configuration is not compatible with the configuration " + "that this level was prepared for. Please see the layout inspector."); } } var panel = MadTransform.FindParent <MadPanel>(transform); panel.onFocusChanged += (MadSprite sprite) => { if (activeIcon != null && sprite != activeIcon) { DeactivateActiveIcon(); } }; onIconActivate += (icon, levelName) => { if (onIconActivateMessage && onIconActivateMessageReceiver != null) { if (onIconActivateMessageIncludeChildren) { onIconActivateMessageReceiver.BroadcastMessage(onIconActivateMessageMethodName, icon); } else { onIconActivateMessageReceiver.SendMessage(onIconActivateMessageMethodName, icon); } } if (onIconActivatePlayAudio && onIconActivatePlayAudioClip != null && cachedAudioListener != null) { AudioSource.PlayClipAtPoint( onIconActivatePlayAudioClip, cachedAudioListener.transform.position, onIconActivatePlayAudioVolume); } }; onIconDeactivate += (icon, levelName) => { if (onIconDeactivateMessage && onIconDeactivateMessageReceiver != null) { if (onIconDeactivateMessageIncludeChildren) { onIconDeactivateMessageReceiver.BroadcastMessage(onIconDeactivateMessageMethodName, icon); } else { onIconDeactivateMessageReceiver.SendMessage(onIconDeactivateMessageMethodName, icon); } } if (onIconDeactivatePlayAudio && onIconDeactivatePlayAudioClip != null && cachedAudioListener != null) { AudioSource.PlayClipAtPoint( onIconDeactivatePlayAudioClip, cachedAudioListener.transform.position, onIconDeactivatePlayAudioVolume); } }; }
/// <summary> /// Activates this icon. /// </summary> public void Activate() { var layout = MadTransform.FindParent <MadLevelAbstractLayout>(transform); layout.Activate(this); }
void ClampPosition() { var position = cameraPos; var rootNode = MadTransform.FindParent <MadRootNode>(transform); var areaBottomLeft = new Vector2(dragBounds.min.x, dragBounds.min.y); var areaTopRight = new Vector2(dragBounds.max.x, dragBounds.max.y); var screenBottomLeft = transform.InverseTransformPoint(rootNode.ScreenGlobal(0, 0)); var screenTopRight = transform.InverseTransformPoint(rootNode.ScreenGlobal(1, 1)); float deltaLeft = screenBottomLeft.x - areaBottomLeft.x; float deltaRight = screenTopRight.x - areaTopRight.x; float deltaTop = screenTopRight.y - areaTopRight.y; float deltaBottom = screenBottomLeft.y - areaBottomLeft.y; // apply scale because transform matrix does not contain it float scale = transform.localScale.x; deltaLeft *= scale; deltaRight *= scale; deltaTop *= scale; deltaBottom *= scale; if (dragBounds.size.x < (screenTopRight.x - screenBottomLeft.x)) // drag area smaller { position.x = (areaTopRight.x + areaBottomLeft.x) / 2; } else if (deltaLeft < 0) { position.x -= deltaLeft; } else if (deltaRight > 0) { position.x -= deltaRight; } if (dragBounds.size.y < (screenTopRight.y - screenBottomLeft.y)) { position.y = (areaBottomLeft.y + areaTopRight.y) / 2; } else if (deltaBottom < 0) { position.y -= deltaBottom; } else if (deltaTop > 0) { position.y -= deltaTop; } cameraPos = position; // fixing position flicker if fit to area width or height (a little hack) switch (scaleMode) { case ScaleMode.FitToAreaWidth: cameraPos = new Vector2(transform.position.x + dragBounds.center.x * transform.localScale.x, cameraPos.y); break; case ScaleMode.FitToAreaHeight: cameraPos = new Vector2(cameraPos.x, transform.position.y + dragBounds.center.y * transform.localScale.y); break; } }
protected virtual void OnEnable() { if (configuration == null) { // not bound to any configuration. Bound it to active if (MadLevel.hasActiveConfiguration) { configuration = MadLevel.activeConfiguration; } else { Debug.LogWarning("There's no active level configuration. Please prepare one and activate it."); } } else if (configuration != MadLevel.activeConfiguration) { if (Application.isPlaying) { Debug.LogWarning("This layout was prepared for different level configuration than the active one. " + "http://goo.gl/AxZqW2", this); } } var panel = MadTransform.FindParent <MadPanel>(transform); panel.onFocusChanged += (MadSprite sprite) => { if (activeIcon != null && sprite != activeIcon) { DeactivateActiveIcon(); } }; onIconActivate += (icon, levelName) => { if (onIconActivateMessage && onIconActivateMessageReceiver != null) { onIconActivateMessageReceiver.SendMessage(onIconActivateMessageMethodName, icon); } if (onIconActivatePlayAudio && onIconActivatePlayAudioClip != null && cachedAudioListener != null) { AudioSource.PlayClipAtPoint( onIconActivatePlayAudioClip, cachedAudioListener.transform.position, onIconActivatePlayAudioVolume); } }; onIconDeactivate += (icon, levelName) => { if (onIconDeactivateMessage && onIconDeactivateMessageReceiver != null) { onIconDeactivateMessageReceiver.SendMessage(onIconDeactivateMessageMethodName, icon); } if (onIconDeactivatePlayAudio && onIconDeactivatePlayAudioClip != null && cachedAudioListener != null) { AudioSource.PlayClipAtPoint( onIconDeactivatePlayAudioClip, cachedAudioListener.transform.position, onIconDeactivatePlayAudioVolume); } }; #if UNITY_EDITOR EditorApplication.playmodeStateChanged = () => { if (configuration != null && EditorApplication.isPlayingOrWillChangePlaymode && !EditorApplication.isPlaying) { if (!configuration.IsValid()) { if (!EditorUtility.DisplayDialog( "Invalid Configuration", "Your level configuration has errors. Do you want to continue anyway?", "Yes", "No")) { EditorApplication.isPlaying = false; Selection.activeObject = configuration; return; } } if (configuration != MadLevel.activeConfiguration || !configuration.CheckBuildSynchronized() || !configuration.active) { if (EditorUtility.DisplayDialog( "Not Synchronized", "Build configuration of choice is not activate/synchronized with this level select layout " + "(errors will occur). Do it now?", "Yes", "No")) { MadLevelConfiguration.GetActive().active = false; // workaround configuration.active = true; configuration.SynchronizeBuild(); } } } }; #endif }