aboutsummaryrefslogtreecommitdiff
path: root/src/Java/binnie/craftgui/resource/Texture.java
blob: 249be8f25726ee0e0b91bebe439ef802bef328f6 (plain)
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
package binnie.craftgui.resource;

import binnie.core.resource.BinnieResource;
import binnie.craftgui.core.geometry.IArea;
import binnie.craftgui.core.geometry.IBorder;
import binnie.craftgui.core.geometry.Position;

public class Texture
{
  public static final Texture NULL = null;
  IArea area;
  IBorder padding = IBorder.ZERO;
  IBorder border = IBorder.ZERO;
  BinnieResource filename;
  
  public Texture(IArea area, BinnieResource filename)
  {
    this(area, IBorder.ZERO, IBorder.ZERO, filename);
  }
  
  public Texture(IArea area, IBorder padding, BinnieResource filename)
  {
    this(area, padding, IBorder.ZERO, filename);
  }
  
  public Texture(IArea area, IBorder padding, IBorder border, BinnieResource filename)
  {
    this.area = new IArea(area);
    this.padding = new IBorder(padding);
    this.border = new IBorder(border);
    this.filename = filename;
  }
  
  public IArea getArea()
  {
    return this.area;
  }
  
  public IBorder getPadding()
  {
    return this.padding;
  }
  
  public IBorder getBorder()
  {
    return this.border;
  }
  
  public BinnieResource getFilename()
  {
    return this.filename;
  }
  
  public IBorder getTotalPadding()
  {
    return this.padding.add(this.border);
  }
  
  public float w()
  {
    return getArea().w();
  }
  
  public float h()
  {
    return getArea().h();
  }
  
  public float u()
  {
    return getArea().x();
  }
  
  public float v()
  {
    return getArea().y();
  }
  
  public Texture crop(Position anchor, float dist)
  {
    return crop(new IBorder(anchor.opposite(), dist));
  }
  
  public Texture crop(IBorder crop)
  {
    Texture copy = new Texture(this.area, this.padding, this.border, this.filename);
    if (crop.b() > 0.0F)
    {
      copy.border.b(0.0F);
      copy.padding.b(copy.padding.b() - Math.min(crop.b(), copy.padding.b()));
      copy.area.h(copy.area.h() - crop.b());
    }
    if (crop.t() > 0.0F)
    {
      copy.border.t(0.0F);
      copy.padding.t(copy.padding.t() - Math.min(crop.t(), copy.padding.t()));
      copy.area.h(copy.area.h() - crop.t());
      copy.area.y(copy.area.y() + crop.t());
    }
    if (crop.r() > 0.0F)
    {
      copy.border.r(0.0F);
      copy.padding.r(copy.padding.r() - Math.min(crop.r(), copy.padding.r()));
      copy.area.w(copy.area.w() - crop.r());
    }
    if (crop.l() > 0.0F)
    {
      copy.border.l(0.0F);
      copy.padding.l(copy.padding.l() - Math.min(crop.l(), copy.padding.l()));
      copy.area.w(copy.area.w() - crop.l());
      copy.area.x(copy.area.x() + crop.l());
    }
    return copy;
  }
  
  public String toString()
  {
    String out = "Texture[";
    out = out + this.area.toString();
    if (!this.padding.isNonZero()) {
      out = out + " padding:" + this.padding.toString();
    }
    if (!this.border.isNonZero()) {
      out = out + " border:" + this.border.toString();
    }
    return out + "]";
  }
}