SHARE
TWEET

Untitled

a guest Jul 22nd, 2019 53 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import strformat
  2.  
  3. type
  4.     WidgetKind = enum
  5.         Frame, Separator, Header, Label, Button
  6.     ButtonKind = enum
  7.         IconButton, TextButton
  8.  
  9. type Widget* = ref object
  10.     id*:int
  11.     x*, y*, w*, h*:int
  12.     case kind:WidgetKind
  13.         of Frame, Separator:   cells*:seq[seq[int]]
  14.         of Label, Header:      text_field*:string   # text
  15.         of Button:
  16.             case button_kind:ButtonKind
  17.                 of IconButton:  button_icon:int
  18.                 of TextButton:  button_text:string  # text
  19.             hit_on_release*:bool
  20.             is_toggle*:bool
  21.  
  22.  
  23.  
  24.  
  25. template text*(self:Widget):string =
  26.     assert(self.kind == Button and self.button_kind == TextButton or self.kind in [Label, Header])
  27.     if self.kind == Label:
  28.         self.text_field
  29.     elif self.kind == Button and self.button_kind == TextButton:
  30.         self.button_text
  31.  
  32. template `text=`*(self:Widget, text:string)=
  33.     assert(self.kind == Button and self.button_kind == TextButton or self.kind in [Label, Header])
  34.     if self.kind == Label:
  35.         self.text_field = text
  36.     elif self.kind == Button and self.button_kind == TextButton:
  37.         self.button_text = text
  38.  
  39.  
  40. template icon*(self:Widget):int =
  41.     assert(self.kind == Button and self.button_kind == IconButton)
  42.     if self.kind == Button and self.button_kind == IconButton:
  43.         self.button_icon
  44.  
  45. template `icon=`*(self:Widget, icon:int) =
  46.     assert(self.kind == Button and self.button_kind == IconButton)
  47.     if self.kind == Button and self.button_kind == IconButton:
  48.         self.button_icon = icon
  49.  
  50.  
  51.  
  52.  
  53. proc `$`*(self:Widget):string =
  54.     case self.kind
  55.         of Label:   echo fmt "Label(x: {self.x}, y: {self.y}, text: {self.text_field})"
  56.         of Button:
  57.             case self.button_kind
  58.                 of TextButton:   echo fmt "TextButton(x: {self.x}, y: {self.y}, text: {self.button_text})"
  59.                 of IconButton:   echo fmt "IconButton(x: {self.x}, y: {self.y}, icon: {self.button_icon})"
  60.         else: discard
  61.  
  62. proc new_widget(kind:WidgetKind, x, y, w, h:int):Widget =
  63.     result = Widget(
  64.         kind : kind,
  65.         x : x,
  66.         y : y,
  67.         w : w,
  68.         h : h
  69.     )
  70.  
  71.  
  72.  
  73.  
  74. proc new_label*(x, y:int, text:string):Widget =
  75.     result = new_widget(Label, x, y, 0, 0)
  76.     result.text = text
  77.  
  78. proc new_icon_button*(x, y, icon:int):Widget =
  79.     result = new_widget(Button, x, y, 1, 1)
  80.     result.button_kind = IconButton
  81.     result.icon = icon
  82.     result.hit_on_release = false
  83.     result.is_toggle = false
  84.  
  85. proc new_text_button*(x, y:int, text:string):Widget =
  86.     result = new_widget(Button, x, y, 1, 1)
  87.     result.button_kind = TextButton
  88.     result.text = text
  89.     result.hit_on_release = false
  90.     result.is_toggle = false
  91.  
  92.  
  93.  
  94.  
  95. #       TESTS
  96. # -----------------------------------------------------------------------------
  97. var l = new_label(10, 5, "derp")
  98. var ib = new_icon_button(10, 10, 254)
  99. var tb = new_text_button(10, 20, "bla bla")
  100.  
  101. echo l      #  >>> Label(x:10, y:5, text: derp)
  102. echo ib     #  >>> IconButton(x:10, y:10, icon: 254)
  103. echo tb     #  >>> TextButton(x:10, y:20, text: bla bla)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top