#include "user32.as"

#pack "start.ax"	; 暗号化防止

#packopt hide 1
#packopt name "nizika-desktop"

#const NIZIKA_WIDTH	300
#const NIZIKA_HEIGHT	300
#const NIZIKA_BASE	236
#const NIZIKA_LEFT	100
#const NIZIKA_RIGHT	250


	; 半透明スクリーン作成
	SCREEN_MAIN = BgScr2 (gInfo_dispX, gInfo_dispY, SCREEN_HIDE, 0, 0)
	SetWindowLong hWnd, GWL_EXSTYLE, GetWindowLong (hWnd, GWL_EXSTYLE) Or WS_EX_LAYERED
	SetLayeredWindowAttributes hWnd, RGBCOLOUR_LIME, False, True

	HWND_DESKTOP = GetDesktopWindow ()	; デスクトップ取得

	Color COLOUR_LIME
	BoxF

	SCREEN_NIZIKA = Buffer2 ()
	PicLoad "assets/nizika.png"

	GSel SCREEN_MAIN, 2

	frame = 0

	nizikaX = Double (-NIZIKA_LEFT)
	nizikaY = Double (gInfo_dispY - NIZIKA_BASE)

	direction = 1

	moving = False
	falling = True
	base = gInfo_dispY

	Repeat
		Dim hWindows, 1
		Dim windowRects, 4, 1

		hWindows.0 = GetTopWindow (HWND_DESKTOP)
		hWindowBefore = hWindows.0
		GetWindowRect hWindows.0, VarPtr (windowRects.0.0)

		windowCnt = 1

		Repeat
			hWindow = GetWindow (hWindowBefore, GW_HWNDNEXT)

			If hWindow = 0 {
				Break
			}

			buf = ""
			GetWindowText hWindow, VarPtr (buf), 256

			If IsWindowVisible (hWindow) {
				If StrLen (buf) != 0 {
					If hWindow != hWnd {
						hWindows.windowCnt = hWindow
						GetWindowRect hWindow, VarPtr (windowRects.0.windowCnt)

						windowCnt++
					}
				}
			}

			hWindowBefore = hWindow
		Loop

		If GetKey2 (KEY_MOUSE_L) {
			If gInfo_act = SCREEN_MAIN {
				If moving {
					nizikaX = LimitF (mouseX - offsetX, -NIZIKA_LEFT, gInfo_dispX - NIZIKA_RIGHT)
					nizikaY = LimitF (mouseY - offsetY, Null, gInfo_dispY - NIZIKA_BASE)
				}
				Else {
					moving = True

					offsetX = Double (mouseX - nizikaX)
					offsetY = Double (mouseY - nizikaY)
				}
			}
		}
		Else: If moving {
			moving = False
			falling = True

			fallVel = 0.
		}
		Else: If falling {
			nizikaY = LimitF (nizikaY + fallVel, Null, base - NIZIKA_BASE)
			fallVel += 9.8

			If nizikaY >= base - NIZIKA_BASE {
				fallVel = 0.
			}

			base = gInfo_dispY

			Repeat windowCnt
				If windowRects.LEFT.cnt > nizikaX + (NIZIKA_WIDTH - NIZIKA_LEFT) {
					Continue
				}

				If nizikaX + NIZIKA_LEFT >= windowRects.RIGHT.cnt {
					Continue
				}

				If nizikaY + NIZIKA_BASE > windowRects.TOP.cnt {
					Continue
				}

				If windowRects.TOP.cnt >= base {
					Continue
				}

				base = windowRects.TOP.cnt
			Loop
		}

		ReDraw False
			Color COLOUR_LIME
			BoxF

			Pos nizikaX, nizikaY
			GCopy SCREEN_NIZIKA, IIf (direction < 0, NIZIKA_WIDTH, 0), NIZIKA_HEIGHT * (frame * IIf (moving, 3, 1) \ 24), NIZIKA_WIDTH, NIZIKA_HEIGHT
		ReDraw True

		nizikaX += direction * 4

		If (nizikaX >= gInfo_dispX - NIZIKA_RIGHT) || (nizikaX < -NIZIKA_LEFT) {
			direction = -direction
		}

		Await 33

		frame++
	Loop

