-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainActivity.java
More file actions
776 lines (673 loc) · 30.6 KB
/
Copy pathMainActivity.java
File metadata and controls
776 lines (673 loc) · 30.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
package com.example.cocodroneblock;
import android.annotation.SuppressLint;
import android.content.ClipData;
import android.content.ClipDescription;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.graphics.Color;
import android.graphics.Point;
import android.os.Bundle;
import android.util.Log;
import android.view.DragEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.Toast;
import androidx.activity.OnBackPressedCallback;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.view.GravityCompat;
import androidx.core.view.WindowCompat;
import androidx.drawerlayout.widget.DrawerLayout;
import com.google.android.material.navigation.NavigationView;
import java.util.Locale;
import android.app.AlertDialog;
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
private DrawerLayout drawerLayout;
private ImageButton hamburgerButton;
private FrameLayout blockArea;
private LinearLayout separateButtonContainer;
private Button[] mainButtons;
private LinearLayout[] subMenus;
private NavigationView navView;
private ScrollView blockScrollView;
// 스냅 관련 상수
private static final float SNAP_THRESHOLD = 300f;
private static final float BLOCK_SPACING = -30f; // 블록 간 간격 (음수 값)
private static final float SNAP_X_THRESHOLD = 200f; // X축 스냅 범위
private View snapIndicator = null;
@SuppressLint("MissingInflatedId")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WindowCompat.setDecorFitsSystemWindows(getWindow(), false);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
drawerLayout = findViewById(R.id.drawer_layout);
navView = findViewById(R.id.nav_view);
hamburgerButton = findViewById(R.id.hamburger_button);
separateButtonContainer = findViewById(R.id.separate_button_container);
blockArea = findViewById(R.id.block_area);
blockScrollView = findViewById(R.id.block_scroll_view);
hamburgerButton.bringToFront();
hamburgerButton.setElevation(9999f);
separateButtonContainer.bringToFront();
separateButtonContainer.setElevation(9999f);
setupBlockDropListener();
findViewById(R.id.delete_button).setOnDragListener(this::onBlockTrashDrop);
getOnBackPressedDispatcher().addCallback(this, new OnBackPressedCallback(true) {
@Override
public void handleOnBackPressed() {
if (drawerLayout.isDrawerOpen(GravityCompat.END)) {
drawerLayout.closeDrawer(GravityCompat.END);
} else {
setEnabled(false);
finish();
}
}
});
if (navView != null) {
navView.setNavigationItemSelectedListener(this);
}
hamburgerButton.setOnClickListener(v -> drawerLayout.openDrawer(navView));
setNavigationDrawerWidth();
ImageView deleteButton = findViewById(R.id.delete_button);
deleteButton.setOnClickListener(v -> {
if (blockArea.getChildCount() > 0) {
blockArea.removeViewAt(blockArea.getChildCount() - 1);
} else {
Toast.makeText(MainActivity.this, "삭제할 블록이 없습니다.", Toast.LENGTH_SHORT).show();
}
});
drawerLayout.addDrawerListener(new DrawerLayout.DrawerListener() {
@Override
public void onDrawerClosed(@NonNull View drawerView) {
Menu menu = navView.getMenu();
menu.clear();
getMenuInflater().inflate(R.menu.drawer_menu, menu);
}
@Override
public void onDrawerSlide(@NonNull View drawerView, float slideOffset) {
}
@Override
public void onDrawerOpened(@NonNull View drawerView) {
}
@Override
public void onDrawerStateChanged(int newState) {
}
});
mainButtons = new Button[]{
findViewById(R.id.takeoff_button),
findViewById(R.id.speed_button),
findViewById(R.id.altitude_button),
findViewById(R.id.move_button),
findViewById(R.id.rotation_button),
findViewById(R.id.flip_button),
findViewById(R.id.setting_block)
};
subMenus = new LinearLayout[]{
findViewById(R.id.takeoff_button_area),
findViewById(R.id.speed_button_area),
findViewById(R.id.altitude_button_area),
findViewById(R.id.move_button_area),
findViewById(R.id.rotation_button_area),
findViewById(R.id.flip_button_area),
findViewById(R.id.setting_button_area)
};
blockArea.post(() -> {
Log.d("Debug", "blockArea left=" + blockArea.getLeft() +
", top=" + blockArea.getTop() +
", width=" + blockArea.getWidth());
});
setupSubMenuDeleteZone();
for (int i = 0; i < mainButtons.length; i++) {
final int idx = i;
if (mainButtons[i] != null && subMenus[i] != null) {
mainButtons[i].setOnClickListener(v -> toggleSubMenu(idx));
}
}
createBlock("이륙", "takeoff", subMenus[0]);
createBlock("착륙", "takeoff", subMenus[0]);
createBlock("속도 증가", "speed", subMenus[1]);
createBlock("고도 상승", "altitude", subMenus[2]);
createBlock("고도 하강", "altitude", subMenus[2]);
createBlock("호버링", "altitude", subMenus[2]);
createBlock("앞으로 이동", "move", subMenus[3]);
createBlock("뒤로 이동", "move", subMenus[3]);
createBlock("오른쪽으로 이동", "move", subMenus[3]);
createBlock("왼쪽으로 이동", "move", subMenus[3]);
createBlock("우회전", "rotation", subMenus[4]);
createBlock("좌회전", "rotation", subMenus[4]);
createBlock("앞으로 플립", "flip", subMenus[5]);
createBlock("뒤로 플립", "flip", subMenus[5]);
createBlock("오른쪽으로 플립", "flip", subMenus[5]);
createBlock("왼쪽으로 플립", "flip", subMenus[5]);
createBlock("반복시작", "setting", subMenus[6]);
createBlock("반복끝", "setting", subMenus[6]);
}
private void setNavigationDrawerWidth() {
int screenWidth = getResources().getDisplayMetrics().widthPixels;
int drawerWidth = screenWidth / 4;
if (navView != null && navView.getLayoutParams() instanceof DrawerLayout.LayoutParams) {
DrawerLayout.LayoutParams params = (DrawerLayout.LayoutParams) navView.getLayoutParams();
params.width = drawerWidth;
navView.setLayoutParams(params);
}
}
private void createBlock(String text, String blockType, LinearLayout container) {
BlockView block = new BlockView(this, null);
block.setText(text);
block.setBlockType(blockType);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(400, 150);
params.setMargins(10, 10, 10, 10);
block.setLayoutParams(params);
container.addView(block);
setupBlockDragListener(block);
}
private void toggleSubMenu(int index) {
boolean isOpen = (subMenus[index].getVisibility() == View.VISIBLE);
if (isOpen) {
subMenus[index].setVisibility(View.GONE);
separateButtonContainer.setVisibility(View.GONE); // 오타 수정
} else {
for (LinearLayout menu : subMenus) {
if (menu != null) menu.setVisibility(View.GONE);
}
separateButtonContainer.setVisibility(View.VISIBLE);
subMenus[index].setVisibility(View.VISIBLE);
separateButtonContainer.bringToFront();
separateButtonContainer.setElevation(9999f);
}
}
private void setupBlockDropListener() {
blockArea.setOnDragListener((v, event) -> {
switch (event.getAction()) {
case DragEvent.ACTION_DRAG_STARTED:
return event.getClipDescription().hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN);
case DragEvent.ACTION_DRAG_LOCATION:
autoScrollIfNeeded(blockScrollView, event);
View draggedView = (View) event.getLocalState();
if (draggedView instanceof BlockView) {
updateSnapIndicatorForDrag((BlockView) draggedView, event.getX(), event.getY());
}
return true;
case DragEvent.ACTION_DROP:
removeSnapIndicator();
View droppedView = (View) event.getLocalState();
if (droppedView instanceof BlockView) {
BlockView original = (BlockView) droppedView;
ViewGroup parent = (ViewGroup) original.getParent();
float dropX = event.getX();
float dropY = event.getY();
float[] convertedCoords = convertToBlockAreaCoords(dropX, dropY);
float finalX = convertedCoords[0] - original.getWidth() / 2f;
float finalY = convertedCoords[1] - original.getHeight() / 2f;
float fixedX = blockArea.getWidth() / 3f - original.getWidth() / 2f;
if (fixedX < 0) fixedX = 20;
BlockView targetBlock = (parent == blockArea) ? original : original.cloneBlock();
if (parent == blockArea) {
parent.removeView(original);
}
blockArea.addView(targetBlock);
targetBlock.setX(fixedX);
targetBlock.setY(finalY);
targetBlock.setVisibility(View.VISIBLE);
snapIfClose(targetBlock);
setupBlockDragListener(targetBlock);
}
return true;
case DragEvent.ACTION_DRAG_ENDED:
removeSnapIndicator();
View view = (View) event.getLocalState();
if (view instanceof View && view.getVisibility() != View.VISIBLE) {
view.setVisibility(View.VISIBLE);
}
return true;
default:
return false;
}
});
}
private void setupBlockDragListener(BlockView blockView) {
blockView.setOnTouchListener((v, event) -> {
if (event.getAction() == MotionEvent.ACTION_DOWN && v instanceof BlockView) {
v.performClick();
if (v.getParent() == blockArea) {
ClipData data = ClipData.newPlainText("", "");
View.DragShadowBuilder shadowBuilder = new OffsetDragShadowBuilder(v, v.getWidth() / 2f, v.getHeight() / 2f);
v.startDragAndDrop(data, shadowBuilder, v, View.DRAG_FLAG_OPAQUE);
v.setVisibility(View.INVISIBLE);
} else {
BlockView original = (BlockView) v;
BlockView clone = original.cloneBlock();
clone.setLayoutParams(new ViewGroup.LayoutParams(original.getWidth(), original.getHeight()));
clone.setX(-1000);
clone.setY(-1000);
blockArea.addView(clone);
clone.post(() -> {
ClipData data = ClipData.newPlainText("", "");
View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(clone);
clone.startDragAndDrop(data, shadowBuilder, clone, View.DRAG_FLAG_OPAQUE);
});
}
return true;
}
return false;
});
}
private void updateSnapIndicatorForDrag(BlockView draggedView, float dragX, float dragY) {
float[] candidateCoords = convertToBlockAreaCoords(dragX, dragY);
float candidateY = candidateCoords[1];
float blockHeight = draggedView.getHeight();
float blockWidth = draggedView.getWidth();
float fixedX = blockArea.getWidth() / 3f - blockWidth / 2f;
if (fixedX < 0) fixedX = 20;
BlockView aboveBlock = null;
BlockView belowBlock = null;
BlockView middleBlock = null;
float aboveDistance = Float.MAX_VALUE;
float belowDistance = Float.MAX_VALUE;
float middleDistance = Float.MAX_VALUE;
final float DETECTION_THRESHOLD = SNAP_THRESHOLD * 1.5f;
for (int i = 0; i < blockArea.getChildCount(); i++) {
View child = blockArea.getChildAt(i);
if (child == draggedView) continue;
if (child instanceof BlockView) {
BlockView otherBlock = (BlockView) child;
float otherY = otherBlock.getY();
float otherH = otherBlock.getHeight();
float otherMiddleY = otherY + otherH / 2;
float distBottomToTop = Math.abs(candidateY - (otherY + otherH));
float distTopToBottom = Math.abs(otherY - (candidateY + blockHeight));
float distToMiddle = Math.abs(candidateY + blockHeight / 2 - otherMiddleY);
if (distBottomToTop < aboveDistance && distBottomToTop <= DETECTION_THRESHOLD) {
aboveDistance = distBottomToTop;
aboveBlock = otherBlock;
}
if (distTopToBottom < belowDistance && distTopToBottom <= DETECTION_THRESHOLD) {
belowDistance = distTopToBottom;
belowBlock = otherBlock;
}
if (distToMiddle < middleDistance && distToMiddle <= DETECTION_THRESHOLD / 1.5f) {
middleDistance = distToMiddle;
middleBlock = otherBlock;
}
}
}
float targetX = fixedX;
float targetY = candidateY;
boolean candidateFound = false;
boolean snappingUpward = false;
boolean snappingMiddle = false;
if (middleBlock != null) {
BlockView topOfMiddle = null;
float minTopDistance = Float.MAX_VALUE;
for (int i = 0; i < blockArea.getChildCount(); i++) {
View child = blockArea.getChildAt(i);
if (child == draggedView || child == middleBlock) continue;
if (child instanceof BlockView) {
BlockView block = (BlockView) child;
float blockBottom = block.getY() + block.getHeight();
if (blockBottom <= middleBlock.getY() &&
(middleBlock.getY() - blockBottom) < minTopDistance) {
minTopDistance = middleBlock.getY() - blockBottom;
topOfMiddle = block;
}
}
}
targetX = fixedX;
if (topOfMiddle != null) {
targetY = topOfMiddle.getY() + topOfMiddle.getHeight() + BLOCK_SPACING;
} else {
targetY = 10;
}
candidateFound = true;
snappingMiddle = true;
} else {
float aboveY = aboveBlock != null ? aboveBlock.getY() + aboveBlock.getHeight() : Float.MAX_VALUE;
float belowY = belowBlock != null ? belowBlock.getY() : Float.MAX_VALUE;
float distToAbove = Math.abs(candidateY - aboveY);
float distToBelow = Math.abs(candidateY + draggedView.getHeight() - belowY);
if (aboveBlock != null && belowBlock != null) {
if (distToAbove < distToBelow && isNotOccupied(aboveBlock, belowBlock)) {
targetX = fixedX;
targetY = aboveY + BLOCK_SPACING;
candidateFound = true;
snappingUpward = false;
} else if (isNotOccupied(aboveBlock, belowBlock)) {
targetX = fixedX;
targetY = belowY - draggedView.getHeight() - BLOCK_SPACING;
candidateFound = true;
snappingUpward = true;
}
} else if (aboveBlock != null && isNotOccupied(aboveBlock, null)) {
targetX = fixedX;
targetY = aboveY + BLOCK_SPACING;
candidateFound = true;
snappingUpward = false;
} else if (belowBlock != null && isNotOccupied(null, belowBlock)) {
targetX = fixedX;
targetY = belowY - draggedView.getHeight() - BLOCK_SPACING;
candidateFound = true;
snappingUpward = true;
}
}
if (candidateFound) {
showOrUpdateSnapIndicator(targetX, targetY, blockWidth, snappingUpward);
} else {
removeSnapIndicator();
}
}
private boolean isNotOccupied(BlockView aboveBlock, BlockView belowBlock) {
final float OCCUPATION_TOLERANCE = 40f;
for (int i = 0; i < blockArea.getChildCount(); i++) {
View child = blockArea.getChildAt(i);
if (child instanceof BlockView) {
BlockView block = (BlockView) child;
if (block == aboveBlock || block == belowBlock) continue;
if (aboveBlock != null) {
float expectedPos = aboveBlock.getY() + aboveBlock.getHeight() + BLOCK_SPACING;
if (Math.abs(block.getY() - expectedPos) < OCCUPATION_TOLERANCE) {
return false;
}
}
if (belowBlock != null) {
float expectedPos = belowBlock.getY() - block.getHeight() - BLOCK_SPACING;
if (Math.abs(block.getY() - expectedPos) < OCCUPATION_TOLERANCE) {
return false;
}
}
}
}
return true;
}
private void snapIfClose(BlockView newBlock) {
float newY = newBlock.getY();
float h = newBlock.getHeight();
float fixedX = blockArea.getWidth() / 3f - newBlock.getWidth() / 2f;
if (fixedX < 0) fixedX = 20;
newBlock.setX(fixedX);
BlockView closestAbove = null;
BlockView closestBelow = null;
BlockView closestMiddle = null;
float minAboveDistance = Float.MAX_VALUE;
float minBelowDistance = Float.MAX_VALUE;
float minMiddleDistance = Float.MAX_VALUE;
final float EXTENDED_SNAP_THRESHOLD = SNAP_THRESHOLD * 1.5f;
for (int i = 0; i < blockArea.getChildCount(); i++) {
View child = blockArea.getChildAt(i);
if (child == newBlock) continue;
if (child instanceof BlockView) {
BlockView other = (BlockView) child;
float otherY = other.getY();
float otherH = other.getHeight();
float otherMiddleY = otherY + otherH / 2;
float distBottomToTop = Math.abs(newY - (otherY + otherH));
float distTopToBottom = Math.abs((newY + h) - otherY);
float distToMiddle = Math.abs((newY + h / 2) - otherMiddleY);
if (distBottomToTop < minAboveDistance && distBottomToTop <= EXTENDED_SNAP_THRESHOLD) {
minAboveDistance = distBottomToTop;
closestAbove = other;
}
if (distTopToBottom < minBelowDistance && distTopToBottom <= EXTENDED_SNAP_THRESHOLD) {
minBelowDistance = distTopToBottom;
closestBelow = other;
}
if (distToMiddle < minMiddleDistance && distToMiddle <= EXTENDED_SNAP_THRESHOLD / 1.5f) {
minMiddleDistance = distToMiddle;
closestMiddle = other;
}
}
}
if (closestMiddle != null) {
BlockView blockAbove = null;
float minAbove = Float.MAX_VALUE;
for (int i = 0; i < blockArea.getChildCount(); i++) {
View child = blockArea.getChildAt(i);
if (child == newBlock || !(child instanceof BlockView)) continue;
BlockView other = (BlockView) child;
float blockBottom = other.getY() + other.getHeight();
if (blockBottom <= closestMiddle.getY() &&
(closestMiddle.getY() - blockBottom) < minAbove) {
minAbove = closestMiddle.getY() - blockBottom;
blockAbove = other;
}
}
newBlock.setX(fixedX);
if (blockAbove != null) {
newBlock.setY(blockAbove.getY() + blockAbove.getHeight() + BLOCK_SPACING);
float fromY = newBlock.getY(); // 현재 삽입되는 블록의 위치
shiftBlocksFromPosition(newBlock, fromY);
} else {
newBlock.setY(10);
shiftBlocksFromPosition(newBlock, 10 + newBlock.getHeight() + BLOCK_SPACING);
}
return;
}
if (closestAbove != null && closestBelow != null) {
if (isNotOccupied(closestAbove, closestBelow)) {
newBlock.setX(fixedX);
newBlock.setY(closestAbove.getY() + closestAbove.getHeight() + BLOCK_SPACING);
shiftBlocksDown(closestBelow);
return;
}
}
if (closestAbove != null && isNotOccupied(closestAbove, null)) {
newBlock.setX(fixedX);
newBlock.setY(closestAbove.getY() + closestAbove.getHeight() + BLOCK_SPACING);
return;
}
if (closestBelow != null && isNotOccupied(null, closestBelow)) {
newBlock.setX(fixedX);
newBlock.setY(closestBelow.getY() - h - BLOCK_SPACING);
} else {
newBlock.setX(fixedX);
}
}
private void shiftBlocksFromPosition(BlockView newBlock, float fromY) {
float shiftAmount = newBlock.getHeight() + BLOCK_SPACING;
float baseX = newBlock.getX();
for (int i = 0; i < blockArea.getChildCount(); i++) {
View child = blockArea.getChildAt(i);
if (child instanceof BlockView && child != newBlock) {
BlockView block = (BlockView) child;
if (block.getY() >= fromY && Math.abs(block.getX() - baseX) <= SNAP_X_THRESHOLD) {
block.setY(block.getY() + shiftAmount);
}
}
}
}
private void shiftBlocksDown(BlockView startBlock) {
float shiftY = startBlock.getHeight() + BLOCK_SPACING;
float baseY = startBlock.getY();
for (int i = 0; i < blockArea.getChildCount(); i++) {
View child = blockArea.getChildAt(i);
if (child instanceof BlockView && child != startBlock) {
BlockView block = (BlockView) child;
if (block.getY() >= baseY) {
block.setY(block.getY() + shiftY);
}
}
}
}
private float[] convertToBlockAreaCoords(float x, float y) {
int[] blockAreaLocation = new int[2];
blockArea.getLocationOnScreen(blockAreaLocation);
float convertedX = x - blockAreaLocation[0];
float convertedY = y - blockAreaLocation[1] + blockScrollView.getScrollY();
return new float[]{convertedX, convertedY};
}
private boolean isInSubMenu(View parent) {
for (LinearLayout sub : subMenus) {
if (sub == parent) return true;
}
return false;
}
private void autoScrollIfNeeded(ScrollView scrollView, DragEvent event) {
final int SCROLL_THRESHOLD = 100;
final int SCROLL_SPEED = 10;
float y = event.getY();
int scrollY = scrollView.getScrollY();
int height = scrollView.getHeight();
if (y - scrollY < SCROLL_THRESHOLD) {
scrollView.scrollBy(0, -SCROLL_SPEED);
} else if ((scrollY + height) - y < SCROLL_THRESHOLD) {
scrollView.scrollBy(0, SCROLL_SPEED);
}
}
private boolean onBlockTrashDrop(View v, DragEvent event) {
if (event.getAction() == DragEvent.ACTION_DROP) {
View draggedView = (View) event.getLocalState();
if (draggedView != null) {
((ViewGroup) draggedView.getParent()).removeView(draggedView);
}
}
return true;
}
private void setupSubMenuDeleteZone() {
for (LinearLayout sub : subMenus) {
sub.setOnDragListener((v, event) -> {
switch (event.getAction()) {
case DragEvent.ACTION_DRAG_STARTED:
return event.getClipDescription().hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN);
case DragEvent.ACTION_DRAG_ENTERED:
v.setBackgroundColor(0x44FF0000);
return true;
case DragEvent.ACTION_DRAG_EXITED:
v.setBackgroundColor(Color.TRANSPARENT);
return true;
case DragEvent.ACTION_DROP:
View draggedView = (View) event.getLocalState();
if (draggedView != null) {
ViewGroup parent = (ViewGroup) draggedView.getParent();
if (parent != null) {
parent.removeView(draggedView);
}
}
v.setBackgroundColor(Color.TRANSPARENT);
return true;
case DragEvent.ACTION_DRAG_ENDED:
v.setBackgroundColor(Color.TRANSPARENT);
return true;
}
return false;
});
}
}
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
int itemId = item.getItemId();
if (itemId == R.id.nav_settings) {
showLanguageDialog();
return false;
} else if (itemId == R.id.nav_virtual_controller) {
openVirtualController();
} else if (itemId == R.id.nav_gyro_reset) {
resetGyro();
} else if (itemId == R.id.nav_save) {
saveData();
} else if (itemId == R.id.nav_load) {
loadData();
}
return true;
}
private void showLanguageDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("언어 설정");
String[] languages = {"한국어", "English"};
builder.setItems(languages, (dialog, which) -> {
if (which == 0) {
changeLanguage("ko");
} else if (which == 1) {
changeLanguage("en");
}
dialog.dismiss();
});
builder.setNegativeButton("취소", (dialog, which) -> dialog.dismiss());
builder.create().show();
}
private void changeLanguage(String languageCode) {
SharedPreferences.Editor editor = getSharedPreferences("AppSettings", MODE_PRIVATE).edit();
editor.putString("language", languageCode);
editor.apply();
Locale locale = new Locale(languageCode);
Locale.setDefault(locale);
Configuration config = new Configuration();
config.setLocale(locale);
Context newContext = createConfigurationContext(config);
recreate();
}
private void openVirtualController() {
Intent intent = new Intent(this, VirtualControllerActivity.class);
startActivity(intent);
}
private void resetGyro() {
Toast.makeText(this, "자이로 초기화 실행됨", Toast.LENGTH_SHORT).show();
Log.d("Gyro", "자이로 초기화가 정상적으로 실행되었습니다.");
}
private void saveData() {
Toast.makeText(this, "데이터 저장 완료", Toast.LENGTH_SHORT).show();
Log.d("SaveData", "데이터 저장 완료");
}
private void loadData() {
Toast.makeText(this, "데이터 불러오기 완료", Toast.LENGTH_SHORT).show();
Log.d("LoadData", "데이터 불러오기 완료");
}
private static class OffsetDragShadowBuilder extends View.DragShadowBuilder {
private final float offsetX;
private final float offsetY;
public OffsetDragShadowBuilder(View view, float offsetX, float offsetY) {
super(view);
this.offsetX = offsetX;
this.offsetY = offsetY;
}
@Override
public void onProvideShadowMetrics(Point outSize, Point outTouchPoint) {
outSize.set(getView().getWidth(), getView().getHeight());
outTouchPoint.set((int) offsetX, (int) offsetY);
}
}
// 1. 스냅 인디케이터 표시/업데이트 메서드
private void showOrUpdateSnapIndicator(float targetX, float targetY, float width, boolean snappingUpward) {
if (snapIndicator == null) {
// 스냅 인디케이터 생성 (빨간색 선)
snapIndicator = new View(this);
snapIndicator.setBackgroundColor(Color.RED);
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
(int) width, 6); // 6픽셀 두께의 선
snapIndicator.setLayoutParams(params);
blockArea.addView(snapIndicator);
}
// 인디케이터 위치 업데이트
snapIndicator.setX(targetX);
// 상단/하단 위치 결정
if (snappingUpward) {
// 블록 상단에 표시
snapIndicator.setY(targetY - 3);
} else {
// 블록 하단에 표시
snapIndicator.setY(targetY + 3);
}
// 확실하게 보이도록
snapIndicator.setVisibility(View.VISIBLE);
snapIndicator.bringToFront();
}
// 2. 스냅 인디케이터 제거 메서드
private void removeSnapIndicator() {
if (snapIndicator != null) {
blockArea.removeView(snapIndicator);
snapIndicator = null;
}
}
}