Example #1
0
    /// <summary>
    /// Draw trade-related UI.
    /// </summary>

    void DrawTradeUI()
    {
        Vector2 mousePos = UI.GetMousePos();

        // Imports and exports for this town
        Town opposite = mSelectedRoute.GetConnectedTown(this);

        // Draw the trade window
        Rect rect = new Rect(Screen.width * 0.5f - 75f, Screen.height * 0.5f - 270f, 150f, 512f);

        // Allow resources to be dropped here
        if (mDragResource != -1 && Input.GetMouseButtonUp(0) && rect.Contains(mousePos))
        {
            mSelectedRoute.SetExportedResource(mDragTown, mDragResource);
            CancelDrag();
        }
        // Allow ships to be dropped here
        else if (mDragShip != null && Input.GetMouseButtonUp(0) && rect.Contains(mousePos))
        {
            mSelectedRoute.AssignShip(mDragShip);
            CancelDrag();
        }

        rect = UI.DrawPanel(rect);
        Rect titleRect = new Rect(rect.x, rect.y - 25.0f, rect.width, 40.0f);

        UI.DrawPanel(titleRect);
        UI.DrawTitle(titleRect, "Trade", Config.Instance.headerStyle);
        rect = UI.Bevel(rect, 8f);

        // Main town imports from the connected city
        {
            UI.DrawTitle(new Rect(rect.x, rect.y, rect.width, 30f), "Imports", Config.Instance.infoStyle);

            rect.y      += 30f;
            rect.height -= 30f;

            // Draw the arrow
            UI.DrawTexture(rect.x + rect.width - 70f + Mathf.Sin(Time.time * 5f) * 10f,
                           rect.y + rect.width * 0.5f - Icons.Instance.arrowRight.height * 0.5f, Icons.Instance.arrowRight);

            // Draw the grid for icons
            bool  tint = (mDragResource != -1 && mDragTown != null && mDragTown != this);
            Color prev = GUI.color;
            GUI.color = tint ? new Color(0f, 1f, 0f, prev.a) : prev;
            List <Vector2> grid = UI.DrawGrid(rect, 50f, 50f, 4f, 4f, 4f, 4);
            GUI.color = prev;

            // Draw the imported resources
            DrawTradedResourcesUI(grid, opposite);

            rect.y      += rect.width;
            rect.height -= rect.width;
        }

        // Assigned ships
        {
            UI.DrawTitle(new Rect(rect.x, rect.y, rect.width, 30f), "Ship Capacity", Config.Instance.infoStyle);

            rect.y      += 30f;
            rect.height -= 30f;

            // Draw the grid, coloring it green if we're dragging an assignable ship
            Color prev = GUI.color;
            GUI.color = (mDragShip == null) ? prev : new Color(0f, 1f, 0f, prev.a);
            List <Vector2> grid = UI.DrawGrid(rect, 50f, 50f, 4f, 4f, 4f, 4);
            GUI.color = prev;

            rect.y      += rect.width;
            rect.height -= rect.width;

            int count = 0;

            // Draw all assigned ships' icons
            foreach (AvailableShips.Owned ship in AvailableShips.Instance.list)
            {
                if (ship.tradeRoute == mSelectedRoute)
                {
                    Vector2 pos = grid[count++];

                    Rect shipRect = new Rect(pos.x, pos.y, 50f, 50f);

                    if (ship.prefab.icon != null)
                    {
                        UI.DrawTexture(pos.x, pos.y, ship.prefab.icon);
                    }

                    UI.DrawLabel(shipRect, ship.prefab.cargo.ToString(), Config.Instance.infoStyle,
                                 Color.white, Color.black, true);

                    // Allow dragging of this icon
                    if (Input.GetMouseButtonDown(0) && shipRect.Contains(mousePos))
                    {
                        CancelDrag();
                        mDragShip = ship;
                    }
                }
            }
        }

        // Main city exports to the connected city
        {
            UI.DrawTitle(new Rect(rect.x, rect.y, rect.width, 30f), "Exports", Config.Instance.infoStyle);

            rect.y      += 30f;
            rect.height -= 30f;

            // Draw the arrow
            UI.DrawTexture(rect.x + rect.width - 70f + Mathf.Sin(Time.time * 5f) * 10f,
                           rect.y + rect.width * 0.5f - Icons.Instance.arrowRight.height * 0.5f, Icons.Instance.arrowRight);

            // Draw the grid for icons
            bool  tint = (mDragResource != -1 && mDragTown == this);
            Color prev = GUI.color;
            GUI.color = tint ? new Color(0f, 1f, 0f, prev.a) : prev;
            List <Vector2> grid = UI.DrawGrid(rect, 50f, 50f, 4f, 4f, 4f, 4);
            GUI.color = prev;

            // Draw the imported resources
            DrawTradedResourcesUI(grid, this);
        }
    }