Пример #1
0
    // Draw Turret Module Preview
    void CreatePreviewWindow(Module module)
    {
        // Local Vars
        var moduleName = module.ToString();
        var moduleIdx  = (int)module;
        var evt        = Event.current;

        // Hover Border Change
        var hoverPreviewBorderStyle = GUIStyle.none;

        if (m_ModulePreviewRects[moduleIdx].Contains(evt.mousePosition))
        {
            hoverPreviewBorderStyle = m_PreviewBorderStyle;
        }

        // Module Cycling via Scroll Wheel
        if (m_ModulePreviewRects[moduleIdx].Contains(evt.mousePosition) &&
            evt.type == EventType.ScrollWheel)
        {
            m_TurretBuilder.CycleModules(moduleName, (int)evt.delta.y);
            m_TurretBuilder.ResetModulePreviewEditor(moduleName);
            m_TurretBuilder.RegenerateTurret();
            evt.Use();
        }

        using (new GUILayout.VerticalScope())
        {
            // Top hover border.
            GUILayout.Box(
                GUIContent.none, hoverPreviewBorderStyle,
                GUILayout.Height(k_BorderWidth));

            var previewRect =
                GUILayoutUtility.GetRect(k_PreviewSize, k_PreviewSize);

            // Need to save this rect during Repaint and use it during Layout for the AreaScope.
            if (evt.type == EventType.Repaint)
            {
                m_ModulePreviewRects[moduleIdx] = previewRect;
            }

            // Don't draw the interactive preview when the mouse is over the PropertyField
            // otherwise the preview steals the MouseDownEvent.
            var editor = m_TurretBuilder.GetPreviewEditor(moduleName);
            if (editor != null &&
                !(evt.type == EventType.MouseDown &&
                  m_ModulePropertyFieldRects[moduleIdx].Contains(evt.mousePosition)))
            {
                editor.OnInteractivePreviewGUI(previewRect, null);
            }

            using (new GUILayout.AreaScope(m_ModulePreviewRects[moduleIdx]))
            {
                GUILayout.FlexibleSpace();

                var property  = serializedObject.FindProperty(moduleName);
                var fieldRect = EditorGUILayout.GetControlRect();

                EditorGUI.BeginChangeCheck();
                EditorGUI.PropertyField(fieldRect, property);
                if (EditorGUI.EndChangeCheck())
                {
                    serializedObject.ApplyModifiedProperties();
                    m_TurretBuilder.ResetModulePreviewEditor(moduleName);
                    m_TurretBuilder.RegenerateTurret();
                }

                if (Event.current.type == EventType.Repaint)
                {
                    // Translate the filed rect to be in "world" space outside the AreaScope.
                    fieldRect.x += m_ModulePreviewRects[moduleIdx].x;
                    fieldRect.y += m_ModulePreviewRects[moduleIdx].y;
                    m_ModulePropertyFieldRects[(int)module] = fieldRect;
                }
            }

            // Bottom hover border.
            GUILayout.Box(
                GUIContent.none, hoverPreviewBorderStyle,
                GUILayout.Height(k_BorderWidth));
        }
    }
    //
    //
    //
    #endregion

    #region CreateInspectorGUI()
    //
    //
    //

    public override VisualElement CreateInspectorGUI()
    {
        // Reset root element and reuse.
        var root = m_RootElement;

        root.Clear();

        // Turn the UXML into a VisualElement hierarchy under root.
        m_ModulesVisualTree.CloneTree(root);

        // Create Module Previews
        root.Query(classes: new string[] { "module-preview" })
        .ForEach((preview) =>
        {
            preview.Add(CreateModuleUI(preview.name));
        });

        // Change Detection
        root.RegisterCallback <ChangeEvent <Object> >(evt =>
        {
            var element    = evt.target as ObjectField;
            var moduleName = element.bindingPath;

            if (moduleName == "Material")
            {
                UpdateMaterialPreview();
                m_TurretBuilder.ResetAllModulePreviewEditors();
            }
            else
            {
                m_TurretBuilder.ResetModulePreviewEditor(moduleName);
            }

            m_TurretBuilder.RegenerateTurret();
        });

        // Material Preview
        {
            var turret       = target as Turret;
            var material     = turret.Material;
            var texture      = material?.mainTexture as Texture2D;
            var imageElement = root.Q("MaterialImage");

            imageElement.style.backgroundImage = null;
            if (texture != null)
            {
                imageElement.style.backgroundImage = texture;
            }
            else if (material != null)
            {
                imageElement.style.backgroundColor = material.color;
            }
            else
            {
                imageElement.style.backgroundColor = Color.clear;
            }
        }
        root.Q("MaterialImage").AddManipulator(new TextureDragger());

        // Module Cycling via Scroll Wheel
        root.Query(classes: new string[] { "preview" }).ForEach((preview) =>
        {
            preview.RegisterCallback <WheelEvent>(evt =>
            {
                var element = evt.currentTarget as VisualElement;
                m_TurretBuilder.CycleModules(element.name, (int)evt.delta.y);
                evt.StopPropagation();
            });
        });

        return(root);
    }