TGC Codebase Backup



Image Batch Re by AuShadow

18th Nov 2019 14:11
Summary

Image batch re-sizing



Description

Image batch re-sizing



Code
                                    ` This code was downloaded from The Game Creators
                                    ` It is reproduced here with full permission
                                    ` http://www.thegamecreators.com
                                    
                                    // Project: Image Batch resize v0.30
// version notes: complete Re-write to use for loops and custom TYPES bedded in arrays
// Created by: AuShadow(aka AbelzAu) 
// Created: 2017-06-27

SetErrorMode(2)

SetWindowTitle( "Image Batch Sizer" )
SetWindowSize( 800, 600, 0 )
SetWindowAllowResize( 1 ) // allow the user to resize the window

SetVirtualResolution( 800, 600 ) // doesn't have to match the window

SetRawWritePath(GetReadPath())

TYPE boxestype
x
y
text as string
endtype

DIM boxes[12] as boxestype

chooseimage=0
global image =0
LoadImage(1,"appimages\bg.png")
CreateSprite(1,1)

SetBoxPositions()
CreateBoxes()
MainLoop()

function MainLoop()
	chooseimage=0
do

	if GetRawKeyPressed(79) then SelectImage() // if press O
	
	if GetRawKeyPressed(67) then clearboxes() // if press C

	if GetRawKeyPressed(9)=1 then editboxfocus() // if press TAB
	
	if GetRawKeyPressed(27) then exit // if press ESC

	if GetRawKeyPressed(13) and image > 0 then resize() // if press ENETER

	if GetVirtualButtonPressed(1)=1 and image > 0 then resize()
	
	if GetVirtualButtonPressed(3)=1 then clearboxes()
	
	if GetVirtualButtonPressed(4)=1 then editboxdefault()
	
	if GetVirtualButtonPressed(2)=1 then SelectImage()

    Sync()
loop

endfunction

function SetBoxPositions()
	i = 1
	
	for a =1 to 12

	if MOD(a,2)=0
		boxes[a].x=700
		boxes[a].y=((a-2)*20)+10
		boxes[a].text=str(2*(512/i))
	else
		boxes[a].x=600
		boxes[a].y=((a-1)*20)+10
		boxes[a].text=str(512/i)
		i=i*2
	endif
		
next a

endfunction

function CreateBoxes()
//Creates all edit boxes, buttons and controls layout
for a=1 to 12
	CreateEditBox(a)
	SetEditBoxPosition(a,boxes[a].x,boxes[a].y)
	SetEditBoxText(a,boxes[a].text)
	SetEditBoxSize(a,50,20)
	next a
	
	AddVirtualButton(1,675,300,50)
	SetVirtualButtonText(1,"GO")
	
	AddVirtualButton(2,40,40,50)
	SetVirtualButtonText(2,"CHOOSE")
	
	AddVirtualButton(3,675,375,50)
	SetVirtualButtonText(3,"CLEAR")
	
	AddVirtualButton(4,675,450,50)
	SetVirtualButtonText(4,"Default")

	
endfunction

function resize()
	SaveImage(image,"original.png")
	
	for a =1 to 12
		if MOD(a,2)=0
		else
			if Val(GetEditBoxText(a)) > 0 and Val(GetEditBoxText(a+1)) > 0
			ResizeImage(image,val(GetEditBoxText(a)),val(GetEditBoxText(a+1)))
			SaveImage(image,GetEditBoxText(a)+".png")
			DeleteSprite(sprite)
			DeleteImage(image)
			Loadimage(image,"original.png")
			sprite=CreateSprite(image)
			SetSpritesize( sprite, 512,512 )
			setspritepositionbyoffset(sprite,300,325)
			endif	
		endif
		
	next a

endfunction

function SelectImage()
	
		DeleteImage(image)
		DeleteSprite(sprite)
	//returns image path to load selected image
	if ShowChooseImageScreen()=1 //Ok

     while IsChoosingImage()=1 //Dialog is Open, Wait
       sync()
     endwhile


    image = GetChosenImage() //Get the image

	sprite = CreateSprite( image )
    SetSpritesize( sprite, 512,512 )
	setspritepositionbyoffset(sprite,300,325)

    endif
	
endfunction image

function clearboxes()
	for a=1 to 12
	
	SetEditBoxText(a,"")
	next a
	
endfunction

function editboxfocus()
   for i=1 to 12
      if GetEditBoxHasFocus(i)
         SetEditBoxFocus(i, 0)
         SetEditBoxFocus(MOD(i, 12)+1, 1)
         exit
      endif
   next i
   sync()
endfunction

function editboxdefault()
	for a=1 to 12
		
		SetEditBoxText(a,boxes[a].text)
		next a
	
endfunction