Skip to content

Instantly share code, notes, and snippets.

@nsf
Last active August 29, 2015 14:27
Show Gist options
  • Select an option

  • Save nsf/67e26f218e4f02dcb3e3 to your computer and use it in GitHub Desktop.

Select an option

Save nsf/67e26f218e4f02dcb3e3 to your computer and use it in GitHub Desktop.
module NG.FS.GUIDemo
open System
open System.Collections.Generic
open NG
open NG.FS.GUI
open NG.FS.GUI.Common
open NG.FS.GUI.Windows
open NG.FS.GUI.Widgets
open NG.FS.Coroutines
open NG.FS.Math
let private createButtonsDemo name = routine {
let! w = createWindow()
w.Caption <- name
let redLeft =
DefaultButtonStyle(
Alignment = C.TextAlignment.Left,
IdleColor = ShadowedTextColor(RGBA8.Red, RGBA8(0, 0, 0, 127)),
HoverColor = ShadowedTextColor(RGBA8.Red, RGBA8(0, 0, 0, 127)),
PressColor = ShadowedTextColor(RGBA8(172, 0, 0), RGBA8(0, 0, 0, 127)))
let red =
DefaultLabelStyle(
Alignment = C.TextAlignment.Left,
Color = ShadowedTextColor(RGBA8.Red, RGBA8(0, 0, 0, 127)))
let lb = Label(w, DefaultLabelStyle.Instance)
|> setText "Label Demo"
let setLabelText text =
lb.Text <- text
if text = "Button 1" then
lb.Style <- red
else
lb.Style <- DefaultLabelStyle.Instance
let b1 = Button(w, redLeft)
|> setText "Button 1"
|> setOnClick (fun () -> Console.WriteLine("Button 1 clicked"); setLabelText "Button 1")
let b2 = Button(w, DefaultButtonStyle.Instance)
|> setText "Button 2"
|> setOnClick (fun () -> Console.WriteLine("Button 2 clicked"); setLabelText "Button 2")
let b3 = Button(w, DefaultButtonStyle.Instance)
|> setText "Button 3"
|> setOnClick (fun () -> Console.WriteLine("Button 3 clicked"); setLabelText "Button 3")
let b4 = Button(w, DefaultButtonStyle.Instance)
|> setText "Button 4"
|> setOnClick (fun () -> Console.WriteLine("Button 4 clicked"); setLabelText "Button 4")
let b5 = Button(w, DefaultButtonStyle.Instance)
|> setText "Button 5"
|> setOnClick (fun () -> Console.WriteLine("Button 5 clicked"); setLabelText "Button 5")
let grid = Grid(w)
grid.Layout [
[lb ; !<X]
[b1 ; !<X]
[b2 ; !<X]
[b3 ; b4 ]
[!^X ; b5 ]
]
grid.ConfigureRows [0] <| setWeight 1.0f
grid.ConfigureCols [0; 1] <| setWeight 1.0f
grid.ConfigureCells [b1; b2; b3; lb] <| setSticky "NWSE"
grid.ConfigureCells [b4] <| (setSticky "E" >> setIPadX 40)
grid.ConfigureCells [b5] <| (setSticky "W" >> setPadX (Vec2i 20))
grid.ConfigureCells [lb] <| setPadX (Vec2i 20)
w.SetTop grid
return w
}
let private createCheckButtonDemo name = routine {
let! w = createWindow()
w.Caption <- name
w.ResizedByHuman <- true // manual size, thank you
w.Rect <- Rect.SizedAt (Vec2i(200, 220)) w.Rect.TopLeft
let lb = Label(w, DefaultLabelStyle.Instance)
|> setText "Initial state"
let checkedCallback text checked' =
lb.Text <- sprintf "%s: %b" text checked'
let cb1 = CheckButton(w, DefaultCheckButtonStyle.Instance)
|> setText "Check Button 1"
|> setChecked true
|> setOnChange (checkedCallback "Check Button 1")
let cb2 = CheckButton(w, DefaultCheckButtonStyle.Instance)
|> setText "Check Button 2"
|> setChecked false
|> setOnChange (checkedCallback "Check Button 2")
let rbg = RadioButtonGroup()
|> setOnChange (fun i -> lb.Text <- sprintf "Radio Button: %d" i)
let rb1 = RadioButton(w, DefaultRadioButtonStyle.Instance, rbg) |> setText "One"
let rb2 = RadioButton(w, DefaultRadioButtonStyle.Instance, rbg) |> setText "Two"
let rb3 = RadioButton(w, DefaultRadioButtonStyle.Instance, rbg) |> setText "Three"
let grid = Grid(w)
grid.Layout [
[lb]
[cb1]
[cb2]
[rb1]
[rb2]
[rb3]
]
grid.ConfigureRows [0] <| setWeight 1.0f
grid.ConfigureCols [0] <| setWeight 1.0f
grid.ConfigureCells [lb;cb1;cb2;rb1;rb2;rb3] <|
(setSticky "NWSE" >> setPadY (Vec2i(0, 5)))
w.SetTop grid
return w
}
let private createEditBoxDemo name = routine {
let! w = createWindow()
w.Caption <- name
let lb = Label(w, DefaultLabelStyle.Instance)
|> setText "Please enter your name"
let eb = EditBox(w, DefaultEditBoxStyle.Instance)
let submit = Button(w, DefaultButtonStyle.Instance)
|> setText "Submit"
|> setOnClick (fun () -> lb.Text <- sprintf "Hello, %s!" eb.Text)
let grid = Grid(w)
grid.Layout [
[lb]
[X]
[eb]
[submit]
]
grid.ConfigureRows [0] <| setWeight 1.0f
grid.ConfigureCols [0] <| setWeight 1.0f
grid.ConfigureRows [1] <| setPrefSize 40
grid.ConfigureCells [lb; eb; submit] <| setSticky "NWSE"
w.SetTop grid
return w
}
let private createWindowDemo name = routine {
let! w = createWindow()
w.Caption <- name
let simpleLabel str = Label(w, DefaultLabelStyle.Instance) |> setText str
let alphaButton str num =
Button(w, DefaultButtonStyle.Instance)
|> setText (sprintf "Alpha %s" str)
|> setOnClick (fun () -> w.TargetAlpha <- num)
let alignmentButton str alignment =
Button(w, DefaultButtonStyle.Instance)
|> setText str
|> setOnClick (fun () -> setWindowAlignment w alignment)
let alb = simpleLabel "Alpha:"
let a01 = alphaButton "10%" 0.1f
let a05 = alphaButton "50%" 0.5f
let a09 = alphaButton "90%" 0.9f
let a10 = alphaButton "100%" 1.0f
let alignlb = simpleLabel "Alignment:"
let al_tl = alignmentButton "TL" WindowAlignment.TopLeft
let al_t = alignmentButton "T" WindowAlignment.Top
let al_tr = alignmentButton "TR" WindowAlignment.TopRight
let al_l = alignmentButton "L" WindowAlignment.Left
let al_c = alignmentButton "C" WindowAlignment.Center
let al_r = alignmentButton "R" WindowAlignment.Right
let al_bl = alignmentButton "BL" WindowAlignment.BottomLeft
let al_b = alignmentButton "B" WindowAlignment.Bottom
let al_br = alignmentButton "BR" WindowAlignment.BottomRight
let al_f = alignmentButton "F" WindowAlignment.Floating
let grid = Grid(w)
grid.Layout [
[alb; alignlb; !<X; !<X ]
[a01; al_tl; al_t; al_tr]
[a05; al_l ; al_c; al_r ]
[a09; al_bl; al_b; al_br]
[a10; al_f ; !<X ; !<X ]
]
grid.ConfigureRows [0] <| setWeight 1.0f
grid.ConfigureCols [0] <| setWeight 1.0f
grid.ConfigureCells [
alb;a01;a05;a09;a10;alignlb;
al_tl;al_t;al_tr;
al_l;al_c;al_r;
al_bl;al_b;al_br;
al_f;
] <| (setSticky "NWSE" >> setIPadX 20)
w.SetTop grid
return w
}
let private windowsList = [
"Button and Label", createButtonsDemo
"Check and Radio Button", createCheckButtonDemo
"EditBox", createEditBoxDemo
"Window Demo", createWindowDemo
]
let mutable private mainWindow = None : Window option
let private windows = Dictionary<string, Window>()
let private createAndShowWindow name (r : string -> Routine<Window>) = coroutine {
do! lockGUIMutex()
if not (windows.ContainsKey name) then
let! w = r name
windows.Add(name, w)
do! w.RedrawMaybe()
}
let private createAndShowMainWindow () = routine {
let! w = createWindow()
w.Caption <- "GUI Demos"
let createButtonWidget (name, r) =
Button(w, DefaultButtonStyle.Instance)
|> setText name
|> setOnClick (fun () -> Scheduler.Go (createAndShowWindow name r))
:> Widget
let buttons = windowsList |> List.map createButtonWidget
let grid = Grid(w)
grid.Layout (buttons |> List.map (fun b -> [b]))
grid.ConfigureCols [0] <| setWeight 1.0f
grid.ConfigureCells buttons <| setSticky "NWSE"
w.SetTop grid
do! w.RedrawMaybe()
mainWindow <- Some w
}
let show () = coroutine {
do! lockGUIMutex()
match mainWindow with
| Some _ -> ()
| None -> do! createAndShowMainWindow()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment