├── LICENSE ├── README.md ├── rdgui.nimble ├── src └── rdgui │ ├── button.nim │ ├── control.nim │ ├── event.nim │ ├── label.nim │ ├── layout.nim │ ├── slider.nim │ ├── textbox.nim │ └── windows.nim └── tests ├── .gitignore ├── base.nim ├── config.nims ├── data ├── LICENSE_OpenSans.txt └── OpenSans-Regular.ttf ├── tbutton.nim ├── tcontextmenu ├── tcontextmenu.nim ├── tslider.nim ├── ttextbox ├── ttextbox.nim └── twm.nim /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 lqdev 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # rdgui 2 | 3 | **Note:** rdgui is not supported anymore and does not compile against latest rapid, it's been superseded by [rapid/ui](https://github.com/liquidev/rapid/blob/master/src/rapid/ui.nim) 4 | 5 | rdgui (pronounced _redgui_) is a modular retained GUI toolkit made for 6 | the rapid game engine. 7 | 8 | The main idea of rdgui is *reusability without complexity*. Everything rdgui 9 | does is fundamentally based on the idea of windows. You can imagine rdgui as 10 | a windowing system, like X11 or Wayland, but apart from windows, rdgui also does 11 | controls—buttons, textboxes, sliders, etc. 12 | 13 | Every component in rdgui has a defined *base implementation*, but only for 14 | handling events—rendering is defined separately in _renderers_, which are 15 | simply procs that render a given control. Whilst event handlers are shared 16 | across all instances of a given control, renderers are not, eg. one button can 17 | use a "basic" renderer, which draws the button outlined, and another can use an 18 | "emphasized" renderer, which draws the button filled. This approach is superior 19 | to using configuration files like CSS, because it's much faster—style lookups 20 | don't have to be done every frame the control is rendered. Instead, everything 21 | is defined once when the control is instantiated, and then the values remain 22 | constant every frame—no need to do expensive string manipulation and table 23 | lookups. 24 | 25 | ## Installing 26 | 27 | rdgui can be installed using nimble: 28 | ```sh 29 | $ nimble install rdgui 30 | ``` 31 | In your nimble file: 32 | ```nim 33 | requires "rdgui" 34 | ``` 35 | 36 | ## Examples 37 | 38 | ### Setting up 39 | 40 | ```nim 41 | import rapid/gfx 42 | import rdgui/windows 43 | 44 | var 45 | # we first set up a basic rapid window 46 | win = initRWindow() 47 | .size(800, 600) 48 | .title("rdgui example") 49 | surface = win.openGfx() 50 | # then, we create a window manager 51 | wm = newWindowManager(win) 52 | 53 | # then, we draw the UI 54 | surface.loop: 55 | draw ctx, step: 56 | ctx.clear(gray(127)) 57 | wm.draw(ctx, step) 58 | ``` 59 | 60 | ### Opening windows 61 | 62 | ```nim 63 | import rdgui/control 64 | 65 | # for draggable, floating windows, use ``FloatingWindow`` 66 | var myFloatingWindow = wm.newFloatingWindow(32, 32, 256, 256) 67 | # the window must be added to the wm before any controls are added to it! 68 | wm.add(myFloatingWindow) 69 | 70 | # to add controls "globally" (without displaying a window), use ``Window`` 71 | import rdgui/button 72 | var 73 | myWindow = wm.newWindow(0, 0) 74 | myButton = newButton(192, 192, 192, 32) 75 | wm.add(myWindow) 76 | myWindow.add(myButton) 77 | ``` 78 | 79 | ### Customizing the rendering 80 | 81 | ```nim 82 | import rdgui/control 83 | import rdgui/button 84 | 85 | # the renderer() template is a shortcut to creating a ControlRenderer 86 | Button.renderer(Rounded, button): 87 | ctx.begin() 88 | # (0, 0) is the position of the control—no need to do any annoying offsetting 89 | ctx.rrect(0, 0, button.width, button.height, 8) 90 | ctx.draw() 91 | 92 | # the above is equivalent to: 93 | proc ButtonRounded*(ctx: RGfxContext, step: float, ctrl: Control) = 94 | var button = ctrl.Button 95 | # (drawing code) 96 | 97 | # then, we can use the renderer when creating the button 98 | # renderers are always the last parameter of control initializers/constructors 99 | var roundedButton = newButton(32, 32, 192, 32, ButtonRounded) 100 | 101 | # renderers can have parameters by wrapping them in closures 102 | proc ButtonColored*(color: RColor): ControlRenderer = 103 | Button.prenderer(ColoredImpl, button): 104 | # prenderer declares a *private* renderer (without an export marker) 105 | ctx.begin() 106 | ctx.color = color 107 | ctx.rect(0, 0, button.width, button.height) 108 | ctx.color = gray(255) 109 | ctx.draw() 110 | result = ButtonColoredImpl 111 | 112 | var redButton = newButton(32, 64, 192, 32, ButtonColored(rgb(255, 0, 0))) 113 | 114 | # renderers can also "inherit" from each other 115 | import rapid/gfx/text 116 | var font = loadRFont("Source Code Pro.ttf", 12) 117 | 118 | proc ButtonText*(text: string): ControlRenderer = 119 | Button.prenderer(TextImpl, button): 120 | # here, we'll inherit from the built-in ButtonRd renderer 121 | # *Rd renderers are default renderers meant as placeholders 122 | ctx.ButtonRd(step, button) 123 | ctx.color = gray(0) 124 | ctx.text(font, 0, 0, text, 125 | button.width, button.height, hAlign = taCenter, vAlign = taMiddle) 126 | ctx.color = gray(255) 127 | ``` 128 | 129 | ### Creating your own controls 130 | 131 | ```nim 132 | # as an example, we'll create a counter which increments when it's clicked 133 | import rdgui/control 134 | import rdgui/event 135 | 136 | type 137 | Counter = ref object of Control 138 | min, value, max: int 139 | 140 | # every control that processes events must have an ``event`` implementation 141 | method event*(counter: Counter, ev: UIEvent) = 142 | if ev.kind == evMousePress: # trigger an increment on mouse press 143 | if counter.mouseInRect(0, 0, 64, 64): 144 | # consume the event to prevent it from propagating further into the 145 | # control tree 146 | ev.consume() 147 | inc(counter.value) 148 | if counter.value >= max: 149 | counter.value = min 150 | 151 | # all controls should also have a default renderer, usually named ``Default`` 152 | Counter.renderer(Default, counter): 153 | ctx.begin() 154 | ctx.color = gray(0) 155 | ctx.lrect(0, 0, 64, 64) 156 | ctx.draw(prLineShape) 157 | ctx.text(font, 0, 0, $counter.value, 158 | 64, 64, hAlign = taCenter, vAlign = taMiddle) 159 | ctx.color = gray(255) 160 | 161 | # controls must have an initializer 162 | proc initCounter*(counter: Counter, x, y: float, min, max: int, 163 | renderer = CounterDefault) = 164 | # the first thing the initializer must do is call 165 | # the parent type's initializer 166 | counter.initControl(x, y, renderer) 167 | # then initialize any fields, etc. 168 | counter.min = min 169 | counter.value = min 170 | counter.max = max 171 | 172 | # controls should have a constructor 173 | proc newCounter*(x, y: float, min, max: int, 174 | renderer = CounterDefault): Counter = 175 | # the constructor should create a new instance and call the initializer 176 | new(result) 177 | result.initCounter(x, y, min, max, renderer) 178 | ``` 179 | -------------------------------------------------------------------------------- /rdgui.nimble: -------------------------------------------------------------------------------- 1 | # Package 2 | 3 | version = "0.1.0" 4 | author = "liquid600pgm" 5 | description = "A modular GUI toolkit for rapid" 6 | license = "MIT" 7 | srcDir = "src" 8 | 9 | # Dependencies 10 | 11 | requires "nim >= 1.0.2" 12 | requires "rapid" 13 | -------------------------------------------------------------------------------- /src/rdgui/button.nim: -------------------------------------------------------------------------------- 1 | #-- 2 | # rdgui - a modular GUI toolkit for rapid 3 | # copyright (C) 2019 iLiquid 4 | #-- 5 | 6 | import rapid/gfx 7 | 8 | import control 9 | import event 10 | 11 | type 12 | Button* = ref object of Control 13 | fWidth, fHeight: float 14 | fPressed: bool 15 | onClick*: proc () 16 | 17 | proc width*(button: Button): float = button.fWidth 18 | proc height*(button: Button): float = button.fHeight 19 | proc `width=`*(button: Button, width: float) = 20 | button.fWidth = width 21 | proc `height=`*(button: Button, height: float) = 22 | button.fHeight = height 23 | 24 | proc pressed*(button: Button): bool = button.fPressed 25 | 26 | method onEvent*(button: Button, ev: UIEvent) = 27 | if ev.kind in {evMousePress, evMouseRelease}: 28 | if button.hasMouse and ev.kind == evMousePress: 29 | ev.consume() 30 | button.fPressed = true 31 | elif ev.kind == evMouseRelease: 32 | if button.pressed: 33 | ev.consume() 34 | if button.onClick != nil: 35 | button.onClick() 36 | button.fPressed = false 37 | 38 | Button.renderer(Rd, button): 39 | ctx.begin() 40 | ctx.color = gray(0, 64) 41 | ctx.lrect(0, 0, button.width, button.height) 42 | ctx.draw(prLineShape) 43 | if button.hasMouse or button.pressed: 44 | ctx.begin() 45 | ctx.color = 46 | if button.pressed: gray(0, 64) 47 | elif button.hasMouse: gray(0, 32) 48 | else: gray(0, 0) 49 | ctx.rect(0, 0, button.width, button.height) 50 | ctx.draw() 51 | ctx.color = gray(255) 52 | 53 | proc initButton*(button: Button, x, y, width, height: float, 54 | renderer = ButtonRd) = 55 | button.initControl(x, y, renderer) 56 | button.width = width 57 | button.height = height 58 | 59 | proc newButton*(x, y, width, height: float, renderer = ButtonRd): Button = 60 | new(result) 61 | result.initButton(x, y, width, height, renderer) 62 | -------------------------------------------------------------------------------- /src/rdgui/control.nim: -------------------------------------------------------------------------------- 1 | #-- 2 | # rdgui - a modular GUI toolkit for rapid 3 | # copyright (C) 2019 iLiquid 4 | #-- 5 | 6 | import rapid/gfx 7 | 8 | import event 9 | 10 | #-- 11 | # Control 12 | #-- 13 | 14 | type 15 | ControlRenderer* = proc (ctx: RGfxContext, step: float, ctrl: Control) 16 | Control* = ref object of RootObj 17 | rwin*: RWindow 18 | parent*: Control 19 | pos*: Vec2[float] 20 | renderer*: ControlRenderer 21 | visible*: bool 22 | lastMousePos*: Vec2[float] 23 | containProc: proc () 24 | 25 | method width*(ctrl: Control): float {.base.} = 0 26 | method height*(ctrl: Control): float {.base.} = 0 27 | 28 | proc screenPos*(ctrl: Control): Vec2[float] = 29 | if ctrl.parent == nil: ctrl.pos 30 | else: ctrl.parent.screenPos + ctrl.pos 31 | 32 | proc mousePos*(ctrl: Control): Vec2[float] = 33 | let mouse = vec2(ctrl.rwin.mouseX, ctrl.rwin.mouseY) 34 | result = mouse - ctrl.screenPos 35 | 36 | proc pointInRect*(ctrl: Control, point: Vec2[float], x, y, w, h: float): bool = 37 | let 38 | sp = ctrl.screenPos 39 | a = sp + vec2(x, y) 40 | b = sp + vec2(x + w, y + h) 41 | result = point.x >= a.x and point.y >= a.y and 42 | point.x < b.x and point.y < b.y 43 | 44 | proc mouseInRect*(ctrl: Control, x, y, w, h: float): bool = 45 | ctrl.pointInRect(vec2(ctrl.rwin.mouseX, ctrl.rwin.mouseY), x, y, w, h) 46 | 47 | proc hasMouse*(ctrl: Control): bool = 48 | ## Shortcut for ``ctrl.mouseInRect(0, 0, ctrl.width, ctrl.height)``. 49 | ctrl.mouseInRect(0, 0, ctrl.width, ctrl.height) 50 | 51 | proc pointInCircle*(ctrl: Control, point: Vec2[float], x, y, r: float): bool = 52 | let 53 | sp = ctrl.screenPos 54 | dx = (x + sp.x) - point.x 55 | dy = (y + sp.y) - point.y 56 | result = dx * dx + dy * dy <= r * r 57 | 58 | proc mouseInCircle*(ctrl: Control, x, y, r: float): bool = 59 | ctrl.pointInCircle(vec2(ctrl.rwin.mouseX, ctrl.rwin.mouseY), x, y, r) 60 | 61 | proc onContain*(ctrl: Control, callback: proc ()) = 62 | ## Specify a proc to call when the control is contained within another 63 | ## control. If the control creates any controls on init, this is where you 64 | ## should create them. 65 | ctrl.containProc = callback 66 | 67 | proc contain*(parent: Control, child: Control) = 68 | child.rwin = parent.rwin 69 | child.parent = parent 70 | if child.containProc != nil: child.containProc() 71 | 72 | proc initControl*(ctrl: Control, x, y: float, rend: ControlRenderer) = 73 | ctrl.pos = vec2(x, y) 74 | ctrl.renderer = rend 75 | ctrl.visible = true 76 | 77 | template renderer*(T, name, varName, body) {.dirty.} = 78 | ## Shortcut for declaring a ControlRenderer. 79 | let `T name`*: ControlRenderer = 80 | proc (ctx: RGfxContext, step: float, ctrl: Control) = 81 | var varName = ctrl.T 82 | body 83 | 84 | template prenderer*(T, name, varName, body) {.dirty.} = 85 | ## Shortcur for declaring a private ControlRenderer. 86 | let `T name`: ControlRenderer = 87 | proc (ctx: RGfxContext, step: float, ctrl: Control) = 88 | var varName = ctrl.T 89 | body 90 | 91 | proc draw*(ctrl: Control, ctx: RGfxContext, step: float) = 92 | if ctrl.visible: 93 | # don't use `ctx.transform()` here to avoid unnecessary matrix copies 94 | ctx.translate(ctrl.pos.x, ctrl.pos.y) 95 | assert not ctrl.renderer.isNil, "control must have a non-nil renderer" 96 | ctrl.renderer(ctx, step, ctrl) 97 | ctx.translate(-ctrl.pos.x, -ctrl.pos.y) 98 | 99 | method onEvent*(ctrl: Control, ev: UIEvent) {.base.} = 100 | discard 101 | 102 | proc event*(ctrl: Control, ev: UIEvent) = 103 | ## Send an event to a control. 104 | if ctrl.visible and ev.sendable: 105 | ctrl.onEvent(ev) 106 | if ev.kind == evMouseMove: 107 | let 108 | (x, y, w, h) = (0.0, 0.0, ctrl.width, ctrl.height) 109 | hadMouse = ctrl.pointInRect(ctrl.lastMousePos, x, y, w, h) 110 | hasMouse = ctrl.pointInRect(ev.mousePos, x, y, w, h) 111 | if not hadMouse and hasMouse: 112 | ctrl.onEvent(mouseEnterEvent()) 113 | elif hadMouse and not hasMouse: 114 | ctrl.onEvent(mouseLeaveEvent()) 115 | ctrl.lastMousePos = ev.mousePos 116 | 117 | #-- 118 | # Box 119 | #-- 120 | 121 | type 122 | Box* = ref object of Control 123 | children*: seq[Control] 124 | 125 | method width*(box: Box): float = 126 | for child in box.children: 127 | let realWidth = child.pos.x + child.width 128 | if realWidth > result: 129 | result = realWidth 130 | 131 | method height*(box: Box): float = 132 | for child in box.children: 133 | let realHeight = child.pos.y + child.height 134 | if realHeight > result: 135 | result = realHeight 136 | 137 | Box.renderer(Children, box): 138 | for child in box.children: 139 | child.draw(ctx, step) 140 | 141 | method onEvent*(box: Box, ev: UIEvent) = 142 | for i in countdown(box.children.len - 1, 0): 143 | box.children[i].event(ev) 144 | if ev.consumed: 145 | break 146 | 147 | proc initBox*(box: Box, x, y: float, rend = BoxChildren) = 148 | box.initControl(x, y, rend) 149 | 150 | proc newBox*(x, y: float, rend = BoxChildren): Box = 151 | result = Box() 152 | result.initBox(x, y, rend) 153 | 154 | proc add*(box: Box, child: Control): Box {.discardable.} = 155 | result = box 156 | result.children.add(child) 157 | result.contain(child) 158 | 159 | proc bringToTop*(box: Box, child: Control) = 160 | let i = box.children.find(child) 161 | box.children.delete(i) 162 | box.children.add(child) 163 | -------------------------------------------------------------------------------- /src/rdgui/event.nim: -------------------------------------------------------------------------------- 1 | #-- 2 | # rdgui - a modular GUI toolkit for rapid 3 | # copyright (C) 2019 iLiquid 4 | #-- 5 | 6 | import unicode 7 | 8 | export unicode 9 | 10 | import rapid/gfx 11 | 12 | type 13 | UiEventKind* = enum 14 | evMousePress = "mousePress" 15 | evMouseRelease = "mouseRelease" 16 | evMouseMove = "mouseMove" 17 | evMouseScroll = "mouseScroll" 18 | evMouseEnter = "mouseEnter" 19 | evMouseLeave = "mouseLeave" 20 | evKeyPress = "keyPress" 21 | evKeyRelease = "keyRelease" 22 | evKeyChar = "keyChar" 23 | evKeyRepeat = "keyRepeat" 24 | UiEvent* = ref object 25 | fConsumed: bool 26 | case kind: UIEventKind 27 | of evMousePress, evMouseRelease: 28 | mbButton: MouseButton 29 | mbPos: Vec2[float] 30 | mbMods: RModKeys 31 | of evMouseMove: 32 | mmPos: Vec2[float] 33 | of evMouseScroll: 34 | sPos: Vec2[float] 35 | of evMouseEnter, evMouseLeave: 36 | discard # sent after an evMouseMove 37 | of evKeyPress, evKeyRelease, evKeyRepeat: 38 | kbKey: Key 39 | kbScancode: int 40 | kbMods: RModKeys 41 | of evKeyChar: 42 | kcRune: Rune 43 | kcMods: RModKeys 44 | UiEventHandler* = proc (event: UiEvent) 45 | 46 | proc kind*(ev: UiEvent): UiEventKind = ev.kind 47 | proc consumed*(ev: UiEvent): bool = ev.fConsumed 48 | proc unique*(ev: UiEvent): bool = ev.kind in {evMouseEnter, evMouseLeave} 49 | proc sendable*(ev: UiEvent): bool = not ev.unique and not ev.consumed 50 | 51 | proc mouseButton*(ev: UiEvent): MouseButton = ev.mbButton 52 | proc mousePos*(ev: UiEvent): Vec2[float] = 53 | case ev.kind 54 | of evMousePress, evMouseRelease: ev.mbPos 55 | of evMouseMove: ev.mmPos 56 | else: vec2(0.0, 0.0) 57 | proc scrollPos*(ev: UiEvent): Vec2[float] = ev.sPos 58 | 59 | proc key*(ev: UiEvent): Key = ev.kbKey 60 | proc scancode*(ev: UiEvent): int = ev.kbScancode 61 | proc rune*(ev: UiEvent): Rune = ev.kcRune 62 | 63 | proc modKeys*(ev: UiEvent): RModKeys = 64 | case ev.kind 65 | of evMousePress, evMouseRelease: ev.mbMods 66 | of evKeyPress, evKeyRelease: ev.kbMods 67 | of evKeyChar: ev.kcMods 68 | else: {} 69 | 70 | proc consume*(ev: UiEvent) = 71 | ev.fConsumed = true 72 | 73 | proc mousePressEvent*(button: MouseButton, pos: Vec2[float], 74 | mods: RModKeys): UiEvent = 75 | UiEvent(kind: evMousePress, mbButton: button, mbPos: pos, mbMods: mods) 76 | 77 | proc mouseReleaseEvent*(button: MouseButton, pos: Vec2[float], 78 | mods: RModKeys): UiEvent = 79 | UiEvent(kind: evMouseRelease, mbButton: button, mbPos: pos, mbMods: mods) 80 | 81 | proc mouseMoveEvent*(pos: Vec2[float]): UiEvent = 82 | UiEvent(kind: evMouseMove, mmPos: pos) 83 | 84 | proc mouseScrollEvent*(pos: Vec2[float]): UiEvent = 85 | UiEvent(kind: evMouseScroll, sPos: pos) 86 | 87 | proc mouseEnterEvent*(): UiEvent = 88 | UiEvent(kind: evMouseEnter) 89 | 90 | proc mouseLeaveEvent*(): UiEvent = 91 | UiEvent(kind: evMouseLeave) 92 | 93 | proc keyPressEvent*(key: Key, scancode: int, mods: RModKeys): UiEvent = 94 | UiEvent(kind: evKeyPress, kbKey: key, kbScancode: scancode, kbMods: mods) 95 | 96 | proc keyReleaseEvent*(key: Key, scancode: int, mods: RModKeys): UiEvent = 97 | UiEvent(kind: evKeyRelease, kbKey: key, kbScancode: scancode, kbMods: mods) 98 | 99 | proc keyRepeatEvent*(key: Key, scancode: int, mods: RModKeys): UiEvent = 100 | UiEvent(kind: evKeyRepeat, kbKey: key, kbScancode: scancode, kbMods: mods) 101 | 102 | proc keyCharEvent*(rune: Rune, mods: RModKeys): UiEvent = 103 | UiEvent(kind: evKeyChar, kcRune: rune, kcMods: mods) 104 | 105 | proc registerEvents*(win: RWindow, handler: UiEventHandler) = 106 | win.onMousePress do (button: MouseButton, mods: RModKeys): 107 | handler(mousePressEvent(button, vec2(win.mouseX, win.mouseY), mods)) 108 | win.onMouseRelease do (button: MouseButton, mods: RModKeys): 109 | handler(mouseReleaseEvent(button, vec2(win.mouseX, win.mouseY), mods)) 110 | win.onCursorMove do (x, y: float): 111 | handler(mouseMoveEvent(vec2(x, y))) 112 | win.onScroll do (x, y: float): 113 | handler(mouseScrollEvent(vec2(x, y))) 114 | 115 | win.onKeyPress do (key: Key, scancode: int, mods: RModKeys): 116 | handler(keyPressEvent(key, scancode, mods)) 117 | win.onKeyRelease do (key: Key, scancode: int, mods: RModKeys): 118 | handler(keyReleaseEvent(key, scancode, mods)) 119 | win.onKeyRepeat do (key: Key, scancode: int, mods: RModKeys): 120 | handler(keyRepeatEvent(key, scancode, mods)) 121 | win.onChar do (rune: Rune, mods: RModKeys): 122 | handler(keyCharEvent(rune, mods)) 123 | 124 | proc `$`*(ev: UiEvent): string = 125 | result.add($ev.kind & ' ') 126 | case ev.kind 127 | of evMousePress, evMouseRelease: 128 | result.add($ev.mouseButton & " mods=" & $ev.modKeys) 129 | of evMouseMove: 130 | result.add($ev.mousePos) 131 | of evMouseScroll: 132 | result.add($ev.scrollPos) 133 | of evMouseEnter, evMouseLeave: discard 134 | of evKeyPress, evKeyRelease, evKeyRepeat: 135 | result.add($ev.key & '(' & $ev.scancode & ") mods=" & $ev.modKeys) 136 | of evKeyChar: 137 | result.add($ev.rune.int & '(' & $ev.rune & ") mods=" & $ev.modKeys) 138 | -------------------------------------------------------------------------------- /src/rdgui/label.nim: -------------------------------------------------------------------------------- 1 | import rapid/gfx 2 | import rapid/gfx/text 3 | 4 | import control 5 | 6 | type 7 | Label* = ref object of Control 8 | text*: string 9 | font*: RFont 10 | fontSize*: int 11 | 12 | method width*(label: Label): float = 13 | let oldFontSize = label.font.height 14 | label.font.height = label.fontSize 15 | result = label.font.widthOf(label.text) 16 | label.font.height = oldFontSize 17 | method height*(label: Label): float = 18 | label.fontSize.float * label.font.lineSpacing 19 | 20 | Label.renderer(Default, label): 21 | let oldFontSize = label.font.height 22 | label.font.height = label.fontSize 23 | ctx.text(label.font, 0, 0, label.text) 24 | label.font.height = oldFontSize 25 | 26 | proc initLabel*(label: Label, x, y: float, text: string, font: RFont, 27 | fontSize = 14, rend = LabelDefault) = 28 | label.initControl(x, y, rend) 29 | label.text = text 30 | label.font = font 31 | label.fontSize = fontSize 32 | 33 | proc newLabel*(x, y: float, text: string, font: RFont, fontSize = 14): Label = 34 | new(result) 35 | result.initLabel(x, y, text, font, fontSize) 36 | -------------------------------------------------------------------------------- /src/rdgui/layout.nim: -------------------------------------------------------------------------------- 1 | ## Auto-layout primitives for Boxes. 2 | 3 | import glm/vec 4 | 5 | import control 6 | 7 | proc listHorizontal*(box: Box, padding, spacing: float) = 8 | var x = padding 9 | for ctrl in box.children: 10 | ctrl.pos = vec2(x, padding) 11 | x += ctrl.width + spacing 12 | 13 | proc listVertical*(box: Box, padding, spacing: float) = 14 | var y = padding 15 | for ctrl in box.children: 16 | ctrl.pos = vec2(padding, y) 17 | y += ctrl.height + spacing 18 | -------------------------------------------------------------------------------- /src/rdgui/slider.nim: -------------------------------------------------------------------------------- 1 | import rapid/gfx 2 | 3 | import control 4 | import event 5 | 6 | type 7 | Slider* = ref object of Control 8 | fWidth, fHeight: float 9 | fValue, min*, max*, step*: float 10 | dragging: bool 11 | onChange*: proc (oldVal, newVal: float) 12 | 13 | method width*(slider: Slider): float = slider.fWidth 14 | method height*(slider: Slider): float = slider.fHeight 15 | 16 | proc value*(slider: Slider): float = 17 | result = round(slider.fValue / slider.step) * slider.step 18 | 19 | proc `value=`*(slider: Slider, val: float) = 20 | slider.fValue = val 21 | 22 | method onEvent*(slider: Slider, ev: UiEvent) = 23 | proc updateValue() = 24 | let 25 | oldVal = slider.value 26 | sp = slider.screenPos 27 | t = clamp((slider.rwin.mouseX - sp.x) / slider.width, 0.0, 1.0) 28 | slider.value = mix(slider.min, slider.max, t) 29 | if slider.value != oldVal and slider.onChange != nil: 30 | slider.onChange(oldVal, slider.value) 31 | 32 | if ev.kind in {evMousePress, evMouseRelease}: 33 | slider.dragging = slider.mouseInRect(0, 0, slider.width, slider.height) and 34 | ev.kind == evMousePress 35 | if slider.dragging: 36 | updateValue() 37 | ev.consume() 38 | elif ev.kind == evMouseMove: 39 | if slider.dragging: 40 | updateValue() 41 | ev.consume() 42 | 43 | Slider.renderer(Rd, slider): 44 | ctx.begin() 45 | ctx.color = gray(192) 46 | ctx.rect(0, slider.height / 2 - 1, slider.width, 2) 47 | let x = (slider.value - slider.min) / (slider.max - slider.min) * slider.width 48 | ctx.color = gray(128) 49 | ctx.rect(x, 0, 2, slider.height) 50 | ctx.color = gray(255) 51 | ctx.draw() 52 | 53 | proc initSlider*(slider: Slider, x, y, width, height: float, 54 | min, max: float, value = 0.0, step = 0.01, 55 | rend = SliderRd) = 56 | slider.initControl(x, y, rend) 57 | slider.fWidth = width 58 | slider.fHeight = height 59 | slider.fValue = value 60 | slider.min = min 61 | slider.max = max 62 | slider.step = step 63 | 64 | proc newSlider*(x, y, width, height, min, max: float, 65 | value = 0.0, step = 0.01, rend = SliderRd): Slider = 66 | new(result) 67 | result.initSlider(x, y, width, height, min, max, value, step, rend) 68 | -------------------------------------------------------------------------------- /src/rdgui/textbox.nim: -------------------------------------------------------------------------------- 1 | import unicode 2 | 3 | import rapid/gfx/text 4 | import rapid/gfx 5 | import rdgui/control 6 | import rdgui/event 7 | 8 | type 9 | TextBox* = ref object of Control 10 | fWidth, fHeight: float 11 | fText: seq[Rune] 12 | textString: string 13 | caret: int 14 | scroll: float 15 | blinkTimer: float 16 | focused*: bool 17 | next*: TextBox 18 | font*: RFont 19 | fontSize*: int 20 | placeholder*: string 21 | onInput*: proc () 22 | onAccept*: proc () 23 | 24 | method width*(tb: TextBox): float = tb.fWidth 25 | method height*(tb: TextBox): float = tb.fHeight 26 | proc `width=`*(tb: TextBox, width: float) = 27 | tb.fWidth = width 28 | proc `height=`*(tb: TextBox, height: float) = 29 | tb.fHeight = height 30 | 31 | proc text*(tb: TextBox): string = tb.textString 32 | proc `text=`*(tb: TextBox, text: string) = 33 | tb.fText = text.toRunes 34 | tb.caret = clamp(tb.caret, 0, tb.fText.len) 35 | tb.textString = text 36 | 37 | proc resetBlink(tb: TextBox) = 38 | tb.blinkTimer = time() 39 | 40 | proc canBackspace(tb: TextBox): bool = tb.caret in 1..tb.fText.len 41 | proc canDelete(tb: TextBox): bool = tb.caret in 0.. 0: 68 | tb.font.widthOf(tb.fText[0.. tb.width: 75 | tb.scroll -= tb.caretPos - tb.width 76 | 77 | method onEvent*(tb: TextBox, ev: UIEvent) = 78 | if ev.kind == evMousePress: 79 | tb.focused = tb.hasMouse 80 | if tb.focused: 81 | tb.resetBlink() 82 | elif tb.focused and ev.kind in {evKeyChar, evKeyPress, evKeyRepeat}: 83 | var used = true 84 | case ev.kind 85 | of evKeyChar: tb.insert(ev.rune) 86 | of evKeyPress, evKeyRepeat: 87 | case ev.key 88 | of keyBackspace: tb.backspace() 89 | of keyDelete: tb.delete() 90 | of keyLeft: tb.left() 91 | of keyRight: tb.right() 92 | of keyEnter: 93 | if tb.onAccept != nil: tb.onAccept() 94 | of keyTab: 95 | if tb.next != nil: 96 | tb.focused = false 97 | tb.next.focused = true 98 | else: used = false 99 | else: discard 100 | tb.textString = $tb.fText 101 | tb.resetBlink() 102 | tb.scrollToCaret() 103 | if used: ev.consume() 104 | if tb.onInput != nil: tb.onInput() 105 | elif ev.kind == evMouseEnter: 106 | tb.rwin.cursor = ibeam 107 | elif ev.kind == evMouseLeave: 108 | tb.rwin.cursor = arrow 109 | 110 | proc drawEditor*(tb: TextBox, ctx: RGfxContext) = 111 | let oldFontHeight = tb.font.height 112 | tb.font.height = tb.fontSize 113 | 114 | let pos = tb.screenPos 115 | ctx.scissor(pos.x, pos.y, tb.width, tb.height): 116 | ctx.text(tb.font, tb.xScroll, tb.height / 2 - 2, tb.fText, 117 | vAlign = taMiddle) 118 | 119 | if tb.focused and floorMod(time() - tb.blinkTimer, 1.0) < 0.5: 120 | ctx.begin() 121 | var x = tb.caretPos 122 | ctx.line((x, 0.0), (x, tb.height)) 123 | ctx.draw(prLineShape) 124 | 125 | tb.font.height = oldFontHeight 126 | 127 | renderer(TextBox, Rd, tb): 128 | ctx.color = gray(255) 129 | ctx.begin() 130 | ctx.rect(-2, -2, tb.width + 4, tb.height + 4) 131 | ctx.draw() 132 | ctx.color = gray(127) 133 | ctx.begin() 134 | ctx.lrect(-2, -2, tb.width + 4, tb.height + 4) 135 | ctx.draw(prLineShape) 136 | ctx.color = gray(0) 137 | tb.drawEditor(ctx) 138 | ctx.color = gray(255) 139 | 140 | proc initTextBox*(tb: TextBox, x, y, width, height: float, font: RFont, 141 | placeholder, text = "", fontSize = 14, prev: TextBox = nil, 142 | rend = TextBoxRd) = 143 | tb.initControl(x, y, rend) 144 | tb.width = width 145 | tb.height = height 146 | tb.font = font 147 | tb.text = text 148 | tb.placeholder = placeholder 149 | tb.fontSize = fontSize 150 | if prev != nil: 151 | prev.next = tb 152 | 153 | proc newTextBox*(x, y, width, height: float, font: RFont, 154 | placeholder, text = "", 155 | fontSize = 14, prev: TextBox = nil, 156 | rend = TextBoxRd): TextBox = 157 | result = TextBox() 158 | result.initTextBox(x, y, width, height, font, placeholder, text, fontSize, 159 | prev, rend) 160 | -------------------------------------------------------------------------------- /src/rdgui/windows.nim: -------------------------------------------------------------------------------- 1 | #-- 2 | # rdgui - a modular GUI toolkit for rapid 3 | # copyright (C) 2019 iLiquid 4 | #-- 5 | 6 | import rapid/gfx 7 | 8 | import control 9 | import event 10 | 11 | {.push warning[LockLevel]: off.} 12 | 13 | #-- 14 | # Basic definitions 15 | #-- 16 | 17 | type 18 | WindowManager* = ref object 19 | rwin: RWindow 20 | windows*: seq[Window] 21 | Window* = ref object of Box 22 | wm*: WindowManager 23 | # Properties 24 | fWidth, fHeight: float 25 | # Callbacks 26 | onClose*: proc (): bool 27 | 28 | #-- 29 | # Window manager 30 | #-- 31 | 32 | proc draw*(wm: WindowManager, ctx: RGfxContext, step: float) = 33 | for win in wm.windows: 34 | win.draw(ctx, step) 35 | 36 | proc event*(wm: WindowManager, ev: UIEvent) = 37 | for i in countdown(wm.windows.len - 1, 0): 38 | wm.windows[i].event(ev) 39 | if ev.consumed: 40 | break 41 | 42 | proc add*(wm: WindowManager, win: Window) = 43 | wm.windows.add(win) 44 | 45 | proc bringToTop*(wm: WindowManager, win: Window) = 46 | let i = wm.windows.find(win) 47 | wm.windows.delete(i) 48 | wm.windows.add(win) 49 | 50 | proc initWindowManager*(wm: WindowManager, win: RWindow) = 51 | wm.rwin = win 52 | win.registerEvents do (ev: UIEvent): 53 | wm.event(ev) 54 | 55 | proc newWindowManager*(win: RWindow): WindowManager = 56 | new(result) 57 | result.initWindowManager(win) 58 | 59 | #-- 60 | # Window 61 | #-- 62 | 63 | method width*(win: Window): float = win.fWidth 64 | method height*(win: Window): float = win.fHeight 65 | proc `width=`*(win: Window, width: float) = 66 | win.fWidth = width 67 | proc `height=`*(win: Window, height: float) = 68 | win.fHeight = height 69 | 70 | proc close*(win: Window) = 71 | if win.onClose == nil or win.onClose(): 72 | let handle = win.wm.windows.find(win) 73 | if handle != -1: 74 | win.wm.windows.delete(handle) 75 | 76 | method onEvent*(win: Window, ev: UIEvent) = 77 | procCall win.Box.onEvent(ev) 78 | 79 | proc initWindow*(win: Window, wm: WindowManager, x, y, width, height: float, 80 | renderer = BoxChildren) = 81 | win.initBox(x, y, renderer) 82 | win.rwin = wm.rwin 83 | win.wm = wm 84 | win.width = width 85 | win.height = height 86 | 87 | proc newWindow*(wm: WindowManager, x, y, width, height: float, 88 | renderer = BoxChildren): Window = 89 | new(result) 90 | result.initWindow(wm, x, y, width, height, renderer) 91 | 92 | #-- 93 | # Floating window 94 | #-- 95 | 96 | type 97 | FloatingWindow* = ref object of Window 98 | draggable*: bool 99 | dragging: bool 100 | prevMousePos: Vec2[float] 101 | 102 | method onEvent*(win: FloatingWindow, ev: UIEvent) = 103 | procCall win.Window.onEvent(ev) 104 | if ev.consumed: return 105 | 106 | if win.hasMouse and ev.kind in {evMousePress, evMouseRelease}: 107 | if win.draggable: 108 | win.dragging = ev.kind == evMousePress 109 | if win.dragging: 110 | win.wm.bringToTop(win) 111 | ev.consume() 112 | else: 113 | if ev.kind == evMousePress: ev.consume() 114 | elif ev.kind == evMouseMove: 115 | if win.dragging: 116 | win.pos += ev.mousePos - win.prevMousePos 117 | win.prevMousePos = ev.mousePos 118 | 119 | FloatingWindow.renderer(Rd, win): 120 | ctx.begin() 121 | ctx.rect(0, 0, win.width, win.height) 122 | ctx.draw() 123 | ctx.color = gray(0, 64) 124 | ctx.begin() 125 | ctx.lrect(0, 0, win.width, win.height) 126 | ctx.draw(prLineShape) 127 | ctx.color = gray(255) 128 | BoxChildren(ctx, step, win) 129 | 130 | proc initFloatingWindow*(win: FloatingWindow, wm: WindowManager, 131 | x, y, width, height: float, 132 | renderer = FloatingWindowRd) = 133 | win.initWindow(wm, x, y, width, height, renderer) 134 | win.draggable = true 135 | 136 | proc newFloatingWindow*(wm: WindowManager, x, y, width, height: float, 137 | renderer = FloatingWindowRd): FloatingWindow = 138 | new(result) 139 | result.initFloatingWindow(wm, x, y, width, height, renderer) 140 | 141 | {.pop.} 142 | -------------------------------------------------------------------------------- /tests/.gitignore: -------------------------------------------------------------------------------- 1 | tbutton 2 | twm 3 | -------------------------------------------------------------------------------- /tests/base.nim: -------------------------------------------------------------------------------- 1 | import rapid/gfx 2 | import rdgui/[control, windows] 3 | 4 | export gfx 5 | export control 6 | export windows 7 | 8 | var 9 | win* = initRWindow() 10 | .size(800, 600) 11 | .title("test") 12 | .open() 13 | surface* = win.openGfx() 14 | wm* = newWindowManager(win) 15 | 16 | win.onKeyPress do (key: Key, scancode: int, mods: RModKeys): 17 | if key == keyQ: 18 | quitGfx() 19 | quit(0) 20 | 21 | proc start*() = 22 | surface.loop: 23 | draw ctx, step: 24 | ctx.clear(gray(128)) 25 | wm.draw(ctx, step) 26 | update: 27 | discard 28 | -------------------------------------------------------------------------------- /tests/config.nims: -------------------------------------------------------------------------------- 1 | switch("path", "$projectDir/../src") 2 | switch("opt", "speed") 3 | -------------------------------------------------------------------------------- /tests/data/LICENSE_OpenSans.txt: -------------------------------------------------------------------------------- 1 | 2 | Apache License 3 | Version 2.0, January 2004 4 | http://www.apache.org/licenses/ 5 | 6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 7 | 8 | 1. Definitions. 9 | 10 | "License" shall mean the terms and conditions for use, reproduction, 11 | and distribution as defined by Sections 1 through 9 of this document. 12 | 13 | "Licensor" shall mean the copyright owner or entity authorized by 14 | the copyright owner that is granting the License. 15 | 16 | "Legal Entity" shall mean the union of the acting entity and all 17 | other entities that control, are controlled by, or are under common 18 | control with that entity. For the purposes of this definition, 19 | "control" means (i) the power, direct or indirect, to cause the 20 | direction or management of such entity, whether by contract or 21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 22 | outstanding shares, or (iii) beneficial ownership of such entity. 23 | 24 | "You" (or "Your") shall mean an individual or Legal Entity 25 | exercising permissions granted by this License. 26 | 27 | "Source" form shall mean the preferred form for making modifications, 28 | including but not limited to software source code, documentation 29 | source, and configuration files. 30 | 31 | "Object" form shall mean any form resulting from mechanical 32 | transformation or translation of a Source form, including but 33 | not limited to compiled object code, generated documentation, 34 | and conversions to other media types. 35 | 36 | "Work" shall mean the work of authorship, whether in Source or 37 | Object form, made available under the License, as indicated by a 38 | copyright notice that is included in or attached to the work 39 | (an example is provided in the Appendix below). 40 | 41 | "Derivative Works" shall mean any work, whether in Source or Object 42 | form, that is based on (or derived from) the Work and for which the 43 | editorial revisions, annotations, elaborations, or other modifications 44 | represent, as a whole, an original work of authorship. For the purposes 45 | of this License, Derivative Works shall not include works that remain 46 | separable from, or merely link (or bind by name) to the interfaces of, 47 | the Work and Derivative Works thereof. 48 | 49 | "Contribution" shall mean any work of authorship, including 50 | the original version of the Work and any modifications or additions 51 | to that Work or Derivative Works thereof, that is intentionally 52 | submitted to Licensor for inclusion in the Work by the copyright owner 53 | or by an individual or Legal Entity authorized to submit on behalf of 54 | the copyright owner. For the purposes of this definition, "submitted" 55 | means any form of electronic, verbal, or written communication sent 56 | to the Licensor or its representatives, including but not limited to 57 | communication on electronic mailing lists, source code control systems, 58 | and issue tracking systems that are managed by, or on behalf of, the 59 | Licensor for the purpose of discussing and improving the Work, but 60 | excluding communication that is conspicuously marked or otherwise 61 | designated in writing by the copyright owner as "Not a Contribution." 62 | 63 | "Contributor" shall mean Licensor and any individual or Legal Entity 64 | on behalf of whom a Contribution has been received by Licensor and 65 | subsequently incorporated within the Work. 66 | 67 | 2. Grant of Copyright License. Subject to the terms and conditions of 68 | this License, each Contributor hereby grants to You a perpetual, 69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 70 | copyright license to reproduce, prepare Derivative Works of, 71 | publicly display, publicly perform, sublicense, and distribute the 72 | Work and such Derivative Works in Source or Object form. 73 | 74 | 3. Grant of Patent License. Subject to the terms and conditions of 75 | this License, each Contributor hereby grants to You a perpetual, 76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 77 | (except as stated in this section) patent license to make, have made, 78 | use, offer to sell, sell, import, and otherwise transfer the Work, 79 | where such license applies only to those patent claims licensable 80 | by such Contributor that are necessarily infringed by their 81 | Contribution(s) alone or by combination of their Contribution(s) 82 | with the Work to which such Contribution(s) was submitted. If You 83 | institute patent litigation against any entity (including a 84 | cross-claim or counterclaim in a lawsuit) alleging that the Work 85 | or a Contribution incorporated within the Work constitutes direct 86 | or contributory patent infringement, then any patent licenses 87 | granted to You under this License for that Work shall terminate 88 | as of the date such litigation is filed. 89 | 90 | 4. Redistribution. You may reproduce and distribute copies of the 91 | Work or Derivative Works thereof in any medium, with or without 92 | modifications, and in Source or Object form, provided that You 93 | meet the following conditions: 94 | 95 | (a) You must give any other recipients of the Work or 96 | Derivative Works a copy of this License; and 97 | 98 | (b) You must cause any modified files to carry prominent notices 99 | stating that You changed the files; and 100 | 101 | (c) You must retain, in the Source form of any Derivative Works 102 | that You distribute, all copyright, patent, trademark, and 103 | attribution notices from the Source form of the Work, 104 | excluding those notices that do not pertain to any part of 105 | the Derivative Works; and 106 | 107 | (d) If the Work includes a "NOTICE" text file as part of its 108 | distribution, then any Derivative Works that You distribute must 109 | include a readable copy of the attribution notices contained 110 | within such NOTICE file, excluding those notices that do not 111 | pertain to any part of the Derivative Works, in at least one 112 | of the following places: within a NOTICE text file distributed 113 | as part of the Derivative Works; within the Source form or 114 | documentation, if provided along with the Derivative Works; or, 115 | within a display generated by the Derivative Works, if and 116 | wherever such third-party notices normally appear. The contents 117 | of the NOTICE file are for informational purposes only and 118 | do not modify the License. You may add Your own attribution 119 | notices within Derivative Works that You distribute, alongside 120 | or as an addendum to the NOTICE text from the Work, provided 121 | that such additional attribution notices cannot be construed 122 | as modifying the License. 123 | 124 | You may add Your own copyright statement to Your modifications and 125 | may provide additional or different license terms and conditions 126 | for use, reproduction, or distribution of Your modifications, or 127 | for any such Derivative Works as a whole, provided Your use, 128 | reproduction, and distribution of the Work otherwise complies with 129 | the conditions stated in this License. 130 | 131 | 5. Submission of Contributions. Unless You explicitly state otherwise, 132 | any Contribution intentionally submitted for inclusion in the Work 133 | by You to the Licensor shall be under the terms and conditions of 134 | this License, without any additional terms or conditions. 135 | Notwithstanding the above, nothing herein shall supersede or modify 136 | the terms of any separate license agreement you may have executed 137 | with Licensor regarding such Contributions. 138 | 139 | 6. Trademarks. This License does not grant permission to use the trade 140 | names, trademarks, service marks, or product names of the Licensor, 141 | except as required for reasonable and customary use in describing the 142 | origin of the Work and reproducing the content of the NOTICE file. 143 | 144 | 7. Disclaimer of Warranty. Unless required by applicable law or 145 | agreed to in writing, Licensor provides the Work (and each 146 | Contributor provides its Contributions) on an "AS IS" BASIS, 147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 148 | implied, including, without limitation, any warranties or conditions 149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 150 | PARTICULAR PURPOSE. You are solely responsible for determining the 151 | appropriateness of using or redistributing the Work and assume any 152 | risks associated with Your exercise of permissions under this License. 153 | 154 | 8. Limitation of Liability. In no event and under no legal theory, 155 | whether in tort (including negligence), contract, or otherwise, 156 | unless required by applicable law (such as deliberate and grossly 157 | negligent acts) or agreed to in writing, shall any Contributor be 158 | liable to You for damages, including any direct, indirect, special, 159 | incidental, or consequential damages of any character arising as a 160 | result of this License or out of the use or inability to use the 161 | Work (including but not limited to damages for loss of goodwill, 162 | work stoppage, computer failure or malfunction, or any and all 163 | other commercial damages or losses), even if such Contributor 164 | has been advised of the possibility of such damages. 165 | 166 | 9. Accepting Warranty or Additional Liability. While redistributing 167 | the Work or Derivative Works thereof, You may choose to offer, 168 | and charge a fee for, acceptance of support, warranty, indemnity, 169 | or other liability obligations and/or rights consistent with this 170 | License. However, in accepting such obligations, You may act only 171 | on Your own behalf and on Your sole responsibility, not on behalf 172 | of any other Contributor, and only if You agree to indemnify, 173 | defend, and hold each Contributor harmless for any liability 174 | incurred by, or claims asserted against, such Contributor by reason 175 | of your accepting any such warranty or additional liability. 176 | 177 | END OF TERMS AND CONDITIONS 178 | 179 | APPENDIX: How to apply the Apache License to your work. 180 | 181 | To apply the Apache License to your work, attach the following 182 | boilerplate notice, with the fields enclosed by brackets "[]" 183 | replaced with your own identifying information. (Don't include 184 | the brackets!) The text should be enclosed in the appropriate 185 | comment syntax for the file format. We also recommend that a 186 | file or class name and description of purpose be included on the 187 | same "printed page" as the copyright notice for easier 188 | identification within third-party archives. 189 | 190 | Copyright [yyyy] [name of copyright owner] 191 | 192 | Licensed under the Apache License, Version 2.0 (the "License"); 193 | you may not use this file except in compliance with the License. 194 | You may obtain a copy of the License at 195 | 196 | http://www.apache.org/licenses/LICENSE-2.0 197 | 198 | Unless required by applicable law or agreed to in writing, software 199 | distributed under the License is distributed on an "AS IS" BASIS, 200 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 201 | See the License for the specific language governing permissions and 202 | limitations under the License. 203 | -------------------------------------------------------------------------------- /tests/data/OpenSans-Regular.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/liquidev/rdgui/360035a29be04348430ad89045687844b5c6b2b4/tests/data/OpenSans-Regular.ttf -------------------------------------------------------------------------------- /tests/tbutton.nim: -------------------------------------------------------------------------------- 1 | import base 2 | import rdgui/button 3 | 4 | var 5 | myWin = wm.newFloatingWindow(32, 32, 128, 128) 6 | myButton = newButton(16, 16, 64, 32) 7 | 8 | myButton.onClick = proc = 9 | echo "hello" 10 | 11 | wm.add(myWin) 12 | myWin.add(myButton) 13 | 14 | start() 15 | -------------------------------------------------------------------------------- /tests/tcontextmenu: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/liquidev/rdgui/360035a29be04348430ad89045687844b5c6b2b4/tests/tcontextmenu -------------------------------------------------------------------------------- /tests/tcontextmenu.nim: -------------------------------------------------------------------------------- 1 | import base 2 | import rdgui/contextmenu 3 | import rdgui/layout 4 | 5 | win.onMousePress do (button: MouseButton, mods: RModKeys): 6 | if button == mb2: 7 | var menu = wm.newContextMenu(win.mouseX, win.mouseY, 128) 8 | menu.addButton(24) do: 9 | echo "button 1" 10 | menu.addButton(24) do: 11 | echo "button 2" 12 | var sub = wm.newContextMenu(0, 0, 128) 13 | sub.addButton(24) do: 14 | echo "sub 1 1" 15 | sub.addButton(24) do: 16 | echo "sub 1 2" 17 | sub.listVertical(padding = 0, spacing = 0) 18 | menu.addSubMenu(24, sub) 19 | menu.listVertical(padding = 0, spacing = 0) 20 | wm.add(menu) 21 | 22 | start() 23 | -------------------------------------------------------------------------------- /tests/tslider.nim: -------------------------------------------------------------------------------- 1 | import base 2 | import rdgui/slider 3 | 4 | var 5 | myWin = wm.newFloatingWindow(32, 32, 128, 128) 6 | mySlider = newSlider(16, 16, 64, 12, 7 | min = 0, max = 100, value = 50, step = 5) 8 | 9 | wm.add(myWin) 10 | myWin.add(mySlider) 11 | 12 | start() 13 | -------------------------------------------------------------------------------- /tests/ttextbox: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/liquidev/rdgui/360035a29be04348430ad89045687844b5c6b2b4/tests/ttextbox -------------------------------------------------------------------------------- /tests/ttextbox.nim: -------------------------------------------------------------------------------- 1 | import rapid/gfx/text 2 | 3 | import rdgui/textbox 4 | 5 | import base 6 | 7 | const 8 | openSansTtf = slurp("data/OpenSans-Regular.ttf") 9 | 10 | var 11 | font = newRFont(openSansTtf, 14) 12 | 13 | myWin = wm.newFloatingWindow(32, 32, 256, 256) 14 | myTextBox = newTextBox(32, 32, 192, font) 15 | 16 | font.lineSpacing = 1.5 17 | 18 | wm.add(myWin) 19 | myWin.add(myTextBox) 20 | 21 | start() 22 | -------------------------------------------------------------------------------- /tests/twm.nim: -------------------------------------------------------------------------------- 1 | import base 2 | 3 | for i in 1..4: 4 | wm.add(wm.newFloatingWindow(i.float * 32, i.float * 32, 128, 128)) 5 | 6 | start() 7 | --------------------------------------------------------------------------------