Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
SalatielSauer authored Oct 1, 2023
1 parent fb996c5 commit 93cbe1f
Show file tree
Hide file tree
Showing 4 changed files with 737 additions and 0 deletions.
392 changes: 392 additions & 0 deletions guicanvas.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,392 @@
// GUICANVAS by @SalatielSauer (01/09/2023)
// github.com/CubeScript/Sauer-GUICANVAS
// the screen consists of a model with dozens of meshes, each mesh has an index that is used as a pixel;
// the pixel color/texture is set using objskin <index>;
// the model can be loaded in game with `mmodel guicanvas` or in a menu with `guimodelpreview "guicanvas"`;
// this file must be executed (/exec guicanvas.cfg) at least once to enable the guicanvas.* commands.

guicanvas.previewMode = 0
_guicanvasEditor = [
notepadfile = $arg1
newgui _ [ guistayopen [
guinoautotab [
guieditor $notepadfile -80 20
textinit $notepadfile $notepadfile
guilist [
guilist [
guifield notepadfile -25
guilist [
guibutton "^f8save file" [textfocus $notepadfile; textsave $notepadfile] 0
guibar
guibutton "^f1exec file" [textfocus $notepadfile; textexec $notepadfile] 0
guibar
guibutton "^f8load file" [textfocus $notepadfile; textload $notepadfile] 0
]
]
guibar
guilist [
guibutton "^f1copy all" [textfocus $notepadfile; textselectall; textcopy] 0
guibutton "^f1paste" [textfocus $notepadfile; textpaste] 0
]
guibar
guilist [
guibutton "^f3clear all" [textfocus $notepadfile; textclear] 0
]
guibar
guilist [
guicheckbox "^f8show preview" guicanvas.previewMode
guibutton "^f1exec & preview" [textfocus $notepadfile; textexec $notepadfile; sleep 0 [guicanvas.refresh]; guicanvas.previewMode = 1] 0
]
]
guistrut -2
guialign 1 [guitextbox "GUICANVAS Editor 1.0" 0 0 0xccaacc]

if (guicanvas.previewMode) [
guistrut -28
guialign 1 [
guistrut 10
guimodelpreview "../../packages/models/guicanvas" [] 1 10
guistrut -8
]
guistrut 8
]
]
]] 0
showgui _
]

_guicanvas = [
if (|| (! (getalias guicanvas)) (=s (getalias guicanvas) "")) [
guicanvas = [
if (=s $arg1 "notepad") [_guicanvasEditor] [
local canvas
canvas = []
loopfiles cfg "guicanvas" "cfg" [append canvas $cfg]
inputcommand $arg1 [
local isNum
isNum = (&& (>= (strcode $commandbuf) 48) (<= (strcode $commandbuf) 57))
if (! $isNum) [
local newCanvas saveCanvas
newCanvas = (concatword "guicanvas/" (strreplace $commandbuf ".cfg" "") ".cfg")
saveCanvas = [
_filesystem.save @newCanvas (escape "exec ^"guicanvas.cfg^"^n^nguicanvas.size 32 32 // this will try to use an existing screen or generate a new one^nguicanvas.background = ^"packages/textures/default.png^"^n^nguicanvas.refreshRate = 0^nguicanvas.play [^n^t// your render commands go here^n^t// they will run every millisecond set by $guicanvas.refreshRate^n^n] [^n^t// this is the interface body^n^tguititle ^"my canvas^"^n] 0 0^n^nguicanvas.refresh // this will draw the background and the pixels set with guicanvas.setPixel") [
_guicanvasEditor @@newCanvas
textfocus @@newCanvas
textload @@newCanvas
]
]
if (findfile $newCanvas) [
inputcommand "0" [
if (= $commandbuf 0) [
_guicanvasEditor @@newCanvas
] [
@@saveCanvas
]
] (format "^f8[GUICANVAS] ^f3warning^n^f7there is already a canvas with the same name in ^f5%1^f7^nuse ^f50 ^f~to load it or ^f51 ^f~to overwrite with preset.^n^f8>^f5" @newCanvas)
] [
@saveCanvas
]

] [
exec (concatword "guicanvas/" (at (concat @@canvas) @commandbuf) ".cfg")
]
] (concat "^f8[GUICANVAS]^n^f7type a name to create a canvas, or a number to load:^n" (loopconcat i (listlen $canvas) [result (format "^f1%1^f7: ^f8%2^n^f5" $i (at $canvas $i))]))
]
]
guicanvas.width = 32
guicanvas.height = 32
guicanvas.pixels = []
guicanvas.background = 1
guicanvas.keepPixels = 0
guicanvas.opacity = 1
guicanvas.refreshRate = -1

// most of these are used only once and shouldn't be changed accidentally,
// but for the sake of variability options:
_guicanvas.screenDir = "packages/models/guicanvas"
_guicanvas.baseTextureBlack = "packages/skyboxes/black_up.jpg"
_guicanvas.baseTextureWhite = "packages/skyboxes/white_ft.jpg"
_guicanvas.baseTextureOpacity = "<grey>packages/skyboxes/cloud_ring.png"
_guicanvas.usedIndexes = []
_guicanvas.lastRefresh = (getmillis)
guicanvas.resetPixels = [
guicanvas.pixels = []
_guicanvas.usedIndexes = []
]
guicanvas.setPixel = [
local pixel_index pixel_texture
pixel_index = (+ (* $arg2 $guicanvas.width) $arg1)
if (= (indexof $_guicanvas.usedIndexes $pixel_index) -1) [
// if arg3 is -1: arg3 is baseTextureOpacity, else: if arg4 is defined: arg3 arg4 arg5 are colors (baseTextureWhite), else: arg3 is custom texture path.
pixel_texture = (? (= $arg3 -1) $_guicanvas.baseTextureOpacity (? (!=s $arg4 "") (concatword "<colorify:" $arg3 "/" $arg4 "/" $arg5 ">" $_guicanvas.baseTextureWhite) $arg3))
guicanvas.pixels = (concatword $guicanvas.pixels "objskin " $pixel_index " ^"" $pixel_texture "^";")
append _guicanvas.usedIndexes $pixel_index // prevents duplicate items in pixels list
]
]
guicanvas.refresh = [
clearmodel (format "../../%1" $_guicanvas.screenDir)
]
guicanvas.fillRect = [
local x y width height r g b
x = $arg1
y = $arg2
width = $arg3
height = $arg4
r = $arg5
g = $arg6
b = $arg7
loop i $width [
loop j $height [
guicanvas.setPixel (+ $x $i) (+ $y $j) $r $g $b
]
]
]
guicanvas.perspective = [
modelpreviewpitch $arg1
modelpreviewfov (max 10 $arg2)
]
guicanvas.onupdate = [
if (!= $guicanvas.refreshRate -1) [
if (>= (getmillis) $_guicanvas.lastRefresh) [
_guicanvas.lastRefresh = (+ (getmillis) $guicanvas.refreshRate)
do $arg1
]
]
]
guicanvas.play = [
local onupdate action actionX actionY
if (= $numargs 0) [
arg1 = (getalias _guicanvas.playarg1)
arg2 = (getalias _guicanvas.playarg2)
arg3 = (getalias _guicanvas.playarg3)
arg4 = (getalias _guicanvas.playarg4)
]
onupdate = $arg1; _guicanvas.playarg1 = $arg1
action = $arg2; _guicanvas.playarg2 = $arg2
actionX = $arg3; _guicanvas.playarg3 = $arg3
actionY = $arg4; _guicanvas.playarg4 = $arg4
_guicanvas.lastRefresh = (getmillis)
newgui guicanvas [
guistrut -30
guialign 0 [
guistrut -35
guialign 0 [
guistrut 22
guimodelpreview (format "../../%1" $_guicanvas.screenDir) "mapmodel" [] 15 0
guistrut -10
]
guistrut -35
]
guialign -1 [
guilist [
guistrut (+ -12 @@@actionY)
guilist [
guistrut @@@@actionX
@@@@action
guistrut (* @@@@actionX -1)
]
guistrut (- 13 @@@actionY)
]
guistrut 20
]
guicanvas.onupdate [
@@onupdate
]
] 0
showgui guicanvas
]
guicanvas.size = [
if (= $numargs 0) [
inputcommand (concat $guicanvas.width $guicanvas.height) [
guicanvas.size (at $commandbuf 0) (at $commandbuf 1)
sleep 0 [echo (concat "^f8[GUICANVAS] ^f1screen size set to" $guicanvas.width $guicanvas.height)]
] "^f8[GUICANVAS] ^f7screen size:^f5"
] [
guicanvas.width = @arg1
guicanvas.height = (? (=s @arg2 "") $guicanvas.width @arg2)
]
if (! (findfile (format "%1/canvas_%2x%3.obj" $_guicanvas.screenDir $guicanvas.width $guicanvas.height))) [
sleep 0 [cleargui]
inputcommand " " [
echo (format "^f8[GUICANVAS] ^f5generating file for screen %1x%2, this may take a while.." $guicanvas.width $guicanvas.height)
sleep 0 [
_guicanvas.genScreen $guicanvas.width $guicanvas.height 1 1 [
local restart
restart = [
if (!=s (getalias _guicanvas.playarg1) "") [
echo "^f8[GUICANVAS] ^f7restarting canvas"
guicanvas.play $_guicanvas.playarg1 $_guicanvas.playarg2 $_guicanvas.playarg3 $_guicanvas.playarg4
if (getalias guicanvas.onstart) [do $guicanvas.onstart; guicanvas.onstart = []]
]
]
if (! (findfile (format "%1/obj.cfg" $_guicanvas.screenDir))) [
_filesystem.save (format "%1/obj.cfg" $_guicanvas.screenDir) (escape $_guicanvas.screenCFG) [
restart
]
] [restart]
]
]
] (format "^f8[GUICANVAS]^t^n> ^f7The screen size ^f5%1^f7x^f5%2 ^f7is not yet available^npress ^f0enter ^f~to generate it or ^f3esc ^f~to cancel." $guicanvas.width $guicanvas.height)
] [if (getalias guicanvas.onstart) [do $guicanvas.onstart; guicanvas.onstart = []]]
]
guicanvas.genScreen = [
local _obj warning checkobj
_gscw = (? (=s $arg1 "") $guicanvas.width $arg1)
_gsch = (? (=s $arg2 "") $guicanvas.height $arg2)
_gscl = 1
_gsct = 1
_obj = (format "canvas_%1x%2.obj" $_gscw $_gsch)
checkobj = [
if (findfile (concatword $_guicanvas.screenDir "/" $_obj)) [
warning = (concat "^f~The^f8" $_obj "^f~file ^f2is already in place^f~, are you sure you want to rewrite it? ")
] [
warning = (format "You are about to ^f2create^f~ a ^f5%1^f~x^f5%2 ^f~screen, " $_gscw $_gsch)
]
]
checkobj
newgui _ [ guistayopen [
guistrut -3
guititle (format "^f8GUICANVAS screen generator^n^f5%1^f~x^f5%2 ^f~contains ^f5%3 ^f~pixels" $_gscw $_gsch (* $_gscw $_gsch))
guistrut 2
guialign 0 [
guitext "^f5width: " 0
guifield _gscw -5 [sleep 500 [guifadein @guifadein]; guifadein 0; guicanvas.genScreen $_gscw $_gsch]
guistrut 1
guitext "^f5height: " 0
guifield _gsch -5 [sleep 500 [guifadein @guifadein]; guifadein 0; guicanvas.genScreen $_gscw $_gsch]
guistrut 1
guitext "^f5layers: " 0
guifield _gscl -5
]
guistrut 0.3
guialign 0 [
guicheckbox "textures support" _gsct 1 0
]
guistrut 0.3
guitextbox (concat @@warning "this operation may take a while depending on the screen size..") 45 0 0xffffff
guialign 0 [
guibutton (concat "^f8continue anyway") [
_guicanvas.genScreen $_gscw $_gsch $_gscl $_gsct
]
guistrut 1
guibutton "^f3cancel" [cleargui] "exit"
]
guistrut 1
]] 0
showgui _
]
_guicanvas.genScreen = [
local width height layers textures index x_offset y_offset buffer callback
width = $arg1
height = $arg2
layers = $arg3
textures = $arg4
callback = $arg5
// offsets for centering
x_offset = (*f (+f (divf $width 2.0) 0.5) -1)
y_offset = (-f (divf $height 2.0) 0.5)
index = 0
buffer = "# generated with GUICANVAS Screen Generator by @SalatielSauer^n"
loop layer $layers [
loop ri $height [
local i
i = (- $height $ri) // start from top row
loop j $width [
local v1 v2 v3 v4 uv start_vertex
// define the 4 vertices for the current plane, considering offsets for centering
v1 = (format "v %1 %2 0.%3" (+f $j $x_offset) (+f $i $y_offset) $layer)
v2 = (format "v %1 %2 0.%3" (+f (+f $j 1) $x_offset) (+f $i $y_offset) $layer)
v3 = (format "v %1 %2 0.%3" (+f (+f $j 1) $x_offset) (+f (+f $i 1) $y_offset) $layer)
v4 = (format "v %1 %2 0.%3" (+f $j $x_offset) (+f (+f $i 1) $y_offset) $layer)
// UV coordinates (standard plane unwrapping)
uv = "vt 0 0^nvt 1 0^nvt 1 1^nvt 0 1^n"
// write the face (polygon) and UV using the above vertices
// 'g' is equivalent to "export as groups" in Blender
buffer = (format "%7g %1 ^n%2 ^n%3 ^n%4 ^n%5^n%6" $index $v1 $v2 $v3 $v4 (? $textures $uv "") $buffer)
start_vertex = (+ (* $index 4) 1)
index = (+ $index 1)
buffer = (format "%5f %1/1 %2/2 %3/3 %4/4^n" $start_vertex (+ $start_vertex 1) (+ $start_vertex 2) (+ $start_vertex 3) $buffer)
]
]
]
_filesystem.save (format "%1/canvas_%2x%3.obj" $_guicanvas.screenDir $width $height) (escape $buffer) [
if (findfile (format "%1/canvas_%2x%3.obj" $_guicanvas.screenDir @width @height)) [
echo (format "^f8[GUICANVAS] ^f1screen saved successfully at ^f7%1/canvas_%2x%3.obj" $_guicanvas.screenDir @@width @@height)
@@callback
] [
error "^f8[GUICANVAS] ^f3could not save screen, try again"
]
]
]
_filesystem.write = [if (!=s $arg1 "") [append _filesystem.linesbuff (concatword "[" $arg1 "]")]]
_filesystem.open = [_filesystem.linesbuff = []; newgui _ [guieditor @arg1 -1 1; textfocus @arg1; textload @arg1; cleargui]; showgui _; if $arg2 [sleep 0 [arg1 = (escape $textshow); arg2 = @arg1; nodebug [do (substr (strreplace (strreplace (strreplace (strreplace (concatword "^n" $textshow) ";" "^n") "//" "^^/^^/") "^n" "]^n _filesystem.write [") "[ ]" "^^n") 1)]; @arg2]]]
_filesystem.save = [newgui _ [local content; content = @arg2; guifield content 1; textfocus content; textsave @arg1; cleargui];showgui _; sleep 0 [@arg3]]
_guicanvas.loadScreen = 1
_guicanvas.screenCFG = [
// the actual model configuration:
objload (concatword "canvas_" $guicanvas.width "x" $guicanvas.height ".obj")
mdlscale 100
mdlspin -36
mdlyaw -90
mdlfullbright 1
mdlalphatest $guicanvas.opacity
objcullface * 0
objdir "../.." // start at home/installation folder
if (|| (!= $guicanvas.background 0) (!=s $guicanvas.background 0)) [
objskin * (? (= $guicanvas.background 1) $_guicanvas.baseTextureBlack $guicanvas.background) // use black background as default (1)
] [
objskin * $_guicanvas.baseTextureOpacity
]
do $guicanvas.pixels
if (! $guicanvas.keepPixels) [
guicanvas.pixels = []
_guicanvas.usedIndexes = []
]
]
] [
if (_guicanvas.loadScreen) [
do $_guicanvas.screenCFG
] [_guicanvas.loadScreen = 1]
]
]
_guicanvas.loadScreen = 0
_guicanvas
if (! (findfile (format "%1/obj.cfg" $_guicanvas.screenDir))) [
// create model configuration file if not available
_filesystem.save (format "%1/obj.cfg" $_guicanvas.screenDir) (escape $_guicanvas.screenCFG) [
if (findfile (format "%1/obj.cfg" $_guicanvas.screenDir)) [
echo "^f8[GUICANVAS] ^f7loaded successfully"
] [
error (format "^f8[GUICANVAS] ^f3could not write ^f7%1/obj.cfg" $_guicanvas.screenDir)
]
]
] [echo "^f7GUICANVAS version: 1.0"]
Loading

0 comments on commit 93cbe1f

Please sign in to comment.