Skip to content

Commit

Permalink
Rewrite builders to take advantage of 4.2 covariance
Browse files Browse the repository at this point in the history
  • Loading branch information
Pyxus committed Jan 26, 2024
1 parent 1a3e895 commit 2fdf608
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 127 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<img src="assets/images/fray_banner.gif" alt="Fray Logo">
</p>

![Fray status](https://img.shields.io/badge/status-alpha-red) ![Godot version](https://img.shields.io/badge/godot-v4.0-blue) ![License](https://img.shields.io/badge/license-MIT-informational)
![Fray status](https://img.shields.io/badge/status-alpha-red) ![Godot version](https://img.shields.io/badge/godot-v4.2+-blue) ![License](https://img.shields.io/badge/license-MIT-informational)

## 📖 About

Expand Down
73 changes: 30 additions & 43 deletions src/input/device/composites/combination_input.gd
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ enum Mode {
## Determines press condition necessary to trigger combination
var mode: Mode = Mode.SYNC

## Returns a builder instance
static func builder() -> Builder:
return Builder.new()

func _is_pressed_impl(device: int) -> bool:
match mode:
Expand All @@ -38,11 +41,6 @@ func _decompose_impl(device: int) -> Array[StringName]:
return binds


## Returns a builder instance
static func builder() -> Builder:
return Builder.new()


# Returns: InputState[]
func _get_decomposed_states(device: int) -> Array:
var decomposed_states := []
Expand Down Expand Up @@ -94,57 +92,46 @@ func _is_combination_in_order(device: int, tolerance: float = 10) -> bool:


class Builder:
extends RefCounted

var _composite_input = FrayCombinationInput.new()
extends FrayCompositeInput.Builder

## Builds the composite input
func _init() -> void:
_composite_input = FrayCombinationInput.new()

## Builds the composite input.
## [br]
## Returns a reference to the newly built CompositeInput
## Returns a reference to the newly built combination input.
func build() -> FrayCombinationInput:
return _composite_input

## Adds a composite input as a component of this combination
## [br]
## Returns a reference to this ComponentBuilder
func add_component(composite_input: FrayCompositeInput) -> Builder:
_composite_input.add_component(composite_input)
return self

## Adds a simple input as a component of this combination
## [br]
## Returns a reference to this ComponentBuilder.
func add_component_simple(bind: StringName) -> Builder:
_composite_input.add_component(FraySimpleInput.from_bind(bind))
return self

## Sets whether the input will be virtual or not.
## If true, components that are still held when the composite is released
## will be treated as if they were just pressed again.
## [br]
## Returns a reference to this ComponentBuilder
func is_virtual(value: bool = true) -> Builder:
_composite_input.is_virtual = value
return self

## Sets the composite input's process priority. Higher priority composites are processed first.

## Sets the combination to async mode.
## [br]
## Returns a reference to this ComponentBuilder
func priority(value: int) -> Builder:
_composite_input.priority = value
return self

## Sets the combination to async mode
## Returns a reference to the newly built combination input.
func mode_async() -> Builder:
_composite_input.mode = FrayCombinationInput.Mode.ASYNC
return self

## Sets the combination to sync mode
## Sets the combination to sync mode.
## [br]
## Returns a reference to the newly built combination input.
func mode_sync() -> Builder:
_composite_input.mode = FrayCombinationInput.Mode.SYNC
return self

## Sets the combination to ordered mode
## Sets the combination to ordered mode.
## [br]
## Returns a reference to the newly built combination input.
func mode_ordered() -> Builder:
_composite_input.mode = FrayCombinationInput.Mode.ORDERED
return self

func add_component(composite_input: FrayCompositeInput) -> Builder:
return super(composite_input)

func add_component_simple(bind: StringName) -> Builder:
return super(bind)

func is_virtual(value: bool = true) -> Builder:
return super(value)

func priority(value: int) -> Builder:
return super(value)
45 changes: 45 additions & 0 deletions src/input/device/composites/composite_input.gd
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ var _components: Array[FrayCompositeInput]
# Type: WeakRef<CompositeInput>
var _root_wf: WeakRef

## Returns a builder instance
static func builder() -> Builder:
return Builder.new()

## get_bind_state is a FuncRef of the type (string) -> InputState
func is_pressed(device: int) -> bool:
Expand Down Expand Up @@ -124,3 +127,45 @@ func _is_pressed_impl(device: int) -> bool:
func _decompose_impl(device: int) -> Array[StringName]:
assert(false, "Method not implemented")
return []


class Builder:
extends RefCounted

var _composite_input: FrayCompositeInput

## Builds the composite input.
## [br]
## Returns a reference to the newly built composite input.
func build() -> FrayCompositeInput:
return _composite_input

## Adds a composite input as a component of the composite being constructed.
## [br]
## Returns a reference to this builder.
func add_component(composite_input: FrayCompositeInput) -> Builder:
_composite_input.add_component(composite_input)
return self

## Adds a simple input as a component of the composite being constructed.
## [br]
## Returns a reference to this builder.
func add_component_simple(bind: StringName) -> Builder:
_composite_input.add_component(FraySimpleInput.from_bind(bind))
return self

## Sets whether the input will be virtual or not.
## If true, components that are still held when the composite is released
## will be treated as if they were just pressed again.
## [br]
## Returns a reference to this builder.
func is_virtual(value: bool = true) -> Builder:
_composite_input.is_virtual = value
return self

## Sets the composite input's process priority. Higher priority composites are processed first.
## [br]
## Returns a reference to this builder.
func priority(value: int) -> Builder:
_composite_input.priority = value
return self
49 changes: 16 additions & 33 deletions src/input/device/composites/conditional_input.gd
Original file line number Diff line number Diff line change
Expand Up @@ -79,34 +79,21 @@ func _get_active_component(device: int) -> FrayCompositeInput:


class Builder:
extends RefCounted

var _composite_input: FrayConditionalInput = FrayConditionalInput.new()
extends FrayCompositeInput.Builder

func _init() -> void:
_composite_input = FrayConditionalInput.new()

## Builds the composite input
## [br]
## Returns a reference to the newly built CompositeInput
func build() -> FrayCompositeInput:
## Returns a reference to the newly built conditional input.
func build() -> FrayConditionalInput:
return _composite_input

## Adds a composite input as a component of this conditional input
## [br]
## Returns a reference to this ComponentBuilder
func add_component(composite_input: FrayCompositeInput) -> Builder:
_composite_input.add_component(composite_input)
return self

## Adds a simple input as a component of this conditional input
## [br]
## Returns a reference to this ComponentBuilder.
func add_component_simple(bind: StringName) -> Builder:
_composite_input.add_component(FraySimpleInput.from_bind(bind))
return self

## Sets the condition of the previously added component.
## Will do nothing for the first component added as this component is trated as default.
## Will do nothing for the first component added as this component is treated as default.
## [br]
## Returns a reference to this ComponentBuilder
## Returns a reference to this builder.
func use_condition(condition: Callable) -> Builder:
var component_count := _composite_input.get_component_count()

Expand All @@ -118,19 +105,15 @@ class Builder:
_composite_input.set_condition(component_count - 1, condition)

return self

func add_component(composite_input: FrayCompositeInput) -> Builder:
return super(composite_input)

func add_component_simple(bind: StringName) -> Builder:
return super(bind)

## Sets whether the input will be virtual or not.
## If true, components that are still held when the composite is released
## will be treated as if they were just pressed again.
## [br]
## Returns a reference to this ComponentBuilder
func is_virtual(value: bool = true) -> Builder:
_composite_input.is_virtual = value
return self
return super(value)

## Sets the composite input's process priority. Higher priority composites are processed first.
## [br]
## Returns a reference to this ComponentBuilder
func priority(value: int) -> Builder:
_composite_input.priority = value
return self
return super(value)
41 changes: 12 additions & 29 deletions src/input/device/composites/group_input.gd
Original file line number Diff line number Diff line change
Expand Up @@ -44,50 +44,33 @@ func _decompose_impl(device: int) -> Array[StringName]:


class Builder:
extends RefCounted
extends FrayCompositeInput.Builder
## [FrayGroupInput] builder.

var _composite_input = FrayGroupInput.new()

## Builds the composite input.
func _init() -> void:
_composite_input = FrayGroupInput.new()

## Builds the composite input
## [br]
## Returns a reference to the newly built CompositeInput.
## Returns a reference to the newly built composite input.
func build() -> FrayGroupInput:
return _composite_input

## Sets the minimum number of components that must be pressed.
## [br]
## Returns a reference to this ComponentBuilder.
## Returns a reference to this builder.
func min_pressed(value: int) -> Builder:
_composite_input.min_pressed = value
return self

## Adds a composite input as a component of this group.
## [br]
## Returns a reference to this ComponentBuilder.
func add_component(composite_input: FrayCompositeInput) -> Builder:
_composite_input.add_component(composite_input)
return self

## Adds a simple input as a component of this group
## [br]
## Returns a reference to this ComponentBuilder.
return super(composite_input)

func add_component_simple(bind: StringName) -> Builder:
_composite_input.add_component(FraySimpleInput.from_bind(bind))
return self
return super(bind)

## Sets whether the input will be virtual or not.
## If true, components that are still held when the composite is released
## will be treated as if they were just pressed again.
## [br]
## Returns a reference to this ComponentBuilder
func is_virtual(value: bool = true) -> Builder:
_composite_input.is_virtual = value
return self
return super(value)

## Sets the composite input's process priority. Higher priority composites are processed first.
## [br]
## Returns a reference to this ComponentBuilder
func priority(value: int) -> Builder:
_composite_input.priority = value
return self
return super(value)
27 changes: 6 additions & 21 deletions src/input/device/composites/simple_input.gd
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,15 @@ func _decompose_impl(device: int) -> Array[StringName]:


class Builder:
extends RefCounted
extends FrayCompositeInput.Builder
## [FraySimpleInput] builder.

var _composite_input = FraySimpleInput.new()

## Builds the composite input.
func _init() -> void:
_composite_input = FraySimpleInput.new()

## Builds the composite input
## [br]
## Returns a reference to the newly built CompositeInput.
## Returns a reference to the newly built simple input.
func build() -> FraySimpleInput:
return _composite_input

Expand All @@ -49,19 +50,3 @@ class Builder:
func bind(bind_name: StringName) -> Builder:
_composite_input.bind = bind_name
return self

## Sets whether the input will be virtual or not.
## If true, components that are still held when the composite is released
## will be treated as if they were just pressed again.
## [br]
## Returns a reference to this ComponentBuilder
func is_virtual(value: bool = true) -> Builder:
_composite_input.is_virtual = value
return self

## Sets the composite input's process priority. Higher priority composites are processed first.
## [br]
## Returns a reference to this ComponentBuilder
func priority(value: int) -> Builder:
_composite_input.priority = value
return self

0 comments on commit 2fdf608

Please sign in to comment.