Spaces:
Sleeping
Sleeping
Commit
·
c443340
1
Parent(s):
b3b4bd2
basic working done except hatpic feedback
Browse files- client/lib/HomeScreen.dart +111 -48
client/lib/HomeScreen.dart
CHANGED
|
@@ -47,8 +47,10 @@ class HomeScreenState extends State<HomeScreen>{
|
|
| 47 |
String username="";
|
| 48 |
String userid="";
|
| 49 |
String profileColor="ff000000";
|
| 50 |
-
|
| 51 |
final double outsidepadding=15;
|
|
|
|
|
|
|
| 52 |
|
| 53 |
@override
|
| 54 |
void initState(){
|
|
@@ -62,24 +64,23 @@ class HomeScreenState extends State<HomeScreen>{
|
|
| 62 |
getPreferences();
|
| 63 |
// setPreferences();
|
| 64 |
connectWebsocket();
|
|
|
|
| 65 |
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
"x":
|
| 73 |
-
"y":
|
| 74 |
},
|
| 75 |
-
|
| 76 |
-
|
| 77 |
};
|
| 78 |
|
| 79 |
-
othersTouchPoints=
|
| 80 |
-
|
| 81 |
-
];
|
| 82 |
-
|
| 83 |
}
|
| 84 |
|
| 85 |
@override
|
|
@@ -106,7 +107,6 @@ class HomeScreenState extends State<HomeScreen>{
|
|
| 106 |
});
|
| 107 |
}
|
| 108 |
|
| 109 |
-
|
| 110 |
|
| 111 |
void connectWebsocket() async{
|
| 112 |
|
|
@@ -128,25 +128,9 @@ class HomeScreenState extends State<HomeScreen>{
|
|
| 128 |
};
|
| 129 |
channel.sink.add(json.encode(data));
|
| 130 |
|
| 131 |
-
|
| 132 |
-
// set username
|
| 133 |
-
data = {
|
| 134 |
-
"route": "set_username",
|
| 135 |
-
"username": prefs.getString("username"),
|
| 136 |
-
};
|
| 137 |
-
channel.sink.add(json.encode(data));
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
// // get rooms
|
| 142 |
-
// data = {
|
| 143 |
-
// "route": "get_rooms",
|
| 144 |
-
// };
|
| 145 |
-
// channel.sink.add(json.encode(data));
|
| 146 |
-
|
| 147 |
channel.stream.listen((message) {
|
| 148 |
// message.get
|
| 149 |
-
|
| 150 |
final String type;
|
| 151 |
try{
|
| 152 |
map = jsonDecode(message);
|
|
@@ -174,6 +158,9 @@ class HomeScreenState extends State<HomeScreen>{
|
|
| 174 |
}
|
| 175 |
else if(type=="set_username_response"){
|
| 176 |
print(map);
|
|
|
|
|
|
|
|
|
|
| 177 |
}
|
| 178 |
else if(type=="get_rooms_response"){
|
| 179 |
print(map);
|
|
@@ -192,13 +179,57 @@ class HomeScreenState extends State<HomeScreen>{
|
|
| 192 |
);
|
| 193 |
}
|
| 194 |
else if(type=="receive_touch"){
|
| 195 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 196 |
}
|
| 197 |
|
| 198 |
});
|
| 199 |
|
| 200 |
}
|
| 201 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 202 |
void enterRoom(Map roomdata){
|
| 203 |
setState((){
|
| 204 |
currRoomId=roomdata['roomId'];
|
|
@@ -368,7 +399,7 @@ class HomeScreenState extends State<HomeScreen>{
|
|
| 368 |
// runs every 1 second
|
| 369 |
void startSendTouch(){
|
| 370 |
sendTouchScheduler=Timer.periodic(
|
| 371 |
-
Duration(
|
| 372 |
// send touch to server
|
| 373 |
Map data;
|
| 374 |
|
|
@@ -394,15 +425,36 @@ class HomeScreenState extends State<HomeScreen>{
|
|
| 394 |
};
|
| 395 |
|
| 396 |
channel.sink.add(jsonEncode(data));
|
| 397 |
-
|
| 398 |
-
|
| 399 |
}
|
| 400 |
);
|
| 401 |
}
|
| 402 |
|
| 403 |
-
void stopSendTouch(){
|
| 404 |
sendTouchScheduler.cancel();
|
| 405 |
// send one last touch to tell touch is ended
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 406 |
|
| 407 |
}
|
| 408 |
|
|
@@ -417,7 +469,8 @@ class HomeScreenState extends State<HomeScreen>{
|
|
| 417 |
color=color.withOpacity(0.5);
|
| 418 |
return Visibility(
|
| 419 |
visible:visible,
|
| 420 |
-
child:
|
|
|
|
| 421 |
left:x-size/2,
|
| 422 |
top:y-size/2,
|
| 423 |
child: Container(
|
|
@@ -445,7 +498,7 @@ class HomeScreenState extends State<HomeScreen>{
|
|
| 445 |
// detect our touch point collision with any othersTouchPoints
|
| 446 |
// print(othersTouchPoints);
|
| 447 |
// print("listening");
|
| 448 |
-
for(Map map in othersTouchPoints){
|
| 449 |
|
| 450 |
// print("inside");
|
| 451 |
double dx = (map["position"]["x"]-x).abs();
|
|
@@ -478,9 +531,15 @@ class HomeScreenState extends State<HomeScreen>{
|
|
| 478 |
});
|
| 479 |
|
| 480 |
// start send Touch Scheduler
|
| 481 |
-
|
| 482 |
-
|
| 483 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 484 |
|
| 485 |
}
|
| 486 |
void detectTouchUpdate(DragUpdateDetails details){
|
|
@@ -516,10 +575,14 @@ class HomeScreenState extends State<HomeScreen>{
|
|
| 516 |
});
|
| 517 |
|
| 518 |
// stop send Touch Scheduler and send stop touch message
|
| 519 |
-
|
| 520 |
-
|
| 521 |
-
|
| 522 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 523 |
}
|
| 524 |
|
| 525 |
Widget mainSection(){
|
|
@@ -629,7 +692,7 @@ class HomeScreenState extends State<HomeScreen>{
|
|
| 629 |
|
| 630 |
|
| 631 |
// render received touch points
|
| 632 |
-
...othersTouchPoints.map((map){
|
| 633 |
return buildTouchPoint(
|
| 634 |
visible:true,
|
| 635 |
x:map['position']['x'],
|
|
|
|
| 47 |
String username="";
|
| 48 |
String userid="";
|
| 49 |
String profileColor="ff000000";
|
| 50 |
+
Map<String,Map> othersTouchPoints={};
|
| 51 |
final double outsidepadding=15;
|
| 52 |
+
final int touchRefreshRate = (1000/30).toInt(); // 30 fps
|
| 53 |
+
// final int touchRefreshRate = (1000/1).toInt(); // 30 fps
|
| 54 |
|
| 55 |
@override
|
| 56 |
void initState(){
|
|
|
|
| 64 |
getPreferences();
|
| 65 |
// setPreferences();
|
| 66 |
connectWebsocket();
|
| 67 |
+
setUsername();
|
| 68 |
|
| 69 |
+
Map data={
|
| 70 |
+
"user": {
|
| 71 |
+
"username": "anne",
|
| 72 |
+
"id": "string",
|
| 73 |
+
},
|
| 74 |
+
"position": {
|
| 75 |
+
"x": 200,
|
| 76 |
+
"y": 200,
|
| 77 |
},
|
| 78 |
+
"color": "ff2244bb", // Hex value.
|
| 79 |
+
"intensity": 1, // Vibration intensity.
|
| 80 |
};
|
| 81 |
|
| 82 |
+
othersTouchPoints[data['user']['id']]=data;
|
| 83 |
+
|
|
|
|
|
|
|
| 84 |
}
|
| 85 |
|
| 86 |
@override
|
|
|
|
| 107 |
});
|
| 108 |
}
|
| 109 |
|
|
|
|
| 110 |
|
| 111 |
void connectWebsocket() async{
|
| 112 |
|
|
|
|
| 128 |
};
|
| 129 |
channel.sink.add(json.encode(data));
|
| 130 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 131 |
channel.stream.listen((message) {
|
| 132 |
// message.get
|
| 133 |
+
Map map;
|
| 134 |
final String type;
|
| 135 |
try{
|
| 136 |
map = jsonDecode(message);
|
|
|
|
| 158 |
}
|
| 159 |
else if(type=="set_username_response"){
|
| 160 |
print(map);
|
| 161 |
+
setState(() {
|
| 162 |
+
userid=map['user']['id'];
|
| 163 |
+
});
|
| 164 |
}
|
| 165 |
else if(type=="get_rooms_response"){
|
| 166 |
print(map);
|
|
|
|
| 179 |
);
|
| 180 |
}
|
| 181 |
else if(type=="receive_touch"){
|
| 182 |
+
if(map['user']['id']==userid){
|
| 183 |
+
// ignore this
|
| 184 |
+
// print("self touch received. Ignore this");
|
| 185 |
+
return;
|
| 186 |
+
}
|
| 187 |
+
|
| 188 |
+
// touch received to update othersTouchPoints
|
| 189 |
+
// print(map);
|
| 190 |
+
|
| 191 |
+
if(map['type']=='enabled'){ // add touch point
|
| 192 |
+
// rescale point according to the screen
|
| 193 |
+
map['position']['x']*=width;
|
| 194 |
+
map['position']['y']*=height;
|
| 195 |
+
// print(map['position']['x'].toString());
|
| 196 |
+
// print(map['position']['y'].toString());
|
| 197 |
+
setState(() {
|
| 198 |
+
othersTouchPoints[map['id']]=map;
|
| 199 |
+
});
|
| 200 |
+
}
|
| 201 |
+
else{// delete the touch point
|
| 202 |
+
setState(() {
|
| 203 |
+
othersTouchPoints.remove(map['id']);
|
| 204 |
+
});
|
| 205 |
+
}
|
| 206 |
+
|
| 207 |
}
|
| 208 |
|
| 209 |
});
|
| 210 |
|
| 211 |
}
|
| 212 |
|
| 213 |
+
|
| 214 |
+
void setUsername() async{
|
| 215 |
+
prefs = await SharedPreferences.getInstance();
|
| 216 |
+
try{
|
| 217 |
+
await channel.ready;
|
| 218 |
+
}
|
| 219 |
+
catch(e){
|
| 220 |
+
print("Can't connect to the web socket server");
|
| 221 |
+
return;
|
| 222 |
+
}
|
| 223 |
+
|
| 224 |
+
// set username
|
| 225 |
+
Map data = {
|
| 226 |
+
"route": "set_username",
|
| 227 |
+
"username": prefs.getString("username"),
|
| 228 |
+
};
|
| 229 |
+
channel.sink.add(json.encode(data));
|
| 230 |
+
|
| 231 |
+
}
|
| 232 |
+
|
| 233 |
void enterRoom(Map roomdata){
|
| 234 |
setState((){
|
| 235 |
currRoomId=roomdata['roomId'];
|
|
|
|
| 399 |
// runs every 1 second
|
| 400 |
void startSendTouch(){
|
| 401 |
sendTouchScheduler=Timer.periodic(
|
| 402 |
+
Duration(milliseconds: touchRefreshRate), (timer) async {
|
| 403 |
// send touch to server
|
| 404 |
Map data;
|
| 405 |
|
|
|
|
| 425 |
};
|
| 426 |
|
| 427 |
channel.sink.add(jsonEncode(data));
|
|
|
|
|
|
|
| 428 |
}
|
| 429 |
);
|
| 430 |
}
|
| 431 |
|
| 432 |
+
void stopSendTouch() async{
|
| 433 |
sendTouchScheduler.cancel();
|
| 434 |
// send one last touch to tell touch is ended
|
| 435 |
+
Map data;
|
| 436 |
+
try{
|
| 437 |
+
await channel.ready;
|
| 438 |
+
}
|
| 439 |
+
catch(e){
|
| 440 |
+
print("Can't connect to the web socket server");
|
| 441 |
+
return;
|
| 442 |
+
}
|
| 443 |
+
data={
|
| 444 |
+
"route": "send_touch",
|
| 445 |
+
"id": 1, // Used to indentify vibrations for updating or disabling them
|
| 446 |
+
"type": "disabled", // Whether the vibration is active or not.
|
| 447 |
+
"position": {
|
| 448 |
+
// "x":20,
|
| 449 |
+
// "y":20,
|
| 450 |
+
"x": touchX/width, // send ratio x e.g. : x:700, width:800, then send 0.9
|
| 451 |
+
"y": touchY/height, // send ratio y e.g. : y:400, height:800 then send 0.5
|
| 452 |
+
},
|
| 453 |
+
"color": profileColor, // Hex value. Default: random
|
| 454 |
+
// "intensity"?: 1 // Vibration intensity. Default: 1
|
| 455 |
+
};
|
| 456 |
+
|
| 457 |
+
channel.sink.add(jsonEncode(data));
|
| 458 |
|
| 459 |
}
|
| 460 |
|
|
|
|
| 469 |
color=color.withOpacity(0.5);
|
| 470 |
return Visibility(
|
| 471 |
visible:visible,
|
| 472 |
+
child: AnimatedPositioned(
|
| 473 |
+
duration: Duration(milliseconds: touchRefreshRate),
|
| 474 |
left:x-size/2,
|
| 475 |
top:y-size/2,
|
| 476 |
child: Container(
|
|
|
|
| 498 |
// detect our touch point collision with any othersTouchPoints
|
| 499 |
// print(othersTouchPoints);
|
| 500 |
// print("listening");
|
| 501 |
+
for(Map map in othersTouchPoints.values){
|
| 502 |
|
| 503 |
// print("inside");
|
| 504 |
double dx = (map["position"]["x"]-x).abs();
|
|
|
|
| 531 |
});
|
| 532 |
|
| 533 |
// start send Touch Scheduler
|
| 534 |
+
if(currRoomId.isNotEmpty){
|
| 535 |
+
SchedulerBinding.instance.addPostFrameCallback((_) {
|
| 536 |
+
startSendTouch();
|
| 537 |
+
});
|
| 538 |
+
}
|
| 539 |
+
else{
|
| 540 |
+
// we are not joined in a room
|
| 541 |
+
// so can't send_touch
|
| 542 |
+
}
|
| 543 |
|
| 544 |
}
|
| 545 |
void detectTouchUpdate(DragUpdateDetails details){
|
|
|
|
| 575 |
});
|
| 576 |
|
| 577 |
// stop send Touch Scheduler and send stop touch message
|
| 578 |
+
if(currRoomId.isNotEmpty){
|
| 579 |
+
SchedulerBinding.instance.addPostFrameCallback((_) {
|
| 580 |
+
stopSendTouch();
|
| 581 |
+
});
|
| 582 |
+
}
|
| 583 |
+
else{
|
| 584 |
+
// can't send_touch end to server cuz we are not in a room
|
| 585 |
+
}
|
| 586 |
}
|
| 587 |
|
| 588 |
Widget mainSection(){
|
|
|
|
| 692 |
|
| 693 |
|
| 694 |
// render received touch points
|
| 695 |
+
...othersTouchPoints.values.map<Widget>((map){
|
| 696 |
return buildTouchPoint(
|
| 697 |
visible:true,
|
| 698 |
x:map['position']['x'],
|