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 ```lua
-- Simple UI example. -- Simple UI example.
local nk = require 'nuklear' local nuklear = require 'nuklear'
local ui
function love.load() function love.load()
nk.init() ui = nuklear.init()
end end
local combo = {value = 1, items = {'A', 'B', 'C'}} local combo = {value = 1, items = {'A', 'B', 'C'}}
function love.update(dt) function love.update(dt)
nk.frameBegin() ui:frameBegin()
if nk.windowBegin('Simple Example', 100, 100, 200, 160, if ui:windowBegin('Simple Example', 100, 100, 200, 160,
'border', 'title', 'movable') then 'border', 'title', 'movable') then
nk.layoutRow('dynamic', 30, 1) ui:layoutRow('dynamic', 30, 1)
nk.label('Hello, world!') ui:label('Hello, world!')
nk.layoutRow('dynamic', 30, 2) ui:layoutRow('dynamic', 30, 2)
nk.label('Combo box:') ui:label('Combo box:')
if nk.combobox(combo, combo.items) then if ui:combobox(combo, combo.items) then
print('Combo!', combo.items[combo.value]) print('Combo!', combo.items[combo.value])
end end
nk.layoutRow('dynamic', 30, 3) ui:layoutRow('dynamic', 30, 3)
nk.label('Buttons:') ui:label('Buttons:')
if nk.button('Sample') then if ui:button('Sample') then
print('Sample!') print('Sample!')
end end
if nk.button('Button') then if ui:button('Button') then
print('Button!') print('Button!')
end end
end end
nk.windowEnd() ui:windowEnd()
nk.frameEnd() ui:frameEnd()
end end
function love.draw() function love.draw()
nk.draw() ui:draw()
end end
function love.keypressed(key, scancode, isrepeat) function love.keypressed(key, scancode, isrepeat)
nk.keypressed(key, scancode, isrepeat) ui:keypressed(key, scancode, isrepeat)
end end
function love.keyreleased(key, scancode) function love.keyreleased(key, scancode)
nk.keyreleased(key, scancode) ui:keyreleased(key, scancode)
end end
function love.mousepressed(x, y, button, istouch) function love.mousepressed(x, y, button, istouch)
nk.mousepressed(x, y, button, istouch) ui:mousepressed(x, y, button, istouch)
end end
function love.mousereleased(x, y, button, istouch) function love.mousereleased(x, y, button, istouch)
nk.mousereleased(x, y, button, istouch) ui:mousereleased(x, y, button, istouch)
end end
function love.mousemoved(x, y, dx, dy, istouch) function love.mousemoved(x, y, dx, dy, istouch)
nk.mousemoved(x, y, dx, dy, istouch) ui:mousemoved(x, y, dx, dy, istouch)
end end
function love.textinput(text) function love.textinput(text)
nk.textinput(text) ui:textinput(text)
end end
function love.wheelmoved(x, y) function love.wheelmoved(x, y)
nk.wheelmoved(x, y) ui:wheelmoved(x, y)
end end
``` ```

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

File diff suppressed because it is too large Load diff