Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
willnationsdev committed Apr 8, 2020
2 parents b2ef424 + 2b1b82a commit 1516c2e
Show file tree
Hide file tree
Showing 3 changed files with 435 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ That's it! I hope you've got ideas of what you'd like to share with others.
|-|-|-|
|[Array2D](addons/godot-next/references/array_2d.gd)|A 2D Array class.|GDScript
|[BitFlag](addons/godot-next/references/bit_flag.gd)|A class that allows abstracts away the complexity of handling bit flag enum types.|GDScript
|[Bitset](addons/godot-next/references/bigset.gd)|A class that allows for easily manipulated bitmasks of any size.|GDScript
|[Behavior](addons/godot-next/resources/behavior.gd)|A Resource type that automatically calls Node-like notification methods when paired with the CallbackDelegator class.|GDScript
|[CallbackDelegator](addons/godot-next/nodes/callback_delegator.gd)|A Node that manages a ResourceSet of resources and delegates Node callbacks to each instance.|GDScript
|[ClassType](addons/godot-next/references/class_type.gd)|A class abstraction, both for engine and user-defined types.|GDScript
Expand All @@ -84,6 +85,7 @@ That's it! I hope you've got ideas of what you'd like to share with others.
|[Singletons](addons/godot-next/global/singletons.gd)|A utility for caching Reference-derived singletons. Resources with a `SELF_RESOURCE` constant with a path to a `*.tres` file will be automatically loaded when accessed.|GDScript
|[Trail2D](addons/godot-next/2d/trail_2d.gd)|Creates a variable-length trail that tracks a "target" node.|GDScript
|[Trail3D](addons/godot-next/3d/trail_3d.gd)|Creates a variable-length trail on an ImmediateGeometry node.|GDScript
|[Tween Sequence](addons/godot-next/references/tween_sequence.gd)|A helper class for easier management and chaining of Tweens dynamically from code.|GDScript
|[Variant](addons/godot-next/global/variant.gd)|A utility class for handling Variants (the type wrapper for all variables in Godot's scripting API).|GDScript
|[VBoxItemList](addons/godot-next/gui/v_box_item_list.gd)|Creates a vertical list of items that can be added or removed. Items are a user-specified Script or Scene Control.|GDScript
|[DiscreteGradientTexture](addons/godot-next/resources/DiscreteGradientTexture.gd)|Creates a not interpolated texture for a gradient.|GDScript
64 changes: 64 additions & 0 deletions addons/godot-next/references/bitset.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# Bitset
# author: milesturin
# license: MIT
# description: A class that allows for easily manipulated bitmasks of any size
# usage: Mostly self explanatory. _init() is equivalent to resize().
# By setting enforce_soft_size to false, the Bitset will allow the user to access
# bits that have been reserved by the script, but are outside of the requested size.

extends Reference
class_name Bitset

### CONSTANTS ###

const MASK_SIZE := 32

### PROPERTIES ###

var bitmasks: PoolIntArray = []
var bits: int

### NOTIFICATIONS ###

func _init(size: int, default_state: bool = false, enforce_soft_size: bool = true) -> void:
resize(size, default_state, enforce_soft_size)

### PUBLIC METHODS ###

func resize(size: int, default_state: bool = false, enforce_soft_size: bool = true) -> void:
assert(size >= 0)
var old_masks := bitmasks.size()
if old_masks > 0 and bits % MASK_SIZE:
if default_state:
bitmasks[old_masks - 1] |= (~0 << (bits % MASK_SIZE))
else:
bitmasks[old_masks - 1] &= ~((~0) << (bits % MASK_SIZE))
bitmasks.resize(ceil(size / float(MASK_SIZE)))
bits = size if enforce_soft_size else bitmasks.size() * MASK_SIZE
for i in range(old_masks, bitmasks.size()):
bitmasks[i] = ~0 if default_state else 0

func check_bit(index: int) -> bool:
assert(index < bits)
return bitmasks[index / MASK_SIZE] & (1 << (index % MASK_SIZE)) != 0

func set_bit(index: int, state: bool) -> void:
assert(index < bits)
if state:
bitmasks[index / MASK_SIZE] |= (1 << (index % MASK_SIZE))
else:
bitmasks[index / MASK_SIZE] &= ~(1 << (index % MASK_SIZE))

func flip_bit(index: int) -> void:
assert(index < bits)
set_bit(index, !check_bit(index))

func print_bits(multiline: bool = true) -> void:
if multiline:
for i in range(bits):
print("bit " + String(i) + ": " + String(check_bit(i)))
else:
var output := ""
for i in range(bits):
output += '1' if check_bit(i) else '0'
print(output)
Loading

0 comments on commit 1516c2e

Please sign in to comment.