diff --git a/.gradle/8.0/executionHistory/executionHistory.lock b/.gradle/8.0/executionHistory/executionHistory.lock index 2e7a9ef..a5cc10d 100644 Binary files a/.gradle/8.0/executionHistory/executionHistory.lock and b/.gradle/8.0/executionHistory/executionHistory.lock differ diff --git a/.gradle/8.0/fileHashes/fileHashes.bin b/.gradle/8.0/fileHashes/fileHashes.bin index 89108dd..4cc16ed 100644 Binary files a/.gradle/8.0/fileHashes/fileHashes.bin and b/.gradle/8.0/fileHashes/fileHashes.bin differ diff --git a/.gradle/8.0/fileHashes/fileHashes.lock b/.gradle/8.0/fileHashes/fileHashes.lock index 1f9c009..45dcd58 100644 Binary files a/.gradle/8.0/fileHashes/fileHashes.lock and b/.gradle/8.0/fileHashes/fileHashes.lock differ diff --git a/.gradle/buildOutputCleanup/buildOutputCleanup.lock b/.gradle/buildOutputCleanup/buildOutputCleanup.lock index a5fb0f6..3d428fb 100644 Binary files a/.gradle/buildOutputCleanup/buildOutputCleanup.lock and b/.gradle/buildOutputCleanup/buildOutputCleanup.lock differ diff --git a/app/src/main/java/com/muqingedit/ConnectionManager.java b/app/src/main/java/com/muqingedit/ConnectionManager.java new file mode 100644 index 0000000..3de91b5 --- /dev/null +++ b/app/src/main/java/com/muqingedit/ConnectionManager.java @@ -0,0 +1,48 @@ +package com.muqingedit; + +import java.util.HashSet; +import java.util.Set; + +/** + * ConnectionManager 类用于管理视图之间的连接关系。 + */ +public class ConnectionManager { + private static Set connections = new HashSet<>(); + /** + * ConnectionManager 构造函数,初始化连接集合。 + */ + /** + * 添加视图之间的连接。 + * + * @param view1Id 第一个视图的ID + * @param view2Id 第二个视图的ID + */ + public static void addConnection(String view1Id, String view2Id) { + String connection = getConnectionKey(view1Id, view2Id); + connections.add(connection); + } + + /** + * 检查两个视图是否已连接。 + * + * @param view1Id 第一个视图的ID + * @param view2Id 第二个视图的ID + * @return 如果两个视图已连接,则返回 true;否则返回 false。 + */ + public static boolean isConnected(String view1Id, String view2Id) { + String connection = getConnectionKey(view1Id, view2Id); + return connections.contains(connection); + } + + /** + * 获取视图连接的唯一标识。 + * + * @param view1Id 第一个视图的ID + * @param view2Id 第二个视图的ID + * @return 视图连接的唯一标识 + */ + private static String getConnectionKey(String view1Id, String view2Id) { + // 这里使用视图 ID 作为连接信息的关键 + return view1Id + "-" + view2Id; + } +} diff --git a/app/src/main/java/com/muqingedit/activity/Edit.java b/app/src/main/java/com/muqingedit/activity/Edit.java index b3369be..09a5fa3 100644 --- a/app/src/main/java/com/muqingedit/activity/Edit.java +++ b/app/src/main/java/com/muqingedit/activity/Edit.java @@ -1,34 +1,49 @@ package com.muqingedit.activity; import android.annotation.SuppressLint; -import android.content.Context; import android.os.Bundle; -import android.view.View; +import android.view.ViewTreeObserver; import android.widget.FrameLayout; import androidx.annotation.Nullable; +import com.muqingedit.ConnectionManager; import com.muqingedit.R; import com.muqingedit.fanjian; -import com.muqingedit.xian; +import com.muqingedit.gj; + +import java.util.ArrayList; +import java.util.List; public class Edit extends ActivityFragment { - @SuppressLint("StaticFieldLeak") - public FrameLayout frameLayout; - + public FrameLayout frameLayout;//后续需要添加fragment的移动和缩放 + public static List list_fanjian = new ArrayList<>(); + @SuppressLint("ClickableViewAccessibility") @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.edit); frameLayout = findViewById(R.id.view); //绘制一个房间 - findViewById(R.id.button).setOnClickListener(view -> { - fanjian button = new fanjian(Edit.this).setPaletteView(frameLayout) - .setLayoutParams(260, 250) - .setXY(200, 100); - frameLayout.addView(button); + findViewById(R.id.button).setOnClickListener(view -> new fanjian(frameLayout)); + frameLayout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { + @Override + public void onGlobalLayout() { + // 在布局完成之后获取 FrameLayout 的高度 + int height = frameLayout.getHeight(); + int width = frameLayout.getWidth(); + + fanjian main = new fanjian(frameLayout, "main");//设置主房间 + int viewWidth = main.getMeasuredWidth(); + int viewHeight = main.getMeasuredHeight(); + float x = (width - viewWidth) / 2.0f; + float y = (height - viewHeight) / 2.0f; + gj.sc(height + ":" + y); + main.setXY((int) x, (int) y); + frameLayout.addView(main); + // 如果只需要获取一次高度,可以在获取之后移除监听器 + frameLayout.getViewTreeObserver().removeOnGlobalLayoutListener(this); + } }); } - - -} +} \ No newline at end of file diff --git a/app/src/main/java/com/muqingedit/fanjian.java b/app/src/main/java/com/muqingedit/fanjian.java index 637484a..a30f967 100644 --- a/app/src/main/java/com/muqingedit/fanjian.java +++ b/app/src/main/java/com/muqingedit/fanjian.java @@ -5,38 +5,61 @@ import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; +import android.text.TextUtils; import android.view.GestureDetector; import android.view.Gravity; +import android.view.LayoutInflater; import android.view.MotionEvent; import android.view.View; import android.view.ViewGroup; +import android.widget.Button; +import android.widget.EditText; import android.widget.FrameLayout; import androidx.annotation.NonNull; +import androidx.appcompat.app.AlertDialog; +import com.google.android.material.dialog.MaterialAlertDialogBuilder; import com.muqingedit.activity.Edit; public class fanjian extends FrameLayout { + private String id; + private int X, Y; + public FrameLayout frameLayout; - private FrameLayout paletteView; - - /** - * 为其设置画板视图 - * - * @param paletteView - */ - public fanjian setPaletteView(FrameLayout paletteView) { - this.paletteView = paletteView; - return this; + public fanjian(FrameLayout context, String id) { + super(context.getContext()); + this.frameLayout = context; + init(); + setId(id.hashCode()); + setLayoutParams(300, 300); } - public static int id = 0; - - private int X, Y; - - public fanjian(Context context) { - super(context); - setId(id++); + public fanjian(FrameLayout context) { + super(context.getContext()); + this.frameLayout = context; + MaterialAlertDialogBuilder dialogBuilder = new MaterialAlertDialogBuilder(this.getContext()); + dialogBuilder.setTitle("创建房间"); + View inflate = LayoutInflater.from(getContext()).inflate(R.layout.new_fangjian, null); + EditText edit_id = inflate.findViewById(R.id.edit_id); + Button end = inflate.findViewById(R.id.button_end); + Button start = inflate.findViewById(R.id.button_start); + dialogBuilder.setView(inflate); + AlertDialog show = dialogBuilder.show(); + end.setOnClickListener(view -> show.dismiss()); + start.setOnClickListener(view -> { + String id = edit_id.getText().toString(); + if (TextUtils.isEmpty(id)) { + return; + } + fanjian.this.id = id; + setId(id.hashCode()); + Edit.list_fanjian.add(fanjian.this); + setLayoutParams(300, 300); + setXY(0, 0); + frameLayout.addView(fanjian.this); + show.dismiss(); + }); init(); } @@ -78,21 +101,41 @@ public class fanjian extends FrameLayout { fanjian.this.getLocationOnScreen(location); csx = (int) fanjian.this.getX() + fanjian.this.getWidth() / 2; csy = (int) fanjian.this.getY() + fanjian.this.getHeight() / 2; - xian = new xian(getContext()).createNewLine(paletteView, csx, csy); - return true; + xian = new xian(fanjian.this.frameLayout).createNewLine(csx, csy); + break; case MotionEvent.ACTION_MOVE: //绘制线 xian.updateCurrentLine(csx + ax, csy + ay); // xian.updateCurrentLine(event.getRawX()-ax, event.getRawY()-ay); - return true; + break; case MotionEvent.ACTION_UP: - //删除线 + //删除线 判断是否连接房间 如果连接则不删除不连接则删除 + int size = Edit.list_fanjian.size(); + //只要大于1个房间才会进行判断操作 + if (size > 1) { + for (int i = 0; i < size; i++) { + fanjian aa = Edit.list_fanjian.get(i); + if (aa == fanjian.this) { + continue; + } + boolean ab = ConnectionManager.isConnected(fanjian.this.Id(), aa.Id()); + if (ab) { + break; + } + boolean a = csx + ax > aa.getX() && csx + ax < aa.getX() + 300; + boolean b = csy + ay > aa.getY() && csy + ay < aa.getY() + 300; + if (a && b) { + ConnectionManager.addConnection(fanjian.this.Id(), aa.Id()); + return true; + } + } + } xian.clearCurrentLine(); - paletteView.removeView(xian); + frameLayout.removeView(xian); xian = null; - return true; + break; } - return super.onTouchEvent(event); + return true; } } @@ -115,12 +158,9 @@ public class fanjian extends FrameLayout { @Override public boolean onScroll(@NonNull MotionEvent e1, @NonNull MotionEvent e2, float distanceX, float distanceY) { - if (Long) { - } else { - Y = (int) (e2.getY() - e1.getY()); - X = (int) (e2.getX() - e1.getX()); - animate().xBy(X).yBy(Y).setDuration(0).start(); - } + Y = (int) (e2.getY() - e1.getY()); + X = (int) (e2.getX() - e1.getX()); + animate().xBy(X).yBy(Y).setDuration(0).start(); return true; } }); @@ -144,17 +184,20 @@ public class fanjian extends FrameLayout { return this; } -/* - @Override - public float getX() { - return this.X; - } + /* + @Override + public float getX() { + return this.X; + } - @Override - public float getY() { - return this.Y; + @Override + public float getY() { + return this.Y; + } + */ + public String Id() { + return id; } -*/ public String getXY() { return X + ":" + Y; diff --git a/app/src/main/java/com/muqingedit/xian.java b/app/src/main/java/com/muqingedit/xian.java index 2b69c11..d989a7a 100644 --- a/app/src/main/java/com/muqingedit/xian.java +++ b/app/src/main/java/com/muqingedit/xian.java @@ -17,9 +17,10 @@ public class xian extends View { private Paint linePaint; private float currentX, currentY; private float startX, startY; - - public xian(Context context) { - super(context); + FrameLayout frameLayout; + public xian(FrameLayout context) { + super(context.getContext()); + this.frameLayout = context; init(); } @@ -33,7 +34,7 @@ public class xian extends View { linePaint.setStrokeWidth(5f); } - public xian createNewLine(FrameLayout frameLayout, float x, float y) { + public xian createNewLine(float x, float y) { startX = x; startY = y; currentX = x; diff --git a/app/src/main/res/layout/new_fangjian.xml b/app/src/main/res/layout/new_fangjian.xml new file mode 100644 index 0000000..da79be6 --- /dev/null +++ b/app/src/main/res/layout/new_fangjian.xml @@ -0,0 +1,43 @@ + + + + + + + + + + +