initial commit
95
example/calculator.lua
Normal file
|
@ -0,0 +1,95 @@
|
|||
-- Simple calculator example, adapted from the original nuklear demo.
|
||||
|
||||
local nk = require 'nuklear'
|
||||
|
||||
local ops = {'+', '-', '*', '/'}
|
||||
local a, b, op = '0'
|
||||
|
||||
local function clear()
|
||||
a, b, op = '0'
|
||||
end
|
||||
|
||||
local function digit(d)
|
||||
if op then
|
||||
if b == nil or b == '0' then
|
||||
b = d
|
||||
else
|
||||
b = b..d
|
||||
end
|
||||
else
|
||||
if a == '0' then
|
||||
a = d
|
||||
else
|
||||
a = a..d
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local function decimal()
|
||||
if op then
|
||||
b = b or '0'
|
||||
b = b:find('.') and b or b..'.'
|
||||
else
|
||||
a = a:find('.') and a or a..'.'
|
||||
end
|
||||
end
|
||||
|
||||
local function equals()
|
||||
if not tonumber(b) then
|
||||
return
|
||||
end
|
||||
if op == '+' then
|
||||
a, b, op = tostring(tonumber(a) + tonumber(b))
|
||||
elseif op == '-' then
|
||||
a, b, op = tostring(tonumber(a) - tonumber(b))
|
||||
elseif op == '*' then
|
||||
a, b, op = tostring(tonumber(a) * tonumber(b))
|
||||
elseif op == '/' then
|
||||
a, b, op = tostring(tonumber(a) / tonumber(b))
|
||||
end
|
||||
end
|
||||
|
||||
local function operator(o)
|
||||
if op then
|
||||
equals()
|
||||
end
|
||||
op = o
|
||||
end
|
||||
|
||||
local function display()
|
||||
return b or a
|
||||
end
|
||||
|
||||
return function ()
|
||||
if nk.window_begin('Calculator', 50, 50, 180, 250, 'border', 'movable', 'title') then
|
||||
nk.layout_row('dynamic', 35, 1)
|
||||
nk.label(display(), 'right')
|
||||
nk.layout_row('dynamic', 35, 4)
|
||||
for i=1,16 do
|
||||
if i >= 13 and i < 16 then
|
||||
if i == 13 then
|
||||
if nk.button('C') then
|
||||
clear()
|
||||
end
|
||||
if nk.button('0') then
|
||||
digit('0')
|
||||
end
|
||||
if nk.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
|
||||
digit(d)
|
||||
end
|
||||
else
|
||||
local o = ops[math.floor(i / 4)]
|
||||
if nk.button(o) then
|
||||
operator(o)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
nk.window_end()
|
||||
end
|
53
example/main.lua
Normal file
|
@ -0,0 +1,53 @@
|
|||
-- Several simple examples.
|
||||
|
||||
local nk = require 'nuklear'
|
||||
|
||||
local calculator = require 'calculator'
|
||||
local overview = require 'overview'
|
||||
local style = require 'style'
|
||||
local skin = require 'skin'
|
||||
|
||||
function love.load()
|
||||
nk.init()
|
||||
end
|
||||
|
||||
function love.update(dt)
|
||||
nk.frame_begin()
|
||||
calculator()
|
||||
style()
|
||||
overview()
|
||||
skin()
|
||||
nk.frame_end()
|
||||
end
|
||||
|
||||
function love.draw()
|
||||
nk.draw()
|
||||
end
|
||||
|
||||
function love.keypressed(key, scancode, isrepeat)
|
||||
nk.keypressed(key, scancode, isrepeat)
|
||||
end
|
||||
|
||||
function love.keyreleased(key, scancode)
|
||||
nk.keyreleased(key, scancode)
|
||||
end
|
||||
|
||||
function love.mousepressed(x, y, button, istouch)
|
||||
nk.mousepressed(x, y, button, istouch)
|
||||
end
|
||||
|
||||
function love.mousereleased(x, y, button, istouch)
|
||||
nk.mousereleased(x, y, button, istouch)
|
||||
end
|
||||
|
||||
function love.mousemoved(x, y, dx, dy, istouch)
|
||||
nk.mousemoved(x, y, dx, dy, istouch)
|
||||
end
|
||||
|
||||
function love.textinput(text)
|
||||
nk.textinput(text)
|
||||
end
|
||||
|
||||
function love.wheelmoved(x, y)
|
||||
nk.wheelmoved(x, y)
|
||||
end
|
106
example/overview.lua
Normal file
|
@ -0,0 +1,106 @@
|
|||
-- An overview of most of the supported widgets.
|
||||
|
||||
local nk = require 'nuklear'
|
||||
|
||||
local checkA = {value = false}
|
||||
local checkB = {value = true}
|
||||
local radio = {value = 'A'}
|
||||
local selectA = {value = false}
|
||||
local selectB = {value = true}
|
||||
local slider = {value = 0.2}
|
||||
local progress = {value = 1}
|
||||
local colorPicker = {value = '#ff0000'}
|
||||
local property = {value = 6}
|
||||
local edit = {value = 'Edit text'}
|
||||
local comboA = {value = 1, items = {'A', 'B', 'C'}}
|
||||
|
||||
return function ()
|
||||
if nk.window_begin('Overview', 100, 100, 600, 450, 'border', 'movable', 'title') then
|
||||
nk.menubar_begin()
|
||||
nk.layout_row('dynamic', 30, 1)
|
||||
if nk.menu_begin('Menu', nil, 120, 90) then
|
||||
nk.layout_row('dynamic', 40, 1)
|
||||
nk.menu_item('Item A')
|
||||
nk.menu_item('Item B')
|
||||
nk.menu_item('Item C')
|
||||
nk.menu_end()
|
||||
end
|
||||
nk.menubar_end()
|
||||
nk.layout_row('dynamic', 400, 3)
|
||||
nk.group_begin('Group 1', 'border')
|
||||
nk.layout_row('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.tree_push('tab', 'Tree Tab') then
|
||||
if nk.tree_push('node', 'Tree Node 1') then
|
||||
nk.label('Label 1')
|
||||
nk.tree_pop()
|
||||
end
|
||||
if nk.tree_push('node', 'Tree Node 2') then
|
||||
nk.label('Label 2')
|
||||
nk.tree_pop()
|
||||
end
|
||||
nk.tree_pop()
|
||||
end
|
||||
nk.spacing(1)
|
||||
if nk.button('Button') then
|
||||
print('button pressed!')
|
||||
end
|
||||
nk.spacing(1)
|
||||
nk.checkbox('Checkbox A', checkA)
|
||||
nk.checkbox('Checkbox B', checkB)
|
||||
nk.group_end()
|
||||
nk.group_begin('Group 2', 'border')
|
||||
nk.layout_row('dynamic', 30, 1)
|
||||
nk.label('Radio buttons:')
|
||||
nk.layout_row('dynamic', 30, 3)
|
||||
nk.radio('A', radio)
|
||||
nk.radio('B', radio)
|
||||
nk.radio('C', radio)
|
||||
nk.layout_row('dynamic', 30, 1)
|
||||
nk.selectable('Selectable A', selectA)
|
||||
nk.selectable('Selectable B', selectB)
|
||||
nk.layout_row('dynamic', 30, {.35, .65})
|
||||
nk.label('Slider:')
|
||||
nk.slider(0, slider, 1, 0.05)
|
||||
nk.label('Progress:')
|
||||
nk.progress(progress, 10, true)
|
||||
nk.layout_row('dynamic', 30, 2)
|
||||
nk.spacing(2)
|
||||
nk.label('Color picker:')
|
||||
nk.button(nil, colorPicker.value)
|
||||
nk.layout_row('dynamic', 90, 1)
|
||||
nk.color_picker(colorPicker)
|
||||
nk.group_end()
|
||||
nk.group_begin('Group 3', 'border')
|
||||
nk.layout_row('dynamic', 30, 1)
|
||||
nk.property('Property', 0, property, 10, 0.25, 0.05)
|
||||
nk.spacing(1)
|
||||
nk.label('Edit:')
|
||||
nk.layout_row('dynamic', 90, 1)
|
||||
nk.edit('box', edit)
|
||||
nk.layout_row('dynamic', 5, 1)
|
||||
nk.spacing(1)
|
||||
nk.layout_row('dynamic', 30, 1)
|
||||
nk.label('Combobox:')
|
||||
nk.combobox(comboA, comboA.items)
|
||||
nk.layout_row('dynamic', 5, 1)
|
||||
nk.spacing(1)
|
||||
nk.layout_row('dynamic', 30, 1)
|
||||
if nk.widget_is_hovered() then
|
||||
nk.tooltip('Test tooltip')
|
||||
end
|
||||
local x, y, w, h = nk.widget_bounds()
|
||||
if nk.contextual_begin(100, 100, x, y, w, h) then
|
||||
nk.layout_row('dynamic', 30, 1)
|
||||
nk.contextual_item('Item A')
|
||||
nk.contextual_item('Item B')
|
||||
nk.contextual_end()
|
||||
end
|
||||
nk.label('Contextual (Right click me)')
|
||||
nk.group_end()
|
||||
end
|
||||
nk.window_end()
|
||||
end
|
62
example/skin.lua
Normal file
|
@ -0,0 +1,62 @@
|
|||
local nk = require 'nuklear'
|
||||
|
||||
local window_header = love.graphics.newImage 'skin/window_header.png'
|
||||
local checkbox_skin = love.graphics.newImage 'skin/checkbox_false.png'
|
||||
local checkbox_check = love.graphics.newImage 'skin/checkbox_true.png'
|
||||
|
||||
local style = {
|
||||
['text'] = {
|
||||
['color'] = '#000000'
|
||||
},
|
||||
['button'] = {
|
||||
['normal'] = love.graphics.newImage 'skin/button.png',
|
||||
['hover'] = love.graphics.newImage 'skin/button_hover.png',
|
||||
['active'] = love.graphics.newImage 'skin/button_active.png',
|
||||
['text background'] = '#00000000',
|
||||
['text normal'] = '#000000',
|
||||
['text hover'] = '#000000',
|
||||
['text active'] = '#ffffff'
|
||||
},
|
||||
['checkbox'] = {
|
||||
['normal'] = checkbox_skin,
|
||||
['hover'] = checkbox_skin,
|
||||
['active'] = checkbox_skin,
|
||||
['cursor normal'] = checkbox_check,
|
||||
['cursor hover'] = checkbox_check,
|
||||
['text normal'] = '#000000',
|
||||
['text hover'] = '#000000',
|
||||
['text active'] = '#000000',
|
||||
['text background'] = '#d3ceaa'
|
||||
},
|
||||
['window'] = {
|
||||
['header'] = {
|
||||
['normal'] = window_header,
|
||||
['hover'] = window_header,
|
||||
['active'] = window_header,
|
||||
['label normal'] = '#000000',
|
||||
['label hover'] = '#000000',
|
||||
['label active'] = '#000000',
|
||||
['label padding'] = {x = 10, y = 8}
|
||||
},
|
||||
['fixed background'] = love.graphics.newImage 'skin/window.png',
|
||||
['background'] = '#d3ceaa'
|
||||
}
|
||||
}
|
||||
|
||||
local check = {value = false}
|
||||
|
||||
return function ()
|
||||
nk.style_push(style)
|
||||
if nk.window_begin('Skin Example', 200, 200, 350, 200, 'title', 'movable') then
|
||||
nk.layout_space_begin('dynamic', 150, 3)
|
||||
nk.layout_space_push(0.14, 0.15, 0.72, 0.3)
|
||||
nk.label('Skin example! Styles can change skins, colors, padding, font, and more.', 'wrap')
|
||||
nk.layout_space_push(0.2, 0.55, 0.2, 0.2)
|
||||
nk.button('Button')
|
||||
nk.layout_space_push(0.55, 0.55, 0.3, 0.2)
|
||||
nk.checkbox('Checkbox', check)
|
||||
nk.layout_space_end()
|
||||
end
|
||||
nk.window_end()
|
||||
nk.style_pop()
|
||||
end
|
BIN
example/skin/button.png
Normal file
After Width: | Height: | Size: 2.4 KiB |
BIN
example/skin/button_active.png
Normal file
After Width: | Height: | Size: 2.3 KiB |
BIN
example/skin/button_hover.png
Normal file
After Width: | Height: | Size: 2.4 KiB |
BIN
example/skin/checkbox_false.png
Normal file
After Width: | Height: | Size: 1.2 KiB |
BIN
example/skin/checkbox_true.png
Normal file
After Width: | Height: | Size: 2.9 KiB |
BIN
example/skin/window.png
Normal file
After Width: | Height: | Size: 2.3 KiB |
BIN
example/skin/window_header.png
Normal file
After Width: | Height: | Size: 1.4 KiB |
69
example/style.lua
Normal file
|
@ -0,0 +1,69 @@
|
|||
-- Show off some of the styling options.
|
||||
|
||||
local nk = require 'nuklear'
|
||||
|
||||
local colors = {
|
||||
['text'] = '#afafaf',
|
||||
['window'] = '#2d2d2d',
|
||||
['header'] = '#282828',
|
||||
['border'] = '#414141',
|
||||
['button'] = '#323232',
|
||||
['button hover'] = '#282828',
|
||||
['button active'] = '#232323',
|
||||
['toggle'] = '#646464',
|
||||
['toggle hover'] = '#787878',
|
||||
['toggle cursor'] = '#2d2d2d',
|
||||
['select'] = '#2d2d2d',
|
||||
['select active'] = '#232323',
|
||||
['slider'] = '#262626',
|
||||
['slider cursor'] = '#646464',
|
||||
['slider cursor hover'] = '#787878',
|
||||
['slider cursor active'] = '#969696',
|
||||
['property'] = '#262626',
|
||||
['edit'] = '#262626',
|
||||
['edit cursor'] = '#afafaf',
|
||||
['combo'] = '#2d2d2d',
|
||||
['chart'] = '#787878',
|
||||
['chart color'] = '#2d2d2d',
|
||||
['chart color highlight'] = '#ff0000',
|
||||
['scrollbar'] = '#282828',
|
||||
['scrollbar cursor'] = '#646464',
|
||||
['scrollbar cursor hover'] = '#787878',
|
||||
['scrollbar cursor active'] = '#969696',
|
||||
['tab header'] = '#282828'
|
||||
}
|
||||
|
||||
local color_names = {}
|
||||
|
||||
for name,_ in pairs(colors) do
|
||||
color_names[#color_names + 1] = name
|
||||
end
|
||||
|
||||
table.sort(color_names)
|
||||
|
||||
return function ()
|
||||
nk.style_load_colors(colors)
|
||||
nk.window_begin('Style', 400, 50, 350, 450, 'border', 'movable', 'title', 'scrollbar')
|
||||
nk.layout_row('dynamic', 25, 2)
|
||||
for _,name in ipairs(color_names) do
|
||||
nk.label(name..':')
|
||||
local color = colors[name]
|
||||
if nk.combobox_begin(nil, color, 200, 200) then
|
||||
nk.layout_row('dynamic', 90, 1)
|
||||
color = nk.color_picker(color)
|
||||
colors[name] = color
|
||||
local r, g, b = nk.color_parse_rgba(color)
|
||||
nk.layout_row('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.color_rgba(r, g, b)
|
||||
nk.layout_row('dynamic', 0, 0) -- Work-around for nuklear issue #282
|
||||
nk.combobox_end()
|
||||
end
|
||||
end
|
||||
nk.window_end()
|
||||
end
|