Advertisement
Guest User

Achievement.cs - MC Achievement Generator Tutorial

a guest
Jul 14th, 2013
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.49 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Drawing.Imaging;
  8.  
  9. namespace AGenTutorial
  10. {
  11.     //Třída zastupující generátor jednoho achievementu
  12.     public class Achievement
  13.     {
  14.         // Pokud je true, tak se musí achievement znovu vygenerovat
  15.         private bool needRegen = false;
  16.         private FontLoader font;
  17.  
  18.         //Prázdné pozadí achievemetnu
  19.         private Bitmap blankBackground;
  20.  
  21.         //Bitmapa pro konečný achievement
  22.         private Bitmap thisAchievement;
  23.  
  24.         //Titulek ach.
  25.         private string _title;
  26.         public string Title
  27.         {
  28.             get
  29.             {
  30.                 return _title;
  31.             }
  32.  
  33.             set
  34.             {
  35.                 //Při změně titulku se musí regenerovat achievement
  36.                 _title = value;
  37.                 needRegen = true;
  38.             }
  39.         }
  40.  
  41.         //Text ach.
  42.         private string _text;
  43.         public string Text
  44.         {
  45.             get
  46.             {
  47.                 return _text;
  48.             }
  49.  
  50.             set
  51.             {
  52.                 _text = value;
  53.                 needRegen = true;
  54.             }
  55.         }
  56.  
  57.         //Ikonka ach.
  58.         private Bitmap _icon;
  59.         public Bitmap Icon
  60.         {
  61.             get
  62.             {
  63.                 return _icon;
  64.             }
  65.  
  66.             set
  67.             {
  68.                 //Ikonka musí být <= 16x16px
  69.                 if (value.Width <= 16 && value.Height <= 16)
  70.                 {
  71.                     _icon = value;
  72.                     needRegen = true;
  73.                 }
  74.             }
  75.         }
  76.  
  77.         //Konstruktor nastavující jen FontLoader, volá "hlavní" konstruktor
  78.         public Achievement(FontLoader font)
  79.             : this(font, "Achievement get!", "", null)
  80.         {
  81.  
  82.         }
  83.  
  84.         //"Hlavní" konstruktor nastavující všechny vlastnosti
  85.         public Achievement(FontLoader font, string title, string text, Bitmap icon)
  86.         {
  87.             this.font = font;
  88.             this.Title = title;
  89.             this.Text = text;
  90.             this.Icon = icon;
  91.             //Pozadí naklonuje z Resources
  92.             this.blankBackground = (Bitmap)Properties.Resources.Blank.Clone();
  93.             Generate();
  94.         }
  95.  
  96.         //Pomocná metoda, která zjistí, kam nakreslit ikonku, aby měla střed na správném místě
  97.         private PointF GetIconDrawPoint(Bitmap image)
  98.         {
  99.             float centerX = 16f;
  100.             float centerY = 16f;
  101.  
  102.             centerY -= (image.Height / 2f);
  103.             centerX -= (image.Width / 2f);
  104.  
  105.             return new PointF(centerX, centerY);
  106.         }
  107.  
  108.         private void Generate()
  109.         {
  110.             //Vytvoří nový objekt v proměnné a zkopíruje do něj prázdné pozadí
  111.             thisAchievement = new Bitmap(blankBackground);
  112.             Graphics g = Graphics.FromImage(thisAchievement);
  113.  
  114.             //Nakreslíme ikonku
  115.             g.DrawImage(this.Icon, GetIconDrawPoint(this.Icon));
  116.  
  117.             //ImageAttributes slouží k nastavování/měnění barev/vlastností obrázku
  118.             ImageAttributes at = new ImageAttributes();
  119.             //ColorMapa mění určitou barvu na jinou - musíme změnit barvu prvního řádku na žluto
  120.             ColorMap map = new ColorMap();
  121.             map.OldColor = Color.White;
  122.             map.NewColor = Color.Yellow;
  123.             //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)
  124.             at.SetRemapTable(new ColorMap[] { map });
  125.  
  126.             //Získáme titulek a vykreslíme i s ImageAttributem at
  127.             Bitmap title = font.GetCropedStringImage(this.Title);
  128.             g.DrawImage(title, new Rectangle(30, 7, title.Width, title.Height), 0, 0, title.Width, title.Height,
  129.                 GraphicsUnit.Pixel, at);
  130.            
  131.             //Vykreslíme text standardním způsobem
  132.             g.DrawImage(font.GetCropedStringImage(this.Text), 30, 18);
  133.  
  134.             //Obrázek je vygenerován, nepotřebuje "přegenerovat"
  135.             needRegen = false;
  136.         }
  137.  
  138.         //Vrátí achievement, pokud je needRegen true, tak jej předtím "přegeneruje"
  139.         public Bitmap GetAchievement()
  140.         {
  141.             if (needRegen)
  142.                 Generate();
  143.  
  144.             return thisAchievement;
  145.         }
  146.     }
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement