Example #1
0
    /// <summary>
    /// Draw the ship-building UI.
    /// </summary>

    void DrawBuildShipsUI()
    {
        Vector2 mousePos = UI.GetMousePos();
        Rect    rect     = new Rect(Screen.width * 0.5f - 425f, Screen.height * 0.5f - 210f, 292f, 166f);

        rect = UI.DrawWindow(rect, "Build Ships");

        // Allow ships to be sold by dragging them here
        if (mDragShip != null && Input.GetMouseButtonUp(0) && rect.Contains(mousePos))
        {
            if (mSelectedRoute != null)
            {
                mSelectedRoute.UnassignShip(mDragShip);
            }
            AvailableShips.Instance.list.Remove(mDragShip);
            Config.Instance.gold += mDragShip.prefab.price;
            CancelDrag();
        }

        // Draw the grid, colored green if we're dragging a sellable ship
        rect = UI.Bevel(rect, 8f);
        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, 8);

        GUI.color = prev;

        int max = Mathf.Min(grid.Count, AvailableShips.Instance.prefabs.Count);

        // Draw all ships that we can currently build
        for (int i = 0; i < max; ++i)
        {
            Vector3 pos = grid[i];
            AvailableShips.Template ship = AvailableShips.Instance.prefabs[i];
            Rect shipRect = new Rect(pos.x, pos.y, 50f, 50f);

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

            // Price caption
            UI.DrawLabel(shipRect, "$" + ship.price, Config.Instance.descStyle,
                         Config.Instance.gold < ship.price ? Color.red : Color.white, Color.black, true);

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

            // Allow dragging of this icon
            if (Config.Instance.gold >= ship.price &&
                Input.GetMouseButtonDown(0) &&
                shipRect.Contains(mousePos))
            {
                CancelDrag();
                mDragPrefab = ship;
            }

            // TODO: Ship tooltips
            //Tooltip.AddArea(shipRect, OnShipTooltip, ship);
        }
    }