1
0
Fork 0
mirror of https://github.com/Orama-Interactive/Pixelorama.git synced 2025-02-21 13:03:13 +00:00
Pixelorama/src/Tools/ShapeDrawer.gd
Emmanouil Papadeas f509dfb9f6
Implement the Keychain Plugin (#700)
* Start implementing the godot_better_input plugin

* Update ShortcutEdit.gd

* Load & save preset option

* Add some groups and fix action events not being deleted on load

* Add MenuInputAction class for multiple menu accelerators

* Create a proper plugin and a BetterInput autoload

* Update menu accelerators

* Move settings to BetterInput

* Move menu enums to Global, make more MenuInputActions

* Add more menu events

* Add new groups

* Optimize BetterInput _input() method

* Remove a lot of lines of code

* Change some previous events, add ignore actions and a View menu group

* Change update_item_accelerator to update_ui

* Move MenuInputAction initialization to BetterInput.gd

* Update hint tooltips when a shortcut changes

Temporarily comment out some code regarding the configurable modifiers

* Some MenuInputAction variable name changes

* Add handle_input() to InputAction

* Update the shortcuts of buttons

* Fix shortcut selector menu position

* Change plugin name into Keychain

* Fix keyboard input dialog exiting when Enter or Space is being pressed

* Add two more groups

* Make groups folded by default

* Temporarily make tool modifier shortcuts not configurable

A temporary change, they will be made configurable again, with different actions that are currently mapped to the same events, local/independent from each other.

* Add license for Keychain

* Fix issue where a key event would be added in other input types

* Fix bug where the assigned state was not updated when the dialog appeared again

* Update Main.tscn

* Add a disabled line edit in keyboard shortcut selector to grab focus

* Load presets in the Keychain autoload

This way, the input actions get updated from the start, instead of only at the ShortcutEdit scene.

WARNING, this currently causes crashes if the menu items have no shortcut binded to them.

* Move custom settings away from Keychain.gd

To keep it the same as the upstream plugin

* Change menu enum names

* Made action_get_first_key() more general

* Use arrays for menu items instead of dictionaries, fixes crash

* Move moveable panels to Window menu

* Format

* Optimize hint tooltip updating

* Add support for translations in Keychain

* Translation changes

* Made tool modifiers configurable

Needs more testing.

* Made camera arrow key movement configurable & joypad axis support

This commit removes the ability to press Shift and Control+Shift to adjust the camera arrow key movement speed. Instead, the speed depends on the zoom level.

The right joypad analog stick is configured to move the camera by default.

* Rename presets into shortcut profiles, use Resources and let users create their own

* [skip ci] Update addons README

* Update Global.gd
2022-05-16 15:07:51 +03:00

217 lines
5.6 KiB
GDScript

extends "res://src/Tools/Draw.gd"
var _start := Vector2.ZERO
var _offset := Vector2.ZERO
var _dest := Vector2.ZERO
var _fill := false
var _drawing := false
var _displace_origin := false
var _thickness := 1
func _init() -> void:
_drawer.color_op = Drawer.ColorOp.new()
update_indicator()
func update_brush() -> void:
pass
func _on_Thickness_value_changed(value: int) -> void:
_thickness = value
update_indicator()
update_config()
save_config()
func update_indicator() -> void:
var indicator := BitMap.new()
var rect := _get_result_rect(_start, _dest)
var points := _get_points(rect.size)
var t_offset := _thickness - 1
var t_offsetv := Vector2(t_offset, t_offset)
indicator.create(rect.size + t_offsetv * 2)
for point in points:
indicator.set_bit(point, 1)
_indicator = indicator
_polylines = _create_polylines(_indicator)
func _on_FillCheckbox_toggled(button_pressed: bool) -> void:
_fill = button_pressed
update_config()
save_config()
func get_config() -> Dictionary:
var config := .get_config()
config["fill"] = _fill
config["thickness"] = _thickness
return config
func set_config(config: Dictionary) -> void:
.set_config(config)
_fill = config.get("fill", _fill)
_thickness = config.get("thickness", _thickness)
func update_config() -> void:
.update_config()
$FillCheckbox.pressed = _fill
$ThicknessSlider.value = _thickness
$ShapeThickness/ThicknessSpinbox.value = _thickness
func _get_shape_points(_size: Vector2) -> PoolVector2Array:
return PoolVector2Array()
func _get_shape_points_filled(_size: Vector2) -> PoolVector2Array:
return PoolVector2Array()
func _input(event: InputEvent) -> void:
if _drawing:
if event.is_action_pressed("shape_displace"):
_displace_origin = true
elif event.is_action_released("shape_displace"):
_displace_origin = false
func draw_start(position: Vector2) -> void:
.draw_start(position)
if Input.is_action_pressed("shape_displace"):
_picking_color = true
_pick_color(position)
return
_picking_color = false
Global.canvas.selection.transform_content_confirm()
update_mask()
_start = position
_offset = position
_dest = position
_drawing = true
func draw_move(position: Vector2) -> void:
.draw_move(position)
if _picking_color: # Still return even if we released Alt
if Input.is_action_pressed("shape_displace"):
_pick_color(position)
return
if _drawing:
if _displace_origin:
_start += position - _offset
_dest = position
_offset = position
_set_cursor_text(_get_result_rect(_start, position))
func draw_end(position: Vector2) -> void:
.draw_end(position)
if _picking_color:
return
if _drawing:
_draw_shape(_start, position)
_start = Vector2.ZERO
_dest = Vector2.ZERO
_drawing = false
_displace_origin = false
cursor_text = ""
func draw_preview() -> void:
if _drawing:
var canvas: CanvasItem = Global.canvas.previews
var indicator := BitMap.new()
var rect := _get_result_rect(_start, _dest)
var points := _get_points(rect.size)
var t_offset := _thickness - 1
var t_offsetv := Vector2(t_offset, t_offset)
indicator.create(rect.size + t_offsetv * 2)
for point in points:
indicator.set_bit(point, 1)
canvas.draw_set_transform(rect.position - t_offsetv, canvas.rotation, canvas.scale)
for line in _create_polylines(indicator):
canvas.draw_polyline(PoolVector2Array(line), Color.black)
canvas.draw_set_transform(canvas.position, canvas.rotation, canvas.scale)
func _draw_shape(origin: Vector2, dest: Vector2) -> void:
var rect := _get_result_rect(origin, dest)
var points := _get_points(rect.size)
prepare_undo("Draw Shape")
for point in points:
# Reset drawer every time because pixel perfect sometimes breaks the tool
_drawer.reset()
# Draw each point offseted based on the shape's thickness
draw_tool(rect.position + point - Vector2.ONE * (_thickness - 1))
commit_undo()
# Given an origin point and destination point, returns a rect representing
# where the shape will be drawn and what is its size
func _get_result_rect(origin: Vector2, dest: Vector2) -> Rect2:
var rect := Rect2(Vector2.ZERO, Vector2.ZERO)
# Center the rect on the mouse
if Input.is_action_pressed("shape_center"):
var new_size := (dest - origin).floor()
# Make rect 1:1 while centering it on the mouse
if Input.is_action_pressed("shape_perfect"):
var square_size := max(abs(new_size.x), abs(new_size.y))
new_size = Vector2(square_size, square_size)
origin -= new_size
dest = origin + 2 * new_size
# Make rect 1:1 while not trying to center it
if Input.is_action_pressed("shape_perfect"):
var square_size := min(abs(origin.x - dest.x), abs(origin.y - dest.y))
rect.position.x = origin.x if origin.x < dest.x else origin.x - square_size
rect.position.y = origin.y if origin.y < dest.y else origin.y - square_size
rect.size = Vector2(square_size, square_size)
# Get the rect without any modifications
else:
rect.position = Vector2(min(origin.x, dest.x), min(origin.y, dest.y))
rect.size = (origin - dest).abs()
rect.size += Vector2.ONE
return rect
func _get_points(size: Vector2) -> PoolVector2Array:
return _get_shape_points_filled(size) if _fill else _get_shape_points(size)
func _outline_point(p: Vector2, thickness: int = 1, include_p: bool = true) -> Array:
var array := []
if thickness != 1:
var t_of = thickness - 1
for x in range(-t_of, thickness):
for y in range(-t_of, thickness):
if x == 0 and y == 0 and not include_p:
continue
array.append(p + Vector2(x, y))
return array
func _set_cursor_text(rect: Rect2) -> void:
cursor_text = "%s, %s" % [rect.position.x, rect.position.y]
cursor_text += " -> %s, %s" % [rect.end.x - 1, rect.end.y - 1]
cursor_text += " (%s, %s)" % [rect.size.x, rect.size.y]