본문 바로가기
Programming/Godot

Godot GUI프로그래밍 예제 - 시계만들기

by Wilkyway 2022. 2. 24.
반응형

이미지.zip
0.04MB


1. Display Size를 (400,500)으로 세팅합니다.
2. Borderless를 설정합니다.
3. Per Pixel Transparency를 Allowed, Enabled 로 바꿉니다.

4. 노드 구성
- clock (control)
ㄴ ColorRect (ColorRect)
ㄴ Body (sprite) : 시계 몸체 이미지
ㄴ pivot_h (Node2D)
ㄴ Hour (sprite) : 시침 이미지
ㄴ pivot_m (Node2D)
ㄴ Minute (sprite) : 분침 이미지
ㄴ pivot_s (Node2D)
ㄴ Second (sprite) : 초침 이미지
5. Body는 앱 중앙에 위치시키고, pivot들은 Body의 중앙에, pivot 내부의 sprite이미지들은 위와같이 정렬해줍니다. Godot 엔진에서 이미지를 특정 포인트를 기준으로 돌리기가 어렵습니다. 그래서 모두 가운데를 기준으로 돌리되, sprite이미지를 아예 적당한 거리에 떨어트려놓으면 가운데를 기준으로 돌아가는 효과를 만들 수 있습니다.

6. 스크립팅 (clock.gd)

extends Control var following = false 
var start_pos = Vector2() 

func _ready(): 
	get_tree().get_root().set_transparent_background(true) 
   
func _physics_process(delta): # ESC버튼으로 프로그램 종료 기능 
	if Input.is_action_pressed("ui_cancel"): 
    	get_tree().quit() # 시침/분침/초침의 회전 각도 계산 
    
    $Body/pivot_h.rotation = (OS.get_time()["hour"] + OS.get_time()["minute"] / 60.0) * PI/6 
    $Body/pivot_m.rotation = OS.get_time()["minute"] * PI/30 
    $Body/pivot_s.rotation = OS.get_time()["second"] * PI/30 
    
# 마우스 클릭&드래그로 시계 프로그램 위치 이동 
func _on_clock_gui_input(event): 
	if event is InputEventMouseButton: 
    	if event.get_button_index()==1: 
        	following = !following 
            start_pos = get_local_mouse_position() 
    if following: 
        OS.set_window_position(OS.window_position + get_local_mouse_position() - start_pos)


<결과>

반응형

댓글