Advertisement
Pyxus

Godot Resizable Container

Mar 30th, 2019
1,172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.38 KB | None | 0 0
  1. extends VBoxContainer
  2.  
  3. signal moved_to_top;
  4. signal was_resized;
  5. signal created;
  6.  
  7. export(bool) var debug_on;
  8.  
  9. var drag_pos = null;
  10. var resizing_window:= false;
  11. var target_edge := 0;
  12. var cursor_type := CURSOR_ARROW;
  13. var size_anchor := Vector2(0, 0);
  14. var prev_win_size := Vector2(0,0);
  15. var prev_win_pos := Vector2(0, 0);
  16. var gmouse_pos := Vector2(0, 0);
  17. var lmouse_pos := Vector2(0, 0);
  18. var on_top := false;
  19. enum edge {left, top, right, bottom, topl, topr, botl, botr};
  20.  
  21.  
  22. ##================================================================================
  23. ##Notifications
  24. ##================================================================================
  25. func _ready():
  26.     emit_signal("window_created");
  27.  
  28. func _draw():
  29.     if (debug_on):
  30.         #Top
  31.         draw_rect(Rect2(Vector2(0,-5), Vector2(get_size().x, 5)),Color( 0.75, 0.75, 0.75, 1 ),false);
  32.         #Left
  33.         draw_rect(Rect2(Vector2(-5,0), Vector2(5, get_size().y)),Color( 0.75, 0.75, 0.75, 1 ),false);
  34.         #Right
  35.         draw_rect(Rect2(Vector2(get_size().x,0), Vector2(5, get_size().y)),Color( 0.75, 0.75, 0.75, 1 ),false);
  36.         #Bottom
  37.         draw_rect(Rect2(Vector2(0,get_size().y), Vector2(get_size().x, 5)),Color( 0.75, 0.75, 0.75, 1 ),false);
  38.  
  39.         #Top Left
  40.         draw_rect(Rect2(Vector2(-6,-5),Vector2(10,10)), Color( 0.75, 0.75, 0.75, 1 ))
  41.         #Top Right
  42.         draw_rect(Rect2(Vector2(get_size().x-5,-5),Vector2(10,10)), Color( 0.75, 0.75, 0.75, 1 ))
  43.         #Bottom Left
  44.         draw_rect(Rect2(Vector2(-6,get_size().y-4),Vector2(10,10)), Color( 0.75, 0.75, 0.75, 1 ))
  45.         #Bottom Right
  46.         #draw_circle(Vector2(get_size().x,get_size().y),5, Color( 0.75, 0.75, 0.75, 1 ))
  47.         draw_rect(Rect2(Vector2(get_size().x-5,get_size().y-4),Vector2(10,10)), Color( 0.75, 0.75, 0.75, 1 ))
  48.     pass
  49.  
  50. func _process(delta):
  51.     on_top = (get_position_in_parent() == 0);
  52.     # Determine Frame Edges
  53.     gmouse_pos = get_global_mouse_position();
  54.     lmouse_pos = get_local_mouse_position();
  55.     if not resizing_window:
  56.         # Check If The Mouse Is Targeting an Edge
  57.         target_edge = get_target_edge(get_position(), get_size(), gmouse_pos);
  58.         Input.set_default_cursor_shape(cursor_type);
  59.         set_win_mouse_filters(MOUSE_FILTER_PASS);
  60.  
  61.         # Check If An Edge is Selected
  62.         if target_edge != -1 and on_top:
  63.             if Input.is_action_just_pressed("mb_left"):
  64.                 size_anchor = Vector2(get_position().x + get_size().x, get_position().y + get_size().y);
  65.                 resizing_window = true;
  66.                 emit_signal("moved_to_top");
  67.                 emit_signal("was_resized");
  68.     else:
  69.         # Resize Window Based On Edge
  70.         set_win_mouse_filters(MOUSE_FILTER_IGNORE);
  71.         if Input.is_action_pressed("mb_left"):
  72.             resize_window();
  73.         else:
  74.             resizing_window = false;
  75.  
  76. ##================================================================================
  77. ##Functions
  78. ##================================================================================
  79. func set_win_mouse_filters(mouse_filter):
  80.     $Header/TitleBar.set_mouse_filter(mouse_filter);
  81.     for option in $Header/Buttons.get_children():
  82.         option.set_mouse_filter(mouse_filter);
  83.  
  84. func get_target_edge(win_pos, win_size, mouse_pos):
  85.     var frame_top   := Rect2(Vector2(win_pos.x, win_pos.y - 5), Vector2(win_size.x, 5));
  86.     var frame_left := Rect2(Vector2(win_pos.x - 5, win_pos.y), Vector2(5, win_size.y));
  87.     var frame_right := Rect2(Vector2(win_pos.x + win_size.x, win_pos.y), Vector2(5, win_size.y));
  88.     var frame_bot := Rect2(Vector2(win_pos.x, win_pos.y + win_size.y), Vector2(win_size.x, 5));
  89.  
  90.     var frame_topl := Rect2(Vector2(win_pos.x - 6,win_pos.y - 5),Vector2(10,10));
  91.     var frame_topr := Rect2(Vector2(win_pos.x + win_size.x - 5,win_pos.y - 5),Vector2(10,10));
  92.     var frame_botl := Rect2(Vector2(win_pos.x - 6,win_pos.y + win_size.y-4),Vector2(10,10));
  93.     var frame_botr := Rect2(Vector2(win_pos.x + win_size.x -5,win_pos.y + win_size.y - 4),Vector2(10,10));
  94.  
  95.     # Determine Mouse Frame Contact
  96.     if frame_topl.has_point(mouse_pos):
  97.         target_edge = edge.topl;
  98.     elif frame_topr.has_point(mouse_pos):
  99.         target_edge = edge.topr;
  100.     elif frame_botl.has_point(mouse_pos):
  101.         target_edge = edge.botl;
  102.     elif frame_botr.has_point(mouse_pos):
  103.         target_edge = edge.botr;
  104.     elif frame_left.has_point(mouse_pos):
  105.         target_edge = edge.left;
  106.     elif frame_top.has_point(mouse_pos):
  107.         target_edge = edge.top;
  108.     elif frame_right.has_point(mouse_pos):
  109.         target_edge = edge.right;
  110.     elif frame_bot.has_point(mouse_pos):
  111.         target_edge = edge.bottom;
  112.     else:
  113.         target_edge = -1;
  114.  
  115.     # Determine Cursor Type
  116.     match target_edge:
  117.         edge.topl, edge.botr:
  118.             cursor_type = CURSOR_FDIAGSIZE;
  119.         edge.topr, edge.botl:
  120.             cursor_type = CURSOR_BDIAGSIZE;
  121.         edge.top, edge.bottom:
  122.             cursor_type = CURSOR_VSIZE;
  123.         edge.left, edge.right:
  124.             cursor_type = CURSOR_HSIZE;
  125.         _:
  126.             cursor_type = CURSOR_ARROW;
  127.     return target_edge;
  128.  
  129. func resize_window():
  130.     match target_edge:
  131.         edge.left:
  132.             set_position(Vector2(gmouse_pos.x, get_position().y));
  133.             set_size(Vector2(size_anchor.x - gmouse_pos.x, get_size().y));
  134.             if (gmouse_pos.x + get_size().x - size_anchor.x) > 0:
  135.                 set_position(Vector2(size_anchor.x - get_size().x, get_position().y))
  136.         edge.top:
  137.             set_position(Vector2(get_position().x, gmouse_pos.y));
  138.             set_size(Vector2(get_size().x, size_anchor.y - get_position().y));
  139.             if (gmouse_pos.y + get_size().y - size_anchor.y) > 0:
  140.                 set_position(Vector2(get_position().x, size_anchor.y - get_size().y))
  141.         edge.right:
  142.             set_size(Vector2(lmouse_pos.x ,get_size().y))
  143.         edge.bottom:
  144.             set_size(Vector2(get_size().x, lmouse_pos.y));
  145.         edge.topl:
  146.             set_position(Vector2(gmouse_pos.x, gmouse_pos.y));
  147.             set_size(Vector2(size_anchor.x - get_position().x, size_anchor.y - get_position().y))
  148.             if (gmouse_pos.x + get_size().x - size_anchor.x) > 0:
  149.                 set_position(Vector2(size_anchor.x - get_size().x, get_position().y))
  150.             if (gmouse_pos.y + get_size().y - size_anchor.y) > 0:
  151.                 set_position(Vector2(get_position().x, size_anchor.y - get_size().y))
  152.         edge.topr:
  153.             set_position(Vector2(get_position().x, gmouse_pos.y));
  154.             set_size(Vector2(lmouse_pos.x, size_anchor.y - get_position().y))
  155.             if (gmouse_pos.y + get_size().y - size_anchor.y) > 0:
  156.                 set_position(Vector2(get_position().x, size_anchor.y - get_size().y))
  157.         edge.botl:
  158.             set_position(Vector2(gmouse_pos.x, get_position().y));
  159.             set_size(Vector2(size_anchor.x - get_position().x, lmouse_pos.y));
  160.             if (gmouse_pos.x - get_size().x + size_anchor.x) > 0:
  161.                 set_position(Vector2(size_anchor.x - get_size().x, get_position().y))
  162.         edge.botr:
  163.             set_size(Vector2(lmouse_pos.x, lmouse_pos.y));
  164.     pass
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement