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(); // final _touchData$ = BehaviorSubject(); // Touch Data from the input String final _touchData$ = PublishSubject(); // Touch Data from the input String final _touchVisualise$ = BehaviorSubject>(); final _touchDataHistory$ = BehaviorSubject>(); //All touches // Controller Input of Data final _inputTouchDataController = StreamController(); // Controller for outputting //SINKS (into bloc) Sink get inputTouchData => _inputTouchDataController.sink; //STREAMS (output bloc) Stream get outputTouchData$ => _touchData$.stream; //-> Stream> get outputVisualise$ => _touchVisualise$.stream; //-> Stream> get outputHistory$ => _touchDataHistory$.stream; //-> void dispose() { _inputTouchDataController.close(); _touchData$.close(); _touchVisualise$.close(); _touchDataHistory$.close(); // _touchDataHistory$.close(); } static List _colorOfTouches = [ Colors.blue, Colors.red, Colors.orange, Colors.white, Colors.yellow, ]; static List _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 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!!!!!!!!!!!!"); } } } }