remstart
Paint program by
Sven B
remend
 
set display mode 1024, 768, 32
sync on : sync rate 0
color backdrop 0
backdrop off
disable escapekey
 
`This are the constants used for the layout
#constant Theme = RGB(189,6,56)
#constant ThemeBack = RGB(255,162,21)
#constant ThemeTxt = rgb(255, 255, 0)
#constant ThemeFont = "Comic Sans MS"
 
`Possible export extensions (taken from http://darkbasicpro.thegamecreators.com/?f=features)
dim Export$(4)
Export$(1) = ".bmp"
Export$(2) = ".jpg"
Export$(3) = ".dib"
Export$(4) = ".dds"
 
`Possible load extensions (taken from http://darkbasicpro.thegamecreators.com/?f=features)
dim Import$(6)
Import$(1) = ".bmp"
Import$(2) = ".dib"
Import$(3) = ".png"
Import$(4) = ".jpg"
Import$(5) = ".dds"
Import$(6) = ".tga"
 
`Initialize the paint program
gosub Initialize
 
`Create media
gosub CreateMedia
 
r = SBCreateWindow(100, 150, 200, 200, 150, 150)
 
do
 
   `Paste hud
   paste image GUI + 2, 0, 0
 
   `Handle buttons
   gosub Buttons
 
   `Handle windows
   gosub Windows
 
   `If zoom window is turned on, create a zoom right from this window
   if ZoomWind = 1
      CreateZoom(mousex(), mousey())
   else
      if sprite exist(ZoomImg) > 0 then delete sprite ZoomImg
      if sprite exist(ZoomImg + 1) > 0 then delete sprite ZoomImg + 1
   endif
 
   sync
loop
 
`********************************
`Subs
`********************************
 
Windows:
 
   `Handle all windows
   Windows = array count(Window(0))
   if Windows > 0
      for w = 1 to Windows
         HandleWindow(w)
      next w
 
      `If there is an active window
      if ActiveWindow > 0
         `Handle it
         HandleActiveWindow(ActiveWindow)
      else
 
         `Else, if the mouse is pressed, search for a new active window
         if mouseclick() > 0
 
            `Go off all windows
            for w = 1 to Windows
               if mousex() > Window(w).x and mousex() < Window(w).x + Window(w).sx
                  if mousey() > Window(w).y and mousey() < Window(w).y + Window(w).sy + 8
                     ActiveWindow = w
                  endif
               endif
            next w
 
         endif
 
      endif
   endif
 
return
 
Buttons:
 
   `New
   if mousex() > 1 and mousex() < 51 and mousey() < 50
      paste image GUI + 3, 1, 1
 
      `Create new
      if mouseclick() > 0
         gosub New
      endif
 
   endif
 
   `Load
   if mousex() > 51 and mousex() < 101 and mousey() < 50
      paste image GUI + 4, 51, 1
      if mouseclick() = 1 then gosub Load
   endif
 
   `Export
   if mousex() > 101 and mousex() < 151 and mousey() < 50
      paste image GUI + 5, 101, 1
      if mouseclick() = 1 then gosub Export
   endif
 
   `Exit
   if mousex() > 151 and mousex() < 201 and mousey() < 50
      paste image GUI + 6, 151, 1
      if mouseclick() = 1 then end
   endif
 
   `Tools
   `Pen tool
   if mousex() > scrX - 49 and mousex() < scrX - 25 and mousey() > 51 and mousey() < 74 or CTool = 1
      paste image GUI + 7, scrX - 49, 51
      if mouseclick() = 1 then CTool = 1
   endif
 
   `Circle tool
   if mousex() > scrX - 24 and mousey() > 51 and mousey() < 74 or CTool = 2
      paste image GUI + 8, scrX - 24, 51
      if mouseclick() = 1 then CTool = 2
   endif
 
   `Box tool
   if mousex() > scrX - 49 and mousex() < scrX - 24 and mousey() > 76 and mousey() < 99 or CTool = 3
      paste image GUI + 9, scrX - 49, 76
      if mouseclick() = 1 then CTool = 3
   endif
 
   `Fill tool
   if mousex() > scrX - 24 and mousey() > 76 and mousey() < 99 or CTool = 4
      paste image GUI + 10, scrX - 24, 76
      if mouseclick() = 1 then CTool = 4
   endif
 
   `Line tool
   if mousex() > scrX - 49 and mousex() < scrX - 25 and mousey() > 101 and mousey() < 124 or CTool = 5
      paste image GUI + 11, scrX - 49, 101
      if mouseclick() = 1 then CTool = 5
   endif
 
   `Zoom window
   if mousex() > scrX - 24 and mousey() > 101 and mousey() < 124
      paste image GUI + 12, scrX - 24, 101
      if mouseclick() = 1
         ZoomWind = abs(ZoomWind - 1)
         repeat : until mouseclick() = 0
      endif
   endif
   if ZoomWind = 1
      paste image GUI + 12, scrX - 24, 101
   endif
 
   `Spray tool
   if mousex() > scrX - 49 and mousex() < scrX - 26 and mousey() > 126 and mousey() < 149 or CTool = 6
      paste image GUI + 13, scrX - 49, 126
      if mouseclick() = 1 then CTool = 6
   endif
 
   `Display brush options when CTool = 7
   if CTool = 6
 
      `Increase/decrease radius
      ink ThemeTxt, 0
      text scrX - 48, 170, "Size"
      ToolSize = IncDecValue(scrX - 48, 194, ToolSize)
 
      `Increase/decrease radius
      ink ThemeTxt, 0
      text scrX - 48, 220, "Density"
      ToolDensity = IncDecValue(scrX - 48, 242, ToolDensity)
 
   endif
 
   `Brush tool
   if mousex() > scrX - 24 and mousey() > 126 and mousey() < 149 or CTool = 7
      paste image GUI + 14, scrX - 24, 126
      if mouseclick() = 1 then CTool = 7
   endif
 
   `RGB color
   `The red value
   if mousey() > scrY - 270 and mousey() < scrY - 5 and mouseclick() = 1
      if mousex() > scrX - 43 and mousex() < scrX - 36
         ForRed = scrY - mousey() - 10
         if ForRed < 0 then ForRed = 0
         if ForRed > 255 then ForRed = 255
      endif
      if mousex() > scrX - 29 and mousex() < scrX - 22
         ForGreen = scrY - mousey() - 10
         if ForGreen < 0 then ForGreen = 0
         if ForGreen > 255 then ForGreen = 255
      endif
      if mousex() > scrX - 15 and mousex() < scrX - 8
         ForBlue = scrY - mousey() - 10
         if ForBlue < 0 then ForBlue = 0
         if ForBlue > 255 then ForBlue = 255
      endif
   endif
   ForColor = rgb(ForRed, ForGreen, ForBlue)
 
   `Display boxes
   box scrX - 43, scrY - 10 - ForRed, scrX - 36, scrY - 10, rgb(255, 255, 255), rgb(255, 255, 255), rgb(255, 0, 0), rgb(255, 0, 0)
   box scrX - 29, scrY - 10 - ForGreen, scrX - 22, scrY - 10, rgb(255, 255, 255), rgb(255, 255, 255), rgb(0, 255, 0), rgb(0, 255, 0)
   box scrX - 15, scrY - 10 - ForBlue, scrX - 8, scrY - 10, rgb(255, 255, 255), rgb(255, 255, 255), rgb(0, 0, 255), rgb(0, 0, 255)
 
   `Main color
   ink ForColor, 0
   box scrX - 47, scrY - 305, scrX - 3, scrY - 271
 
   `Color picker
   if mouseclick() = 1 and mousex() > scrX - 47 and mousex() < scrX - 3
      if mousey() > scrY - 305 and mousey() < scrY - 271
         ActiveWindow = 0
 
         `Color picker
         size sprite GUI, 320, 8
         paste sprite GUI, 0, 0
         paste image GUI + 1, 313, 1
         ink Theme, 0
         box 0, 8, 320, 208
         ink ThemeBack, 0
         box 1, 8, 319, 207
         `Create color picker
         for i = 0 to 255 step 5
            box 5 + (i/5), 12, 6 + (i/5), 102, rgb(255, i, 0), rgb(255, 255, 255), rgb(255, i, 0), rgb(255, 255, 255)
            box 56 + (i/5), 12, 57 + (i/5), 102, rgb(255 - i, 255, 0), rgb(255, 255, 255), rgb(255 - i, 255, 0), rgb(255, 255, 255)
            box 107 + (i/5), 12, 108 + (i/5), 102, rgb(0, 255, i), rgb(255, 255, 255), rgb(0, 255, i), rgb(255, 255, 255)
            box 158 + (i/5), 12, 159 + (i/5), 102, rgb(0, 255 - i, 255), rgb(255, 255, 255), rgb(0, 255 - i, 255), rgb(255, 255, 255)
            box 209 + (i/5), 12, 210 + (i/5), 102, rgb(i, 0, 255), rgb(255, 255, 255), rgb(i, 0, 255), rgb(255, 255, 255)
            box 260 + (i/5), 12, 261 + (i/5), 102, rgb(255, 0, 255 - i), rgb(255, 255, 255), rgb(255, 0, 255 - i), rgb(255, 255, 255)
         next i
         box 5, 107, 311, 122, rgb(0, 0, 0), rgb(0, 0, 0), rgb(255, 255, 255), rgb(255, 255, 255)
         box 90, 156, 190, 176, Theme, rgb(255, 255, 255), Theme, rgb(255, 255, 255)
         get image Bin, 0, 0, 320, 208, 1
         make memblock from image Bin, Bin
 
         `Get X and Y window coords
         wx = (scrX/2) - 160
         wy = (scrY/2) - 104
         mx = wx + 5
         my = wy + 12
         tempRed = 255
         tempGreen = 255
         tempBlue = 255
         tempAlpha = 255
 
         do
 
            `Refresh
            paste image GUI + 2, 0, 0
 
            `Handle windows
            Windows = array count(Window(0))
            if Windows > 0
               for w = 1 to Windows
                  HandleWindow(w)
               next w
            endif
 
            `Display window
            paste image Bin, wx, wy
 
            `Clicking
            if mouseclick() > 0
 
               `Quit
               if mousex() > wx + 313 and mousex() < wx + 319
                  if mousey() > wy + 1 and mousey() < wy + 7
                     exit
                  endif
               endif
 
               `Color picking
               posx = mousex() - wx
               posy = mousey() - wy
               if posx >= 5 and posy >= 12
                  if posx <= 311 and posy < 102
 
                     mx = mousex() : my = mousey()
 
                     `Extract color
                     pos = 12 + (((posy*320) + posx)*4)
                     tempBlue = memblock byte(Bin, pos)
                     tempGreen = memblock byte(Bin, pos+1)
                     tempRed = memblock byte(Bin, pos + 2)
 
                  endif
               endif
 
               `Alpha
               if posx >= 5 and posx <= 311
                  if posy >= 107 and posy <= 122
                     tempAlpha = 255.0 / 306.0 * (posx - 5)
                  endif
               endif
 
            endif
 
            `Accept
            if mousex() > wx + 90 and mousex() < wx + 190
               if mousey() > wy + 156 and mousey() < wy + 176
                  box wx + 90, wy + 156, wx + 190, wy + 176, rgb(255, 255, 255), Theme, rgb(255, 255, 255), Theme
                  if mouseclick() = 1
                     ForRed = tempRed / 255.0 * tempAlpha
                     ForGreen = tempGreen / 255.0 * tempAlpha
                     ForBlue = tempBlue / 255.0 * tempAlpha
                     exit
                  endif
               endif
            endif
            ink ThemeTxt, 0
            center text wx + 140, wy + 157, "Change"
 
            `Display cursor
            ink 0, 0
            circle mx, my, 2
            line wx + 5 + (306.0/255.0*tempAlpha), wy + 105, wx + 5 + (306.0/255.0*tempAlpha), wy + 124
 
            `Display final color
            ink rgb(tempRed / 255.0 * tempAlpha, tempGreen / 255.0 * tempAlpha, tempBlue / 255.0 * tempAlpha), 0
            box wx + 5, wy + 130, wx + 78, wy + 203
 
            sync
         loop
 
         `Clear image and memblock
         delete image Bin
         delete memblock Bin
 
      endif
   endif
 
return
 
CreateMedia:
 
   `Create title bar image
   cls
   box 0, 0, 8, 8, Theme, rgb(255, 255, 255), Theme, rgb(255, 255, 255)
   get image GUI, 0, 0, 8, 8, 1
 
   `Create sprite from it and hide it
   sprite GUI, 0, 0, GUI
   set sprite GUI, 0, 0
   hide sprite GUI
 
   `Create X button
   cls
   box 0, 0, 6, 6, Theme, rgb(255, 255, 255), Theme, Theme
   ink ThemeTxt, 0
   line 1, 1, 5, 5
   line 1, 4, 5, 0
   get image GUI + 1, 0, 0, 6, 6, 1
 
   `Create Main HUD
   cls
   box 0, 0, scrX, 50, rgb(255,255,255), Theme, rgb(255,255,255), Theme
   box scrX - 50, 0, scrX, scrY, rgb(255, 255, 255), rgb(255,255,255),Theme,Theme
   `New button
   `up
   box 1, 1, 49, 49, Theme, ThemeBack, Theme, ThemeBack
   ink rgb(255, 255, 255), 0
   box 16, 10, 31, 38
   box 31, 13, 34, 38
   ink rgb(150, 150, 150), 0
   for i = 0 to 3
      line 34 - i, 13 - i, 34 - i, 13
   next i
 
   `Load button
   `up
   box 51, 1, 99, 49, Theme, ThemeBack, Theme, ThemeBack
   ink rgb(200,135,1), 0
   for i = 0 to 20
      line 61 + (1.0/6*i), 34 - i, 86 + (1.0/6*i), 34 - i
   next i
   ink rgb(255,255,255), 0
   box 63, 15, 87, 33
   ink rgb(248,235,1), 0
   for i = 0 to 18
      line 61 - (1.0/6*i), 34 - i, 86 - (1.0/6*i), 34 - i
   next i
 
   `Export button
   `up
   box 101, 1, 149, 49, Theme, ThemeBack, Theme, ThemeBack
   ink rgb(150,150,150), 0 : box 110, 9, 135, 39 : box 110, 13, 140, 39
   for i = 0 to 5 : line 135, 8 + i, 135 + i, 8 + i : next i
   ink rgb(200, 200, 200), 0 : box 117, 9, 133, 16
   for i = 0 to 10
      ink rgb(220, 220, 220), 0 : h = sqrt(25 - (i-5)^2) : line 120 + i, 26 - h, 120 + i, 26 + h
      ink rgb(250, 250, 250), 0 : dot 120 + i, 26 - h
   next i
   ink rgb(220, 220, 220), 0 : line 117, 9, 133, 9
   ink rgb(180, 180, 180), 0 : line 110, 9, 116, 9 : line 134, 9, 135, 9
   ink rgb(180, 180, 180), 0 : dot 125, 26
 
   `Exit button
   `up
   box 151, 1, 199, 49, Theme, ThemeBack, Theme, ThemeBack
   ink rgb(255, 0, 0), 0
   for i = 0 to 26
      line 161 + i, 11 + i, 165 + i, 11 + i
      line 185 - i, 11 + i, 189 - i, 11 + i
   next i
 
   `*** Create Tool buttons ***
   `pen tool
   `up
   box scrX - 49, 51, scrX - 27, 74, Theme, rgb(255,255,255), Theme, Theme
   ink RGB(128,64,0), 0
   line scrX - 46, 54, scrX - 34, 66 : line scrX - 45, 53, scrX - 33, 65 : line scrX - 47, 55, scrX - 35, 67
   ink RGB(100,50,0), 0
   line scrX - 46, 55, scrX - 35, 66 : line scrX - 45, 54, scrX - 34, 65
   ink RGB(196,98,0), 0
   line scrX - 34, 66, scrX - 31, 69 : line scrX - 33, 65, scrX - 31, 69 : line scrX - 35, 67, scrX - 31, 69
   dot scrX - 35, 66 : dot scrX - 33, 65
   ink 0, 0
   dot scrX - 31, 69
 
   `Circle shape tool
   `Up
   box scrX - 24, 51, scrX - 1, 74, Theme, rgb(255,255,255), Theme, Theme
   ink 0, 0 : circle scrX - 13, 62, 8
 
   `Box shape tool
   `Up
   box scrX - 49, 76, scrX - 27, 99, Theme, rgb(255,255,255), Theme, Theme
   ink 0, 0
   line scrX - 46, 83, scrX - 30, 83 : line scrX - 46, 83, scrX - 46, 92
   line scrX - 46, 92, scrX - 31, 92 : line scrX - 31, 92, scrX - 31, 83
 
   `Fill tool
   `Up
   box scrX - 24, 76, scrX - 1, 99, Theme, rgb(255,255,255), Theme, Theme
   ink rgb(150, 150, 150), 0
   for i = 1 to 5
      line scrX - 12 - i, 86 - i, scrX - 12 - i, 86 + i
      line scrX - 22 + i, 86 - i, scrX - 22 + i, 86 + i
   next i
   ink rgb(230, 230, 230), 0
   line scrX - 21, 86, scrX - 16, 81
   ink rgb(255, 255, 255), 0
   line scrX - 12, 86, scrX - 12, 94
   line scrX - 13, 89, scrX - 13, 93
   line scrX - 11, 88, scrX - 11, 94
 
   `Line tool
   `Up
   box scrX - 49, 101, scrX - 27, 124, Theme, rgb(255,255,255), Theme, Theme
   ink rgb(0, 0, 0), 0
   line scrX - 46, 104, scrX - 29, 121
 
   `Zoom Window
   box scrX - 24, 101, scrX - 1, 124, Theme, rgb(255, 255, 255), Theme, Theme
   ink rgb(128,64,0), 0
   line scrX - 21, 121, scrX - 15, 111
   line scrX - 20, 121, scrX - 14, 111
   line scrX - 22, 121, scrX - 16, 111
   ink rgb(150, 150, 150), 0
   circle scrX - 12, 107, 5
 
   `Spray tool
   box scrX - 49, 126, scrX - 26, 149, Theme, rgb(255, 255, 255), Theme, Theme
   ink rgb(150, 150, 150), 0
   box scrX - 47, 134, scrX - 40, 146
   for i = 1 to 3
      line scrX - 47 + (i*2), 134 - i, scrX - 40, 134 - i
   next i
   ink rgb(200, 200, 200), 0 : box scrX - 40, 130, scrX - 38, 132 : line scrX - 47, 134, scrX - 47, 146
   ink rgb(0, 0, 255), 0 : box scrX - 45, 131, scrX - 43, 133
   ink rgb(255, 255, 255), 0
   dot scrX - 49 + 11, 126 + 4
   line scrX - 49 + 12, 126 + 3, scrX - 49 + 12, 126 + 4
   line scrX - 49 + 13, 126 + 3, scrX - 49 + 13, 126 + 6
   line scrX - 49 + 14, 126 + 2, scrX - 49 + 14, 126 + 3 : dot scrX - 49 + 14, 126 + 5
   dot scrX - 49 + 15, 126 + 2 : dot scrX - 49 + 15, 126 + 7
   dot scrX - 49 + 16, 126 + 4
   dot scrX - 49 + 17, 126 + 1 : dot scrX - 49 + 17, 126 + 7
   dot scrX - 49 + 18, 126 + 3 : dot scrX - 49 + 18, 126 + 9
   dot scrX - 49 + 19, 126 + 7
   dot scrX - 49 + 20, 126 + 1 : dot scrX - 49 + 20, 126 + 5
   dot scrX - 49 + 21, 126 + 9
 
   `Brush tool
   box scrX - 24, 126, scrX - 1, 149, Theme, rgb(255, 255, 255), Theme, Theme
   ink rgb(255, 255, 255), 0
   box scrX - 20, 128, scrX - 18, 147
   box scrX - 20, 145, scrX - 3, 147
 
   `RGB color picker
   box scrX - 48, scrY - 270, scrX - 2, scrY - 5, Theme, Theme, rgb(255, 255, 255), rgb(255, 255, 255)
   ink 0, 0
   box scrX - 43, scrY - 265, scrX - 36, scrY - 10
   box scrX - 29, scrY - 265, scrX - 22, scrY - 10
   box scrX - 15, scrY - 265, scrX - 8, scrY - 10
 
   `Get image
   get image GUI + 2, 0, 0, scrX, scrY, 1
   cls
 
   `Down - new
   box 0, 0, 48, 48, ThemeBack, Theme, ThemeBack, Theme
   ink rgb(255, 255, 255), 0
   box 15, 10, 30, 38
   box 30, 13, 33, 38
   ink rgb(150, 150, 150), 0
   for i = 0 to 3
      line 33 - i, 13 - i, 33 - i, 13
   next i
   get image GUI + 3, 0, 0, 48, 48, 1
 
   `down - load
   box 0, 0, 48, 48, ThemeBack, Theme, ThemeBack, Theme
   ink rgb(200,135,1), 0
   for i = 0 to 20
      line 10 + (1.0/6*i), 34 - i, 35 + (1.0/6*i), 34 - i
   next i
   ink rgb(255,255,255), 0
   box 12, 15, 36, 33
   ink rgb(248,235,1), 0
   for i = 0 to 18
      line 10 - (1.0/6*i), 34 - i, 35 - (1.0/6*i), 34 - i
   next i
   get image GUI + 4, 0, 0, 48, 48, 1
 
   `Down - export
   box 0, 0, 48, 48, ThemeBack, Theme, ThemeBack, Theme
   ink rgb(150,150,150), 0 : box 9, 9, 34, 39 : box 9, 13, 39, 39
   for i = 0 to 5 : line 34, 8 + i, 34 + i, 8 + i : next i
   ink rgb(200, 200, 200), 0 : box 16, 9, 32, 16
   for i = 0 to 10
      ink rgb(220, 220, 220), 0 : h = sqrt(25 - (i-5)^2) : line 19 + i, 26 - h, 19 + i, 26 + h
      ink rgb(250, 250, 250), 0 : dot 19 + i, 26 - h
   next i
   ink rgb(220, 220, 220), 0 : line 16, 9, 32, 9
   ink rgb(180, 180, 180), 0 : line 9, 9, 15, 9 : line 33, 9, 34, 9
   ink rgb(180, 180, 180), 0 : dot 24, 26
   get image GUI + 5, 0, 0, 48, 48, 1
 
   `down - exit
   box 0, 0, 48, 48, ThemeBack, Theme, ThemeBack, Theme
   ink rgb(255, 0, 0), 0
   for i = 0 to 26
      line 10 + i, 11 + i, 14 + i, 11 + i
      line 34 - i, 11 + i, 38 - i, 11 + i
   next i
   get image GUI + 6, 0, 0, 48, 48, 1
 
   `down - pen tool
   box 0, 0, 23, 23, Theme, Theme, rgb(255, 255, 255), Theme
   ink RGB(128,64,0), 0
   line 3, 3, 15, 15 : line 4, 2, 16, 14 : line 2, 4, 14, 16
   ink RGB(100,50,0), 0
   line 3, 4, 14, 15 : line 4, 3, 15, 14
   ink RGB(196,98,0), 0
   line 15, 15, 18, 18 : line 16, 14, 18, 18 : line 14, 16, 18, 18
   dot 14, 15 : dot 15, 14
   ink 0, 0
   dot 18, 18
   get image GUI + 7, 0, 0, 23, 23, 1
 
   `Down - circle tool
   box 0, 0, 23, 23, Theme, Theme, rgb(255, 255, 255), Theme
   ink 0, 0 : circle 11, 11, 8
   get image GUI + 8, 0, 0, 23, 23, 1
 
   `Down - box tool
   box 0, 0, 23, 23, Theme, Theme, rgb(255, 255, 255), Theme
   ink 0, 0
   line 3, 7, 20, 7 : line 3, 7, 3, 16
   line 3, 16, 20, 16 : line 19, 7, 19, 16
   get image GUI + 9, 0, 0, 23, 23, 1
 
   `Down - Fill tool
   box 0, 0, 23, 23, Theme, Theme, rgb(255, 255, 255), Theme
   ink rgb(150, 150, 150), 0
   for i = 1 to 5
      line 13 - i, 10 - i, 13 - i, 10 + i
      line 2 + i, 10 - i, 2 + i, 10 + i
   next i
   ink rgb(230, 230, 230), 0
   line 3, 10, 8, 5
   ink rgb(255, 255, 255), 0
   line 12, 10, 12, 18
   line 11, 13, 11, 17
   line 13, 12, 13, 18
   get image GUI + 10, 0, 0, 23, 23, 1
 
   `Down - line tool
   box 0, 0, 23, 23, Theme, Theme, rgb(255, 255, 255), Theme
   ink 0, 0
   line 3, 3, 20, 20
   get image GUI + 11, 0, 0, 23, 23, 1
 
   `Down zoom window
   box 0, 0, 23, 23, Theme, Theme, rgb(255, 255, 255), Theme
   ink rgb(128,64,0), 0
   line 2, 20, 8, 10
   line 3, 20, 9, 10
   line 1, 20, 7, 10
   ink rgb(150, 150, 150), 0
   circle 12, 6, 5
   get image GUI + 12, 0, 0, 23, 23, 1
 
   `Down - spray tool
   box 0, 0, 23, 23, Theme, Theme, rgb(255, 255, 255), Theme
   ink rgb(150, 150, 150), 0
   box 2, 8, 9, 20
   for i = 1 to 3
      line 2 + (i*2), 8 - i, 9, 8 - i
   next i
   ink rgb(200, 200, 200), 0 : box 9, 4, 11, 6 : line 2, 8, 2, 20
   ink rgb(0, 0, 255), 0 : box 4, 5, 6, 7
   ink rgb(255, 255, 255), 0
   dot 11, 4
   line 12, 3, 12, 4
   line 13, 3, 13, 6
   line 14, 2, 14, 3 : dot 14, 5
   dot 15, 2 : dot 15, 7
   dot 16, 4
   dot 17, 1 : dot 17, 7
   dot 18, 3 : dot 18, 9
   dot 19, 7
   dot 20, 1 : dot 20, 5
   dot 21, 9
   get image GUI + 13, 0, 0, 23, 23, 1
 
   `Down - brush loading tool
   box 0, 0, 23, 23, Theme, Theme, rgb(255, 255, 255), Theme
   ink rgb(255, 255, 255), 0
   box 4, 3, 6, 21
   box 4, 19, 21, 21
   get image GUI + 14, 0, 0, 23, 23, 1
 
   `+ and - buttons
   box 0, 0, 8, 8, Theme, rgb(255, 255, 255), Theme, Theme
   ink ThemeTxt, 0
   line 4, 1, 4, 7 : line 1, 4, 7, 4
   get image GUI + 15, 0, 0, 8, 8, 1
 
   box 0, 0, 8, 8, Theme, rgb(255, 255, 255), Theme, Theme
   ink ThemeTxt, 0
   line 1, 4, 7, 4
   get image GUI + 16, 0, 0, 8, 8, 1
 
   cls
 
   `Zoom cursor
   ink rgb(255, 255, 255), 0
   box 20, 25, 25, 27
   box 25, 20, 27, 25
   box 27, 25, 32, 27
   box 25, 27, 27, 32
   get image ZoomImg + 1, 0, 0, 50, 50, 1
 
return
 
Initialize:
 
   `Constants
   #constant maxWindows = 20
   #constant GUI = 20
   #constant Canvas = 1
   #constant WindImg = 31
   #constant Bin = 100
   #constant ZoomImg = 101
 
   #constant BaseDir = get dir$()
 
   #constant scrX screen width()
   #constant scrY screen height()
 
   `types
   type window
      x as integer
      y as integer
      sx as integer
      sy as integer
      Cont as integer
      img as integer
   endtype
 
   `Arrays
   dim window(0) as window
 
   `Globals
   global ActiveWindow as integer
   ActiveWindow = 1
 
   global ForRed as integer
   global ForGreen as integer
   global ForBlue as integer
   global ForColor as integer
   ForColor = rgb(ForRed, ForGreen, ForBlue)
 
   global CTool as integer
   global ZoomWind as integer
   global ToolSize as integer
   ToolSize = 10
   global ToolDensity as integer
   ToolDensity = 20
 
   `This comes in handy for passing between functions
   global x as integer
   global y as integer
   global sx as integer
   global img as integer
   global imgsx as integer
   global imgsy as integer
   global imgx as integer
   global imgy as integer
   global LastX as integer
   global LastY as integer
 
   `Setup
   set text font ThemeFont
   set text size 18
 
   `Create a vector for distances
   r = make vector2(1)
 
return
 
`>>>>>>>>>>>>>>>>>>>>>>>>>> NEW - EXPORT - LOAD <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
New:
 
   `Create new
   ActiveWindow = 0
 
   `Create window
   size sprite GUI, 200, 8
   paste sprite GUI, 0, 0
   paste image GUI+1, 193, 1
   ink Theme, 0
   box 0, 8, 200, 125
   ink ThemeBack, 0
   box 1, 8, 199, 124
   get image Bin, 0, 0, 200, 150, 1
 
   `Reset values
   width$ = "0"
   height$= "0"
   CInp = 1
   clear entry buffer
 
   do
 
      `Refres
      paste image GUI + 2, 0, 0
 
      `Handle windows
      Windows = array count(Window(0))
      if Windows > 0
         for w = 1 to Windows
            HandleWindow(w)
         next w
      endif
 
      `The window
      paste image Bin, (scrX/2) - 100, (scrY/2) - 75
 
      `Display text
      if CInp = 1 then dwidth$ = width$ + "_" else dwidth$ = width$
      if CInp = 2 then dheight$ = height$ + "_" else dheight$ = height$
      ink ThemeTxt, 0
      text (scrX/2) - 90, (scrY/2) - 60, "Width: " + dwidth$
      text (scrX/2) - 90, (scrY/2) - 40, "Height: " + dheight$
 
      `Select current input
      if mouseclick() > 0
         if mousex() > (scrX/2) - 90 and mousex() < (scrX/2) + 90
            if mousey() > (scrY/2) - 60 and mousey() < (scrY/2) - 43
               CInp = 1
            endif
            if mousey() > (scrY/2) - 40 and mousey() < (scrY/2) - 23
               CInp = 2
            endif
         endif
      endif
 
      `Input
      char$ = entry$()
      select asc(char$)
         case 8
            if CInp = 1 and len(width$) > 0 then width$ = left$(width$, len(width$) - 1)
            if CInp = 2 and len(height$) > 0 then height$ = left$(height$, len(height$) - 1)
         endcase
         case 13
            CInp = 0
         endcase
         case default
            if CInp = 1 then width$ = width$ + char$
            if CInp = 2 then height$ = height$ + char$
         endcase
      endselect
      clear entry buffer
 
      `Button
      if mousex() > (scrX/2) - 50 and mousex() < (scrX/2) + 50 and mousey() > (scrY/2) + 10 and mousey() < (scrY/2) + 30
         box (scrX/2) - 50, (scrY/2)+10, (scrX/2) + 50, (scrY/2) + 30, rgb(255, 255, 255), Theme, rgb(255, 255, 255), Theme
         if mouseclick() > 0 then Create = 1 : exit
      else
         box (scrX/2) - 50, (scrY/2)+10, (scrX/2) + 50, (scrY/2) + 30, Theme, rgb(255, 255, 255), Theme, rgb(255, 255, 255)
      endif
      ink ThemeTxt, 0
      center text (scrX/2), (scrY/2) + 11, "New"
 
      `Exit
      if mousex() > (scrX/2)+93 and mousex() < (scrX/2) + 100 and mousey() > (scrY/2)-74 and mousey() < (scrY/2) - 69
         if mouseclick() = 1 then Create = 0 : exit
      endif
 
      sync
   loop
 
   `If create = 1 then create a new window
   if Create > 0
 
      `Calculate values
      width = val(width$)
      height= val(height$)
 
      `Boundries
      if width < 16 then width = 16
      if height < 16 then height = 16
      if width > scrX - 60 then width = scrX - 60
      if height > scrY - 70 then height = scrY - 70
 
      `Create new window
      r = SBCreateWindow(0, 50, width + 5, height + 5, width, height)
      if r = 0
         startTimer = timer()
         ink ThemeBack, 0
         box 0, (scrY/2) - 20, scrX, (scrY/2) + 20
         ink ThemeTxt, 0
         center text scrX/2, scrY/2, "There are too many windows to load another"
         repeat : until timer() - startTimer >= 3000
         sync : sync
      endif
 
   endif
 
return
 
Export:
 
   `Export ActiveWindow
   ExportWindow = ActiveWindow
   ActiveWindow = 0
   Filename$ = "New"
   Ext = 1
   CInp = 0
 
   `Create export window
   size sprite GUI, 300, 8
   paste sprite GUI, 0, 0
   paste image GUI + 1, 293, 1
   ink Theme, 0 : box 0, 8, 300, 130
   ink ThemeBack, 0 : box 1, 8, 299, 129
   get image Bin, 0, 0, 300, 208, 1
   wx = (scrX/2) - 150
   wy = (scrY/2) - 115
 
   do
 
      `Refresh screen
      paste image GUI + 2, 0, 0
 
      `Display images1
      for w = 1 to array count(Window(0))
         HandleWindow(w)
      next w
 
      `Display the export window
      paste image Bin, wx, wy
 
      `Display filename
      ink ThemeTxt, 0
      if CInp = 1 then add$ = "_" else add$ = ""
      text wx + 5, wy + 20, "Filename: " + Filename$ + add$
      text wx + 5, wy + 40, "File type: " + Export$(Ext)
 
      `Click on filename or Export
      if mousex() > wx and mousex() < wx + 300
         if mousey() > wy + 20 and mousey() < wy + 38
            if mouseclick() = 1 then CInp = 1
         endif
         if mousey() > wy + 40 and mousey() < wy + 58
            if mouseclick() = 1
               inc Ext
               if Ext > array count(Import$(0)) then Ext = 1
               repeat : until mouseclick() = 0
            endif
         endif
      endif
 
      `Input
      if CInp = 1
         char$ = entry$()
         select asc(char$)
            case 8
               Filename$ = left$(Filename$, len(Filename$) - 1)
            endcase
            case 13
               CInp = 0
            endcase
            case 46 : endcase : case 47 : endcase
            case 92 : endcase : case 58 : endcase
            case 42 : endcase : case 63 : endcase
            case 34 : endcase : case 60 : endcase
            case 62 : endcase : case 124 : endcase
            case default
               if asc(char$) > 31
                  Filename$ = Filename$ + char$
               endif
            endcase
         endselect
      endif
      clear entry buffer
 
      `Exit
      if mousex() > wx + 293 and mousex() < wx + 299
         if mousey() > wy + 1 and mousey() < wy + 7
            if mouseclick() = 1 then Export = 0 : exit
         endif
      endif
 
      `Button export
      if mousex() > wx + 100 and mousex() < wx + 200 and mousey() > wy + 100 and mousey() < wy + 120
         box wx + 100, wy + 100, wx + 200, wy + 120, rgb(255, 255, 255), Theme, rgb(255, 255, 255), Theme
         if mouseclick() = 1 then Export = 1 : exit
      else
         box wx + 100, wy + 100, wx + 200, wy + 120, Theme, rgb(255, 255, 255), Theme, rgb(255, 255, 255)
      endif
      ink ThemeTxt, 0
      center text wx + 150, wy + 101, "Export"
 
      sync
   loop
 
   `Export if Export = 1
   if Export = 1
 
      `If file doesn't exit, export
      if file exist(Filename$ + Export$(Ext)) > 0
 
         `Give an error message that it already exists
         startTimer = timer()
         ink ThemeBack, 0
         box 0, (scrY/2) - 20, scrX, (scrY/2) + 20
         ink ThemeTxt, 0
         center text scrX/2, scrY/2, "File already exist. Please export again with a different filename."
         sync : sync
         repeat : until timer() - startTimer >= 3000
 
      else
 
         `Save the image
         save image Filename$ + Export$(Ext), Window(ExportWindow).Cont
 
         `Message that it succeeded
         startTimer = timer()
         ink ThemeBack, 0
         box 0, (scrY/2) - 20, scrX, (scrY/2) + 20
         ink ThemeTxt, 0
         center text scrX/2, scrY/2, Filename$ + Export$(Ext) + " exported."
         sync
         sync
         repeat : until timer() - startTimer >= 3000
      endif
 
   endif
 
return
 
Load:
 
   `Load an image
   FileName$ = LoadImage()
 
   `Load the image if the size isn't too big
   if FileName$ <> ""
      load image FileName$, Bin, 1
      make memblock from image Bin, Bin
 
      if memblock dword(Bin, 0) > scrX - 60 or memblock dword(Bin, 4) > scrY - 70
         `Give an error that the image is too big
         startTimer = timer()
         ink ThemeBack, 0
         box 0, (scrY/2) - 20, scrX, (scrY/2) + 20
         ink ThemeTxt, 0
         center text scrX/2, scrY/2, "The image is too big. Could not load"
         sync : sync
         repeat : until timer() - startTimer >= 3000
 
      else
 
         `Create a new window
         w = SBCreateWindow(50, 100, memblock dword(Bin, 0) + 5, memblock dword(Bin, 4) + 5, memblock dword(Bin, 0), memblock dword(Bin, 4))
         `Delete the window image and replace with loaded image
         delete image Window(w).Cont
         delete memblock Window(w).Cont
         load image FileName$, Window(w).Cont, 1
         make memblock from image Window(w).Cont, Window(w).Cont
      endif
 
      `Delete remains
      delete image Bin
      delete memblock Bin
   endif
 
return
 
`***********************************
`Functions
`***********************************
 
function HandleWindow(id)
 
   `Paste the image on his place
   if id <> ActiveWindow
 
      `Extract data
      Hx = Window(id).x
      Hy = Window(id).y
      Hsx = Window(id).sx
      Hsy = Window(id).sy
      Himg = Window(id).Cont
 
      if memblock exist(Himg) > 0
         Himgsx = memblock dword(Himg, 0)
         Himgsy = memblock dword(Himg, 4)
      else
         Himgsx = 50
         Himgsy = 50
      endif
      Himgx = Hx + (Hsx/2) - (Himgsx/2)
      Himgy = Hy + 8 + (Hsy/2) - (Himgsy/2)
 
      paste image Window(id).img, Hx, Hy
 
      if memblock exist(Himg) > 0 then paste image Himg, Himgx, Himgy
   endif
 
endfunction
 
function HandleActiveWindow(id)
 
   `Paste the image on his place
   paste image Window(ActiveWindow).img, Window(ActiveWindow).x, Window(ActiveWindow).y
 
   `Extract data
   x = Window(ActiveWindow).x
   y = Window(ActiveWindow).y
   sx = Window(ActiveWindow).sx
   sy = Window(ActiveWindow).sy
   img = Window(ActiveWindow).Cont
   imgsx = memblock dword(img, 0)
   imgsy = memblock dword(img, 4)
   imgx = x + (sx/2) - (imgsx/2)
   imgy = y + 8 + (sy/2) - (imgsy/2)
 
   `Paste canvas
   paste image img, imgx, imgy
 
   `If any action is taken with the mouse
   if mouseclick() = 1
 
      `Drawing on the image
      if mousex() > x + 1 and mousex() < x + sx - 1
         if mousey() > y + 8 and mousey() < y + sy + 7
 
            `Draw on image
            DrawOnImage(id)
            exitfunction
 
         endif
      endif
 
      `Exit the window
      if mousex() > x + sx - 7 and mousex() < x + sx - 1
         if mousey() > y + 1 and mousey() < y + 8
            delete memblock img
            delete image img
            delete image Window(id).img
            array delete element Window(0), id
            ActiveWindow = 0
         endif
      endif
 
      `Move the window
      if mousex() > x and mousex() < x + sx - 7
         if mousey() > y and mousey() < y + 8
 
            difX = mousex() - Window(id).x
            difY = mousey() - Window(id).y
 
            repeat
 
               `Refresh screen
               paste image GUI + 2, 0, 0
 
               `Paste all windows(no condition: array count is minimum 1 -> ActiveWindow)
               for i = 1 to array count(Window(0))
                  HandleWindow(i)
               next i
 
               `Calculate new position
               Window(id).x = mousex() - difX
               Window(id).y = mousey() - difY
 
               `Boundries
               if Window(id).x < 0 then Window(id).x = 0
               if Window(id).y < 50 then Window(id).y = 50
               if Window(id).x + Window(id).sx > scrX - 50 then Window(id).x = scrX - Window(id).sx - 50
               if Window(id).y + Window(id).sy + 8 > scrY then Window(id).y = scrY - Window(id).sy - 8
 
               `paste image
               paste image Window(id).img, Window(id).x, Window(id).y
 
               `Paste content
               imgx = Window(id).x + (sx/2) - (imgsx/2)
               imgy = Window(id).y + 8 + (sy/2) - (imgsy/2)
               paste image Window(id).Cont, imgx, imgy
 
               sync
            until mouseclick() = 0
         endif
      endif
 
      `Resize the window
      if mousex() > x + sx - 1 and mousex() < x + sx + 2 and mousey() > y and mousey() < y + sy + 10
         ResizeX = 1
      else
         ResizeX = 0
      endif
      if mousey() > y + sy + 7 and mousey() < y + sy + 10 and mousex() > x and mousex() < x + sx + 2
         ResizeY = 1
      else
         ResizeY = 0
      endif
      if ResizeX + ResizeY > 0
 
         repeat
 
            `Refresh screen
            paste image GUI + 2, 0, 0
 
            `Paste all windows
            for i = 1 to array count(Window(0))
               HandleWindow(i)
            next i
 
            `Calculate new width and height
            if ResizeX > 0 then Window(id).sx = mousex() - Window(id).x
            if ResizeY > 0 then Window(id).sy = mousey() - Window(id).y - 8
 
            `Boundries
            if Window(id).sx < imgsx + 5 then Window(id).sx = imgsx + 5
            if Window(id).sy < imgsy + 13 then Window(id).sy = imgsy + 13
            if Window(id).x + Window(id).sx > scrX - 50 then Window(id).sx = scrX - 50 - Window(id).x
            if Window(id).y + Window(id).sy > scrY then Window(id).sy = scrY - Window(id).y
 
            `Create window image
            size sprite GUI, Window(id).sx, 8
            paste sprite GUI, Window(id).x, Window(id).y
            `Paste exit button
            paste image GUI + 1, Window(id).x + Window(id).sx - 7, Window(id).y + 1
            `Paste window
            ink Theme, 0
            box Window(id).x, Window(id).y + 8, Window(id).x + Window(id).sx, Window(id).y + Window(id).sy + 8
            ink ThemeBack, 0
            box Window(id).x + 1, Window(id).y + 8, Window(id).x + Window(id).sx - 1, Window(id).y + Window(id).sy + 7
 
            `Paste content
            imgx = Window(id).x + (Window(id).sx/2) - (imgsx/2)
            imgy = Window(id).y + 8 + (Window(id).sy/2) - (imgsy/2)
            paste image Window(id).Cont, imgx, imgy
 
            sync
         until mouseclick() = 0
         get image Window(id).img, Window(id).x, Window(id).y, Window(id).x + Window(id).sx, Window(id).y + Window(id).sy + 8, 1
      endif
 
      `Selecting another window
      if mousex() < x or mousex() > x + sx + 2 or mousey() < y or mousey() > y + sy + 2
         if mousex() < scrX - 50 and mousey() > 50
 
            `Reset ActiveWindow
            ActiveWindow = 0
 
            `Search for another window beneath mouse
            for i = 1 to array count(Window(0))
               if mousex() > Window(i).x and mousex() < Window(i).x + Window(i).sx
                  if mousey() > Window(i).y and mousey() < Window(i).y + Window(i).sy + 8
                     ActiveWindow = i
                  endif
               endif
            next i
            exitfunction
         endif
      endif
 
   else
 
      `Reset some values
      LastX = -1
      LastY = -1
 
   endif
 
   `Get color beneath mousepointer if mouseclick = 2
   if mouseclick() = 2
 
      `Check if it is on the image
      if mousex() > imgx and mousex() < imgx + imgsx
         if mousey() > imgy and mousey() < imgy + imgsy
 
            `Get position
            px = mousex() - imgx
            py = mousey() - imgy
 
            `Store color
            ForColor = memblock dword(img, 12 + (((py*imgsx) + px)*4))
            ForRed = rgbr(ForColor)
            ForGreen = rgbg(ForColor)
            ForBlue = rgbb(ForColor)
 
         endif
      endif
 
   endif
 
endfunction
 
function DrawOnImage(id)
 
   `Draw with the selected tool
   select CTool
      case 1
 
         `Detect position
         px = mousex() - imgx
         py = mousey() - imgy
 
         `Change memblock
         if px >=0 and py >=0 and px < imgsx and py < imgsy
            if LastX < 0 or LastY < 0
               pos = 12 + ( ((py*imgsx)*4) + (px*4) )
               write memblock dword img, pos, ForColor
            else
               WriteMemLine(LastX, LastY, px, py)
            endif
         endif
         LastX = px
         LastY = py
 
         `Change to image
         make image from memblock img, img
 
      endcase
      case 2
 
         `detect position
         px1 = mousex() - imgx
         py1 = mousey() - imgy
 
         `Get background
         get image Bin, 0, 0, scrX, scrY, 1
 
         repeat
 
            `Refresh screen
            paste image Bin, 0, 0
 
            `Get second coords
            px2 = mousex() - imgx
            py2 = mousey() - imgy
            if controlkey() > 0 then py2 = py1 + (2*(px2 - cx#))
 
            `Draw ellipse
            ink ForColor, 0
            cx# = (px1 + px2)*0.5
            cy# = (py1 + py2)*0.5
            FilledEllipse(imgx + cx#, imgy + cy#, px2 - cx#, py2 - cy#)
 
            `If zoom window is turned on, create a zoom right from this window
            if ZoomWind = 1
               CreateZoom(mousex(), mousey())
            else
               if sprite exist(ZoomImg) > 0 then delete sprite ZoomImg
               if sprite exist(ZoomImg + 1) > 0 then delete sprite ZoomImg + 1
            endif
 
            sync
         until mouseclick() = 0
 
         `draw ellipse
         if px2 < px1 then temp = px2 : px2 = px1 : px1 = temp
         if py2 < py1 then temp = py2 : py2 = py1 : py1 = temp
         for ix = px1 to px2
            for iy = py1 to py2
 
               `Detect if it is in the image
               if ix>0 and ix<=imgsx
                  if iy>0 and iy<=imgsy
 
                     `Calculate
                     dx = (ix - cx#)
                     dy = (iy - cy#) / (py2-cy#) * (px2-cx#)
                     set vector2 1, dx, dy
                     if length vector2(1) < (px2-cx#)
                        pos = 12 + ((((iy-1)*imgsx) + (ix-1))*4)
                        write memblock dword img, pos, ForColor
                     endif
                  endif
               endif
 
            next iy
         next ix
 
         make image from memblock img, img
 
      endcase
      case 3
 
         `first coords
         px1 = mousex() - imgx
         py1 = mousey() - imgy
 
         `Get background
         get image Bin, 0, 0, scrX, scrY, 1
 
         repeat
 
            `Refresh screen
            paste image Bin, 0, 0
 
            `Get second coords
            px2 = mousex() - imgx
            py2 = mousey() - imgy
 
            `Draw box
            ink ForColor, 0
            box imgx + px1, imgy + py1, mousex(), mousey()
 
            `If zoom window is turned on, create a zoom right from this window
            if ZoomWind = 1
               CreateZoom(mousex(), mousey())
            else
               if sprite exist(ZoomImg) > 0 then delete sprite ZoomImg
               if sprite exist(ZoomImg + 1) > 0 then delete sprite ZoomImg + 1
            endif
 
            sync
         until mouseclick() = 0
 
         `update image
         for iy = py1 to py2
            for ix = px1 to px2
 
               `boundries
               if ix>=0 and ix<imgsx
                  if iy>=0 and iy<imgsy
 
                     `calc position and update memblock
                     pos = 12 + (((iy*imgsx) + ix)*4)
                     write memblock dword img, pos, ForColor
 
                  endif
               endif
 
            next ix
         next iy
 
         `update image
         make image from memblock img, img
 
      endcase
 
      case 4
 
         `Floodfill
         px = mousex() - imgx
         py = mousey() - imgy
 
         `Call function from seed
         if px >= 0 and px < imgsx
            if py >= 0 and py < imgsy
 
               `Get the color that should be changed
               pos = 12 + (((py*imgsx) + px)*4)
               BaseColor = memblock dword(img, pos)
 
               `Fill
               if BaseColor <> ForColor
                  FloodFill(img, BaseColor, px, py)
               endif
            endif
         endif
 
         `update image
         make image from memblock img, img
 
      endcase
 
      case 5
 
         `Line tool
         px1 = mousex() - imgx
         py1 = mousey() - imgy
 
         `Get background
         get image Bin, 0, 0, scrX, scrY, 1
 
         repeat
 
            `Refresh screen
            paste image Bin, 0, 0
 
            `Get other data
            px2 = mousex() - imgx
            py2 = mousey() - imgy
 
            `Draw line
            ink ForColor, 0
            line px1 + imgx, py1 + imgy, mousex(),  mousey()
 
            `If zoom window is turned on, create a zoom right from this window
            if ZoomWind = 1
               CreateZoom(mousex(), mousey())
            else
               if sprite exist(ZoomImg) > 0 then delete sprite ZoomImg
               if sprite exist(ZoomImg + 1) > 0 then delete sprite ZoomImg + 1
            endif
 
            sync
         until mouseclick() = 0
 
         WriteMemLine(px1, py1, px2, py2)
         make image from memblock img, img
 
      endcase
 
      case 6
 
         `Spray tool
         px = mousex() - imgx
         py = mousey() - imgy
 
         `Draw tool
         for i = 1 to ToolDensity
 
            `Get random position
            ga = rnd(360) : gd = rnd(ToolSize)
            gx = px + (cos(ga) * gd)
            gy = py + (sin(ga) * gd)
 
            if gx >= 0 and gx < imgsx
               if gy >= 0 and gy < imgsy
 
                  `calc position and update memblock
                  pos = 12 + (((gy*imgsx) + gx)*4)
                  write memblock dword img, pos, ForColor
 
               endif
            endif
         next i
 
         `update the image
         make image from memblock img, img
 
      endcase
 
   endselect
 
endfunction
 
function SBCreateWindow(x, y, sx, sy, width, height)
 
   `Increase number of windows
   array insert at bottom Window(0), array count(Window(0))
   item = array count(Window(0))
   if array count(Window(0)) > maxWindows then exitfunction 0
 
   `Store data
   Window(item).x = x
   Window(item).y = y
   Window(item).sx = sx
   Window(item).sy = sy
   img = freeImg()
   Window(item).Cont = img
   Window(item).img = freeWind()
 
   `Create window image
   size sprite GUI, sx, 8
   paste sprite GUI, 0, 0
   `Paste exit button
   paste image GUI + 1, sx - 7, 1
   `Paste window
   ink Theme, 0
   box 0, 8, sx, sy + 8
   ink ThemeBack, 0
   box 1, 8, sx - 1, sy + 7
 
   `Get image
   get image Window(item).img, 0, 0, sx, sy + 8, 1
 
   `Create image for drawing
   make memblock img, 12 + (width*height*4)
   write memblock dword img, 0, width
   write memblock dword img, 4, height
   write memblock dword img, 8, 32
 
   `Create image from it
   make image from memblock img, img
 
endfunction item
 
function freeImg()
   for i = Canvas to Canvas + maxWindows
      if image exist(i) = 0 then exitfunction i
   next i
endfunction -1
 
function freeWind()
   for w = WindImg to WindImg + maxWindows
      if image exist(w) = 0 then exitfunction w
   next w
endfunction -1
 
function FloodFill(mem, Base, gx, gy)
 
      `Change this color
      pos = 12 + (((gy*imgsx) + gx)*4)
      write memblock dword mem, pos, ForColor
 
      `Check for surrounding
      if gx > 0
         if memblock dword(mem, pos - 4) = Base then FloodFill(mem, Base, gx - 1, gy)
      endif
      if gx < imgsx - 1
         if memblock dword(mem, pos + 4) = Base then FloodFill(mem, Base, gx + 1, gy)
      endif
      if gy > 0
         if memblock dword(mem, pos - (imgsx*4)) = Base then FloodFill(mem, Base, gx, gy - 1)
      endif
      if gy < imgsy - 1
         if memblock dword(mem, pos + (imgsx*4)) = Base then FloodFill(mem, Base, gx, gy + 1)
      endif
 
endfunction
 
function CreateZoom(mx, my)
 
   `Create zoom sprite
   if mx - 10 < 0 then mx = 10
   if my - 10 < 0 then my = 10
   if mx + 10 > scrX then mx = scrX - 10
   if my + 10 > scrY then my = scrY - 10
   get image ZoomImg, mx - 10, my - 10, mx + 10, my + 10, 1
   sprite ZoomImg, scrX - 50, 0, ZoomImg
   size sprite ZoomImg, 50, 50
   set sprite ZoomImg, 0, 0
   sprite ZoomImg + 1, scrX - 50, 0, ZoomImg + 1
 
endfunction
 
function WriteMemLine(x1, y1, x2, y2)
 
   `Draw line on memblock
   dx# = x2 - x1
   dy# = y2 - y1
   local i as integer
   i = 0
   if abs(dx#) > abs(dy#)
      repeat
 
         `Get position
         gx = x1 + i
         gy = y1 + int(dy#*i/dx#)
 
         `Change memblock
         if gx >=0 and gy >=0 and gx < imgsx and gy < imgsy
            pos = 12 + ( ((gy*imgsx)*4) + (gx*4) )
            write memblock dword img, pos, ForColor
         endif
 
         `Increase/decrease
         if dx# > 0 then inc i else : if dx# < 0 then dec i
 
      until abs(i) >= abs(dx#)
   else
      repeat
 
         `Get position
         gx = x1 + (dx#/dy#*i)
         gy = y1 + i
 
         `Change memblock
         if gx >=0 and gy >=0 and gx < imgsx and gy < imgsy
            pos = 12 + ( ((gy*imgsx)*4) + (gx*4) )
            write memblock dword img, pos, ForColor
         endif
 
         `Increase/decrease
         if dy# > 0 then inc i else dec i
 
      until abs(i) >= abs(dy#)
   endif
 
endfunction
 
function FilledEllipse(x, y, xrad#, yrad#)
   f# = yrad# / xrad#
   for i = 0 to int(xrad#)+1
      h = sqrt(xrad#*xrad# - (i*i)) * f#
      box x - i, y - h, x + i, y + h
   next i
endfunction
 
function LoadImage()
 
   `Set base directory
   set dir BaseDir
 
   `Perform checklist
   perform checklist for files
 
   `Create window
   size sprite GUI, 300, 8
   paste sprite GUI, 0, 0
   paste image GUI + 1, 293, 1
   ink Theme, 0
   box 0, 8, 300, scrY - 50
   ink ThemeBack, 0
   box 1, 8, 299, scrY - 51
   get image Bin, 0, 0, 300, scrY - 50
 
   `Reset
   ActiveWindow = 0
   scrolly = 0
   oldmousez = mousez()
 
   `Store file list
   StoreFileList(get dir$())
 
   `Loop
   do
 
      `Refresh
      paste image GUI + 2, 0, 0
 
      `Handle windows
      if array count(Window(0)) > 0
         for w = 1 to array count(Window(0))
            HandleWindow(w)
         next w
      endif
 
      `Paste load window
      paste image Bin, (scrX/2) - 150, 25
 
      `Display files
      OpenFile = 0
      for i = 1 to array count(File(0))
 
         ty = scrolly + (i-1)*20
         if ty >= 0 and ty < scrY - 45
            `Select
            if mousex() > (scrX/2) - 150 and mousex() < (scrX/2) + 150
               if mousey() > 33 + ty and mousey() < 53 + ty
                  ink Theme, 0
                  box (scrX/2) - 149, 33 + ty, (scrX/2) + 149, 53 + ty
 
                  `Clicking
                  if mouseclick() = 1 and hold = 0
                     hold = 1
                     OpenFile = i
                  endif
               endif
            endif
 
            if File(i) = 1 then ink ThemeTxt, 0
            if File(i) = 0 then ink rgb(0, 255, 0), 0
            text (scrX/2) - 145, 33 + ty, File$(i)
         endif
      next i
 
      `Open a file
      if OpenFile > 0
         if File(OpenFile) = 0
            FileName$ = get dir$() + "/" + FileName$(OpenFile)
            undim File(0)
            undim File$(0)
            undim FileName$(0)
            exitfunction FileName$
         endif
         if File(OpenFile) = 1
            if File(OpenFile) = 1 then cd FileName$(OpenFile)
            StoreFileList(get dir$())
            scrolly = 0
         endif
      endif
      OpenFile = 0
 
      `Use mouse to unlock and scroll
      if mouseclick() = 0 then hold = 0
      if mousez() <> oldmousez
         inc scrolly, (mousez() - oldmousez) / 6
         oldmousez = mousez()
      endif
      if scrolly > 0 then scrolly = 0
      if scrolly < -array count(File(0))*20 then scrolly = -array count(File(0))*20
 
      `Exit
      if mousex() > (scrX/2) + 143 and mousex() < (scrX/2) + 149
         if mousey() > 26 and mousey() < 32
            if mouseclick() = 1
               repeat : until mouseclick() = 0
               exitfunction ""
            endif
         endif
      endif
 
      sync
   loop
 
endfunction FileName$
 
function StoreFileList(d$)
 
   `Initialize
   undim File(0)
   undim File$(0)
   undim FileName$(0)
   dim File(0)
   dim File$(0)
   dim FileName$(0)
 
   `Set directory and perform checklist
   set dir d$
   perform checklist for files
 
   `Get back files
   find first
   find next
   array insert at bottom File(0)
   array insert at bottom File$(0)
   array insert at bottom FileName$(0)
   File(1) = get file type()
   File$(1) = "Parent"
   FileName$(1) = get file name$()
 
   `The rest of the files
   for f = 2 to checklist quantity() - 1
 
      `Find the next file in the row
      find next
 
      `If the file is a directory or a valid file
      if get file type() => 0
         pass = 0
         for i = 1 to array count(Import$(0))
            if right$(get file name$(), len(Import$(i))) = Import$(i) then pass = 1
         next i
         if pass > 0 or get file type() = 1
            `Insert array and store data
            array insert at bottom File(0)
            array insert at bottom File$(0)
            array insert at bottom FileName$(0)
            a = array count(File(0))
            File(a) = get file type()
            File$(a) = get file name$()
            FileName$(a) = get file name$()
         endif
      endif
 
   next f
 
endfunction
 
function IncDecValue(x, y, CValue)
 
   `- button
   paste image GUI + 16, x, y
 
   `Text value
   center text x + 22, y - 4, str$(CValue)
 
   `+ button
   paste image GUI + 15, x + 35, y
 
   `Handle increasing and decreasing
   if mousey() > y and mousey() < y + 8
      if mousex() > x and mousex() < x + 8
         if mouseclick() = 1
            repeat : until mouseclick() = 0
            GetVal = -1
         endif
      endif
      if mousex() > x + 35 and mousex() < x + 43
         if mouseclick() = 1
            repeat : until mouseclick() = 0
            GetVal = 1
         endif
      endif
   endif
   GetVal = GetVal + CValue
 
endfunction GetVal