'
'	VOXEDIT
'	Dozzen
' 	2025/10/27
'	2026/11/19

'	---------------------
'	x,y,z	:	change axis
'	space	:	add voxel
'	backspc	:	delete voxel
'	+/-		:	change color
'	p		:	display palette
'	u		:	undo
'	s		:	save
'	d		:	demo mode (camera)
'	1		:	zoom in
'	2		:	zoom out
'	---------------------

'
'
'	export voxels
load database "1"
select data "build"

'	undo
?undo_step = 0

'
background #004
power "glow" to 50
power "light" to 50
move "light" to 50, 40, -30

'
?size = 16
?half = round ?size / 2
?x = ?half
?y = ?x
?z = ?x

?color = 1
?zoom = 5

'	menu 1 : palette
'	menu 2 : demo
?menu = 0


fov 35

'
color #222
create sprite ?size as "frame"
'paint "frame" to "neon"
center "frame" to 1, 1, 1
move "frame" to 1, 1, 1
add "frame" frame 1, 1, 1 to ?size, ?size, 1

'
create sprite ?size as "voxel"
paint "voxel" to "glass"' priority +
center "voxel" to 1, 1, 1
move "voxel" to 1, 1, 1

'
create sprite 1 as "cursor"
paint "cursor" to "ui"
center "cursor" to 1, 1, 1
color #888
add "cursor" dot 1, 1, 1


'	default

goto "select color", 1
goto "select mode", "x"


do
	
	
	'	Position Caméra
	?cx + (?camx - ?cx) / 20
	?cy + (?camy - ?cy) / 20
	?cz + (?camz - ?cz) / 20
	move "camera" to ?cx, ?cy, ?cz
	target "camera" to ?half, ?half, ?half
	
	goto "cursor"
	
	
	'	Edition
	
	
	goto "controls"
	
	goto "interface"
	
	update
	
loop

'

label "cursor"
	
	?mx = 0
	?my = 0
	
	'	Déplacement
	if (press "left") = 1
		?mx - 1
	else if (press "right") = 1
		?mx + 1
	end
	
	if (press "up") = 1
		?my + 1
	else if (press "down") = 1
		?my - 1
	end
	
	if ?mode = "x"
		'	de face
		?x + ?mx
		?y + ?my
	else if ?mode = "y"
		'	au dessus
		?x + ?mx
		?z + ?my
	else if ?mode = "z"
		'	au dessus
		?z + ?mx
		
		?y + ?my
	end
	
	'?x = limit ?x from 1 to ?size
	limit ?x min 1 max ?size
	limit ?y min 1 max ?size
	limit ?z min 1 max ?size
	
	move "cursor" to ?x, ?y, ?z
	
	' make it blink
	if (mod (frame), 30) < 5
		hide "cursor"
	else
		show "cursor"
	end
	
return

'

label "interface"
	
	if ?menu = 0
		show "frame"
		clear screen
		color 1
		font size 2
		trace frame -100, -100 to 100, 100
		
		trace text "size: ?size" to -95, 95
		trace text "pos : ?x ?y ?z" to -95, 90
		trace text "axis: ?mode" to -95, 85
		trace text "colr: ?color" to -95, 80
		trace text "step: ?undo_step" to -95, 75
		trace text "zoom: ?zoom" to -95, 70
		
		trace text "fps : " + (fps) to -95, 0
	else if ?menu = 2
		hide "frame"
		?angle + 1
		?camx = ?half + ?half * ?zoom * (cos ?angle)
		?camz = ?half + ?half * ?zoom * (sin ?angle)
		?camy = ?half + ?half * ?zoom / 2 * (sin ?angle / 3)
	end
	
return

'

label "select mode"
	param ?new_mode
	
	?mode = ?new_mode
	limit ?zoom min 1 max 8
	
	?camx = ?half
	?camy = ?half
	?camz = ?half
	
	if ?mode = "x"
		?camz = -?half * (?zoom - 1)' car déjà +1
		?camy + 1
	else if ?mode = "y"
		?camy = ?half * (?zoom + 1)
		?camz - 1
	else if ?mode = "z"
		?camx = ?half * (?zoom + 1)
		?camy + 1
	end
	
return

'

label "select color" local
	param ?c
	global ?color
	
	limit ?c min 0 max 99
	?color = ?c
	color ?c
	add "cursor" dot 1, 1, 1
return

'


label "controls"
	
	'	Modes X/Y/Z
	if (press "x") = 1
		goto "select mode", "x"
	else if (press "y") = 1
		goto "select mode", "y"
	else if (press "z") = 1
		goto "select mode", "z"
	end
	
	'	palette
	if (press "+") = 1
		goto "select color", ?color + 1
	else if (press "-") = 1
		goto "select color", ?color - 1
	end
	
	'
	if (press "1") = 1
		?zoom - 1
		goto "select mode", ?mode
	else if (press "2") = 1
		?zoom + 1
		goto "select mode", ?mode
	end
	
	
	
	'  KEY = LOOP DRAW
	if (key "space") = 1
		' vérifier si modification !!
		if (cell "voxel" to ?x, ?y, ?z) <> ?color
			goto "add undo step"
			cell "voxel" to ?x, ?y, ?z = ?color
		end
	else if (press "back") = 1
		goto "add undo step"
		cell "voxel" to ?x, ?y, ?z = 0
	end
	
	'	tools
	if (press "p") = 1
		goto "display palette"
	end
	
	if (press "u") = 1
		goto "undo"
	end
	
	if (press "d") = 1
		if ?menu = 0
			?menu = 2
		else
			?menu = 0
		end
	end
	
	' 	save
	if (press "s") = 1
		pack "voxel" as "voxel"
	end
	
return

'

label "add undo step"
	
	?undo_step + 1
	[undo, ?undo_step, x] = ?x
	[undo, ?undo_step, y] = ?y
	[undo, ?undo_step, z] = ?z
	[undo, ?undo_step, c] = cell "voxel" to ?x, ?y, ?z
	
return

'

label "undo"
	
	if ?undo_step > 0
		?x = [undo, ?undo_step, x]
		?y = [undo, ?undo_step, y]
		?z = [undo, ?undo_step, z]
		?c = [undo, ?undo_step, c]
		
		?now = cell "voxel" to ?x, ?y, ?z
		
		if ?now > 0' not if blank
			goto "select color", ?now
		end
		
		cell "voxel" to ?x, ?y, ?z = ?c
		?undo_step - 1
	end
	
return

'

label "display palette" local
	global ?menu
	
	if ?menu = 1
		?menu = 0
	else
		?menu = 1
		
		clear screen
		font size 5
		for 0 to 99 as ?co
			?x = mod ?co, 10
			?y = floor ?co / 10
			color ?co
			?xx = (?x - 5) * 12
			?yy = -((?y - 5) * 15)
			if ?co > 0
				trace text ?co center ?xx, ?yy
			end
		next
		
	end
	
return
x