-
Notifications
You must be signed in to change notification settings - Fork 324
/
ImageLabel.cs
58 lines (51 loc) · 1.86 KB
/
ImageLabel.cs
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
// Project: Daggerfall Unity
// Copyright: Copyright (C) 2009-2022 Daggerfall Workshop
// Web Site: http://www.dfworkshop.net
// License: MIT License (http://www.opensource.org/licenses/mit-license.php)
// Source Code: https://github.com/Interkarma/daggerfall-unity
// Original Author: Gavin Clayton (interkarma@dfworkshop.net)
// Contributors:
//
// Notes:
//
using UnityEngine;
namespace DaggerfallWorkshop.Game.UserInterface
{
/// <summary>
/// Renders an image in place of a text label in book reader UI.
/// </summary>
public class ImageLabel : TextLabel
{
Texture2D image;
float imageWidth;
float imageHeight;
float scaleFactor;
public Texture2D Image
{
get { return image; }
set { image = value; RefreshLayout(); }
}
public override void Draw()
{
if (image == null || image.width == 0 || image.height == 0)
return;
// Image position is always centred to page
RefreshLayout();
Rect totalRect = Rectangle;
Rect rect = new Rect(totalRect.x + imageWidth / 2, totalRect.y, imageWidth, imageHeight);
Size = new Vector2(imageWidth, imageHeight);
DaggerfallUI.DrawTexture(rect, image, ScaleMode.StretchToFill);
}
public override void RefreshLayout()
{
if (image == null || image.width == 0 || image.height == 0)
return;
// Image size is always half width of page area
base.RefreshLayout();
imageWidth = (float)MaxWidth * LocalScale.x / 2f;
scaleFactor = (float)MaxWidth / (float)image.width;
imageHeight = (float)image.height * scaleFactor * LocalScale.y / 2f;
Size = new Vector2(imageWidth, imageHeight);
}
}
}