123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104 |
- import 'dart:async';
- import 'dart:ui';
- import 'package:flutter/material.dart';
-
- import 'package:rxdart/rxdart.dart';
- import 'package:rxdart/subjects.dart';
- import 'package:touch_demonstrator/model/touchData.dart';
-
- class TouchDataBloc {
- final int xMax = 1200;
- String inputString = "";
- final history = List<TouchData>();
-
- // final _touchData$ = BehaviorSubject<TouchData>(); // Touch Data from the input String
- final _touchData$ = PublishSubject<TouchData>(); // Touch Data from the input String
- final _touchVisualise$ = BehaviorSubject<Map<TouchData, Color>>();
- final _touchDataHistory$ = BehaviorSubject<List<TouchData>>(); //All touches
-
- // Controller Input of Data
- final _inputTouchDataController = StreamController<String>();
- // Controller for outputting
-
- //SINKS (into bloc)
- Sink<String> get inputTouchData => _inputTouchDataController.sink;
- //STREAMS (output bloc)
- Stream<TouchData> get outputTouchData$ => _touchData$.stream; //->
- Stream<Map<TouchData,Color>> get outputVisualise$ => _touchVisualise$.stream; //->
- Stream<List<TouchData>> get outputHistory$ => _touchDataHistory$.stream; //->
-
- void dispose() {
- _inputTouchDataController.close();
- _touchData$.close();
- _touchVisualise$.close();
- _touchDataHistory$.close();
- // _touchDataHistory$.close();
- }
-
- static List<Color> _colorOfTouches = [
- Colors.blue,
- Colors.red,
- Colors.orange,
- Colors.white,
- Colors.yellow,
- ];
-
- static List<TouchData> _lastFingerTouch = [
- TouchData(5, 0, 1200, 1200),
- TouchData(5, 1, 1200, 1200),
- TouchData(5, 2, 1200, 1200),
- TouchData(5, 3, 1200, 1200),
- TouchData(5, 4, 1200, 1200),
- ];
-
- Map<TouchData, Color> touchCollection = {
- _lastFingerTouch[0]: _colorOfTouches[0],
- _lastFingerTouch[1]: _colorOfTouches[1],
- _lastFingerTouch[2]: _colorOfTouches[2],
- _lastFingerTouch[3]: _colorOfTouches[3],
- _lastFingerTouch[4]: _colorOfTouches[4],
- };
-
- TouchDataBloc() {
- print('init: Touch Data BLOC');
- _inputTouchDataController.stream.listen(_testTouchEventData);
- }
-
- void _updateStreams(TouchData t){
- /* print("<- Update Streams (${t.e}|${t.f}|${t.x.toString().padLeft(4,'0')}|"
- "${t.y.toString().padLeft(4,'0')})");*/
- _lastFingerTouch[t.fingerNumber].touchEvent(t.event, t.x, t.y);
- _touchData$.add(t); // Stream of single touches
- _touchVisualise$.add(touchCollection);
- //new:
- history.add(t);
- _touchDataHistory$.add(history);
- }
-
- void _testTouchEventData(String i) {
- print('_testTouchEventData');
- TouchData t = TouchData(0, 0, 0, 0);
- inputString = i;
- // print("input: $inputString");
- RegExp regExp = new RegExp(
- r"\(\d{1}\|\d{1}\|\d{4}\|\d{4}\)",
- caseSensitive: false,
- multiLine: false,
- );
- var matches = regExp.allMatches(inputString);
- for (var m in matches) {
- // print('${m.group(0)}');
- t.event = int.tryParse(m.group(0)[1]);
- t.fingerNumber = int.tryParse(m.group(0)[3]);
- t.x = int.tryParse(m.group(0).substring(5, 9));
- t.y = int.tryParse(m.group(0).substring(10, 14));
- if (t.x < 1024 && t.y < 1024 && (t.fingerNumber <=4) && (t.event==1 || t.event==4 || t.event==5)){
- _updateStreams(t);
- print('straem update');
- }
- else{
- print("Touch not Added!!!!!!!!!!!!");
- }
- }
- }
- }
|