Permalink
Browse files

drag and pinch to window test

  • Loading branch information...
1 parent 23bd12d commit 0f07164c686eef689c0cf74c58bbf1a27d050bce @napthats committed Nov 10, 2012
Showing with 62 additions and 20 deletions.
  1. +62 −20 src/main/scala/BoardView.scala
@@ -17,54 +17,96 @@ import _root_.org.metalev.multitouch.controller.MultiTouchController.PointInfo;
import _root_.org.metalev.multitouch.controller.MultiTouchController.PositionAndScale;
-class Page(var x: Int, var y: Int, var size: Int, val color: Int, title: String) {
-}
+class Page(var x: Double, var y: Double, var size: Double, val color: Int, title: String) {
+ def dragAndPinch(pos_scale: PositionAndScale) {
+ val pos = BoardView.window2WorldPosition(pos_scale.getXOff, pos_scale.getYOff)
+ this.x = pos.x
+ this.y = pos.y
+ this.size = pos_scale.getScale * 100
+ }
+}
-class BoardView(context: Context, attrs:AttributeSet) extends View(context, attrs) with MultiTouchObjectCanvas[Page]{
-/*
- var prev_touch_x = 0
- var prev_touch_y = 0
-*/
- var page = new Page(200, 100, 100, Color.RED, "test")
- var page_alt = new Page(400, 400, 100, Color.BLUE, "test_alt")
+object BoardView {
+ var base_x = 0.0
+ var base_y = 0.0
+ var base_scale = 1.0
+
+ def window2WorldPosition(x_orig: Double, y_orig: Double): {val x: Double; val y: Double} = {
+ new {val x = (x_orig / base_scale) + base_x; val y = (y_orig / base_scale) + base_y}
+ }
+
+ def world2WindowPosition(x_orig: Double, y_orig: Double): {val x: Double; val y: Double} = {
+ new {val x = (x_orig - base_x) * base_scale; val y = (y_orig - base_y) * base_scale}
+ }
+}
+
+
+class BoardView(context: Context, attrs:AttributeSet) extends View(context, attrs) with MultiTouchObjectCanvas[Page]{
val multiTouchController = new MultiTouchController[Page](this)
+ var page = new Page(200.0, 100.0, 100.0, Color.RED, "test")
+ var page_alt = new Page(400.0, 400.0, 100.0, Color.BLUE, "test_alt")
+ var page_dummy = new Page(0.0,0.0,100.0,Color.RED,"") {
+ override def dragAndPinch(pos_scale: PositionAndScale) {
+ super.dragAndPinch(pos_scale)
+ val pos = BoardView.window2WorldPosition(pos_scale.getXOff, pos_scale.getYOff)
+ BoardView.base_x = -x
+ BoardView.base_y = -y
+ BoardView.base_scale = size / 100.0
+ }
+ }
+
+ //for Canvas#drawRect
+ private implicit def double2Float(x: Double): Float = x.toFloat
+
override def onDraw(canvas: Canvas) {
super.onDraw(canvas)
val paint = new Paint()
paint.setColor(page.color)
- canvas.drawRect(page.x, page.y, page.x + page.size, page.y + page.size, paint)
+ canvas.drawRect(
+ (page.x - BoardView.base_x) * BoardView.base_scale,
+ (page.y - BoardView.base_y) * BoardView.base_scale,
+ (page.x + page.size - BoardView.base_x) * BoardView.base_scale,
+ (page.y + page.size - BoardView.base_y) * BoardView.base_scale,
+ paint
+ )
paint.setColor(page_alt.color)
- canvas.drawRect(page_alt.x, page_alt.y, page_alt.x + page_alt.size, page_alt.y + page_alt.size, paint)
+ canvas.drawRect(
+ (page_alt.x - BoardView.base_x) * BoardView.base_scale,
+ (page_alt.y - BoardView.base_y) * BoardView.base_scale,
+ (page_alt.x + page_alt.size - BoardView.base_x) * BoardView.base_scale,
+ (page_alt.y + page_alt.size - BoardView.base_y) * BoardView.base_scale,
+ paint
+ )
}
override def selectObject(page: Page, point: PointInfo) {
Toast.makeText(context, page.color.toString, Toast.LENGTH_SHORT).show()
}
override def setPositionAndScale(page: Page, pos_scale: PositionAndScale, point: PointInfo): Boolean = {
- page.x = pos_scale.getXOff.toInt
- page.y = pos_scale.getYOff.toInt
- page.size = (pos_scale.getScale * 100).toInt
+ page.dragAndPinch(pos_scale)
invalidate()
return true
}
override def getPositionAndScale(page: Page, pos_scale: PositionAndScale) {
- pos_scale.set(page.x, page.y, true, page.size/100, false, page.size/100, page.size/100, false, 0)
+ val pos = BoardView.world2WindowPosition(page.x, page.y)
+ pos_scale.set(pos.x, pos.y, true, page.size/100, false, page.size/100, page.size/100, false, 0)
}
override def getDraggableObjectAtPoint(point: PointInfo): Page = {
- if (point.getX >= page.x && point.getX <= page.x + page.size &&
- point.getY >= page.y && point.getY <= page.y + page.size) return page
- else if (point.getX >= page_alt.x && point.getX <= page_alt.x + page_alt.size &&
- point.getY >= page_alt.y && point.getY <= page_alt.y + page_alt.size) return page_alt
- return null
+ val pos = BoardView.window2WorldPosition(point.getX, point.getY)
+ if (pos.x >= page.x && pos.x <= page.x + page.size &&
+ pos.y >= page.y && pos.y <= page.y + page.size) return page
+ else if (pos.x >= page_alt.x && pos.x <= page_alt.x + page_alt.size &&
+ pos.y >= page_alt.y && pos.y <= page_alt.y + page_alt.size) return page_alt
+ return page_dummy
}
override def onTouchEvent(event: MotionEvent): Boolean = multiTouchController.onTouchEvent(event)

0 comments on commit 0f07164

Please sign in to comment.