Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import strformat
- type
- WidgetKind = enum
- Frame, Separator, Header, Label, Button
- ButtonKind = enum
- IconButton, TextButton
- type Widget* = ref object
- id*:int
- x*, y*, w*, h*:int
- case kind:WidgetKind
- of Frame, Separator: cells*:seq[seq[int]]
- of Label, Header: text_field*:string # text
- of Button:
- case button_kind:ButtonKind
- of IconButton: button_icon:int
- of TextButton: button_text:string # text
- hit_on_release*:bool
- is_toggle*:bool
- template text*(self:Widget):string =
- assert(self.kind == Button and self.button_kind == TextButton or self.kind in [Label, Header])
- if self.kind == Label:
- self.text_field
- elif self.kind == Button and self.button_kind == TextButton:
- self.button_text
- template `text=`*(self:Widget, text:string)=
- assert(self.kind == Button and self.button_kind == TextButton or self.kind in [Label, Header])
- if self.kind == Label:
- self.text_field = text
- elif self.kind == Button and self.button_kind == TextButton:
- self.button_text = text
- template icon*(self:Widget):int =
- assert(self.kind == Button and self.button_kind == IconButton)
- if self.kind == Button and self.button_kind == IconButton:
- self.button_icon
- template `icon=`*(self:Widget, icon:int) =
- assert(self.kind == Button and self.button_kind == IconButton)
- if self.kind == Button and self.button_kind == IconButton:
- self.button_icon = icon
- proc `$`*(self:Widget):string =
- case self.kind
- of Label: echo fmt "Label(x: {self.x}, y: {self.y}, text: {self.text_field})"
- of Button:
- case self.button_kind
- of TextButton: echo fmt "TextButton(x: {self.x}, y: {self.y}, text: {self.button_text})"
- of IconButton: echo fmt "IconButton(x: {self.x}, y: {self.y}, icon: {self.button_icon})"
- else: discard
- proc new_widget(kind:WidgetKind, x, y, w, h:int):Widget =
- result = Widget(
- kind : kind,
- x : x,
- y : y,
- w : w,
- h : h
- )
- proc new_label*(x, y:int, text:string):Widget =
- result = new_widget(Label, x, y, 0, 0)
- result.text = text
- proc new_icon_button*(x, y, icon:int):Widget =
- result = new_widget(Button, x, y, 1, 1)
- result.button_kind = IconButton
- result.icon = icon
- result.hit_on_release = false
- result.is_toggle = false
- proc new_text_button*(x, y:int, text:string):Widget =
- result = new_widget(Button, x, y, 1, 1)
- result.button_kind = TextButton
- result.text = text
- result.hit_on_release = false
- result.is_toggle = false
- # TESTS
- # -----------------------------------------------------------------------------
- var l = new_label(10, 5, "derp")
- var ib = new_icon_button(10, 10, 254)
- var tb = new_text_button(10, 20, "bla bla")
- echo l # >>> Label(x:10, y:5, text: derp)
- echo ib # >>> IconButton(x:10, y:10, icon: 254)
- echo tb # >>> TextButton(x:10, y:20, text: bla bla)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement