/
sideselectionwidget.scala
321 lines (263 loc) · 9.63 KB
/
sideselectionwidget.scala
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
package mrtjp.projectred.core.libmc.gui
import org.lwjgl.input.Mouse
import org.lwjgl.opengl.GL11
import mrtjp.projectred.core.libmc.{OpenGLLib, PRColors, MathLib, DirectionalRayTracer}
import net.minecraftforge.common.util.ForgeDirection
import scala.collection.JavaConversions
import scala.collection.mutable.ListBuffer
import mrtjp.projectred.core.libmc.DirectionalRayTracer.HitCoord
import net.minecraft.client.renderer.{RenderBlocks, Tessellator}
import codechicken.lib.render.{CCRenderState, Vertex5, CCModel}
import codechicken.lib.vec.Rotation
import net.minecraft.block.Block
import net.minecraft.client.renderer.texture.TextureMap
import net.minecraft.init.Blocks
import net.minecraft.tileentity.TileEntity
import net.minecraft.client.renderer.tileentity.TileEntityRendererDispatcher
import org.lwjgl.util.vector.{Vector3f, Matrix4f}
import codechicken.lib.math.MathHelper
import com.google.common.base.Preconditions
import codechicken.lib.gui.GuiDraw
class WidgetSideSelect(x:Int, y:Int, w:Int, h:Int, scale:Double) extends TWidget
{
override val bounds = new Rect().setMin(x, y).setWH(w, h)
private val rotHook = new SphereRotationHook(0, 40)
private var init = false
private var lastSideHovered = -1
private var ticksHeld = 0
var sides = 0
override def drawBack_Impl(mouse:Point, frame:Float)
{
if (!init || Mouse.isButtonDown(2))
{
rotHook.setTransform(getInitialRot)
init = true
}
GL11.glPushMatrix()
GL11.glTranslated(x+(scale/2), y+(scale/2), scale)
GL11.glScaled(scale, -scale, scale)
rotHook.update(mouse.x-bWidth, -(mouse.y-bHeight), bounds.intersects(mouse))
draw()
val coord = new DirectionalRayTracer(0.5).getNearestHit
if (coord != null) onSideHitByCursor(coord)
lastSideHovered = if (coord!=null && coord.side!=ForgeDirection.UNKNOWN) coord.side.ordinal() else -1
GL11.glPopMatrix()
}
override def drawFront_Impl(mouse:Point, frame:Float)
{
val list = ListBuffer[String]()
buildToolTip(list, lastSideHovered)
GuiDraw.drawMultilineTip(mouse.x+12, mouse.y-12, JavaConversions.bufferAsJavaList(list))
}
override def mouseClicked_Impl(p:Point, button:Int, consumed:Boolean) =
{
ticksHeld = 0
lastSideHovered = -1
bounds.intersects(p)
}
override def mouseReleased_Impl(p:Point, button:Int, consumed:Boolean) =
{
if (ticksHeld < 5 && lastSideHovered >= 0)
{
onSideClicked(lastSideHovered, button)
ticksHeld = 5
}
true
}
override def mouseDragged_Impl(p:Point, button:Int, time:Long, consumed:Boolean) =
{
ticksHeld += 1
true
}
def getInitialRot = MathLib.createEntityRotateMatrix(mcInst.renderViewEntity)
def onSideHitByCursor(hit:HitCoord){}
def draw(){}
def onSideClicked(s:Int, button:Int){}
def buildToolTip(list:ListBuffer[String], hoverSide:Int){}
}
trait TWidgetSidePicker extends WidgetSideSelect
{
private var exclusiveSide = false
def setExclusive(flag:Boolean):this.type = {exclusiveSide = flag; this}
abstract override def onSideClicked(s:Int, button:Int)
{
super.onSideClicked(s, button)
toggleSide(s)
}
def toggleSide(side:Int)
{
val old = sides
sides ^= 1<<side
if (exclusiveSide) sides &= 1<<side
if (old != sides) onSideChanged(side)
}
def clearSides()
{
val old = sides
sides = 0
if (old != sides) onSideChanged(-1)
}
def sideMask = sides
def setSideMask(mask:Int):this.type = {sides = mask; this}
def onSideChanged(s:Int){}
}
trait TWidgetSideHighlight extends WidgetSideSelect
{
private var color = PRColors.LIME.rgba
private var activeHighlight = false
def setColor(c:Int):this.type = {color = c; this}
def setSideHighlighting(flag:Boolean):this.type = {activeHighlight = flag; this}
abstract override def onSideHitByCursor(hit:HitCoord)
{
super.onSideHitByCursor(hit)
renderHighlight(hit.side.ordinal())
}
abstract override def draw()
{
super.draw()
if (activeHighlight)
for (s <- 0 until 6) if ((sides&1<<s) != 0) renderHighlight(s)
}
private def renderHighlight(side:Int)
{
GL11.glDisable(GL11.GL_LIGHTING)
GL11.glEnable(GL11.GL_BLEND)
GL11.glDisable(GL11.GL_DEPTH_TEST)
GL11.glDisable(GL11.GL_TEXTURE_2D)
GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA)
CCRenderState.reset()
CCRenderState.pullLightmap()
CCRenderState.baseColour = color
CCRenderState.alphaOverride = 64
CCRenderState.startDrawing()
TWidgetSideHighlight.highlights(side).render()
CCRenderState.draw()
GL11.glEnable(GL11.GL_DEPTH_TEST)
GL11.glEnable(GL11.GL_TEXTURE_2D)
GL11.glDisable(GL11.GL_BLEND)
}
}
object TWidgetSideHighlight
{
private def genHighlightModel()
{
val model = CCModel.quadModel(4)
model.verts(0) = new Vertex5(-0.5, -0.5, -0.5, 0, 0)
model.verts(1) = new Vertex5(0.5, -0.5, -0.5, 0, 0)
model.verts(2) = new Vertex5(0.5, -0.5, 0.5, 0, 0)
model.verts(3) = new Vertex5(-0.5, -0.5, 0.5, 0, 0)
highlights = new Array[CCModel](6)
highlights(0) = model
for (s <- 1 until 6)
highlights(s) = model.copy.apply(Rotation.sideRotations(s))
highlights.foreach(_.computeNormals())
}
var highlights:Array[CCModel] = _
genHighlightModel()
val renderBlocks = new RenderBlocks
}
trait TWidgetSideBlock extends WidgetSideSelect
{
var block:Block = null
var meta = 0
def setBlock(b:Block, md:Int):this.type = {block = b; meta = md; this}
abstract override def draw()
{
super.draw()
val t = Tessellator.instance
val renderBlocks = TWidgetSideBlock.renderBlocks
GL11.glColor4f(1, 1, 1, 1)
renderEngine.bindTexture(TextureMap.locationBlocksTexture)
renderBlocks.setRenderBounds(0, 0, 0, 1, 1, 1)
t.startDrawingQuads()
renderBlocks.renderFaceXNeg(Blocks.stone, -0.5, -0.5, -0.5, block.getIcon(4, meta))
renderBlocks.renderFaceXPos(Blocks.stone, -0.5, -0.5, -0.5, block.getIcon(5, meta))
renderBlocks.renderFaceYPos(Blocks.stone, -0.5, -0.5, -0.5, block.getIcon(1, meta))
renderBlocks.renderFaceYNeg(Blocks.stone, -0.5, -0.5, -0.5, block.getIcon(0, meta))
renderBlocks.renderFaceZNeg(Blocks.stone, -0.5, -0.5, -0.5, block.getIcon(2, meta))
renderBlocks.renderFaceZPos(Blocks.stone, -0.5, -0.5, -0.5, block.getIcon(3, meta))
t.draw
}
}
object TWidgetSideBlock
{
val renderBlocks = new RenderBlocks
}
trait TWidgetSideTE extends WidgetSideSelect
{
var tile:TileEntity = null
def setTile(te:TileEntity):this.type = {tile = te; this}
abstract override def draw()
{
super.draw()
TileEntityRendererDispatcher.instance.renderTileEntityAt(tile, -0.5, -0.5, -0.5, 0)
}
}
trait TWidgetSideStaticRotation extends WidgetSideSelect
{
override def getInitialRot =
{
val initial = new Matrix4f
initial.rotate((20*MathHelper.torad).asInstanceOf[Float], new Vector3f(1, 0, 0))
initial.rotate((30*MathHelper.torad).asInstanceOf[Float], new Vector3f(0, 1, 0))
initial
}
}
class JWidgetSideSelect(x:Int, y:Int, width:Int, height:Int, scale:Double)
extends WidgetSideSelect(x, y, width, height, scale) with TWidgetSideTE with TWidgetSideHighlight with TWidgetSidePicker
class SphericalRotation
{
private var dragStart:Vector3f = null
private var lastTransform = new Matrix4f
def setTransformation(t:Matrix4f){lastTransform = t}
private def calculateSpherePoint(x:Float, y:Float) =
{
val result = new Vector3f(x, y, 0)
val sqrZ = 1-Vector3f.dot(result, result)
if (sqrZ > 0) result.z = Math.sqrt(sqrZ).asInstanceOf[Float]
else result.normalise()
result
}
private def getTransform(mouseX:Float, mouseY:Float):Matrix4f =
{
Preconditions.checkNotNull(dragStart, "", "Draging not started")
val current = calculateSpherePoint(mouseX, mouseY)
val dot = Vector3f.dot(dragStart, current)
if (Math.abs(dot - 1) < 0.0001) return lastTransform
val axis = Vector3f.cross(dragStart, current, null)
axis.normalise()
val angle = 2*Math.acos(dot).asInstanceOf[Float]
val rotation = new Matrix4f
rotation.rotate(angle, axis)
Matrix4f.mul(rotation, lastTransform, null)
}
def applyTransform(mouseX:Float, mouseY:Float, isDragging:Boolean)
{
OpenGLLib.loadMatrix(if (isDragging) getTransform(mouseX, mouseY) else lastTransform)
}
def startDrag(mouseX:Float, mouseY:Float){dragStart = calculateSpherePoint(mouseX, mouseY)}
def endDrag(mouseX:Float, mouseY:Float){lastTransform = getTransform(mouseX, mouseY)}
}
class SphereRotationHook(mouseButton:Int, radius:Int)
{
private final val target = new SphericalRotation
private var isDragging = false
def update(mouseX:Int, mouseY:Int, drag:Boolean)
{
val mx = mouseX/radius
val my = mouseY/radius
val buttonState = Mouse.isButtonDown(mouseButton)
if (!isDragging && buttonState && drag)
{
isDragging = true
target.startDrag(mx, my)
}
else if (isDragging && !buttonState)
{
isDragging = false
target.endDrag(mx, my)
}
target.applyTransform(mx, my, isDragging)
}
def setTransform(t:Matrix4f){target.setTransformation(t)}
}