kle2xy.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. """ Original code from https://github.com/skullydazed/kle2xy
  2. """
  3. import hjson
  4. from decimal import Decimal
  5. class KLE2xy(list):
  6. """Abstract interface for interacting with a KLE layout.
  7. """
  8. def __init__(self, layout=None, name='', invert_y=True):
  9. super(KLE2xy, self).__init__()
  10. self.name = name
  11. self.invert_y = invert_y
  12. self.key_width = Decimal('19.05')
  13. self.key_skel = {'decal': False, 'border_color': 'none', 'keycap_profile': '', 'keycap_color': 'grey', 'label_color': 'black', 'label_size': 3, 'label_style': 4, 'width': Decimal('1'), 'height': Decimal('1'), 'x': Decimal('0'), 'y': Decimal('0')}
  14. self.rows = Decimal(0)
  15. self.columns = Decimal(0)
  16. if layout:
  17. self.parse_layout(layout)
  18. @property
  19. def width(self):
  20. """Returns the width of the keyboard plate.
  21. """
  22. return (Decimal(self.columns) * self.key_width) + self.key_width / 2
  23. @property
  24. def height(self):
  25. """Returns the height of the keyboard plate.
  26. """
  27. return (self.rows * self.key_width) + self.key_width / 2
  28. @property
  29. def size(self):
  30. """Returns the size of the keyboard plate.
  31. """
  32. return (self.width, self.height)
  33. def attrs(self, properties):
  34. """Parse the keyboard properties dictionary.
  35. """
  36. # FIXME: Store more than just the keyboard name.
  37. if 'name' in properties:
  38. self.name = properties['name']
  39. def parse_layout(self, layout): # noqa FIXME(skullydazed): flake8 says this has a complexity of 25, it should be refactored.
  40. # Wrap this in a dictionary so hjson will parse KLE raw data
  41. layout = '{"layout": [' + layout + ']}'
  42. layout = hjson.loads(layout)['layout']
  43. # Initialize our state machine
  44. current_key = self.key_skel.copy()
  45. current_row = Decimal(0)
  46. current_col = Decimal(0)
  47. current_x = 0
  48. current_y = self.key_width / 2
  49. if isinstance(layout[0], dict):
  50. self.attrs(layout[0])
  51. layout = layout[1:]
  52. for row_num, row in enumerate(layout):
  53. self.append([])
  54. # Process the current row
  55. for key in row:
  56. if isinstance(key, dict):
  57. if 'w' in key and key['w'] != Decimal(1):
  58. current_key['width'] = Decimal(key['w'])
  59. if 'w2' in key and 'h2' in key and key['w2'] == 1.5 and key['h2'] == 1:
  60. # FIXME: ISO Key uses these params: {x:0.25,w:1.25,h:2,w2:1.5,h2:1,x2:-0.25}
  61. current_key['isoenter'] = True
  62. if 'h' in key and key['h'] != Decimal(1):
  63. current_key['height'] = Decimal(key['h'])
  64. if 'a' in key:
  65. current_key['label_style'] = self.key_skel['label_style'] = int(key['a'])
  66. if current_key['label_style'] < 0:
  67. current_key['label_style'] = 0
  68. elif current_key['label_style'] > 9:
  69. current_key['label_style'] = 9
  70. if 'f' in key:
  71. font_size = int(key['f'])
  72. if font_size > 9:
  73. font_size = 9
  74. elif font_size < 1:
  75. font_size = 1
  76. current_key['label_size'] = self.key_skel['label_size'] = font_size
  77. if 'p' in key:
  78. current_key['keycap_profile'] = self.key_skel['keycap_profile'] = key['p']
  79. if 'c' in key:
  80. current_key['keycap_color'] = self.key_skel['keycap_color'] = key['c']
  81. if 't' in key:
  82. # FIXME: Need to do better validation, plus figure out how to support multiple colors
  83. if '\n' in key['t']:
  84. key['t'] = key['t'].split('\n')[0]
  85. if key['t'] == "0":
  86. key['t'] = "#000000"
  87. current_key['label_color'] = self.key_skel['label_color'] = key['t']
  88. if 'x' in key:
  89. current_col += Decimal(key['x'])
  90. current_x += Decimal(key['x']) * self.key_width
  91. if 'y' in key:
  92. current_row += Decimal(key['y'])
  93. current_y += Decimal(key['y']) * self.key_width
  94. if 'd' in key:
  95. current_key['decal'] = True
  96. else:
  97. current_key['name'] = key
  98. current_key['row'] = current_row
  99. current_key['column'] = current_col
  100. # Determine the X center
  101. x_center = (current_key['width'] * self.key_width) / 2
  102. current_x += x_center
  103. current_key['x'] = current_x
  104. current_x += x_center
  105. # Determine the Y center
  106. y_center = (current_key['height'] * self.key_width) / 2
  107. y_offset = y_center - (self.key_width / 2)
  108. current_key['y'] = (current_y + y_offset)
  109. # Tend to our row/col count
  110. current_col += current_key['width']
  111. if current_col > self.columns:
  112. self.columns = current_col
  113. # Invert the y-axis if neccesary
  114. if self.invert_y:
  115. current_key['y'] = -current_key['y']
  116. # Store this key
  117. self[-1].append(current_key)
  118. current_key = self.key_skel.copy()
  119. # Move to the next row
  120. current_x = 0
  121. current_y += self.key_width
  122. current_col = Decimal(0)
  123. current_row += Decimal(1)
  124. if current_row > self.rows:
  125. self.rows = Decimal(current_row)