pub struct HexLayout {
pub orientation: HexOrientation,
pub origin: Vec2,
pub scale: Vec2,
}
Expand description
Hexagonal layout. This type is the bridge between your world/pixel coordinate system and the hexagonal coordinate system.
§Axis
By default, the Hex
y
axis is pointing up and the x
axis is
pointing right but you have the option to invert them using invert_x
and
invert_y
This may be useful depending on the coordinate system of your
display.
§Example
let layout = HexLayout {
// We want flat topped hexagons
orientation: HexOrientation::Flat,
// We define the world space origin equivalent of `Hex::ZERO` in hex space
origin: Vec2::new(1.0, 2.0),
// We define the world space scale of the hexagons
scale: Vec2::new(1.0, 1.0),
};
// You can now find the world positon (center) of any given hexagon
let world_pos = layout.hex_to_world_pos(Hex::ZERO);
// You can also find which hexagon is at a given world/screen position
let hex_pos = layout.world_pos_to_hex(Vec2::new(1.23, 45.678));
§Builder
HexLayout
provides a builder pattern:
let mut layout = HexLayout::flat()
.with_scale(Vec2::new(2.0, 3.0)) // Individual Hexagon size
.with_origin(Vec2::new(-1.0, 0.0)); // World origin
// Invert the x axis, which will now go left. Will change `scale.x` to `-2.0`
layout.invert_x();
// Invert the y axis, which will now go down. Will change `scale.y` to `-3.0`
layout.invert_y();
§Working with Sprites
If you intend to use the hexagonal grid to place images/sprites you may use
HexLayout::with_rect_size
to make the hexagon scale fit the your sprite
dimensions.
You can also retrieve the matching rect size from any layout using
HexLayout::rect_size()
Fields§
§orientation: HexOrientation
The hexagonal orientation of the layout (usually “flat” or “pointy”)
origin: Vec2
The origin of the hexagonal representation in world/pixel space, usually
Vec2::ZERO
scale: Vec2
The size of individual hexagons in world/pixel space. The scale can be irregular or negative
Implementations§
Source§impl HexLayout
impl HexLayout
Sourcepub fn transform_vector(&self, vector: Vec2) -> Vec2
pub fn transform_vector(&self, vector: Vec2) -> Vec2
Transforms a local hex space vector to world space
by applying the layout scale
but NOT the origin
Sourcepub fn transform_point(&self, point: Vec2) -> Vec2
pub fn transform_point(&self, point: Vec2) -> Vec2
Transforms a local hex point to world space
by applying the layout scale
and origin
Sourcepub fn inverse_transform_vector(&self, vector: Vec2) -> Vec2
pub fn inverse_transform_vector(&self, vector: Vec2) -> Vec2
Transforms a world space vector to local hex space
by applying the layout scale
but NOT the origin
Sourcepub fn inverse_transform_point(&self, point: Vec2) -> Vec2
pub fn inverse_transform_point(&self, point: Vec2) -> Vec2
Transforms a world pace point to local hex space
by applying the layout scale
and origin
Source§impl HexLayout
impl HexLayout
Sourcepub fn hex_to_world_pos(&self, hex: Hex) -> Vec2
pub fn hex_to_world_pos(&self, hex: Hex) -> Vec2
Computes hexagonal coordinates hex
into world/pixel coordinates
Sourcepub fn fract_hex_to_world_pos(&self, hex: Vec2) -> Vec2
pub fn fract_hex_to_world_pos(&self, hex: Vec2) -> Vec2
Computes fractional hexagonal coordinates hex
into world/pixel
coordinates
Sourcepub fn world_pos_to_hex(&self, pos: Vec2) -> Hex
pub fn world_pos_to_hex(&self, pos: Vec2) -> Hex
Computes world/pixel coordinates pos
into hexagonal coordinates
Sourcepub fn world_pos_to_fract_hex(&self, pos: Vec2) -> Vec2
pub fn world_pos_to_fract_hex(&self, pos: Vec2) -> Vec2
Computes world/pixel coordinates pos
into fractional hexagonal
coordinates
Sourcepub fn hex_corners(&self, hex: Hex) -> [Vec2; 6]
pub fn hex_corners(&self, hex: Hex) -> [Vec2; 6]
Retrieves all 6 corner coordinates of the given hexagonal coordinates
hex
Sourcepub fn hex_edge_corners(&self, hex: Hex) -> [[Vec2; 2]; 6]
pub fn hex_edge_corners(&self, hex: Hex) -> [[Vec2; 2]; 6]
Retrieves all 6 edge corner pair coordinates of the given hexagonal
coordinates hex
Sourcepub fn center_aligned_hex_corners(&self) -> [Vec2; 6]
pub fn center_aligned_hex_corners(&self) -> [Vec2; 6]
Retrieves all 6 edge corner pair coordinates of the given hexagonal
coordinates hex
without offsetting at the origin
Source§impl HexLayout
impl HexLayout
Sourcepub fn edge_coordinates(&self, edge: GridEdge) -> [Vec2; 2]
pub fn edge_coordinates(&self, edge: GridEdge) -> [Vec2; 2]
Returns the world coordinate of the two edge vertices in clockwise order
Sourcepub fn all_edge_coordinates(&self, coord: Hex) -> [[Vec2; 2]; 6]
pub fn all_edge_coordinates(&self, coord: Hex) -> [[Vec2; 2]; 6]
Returns the world coordinate of all edge vertex pairs in clockwise order
Sourcepub fn vertex_coordinates(&self, vertex: GridVertex) -> Vec2
pub fn vertex_coordinates(&self, vertex: GridVertex) -> Vec2
Returns the world coordinate of the vertex
Source§impl HexLayout
impl HexLayout
Sourcepub const fn new(orientation: HexOrientation) -> Self
pub const fn new(orientation: HexOrientation) -> Self
Constructs a new layout with the given orientation
and default
values
Sourcepub const fn with_origin(self, origin: Vec2) -> Self
pub const fn with_origin(self, origin: Vec2) -> Self
Specifies the world/pixel origin of the layout
Sourcepub const fn with_hex_size(self, size: f32) -> Self
pub const fn with_hex_size(self, size: f32) -> Self
Specifies the world/pixel regular size of individual hexagons
Sourcepub fn with_rect_size(self, rect_size: Vec2) -> Self
pub fn with_rect_size(self, rect_size: Vec2) -> Self
Specifies the world/pixel size of individual hexagons to match
the given rect_size
. This is useful if you want hexagons
to match a sprite size
Sourcepub const fn with_scale(self, scale: Vec2) -> Self
pub const fn with_scale(self, scale: Vec2) -> Self
Specifies the world/pixel scale of individual hexagons.
§Note
For most use cases prefer Self::with_hex_size
instead.
Trait Implementations§
Source§impl<'de> Deserialize<'de> for HexLayout
impl<'de> Deserialize<'de> for HexLayout
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Source§impl FromReflect for HexLayoutwhere
HexLayout: Any + Send + Sync,
HexOrientation: FromReflect + TypePath + MaybeTyped + RegisterForReflection,
Vec2: FromReflect + TypePath + MaybeTyped + RegisterForReflection,
impl FromReflect for HexLayoutwhere
HexLayout: Any + Send + Sync,
HexOrientation: FromReflect + TypePath + MaybeTyped + RegisterForReflection,
Vec2: FromReflect + TypePath + MaybeTyped + RegisterForReflection,
Source§fn from_reflect(reflect: &dyn PartialReflect) -> Option<Self>
fn from_reflect(reflect: &dyn PartialReflect) -> Option<Self>
Self
from a reflected value.Source§fn take_from_reflect(
reflect: Box<dyn PartialReflect>,
) -> Result<Self, Box<dyn PartialReflect>>
fn take_from_reflect( reflect: Box<dyn PartialReflect>, ) -> Result<Self, Box<dyn PartialReflect>>
Self
using,
constructing the value using from_reflect
if that fails. Read moreSource§impl GetTypeRegistration for HexLayoutwhere
HexLayout: Any + Send + Sync,
HexOrientation: FromReflect + TypePath + MaybeTyped + RegisterForReflection,
Vec2: FromReflect + TypePath + MaybeTyped + RegisterForReflection,
impl GetTypeRegistration for HexLayoutwhere
HexLayout: Any + Send + Sync,
HexOrientation: FromReflect + TypePath + MaybeTyped + RegisterForReflection,
Vec2: FromReflect + TypePath + MaybeTyped + RegisterForReflection,
Source§fn get_type_registration() -> TypeRegistration
fn get_type_registration() -> TypeRegistration
TypeRegistration
for this type.Source§fn register_type_dependencies(registry: &mut TypeRegistry)
fn register_type_dependencies(registry: &mut TypeRegistry)
Source§impl PartialReflect for HexLayoutwhere
HexLayout: Any + Send + Sync,
HexOrientation: FromReflect + TypePath + MaybeTyped + RegisterForReflection,
Vec2: FromReflect + TypePath + MaybeTyped + RegisterForReflection,
impl PartialReflect for HexLayoutwhere
HexLayout: Any + Send + Sync,
HexOrientation: FromReflect + TypePath + MaybeTyped + RegisterForReflection,
Vec2: FromReflect + TypePath + MaybeTyped + RegisterForReflection,
Source§fn get_represented_type_info(&self) -> Option<&'static TypeInfo>
fn get_represented_type_info(&self) -> Option<&'static TypeInfo>
Source§fn try_apply(&mut self, value: &dyn PartialReflect) -> Result<(), ApplyError>
fn try_apply(&mut self, value: &dyn PartialReflect) -> Result<(), ApplyError>
Source§fn reflect_kind(&self) -> ReflectKind
fn reflect_kind(&self) -> ReflectKind
Source§fn reflect_ref(&self) -> ReflectRef<'_>
fn reflect_ref(&self) -> ReflectRef<'_>
Source§fn reflect_mut(&mut self) -> ReflectMut<'_>
fn reflect_mut(&mut self) -> ReflectMut<'_>
Source§fn reflect_owned(self: Box<Self>) -> ReflectOwned
fn reflect_owned(self: Box<Self>) -> ReflectOwned
Source§fn try_into_reflect(
self: Box<Self>,
) -> Result<Box<dyn Reflect>, Box<dyn PartialReflect>>
fn try_into_reflect( self: Box<Self>, ) -> Result<Box<dyn Reflect>, Box<dyn PartialReflect>>
Source§fn try_as_reflect(&self) -> Option<&dyn Reflect>
fn try_as_reflect(&self) -> Option<&dyn Reflect>
Source§fn try_as_reflect_mut(&mut self) -> Option<&mut dyn Reflect>
fn try_as_reflect_mut(&mut self) -> Option<&mut dyn Reflect>
Source§fn into_partial_reflect(self: Box<Self>) -> Box<dyn PartialReflect>
fn into_partial_reflect(self: Box<Self>) -> Box<dyn PartialReflect>
Source§fn as_partial_reflect(&self) -> &dyn PartialReflect
fn as_partial_reflect(&self) -> &dyn PartialReflect
Source§fn as_partial_reflect_mut(&mut self) -> &mut dyn PartialReflect
fn as_partial_reflect_mut(&mut self) -> &mut dyn PartialReflect
Source§fn reflect_partial_eq(&self, value: &dyn PartialReflect) -> Option<bool>
fn reflect_partial_eq(&self, value: &dyn PartialReflect) -> Option<bool>
Source§fn reflect_clone(&self) -> Result<Box<dyn Reflect>, ReflectCloneError>
fn reflect_clone(&self) -> Result<Box<dyn Reflect>, ReflectCloneError>
Self
using reflection. Read moreSource§fn apply(&mut self, value: &(dyn PartialReflect + 'static))
fn apply(&mut self, value: &(dyn PartialReflect + 'static))
Source§fn clone_value(&self) -> Box<dyn PartialReflect>
fn clone_value(&self) -> Box<dyn PartialReflect>
reflect_clone
. To convert reflected values to dynamic ones, use to_dynamic
.Self
into its dynamic representation. Read moreSource§fn to_dynamic(&self) -> Box<dyn PartialReflect>
fn to_dynamic(&self) -> Box<dyn PartialReflect>
Source§fn reflect_hash(&self) -> Option<u64>
fn reflect_hash(&self) -> Option<u64>
Source§fn debug(&self, f: &mut Formatter<'_>) -> Result<(), Error>
fn debug(&self, f: &mut Formatter<'_>) -> Result<(), Error>
Source§fn is_dynamic(&self) -> bool
fn is_dynamic(&self) -> bool
Source§impl Reflect for HexLayoutwhere
HexLayout: Any + Send + Sync,
HexOrientation: FromReflect + TypePath + MaybeTyped + RegisterForReflection,
Vec2: FromReflect + TypePath + MaybeTyped + RegisterForReflection,
impl Reflect for HexLayoutwhere
HexLayout: Any + Send + Sync,
HexOrientation: FromReflect + TypePath + MaybeTyped + RegisterForReflection,
Vec2: FromReflect + TypePath + MaybeTyped + RegisterForReflection,
Source§fn as_any_mut(&mut self) -> &mut dyn Any
fn as_any_mut(&mut self) -> &mut dyn Any
&mut dyn Any
. Read moreSource§fn into_reflect(self: Box<Self>) -> Box<dyn Reflect>
fn into_reflect(self: Box<Self>) -> Box<dyn Reflect>
Source§fn as_reflect(&self) -> &dyn Reflect
fn as_reflect(&self) -> &dyn Reflect
Source§fn as_reflect_mut(&mut self) -> &mut dyn Reflect
fn as_reflect_mut(&mut self) -> &mut dyn Reflect
Source§impl Struct for HexLayoutwhere
HexLayout: Any + Send + Sync,
HexOrientation: FromReflect + TypePath + MaybeTyped + RegisterForReflection,
Vec2: FromReflect + TypePath + MaybeTyped + RegisterForReflection,
impl Struct for HexLayoutwhere
HexLayout: Any + Send + Sync,
HexOrientation: FromReflect + TypePath + MaybeTyped + RegisterForReflection,
Vec2: FromReflect + TypePath + MaybeTyped + RegisterForReflection,
Source§fn field(&self, name: &str) -> Option<&dyn PartialReflect>
fn field(&self, name: &str) -> Option<&dyn PartialReflect>
name
as a &dyn PartialReflect
.Source§fn field_mut(&mut self, name: &str) -> Option<&mut dyn PartialReflect>
fn field_mut(&mut self, name: &str) -> Option<&mut dyn PartialReflect>
name
as a
&mut dyn PartialReflect
.Source§fn field_at(&self, index: usize) -> Option<&dyn PartialReflect>
fn field_at(&self, index: usize) -> Option<&dyn PartialReflect>
index
as a
&dyn PartialReflect
.Source§fn field_at_mut(&mut self, index: usize) -> Option<&mut dyn PartialReflect>
fn field_at_mut(&mut self, index: usize) -> Option<&mut dyn PartialReflect>
index
as a &mut dyn PartialReflect
.Source§fn name_at(&self, index: usize) -> Option<&str>
fn name_at(&self, index: usize) -> Option<&str>
index
.Source§fn iter_fields(&self) -> FieldIter<'_>
fn iter_fields(&self) -> FieldIter<'_>
fn to_dynamic_struct(&self) -> DynamicStruct
Source§fn clone_dynamic(&self) -> DynamicStruct
fn clone_dynamic(&self) -> DynamicStruct
to_dynamic_struct
insteadDynamicStruct
.Source§fn get_represented_struct_info(&self) -> Option<&'static StructInfo>
fn get_represented_struct_info(&self) -> Option<&'static StructInfo>
None
if TypeInfo
is not available.Source§impl TypePath for HexLayout
impl TypePath for HexLayout
Source§fn type_path() -> &'static str
fn type_path() -> &'static str
Source§fn short_type_path() -> &'static str
fn short_type_path() -> &'static str
Source§fn type_ident() -> Option<&'static str>
fn type_ident() -> Option<&'static str>
Source§fn crate_name() -> Option<&'static str>
fn crate_name() -> Option<&'static str>
Source§impl Typed for HexLayoutwhere
HexLayout: Any + Send + Sync,
HexOrientation: FromReflect + TypePath + MaybeTyped + RegisterForReflection,
Vec2: FromReflect + TypePath + MaybeTyped + RegisterForReflection,
impl Typed for HexLayoutwhere
HexLayout: Any + Send + Sync,
HexOrientation: FromReflect + TypePath + MaybeTyped + RegisterForReflection,
Vec2: FromReflect + TypePath + MaybeTyped + RegisterForReflection,
Auto Trait Implementations§
impl Freeze for HexLayout
impl RefUnwindSafe for HexLayout
impl Send for HexLayout
impl Sync for HexLayout
impl Unpin for HexLayout
impl UnwindSafe for HexLayout
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait>
(where Trait: Downcast
) to Box<dyn Any>
, which can then be
downcast
into Box<dyn ConcreteType>
where ConcreteType
implements Trait
.Source§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait>
(where Trait: Downcast
) to Rc<Any>
, which can then be further
downcast
into Rc<ConcreteType>
where ConcreteType
implements Trait
.Source§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &Any
’s vtable from &Trait
’s.Source§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &mut Any
’s vtable from &mut Trait
’s.Source§impl<T> DowncastSend for T
impl<T> DowncastSend for T
Source§impl<T> DynamicTypePath for Twhere
T: TypePath,
impl<T> DynamicTypePath for Twhere
T: TypePath,
Source§fn reflect_type_path(&self) -> &str
fn reflect_type_path(&self) -> &str
TypePath::type_path
.Source§fn reflect_short_type_path(&self) -> &str
fn reflect_short_type_path(&self) -> &str
Source§fn reflect_type_ident(&self) -> Option<&str>
fn reflect_type_ident(&self) -> Option<&str>
TypePath::type_ident
.Source§fn reflect_crate_name(&self) -> Option<&str>
fn reflect_crate_name(&self) -> Option<&str>
TypePath::crate_name
.Source§fn reflect_module_path(&self) -> Option<&str>
fn reflect_module_path(&self) -> Option<&str>
Source§impl<T> DynamicTyped for Twhere
T: Typed,
impl<T> DynamicTyped for Twhere
T: Typed,
Source§fn reflect_type_info(&self) -> &'static TypeInfo
fn reflect_type_info(&self) -> &'static TypeInfo
Typed::type_info
.Source§impl<S> GetField for Swhere
S: Struct,
impl<S> GetField for Swhere
S: Struct,
Source§impl<T> GetPath for T
impl<T> GetPath for T
Source§fn reflect_path<'p>(
&self,
path: impl ReflectPath<'p>,
) -> Result<&(dyn PartialReflect + 'static), ReflectPathError<'p>>
fn reflect_path<'p>( &self, path: impl ReflectPath<'p>, ) -> Result<&(dyn PartialReflect + 'static), ReflectPathError<'p>>
path
. Read moreSource§fn reflect_path_mut<'p>(
&mut self,
path: impl ReflectPath<'p>,
) -> Result<&mut (dyn PartialReflect + 'static), ReflectPathError<'p>>
fn reflect_path_mut<'p>( &mut self, path: impl ReflectPath<'p>, ) -> Result<&mut (dyn PartialReflect + 'static), ReflectPathError<'p>>
path
. Read moreSource§fn path<'p, T>(
&self,
path: impl ReflectPath<'p>,
) -> Result<&T, ReflectPathError<'p>>where
T: Reflect,
fn path<'p, T>(
&self,
path: impl ReflectPath<'p>,
) -> Result<&T, ReflectPathError<'p>>where
T: Reflect,
path
. Read moreSource§fn path_mut<'p, T>(
&mut self,
path: impl ReflectPath<'p>,
) -> Result<&mut T, ReflectPathError<'p>>where
T: Reflect,
fn path_mut<'p, T>(
&mut self,
path: impl ReflectPath<'p>,
) -> Result<&mut T, ReflectPathError<'p>>where
T: Reflect,
path
. Read more