Subversion Repositories AndroidProjects

Rev

Rev 1431 | Rev 1433 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using BurutaruEditor.file;

using BauzoidNET.graphics;
using BauzoidNET.graphics.sprite;
using BauzoidNET.graphics.renderstates;
using BauzoidNET.math;

namespace BurutaruEditor.view
{
    public class DocumentView
    {
        public const float START_POSITION_SIZE = 10;
        public const float CHECKPOINT_SIZE = 10;

        public static readonly Vector4 CHECKPOINT_PATH_COLOR = new Vector4(0.3f, 0.8f, 0.3f, 1.0f);
        public static readonly Vector4 CHECKPOINT_FRAME_PATH_COLOR = new Vector4(0.8f, 0.3f, 0.3f, 1.0f);
        public static readonly Vector4 CHECKPOINT_FRAME_COLOR = new Vector4(0.3f, 0.5f, 0.5f, 1.0f);

        //========================================================================================================


        private MainForm mOwner = null;
        public MainForm Owner { get { return mOwner; } }

        ///< Current Camera Position relative to origin/startposition
        private Vector2 mCurrentPosition = new Vector2();

        ///< Zoom Factor
        private float mZoomFactor = 1.0f;
        public float ZoomFactor
        {
            get { return mZoomFactor; }
            set
            {
                mZoomFactor = Math.Max(value, 0.1f);
                Owner.GlView.Refresh();            
            }
        }

        public SimpleSprite StartPositionSprite { get; set; }
        public SimpleSprite CheckPointSprite { get; set; }

        private Grid mGrid = null;

        private static ViewResources mViewResources = null;
        public static ViewResources Resources { get { return mViewResources; } }

        //========================================================================================================

        public DocumentView(MainForm owner)
        {
            mOwner = owner;

            StartPositionSprite = new SimpleSprite(MainForm.App.getGraphics(), "data/textures/ui/startposition.png");
            CheckPointSprite = new SimpleSprite(MainForm.App.getGraphics(), "data/textures/ui/checkpoint.png");

            mGrid = new Grid(this);

            ResetView();
        }

        public void Init()
        {
            mViewResources = new ViewResources(Owner);
            mViewResources.Init();

            StartPositionSprite.init();
            StartPositionSprite.getSpriteTransform().set(0, 0, START_POSITION_SIZE, START_POSITION_SIZE);

            CheckPointSprite.init();
            CheckPointSprite.getSpriteTransform().set(0, 0, CHECKPOINT_SIZE, CHECKPOINT_SIZE);
        }

        public void Exit()
        {
            StartPositionSprite.dispose();
            CheckPointSprite.dispose();

            mViewResources.Exit();
            mViewResources = null;
        }

        public void ResetView()
        {
            mCurrentPosition.set(0, 0);
        }

        public void Render()
        {
            RenderStates rs = MainForm.App.getGraphics().renderStates;

            mGrid.Render(mCurrentPosition.x, mCurrentPosition.y);

            rs.pushViewMatrix();
            {
                Matrix4 translate1 = Matrix4.createTranslation(MainForm.App.getGraphics().getWidth() / 2,
                    MainForm.App.getGraphics().getHeight() / 2, 0);
               
                Matrix4 translate2 = Matrix4.createTranslation(mCurrentPosition.x, mCurrentPosition.y, 0);

                Matrix4 scale = Matrix4.createScale(ZoomFactor);

                rs.view.identity();
                rs.view.multiply(translate2);
                rs.view.multiply(scale);
                rs.view.multiply(translate1);

                // render checkpoints
                RenderCheckPoints();



            }

            rs.popViewMatrix();
        }

        public void RenderCheckPoints()
        {
            float halfW = Document.VIRTUAL_WIDTH / 2;
            float halfH = Document.VIRTUAL_HEIGHT / 2;

            Vector2 lastPt = new Vector2(0, 0);
            for (int i = 0; i < Owner.Doc.CheckPoints.Count; i++)
            {
                Vector2 p = Owner.Doc.CheckPoints[i].Position;
                RenderUtil.drawLine(MainForm.App.getGraphics(), lastPt.x, lastPt.y, p.x, p.y, CHECKPOINT_PATH_COLOR);

                // line from frame to frame
                RenderUtil.drawLine(MainForm.App.getGraphics(), lastPt.x - halfW, lastPt.y - halfH, p.x - halfW, p.y - halfH, CHECKPOINT_FRAME_PATH_COLOR);
                RenderUtil.drawLine(MainForm.App.getGraphics(), lastPt.x + halfW, lastPt.y - halfH, p.x + halfW, p.y - halfH, CHECKPOINT_FRAME_PATH_COLOR);
                RenderUtil.drawLine(MainForm.App.getGraphics(), lastPt.x - halfW, lastPt.y + halfH, p.x - halfW, p.y + halfH, CHECKPOINT_FRAME_PATH_COLOR);
                RenderUtil.drawLine(MainForm.App.getGraphics(), lastPt.x + halfW, lastPt.y + halfH, p.x + halfW, p.y + halfH, CHECKPOINT_FRAME_PATH_COLOR);

                lastPt = p;
            }

            for (int i = 0; i < Owner.Doc.CheckPoints.Count; i++)
            {
                RenderCheckPoint(Owner.Doc.CheckPoints[i].Position.x, Owner.Doc.CheckPoints[i].Position.y);
            }

            StartPositionSprite.render();
            RenderUtil.drawQuad(MainForm.App.getGraphics(), -halfW, -halfH, halfW, halfH, CHECKPOINT_FRAME_COLOR);
        }

        public void RenderCheckPoint(float x, float y)
        {
            float halfW = Document.VIRTUAL_WIDTH / 2;
            float halfH = Document.VIRTUAL_HEIGHT / 2;
            RenderUtil.drawQuad(MainForm.App.getGraphics(), x - halfW, y - halfH, x + halfW, y + halfH, CHECKPOINT_FRAME_COLOR);

            CheckPointSprite.getSpriteTransform().setPosition(x, y);
            CheckPointSprite.render();
        }

        public void Pan(float dx, float dy)
        {
            mCurrentPosition.x += dx;
            mCurrentPosition.y += dy;

            Owner.GlView.Refresh();            
        }

    }
}