82327c1d |
1 | package com.wurmonline.client.renderer.gui; |
2 | |
3 | import com.wurmonline.client.renderer.Matrix; |
4 | import com.wurmonline.client.renderer.PickData; |
5 | import com.wurmonline.client.renderer.backend.Primitive; |
6 | import com.wurmonline.client.renderer.backend.Queue; |
7 | import com.wurmonline.client.renderer.backend.VertexBuffer; |
8 | import java.nio.FloatBuffer; |
9 | |
10 | final class EspWCheckBox |
11 | extends FlexComponent |
12 | implements ConfirmListener { |
13 | private String label; |
14 | boolean checked = false; |
15 | boolean enabled = true; |
16 | private boolean needsConfirmOnTick = false; |
17 | private String confirmMessageOnTick; |
18 | private String confirmQuestionOnTick; |
19 | private boolean needsConfirmUnTick = false; |
20 | private String confirmMessageUnTick; |
21 | private String confirmQuestionUnTick; |
22 | private ConfirmWindow confirmWindow = null; |
23 | private String hoverString; |
24 | private float customR = 1.0f; |
25 | private float customG = 1.0f; |
26 | private float customB = 1.0f; |
27 | private static final VertexBuffer vbo = VertexBuffer.create((VertexBuffer.Usage)VertexBuffer.Usage.GUI, (int)12, (boolean)true, (boolean)false, (boolean)false, (boolean)false, (boolean)false, (int)0, (int)0, (boolean)false, (boolean)true); |
28 | private final Matrix modelMatrix; |
29 | private CheckBoxListener checkboxListener; |
30 | |
31 | EspWCheckBox(String label, CheckBoxListener checkboxListener) { |
32 | super("Checkbox " + label); |
33 | this.height = this.text.getHeight() + 1; |
34 | this.setLabel(label); |
35 | this.modelMatrix = Matrix.createIdentity(); |
36 | this.checkboxListener = checkboxListener; |
37 | } |
38 | |
39 | public void setLabel(String newLabel) { |
40 | this.label = newLabel; |
41 | this.setSize(this.text.getWidth(this.label) + 16, this.height); |
42 | } |
43 | |
44 | protected void renderComponent(Queue queue, float alpha) { |
45 | float colR = 0.8f; |
46 | float colG = 0.8f; |
47 | float colB = 0.8f; |
48 | if (this.enabled) { |
49 | colR = this.customR; |
50 | colG = this.customG; |
51 | colB = this.customB; |
52 | } |
53 | Primitive prim = queue.reservePrimitive(); |
54 | prim.type = Primitive.Type.LINES; |
55 | prim.num = this.checked ? 6 : 4; |
56 | prim.r = colR; |
57 | prim.g = colG; |
58 | prim.b = colB; |
59 | prim.a = 1.0f; |
60 | prim.texture[1] = null; |
61 | prim.texture[0] = null; |
62 | prim.texenv[0] = Primitive.TexEnv.MODULATE; |
63 | prim.vertex = vbo; |
64 | prim.index = null; |
65 | prim.clipRect = HeadsUpDisplay.scissor.getCurrent(); |
66 | int dy = (this.height - 8) / 2; |
67 | this.modelMatrix.setTranslation((float)this.x, (float)(this.y + dy), 0.0f); |
68 | queue.queue(prim, this.modelMatrix); |
69 | this.text.moveTo(this.x + this.height, this.y + this.text.getHeight()); |
70 | this.text.paint(queue, this.label, colR, colG, colB, 1.0f); |
71 | } |
72 | |
73 | protected void leftPressed(int xMouse, int yMouse, int clickCount) { |
74 | if (this.enabled && xMouse <= this.x + 16 && xMouse >= this.x && yMouse >= this.y && yMouse <= this.y + this.height) { |
75 | if (this.needsConfirmOnTick && !this.checked) { |
76 | this.confirmWindow = new ConfirmWindow((ConfirmListener)this, this.getConfirmMessageOnTick(), this.getConfirmQuestionOnTick()); |
77 | } else if (this.needsConfirmUnTick && this.checked) { |
78 | this.confirmWindow = new ConfirmWindow((ConfirmListener)this, this.getConfirmMessageUnTick(), this.getConfirmQuestionUnTick()); |
79 | } else { |
80 | this.checked = !this.checked; |
81 | } |
82 | this.checkboxListener.checkboxClicked(this); |
83 | } |
84 | } |
85 | |
86 | protected int getMouseCursor(int x, int y) { |
87 | if (this.enabled && x <= this.x + 16 && x >= this.x && y >= this.y && y <= this.y + this.height) { |
88 | return 1; |
89 | } |
90 | return super.getMouseCursor(x, y); |
91 | } |
92 | |
93 | public void pick(PickData pickData, int xMouse, int yMouse) { |
94 | if (this.hoverString != null) { |
95 | pickData.addText(this.hoverString); |
96 | } |
97 | } |
98 | |
99 | void setCustomColor(float r, float g, float b) { |
100 | this.customR = r; |
101 | this.customG = g; |
102 | this.customB = b; |
103 | } |
104 | |
105 | public void setHoverString(String description) { |
106 | this.hoverString = description; |
107 | } |
108 | |
109 | final void setConfirmOnTickMessage(String message) { |
110 | this.confirmMessageOnTick = message; |
111 | } |
112 | |
113 | final void setConfirmOnTickQuestion(String question) { |
114 | this.confirmQuestionOnTick = question; |
115 | this.needsConfirmOnTick = true; |
116 | } |
117 | |
118 | final void setConfirm(String messageOnTick, String questionOnTick, String messageUnTick, String questionUnTick) { |
119 | this.confirmMessageOnTick = messageOnTick; |
120 | this.confirmQuestionOnTick = questionOnTick; |
121 | this.confirmMessageUnTick = messageUnTick; |
122 | this.confirmQuestionUnTick = questionUnTick; |
123 | this.needsConfirmOnTick = questionOnTick.length() > 0; |
124 | this.needsConfirmUnTick = questionUnTick.length() > 0; |
125 | } |
126 | |
127 | public String getConfirmMessageOnTick() { |
128 | return this.confirmMessageOnTick; |
129 | } |
130 | |
131 | public String getConfirmQuestionOnTick() { |
132 | return this.confirmQuestionOnTick; |
133 | } |
134 | |
135 | public String getConfirmMessageUnTick() { |
136 | return this.confirmMessageUnTick; |
137 | } |
138 | |
139 | public String getConfirmQuestionUnTick() { |
140 | return this.confirmQuestionUnTick; |
141 | } |
142 | |
143 | public void closeConfirmWindow() { |
144 | if (this.confirmWindow != null) { |
145 | this.confirmWindow.close(); |
146 | this.confirmWindow = null; |
147 | } |
148 | } |
149 | |
150 | public void confirmed() { |
151 | this.closeConfirmWindow(); |
152 | this.checked = !this.checked; |
153 | } |
154 | |
155 | public void cancelled() { |
156 | this.closeConfirmWindow(); |
157 | } |
158 | |
159 | static { |
160 | FloatBuffer vertex = vbo.lock(); |
161 | vertex.put(4.0f); |
162 | vertex.put(0.0f); |
163 | vertex.put(0.0f); |
164 | vertex.put(13.0f); |
165 | vertex.put(0.0f); |
166 | vertex.put(0.0f); |
167 | vertex.put(3.0f); |
168 | vertex.put(8.0f); |
169 | vertex.put(0.0f); |
170 | vertex.put(13.0f); |
171 | vertex.put(8.0f); |
172 | vertex.put(0.0f); |
173 | vertex.put(4.0f); |
174 | vertex.put(0.0f); |
175 | vertex.put(0.0f); |
176 | vertex.put(4.0f); |
177 | vertex.put(8.0f); |
178 | vertex.put(0.0f); |
179 | vertex.put(13.0f); |
180 | vertex.put(0.0f); |
181 | vertex.put(0.0f); |
182 | vertex.put(13.0f); |
183 | vertex.put(8.0f); |
184 | vertex.put(0.0f); |
185 | vertex.put(6.0f); |
186 | vertex.put(2.0f); |
187 | vertex.put(0.0f); |
188 | vertex.put(11.0f); |
189 | vertex.put(7.0f); |
190 | vertex.put(0.0f); |
191 | vertex.put(11.0f); |
192 | vertex.put(2.0f); |
193 | vertex.put(0.0f); |
194 | vertex.put(6.0f); |
195 | vertex.put(7.0f); |
196 | vertex.put(0.0f); |
197 | vbo.unlock(); |
198 | } |
199 | } |
200 | |