Skip to content

Commit

Permalink
add widgets to adjust Lsystem parms
Browse files Browse the repository at this point in the history
  • Loading branch information
inkydragon committed Aug 29, 2019
1 parent 95f2fe7 commit b1b0061
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 25 deletions.
52 changes: 31 additions & 21 deletions test/Fractals.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ using CImGui: ImVec2, ImVec4, IM_COL32, ImU32
using CImGui.CSyntax.CFor

include("LSystem.jl")
using .LSystem: RULES, genStep

using .LSystem: RULES, genStep, getAllRules

@static if Sys.isapple()
# OpenGL 3.2 + GLSL 150
Expand Down Expand Up @@ -40,22 +39,16 @@ GLFW.SwapInterval(1) # enable vsync

# setup Dear ImGui context
ctx = CImGui.CreateContext()

# setup Dear ImGui style
CImGui.StyleColorsDark()
# CImGui.StyleColorsClassic()
# CImGui.StyleColorsLight()

# setup Platform/Renderer bindings
ImGui_ImplGlfw_InitForOpenGL(window, true)
ImGui_ImplOpenGL3_Init(glsl_version)

clear_color = Cfloat[0.45, 0.55, 0.60, 1.00]
while !GLFW.WindowShouldClose(window)
# oh my global scope
# TODO: remove all global variables
global clear_color
rule_list = getAllRules()

while !GLFW.WindowShouldClose(window)

GLFW.PollEvents()
# start the Dear ImGui frame
Expand All @@ -67,17 +60,29 @@ while !GLFW.WindowShouldClose(window)
CImGui.SetNextWindowSize((350, 560), CImGui.ImGuiCond_FirstUseEver)
CImGui.Begin("Example: Custom rendering")
draw_list = CImGui.GetWindowDrawList()

# primitives
CImGui.Text("Primitives")
thickness, col = @cstatic thickness=Cfloat(4.0) col=Cfloat[1.0,1.0,0.4,1.0] begin
@c CImGui.DragFloat("Thickness", &thickness, 0.05, 1.0, 8.0, "%.02f")

CImGui.Text("L-Systems")
listbox_item_current, listbox_items = @cstatic listbox_item_current=Cint(0) listbox_items=rule_list begin
# list box
@c CImGui.ListBox("L-Systems\n(click to select one)", &listbox_item_current, listbox_items, length(listbox_items), 3)
end
CImGui.Text("L-System parameters")
iter, angle, slen = @cstatic iter=Cint(4) angle=Cfloat(0.0) slen=Cfloat(10) begin
@c CImGui.CImGui.SliderInt("Iteration times", &iter, 1, 6, "%d")
@c CImGui.DragFloat("Initial angle", &angle, 1, -180.0, 180.0, "%.0f")
@c CImGui.DragFloat("Step length", &slen, 0.5, 0.5, 30, "%.0f")
end
CImGui.Separator()

CImGui.Text("Lines")
thickness, col = @cstatic thickness=Cfloat(2.0) col=Cfloat[1.0,1.0,0.4,1.0] begin
@c CImGui.DragFloat("Width", &thickness, 0.05, 1.0, 8.0, "%.02f")
CImGui.ColorEdit4("Color", col)
end
CImGui.Text("Adjust Original Point")
dx, dy = @cstatic dx=Cfloat(0.0) dy=Cfloat(0.0) begin
dx, dy = @cstatic dx=Cfloat(256.0) dy=Cfloat(128.0) begin
@c CImGui.DragFloat("dx ->", &dx, 4, 0.0, 512.0, "%.0f")
@c CImGui.DragFloat("dy V", &dy, 4, 0.0, 512.0, "%.0f")
@c CImGui.DragFloat("dy V", &dy, 4, 0.0, 512.0, "%.0f")
end
CImGui.Separator()

Expand All @@ -88,13 +93,19 @@ while !GLFW.WindowShouldClose(window)
x::Cfloat = p.x + 4.0 + dx
y::Cfloat = p.y + 4.0 + dy
th::Cfloat = thickness
param = Dict(
"iter" => iter,
"direct" => angle,
"start point" => (x,y),
"step length" => slen,
)

# L System
steps = genStep(RULES[6])
steps = genStep(RULES[listbox_item_current+1], param)
for ((sx, sy), (ex, ey)) in steps
CImGui.AddLine(draw_list,
ImVec2(x+sx, y+sy),
ImVec2(x+ex, y+ey),
ImVec2(sx, sy),
ImVec2(ex, ey),
col32,
th
);
Expand All @@ -109,7 +120,6 @@ while !GLFW.WindowShouldClose(window)
GLFW.MakeContextCurrent(window)
display_w, display_h = GLFW.GetFramebufferSize(window)
glViewport(0, 0, display_w, display_h)
glClearColor(clear_color...)
glClear(GL_COLOR_BUFFER_BIT)
ImGui_ImplOpenGL3_RenderDrawData(CImGui.GetDrawData())

Expand Down
14 changes: 10 additions & 4 deletions test/LSystem.jl
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,13 @@ function genStepString(rule::Rule) :: String
start
end

function genStep(rule::Rule=DEFAULT_RULE)
function genStep(rule::Rule=DEFAULT_RULE, param=Dict())
merge!(rule, param)

angle = rule["direct"]
Δθ = rule["angle"]
sp = (0, 0) # start point
l = 10 # step length
Δθ = rule["angle"]
sp = get(rule, "start point", (0, 0))
l = get(rule, "step length", 10)

steps = []
stack = []
Expand Down Expand Up @@ -97,4 +99,8 @@ function genStep(rule::Rule=DEFAULT_RULE)
steps
end

function getAllRules(rules=RULES)
[d["title"] for d in rules]
end

end # module LSystem

0 comments on commit b1b0061

Please sign in to comment.