keebie/scripts/key_props.gd

142 lines
2.7 KiB
GDScript

@tool
class_name KeyProps extends Resource
const CHAR_MAIN = "char_main"
const CHAR_SHIFT = "char_shift"
const CHAR_ALT = "char_alt"
const CHAR_ALT_SHIFT = "char_alt_shift"
const KEY = "key"
const LOC = "loc"
const W = "w"
const H = "h"
const X = "x"
const Y = "y"
const W2 = "w2"
const H2 = "h2"
const X2 = "x2"
const Y2 = "y2"
const R = "r"
const PX = "rx"
const PY = "ry"
const NUB = "nub"
var keycode: Key = KEY_NONE
var location: KeyLocation = KEY_LOCATION_UNSPECIFIED
var width: float = 1
var height: float = 1
var x: float = 0
var y: float = 0
var width2: float = 1
var height2: float = 1
var x2: float = 0
var y2: float = 0
var angle: float = -INF
var pivot_x: float = -INF
var pivot_y: float = -INF
var homing_nub: bool = false
var main_char: String
var shift_char: String
var alt_char: String
var alt_shift_char: String
func is_unicode() -> bool:
return OS.is_keycode_unicode(keycode)
func is_player_pos_key() -> bool:
return KeyHelper.ADJACENCY_MAP.has(keycode)
func has_secondary_rect() -> bool:
return width2 != 1 or height2 != 1 or x2 != 0 or y2 != 0
func has_angle() -> bool:
return angle != -INF
func has_pivot_x() -> bool:
return pivot_x != -INF
func has_pivot_y() -> bool:
return pivot_y != -INF
func props_from_dict(dict: Dictionary) -> KeyProps:
if dict.has(KEY):
keycode = dict[KEY]
if dict.has(LOC):
location = dict[LOC]
if dict.has(W):
width = dict[W]
if dict.has(H):
height = dict[H]
if dict.has(X):
x = dict[X]
if dict.has(Y):
y = dict[Y]
if dict.has(W2):
width2 = dict[W2]
if dict.has(H2):
height2 = dict[H2]
if dict.has(X2):
x2 = dict[X2]
if dict.has(Y2):
y2 = dict[Y2]
if dict.has(R):
angle = dict[R]
if dict.has(PX):
pivot_x = dict[PX]
if dict.has(PY):
pivot_y = dict[PY]
if dict.has(NUB):
homing_nub = dict[NUB]
return self
func chars_from_dict(dict: Dictionary, override: bool = true) -> KeyProps:
if dict.has(CHAR_MAIN) and (override or not main_char):
main_char = dict[CHAR_MAIN]
if dict.has(CHAR_SHIFT) and (override or not shift_char):
shift_char = dict[CHAR_SHIFT]
if dict.has(CHAR_ALT) and (override or not alt_char):
alt_char = dict[CHAR_ALT]
if dict.has(CHAR_ALT_SHIFT) and (override or not alt_shift_char):
alt_shift_char = dict[CHAR_ALT_SHIFT]
if main_char == shift_char:
shift_char = ""
if main_char == alt_char:
alt_char = ""
if alt_char == alt_shift_char or shift_char == alt_shift_char:
alt_shift_char = ""
return self
func chars_to_dict() -> Dictionary[String, String]:
var dict: Dictionary[String, String] = {}
if main_char:
dict[CHAR_MAIN] = main_char
if shift_char:
dict[CHAR_SHIFT] = shift_char
if alt_char:
dict[CHAR_ALT] = alt_char
if alt_shift_char:
dict[CHAR_ALT_SHIFT] = alt_shift_char
return dict