Overview

Classes

  • Card
  • CardRegular
  • Component
  • ComponentAvatar
  • ComponentFlag
  • ComponentImage
  • ComponentLabel
  • ComponentXPBar
  • ErrorImage
  • OsuAPI
  • OsuSignature
  • PredefinedColours
  • Signature
  • Template
  • TemplateNormal
  • Utils
  • Overview
  • Class
  1:   2:   3:   4:   5:   6:   7:   8:   9:  10:  11:  12:  13:  14:  15:  16:  17:  18:  19:  20:  21:  22:  23:  24:  25:  26:  27:  28:  29:  30:  31:  32:  33:  34:  35:  36:  37:  38:  39:  40:  41:  42:  43:  44:  45:  46:  47:  48:  49:  50:  51:  52:  53:  54:  55:  56:  57:  58:  59:  60:  61:  62:  63:  64:  65:  66:  67:  68:  69:  70:  71:  72:  73:  74:  75:  76:  77:  78:  79:  80:  81:  82:  83:  84:  85:  86:  87:  88:  89:  90:  91:  92:  93:  94:  95:  96:  97:  98:  99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 
<?php
/**
 * A level/xp bar component.
 *
 * @author Lemmmy
 */
class ComponentXPBar extends Component
{
    /**
     * The width of this xp bar
     *
     * @var int
     */
    private $width;

    /**
     * The height of this xp bar
     *
     * @var int
     */
    private $height;

    /**
     * The rounding of this xp bar
     *
     * @var int
     */
    private $rounding;

    /**
     * The hex colour of this xp bar
     *
     * @var string
     */
    private $hexColour;

    /**
     * @param OsuSignature $signature The base signature
     * @param int $x The X position of this xp bar
     * @param int $y The Y position of this xp bar
     * @param string $hexColour The colour of this xp bar. Empty will default to sig's colour.
     * @param int $width The width of this xp bar
     * @param int $height The height of this xp bar
     * @param int $rounding How much to round this xp bar
     */
    public function __construct(
        OsuSignature $signature,
        $x = 0,
        $y = 0,
        $hexColour = "#ffa200",
        $width = 0,
        $height = 0,
        $rounding = 1) {

        parent::__construct($signature, $x, $y);

        $this->width = $width;
        $this->height = $height;

        $this->rounding = $rounding;

        $this->hexColour = $hexColour;
    }

    public function getWidth() {
        return $this->width;
    }

    public function getHeight() {
        return $this->height;
    }

    public function draw(OsuSignature $signature) {
        if (empty($this->hexColour)) {
            $this->hexColour = $signature->getHexColour();
        }

        $composite = new Imagick();
        $composite->newPseudoImage($this->getWidth(), $this->getHeight(), "canvas:transparent");

        // Background

        $draw = new ImagickDraw();
        $draw->setFillColor(new ImagickPixel("#555555"));
        $draw->rectangle(0, 0, $this->getWidth(), $this->getHeight());

        $composite->drawImage($draw);

        // Main bar

        $level = $signature->getUser()['level'];
        $xp = $level - floor($level);

        $draw = new ImagickDraw();
        $draw->setFillColor(new ImagickPixel($this->hexColour));
        $draw->rectangle(0, 0, $this->getWidth() * $xp, $this->getHeight());

        $composite->drawImage($draw);

        // Bar end glow

        $draw = new ImagickDraw();
        $draw->setFillColor(new ImagickPixel('#ffffff'));
        $draw->setFillOpacity(0.35);
        $draw->rectangle(($this->getWidth() * $xp) - $this->getHeight(), 0, $this->getWidth() * $xp, $this->getHeight());

        $composite->drawImage($draw);

        // Text draw & metrics

        $textDraw = new ImagickDraw();
        $textDraw->setFillColor(new ImagickPixel('#555555'));
        $textDraw->setFontSize(12);
        $textDraw->setFont(ComponentLabel::FONT_DIRECTORY . ComponentLabel::FONT_REGULAR);
        $textDraw->setGravity(Imagick::GRAVITY_NORTHWEST);

        $metrics = $composite->queryFontMetrics($textDraw, 'lv' . floor($level));

        // Text white bg

        $draw = new ImagickDraw();
        $draw->setFillColor(new ImagickPixel('#ffffff'));

        $draw->rectangle(
            ($this->getWidth() - $metrics['textWidth']) / 2 - 2,
            0,
            ($this->getWidth() + $metrics['textWidth']) / 2 + 1,
            $this->getHeight());

        $composite->drawImage($draw);

        // Rounding

        $roundMask = new Imagick();
        $roundMask->newPseudoImage($this->getWidth(), $this->getHeight(), "canvas:transparent");

        $draw = new ImagickDraw();
        $draw->setFillColor(new ImagickPixel("black"));
        $draw->roundRectangle(0, 0, $this->getWidth() - 1, $this->getHeight() - 1, $this->rounding, $this->rounding);

        $roundMask->drawImage($draw);
        $roundMask->setImageFormat('png');

        $composite->compositeImage(
            $roundMask,
            Imagick::COMPOSITE_DSTIN,
            0,
            0,
            Imagick::CHANNEL_ALPHA
        );

        $signature->getCanvas()->compositeImage($composite, Imagick::COMPOSITE_DEFAULT, $this->x, $this->y);

        // Level text

        $signature->getCanvas()->annotateImage(
            $textDraw,
            $this->x + ($this->getWidth() - $metrics['textWidth']) / 2,
            $this->y + ($this->getHeight() - $metrics['textHeight']) / 2 - 2,
            0,
            'lv' . floor($level));
    }
}
API documentation generated by ApiGen