Allow multiple contexts to persist at once

This commit is contained in:
Kevin Harrison 2016-12-19 22:27:42 -05:00
parent c2fefb0601
commit 47128fed66
8 changed files with 1748 additions and 1692 deletions

View file

@ -8,68 +8,70 @@ Provides a lightweight immediate mode GUI for LÖVE games.
```lua
-- Simple UI example.
local nk = require 'nuklear'
local nuklear = require 'nuklear'
local ui
function love.load()
nk.init()
ui = nuklear.init()
end
local combo = {value = 1, items = {'A', 'B', 'C'}}
function love.update(dt)
nk.frameBegin()
if nk.windowBegin('Simple Example', 100, 100, 200, 160,
ui:frameBegin()
if ui:windowBegin('Simple Example', 100, 100, 200, 160,
'border', 'title', 'movable') then
nk.layoutRow('dynamic', 30, 1)
nk.label('Hello, world!')
nk.layoutRow('dynamic', 30, 2)
nk.label('Combo box:')
if nk.combobox(combo, combo.items) then
ui:layoutRow('dynamic', 30, 1)
ui:label('Hello, world!')
ui:layoutRow('dynamic', 30, 2)
ui:label('Combo box:')
if ui:combobox(combo, combo.items) then
print('Combo!', combo.items[combo.value])
end
nk.layoutRow('dynamic', 30, 3)
nk.label('Buttons:')
if nk.button('Sample') then
ui:layoutRow('dynamic', 30, 3)
ui:label('Buttons:')
if ui:button('Sample') then
print('Sample!')
end
if nk.button('Button') then
if ui:button('Button') then
print('Button!')
end
end
nk.windowEnd()
nk.frameEnd()
ui:windowEnd()
ui:frameEnd()
end
function love.draw()
nk.draw()
ui:draw()
end
function love.keypressed(key, scancode, isrepeat)
nk.keypressed(key, scancode, isrepeat)
ui:keypressed(key, scancode, isrepeat)
end
function love.keyreleased(key, scancode)
nk.keyreleased(key, scancode)
ui:keyreleased(key, scancode)
end
function love.mousepressed(x, y, button, istouch)
nk.mousepressed(x, y, button, istouch)
ui:mousepressed(x, y, button, istouch)
end
function love.mousereleased(x, y, button, istouch)
nk.mousereleased(x, y, button, istouch)
ui:mousereleased(x, y, button, istouch)
end
function love.mousemoved(x, y, dx, dy, istouch)
nk.mousemoved(x, y, dx, dy, istouch)
ui:mousemoved(x, y, dx, dy, istouch)
end
function love.textinput(text)
nk.textinput(text)
ui:textinput(text)
end
function love.wheelmoved(x, y)
nk.wheelmoved(x, y)
ui:wheelmoved(x, y)
end
```

View file

@ -1,7 +1,5 @@
-- Simple calculator example, adapted from the original nuklear demo.
local nk = require 'nuklear'
local ops = {'+', '-', '*', '/'}
local a, b, op = '0'
@ -60,36 +58,36 @@ local function display()
return b or a
end
return function ()
if nk.windowBegin('Calculator', 50, 50, 180, 250, 'border', 'movable', 'title') then
nk.layoutRow('dynamic', 35, 1)
nk.label(display(), 'right')
nk.layoutRow('dynamic', 35, 4)
return function (ui)
if ui:windowBegin('Calculator', 50, 50, 180, 250, 'border', 'movable', 'title') then
ui:layoutRow('dynamic', 35, 1)
ui:label(display(), 'right')
ui:layoutRow('dynamic', 35, 4)
for i=1,16 do
if i >= 13 and i < 16 then
if i == 13 then
if nk.button('C') then
if ui:button('C') then
clear()
end
if nk.button('0') then
if ui:button('0') then
digit('0')
end
if nk.button('=') then
if ui:button('=') then
equals()
end
end
elseif i % 4 ~= 0 then
local d = tostring(math.floor(i / 4) * 3 + (i % 4))
if nk.button(d) then
if ui:button(d) then
digit(d)
end
else
local o = ops[math.floor(i / 4)]
if nk.button(o) then
if ui:button(o) then
operator(o)
end
end
end
end
nk.windowEnd()
ui:windowEnd()
end

View file

@ -1,21 +1,19 @@
local nk = require "nuklear"
local img = love.graphics.newImage 'skin/button.png'
return function ()
if nk.windowBegin('Draw Example', 300, 300, 200, 200, 'title', 'movable', 'border') then
local x, y, w, h = nk.windowGetBounds()
return function (ui)
if ui:windowBegin('Draw Example', 300, 300, 200, 200, 'title', 'movable', 'border') then
local x, y, w, h = ui:windowGetBounds()
love.graphics.setColor(255, 0, 0)
nk.line(x + 10, y + 40, x + 50, y + 40, x + 50, y + 80)
nk.curve(x + 50, y + 80, x + 80, y + 40, x + 100, y + 80, x + 80, y + 80)
nk.polygon('line', x + 100, y + 150, x + 60, y + 140, x + 70, y + 70)
nk.circle('line', x + 130, y + 140, 50)
nk.ellipse('fill', x + 30, y + 150, 20, 40)
nk.arc('fill', x + 150, y + 80, 40, 3 * math.pi / 2, 2 * math.pi);
nk.rectMultiColor(x + 95, y + 50, 50, 50, '#ff0000', '#00ff00', '#0000ff', '#000000')
ui:line(x + 10, y + 40, x + 50, y + 40, x + 50, y + 80)
ui:curve(x + 50, y + 80, x + 80, y + 40, x + 100, y + 80, x + 80, y + 80)
ui:polygon('line', x + 100, y + 150, x + 60, y + 140, x + 70, y + 70)
ui:circle('line', x + 130, y + 140, 50)
ui:ellipse('fill', x + 30, y + 150, 20, 40)
ui:arc('fill', x + 150, y + 80, 40, 3 * math.pi / 2, 2 * math.pi);
ui:rectMultiColor(x + 95, y + 50, 50, 50, '#ff0000', '#00ff00', '#0000ff', '#000000')
love.graphics.setColor(255, 255, 255)
nk.image(img, x + 120, y + 120, 70, 50)
nk.text('DRAW TEXT', x + 15, y + 75, 100, 100)
ui:image(img, x + 120, y + 120, 70, 50)
ui:text('DRAW TEXT', x + 15, y + 75, 100, 100)
end
nk.windowEnd()
ui:windowEnd()
end

View file

@ -1,6 +1,6 @@
-- Several simple examples.
local nk = require 'nuklear'
local nuklear = require 'nuklear'
local calculator = require 'calculator'
local draw = require 'draw'
@ -8,49 +8,51 @@ local overview = require 'overview'
local style = require 'style'
local skin = require 'skin'
local ui
function love.load()
nk.init()
ui = nuklear.init()
end
function love.update(dt)
nk.frameBegin()
calculator()
style()
overview()
draw()
skin()
nk.frameEnd()
ui:frameBegin()
calculator(ui)
style(ui)
overview(ui)
draw(ui)
skin(ui)
ui:frameEnd()
end
function love.draw()
nk.draw()
ui:draw()
love.graphics.print("Current FPS: "..tostring(love.timer.getFPS( )), 10, 10)
end
function love.keypressed(key, scancode, isrepeat)
nk.keypressed(key, scancode, isrepeat)
ui:keypressed(key, scancode, isrepeat)
end
function love.keyreleased(key, scancode)
nk.keyreleased(key, scancode)
ui:keyreleased(key, scancode)
end
function love.mousepressed(x, y, button, istouch)
nk.mousepressed(x, y, button, istouch)
ui:mousepressed(x, y, button, istouch)
end
function love.mousereleased(x, y, button, istouch)
nk.mousereleased(x, y, button, istouch)
ui:mousereleased(x, y, button, istouch)
end
function love.mousemoved(x, y, dx, dy, istouch)
nk.mousemoved(x, y, dx, dy, istouch)
ui:mousemoved(x, y, dx, dy, istouch)
end
function love.textinput(text)
nk.textinput(text)
ui:textinput(text)
end
function love.wheelmoved(x, y)
nk.wheelmoved(x, y)
ui:wheelmoved(x, y)
end

View file

@ -1,7 +1,5 @@
-- An overview of most of the supported widgets.
local nk = require 'nuklear'
local checkA = {value = false}
local checkB = {value = true}
local radio = {value = 'A'}
@ -14,93 +12,96 @@ local property = {value = 6}
local edit = {value = 'Edit text'}
local comboA = {value = 1, items = {'A', 'B', 'C'}}
return function ()
if nk.windowBegin('Overview', 100, 100, 600, 450, 'border', 'movable', 'title') then
nk.menubarBegin()
nk.layoutRow('dynamic', 30, 1)
if nk.menuBegin('Menu', nil, 120, 90) then
nk.layoutRow('dynamic', 40, 1)
nk.menuItem('Item A')
nk.menuItem('Item B')
nk.menuItem('Item C')
nk.menuEnd()
return function (ui)
if ui:windowBegin('Overview', 100, 100, 600, 450, 'border', 'movable', 'title') then
ui:menubarBegin()
ui:layoutRow('dynamic', 30, 1)
if ui:menuBegin('Menu', nil, 120, 90) then
ui:layoutRow('dynamic', 40, 1)
ui:menuItem('Item A')
ui:menuItem('Item B')
ui:menuItem('Item C')
ui:menuEnd()
end
nk.menubarEnd()
nk.layoutRow('dynamic', 400, 3)
nk.groupBegin('Group 1', 'border')
nk.layoutRow('dynamic', 30, 1)
nk.label('Left label')
nk.label('Centered label', 'centered')
nk.label('Right label', 'right')
nk.label('Colored label', 'left', '#ff0000')
if nk.treePush('tab', 'Tree Tab') then
if nk.treePush('node', 'Tree Node 1') then
nk.label('Label 1')
nk.treePop()
ui:menubarEnd()
ui:layoutRow('dynamic', 400, 3)
if ui:groupBegin('Group 1', 'border') then
ui:layoutRow('dynamic', 30, 1)
ui:label('Left label')
ui:label('Centered label', 'centered')
ui:label('Right label', 'right')
ui:label('Colored label', 'left', '#ff0000')
if ui:treePush('tab', 'Tree Tab') then
if ui:treePush('node', 'Tree Node 1') then
ui:label('Label 1')
ui:treePop()
end
if nk.treePush('node', 'Tree Node 2') then
nk.label('Label 2')
nk.treePop()
if ui:treePush('node', 'Tree Node 2') then
ui:label('Label 2')
ui:treePop()
end
nk.treePop()
ui:treePop()
end
nk.spacing(1)
if nk.button('Button') then
ui:spacing(1)
if ui:button('Button') then
print('button pressed!')
end
nk.spacing(1)
nk.checkbox('Checkbox A', checkA)
nk.checkbox('Checkbox B', checkB)
nk.groupEnd()
nk.groupBegin('Group 2', 'border')
nk.layoutRow('dynamic', 30, 1)
nk.label('Radio buttons:')
nk.layoutRow('dynamic', 30, 3)
nk.radio('A', radio)
nk.radio('B', radio)
nk.radio('C', radio)
nk.layoutRow('dynamic', 30, 1)
nk.selectable('Selectable A', selectA)
nk.selectable('Selectable B', selectB)
nk.layoutRow('dynamic', 30, {.35, .65})
nk.label('Slider:')
nk.slider(0, slider, 1, 0.05)
nk.label('Progress:')
nk.progress(progress, 10, true)
nk.layoutRow('dynamic', 30, 2)
nk.spacing(2)
nk.label('Color picker:')
nk.button(nil, colorPicker.value)
nk.layoutRow('dynamic', 90, 1)
nk.colorPicker(colorPicker)
nk.groupEnd()
nk.groupBegin('Group 3', 'border')
nk.layoutRow('dynamic', 30, 1)
nk.property('Property', 0, property, 10, 0.25, 0.05)
nk.spacing(1)
nk.label('Edit:')
nk.layoutRow('dynamic', 90, 1)
nk.edit('box', edit)
nk.layoutRow('dynamic', 5, 1)
nk.spacing(1)
nk.layoutRow('dynamic', 30, 1)
nk.label('Combobox:')
nk.combobox(comboA, comboA.items)
nk.layoutRow('dynamic', 5, 1)
nk.spacing(1)
nk.layoutRow('dynamic', 30, 1)
if nk.widgetIsHovered() then
nk.tooltip('Test tooltip')
ui:spacing(1)
ui:checkbox('Checkbox A', checkA)
ui:checkbox('Checkbox B', checkB)
ui:groupEnd()
end
local x, y, w, h = nk.widgetBounds()
if nk.contextualBegin(100, 100, x, y, w, h) then
nk.layoutRow('dynamic', 30, 1)
nk.contextualItem('Item A')
nk.contextualItem('Item B')
nk.contextualEnd()
if ui:groupBegin('Group 2', 'border') then
ui:layoutRow('dynamic', 30, 1)
ui:label('Radio buttons:')
ui:layoutRow('dynamic', 30, 3)
ui:radio('A', radio)
ui:radio('B', radio)
ui:radio('C', radio)
ui:layoutRow('dynamic', 30, 1)
ui:selectable('Selectable A', selectA)
ui:selectable('Selectable B', selectB)
ui:layoutRow('dynamic', 30, {.35, .65})
ui:label('Slider:')
ui:slider(0, slider, 1, 0.05)
ui:label('Progress:')
ui:progress(progress, 10, true)
ui:layoutRow('dynamic', 30, 2)
ui:spacing(2)
ui:label('Color picker:')
ui:button(nil, colorPicker.value)
ui:layoutRow('dynamic', 90, 1)
ui:colorPicker(colorPicker)
ui:groupEnd()
end
nk.label('Contextual (Right click me)')
nk.groupEnd()
if ui:groupBegin('Group 3', 'border') then
ui:layoutRow('dynamic', 30, 1)
ui:property('Property', 0, property, 10, 0.25, 0.05)
ui:spacing(1)
ui:label('Edit:')
ui:layoutRow('dynamic', 90, 1)
ui:edit('box', edit)
ui:layoutRow('dynamic', 5, 1)
ui:spacing(1)
ui:layoutRow('dynamic', 30, 1)
ui:label('Combobox:')
ui:combobox(comboA, comboA.items)
ui:layoutRow('dynamic', 5, 1)
ui:spacing(1)
ui:layoutRow('dynamic', 30, 1)
if ui:widgetIsHovered() then
ui:tooltip('Test tooltip')
end
nk.windowEnd()
local x, y, w, h = ui:widgetBounds()
if ui:contextualBegin(100, 100, x, y, w, h) then
ui:layoutRow('dynamic', 30, 1)
ui:contextualItem('Item A')
ui:contextualItem('Item B')
ui:contextualEnd()
end
ui:label('Contextual (Right click me)')
ui:groupEnd()
end
end
ui:windowEnd()
end

View file

@ -1,5 +1,3 @@
local nk = require 'nuklear'
local windowHeader = love.graphics.newImage 'skin/window_header.png'
local checkboxSkin = love.graphics.newImage 'skin/checkbox_false.png'
local checkboxCheck = love.graphics.newImage 'skin/checkbox_true.png'
@ -45,18 +43,18 @@ local style = {
local check = {value = false}
return function ()
nk.stylePush(style)
if nk.windowBegin('Skin Example', 200, 200, 350, 200, 'title', 'movable') then
nk.layoutSpaceBegin('dynamic', 150, 3)
nk.layoutSpacePush(0.14, 0.15, 0.72, 0.3)
nk.label('Skin example! Styles can change skins, colors, padding, font, and more.', 'wrap')
nk.layoutSpacePush(0.2, 0.55, 0.2, 0.2)
nk.button('Button')
nk.layoutSpacePush(0.55, 0.55, 0.3, 0.2)
nk.checkbox('Checkbox', check)
nk.layoutSpaceEnd()
return function (ui)
ui:stylePush(style)
if ui:windowBegin('Skin Example', 200, 200, 350, 200, 'title', 'movable') then
ui:layoutSpaceBegin('dynamic', 150, 3)
ui:layoutSpacePush(0.14, 0.15, 0.72, 0.3)
ui:label('Skin example! Styles can change skins, colors, padding, font, and more.', 'wrap')
ui:layoutSpacePush(0.2, 0.55, 0.2, 0.2)
ui:button('Button')
ui:layoutSpacePush(0.55, 0.55, 0.3, 0.2)
ui:checkbox('Checkbox', check)
ui:layoutSpaceEnd()
end
nk.windowEnd()
nk.stylePop()
ui:windowEnd()
ui:stylePop()
end

View file

@ -1,6 +1,6 @@
-- Show off some of the styling options.
local nk = require 'nuklear'
local nuklear = require "nuklear"
local colors = {
['text'] = '#afafaf',
@ -41,28 +41,29 @@ end
table.sort(colorNames)
return function ()
nk.styleLoadColors(colors)
nk.windowBegin('Style', 400, 50, 350, 450, 'border', 'movable', 'title', 'scrollbar')
nk.layoutRow('dynamic', 25, 2)
return function (ui)
ui:styleLoadColors(colors)
if ui:windowBegin('Style', 400, 50, 350, 450, 'border', 'movable', 'title', 'scrollbar') then
ui:layoutRow('dynamic', 25, 2)
for _,name in ipairs(colorNames) do
nk.label(name..':')
ui:label(name..':')
local color = colors[name]
if nk.comboboxBegin(nil, color, 200, 200) then
nk.layoutRow('dynamic', 90, 1)
color = nk.colorPicker(color)
if ui:comboboxBegin(nil, color, 200, 200) then
ui:layoutRow('dynamic', 90, 1)
color = ui:colorPicker(color)
colors[name] = color
local r, g, b = nk.colorParseRGBA(color)
nk.layoutRow('dynamic', 25, {.25, .75})
nk.label('R: '..r)
r = nk.slider(0, r, 255, 1)
nk.label('G: '..g)
g = nk.slider(0, g, 255, 1)
nk.label('B: '..b)
b = nk.slider(0, b, 255, 1)
colors[name] = nk.colorRGBA(r, g, b)
nk.comboboxEnd()
local r, g, b = nuklear.colorParseRGBA(color)
ui:layoutRow('dynamic', 25, {.25, .75})
ui:label('R: '..r)
r = ui:slider(0, r, 255, 1)
ui:label('G: '..g)
g = ui:slider(0, g, 255, 1)
ui:label('B: '..b)
b = ui:slider(0, b, 255, 1)
colors[name] = nuklear.colorRGBA(r, g, b)
ui:comboboxEnd()
end
end
nk.windowEnd()
end
ui:windowEnd()
end

File diff suppressed because it is too large Load diff