Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Improved scrollview to allow scrolling in ServerSelectScreen.

  • Loading branch information...
commit aa304429fd59e688a8f1b6b39c7f2785e309777c 1 parent faf5854
@greywhind greywhind authored
View
60 engineSrc/bbth/engine/ui/UIScrollView.java
@@ -5,6 +5,7 @@
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Region;
+import android.util.Log;
import bbth.engine.util.MathUtils;
public class UIScrollView extends UIView {
@@ -14,6 +15,8 @@
protected RectF _content_bounds, _v_scroll_handle_rect, _v_track_rect, _h_track_rect, _h_scroll_handle_rect;
protected Paint _scroll_paint, _track_paint;
+ private static final float MIN_SCROLLING_SWIPE_DIST = 900.0f;
+
private float CORNER_RADIUS = 5, TRACK_THICKNESS = 8, SPACE_BETWEEN_TRACKS = 4, MIN_SCROLL_VELOCITY = 10, SCROLL_DECELERATION = -500f;
public UIScrollView(Object tag) {
@@ -86,29 +89,21 @@ public void onDraw(Canvas canvas) {
@Override
public void onTouchDown(float x, float y) {
- boolean ignore = false;
_vy = 0;
_vx = 0;
+ _x = x;
+ _y = y;
+
int idx = subviewCount;
while(idx-- > 0)
{
UIView e = subviews.get(idx);
if(e.containsPoint(x+pos_x, y+pos_y) && !e.isDraggable())
{
- ignore = true;
e.onTouchDown(x+pos_x, y+pos_y);
}
}
-
- if(!ignore)
- {
- isDown = true;
- isScrolling = true;
- _x = x;
- _y = y;
- _iy = y;
- }
}
@Override
@@ -124,6 +119,24 @@ public void onTouchUp(float x, float y) {
@Override
public void onTouchMove(float x, float y) {
+ dx = x - _x;
+ dy = y - _y;
+
+ if (dx * dx + dy * dy > MIN_SCROLLING_SWIPE_DIST)
+ {
+ isDown = true;
+ isScrolling = true;
+ _iy = y;
+
+ int idx = subviewCount;
+ while(idx-- > 0)
+ {
+ UIView e = subviews.get(idx);
+ // XXX: HACK TO TELL OBJECT IT'S NO LONGER TOUCHED. SHOULD BE IMPROVED.
+ e.onTouchMove(e._position.x + e._width + 1, e._position.y + e._height + 1);
+ }
+ }
+
if(isDown && scrollEnabled)
{
if(scrollsVertical && max_y > 0)
@@ -148,6 +161,12 @@ public void onTouchMove(float x, float y) {
@Override
public void addSubview(UIView view)
{
+ int i = 0;
+ _content_bounds.setEmpty();
+ for (i = 0; i < subviews.size(); i++)
+ {
+ _content_bounds.union(subviews.get(i)._rect);
+ }
_content_bounds.union(view._rect);
_v_scroll_handle_rect.bottom = _v_scroll_handle_rect.top + getVerticalHandleHeight();
@@ -158,6 +177,25 @@ public void addSubview(UIView view)
super.addSubview(view);
}
+
+ @Override
+ public void removeSubview(UIView view)
+ {
+ super.removeSubview(view);
+
+ int i = 0;
+ _content_bounds.setEmpty();
+ for (i = 0; i < subviews.size(); i++)
+ {
+ _content_bounds.union(subviews.get(i)._rect);
+ }
+
+ _v_scroll_handle_rect.bottom = _v_scroll_handle_rect.top + getVerticalHandleHeight();
+ _h_scroll_handle_rect.right = _h_scroll_handle_rect.left + getHorizontalHandleWidth();
+
+ max_offset_y = _v_track_rect.height() - _v_scroll_handle_rect.height();
+ max_offset_x = _h_track_rect.width() - _h_scroll_handle_rect.width();
+ }
@Override
public void setBounds(float left, float top, float right, float bottom) {
View
19 gameSrc/bbth/game/ServerSelectScreen.java
@@ -5,6 +5,7 @@
import android.bluetooth.BluetoothDevice;
import android.graphics.Paint.Align;
+import android.util.Log;
import bbth.engine.net.bluetooth.Bluetooth;
import bbth.engine.net.bluetooth.State;
import bbth.engine.net.simulation.LockStepProtocol;
@@ -34,7 +35,7 @@ public ServerSelectScreen(UINavigationController controller, LockStepProtocol pr
// UIScrollView needs to be tamed for adding and removing
// content, but without scrolling we support up to nine
// nearby devices :)
- setScrollsVertical(false);
+ //setScrollsVertical(false);
this.controller = controller;
this.protocol = protocol;
@@ -73,16 +74,25 @@ public void onUpdate(float seconds) {
List<BluetoothDevice> devices = bluetooth.getDevices();
while (lastCount < devices.size()) {
BluetoothDevice device = devices.get(lastCount);
+ if (device == null || device.getName() == null) {
+ Log.d("Error", "Wtf, device name is null?");
+ continue;
+ }
UIButton button = new UIButton(device.getName());
- button.setPosition(75, BBTHGame.CONTENT_TOP + 25 + 65 + lastCount * 40);
- button.setSize(BBTHGame.WIDTH - 150, 30);
+ button.setAnchor(Anchor.TOP_CENTER);
+ button.setPosition(BBTHGame.WIDTH/2, BBTHGame.CONTENT_TOP + 25 + 65 + lastCount * 50);
+ button.setSize(BBTHGame.WIDTH * 0.75f, 40);
button.setButtonDelegate(this);
button.tag = lastCount;
button.isDisabled = false;
buttons.add(button);
addSubview(button);
+ layoutSubviews(false);
lastCount++;
}
+
+ // Disable the refresh button while refreshing.
+ refreshButton.isDisabled = bluetooth.getState() == State.GET_NEARBY_DEVICES;
// Update the current status label
String statusMessage = bluetooth.getString();
@@ -96,6 +106,8 @@ public void onUpdate(float seconds) {
controller.pushUnder(new InGameScreen(controller, Team.CLIENT, bluetooth, null, protocol, false));
controller.pop(BBTHGame.FROM_RIGHT_TRANSITION);
}
+
+ super.onUpdate(seconds);
}
@Override
@@ -110,6 +122,7 @@ public void onClick(UIButton sender) {
for (UIButton button : buttons) {
removeSubview(button);
}
+ layoutSubviews(false);
buttons.clear();
lastCount = 0;
} else {
Please sign in to comment.
Something went wrong with that request. Please try again.