Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Drawing.Imaging;
- namespace AGenTutorial
- {
- //Třída zastupující generátor jednoho achievementu
- public class Achievement
- {
- // Pokud je true, tak se musí achievement znovu vygenerovat
- private bool needRegen = false;
- private FontLoader font;
- //Prázdné pozadí achievemetnu
- private Bitmap blankBackground;
- //Bitmapa pro konečný achievement
- private Bitmap thisAchievement;
- //Titulek ach.
- private string _title;
- public string Title
- {
- get
- {
- return _title;
- }
- set
- {
- //Při změně titulku se musí regenerovat achievement
- _title = value;
- needRegen = true;
- }
- }
- //Text ach.
- private string _text;
- public string Text
- {
- get
- {
- return _text;
- }
- set
- {
- _text = value;
- needRegen = true;
- }
- }
- //Ikonka ach.
- private Bitmap _icon;
- public Bitmap Icon
- {
- get
- {
- return _icon;
- }
- set
- {
- //Ikonka musí být <= 16x16px
- if (value.Width <= 16 && value.Height <= 16)
- {
- _icon = value;
- needRegen = true;
- }
- }
- }
- //Konstruktor nastavující jen FontLoader, volá "hlavní" konstruktor
- public Achievement(FontLoader font)
- : this(font, "Achievement get!", "", null)
- {
- }
- //"Hlavní" konstruktor nastavující všechny vlastnosti
- public Achievement(FontLoader font, string title, string text, Bitmap icon)
- {
- this.font = font;
- this.Title = title;
- this.Text = text;
- this.Icon = icon;
- //Pozadí naklonuje z Resources
- this.blankBackground = (Bitmap)Properties.Resources.Blank.Clone();
- Generate();
- }
- //Pomocná metoda, která zjistí, kam nakreslit ikonku, aby měla střed na správném místě
- private PointF GetIconDrawPoint(Bitmap image)
- {
- float centerX = 16f;
- float centerY = 16f;
- centerY -= (image.Height / 2f);
- centerX -= (image.Width / 2f);
- return new PointF(centerX, centerY);
- }
- private void Generate()
- {
- //Vytvoří nový objekt v proměnné a zkopíruje do něj prázdné pozadí
- thisAchievement = new Bitmap(blankBackground);
- Graphics g = Graphics.FromImage(thisAchievement);
- //Nakreslíme ikonku
- g.DrawImage(this.Icon, GetIconDrawPoint(this.Icon));
- //ImageAttributes slouží k nastavování/měnění barev/vlastností obrázku
- ImageAttributes at = new ImageAttributes();
- //ColorMapa mění určitou barvu na jinou - musíme změnit barvu prvního řádku na žluto
- ColorMap map = new ColorMap();
- map.OldColor = Color.White;
- map.NewColor = Color.Yellow;
- //Přidáme mapu do objektu at (ImageAttributes.SetRemapTable přebírá jen pole ColorMap, musíme jej tedy vytvořit a přidat do něj jen náš objekt ColorMap)
- at.SetRemapTable(new ColorMap[] { map });
- //Získáme titulek a vykreslíme i s ImageAttributem at
- Bitmap title = font.GetCropedStringImage(this.Title);
- g.DrawImage(title, new Rectangle(30, 7, title.Width, title.Height), 0, 0, title.Width, title.Height,
- GraphicsUnit.Pixel, at);
- //Vykreslíme text standardním způsobem
- g.DrawImage(font.GetCropedStringImage(this.Text), 30, 18);
- //Obrázek je vygenerován, nepotřebuje "přegenerovat"
- needRegen = false;
- }
- //Vrátí achievement, pokud je needRegen true, tak jej předtím "přegeneruje"
- public Bitmap GetAchievement()
- {
- if (needRegen)
- Generate();
- return thisAchievement;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement