Newer
Older
PureATN_M / src / main / java / cit / PureATN / PenUser.java
@motoki miura motoki miura on 2 Jun 2022 3 KB d
package cit.PureATN;

import java.awt.Color;
import java.awt.Graphics;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;

import org.piccolo2d.PNode;

public class PenUser {
	public static Hashtable<String, PenUser> penusers = new Hashtable<String, PenUser>();

	public static PenUser find(String penid) {
		penid = penid.trim();
		PenUser pu = penusers.get(penid);
		if (pu == null) {
			pu = new PenUser(penid, Note.theapp);
			penusers.put(penid, pu);
		}
		return pu;
	}

	public static void add(String penid, int x, int y) {
		PenUser pu = PenUser.find(penid);
		pu.add(x, y);
	}

	public static void press(String penid, int phase) {
		PenUser pu = PenUser.find(penid);
		pu.press(phase);
	}

	public static void drawAll(Graphics g) {
		for (PenUser pu : penusers.values()) {
			pu.draw(g);
		}
	}

	String penid;
	ArrayList<Stroke> strokes;
	Stroke lastStroke = null;
	ShortStroke squiggle = null; // for Note View
	transient Note parent;
	int num = 0;

	public PenUser(String pid, Note _parent) {
		parent = _parent;
		penid = pid;
		strokes = new ArrayList<Stroke>();
		// num = penusers.size();
		num = Integer.parseInt(pid.replace("pen", ""));
		// System.out.println("num = "+num+" "+pid);
	}

	public void add(int x, int y) {
		System.out.println(penid + " " + x + " " + y);
		if (lastStroke == null) {
			lastStroke = new Stroke(this);
			strokes.add(lastStroke);
		}
		if (squiggle == null) {
			squiggle = new ShortStroke();
			PNode layer = Note.theapp.getCanvas().getLayer();
			layer.addChild(squiggle);
			squiggle.startDrag_on_draw(x, y);
		} else {
			squiggle.drag_on_draw(x, y);
			PNode layer = Note.theapp.getCanvas().getLayer();
			layer.repaint();
		}
		lastStroke.add(x, y);
		Note.theapp.repaint();
	}

	public void press(int ph) {
		if (ph == 0) {
			if (lastStroke != null) {
				lastStroke.finish();
				// bundlesend(lastStroke);
				lastStroke = null;

				squiggle = null;
			}
		} else {
			// 最初のPress

		}
	}

	public void draw(Graphics g) {
		g.setColor(Color.getHSBColor((float) (num / 10.0f), 0.8f, 0.8f));
		int count = strokes.size();
		for (int i = 0; i < count; i++/* Stroke st: strokes */) {
			Stroke st = strokes.get(i);
			st.draw(g);
		}
	}

	public void send(int x, int y, int x2, int y2) {
		Map<String, Object> arg2;
		arg2 = new HashMap<String, Object>();
		arg2.put("penid", penid);
		// arg2.put("phase", drawphase012);
		arg2.put("rgb", getColorStringRGB());
		arg2.put("date", DPenReceiver.sdfdate.format(new Date()));
		arg2.put("time", DPenReceiver.sdftime.format(new Date()));
		arg2.put("epoch", System.currentTimeMillis());
		arg2.put("x1", x);
		arg2.put("y1", y);
		arg2.put("x2", x2);
		arg2.put("y2", y2);
		// parent.ddp.call("/points/insert", new Object[]{arg2});
	}

	public void bundlesend(Stroke st) {
		// Map<String,Object> arg2;
		// arg2 = new HashMap<String, Object>();
		// arg2.put("penid", penid);
		// arg2.put("stroke", st);
		// arg2.put("rgb", getColorStringRGB());
		// parent.ddp.call("/strokes/insert", new Object[]{arg2});

		// arg2.remove("stroke");
		// parent.ddp.call("pointsclear", new Object[]{arg2});

	}

	public String getColorStringRGB(){
		Color c = Color.getHSBColor((float)(num/11.0f), 0.8f, 0.8f);
		int r = c.getRed();
		int g = c.getGreen();
		int b = c.getBlue();
		return "rgb("+r+","+g+","+b+")";
	}
}